common.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * common.c - C code for kernel entry and exit
  3. * Copyright (c) 2015 Andrew Lutomirski
  4. * GPL v2
  5. *
  6. * Based on asm and ptrace code by many authors. The code here originated
  7. * in ptrace.c and signal.c.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/tracehook.h>
  16. #include <linux/audit.h>
  17. #include <linux/seccomp.h>
  18. #include <linux/signal.h>
  19. #include <linux/export.h>
  20. #include <linux/context_tracking.h>
  21. #include <linux/user-return-notifier.h>
  22. #include <linux/uprobes.h>
  23. #include <asm/desc.h>
  24. #include <asm/traps.h>
  25. #define CREATE_TRACE_POINTS
  26. #include <trace/events/syscalls.h>
  27. #ifdef CONFIG_CONTEXT_TRACKING
  28. /* Called on entry from user mode with IRQs off. */
  29. __visible void enter_from_user_mode(void)
  30. {
  31. CT_WARN_ON(ct_state() != CONTEXT_USER);
  32. user_exit();
  33. }
  34. #endif
  35. static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
  36. {
  37. #ifdef CONFIG_X86_64
  38. if (arch == AUDIT_ARCH_X86_64) {
  39. audit_syscall_entry(regs->orig_ax, regs->di,
  40. regs->si, regs->dx, regs->r10);
  41. } else
  42. #endif
  43. {
  44. audit_syscall_entry(regs->orig_ax, regs->bx,
  45. regs->cx, regs->dx, regs->si);
  46. }
  47. }
  48. /*
  49. * We can return 0 to resume the syscall or anything else to go to phase
  50. * 2. If we resume the syscall, we need to put something appropriate in
  51. * regs->orig_ax.
  52. *
  53. * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
  54. * are fully functional.
  55. *
  56. * For phase 2's benefit, our return value is:
  57. * 0: resume the syscall
  58. * 1: go to phase 2; no seccomp phase 2 needed
  59. * anything else: go to phase 2; pass return value to seccomp
  60. */
  61. unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
  62. {
  63. unsigned long ret = 0;
  64. u32 work;
  65. BUG_ON(regs != task_pt_regs(current));
  66. work = ACCESS_ONCE(current_thread_info()->flags) &
  67. _TIF_WORK_SYSCALL_ENTRY;
  68. #ifdef CONFIG_CONTEXT_TRACKING
  69. /*
  70. * If TIF_NOHZ is set, we are required to call user_exit() before
  71. * doing anything that could touch RCU.
  72. */
  73. if (work & _TIF_NOHZ) {
  74. enter_from_user_mode();
  75. work &= ~_TIF_NOHZ;
  76. }
  77. #endif
  78. #ifdef CONFIG_SECCOMP
  79. /*
  80. * Do seccomp first -- it should minimize exposure of other
  81. * code, and keeping seccomp fast is probably more valuable
  82. * than the rest of this.
  83. */
  84. if (work & _TIF_SECCOMP) {
  85. struct seccomp_data sd;
  86. sd.arch = arch;
  87. sd.nr = regs->orig_ax;
  88. sd.instruction_pointer = regs->ip;
  89. #ifdef CONFIG_X86_64
  90. if (arch == AUDIT_ARCH_X86_64) {
  91. sd.args[0] = regs->di;
  92. sd.args[1] = regs->si;
  93. sd.args[2] = regs->dx;
  94. sd.args[3] = regs->r10;
  95. sd.args[4] = regs->r8;
  96. sd.args[5] = regs->r9;
  97. } else
  98. #endif
  99. {
  100. sd.args[0] = regs->bx;
  101. sd.args[1] = regs->cx;
  102. sd.args[2] = regs->dx;
  103. sd.args[3] = regs->si;
  104. sd.args[4] = regs->di;
  105. sd.args[5] = regs->bp;
  106. }
  107. BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
  108. BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
  109. ret = seccomp_phase1(&sd);
  110. if (ret == SECCOMP_PHASE1_SKIP) {
  111. regs->orig_ax = -1;
  112. ret = 0;
  113. } else if (ret != SECCOMP_PHASE1_OK) {
  114. return ret; /* Go directly to phase 2 */
  115. }
  116. work &= ~_TIF_SECCOMP;
  117. }
  118. #endif
  119. /* Do our best to finish without phase 2. */
  120. if (work == 0)
  121. return ret; /* seccomp and/or nohz only (ret == 0 here) */
  122. #ifdef CONFIG_AUDITSYSCALL
  123. if (work == _TIF_SYSCALL_AUDIT) {
  124. /*
  125. * If there is no more work to be done except auditing,
  126. * then audit in phase 1. Phase 2 always audits, so, if
  127. * we audit here, then we can't go on to phase 2.
  128. */
  129. do_audit_syscall_entry(regs, arch);
  130. return 0;
  131. }
  132. #endif
  133. return 1; /* Something is enabled that we can't handle in phase 1 */
  134. }
  135. /* Returns the syscall nr to run (which should match regs->orig_ax). */
  136. long syscall_trace_enter_phase2(struct pt_regs *regs, u32 arch,
  137. unsigned long phase1_result)
  138. {
  139. long ret = 0;
  140. u32 work = ACCESS_ONCE(current_thread_info()->flags) &
  141. _TIF_WORK_SYSCALL_ENTRY;
  142. BUG_ON(regs != task_pt_regs(current));
  143. /*
  144. * If we stepped into a sysenter/syscall insn, it trapped in
  145. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  146. * If user-mode had set TF itself, then it's still clear from
  147. * do_debug() and we need to set it again to restore the user
  148. * state. If we entered on the slow path, TF was already set.
  149. */
  150. if (work & _TIF_SINGLESTEP)
  151. regs->flags |= X86_EFLAGS_TF;
  152. #ifdef CONFIG_SECCOMP
  153. /*
  154. * Call seccomp_phase2 before running the other hooks so that
  155. * they can see any changes made by a seccomp tracer.
  156. */
  157. if (phase1_result > 1 && seccomp_phase2(phase1_result)) {
  158. /* seccomp failures shouldn't expose any additional code. */
  159. return -1;
  160. }
  161. #endif
  162. if (unlikely(work & _TIF_SYSCALL_EMU))
  163. ret = -1L;
  164. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  165. tracehook_report_syscall_entry(regs))
  166. ret = -1L;
  167. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  168. trace_sys_enter(regs, regs->orig_ax);
  169. do_audit_syscall_entry(regs, arch);
  170. return ret ?: regs->orig_ax;
  171. }
  172. long syscall_trace_enter(struct pt_regs *regs)
  173. {
  174. u32 arch = is_ia32_task() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
  175. unsigned long phase1_result = syscall_trace_enter_phase1(regs, arch);
  176. if (phase1_result == 0)
  177. return regs->orig_ax;
  178. else
  179. return syscall_trace_enter_phase2(regs, arch, phase1_result);
  180. }
  181. /* Deprecated. */
  182. void syscall_trace_leave(struct pt_regs *regs)
  183. {
  184. bool step;
  185. /*
  186. * We may come here right after calling schedule_user()
  187. * or do_notify_resume(), in which case we can be in RCU
  188. * user mode.
  189. */
  190. user_exit();
  191. audit_syscall_exit(regs);
  192. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  193. trace_sys_exit(regs, regs->ax);
  194. /*
  195. * If TIF_SYSCALL_EMU is set, we only get here because of
  196. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  197. * We already reported this syscall instruction in
  198. * syscall_trace_enter().
  199. */
  200. step = unlikely(test_thread_flag(TIF_SINGLESTEP)) &&
  201. !test_thread_flag(TIF_SYSCALL_EMU);
  202. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  203. tracehook_report_syscall_exit(regs, step);
  204. user_enter();
  205. }
  206. static struct thread_info *pt_regs_to_thread_info(struct pt_regs *regs)
  207. {
  208. unsigned long top_of_stack =
  209. (unsigned long)(regs + 1) + TOP_OF_KERNEL_STACK_PADDING;
  210. return (struct thread_info *)(top_of_stack - THREAD_SIZE);
  211. }
  212. /* Called with IRQs disabled. */
  213. __visible void prepare_exit_to_usermode(struct pt_regs *regs)
  214. {
  215. if (WARN_ON(!irqs_disabled()))
  216. local_irq_disable();
  217. /*
  218. * In order to return to user mode, we need to have IRQs off with
  219. * none of _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_USER_RETURN_NOTIFY,
  220. * _TIF_UPROBE, or _TIF_NEED_RESCHED set. Several of these flags
  221. * can be set at any time on preemptable kernels if we have IRQs on,
  222. * so we need to loop. Disabling preemption wouldn't help: doing the
  223. * work to clear some of the flags can sleep.
  224. */
  225. while (true) {
  226. u32 cached_flags =
  227. READ_ONCE(pt_regs_to_thread_info(regs)->flags);
  228. if (!(cached_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME |
  229. _TIF_UPROBE | _TIF_NEED_RESCHED |
  230. _TIF_USER_RETURN_NOTIFY)))
  231. break;
  232. /* We have work to do. */
  233. local_irq_enable();
  234. if (cached_flags & _TIF_NEED_RESCHED)
  235. schedule();
  236. if (cached_flags & _TIF_UPROBE)
  237. uprobe_notify_resume(regs);
  238. /* deal with pending signal delivery */
  239. if (cached_flags & _TIF_SIGPENDING)
  240. do_signal(regs);
  241. if (cached_flags & _TIF_NOTIFY_RESUME) {
  242. clear_thread_flag(TIF_NOTIFY_RESUME);
  243. tracehook_notify_resume(regs);
  244. }
  245. if (cached_flags & _TIF_USER_RETURN_NOTIFY)
  246. fire_user_return_notifiers();
  247. /* Disable IRQs and retry */
  248. local_irq_disable();
  249. }
  250. user_enter();
  251. }
  252. /*
  253. * Called with IRQs on and fully valid regs. Returns with IRQs off in a
  254. * state such that we can immediately switch to user mode.
  255. */
  256. __visible void syscall_return_slowpath(struct pt_regs *regs)
  257. {
  258. struct thread_info *ti = pt_regs_to_thread_info(regs);
  259. u32 cached_flags = READ_ONCE(ti->flags);
  260. bool step;
  261. CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
  262. if (WARN(irqs_disabled(), "syscall %ld left IRQs disabled",
  263. regs->orig_ax))
  264. local_irq_enable();
  265. /*
  266. * First do one-time work. If these work items are enabled, we
  267. * want to run them exactly once per syscall exit with IRQs on.
  268. */
  269. if (cached_flags & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |
  270. _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)) {
  271. audit_syscall_exit(regs);
  272. if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
  273. trace_sys_exit(regs, regs->ax);
  274. /*
  275. * If TIF_SYSCALL_EMU is set, we only get here because of
  276. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  277. * We already reported this syscall instruction in
  278. * syscall_trace_enter().
  279. */
  280. step = unlikely(
  281. (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
  282. == _TIF_SINGLESTEP);
  283. if (step || cached_flags & _TIF_SYSCALL_TRACE)
  284. tracehook_report_syscall_exit(regs, step);
  285. }
  286. #ifdef CONFIG_COMPAT
  287. /*
  288. * Compat syscalls set TS_COMPAT. Make sure we clear it before
  289. * returning to user mode.
  290. */
  291. ti->status &= ~TS_COMPAT;
  292. #endif
  293. local_irq_disable();
  294. prepare_exit_to_usermode(regs);
  295. }
  296. /*
  297. * Deprecated notification of userspace execution resumption
  298. * - triggered by the TIF_WORK_MASK flags
  299. */
  300. __visible void
  301. do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
  302. {
  303. user_exit();
  304. if (thread_info_flags & _TIF_UPROBE)
  305. uprobe_notify_resume(regs);
  306. /* deal with pending signal delivery */
  307. if (thread_info_flags & _TIF_SIGPENDING)
  308. do_signal(regs);
  309. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  310. clear_thread_flag(TIF_NOTIFY_RESUME);
  311. tracehook_notify_resume(regs);
  312. }
  313. if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
  314. fire_user_return_notifiers();
  315. user_enter();
  316. }