ptrace.c 22 KB

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