ptrace.c 20 KB

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