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

x86/retpoline,kprobes: Fix position of thunk sections with CONFIG_LTO_CLANG

The linker script arch/x86/kernel/vmlinux.lds.S matches the thunk
sections ".text.__x86.*" from arch/x86/lib/retpoline.S as follows:

.text {
[...]
TEXT_TEXT
[...]
__indirect_thunk_start = .;
*(.text.__x86.*)
__indirect_thunk_end = .;
[...]
}

Macro TEXT_TEXT references TEXT_MAIN which normally expands to only
".text". However, with CONFIG_LTO_CLANG, TEXT_MAIN becomes
".text .text.[0-9a-zA-Z_]*" which wrongly matches also the thunk
sections. The output layout is then different than expected. For
instance, the currently defined range [__indirect_thunk_start,
__indirect_thunk_end] becomes empty.

Prevent the problem by using ".." as the first separator, for example,
".text..__x86.indirect_thunk". This pattern is utilized by other
explicit section names which start with one of the standard prefixes,
such as ".text" or ".data", and that need to be individually selected in
the linker script.

[ nathan: Fix conflicts with SRSO and fold in fix issue brought up by
Andrew Cooper in post-review:
https://lore.kernel.org/20230803230323.1478869-1-andrew.cooper3@citrix.com ]

Fixes: dc5723b02e52 ("kbuild: add support for Clang LTO")
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Link: https://lore.kernel.org/r/20230711091952.27944-2-petr.pavlu@suse.com

authored by

Petr Pavlu and committed by
Borislav Petkov (AMD)
79cd2a11 e9fbc47b

+9 -9
+4 -4
arch/x86/kernel/vmlinux.lds.S
··· 134 134 SOFTIRQENTRY_TEXT 135 135 #ifdef CONFIG_RETPOLINE 136 136 __indirect_thunk_start = .; 137 - *(.text.__x86.indirect_thunk) 138 - *(.text.__x86.return_thunk) 137 + *(.text..__x86.indirect_thunk) 138 + *(.text..__x86.return_thunk) 139 139 __indirect_thunk_end = .; 140 140 #endif 141 141 STATIC_CALL_TEXT 142 142 143 143 ALIGN_ENTRY_TEXT_BEGIN 144 144 #ifdef CONFIG_CPU_SRSO 145 - *(.text.__x86.rethunk_untrain) 145 + *(.text..__x86.rethunk_untrain) 146 146 #endif 147 147 148 148 ENTRY_TEXT ··· 153 153 * definition. 154 154 */ 155 155 . = srso_untrain_ret_alias | (1 << 2) | (1 << 8) | (1 << 14) | (1 << 20); 156 - *(.text.__x86.rethunk_safe) 156 + *(.text..__x86.rethunk_safe) 157 157 #endif 158 158 ALIGN_ENTRY_TEXT_END 159 159 *(.gnu.warning)
+4 -4
arch/x86/lib/retpoline.S
··· 13 13 #include <asm/frame.h> 14 14 #include <asm/nops.h> 15 15 16 - .section .text.__x86.indirect_thunk 16 + .section .text..__x86.indirect_thunk 17 17 18 18 19 19 .macro POLINE reg ··· 148 148 * As a result, srso_safe_ret_alias() becomes a safe return. 149 149 */ 150 150 #ifdef CONFIG_CPU_SRSO 151 - .section .text.__x86.rethunk_untrain 151 + .section .text..__x86.rethunk_untrain 152 152 153 153 SYM_START(srso_untrain_ret_alias, SYM_L_GLOBAL, SYM_A_NONE) 154 154 ANNOTATE_NOENDBR ··· 158 158 SYM_FUNC_END(srso_untrain_ret_alias) 159 159 __EXPORT_THUNK(srso_untrain_ret_alias) 160 160 161 - .section .text.__x86.rethunk_safe 161 + .section .text..__x86.rethunk_safe 162 162 #endif 163 163 164 164 /* Needs a definition for the __x86_return_thunk alternative below. */ ··· 172 172 int3 173 173 SYM_FUNC_END(srso_safe_ret_alias) 174 174 175 - .section .text.__x86.return_thunk 175 + .section .text..__x86.return_thunk 176 176 177 177 /* 178 178 * Safety details here pertain to the AMD Zen{1,2} microarchitecture:
+1 -1
tools/objtool/check.c
··· 389 389 if (!strcmp(sec->name, ".noinstr.text") || 390 390 !strcmp(sec->name, ".entry.text") || 391 391 !strcmp(sec->name, ".cpuidle.text") || 392 - !strncmp(sec->name, ".text.__x86.", 12)) 392 + !strncmp(sec->name, ".text..__x86.", 13)) 393 393 sec->noinstr = true; 394 394 395 395 /*