Introduce the STACKLEAK feature and a test for it
Subsystems (9)
v4 cover Introduce the STACKLEAK feature and a test for it
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed by some kernel stack leak bugs
(e.g. CVE-2016-4569 and CVE-2016-4578);
- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.
Further work:
- think of erasing the kernel stack after invoking EFI runtime services;
- try to port STACKLEAK to arm64 (Laura Abbott is working on it).
Changes in v4
1. Introduced the CONFIG_STACKLEAK_TRACK_MIN_SIZE parameter instead of hard-coded track-lowest-sp.
2. Carefully looked into the assertions in track_stack() and check_alloca().
- Fixed the incorrect BUG() condition in track_stack(), thanks to Tycho Andersen. Also disabled that check if CONFIG_VMAP_STACK is enabled.
- Fixed the surplus and erroneous code for calculating stack_left in check_alloca() on x86_64. That code repeats the work which is already done in get_stack_info() and it misses the fact that different exception stacks on x86_64 have different size.
3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho).
4. Added info about STACKLEAK to Documentation/security/self-protection.rst.
5. Improved the comments.
6. Decided not to change "unsigned long sp = (unsigned long)&sp" to current_stack_pointer. The original variant is more platform independent since current_stack_pointer has different type on x86 and arm.
Changes in v3
1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation, which is available from gcc sources;
- wonderful slides by Diego Novillo
https://www.airs.com/dnovillo/200711-GCC-Internals/
- nice introduction to gcc plugins at LWN
https://lwn.net/Articles/457543/
2. Improved the commit message and Kconfig description according the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.
3. Removed arch-specific ix86_cmodel check from stackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.
4. Fixed a minor mistake in stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from the ENTRY_BLOCK_PTR doesn't always go to that basic block.
So later it was changed it to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely. next_bb was still used for entry_bb assignment, which could cause the wrong value of the prologue_instrumented variable.
I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.
5. Introduced the STACKLEAK_POISON macro and renamed the config option according the feedback from Kees Cook.
Alexander Popov (3):
gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of
syscalls
lkdtm: Add a test for STACKLEAK
doc: self-protection: Add information about STACKLEAK feature
Documentation/security/self-protection.rst | 23 +- arch/Kconfig | 39 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 95 +++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 + drivers/misc/lkdtm_core.c | 2 + drivers/misc/lkdtm_stackleak.c | 139 ++++++++++ fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++ 21 files changed, 872 insertions(+), 12 deletions(-) create mode 100644 drivers/misc/lkdtm_stackleak.c create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
Signature · 1 line
2.7.4
RFC PATCH v4 1/3 gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which a kernel stack leak bug can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.
This feature consists of:
- the architecture-specific code filling the used part of the kernel
stack with a poison value before returning to the userspace;
- the STACKLEAK gcc plugin. It instruments the kernel code inserting the track_stack() call for tracking the lowest border of the kernel stack and check_alloca() call for checking alloca size.
The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/
This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.
--- arch/Kconfig | 39 ++++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 ++++++ arch/x86/entry/entry_64.S | 95 ++++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++++++ 16 files changed, 710 insertions(+), 3 deletions(-) create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
arch/Kconfig +39 -0
@@ -386,6 +386,13 @@ config SECCOMP_FILTER See Documentation/prctl/seccomp_filter.txt for details. +config HAVE_ARCH_STACKLEAK + bool + help + An architecture should select this if it has the code which + fills the used part of the kernel stack with the STACKLEAK_POISON + value before returning from system calls. + config HAVE_GCC_PLUGINS bool help
@@ -516,6 +523,38 @@ config GCC_PLUGIN_RANDSTRUCT_PERFORMANCE in structures. This reduces the performance hit of RANDSTRUCT at the cost of weakened randomization. +config GCC_PLUGIN_STACKLEAK + bool "Erase the kernel stack before returning from syscalls" + depends on GCC_PLUGINS + depends on HAVE_ARCH_STACKLEAK + help + This option makes the kernel erase the kernel stack before it + returns from a system call. That reduces the information which + a kernel stack leak bug can reveal and blocks some uninitialized + stack variable attacks. This option also provides runtime checks + for kernel stack overflow detection. + + The tradeoff is the performance impact: on a single CPU system kernel + compilation sees a 1% slowdown, other systems and workloads may vary + and you are advised to test this feature on your expected workload + before deploying it. + + This plugin was ported from grsecurity/PaX. More information at: + * https://grsecurity.net/ + * https://pax.grsecurity.net/ + +config STACKLEAK_TRACK_MIN_SIZE + int "Minimum stack frame size of functions tracked by STACKLEAK" + default 100 + range 0 4096 + depends on GCC_PLUGIN_STACKLEAK + help + The STACKLEAK gcc plugin instruments the kernel code for tracking + the lowest border of the kernel stack (and for some other purposes). + It inserts the track_stack() call for the functions with a stack + frame size greater than or equal to this parameter. If unsure, + leave the default value 100. + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/Kconfig +1 -0
@@ -114,6 +114,7 @@ config X86 select HAVE_ARCH_MMAP_RND_COMPAT_BITS if MMU && COMPAT select HAVE_ARCH_COMPAT_MMAP_BASES if MMU && COMPAT select HAVE_ARCH_SECCOMP_FILTER + select HAVE_ARCH_STACKLEAK select HAVE_ARCH_TRACEHOOK select HAVE_ARCH_TRANSPARENT_HUGEPAGE select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64
arch/x86/entry/common.c +14 -3
@@ -45,6 +45,12 @@ __visible inline void enter_from_user_mode(void)static inline void enter_from_user_mode (void ) {}#endif +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +asmlinkage void erase_kstack(void); +#else +static void erase_kstack (void ) {} +#endif +static void do_audit_syscall_entry (struct pt_regs *regs,u32 arch) {#ifdef CONFIG_X86_64
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs) emulated = true;if ((emulated || (work & _TIF_SYSCALL_TRACE)) && -tracehook_report_syscall_entry (regs)) +tracehook_report_syscall_entry (regs)) { +erase_kstack ();return -1L ; + }if (emulated)return -1L ;
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs) sd.args[5] = regs->bp; } - ret =__secure_computing (&sd); -if (ret ==-1 ) + ret =secure_computing (&sd); +if (ret ==-1 ) { +erase_kstack ();return ret; + } }#endif
@@ -127,6 +137,7 @@ static long syscall_trace_enter(struct pt_regs *regs)do_audit_syscall_entry (regs, arch); +erase_kstack ();return ret ?: regs->orig_ax; }
arch/x86/entry/entry_32.S +69 -0
87 lines not on this page · raw message
arch/x86/entry/entry_64.S +95 -0
125 lines not on this page · raw message
arch/x86/entry/entry_64_compat.S +8 -0
@@ -18,6 +18,12 @@ .section .entry.text, "ax" + .macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif + .endm + /* * 32-bit SYSENTER entry. *
@@ -228,6 +234,7 @@ GLOBAL(entry_SYSCALL_compat_after_hwframe) /* Opportunistic SYSRET */ sysret32_from_system_call: + erase_kstack TRACE_IRQS_ON /* User mode traces as IRQs on. */ movq RBX(%rsp), %rbx /* pt_regs->rbx */ movq RBP(%rsp), %rbp /* pt_regs->rbp */
@@ -335,6 +342,7 @@ ENTRY(entry_INT80_compat) .Lsyscall_32_done: /* Go back to user mode. */ + erase_kstack TRACE_IRQS_ON SWAPGS jmp restore_regs_and_iret
arch/x86/include/asm/processor.h +4 -0
@@ -477,6 +477,10 @@ struct thread_struct {mm_segment_t addr_limit; +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +unsigned long lowest_stack; +#endif + unsigned int sig_on_uaccess_err:1; unsigned int uaccess_err:1; /* uaccess failed */
arch/x86/kernel/asm-offsets.c +9 -0
@@ -37,6 +37,10 @@ void common(void) {BLANK ();OFFSET (TASK_TI_flags, task_struct, thread_info.flags);OFFSET (TASK_addr_limit, task_struct, thread.addr_limit); +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +OFFSET (TASK_lowest_stack, task_struct, thread.lowest_stack); +OFFSET (TASK_thread_sp0, task_struct, thread.sp0); +#endif BLANK ();OFFSET (crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
@@ -73,6 +77,11 @@ void common(void) {OFFSET (PV_MMU_read_cr2, pv_mmu_ops, read_cr2);#endif +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +BLANK (); +DEFINE (THREAD_SIZE_asm,THREAD_SIZE ); +#endif +#ifdef CONFIG_XEN BLANK ();OFFSET (XEN_vcpu_info_mask, vcpu_info, evtchn_upcall_mask);
arch/x86/kernel/dumpstack_32.c +12 -0
@@ -162,3 +162,15 @@ void show_regs(struct pt_regs *regs) }pr_cont ("\n" ); } + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +void __used check_alloca (unsigned long size) +{ +unsigned long sp = (unsigned long )&sp, stack_left; + +/* all kernel stacks are of the same size */ + stack_left = sp & (THREAD_SIZE -1 ); +BUG_ON (stack_left <256 || size >= stack_left -256 ); +} +EXPORT_SYMBOL (check_alloca); +#endif
arch/x86/kernel/dumpstack_64.c +15 -0
@@ -178,3 +178,18 @@ void show_regs(struct pt_regs *regs) }pr_cont ("\n" ); } + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +void __used check_alloca (unsigned long size) +{ +unsigned long sp = (unsigned long )&sp; +struct stack_info stack_info = {0 }; +unsigned long visit_mask =0 ; +unsigned long stack_left; + +BUG_ON (get_stack_info (&sp, current, &stack_info, &visit_mask)); + stack_left = sp - (unsigned long )stack_info.begin; +BUG_ON (stack_left <256 || size >= stack_left -256 ); +} +EXPORT_SYMBOL (check_alloca); +#endif
arch/x86/kernel/process_32.c +5 -0
@@ -136,6 +136,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, p->thread.sp0 = (unsigned long ) (childregs+1 );memset (p->thread.ptrace_bps,0 ,sizeof (p->thread.ptrace_bps)); +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + p->thread.lowest_stack = (unsigned long )task_stack_page (p) + +2 *sizeof (unsigned long ); +#endif +if (unlikely (p->flags &PF_KTHREAD )) {/* kernel thread */ memset (childregs,0 ,sizeof (struct pt_regs ));
arch/x86/kernel/process_64.c +5 -0
@@ -283,6 +283,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, p->thread.sp = (unsigned long ) fork_frame; p->thread.io_bitmap_ptr =NULL ; +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + p->thread.lowest_stack = (unsigned long )task_stack_page (p) + +2 *sizeof (unsigned long ); +#endif +savesegment (gs, p->thread.gsindex); p->thread.gsbase = p->thread.gsindex ?0 : me->thread.gsbase;savesegment (fs, p->thread.fsindex);
fs/exec.c +30 -0
@@ -1957,3 +1957,33 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd, argv, envp, flags); }#endif + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +void __used track_stack (void ) +{ +/* +* N.B. The arch-specific part of the STACKLEAK feature fills the +* kernel stack with the poison value, which has the register width. +* That code assumes that the value of thread.lowest_stack is aligned +* on the register width boundary. +* +* That is true for x86 and x86_64 because of the kernel stack +* alignment on these platforms (for details, see cc_stack_align in +* arch/x86/Makefile). Take care of that when you port STACKLEAK to +* new platforms. +*/ +unsigned long sp = (unsigned long )&sp; + +if (sp < current->thread.lowest_stack && + sp >= (unsigned long )task_stack_page (current) + +2 *sizeof (unsigned long )) { + current->thread.lowest_stack = sp; + } + +#ifndef CONFIG_VMAP_STACK +if (unlikely ((sp & (THREAD_SIZE -1 )) < (THREAD_SIZE /16 ))) +BUG (); +#endif /* !CONFIG_VMAP_STACK */ +} +EXPORT_SYMBOL (track_stack); +#endif /* CONFIG_GCC_PLUGIN_STACKLEAK */
include/linux/compiler.h +4 -0
@@ -624,4 +624,8 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s (_________p1); \ }) +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +# define STACKLEAK_POISON -0xBEEF +#endif +#endif /* __LINUX_COMPILER_H */
scripts/Makefile.gcc-plugins +3 -0
@@ -34,6 +34,9 @@ ifdef CONFIG_GCC_PLUGINS gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) += -fplugin-arg-randomize_layout_plugin-performance-mode + gcc-plugin-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak_plugin.so + gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) += -DSTACKLEAK_PLUGIN -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE) + GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
scripts/gcc-plugins/stackleak_plugin.c new file +397 -0
397 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC PATCH v4 2/3 lkdtm: Add a test for STACKLEAK
In reply to Alexander PopovIntroduce two lkdtm tests for the STACKLEAK feature: STACKLEAK_CHECK_ALLOCA and STACKLEAK_TRACK_STACK. Both of them check that the current task stack is properly erased (filled with STACKLEAK_POISON).
In addition, STACKLEAK_CHECK_ALLOCA tests that:
- check_alloca() allows alloca calls which don't exhaust the kernel stack;
- alloca calls which exhaust/overflow the kernel stack hit BUG() in
check_alloca().
And STACKLEAK_TRACK_STACK checks that exhausting the thread stack with a recursion is detected:
- it hits the BUG() in track_stack() if CONFIG_VMAP_STACK is not enabled,
- or hits the guard page otherwise.
--- drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 ++ drivers/misc/lkdtm_core.c | 2 + drivers/misc/lkdtm_stackleak.c | 139 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 drivers/misc/lkdtm_stackleak.c
drivers/misc/Makefile +3 -0
@@ -63,6 +63,9 @@ lkdtm-$(CONFIG_LKDTM) += lkdtm_perms.o lkdtm-$(CONFIG_LKDTM) += lkdtm_refcount.o lkdtm-$(CONFIG_LKDTM) += lkdtm_rodata_objcopy.o lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o +lkdtm-$(CONFIG_LKDTM) += lkdtm_stackleak.o + +KASAN_SANITIZE_lkdtm_stackleak.o := n KCOV_INSTRUMENT_lkdtm_rodata.o := n
drivers/misc/lkdtm.h +4 -0
@@ -82,4 +82,8 @@ void lkdtm_USERCOPY_STACK_FRAME_FROM(void);void lkdtm_USERCOPY_STACK_BEYOND (void );void lkdtm_USERCOPY_KERNEL (void ); +/* lkdtm_stackleak.c */ +void lkdtm_STACKLEAK_CHECK_ALLOCA (void ); +void lkdtm_STACKLEAK_TRACK_STACK (void ); +#endif
drivers/misc/lkdtm_core.c +2 -0
@@ -251,6 +251,8 @@ struct crashtype crashtypes[] = {CRASHTYPE (USERCOPY_STACK_FRAME_FROM ),CRASHTYPE (USERCOPY_STACK_BEYOND ),CRASHTYPE (USERCOPY_KERNEL ), +CRASHTYPE (STACKLEAK_CHECK_ALLOCA ), + CRASHTYPE(STACKLEAK_TRACK_STACK), };
drivers/misc/lkdtm_stackleak.c new file +139 -0
139 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC PATCH v4 3/3 doc: self-protection: Add information about STACKLEAK feature
In reply to Alexander PopovAdd information about STACKLEAK feature to "Stack depth overflow" and "Memory poisoning" sections of self-protection.rst.
--- Documentation/security/self-protection.rst | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
Documentation/security/self-protection.rst +14 -9
@@ -165,10 +165,15 @@ Stack depth overflow A less well understood attack is using a bug that triggers the kernel to consume stack memory with deep function calls or large stack allocations. With this attack it is possible to write beyond the end of -the kernel's preallocated stack space and into sensitive structures. Two -important changes need to be made for better protections: moving the -sensitive thread_info structure elsewhere, and adding a faulting memory -hole at the bottom of the stack to catch these overflows. +the kernel's preallocated stack space and into sensitive structures. +The combination of the following measures gives better protection: + +* moving the sensitive thread_info structure off the stack + (``CONFIG_THREAD_INFO_IN_TASK``); +* adding a faulting memory hole at the bottom of the stack to catch + these overflows (``CONFIG_VMAP_STACK``); +* runtime checking that alloca() calls don't overstep the stack boundary + (``CONFIG_GCC_PLUGIN_STACKLEAK``). Heap memory integrity ---------------------
@@ -287,11 +292,11 @@ sure structure holes are cleared. Memory poisoning ---------------- -When releasing memory, it is best to poison the contents (clear stack on -syscall return, wipe heap memory on a free), to avoid reuse attacks that -rely on the old contents of memory. This frustrates many uninitialized -variable attacks, stack content exposures, heap content exposures, and -use-after-free attacks. +When releasing memory, it is best to poison the contents, to avoid reuse +attacks that rely on the old contents of memory. E.g., clear stack on a +syscall return (``CONFIG_GCC_PLUGIN_STACKLEAK``), wipe heap memory on a +free. This frustrates many uninitialized variable attacks, stack content +exposures, heap content exposures, and use-after-free attacks. Destination tracking --------------------
Signature · 1 line
2.7.4
[kernel-hardening] Re: [PATCH RFC v4 1/3] gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
In reply to Alexander PopovQuoted 16 lines · Alexander Popov
The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which a kernel stack leak bug can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.
This feature consists of:
- the architecture-specific code filling the used part of the kernelstack with a poison value before returning to the userspace;- the STACKLEAK gcc plugin. It instruments the kernel code inserting the
track_stack()call for tracking the lowest border of the kernel stack andcheck_alloca()call for checking alloca size.The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/
Quoted 6 lines
This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.
Signed-off-by Alexander Popov <[email protected]>
Thanks for the continuing work on this!
If I can get some review from Andy or other x86 folks, I'd appreciate it. If they're happy, I'll add this to the gcc-plugins tree...
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Quoted 3 lines · Alexander Popov
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
I'm working on some entry changes that could potentially interact with this. I'll try to get them out for review soon.
[kernel-hardening] Re: [PATCH RFC v4 1/3] gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
In reply to Alexander PopovQuoted 174 lines · Alexander Popov
arch/x86/entry/entry_32.S +69
-0@@ -75,6 +75,71 @@ #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +/* For the detailed comments, see erase_kstack in entry_64.S */ +ENTRY(erase_kstack) + pushl %edi + pushl %ecx + pushl %eax + pushl %ebp + + movl PER_CPU_VAR(current_task), %ebp + mov TASK_lowest_stack(%ebp), %edi + mov $STACKLEAK_POISON, %eax + std + +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $2, %ecx + repne scasl + jecxz 2f + + cmp $2*16, %ecx + jc 2f + + mov $2*16, %ecx + repe scasl + jecxz 2f + jne 1b + +2: + cld + or $2*4, %edi + mov %esp, %ecx + sub %edi, %ecx + + cmp $THREAD_SIZE_asm, %ecx + jb 3f + ud2 + +3: + shr $2, %ecx + rep stosl + + /* + * TODO: sp0 on x86_32 is not reliable, right? + * Doubt because of the definition of cpu_current_top_of_stack + * in arch/x86/kernel/cpu/common.c. + */ + mov TASK_thread_sp0(%ebp), %edi + sub $128, %edi + mov %edi, TASK_lowest_stack(%ebp) + + popl %ebp + popl %eax + popl %ecx + popl %edi + ret +ENDPROC(erase_kstack) +#endif + /* * User gs save/restore *@@ -445,6 +510,8 @@ ENTRY(entry_SYSENTER_32) ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \ "jmp .Lsyscall_32_done", X86_FEATURE_XENPV + erase_kstack + /* Opportunistic SYSEXIT */ TRACE_IRQS_ON /* User mode traces as IRQs on. */ movl PT_EIP(%esp), %edx /* pt_regs->ip */@@ -531,6 +598,8 @@ ENTRY(entry_INT80_32) call do_int80_syscall_32 .Lsyscall_32_done: + erase_kstack + restore_all: TRACE_IRQS_IRET .Lrestore_all_notrace:arch/x86/entry/entry_64.S +72
-0@@ -59,6 +59,90 @@ END(native_usergs_sysret64) #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +ENTRY(erase_kstack) + pushq %rdi + pushq %rcx + pushq %rax + pushq %r11 + + movq PER_CPU_VAR(current_task), %r11 + mov TASK_lowest_stack(%r11), %rdi + mov $STACKLEAK_POISON, %rax + std + + /* + * Let's search for the poison value in the stack. + * Start from the lowest_stack and go to the bottom (see std above). + */ +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $3, %ecx + repne scasq + jecxz 2f /* Didn't find it. Go to poisoning. */ + + /* + * Found the poison value in the stack. Go to poisoning if there are + * less than 16 qwords left. + */ + cmp $2*8, %ecx + jc 2f + + /* + * Check that 16 further qwords contain poison (avoid false positives). + * If so, the part of the stack below the address in %rdi is likely + * to be poisoned. Otherwise we need to search deeper. + */ + mov $2*8, %ecx + repe scasq + jecxz 2f /* Poison the upper part of the stack. */ + jne 1b /* Search deeper. */ + +2: + /* + * Prepare the counter for poisoning the kernel stack between + * %rdi and %rsp. Two qwords at the bottom of the stack are reserved + * and should not be poisoned (see CONFIG_SCHED_STACK_END_CHECK). + */ + cld + or $2*8, %rdi + mov %esp, %ecx + sub %edi, %ecx + + /* Check that the counter value is sane. */ + cmp $THREAD_SIZE_asm, %rcx + jb 3f + ud2 + +3: + /* + * So let's write the poison value to the kernel stack. Start from the + * address in %rdi and move up (see cld above) to the address in %rsp + * (not included, used memory). + */ + shr $3, %ecx + rep stosq + + /* Set the lowest_stack value to the top_of_stack - 256. */
arch/x86/entry/entry_64.S END(native_usergs_sysret64) · line 143
Quoted 10 lines
arch/x86/entry/entry_64.S +10
-0+ mov TASK_thread_sp0(%r11), %rdi + sub $256, %rdi + mov %rdi, TASK_lowest_stack(%r11) + + popq %r11 + popq %rax + popq %rcx + popq %rdi + ret +ENDPROC(erase_kstack)
A couple of (first round) review observations:
- Why is the erase_kstack() function written in assembly, instead of plain C? The complexity and fragility of this patch could be reduced if it was moved to C.
- The GCC plugin adds instrumentation in form of extra 'track_stack()' and 'check_alloca()' calls. Could you please provide a frequency analysis of the impact of this: x86-64 defconfig vmlinux size before/after the patch, and the number of instrumentation function calls inserted, compared to the number of functions?
- Is there a debug facility to query the current (latest) lowest_stack value of any task in the system, via /proc? Observing this threshold over time would give a good idea about the typical work the clearing function has to perform for every system call.
- Please clean up the GCC plugin code to follow proper kernel coding style. The '//' comment lines in particular are a big eyesore, plus there are a lot of other stylistic variations as well that make the code unnecessarily difficult to read.
- Also, this patch is way too big - there's no reason why the GCC plugin and the stack erasure features should be introduced in the same patch, etc.
Thanks,
Ingo
[kernel-hardening] Re: [PATCH RFC v4 1/3] gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
Quoted 179 lines · Ingo Molnar
Quoted 176 lines · Alexander Popov
arch/x86/entry/entry_32.S +69
-0@@ -75,6 +75,71 @@ #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +/* For the detailed comments, see erase_kstack in entry_64.S */ +ENTRY(erase_kstack) + pushl %edi + pushl %ecx + pushl %eax + pushl %ebp + + movl PER_CPU_VAR(current_task), %ebp + mov TASK_lowest_stack(%ebp), %edi + mov $STACKLEAK_POISON, %eax + std + +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $2, %ecx + repne scasl + jecxz 2f + + cmp $2*16, %ecx + jc 2f + + mov $2*16, %ecx + repe scasl + jecxz 2f + jne 1b + +2: + cld + or $2*4, %edi + mov %esp, %ecx + sub %edi, %ecx + + cmp $THREAD_SIZE_asm, %ecx + jb 3f + ud2 + +3: + shr $2, %ecx + rep stosl + + /* + * TODO: sp0 on x86_32 is not reliable, right? + * Doubt because of the definition of cpu_current_top_of_stack + * in arch/x86/kernel/cpu/common.c. + */ + mov TASK_thread_sp0(%ebp), %edi + sub $128, %edi + mov %edi, TASK_lowest_stack(%ebp) + + popl %ebp + popl %eax + popl %ecx + popl %edi + ret +ENDPROC(erase_kstack) +#endif + /* * User gs save/restore *@@ -445,6 +510,8 @@ ENTRY(entry_SYSENTER_32) ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \ "jmp .Lsyscall_32_done", X86_FEATURE_XENPV + erase_kstack + /* Opportunistic SYSEXIT */ TRACE_IRQS_ON /* User mode traces as IRQs on. */ movl PT_EIP(%esp), %edx /* pt_regs->ip */@@ -531,6 +598,8 @@ ENTRY(entry_INT80_32) call do_int80_syscall_32 .Lsyscall_32_done: + erase_kstack + restore_all: TRACE_IRQS_IRET .Lrestore_all_notrace:arch/x86/entry/entry_64.S +74
-0@@ -59,6 +59,90 @@ END(native_usergs_sysret64) #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +ENTRY(erase_kstack) + pushq %rdi + pushq %rcx + pushq %rax + pushq %r11 + + movq PER_CPU_VAR(current_task), %r11 + mov TASK_lowest_stack(%r11), %rdi + mov $STACKLEAK_POISON, %rax + std + + /* + * Let's search for the poison value in the stack. + * Start from the lowest_stack and go to the bottom (see std above). + */ +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $3, %ecx + repne scasq + jecxz 2f /* Didn't find it. Go to poisoning. */ + + /* + * Found the poison value in the stack. Go to poisoning if there are + * less than 16 qwords left. + */ + cmp $2*8, %ecx + jc 2f + + /* + * Check that 16 further qwords contain poison (avoid false positives). + * If so, the part of the stack below the address in %rdi is likely + * to be poisoned. Otherwise we need to search deeper. + */ + mov $2*8, %ecx + repe scasq + jecxz 2f /* Poison the upper part of the stack. */ + jne 1b /* Search deeper. */ + +2: + /* + * Prepare the counter for poisoning the kernel stack between + * %rdi and %rsp. Two qwords at the bottom of the stack are reserved + * and should not be poisoned (see CONFIG_SCHED_STACK_END_CHECK). + */ + cld + or $2*8, %rdi + mov %esp, %ecx + sub %edi, %ecx + + /* Check that the counter value is sane. */ + cmp $THREAD_SIZE_asm, %rcx + jb 3f + ud2 + +3: + /* + * So let's write the poison value to the kernel stack. Start from the + * address in %rdi and move up (see cld above) to the address in %rsp + * (not included, used memory). + */ + shr $3, %ecx + rep stosq + + /* Set the lowest_stack value to the top_of_stack - 256. */ + mov TASK_thread_sp0(%r11), %rdi + sub $256, %rdi
Quoted 8 lines
arch/x86/entry/entry_64.S END(native_usergs_sysret64) · line 143
Quoted 8 lines
arch/x86/entry/entry_64.S +8
-0+ mov %rdi, TASK_lowest_stack(%r11) + + popq %r11 + popq %rax + popq %rcx + popq %rdi + ret +ENDPROC(erase_kstack)
Hello Ingo,
Thanks a lot for your review.
Quoted 4 lines
A couple of (first round) review observations:
- Why is the
erase_kstack()function written in assembly, instead of plain C? The complexity and fragility of this patch could be reduced if it was moved to C.
Let me shortly describe my tactics.
Initially the erase_kstack() function is written in assembly in Grsecurity/PaX patch. I've extracted the STACKLEAK feature from that huge patch and carefully learned it bit by bit (it's quite complex). There are several bugs which I've found and fixed in it (they are listed in the cover letter), but generally I stick to the initial implementation in order not to accidentally break it.
I've added the detailed comments describing erase_kstack() for x86_64. IMO this code is really cool (my respect to PaX Team). However, if you think that rewriting it in C is obligatory, I'll do that. But let me at first fix the other issues which you listed below.
Quoted 5 lines
- The GCC plugin adds instrumentation in form of extra '
track_stack()' and 'check_alloca()' calls. Could you please provide a frequency analysis of the impact of this: x86-64 defconfig vmlinux size before/after the patch, and the number of instrumentation function calls inserted, compared to the number of functions?
Ok, I'll provide that information.
Quoted 4 lines
- Is there a debug facility to query the current (latest)
lowest_stackvalue of any task in the system, via /proc? Observing this threshold over time would give a good idea about the typical work the clearing function has to perform for every system call.
Yes, I'll create a PoC for it and maybe return with some questions.
Quoted 4 lines
- Please clean up the GCC plugin code to follow proper kernel coding style. The '//' comment lines in particular are a big eyesore, plus there are a lot of other stylistic variations as well that make the code unnecessarily difficult to read.
Yes, sure, I'll fix it.
Which line length limit should I use? I'm asking because GCC plugins are written in C++ and, as I see, other plugins in scripts/gcc-plugins/ have some very long lines.
Quoted 2 lines
- Also, this patch is way too big - there's no reason why the GCC plugin and the stack erasure features should be introduced in the same patch, etc.
Ok, I'll split it.
Thanks again.
Best regards,
Alexander
[kernel-hardening] Re: [PATCH RFC v4 1/3] gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
Quoted 207 lines · Alexander Popov
Quoted 179 lines · Ingo Molnar
Quoted 176 lines · Alexander Popov
arch/x86/entry/entry_32.S +69
-0@@ -75,6 +75,71 @@ #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +/* For the detailed comments, see erase_kstack in entry_64.S */ +ENTRY(erase_kstack) + pushl %edi + pushl %ecx + pushl %eax + pushl %ebp + + movl PER_CPU_VAR(current_task), %ebp + mov TASK_lowest_stack(%ebp), %edi + mov $STACKLEAK_POISON, %eax + std + +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $2, %ecx + repne scasl + jecxz 2f + + cmp $2*16, %ecx + jc 2f + + mov $2*16, %ecx + repe scasl + jecxz 2f + jne 1b + +2: + cld + or $2*4, %edi + mov %esp, %ecx + sub %edi, %ecx + + cmp $THREAD_SIZE_asm, %ecx + jb 3f + ud2 + +3: + shr $2, %ecx + rep stosl + + /* + * TODO: sp0 on x86_32 is not reliable, right? + * Doubt because of the definition of cpu_current_top_of_stack + * in arch/x86/kernel/cpu/common.c. + */ + mov TASK_thread_sp0(%ebp), %edi + sub $128, %edi + mov %edi, TASK_lowest_stack(%ebp) + + popl %ebp + popl %eax + popl %ecx + popl %edi + ret +ENDPROC(erase_kstack) +#endif + /* * User gs save/restore *@@ -445,6 +510,8 @@ ENTRY(entry_SYSENTER_32) ALTERNATIVE "testl %eax, %eax; jz .Lsyscall_32_done", \ "jmp .Lsyscall_32_done", X86_FEATURE_XENPV + erase_kstack + /* Opportunistic SYSEXIT */ TRACE_IRQS_ON /* User mode traces as IRQs on. */ movl PT_EIP(%esp), %edx /* pt_regs->ip */@@ -531,6 +598,8 @@ ENTRY(entry_INT80_32) call do_int80_syscall_32 .Lsyscall_32_done: + erase_kstack + restore_all: TRACE_IRQS_IRET .Lrestore_all_notrace:arch/x86/entry/entry_64.S +74
-0@@ -59,6 +59,90 @@ END(native_usergs_sysret64) #endif .endm +.macro erase_kstack +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK + call erase_kstack +#endif +.endm + +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +ENTRY(erase_kstack) + pushq %rdi + pushq %rcx + pushq %rax + pushq %r11 + + movq PER_CPU_VAR(current_task), %r11 + mov TASK_lowest_stack(%r11), %rdi + mov $STACKLEAK_POISON, %rax + std + + /* + * Let's search for the poison value in the stack. + * Start from the lowest_stack and go to the bottom (see std above). + */ +1: + mov %edi, %ecx + and $THREAD_SIZE_asm - 1, %ecx + shr $3, %ecx + repne scasq + jecxz 2f /* Didn't find it. Go to poisoning. */ + + /* + * Found the poison value in the stack. Go to poisoning if there are + * less than 16 qwords left. + */ + cmp $2*8, %ecx + jc 2f + + /* + * Check that 16 further qwords contain poison (avoid false positives). + * If so, the part of the stack below the address in %rdi is likely + * to be poisoned. Otherwise we need to search deeper. + */ + mov $2*8, %ecx + repe scasq + jecxz 2f /* Poison the upper part of the stack. */ + jne 1b /* Search deeper. */ + +2: + /* + * Prepare the counter for poisoning the kernel stack between + * %rdi and %rsp. Two qwords at the bottom of the stack are reserved + * and should not be poisoned (see CONFIG_SCHED_STACK_END_CHECK). + */ + cld + or $2*8, %rdi + mov %esp, %ecx + sub %edi, %ecx + + /* Check that the counter value is sane. */ + cmp $THREAD_SIZE_asm, %rcx + jb 3f + ud2 + +3: + /* + * So let's write the poison value to the kernel stack. Start from the + * address in %rdi and move up (see cld above) to the address in %rsp + * (not included, used memory). + */ + shr $3, %ecx + rep stosq + + /* Set the lowest_stack value to the top_of_stack - 256. */ + mov TASK_thread_sp0(%r11), %rdi + sub $256, %rdiQuoted 8 lines
arch/x86/entry/entry_64.S END(native_usergs_sysret64) · line 143
Quoted 8 lines
arch/x86/entry/entry_64.S +8
-0+ mov %rdi, TASK_lowest_stack(%r11) + + popq %r11 + popq %rax + popq %rcx + popq %rdi + ret +ENDPROC(erase_kstack)Hello Ingo,
Thanks a lot for your review.
Quoted 4 lines
A couple of (first round) review observations:
- Why is the
erase_kstack()function written in assembly, instead of plain C? The complexity and fragility of this patch could be reduced if it was moved to C.Let me shortly describe my tactics.
Initially the
erase_kstack()function is written in assembly in Grsecurity/PaX patch. I've extracted the STACKLEAK feature from that huge patch and carefully learned it bit by bit (it's quite complex). There are several bugs which I've found and fixed in it (they are listed in the cover letter), but generally I stick to the initial implementation in order not to accidentally break it.
Quoted 5 lines
I've added the detailed comments describing
erase_kstack()forx86_64. IMO this code is really cool (my respect to PaX Team). However, if you think that rewriting it in C is obligatory, I'll do that. But let me at first fix the other issues which you listed below.
I played around with reworking my arm64 version in C. I had to do some tricks to save x0 which is used on the syscall fastpath. I'm concerned about relying on gcc to not place anything on the stack when we clear it. This might be mitigated if we don't make function calls, or maybe I should have more faith in gcc?
Thanks,
Laura
Quoted 93 lines · Alexander Popov
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed by some kernel stack leak bugs(e.g. CVE-2016-4569 and CVE-2016-4578);- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.Further work:
- think of erasing the kernel stack after invoking EFI runtime services;
- try to port STACKLEAK to arm64 (Laura Abbott is working on it).Changes in v4
1. Introduced the
CONFIG_STACKLEAK_TRACK_MIN_SIZEparameter instead of hard-coded track-lowest-sp.2. Carefully looked into the assertions in
track_stack()andcheck_alloca().
- Fixed the incorrectBUG()condition intrack_stack(), thanks to Tycho Andersen. Also disabled that check ifCONFIG_VMAP_STACKis enabled.
- Fixed the surplus and erroneous code for calculatingstack_leftincheck_alloca()onx86_64. That code repeats the work which is already done inget_stack_info()and it misses the fact that different exception stacks onx86_64have different size.3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho).
4. Added info about STACKLEAK to
Documentation/security/self-protection.rst.5. Improved the comments.
6. Decided not to change "unsigned long sp = (unsigned long)&sp" to
current_stack_pointer. The original variant is more platform independent sincecurrent_stack_pointerhas different type on x86 and arm.Changes in v3
1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation, which is available from gcc sources;
- wonderful slides by Diego Novillo
https://www.airs.com/dnovillo/200711-GCC-Internals/
- nice introduction to gcc plugins at LWN
https://lwn.net/Articles/457543/2. Improved the commit message and
Kconfigdescription according the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.3. Removed arch-specific
ix86_cmodelcheck fromstackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.4. Fixed a minor mistake in
stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from theENTRY_BLOCK_PTRdoesn't always go to that basic block.So later it was changed it to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely.
next_bbwas still used forentry_bbassignment, which could cause the wrong value of theprologue_instrumentedvariable.I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.
5. Introduced the
STACKLEAK_POISONmacro and renamed the config option according the feedback from Kees Cook.Alexander Popov (3):
gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of
syscalls
lkdtm: Add a test for STACKLEAK
doc: self-protection: Add information about STACKLEAK featureDocumentation/security/self-protection.rst | 23 +- arch/Kconfig | 39 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 95 +++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 + drivers/misc/lkdtm_core.c | 2 +
Quoted 9 lines
drivers/misc/lkdtm_stackleak.c | 139 ++++++++++ fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++ 21 files changed, 872 insertions(+), 12 deletions(-) create mode 100644 drivers/misc/lkdtm_stackleak.c create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
I tried this series with CVE-2017-14954 . That particular bug is not helped here because the poisoning has already been overwritten by other leaf functions. Given the call stack this may be a particularly bad case but I'm wondering how common this might be if we're only erasing at the end of a system call. One previous copy_to_user which has to go through the fault path can get fairly deep.
Thanks,
Laura
Quoted 108 lines · Laura Abbott
Quoted 101 lines · Alexander Popov
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed by some kernel stack leak bugs (e.g. CVE-2016-4569 and CVE-2016-4578);
- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.Further work:
- think of erasing the kernel stack after invoking EFI runtime services;
- try to port STACKLEAK to arm64 (Laura Abbott is working on it).Changes in v4
1. Introduced the
CONFIG_STACKLEAK_TRACK_MIN_SIZEparameter instead of hard-coded track-lowest-sp.2. Carefully looked into the assertions in
track_stack()andcheck_alloca().
- Fixed the incorrectBUG()condition intrack_stack(), thanks to Tycho Andersen. Also disabled that check ifCONFIG_VMAP_STACKis enabled.
- Fixed the surplus and erroneous code for calculatingstack_leftincheck_alloca()onx86_64. That code repeats the work which is already done inget_stack_info()and it misses the fact that different exception stacks onx86_64have different size.3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho).
4. Added info about STACKLEAK to
Documentation/security/self-protection.rst.5. Improved the comments.
6. Decided not to change "unsigned long sp = (unsigned long)&sp" to
current_stack_pointer. The original variant is more platform independent sincecurrent_stack_pointerhas different type on x86 and arm.Changes in v3
1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation, which is available from gcc sources;
- wonderful slides by Diego Novillohttps://www.airs.com/dnovillo/200711-GCC-Internals/ - nice introduction to gcc plugins at LWN https://lwn.net/Articles/457543/2. Improved the commit message and
Kconfigdescription according the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.3. Removed arch-specific
ix86_cmodelcheck fromstackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.4. Fixed a minor mistake in
stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from theENTRY_BLOCK_PTRdoesn't always go to that basic block.So later it was changed it to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely.
next_bbwas still used forentry_bbassignment, which could cause the wrong value of theprologue_instrumentedvariable.I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.
5. Introduced the
STACKLEAK_POISONmacro and renamed the config option according the feedback from Kees Cook.Alexander Popov (3):
gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of
syscalls
lkdtm: Add a test for STACKLEAK
doc: self-protection: Add information about STACKLEAK featureDocumentation/security/self-protection.rst | 23 +- arch/Kconfig | 39 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 95 +++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 + drivers/misc/lkdtm_core.c | 2 + drivers/misc/lkdtm_stackleak.c | 139 ++++++++++ fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++ 21 files changed, 872 insertions(+), 12 deletions(-)Quoted 3 lines
create mode 100644
drivers/misc/lkdtm_stackleak.ccreate mode 100644scripts/gcc-plugins/stackleak_plugin.c
Quoted 8 lines
I tried this series with CVE-2017-14954 . That particular bug is not helped here because the poisoning has already been overwritten by other leaf functions. Given the call stack this may be a particularly bad case but I'm wondering how common this might be if we're only erasing at the end of a system call. One previous
copy_to_userwhich has to go through the fault path can get fairly deep.
On x86, the bad guys can force this is using 32-bit fast syscalls for *any* syscall. I suppose we could wipe the stack on the way out of exception handlers, too...
OTOH, I think we should consider KASLR to be worthless against local attackers in general. See all the papers on TSX timing leaks, etc. It's a nice defense against a limited class of remote attack. And the plugin is still plausibly useful to protect more serious secrets.
Quoted 2 lines
Thanks,
Laura
Quoted 108 lines · Andy Lutomirski
Quoted 108 lines · Laura Abbott
Quoted 104 lines · Alexander Popov
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed by some kernel stack leak bugs (e.g. CVE-2016-4569 and CVE-2016-4578);
- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.Further work:
- think of erasing the kernel stack after invoking EFI runtime services;
- try to port STACKLEAK to arm64 (Laura Abbott is working on it).Changes in v4
1. Introduced the
CONFIG_STACKLEAK_TRACK_MIN_SIZEparameter instead of hard-coded track-lowest-sp.2. Carefully looked into the assertions in
track_stack()andcheck_alloca().
- Fixed the incorrectBUG()condition intrack_stack(), thanks to Tycho Andersen. Also disabled that check ifCONFIG_VMAP_STACKis enabled.
- Fixed the surplus and erroneous code for calculatingstack_leftincheck_alloca()onx86_64. That code repeats the work which is already done inget_stack_info()and it misses the fact that different exception stacks onx86_64have different size.3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho).
4. Added info about STACKLEAK to
Documentation/security/self-protection.rst.5. Improved the comments.
6. Decided not to change "unsigned long sp = (unsigned long)&sp" to
current_stack_pointer. The original variant is more platform independent sincecurrent_stack_pointerhas different type on x86 and arm.Changes in v3
1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation, which is available from gcc sources;
- wonderful slides by Diego Novillohttps://www.airs.com/dnovillo/200711-GCC-Internals/ - nice introduction to gcc plugins at LWN https://lwn.net/Articles/457543/2. Improved the commit message and
Kconfigdescription according the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.3. Removed arch-specific
ix86_cmodelcheck fromstackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.4. Fixed a minor mistake in
stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from theENTRY_BLOCK_PTRdoesn't always go to that basic block.So later it was changed it to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely.
next_bbwas still used forentry_bbassignment, which could cause the wrong value of theprologue_instrumentedvariable.I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.
5. Introduced the
STACKLEAK_POISONmacro and renamed the config option according the feedback from Kees Cook.Alexander Popov (3):
gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of
syscalls
lkdtm: Add a test for STACKLEAK
doc: self-protection: Add information about STACKLEAK featureDocumentation/security/self-protection.rst | 23 +- arch/Kconfig | 39 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 95 +++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 + drivers/misc/lkdtm_core.c | 2 + drivers/misc/lkdtm_stackleak.c | 139 ++++++++++ fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++ 21 files changed, 872 insertions(+), 12 deletions(-)create mode 100644
drivers/misc/lkdtm_stackleak.ccreate mode 100644scripts/gcc-plugins/stackleak_plugin.c
Quoted 7 lines
Quoted 7 lines
I tried this series with CVE-2017-14954 . That particular bug is not helped here because the poisoning has already been overwritten by other leaf functions. Given the call stack this may be a particularly bad case but I'm wondering how common this might be if we're only erasing at the end of a system call. One previous
copy_to_userwhich has to go through the fault path can get fairly deep.
Laura, thanks for your observation. I've tested Brad's PoC with STACKLEAK too and see similar results. There is only one STACKLEAK_POISON value in the leaked data. Other leaked data is put on the stack by the current syscall.
I don't know any statistics on infoleaks and I can't say how many of them would be neutralized by STACKLEAK. But, anyway, it is be better than nothing for those who accept the STACKLEAK performance penalty.
Quoted 3 lines
On x86, the bad guys can force this is using 32-bit fast syscalls for *any* syscall. I suppose we could wipe the stack on the way out of exception handlers, too...
Andy, excuse me, could you elaborate on that? Do you mean that there are some more cases when erase_kstack() should be called?
Quoted 4 lines
OTOH, I think we should consider KASLR to be worthless against local attackers in general. See all the papers on TSX timing leaks, etc. It's a nice defense against a limited class of remote attack. And the plugin is still plausibly useful to protect more serious secrets.
Thanks for that remark! Could you give some examples of such secrets which should not leak to the userspace?
Best regards,
Alexander
[kernel-hardening] Re: [PATCH RFC v4 1/3] gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of syscalls
In reply to Ingo MolnarHello Ingo,
Quoted 5 lines · Ingo Molnar
- The GCC plugin adds instrumentation in form of extra '
track_stack()' and 'check_alloca()' calls. Could you please provide a frequency analysis of the impact of this: x86-64 defconfig vmlinux size before/after the patch, and the number of instrumentation function calls inserted, compared to the number of functions?
Size of vmlinux (x86_64_defconfig):
file size:
- STACKLEAK disabled: 35014784 bytes
- STACKLEAK enabled: 35044952 bytes (+0.086%)
.text section size (calculated by size utility):
- STACKLEAK disabled: 10752983
- STACKLEAK enabled: 11062221 (+2.876%)
The readelf utility shows 45602 functions in vmlinux.
The STACKLEAK gcc plugin inserted 36 check_alloca() calls and 1265 track_stack() calls (42274 calls are inserted during GIMPLE pass and 41009 calls are deleted during RTL pass). So 2.853% of functions are instrumented.
I will add this information to the cover letter of the 5'th version, which I'm currently preparing.
Best regards,
Alexander
Quoted 125 lines · Alexander Popov
Quoted 108 lines · Andy Lutomirski
Quoted 108 lines · Laura Abbott
Quoted 104 lines · Alexander Popov
This is the 4th version of the patch introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed by some kernel stack leak bugs (e.g. CVE-2016-4569 and CVE-2016-4578);
- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.Further work:
- think of erasing the kernel stack after invoking EFI runtime services;
- try to port STACKLEAK to arm64 (Laura Abbott is working on it).Changes in v4
1. Introduced the
CONFIG_STACKLEAK_TRACK_MIN_SIZEparameter instead of hard-coded track-lowest-sp.2. Carefully looked into the assertions in
track_stack()andcheck_alloca().
- Fixed the incorrectBUG()condition intrack_stack(), thanks to Tycho Andersen. Also disabled that check ifCONFIG_VMAP_STACKis enabled.
- Fixed the surplus and erroneous code for calculatingstack_leftincheck_alloca()onx86_64. That code repeats the work which is already done inget_stack_info()and it misses the fact that different exception stacks onx86_64have different size.3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho).
4. Added info about STACKLEAK to
Documentation/security/self-protection.rst.5. Improved the comments.
6. Decided not to change "unsigned long sp = (unsigned long)&sp" to
current_stack_pointer. The original variant is more platform independent sincecurrent_stack_pointerhas different type on x86 and arm.Changes in v3
1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation, which is available from gcc sources;
- wonderful slides by Diego Novillohttps://www.airs.com/dnovillo/200711-GCC-Internals/ - nice introduction to gcc plugins at LWN https://lwn.net/Articles/457543/2. Improved the commit message and
Kconfigdescription according the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.3. Removed arch-specific
ix86_cmodelcheck fromstackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.4. Fixed a minor mistake in
stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from theENTRY_BLOCK_PTRdoesn't always go to that basic block.So later it was changed it to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely.
next_bbwas still used forentry_bbassignment, which could cause the wrong value of theprologue_instrumentedvariable.I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.
5. Introduced the
STACKLEAK_POISONmacro and renamed the config option according the feedback from Kees Cook.Alexander Popov (3):
gcc-plugins: Add STACKLEAK erasing the kernel stack at the end of
syscalls
lkdtm: Add a test for STACKLEAK
doc: self-protection: Add information about STACKLEAK featureDocumentation/security/self-protection.rst | 23 +- arch/Kconfig | 39 +++ arch/x86/Kconfig | 1 + arch/x86/entry/common.c | 17 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 95 +++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 4 + arch/x86/kernel/asm-offsets.c | 9 + arch/x86/kernel/dumpstack_32.c | 12 + arch/x86/kernel/dumpstack_64.c | 15 ++ arch/x86/kernel/process_32.c | 5 + arch/x86/kernel/process_64.c | 5 + drivers/misc/Makefile | 3 + drivers/misc/lkdtm.h | 4 + drivers/misc/lkdtm_core.c | 2 + drivers/misc/lkdtm_stackleak.c | 139 ++++++++++ fs/exec.c | 30 +++ include/linux/compiler.h | 4 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 397 +++++++++++++++++++++++++++++ 21 files changed, 872 insertions(+), 12 deletions(-)create mode 100644
drivers/misc/lkdtm_stackleak.ccreate mode 100644scripts/gcc-plugins/stackleak_plugin.cQuoted 7 lines
Quoted 7 lines
I tried this series with CVE-2017-14954 . That particular bug is not helped here because the poisoning has already been overwritten by other leaf functions. Given the call stack this may be a particularly bad case but I'm wondering how common this might be if we're only erasing at the end of a system call. One previous
copy_to_userwhich has to go through the fault path can get fairly deep.Laura, thanks for your observation. I've tested Brad's PoC with STACKLEAK too and see similar results. There is only one
STACKLEAK_POISONvalue in the leaked data. Other leaked data is put on the stack by the current syscall.I don't know any statistics on infoleaks and I can't say how many of them would be neutralized by STACKLEAK. But, anyway, it is be better than nothing for those who accept the STACKLEAK performance penalty.
Quoted 6 lines
Quoted 3 lines
On x86, the bad guys can force this is using 32-bit fast syscalls for *any* syscall. I suppose we could wipe the stack on the way out of exception handlers, too...
Andy, excuse me, could you elaborate on that? Do you mean that there are some more cases when
erase_kstack()should be called?
Kinda. I was thinking that certain exception handlers should erase their portion of the stack (i.e. from their entry frame to the bottom) when they return back to *kernel* mode. On x86, this should include at least #PF and probably #GP and #DB, too. #DB is weird, so it might not actually be needed.
Quoted 8 lines
Quoted 5 lines
OTOH, I think we should consider KASLR to be worthless against local attackers in general. See all the papers on TSX timing leaks, etc. It's a nice defense against a limited class of remote attack. And the plugin is still plausibly useful to protect more serious secrets.
Thanks for that remark! Could you give some examples of such secrets which should not leak to the userspace?
Crypto secrets. Stuff in /dev/urandom, for example. Keys, too.
Quoted 3 lines
Best regards,
Alexander
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Quoted 23 lines · Andy Lutomirski
Quoted 22 lines · Alexander Popov
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Laura Abbott
I tried this series with CVE-2017-14954 . That particular bug is not helped here because the poisoning has already been overwritten by other leaf functions. Given the call stack this may be a particularly bad case but I'm wondering how common this might be if we're only erasing at the end of a system call. One previous
copy_to_userwhich has to go through the fault path can get fairly deep.Laura, thanks for your observation. I've tested Brad's PoC with STACKLEAK too and see similar results. There is only one
STACKLEAK_POISONvalue in the leaked data. Other leaked data is put on the stack by the current syscall.I don't know any statistics on infoleaks and I can't say how many of them would be neutralized by STACKLEAK. But, anyway, it is be better than nothing for those who accept the STACKLEAK performance penalty.
Quoted 3 lines
On x86, the bad guys can force this is using 32-bit fast syscalls for *any* syscall. I suppose we could wipe the stack on the way out of exception handlers, too...
Quoted 6 lines
Quoted 2 lines
Andy, excuse me, could you elaborate on that? Do you mean that there are some more cases when
erase_kstack()should be called?Kinda. I was thinking that certain exception handlers should erase their portion of the stack (i.e. from their entry frame to the bottom) when they return back to *kernel* mode.
Andy, if we return to the kernel mode, which profit would we make out of cleaning exception stacks?
I've spent some time learning arch/x86/entry/. But, honestly, I need help with checking that erase_kstack() is called at the right places.
Anyway, I'll shortly send the 5-th version of the STACKLEAK patch series. And I'll include your idea into the "further work" section in the cover letter. Thank you.
Best regards,
Alexander