ptrace.c 26 KB

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