ptrace.c 20 KB

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