ptrace.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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/uio.h>
  20. #include <linux/audit.h>
  21. #include <linux/pid_namespace.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/regset.h>
  25. #include <linux/hw_breakpoint.h>
  26. #include <linux/cn_proc.h>
  27. #include <linux/compat.h>
  28. /*
  29. * ptrace a task: make the debugger its new parent and
  30. * move it to the ptrace list.
  31. *
  32. * Must be called with the tasklist lock write-held.
  33. */
  34. void __ptrace_link(struct task_struct *child, struct task_struct *new_parent)
  35. {
  36. BUG_ON(!list_empty(&child->ptrace_entry));
  37. list_add(&child->ptrace_entry, &new_parent->ptraced);
  38. child->parent = new_parent;
  39. }
  40. /**
  41. * __ptrace_unlink - unlink ptracee and restore its execution state
  42. * @child: ptracee to be unlinked
  43. *
  44. * Remove @child from the ptrace list, move it back to the original parent,
  45. * and restore the execution state so that it conforms to the group stop
  46. * state.
  47. *
  48. * Unlinking can happen via two paths - explicit PTRACE_DETACH or ptracer
  49. * exiting. For PTRACE_DETACH, unless the ptracee has been killed between
  50. * ptrace_check_attach() and here, it's guaranteed to be in TASK_TRACED.
  51. * If the ptracer is exiting, the ptracee can be in any state.
  52. *
  53. * After detach, the ptracee should be in a state which conforms to the
  54. * group stop. If the group is stopped or in the process of stopping, the
  55. * ptracee should be put into TASK_STOPPED; otherwise, it should be woken
  56. * up from TASK_TRACED.
  57. *
  58. * If the ptracee is in TASK_TRACED and needs to be moved to TASK_STOPPED,
  59. * it goes through TRACED -> RUNNING -> STOPPED transition which is similar
  60. * to but in the opposite direction of what happens while attaching to a
  61. * stopped task. However, in this direction, the intermediate RUNNING
  62. * state is not hidden even from the current ptracer and if it immediately
  63. * re-attaches and performs a WNOHANG wait(2), it may fail.
  64. *
  65. * CONTEXT:
  66. * write_lock_irq(tasklist_lock)
  67. */
  68. void __ptrace_unlink(struct task_struct *child)
  69. {
  70. BUG_ON(!child->ptrace);
  71. child->ptrace = 0;
  72. child->parent = child->real_parent;
  73. list_del_init(&child->ptrace_entry);
  74. spin_lock(&child->sighand->siglock);
  75. /*
  76. * Clear all pending traps and TRAPPING. TRAPPING should be
  77. * cleared regardless of JOBCTL_STOP_PENDING. Do it explicitly.
  78. */
  79. task_clear_jobctl_pending(child, JOBCTL_TRAP_MASK);
  80. task_clear_jobctl_trapping(child);
  81. /*
  82. * Reinstate JOBCTL_STOP_PENDING if group stop is in effect and
  83. * @child isn't dead.
  84. */
  85. if (!(child->flags & PF_EXITING) &&
  86. (child->signal->flags & SIGNAL_STOP_STOPPED ||
  87. child->signal->group_stop_count)) {
  88. child->jobctl |= JOBCTL_STOP_PENDING;
  89. /*
  90. * This is only possible if this thread was cloned by the
  91. * traced task running in the stopped group, set the signal
  92. * for the future reports.
  93. * FIXME: we should change ptrace_init_task() to handle this
  94. * case.
  95. */
  96. if (!(child->jobctl & JOBCTL_STOP_SIGMASK))
  97. child->jobctl |= SIGSTOP;
  98. }
  99. /*
  100. * If transition to TASK_STOPPED is pending or in TASK_TRACED, kick
  101. * @child in the butt. Note that @resume should be used iff @child
  102. * is in TASK_TRACED; otherwise, we might unduly disrupt
  103. * TASK_KILLABLE sleeps.
  104. */
  105. if (child->jobctl & JOBCTL_STOP_PENDING || task_is_traced(child))
  106. ptrace_signal_wake_up(child, true);
  107. spin_unlock(&child->sighand->siglock);
  108. }
  109. /* Ensure that nothing can wake it up, even SIGKILL */
  110. static bool ptrace_freeze_traced(struct task_struct *task)
  111. {
  112. bool ret = false;
  113. /* Lockless, nobody but us can set this flag */
  114. if (task->jobctl & JOBCTL_LISTENING)
  115. return ret;
  116. spin_lock_irq(&task->sighand->siglock);
  117. if (task_is_traced(task) && !__fatal_signal_pending(task)) {
  118. task->state = __TASK_TRACED;
  119. ret = true;
  120. }
  121. spin_unlock_irq(&task->sighand->siglock);
  122. return ret;
  123. }
  124. static void ptrace_unfreeze_traced(struct task_struct *task)
  125. {
  126. if (task->state != __TASK_TRACED)
  127. return;
  128. WARN_ON(!task->ptrace || task->parent != current);
  129. spin_lock_irq(&task->sighand->siglock);
  130. if (__fatal_signal_pending(task))
  131. wake_up_state(task, __TASK_TRACED);
  132. else
  133. task->state = TASK_TRACED;
  134. spin_unlock_irq(&task->sighand->siglock);
  135. }
  136. /**
  137. * ptrace_check_attach - check whether ptracee is ready for ptrace operation
  138. * @child: ptracee to check for
  139. * @ignore_state: don't check whether @child is currently %TASK_TRACED
  140. *
  141. * Check whether @child is being ptraced by %current and ready for further
  142. * ptrace operations. If @ignore_state is %false, @child also should be in
  143. * %TASK_TRACED state and on return the child is guaranteed to be traced
  144. * and not executing. If @ignore_state is %true, @child can be in any
  145. * state.
  146. *
  147. * CONTEXT:
  148. * Grabs and releases tasklist_lock and @child->sighand->siglock.
  149. *
  150. * RETURNS:
  151. * 0 on success, -ESRCH if %child is not ready.
  152. */
  153. static int ptrace_check_attach(struct task_struct *child, bool ignore_state)
  154. {
  155. int ret = -ESRCH;
  156. /*
  157. * We take the read lock around doing both checks to close a
  158. * possible race where someone else was tracing our child and
  159. * detached between these two checks. After this locked check,
  160. * we are sure that this is our traced child and that can only
  161. * be changed by us so it's not changing right after this.
  162. */
  163. read_lock(&tasklist_lock);
  164. if (child->ptrace && child->parent == current) {
  165. WARN_ON(child->state == __TASK_TRACED);
  166. /*
  167. * child->sighand can't be NULL, release_task()
  168. * does ptrace_unlink() before __exit_signal().
  169. */
  170. if (ignore_state || ptrace_freeze_traced(child))
  171. ret = 0;
  172. }
  173. read_unlock(&tasklist_lock);
  174. if (!ret && !ignore_state) {
  175. if (!wait_task_inactive(child, __TASK_TRACED)) {
  176. /*
  177. * This can only happen if may_ptrace_stop() fails and
  178. * ptrace_stop() changes ->state back to TASK_RUNNING,
  179. * so we should not worry about leaking __TASK_TRACED.
  180. */
  181. WARN_ON(child->state == __TASK_TRACED);
  182. ret = -ESRCH;
  183. }
  184. }
  185. return ret;
  186. }
  187. static int ptrace_has_cap(struct user_namespace *ns, unsigned int mode)
  188. {
  189. if (mode & PTRACE_MODE_NOAUDIT)
  190. return has_ns_capability_noaudit(current, ns, CAP_SYS_PTRACE);
  191. else
  192. return has_ns_capability(current, ns, CAP_SYS_PTRACE);
  193. }
  194. /* Returns 0 on success, -errno on denial. */
  195. static int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  196. {
  197. const struct cred *cred = current_cred(), *tcred;
  198. /* May we inspect the given task?
  199. * This check is used both for attaching with ptrace
  200. * and for allowing access to sensitive information in /proc.
  201. *
  202. * ptrace_attach denies several cases that /proc allows
  203. * because setting up the necessary parent/child relationship
  204. * or halting the specified task is impossible.
  205. */
  206. int dumpable = 0;
  207. /* Don't let security modules deny introspection */
  208. if (same_thread_group(task, current))
  209. return 0;
  210. rcu_read_lock();
  211. tcred = __task_cred(task);
  212. if (uid_eq(cred->uid, tcred->euid) &&
  213. uid_eq(cred->uid, tcred->suid) &&
  214. uid_eq(cred->uid, tcred->uid) &&
  215. gid_eq(cred->gid, tcred->egid) &&
  216. gid_eq(cred->gid, tcred->sgid) &&
  217. gid_eq(cred->gid, tcred->gid))
  218. goto ok;
  219. if (ptrace_has_cap(tcred->user_ns, mode))
  220. goto ok;
  221. rcu_read_unlock();
  222. return -EPERM;
  223. ok:
  224. rcu_read_unlock();
  225. smp_rmb();
  226. if (task->mm)
  227. dumpable = get_dumpable(task->mm);
  228. rcu_read_lock();
  229. if (dumpable != SUID_DUMP_USER &&
  230. !ptrace_has_cap(__task_cred(task)->user_ns, mode)) {
  231. rcu_read_unlock();
  232. return -EPERM;
  233. }
  234. rcu_read_unlock();
  235. return security_ptrace_access_check(task, mode);
  236. }
  237. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  238. {
  239. int err;
  240. task_lock(task);
  241. err = __ptrace_may_access(task, mode);
  242. task_unlock(task);
  243. return !err;
  244. }
  245. static int ptrace_attach(struct task_struct *task, long request,
  246. unsigned long addr,
  247. unsigned long flags)
  248. {
  249. bool seize = (request == PTRACE_SEIZE);
  250. int retval;
  251. retval = -EIO;
  252. if (seize) {
  253. if (addr != 0)
  254. goto out;
  255. if (flags & ~(unsigned long)PTRACE_O_MASK)
  256. goto out;
  257. flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
  258. } else {
  259. flags = PT_PTRACED;
  260. }
  261. audit_ptrace(task);
  262. retval = -EPERM;
  263. if (unlikely(task->flags & PF_KTHREAD))
  264. goto out;
  265. if (same_thread_group(task, current))
  266. goto out;
  267. /*
  268. * Protect exec's credential calculations against our interference;
  269. * SUID, SGID and LSM creds get determined differently
  270. * under ptrace.
  271. */
  272. retval = -ERESTARTNOINTR;
  273. if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
  274. goto out;
  275. task_lock(task);
  276. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
  277. task_unlock(task);
  278. if (retval)
  279. goto unlock_creds;
  280. write_lock_irq(&tasklist_lock);
  281. retval = -EPERM;
  282. if (unlikely(task->exit_state))
  283. goto unlock_tasklist;
  284. if (task->ptrace)
  285. goto unlock_tasklist;
  286. if (seize)
  287. flags |= PT_SEIZED;
  288. rcu_read_lock();
  289. if (ns_capable(__task_cred(task)->user_ns, CAP_SYS_PTRACE))
  290. flags |= PT_PTRACE_CAP;
  291. rcu_read_unlock();
  292. task->ptrace = flags;
  293. __ptrace_link(task, current);
  294. /* SEIZE doesn't trap tracee on attach */
  295. if (!seize)
  296. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  297. spin_lock(&task->sighand->siglock);
  298. /*
  299. * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
  300. * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
  301. * will be cleared if the child completes the transition or any
  302. * event which clears the group stop states happens. We'll wait
  303. * for the transition to complete before returning from this
  304. * function.
  305. *
  306. * This hides STOPPED -> RUNNING -> TRACED transition from the
  307. * attaching thread but a different thread in the same group can
  308. * still observe the transient RUNNING state. IOW, if another
  309. * thread's WNOHANG wait(2) on the stopped tracee races against
  310. * ATTACH, the wait(2) may fail due to the transient RUNNING.
  311. *
  312. * The following task_is_stopped() test is safe as both transitions
  313. * in and out of STOPPED are protected by siglock.
  314. */
  315. if (task_is_stopped(task) &&
  316. task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
  317. signal_wake_up_state(task, __TASK_STOPPED);
  318. spin_unlock(&task->sighand->siglock);
  319. retval = 0;
  320. unlock_tasklist:
  321. write_unlock_irq(&tasklist_lock);
  322. unlock_creds:
  323. mutex_unlock(&task->signal->cred_guard_mutex);
  324. out:
  325. if (!retval) {
  326. wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
  327. TASK_UNINTERRUPTIBLE);
  328. proc_ptrace_connector(task, PTRACE_ATTACH);
  329. }
  330. return retval;
  331. }
  332. /**
  333. * ptrace_traceme -- helper for PTRACE_TRACEME
  334. *
  335. * Performs checks and sets PT_PTRACED.
  336. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  337. */
  338. static int ptrace_traceme(void)
  339. {
  340. int ret = -EPERM;
  341. write_lock_irq(&tasklist_lock);
  342. /* Are we already being traced? */
  343. if (!current->ptrace) {
  344. ret = security_ptrace_traceme(current->parent);
  345. /*
  346. * Check PF_EXITING to ensure ->real_parent has not passed
  347. * exit_ptrace(). Otherwise we don't report the error but
  348. * pretend ->real_parent untraces us right after return.
  349. */
  350. if (!ret && !(current->real_parent->flags & PF_EXITING)) {
  351. current->ptrace = PT_PTRACED;
  352. __ptrace_link(current, current->real_parent);
  353. }
  354. }
  355. write_unlock_irq(&tasklist_lock);
  356. return ret;
  357. }
  358. /*
  359. * Called with irqs disabled, returns true if childs should reap themselves.
  360. */
  361. static int ignoring_children(struct sighand_struct *sigh)
  362. {
  363. int ret;
  364. spin_lock(&sigh->siglock);
  365. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  366. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  367. spin_unlock(&sigh->siglock);
  368. return ret;
  369. }
  370. /*
  371. * Called with tasklist_lock held for writing.
  372. * Unlink a traced task, and clean it up if it was a traced zombie.
  373. * Return true if it needs to be reaped with release_task().
  374. * (We can't call release_task() here because we already hold tasklist_lock.)
  375. *
  376. * If it's a zombie, our attachedness prevented normal parent notification
  377. * or self-reaping. Do notification now if it would have happened earlier.
  378. * If it should reap itself, return true.
  379. *
  380. * If it's our own child, there is no notification to do. But if our normal
  381. * children self-reap, then this child was prevented by ptrace and we must
  382. * reap it now, in that case we must also wake up sub-threads sleeping in
  383. * do_wait().
  384. */
  385. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  386. {
  387. bool dead;
  388. __ptrace_unlink(p);
  389. if (p->exit_state != EXIT_ZOMBIE)
  390. return false;
  391. dead = !thread_group_leader(p);
  392. if (!dead && thread_group_empty(p)) {
  393. if (!same_thread_group(p->real_parent, tracer))
  394. dead = do_notify_parent(p, p->exit_signal);
  395. else if (ignoring_children(tracer->sighand)) {
  396. __wake_up_parent(p, tracer);
  397. dead = true;
  398. }
  399. }
  400. /* Mark it as in the process of being reaped. */
  401. if (dead)
  402. p->exit_state = EXIT_DEAD;
  403. return dead;
  404. }
  405. static int ptrace_detach(struct task_struct *child, unsigned int data)
  406. {
  407. if (!valid_signal(data))
  408. return -EIO;
  409. /* Architecture-specific hardware disable .. */
  410. ptrace_disable(child);
  411. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  412. write_lock_irq(&tasklist_lock);
  413. /*
  414. * We rely on ptrace_freeze_traced(). It can't be killed and
  415. * untraced by another thread, it can't be a zombie.
  416. */
  417. WARN_ON(!child->ptrace || child->exit_state);
  418. /*
  419. * tasklist_lock avoids the race with wait_task_stopped(), see
  420. * the comment in ptrace_resume().
  421. */
  422. child->exit_code = data;
  423. __ptrace_detach(current, child);
  424. write_unlock_irq(&tasklist_lock);
  425. proc_ptrace_connector(child, PTRACE_DETACH);
  426. return 0;
  427. }
  428. /*
  429. * Detach all tasks we were using ptrace on. Called with tasklist held
  430. * for writing.
  431. */
  432. void exit_ptrace(struct task_struct *tracer, struct list_head *dead)
  433. {
  434. struct task_struct *p, *n;
  435. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  436. if (unlikely(p->ptrace & PT_EXITKILL))
  437. send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
  438. if (__ptrace_detach(tracer, p))
  439. list_add(&p->ptrace_entry, dead);
  440. }
  441. }
  442. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  443. {
  444. int copied = 0;
  445. while (len > 0) {
  446. char buf[128];
  447. int this_len, retval;
  448. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  449. retval = access_process_vm(tsk, src, buf, this_len, 0);
  450. if (!retval) {
  451. if (copied)
  452. break;
  453. return -EIO;
  454. }
  455. if (copy_to_user(dst, buf, retval))
  456. return -EFAULT;
  457. copied += retval;
  458. src += retval;
  459. dst += retval;
  460. len -= retval;
  461. }
  462. return copied;
  463. }
  464. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  465. {
  466. int copied = 0;
  467. while (len > 0) {
  468. char buf[128];
  469. int this_len, retval;
  470. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  471. if (copy_from_user(buf, src, this_len))
  472. return -EFAULT;
  473. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  474. if (!retval) {
  475. if (copied)
  476. break;
  477. return -EIO;
  478. }
  479. copied += retval;
  480. src += retval;
  481. dst += retval;
  482. len -= retval;
  483. }
  484. return copied;
  485. }
  486. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  487. {
  488. unsigned flags;
  489. if (data & ~(unsigned long)PTRACE_O_MASK)
  490. return -EINVAL;
  491. /* Avoid intermediate state when all opts are cleared */
  492. flags = child->ptrace;
  493. flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
  494. flags |= (data << PT_OPT_FLAG_SHIFT);
  495. child->ptrace = flags;
  496. return 0;
  497. }
  498. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  499. {
  500. unsigned long flags;
  501. int error = -ESRCH;
  502. if (lock_task_sighand(child, &flags)) {
  503. error = -EINVAL;
  504. if (likely(child->last_siginfo != NULL)) {
  505. *info = *child->last_siginfo;
  506. error = 0;
  507. }
  508. unlock_task_sighand(child, &flags);
  509. }
  510. return error;
  511. }
  512. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  513. {
  514. unsigned long flags;
  515. int error = -ESRCH;
  516. if (lock_task_sighand(child, &flags)) {
  517. error = -EINVAL;
  518. if (likely(child->last_siginfo != NULL)) {
  519. *child->last_siginfo = *info;
  520. error = 0;
  521. }
  522. unlock_task_sighand(child, &flags);
  523. }
  524. return error;
  525. }
  526. static int ptrace_peek_siginfo(struct task_struct *child,
  527. unsigned long addr,
  528. unsigned long data)
  529. {
  530. struct ptrace_peeksiginfo_args arg;
  531. struct sigpending *pending;
  532. struct sigqueue *q;
  533. int ret, i;
  534. ret = copy_from_user(&arg, (void __user *) addr,
  535. sizeof(struct ptrace_peeksiginfo_args));
  536. if (ret)
  537. return -EFAULT;
  538. if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
  539. return -EINVAL; /* unknown flags */
  540. if (arg.nr < 0)
  541. return -EINVAL;
  542. if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
  543. pending = &child->signal->shared_pending;
  544. else
  545. pending = &child->pending;
  546. for (i = 0; i < arg.nr; ) {
  547. siginfo_t info;
  548. s32 off = arg.off + i;
  549. spin_lock_irq(&child->sighand->siglock);
  550. list_for_each_entry(q, &pending->list, list) {
  551. if (!off--) {
  552. copy_siginfo(&info, &q->info);
  553. break;
  554. }
  555. }
  556. spin_unlock_irq(&child->sighand->siglock);
  557. if (off >= 0) /* beyond the end of the list */
  558. break;
  559. #ifdef CONFIG_COMPAT
  560. if (unlikely(is_compat_task())) {
  561. compat_siginfo_t __user *uinfo = compat_ptr(data);
  562. if (copy_siginfo_to_user32(uinfo, &info) ||
  563. __put_user(info.si_code, &uinfo->si_code)) {
  564. ret = -EFAULT;
  565. break;
  566. }
  567. } else
  568. #endif
  569. {
  570. siginfo_t __user *uinfo = (siginfo_t __user *) data;
  571. if (copy_siginfo_to_user(uinfo, &info) ||
  572. __put_user(info.si_code, &uinfo->si_code)) {
  573. ret = -EFAULT;
  574. break;
  575. }
  576. }
  577. data += sizeof(siginfo_t);
  578. i++;
  579. if (signal_pending(current))
  580. break;
  581. cond_resched();
  582. }
  583. if (i > 0)
  584. return i;
  585. return ret;
  586. }
  587. #ifdef PTRACE_SINGLESTEP
  588. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  589. #else
  590. #define is_singlestep(request) 0
  591. #endif
  592. #ifdef PTRACE_SINGLEBLOCK
  593. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  594. #else
  595. #define is_singleblock(request) 0
  596. #endif
  597. #ifdef PTRACE_SYSEMU
  598. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  599. #else
  600. #define is_sysemu_singlestep(request) 0
  601. #endif
  602. static int ptrace_resume(struct task_struct *child, long request,
  603. unsigned long data)
  604. {
  605. bool need_siglock;
  606. if (!valid_signal(data))
  607. return -EIO;
  608. if (request == PTRACE_SYSCALL)
  609. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  610. else
  611. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  612. #ifdef TIF_SYSCALL_EMU
  613. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  614. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  615. else
  616. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  617. #endif
  618. if (is_singleblock(request)) {
  619. if (unlikely(!arch_has_block_step()))
  620. return -EIO;
  621. user_enable_block_step(child);
  622. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  623. if (unlikely(!arch_has_single_step()))
  624. return -EIO;
  625. user_enable_single_step(child);
  626. } else {
  627. user_disable_single_step(child);
  628. }
  629. /*
  630. * Change ->exit_code and ->state under siglock to avoid the race
  631. * with wait_task_stopped() in between; a non-zero ->exit_code will
  632. * wrongly look like another report from tracee.
  633. *
  634. * Note that we need siglock even if ->exit_code == data and/or this
  635. * status was not reported yet, the new status must not be cleared by
  636. * wait_task_stopped() after resume.
  637. *
  638. * If data == 0 we do not care if wait_task_stopped() reports the old
  639. * status and clears the code too; this can't race with the tracee, it
  640. * takes siglock after resume.
  641. */
  642. need_siglock = data && !thread_group_empty(current);
  643. if (need_siglock)
  644. spin_lock_irq(&child->sighand->siglock);
  645. child->exit_code = data;
  646. wake_up_state(child, __TASK_TRACED);
  647. if (need_siglock)
  648. spin_unlock_irq(&child->sighand->siglock);
  649. return 0;
  650. }
  651. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  652. static const struct user_regset *
  653. find_regset(const struct user_regset_view *view, unsigned int type)
  654. {
  655. const struct user_regset *regset;
  656. int n;
  657. for (n = 0; n < view->n; ++n) {
  658. regset = view->regsets + n;
  659. if (regset->core_note_type == type)
  660. return regset;
  661. }
  662. return NULL;
  663. }
  664. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  665. struct iovec *kiov)
  666. {
  667. const struct user_regset_view *view = task_user_regset_view(task);
  668. const struct user_regset *regset = find_regset(view, type);
  669. int regset_no;
  670. if (!regset || (kiov->iov_len % regset->size) != 0)
  671. return -EINVAL;
  672. regset_no = regset - view->regsets;
  673. kiov->iov_len = min(kiov->iov_len,
  674. (__kernel_size_t) (regset->n * regset->size));
  675. if (req == PTRACE_GETREGSET)
  676. return copy_regset_to_user(task, view, regset_no, 0,
  677. kiov->iov_len, kiov->iov_base);
  678. else
  679. return copy_regset_from_user(task, view, regset_no, 0,
  680. kiov->iov_len, kiov->iov_base);
  681. }
  682. /*
  683. * This is declared in linux/regset.h and defined in machine-dependent
  684. * code. We put the export here, near the primary machine-neutral use,
  685. * to ensure no machine forgets it.
  686. */
  687. EXPORT_SYMBOL_GPL(task_user_regset_view);
  688. #endif
  689. int ptrace_request(struct task_struct *child, long request,
  690. unsigned long addr, unsigned long data)
  691. {
  692. bool seized = child->ptrace & PT_SEIZED;
  693. int ret = -EIO;
  694. siginfo_t siginfo, *si;
  695. void __user *datavp = (void __user *) data;
  696. unsigned long __user *datalp = datavp;
  697. unsigned long flags;
  698. switch (request) {
  699. case PTRACE_PEEKTEXT:
  700. case PTRACE_PEEKDATA:
  701. return generic_ptrace_peekdata(child, addr, data);
  702. case PTRACE_POKETEXT:
  703. case PTRACE_POKEDATA:
  704. return generic_ptrace_pokedata(child, addr, data);
  705. #ifdef PTRACE_OLDSETOPTIONS
  706. case PTRACE_OLDSETOPTIONS:
  707. #endif
  708. case PTRACE_SETOPTIONS:
  709. ret = ptrace_setoptions(child, data);
  710. break;
  711. case PTRACE_GETEVENTMSG:
  712. ret = put_user(child->ptrace_message, datalp);
  713. break;
  714. case PTRACE_PEEKSIGINFO:
  715. ret = ptrace_peek_siginfo(child, addr, data);
  716. break;
  717. case PTRACE_GETSIGINFO:
  718. ret = ptrace_getsiginfo(child, &siginfo);
  719. if (!ret)
  720. ret = copy_siginfo_to_user(datavp, &siginfo);
  721. break;
  722. case PTRACE_SETSIGINFO:
  723. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  724. ret = -EFAULT;
  725. else
  726. ret = ptrace_setsiginfo(child, &siginfo);
  727. break;
  728. case PTRACE_GETSIGMASK:
  729. if (addr != sizeof(sigset_t)) {
  730. ret = -EINVAL;
  731. break;
  732. }
  733. if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
  734. ret = -EFAULT;
  735. else
  736. ret = 0;
  737. break;
  738. case PTRACE_SETSIGMASK: {
  739. sigset_t new_set;
  740. if (addr != sizeof(sigset_t)) {
  741. ret = -EINVAL;
  742. break;
  743. }
  744. if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
  745. ret = -EFAULT;
  746. break;
  747. }
  748. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  749. /*
  750. * Every thread does recalc_sigpending() after resume, so
  751. * retarget_shared_pending() and recalc_sigpending() are not
  752. * called here.
  753. */
  754. spin_lock_irq(&child->sighand->siglock);
  755. child->blocked = new_set;
  756. spin_unlock_irq(&child->sighand->siglock);
  757. ret = 0;
  758. break;
  759. }
  760. case PTRACE_INTERRUPT:
  761. /*
  762. * Stop tracee without any side-effect on signal or job
  763. * control. At least one trap is guaranteed to happen
  764. * after this request. If @child is already trapped, the
  765. * current trap is not disturbed and another trap will
  766. * happen after the current trap is ended with PTRACE_CONT.
  767. *
  768. * The actual trap might not be PTRACE_EVENT_STOP trap but
  769. * the pending condition is cleared regardless.
  770. */
  771. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  772. break;
  773. /*
  774. * INTERRUPT doesn't disturb existing trap sans one
  775. * exception. If ptracer issued LISTEN for the current
  776. * STOP, this INTERRUPT should clear LISTEN and re-trap
  777. * tracee into STOP.
  778. */
  779. if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
  780. ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
  781. unlock_task_sighand(child, &flags);
  782. ret = 0;
  783. break;
  784. case PTRACE_LISTEN:
  785. /*
  786. * Listen for events. Tracee must be in STOP. It's not
  787. * resumed per-se but is not considered to be in TRACED by
  788. * wait(2) or ptrace(2). If an async event (e.g. group
  789. * stop state change) happens, tracee will enter STOP trap
  790. * again. Alternatively, ptracer can issue INTERRUPT to
  791. * finish listening and re-trap tracee into STOP.
  792. */
  793. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  794. break;
  795. si = child->last_siginfo;
  796. if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
  797. child->jobctl |= JOBCTL_LISTENING;
  798. /*
  799. * If NOTIFY is set, it means event happened between
  800. * start of this trap and now. Trigger re-trap.
  801. */
  802. if (child->jobctl & JOBCTL_TRAP_NOTIFY)
  803. ptrace_signal_wake_up(child, true);
  804. ret = 0;
  805. }
  806. unlock_task_sighand(child, &flags);
  807. break;
  808. case PTRACE_DETACH: /* detach a process that was attached. */
  809. ret = ptrace_detach(child, data);
  810. break;
  811. #ifdef CONFIG_BINFMT_ELF_FDPIC
  812. case PTRACE_GETFDPIC: {
  813. struct mm_struct *mm = get_task_mm(child);
  814. unsigned long tmp = 0;
  815. ret = -ESRCH;
  816. if (!mm)
  817. break;
  818. switch (addr) {
  819. case PTRACE_GETFDPIC_EXEC:
  820. tmp = mm->context.exec_fdpic_loadmap;
  821. break;
  822. case PTRACE_GETFDPIC_INTERP:
  823. tmp = mm->context.interp_fdpic_loadmap;
  824. break;
  825. default:
  826. break;
  827. }
  828. mmput(mm);
  829. ret = put_user(tmp, datalp);
  830. break;
  831. }
  832. #endif
  833. #ifdef PTRACE_SINGLESTEP
  834. case PTRACE_SINGLESTEP:
  835. #endif
  836. #ifdef PTRACE_SINGLEBLOCK
  837. case PTRACE_SINGLEBLOCK:
  838. #endif
  839. #ifdef PTRACE_SYSEMU
  840. case PTRACE_SYSEMU:
  841. case PTRACE_SYSEMU_SINGLESTEP:
  842. #endif
  843. case PTRACE_SYSCALL:
  844. case PTRACE_CONT:
  845. return ptrace_resume(child, request, data);
  846. case PTRACE_KILL:
  847. if (child->exit_state) /* already dead */
  848. return 0;
  849. return ptrace_resume(child, request, SIGKILL);
  850. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  851. case PTRACE_GETREGSET:
  852. case PTRACE_SETREGSET: {
  853. struct iovec kiov;
  854. struct iovec __user *uiov = datavp;
  855. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  856. return -EFAULT;
  857. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  858. __get_user(kiov.iov_len, &uiov->iov_len))
  859. return -EFAULT;
  860. ret = ptrace_regset(child, request, addr, &kiov);
  861. if (!ret)
  862. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  863. break;
  864. }
  865. #endif
  866. default:
  867. break;
  868. }
  869. return ret;
  870. }
  871. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  872. {
  873. struct task_struct *child;
  874. rcu_read_lock();
  875. child = find_task_by_vpid(pid);
  876. if (child)
  877. get_task_struct(child);
  878. rcu_read_unlock();
  879. if (!child)
  880. return ERR_PTR(-ESRCH);
  881. return child;
  882. }
  883. #ifndef arch_ptrace_attach
  884. #define arch_ptrace_attach(child) do { } while (0)
  885. #endif
  886. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  887. unsigned long, data)
  888. {
  889. struct task_struct *child;
  890. long ret;
  891. if (request == PTRACE_TRACEME) {
  892. ret = ptrace_traceme();
  893. if (!ret)
  894. arch_ptrace_attach(current);
  895. goto out;
  896. }
  897. child = ptrace_get_task_struct(pid);
  898. if (IS_ERR(child)) {
  899. ret = PTR_ERR(child);
  900. goto out;
  901. }
  902. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  903. ret = ptrace_attach(child, request, addr, data);
  904. /*
  905. * Some architectures need to do book-keeping after
  906. * a ptrace attach.
  907. */
  908. if (!ret)
  909. arch_ptrace_attach(child);
  910. goto out_put_task_struct;
  911. }
  912. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  913. request == PTRACE_INTERRUPT);
  914. if (ret < 0)
  915. goto out_put_task_struct;
  916. ret = arch_ptrace(child, request, addr, data);
  917. if (ret || request != PTRACE_DETACH)
  918. ptrace_unfreeze_traced(child);
  919. out_put_task_struct:
  920. put_task_struct(child);
  921. out:
  922. return ret;
  923. }
  924. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  925. unsigned long data)
  926. {
  927. unsigned long tmp;
  928. int copied;
  929. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  930. if (copied != sizeof(tmp))
  931. return -EIO;
  932. return put_user(tmp, (unsigned long __user *)data);
  933. }
  934. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  935. unsigned long data)
  936. {
  937. int copied;
  938. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  939. return (copied == sizeof(data)) ? 0 : -EIO;
  940. }
  941. #if defined CONFIG_COMPAT
  942. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  943. compat_ulong_t addr, compat_ulong_t data)
  944. {
  945. compat_ulong_t __user *datap = compat_ptr(data);
  946. compat_ulong_t word;
  947. siginfo_t siginfo;
  948. int ret;
  949. switch (request) {
  950. case PTRACE_PEEKTEXT:
  951. case PTRACE_PEEKDATA:
  952. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  953. if (ret != sizeof(word))
  954. ret = -EIO;
  955. else
  956. ret = put_user(word, datap);
  957. break;
  958. case PTRACE_POKETEXT:
  959. case PTRACE_POKEDATA:
  960. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  961. ret = (ret != sizeof(data) ? -EIO : 0);
  962. break;
  963. case PTRACE_GETEVENTMSG:
  964. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  965. break;
  966. case PTRACE_GETSIGINFO:
  967. ret = ptrace_getsiginfo(child, &siginfo);
  968. if (!ret)
  969. ret = copy_siginfo_to_user32(
  970. (struct compat_siginfo __user *) datap,
  971. &siginfo);
  972. break;
  973. case PTRACE_SETSIGINFO:
  974. memset(&siginfo, 0, sizeof siginfo);
  975. if (copy_siginfo_from_user32(
  976. &siginfo, (struct compat_siginfo __user *) datap))
  977. ret = -EFAULT;
  978. else
  979. ret = ptrace_setsiginfo(child, &siginfo);
  980. break;
  981. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  982. case PTRACE_GETREGSET:
  983. case PTRACE_SETREGSET:
  984. {
  985. struct iovec kiov;
  986. struct compat_iovec __user *uiov =
  987. (struct compat_iovec __user *) datap;
  988. compat_uptr_t ptr;
  989. compat_size_t len;
  990. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  991. return -EFAULT;
  992. if (__get_user(ptr, &uiov->iov_base) ||
  993. __get_user(len, &uiov->iov_len))
  994. return -EFAULT;
  995. kiov.iov_base = compat_ptr(ptr);
  996. kiov.iov_len = len;
  997. ret = ptrace_regset(child, request, addr, &kiov);
  998. if (!ret)
  999. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  1000. break;
  1001. }
  1002. #endif
  1003. default:
  1004. ret = ptrace_request(child, request, addr, data);
  1005. }
  1006. return ret;
  1007. }
  1008. COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
  1009. compat_long_t, addr, compat_long_t, data)
  1010. {
  1011. struct task_struct *child;
  1012. long ret;
  1013. if (request == PTRACE_TRACEME) {
  1014. ret = ptrace_traceme();
  1015. goto out;
  1016. }
  1017. child = ptrace_get_task_struct(pid);
  1018. if (IS_ERR(child)) {
  1019. ret = PTR_ERR(child);
  1020. goto out;
  1021. }
  1022. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1023. ret = ptrace_attach(child, request, addr, data);
  1024. /*
  1025. * Some architectures need to do book-keeping after
  1026. * a ptrace attach.
  1027. */
  1028. if (!ret)
  1029. arch_ptrace_attach(child);
  1030. goto out_put_task_struct;
  1031. }
  1032. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1033. request == PTRACE_INTERRUPT);
  1034. if (!ret) {
  1035. ret = compat_arch_ptrace(child, request, addr, data);
  1036. if (ret || request != PTRACE_DETACH)
  1037. ptrace_unfreeze_traced(child);
  1038. }
  1039. out_put_task_struct:
  1040. put_task_struct(child);
  1041. out:
  1042. return ret;
  1043. }
  1044. #endif /* CONFIG_COMPAT */