common.c 14 KB

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