ptrace.c 18 KB

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