ptrace.c 32 KB

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