ptrace.c 23 KB

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