common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /*
  241. * Called with IRQs on and fully valid regs. Returns with IRQs off in a
  242. * state such that we can immediately switch to user mode.
  243. */
  244. __visible void syscall_return_slowpath(struct pt_regs *regs)
  245. {
  246. struct thread_info *ti = pt_regs_to_thread_info(regs);
  247. u32 cached_flags = READ_ONCE(ti->flags);
  248. bool step;
  249. CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
  250. if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
  251. WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
  252. local_irq_enable();
  253. /*
  254. * First do one-time work. If these work items are enabled, we
  255. * want to run them exactly once per syscall exit with IRQs on.
  256. */
  257. if (cached_flags & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT |
  258. _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)) {
  259. audit_syscall_exit(regs);
  260. if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
  261. trace_sys_exit(regs, regs->ax);
  262. /*
  263. * If TIF_SYSCALL_EMU is set, we only get here because of
  264. * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
  265. * We already reported this syscall instruction in
  266. * syscall_trace_enter().
  267. */
  268. step = unlikely(
  269. (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
  270. == _TIF_SINGLESTEP);
  271. if (step || cached_flags & _TIF_SYSCALL_TRACE)
  272. tracehook_report_syscall_exit(regs, step);
  273. }
  274. #ifdef CONFIG_COMPAT
  275. /*
  276. * Compat syscalls set TS_COMPAT. Make sure we clear it before
  277. * returning to user mode.
  278. */
  279. ti->status &= ~TS_COMPAT;
  280. #endif
  281. local_irq_disable();
  282. prepare_exit_to_usermode(regs);
  283. }
  284. #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
  285. /*
  286. * Does a 32-bit syscall. Called with IRQs on and does all entry and
  287. * exit work and returns with IRQs off. This function is extremely hot
  288. * in workloads that use it, and it's usually called from
  289. * do_fast_syscall_32, so forcibly inline it to improve performance.
  290. */
  291. static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
  292. {
  293. struct thread_info *ti = pt_regs_to_thread_info(regs);
  294. unsigned int nr = (unsigned int)regs->orig_ax;
  295. #ifdef CONFIG_IA32_EMULATION
  296. ti->status |= TS_COMPAT;
  297. #endif
  298. if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
  299. /*
  300. * Subtlety here: if ptrace pokes something larger than
  301. * 2^32-1 into orig_ax, this truncates it. This may or
  302. * may not be necessary, but it matches the old asm
  303. * behavior.
  304. */
  305. nr = syscall_trace_enter(regs);
  306. }
  307. if (likely(nr < IA32_NR_syscalls)) {
  308. /*
  309. * It's possible that a 32-bit syscall implementation
  310. * takes a 64-bit parameter but nonetheless assumes that
  311. * the high bits are zero. Make sure we zero-extend all
  312. * of the args.
  313. */
  314. regs->ax = ia32_sys_call_table[nr](
  315. (unsigned int)regs->bx, (unsigned int)regs->cx,
  316. (unsigned int)regs->dx, (unsigned int)regs->si,
  317. (unsigned int)regs->di, (unsigned int)regs->bp);
  318. }
  319. syscall_return_slowpath(regs);
  320. }
  321. /* Handles int $0x80 */
  322. __visible void do_int80_syscall_32(struct pt_regs *regs)
  323. {
  324. local_irq_enable();
  325. do_syscall_32_irqs_on(regs);
  326. }
  327. /* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
  328. __visible long do_fast_syscall_32(struct pt_regs *regs)
  329. {
  330. /*
  331. * Called using the internal vDSO SYSENTER/SYSCALL32 calling
  332. * convention. Adjust regs so it looks like we entered using int80.
  333. */
  334. unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
  335. vdso_image_32.sym_int80_landing_pad;
  336. /*
  337. * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
  338. * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
  339. * Fix it up.
  340. */
  341. regs->ip = landing_pad;
  342. /*
  343. * Fetch ECX from where the vDSO stashed it.
  344. *
  345. * WARNING: We are in CONTEXT_USER and RCU isn't paying attention!
  346. */
  347. local_irq_enable();
  348. if (
  349. #ifdef CONFIG_X86_64
  350. /*
  351. * Micro-optimization: the pointer we're following is explicitly
  352. * 32 bits, so it can't be out of range.
  353. */
  354. __get_user(*(u32 *)&regs->cx,
  355. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  356. #else
  357. get_user(*(u32 *)&regs->cx,
  358. (u32 __user __force *)(unsigned long)(u32)regs->sp)
  359. #endif
  360. ) {
  361. /* User code screwed up. */
  362. local_irq_disable();
  363. regs->ax = -EFAULT;
  364. #ifdef CONFIG_CONTEXT_TRACKING
  365. enter_from_user_mode();
  366. #endif
  367. prepare_exit_to_usermode(regs);
  368. return 0; /* Keep it simple: use IRET. */
  369. }
  370. /* Now this is just like a normal syscall. */
  371. do_syscall_32_irqs_on(regs);
  372. #ifdef CONFIG_X86_64
  373. /*
  374. * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
  375. * SYSRETL is available on all 64-bit CPUs, so we don't need to
  376. * bother with SYSEXIT.
  377. *
  378. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  379. * because the ECX fixup above will ensure that this is essentially
  380. * never the case.
  381. */
  382. return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
  383. regs->ip == landing_pad &&
  384. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
  385. #else
  386. /*
  387. * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
  388. *
  389. * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
  390. * because the ECX fixup above will ensure that this is essentially
  391. * never the case.
  392. *
  393. * We don't allow syscalls at all from VM86 mode, but we still
  394. * need to check VM, because we might be returning from sys_vm86.
  395. */
  396. return static_cpu_has(X86_FEATURE_SEP) &&
  397. regs->cs == __USER_CS && regs->ss == __USER_DS &&
  398. regs->ip == landing_pad &&
  399. (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
  400. #endif
  401. }
  402. #endif