ptrace.c 37 KB

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