ptrace.c 20 KB

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