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