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