ptrace.c 25 KB

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