ptrace.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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/export.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/sched/coredump.h>
  14. #include <linux/sched/task.h>
  15. #include <linux/errno.h>
  16. #include <linux/mm.h>
  17. #include <linux/highmem.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/security.h>
  21. #include <linux/signal.h>
  22. #include <linux/uio.h>
  23. #include <linux/audit.h>
  24. #include <linux/pid_namespace.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/regset.h>
  28. #include <linux/hw_breakpoint.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/compat.h>
  31. #include <linux/sched/signal.h>
  32. /*
  33. * Access another process' address space via ptrace.
  34. * Source/target buffer must be kernel space,
  35. * Do not walk the page table directly, use get_user_pages
  36. */
  37. int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
  38. void *buf, int len, unsigned int gup_flags)
  39. {
  40. struct mm_struct *mm;
  41. int ret;
  42. mm = get_task_mm(tsk);
  43. if (!mm)
  44. return 0;
  45. if (!tsk->ptrace ||
  46. (current != tsk->parent) ||
  47. ((get_dumpable(mm) != SUID_DUMP_USER) &&
  48. !ptracer_capable(tsk, mm->user_ns))) {
  49. mmput(mm);
  50. return 0;
  51. }
  52. ret = __access_remote_vm(tsk, mm, addr, buf, len, gup_flags);
  53. mmput(mm);
  54. return ret;
  55. }
  56. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent,
  57. const struct cred *ptracer_cred)
  58. {
  59. BUG_ON(!list_empty(&child->ptrace_entry));
  60. list_add(&child->ptrace_entry, &new_parent->ptraced);
  61. child->parent = new_parent;
  62. child->ptracer_cred = get_cred(ptracer_cred);
  63. }
  64. /*
  65. * ptrace a task: make the debugger its new parent and
  66. * move it to the ptrace list.
  67. *
  68. * Must be called with the tasklist lock write-held.
  69. */
  70. static void ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  71. {
  72. __ptrace_link(child, new_parent, current_cred());
  73. }
  74. /**
  75. * __ptrace_unlink - unlink ptracee and restore its execution state
  76. * @child: ptracee to be unlinked
  77. *
  78. * Remove @child from the ptrace list, move it back to the original parent,
  79. * and restore the execution state so that it conforms to the group stop
  80. * state.
  81. *
  82. * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
  83. * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
  84. * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
  85. * If the ptracer is exiting, the ptracee can be in any state.
  86. *
  87. * After detach, the ptracee should be in a state which conforms to the
  88. * group stop. If the group is stopped or in the process of stopping, the
  89. * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
  90. * up from TASK_TRACED.
  91. *
  92. * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
  93. * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
  94. * to but in the opposite direction of what happens while attaching to a
  95. * stopped task. However, in this direction, the intermediate RUNNING
  96. * state is not hidden even from the current ptracer and if it immediately
  97. * re-attaches and performs a WNOHANG wait(2), it may fail.
  98. *
  99. * CONTEXT:
  100. * write_lock_irq(tasklist_lock)
  101. */
  102. void __ptrace_unlink(struct task_struct *child)
  103. {
  104. const struct cred *old_cred;
  105. BUG_ON(!child->ptrace);
  106. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  107. child->parent = child->real_parent;
  108. list_del_init(&child->ptrace_entry);
  109. old_cred = child->ptracer_cred;
  110. child->ptracer_cred = NULL;
  111. put_cred(old_cred);
  112. spin_lock(&child->sighand->siglock);
  113. child->ptrace = 0;
  114. /*
  115. * Clear all pending traps and TRAPPING. TRAPPING should be
  116. * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly.
  117. */
  118. task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
  119. task_clear_jobctl_trapping(child);
  120. /*
  121. * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
  122. * @child isn't dead.
  123. */
  124. if (!(child->flags & PF_EXITING) &&
  125. (child->signal->flags & SIGNAL_STOP_STOPPED ||
  126. child->signal->group_stop_count)) {
  127. child->jobctl |= JOBCTL_STOP_PENDING;
  128. /*
  129. * This is only possible if this thread was cloned by the
  130. * traced task running in the stopped group, set the signal
  131. * for the future reports.
  132. * FIXME: we should change ptrace_init_task() to handle this
  133. * case.
  134. */
  135. if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
  136. child->jobctl |= SIGSTOP;
  137. }
  138. /*
  139. * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
  140. * @child in the butt. Note that @resume should be used iff @child
  141. * is in TASK_TRACED; otherwise, we might unduly disrupt
  142. * TASK_KILLABLE sleeps.
  143. */
  144. if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
  145. ptrace_signal_wake_up(child, true);
  146. spin_unlock(&child->sighand->siglock);
  147. }
  148. static bool looks_like_a_spurious_pid(struct task_struct *task)
  149. {
  150. if (task->exit_code != ((PTRACE_EVENT_EXEC << 8) | SIGTRAP))
  151. return false;
  152. if (task_pid_vnr(task) == task->ptrace_message)
  153. return false;
  154. /*
  155. * The tracee changed its pid but the PTRACE_EVENT_EXEC event
  156. * was not wait()'ed, most probably debugger targets the old
  157. * leader which was destroyed in de_thread().
  158. */
  159. return true;
  160. }
  161. /* Ensure that nothing can wake it up, even SIGKILL */
  162. static bool ptrace_freeze_traced(struct task_struct *task)
  163. {
  164. bool ret = false;
  165. /* Lockless, nobody but us can set this flag */
  166. if (task->jobctl & JOBCTL_LISTENING)
  167. return ret;
  168. spin_lock_irq(&task->sighand->siglock);
  169. if (task_is_traced(task) && !looks_like_a_spurious_pid(task) &&
  170. !__fatal_signal_pending(task)) {
  171. task->state = __TASK_TRACED;
  172. ret = true;
  173. }
  174. spin_unlock_irq(&task->sighand->siglock);
  175. return ret;
  176. }
  177. static void ptrace_unfreeze_traced(struct task_struct *task)
  178. {
  179. if (task->state != __TASK_TRACED)
  180. return;
  181. WARN_ON(!task->ptrace || task->parent != current);
  182. /*
  183. * PTRACE_LISTEN can allow ptrace_trap_notify to wake us up remotely.
  184. * Recheck state under the lock to close this race.
  185. */
  186. spin_lock_irq(&task->sighand->siglock);
  187. if (task->state == __TASK_TRACED) {
  188. if (__fatal_signal_pending(task))
  189. wake_up_state(task, __TASK_TRACED);
  190. else
  191. task->state = TASK_TRACED;
  192. }
  193. spin_unlock_irq(&task->sighand->siglock);
  194. }
  195. /**
  196. * ptrace_check_attach - check whether ptracee is ready for ptrace operation
  197. * @child: ptracee to check for
  198. * @ignore_state: don't check whether @child is currently %TASK_TRACED
  199. *
  200. * Check whether @child is being ptraced by %current and ready for further
  201. * ptrace operations. If @ignore_state is %false, @child also should be in
  202. * %TASK_TRACED state and on return the child is guaranteed to be traced
  203. * and not executing. If @ignore_state is %true, @child can be in any
  204. * state.
  205. *
  206. * CONTEXT:
  207. * Grabs and releases tasklist_lock and @child->sighand->siglock.
  208. *
  209. * RETURNS:
  210. * 0 on success, -ESRCH if %child is not ready.
  211. */
  212. static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
  213. {
  214. int ret = -ESRCH;
  215. /*
  216. * We take the read lock around doing both checks to close a
  217. * possible race where someone else was tracing our child and
  218. * detached between these two checks. After this locked check,
  219. * we are sure that this is our traced child and that can only
  220. * be changed by us so it's not changing right after this.
  221. */
  222. read_lock(&tasklist_lock);
  223. if (child->ptrace && child->parent == current) {
  224. WARN_ON(child->state == __TASK_TRACED);
  225. /*
  226. * child->sighand can't be NULL, release_task()
  227. * does ptrace_unlink() before __exit_signal().
  228. */
  229. if (ignore_state || ptrace_freeze_traced(child))
  230. ret = 0;
  231. }
  232. read_unlock(&tasklist_lock);
  233. if (!ret && !ignore_state) {
  234. if (!wait_task_inactive(child, __TASK_TRACED)) {
  235. /*
  236. * This can only happen if may_ptrace_stop() fails and
  237. * ptrace_stop() changes ->state back to TASK_RUNNING,
  238. * so we should not worry about leaking __TASK_TRACED.
  239. */
  240. WARN_ON(child->state == __TASK_TRACED);
  241. ret = -ESRCH;
  242. }
  243. }
  244. return ret;
  245. }
  246. static bool ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
  247. {
  248. if (mode & PTRACE_MODE_NOAUDIT)
  249. return ns_capable_noaudit(ns, CAP_SYS_PTRACE);
  250. return ns_capable(ns, CAP_SYS_PTRACE);
  251. }
  252. /* Returns 0 on success, -errno on denial. */
  253. static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  254. {
  255. const struct cred *cred = current_cred(), *tcred;
  256. struct mm_struct *mm;
  257. kuid_t caller_uid;
  258. kgid_t caller_gid;
  259. if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
  260. WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
  261. return -EPERM;
  262. }
  263. /* May we inspect the given task?
  264. * This check is used both for attaching with ptrace
  265. * and for allowing access to sensitive information in /proc.
  266. *
  267. * ptrace_attach denies several cases that /proc allows
  268. * because setting up the necessary parent/child relationship
  269. * or halting the specified task is impossible.
  270. */
  271. /* Don't let security modules deny introspection */
  272. if (same_thread_group(task, current))
  273. return 0;
  274. rcu_read_lock();
  275. if (mode & PTRACE_MODE_FSCREDS) {
  276. caller_uid = cred->fsuid;
  277. caller_gid = cred->fsgid;
  278. } else {
  279. /*
  280. * Using the euid would make more sense here, but something
  281. * in userland might rely on the old behavior, and this
  282. * shouldn't be a security problem since
  283. * PTRACE_MODE_REALCREDS implies that the caller explicitly
  284. * used a syscall that requests access to another process
  285. * (and not a filesystem syscall to procfs).
  286. */
  287. caller_uid = cred->uid;
  288. caller_gid = cred->gid;
  289. }
  290. tcred = __task_cred(task);
  291. if (uid_eq(caller_uid, tcred->euid) &&
  292. uid_eq(caller_uid, tcred->suid) &&
  293. uid_eq(caller_uid, tcred->uid) &&
  294. gid_eq(caller_gid, tcred->egid) &&
  295. gid_eq(caller_gid, tcred->sgid) &&
  296. gid_eq(caller_gid, tcred->gid))
  297. goto ok;
  298. if (ptrace_has_cap(tcred->user_ns, mode))
  299. goto ok;
  300. rcu_read_unlock();
  301. return -EPERM;
  302. ok:
  303. rcu_read_unlock();
  304. /*
  305. * If a task drops privileges and becomes nondumpable (through a syscall
  306. * like setresuid()) while we are trying to access it, we must ensure
  307. * that the dumpability is read after the credentials; otherwise,
  308. * we may be able to attach to a task that we shouldn't be able to
  309. * attach to (as if the task had dropped privileges without becoming
  310. * nondumpable).
  311. * Pairs with a write barrier in commit_creds().
  312. */
  313. smp_rmb();
  314. mm = task->mm;
  315. if (mm &&
  316. ((get_dumpable(mm) != SUID_DUMP_USER) &&
  317. !ptrace_has_cap(mm->user_ns, mode)))
  318. return -EPERM;
  319. return security_ptrace_access_check(task, mode);
  320. }
  321. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  322. {
  323. int err;
  324. task_lock(task);
  325. err = __ptrace_may_access(task, mode);
  326. task_unlock(task);
  327. return !err;
  328. }
  329. static int ptrace_attach(struct task_struct *task, long request,
  330. unsigned long addr,
  331. unsigned long flags)
  332. {
  333. bool seize = (request == PTRACE_SEIZE);
  334. int retval;
  335. retval = -EIO;
  336. if (seize) {
  337. if (addr != 0)
  338. goto out;
  339. if (flags & ~(unsigned long)PTRACE_O_MASK)
  340. goto out;
  341. flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
  342. } else {
  343. flags = PT_PTRACED;
  344. }
  345. audit_ptrace(task);
  346. retval = -EPERM;
  347. if (unlikely(task->flags & PF_KTHREAD))
  348. goto out;
  349. if (same_thread_group(task, current))
  350. goto out;
  351. /*
  352. * Protect exec's credential calculations against our interference;
  353. * SUID, SGID and LSM creds get determined differently
  354. * under ptrace.
  355. */
  356. retval = -ERESTARTNOINTR;
  357. if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
  358. goto out;
  359. task_lock(task);
  360. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
  361. task_unlock(task);
  362. if (retval)
  363. goto unlock_creds;
  364. write_lock_irq(&tasklist_lock);
  365. retval = -EPERM;
  366. if (unlikely(task->exit_state))
  367. goto unlock_tasklist;
  368. if (task->ptrace)
  369. goto unlock_tasklist;
  370. if (seize)
  371. flags |= PT_SEIZED;
  372. task->ptrace = flags;
  373. ptrace_link(task, current);
  374. /* SEIZE doesn't trap tracee on attach */
  375. if (!seize)
  376. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  377. spin_lock(&task->sighand->siglock);
  378. /*
  379. * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
  380. * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
  381. * will be cleared if the child completes the transition or any
  382. * event which clears the group stop states happens. We'll wait
  383. * for the transition to complete before returning from this
  384. * function.
  385. *
  386. * This hides STOPPED -> RUNNING -> TRACED transition from the
  387. * attaching thread but a different thread in the same group can
  388. * still observe the transient RUNNING state. IOW, if another
  389. * thread's WNOHANG wait(2) on the stopped tracee races against
  390. * ATTACH, the wait(2) may fail due to the transient RUNNING.
  391. *
  392. * The following task_is_stopped() test is safe as both transitions
  393. * in and out of STOPPED are protected by siglock.
  394. */
  395. if (task_is_stopped(task) &&
  396. task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
  397. signal_wake_up_state(task, __TASK_STOPPED);
  398. spin_unlock(&task->sighand->siglock);
  399. retval = 0;
  400. unlock_tasklist:
  401. write_unlock_irq(&tasklist_lock);
  402. unlock_creds:
  403. mutex_unlock(&task->signal->cred_guard_mutex);
  404. out:
  405. if (!retval) {
  406. /*
  407. * We do not bother to change retval or clear JOBCTL_TRAPPING
  408. * if wait_on_bit() was interrupted by SIGKILL. The tracer will
  409. * not return to user-mode, it will exit and clear this bit in
  410. * __ptrace_unlink() if it wasn't already cleared by the tracee;
  411. * and until then nobody can ptrace this task.
  412. */
  413. wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
  414. proc_ptrace_connector(task, PTRACE_ATTACH);
  415. }
  416. return retval;
  417. }
  418. /**
  419. * ptrace_traceme -- helper for PTRACE_TRACEME
  420. *
  421. * Performs checks and sets PT_PTRACED.
  422. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  423. */
  424. static int ptrace_traceme(void)
  425. {
  426. int ret = -EPERM;
  427. write_lock_irq(&tasklist_lock);
  428. /* Are we already being traced? */
  429. if (!current->ptrace) {
  430. ret = security_ptrace_traceme(current->parent);
  431. /*
  432. * Check PF_EXITING to ensure ->real_parent has not passed
  433. * exit_ptrace(). Otherwise we don't report the error but
  434. * pretend ->real_parent untraces us right after return.
  435. */
  436. if (!ret && !(current->real_parent->flags & PF_EXITING)) {
  437. current->ptrace = PT_PTRACED;
  438. ptrace_link(current, current->real_parent);
  439. }
  440. }
  441. write_unlock_irq(&tasklist_lock);
  442. return ret;
  443. }
  444. /*
  445. * Called with irqs disabled, returns true if childs should reap themselves.
  446. */
  447. static int ignoring_children(struct sighand_struct *sigh)
  448. {
  449. int ret;
  450. spin_lock(&sigh->siglock);
  451. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  452. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  453. spin_unlock(&sigh->siglock);
  454. return ret;
  455. }
  456. /*
  457. * Called with tasklist_lock held for writing.
  458. * Unlink a traced task, and clean it up if it was a traced zombie.
  459. * Return true if it needs to be reaped with release_task().
  460. * (We can't call release_task() here because we already hold tasklist_lock.)
  461. *
  462. * If it's a zombie, our attachedness prevented normal parent notification
  463. * or self-reaping. Do notification now if it would have happened earlier.
  464. * If it should reap itself, return true.
  465. *
  466. * If it's our own child, there is no notification to do. But if our normal
  467. * children self-reap, then this child was prevented by ptrace and we must
  468. * reap it now, in that case we must also wake up sub-threads sleeping in
  469. * do_wait().
  470. */
  471. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  472. {
  473. bool dead;
  474. __ptrace_unlink(p);
  475. if (p->exit_state != EXIT_ZOMBIE)
  476. return false;
  477. dead = !thread_group_leader(p);
  478. if (!dead && thread_group_empty(p)) {
  479. if (!same_thread_group(p->real_parent, tracer))
  480. dead = do_notify_parent(p, p->exit_signal);
  481. else if (ignoring_children(tracer->sighand)) {
  482. __wake_up_parent(p, tracer);
  483. dead = true;
  484. }
  485. }
  486. /* Mark it as in the process of being reaped. */
  487. if (dead)
  488. p->exit_state = EXIT_DEAD;
  489. return dead;
  490. }
  491. static int ptrace_detach(struct task_struct *child, unsigned int data)
  492. {
  493. if (!valid_signal(data))
  494. return -EIO;
  495. /* Architecture-specific hardware disable .. */
  496. ptrace_disable(child);
  497. write_lock_irq(&tasklist_lock);
  498. /*
  499. * We rely on ptrace_freeze_traced(). It can't be killed and
  500. * untraced by another thread, it can't be a zombie.
  501. */
  502. WARN_ON(!child->ptrace || child->exit_state);
  503. /*
  504. * tasklist_lock avoids the race with wait_task_stopped(), see
  505. * the comment in ptrace_resume().
  506. */
  507. child->exit_code = data;
  508. __ptrace_detach(current, child);
  509. write_unlock_irq(&tasklist_lock);
  510. proc_ptrace_connector(child, PTRACE_DETACH);
  511. return 0;
  512. }
  513. /*
  514. * Detach all tasks we were using ptrace on. Called with tasklist held
  515. * for writing.
  516. */
  517. void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
  518. {
  519. struct task_struct *p, *n;
  520. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  521. if (unlikely(p->ptrace & PT_EXITKILL))
  522. send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
  523. if (__ptrace_detach(tracer, p))
  524. list_add(&p->ptrace_entry, dead);
  525. }
  526. }
  527. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  528. {
  529. int copied = 0;
  530. while (len > 0) {
  531. char buf[128];
  532. int this_len, retval;
  533. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  534. retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE);
  535. if (!retval) {
  536. if (copied)
  537. break;
  538. return -EIO;
  539. }
  540. if (copy_to_user(dst, buf, retval))
  541. return -EFAULT;
  542. copied += retval;
  543. src += retval;
  544. dst += retval;
  545. len -= retval;
  546. }
  547. return copied;
  548. }
  549. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  550. {
  551. int copied = 0;
  552. while (len > 0) {
  553. char buf[128];
  554. int this_len, retval;
  555. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  556. if (copy_from_user(buf, src, this_len))
  557. return -EFAULT;
  558. retval = ptrace_access_vm(tsk, dst, buf, this_len,
  559. FOLL_FORCE | FOLL_WRITE);
  560. if (!retval) {
  561. if (copied)
  562. break;
  563. return -EIO;
  564. }
  565. copied += retval;
  566. src += retval;
  567. dst += retval;
  568. len -= retval;
  569. }
  570. return copied;
  571. }
  572. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  573. {
  574. unsigned flags;
  575. if (data & ~(unsigned long)PTRACE_O_MASK)
  576. return -EINVAL;
  577. if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
  578. if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
  579. !IS_ENABLED(CONFIG_SECCOMP))
  580. return -EINVAL;
  581. if (!capable(CAP_SYS_ADMIN))
  582. return -EPERM;
  583. if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
  584. current->ptrace & PT_SUSPEND_SECCOMP)
  585. return -EPERM;
  586. }
  587. /* Avoid intermediate state when all opts are cleared */
  588. flags = child->ptrace;
  589. flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
  590. flags |= (data << PT_OPT_FLAG_SHIFT);
  591. child->ptrace = flags;
  592. return 0;
  593. }
  594. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  595. {
  596. unsigned long flags;
  597. int error = -ESRCH;
  598. if (lock_task_sighand(child, &flags)) {
  599. error = -EINVAL;
  600. if (likely(child->last_siginfo != NULL)) {
  601. copy_siginfo(info, child->last_siginfo);
  602. error = 0;
  603. }
  604. unlock_task_sighand(child, &flags);
  605. }
  606. return error;
  607. }
  608. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  609. {
  610. unsigned long flags;
  611. int error = -ESRCH;
  612. if (lock_task_sighand(child, &flags)) {
  613. error = -EINVAL;
  614. if (likely(child->last_siginfo != NULL)) {
  615. copy_siginfo(child->last_siginfo, info);
  616. error = 0;
  617. }
  618. unlock_task_sighand(child, &flags);
  619. }
  620. return error;
  621. }
  622. static int ptrace_peek_siginfo(struct task_struct *child,
  623. unsigned long addr,
  624. unsigned long data)
  625. {
  626. struct ptrace_peeksiginfo_args arg;
  627. struct sigpending *pending;
  628. struct sigqueue *q;
  629. int ret, i;
  630. ret = copy_from_user(&arg, (void __user *) addr,
  631. sizeof(struct ptrace_peeksiginfo_args));
  632. if (ret)
  633. return -EFAULT;
  634. if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
  635. return -EINVAL; /* unknown flags */
  636. if (arg.nr < 0)
  637. return -EINVAL;
  638. /* Ensure arg.off fits in an unsigned long */
  639. if (arg.off > ULONG_MAX)
  640. return 0;
  641. if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
  642. pending = &child->signal->shared_pending;
  643. else
  644. pending = &child->pending;
  645. for (i = 0; i < arg.nr; ) {
  646. siginfo_t info;
  647. unsigned long off = arg.off + i;
  648. bool found = false;
  649. spin_lock_irq(&child->sighand->siglock);
  650. list_for_each_entry(q, &pending->list, list) {
  651. if (!off--) {
  652. found = true;
  653. copy_siginfo(&info, &q->info);
  654. break;
  655. }
  656. }
  657. spin_unlock_irq(&child->sighand->siglock);
  658. if (!found) /* beyond the end of the list */
  659. break;
  660. #ifdef CONFIG_COMPAT
  661. if (unlikely(in_compat_syscall())) {
  662. compat_siginfo_t __user *uinfo = compat_ptr(data);
  663. if (copy_siginfo_to_user32(uinfo, &info)) {
  664. ret = -EFAULT;
  665. break;
  666. }
  667. } else
  668. #endif
  669. {
  670. siginfo_t __user *uinfo = (siginfo_t __user *) data;
  671. if (copy_siginfo_to_user(uinfo, &info)) {
  672. ret = -EFAULT;
  673. break;
  674. }
  675. }
  676. data += sizeof(siginfo_t);
  677. i++;
  678. if (signal_pending(current))
  679. break;
  680. cond_resched();
  681. }
  682. if (i > 0)
  683. return i;
  684. return ret;
  685. }
  686. #ifdef PTRACE_SINGLESTEP
  687. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  688. #else
  689. #define is_singlestep(request) 0
  690. #endif
  691. #ifdef PTRACE_SINGLEBLOCK
  692. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  693. #else
  694. #define is_singleblock(request) 0
  695. #endif
  696. #ifdef PTRACE_SYSEMU
  697. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  698. #else
  699. #define is_sysemu_singlestep(request) 0
  700. #endif
  701. static int ptrace_resume(struct task_struct *child, long request,
  702. unsigned long data)
  703. {
  704. bool need_siglock;
  705. if (!valid_signal(data))
  706. return -EIO;
  707. if (request == PTRACE_SYSCALL)
  708. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  709. else
  710. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  711. #ifdef TIF_SYSCALL_EMU
  712. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  713. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  714. else
  715. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  716. #endif
  717. if (is_singleblock(request)) {
  718. if (unlikely(!arch_has_block_step()))
  719. return -EIO;
  720. user_enable_block_step(child);
  721. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  722. if (unlikely(!arch_has_single_step()))
  723. return -EIO;
  724. user_enable_single_step(child);
  725. } else {
  726. user_disable_single_step(child);
  727. }
  728. /*
  729. * Change ->exit_code and ->state under siglock to avoid the race
  730. * with wait_task_stopped() in between; a non-zero ->exit_code will
  731. * wrongly look like another report from tracee.
  732. *
  733. * Note that we need siglock even if ->exit_code == data and/or this
  734. * status was not reported yet, the new status must not be cleared by
  735. * wait_task_stopped() after resume.
  736. *
  737. * If data == 0 we do not care if wait_task_stopped() reports the old
  738. * status and clears the code too; this can't race with the tracee, it
  739. * takes siglock after resume.
  740. */
  741. need_siglock = data && !thread_group_empty(current);
  742. if (need_siglock)
  743. spin_lock_irq(&child->sighand->siglock);
  744. child->exit_code = data;
  745. wake_up_state(child, __TASK_TRACED);
  746. if (need_siglock)
  747. spin_unlock_irq(&child->sighand->siglock);
  748. return 0;
  749. }
  750. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  751. static const struct user_regset *
  752. find_regset(const struct user_regset_view *view, unsigned int type)
  753. {
  754. const struct user_regset *regset;
  755. int n;
  756. for (n = 0; n < view->n; ++n) {
  757. regset = view->regsets + n;
  758. if (regset->core_note_type == type)
  759. return regset;
  760. }
  761. return NULL;
  762. }
  763. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  764. struct iovec *kiov)
  765. {
  766. const struct user_regset_view *view = task_user_regset_view(task);
  767. const struct user_regset *regset = find_regset(view, type);
  768. int regset_no;
  769. if (!regset || (kiov->iov_len % regset->size) != 0)
  770. return -EINVAL;
  771. regset_no = regset - view->regsets;
  772. kiov->iov_len = min(kiov->iov_len,
  773. (__kernel_size_t) (regset->n * regset->size));
  774. if (req == PTRACE_GETREGSET)
  775. return copy_regset_to_user(task, view, regset_no, 0,
  776. kiov->iov_len, kiov->iov_base);
  777. else
  778. return copy_regset_from_user(task, view, regset_no, 0,
  779. kiov->iov_len, kiov->iov_base);
  780. }
  781. /*
  782. * This is declared in linux/regset.h and defined in machine-dependent
  783. * code. We put the export here, near the primary machine-neutral use,
  784. * to ensure no machine forgets it.
  785. */
  786. EXPORT_SYMBOL_GPL(task_user_regset_view);
  787. #endif
  788. int ptrace_request(struct task_struct *child, long request,
  789. unsigned long addr, unsigned long data)
  790. {
  791. bool seized = child->ptrace & PT_SEIZED;
  792. int ret = -EIO;
  793. siginfo_t siginfo, *si;
  794. void __user *datavp = (void __user *) data;
  795. unsigned long __user *datalp = datavp;
  796. unsigned long flags;
  797. switch (request) {
  798. case PTRACE_PEEKTEXT:
  799. case PTRACE_PEEKDATA:
  800. return generic_ptrace_peekdata(child, addr, data);
  801. case PTRACE_POKETEXT:
  802. case PTRACE_POKEDATA:
  803. return generic_ptrace_pokedata(child, addr, data);
  804. #ifdef PTRACE_OLDSETOPTIONS
  805. case PTRACE_OLDSETOPTIONS:
  806. #endif
  807. case PTRACE_SETOPTIONS:
  808. ret = ptrace_setoptions(child, data);
  809. break;
  810. case PTRACE_GETEVENTMSG:
  811. ret = put_user(child->ptrace_message, datalp);
  812. break;
  813. case PTRACE_PEEKSIGINFO:
  814. ret = ptrace_peek_siginfo(child, addr, data);
  815. break;
  816. case PTRACE_GETSIGINFO:
  817. ret = ptrace_getsiginfo(child, &siginfo);
  818. if (!ret)
  819. ret = copy_siginfo_to_user(datavp, &siginfo);
  820. break;
  821. case PTRACE_SETSIGINFO:
  822. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  823. ret = -EFAULT;
  824. else
  825. ret = ptrace_setsiginfo(child, &siginfo);
  826. break;
  827. case PTRACE_GETSIGMASK: {
  828. sigset_t *mask;
  829. if (addr != sizeof(sigset_t)) {
  830. ret = -EINVAL;
  831. break;
  832. }
  833. if (test_tsk_restore_sigmask(child))
  834. mask = &child->saved_sigmask;
  835. else
  836. mask = &child->blocked;
  837. if (copy_to_user(datavp, mask, sizeof(sigset_t)))
  838. ret = -EFAULT;
  839. else
  840. ret = 0;
  841. break;
  842. }
  843. case PTRACE_SETSIGMASK: {
  844. sigset_t new_set;
  845. if (addr != sizeof(sigset_t)) {
  846. ret = -EINVAL;
  847. break;
  848. }
  849. if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
  850. ret = -EFAULT;
  851. break;
  852. }
  853. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  854. /*
  855. * Every thread does recalc_sigpending() after resume, so
  856. * retarget_shared_pending() and recalc_sigpending() are not
  857. * called here.
  858. */
  859. spin_lock_irq(&child->sighand->siglock);
  860. child->blocked = new_set;
  861. spin_unlock_irq(&child->sighand->siglock);
  862. clear_tsk_restore_sigmask(child);
  863. ret = 0;
  864. break;
  865. }
  866. case PTRACE_INTERRUPT:
  867. /*
  868. * Stop tracee without any side-effect on signal or job
  869. * control. At least one trap is guaranteed to happen
  870. * after this request. If @child is already trapped, the
  871. * current trap is not disturbed and another trap will
  872. * happen after the current trap is ended with PTRACE_CONT.
  873. *
  874. * The actual trap might not be PTRACE_EVENT_STOP trap but
  875. * the pending condition is cleared regardless.
  876. */
  877. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  878. break;
  879. /*
  880. * INTERRUPT doesn't disturb existing trap sans one
  881. * exception. If ptracer issued LISTEN for the current
  882. * STOP, this INTERRUPT should clear LISTEN and re-trap
  883. * tracee into STOP.
  884. */
  885. if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
  886. ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
  887. unlock_task_sighand(child, &flags);
  888. ret = 0;
  889. break;
  890. case PTRACE_LISTEN:
  891. /*
  892. * Listen for events. Tracee must be in STOP. It's not
  893. * resumed per-se but is not considered to be in TRACED by
  894. * wait(2) or ptrace(2). If an async event (e.g. group
  895. * stop state change) happens, tracee will enter STOP trap
  896. * again. Alternatively, ptracer can issue INTERRUPT to
  897. * finish listening and re-trap tracee into STOP.
  898. */
  899. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  900. break;
  901. si = child->last_siginfo;
  902. if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
  903. child->jobctl |= JOBCTL_LISTENING;
  904. /*
  905. * If NOTIFY is set, it means event happened between
  906. * start of this trap and now. Trigger re-trap.
  907. */
  908. if (child->jobctl & JOBCTL_TRAP_NOTIFY)
  909. ptrace_signal_wake_up(child, true);
  910. ret = 0;
  911. }
  912. unlock_task_sighand(child, &flags);
  913. break;
  914. case PTRACE_DETACH: /* detach a process that was attached. */
  915. ret = ptrace_detach(child, data);
  916. break;
  917. #ifdef CONFIG_BINFMT_ELF_FDPIC
  918. case PTRACE_GETFDPIC: {
  919. struct mm_struct *mm = get_task_mm(child);
  920. unsigned long tmp = 0;
  921. ret = -ESRCH;
  922. if (!mm)
  923. break;
  924. switch (addr) {
  925. case PTRACE_GETFDPIC_EXEC:
  926. tmp = mm->context.exec_fdpic_loadmap;
  927. break;
  928. case PTRACE_GETFDPIC_INTERP:
  929. tmp = mm->context.interp_fdpic_loadmap;
  930. break;
  931. default:
  932. break;
  933. }
  934. mmput(mm);
  935. ret = put_user(tmp, datalp);
  936. break;
  937. }
  938. #endif
  939. #ifdef PTRACE_SINGLESTEP
  940. case PTRACE_SINGLESTEP:
  941. #endif
  942. #ifdef PTRACE_SINGLEBLOCK
  943. case PTRACE_SINGLEBLOCK:
  944. #endif
  945. #ifdef PTRACE_SYSEMU
  946. case PTRACE_SYSEMU:
  947. case PTRACE_SYSEMU_SINGLESTEP:
  948. #endif
  949. case PTRACE_SYSCALL:
  950. case PTRACE_CONT:
  951. return ptrace_resume(child, request, data);
  952. case PTRACE_KILL:
  953. if (child->exit_state) /* already dead */
  954. return 0;
  955. return ptrace_resume(child, request, SIGKILL);
  956. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  957. case PTRACE_GETREGSET:
  958. case PTRACE_SETREGSET: {
  959. struct iovec kiov;
  960. struct iovec __user *uiov = datavp;
  961. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  962. return -EFAULT;
  963. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  964. __get_user(kiov.iov_len, &uiov->iov_len))
  965. return -EFAULT;
  966. ret = ptrace_regset(child, request, addr, &kiov);
  967. if (!ret)
  968. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  969. break;
  970. }
  971. #endif
  972. case PTRACE_SECCOMP_GET_FILTER:
  973. ret = seccomp_get_filter(child, addr, datavp);
  974. break;
  975. case PTRACE_SECCOMP_GET_METADATA:
  976. ret = seccomp_get_metadata(child, addr, datavp);
  977. break;
  978. default:
  979. break;
  980. }
  981. return ret;
  982. }
  983. #ifndef arch_ptrace_attach
  984. #define arch_ptrace_attach(child) do { } while (0)
  985. #endif
  986. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  987. unsigned long, data)
  988. {
  989. struct task_struct *child;
  990. long ret;
  991. if (request == PTRACE_TRACEME) {
  992. ret = ptrace_traceme();
  993. if (!ret)
  994. arch_ptrace_attach(current);
  995. goto out;
  996. }
  997. child = find_get_task_by_vpid(pid);
  998. if (!child) {
  999. ret = -ESRCH;
  1000. goto out;
  1001. }
  1002. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1003. ret = ptrace_attach(child, request, addr, data);
  1004. /*
  1005. * Some architectures need to do book-keeping after
  1006. * a ptrace attach.
  1007. */
  1008. if (!ret)
  1009. arch_ptrace_attach(child);
  1010. goto out_put_task_struct;
  1011. }
  1012. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1013. request == PTRACE_INTERRUPT);
  1014. if (ret < 0)
  1015. goto out_put_task_struct;
  1016. ret = arch_ptrace(child, request, addr, data);
  1017. if (ret || request != PTRACE_DETACH)
  1018. ptrace_unfreeze_traced(child);
  1019. out_put_task_struct:
  1020. put_task_struct(child);
  1021. out:
  1022. return ret;
  1023. }
  1024. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  1025. unsigned long data)
  1026. {
  1027. unsigned long tmp;
  1028. int copied;
  1029. copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
  1030. if (copied != sizeof(tmp))
  1031. return -EIO;
  1032. return put_user(tmp, (unsigned long __user *)data);
  1033. }
  1034. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  1035. unsigned long data)
  1036. {
  1037. int copied;
  1038. copied = ptrace_access_vm(tsk, addr, &data, sizeof(data),
  1039. FOLL_FORCE | FOLL_WRITE);
  1040. return (copied == sizeof(data)) ? 0 : -EIO;
  1041. }
  1042. #if defined CONFIG_COMPAT
  1043. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  1044. compat_ulong_t addr, compat_ulong_t data)
  1045. {
  1046. compat_ulong_t __user *datap = compat_ptr(data);
  1047. compat_ulong_t word;
  1048. siginfo_t siginfo;
  1049. int ret;
  1050. switch (request) {
  1051. case PTRACE_PEEKTEXT:
  1052. case PTRACE_PEEKDATA:
  1053. ret = ptrace_access_vm(child, addr, &word, sizeof(word),
  1054. FOLL_FORCE);
  1055. if (ret != sizeof(word))
  1056. ret = -EIO;
  1057. else
  1058. ret = put_user(word, datap);
  1059. break;
  1060. case PTRACE_POKETEXT:
  1061. case PTRACE_POKEDATA:
  1062. ret = ptrace_access_vm(child, addr, &data, sizeof(data),
  1063. FOLL_FORCE | FOLL_WRITE);
  1064. ret = (ret != sizeof(data) ? -EIO : 0);
  1065. break;
  1066. case PTRACE_GETEVENTMSG:
  1067. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  1068. break;
  1069. case PTRACE_GETSIGINFO:
  1070. ret = ptrace_getsiginfo(child, &siginfo);
  1071. if (!ret)
  1072. ret = copy_siginfo_to_user32(
  1073. (struct compat_siginfo __user *) datap,
  1074. &siginfo);
  1075. break;
  1076. case PTRACE_SETSIGINFO:
  1077. if (copy_siginfo_from_user32(
  1078. &siginfo, (struct compat_siginfo __user *) datap))
  1079. ret = -EFAULT;
  1080. else
  1081. ret = ptrace_setsiginfo(child, &siginfo);
  1082. break;
  1083. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  1084. case PTRACE_GETREGSET:
  1085. case PTRACE_SETREGSET:
  1086. {
  1087. struct iovec kiov;
  1088. struct compat_iovec __user *uiov =
  1089. (struct compat_iovec __user *) datap;
  1090. compat_uptr_t ptr;
  1091. compat_size_t len;
  1092. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  1093. return -EFAULT;
  1094. if (__get_user(ptr, &uiov->iov_base) ||
  1095. __get_user(len, &uiov->iov_len))
  1096. return -EFAULT;
  1097. kiov.iov_base = compat_ptr(ptr);
  1098. kiov.iov_len = len;
  1099. ret = ptrace_regset(child, request, addr, &kiov);
  1100. if (!ret)
  1101. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  1102. break;
  1103. }
  1104. #endif
  1105. default:
  1106. ret = ptrace_request(child, request, addr, data);
  1107. }
  1108. return ret;
  1109. }
  1110. COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
  1111. compat_long_t, addr, compat_long_t, data)
  1112. {
  1113. struct task_struct *child;
  1114. long ret;
  1115. if (request == PTRACE_TRACEME) {
  1116. ret = ptrace_traceme();
  1117. goto out;
  1118. }
  1119. child = find_get_task_by_vpid(pid);
  1120. if (!child) {
  1121. ret = -ESRCH;
  1122. goto out;
  1123. }
  1124. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1125. ret = ptrace_attach(child, request, addr, data);
  1126. /*
  1127. * Some architectures need to do book-keeping after
  1128. * a ptrace attach.
  1129. */
  1130. if (!ret)
  1131. arch_ptrace_attach(child);
  1132. goto out_put_task_struct;
  1133. }
  1134. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1135. request == PTRACE_INTERRUPT);
  1136. if (!ret) {
  1137. ret = compat_arch_ptrace(child, request, addr, data);
  1138. if (ret || request != PTRACE_DETACH)
  1139. ptrace_unfreeze_traced(child);
  1140. }
  1141. out_put_task_struct:
  1142. put_task_struct(child);
  1143. out:
  1144. return ret;
  1145. }
  1146. #endif /* CONFIG_COMPAT */