ptrace.c 23 KB

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