ptrace.c 26 KB

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