common.c 13 KB

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