ptrace.c 32 KB

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