ptrace.c 32 KB

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