ptrace.c 26 KB

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