ptrace.c 23 KB

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