ptrace.c 33 KB

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