ptrace.c 32 KB

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