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