ptrace.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * linux/kernel/ptrace.c
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. *
  6. * Common interfaces for "ptrace()" which we do not want
  7. * to continually duplicate across every architecture.
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/mm.h>
  14. #include <linux/highmem.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/security.h>
  19. #include <linux/signal.h>
  20. #include <linux/audit.h>
  21. #include <linux/pid_namespace.h>
  22. #include <linux/syscalls.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/uaccess.h>
  25. /*
  26. * ptrace a task: make the debugger its new parent and
  27. * move it to the ptrace list.
  28. *
  29. * Must be called with the tasklist lock write-held.
  30. */
  31. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  32. {
  33. BUG_ON(!list_empty(&child->ptrace_entry));
  34. list_add(&child->ptrace_entry, &new_parent->ptraced);
  35. child->parent = new_parent;
  36. }
  37. /*
  38. * Turn a tracing stop into a normal stop now, since with no tracer there
  39. * would be no way to wake it up with SIGCONT or SIGKILL. If there was a
  40. * signal sent that would resume the child, but didn't because it was in
  41. * TASK_TRACED, resume it now.
  42. * Requires that irqs be disabled.
  43. */
  44. static void ptrace_untrace(struct task_struct *child)
  45. {
  46. spin_lock(&child->sighand->siglock);
  47. if (task_is_traced(child)) {
  48. /*
  49. * If the group stop is completed or in progress,
  50. * this thread was already counted as stopped.
  51. */
  52. if (child->signal->flags & SIGNAL_STOP_STOPPED ||
  53. child->signal->group_stop_count)
  54. __set_task_state(child, TASK_STOPPED);
  55. else
  56. signal_wake_up(child, 1);
  57. }
  58. spin_unlock(&child->sighand->siglock);
  59. }
  60. /*
  61. * unptrace a task: move it back to its original parent and
  62. * remove it from the ptrace list.
  63. *
  64. * Must be called with the tasklist lock write-held.
  65. */
  66. void __ptrace_unlink(struct task_struct *child)
  67. {
  68. BUG_ON(!child->ptrace);
  69. child->ptrace = 0;
  70. child->parent = child->real_parent;
  71. list_del_init(&child->ptrace_entry);
  72. arch_ptrace_untrace(child);
  73. if (task_is_traced(child))
  74. ptrace_untrace(child);
  75. }
  76. /*
  77. * Check that we have indeed attached to the thing..
  78. */
  79. int ptrace_check_attach(struct task_struct *child, int kill)
  80. {
  81. int ret = -ESRCH;
  82. /*
  83. * We take the read lock around doing both checks to close a
  84. * possible race where someone else was tracing our child and
  85. * detached between these two checks. After this locked check,
  86. * we are sure that this is our traced child and that can only
  87. * be changed by us so it's not changing right after this.
  88. */
  89. read_lock(&tasklist_lock);
  90. if ((child->ptrace & PT_PTRACED) && child->parent == current) {
  91. ret = 0;
  92. /*
  93. * child->sighand can't be NULL, release_task()
  94. * does ptrace_unlink() before __exit_signal().
  95. */
  96. spin_lock_irq(&child->sighand->siglock);
  97. if (task_is_stopped(child))
  98. child->state = TASK_TRACED;
  99. else if (!task_is_traced(child) && !kill)
  100. ret = -ESRCH;
  101. spin_unlock_irq(&child->sighand->siglock);
  102. }
  103. read_unlock(&tasklist_lock);
  104. if (!ret && !kill)
  105. ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
  106. /* All systems go.. */
  107. return ret;
  108. }
  109. int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  110. {
  111. const struct cred *cred = current_cred(), *tcred;
  112. /* May we inspect the given task?
  113. * This check is used both for attaching with ptrace
  114. * and for allowing access to sensitive information in /proc.
  115. *
  116. * ptrace_attach denies several cases that /proc allows
  117. * because setting up the necessary parent/child relationship
  118. * or halting the specified task is impossible.
  119. */
  120. int dumpable = 0;
  121. /* Don't let security modules deny introspection */
  122. if (task == current)
  123. return 0;
  124. rcu_read_lock();
  125. tcred = __task_cred(task);
  126. if ((cred->uid != tcred->euid ||
  127. cred->uid != tcred->suid ||
  128. cred->uid != tcred->uid ||
  129. cred->gid != tcred->egid ||
  130. cred->gid != tcred->sgid ||
  131. cred->gid != tcred->gid) &&
  132. !capable(CAP_SYS_PTRACE)) {
  133. rcu_read_unlock();
  134. return -EPERM;
  135. }
  136. rcu_read_unlock();
  137. smp_rmb();
  138. if (task->mm)
  139. dumpable = get_dumpable(task->mm);
  140. if (!dumpable && !capable(CAP_SYS_PTRACE))
  141. return -EPERM;
  142. return security_ptrace_may_access(task, mode);
  143. }
  144. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  145. {
  146. int err;
  147. task_lock(task);
  148. err = __ptrace_may_access(task, mode);
  149. task_unlock(task);
  150. return (!err ? true : false);
  151. }
  152. int ptrace_attach(struct task_struct *task)
  153. {
  154. int retval;
  155. unsigned long flags;
  156. audit_ptrace(task);
  157. retval = -EPERM;
  158. if (same_thread_group(task, current))
  159. goto out;
  160. /* Protect exec's credential calculations against our interference;
  161. * SUID, SGID and LSM creds get determined differently under ptrace.
  162. */
  163. retval = mutex_lock_interruptible(&current->cred_exec_mutex);
  164. if (retval < 0)
  165. goto out;
  166. retval = -EPERM;
  167. repeat:
  168. /*
  169. * Nasty, nasty.
  170. *
  171. * We want to hold both the task-lock and the
  172. * tasklist_lock for writing at the same time.
  173. * But that's against the rules (tasklist_lock
  174. * is taken for reading by interrupts on other
  175. * cpu's that may have task_lock).
  176. */
  177. task_lock(task);
  178. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  179. task_unlock(task);
  180. do {
  181. cpu_relax();
  182. } while (!write_can_lock(&tasklist_lock));
  183. goto repeat;
  184. }
  185. if (!task->mm)
  186. goto bad;
  187. /* the same process cannot be attached many times */
  188. if (task->ptrace & PT_PTRACED)
  189. goto bad;
  190. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
  191. if (retval)
  192. goto bad;
  193. /* Go */
  194. task->ptrace |= PT_PTRACED;
  195. if (capable(CAP_SYS_PTRACE))
  196. task->ptrace |= PT_PTRACE_CAP;
  197. __ptrace_link(task, current);
  198. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  199. bad:
  200. write_unlock_irqrestore(&tasklist_lock, flags);
  201. task_unlock(task);
  202. mutex_unlock(&current->cred_exec_mutex);
  203. out:
  204. return retval;
  205. }
  206. /*
  207. * Called with irqs disabled, returns true if childs should reap themselves.
  208. */
  209. static int ignoring_children(struct sighand_struct *sigh)
  210. {
  211. int ret;
  212. spin_lock(&sigh->siglock);
  213. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  214. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  215. spin_unlock(&sigh->siglock);
  216. return ret;
  217. }
  218. /*
  219. * Called with tasklist_lock held for writing.
  220. * Unlink a traced task, and clean it up if it was a traced zombie.
  221. * Return true if it needs to be reaped with release_task().
  222. * (We can't call release_task() here because we already hold tasklist_lock.)
  223. *
  224. * If it's a zombie, our attachedness prevented normal parent notification
  225. * or self-reaping. Do notification now if it would have happened earlier.
  226. * If it should reap itself, return true.
  227. *
  228. * If it's our own child, there is no notification to do.
  229. * But if our normal children self-reap, then this child
  230. * was prevented by ptrace and we must reap it now.
  231. */
  232. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  233. {
  234. __ptrace_unlink(p);
  235. if (p->exit_state == EXIT_ZOMBIE) {
  236. if (!task_detached(p) && thread_group_empty(p)) {
  237. if (!same_thread_group(p->real_parent, tracer))
  238. do_notify_parent(p, p->exit_signal);
  239. else if (ignoring_children(tracer->sighand))
  240. p->exit_signal = -1;
  241. }
  242. if (task_detached(p)) {
  243. /* Mark it as in the process of being reaped. */
  244. p->exit_state = EXIT_DEAD;
  245. return true;
  246. }
  247. }
  248. return false;
  249. }
  250. int ptrace_detach(struct task_struct *child, unsigned int data)
  251. {
  252. bool dead = false;
  253. if (!valid_signal(data))
  254. return -EIO;
  255. /* Architecture-specific hardware disable .. */
  256. ptrace_disable(child);
  257. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  258. write_lock_irq(&tasklist_lock);
  259. /*
  260. * This child can be already killed. Make sure de_thread() or
  261. * our sub-thread doing do_wait() didn't do release_task() yet.
  262. */
  263. if (child->ptrace) {
  264. child->exit_code = data;
  265. dead = __ptrace_detach(current, child);
  266. }
  267. write_unlock_irq(&tasklist_lock);
  268. if (unlikely(dead))
  269. release_task(child);
  270. return 0;
  271. }
  272. /*
  273. * Detach all tasks we were using ptrace on.
  274. */
  275. void exit_ptrace(struct task_struct *tracer)
  276. {
  277. struct task_struct *p, *n;
  278. LIST_HEAD(ptrace_dead);
  279. write_lock_irq(&tasklist_lock);
  280. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  281. if (__ptrace_detach(tracer, p))
  282. list_add(&p->ptrace_entry, &ptrace_dead);
  283. }
  284. write_unlock_irq(&tasklist_lock);
  285. BUG_ON(!list_empty(&tracer->ptraced));
  286. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  287. list_del_init(&p->ptrace_entry);
  288. release_task(p);
  289. }
  290. }
  291. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  292. {
  293. int copied = 0;
  294. while (len > 0) {
  295. char buf[128];
  296. int this_len, retval;
  297. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  298. retval = access_process_vm(tsk, src, buf, this_len, 0);
  299. if (!retval) {
  300. if (copied)
  301. break;
  302. return -EIO;
  303. }
  304. if (copy_to_user(dst, buf, retval))
  305. return -EFAULT;
  306. copied += retval;
  307. src += retval;
  308. dst += retval;
  309. len -= retval;
  310. }
  311. return copied;
  312. }
  313. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  314. {
  315. int copied = 0;
  316. while (len > 0) {
  317. char buf[128];
  318. int this_len, retval;
  319. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  320. if (copy_from_user(buf, src, this_len))
  321. return -EFAULT;
  322. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  323. if (!retval) {
  324. if (copied)
  325. break;
  326. return -EIO;
  327. }
  328. copied += retval;
  329. src += retval;
  330. dst += retval;
  331. len -= retval;
  332. }
  333. return copied;
  334. }
  335. static int ptrace_setoptions(struct task_struct *child, long data)
  336. {
  337. child->ptrace &= ~PT_TRACE_MASK;
  338. if (data & PTRACE_O_TRACESYSGOOD)
  339. child->ptrace |= PT_TRACESYSGOOD;
  340. if (data & PTRACE_O_TRACEFORK)
  341. child->ptrace |= PT_TRACE_FORK;
  342. if (data & PTRACE_O_TRACEVFORK)
  343. child->ptrace |= PT_TRACE_VFORK;
  344. if (data & PTRACE_O_TRACECLONE)
  345. child->ptrace |= PT_TRACE_CLONE;
  346. if (data & PTRACE_O_TRACEEXEC)
  347. child->ptrace |= PT_TRACE_EXEC;
  348. if (data & PTRACE_O_TRACEVFORKDONE)
  349. child->ptrace |= PT_TRACE_VFORK_DONE;
  350. if (data & PTRACE_O_TRACEEXIT)
  351. child->ptrace |= PT_TRACE_EXIT;
  352. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  353. }
  354. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  355. {
  356. int error = -ESRCH;
  357. read_lock(&tasklist_lock);
  358. if (likely(child->sighand != NULL)) {
  359. error = -EINVAL;
  360. spin_lock_irq(&child->sighand->siglock);
  361. if (likely(child->last_siginfo != NULL)) {
  362. *info = *child->last_siginfo;
  363. error = 0;
  364. }
  365. spin_unlock_irq(&child->sighand->siglock);
  366. }
  367. read_unlock(&tasklist_lock);
  368. return error;
  369. }
  370. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  371. {
  372. int error = -ESRCH;
  373. read_lock(&tasklist_lock);
  374. if (likely(child->sighand != NULL)) {
  375. error = -EINVAL;
  376. spin_lock_irq(&child->sighand->siglock);
  377. if (likely(child->last_siginfo != NULL)) {
  378. *child->last_siginfo = *info;
  379. error = 0;
  380. }
  381. spin_unlock_irq(&child->sighand->siglock);
  382. }
  383. read_unlock(&tasklist_lock);
  384. return error;
  385. }
  386. #ifdef PTRACE_SINGLESTEP
  387. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  388. #else
  389. #define is_singlestep(request) 0
  390. #endif
  391. #ifdef PTRACE_SINGLEBLOCK
  392. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  393. #else
  394. #define is_singleblock(request) 0
  395. #endif
  396. #ifdef PTRACE_SYSEMU
  397. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  398. #else
  399. #define is_sysemu_singlestep(request) 0
  400. #endif
  401. static int ptrace_resume(struct task_struct *child, long request, long data)
  402. {
  403. if (!valid_signal(data))
  404. return -EIO;
  405. if (request == PTRACE_SYSCALL)
  406. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  407. else
  408. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  409. #ifdef TIF_SYSCALL_EMU
  410. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  411. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  412. else
  413. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  414. #endif
  415. if (is_singleblock(request)) {
  416. if (unlikely(!arch_has_block_step()))
  417. return -EIO;
  418. user_enable_block_step(child);
  419. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  420. if (unlikely(!arch_has_single_step()))
  421. return -EIO;
  422. user_enable_single_step(child);
  423. }
  424. else
  425. user_disable_single_step(child);
  426. child->exit_code = data;
  427. wake_up_process(child);
  428. return 0;
  429. }
  430. int ptrace_request(struct task_struct *child, long request,
  431. long addr, long data)
  432. {
  433. int ret = -EIO;
  434. siginfo_t siginfo;
  435. switch (request) {
  436. case PTRACE_PEEKTEXT:
  437. case PTRACE_PEEKDATA:
  438. return generic_ptrace_peekdata(child, addr, data);
  439. case PTRACE_POKETEXT:
  440. case PTRACE_POKEDATA:
  441. return generic_ptrace_pokedata(child, addr, data);
  442. #ifdef PTRACE_OLDSETOPTIONS
  443. case PTRACE_OLDSETOPTIONS:
  444. #endif
  445. case PTRACE_SETOPTIONS:
  446. ret = ptrace_setoptions(child, data);
  447. break;
  448. case PTRACE_GETEVENTMSG:
  449. ret = put_user(child->ptrace_message, (unsigned long __user *) data);
  450. break;
  451. case PTRACE_GETSIGINFO:
  452. ret = ptrace_getsiginfo(child, &siginfo);
  453. if (!ret)
  454. ret = copy_siginfo_to_user((siginfo_t __user *) data,
  455. &siginfo);
  456. break;
  457. case PTRACE_SETSIGINFO:
  458. if (copy_from_user(&siginfo, (siginfo_t __user *) data,
  459. sizeof siginfo))
  460. ret = -EFAULT;
  461. else
  462. ret = ptrace_setsiginfo(child, &siginfo);
  463. break;
  464. case PTRACE_DETACH: /* detach a process that was attached. */
  465. ret = ptrace_detach(child, data);
  466. break;
  467. #ifdef PTRACE_SINGLESTEP
  468. case PTRACE_SINGLESTEP:
  469. #endif
  470. #ifdef PTRACE_SINGLEBLOCK
  471. case PTRACE_SINGLEBLOCK:
  472. #endif
  473. #ifdef PTRACE_SYSEMU
  474. case PTRACE_SYSEMU:
  475. case PTRACE_SYSEMU_SINGLESTEP:
  476. #endif
  477. case PTRACE_SYSCALL:
  478. case PTRACE_CONT:
  479. return ptrace_resume(child, request, data);
  480. case PTRACE_KILL:
  481. if (child->exit_state) /* already dead */
  482. return 0;
  483. return ptrace_resume(child, request, SIGKILL);
  484. default:
  485. break;
  486. }
  487. return ret;
  488. }
  489. /**
  490. * ptrace_traceme -- helper for PTRACE_TRACEME
  491. *
  492. * Performs checks and sets PT_PTRACED.
  493. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  494. */
  495. int ptrace_traceme(void)
  496. {
  497. int ret = -EPERM;
  498. /*
  499. * Are we already being traced?
  500. */
  501. repeat:
  502. task_lock(current);
  503. if (!(current->ptrace & PT_PTRACED)) {
  504. /*
  505. * See ptrace_attach() comments about the locking here.
  506. */
  507. unsigned long flags;
  508. if (!write_trylock_irqsave(&tasklist_lock, flags)) {
  509. task_unlock(current);
  510. do {
  511. cpu_relax();
  512. } while (!write_can_lock(&tasklist_lock));
  513. goto repeat;
  514. }
  515. ret = security_ptrace_traceme(current->parent);
  516. /*
  517. * Set the ptrace bit in the process ptrace flags.
  518. * Then link us on our parent's ptraced list.
  519. */
  520. if (!ret) {
  521. current->ptrace |= PT_PTRACED;
  522. __ptrace_link(current, current->real_parent);
  523. }
  524. write_unlock_irqrestore(&tasklist_lock, flags);
  525. }
  526. task_unlock(current);
  527. return ret;
  528. }
  529. /**
  530. * ptrace_get_task_struct -- grab a task struct reference for ptrace
  531. * @pid: process id to grab a task_struct reference of
  532. *
  533. * This function is a helper for ptrace implementations. It checks
  534. * permissions and then grabs a task struct for use of the actual
  535. * ptrace implementation.
  536. *
  537. * Returns the task_struct for @pid or an ERR_PTR() on failure.
  538. */
  539. struct task_struct *ptrace_get_task_struct(pid_t pid)
  540. {
  541. struct task_struct *child;
  542. read_lock(&tasklist_lock);
  543. child = find_task_by_vpid(pid);
  544. if (child)
  545. get_task_struct(child);
  546. read_unlock(&tasklist_lock);
  547. if (!child)
  548. return ERR_PTR(-ESRCH);
  549. return child;
  550. }
  551. #ifndef arch_ptrace_attach
  552. #define arch_ptrace_attach(child) do { } while (0)
  553. #endif
  554. SYSCALL_DEFINE4(ptrace, long, request, long, pid, long, addr, long, data)
  555. {
  556. struct task_struct *child;
  557. long ret;
  558. /*
  559. * This lock_kernel fixes a subtle race with suid exec
  560. */
  561. lock_kernel();
  562. if (request == PTRACE_TRACEME) {
  563. ret = ptrace_traceme();
  564. if (!ret)
  565. arch_ptrace_attach(current);
  566. goto out;
  567. }
  568. child = ptrace_get_task_struct(pid);
  569. if (IS_ERR(child)) {
  570. ret = PTR_ERR(child);
  571. goto out;
  572. }
  573. if (request == PTRACE_ATTACH) {
  574. ret = ptrace_attach(child);
  575. /*
  576. * Some architectures need to do book-keeping after
  577. * a ptrace attach.
  578. */
  579. if (!ret)
  580. arch_ptrace_attach(child);
  581. goto out_put_task_struct;
  582. }
  583. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  584. if (ret < 0)
  585. goto out_put_task_struct;
  586. ret = arch_ptrace(child, request, addr, data);
  587. out_put_task_struct:
  588. put_task_struct(child);
  589. out:
  590. unlock_kernel();
  591. return ret;
  592. }
  593. int generic_ptrace_peekdata(struct task_struct *tsk, long addr, long data)
  594. {
  595. unsigned long tmp;
  596. int copied;
  597. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  598. if (copied != sizeof(tmp))
  599. return -EIO;
  600. return put_user(tmp, (unsigned long __user *)data);
  601. }
  602. int generic_ptrace_pokedata(struct task_struct *tsk, long addr, long data)
  603. {
  604. int copied;
  605. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  606. return (copied == sizeof(data)) ? 0 : -EIO;
  607. }
  608. #if defined CONFIG_COMPAT
  609. #include <linux/compat.h>
  610. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  611. compat_ulong_t addr, compat_ulong_t data)
  612. {
  613. compat_ulong_t __user *datap = compat_ptr(data);
  614. compat_ulong_t word;
  615. siginfo_t siginfo;
  616. int ret;
  617. switch (request) {
  618. case PTRACE_PEEKTEXT:
  619. case PTRACE_PEEKDATA:
  620. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  621. if (ret != sizeof(word))
  622. ret = -EIO;
  623. else
  624. ret = put_user(word, datap);
  625. break;
  626. case PTRACE_POKETEXT:
  627. case PTRACE_POKEDATA:
  628. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  629. ret = (ret != sizeof(data) ? -EIO : 0);
  630. break;
  631. case PTRACE_GETEVENTMSG:
  632. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  633. break;
  634. case PTRACE_GETSIGINFO:
  635. ret = ptrace_getsiginfo(child, &siginfo);
  636. if (!ret)
  637. ret = copy_siginfo_to_user32(
  638. (struct compat_siginfo __user *) datap,
  639. &siginfo);
  640. break;
  641. case PTRACE_SETSIGINFO:
  642. memset(&siginfo, 0, sizeof siginfo);
  643. if (copy_siginfo_from_user32(
  644. &siginfo, (struct compat_siginfo __user *) datap))
  645. ret = -EFAULT;
  646. else
  647. ret = ptrace_setsiginfo(child, &siginfo);
  648. break;
  649. default:
  650. ret = ptrace_request(child, request, addr, data);
  651. }
  652. return ret;
  653. }
  654. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  655. compat_long_t addr, compat_long_t data)
  656. {
  657. struct task_struct *child;
  658. long ret;
  659. /*
  660. * This lock_kernel fixes a subtle race with suid exec
  661. */
  662. lock_kernel();
  663. if (request == PTRACE_TRACEME) {
  664. ret = ptrace_traceme();
  665. goto out;
  666. }
  667. child = ptrace_get_task_struct(pid);
  668. if (IS_ERR(child)) {
  669. ret = PTR_ERR(child);
  670. goto out;
  671. }
  672. if (request == PTRACE_ATTACH) {
  673. ret = ptrace_attach(child);
  674. /*
  675. * Some architectures need to do book-keeping after
  676. * a ptrace attach.
  677. */
  678. if (!ret)
  679. arch_ptrace_attach(child);
  680. goto out_put_task_struct;
  681. }
  682. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  683. if (!ret)
  684. ret = compat_arch_ptrace(child, request, addr, data);
  685. out_put_task_struct:
  686. put_task_struct(child);
  687. out:
  688. unlock_kernel();
  689. return ret;
  690. }
  691. #endif /* CONFIG_COMPAT */