Skip to content

Commit d6d3991

Browse files
ardbiesheuvelintel-lab-lkp
authored andcommitted
x86/stackprotector: Work around strict Clang TLS symbol requirements
GCC and Clang both implement stack protector support based on Thread Local Storage (TLS) variables, and this is used in the kernel to implement per-task stack cookies, by copying a task's stack cookie into a per-CPU variable every time it is scheduled in. Both now also implement -mstack-protector-guard-symbol=, which permits the TLS variable to be specified directly. This is useful because it will allow us to move away from using a fixed offset of 40 bytes into the per-CPU area on x86_64, which requires a lot of special handling in the per-CPU code and the runtime relocation code. However, while GCC is rather lax in its implementation of this command line option, Clang actually requires that the provided symbol name refers to a TLS variable (i.e., one declared with __thread), although it also permits the variable to be undeclared entirely, in which case it will use an implicit declaration of the right type. The upshot of this is that Clang will emit the correct references to the stack cookie variable in most cases, e.g., 10d: 64 a1 00 00 00 00 mov %fs:0x0,%eax 10f: R_386_32 __stack_chk_guard However, if a non-TLS definition of the symbol in question is visible in the same compilation unit (which amounts to the whole of vmlinux if LTO is enabled), it will drop the per-CPU prefix and emit a load from a bogus address. Work around this by using a symbol name that never occurs in C code, and emit it as an alias in the linker script. Fixes: 3fb0fdb ("x86/stackprotector/32: Make the canary into a regular percpu variable") Cc: <[email protected]> Cc: Fangrui Song <[email protected]> Cc: Uros Bizjak <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Andy Lutomirski <[email protected]> Link: ClangBuiltLinux#1854 Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Brian Gerst <[email protected]>
1 parent 4b99847 commit d6d3991

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

arch/x86/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ ifeq ($(CONFIG_X86_32),y)
142142

143143
ifeq ($(CONFIG_STACKPROTECTOR),y)
144144
ifeq ($(CONFIG_SMP),y)
145-
KBUILD_CFLAGS += -mstack-protector-guard-reg=fs -mstack-protector-guard-symbol=__stack_chk_guard
145+
KBUILD_CFLAGS += -mstack-protector-guard-reg=fs \
146+
-mstack-protector-guard-symbol=__ref_stack_chk_guard
146147
else
147-
KBUILD_CFLAGS += -mstack-protector-guard=global
148+
KBUILD_CFLAGS += -mstack-protector-guard=global
148149
endif
149150
endif
150151
else

arch/x86/entry/entry.S

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,19 @@ EXPORT_SYMBOL_GPL(mds_verw_sel);
5151
.popsection
5252

5353
THUNK warn_thunk_thunk, __warn_thunk
54+
55+
#ifndef CONFIG_X86_64
56+
/*
57+
* Clang's implementation of TLS stack cookies requires the variable in
58+
* question to be a TLS variable. If the variable happens to be defined as an
59+
* ordinary variable with external linkage in the same compilation unit (which
60+
* amounts to the whole of vmlinux with LTO enabled), Clang will drop the
61+
* segment register prefix from the references, resulting in broken code. Work
62+
* around this by avoiding the symbol used in -mstack-protector-guard-symbol=
63+
* entirely in the C code, and use an alias emitted by the linker script
64+
* instead.
65+
*/
66+
#ifdef CONFIG_STACKPROTECTOR
67+
EXPORT_SYMBOL(__ref_stack_chk_guard);
68+
#endif
69+
#endif

arch/x86/include/asm/asm-prototypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@
2020
extern void cmpxchg8b_emu(void);
2121
#endif
2222

23+
#if defined(__GENKSYMS__) && defined(CONFIG_STACKPROTECTOR)
24+
extern unsigned long __ref_stack_chk_guard;
25+
#endif

arch/x86/kernel/cpu/common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,8 +2091,10 @@ void syscall_init(void)
20912091

20922092
#ifdef CONFIG_STACKPROTECTOR
20932093
DEFINE_PER_CPU(unsigned long, __stack_chk_guard);
2094+
#ifndef CONFIG_SMP
20942095
EXPORT_PER_CPU_SYMBOL(__stack_chk_guard);
20952096
#endif
2097+
#endif
20962098

20972099
#endif /* CONFIG_X86_64 */
20982100

arch/x86/kernel/vmlinux.lds.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ SECTIONS
468468
. = ASSERT((_end - LOAD_OFFSET <= KERNEL_IMAGE_SIZE),
469469
"kernel image bigger than KERNEL_IMAGE_SIZE");
470470

471+
/* needed for Clang - see arch/x86/entry/entry.S */
472+
PROVIDE(__ref_stack_chk_guard = __stack_chk_guard);
473+
471474
#ifdef CONFIG_X86_64
472475
/*
473476
* Per-cpu symbols which need to be offset from __per_cpu_load

0 commit comments

Comments
 (0)