ptrace.c 33 KB

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