ptrace.c 21 KB

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