blob: d69d1b6e6c3173f72c349615d9d7dc7459bb01c8 [file] [log] [blame]
Andy Lutomirski1f484aa2015-07-03 12:44:23 -07001/*
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/uprobes.h>
24
25#include <asm/desc.h>
26#include <asm/traps.h>
Andy Lutomirski710246d2015-10-05 17:48:10 -070027#include <asm/vdso.h>
28#include <asm/uaccess.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010029#include <asm/cpufeature.h>
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070030
31#define CREATE_TRACE_POINTS
32#include <trace/events/syscalls.h>
33
Andy Lutomirskidd636072015-10-05 17:48:22 -070034static struct thread_info *pt_regs_to_thread_info(struct pt_regs *regs)
35{
36 unsigned long top_of_stack =
37 (unsigned long)(regs + 1) + TOP_OF_KERNEL_STACK_PADDING;
38 return (struct thread_info *)(top_of_stack - THREAD_SIZE);
39}
40
Andy Lutomirskifeed36c2015-07-03 12:44:25 -070041#ifdef CONFIG_CONTEXT_TRACKING
42/* Called on entry from user mode with IRQs off. */
43__visible void enter_from_user_mode(void)
44{
45 CT_WARN_ON(ct_state() != CONTEXT_USER);
46 user_exit();
47}
48#endif
49
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070050static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
51{
52#ifdef CONFIG_X86_64
53 if (arch == AUDIT_ARCH_X86_64) {
54 audit_syscall_entry(regs->orig_ax, regs->di,
55 regs->si, regs->dx, regs->r10);
56 } else
57#endif
58 {
59 audit_syscall_entry(regs->orig_ax, regs->bx,
60 regs->cx, regs->dx, regs->si);
61 }
62}
63
64/*
65 * We can return 0 to resume the syscall or anything else to go to phase
66 * 2. If we resume the syscall, we need to put something appropriate in
67 * regs->orig_ax.
68 *
69 * NB: We don't have full pt_regs here, but regs->orig_ax and regs->ax
70 * are fully functional.
71 *
72 * For phase 2's benefit, our return value is:
73 * 0: resume the syscall
74 * 1: go to phase 2; no seccomp phase 2 needed
75 * anything else: go to phase 2; pass return value to seccomp
76 */
77unsigned long syscall_trace_enter_phase1(struct pt_regs *regs, u32 arch)
78{
Andy Lutomirskidd636072015-10-05 17:48:22 -070079 struct thread_info *ti = pt_regs_to_thread_info(regs);
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070080 unsigned long ret = 0;
81 u32 work;
82
Andy Lutomirski4aabd142015-10-05 17:48:21 -070083 if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
84 BUG_ON(regs != task_pt_regs(current));
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070085
Andy Lutomirskidd636072015-10-05 17:48:22 -070086 work = ACCESS_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070087
Andy Lutomirskifeed36c2015-07-03 12:44:25 -070088#ifdef CONFIG_CONTEXT_TRACKING
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070089 /*
90 * If TIF_NOHZ is set, we are required to call user_exit() before
91 * doing anything that could touch RCU.
92 */
93 if (work & _TIF_NOHZ) {
Andy Lutomirskifeed36c2015-07-03 12:44:25 -070094 enter_from_user_mode();
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070095 work &= ~_TIF_NOHZ;
96 }
Andy Lutomirskifeed36c2015-07-03 12:44:25 -070097#endif
Andy Lutomirski1f484aa2015-07-03 12:44:23 -070098
99#ifdef CONFIG_SECCOMP
100 /*
101 * Do seccomp first -- it should minimize exposure of other
102 * code, and keeping seccomp fast is probably more valuable
103 * than the rest of this.
104 */
105 if (work & _TIF_SECCOMP) {
106 struct seccomp_data sd;
107
108 sd.arch = arch;
109 sd.nr = regs->orig_ax;
110 sd.instruction_pointer = regs->ip;
111#ifdef CONFIG_X86_64
112 if (arch == AUDIT_ARCH_X86_64) {
113 sd.args[0] = regs->di;
114 sd.args[1] = regs->si;
115 sd.args[2] = regs->dx;
116 sd.args[3] = regs->r10;
117 sd.args[4] = regs->r8;
118 sd.args[5] = regs->r9;
119 } else
120#endif
121 {
122 sd.args[0] = regs->bx;
123 sd.args[1] = regs->cx;
124 sd.args[2] = regs->dx;
125 sd.args[3] = regs->si;
126 sd.args[4] = regs->di;
127 sd.args[5] = regs->bp;
128 }
129
130 BUILD_BUG_ON(SECCOMP_PHASE1_OK != 0);
131 BUILD_BUG_ON(SECCOMP_PHASE1_SKIP != 1);
132
133 ret = seccomp_phase1(&sd);
134 if (ret == SECCOMP_PHASE1_SKIP) {
135 regs->orig_ax = -1;
136 ret = 0;
137 } else if (ret != SECCOMP_PHASE1_OK) {
138 return ret; /* Go directly to phase 2 */
139 }
140
141 work &= ~_TIF_SECCOMP;
142 }
143#endif
144
145 /* Do our best to finish without phase 2. */
146 if (work == 0)
147 return ret; /* seccomp and/or nohz only (ret == 0 here) */
148
149#ifdef CONFIG_AUDITSYSCALL
150 if (work == _TIF_SYSCALL_AUDIT) {
151 /*
152 * If there is no more work to be done except auditing,
153 * then audit in phase 1. Phase 2 always audits, so, if
154 * we audit here, then we can't go on to phase 2.
155 */
156 do_audit_syscall_entry(regs, arch);
157 return 0;
158 }
159#endif
160
161 return 1; /* Something is enabled that we can't handle in phase 1 */
162}
163
164/* Returns the syscall nr to run (which should match regs->orig_ax). */
165long syscall_trace_enter_phase2(struct pt_regs *regs, u32 arch,
166 unsigned long phase1_result)
167{
Andy Lutomirskidd636072015-10-05 17:48:22 -0700168 struct thread_info *ti = pt_regs_to_thread_info(regs);
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700169 long ret = 0;
Andy Lutomirskidd636072015-10-05 17:48:22 -0700170 u32 work = ACCESS_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY;
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700171
Andy Lutomirski4aabd142015-10-05 17:48:21 -0700172 if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
173 BUG_ON(regs != task_pt_regs(current));
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700174
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700175#ifdef CONFIG_SECCOMP
176 /*
177 * Call seccomp_phase2 before running the other hooks so that
178 * they can see any changes made by a seccomp tracer.
179 */
180 if (phase1_result > 1 && seccomp_phase2(phase1_result)) {
181 /* seccomp failures shouldn't expose any additional code. */
182 return -1;
183 }
184#endif
185
186 if (unlikely(work & _TIF_SYSCALL_EMU))
187 ret = -1L;
188
189 if ((ret || test_thread_flag(TIF_SYSCALL_TRACE)) &&
190 tracehook_report_syscall_entry(regs))
191 ret = -1L;
192
193 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
194 trace_sys_enter(regs, regs->orig_ax);
195
196 do_audit_syscall_entry(regs, arch);
197
198 return ret ?: regs->orig_ax;
199}
200
201long syscall_trace_enter(struct pt_regs *regs)
202{
203 u32 arch = is_ia32_task() ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
204 unsigned long phase1_result = syscall_trace_enter_phase1(regs, arch);
205
206 if (phase1_result == 0)
207 return regs->orig_ax;
208 else
209 return syscall_trace_enter_phase2(regs, arch, phase1_result);
210}
211
Andy Lutomirski39b48e52015-10-05 17:48:23 -0700212#define EXIT_TO_USERMODE_LOOP_FLAGS \
213 (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
214 _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY)
215
216static void exit_to_usermode_loop(struct pt_regs *regs, u32 cached_flags)
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700217{
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700218 /*
219 * In order to return to user mode, we need to have IRQs off with
220 * none of _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_USER_RETURN_NOTIFY,
221 * _TIF_UPROBE, or _TIF_NEED_RESCHED set. Several of these flags
222 * can be set at any time on preemptable kernels if we have IRQs on,
223 * so we need to loop. Disabling preemption wouldn't help: doing the
224 * work to clear some of the flags can sleep.
225 */
226 while (true) {
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700227 /* We have work to do. */
228 local_irq_enable();
229
230 if (cached_flags & _TIF_NEED_RESCHED)
231 schedule();
232
233 if (cached_flags & _TIF_UPROBE)
234 uprobe_notify_resume(regs);
235
236 /* deal with pending signal delivery */
237 if (cached_flags & _TIF_SIGPENDING)
238 do_signal(regs);
239
240 if (cached_flags & _TIF_NOTIFY_RESUME) {
241 clear_thread_flag(TIF_NOTIFY_RESUME);
242 tracehook_notify_resume(regs);
243 }
244
245 if (cached_flags & _TIF_USER_RETURN_NOTIFY)
246 fire_user_return_notifiers();
247
248 /* Disable IRQs and retry */
249 local_irq_disable();
Andy Lutomirski39b48e52015-10-05 17:48:23 -0700250
251 cached_flags = READ_ONCE(pt_regs_to_thread_info(regs)->flags);
252
253 if (!(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
254 break;
255
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700256 }
Andy Lutomirski39b48e52015-10-05 17:48:23 -0700257}
258
259/* Called with IRQs disabled. */
260__visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
261{
Andy Lutomirski4e79e182016-02-10 14:15:27 -0800262 struct thread_info *ti = pt_regs_to_thread_info(regs);
Andy Lutomirski39b48e52015-10-05 17:48:23 -0700263 u32 cached_flags;
264
265 if (IS_ENABLED(CONFIG_PROVE_LOCKING) && WARN_ON(!irqs_disabled()))
266 local_irq_disable();
267
268 lockdep_sys_exit();
269
Andy Lutomirski4e79e182016-02-10 14:15:27 -0800270 cached_flags = READ_ONCE(ti->flags);
Andy Lutomirski39b48e52015-10-05 17:48:23 -0700271
272 if (unlikely(cached_flags & EXIT_TO_USERMODE_LOOP_FLAGS))
273 exit_to_usermode_loop(regs, cached_flags);
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700274
Andy Lutomirski4e79e182016-02-10 14:15:27 -0800275#ifdef CONFIG_COMPAT
276 /*
277 * Compat syscalls set TS_COMPAT. Make sure we clear it before
278 * returning to user mode. We need to clear it *after* signal
279 * handling, because syscall restart has a fixup for compat
280 * syscalls. The fixup is exercised by the ptrace_syscall_32
281 * selftest.
282 */
283 ti->status &= ~TS_COMPAT;
284#endif
285
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700286 user_enter();
287}
288
Andy Lutomirskif5e6a972015-10-05 17:48:24 -0700289#define SYSCALL_EXIT_WORK_FLAGS \
290 (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
291 _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)
292
293static void syscall_slow_exit_work(struct pt_regs *regs, u32 cached_flags)
294{
295 bool step;
296
297 audit_syscall_exit(regs);
298
299 if (cached_flags & _TIF_SYSCALL_TRACEPOINT)
300 trace_sys_exit(regs, regs->ax);
301
302 /*
303 * If TIF_SYSCALL_EMU is set, we only get here because of
304 * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP).
305 * We already reported this syscall instruction in
306 * syscall_trace_enter().
307 */
308 step = unlikely(
309 (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU))
310 == _TIF_SINGLESTEP);
311 if (step || cached_flags & _TIF_SYSCALL_TRACE)
312 tracehook_report_syscall_exit(regs, step);
313}
314
Andy Lutomirski1f484aa2015-07-03 12:44:23 -0700315/*
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700316 * Called with IRQs on and fully valid regs. Returns with IRQs off in a
317 * state such that we can immediately switch to user mode.
318 */
Andy Lutomirskif5e6a972015-10-05 17:48:24 -0700319__visible inline void syscall_return_slowpath(struct pt_regs *regs)
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700320{
321 struct thread_info *ti = pt_regs_to_thread_info(regs);
322 u32 cached_flags = READ_ONCE(ti->flags);
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700323
324 CT_WARN_ON(ct_state() != CONTEXT_KERNEL);
325
Andy Lutomirski460d1242015-10-05 17:48:18 -0700326 if (IS_ENABLED(CONFIG_PROVE_LOCKING) &&
327 WARN(irqs_disabled(), "syscall %ld left IRQs disabled", regs->orig_ax))
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700328 local_irq_enable();
329
330 /*
331 * First do one-time work. If these work items are enabled, we
332 * want to run them exactly once per syscall exit with IRQs on.
333 */
Andy Lutomirskif5e6a972015-10-05 17:48:24 -0700334 if (unlikely(cached_flags & SYSCALL_EXIT_WORK_FLAGS))
335 syscall_slow_exit_work(regs, cached_flags);
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700336
Andy Lutomirskic5c46f52015-07-03 12:44:26 -0700337 local_irq_disable();
338 prepare_exit_to_usermode(regs);
339}
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700340
Andy Lutomirski1e423bf2016-01-28 15:11:28 -0800341#ifdef CONFIG_X86_64
342__visible void do_syscall_64(struct pt_regs *regs)
343{
344 struct thread_info *ti = pt_regs_to_thread_info(regs);
345 unsigned long nr = regs->orig_ax;
346
347 local_irq_enable();
348
349 if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY)
350 nr = syscall_trace_enter(regs);
351
352 /*
353 * NB: Native and x32 syscalls are dispatched from the same
354 * table. The only functional difference is the x32 bit in
355 * regs->orig_ax, which changes the behavior of some syscalls.
356 */
357 if (likely((nr & __SYSCALL_MASK) < NR_syscalls)) {
358 regs->ax = sys_call_table[nr & __SYSCALL_MASK](
359 regs->di, regs->si, regs->dx,
360 regs->r10, regs->r8, regs->r9);
361 }
362
363 syscall_return_slowpath(regs);
364}
365#endif
366
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700367#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
368/*
Andy Lutomirski8b13c252015-10-05 17:48:17 -0700369 * Does a 32-bit syscall. Called with IRQs on and does all entry and
Andy Lutomirski33c521292015-10-05 17:48:19 -0700370 * exit work and returns with IRQs off. This function is extremely hot
371 * in workloads that use it, and it's usually called from
372 * do_fast_syscall_32, so forcibly inline it to improve performance.
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700373 */
Andy Lutomirskia798f092016-03-09 13:24:32 -0800374static __always_inline void do_syscall_32_irqs_on(struct pt_regs *regs)
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700375{
376 struct thread_info *ti = pt_regs_to_thread_info(regs);
377 unsigned int nr = (unsigned int)regs->orig_ax;
378
379#ifdef CONFIG_IA32_EMULATION
380 ti->status |= TS_COMPAT;
381#endif
382
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700383 if (READ_ONCE(ti->flags) & _TIF_WORK_SYSCALL_ENTRY) {
384 /*
385 * Subtlety here: if ptrace pokes something larger than
386 * 2^32-1 into orig_ax, this truncates it. This may or
387 * may not be necessary, but it matches the old asm
388 * behavior.
389 */
390 nr = syscall_trace_enter(regs);
391 }
392
Andy Lutomirski33c521292015-10-05 17:48:19 -0700393 if (likely(nr < IA32_NR_syscalls)) {
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700394 /*
395 * It's possible that a 32-bit syscall implementation
396 * takes a 64-bit parameter but nonetheless assumes that
397 * the high bits are zero. Make sure we zero-extend all
398 * of the args.
399 */
400 regs->ax = ia32_sys_call_table[nr](
401 (unsigned int)regs->bx, (unsigned int)regs->cx,
402 (unsigned int)regs->dx, (unsigned int)regs->si,
403 (unsigned int)regs->di, (unsigned int)regs->bp);
404 }
405
406 syscall_return_slowpath(regs);
407}
Andy Lutomirski710246d2015-10-05 17:48:10 -0700408
Andy Lutomirskia798f092016-03-09 13:24:32 -0800409/* Handles int $0x80 */
410__visible void do_int80_syscall_32(struct pt_regs *regs)
Andy Lutomirski8b13c252015-10-05 17:48:17 -0700411{
412 local_irq_enable();
413 do_syscall_32_irqs_on(regs);
414}
415
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700416/* Returns 0 to return using IRET or 1 to return using SYSEXIT/SYSRETL. */
Andy Lutomirski7841b402015-10-05 17:48:12 -0700417__visible long do_fast_syscall_32(struct pt_regs *regs)
Andy Lutomirski710246d2015-10-05 17:48:10 -0700418{
419 /*
420 * Called using the internal vDSO SYSENTER/SYSCALL32 calling
421 * convention. Adjust regs so it looks like we entered using int80.
422 */
423
424 unsigned long landing_pad = (unsigned long)current->mm->context.vdso +
425 vdso_image_32.sym_int80_landing_pad;
426
427 /*
428 * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward
429 * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
430 * Fix it up.
431 */
432 regs->ip = landing_pad;
433
434 /*
Andy Lutomirski30bfa7b2015-12-16 23:18:48 -0800435 * Fetch EBP from where the vDSO stashed it.
Andy Lutomirski710246d2015-10-05 17:48:10 -0700436 *
437 * WARNING: We are in CONTEXT_USER and RCU isn't paying attention!
438 */
439 local_irq_enable();
Andy Lutomirskic68ca672015-10-05 17:48:20 -0700440 if (
441#ifdef CONFIG_X86_64
442 /*
443 * Micro-optimization: the pointer we're following is explicitly
444 * 32 bits, so it can't be out of range.
445 */
Andy Lutomirski30bfa7b2015-12-16 23:18:48 -0800446 __get_user(*(u32 *)&regs->bp,
Andy Lutomirskic68ca672015-10-05 17:48:20 -0700447 (u32 __user __force *)(unsigned long)(u32)regs->sp)
448#else
Andy Lutomirski30bfa7b2015-12-16 23:18:48 -0800449 get_user(*(u32 *)&regs->bp,
Andy Lutomirskic68ca672015-10-05 17:48:20 -0700450 (u32 __user __force *)(unsigned long)(u32)regs->sp)
451#endif
452 ) {
453
Andy Lutomirski710246d2015-10-05 17:48:10 -0700454 /* User code screwed up. */
455 local_irq_disable();
456 regs->ax = -EFAULT;
457#ifdef CONFIG_CONTEXT_TRACKING
458 enter_from_user_mode();
459#endif
460 prepare_exit_to_usermode(regs);
Andy Lutomirski7841b402015-10-05 17:48:12 -0700461 return 0; /* Keep it simple: use IRET. */
Andy Lutomirski710246d2015-10-05 17:48:10 -0700462 }
Andy Lutomirski710246d2015-10-05 17:48:10 -0700463
464 /* Now this is just like a normal syscall. */
Andy Lutomirski8b13c252015-10-05 17:48:17 -0700465 do_syscall_32_irqs_on(regs);
Andy Lutomirski7841b402015-10-05 17:48:12 -0700466
467#ifdef CONFIG_X86_64
468 /*
469 * Opportunistic SYSRETL: if possible, try to return using SYSRETL.
470 * SYSRETL is available on all 64-bit CPUs, so we don't need to
471 * bother with SYSEXIT.
472 *
473 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
474 * because the ECX fixup above will ensure that this is essentially
475 * never the case.
476 */
477 return regs->cs == __USER32_CS && regs->ss == __USER_DS &&
478 regs->ip == landing_pad &&
479 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF)) == 0;
480#else
Andy Lutomirski5f310f72015-10-05 17:48:15 -0700481 /*
482 * Opportunistic SYSEXIT: if possible, try to return using SYSEXIT.
483 *
484 * Unlike 64-bit opportunistic SYSRET, we can't check that CX == IP,
485 * because the ECX fixup above will ensure that this is essentially
486 * never the case.
487 *
488 * We don't allow syscalls at all from VM86 mode, but we still
489 * need to check VM, because we might be returning from sys_vm86.
490 */
491 return static_cpu_has(X86_FEATURE_SEP) &&
492 regs->cs == __USER_CS && regs->ss == __USER_DS &&
493 regs->ip == landing_pad &&
494 (regs->flags & (X86_EFLAGS_RF | X86_EFLAGS_TF | X86_EFLAGS_VM)) == 0;
Andy Lutomirski7841b402015-10-05 17:48:12 -0700495#endif
Andy Lutomirski710246d2015-10-05 17:48:10 -0700496}
Andy Lutomirskibd2d3a32015-10-05 17:48:08 -0700497#endif