Clean up x86_32 stackprotector
Subsystems (6)
Andy Lutomirski
maintainercover Clean up x86_32 stackprotector
x86_32 stackprotector is a maintenance nightmare. Clean it up. This disables stackprotector on x86_32 on GCC 8.1 and on all clang versions -- I'll file a bug for the latter.
Andy Lutomirski (2):
x86/stackprotector/32: Make the canary into a regular percpu variable x86/entry/32: Remove leftover macros after stackprotector cleanups
arch/x86/Kconfig | 7 +- arch/x86/Makefile | 4 + arch/x86/entry/entry_32.S | 95 +---------------------- arch/x86/include/asm/processor.h | 15 +--- arch/x86/include/asm/ptrace.h | 5 +- arch/x86/include/asm/segment.h | 27 ++----- arch/x86/include/asm/stackprotector.h | 79 ++++--------------- arch/x86/include/asm/suspend_32.h | 2 - arch/x86/kernel/asm-offsets_32.c | 5 -- arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/doublefault_32.c | 4 +- arch/x86/kernel/head_32.S | 18 +---- arch/x86/kernel/setup_percpu.c | 1 - arch/x86/kernel/tls.c | 8 +- arch/x86/kvm/svm/svm.c | 11 +-- arch/x86/lib/insn-eval.c | 4 - arch/x86/platform/pvh/head.S | 14 ---- arch/x86/power/cpu.c | 6 +- scripts/gcc-x86_32-has-stack-protector.sh | 6 +- 19 files changed, 56 insertions(+), 260 deletions(-)
Signature · 1 line
2.26.2
Andy Lutomirski
maintainerPATCH 1/2 x86/stackprotector/32: Make the canary into a regular percpu variable
On 32-bit kernels, the stackprotector canary is quite nasty -- it is stored at %gs:(20), which is nasty because 32-bit kernels use %fs for percpu storage. It's even nastier because it means that whether %gs contains userspace state or kernel state while running kernel code sepends on whether stackprotector is enabled (this is CONFIG_X86_32_LAZY_GS), and this setting radically changes the way that segment selectors work. Supporting both variants is a maintenance and testing mess.
Merely rearranging so that percpu and the stack canary share the same segment would be messy as the 32-bit percpu address layout isn't currently compatible with putting a variable at a fixed offset.
Fortunately, GCC 8.1 added options that allow the stack canary to be accessed as %fs:stack_canary, effectively turning it into an ordinary percpu variable. This lets us get rid of all of the code to manage the stack canary GDT descriptor and the CONFIG_X86_32_LAZY_GS mess.
This patch forcibly disables stackprotector on older compilers that don't support the new options and makes the stack canary into a percpu variable.
--- arch/x86/Kconfig | 7 +- arch/x86/Makefile | 4 ++ arch/x86/entry/entry_32.S | 56 ++-------------- arch/x86/include/asm/processor.h | 15 ++--- arch/x86/include/asm/ptrace.h | 5 +- arch/x86/include/asm/segment.h | 27 ++------ arch/x86/include/asm/stackprotector.h | 79 +++++------------------ arch/x86/include/asm/suspend_32.h | 2 - arch/x86/kernel/asm-offsets_32.c | 5 -- arch/x86/kernel/cpu/common.c | 5 +- arch/x86/kernel/doublefault_32.c | 4 +- arch/x86/kernel/head_32.S | 18 +----- arch/x86/kernel/setup_percpu.c | 1 - arch/x86/kernel/tls.c | 8 +-- arch/x86/kvm/svm/svm.c | 11 +--- arch/x86/lib/insn-eval.c | 4 -- arch/x86/platform/pvh/head.S | 14 ---- arch/x86/power/cpu.c | 6 +- scripts/gcc-x86_32-has-stack-protector.sh | 6 +- 19 files changed, 56 insertions(+), 221 deletions(-)
arch/x86/Kconfig +2 -5
@@ -346,10 +346,6 @@ config X86_64_SMP def_bool y depends on X86_64 && SMP -config X86_32_LAZY_GS - def_bool y - depends on X86_32 && !STACKPROTECTOR - config ARCH_SUPPORTS_UPROBES def_bool y
@@ -372,7 +368,8 @@ config CC_HAS_SANE_STACKPROTECTOR default $(success,$(srctree)/scripts/gcc-x86_32-has-stack-protector.sh $(CC)) help We have to make sure stack protector is unconditionally disabled if - the compiler produces broken code. + the compiler produces broken code or if it does not let us control + the segment on 32-bit kernels. menu "Processor type and features"
arch/x86/Makefile +4 -0
@@ -83,6 +83,10 @@ ifeq ($(CONFIG_X86_32),y) # temporary until string.h is fixed KBUILD_CFLAGS += -ffreestanding + + ifeq ($(CONFIG_STACKPROTECTOR),y) + KBUILD_CFLAGS += -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=stack_canary + endif else BITS := 64 UTS_MACHINE := x86_64
arch/x86/entry/entry_32.S +4 -52
81 lines not on this page · raw message
arch/x86/include/asm/processor.h +4 -11
@@ -441,6 +441,9 @@ struct fixed_percpu_data {* GCC hardcodes the stack canary as %gs:40. Since the * irq_stack is the object at %gs:0, we reserve the bottom * 48 bytes of the irq stack for the canary. +* +* Once we are willing to require -mstack-protector-guard-symbol= +* support for x86_64 stackprotector, we can get rid of this. */ char gs_base[40 ];unsigned long stack_canary;
@@ -461,17 +464,7 @@ extern asmlinkage void ignore_sysret(void);void current_save_fsgs (void );#else /* X86_64 */ #ifdef CONFIG_STACKPROTECTOR -/* -* Make sure stack canary segment base is cached-aligned: -* "For Intel Atom processors, avoid non zero segment base address -* that is not aligned to cache line boundary at all cost." -* (Optim Ref Manual Assembly/Compiler Coding Rule 15.) -*/ -struct stack_canary { -char __pad[20 ];/* canary at %gs:20 */ -unsigned long canary; -}; -DECLARE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); +DECLARE_PER_CPU(unsigned long, stack_canary);#endif /* Per CPU softirq stack pointer */ DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);
arch/x86/include/asm/ptrace.h +4 -1
@@ -37,7 +37,10 @@ struct pt_regs {unsigned short __esh;unsigned short fs;unsigned short __fsh; -/* On interrupt, gs and __gsh store the vector number. */ +/* +* On interrupt, gs and __gsh store the vector number. They never +* store gs any more. +*/ unsigned short gs;unsigned short __gsh;/* On interrupt, this is the error code. */
arch/x86/include/asm/segment.h +7 -20
@@ -158,12 +158,6 @@# define __KERNEL_PERCPU 0#endif -#ifdef CONFIG_STACKPROTECTOR -# define __KERNEL_STACK_CANARY (GDT_ENTRY_STACK_CANARY*8) -#else -# define __KERNEL_STACK_CANARY 0 -#endif -#else /* 64-bit: */ #include <asm/cache.h>
@@ -368,22 +362,15 @@ static inline void __loadsegment_fs(unsigned short value) asm("mov %%" #seg ",%0":"=r" (value) : : "memory")/* -* x86-32 user GS accessors: +* x86-32 user GS accessors. This is ugly and could do with some cleaning up. */#ifdef CONFIG_X86_32 -# ifdef CONFIG_X86_32_LAZY_GS -# define get_user_gs (regs) (u16)({ unsigned long v; savesegment(gs, v); v; }) -# define set_user_gs (regs, v) loadsegment(gs, (unsigned long)(v)) -# define task_user_gs (tsk) ((tsk)->thread.gs) -# define lazy_save_gs (v) savesegment(gs, (v)) -# define lazy_load_gs (v) loadsegment(gs, (v)) -# else /* X86_32_LAZY_GS */ -# define get_user_gs (regs) (u16)((regs)->gs) -# define set_user_gs (regs, v) do { (regs)->gs = (v); } while (0) -# define task_user_gs (tsk) (task_pt_regs(tsk)->gs) -# define lazy_save_gs (v) do { } while (0) -# define lazy_load_gs (v) do { } while (0) -# endif /* X86_32_LAZY_GS */ +# define get_user_gs (regs) (u16)({ unsigned long v; savesegment(gs, v); v; }) +# define set_user_gs (regs, v) loadsegment(gs, (unsigned long)(v)) +# define task_user_gs (tsk) ((tsk)->thread.gs) +# define lazy_save_gs (v) savesegment(gs, (v)) +# define lazy_load_gs (v) loadsegment(gs, (v)) +# define load_gs_index (v) loadsegment(gs, (v))#endif /* X86_32 */ #endif /* !__ASSEMBLY__ */
arch/x86/include/asm/stackprotector.h +16 -63
115 lines not on this page · raw message
arch/x86/include/asm/suspend_32.h +0 -2
@@ -16,9 +16,7 @@ struct saved_context {* On x86_32, all segment registers, with the possible exception of * gs, are saved at kernel entry in pt_regs. */ -#ifdef CONFIG_X86_32_LAZY_GS u16 gs; -#endif unsigned long cr0, cr2, cr3, cr4;u64 misc_enable;bool misc_enable_saved;
arch/x86/kernel/asm-offsets_32.c +0 -5
@@ -53,11 +53,6 @@ void foo(void) offsetof(struct cpu_entry_area, tss.x86_tss.sp1) - offsetofend(struct cpu_entry_area, entry_stack_page.stack)); -#ifdef CONFIG_STACKPROTECTOR -BLANK (); -OFFSET (stack_canary_offset, stack_canary, canary); -#endif -BLANK ();DEFINE (EFI_svam, offsetof(efi_runtime_services_t , set_virtual_address_map)); }
arch/x86/kernel/cpu/common.c +2 -3
@@ -160,7 +160,6 @@ DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), - GDT_STACK_CANARY_INIT#endif } };EXPORT_PER_CPU_SYMBOL_GPL (gdt_page);
@@ -598,7 +597,6 @@ void load_percpu_segment(int cpu)__loadsegment_simple (gs,0 );wrmsrl (MSR_GS_BASE ,cpu_kernelmode_gs_base (cpu));#endif -load_stack_canary_segment (); }#ifdef CONFIG_X86_32
@@ -1746,7 +1744,8 @@ DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) =EXPORT_PER_CPU_SYMBOL (cpu_current_top_of_stack);#ifdef CONFIG_STACKPROTECTOR -DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); +DEFINE_PER_CPU(unsigned long, stack_canary); +EXPORT_PER_CPU_SYMBOL (stack_canary);#endif #endif /* CONFIG_X86_64 */
arch/x86/kernel/doublefault_32.c +1 -3
@@ -100,9 +100,7 @@ DEFINE_PER_CPU_PAGE_ALIGNED(struct doublefault_stack, doublefault_stack) = { .ss = __KERNEL_DS, .ds = __USER_DS, .fs = __KERNEL_PERCPU, -#ifndef CONFIG_X86_32_LAZY_GS - .gs = __KERNEL_STACK_CANARY, -#endif + .gs = 0, .__cr3 = __pa_nodebug(swapper_pg_dir), },
arch/x86/kernel/head_32.S +2 -16
@@ -318,8 +318,8 @@ SYM_FUNC_START(startup_32_smp) movl $(__KERNEL_PERCPU), %eax movl %eax,%fs # set this cpu's percpu - movl $(__KERNEL_STACK_CANARY),%eax - movl %eax,%gs + xorl %eax,%eax + movl %eax,%gs # clear possible garbage in %gs xorl %eax,%eax # Clear LDT lldt %ax
@@ -339,20 +339,6 @@ SYM_FUNC_END(startup_32_smp) */ __INIT setup_once: -#ifdef CONFIG_STACKPROTECTOR - /* - * Configure the stack canary. The linker can't handle this by - * relocation. Manually set base address in stack canary - * segment descriptor. - */ - movl $gdt_page,%eax - movl $stack_canary,%ecx - movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax) - shrl $16, %ecx - movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax) - movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax) -#endif - andl $0,setup_once_ref /* Once is enough, thanks */ ret
arch/x86/kernel/setup_percpu.c +0 -1
@@ -224,7 +224,6 @@ void __init setup_per_cpu_areas(void)per_cpu (this_cpu_off, cpu) =per_cpu_offset (cpu);per_cpu (cpu_number, cpu) = cpu;setup_percpu_segment (cpu); -setup_stack_canary_segment (cpu);/* * Copy data used in early init routines from the * initial arrays to the per cpu data areas. These
arch/x86/kernel/tls.c +1 -7
@@ -164,17 +164,11 @@ int do_set_thread_area(struct task_struct *p, int idx,savesegment (fs, sel);if (sel == modified_sel)loadsegment (fs, sel); - -savesegment (gs, sel); -if (sel == modified_sel) -load_gs_index (sel);#endif -#ifdef CONFIG_X86_32_LAZY_GS savesegment (gs, sel);if (sel == modified_sel) -loadsegment (gs, sel); -#endif +load_gs_index (sel); }else {#ifdef CONFIG_X86_64 if (p->thread.fsindex == modified_sel)
arch/x86/kvm/svm/svm.c +3 -8
@@ -1313,14 +1313,11 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu) ++vcpu->stat.host_state_reload;kvm_load_ldt (svm->host.ldt);#ifdef CONFIG_X86_64 +/* NB: this code is rather buggy. */ loadsegment (fs, svm->host.fs);wrmsrl (MSR_KERNEL_GS_BASE , current->thread.gsbase); -load_gs_index (svm->host.gs); -#else -#ifdef CONFIG_X86_32_LAZY_GS -loadsegment (gs, svm->host.gs); -#endif #endif +load_gs_index (svm->host.gs);for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++)wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); }
@@ -3388,13 +3385,11 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,__svm_vcpu_run (svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs); +/* Restore the percpu segment immediately. */ #ifdef CONFIG_X86_64 native_wrmsrl (MSR_GS_BASE , svm->host.gs_base);#else loadsegment (fs, svm->host.fs); -#ifndef CONFIG_X86_32_LAZY_GS -loadsegment (gs, svm->host.gs); -#endif #endif /*
arch/x86/lib/insn-eval.c +0 -4
@@ -379,10 +379,6 @@ static short get_segment_selector(struct pt_regs *regs, int seg_reg_idx)case INAT_SEG_REG_FS :return (unsigned short )(regs->fs &0xffff );case INAT_SEG_REG_GS : -/* -* GS may or may not be in regs as per CONFIG_X86_32_LAZY_GS. -* The macro below takes care of both cases. -*/ return get_user_gs (regs);case INAT_SEG_REG_IGNORE :default :
arch/x86/platform/pvh/head.S +0 -14
@@ -45,10 +45,8 @@ #define PVH_GDT_ENTRY_CS 1 #define PVH_GDT_ENTRY_DS 2 -#define PVH_GDT_ENTRY_CANARY 3 #define PVH_CS_SEL (PVH_GDT_ENTRY_CS * 8) #define PVH_DS_SEL (PVH_GDT_ENTRY_DS * 8) -#define PVH_CANARY_SEL (PVH_GDT_ENTRY_CANARY * 8) SYM_CODE_START_LOCAL(pvh_start_xen) cld
@@ -109,17 +107,6 @@ SYM_CODE_START_LOCAL(pvh_start_xen) #else /* CONFIG_X86_64 */ - /* Set base address in stack canary descriptor. */ - movl $_pa(gdt_start),%eax - movl $_pa(canary),%ecx - movw %cx, (PVH_GDT_ENTRY_CANARY * 8) + 2(%eax) - shrl $16, %ecx - movb %cl, (PVH_GDT_ENTRY_CANARY * 8) + 4(%eax) - movb %ch, (PVH_GDT_ENTRY_CANARY * 8) + 7(%eax) - - mov $PVH_CANARY_SEL,%eax - mov %eax,%gs - call mk_early_pgtbl_32 mov $_pa(initial_page_table), %eax
@@ -163,7 +150,6 @@ SYM_DATA_START_LOCAL(gdt_start) .quad GDT_ENTRY(0xc09a, 0, 0xfffff) /* PVH_CS_SEL */ #endif .quad GDT_ENTRY(0xc092, 0, 0xfffff) /* PVH_DS_SEL */ - .quad GDT_ENTRY(0x4090, 0, 0x18) /* PVH_CANARY_SEL */ SYM_DATA_END_LABEL(gdt_start, SYM_L_LOCAL, gdt_end) .balign 16
arch/x86/power/cpu.c +1 -5
@@ -99,11 +99,8 @@ static void __save_processor_state(struct saved_context *ctxt)/* * segment registers */ -#ifdef CONFIG_X86_32_LAZY_GS savesegment (gs, ctxt->gs); -#endif #ifdef CONFIG_X86_64 -savesegment (gs, ctxt->gs);savesegment (fs, ctxt->fs);savesegment (ds, ctxt->ds);savesegment (es, ctxt->es);
@@ -232,7 +229,6 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)wrmsrl (MSR_GS_BASE , ctxt->kernelmode_gs_base);#else loadsegment (fs, __KERNEL_PERCPU); -loadsegment (gs, __KERNEL_STACK_CANARY);#endif /* Restore the TSS, RO GDT, LDT, and usermode-relevant MSRs. */
@@ -255,7 +251,7 @@ static void notrace __restore_processor_state(struct saved_context *ctxt)*/ wrmsrl (MSR_FS_BASE , ctxt->fs_base);wrmsrl (MSR_KERNEL_GS_BASE , ctxt->usermode_gs_base); -#elif defined(CONFIG_X86_32_LAZY_GS ) +#else loadsegment (gs, ctxt->gs);#endif
scripts/gcc-x86_32-has-stack-protector.sh +5 -1
@@ -1,4 +1,8 @@ #!/bin/sh # SPDX-License-Identifier: GPL-2.0 -echo "int foo(void) { char X[200]; return 3; }" | $* -S -x c -c -m32 -O0 -fstack-protector - -o - 2> /dev/null | grep -q "%gs" +# This requires GCC 8.1 or better. Specifically, we require +# -mstack-protector-guard-reg, added by +# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81708 + +echo "int foo(void) { char X[200]; return 3; }" | $* -S -x c -c -m32 -O0 -fstack-protector -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=stack_canary - -o - 2> /dev/null | grep -q "%fs"
Signature · 1 line
2.26.2
Andy Lutomirski
maintainerPATCH 2/2 x86/entry/32: Remove leftover macros after stackprotector cleanups
In reply to Andy LutomirskiNow that nonlazy-GS mode is gone, remove the macros from entry_32.S that obfuscated^Wabstracted GS handling. The assembled output is identical before and after this patch.
--- arch/x86/entry/entry_32.S | 43 ++------------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-)
arch/x86/entry/entry_32.S +2 -41
@@ -53,35 +53,6 @@ #define PTI_SWITCH_MASK (1 << PAGE_SHIFT) -/* - * User gs save/restore - * - * This is leftover junk from CONFIG_X86_32_LAZY_GS. A subsequent patch - * will remove it entirely. - */ - /* unfortunately push/pop can't be no-op */ -.macro PUSH_GS - pushl $0 -.endm -.macro POP_GS pop=0 - addl $(4 + \pop), %esp -.endm -.macro POP_GS_EX -.endm - - /* all the rest are no-op */ -.macro PTGS_TO_GS -.endm -.macro PTGS_TO_GS_EX -.endm -.macro GS_TO_REG reg -.endm -.macro REG_TO_PTGS reg -.endm -.macro SET_KERNEL_GS reg -.endm - - /* Unconditionally switch to user cr3 */ .macro SWITCH_TO_USER_CR3 scratch_reg:req ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
@@ -234,7 +205,7 @@ .macro SAVE_ALL pt_regs_ax=%eax switch_stacks=0 skip_gs=0 unwind_espfix=0 cld .if \skip_gs == 0 - PUSH_GS + pushl $0 .endif pushl %fs
@@ -259,9 +230,6 @@ movl $(__USER_DS), %edx movl %edx, %ds movl %edx, %es -.if \skip_gs == 0 - SET_KERNEL_GS %edx -.endif /* Switch to kernel stack if necessary */ .if \switch_stacks > 0 SWITCH_TO_KERNEL_STACK
@@ -300,7 +268,7 @@ 1: popl %ds 2: popl %es 3: popl %fs - POP_GS \pop + addl $(4 + \pop), %esp /* pop the unused "gs" slot */ IRET_FRAME .pushsection .fixup, "ax" 4: movl $0, (%esp)
@@ -313,7 +281,6 @@ _ASM_EXTABLE(1b, 4b) _ASM_EXTABLE(2b, 5b) _ASM_EXTABLE(3b, 6b) - POP_GS_EX .endm .macro RESTORE_ALL_NMI cr3_reg:req pop=0
@@ -928,7 +895,6 @@ SYM_FUNC_START(entry_SYSENTER_32) movl PT_EIP(%esp), %edx /* pt_regs->ip */ movl PT_OLDESP(%esp), %ecx /* pt_regs->sp */ 1: mov PT_FS(%esp), %fs - PTGS_TO_GS popl %ebx /* pt_regs->bx */ addl $2*4, %esp /* skip pt_regs->cx and pt_regs->dx */
@@ -964,7 +930,6 @@ SYM_FUNC_START(entry_SYSENTER_32) jmp 1b .popsection _ASM_EXTABLE(1b, 2b) - PTGS_TO_GS_EX .Lsysenter_fix_flags: pushl $X86_EFLAGS_FIXED
@@ -1106,11 +1071,7 @@ SYM_CODE_START_LOCAL_NOALIGN(handle_exception) SAVE_ALL switch_stacks=1 skip_gs=1 unwind_espfix=1 ENCODE_FRAME_POINTER - /* fixup %gs */ - GS_TO_REG %ecx movl PT_GS(%esp), %edi # get the function address - REG_TO_PTGS %ecx - SET_KERNEL_GS %ecx /* fixup orig %eax */ movl PT_ORIG_EAX(%esp), %edx # get the error code
Signature · 1 line
2.26.2
Quoted 4 lines · Andy Lutomirski
x86_32stackprotector is a maintenance nightmare. Clean it up. This disables stackprotector onx86_32on GCC 8.1 and on all clang versions -- I'll file a bug for the latter.
This should be doable on 64-bit too. All that would need to be done is to remove the zero-base of the percpu segment (which would simplify alot of other code).
--
Brian Gerst
Re: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
In reply to Andy LutomirskiQuoted 5 lines · Andy Lutomirski
On 32-bit kernels, the stackprotector canary is quite nasty -- it is stored at %gs:(20), which is nasty because 32-bit kernels use %fs for percpu storage. It's even nastier because it means that whether %gs contains userspace state or kernel state while running kernel code sepends on whether stackprotector is enabled (this is
depends
Quoted 9 lines
CONFIG_X86_32_LAZY_GS), and this setting radically changes the way that segment selectors work. Supporting both variants is a maintenance and testing mess.Merely rearranging so that percpu and the stack canary share the same segment would be messy as the 32-bit percpu address layout isn't currently compatible with putting a variable at a fixed offset.
Quoted 8 lines
Fortunately, GCC 8.1 added options that allow the stack canary to be accessed as %fs:stack_canary, effectively turning it into an ordinary percpu variable. This lets us get rid of all of the code to manage the stack canary GDT descriptor and the
CONFIG_X86_32_LAZY_GSmess.This patch forcibly disables stackprotector on older compilers that don't support the new options and makes the stack canary into a percpu variable.
It'd be helpful to explicitly state that the so called "lazy GS" approach is now always used for i386.
Quoted 2 lines
Signed-off-by Andy Lutomirski <[email protected]>---
...
arch/x86/include/asm/suspend_32.h struct saved_context { · line 16
Quoted 6 lines
arch/x86/include/asm/suspend_32.h +0
-0@@ -16,9 +16,7 @@ struct saved_context {* On x86_32, all segment registers, with the possible exception of
Is this still a "possible" exception, or is it now always an exception?
Quoted 8 lines
arch/x86/include/asm/suspend_32.h +0
-2* gs, are saved at kernel entry in pt_regs. */ -#ifdef CONFIG_X86_32_LAZY_GS u16 gs; -#endif unsigned long cr0, cr2, cr3, cr4;u64 misc_enable;bool misc_enable_saved;
...
Quoted 10 lines
arch/x86/kernel/tls.c +0
-2@@ -164,17 +164,11 @@ int do_set_thread_area(struct task_struct *p, int idx,savesegment (fs, sel);if (sel == modified_sel)loadsegment (fs, sel); - -savesegment (gs, sel);
arch/x86/kernel/tls.c int do_set_thread_area(struct task_struct *p, int idx, · line 171
Quoted 10 lines
arch/x86/kernel/tls.c +1
-5-if (sel == modified_sel) -load_gs_index (sel);#endif -#ifdef CONFIG_X86_32_LAZY_GS savesegment (gs, sel);if (sel == modified_sel) -loadsegment (gs, sel); -#endif +load_gs_index (sel);
Side topic, the "index" part of this is super confusing. I had to reread this entire patch after discovering load_gs_index is loadsegment on i386.
Maybe also worth a shout out in the changelog?
Quoted 3 lines
arch/x86/kernel/tls.c +0
-0}else {#ifdef CONFIG_X86_64 if (p->thread.fsindex == modified_sel)
Andy Lutomirski
maintainerRe: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
Quoted 36 lines · Sean Christopherson
Quoted 5 lines · Andy Lutomirski
On 32-bit kernels, the stackprotector canary is quite nasty -- it is stored at %gs:(20), which is nasty because 32-bit kernels use %fs for percpu storage. It's even nastier because it means that whether %gs contains userspace state or kernel state while running kernel code sepends on whether stackprotector is enabled (this is
depends
Quoted 9 lines
CONFIG_X86_32_LAZY_GS), and this setting radically changes the way that segment selectors work. Supporting both variants is a maintenance and testing mess.Merely rearranging so that percpu and the stack canary share the same segment would be messy as the 32-bit percpu address layout isn't currently compatible with putting a variable at a fixed offset.
Quoted 8 lines
Fortunately, GCC 8.1 added options that allow the stack canary to be accessed as %fs:stack_canary, effectively turning it into an ordinary percpu variable. This lets us get rid of all of the code to manage the stack canary GDT descriptor and the
CONFIG_X86_32_LAZY_GSmess.This patch forcibly disables stackprotector on older compilers that don't support the new options and makes the stack canary into a percpu variable.
It'd be helpful to explicitly state that the so called "lazy GS" approach is now always used for i386.
Quoted 2 lines
Signed-off-by Andy Lutomirski <[email protected]>---
...
Quoted 8 lines
arch/x86/include/asm/suspend_32.h struct saved_context { · line 16
Quoted 6 lines
arch/x86/include/asm/suspend_32.h +0
-0@@ -16,9 +16,7 @@ struct saved_context {* On x86_32, all segment registers, with the possible exception of Is this still a "possible" exception, or is it now always an exception?
Good catch.
Quoted 36 lines
Quoted 9 lines
arch/x86/include/asm/suspend_32.h +0
-2* gs, are saved at kernel entry in pt_regs. */ -#ifdef CONFIG_X86_32_LAZY_GS u16 gs; -#endif unsigned long cr0, cr2, cr3, cr4;u64 misc_enable;bool misc_enable_saved;...
Quoted 10 lines
arch/x86/kernel/tls.c +0
-2@@ -164,17 +164,11 @@ int do_set_thread_area(struct task_struct *p, int idx,savesegment (fs, sel);if (sel == modified_sel)loadsegment (fs, sel); - -savesegment (gs, sel);arch/x86/kernel/tls.c int do_set_thread_area(struct task_struct *p, int idx, · line 171
Quoted 10 lines
arch/x86/kernel/tls.c +1
-5-if (sel == modified_sel) -load_gs_index (sel);#endif -#ifdef CONFIG_X86_32_LAZY_GS savesegment (gs, sel);if (sel == modified_sel) -loadsegment (gs, sel); -#endif +load_gs_index (sel);Side topic, the "index" part of this is super confusing. I had to reread this entire patch after discovering
load_gs_indexis loadsegment on i386.
Quoted 1 line
Maybe also worth a shout out in the changelog?
Sure.
load_gs_index() makes perfect sense to me because I've been drinking the kool-aid for too long. Maybe some day we should rename it, but I"m not sure what the best name would be. set_gs_update_user_base()? The semantics are that it loads GS except that it changes the user GSBASE instead of the kernel GSBASE. Thanks, AMD.
--Andy
Re: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
In reply to Andy Lutomirskiarch/x86/include/asm/processor.h struct fixed_percpu_data { · line 449
Quoted 10 lines · Andy Lutomirski
arch/x86/include/asm/processor.h +3
-0@@ -441,6 +441,9 @@ struct fixed_percpu_data {* GCC hardcodes the stack canary as %gs:40. Since the * irq_stack is the object at %gs:0, we reserve the bottom * 48 bytes of the irq stack for the canary. +* +* Once we are willing to require -mstack-protector-guard-symbol= +* support for x86_64 stackprotector, we can get rid of this. */ char gs_base[40 ];unsigned long stack_canary;
I'm all in favour of simply requiring GCC-8.1 to build a more secure x86_64 kernel. Gives people an incentive to not use ancient compilers.
And if you do want to use your ancient compiler, we'll still build, you just don't get to have stackprotector.
Quoted 9 lines · Brian Gerst
Quoted 4 lines · Andy Lutomirski
x86_32stackprotector is a maintenance nightmare. Clean it up. This disables stackprotector onx86_32on GCC 8.1 and on all clang versions -- I'll file a bug for the latter.This should be doable on 64-bit too. All that would need to be done is to remove the zero-base of the percpu segment (which would simplify alot of other code).
Like what?
I don't think it'd be hard to do, but I really don't see it doing anything other than make things confusing as heck.
Re: [PATCH 2/2] x86/entry/32: Remove leftover macros after stackprotector cleanups
In reply to Andy Lutomirskiarch/x86/entry/entry_32.S line 210
Quoted 8 lines · Andy Lutomirski
.macro
SAVE_ALLpt_regs_ax=%eax switch_stacks=0 skip_gs=0 unwind_espfix=0arch/x86/entry/entry_32.S +1
-1cld .if \skip_gs == 0 - PUSH_GS + pushl $0 .endif pushl %fs
Is there a good reason not to just push gs into it? It might be nice if pt_regs is complete.
Re: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
In reply to Andy LutomirskiQuoted 15 lines · Andy Lutomirski
On 32-bit kernels, the stackprotector canary is quite nasty -- it is stored at %gs:(20), which is nasty because 32-bit kernels use %fs for percpu storage. It's even nastier because it means that whether %gs contains userspace state or kernel state while running kernel code sepends on whether stackprotector is enabled (this is
CONFIG_X86_32_LAZY_GS), and this setting radically changes the way that segment selectors work. Supporting both variants is a maintenance and testing mess.Merely rearranging so that percpu and the stack canary share the same segment would be messy as the 32-bit percpu address layout isn't currently compatible with putting a variable at a fixed offset.
Quoted 8 lines
Fortunately, GCC 8.1 added options that allow the stack canary to be accessed as %fs:stack_canary, effectively turning it into an ordinary percpu variable. This lets us get rid of all of the code to manage the stack canary GDT descriptor and the
CONFIG_X86_32_LAZY_GSmess.This patch forcibly disables stackprotector on older compilers that don't support the new options and makes the stack canary into a percpu variable.
This doesn't consider !SMP builds, where per-cpu variable are just normal variables, and the FS segment is disabled. Unfortunately, -mstack-protector-guard-reg=ds is not accepted. The simplest fix would be to define __KERNEL_PERCPU when either SMP or STACKPROTECTOR are enabled.
--
Brian Gerst
Andy Lutomirski
maintainerRe: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
Quoted 26 lines · Brian Gerst
Quoted 15 lines · Andy Lutomirski
On 32-bit kernels, the stackprotector canary is quite nasty -- it is stored at %gs:(20), which is nasty because 32-bit kernels use %fs for percpu storage. It's even nastier because it means that whether %gs contains userspace state or kernel state while running kernel code sepends on whether stackprotector is enabled (this is
CONFIG_X86_32_LAZY_GS), and this setting radically changes the way that segment selectors work. Supporting both variants is a maintenance and testing mess.Merely rearranging so that percpu and the stack canary share the same segment would be messy as the 32-bit percpu address layout isn't currently compatible with putting a variable at a fixed offset.
Quoted 8 lines
Fortunately, GCC 8.1 added options that allow the stack canary to be accessed as %fs:stack_canary, effectively turning it into an ordinary percpu variable. This lets us get rid of all of the code to manage the stack canary GDT descriptor and the
CONFIG_X86_32_LAZY_GSmess.This patch forcibly disables stackprotector on older compilers that don't support the new options and makes the stack canary into a percpu variable.
Quoted 5 lines
This doesn't consider !SMP builds, where per-cpu variable are just normal variables, and the FS segment is disabled. Unfortunately, -mstack-protector-guard-reg=ds is not accepted. The simplest fix would be to define
__KERNEL_PERCPUwhen either SMP or STACKPROTECTOR are enabled.
I don't love this because it breaks the "stack canary is just a regular PERCPU variable" idea. GCC accepts -mstack-protector-guard=global to get rid of the segment override, but then it ignores -mstack-protector-guard-offset=stack_canary. So I could do this:
#ifdef CONFIG_SMPEXPORT_PER_CPU_SYMBOL(stack_canary);
#else
/*
* GCC can't use a symbol called 'stack_canary' as the stack canary with
* an FS or GS segment override, and SMP=n percpu variables are just normal
* variables. But GCC can use '__stack_chk_guard'.
*/
extern unsigned long __attribute__((alias("stack_canary"))) __stack_chk_guard; EXPORT_SYMBOL(__stack_chk_guard);
#endif
Or I suppose I could just rename the thing __stack_chk_guard. The only downside of the latter seems to be that an accidental mix of SMP and !SMP object files (or modules!) would crash, but I suppose they likely crash anyway.
kernel test robot
build botRe: [PATCH 1/2] x86/stackprotector/32: Make the canary into a regular percpu variable
In reply to Andy LutomirskiI love your patch! Yet something to improve:
Hi Andy,
I love your patch! Yet something to improve:
[auto build test ERROR on tip/x86/core]
[also build test ERROR on tip/master tip/x86/asm linus/master v5.9-rc8 next-20201006] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Andy-Lutomirski/Clean-up-x86_32-stackprotector/20201006-035405 base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git 238c91115cd05c71447ea071624a4c9fe661f970 config: x86_64-rhel (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/b731c7878a867036dadbd13c7268893dee360b69 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Andy-Lutomirski/Clean-up-x86_32-stackprotector/20201006-035405 git checkout b731c7878a867036dadbd13c7268893dee360b69 # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/x86/xen/enlighten_pv.c: In function 'xen_setup_gdt':
>> arch/x86/xen/enlighten_pv.c:1182:2: error: implicit declaration of function 'setup_stack_canary_segment' [-Werror=implicit-function-declaration]
1182 | setup_stack_canary_segment(cpu);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~ arch/x86/xen/enlighten_pv.c: At top level:
arch/x86/xen/enlighten_pv.c:1195:34: warning: no previous prototype for 'xen_start_kernel' [-Wmissing-prototypes]
1195 | asmlinkage __visible void __init xen_start_kernel(void)
| ^~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
vim +/setup_stack_canary_segment +1182 arch/x86/xen/enlighten_pv.c
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1171
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1172 /*
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1173 * Set up the GDT and segment registers for -fstack-protector. Until
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1174 * we do this, we have to be careful not to call any stack-protected
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1175 * function, which is most of the kernel.
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1176 */
2197082afd4ef71 Jan Beulich 2018-06-25 1177 static void __init xen_setup_gdt(int cpu)
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1178 {
5c83511bdb9832c Juergen Gross 2018-08-28 1179 pv_ops.cpu.write_gdt_entry = xen_write_gdt_entry_boot;
5c83511bdb9832c Juergen Gross 2018-08-28 1180 pv_ops.cpu.load_gdt = xen_load_gdt_boot;
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1181
2197082afd4ef71 Jan Beulich 2018-06-25 @1182 setup_stack_canary_segment(cpu);
2197082afd4ef71 Jan Beulich 2018-06-25 1183 switch_to_new_gdt(cpu);
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1184
5c83511bdb9832c Juergen Gross 2018-08-28 1185 pv_ops.cpu.write_gdt_entry = xen_write_gdt_entry;
5c83511bdb9832c Juergen Gross 2018-08-28 1186 pv_ops.cpu.load_gdt = xen_load_gdt;
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1187 }
e1dab14cf68d1e0 Vitaly Kuznetsov 2017-03-14 1188 ---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org