ptrace.c 30 KB

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