ptrace.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 (task_is_traced(child) || ignore_state)
  137. ret = 0;
  138. spin_unlock_irq(&child->sighand->siglock);
  139. }
  140. read_unlock(&tasklist_lock);
  141. if (!ret && !ignore_state)
  142. ret = wait_task_inactive(child, TASK_TRACED) ? 0 : -ESRCH;
  143. /* All systems go.. */
  144. return ret;
  145. }
  146. int __ptrace_may_access(struct task_struct *task, unsigned int mode)
  147. {
  148. const struct cred *cred = current_cred(), *tcred;
  149. /* May we inspect the given task?
  150. * This check is used both for attaching with ptrace
  151. * and for allowing access to sensitive information in /proc.
  152. *
  153. * ptrace_attach denies several cases that /proc allows
  154. * because setting up the necessary parent/child relationship
  155. * or halting the specified task is impossible.
  156. */
  157. int dumpable = 0;
  158. /* Don't let security modules deny introspection */
  159. if (task == current)
  160. return 0;
  161. rcu_read_lock();
  162. tcred = __task_cred(task);
  163. if (cred->user->user_ns == tcred->user->user_ns &&
  164. (cred->uid == tcred->euid &&
  165. cred->uid == tcred->suid &&
  166. cred->uid == tcred->uid &&
  167. cred->gid == tcred->egid &&
  168. cred->gid == tcred->sgid &&
  169. cred->gid == tcred->gid))
  170. goto ok;
  171. if (ns_capable(tcred->user->user_ns, CAP_SYS_PTRACE))
  172. goto ok;
  173. rcu_read_unlock();
  174. return -EPERM;
  175. ok:
  176. rcu_read_unlock();
  177. smp_rmb();
  178. if (task->mm)
  179. dumpable = get_dumpable(task->mm);
  180. if (!dumpable && !task_ns_capable(task, CAP_SYS_PTRACE))
  181. return -EPERM;
  182. return security_ptrace_access_check(task, mode);
  183. }
  184. bool ptrace_may_access(struct task_struct *task, unsigned int mode)
  185. {
  186. int err;
  187. task_lock(task);
  188. err = __ptrace_may_access(task, mode);
  189. task_unlock(task);
  190. return !err;
  191. }
  192. static int ptrace_attach(struct task_struct *task)
  193. {
  194. int retval;
  195. audit_ptrace(task);
  196. retval = -EPERM;
  197. if (unlikely(task->flags & PF_KTHREAD))
  198. goto out;
  199. if (same_thread_group(task, current))
  200. goto out;
  201. /*
  202. * Protect exec's credential calculations against our interference;
  203. * interference; SUID, SGID and LSM creds get determined differently
  204. * under ptrace.
  205. */
  206. retval = -ERESTARTNOINTR;
  207. if (mutex_lock_interruptible(&task->signal->cred_guard_mutex))
  208. goto out;
  209. task_lock(task);
  210. retval = __ptrace_may_access(task, PTRACE_MODE_ATTACH);
  211. task_unlock(task);
  212. if (retval)
  213. goto unlock_creds;
  214. write_lock_irq(&tasklist_lock);
  215. retval = -EPERM;
  216. if (unlikely(task->exit_state))
  217. goto unlock_tasklist;
  218. if (task->ptrace)
  219. goto unlock_tasklist;
  220. task->ptrace = PT_PTRACED;
  221. if (task_ns_capable(task, CAP_SYS_PTRACE))
  222. task->ptrace |= PT_PTRACE_CAP;
  223. __ptrace_link(task, current);
  224. send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
  225. spin_lock(&task->sighand->siglock);
  226. /*
  227. * If the task is already STOPPED, set JOBCTL_TRAP_STOP and
  228. * TRAPPING, and kick it so that it transits to TRACED. TRAPPING
  229. * will be cleared if the child completes the transition or any
  230. * event which clears the group stop states happens. We'll wait
  231. * for the transition to complete before returning from this
  232. * function.
  233. *
  234. * This hides STOPPED -> RUNNING -> TRACED transition from the
  235. * attaching thread but a different thread in the same group can
  236. * still observe the transient RUNNING state. IOW, if another
  237. * thread's WNOHANG wait(2) on the stopped tracee races against
  238. * ATTACH, the wait(2) may fail due to the transient RUNNING.
  239. *
  240. * The following task_is_stopped() test is safe as both transitions
  241. * in and out of STOPPED are protected by siglock.
  242. */
  243. if (task_is_stopped(task) &&
  244. task_set_jobctl_pending(task, JOBCTL_TRAP_STOP | JOBCTL_TRAPPING))
  245. signal_wake_up(task, 1);
  246. spin_unlock(&task->sighand->siglock);
  247. retval = 0;
  248. unlock_tasklist:
  249. write_unlock_irq(&tasklist_lock);
  250. unlock_creds:
  251. mutex_unlock(&task->signal->cred_guard_mutex);
  252. out:
  253. if (!retval)
  254. wait_on_bit(&task->jobctl, JOBCTL_TRAPPING_BIT,
  255. ptrace_trapping_sleep_fn, TASK_UNINTERRUPTIBLE);
  256. return retval;
  257. }
  258. /**
  259. * ptrace_traceme -- helper for PTRACE_TRACEME
  260. *
  261. * Performs checks and sets PT_PTRACED.
  262. * Should be used by all ptrace implementations for PTRACE_TRACEME.
  263. */
  264. static int ptrace_traceme(void)
  265. {
  266. int ret = -EPERM;
  267. write_lock_irq(&tasklist_lock);
  268. /* Are we already being traced? */
  269. if (!current->ptrace) {
  270. ret = security_ptrace_traceme(current->parent);
  271. /*
  272. * Check PF_EXITING to ensure ->real_parent has not passed
  273. * exit_ptrace(). Otherwise we don't report the error but
  274. * pretend ->real_parent untraces us right after return.
  275. */
  276. if (!ret && !(current->real_parent->flags & PF_EXITING)) {
  277. current->ptrace = PT_PTRACED;
  278. __ptrace_link(current, current->real_parent);
  279. }
  280. }
  281. write_unlock_irq(&tasklist_lock);
  282. return ret;
  283. }
  284. /*
  285. * Called with irqs disabled, returns true if childs should reap themselves.
  286. */
  287. static int ignoring_children(struct sighand_struct *sigh)
  288. {
  289. int ret;
  290. spin_lock(&sigh->siglock);
  291. ret = (sigh->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) ||
  292. (sigh->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT);
  293. spin_unlock(&sigh->siglock);
  294. return ret;
  295. }
  296. /*
  297. * Called with tasklist_lock held for writing.
  298. * Unlink a traced task, and clean it up if it was a traced zombie.
  299. * Return true if it needs to be reaped with release_task().
  300. * (We can't call release_task() here because we already hold tasklist_lock.)
  301. *
  302. * If it's a zombie, our attachedness prevented normal parent notification
  303. * or self-reaping. Do notification now if it would have happened earlier.
  304. * If it should reap itself, return true.
  305. *
  306. * If it's our own child, there is no notification to do. But if our normal
  307. * children self-reap, then this child was prevented by ptrace and we must
  308. * reap it now, in that case we must also wake up sub-threads sleeping in
  309. * do_wait().
  310. */
  311. static bool __ptrace_detach(struct task_struct *tracer, struct task_struct *p)
  312. {
  313. __ptrace_unlink(p);
  314. if (p->exit_state == EXIT_ZOMBIE) {
  315. if (!task_detached(p) && thread_group_empty(p)) {
  316. if (!same_thread_group(p->real_parent, tracer))
  317. do_notify_parent(p, p->exit_signal);
  318. else if (ignoring_children(tracer->sighand)) {
  319. __wake_up_parent(p, tracer);
  320. p->exit_signal = -1;
  321. }
  322. }
  323. if (task_detached(p)) {
  324. /* Mark it as in the process of being reaped. */
  325. p->exit_state = EXIT_DEAD;
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. static int ptrace_detach(struct task_struct *child, unsigned int data)
  332. {
  333. bool dead = false;
  334. if (!valid_signal(data))
  335. return -EIO;
  336. /* Architecture-specific hardware disable .. */
  337. ptrace_disable(child);
  338. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  339. write_lock_irq(&tasklist_lock);
  340. /*
  341. * This child can be already killed. Make sure de_thread() or
  342. * our sub-thread doing do_wait() didn't do release_task() yet.
  343. */
  344. if (child->ptrace) {
  345. child->exit_code = data;
  346. dead = __ptrace_detach(current, child);
  347. }
  348. write_unlock_irq(&tasklist_lock);
  349. if (unlikely(dead))
  350. release_task(child);
  351. return 0;
  352. }
  353. /*
  354. * Detach all tasks we were using ptrace on. Called with tasklist held
  355. * for writing, and returns with it held too. But note it can release
  356. * and reacquire the lock.
  357. */
  358. void exit_ptrace(struct task_struct *tracer)
  359. __releases(&tasklist_lock)
  360. __acquires(&tasklist_lock)
  361. {
  362. struct task_struct *p, *n;
  363. LIST_HEAD(ptrace_dead);
  364. if (likely(list_empty(&tracer->ptraced)))
  365. return;
  366. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  367. if (__ptrace_detach(tracer, p))
  368. list_add(&p->ptrace_entry, &ptrace_dead);
  369. }
  370. write_unlock_irq(&tasklist_lock);
  371. BUG_ON(!list_empty(&tracer->ptraced));
  372. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  373. list_del_init(&p->ptrace_entry);
  374. release_task(p);
  375. }
  376. write_lock_irq(&tasklist_lock);
  377. }
  378. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  379. {
  380. int copied = 0;
  381. while (len > 0) {
  382. char buf[128];
  383. int this_len, retval;
  384. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  385. retval = access_process_vm(tsk, src, buf, this_len, 0);
  386. if (!retval) {
  387. if (copied)
  388. break;
  389. return -EIO;
  390. }
  391. if (copy_to_user(dst, buf, retval))
  392. return -EFAULT;
  393. copied += retval;
  394. src += retval;
  395. dst += retval;
  396. len -= retval;
  397. }
  398. return copied;
  399. }
  400. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long 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. if (copy_from_user(buf, src, this_len))
  408. return -EFAULT;
  409. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  410. if (!retval) {
  411. if (copied)
  412. break;
  413. return -EIO;
  414. }
  415. copied += retval;
  416. src += retval;
  417. dst += retval;
  418. len -= retval;
  419. }
  420. return copied;
  421. }
  422. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  423. {
  424. child->ptrace &= ~PT_TRACE_MASK;
  425. if (data & PTRACE_O_TRACESYSGOOD)
  426. child->ptrace |= PT_TRACESYSGOOD;
  427. if (data & PTRACE_O_TRACEFORK)
  428. child->ptrace |= PT_TRACE_FORK;
  429. if (data & PTRACE_O_TRACEVFORK)
  430. child->ptrace |= PT_TRACE_VFORK;
  431. if (data & PTRACE_O_TRACECLONE)
  432. child->ptrace |= PT_TRACE_CLONE;
  433. if (data & PTRACE_O_TRACEEXEC)
  434. child->ptrace |= PT_TRACE_EXEC;
  435. if (data & PTRACE_O_TRACEVFORKDONE)
  436. child->ptrace |= PT_TRACE_VFORK_DONE;
  437. if (data & PTRACE_O_TRACEEXIT)
  438. child->ptrace |= PT_TRACE_EXIT;
  439. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  440. }
  441. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  442. {
  443. unsigned long flags;
  444. int error = -ESRCH;
  445. if (lock_task_sighand(child, &flags)) {
  446. error = -EINVAL;
  447. if (likely(child->last_siginfo != NULL)) {
  448. *info = *child->last_siginfo;
  449. error = 0;
  450. }
  451. unlock_task_sighand(child, &flags);
  452. }
  453. return error;
  454. }
  455. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  456. {
  457. unsigned long flags;
  458. int error = -ESRCH;
  459. if (lock_task_sighand(child, &flags)) {
  460. error = -EINVAL;
  461. if (likely(child->last_siginfo != NULL)) {
  462. *child->last_siginfo = *info;
  463. error = 0;
  464. }
  465. unlock_task_sighand(child, &flags);
  466. }
  467. return error;
  468. }
  469. #ifdef PTRACE_SINGLESTEP
  470. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  471. #else
  472. #define is_singlestep(request) 0
  473. #endif
  474. #ifdef PTRACE_SINGLEBLOCK
  475. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  476. #else
  477. #define is_singleblock(request) 0
  478. #endif
  479. #ifdef PTRACE_SYSEMU
  480. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  481. #else
  482. #define is_sysemu_singlestep(request) 0
  483. #endif
  484. static int ptrace_resume(struct task_struct *child, long request,
  485. unsigned long data)
  486. {
  487. if (!valid_signal(data))
  488. return -EIO;
  489. if (request == PTRACE_SYSCALL)
  490. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  491. else
  492. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  493. #ifdef TIF_SYSCALL_EMU
  494. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  495. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  496. else
  497. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  498. #endif
  499. if (is_singleblock(request)) {
  500. if (unlikely(!arch_has_block_step()))
  501. return -EIO;
  502. user_enable_block_step(child);
  503. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  504. if (unlikely(!arch_has_single_step()))
  505. return -EIO;
  506. user_enable_single_step(child);
  507. } else {
  508. user_disable_single_step(child);
  509. }
  510. child->exit_code = data;
  511. wake_up_state(child, __TASK_TRACED);
  512. return 0;
  513. }
  514. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  515. static const struct user_regset *
  516. find_regset(const struct user_regset_view *view, unsigned int type)
  517. {
  518. const struct user_regset *regset;
  519. int n;
  520. for (n = 0; n < view->n; ++n) {
  521. regset = view->regsets + n;
  522. if (regset->core_note_type == type)
  523. return regset;
  524. }
  525. return NULL;
  526. }
  527. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  528. struct iovec *kiov)
  529. {
  530. const struct user_regset_view *view = task_user_regset_view(task);
  531. const struct user_regset *regset = find_regset(view, type);
  532. int regset_no;
  533. if (!regset || (kiov->iov_len % regset->size) != 0)
  534. return -EINVAL;
  535. regset_no = regset - view->regsets;
  536. kiov->iov_len = min(kiov->iov_len,
  537. (__kernel_size_t) (regset->n * regset->size));
  538. if (req == PTRACE_GETREGSET)
  539. return copy_regset_to_user(task, view, regset_no, 0,
  540. kiov->iov_len, kiov->iov_base);
  541. else
  542. return copy_regset_from_user(task, view, regset_no, 0,
  543. kiov->iov_len, kiov->iov_base);
  544. }
  545. #endif
  546. int ptrace_request(struct task_struct *child, long request,
  547. unsigned long addr, unsigned long data)
  548. {
  549. int ret = -EIO;
  550. siginfo_t siginfo;
  551. void __user *datavp = (void __user *) data;
  552. unsigned long __user *datalp = datavp;
  553. switch (request) {
  554. case PTRACE_PEEKTEXT:
  555. case PTRACE_PEEKDATA:
  556. return generic_ptrace_peekdata(child, addr, data);
  557. case PTRACE_POKETEXT:
  558. case PTRACE_POKEDATA:
  559. return generic_ptrace_pokedata(child, addr, data);
  560. #ifdef PTRACE_OLDSETOPTIONS
  561. case PTRACE_OLDSETOPTIONS:
  562. #endif
  563. case PTRACE_SETOPTIONS:
  564. ret = ptrace_setoptions(child, data);
  565. break;
  566. case PTRACE_GETEVENTMSG:
  567. ret = put_user(child->ptrace_message, datalp);
  568. break;
  569. case PTRACE_GETSIGINFO:
  570. ret = ptrace_getsiginfo(child, &siginfo);
  571. if (!ret)
  572. ret = copy_siginfo_to_user(datavp, &siginfo);
  573. break;
  574. case PTRACE_SETSIGINFO:
  575. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  576. ret = -EFAULT;
  577. else
  578. ret = ptrace_setsiginfo(child, &siginfo);
  579. break;
  580. case PTRACE_DETACH: /* detach a process that was attached. */
  581. ret = ptrace_detach(child, data);
  582. break;
  583. #ifdef CONFIG_BINFMT_ELF_FDPIC
  584. case PTRACE_GETFDPIC: {
  585. struct mm_struct *mm = get_task_mm(child);
  586. unsigned long tmp = 0;
  587. ret = -ESRCH;
  588. if (!mm)
  589. break;
  590. switch (addr) {
  591. case PTRACE_GETFDPIC_EXEC:
  592. tmp = mm->context.exec_fdpic_loadmap;
  593. break;
  594. case PTRACE_GETFDPIC_INTERP:
  595. tmp = mm->context.interp_fdpic_loadmap;
  596. break;
  597. default:
  598. break;
  599. }
  600. mmput(mm);
  601. ret = put_user(tmp, datalp);
  602. break;
  603. }
  604. #endif
  605. #ifdef PTRACE_SINGLESTEP
  606. case PTRACE_SINGLESTEP:
  607. #endif
  608. #ifdef PTRACE_SINGLEBLOCK
  609. case PTRACE_SINGLEBLOCK:
  610. #endif
  611. #ifdef PTRACE_SYSEMU
  612. case PTRACE_SYSEMU:
  613. case PTRACE_SYSEMU_SINGLESTEP:
  614. #endif
  615. case PTRACE_SYSCALL:
  616. case PTRACE_CONT:
  617. return ptrace_resume(child, request, data);
  618. case PTRACE_KILL:
  619. if (child->exit_state) /* already dead */
  620. return 0;
  621. return ptrace_resume(child, request, SIGKILL);
  622. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  623. case PTRACE_GETREGSET:
  624. case PTRACE_SETREGSET:
  625. {
  626. struct iovec kiov;
  627. struct iovec __user *uiov = datavp;
  628. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  629. return -EFAULT;
  630. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  631. __get_user(kiov.iov_len, &uiov->iov_len))
  632. return -EFAULT;
  633. ret = ptrace_regset(child, request, addr, &kiov);
  634. if (!ret)
  635. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  636. break;
  637. }
  638. #endif
  639. default:
  640. break;
  641. }
  642. return ret;
  643. }
  644. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  645. {
  646. struct task_struct *child;
  647. rcu_read_lock();
  648. child = find_task_by_vpid(pid);
  649. if (child)
  650. get_task_struct(child);
  651. rcu_read_unlock();
  652. if (!child)
  653. return ERR_PTR(-ESRCH);
  654. return child;
  655. }
  656. #ifndef arch_ptrace_attach
  657. #define arch_ptrace_attach(child) do { } while (0)
  658. #endif
  659. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  660. unsigned long, data)
  661. {
  662. struct task_struct *child;
  663. long ret;
  664. if (request == PTRACE_TRACEME) {
  665. ret = ptrace_traceme();
  666. if (!ret)
  667. arch_ptrace_attach(current);
  668. goto out;
  669. }
  670. child = ptrace_get_task_struct(pid);
  671. if (IS_ERR(child)) {
  672. ret = PTR_ERR(child);
  673. goto out;
  674. }
  675. if (request == PTRACE_ATTACH) {
  676. ret = ptrace_attach(child);
  677. /*
  678. * Some architectures need to do book-keeping after
  679. * a ptrace attach.
  680. */
  681. if (!ret)
  682. arch_ptrace_attach(child);
  683. goto out_put_task_struct;
  684. }
  685. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  686. if (ret < 0)
  687. goto out_put_task_struct;
  688. ret = arch_ptrace(child, request, addr, data);
  689. out_put_task_struct:
  690. put_task_struct(child);
  691. out:
  692. return ret;
  693. }
  694. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  695. unsigned long data)
  696. {
  697. unsigned long tmp;
  698. int copied;
  699. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  700. if (copied != sizeof(tmp))
  701. return -EIO;
  702. return put_user(tmp, (unsigned long __user *)data);
  703. }
  704. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  705. unsigned long data)
  706. {
  707. int copied;
  708. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  709. return (copied == sizeof(data)) ? 0 : -EIO;
  710. }
  711. #if defined CONFIG_COMPAT
  712. #include <linux/compat.h>
  713. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  714. compat_ulong_t addr, compat_ulong_t data)
  715. {
  716. compat_ulong_t __user *datap = compat_ptr(data);
  717. compat_ulong_t word;
  718. siginfo_t siginfo;
  719. int ret;
  720. switch (request) {
  721. case PTRACE_PEEKTEXT:
  722. case PTRACE_PEEKDATA:
  723. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  724. if (ret != sizeof(word))
  725. ret = -EIO;
  726. else
  727. ret = put_user(word, datap);
  728. break;
  729. case PTRACE_POKETEXT:
  730. case PTRACE_POKEDATA:
  731. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  732. ret = (ret != sizeof(data) ? -EIO : 0);
  733. break;
  734. case PTRACE_GETEVENTMSG:
  735. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  736. break;
  737. case PTRACE_GETSIGINFO:
  738. ret = ptrace_getsiginfo(child, &siginfo);
  739. if (!ret)
  740. ret = copy_siginfo_to_user32(
  741. (struct compat_siginfo __user *) datap,
  742. &siginfo);
  743. break;
  744. case PTRACE_SETSIGINFO:
  745. memset(&siginfo, 0, sizeof siginfo);
  746. if (copy_siginfo_from_user32(
  747. &siginfo, (struct compat_siginfo __user *) datap))
  748. ret = -EFAULT;
  749. else
  750. ret = ptrace_setsiginfo(child, &siginfo);
  751. break;
  752. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  753. case PTRACE_GETREGSET:
  754. case PTRACE_SETREGSET:
  755. {
  756. struct iovec kiov;
  757. struct compat_iovec __user *uiov =
  758. (struct compat_iovec __user *) datap;
  759. compat_uptr_t ptr;
  760. compat_size_t len;
  761. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  762. return -EFAULT;
  763. if (__get_user(ptr, &uiov->iov_base) ||
  764. __get_user(len, &uiov->iov_len))
  765. return -EFAULT;
  766. kiov.iov_base = compat_ptr(ptr);
  767. kiov.iov_len = len;
  768. ret = ptrace_regset(child, request, addr, &kiov);
  769. if (!ret)
  770. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  771. break;
  772. }
  773. #endif
  774. default:
  775. ret = ptrace_request(child, request, addr, data);
  776. }
  777. return ret;
  778. }
  779. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  780. compat_long_t addr, compat_long_t data)
  781. {
  782. struct task_struct *child;
  783. long ret;
  784. if (request == PTRACE_TRACEME) {
  785. ret = ptrace_traceme();
  786. goto out;
  787. }
  788. child = ptrace_get_task_struct(pid);
  789. if (IS_ERR(child)) {
  790. ret = PTR_ERR(child);
  791. goto out;
  792. }
  793. if (request == PTRACE_ATTACH) {
  794. ret = ptrace_attach(child);
  795. /*
  796. * Some architectures need to do book-keeping after
  797. * a ptrace attach.
  798. */
  799. if (!ret)
  800. arch_ptrace_attach(child);
  801. goto out_put_task_struct;
  802. }
  803. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  804. if (!ret)
  805. ret = compat_arch_ptrace(child, request, addr, data);
  806. out_put_task_struct:
  807. put_task_struct(child);
  808. out:
  809. return ret;
  810. }
  811. #endif /* CONFIG_COMPAT */
  812. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  813. int ptrace_get_breakpoints(struct task_struct *tsk)
  814. {
  815. if (atomic_inc_not_zero(&tsk->ptrace_bp_refcnt))
  816. return 0;
  817. return -1;
  818. }
  819. void ptrace_put_breakpoints(struct task_struct *tsk)
  820. {
  821. if (atomic_dec_and_test(&tsk->ptrace_bp_refcnt))
  822. flush_ptrace_hw_breakpoint(tsk);
  823. }
  824. #endif /* CONFIG_HAVE_HW_BREAKPOINT */