ptrace.c 30 KB

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