ptrace.c 29 KB

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