common.c 11 KB

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