ptrace.c 27 KB

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