ptrace.c 17 KB

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