ptrace.c 26 KB

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