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