common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/nospec.h>
  23. #include <linux/uprobes.h>
  24. #include <asm/desc.h>
  25. #include <asm/traps.h>
  26. #include <asm/vdso.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/cpufeature.h>
  29. #define CREATE_TRACE_POINTS
  30. #include <trace/events/syscalls.h>
  31. static struct thread_info *pt_regs_to_thread_info(struct pt_regs *regs)
  32. {
  33. unsigned long top_of_stack =
  34. (unsigned long)(regs + 1) + TOP_OF_KERNEL_STACK_PADDING;
  35. return (struct thread_info *)(top_of_stack - THREAD_SIZE);
  36. }
  37. #ifdef CONFIG_CONTEXT_TRACKING
  38. /* Called on entry from user mode with IRQs off. */
  39. __visible void enter_from_user_mode(void)
  40. {
  41. CT_WARN_ON(ct_state() != CONTEXT_USER);
  42. user_exit();
  43. }
  44. #endif
  45. static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
  46. {
  47. #ifdef CONFIG_X86_64
  48. if (arch == AUDIT_ARCH_X86_64) {
  49. audit_syscall_entry(regs->orig_ax, regs->di,
  50. regs->si, regs->dx, regs->r10);
  51. } else
  52. #endif
  53. {
  54. audit_syscall_entry(regs->orig_ax, regs->bx,
  55. regs->cx, regs->dx, regs->si);
  56. }
  57. }
  58. /*
  59. * We can return 0 to resume the syscall or anything else to go to phase
  60. * 2. If we resume the syscall, we need to put something appropriate in
  61. * regs->orig_ax.
  62. *
  63. * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
  64. * are fully functional.
  65. *
  66. * For phase 2's benefit, our return value is:
  67. * 0: resume the syscall
  68. * 1: go to phase 2; no seccomp phase 2 needed
  69. * anything else: go to phase 2; pass return value to seccomp
  70. */
  71. unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
  72. {
  73. struct thread_info *ti = pt_regs_to_thread_info(regs);
  74. unsigned long ret = 0;
  75. u32 work;
  76. if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
  77. BUG_ON(regs != task_pt_regs(current));
  78. work = ACCESS_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
  79. #ifdef CONFIG_CONTEXT_TRACKING
  80. /*
  81. * If TIF_NOHZ is set, we are required to call user_exit() before
  82. * doing anything that could touch RCU.
  83. */
  84. if (work & _TIF_NOHZ) {
  85. enter_from_user_mode();
  86. work &= ~_TIF_NOHZ;
  87. }
  88. #endif
  89. #ifdef CONFIG_SECCOMP
  90. /*
  91. * Do seccomp first -- it should minimize exposure of other
  92. * code, and keeping seccomp fast is probably more valuable
  93. * than the rest of this.
  94. */
  95. if (work & _TIF_SECCOMP) {
  96. struct seccomp_data sd;
  97. sd.arch = arch;
  98. sd.nr = regs->orig_ax;
  99. sd.instruction_pointer = regs->ip;
  100. #ifdef CONFIG_X86_64
  101. if (arch == AUDIT_ARCH_X86_64) {
  102. sd.args[0] = regs->di;
  103. sd.args[1] = regs->si;
  104. sd.args[2] = regs->dx;
  105. sd.args[3] = regs->r10;
  106. sd.args[4] = regs->r8;
  107. sd.args[5] = regs->r9;
  108. } else
  109. #endif
  110. {
  111. sd.args[0] = regs->bx;
  112. sd.args[1] = regs->cx;
  113. sd.args[2] = regs->dx;
  114. sd.args[3] = regs->si;
  115. sd.args[4] = regs->di;
  116. sd.args[5] = regs->bp;
  117. }
  118. BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
  119. BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
  120. ret = seccomp_phase1(&sd);
  121. if (ret == SECCOMP_PHASE1_SKIP) {
  122. regs->orig_ax = -1;
  123. ret = 0;
  124. } else if (ret != SECCOMP_PHASE1_OK) {
  125. return ret; /* Go directly to phase 2 */
  126. }
  127. work &= ~_TIF_SECCOMP;
  128. }
  129. #endif
  130. /* Do our best to finish without phase 2. */
  131. if (work == 0)
  132. return ret; /* seccomp and/or nohz only (ret == 0 here) */
  133. #ifdef CONFIG_AUDITSYSCALL
  134. if (work == _TIF_SYSCALL_AUDIT) {
  135. /*
  136. * If there is no more work to be done except auditing,
  137. * then audit in phase 1. Phase 2 always audits, so, if
  138. * we audit here, then we can't go on to phase 2.
  139. */
  140. do_audit_syscall_entry(regs, arch);
  141. return 0;
  142. }
  143. #endif
  144. return 1; /* Something is enabled that we can't handle in phase 1 */
  145. }
  146. /* Returns the syscall nr to run (which should match regs->orig_ax). */
  147. long syscall_trace_enter_phase2(struct pt_regs *regs, u32 arch,
  148. unsigned long phase1_result)
  149. {
  150. struct thread_info *ti = pt_regs_to_thread_info(regs);
  151. long ret = 0;
  152. u32 work = ACCESS_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
  153. if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
  154. BUG_ON(regs != task_pt_regs(current));
  155. /*
  156. * If we stepped into a sysenter/syscall insn, it trapped in
  157. * kernel mode; do_debug() cleared TF and set TIF_SINGLESTEP.
  158. * If user-mode had set TF itself, then it's still clear from
  159. * do_debug() and we need to set it again to restore the user
  160. * state. If we entered on the slow path, TF was already set.
  161. */
  162. if (work & _TIF_SINGLESTEP)
  163. regs->flags |= X86_EFLAGS_TF;
  164. #ifdef CONFIG_SECCOMP
  165. /*
  166. * Call seccomp_phase2 before running the other hooks so that
  167. * they can see any changes made by a seccomp tracer.
  168. */
  169. if (phase1_result > 1 && seccomp_phase2(phase1_result)) {
  170. /* seccomp failures shouldn't expose any additional code. */
  171. return -1;
  172. }
  173. #endif
  174. if (unlikely(work & _TIF_SYSCALL_EMU))
  175. ret = -1L;
  176. if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
  177. tracehook_report_syscall_entry(regs))
  178. ret = -1L;
  179. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  180. trace_sys_enter(regs, regs->orig_ax);
  181. do_audit_syscall_entry(regs, arch);
  182. return ret ?: regs->orig_ax;
  183. }
  184. long syscall_trace_enter(struct pt_regs *regs)
  185. {
  186. u32 arch = is_ia32_task() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
  187. unsigned long phase1_result = syscall_trace_enter_phase1(regs, arch);
  188. if (phase1_result == 0)
  189. return regs->orig_ax;
  190. else
  191. return syscall_trace_enter_phase2(regs, arch, phase1_result);
  192. }
  193. #define EXIT_TO_USERMODE_LOOP_FLAGS \
  194. (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
  195. _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
  196. static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
  197. {
  198. /*
  199. * In order to return to user mode, we need to have IRQs off with
  200. * none of _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_USER_RETURN_NOTIFY,
  201. * _TIF_UPROBE, or _TIF_NEED_RESCHED set. Several of these flags
  202. * can be set at any time on preemptable kernels if we have IRQs on,
  203. * so we need to loop. Disabling preemption wouldn't help: doing the
  204. * work to clear some of the flags can sleep.
  205. */
  206. while (true) {
  207. /* We have work to do. */
  208. local_irq_enable();
  209. if (cached_flags & _TIF_NEED_RESCHED)
  210. schedule();
  211. if (cached_flags & _TIF_UPROBE)
  212. uprobe_notify_resume(regs);
  213. /* deal with pending signal delivery */
  214. if (cached_flags & _TIF_SIGPENDING)
  215. do_signal(regs);
  216. if (cached_flags & _TIF_NOTIFY_RESUME) {
  217. clear_thread_flag(TIF_NOTIFY_RESUME);
  218. tracehook_notify_resume(regs);
  219. }
  220. if (cached_flags & _TIF_USER_RETURN_NOTIFY)
  221. fire_user_return_notifiers();
  222. /* Disable IRQs and retry */
  223. local_irq_disable();
  224. cached_flags = READ_ONCE(pt_regs_to_thread_info(regs)->flags);
  225. if (!(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
  226. break;
  227. }
  228. }
  229. /* Called with IRQs disabled. */
  230. __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
  231. {
  232. struct thread_info *ti = pt_regs_to_thread_info(regs);
  233. u32 cached_flags;
  234. if (IS_ENABLED(CONFIG_PROVE_LOCKING) && WARN_ON(!irqs_disabled()))
  235. local_irq_disable();
  236. lockdep_sys_exit();
  237. cached_flags = READ_ONCE(ti->flags);
  238. if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
  239. exit_to_usermode_loop(regs, cached_flags);
  240. #ifdef CONFIG_COMPAT
  241. /*
  242. * Compat syscalls set TS_COMPAT. Make sure we clear it before
  243. * returning to user mode. We need to clear it *after* signal
  244. * handling, because syscall restart has a fixup for compat
  245. * syscalls. The fixup is exercised by the ptrace_syscall_32
  246. * selftest.
  247. */
  248. ti->status &= ~TS_COMPAT;
  249. #endif
  250. user_enter();
  251. }
  252. #define SYSCALL_EXIT_WORK_FLAGS \
  253. (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
  254. _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)
  255. static void syscall_slow_exit_work(struct pt_regs *regs, u32 cached_flags)
  256. {
  257. bool step;
  258. audit_syscall_exit(regs);
  259. if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
  260. trace_sys_exit(regs, regs->ax);
  261. /*
  262. * If TIF_SYSCALL_EMU is set, we only get here because of
  263. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  264. * We already reported this syscall instruction in
  265. * syscall_trace_enter().
  266. */
  267. step = unlikely(
  268. (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
  269. == _TIF_SINGLESTEP);
  270. if (step || cached_flags & _TIF_SYSCALL_TRACE)
  271. tracehook_report_syscall_exit(regs, step);
  272. }
  273. /*
  274. * Called with IRQs on and fully valid regs. Returns with IRQs off in a
  275. * state such that we can immediately switch to user mode.
  276. */
  277. __visible inline void syscall_return_slowpath(struct pt_regs *regs)
  278. {
  279. struct thread_info *ti = pt_regs_to_thread_info(regs);
  280. u32 cached_flags = READ_ONCE(ti->flags);
  281. CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
  282. if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
  283. WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
  284. local_irq_enable();
  285. /*
  286. * First do one-time work. If these work items are enabled, we
  287. * want to run them exactly once per syscall exit with IRQs on.
  288. */
  289. if (unlikely(cached_flags & SYSCALL_EXIT_WORK_FLAGS))
  290. syscall_slow_exit_work(regs, cached_flags);
  291. local_irq_disable();
  292. prepare_exit_to_usermode(regs);
  293. }
  294. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  295. /*
  296. * Does a 32-bit syscall. Called with IRQs on and does all entry and
  297. * exit work and returns with IRQs off. This function is extremely hot
  298. * in workloads that use it, and it's usually called from
  299. * do_fast_syscall_32, so forcibly inline it to improve performance.
  300. */
  301. #ifdef CONFIG_X86_32
  302. /* 32-bit kernels use a trap gate for INT80, and the asm code calls here. */
  303. __visible
  304. #else
  305. /* 64-bit kernels use do_syscall_32_irqs_off() instead. */
  306. static
  307. #endif
  308. __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
  309. {
  310. struct thread_info *ti = pt_regs_to_thread_info(regs);
  311. unsigned int nr = (unsigned int)regs->orig_ax;
  312. #ifdef CONFIG_IA32_EMULATION
  313. ti->status |= TS_COMPAT;
  314. #endif
  315. if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
  316. /*
  317. * Subtlety here: if ptrace pokes something larger than
  318. * 2^32-1 into orig_ax, this truncates it. This may or
  319. * may not be necessary, but it matches the old asm
  320. * behavior.
  321. */
  322. nr = syscall_trace_enter(regs);
  323. }
  324. if (likely(nr < IA32_NR_syscalls)) {
  325. nr = array_index_nospec(nr, IA32_NR_syscalls);
  326. /*
  327. * It's possible that a 32-bit syscall implementation
  328. * takes a 64-bit parameter but nonetheless assumes that
  329. * the high bits are zero. Make sure we zero-extend all
  330. * of the args.
  331. */
  332. regs->ax = ia32_sys_call_table[nr](
  333. (unsigned int)regs->bx, (unsigned int)regs->cx,
  334. (unsigned int)regs->dx, (unsigned int)regs->si,
  335. (unsigned int)regs->di, (unsigned int)regs->bp);
  336. }
  337. syscall_return_slowpath(regs);
  338. }
  339. #ifdef CONFIG_X86_64
  340. /* Handles INT80 on 64-bit kernels */
  341. __visible void do_syscall_32_irqs_off(struct pt_regs *regs)
  342. {
  343. local_irq_enable();
  344. do_syscall_32_irqs_on(regs);
  345. }
  346. #endif
  347. /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
  348. __visible long do_fast_syscall_32(struct pt_regs *regs)
  349. {
  350. /*
  351. * Called using the internal vDSO SYSENTER/SYSCALL32 calling
  352. * convention. Adjust regs so it looks like we entered using int80.
  353. */
  354. unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
  355. vdso_image_32.sym_int80_landing_pad;
  356. /*
  357. * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
  358. * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
  359. * Fix it up.
  360. */
  361. regs->ip = landing_pad;
  362. /*
  363. * Fetch EBP from where the vDSO stashed it.
  364. *
  365. * WARNING: We are in CONTEXT_USER and RCU isn't paying attention!
  366. */
  367. local_irq_enable();
  368. if (
  369. #ifdef CONFIG_X86_64
  370. /*
  371. * Micro-optimization: the pointer we're following is explicitly
  372. * 32 bits, so it can't be out of range.
  373. */
  374. __get_user(*(u32 *)&regs->bp,
  375. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  376. #else
  377. get_user(*(u32 *)&regs->bp,
  378. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  379. #endif
  380. ) {
  381. /* User code screwed up. */
  382. local_irq_disable();
  383. regs->ax = -EFAULT;
  384. #ifdef CONFIG_CONTEXT_TRACKING
  385. enter_from_user_mode();
  386. #endif
  387. prepare_exit_to_usermode(regs);
  388. return 0; /* Keep it simple: use IRET. */
  389. }
  390. /* Now this is just like a normal syscall. */
  391. do_syscall_32_irqs_on(regs);
  392. #ifdef CONFIG_X86_64
  393. /*
  394. * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
  395. * SYSRETL is available on all 64-bit CPUs, so we don't need to
  396. * bother with SYSEXIT.
  397. *
  398. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  399. * because the ECX fixup above will ensure that this is essentially
  400. * never the case.
  401. */
  402. return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
  403. regs->ip == landing_pad &&
  404. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
  405. #else
  406. /*
  407. * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
  408. *
  409. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  410. * because the ECX fixup above will ensure that this is essentially
  411. * never the case.
  412. *
  413. * We don't allow syscalls at all from VM86 mode, but we still
  414. * need to check VM, because we might be returning from sys_vm86.
  415. */
  416. return static_cpu_has(X86_FEATURE_SEP) &&
  417. regs->cs == __USER_CS && regs->ss == __USER_DS &&
  418. regs->ip == landing_pad &&
  419. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
  420. #endif
  421. }
  422. #endif