Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/entry/32: Add and check a stack canary for the SYSENTER stack

The first instruction of the SYSENTER entry runs on its own tiny
stack. That stack can be used if a #DB or NMI is delivered before
the SYSENTER prologue switches to a real stack.

We have code in place to prevent us from overflowing the tiny stack.
For added paranoia, add a canary to the stack and check it in
do_debug() -- that way, if something goes wrong with the #DB logic,
we'll eventually notice.

Signed-off-by: Andy Lutomirski <luto@kernel.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/6ff9a806f39098b166dc2c41c1db744df5272f29.1457578375.git.luto@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Andy Lutomirski and committed by
Ingo Molnar
2a41aa4f 7536656f

+13 -1
+2 -1
arch/x86/include/asm/processor.h
··· 299 299 300 300 #ifdef CONFIG_X86_32 301 301 /* 302 - * Space for the temporary SYSENTER stack: 302 + * Space for the temporary SYSENTER stack. 303 303 */ 304 + unsigned long SYSENTER_stack_canary; 304 305 unsigned long SYSENTER_stack[64]; 305 306 #endif 306 307
+3
arch/x86/kernel/process.c
··· 57 57 */ 58 58 .io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 }, 59 59 #endif 60 + #ifdef CONFIG_X86_32 61 + .SYSENTER_stack_canary = STACK_END_MAGIC, 62 + #endif 60 63 }; 61 64 EXPORT_PER_CPU_SYMBOL(cpu_tss); 62 65
+8
arch/x86/kernel/traps.c
··· 713 713 debug_stack_usage_dec(); 714 714 715 715 exit: 716 + #if defined(CONFIG_X86_32) 717 + /* 718 + * This is the most likely code path that involves non-trivial use 719 + * of the SYSENTER stack. Check that we haven't overrun it. 720 + */ 721 + WARN(this_cpu_read(cpu_tss.SYSENTER_stack_canary) != STACK_END_MAGIC, 722 + "Overran or corrupted SYSENTER stack\n"); 723 + #endif 716 724 ist_exit(regs); 717 725 } 718 726 NOKPROBE_SYMBOL(do_debug);