ptrace.c 27 KB

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