ptrace.c 37 KB

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