ptrace.c 27 KB

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