ptrace.c 21 KB

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