ptrace.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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(const struct cred *cred, struct user_namespace *ns,
  247. unsigned int mode)
  248. {
  249. int ret;
  250. if (mode & PTRACE_MODE_NOAUDIT)
  251. ret = security_capable(cred, ns, CAP_SYS_PTRACE);
  252. else
  253. ret = security_capable(cred, ns, CAP_SYS_PTRACE);
  254. return ret == 0;
  255. }
  256. /* Returns 0 on success, -errno on denial. */
  257. static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  258. {
  259. const struct cred *cred = current_cred(), *tcred;
  260. struct mm_struct *mm;
  261. kuid_t caller_uid;
  262. kgid_t caller_gid;
  263. if (!(mode & PTRACE_MODE_FSCREDS) == !(mode & PTRACE_MODE_REALCREDS)) {
  264. WARN(1, "denying ptrace access check without PTRACE_MODE_*CREDS\n");
  265. return -EPERM;
  266. }
  267. /* May we inspect the given task?
  268. * This check is used both for attaching with ptrace
  269. * and for allowing access to sensitive information in /proc.
  270. *
  271. * ptrace_attach denies several cases that /proc allows
  272. * because setting up the necessary parent/child relationship
  273. * or halting the specified task is impossible.
  274. */
  275. /* Don't let security modules deny introspection */
  276. if (same_thread_group(task, current))
  277. return 0;
  278. rcu_read_lock();
  279. if (mode & PTRACE_MODE_FSCREDS) {
  280. caller_uid = cred->fsuid;
  281. caller_gid = cred->fsgid;
  282. } else {
  283. /*
  284. * Using the euid would make more sense here, but something
  285. * in userland might rely on the old behavior, and this
  286. * shouldn't be a security problem since
  287. * PTRACE_MODE_REALCREDS implies that the caller explicitly
  288. * used a syscall that requests access to another process
  289. * (and not a filesystem syscall to procfs).
  290. */
  291. caller_uid = cred->uid;
  292. caller_gid = cred->gid;
  293. }
  294. tcred = __task_cred(task);
  295. if (uid_eq(caller_uid, tcred->euid) &&
  296. uid_eq(caller_uid, tcred->suid) &&
  297. uid_eq(caller_uid, tcred->uid) &&
  298. gid_eq(caller_gid, tcred->egid) &&
  299. gid_eq(caller_gid, tcred->sgid) &&
  300. gid_eq(caller_gid, tcred->gid))
  301. goto ok;
  302. if (ptrace_has_cap(cred, tcred->user_ns, mode))
  303. goto ok;
  304. rcu_read_unlock();
  305. return -EPERM;
  306. ok:
  307. rcu_read_unlock();
  308. /*
  309. * If a task drops privileges and becomes nondumpable (through a syscall
  310. * like setresuid()) while we are trying to access it, we must ensure
  311. * that the dumpability is read after the credentials; otherwise,
  312. * we may be able to attach to a task that we shouldn't be able to
  313. * attach to (as if the task had dropped privileges without becoming
  314. * nondumpable).
  315. * Pairs with a write barrier in commit_creds().
  316. */
  317. smp_rmb();
  318. mm = task->mm;
  319. if (mm &&
  320. ((get_dumpable(mm) != SUID_DUMP_USER) &&
  321. !ptrace_has_cap(cred, mm->user_ns, mode)))
  322. return -EPERM;
  323. return security_ptrace_access_check(task, mode);
  324. }
  325. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  326. {
  327. int err;
  328. task_lock(task);
  329. err = __ptrace_may_access(task, mode);
  330. task_unlock(task);
  331. return !err;
  332. }
  333. static int ptrace_attach(struct task_struct *task, long request,
  334. unsigned long addr,
  335. unsigned long flags)
  336. {
  337. bool seize = (request == PTRACE_SEIZE);
  338. int retval;
  339. retval = -EIO;
  340. if (seize) {
  341. if (addr != 0)
  342. goto out;
  343. if (flags & ~(unsigned long)PTRACE_O_MASK)
  344. goto out;
  345. flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
  346. } else {
  347. flags = PT_PTRACED;
  348. }
  349. audit_ptrace(task);
  350. retval = -EPERM;
  351. if (unlikely(task->flags & PF_KTHREAD))
  352. goto out;
  353. if (same_thread_group(task, current))
  354. goto out;
  355. /*
  356. * Protect exec's credential calculations against our interference;
  357. * SUID, SGID and LSM creds get determined differently
  358. * under ptrace.
  359. */
  360. retval = -ERESTARTNOINTR;
  361. if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
  362. goto out;
  363. task_lock(task);
  364. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH_REALCREDS);
  365. task_unlock(task);
  366. if (retval)
  367. goto unlock_creds;
  368. write_lock_irq(&tasklist_lock);
  369. retval = -EPERM;
  370. if (unlikely(task->exit_state))
  371. goto unlock_tasklist;
  372. if (task->ptrace)
  373. goto unlock_tasklist;
  374. if (seize)
  375. flags |= PT_SEIZED;
  376. task->ptrace = flags;
  377. ptrace_link(task, current);
  378. /* SEIZE doesn't trap tracee on attach */
  379. if (!seize)
  380. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  381. spin_lock(&task->sighand->siglock);
  382. /*
  383. * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
  384. * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
  385. * will be cleared if the child completes the transition or any
  386. * event which clears the group stop states happens. We'll wait
  387. * for the transition to complete before returning from this
  388. * function.
  389. *
  390. * This hides STOPPED -> RUNNING -> TRACED transition from the
  391. * attaching thread but a different thread in the same group can
  392. * still observe the transient RUNNING state. IOW, if another
  393. * thread's WNOHANG wait(2) on the stopped tracee races against
  394. * ATTACH, the wait(2) may fail due to the transient RUNNING.
  395. *
  396. * The following task_is_stopped() test is safe as both transitions
  397. * in and out of STOPPED are protected by siglock.
  398. */
  399. if (task_is_stopped(task) &&
  400. task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
  401. signal_wake_up_state(task, __TASK_STOPPED);
  402. spin_unlock(&task->sighand->siglock);
  403. retval = 0;
  404. unlock_tasklist:
  405. write_unlock_irq(&tasklist_lock);
  406. unlock_creds:
  407. mutex_unlock(&task->signal->cred_guard_mutex);
  408. out:
  409. if (!retval) {
  410. /*
  411. * We do not bother to change retval or clear JOBCTL_TRAPPING
  412. * if wait_on_bit() was interrupted by SIGKILL. The tracer will
  413. * not return to user-mode, it will exit and clear this bit in
  414. * __ptrace_unlink() if it wasn't already cleared by the tracee;
  415. * and until then nobody can ptrace this task.
  416. */
  417. wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT, TASK_KILLABLE);
  418. proc_ptrace_connector(task, PTRACE_ATTACH);
  419. }
  420. return retval;
  421. }
  422. /**
  423. * ptrace_traceme -- helper for PTRACE_TRACEME
  424. *
  425. * Performs checks and sets PT_PTRACED.
  426. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  427. */
  428. static int ptrace_traceme(void)
  429. {
  430. int ret = -EPERM;
  431. write_lock_irq(&tasklist_lock);
  432. /* Are we already being traced? */
  433. if (!current->ptrace) {
  434. ret = security_ptrace_traceme(current->parent);
  435. /*
  436. * Check PF_EXITING to ensure ->real_parent has not passed
  437. * exit_ptrace(). Otherwise we don't report the error but
  438. * pretend ->real_parent untraces us right after return.
  439. */
  440. if (!ret && !(current->real_parent->flags & PF_EXITING)) {
  441. current->ptrace = PT_PTRACED;
  442. ptrace_link(current, current->real_parent);
  443. }
  444. }
  445. write_unlock_irq(&tasklist_lock);
  446. return ret;
  447. }
  448. /*
  449. * Called with irqs disabled, returns true if childs should reap themselves.
  450. */
  451. static int ignoring_children(struct sighand_struct *sigh)
  452. {
  453. int ret;
  454. spin_lock(&sigh->siglock);
  455. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  456. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  457. spin_unlock(&sigh->siglock);
  458. return ret;
  459. }
  460. /*
  461. * Called with tasklist_lock held for writing.
  462. * Unlink a traced task, and clean it up if it was a traced zombie.
  463. * Return true if it needs to be reaped with release_task().
  464. * (We can't call release_task() here because we already hold tasklist_lock.)
  465. *
  466. * If it's a zombie, our attachedness prevented normal parent notification
  467. * or self-reaping. Do notification now if it would have happened earlier.
  468. * If it should reap itself, return true.
  469. *
  470. * If it's our own child, there is no notification to do. But if our normal
  471. * children self-reap, then this child was prevented by ptrace and we must
  472. * reap it now, in that case we must also wake up sub-threads sleeping in
  473. * do_wait().
  474. */
  475. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  476. {
  477. bool dead;
  478. __ptrace_unlink(p);
  479. if (p->exit_state != EXIT_ZOMBIE)
  480. return false;
  481. dead = !thread_group_leader(p);
  482. if (!dead && thread_group_empty(p)) {
  483. if (!same_thread_group(p->real_parent, tracer))
  484. dead = do_notify_parent(p, p->exit_signal);
  485. else if (ignoring_children(tracer->sighand)) {
  486. __wake_up_parent(p, tracer);
  487. dead = true;
  488. }
  489. }
  490. /* Mark it as in the process of being reaped. */
  491. if (dead)
  492. p->exit_state = EXIT_DEAD;
  493. return dead;
  494. }
  495. static int ptrace_detach(struct task_struct *child, unsigned int data)
  496. {
  497. if (!valid_signal(data))
  498. return -EIO;
  499. /* Architecture-specific hardware disable .. */
  500. ptrace_disable(child);
  501. write_lock_irq(&tasklist_lock);
  502. /*
  503. * We rely on ptrace_freeze_traced(). It can't be killed and
  504. * untraced by another thread, it can't be a zombie.
  505. */
  506. WARN_ON(!child->ptrace || child->exit_state);
  507. /*
  508. * tasklist_lock avoids the race with wait_task_stopped(), see
  509. * the comment in ptrace_resume().
  510. */
  511. child->exit_code = data;
  512. __ptrace_detach(current, child);
  513. write_unlock_irq(&tasklist_lock);
  514. proc_ptrace_connector(child, PTRACE_DETACH);
  515. return 0;
  516. }
  517. /*
  518. * Detach all tasks we were using ptrace on. Called with tasklist held
  519. * for writing.
  520. */
  521. void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
  522. {
  523. struct task_struct *p, *n;
  524. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  525. if (unlikely(p->ptrace & PT_EXITKILL))
  526. send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
  527. if (__ptrace_detach(tracer, p))
  528. list_add(&p->ptrace_entry, dead);
  529. }
  530. }
  531. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  532. {
  533. int copied = 0;
  534. while (len > 0) {
  535. char buf[128];
  536. int this_len, retval;
  537. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  538. retval = ptrace_access_vm(tsk, src, buf, this_len, FOLL_FORCE);
  539. if (!retval) {
  540. if (copied)
  541. break;
  542. return -EIO;
  543. }
  544. if (copy_to_user(dst, buf, retval))
  545. return -EFAULT;
  546. copied += retval;
  547. src += retval;
  548. dst += retval;
  549. len -= retval;
  550. }
  551. return copied;
  552. }
  553. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  554. {
  555. int copied = 0;
  556. while (len > 0) {
  557. char buf[128];
  558. int this_len, retval;
  559. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  560. if (copy_from_user(buf, src, this_len))
  561. return -EFAULT;
  562. retval = ptrace_access_vm(tsk, dst, buf, this_len,
  563. FOLL_FORCE | FOLL_WRITE);
  564. if (!retval) {
  565. if (copied)
  566. break;
  567. return -EIO;
  568. }
  569. copied += retval;
  570. src += retval;
  571. dst += retval;
  572. len -= retval;
  573. }
  574. return copied;
  575. }
  576. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  577. {
  578. unsigned flags;
  579. if (data & ~(unsigned long)PTRACE_O_MASK)
  580. return -EINVAL;
  581. if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
  582. if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
  583. !IS_ENABLED(CONFIG_SECCOMP))
  584. return -EINVAL;
  585. if (!capable(CAP_SYS_ADMIN))
  586. return -EPERM;
  587. if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
  588. current->ptrace & PT_SUSPEND_SECCOMP)
  589. return -EPERM;
  590. }
  591. /* Avoid intermediate state when all opts are cleared */
  592. flags = child->ptrace;
  593. flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
  594. flags |= (data << PT_OPT_FLAG_SHIFT);
  595. child->ptrace = flags;
  596. return 0;
  597. }
  598. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  599. {
  600. unsigned long flags;
  601. int error = -ESRCH;
  602. if (lock_task_sighand(child, &flags)) {
  603. error = -EINVAL;
  604. if (likely(child->last_siginfo != NULL)) {
  605. *info = *child->last_siginfo;
  606. error = 0;
  607. }
  608. unlock_task_sighand(child, &flags);
  609. }
  610. return error;
  611. }
  612. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  613. {
  614. unsigned long flags;
  615. int error = -ESRCH;
  616. if (lock_task_sighand(child, &flags)) {
  617. error = -EINVAL;
  618. if (likely(child->last_siginfo != NULL)) {
  619. *child->last_siginfo = *info;
  620. error = 0;
  621. }
  622. unlock_task_sighand(child, &flags);
  623. }
  624. return error;
  625. }
  626. static int ptrace_peek_siginfo(struct task_struct *child,
  627. unsigned long addr,
  628. unsigned long data)
  629. {
  630. struct ptrace_peeksiginfo_args arg;
  631. struct sigpending *pending;
  632. struct sigqueue *q;
  633. int ret, i;
  634. ret = copy_from_user(&arg, (void __user *) addr,
  635. sizeof(struct ptrace_peeksiginfo_args));
  636. if (ret)
  637. return -EFAULT;
  638. if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
  639. return -EINVAL; /* unknown flags */
  640. if (arg.nr < 0)
  641. return -EINVAL;
  642. /* Ensure arg.off fits in an unsigned long */
  643. if (arg.off > ULONG_MAX)
  644. return 0;
  645. if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
  646. pending = &child->signal->shared_pending;
  647. else
  648. pending = &child->pending;
  649. for (i = 0; i < arg.nr; ) {
  650. siginfo_t info;
  651. unsigned long off = arg.off + i;
  652. bool found = false;
  653. spin_lock_irq(&child->sighand->siglock);
  654. list_for_each_entry(q, &pending->list, list) {
  655. if (!off--) {
  656. found = true;
  657. copy_siginfo(&info, &q->info);
  658. break;
  659. }
  660. }
  661. spin_unlock_irq(&child->sighand->siglock);
  662. if (!found) /* beyond the end of the list */
  663. break;
  664. #ifdef CONFIG_COMPAT
  665. if (unlikely(in_compat_syscall())) {
  666. compat_siginfo_t __user *uinfo = compat_ptr(data);
  667. if (copy_siginfo_to_user32(uinfo, &info)) {
  668. ret = -EFAULT;
  669. break;
  670. }
  671. } else
  672. #endif
  673. {
  674. siginfo_t __user *uinfo = (siginfo_t __user *) data;
  675. if (copy_siginfo_to_user(uinfo, &info)) {
  676. ret = -EFAULT;
  677. break;
  678. }
  679. }
  680. data += sizeof(siginfo_t);
  681. i++;
  682. if (signal_pending(current))
  683. break;
  684. cond_resched();
  685. }
  686. if (i > 0)
  687. return i;
  688. return ret;
  689. }
  690. #ifdef PTRACE_SINGLESTEP
  691. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  692. #else
  693. #define is_singlestep(request) 0
  694. #endif
  695. #ifdef PTRACE_SINGLEBLOCK
  696. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  697. #else
  698. #define is_singleblock(request) 0
  699. #endif
  700. #ifdef PTRACE_SYSEMU
  701. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  702. #else
  703. #define is_sysemu_singlestep(request) 0
  704. #endif
  705. static int ptrace_resume(struct task_struct *child, long request,
  706. unsigned long data)
  707. {
  708. bool need_siglock;
  709. if (!valid_signal(data))
  710. return -EIO;
  711. if (request == PTRACE_SYSCALL)
  712. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  713. else
  714. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  715. #ifdef TIF_SYSCALL_EMU
  716. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  717. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  718. else
  719. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  720. #endif
  721. if (is_singleblock(request)) {
  722. if (unlikely(!arch_has_block_step()))
  723. return -EIO;
  724. user_enable_block_step(child);
  725. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  726. if (unlikely(!arch_has_single_step()))
  727. return -EIO;
  728. user_enable_single_step(child);
  729. } else {
  730. user_disable_single_step(child);
  731. }
  732. /*
  733. * Change ->exit_code and ->state under siglock to avoid the race
  734. * with wait_task_stopped() in between; a non-zero ->exit_code will
  735. * wrongly look like another report from tracee.
  736. *
  737. * Note that we need siglock even if ->exit_code == data and/or this
  738. * status was not reported yet, the new status must not be cleared by
  739. * wait_task_stopped() after resume.
  740. *
  741. * If data == 0 we do not care if wait_task_stopped() reports the old
  742. * status and clears the code too; this can't race with the tracee, it
  743. * takes siglock after resume.
  744. */
  745. need_siglock = data && !thread_group_empty(current);
  746. if (need_siglock)
  747. spin_lock_irq(&child->sighand->siglock);
  748. child->exit_code = data;
  749. wake_up_state(child, __TASK_TRACED);
  750. if (need_siglock)
  751. spin_unlock_irq(&child->sighand->siglock);
  752. return 0;
  753. }
  754. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  755. static const struct user_regset *
  756. find_regset(const struct user_regset_view *view, unsigned int type)
  757. {
  758. const struct user_regset *regset;
  759. int n;
  760. for (n = 0; n < view->n; ++n) {
  761. regset = view->regsets + n;
  762. if (regset->core_note_type == type)
  763. return regset;
  764. }
  765. return NULL;
  766. }
  767. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  768. struct iovec *kiov)
  769. {
  770. const struct user_regset_view *view = task_user_regset_view(task);
  771. const struct user_regset *regset = find_regset(view, type);
  772. int regset_no;
  773. if (!regset || (kiov->iov_len % regset->size) != 0)
  774. return -EINVAL;
  775. regset_no = regset - view->regsets;
  776. kiov->iov_len = min(kiov->iov_len,
  777. (__kernel_size_t) (regset->n * regset->size));
  778. if (req == PTRACE_GETREGSET)
  779. return copy_regset_to_user(task, view, regset_no, 0,
  780. kiov->iov_len, kiov->iov_base);
  781. else
  782. return copy_regset_from_user(task, view, regset_no, 0,
  783. kiov->iov_len, kiov->iov_base);
  784. }
  785. /*
  786. * This is declared in linux/regset.h and defined in machine-dependent
  787. * code. We put the export here, near the primary machine-neutral use,
  788. * to ensure no machine forgets it.
  789. */
  790. EXPORT_SYMBOL_GPL(task_user_regset_view);
  791. #endif
  792. int ptrace_request(struct task_struct *child, long request,
  793. unsigned long addr, unsigned long data)
  794. {
  795. bool seized = child->ptrace & PT_SEIZED;
  796. int ret = -EIO;
  797. siginfo_t siginfo, *si;
  798. void __user *datavp = (void __user *) data;
  799. unsigned long __user *datalp = datavp;
  800. unsigned long flags;
  801. switch (request) {
  802. case PTRACE_PEEKTEXT:
  803. case PTRACE_PEEKDATA:
  804. return generic_ptrace_peekdata(child, addr, data);
  805. case PTRACE_POKETEXT:
  806. case PTRACE_POKEDATA:
  807. return generic_ptrace_pokedata(child, addr, data);
  808. #ifdef PTRACE_OLDSETOPTIONS
  809. case PTRACE_OLDSETOPTIONS:
  810. #endif
  811. case PTRACE_SETOPTIONS:
  812. ret = ptrace_setoptions(child, data);
  813. break;
  814. case PTRACE_GETEVENTMSG:
  815. ret = put_user(child->ptrace_message, datalp);
  816. break;
  817. case PTRACE_PEEKSIGINFO:
  818. ret = ptrace_peek_siginfo(child, addr, data);
  819. break;
  820. case PTRACE_GETSIGINFO:
  821. ret = ptrace_getsiginfo(child, &siginfo);
  822. if (!ret)
  823. ret = copy_siginfo_to_user(datavp, &siginfo);
  824. break;
  825. case PTRACE_SETSIGINFO:
  826. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  827. ret = -EFAULT;
  828. else
  829. ret = ptrace_setsiginfo(child, &siginfo);
  830. break;
  831. case PTRACE_GETSIGMASK: {
  832. sigset_t *mask;
  833. if (addr != sizeof(sigset_t)) {
  834. ret = -EINVAL;
  835. break;
  836. }
  837. if (test_tsk_restore_sigmask(child))
  838. mask = &child->saved_sigmask;
  839. else
  840. mask = &child->blocked;
  841. if (copy_to_user(datavp, mask, sizeof(sigset_t)))
  842. ret = -EFAULT;
  843. else
  844. ret = 0;
  845. break;
  846. }
  847. case PTRACE_SETSIGMASK: {
  848. sigset_t new_set;
  849. if (addr != sizeof(sigset_t)) {
  850. ret = -EINVAL;
  851. break;
  852. }
  853. if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
  854. ret = -EFAULT;
  855. break;
  856. }
  857. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  858. /*
  859. * Every thread does recalc_sigpending() after resume, so
  860. * retarget_shared_pending() and recalc_sigpending() are not
  861. * called here.
  862. */
  863. spin_lock_irq(&child->sighand->siglock);
  864. child->blocked = new_set;
  865. spin_unlock_irq(&child->sighand->siglock);
  866. clear_tsk_restore_sigmask(child);
  867. ret = 0;
  868. break;
  869. }
  870. case PTRACE_INTERRUPT:
  871. /*
  872. * Stop tracee without any side-effect on signal or job
  873. * control. At least one trap is guaranteed to happen
  874. * after this request. If @child is already trapped, the
  875. * current trap is not disturbed and another trap will
  876. * happen after the current trap is ended with PTRACE_CONT.
  877. *
  878. * The actual trap might not be PTRACE_EVENT_STOP trap but
  879. * the pending condition is cleared regardless.
  880. */
  881. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  882. break;
  883. /*
  884. * INTERRUPT doesn't disturb existing trap sans one
  885. * exception. If ptracer issued LISTEN for the current
  886. * STOP, this INTERRUPT should clear LISTEN and re-trap
  887. * tracee into STOP.
  888. */
  889. if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
  890. ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
  891. unlock_task_sighand(child, &flags);
  892. ret = 0;
  893. break;
  894. case PTRACE_LISTEN:
  895. /*
  896. * Listen for events. Tracee must be in STOP. It's not
  897. * resumed per-se but is not considered to be in TRACED by
  898. * wait(2) or ptrace(2). If an async event (e.g. group
  899. * stop state change) happens, tracee will enter STOP trap
  900. * again. Alternatively, ptracer can issue INTERRUPT to
  901. * finish listening and re-trap tracee into STOP.
  902. */
  903. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  904. break;
  905. si = child->last_siginfo;
  906. if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
  907. child->jobctl |= JOBCTL_LISTENING;
  908. /*
  909. * If NOTIFY is set, it means event happened between
  910. * start of this trap and now. Trigger re-trap.
  911. */
  912. if (child->jobctl & JOBCTL_TRAP_NOTIFY)
  913. ptrace_signal_wake_up(child, true);
  914. ret = 0;
  915. }
  916. unlock_task_sighand(child, &flags);
  917. break;
  918. case PTRACE_DETACH: /* detach a process that was attached. */
  919. ret = ptrace_detach(child, data);
  920. break;
  921. #ifdef CONFIG_BINFMT_ELF_FDPIC
  922. case PTRACE_GETFDPIC: {
  923. struct mm_struct *mm = get_task_mm(child);
  924. unsigned long tmp = 0;
  925. ret = -ESRCH;
  926. if (!mm)
  927. break;
  928. switch (addr) {
  929. case PTRACE_GETFDPIC_EXEC:
  930. tmp = mm->context.exec_fdpic_loadmap;
  931. break;
  932. case PTRACE_GETFDPIC_INTERP:
  933. tmp = mm->context.interp_fdpic_loadmap;
  934. break;
  935. default:
  936. break;
  937. }
  938. mmput(mm);
  939. ret = put_user(tmp, datalp);
  940. break;
  941. }
  942. #endif
  943. #ifdef PTRACE_SINGLESTEP
  944. case PTRACE_SINGLESTEP:
  945. #endif
  946. #ifdef PTRACE_SINGLEBLOCK
  947. case PTRACE_SINGLEBLOCK:
  948. #endif
  949. #ifdef PTRACE_SYSEMU
  950. case PTRACE_SYSEMU:
  951. case PTRACE_SYSEMU_SINGLESTEP:
  952. #endif
  953. case PTRACE_SYSCALL:
  954. case PTRACE_CONT:
  955. return ptrace_resume(child, request, data);
  956. case PTRACE_KILL:
  957. if (child->exit_state) /* already dead */
  958. return 0;
  959. return ptrace_resume(child, request, SIGKILL);
  960. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  961. case PTRACE_GETREGSET:
  962. case PTRACE_SETREGSET: {
  963. struct iovec kiov;
  964. struct iovec __user *uiov = datavp;
  965. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  966. return -EFAULT;
  967. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  968. __get_user(kiov.iov_len, &uiov->iov_len))
  969. return -EFAULT;
  970. ret = ptrace_regset(child, request, addr, &kiov);
  971. if (!ret)
  972. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  973. break;
  974. }
  975. #endif
  976. case PTRACE_SECCOMP_GET_FILTER:
  977. ret = seccomp_get_filter(child, addr, datavp);
  978. break;
  979. default:
  980. break;
  981. }
  982. return ret;
  983. }
  984. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  985. {
  986. struct task_struct *child;
  987. rcu_read_lock();
  988. child = find_task_by_vpid(pid);
  989. if (child)
  990. get_task_struct(child);
  991. rcu_read_unlock();
  992. if (!child)
  993. return ERR_PTR(-ESRCH);
  994. return child;
  995. }
  996. #ifndef arch_ptrace_attach
  997. #define arch_ptrace_attach(child) do { } while (0)
  998. #endif
  999. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  1000. unsigned long, data)
  1001. {
  1002. struct task_struct *child;
  1003. long ret;
  1004. if (request == PTRACE_TRACEME) {
  1005. ret = ptrace_traceme();
  1006. if (!ret)
  1007. arch_ptrace_attach(current);
  1008. goto out;
  1009. }
  1010. child = ptrace_get_task_struct(pid);
  1011. if (IS_ERR(child)) {
  1012. ret = PTR_ERR(child);
  1013. goto out;
  1014. }
  1015. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1016. ret = ptrace_attach(child, request, addr, data);
  1017. /*
  1018. * Some architectures need to do book-keeping after
  1019. * a ptrace attach.
  1020. */
  1021. if (!ret)
  1022. arch_ptrace_attach(child);
  1023. goto out_put_task_struct;
  1024. }
  1025. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1026. request == PTRACE_INTERRUPT);
  1027. if (ret < 0)
  1028. goto out_put_task_struct;
  1029. ret = arch_ptrace(child, request, addr, data);
  1030. if (ret || request != PTRACE_DETACH)
  1031. ptrace_unfreeze_traced(child);
  1032. out_put_task_struct:
  1033. put_task_struct(child);
  1034. out:
  1035. return ret;
  1036. }
  1037. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  1038. unsigned long data)
  1039. {
  1040. unsigned long tmp;
  1041. int copied;
  1042. copied = ptrace_access_vm(tsk, addr, &tmp, sizeof(tmp), FOLL_FORCE);
  1043. if (copied != sizeof(tmp))
  1044. return -EIO;
  1045. return put_user(tmp, (unsigned long __user *)data);
  1046. }
  1047. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  1048. unsigned long data)
  1049. {
  1050. int copied;
  1051. copied = ptrace_access_vm(tsk, addr, &data, sizeof(data),
  1052. FOLL_FORCE | FOLL_WRITE);
  1053. return (copied == sizeof(data)) ? 0 : -EIO;
  1054. }
  1055. #if defined CONFIG_COMPAT
  1056. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  1057. compat_ulong_t addr, compat_ulong_t data)
  1058. {
  1059. compat_ulong_t __user *datap = compat_ptr(data);
  1060. compat_ulong_t word;
  1061. siginfo_t siginfo;
  1062. int ret;
  1063. switch (request) {
  1064. case PTRACE_PEEKTEXT:
  1065. case PTRACE_PEEKDATA:
  1066. ret = ptrace_access_vm(child, addr, &word, sizeof(word),
  1067. FOLL_FORCE);
  1068. if (ret != sizeof(word))
  1069. ret = -EIO;
  1070. else
  1071. ret = put_user(word, datap);
  1072. break;
  1073. case PTRACE_POKETEXT:
  1074. case PTRACE_POKEDATA:
  1075. ret = ptrace_access_vm(child, addr, &data, sizeof(data),
  1076. FOLL_FORCE | FOLL_WRITE);
  1077. ret = (ret != sizeof(data) ? -EIO : 0);
  1078. break;
  1079. case PTRACE_GETEVENTMSG:
  1080. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  1081. break;
  1082. case PTRACE_GETSIGINFO:
  1083. ret = ptrace_getsiginfo(child, &siginfo);
  1084. if (!ret)
  1085. ret = copy_siginfo_to_user32(
  1086. (struct compat_siginfo __user *) datap,
  1087. &siginfo);
  1088. break;
  1089. case PTRACE_SETSIGINFO:
  1090. memset(&siginfo, 0, sizeof siginfo);
  1091. if (copy_siginfo_from_user32(
  1092. &siginfo, (struct compat_siginfo __user *) datap))
  1093. ret = -EFAULT;
  1094. else
  1095. ret = ptrace_setsiginfo(child, &siginfo);
  1096. break;
  1097. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  1098. case PTRACE_GETREGSET:
  1099. case PTRACE_SETREGSET:
  1100. {
  1101. struct iovec kiov;
  1102. struct compat_iovec __user *uiov =
  1103. (struct compat_iovec __user *) datap;
  1104. compat_uptr_t ptr;
  1105. compat_size_t len;
  1106. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  1107. return -EFAULT;
  1108. if (__get_user(ptr, &uiov->iov_base) ||
  1109. __get_user(len, &uiov->iov_len))
  1110. return -EFAULT;
  1111. kiov.iov_base = compat_ptr(ptr);
  1112. kiov.iov_len = len;
  1113. ret = ptrace_regset(child, request, addr, &kiov);
  1114. if (!ret)
  1115. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  1116. break;
  1117. }
  1118. #endif
  1119. default:
  1120. ret = ptrace_request(child, request, addr, data);
  1121. }
  1122. return ret;
  1123. }
  1124. COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
  1125. compat_long_t, addr, compat_long_t, data)
  1126. {
  1127. struct task_struct *child;
  1128. long ret;
  1129. if (request == PTRACE_TRACEME) {
  1130. ret = ptrace_traceme();
  1131. goto out;
  1132. }
  1133. child = ptrace_get_task_struct(pid);
  1134. if (IS_ERR(child)) {
  1135. ret = PTR_ERR(child);
  1136. goto out;
  1137. }
  1138. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1139. ret = ptrace_attach(child, request, addr, data);
  1140. /*
  1141. * Some architectures need to do book-keeping after
  1142. * a ptrace attach.
  1143. */
  1144. if (!ret)
  1145. arch_ptrace_attach(child);
  1146. goto out_put_task_struct;
  1147. }
  1148. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1149. request == PTRACE_INTERRUPT);
  1150. if (!ret) {
  1151. ret = compat_arch_ptrace(child, request, addr, data);
  1152. if (ret || request != PTRACE_DETACH)
  1153. ptrace_unfreeze_traced(child);
  1154. }
  1155. out_put_task_struct:
  1156. put_task_struct(child);
  1157. out:
  1158. return ret;
  1159. }
  1160. #endif /* CONFIG_COMPAT */