common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/sched/task_stack.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/errno.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/tracehook.h>
  17. #include <linux/audit.h>
  18. #include <linux/seccomp.h>
  19. #include <linux/signal.h>
  20. #include <linux/export.h>
  21. #include <linux/context_tracking.h>
  22. #include <linux/user-return-notifier.h>
  23. #include <linux/nospec.h>
  24. #include <linux/uprobes.h>
  25. #include <linux/livepatch.h>
  26. #include <linux/syscalls.h>
  27. #include <asm/desc.h>
  28. #include <asm/traps.h>
  29. #include <asm/vdso.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/cpufeature.h>
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/syscalls.h>
  34. #ifdef CONFIG_CONTEXT_TRACKING
  35. /* Called on entry from user mode with IRQs off. */
  36. __visible inline void enter_from_user_mode(void)
  37. {
  38. CT_WARN_ON(ct_state() != CONTEXT_USER);
  39. user_exit_irqoff();
  40. }
  41. #else
  42. static inline void enter_from_user_mode(void) {}
  43. #endif
  44. static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
  45. {
  46. #ifdef CONFIG_X86_64
  47. if (arch == AUDIT_ARCH_X86_64) {
  48. audit_syscall_entry(regs->orig_ax, regs->di,
  49. regs->si, regs->dx, regs->r10);
  50. } else
  51. #endif
  52. {
  53. audit_syscall_entry(regs->orig_ax, regs->bx,
  54. regs->cx, regs->dx, regs->si);
  55. }
  56. }
  57. /*
  58. * Returns the syscall nr to run (which should match regs->orig_ax) or -1
  59. * to skip the syscall.
  60. */
  61. static long syscall_trace_enter(struct pt_regs *regs)
  62. {
  63. u32 arch = in_ia32_syscall() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
  64. struct thread_info *ti = current_thread_info();
  65. unsigned long ret = 0;
  66. bool emulated = false;
  67. u32 work;
  68. if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
  69. BUG_ON(regs != task_pt_regs(current));
  70. work = READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
  71. if (unlikely(work & _TIF_SYSCALL_EMU))
  72. emulated = true;
  73. if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
  74. tracehook_report_syscall_entry(regs))
  75. return -1L;
  76. if (emulated)
  77. return -1L;
  78. #ifdef CONFIG_SECCOMP
  79. /*
  80. * Do seccomp after ptrace, to catch any tracer changes.
  81. */
  82. if (work & _TIF_SECCOMP) {
  83. struct seccomp_data sd;
  84. sd.arch = arch;
  85. sd.nr = regs->orig_ax;
  86. sd.instruction_pointer = regs->ip;
  87. #ifdef CONFIG_X86_64
  88. if (arch == AUDIT_ARCH_X86_64) {
  89. sd.args[0] = regs->di;
  90. sd.args[1] = regs->si;
  91. sd.args[2] = regs->dx;
  92. sd.args[3] = regs->r10;
  93. sd.args[4] = regs->r8;
  94. sd.args[5] = regs->r9;
  95. } else
  96. #endif
  97. {
  98. sd.args[0] = regs->bx;
  99. sd.args[1] = regs->cx;
  100. sd.args[2] = regs->dx;
  101. sd.args[3] = regs->si;
  102. sd.args[4] = regs->di;
  103. sd.args[5] = regs->bp;
  104. }
  105. ret = __secure_computing(&sd);
  106. if (ret == -1)
  107. return ret;
  108. }
  109. #endif
  110. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  111. trace_sys_enter(regs, regs->orig_ax);
  112. do_audit_syscall_entry(regs, arch);
  113. return ret ?: regs->orig_ax;
  114. }
  115. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  116. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  117. _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY | _TIF_PATCH_PENDING)
  118. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  119. {
  120. /*
  121. * In order to return to user mode, we need to have IRQs off with
  122. * none of EXIT_TO_USERMODE_LOOP_FLAGS set. Several of these flags
  123. * can be set at any time on preemptable kernels if we have IRQs on,
  124. * so we need to loop. Disabling preemption wouldn't help: doing the
  125. * work to clear some of the flags can sleep.
  126. */
  127. while (true) {
  128. /* We have work to do. */
  129. local_irq_enable();
  130. if (cached_flags & _TIF_NEED_RESCHED)
  131. schedule();
  132. if (cached_flags & _TIF_UPROBE)
  133. uprobe_notify_resume(regs);
  134. if (cached_flags & _TIF_PATCH_PENDING)
  135. klp_update_patch_state(current);
  136. /* deal with pending signal delivery */
  137. if (cached_flags & _TIF_SIGPENDING)
  138. do_signal(regs);
  139. if (cached_flags & _TIF_NOTIFY_RESUME) {
  140. clear_thread_flag(TIF_NOTIFY_RESUME);
  141. tracehook_notify_resume(regs);
  142. rseq_handle_notify_resume(NULL, regs);
  143. }
  144. if (cached_flags & _TIF_USER_RETURN_NOTIFY)
  145. fire_user_return_notifiers();
  146. /* Disable IRQs and retry */
  147. local_irq_disable();
  148. cached_flags = READ_ONCE(current_thread_info()->flags);
  149. if (!(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
  150. break;
  151. }
  152. }
  153. /* Called with IRQs disabled. */
  154. __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
  155. {
  156. struct thread_info *ti = current_thread_info();
  157. u32 cached_flags;
  158. addr_limit_user_check();
  159. lockdep_assert_irqs_disabled();
  160. lockdep_sys_exit();
  161. cached_flags = READ_ONCE(ti->flags);
  162. if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
  163. exit_to_usermode_loop(regs, cached_flags);
  164. #ifdef CONFIG_COMPAT
  165. /*
  166. * Compat syscalls set TS_COMPAT. Make sure we clear it before
  167. * returning to user mode. We need to clear it *after* signal
  168. * handling, because syscall restart has a fixup for compat
  169. * syscalls. The fixup is exercised by the ptrace_syscall_32
  170. * selftest.
  171. *
  172. * We also need to clear TS_REGS_POKED_I386: the 32-bit tracer
  173. * special case only applies after poking regs and before the
  174. * very next return to user mode.
  175. */
  176. ti->status &= ~(TS_COMPAT|TS_I386_REGS_POKED);
  177. #endif
  178. user_enter_irqoff();
  179. }
  180. #define SYSCALL_EXIT_WORK_FLAGS \
  181. (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  182. _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)
  183. static void syscall_slow_exit_work(struct pt_regs *regs, u32 cached_flags)
  184. {
  185. bool step;
  186. audit_syscall_exit(regs);
  187. if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
  188. trace_sys_exit(regs, regs->ax);
  189. /*
  190. * If TIF_SYSCALL_EMU is set, we only get here because of
  191. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  192. * We already reported this syscall instruction in
  193. * syscall_trace_enter().
  194. */
  195. step = unlikely(
  196. (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
  197. == _TIF_SINGLESTEP);
  198. if (step || cached_flags & _TIF_SYSCALL_TRACE)
  199. tracehook_report_syscall_exit(regs, step);
  200. }
  201. /*
  202. * Called with IRQs on and fully valid regs. Returns with IRQs off in a
  203. * state such that we can immediately switch to user mode.
  204. */
  205. __visible inline void syscall_return_slowpath(struct pt_regs *regs)
  206. {
  207. struct thread_info *ti = current_thread_info();
  208. u32 cached_flags = READ_ONCE(ti->flags);
  209. CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
  210. if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
  211. WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
  212. local_irq_enable();
  213. rseq_syscall(regs);
  214. /*
  215. * First do one-time work. If these work items are enabled, we
  216. * want to run them exactly once per syscall exit with IRQs on.
  217. */
  218. if (unlikely(cached_flags & SYSCALL_EXIT_WORK_FLAGS))
  219. syscall_slow_exit_work(regs, cached_flags);
  220. local_irq_disable();
  221. prepare_exit_to_usermode(regs);
  222. }
  223. #ifdef CONFIG_X86_64
  224. __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs)
  225. {
  226. struct thread_info *ti;
  227. enter_from_user_mode();
  228. local_irq_enable();
  229. ti = current_thread_info();
  230. if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY)
  231. nr = syscall_trace_enter(regs);
  232. /*
  233. * NB: Native and x32 syscalls are dispatched from the same
  234. * table. The only functional difference is the x32 bit in
  235. * regs->orig_ax, which changes the behavior of some syscalls.
  236. */
  237. nr &= __SYSCALL_MASK;
  238. if (likely(nr < NR_syscalls)) {
  239. nr = array_index_nospec(nr, NR_syscalls);
  240. regs->ax = sys_call_table[nr](regs);
  241. }
  242. syscall_return_slowpath(regs);
  243. }
  244. #endif
  245. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  246. /*
  247. * Does a 32-bit syscall. Called with IRQs on in CONTEXT_KERNEL. Does
  248. * all entry and exit work and returns with IRQs off. This function is
  249. * extremely hot in workloads that use it, and it's usually called from
  250. * do_fast_syscall_32, so forcibly inline it to improve performance.
  251. */
  252. static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
  253. {
  254. struct thread_info *ti = current_thread_info();
  255. unsigned int nr = (unsigned int)regs->orig_ax;
  256. #ifdef CONFIG_IA32_EMULATION
  257. ti->status |= TS_COMPAT;
  258. #endif
  259. if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
  260. /*
  261. * Subtlety here: if ptrace pokes something larger than
  262. * 2^32-1 into orig_ax, this truncates it. This may or
  263. * may not be necessary, but it matches the old asm
  264. * behavior.
  265. */
  266. nr = syscall_trace_enter(regs);
  267. }
  268. if (likely(nr < IA32_NR_syscalls)) {
  269. nr = array_index_nospec(nr, IA32_NR_syscalls);
  270. #ifdef CONFIG_IA32_EMULATION
  271. regs->ax = ia32_sys_call_table[nr](regs);
  272. #else
  273. /*
  274. * It's possible that a 32-bit syscall implementation
  275. * takes a 64-bit parameter but nonetheless assumes that
  276. * the high bits are zero. Make sure we zero-extend all
  277. * of the args.
  278. */
  279. regs->ax = ia32_sys_call_table[nr](
  280. (unsigned int)regs->bx, (unsigned int)regs->cx,
  281. (unsigned int)regs->dx, (unsigned int)regs->si,
  282. (unsigned int)regs->di, (unsigned int)regs->bp);
  283. #endif /* CONFIG_IA32_EMULATION */
  284. }
  285. syscall_return_slowpath(regs);
  286. }
  287. /* Handles int $0x80 */
  288. __visible void do_int80_syscall_32(struct pt_regs *regs)
  289. {
  290. enter_from_user_mode();
  291. local_irq_enable();
  292. do_syscall_32_irqs_on(regs);
  293. }
  294. /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
  295. __visible long do_fast_syscall_32(struct pt_regs *regs)
  296. {
  297. /*
  298. * Called using the internal vDSO SYSENTER/SYSCALL32 calling
  299. * convention. Adjust regs so it looks like we entered using int80.
  300. */
  301. unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
  302. vdso_image_32.sym_int80_landing_pad;
  303. /*
  304. * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
  305. * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
  306. * Fix it up.
  307. */
  308. regs->ip = landing_pad;
  309. enter_from_user_mode();
  310. local_irq_enable();
  311. /* Fetch EBP from where the vDSO stashed it. */
  312. if (
  313. #ifdef CONFIG_X86_64
  314. /*
  315. * Micro-optimization: the pointer we're following is explicitly
  316. * 32 bits, so it can't be out of range.
  317. */
  318. __get_user(*(u32 *)&regs->bp,
  319. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  320. #else
  321. get_user(*(u32 *)&regs->bp,
  322. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  323. #endif
  324. ) {
  325. /* User code screwed up. */
  326. local_irq_disable();
  327. regs->ax = -EFAULT;
  328. prepare_exit_to_usermode(regs);
  329. return 0; /* Keep it simple: use IRET. */
  330. }
  331. /* Now this is just like a normal syscall. */
  332. do_syscall_32_irqs_on(regs);
  333. #ifdef CONFIG_X86_64
  334. /*
  335. * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
  336. * SYSRETL is available on all 64-bit CPUs, so we don't need to
  337. * bother with SYSEXIT.
  338. *
  339. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  340. * because the ECX fixup above will ensure that this is essentially
  341. * never the case.
  342. */
  343. return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
  344. regs->ip == landing_pad &&
  345. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
  346. #else
  347. /*
  348. * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
  349. *
  350. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  351. * because the ECX fixup above will ensure that this is essentially
  352. * never the case.
  353. *
  354. * We don't allow syscalls at all from VM86 mode, but we still
  355. * need to check VM, because we might be returning from sys_vm86.
  356. */
  357. return static_cpu_has(X86_FEATURE_SEP) &&
  358. regs->cs == __USER_CS && regs->ss == __USER_DS &&
  359. regs->ip == landing_pad &&
  360. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
  361. #endif
  362. }
  363. #endif