common.c 12 KB

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