x86-64: Stack protector and percpu improvements
Subsystems (6)
v2 cover x86-64: Stack protector and percpu improvements
Currently, x86-64 uses an unusual per-cpu layout, where the percpu section is linked at absolute address 0. The reason behind this is that older GCC versions placed the stack protector (if enabled) at a fixed offset from the GS segment base. Since the GS segement is also used for percpu variables, this forced the current layout.
GCC since version 8.1 supports a configurable location for the stack protector value, which allows removal of the restriction on how the percpu section is linked.
Changes from v1:
- Remove fixed location stack protector support
Brian Gerst (6):
x86: Remove stack protector test scripts
x86-64: Convert stack protector to normal percpu variable
x86-64: Use relative per-cpu offsets
x86-64: Remove inverse relocations
kallsyms: Remove KALLSYMS_ABSOLUTE_PERCPU
percpu: Remove PER_CPU_FIRST_SECTION
arch/x86/Kconfig | 7 +- arch/x86/Makefile | 19 +-- arch/x86/boot/compressed/misc.c | 14 +-- arch/x86/entry/entry_64.S | 2 +- arch/x86/include/asm/percpu.h | 22 ---- arch/x86/include/asm/processor.h | 25 +--- arch/x86/include/asm/stackprotector.h | 36 ++---- arch/x86/kernel/asm-offsets_64.c | 6 - arch/x86/kernel/cpu/common.c | 8 +- arch/x86/kernel/head_64.S | 10 +- arch/x86/kernel/irq_64.c | 1 - arch/x86/kernel/setup_percpu.c | 12 +- arch/x86/kernel/vmlinux.lds.S | 33 ----- arch/x86/tools/relocs.c | 145 +--------------------- arch/x86/xen/xen-head.S | 10 +- include/asm-generic/vmlinux.lds.h | 1 - include/linux/percpu-defs.h | 12 -- init/Kconfig | 11 +- kernel/kallsyms.c | 12 +- scripts/gcc-x86_32-has-stack-protector.sh | 8 -- scripts/gcc-x86_64-has-stack-protector.sh | 4 - scripts/kallsyms.c | 68 ++-------- scripts/link-vmlinux.sh | 4 - 23 files changed, 56 insertions(+), 414 deletions(-) delete mode 100755 scripts/gcc-x86_32-has-stack-protector.sh delete mode 100755 scripts/gcc-x86_64-has-stack-protector.sh
Signature · 1 line
2.31.1
PATCH v2 4/6 x86-64: Remove inverse relocations
Now that the percpu section is not at a fixed address, inverse relocations (which were needed to offset the effects of relocation on RIP-relative percpu references) are no longer used.
--- arch/x86/boot/compressed/misc.c | 14 +------------- arch/x86/tools/relocs.c | 9 --------- 2 files changed, 1 insertion(+), 22 deletions(-)
arch/x86/boot/compressed/misc.c +1 -13
@@ -215,7 +215,7 @@ static void handle_relocations(void *output, unsigned long output_len,/* * Process relocations: 32 bit relocations first then 64 bit after. -* Three sets of binary relocations are added to the end of the kernel +* Two sets of binary relocations are added to the end of the kernel * before compression. Each relocation table entry is the kernel * address of the location which needs to be updated stored as a * 32-bit value which is sign extended to 64 bits.
@@ -225,8 +225,6 @@ static void handle_relocations(void *output, unsigned long output_len,* kernel bits... * 0 - zero terminator for 64 bit relocations * 64 bit relocation repeated -* 0 - zero terminator for inverse 32 bit relocations -* 32 bit inverse relocation repeated * 0 - zero terminator for 32 bit relocations * 32 bit relocation repeated *
@@ -243,16 +241,6 @@ static void handle_relocations(void *output, unsigned long output_len, *(uint32_t *)ptr += delta; }#ifdef CONFIG_X86_64 -while (*--reloc) { -long extended = *reloc; - extended += map; - - ptr = (unsigned long )extended; -if (ptr < min_addr || ptr > max_addr) -error ("inverse 32-bit relocation outside of kernel!\n" ); - - *(int32_t *)ptr -= delta; - }for (reloc--; *reloc; reloc--) {long extended = *reloc; extended += map;
arch/x86/tools/relocs.c +0 -9
@@ -28,7 +28,6 @@ struct relocs {static struct relocs relocs16;static struct relocs relocs32;#if ELF_BITS ==64 -static struct relocs relocs32neg;static struct relocs relocs64;#define FMT PRIu64#else
@@ -959,7 +958,6 @@ static void emit_relocs(int as_text, int use_real_mode)/* Order the relocations for more efficient processing */ sort_relocs (&relocs32);#if ELF_BITS ==64 -sort_relocs (&relocs32neg);sort_relocs (&relocs64);#else sort_relocs (&relocs16);
@@ -991,13 +989,6 @@ static void emit_relocs(int as_text, int use_real_mode)/* Now print each relocation */ for (i =0 ; i < relocs64.count; i++)write_reloc (relocs64.offset[i], stdout); - -/* Print a stop */ -write_reloc (0 , stdout); - -/* Now print each inverse 32-bit relocation */ -for (i =0 ; i < relocs32neg.count; i++) -write_reloc (relocs32neg.offset[i], stdout);#endif /* Print a stop */
Signature · 1 line
2.31.1
PATCH v2 6/6 percpu: Remove PER_CPU_FIRST_SECTION
In reply to Brian Gerstx86-64 was the only user.
--- include/asm-generic/vmlinux.lds.h | 1 - include/linux/percpu-defs.h | 12 ------------ 2 files changed, 13 deletions(-)
include/asm-generic/vmlinux.lds.h +0 -1
@@ -1056,7 +1056,6 @@*/ #define PERCPU_INPUT (cacheline) \ __per_cpu_start = .; \ - *(.data..percpu..first) \ . = ALIGN(PAGE_SIZE); \ *(.data..percpu..page_aligned) \ . = ALIGN(cacheline); \
include/linux/percpu-defs.h +0 -12
@@ -26,13 +26,11 @@#define PER_CPU_SHARED_ALIGNED_SECTION "..shared_aligned"#define PER_CPU_ALIGNED_SECTION "..shared_aligned"#endif -#define PER_CPU_FIRST_SECTION "..first"#else #define PER_CPU_SHARED_ALIGNED_SECTION ""#define PER_CPU_ALIGNED_SECTION "..shared_aligned" -#define PER_CPU_FIRST_SECTION ""#endif
@@ -114,16 +112,6 @@#define DEFINE_PER_CPU (type, name) \ DEFINE_PER_CPU_SECTION(type, name, "") -/* -* Declaration/definition used for per-CPU variables that must come first in -* the set of variables. -*/ -#define DECLARE_PER_CPU_FIRST (type, name) \ - DECLARE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION) - -#define DEFINE_PER_CPU_FIRST (type, name) \ - DEFINE_PER_CPU_SECTION(type, name, PER_CPU_FIRST_SECTION) -/* * Declaration/definition used for per-CPU variables that must be cacheline * aligned under SMP conditions so that, whilst a particular instance of the
Signature · 1 line
2.31.1
PATCH v2 5/6 kallsyms: Remove KALLSYMS_ABSOLUTE_PERCPU
In reply to Brian Gerstx86-64 was the only user.
--- init/Kconfig | 10 +----- kernel/kallsyms.c | 12 ++------ scripts/kallsyms.c | 68 ++++++----------------------------------- scripts/link-vmlinux.sh | 4 --- 4 files changed, 12 insertions(+), 82 deletions(-)
init/Kconfig +1 -9
@@ -1711,10 +1711,6 @@ config KALLSYMS_ALL Say N unless you really need all symbols. -config KALLSYMS_ABSOLUTE_PERCPU - bool - depends on KALLSYMS - config KALLSYMS_BASE_RELATIVE bool depends on KALLSYMS
@@ -1722,11 +1718,7 @@ config KALLSYMS_BASE_RELATIVE help Instead of emitting them as absolute values in the native word size, emit the symbol references in the kallsyms table as 32-bit entries, - each containing a relative value in the range [base, base + U32_MAX] - or, when KALLSYMS_ABSOLUTE_PERCPU is in effect, each containing either - an absolute value in the range [0, S32_MAX] or a relative value in the - range [base, base + S32_MAX], where base is the lowest relative symbol - address encountered in the image. + each containing a relative value in the range [base, base + U32_MAX]. On 64-bit builds, this reduces the size of the address table by 50%, but more importantly, it results in entries whose values are build
kernel/kallsyms.c +2 -10
@@ -152,16 +152,8 @@ static unsigned long kallsyms_sym_address(int idx)if (!IS_ENABLED (CONFIG_KALLSYMS_BASE_RELATIVE ))return kallsyms_addresses[idx]; -/* values are unsigned offsets if --absolute-percpu is not in effect */ -if (!IS_ENABLED (CONFIG_KALLSYMS_ABSOLUTE_PERCPU )) -return kallsyms_relative_base + (u32 )kallsyms_offsets[idx]; - -/* ...otherwise, positive offsets are absolute values */ -if (kallsyms_offsets[idx] >=0 ) -return kallsyms_offsets[idx]; - -/* ...and negative offsets are relative to kallsyms_relative_base - 1 */ -return kallsyms_relative_base -1 - kallsyms_offsets[idx]; +/* values are unsigned offsets */ +return kallsyms_relative_base + (u32 )kallsyms_offsets[idx]; }static bool cleanup_symbol_name (char *s)
scripts/kallsyms.c +9 -59
135 lines not on this page · raw message
scripts/link-vmlinux.sh +0 -4
@@ -243,10 +243,6 @@ kallsyms() kallsymopt="${kallsymopt} --all-symbols" fi - if [ -n "${CONFIG_KALLSYMS_ABSOLUTE_PERCPU}" ]; then - kallsymopt="${kallsymopt} --absolute-percpu" - fi - if [ -n "${CONFIG_KALLSYMS_BASE_RELATIVE}" ]; then kallsymopt="${kallsymopt} --base-relative" fi
Signature · 1 line
2.31.1
PATCH v2 3/6 x86-64: Use relative per-cpu offsets
In reply to Brian GerstThe per-cpu section is currently linked at virtual address 0, because older compilers hardcoded the stack protector canary value at a fixed offset from the start of the GS segment. Now that the canary is a normal per-cpu variable, the special handling of the per-cpu section can be removed.
--- arch/x86/include/asm/percpu.h | 22 ------ arch/x86/kernel/head_64.S | 6 +- arch/x86/kernel/irq_64.c | 1 - arch/x86/kernel/setup_percpu.c | 12 +-- arch/x86/kernel/vmlinux.lds.S | 27 ------- arch/x86/tools/relocs.c | 135 +-------------------------------- init/Kconfig | 1 - 7 files changed, 6 insertions(+), 198 deletions(-)
arch/x86/include/asm/percpu.h +0 -22
@@ -16,12 +16,6 @@#define PER_CPU_VAR (var) var#endif /* SMP */ -#ifdef CONFIG_X86_64_SMP -#define INIT_PER_CPU_VAR (var) init_per_cpu__##var -#else -#define INIT_PER_CPU_VAR (var) var -#endif -#else /* ...!ASSEMBLY */ #include <linux/kernel.h>
@@ -49,22 +43,6 @@#define __percpu_arg (x) __percpu_prefix "%" #x -/* -* Initialized pointers to per-cpu variables needed for the boot -* processor need to use these macros to get the proper address -* offset from __per_cpu_load on SMP. -* -* There also must be an entry in vmlinux_64.lds.S -*/ -#define DECLARE_INIT_PER_CPU (var) \ - extern typeof(var) init_per_cpu_var(var) - -#ifdef CONFIG_X86_64_SMP -#define init_per_cpu_var (var) init_per_cpu__##var -#else -#define init_per_cpu_var (var) var -#endif -/* For arch-specific code, we can use direct single-insn ops (they * don't give an lvalue though). */
arch/x86/kernel/head_64.S +1 -5
@@ -335,11 +335,7 @@ SYM_CODE_END(vc_boot_ghcb) __REFDATA .balign 8 SYM_DATA(initial_code, .quad x86_64_start_kernel) -#ifdef CONFIG_SMP -SYM_DATA(initial_gs, .quad __per_cpu_load) -#else SYM_DATA(initial_gs, .quad 0) -#endif #ifdef CONFIG_AMD_MEM_ENCRYPT SYM_DATA(initial_vc_handler, .quad handle_vc_boot_ghcb) #endif
@@ -572,7 +568,7 @@ SYM_DATA_END(level1_fixmap_pgt) .align 16 SYM_DATA(early_gdt_descr, .word GDT_ENTRIES*8-1) -SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) +SYM_DATA_LOCAL(early_gdt_descr_base, .quad gdt_page) .align 16 /* This must match the first entry in level2_kernel_pgt */
arch/x86/kernel/irq_64.c +0 -1
@@ -26,7 +26,6 @@#include <asm/apic.h> DEFINE_PER_CPU_PAGE_ALIGNED(struct irq_stack, irq_stack_backing_store) __visible; -DECLARE_INIT_PER_CPU (irq_stack_backing_store);#ifdef CONFIG_VMAP_STACK /*
arch/x86/kernel/setup_percpu.c +2 -10
@@ -26,18 +26,10 @@DEFINE_PER_CPU_READ_MOSTLY (int, cpu_number);EXPORT_PER_CPU_SYMBOL (cpu_number); -#ifdef CONFIG_X86_64 -#define BOOT_PERCPU_OFFSET ((unsigned long)__per_cpu_load) -#else -#define BOOT_PERCPU_OFFSET 0 -#endif - -DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off) = BOOT_PERCPU_OFFSET; +DEFINE_PER_CPU_READ_MOSTLY(unsigned long, this_cpu_off);EXPORT_PER_CPU_SYMBOL (this_cpu_off); -unsigned long __per_cpu_offset[NR_CPUS ]__ro_after_init = { - [0 ...NR_CPUS -1 ] =BOOT_PERCPU_OFFSET , -}; +unsigned long __per_cpu_offset[NR_CPUS ]__ro_after_init ;EXPORT_SYMBOL (__per_cpu_offset);/*
arch/x86/kernel/vmlinux.lds.S +0 -27
@@ -103,9 +103,6 @@ PHDRS { text PT_LOAD FLAGS(5); /* R_E */ data PT_LOAD FLAGS(6); /* RW_ */ #ifdef CONFIG_X86_64 -#ifdef CONFIG_SMP - percpu PT_LOAD FLAGS(6); /* RW_ */ -#endif init PT_LOAD FLAGS(7); /* RWE */ #endif note PT_NOTE FLAGS(0); /* ___ */
@@ -215,17 +212,6 @@ SECTIONS __init_begin = .; /* paired with __init_end */ } -#if defined(CONFIG_X86_64) && defined(CONFIG_SMP) - /* - * percpu offsets are zero-based on SMP. PERCPU_VADDR() changes the - * output PHDR, so the next output section - .init.text - should - * start another segment - init. - */ - PERCPU_VADDR(INTERNODE_CACHE_BYTES, 0, :percpu) - ASSERT(SIZEOF(.data..percpu) < CONFIG_PHYSICAL_START, - "per-CPU data too large - increase CONFIG_PHYSICAL_START") -#endif - INIT_TEXT_SECTION(PAGE_SIZE) #ifdef CONFIG_X86_64 :init
@@ -339,9 +325,7 @@ SECTIONS EXIT_DATA } -#if !defined(CONFIG_X86_64) || !defined(CONFIG_SMP) PERCPU_SECTION(INTERNODE_CACHE_BYTES) -#endif . = ALIGN(PAGE_SIZE);
@@ -474,17 +458,6 @@ SECTIONS . = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE), "kernel image bigger than KERNEL_IMAGE_SIZE"); -#ifdef CONFIG_X86_64 -/* - * Per-cpu symbols which need to be offset from __per_cpu_load - * for the boot processor. - */ -#define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load -INIT_PER_CPU(gdt_page); -INIT_PER_CPU(irq_stack_backing_store); - -#endif /* CONFIG_X86_64 */ - #ifdef CONFIG_KEXEC_CORE #include <asm/kexec.h>
arch/x86/tools/relocs.c +3 -132
179 lines not on this page · raw message
init/Kconfig +0 -1
@@ -1714,7 +1714,6 @@ config KALLSYMS_ALL config KALLSYMS_ABSOLUTE_PERCPU bool depends on KALLSYMS - default X86_64 && SMP config KALLSYMS_BASE_RELATIVE bool
Signature · 1 line
2.31.1
PATCH v2 1/6 x86: Remove stack protector test scripts
In reply to Brian GerstFor 64-bit, this test for the stack protector was added in 2006 to make sure the compiler had the PR28281 patch applied. With GCC 5.1 being the minimum supported compiler now, this test is no longer necessary.
For 32-bit, testing for compiler option support can be done directly in Kconfig.
--- arch/x86/Kconfig | 7 +++---- scripts/gcc-x86_32-has-stack-protector.sh | 8 -------- scripts/gcc-x86_64-has-stack-protector.sh | 4 ---- 3 files changed, 3 insertions(+), 16 deletions(-) delete mode 100755 scripts/gcc-x86_32-has-stack-protector.sh delete mode 100755 scripts/gcc-x86_64-has-stack-protector.sh
arch/x86/Kconfig +3 -4
@@ -392,12 +392,11 @@ config PGTABLE_LEVELS config CC_HAS_SANE_STACKPROTECTOR bool - default $(success,$(srctree)/scripts/gcc-x86_64-has-stack-protector.sh $(CC)) if 64BIT - default $(success,$(srctree)/scripts/gcc-x86_32-has-stack-protector.sh $(CC)) + default y if 64BIT + default $(cc-option,-mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard) help We have to make sure stack protector is unconditionally disabled if - the compiler produces broken code or if it does not let us control - the segment on 32-bit kernels. + the compiler does not let us control the segment and symbol. menu "Processor type and features"
scripts/gcc-x86_32-has-stack-protector.sh deleted +0 -8
@@ -1,8 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0 - -# 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_chk_guard - -o - 2> /dev/null | grep -q "%fs"
scripts/gcc-x86_64-has-stack-protector.sh deleted +0 -4
@@ -1,4 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: GPL-2.0 - -echo "int foo(void) { char X[200]; return 3; }" | $* -S -x c -c -m64 -O0 -mcmodel=kernel -fno-PIE -fstack-protector - -o - 2> /dev/null | grep -q "%gs"
Signature · 1 line
2.31.1
PATCH v2 2/6 x86-64: Convert stack protector to normal percpu variable
In reply to Brian GerstOlder versions of GCC fixed the location of the stack protector canary at %gs:40. This constraint forced the percpu section to be linked at virtual address 0 so that the canary could be the first data object in the percpu section. Supporting the zero-based percpu section requires additional code to handle relocations for RIP-relative references to percpu data, extra complexity to kallsyms, and workarounds for linker bugs due to the use of absolute symbols.
Since version 8.1, GCC has options to configure the location of the canary value. This allows the canary to be turned into a normal percpu variable and removes the constraint that the percpu section be zero-based.
--- arch/x86/Kconfig | 2 +- arch/x86/Makefile | 19 ++++++++------ arch/x86/entry/entry_64.S | 2 +- arch/x86/include/asm/processor.h | 25 ++++--------------- arch/x86/include/asm/stackprotector.h | 36 ++++++--------------------- arch/x86/kernel/asm-offsets_64.c | 6 ----- arch/x86/kernel/cpu/common.c | 8 ++---- arch/x86/kernel/head_64.S | 12 +++++---- arch/x86/kernel/vmlinux.lds.S | 6 ----- arch/x86/tools/relocs.c | 1 - arch/x86/xen/xen-head.S | 10 +++----- 11 files changed, 39 insertions(+), 88 deletions(-)
arch/x86/Kconfig +1 -1
@@ -392,7 +392,7 @@ config PGTABLE_LEVELS config CC_HAS_SANE_STACKPROTECTOR bool - default y if 64BIT + default $(cc-option,-mstack-protector-guard-reg=gs -mstack-protector-guard-symbol=__stack_chk_guard) if 64BIT default $(cc-option,-mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard) help We have to make sure stack protector is unconditionally disabled if
arch/x86/Makefile +12 -7
@@ -79,13 +79,7 @@ ifeq ($(CONFIG_X86_32),y) # temporary until string.h is fixed KBUILD_CFLAGS += -ffreestanding - ifeq ($(CONFIG_STACKPROTECTOR),y) - ifeq ($(CONFIG_SMP),y) - KBUILD_CFLAGS += -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard - else - KBUILD_CFLAGS += -mstack-protector-guard=global - endif - endif + percpu_seg := fs else BITS := 64 UTS_MACHINE := x86_64
@@ -126,6 +120,17 @@ else KBUILD_CFLAGS += -mno-red-zone KBUILD_CFLAGS += -mcmodel=kernel + + percpu_seg := gs +endif + +ifeq ($(CONFIG_STACKPROTECTOR),y) + ifeq ($(CONFIG_SMP),y) + KBUILD_CFLAGS += -mstack-protector-guard-reg=$(percpu_seg) \ + -mstack-protector-guard-symbol=__stack_chk_guard + else + KBUILD_CFLAGS += -mstack-protector-guard=global + endif endif ifdef CONFIG_X86_X32
arch/x86/entry/entry_64.S +1 -1
@@ -240,7 +240,7 @@ SYM_FUNC_START(__switch_to_asm) #ifdef CONFIG_STACKPROTECTOR movq TASK_stack_canary(%rsi), %rbx - movq %rbx, PER_CPU_VAR(fixed_percpu_data) + stack_canary_offset + movq %rbx, PER_CPU_VAR(__stack_chk_guard) #endif #ifdef CONFIG_RETPOLINE
arch/x86/include/asm/processor.h +5 -20
@@ -427,25 +427,13 @@ struct irq_stack { DECLARE_PER_CPU(unsigned long, cpu_current_top_of_stack);#ifdef CONFIG_X86_64 -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; -}; - -DECLARE_PER_CPU_FIRST(struct fixed_percpu_data, fixed_percpu_data) __visible; -DECLARE_INIT_PER_CPU (fixed_percpu_data); -static inline unsigned long cpu_kernelmode_gs_base (int cpu) { -return (unsigned long )per_cpu (fixed_percpu_data.gs_base, cpu); +#ifdef CONFIG_SMP +return per_cpu_offset (cpu); +#else +return 0 ; +#endif } DECLARE_PER_CPU(void *, hardirq_stack_ptr);
@@ -455,9 +443,6 @@ extern asmlinkage void ignore_sysret(void);/* Save actual FS/GS selectors and bases to current->thread */ void current_save_fsgs (void );#else /* X86_64 */ -#ifdef CONFIG_STACKPROTECTOR -DECLARE_PER_CPU(unsigned long, __stack_chk_guard); -#endif DECLARE_PER_CPU(struct irq_stack *, hardirq_stack_ptr); DECLARE_PER_CPU(struct irq_stack *, softirq_stack_ptr);#endif /* !X86_64 */
arch/x86/include/asm/stackprotector.h +7 -29
@@ -2,26 +2,13 @@/* * GCC stack protector support. * -* Stack protector works by putting predefined pattern at the start of +* Stack protector works by putting a predefined pattern at the start of * the stack frame and verifying that it hasn't been overwritten when -* returning from the function. The pattern is called stack canary -* and unfortunately gcc historically required it to be at a fixed offset -* from the percpu segment base. On x86_64, the offset is 40 bytes. +* returning from the function. The pattern is called the stack canary +* and is a unique value for each task. * -* The same segment is shared by percpu area and stack canary. On -* x86_64, percpu symbols are zero based and %gs (64-bit) points to the -* base of percpu area. The first occupant of the percpu area is always -* fixed_percpu_data which contains stack_canary at the appropriate -* offset. On x86_32, the stack canary is just a regular percpu -* variable. -* -* Putting percpu data in %fs on 32-bit is a minor optimization compared to -* using %gs. Since 32-bit userspace normally has %fs == 0, we are likely -* to load 0 into %fs on exit to usermode, whereas with percpu data in -* %gs, we are likely to load a non-null %gs on return to user mode. -* -* Once we are willing to require GCC 8.1 or better for 64-bit stackprotector -* support, we can remove some of this complexity. +* GCC is configured to read the stack canary value from the __stack_chk_guard +* per-cpu variable, which is changed on task switch. */ #ifndef _ASM_STACKPROTECTOR_H
@@ -37,6 +24,8 @@#include <linux/random.h> #include <linux/sched.h> +DECLARE_PER_CPU(unsigned long, __stack_chk_guard); +/* * Initialize the stackprotector canary value. *
@@ -53,9 +42,6 @@ static __always_inline void boot_init_stack_canary(void)u64 canary;u64 tsc; -#ifdef CONFIG_X86_64 -BUILD_BUG_ON (offsetof(struct fixed_percpu_data , stack_canary) !=40 ); -#endif /* * We both use the random pool and the current TSC as a source * of randomness. The TSC only matters for very early init,
@@ -68,20 +54,12 @@ static __always_inline void boot_init_stack_canary(void) canary &=CANARY_MASK ; current->stack_canary = canary; -#ifdef CONFIG_X86_64 -this_cpu_write (fixed_percpu_data.stack_canary, canary); -#else this_cpu_write(__stack_chk_guard, canary); -#endif }static inline void cpu_init_stack_canary (int cpu,struct task_struct *idle) { -#ifdef CONFIG_X86_64 -per_cpu (fixed_percpu_data.stack_canary, cpu) = idle->stack_canary; -#else per_cpu (__stack_chk_guard, cpu) = idle->stack_canary; -#endif }#else /* STACKPROTECTOR */
arch/x86/kernel/asm-offsets_64.c +0 -6
@@ -54,11 +54,5 @@ int main(void)BLANK ();#undef ENTRY -BLANK (); - -#ifdef CONFIG_STACKPROTECTOR -DEFINE (stack_canary_offset, offsetof(struct fixed_percpu_data , stack_canary)); -BLANK (); -#endif return 0 ; }
arch/x86/kernel/cpu/common.c +2 -6
@@ -1767,10 +1767,6 @@ static __init int setup_clearcpuid(char *arg)__setup ("clearcpuid=" , setup_clearcpuid);#ifdef CONFIG_X86_64 -DEFINE_PER_CPU_FIRST(struct fixed_percpu_data, - fixed_percpu_data)__aligned(PAGE_SIZE) __visible; -EXPORT_PER_CPU_SYMBOL_GPL (fixed_percpu_data); -/* * The following percpu variables are hot. Align current_task to * cacheline size such that they fall in the same cacheline.
@@ -1851,13 +1847,13 @@ DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = (unsigned long )&init_thread_union +THREAD_SIZE ;EXPORT_PER_CPU_SYMBOL (cpu_current_top_of_stack); +#endif /* CONFIG_X86_64 */ +#ifdef CONFIG_STACKPROTECTOR DEFINE_PER_CPU(unsigned long, __stack_chk_guard);EXPORT_PER_CPU_SYMBOL (__stack_chk_guard);#endif -#endif /* CONFIG_X86_64 */ -/* * Clear all 6 debug registers: */
arch/x86/kernel/head_64.S +7 -5
@@ -198,10 +198,8 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) movl %eax,%fs movl %eax,%gs - /* Set up %gs. - * - * The base of %gs always points to fixed_percpu_data. If the - * stack protector canary is enabled, it is located at %gs:40. + /* + * Set up GS base. * Note that, on SMP, the boot cpu uses init data section until * the per cpu areas are set up. */
@@ -337,7 +335,11 @@ SYM_CODE_END(vc_boot_ghcb) __REFDATA .balign 8 SYM_DATA(initial_code, .quad x86_64_start_kernel) -SYM_DATA(initial_gs, .quad INIT_PER_CPU_VAR(fixed_percpu_data)) +#ifdef CONFIG_SMP +SYM_DATA(initial_gs, .quad __per_cpu_load) +#else +SYM_DATA(initial_gs, .quad 0) +#endif #ifdef CONFIG_AMD_MEM_ENCRYPT SYM_DATA(initial_vc_handler, .quad handle_vc_boot_ghcb) #endif
arch/x86/kernel/vmlinux.lds.S +0 -6
@@ -481,14 +481,8 @@ SECTIONS */ #define INIT_PER_CPU(x) init_per_cpu__##x = ABSOLUTE(x) + __per_cpu_load INIT_PER_CPU(gdt_page); -INIT_PER_CPU(fixed_percpu_data); INIT_PER_CPU(irq_stack_backing_store); -#ifdef CONFIG_SMP -. = ASSERT((fixed_percpu_data == 0), - "fixed_percpu_data is not at start of per-cpu area"); -#endif - #endif /* CONFIG_X86_64 */ #ifdef CONFIG_KEXEC_CORE
arch/x86/tools/relocs.c +0 -1
@@ -810,7 +810,6 @@ static void percpu_init(void)* __per_cpu_load * * The "gold" linker incorrectly associates: -* init_per_cpu__fixed_percpu_data * init_per_cpu__gdt_page */ static int is_percpu_sym (ElfW (Sym) *sym,const char *symname)
arch/x86/xen/xen-head.S +4 -6
@@ -54,16 +54,14 @@ SYM_CODE_START(startup_xen) mov %rsi, xen_start_info mov initial_stack(%rip), %rsp - /* Set up %gs. - * - * The base of %gs always points to fixed_percpu_data. If the - * stack protector canary is enabled, it is located at %gs:40. + /* + * Set up GS base. * Note that, on SMP, the boot cpu uses init data section until * the per cpu areas are set up. */ movl $MSR_GS_BASE,%ecx - movq $INIT_PER_CPU_VAR(fixed_percpu_data),%rax - cdq + movl initial_gs(%rip),%eax + movl initial_gs+4(%rip),%edx wrmsr call xen_start_kernel
Signature · 1 line
2.31.1
RE: [PATCH v2 2/6] x86-64: Convert stack protector to normal percpu variable
From: Brian Gerst
Quoted 10 lines
Sent: 30 November 2021 20:56
Older versions of GCC fixed the location of the stack protector canary at %gs:40. This constraint forced the percpu section to be linked at virtual address 0 so that the canary could be the first data object in the percpu section. Supporting the zero-based percpu section requires additional code to handle relocations for RIP-relative references to percpu data, extra complexity to kallsyms, and workarounds for linker bugs due to the use of absolute symbols.
Quoted 4 lines
Since version 8.1, GCC has options to configure the location of the canary value. This allows the canary to be turned into a normal percpu variable and removes the constraint that the percpu section be zero-based.
I didn't think the minimum gcc version has been raised as far as 8.1?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [PATCH v2 2/6] x86-64: Convert stack protector to normal percpu variable
Quoted 12 lines · David Laight
From: Brian Gerst
Quoted 10 lines
Sent: 30 November 2021 20:56
Older versions of GCC fixed the location of the stack protector canary at %gs:40. This constraint forced the percpu section to be linked at virtual address 0 so that the canary could be the first data object in the percpu section. Supporting the zero-based percpu section requires additional code to handle relocations for RIP-relative references to percpu data, extra complexity to kallsyms, and workarounds for linker bugs due to the use of absolute symbols.
Quoted 6 lines
Quoted 4 lines
Since version 8.1, GCC has options to configure the location of the canary value. This allows the canary to be turned into a normal percpu variable and removes the constraint that the percpu section be zero-based.
I didn't think the minimum gcc version has been raised as far as 8.1?
The first version of this patchset retained compatibility with older compilers, but there were objections to the increased code size to support both versions. x86-32 already dropped support for stack protector on older compilers, so v2 of this patchset does the same. The benefit of removing the fixed location version is all of the supporting code that can be removed when the stack protector becomes a standard percpu variable.
--
Brian Gerst
Re: [PATCH v2 2/6] x86-64: Convert stack protector to normal percpu variable
In reply to David LaightQuoted 11 lines · David Laight
From: Brian Gerst
Quoted 10 lines
Sent: 30 November 2021 20:56
Older versions of GCC fixed the location of the stack protector canary at %gs:40. This constraint forced the percpu section to be linked at virtual address 0 so that the canary could be the first data object in the percpu section. Supporting the zero-based percpu section requires additional code to handle relocations for RIP-relative references to percpu data, extra complexity to kallsyms, and workarounds for linker bugs due to the use of absolute symbols.
Quoted 6 lines
Quoted 4 lines
Since version 8.1, GCC has options to configure the location of the canary value. This allows the canary to be turned into a normal percpu variable and removes the constraint that the percpu section be zero-based.
I didn't think the minimum gcc version has been raised as far as 8.1?
Older GCC can still build a kernel, just not with stack protector on. And 8.1 is already 3 years old. If you run ancient distros, you can run ancient kernels too.