ptrace.c 33 KB

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