ptrace.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 (unlikely(p->ptrace & PT_EXITKILL))
  413. send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
  414. if (__ptrace_detach(tracer, p))
  415. list_add(&p->ptrace_entry, &ptrace_dead);
  416. }
  417. write_unlock_irq(&tasklist_lock);
  418. BUG_ON(!list_empty(&tracer->ptraced));
  419. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  420. list_del_init(&p->ptrace_entry);
  421. release_task(p);
  422. }
  423. write_lock_irq(&tasklist_lock);
  424. }
  425. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  426. {
  427. int copied = 0;
  428. while (len > 0) {
  429. char buf[128];
  430. int this_len, retval;
  431. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  432. retval = access_process_vm(tsk, src, buf, this_len, 0);
  433. if (!retval) {
  434. if (copied)
  435. break;
  436. return -EIO;
  437. }
  438. if (copy_to_user(dst, buf, retval))
  439. return -EFAULT;
  440. copied += retval;
  441. src += retval;
  442. dst += retval;
  443. len -= retval;
  444. }
  445. return copied;
  446. }
  447. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  448. {
  449. int copied = 0;
  450. while (len > 0) {
  451. char buf[128];
  452. int this_len, retval;
  453. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  454. if (copy_from_user(buf, src, this_len))
  455. return -EFAULT;
  456. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  457. if (!retval) {
  458. if (copied)
  459. break;
  460. return -EIO;
  461. }
  462. copied += retval;
  463. src += retval;
  464. dst += retval;
  465. len -= retval;
  466. }
  467. return copied;
  468. }
  469. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  470. {
  471. unsigned flags;
  472. if (data & ~(unsigned long)PTRACE_O_MASK)
  473. return -EINVAL;
  474. /* Avoid intermediate state when all opts are cleared */
  475. flags = child->ptrace;
  476. flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
  477. flags |= (data << PT_OPT_FLAG_SHIFT);
  478. child->ptrace = flags;
  479. return 0;
  480. }
  481. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  482. {
  483. unsigned long flags;
  484. int error = -ESRCH;
  485. if (lock_task_sighand(child, &flags)) {
  486. error = -EINVAL;
  487. if (likely(child->last_siginfo != NULL)) {
  488. *info = *child->last_siginfo;
  489. error = 0;
  490. }
  491. unlock_task_sighand(child, &flags);
  492. }
  493. return error;
  494. }
  495. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  496. {
  497. unsigned long flags;
  498. int error = -ESRCH;
  499. if (lock_task_sighand(child, &flags)) {
  500. error = -EINVAL;
  501. if (likely(child->last_siginfo != NULL)) {
  502. *child->last_siginfo = *info;
  503. error = 0;
  504. }
  505. unlock_task_sighand(child, &flags);
  506. }
  507. return error;
  508. }
  509. #ifdef PTRACE_SINGLESTEP
  510. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  511. #else
  512. #define is_singlestep(request) 0
  513. #endif
  514. #ifdef PTRACE_SINGLEBLOCK
  515. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  516. #else
  517. #define is_singleblock(request) 0
  518. #endif
  519. #ifdef PTRACE_SYSEMU
  520. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  521. #else
  522. #define is_sysemu_singlestep(request) 0
  523. #endif
  524. static int ptrace_resume(struct task_struct *child, long request,
  525. unsigned long data)
  526. {
  527. if (!valid_signal(data))
  528. return -EIO;
  529. if (request == PTRACE_SYSCALL)
  530. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  531. else
  532. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  533. #ifdef TIF_SYSCALL_EMU
  534. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  535. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  536. else
  537. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  538. #endif
  539. if (is_singleblock(request)) {
  540. if (unlikely(!arch_has_block_step()))
  541. return -EIO;
  542. user_enable_block_step(child);
  543. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  544. if (unlikely(!arch_has_single_step()))
  545. return -EIO;
  546. user_enable_single_step(child);
  547. } else {
  548. user_disable_single_step(child);
  549. }
  550. child->exit_code = data;
  551. wake_up_state(child, __TASK_TRACED);
  552. return 0;
  553. }
  554. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  555. static const struct user_regset *
  556. find_regset(const struct user_regset_view *view, unsigned int type)
  557. {
  558. const struct user_regset *regset;
  559. int n;
  560. for (n = 0; n < view->n; ++n) {
  561. regset = view->regsets + n;
  562. if (regset->core_note_type == type)
  563. return regset;
  564. }
  565. return NULL;
  566. }
  567. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  568. struct iovec *kiov)
  569. {
  570. const struct user_regset_view *view = task_user_regset_view(task);
  571. const struct user_regset *regset = find_regset(view, type);
  572. int regset_no;
  573. if (!regset || (kiov->iov_len % regset->size) != 0)
  574. return -EINVAL;
  575. regset_no = regset - view->regsets;
  576. kiov->iov_len = min(kiov->iov_len,
  577. (__kernel_size_t) (regset->n * regset->size));
  578. if (req == PTRACE_GETREGSET)
  579. return copy_regset_to_user(task, view, regset_no, 0,
  580. kiov->iov_len, kiov->iov_base);
  581. else
  582. return copy_regset_from_user(task, view, regset_no, 0,
  583. kiov->iov_len, kiov->iov_base);
  584. }
  585. #endif
  586. int ptrace_request(struct task_struct *child, long request,
  587. unsigned long addr, unsigned long data)
  588. {
  589. bool seized = child->ptrace & PT_SEIZED;
  590. int ret = -EIO;
  591. siginfo_t siginfo, *si;
  592. void __user *datavp = (void __user *) data;
  593. unsigned long __user *datalp = datavp;
  594. unsigned long flags;
  595. switch (request) {
  596. case PTRACE_PEEKTEXT:
  597. case PTRACE_PEEKDATA:
  598. return generic_ptrace_peekdata(child, addr, data);
  599. case PTRACE_POKETEXT:
  600. case PTRACE_POKEDATA:
  601. return generic_ptrace_pokedata(child, addr, data);
  602. #ifdef PTRACE_OLDSETOPTIONS
  603. case PTRACE_OLDSETOPTIONS:
  604. #endif
  605. case PTRACE_SETOPTIONS:
  606. ret = ptrace_setoptions(child, data);
  607. break;
  608. case PTRACE_GETEVENTMSG:
  609. ret = put_user(child->ptrace_message, datalp);
  610. break;
  611. case PTRACE_GETSIGINFO:
  612. ret = ptrace_getsiginfo(child, &siginfo);
  613. if (!ret)
  614. ret = copy_siginfo_to_user(datavp, &siginfo);
  615. break;
  616. case PTRACE_SETSIGINFO:
  617. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  618. ret = -EFAULT;
  619. else
  620. ret = ptrace_setsiginfo(child, &siginfo);
  621. break;
  622. case PTRACE_INTERRUPT:
  623. /*
  624. * Stop tracee without any side-effect on signal or job
  625. * control. At least one trap is guaranteed to happen
  626. * after this request. If @child is already trapped, the
  627. * current trap is not disturbed and another trap will
  628. * happen after the current trap is ended with PTRACE_CONT.
  629. *
  630. * The actual trap might not be PTRACE_EVENT_STOP trap but
  631. * the pending condition is cleared regardless.
  632. */
  633. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  634. break;
  635. /*
  636. * INTERRUPT doesn't disturb existing trap sans one
  637. * exception. If ptracer issued LISTEN for the current
  638. * STOP, this INTERRUPT should clear LISTEN and re-trap
  639. * tracee into STOP.
  640. */
  641. if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
  642. signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
  643. unlock_task_sighand(child, &flags);
  644. ret = 0;
  645. break;
  646. case PTRACE_LISTEN:
  647. /*
  648. * Listen for events. Tracee must be in STOP. It's not
  649. * resumed per-se but is not considered to be in TRACED by
  650. * wait(2) or ptrace(2). If an async event (e.g. group
  651. * stop state change) happens, tracee will enter STOP trap
  652. * again. Alternatively, ptracer can issue INTERRUPT to
  653. * finish listening and re-trap tracee into STOP.
  654. */
  655. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  656. break;
  657. si = child->last_siginfo;
  658. if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
  659. child->jobctl |= JOBCTL_LISTENING;
  660. /*
  661. * If NOTIFY is set, it means event happened between
  662. * start of this trap and now. Trigger re-trap.
  663. */
  664. if (child->jobctl & JOBCTL_TRAP_NOTIFY)
  665. signal_wake_up(child, true);
  666. ret = 0;
  667. }
  668. unlock_task_sighand(child, &flags);
  669. break;
  670. case PTRACE_DETACH: /* detach a process that was attached. */
  671. ret = ptrace_detach(child, data);
  672. break;
  673. #ifdef CONFIG_BINFMT_ELF_FDPIC
  674. case PTRACE_GETFDPIC: {
  675. struct mm_struct *mm = get_task_mm(child);
  676. unsigned long tmp = 0;
  677. ret = -ESRCH;
  678. if (!mm)
  679. break;
  680. switch (addr) {
  681. case PTRACE_GETFDPIC_EXEC:
  682. tmp = mm->context.exec_fdpic_loadmap;
  683. break;
  684. case PTRACE_GETFDPIC_INTERP:
  685. tmp = mm->context.interp_fdpic_loadmap;
  686. break;
  687. default:
  688. break;
  689. }
  690. mmput(mm);
  691. ret = put_user(tmp, datalp);
  692. break;
  693. }
  694. #endif
  695. #ifdef PTRACE_SINGLESTEP
  696. case PTRACE_SINGLESTEP:
  697. #endif
  698. #ifdef PTRACE_SINGLEBLOCK
  699. case PTRACE_SINGLEBLOCK:
  700. #endif
  701. #ifdef PTRACE_SYSEMU
  702. case PTRACE_SYSEMU:
  703. case PTRACE_SYSEMU_SINGLESTEP:
  704. #endif
  705. case PTRACE_SYSCALL:
  706. case PTRACE_CONT:
  707. return ptrace_resume(child, request, data);
  708. case PTRACE_KILL:
  709. if (child->exit_state) /* already dead */
  710. return 0;
  711. return ptrace_resume(child, request, SIGKILL);
  712. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  713. case PTRACE_GETREGSET:
  714. case PTRACE_SETREGSET:
  715. {
  716. struct iovec kiov;
  717. struct iovec __user *uiov = datavp;
  718. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  719. return -EFAULT;
  720. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  721. __get_user(kiov.iov_len, &uiov->iov_len))
  722. return -EFAULT;
  723. ret = ptrace_regset(child, request, addr, &kiov);
  724. if (!ret)
  725. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  726. break;
  727. }
  728. #endif
  729. default:
  730. break;
  731. }
  732. return ret;
  733. }
  734. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  735. {
  736. struct task_struct *child;
  737. rcu_read_lock();
  738. child = find_task_by_vpid(pid);
  739. if (child)
  740. get_task_struct(child);
  741. rcu_read_unlock();
  742. if (!child)
  743. return ERR_PTR(-ESRCH);
  744. return child;
  745. }
  746. #ifndef arch_ptrace_attach
  747. #define arch_ptrace_attach(child) do { } while (0)
  748. #endif
  749. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  750. unsigned long, data)
  751. {
  752. struct task_struct *child;
  753. long ret;
  754. if (request == PTRACE_TRACEME) {
  755. ret = ptrace_traceme();
  756. if (!ret)
  757. arch_ptrace_attach(current);
  758. goto out;
  759. }
  760. child = ptrace_get_task_struct(pid);
  761. if (IS_ERR(child)) {
  762. ret = PTR_ERR(child);
  763. goto out;
  764. }
  765. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  766. ret = ptrace_attach(child, request, addr, data);
  767. /*
  768. * Some architectures need to do book-keeping after
  769. * a ptrace attach.
  770. */
  771. if (!ret)
  772. arch_ptrace_attach(child);
  773. goto out_put_task_struct;
  774. }
  775. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  776. request == PTRACE_INTERRUPT);
  777. if (ret < 0)
  778. goto out_put_task_struct;
  779. ret = arch_ptrace(child, request, addr, data);
  780. out_put_task_struct:
  781. put_task_struct(child);
  782. out:
  783. return ret;
  784. }
  785. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  786. unsigned long data)
  787. {
  788. unsigned long tmp;
  789. int copied;
  790. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  791. if (copied != sizeof(tmp))
  792. return -EIO;
  793. return put_user(tmp, (unsigned long __user *)data);
  794. }
  795. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  796. unsigned long data)
  797. {
  798. int copied;
  799. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  800. return (copied == sizeof(data)) ? 0 : -EIO;
  801. }
  802. #if defined CONFIG_COMPAT
  803. #include <linux/compat.h>
  804. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  805. compat_ulong_t addr, compat_ulong_t data)
  806. {
  807. compat_ulong_t __user *datap = compat_ptr(data);
  808. compat_ulong_t word;
  809. siginfo_t siginfo;
  810. int ret;
  811. switch (request) {
  812. case PTRACE_PEEKTEXT:
  813. case PTRACE_PEEKDATA:
  814. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  815. if (ret != sizeof(word))
  816. ret = -EIO;
  817. else
  818. ret = put_user(word, datap);
  819. break;
  820. case PTRACE_POKETEXT:
  821. case PTRACE_POKEDATA:
  822. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  823. ret = (ret != sizeof(data) ? -EIO : 0);
  824. break;
  825. case PTRACE_GETEVENTMSG:
  826. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  827. break;
  828. case PTRACE_GETSIGINFO:
  829. ret = ptrace_getsiginfo(child, &siginfo);
  830. if (!ret)
  831. ret = copy_siginfo_to_user32(
  832. (struct compat_siginfo __user *) datap,
  833. &siginfo);
  834. break;
  835. case PTRACE_SETSIGINFO:
  836. memset(&siginfo, 0, sizeof siginfo);
  837. if (copy_siginfo_from_user32(
  838. &siginfo, (struct compat_siginfo __user *) datap))
  839. ret = -EFAULT;
  840. else
  841. ret = ptrace_setsiginfo(child, &siginfo);
  842. break;
  843. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  844. case PTRACE_GETREGSET:
  845. case PTRACE_SETREGSET:
  846. {
  847. struct iovec kiov;
  848. struct compat_iovec __user *uiov =
  849. (struct compat_iovec __user *) datap;
  850. compat_uptr_t ptr;
  851. compat_size_t len;
  852. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  853. return -EFAULT;
  854. if (__get_user(ptr, &uiov->iov_base) ||
  855. __get_user(len, &uiov->iov_len))
  856. return -EFAULT;
  857. kiov.iov_base = compat_ptr(ptr);
  858. kiov.iov_len = len;
  859. ret = ptrace_regset(child, request, addr, &kiov);
  860. if (!ret)
  861. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  862. break;
  863. }
  864. #endif
  865. default:
  866. ret = ptrace_request(child, request, addr, data);
  867. }
  868. return ret;
  869. }
  870. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  871. compat_long_t addr, compat_long_t data)
  872. {
  873. struct task_struct *child;
  874. long ret;
  875. if (request == PTRACE_TRACEME) {
  876. ret = ptrace_traceme();
  877. goto out;
  878. }
  879. child = ptrace_get_task_struct(pid);
  880. if (IS_ERR(child)) {
  881. ret = PTR_ERR(child);
  882. goto out;
  883. }
  884. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  885. ret = ptrace_attach(child, request, addr, data);
  886. /*
  887. * Some architectures need to do book-keeping after
  888. * a ptrace attach.
  889. */
  890. if (!ret)
  891. arch_ptrace_attach(child);
  892. goto out_put_task_struct;
  893. }
  894. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  895. request == PTRACE_INTERRUPT);
  896. if (!ret)
  897. ret = compat_arch_ptrace(child, request, addr, data);
  898. out_put_task_struct:
  899. put_task_struct(child);
  900. out:
  901. return ret;
  902. }
  903. #endif /* CONFIG_COMPAT */
  904. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  905. int ptrace_get_breakpoints(struct task_struct *tsk)
  906. {
  907. if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
  908. return 0;
  909. return -1;
  910. }
  911. void ptrace_put_breakpoints(struct task_struct *tsk)
  912. {
  913. if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
  914. flush_ptrace_hw_breakpoint(tsk);
  915. }
  916. #endif /* CONFIG_HAVE_HW_BREAKPOINT */