ptrace.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. static 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->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. retval = 0;
  184. unlock_tasklist:
  185. write_unlock_irq(&tasklist_lock);
  186. unlock_creds:
  187. mutex_unlock(&task->signal->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. static 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. static 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_state(child, TASK_TRACED | TASK_STOPPED);
  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. __releases(&tasklist_lock)
  295. __acquires(&tasklist_lock)
  296. {
  297. struct task_struct *p, *n;
  298. LIST_HEAD(ptrace_dead);
  299. if (likely(list_empty(&tracer->ptraced)))
  300. return;
  301. list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
  302. if (__ptrace_detach(tracer, p))
  303. list_add(&p->ptrace_entry, &ptrace_dead);
  304. }
  305. write_unlock_irq(&tasklist_lock);
  306. BUG_ON(!list_empty(&tracer->ptraced));
  307. list_for_each_entry_safe(p, n, &ptrace_dead, ptrace_entry) {
  308. list_del_init(&p->ptrace_entry);
  309. release_task(p);
  310. }
  311. write_lock_irq(&tasklist_lock);
  312. }
  313. int ptrace_readdata(struct task_struct *tsk, unsigned long src, char __user *dst, int len)
  314. {
  315. int copied = 0;
  316. while (len > 0) {
  317. char buf[128];
  318. int this_len, retval;
  319. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  320. retval = access_process_vm(tsk, src, buf, this_len, 0);
  321. if (!retval) {
  322. if (copied)
  323. break;
  324. return -EIO;
  325. }
  326. if (copy_to_user(dst, buf, retval))
  327. return -EFAULT;
  328. copied += retval;
  329. src += retval;
  330. dst += retval;
  331. len -= retval;
  332. }
  333. return copied;
  334. }
  335. int ptrace_writedata(struct task_struct *tsk, char __user *src, unsigned long dst, int len)
  336. {
  337. int copied = 0;
  338. while (len > 0) {
  339. char buf[128];
  340. int this_len, retval;
  341. this_len = (len > sizeof(buf)) ? sizeof(buf) : len;
  342. if (copy_from_user(buf, src, this_len))
  343. return -EFAULT;
  344. retval = access_process_vm(tsk, dst, buf, this_len, 1);
  345. if (!retval) {
  346. if (copied)
  347. break;
  348. return -EIO;
  349. }
  350. copied += retval;
  351. src += retval;
  352. dst += retval;
  353. len -= retval;
  354. }
  355. return copied;
  356. }
  357. static int ptrace_setoptions(struct task_struct *child, unsigned long data)
  358. {
  359. child->ptrace &= ~PT_TRACE_MASK;
  360. if (data & PTRACE_O_TRACESYSGOOD)
  361. child->ptrace |= PT_TRACESYSGOOD;
  362. if (data & PTRACE_O_TRACEFORK)
  363. child->ptrace |= PT_TRACE_FORK;
  364. if (data & PTRACE_O_TRACEVFORK)
  365. child->ptrace |= PT_TRACE_VFORK;
  366. if (data & PTRACE_O_TRACECLONE)
  367. child->ptrace |= PT_TRACE_CLONE;
  368. if (data & PTRACE_O_TRACEEXEC)
  369. child->ptrace |= PT_TRACE_EXEC;
  370. if (data & PTRACE_O_TRACEVFORKDONE)
  371. child->ptrace |= PT_TRACE_VFORK_DONE;
  372. if (data & PTRACE_O_TRACEEXIT)
  373. child->ptrace |= PT_TRACE_EXIT;
  374. return (data & ~PTRACE_O_MASK) ? -EINVAL : 0;
  375. }
  376. static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
  377. {
  378. unsigned long flags;
  379. int error = -ESRCH;
  380. if (lock_task_sighand(child, &flags)) {
  381. error = -EINVAL;
  382. if (likely(child->last_siginfo != NULL)) {
  383. *info = *child->last_siginfo;
  384. error = 0;
  385. }
  386. unlock_task_sighand(child, &flags);
  387. }
  388. return error;
  389. }
  390. static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
  391. {
  392. unsigned long flags;
  393. int error = -ESRCH;
  394. if (lock_task_sighand(child, &flags)) {
  395. error = -EINVAL;
  396. if (likely(child->last_siginfo != NULL)) {
  397. *child->last_siginfo = *info;
  398. error = 0;
  399. }
  400. unlock_task_sighand(child, &flags);
  401. }
  402. return error;
  403. }
  404. #ifdef PTRACE_SINGLESTEP
  405. #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
  406. #else
  407. #define is_singlestep(request) 0
  408. #endif
  409. #ifdef PTRACE_SINGLEBLOCK
  410. #define is_singleblock(request) ((request) == PTRACE_SINGLEBLOCK)
  411. #else
  412. #define is_singleblock(request) 0
  413. #endif
  414. #ifdef PTRACE_SYSEMU
  415. #define is_sysemu_singlestep(request) ((request) == PTRACE_SYSEMU_SINGLESTEP)
  416. #else
  417. #define is_sysemu_singlestep(request) 0
  418. #endif
  419. static int ptrace_resume(struct task_struct *child, long request,
  420. unsigned long data)
  421. {
  422. if (!valid_signal(data))
  423. return -EIO;
  424. if (request == PTRACE_SYSCALL)
  425. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  426. else
  427. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  428. #ifdef TIF_SYSCALL_EMU
  429. if (request == PTRACE_SYSEMU || request == PTRACE_SYSEMU_SINGLESTEP)
  430. set_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  431. else
  432. clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
  433. #endif
  434. if (is_singleblock(request)) {
  435. if (unlikely(!arch_has_block_step()))
  436. return -EIO;
  437. user_enable_block_step(child);
  438. } else if (is_singlestep(request) || is_sysemu_singlestep(request)) {
  439. if (unlikely(!arch_has_single_step()))
  440. return -EIO;
  441. user_enable_single_step(child);
  442. } else {
  443. user_disable_single_step(child);
  444. }
  445. child->exit_code = data;
  446. wake_up_process(child);
  447. return 0;
  448. }
  449. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  450. static const struct user_regset *
  451. find_regset(const struct user_regset_view *view, unsigned int type)
  452. {
  453. const struct user_regset *regset;
  454. int n;
  455. for (n = 0; n < view->n; ++n) {
  456. regset = view->regsets + n;
  457. if (regset->core_note_type == type)
  458. return regset;
  459. }
  460. return NULL;
  461. }
  462. static int ptrace_regset(struct task_struct *task, int req, unsigned int type,
  463. struct iovec *kiov)
  464. {
  465. const struct user_regset_view *view = task_user_regset_view(task);
  466. const struct user_regset *regset = find_regset(view, type);
  467. int regset_no;
  468. if (!regset || (kiov->iov_len % regset->size) != 0)
  469. return -EINVAL;
  470. regset_no = regset - view->regsets;
  471. kiov->iov_len = min(kiov->iov_len,
  472. (__kernel_size_t) (regset->n * regset->size));
  473. if (req == PTRACE_GETREGSET)
  474. return copy_regset_to_user(task, view, regset_no, 0,
  475. kiov->iov_len, kiov->iov_base);
  476. else
  477. return copy_regset_from_user(task, view, regset_no, 0,
  478. kiov->iov_len, kiov->iov_base);
  479. }
  480. #endif
  481. int ptrace_request(struct task_struct *child, long request,
  482. unsigned long addr, unsigned long data)
  483. {
  484. int ret = -EIO;
  485. siginfo_t siginfo;
  486. void __user *datavp = (void __user *) data;
  487. unsigned long __user *datalp = datavp;
  488. switch (request) {
  489. case PTRACE_PEEKTEXT:
  490. case PTRACE_PEEKDATA:
  491. return generic_ptrace_peekdata(child, addr, data);
  492. case PTRACE_POKETEXT:
  493. case PTRACE_POKEDATA:
  494. return generic_ptrace_pokedata(child, addr, data);
  495. #ifdef PTRACE_OLDSETOPTIONS
  496. case PTRACE_OLDSETOPTIONS:
  497. #endif
  498. case PTRACE_SETOPTIONS:
  499. ret = ptrace_setoptions(child, data);
  500. break;
  501. case PTRACE_GETEVENTMSG:
  502. ret = put_user(child->ptrace_message, datalp);
  503. break;
  504. case PTRACE_GETSIGINFO:
  505. ret = ptrace_getsiginfo(child, &siginfo);
  506. if (!ret)
  507. ret = copy_siginfo_to_user(datavp, &siginfo);
  508. break;
  509. case PTRACE_SETSIGINFO:
  510. if (copy_from_user(&siginfo, datavp, sizeof siginfo))
  511. ret = -EFAULT;
  512. else
  513. ret = ptrace_setsiginfo(child, &siginfo);
  514. break;
  515. case PTRACE_DETACH: /* detach a process that was attached. */
  516. ret = ptrace_detach(child, data);
  517. break;
  518. #ifdef CONFIG_BINFMT_ELF_FDPIC
  519. case PTRACE_GETFDPIC: {
  520. struct mm_struct *mm = get_task_mm(child);
  521. unsigned long tmp = 0;
  522. ret = -ESRCH;
  523. if (!mm)
  524. break;
  525. switch (addr) {
  526. case PTRACE_GETFDPIC_EXEC:
  527. tmp = mm->context.exec_fdpic_loadmap;
  528. break;
  529. case PTRACE_GETFDPIC_INTERP:
  530. tmp = mm->context.interp_fdpic_loadmap;
  531. break;
  532. default:
  533. break;
  534. }
  535. mmput(mm);
  536. ret = put_user(tmp, datalp);
  537. break;
  538. }
  539. #endif
  540. #ifdef PTRACE_SINGLESTEP
  541. case PTRACE_SINGLESTEP:
  542. #endif
  543. #ifdef PTRACE_SINGLEBLOCK
  544. case PTRACE_SINGLEBLOCK:
  545. #endif
  546. #ifdef PTRACE_SYSEMU
  547. case PTRACE_SYSEMU:
  548. case PTRACE_SYSEMU_SINGLESTEP:
  549. #endif
  550. case PTRACE_SYSCALL:
  551. case PTRACE_CONT:
  552. return ptrace_resume(child, request, data);
  553. case PTRACE_KILL:
  554. if (child->exit_state) /* already dead */
  555. return 0;
  556. return ptrace_resume(child, request, SIGKILL);
  557. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  558. case PTRACE_GETREGSET:
  559. case PTRACE_SETREGSET:
  560. {
  561. struct iovec kiov;
  562. struct iovec __user *uiov = datavp;
  563. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  564. return -EFAULT;
  565. if (__get_user(kiov.iov_base, &uiov->iov_base) ||
  566. __get_user(kiov.iov_len, &uiov->iov_len))
  567. return -EFAULT;
  568. ret = ptrace_regset(child, request, addr, &kiov);
  569. if (!ret)
  570. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  571. break;
  572. }
  573. #endif
  574. default:
  575. break;
  576. }
  577. return ret;
  578. }
  579. static struct task_struct *ptrace_get_task_struct(pid_t pid)
  580. {
  581. struct task_struct *child;
  582. rcu_read_lock();
  583. child = find_task_by_vpid(pid);
  584. if (child)
  585. get_task_struct(child);
  586. rcu_read_unlock();
  587. if (!child)
  588. return ERR_PTR(-ESRCH);
  589. return child;
  590. }
  591. #ifndef arch_ptrace_attach
  592. #define arch_ptrace_attach(child) do { } while (0)
  593. #endif
  594. SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
  595. unsigned long, data)
  596. {
  597. struct task_struct *child;
  598. long ret;
  599. if (request == PTRACE_TRACEME) {
  600. ret = ptrace_traceme();
  601. if (!ret)
  602. arch_ptrace_attach(current);
  603. goto out;
  604. }
  605. child = ptrace_get_task_struct(pid);
  606. if (IS_ERR(child)) {
  607. ret = PTR_ERR(child);
  608. goto out;
  609. }
  610. if (request == PTRACE_ATTACH) {
  611. ret = ptrace_attach(child);
  612. /*
  613. * Some architectures need to do book-keeping after
  614. * a ptrace attach.
  615. */
  616. if (!ret)
  617. arch_ptrace_attach(child);
  618. goto out_put_task_struct;
  619. }
  620. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  621. if (ret < 0)
  622. goto out_put_task_struct;
  623. ret = arch_ptrace(child, request, addr, data);
  624. out_put_task_struct:
  625. put_task_struct(child);
  626. out:
  627. return ret;
  628. }
  629. int generic_ptrace_peekdata(struct task_struct *tsk, unsigned long addr,
  630. unsigned long data)
  631. {
  632. unsigned long tmp;
  633. int copied;
  634. copied = access_process_vm(tsk, addr, &tmp, sizeof(tmp), 0);
  635. if (copied != sizeof(tmp))
  636. return -EIO;
  637. return put_user(tmp, (unsigned long __user *)data);
  638. }
  639. int generic_ptrace_pokedata(struct task_struct *tsk, unsigned long addr,
  640. unsigned long data)
  641. {
  642. int copied;
  643. copied = access_process_vm(tsk, addr, &data, sizeof(data), 1);
  644. return (copied == sizeof(data)) ? 0 : -EIO;
  645. }
  646. #if defined CONFIG_COMPAT
  647. #include <linux/compat.h>
  648. int compat_ptrace_request(struct task_struct *child, compat_long_t request,
  649. compat_ulong_t addr, compat_ulong_t data)
  650. {
  651. compat_ulong_t __user *datap = compat_ptr(data);
  652. compat_ulong_t word;
  653. siginfo_t siginfo;
  654. int ret;
  655. switch (request) {
  656. case PTRACE_PEEKTEXT:
  657. case PTRACE_PEEKDATA:
  658. ret = access_process_vm(child, addr, &word, sizeof(word), 0);
  659. if (ret != sizeof(word))
  660. ret = -EIO;
  661. else
  662. ret = put_user(word, datap);
  663. break;
  664. case PTRACE_POKETEXT:
  665. case PTRACE_POKEDATA:
  666. ret = access_process_vm(child, addr, &data, sizeof(data), 1);
  667. ret = (ret != sizeof(data) ? -EIO : 0);
  668. break;
  669. case PTRACE_GETEVENTMSG:
  670. ret = put_user((compat_ulong_t) child->ptrace_message, datap);
  671. break;
  672. case PTRACE_GETSIGINFO:
  673. ret = ptrace_getsiginfo(child, &siginfo);
  674. if (!ret)
  675. ret = copy_siginfo_to_user32(
  676. (struct compat_siginfo __user *) datap,
  677. &siginfo);
  678. break;
  679. case PTRACE_SETSIGINFO:
  680. memset(&siginfo, 0, sizeof siginfo);
  681. if (copy_siginfo_from_user32(
  682. &siginfo, (struct compat_siginfo __user *) datap))
  683. ret = -EFAULT;
  684. else
  685. ret = ptrace_setsiginfo(child, &siginfo);
  686. break;
  687. #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
  688. case PTRACE_GETREGSET:
  689. case PTRACE_SETREGSET:
  690. {
  691. struct iovec kiov;
  692. struct compat_iovec __user *uiov =
  693. (struct compat_iovec __user *) datap;
  694. compat_uptr_t ptr;
  695. compat_size_t len;
  696. if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
  697. return -EFAULT;
  698. if (__get_user(ptr, &uiov->iov_base) ||
  699. __get_user(len, &uiov->iov_len))
  700. return -EFAULT;
  701. kiov.iov_base = compat_ptr(ptr);
  702. kiov.iov_len = len;
  703. ret = ptrace_regset(child, request, addr, &kiov);
  704. if (!ret)
  705. ret = __put_user(kiov.iov_len, &uiov->iov_len);
  706. break;
  707. }
  708. #endif
  709. default:
  710. ret = ptrace_request(child, request, addr, data);
  711. }
  712. return ret;
  713. }
  714. asmlinkage long compat_sys_ptrace(compat_long_t request, compat_long_t pid,
  715. compat_long_t addr, compat_long_t data)
  716. {
  717. struct task_struct *child;
  718. long ret;
  719. if (request == PTRACE_TRACEME) {
  720. ret = ptrace_traceme();
  721. goto out;
  722. }
  723. child = ptrace_get_task_struct(pid);
  724. if (IS_ERR(child)) {
  725. ret = PTR_ERR(child);
  726. goto out;
  727. }
  728. if (request == PTRACE_ATTACH) {
  729. ret = ptrace_attach(child);
  730. /*
  731. * Some architectures need to do book-keeping after
  732. * a ptrace attach.
  733. */
  734. if (!ret)
  735. arch_ptrace_attach(child);
  736. goto out_put_task_struct;
  737. }
  738. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  739. if (!ret)
  740. ret = compat_arch_ptrace(child, request, addr, data);
  741. out_put_task_struct:
  742. put_task_struct(child);
  743. out:
  744. return ret;
  745. }
  746. #endif /* CONFIG_COMPAT */