ptrace.c 28 KB

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