ptrace.c 37 KB

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