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