ptrace.c 20 KB

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