ptrace.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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. flush_ptrace_hw_breakpoint(child);
  418. write_lock_irq(&tasklist_lock);
  419. /*
  420. * This child can be already killed. Make sure de_thread() or
  421. * our sub-thread doing do_wait() didn't do release_task() yet.
  422. */
  423. if (child->ptrace) {
  424. child->exit_code = data;
  425. dead = __ptrace_detach(current, child);
  426. }
  427. write_unlock_irq(&tasklist_lock);
  428. proc_ptrace_connector(child, PTRACE_DETACH);
  429. if (unlikely(dead))
  430. release_task(child);
  431. return 0;
  432. }
  433. /*
  434. * Detach all tasks we were using ptrace on. Called with tasklist held
  435. * for writing, and returns with it held too. But note it can release
  436. * and reacquire the lock.
  437. */
  438. void exit_ptrace(struct task_struct *tracer)
  439. __releases(&tasklist_lock)
  440. __acquires(&tasklist_lock)
  441. {
  442. struct task_struct *p, *n;
  443. LIST_HEAD(ptrace_dead);
  444. if (likely(list_empty(&tracer->ptraced)))
  445. return;
  446. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  447. if (unlikely(p->ptrace & PT_EXITKILL))
  448. send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
  449. if (__ptrace_detach(tracer, p))
  450. list_add(&p->ptrace_entry, &ptrace_dead);
  451. }
  452. write_unlock_irq(&tasklist_lock);
  453. BUG_ON(!list_empty(&tracer->ptraced));
  454. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  455. list_del_init(&p->ptrace_entry);
  456. release_task(p);
  457. }
  458. write_lock_irq(&tasklist_lock);
  459. }
  460. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  461. {
  462. int copied = 0;
  463. while (len > 0) {
  464. char buf[128];
  465. int this_len, retval;
  466. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  467. retval = access_process_vm(tsk, src, buf, this_len, 0);
  468. if (!retval) {
  469. if (copied)
  470. break;
  471. return -EIO;
  472. }
  473. if (copy_to_user(dst, buf, retval))
  474. return -EFAULT;
  475. copied += retval;
  476. src += retval;
  477. dst += retval;
  478. len -= retval;
  479. }
  480. return copied;
  481. }
  482. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  483. {
  484. int copied = 0;
  485. while (len > 0) {
  486. char buf[128];
  487. int this_len, retval;
  488. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  489. if (copy_from_user(buf, src, this_len))
  490. return -EFAULT;
  491. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  492. if (!retval) {
  493. if (copied)
  494. break;
  495. return -EIO;
  496. }
  497. copied += retval;
  498. src += retval;
  499. dst += retval;
  500. len -= retval;
  501. }
  502. return copied;
  503. }
  504. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  505. {
  506. unsigned flags;
  507. if (data & ~(unsigned long)PTRACE_O_MASK)
  508. return -EINVAL;
  509. /* Avoid intermediate state when all opts are cleared */
  510. flags = child->ptrace;
  511. flags &= ~(PTRACE_O_MASK << PT_OPT_FLAG_SHIFT);
  512. flags |= (data << PT_OPT_FLAG_SHIFT);
  513. child->ptrace = flags;
  514. return 0;
  515. }
  516. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  517. {
  518. unsigned long flags;
  519. int error = -ESRCH;
  520. if (lock_task_sighand(child, &flags)) {
  521. error = -EINVAL;
  522. if (likely(child->last_siginfo != NULL)) {
  523. *info = *child->last_siginfo;
  524. error = 0;
  525. }
  526. unlock_task_sighand(child, &flags);
  527. }
  528. return error;
  529. }
  530. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  531. {
  532. unsigned long flags;
  533. int error = -ESRCH;
  534. if (lock_task_sighand(child, &flags)) {
  535. error = -EINVAL;
  536. if (likely(child->last_siginfo != NULL)) {
  537. *child->last_siginfo = *info;
  538. error = 0;
  539. }
  540. unlock_task_sighand(child, &flags);
  541. }
  542. return error;
  543. }
  544. static int ptrace_peek_siginfo(struct task_struct *child,
  545. unsigned long addr,
  546. unsigned long data)
  547. {
  548. struct ptrace_peeksiginfo_args arg;
  549. struct sigpending *pending;
  550. struct sigqueue *q;
  551. int ret, i;
  552. ret = copy_from_user(&arg, (void __user *) addr,
  553. sizeof(struct ptrace_peeksiginfo_args));
  554. if (ret)
  555. return -EFAULT;
  556. if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
  557. return -EINVAL; /* unknown flags */
  558. if (arg.nr < 0)
  559. return -EINVAL;
  560. if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
  561. pending = &child->signal->shared_pending;
  562. else
  563. pending = &child->pending;
  564. for (i = 0; i < arg.nr; ) {
  565. siginfo_t info;
  566. s32 off = arg.off + i;
  567. spin_lock_irq(&child->sighand->siglock);
  568. list_for_each_entry(q, &pending->list, list) {
  569. if (!off--) {
  570. copy_siginfo(&info, &q->info);
  571. break;
  572. }
  573. }
  574. spin_unlock_irq(&child->sighand->siglock);
  575. if (off >= 0) /* beyond the end of the list */
  576. break;
  577. #ifdef CONFIG_COMPAT
  578. if (unlikely(is_compat_task())) {
  579. compat_siginfo_t __user *uinfo = compat_ptr(data);
  580. if (copy_siginfo_to_user32(uinfo, &info) ||
  581. __put_user(info.si_code, &uinfo->si_code)) {
  582. ret = -EFAULT;
  583. break;
  584. }
  585. } else
  586. #endif
  587. {
  588. siginfo_t __user *uinfo = (siginfo_t __user *) data;
  589. if (copy_siginfo_to_user(uinfo, &info) ||
  590. __put_user(info.si_code, &uinfo->si_code)) {
  591. ret = -EFAULT;
  592. break;
  593. }
  594. }
  595. data += sizeof(siginfo_t);
  596. i++;
  597. if (signal_pending(current))
  598. break;
  599. cond_resched();
  600. }
  601. if (i > 0)
  602. return i;
  603. return ret;
  604. }
  605. #ifdef PTRACE_SINGLESTEP
  606. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  607. #else
  608. #define is_singlestep(request) 0
  609. #endif
  610. #ifdef PTRACE_SINGLEBLOCK
  611. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  612. #else
  613. #define is_singleblock(request) 0
  614. #endif
  615. #ifdef PTRACE_SYSEMU
  616. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  617. #else
  618. #define is_sysemu_singlestep(request) 0
  619. #endif
  620. static int ptrace_resume(struct task_struct *child, long request,
  621. unsigned long data)
  622. {
  623. if (!valid_signal(data))
  624. return -EIO;
  625. if (request == PTRACE_SYSCALL)
  626. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  627. else
  628. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  629. #ifdef TIF_SYSCALL_EMU
  630. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  631. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  632. else
  633. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  634. #endif
  635. if (is_singleblock(request)) {
  636. if (unlikely(!arch_has_block_step()))
  637. return -EIO;
  638. user_enable_block_step(child);
  639. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  640. if (unlikely(!arch_has_single_step()))
  641. return -EIO;
  642. user_enable_single_step(child);
  643. } else {
  644. user_disable_single_step(child);
  645. }
  646. child->exit_code = data;
  647. wake_up_state(child, __TASK_TRACED);
  648. return 0;
  649. }
  650. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  651. static const struct user_regset *
  652. find_regset(const struct user_regset_view *view, unsigned int type)
  653. {
  654. const struct user_regset *regset;
  655. int n;
  656. for (n = 0; n < view->n; ++n) {
  657. regset = view->regsets + n;
  658. if (regset->core_note_type == type)
  659. return regset;
  660. }
  661. return NULL;
  662. }
  663. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  664. struct iovec *kiov)
  665. {
  666. const struct user_regset_view *view = task_user_regset_view(task);
  667. const struct user_regset *regset = find_regset(view, type);
  668. int regset_no;
  669. if (!regset || (kiov->iov_len % regset->size) != 0)
  670. return -EINVAL;
  671. regset_no = regset - view->regsets;
  672. kiov->iov_len = min(kiov->iov_len,
  673. (__kernel_size_t) (regset->n * regset->size));
  674. if (req == PTRACE_GETREGSET)
  675. return copy_regset_to_user(task, view, regset_no, 0,
  676. kiov->iov_len, kiov->iov_base);
  677. else
  678. return copy_regset_from_user(task, view, regset_no, 0,
  679. kiov->iov_len, kiov->iov_base);
  680. }
  681. /*
  682. * This is declared in linux/regset.h and defined in machine-dependent
  683. * code. We put the export here, near the primary machine-neutral use,
  684. * to ensure no machine forgets it.
  685. */
  686. EXPORT_SYMBOL_GPL(task_user_regset_view);
  687. #endif
  688. int ptrace_request(struct task_struct *child, long request,
  689. unsigned long addr, unsigned long data)
  690. {
  691. bool seized = child->ptrace & PT_SEIZED;
  692. int ret = -EIO;
  693. siginfo_t siginfo, *si;
  694. void __user *datavp = (void __user *) data;
  695. unsigned long __user *datalp = datavp;
  696. unsigned long flags;
  697. switch (request) {
  698. case PTRACE_PEEKTEXT:
  699. case PTRACE_PEEKDATA:
  700. return generic_ptrace_peekdata(child, addr, data);
  701. case PTRACE_POKETEXT:
  702. case PTRACE_POKEDATA:
  703. return generic_ptrace_pokedata(child, addr, data);
  704. #ifdef PTRACE_OLDSETOPTIONS
  705. case PTRACE_OLDSETOPTIONS:
  706. #endif
  707. case PTRACE_SETOPTIONS:
  708. ret = ptrace_setoptions(child, data);
  709. break;
  710. case PTRACE_GETEVENTMSG:
  711. ret = put_user(child->ptrace_message, datalp);
  712. break;
  713. case PTRACE_PEEKSIGINFO:
  714. ret = ptrace_peek_siginfo(child, addr, data);
  715. break;
  716. case PTRACE_GETSIGINFO:
  717. ret = ptrace_getsiginfo(child, &siginfo);
  718. if (!ret)
  719. ret = copy_siginfo_to_user(datavp, &siginfo);
  720. break;
  721. case PTRACE_SETSIGINFO:
  722. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  723. ret = -EFAULT;
  724. else
  725. ret = ptrace_setsiginfo(child, &siginfo);
  726. break;
  727. case PTRACE_GETSIGMASK:
  728. if (addr != sizeof(sigset_t)) {
  729. ret = -EINVAL;
  730. break;
  731. }
  732. if (copy_to_user(datavp, &child->blocked, sizeof(sigset_t)))
  733. ret = -EFAULT;
  734. else
  735. ret = 0;
  736. break;
  737. case PTRACE_SETSIGMASK: {
  738. sigset_t new_set;
  739. if (addr != sizeof(sigset_t)) {
  740. ret = -EINVAL;
  741. break;
  742. }
  743. if (copy_from_user(&new_set, datavp, sizeof(sigset_t))) {
  744. ret = -EFAULT;
  745. break;
  746. }
  747. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  748. /*
  749. * Every thread does recalc_sigpending() after resume, so
  750. * retarget_shared_pending() and recalc_sigpending() are not
  751. * called here.
  752. */
  753. spin_lock_irq(&child->sighand->siglock);
  754. child->blocked = new_set;
  755. spin_unlock_irq(&child->sighand->siglock);
  756. ret = 0;
  757. break;
  758. }
  759. case PTRACE_INTERRUPT:
  760. /*
  761. * Stop tracee without any side-effect on signal or job
  762. * control. At least one trap is guaranteed to happen
  763. * after this request. If @child is already trapped, the
  764. * current trap is not disturbed and another trap will
  765. * happen after the current trap is ended with PTRACE_CONT.
  766. *
  767. * The actual trap might not be PTRACE_EVENT_STOP trap but
  768. * the pending condition is cleared regardless.
  769. */
  770. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  771. break;
  772. /*
  773. * INTERRUPT doesn't disturb existing trap sans one
  774. * exception. If ptracer issued LISTEN for the current
  775. * STOP, this INTERRUPT should clear LISTEN and re-trap
  776. * tracee into STOP.
  777. */
  778. if (likely(task_set_jobctl_pending(child, JOBCTL_TRAP_STOP)))
  779. ptrace_signal_wake_up(child, child->jobctl & JOBCTL_LISTENING);
  780. unlock_task_sighand(child, &flags);
  781. ret = 0;
  782. break;
  783. case PTRACE_LISTEN:
  784. /*
  785. * Listen for events. Tracee must be in STOP. It's not
  786. * resumed per-se but is not considered to be in TRACED by
  787. * wait(2) or ptrace(2). If an async event (e.g. group
  788. * stop state change) happens, tracee will enter STOP trap
  789. * again. Alternatively, ptracer can issue INTERRUPT to
  790. * finish listening and re-trap tracee into STOP.
  791. */
  792. if (unlikely(!seized || !lock_task_sighand(child, &flags)))
  793. break;
  794. si = child->last_siginfo;
  795. if (likely(si && (si->si_code >> 8) == PTRACE_EVENT_STOP)) {
  796. child->jobctl |= JOBCTL_LISTENING;
  797. /*
  798. * If NOTIFY is set, it means event happened between
  799. * start of this trap and now. Trigger re-trap.
  800. */
  801. if (child->jobctl & JOBCTL_TRAP_NOTIFY)
  802. ptrace_signal_wake_up(child, true);
  803. ret = 0;
  804. }
  805. unlock_task_sighand(child, &flags);
  806. break;
  807. case PTRACE_DETACH: /* detach a process that was attached. */
  808. ret = ptrace_detach(child, data);
  809. break;
  810. #ifdef CONFIG_BINFMT_ELF_FDPIC
  811. case PTRACE_GETFDPIC: {
  812. struct mm_struct *mm = get_task_mm(child);
  813. unsigned long tmp = 0;
  814. ret = -ESRCH;
  815. if (!mm)
  816. break;
  817. switch (addr) {
  818. case PTRACE_GETFDPIC_EXEC:
  819. tmp = mm->context.exec_fdpic_loadmap;
  820. break;
  821. case PTRACE_GETFDPIC_INTERP:
  822. tmp = mm->context.interp_fdpic_loadmap;
  823. break;
  824. default:
  825. break;
  826. }
  827. mmput(mm);
  828. ret = put_user(tmp, datalp);
  829. break;
  830. }
  831. #endif
  832. #ifdef PTRACE_SINGLESTEP
  833. case PTRACE_SINGLESTEP:
  834. #endif
  835. #ifdef PTRACE_SINGLEBLOCK
  836. case PTRACE_SINGLEBLOCK:
  837. #endif
  838. #ifdef PTRACE_SYSEMU
  839. case PTRACE_SYSEMU:
  840. case PTRACE_SYSEMU_SINGLESTEP:
  841. #endif
  842. case PTRACE_SYSCALL:
  843. case PTRACE_CONT:
  844. return ptrace_resume(child, request, data);
  845. case PTRACE_KILL:
  846. if (child->exit_state) /* already dead */
  847. return 0;
  848. return ptrace_resume(child, request, SIGKILL);
  849. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  850. case PTRACE_GETREGSET:
  851. case PTRACE_SETREGSET: {
  852. struct iovec kiov;
  853. struct iovec __user *uiov = datavp;
  854. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  855. return -EFAULT;
  856. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  857. __get_user(kiov.iov_len, &uiov->iov_len))
  858. return -EFAULT;
  859. ret = ptrace_regset(child, request, addr, &kiov);
  860. if (!ret)
  861. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  862. break;
  863. }
  864. #endif
  865. default:
  866. break;
  867. }
  868. return ret;
  869. }
  870. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  871. {
  872. struct task_struct *child;
  873. rcu_read_lock();
  874. child = find_task_by_vpid(pid);
  875. if (child)
  876. get_task_struct(child);
  877. rcu_read_unlock();
  878. if (!child)
  879. return ERR_PTR(-ESRCH);
  880. return child;
  881. }
  882. #ifndef arch_ptrace_attach
  883. #define arch_ptrace_attach(child) do { } while (0)
  884. #endif
  885. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  886. unsigned long, data)
  887. {
  888. struct task_struct *child;
  889. long ret;
  890. if (request == PTRACE_TRACEME) {
  891. ret = ptrace_traceme();
  892. if (!ret)
  893. arch_ptrace_attach(current);
  894. goto out;
  895. }
  896. child = ptrace_get_task_struct(pid);
  897. if (IS_ERR(child)) {
  898. ret = PTR_ERR(child);
  899. goto out;
  900. }
  901. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  902. ret = ptrace_attach(child, request, addr, data);
  903. /*
  904. * Some architectures need to do book-keeping after
  905. * a ptrace attach.
  906. */
  907. if (!ret)
  908. arch_ptrace_attach(child);
  909. goto out_put_task_struct;
  910. }
  911. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  912. request == PTRACE_INTERRUPT);
  913. if (ret < 0)
  914. goto out_put_task_struct;
  915. ret = arch_ptrace(child, request, addr, data);
  916. if (ret || request != PTRACE_DETACH)
  917. ptrace_unfreeze_traced(child);
  918. out_put_task_struct:
  919. put_task_struct(child);
  920. out:
  921. return ret;
  922. }
  923. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  924. unsigned long data)
  925. {
  926. unsigned long tmp;
  927. int copied;
  928. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  929. if (copied != sizeof(tmp))
  930. return -EIO;
  931. return put_user(tmp, (unsigned long __user *)data);
  932. }
  933. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  934. unsigned long data)
  935. {
  936. int copied;
  937. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  938. return (copied == sizeof(data)) ? 0 : -EIO;
  939. }
  940. #if defined CONFIG_COMPAT
  941. #include <linux/compat.h>
  942. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  943. compat_ulong_t addr, compat_ulong_t data)
  944. {
  945. compat_ulong_t __user *datap = compat_ptr(data);
  946. compat_ulong_t word;
  947. siginfo_t siginfo;
  948. int ret;
  949. switch (request) {
  950. case PTRACE_PEEKTEXT:
  951. case PTRACE_PEEKDATA:
  952. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  953. if (ret != sizeof(word))
  954. ret = -EIO;
  955. else
  956. ret = put_user(word, datap);
  957. break;
  958. case PTRACE_POKETEXT:
  959. case PTRACE_POKEDATA:
  960. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  961. ret = (ret != sizeof(data) ? -EIO : 0);
  962. break;
  963. case PTRACE_GETEVENTMSG:
  964. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  965. break;
  966. case PTRACE_GETSIGINFO:
  967. ret = ptrace_getsiginfo(child, &siginfo);
  968. if (!ret)
  969. ret = copy_siginfo_to_user32(
  970. (struct compat_siginfo __user *) datap,
  971. &siginfo);
  972. break;
  973. case PTRACE_SETSIGINFO:
  974. memset(&siginfo, 0, sizeof siginfo);
  975. if (copy_siginfo_from_user32(
  976. &siginfo, (struct compat_siginfo __user *) datap))
  977. ret = -EFAULT;
  978. else
  979. ret = ptrace_setsiginfo(child, &siginfo);
  980. break;
  981. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  982. case PTRACE_GETREGSET:
  983. case PTRACE_SETREGSET:
  984. {
  985. struct iovec kiov;
  986. struct compat_iovec __user *uiov =
  987. (struct compat_iovec __user *) datap;
  988. compat_uptr_t ptr;
  989. compat_size_t len;
  990. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  991. return -EFAULT;
  992. if (__get_user(ptr, &uiov->iov_base) ||
  993. __get_user(len, &uiov->iov_len))
  994. return -EFAULT;
  995. kiov.iov_base = compat_ptr(ptr);
  996. kiov.iov_len = len;
  997. ret = ptrace_regset(child, request, addr, &kiov);
  998. if (!ret)
  999. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  1000. break;
  1001. }
  1002. #endif
  1003. default:
  1004. ret = ptrace_request(child, request, addr, data);
  1005. }
  1006. return ret;
  1007. }
  1008. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  1009. compat_long_t addr, compat_long_t data)
  1010. {
  1011. struct task_struct *child;
  1012. long ret;
  1013. if (request == PTRACE_TRACEME) {
  1014. ret = ptrace_traceme();
  1015. goto out;
  1016. }
  1017. child = ptrace_get_task_struct(pid);
  1018. if (IS_ERR(child)) {
  1019. ret = PTR_ERR(child);
  1020. goto out;
  1021. }
  1022. if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
  1023. ret = ptrace_attach(child, request, addr, data);
  1024. /*
  1025. * Some architectures need to do book-keeping after
  1026. * a ptrace attach.
  1027. */
  1028. if (!ret)
  1029. arch_ptrace_attach(child);
  1030. goto out_put_task_struct;
  1031. }
  1032. ret = ptrace_check_attach(child, request == PTRACE_KILL ||
  1033. request == PTRACE_INTERRUPT);
  1034. if (!ret) {
  1035. ret = compat_arch_ptrace(child, request, addr, data);
  1036. if (ret || request != PTRACE_DETACH)
  1037. ptrace_unfreeze_traced(child);
  1038. }
  1039. out_put_task_struct:
  1040. put_task_struct(child);
  1041. out:
  1042. return ret;
  1043. }
  1044. #endif /* CONFIG_COMPAT */