common.c 11 KB

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