ptrace.c 28 KB

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