ptrace.c 29 KB

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