ptrace.c 32 KB

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