From: Klara Modin <klarasmodin@gmail.com>
To: Borislav Petkov <bp@alien8.de>, Josh Poimboeuf <jpoimboe@kernel.org>
Cc: "Kaplan, David" <David.Kaplan@amd.com>,
Ingo Molnar <mingo@kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-tip-commits@vger.kernel.org"
<linux-tip-commits@vger.kernel.org>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
"x86@kernel.org" <x86@kernel.org>,
David Howells <dhowells@redhat.com>
Subject: Re: [PATCH -v2] x86/retpoline: Ensure default return thunk isn't used at runtime
Date: Wed, 3 Apr 2024 19:10:17 +0200 [thread overview]
Message-ID: <78e0d19c-b77a-4169-a80f-2eef91f4a1d6@gmail.com> (raw)
In-Reply-To: <20240212104348.GCZcn2ZPr445KUyQ7k@fat_crate.local>
[-- Attachment #1: Type: text/plain, Size: 8053 bytes --]
Hi,
On 2024-02-12 11:43, Borislav Petkov wrote:
> On Wed, Feb 07, 2024 at 11:49:19AM -0800, Josh Poimboeuf wrote:
>> LGTM, thanks!
>
> Thanks, had to hoist up both THUNK macros into the header to make that
> nuisance 32-bit build too :)
>
> ---
>
> commit 4461438a8405e800f90e0e40409e5f3d07eed381 (HEAD -> refs/heads/tip-x86-bugs)
> Author: Josh Poimboeuf <jpoimboe@kernel.org>
> Date: Wed Jan 3 19:36:26 2024 +0100
>
> x86/retpoline: Ensure default return thunk isn't used at runtime
>
> Make sure the default return thunk is not used after all return
> instructions have been patched by the alternatives because the default
> return thunk is insufficient when it comes to mitigating Retbleed or
> SRSO.
>
> Fix based on an earlier version by David Kaplan <david.kaplan@amd.com>.
>
> [ bp: Fix the compilation error of warn_thunk_thunk being an invisible
> symbol, hoist thunk macro into calling.h ]
>
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de>
> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
> Link: https://lore.kernel.org/r/20231010171020.462211-4-david.kaplan@amd.com
> Link: https://lore.kernel.org/r/20240104132446.GEZZaxnrIgIyat0pqf@fat_crate.local
>
With this patch/commit, one of my machines (older P4 Xeon, 32-bit only)
hangs on boot with CONFIG_RETHUNK=y / CONFIG_MITIGATION_RETHUNK=y.
> diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
> index 39e069b68c6e..bd31b2534053 100644
> --- a/arch/x86/entry/calling.h
> +++ b/arch/x86/entry/calling.h
> @@ -426,3 +426,63 @@ For 32-bit we have the following conventions - kernel is built with
> .endm
>
> #endif /* CONFIG_SMP */
> +
> +#ifdef CONFIG_X86_64
> +
> +/* rdi: arg1 ... normal C conventions. rax is saved/restored. */
> +.macro THUNK name, func
> +SYM_FUNC_START(\name)
> + pushq %rbp
> + movq %rsp, %rbp
> +
> + pushq %rdi
> + pushq %rsi
> + pushq %rdx
> + pushq %rcx
> + pushq %rax
> + pushq %r8
> + pushq %r9
> + pushq %r10
> + pushq %r11
> +
> + call \func
> +
> + popq %r11
> + popq %r10
> + popq %r9
> + popq %r8
> + popq %rax
> + popq %rcx
> + popq %rdx
> + popq %rsi
> + popq %rdi
> + popq %rbp
> + RET
> +SYM_FUNC_END(\name)
> + _ASM_NOKPROBE(\name)
> +.endm
> +
> +#else /* CONFIG_X86_32 */
> +
> +/* put return address in eax (arg1) */
> +.macro THUNK name, func, put_ret_addr_in_eax=0
> +SYM_CODE_START_NOALIGN(\name)
> + pushl %eax
> + pushl %ecx
> + pushl %edx
> +
> + .if \put_ret_addr_in_eax
> + /* Place EIP in the arg1 */
> + movl 3*4(%esp), %eax
> + .endif
> +
> + call \func
> + popl %edx
> + popl %ecx
> + popl %eax
> + RET
> + _ASM_NOKPROBE(\name)
> +SYM_CODE_END(\name)
> + .endm
> +
> +#endif
> diff --git a/arch/x86/entry/entry.S b/arch/x86/entry/entry.S
> index 8c8d38f0cb1d..582731f74dc8 100644
> --- a/arch/x86/entry/entry.S
> +++ b/arch/x86/entry/entry.S
> @@ -7,6 +7,8 @@
> #include <linux/linkage.h>
> #include <asm/msr-index.h>
>
> +#include "calling.h"
> +
> .pushsection .noinstr.text, "ax"
>
> SYM_FUNC_START(entry_ibpb)
> @@ -20,3 +22,5 @@ SYM_FUNC_END(entry_ibpb)
> EXPORT_SYMBOL_GPL(entry_ibpb);
>
> .popsection
> +
> +THUNK warn_thunk_thunk, __warn_thunk
> diff --git a/arch/x86/entry/thunk_32.S b/arch/x86/entry/thunk_32.S
> index 0103e103a657..da37f42f4549 100644
> --- a/arch/x86/entry/thunk_32.S
> +++ b/arch/x86/entry/thunk_32.S
> @@ -4,33 +4,15 @@
> * Copyright 2008 by Steven Rostedt, Red Hat, Inc
> * (inspired by Andi Kleen's thunk_64.S)
> */
> - #include <linux/export.h>
> - #include <linux/linkage.h>
> - #include <asm/asm.h>
>
> - /* put return address in eax (arg1) */
> - .macro THUNK name, func, put_ret_addr_in_eax=0
> -SYM_CODE_START_NOALIGN(\name)
> - pushl %eax
> - pushl %ecx
> - pushl %edx
> +#include <linux/export.h>
> +#include <linux/linkage.h>
> +#include <asm/asm.h>
>
> - .if \put_ret_addr_in_eax
> - /* Place EIP in the arg1 */
> - movl 3*4(%esp), %eax
> - .endif
> +#include "calling.h"
>
> - call \func
> - popl %edx
> - popl %ecx
> - popl %eax
> - RET
> - _ASM_NOKPROBE(\name)
> -SYM_CODE_END(\name)
> - .endm
> -
> - THUNK preempt_schedule_thunk, preempt_schedule
> - THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
> - EXPORT_SYMBOL(preempt_schedule_thunk)
> - EXPORT_SYMBOL(preempt_schedule_notrace_thunk)
> +THUNK preempt_schedule_thunk, preempt_schedule
> +THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
> +EXPORT_SYMBOL(preempt_schedule_thunk)
> +EXPORT_SYMBOL(preempt_schedule_notrace_thunk)
>
> diff --git a/arch/x86/entry/thunk_64.S b/arch/x86/entry/thunk_64.S
> index 416b400f39db..119ebdc3d362 100644
> --- a/arch/x86/entry/thunk_64.S
> +++ b/arch/x86/entry/thunk_64.S
> @@ -9,39 +9,6 @@
> #include "calling.h"
> #include <asm/asm.h>
>
> - /* rdi: arg1 ... normal C conventions. rax is saved/restored. */
> - .macro THUNK name, func
> -SYM_FUNC_START(\name)
> - pushq %rbp
> - movq %rsp, %rbp
> -
> - pushq %rdi
> - pushq %rsi
> - pushq %rdx
> - pushq %rcx
> - pushq %rax
> - pushq %r8
> - pushq %r9
> - pushq %r10
> - pushq %r11
> -
> - call \func
> -
> - popq %r11
> - popq %r10
> - popq %r9
> - popq %r8
> - popq %rax
> - popq %rcx
> - popq %rdx
> - popq %rsi
> - popq %rdi
> - popq %rbp
> - RET
> -SYM_FUNC_END(\name)
> - _ASM_NOKPROBE(\name)
> - .endm
> -
> THUNK preempt_schedule_thunk, preempt_schedule
> THUNK preempt_schedule_notrace_thunk, preempt_schedule_notrace
> EXPORT_SYMBOL(preempt_schedule_thunk)
> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
> index 2c0679ebe914..55754617eaee 100644
> --- a/arch/x86/include/asm/nospec-branch.h
> +++ b/arch/x86/include/asm/nospec-branch.h
> @@ -357,6 +357,8 @@ extern void entry_ibpb(void);
>
> extern void (*x86_return_thunk)(void);
>
> +extern void __warn_thunk(void);
> +
> #ifdef CONFIG_MITIGATION_CALL_DEPTH_TRACKING
> extern void call_depth_return_thunk(void);
>
> diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
> index f2775417bda2..a78892b0f823 100644
> --- a/arch/x86/kernel/cpu/bugs.c
> +++ b/arch/x86/kernel/cpu/bugs.c
> @@ -2850,3 +2850,8 @@ ssize_t cpu_show_gds(struct device *dev, struct device_attribute *attr, char *bu
> return cpu_show_common(dev, attr, buf, X86_BUG_GDS);
> }
> #endif
> +
> +void __warn_thunk(void)
> +{
> + WARN_ONCE(1, "Unpatched return thunk in use. This should not happen!\n");
> +}
> diff --git a/arch/x86/lib/retpoline.S b/arch/x86/lib/retpoline.S
> index 0045153ba222..721b528da9ac 100644
> --- a/arch/x86/lib/retpoline.S
> +++ b/arch/x86/lib/retpoline.S
> @@ -369,19 +369,16 @@ SYM_FUNC_END(call_depth_return_thunk)
> * 'JMP __x86_return_thunk' sites are changed to something else by
> * apply_returns().
> *
> - * This should be converted eventually to call a warning function which
> - * should scream loudly when the default return thunk is called after
> - * alternatives have been applied.
> - *
> - * That warning function cannot BUG() because the bug splat cannot be
> - * displayed in all possible configurations, leading to users not really
> - * knowing why the machine froze.
> + * The ALTERNATIVE below adds a really loud warning to catch the case
> + * where the insufficient default return thunk ends up getting used for
> + * whatever reason like miscompilation or failure of
> + * objtool/alternatives/etc to patch all the return sites.
> */
> SYM_CODE_START(__x86_return_thunk)
> UNWIND_HINT_FUNC
> ANNOTATE_NOENDBR
> - ANNOTATE_UNRET_SAFE
> - ret
> + ALTERNATIVE __stringify(ANNOTATE_UNRET_SAFE; ret), \
> + "jmp warn_thunk_thunk", X86_FEATURE_ALWAYS
> int3
> SYM_CODE_END(__x86_return_thunk)
> EXPORT_SYMBOL(__x86_return_thunk)
>
This seems to be the problematic snippet. Reverting it alone fixes the
issue for me. I wonder if it could have anything to do with the previous
comment text?
Please let me know if there's anything else you need.
Kind regards,
Klara Modin
[-- Attachment #2: xeon2p-console.log.gz --]
[-- Type: application/gzip, Size: 3145 bytes --]
[-- Attachment #3: xeon2p-no-boot-bisect.log --]
[-- Type: text/x-log, Size: 2654 bytes --]
# bad: [39cd87c4eb2b893354f3b850f916353f2658ae6f] Linux 6.9-rc2
# good: [e8f897f4afef0031fe618a8e94127a0934896aba] Linux 6.8
git bisect start 'v6.9-rc2' 'v6.8'
# bad: [e5e038b7ae9da96b93974bf072ca1876899a01a3] Merge tag 'fs_for_v6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
git bisect bad e5e038b7ae9da96b93974bf072ca1876899a01a3
# bad: [1f440397665f4241346e4cc6d93f8b73880815d1] Merge tag 'docs-6.9' of git://git.lwn.net/linux
git bisect bad 1f440397665f4241346e4cc6d93f8b73880815d1
# bad: [508f34f2381eb84b2335abb970b940aefef50a19] Merge tag 'm68k-for-v6.9-tag1' of git://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k
git bisect bad 508f34f2381eb84b2335abb970b940aefef50a19
# good: [8ede842f669b6f78812349bbef4d1efd0fbdafce] Merge tag 'rust-6.9' of https://github.com/Rust-for-Linux/linux
git bisect good 8ede842f669b6f78812349bbef4d1efd0fbdafce
# good: [bfdb395a7cde12d83a623949ed029b0ab38d765b] Merge tag 'x86_mtrr_for_v6.9_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect good bfdb395a7cde12d83a623949ed029b0ab38d765b
# bad: [685d98211273f60e38a6d361b62d7016c545297e] Merge tag 'x86-core-2024-03-11' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
git bisect bad 685d98211273f60e38a6d361b62d7016c545297e
# good: [b0402403e54ae9eb94ce1cbb53c7def776e97426] Merge tag 'edac_updates_for_v6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/ras/ras
git bisect good b0402403e54ae9eb94ce1cbb53c7def776e97426
# bad: [cb81deefb59de01325ab822f900c13941bfaf67f] x86/idle: Sanitize X86_BUG_AMD_E400 handling
git bisect bad cb81deefb59de01325ab822f900c13941bfaf67f
# good: [3a1d3829e193c091475ceab481c5f8deab385023] x86/percpu: Avoid sparse warning with cast to named address space
git bisect good 3a1d3829e193c091475ceab481c5f8deab385023
# bad: [4461438a8405e800f90e0e40409e5f3d07eed381] x86/retpoline: Ensure default return thunk isn't used at runtime
git bisect bad 4461438a8405e800f90e0e40409e5f3d07eed381
# good: [aefb2f2e619b6c334bcb31de830aa00ba0b11129] x86/bugs: Rename CONFIG_RETPOLINE => CONFIG_MITIGATION_RETPOLINE
git bisect good aefb2f2e619b6c334bcb31de830aa00ba0b11129
# good: [1da8d2172ce5175118929363fe568e41f24ad3d6] x86/bugs: Rename CONFIG_CPU_IBRS_ENTRY => CONFIG_MITIGATION_IBRS_ENTRY
git bisect good 1da8d2172ce5175118929363fe568e41f24ad3d6
# good: [0911b8c52c4d68c57d02f172daa55a42bce703f0] x86/bugs: Rename CONFIG_RETHUNK => CONFIG_MITIGATION_RETHUNK
git bisect good 0911b8c52c4d68c57d02f172daa55a42bce703f0
# first bad commit: [4461438a8405e800f90e0e40409e5f3d07eed381] x86/retpoline: Ensure default return thunk isn't used at runtime
[-- Attachment #4: xeon2p-working.log.gz --]
[-- Type: application/gzip, Size: 12934 bytes --]
[-- Attachment #5: config.gz --]
[-- Type: application/gzip, Size: 32562 bytes --]
next prev parent reply other threads:[~2024-04-03 17:10 UTC|newest]
Thread overview: 93+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-10 17:10 [PATCH 0/3] " David Kaplan
2023-10-10 17:10 ` [PATCH 1/3] Revert "x86/retpoline: Remove .text..__x86.return_thunk section" David Kaplan
2023-10-10 17:48 ` Peter Zijlstra
2023-10-10 19:57 ` Josh Poimboeuf
2023-10-10 20:04 ` Borislav Petkov
2023-10-10 20:19 ` Josh Poimboeuf
2023-10-10 20:40 ` Kaplan, David
2023-10-10 21:22 ` Josh Poimboeuf
2023-10-11 7:41 ` Peter Zijlstra
2023-10-11 9:34 ` Borislav Petkov
2023-10-11 16:28 ` Josh Poimboeuf
2023-10-11 22:35 ` Peter Zijlstra
2023-10-11 22:42 ` Ingo Molnar
2023-10-12 2:27 ` Josh Poimboeuf
2023-10-12 2:47 ` [PATCH v2] objtool: Fix return thunk patching in retpolines Josh Poimboeuf
2023-10-12 6:25 ` [tip: x86/bugs] " tip-bot2 for Josh Poimboeuf
2023-10-12 8:16 ` [PATCH v2] " Peter Zijlstra
2023-10-12 17:50 ` [tip: x86/bugs] " tip-bot2 for Josh Poimboeuf
2023-10-20 11:37 ` tip-bot2 for Josh Poimboeuf
2023-10-12 8:16 ` [PATCH 1/3] Revert "x86/retpoline: Remove .text..__x86.return_thunk section" Peter Zijlstra
2023-10-10 17:10 ` [PATCH 2/3] x86/vdso: Run objtool on vdso32-setup David Kaplan
2023-10-12 17:50 ` [tip: x86/bugs] x86/vdso: Run objtool on vdso32-setup.o tip-bot2 for David Kaplan
2023-10-20 11:37 ` tip-bot2 for David Kaplan
2023-10-10 17:10 ` [PATCH 3/3] x86/retpoline: Ensure default return thunk isn't used at runtime David Kaplan
2023-10-10 19:36 ` Josh Poimboeuf
2023-10-10 20:14 ` Kaplan, David
2023-10-10 20:41 ` Josh Poimboeuf
2023-10-12 14:10 ` [PATCH -v2] " Borislav Petkov
2023-10-12 17:11 ` Josh Poimboeuf
2023-10-12 17:50 ` [tip: x86/bugs] " tip-bot2 for David Kaplan
2023-10-16 21:10 ` Nathan Chancellor
2023-10-16 21:29 ` Borislav Petkov
2023-10-16 21:48 ` Nathan Chancellor
2023-10-17 4:31 ` Kaplan, David
2023-10-17 5:28 ` Josh Poimboeuf
2023-10-17 13:54 ` Kaplan, David
2023-10-17 15:24 ` Nick Desaulniers
2023-10-17 15:26 ` Marco Elver
2023-10-17 15:32 ` Nathan Chancellor
2023-10-17 16:59 ` [PATCH] x86/srso: Fix panic in return thunk during boot Josh Poimboeuf
2023-10-17 17:52 ` [tip: x86/bugs] x86/retpoline: Make sure there are no unconverted return thunks due to KCSAN tip-bot2 for Josh Poimboeuf
2023-10-20 11:37 ` tip-bot2 for Josh Poimboeuf
2023-10-18 13:23 ` [tip: x86/bugs] x86/retpoline: Ensure default return thunk isn't used at runtime Borislav Petkov
2023-10-18 13:38 ` Ingo Molnar
2023-10-18 15:12 ` Borislav Petkov
2023-10-18 15:54 ` Josh Poimboeuf
2023-10-18 17:55 ` Borislav Petkov
2023-10-18 18:14 ` Josh Poimboeuf
2023-10-18 18:22 ` Borislav Petkov
2023-10-18 18:39 ` Josh Poimboeuf
2023-10-18 18:44 ` Borislav Petkov
2023-10-18 19:14 ` Josh Poimboeuf
2023-10-18 20:04 ` Borislav Petkov
2023-10-18 20:37 ` Borislav Petkov
2023-10-19 6:35 ` Josh Poimboeuf
2023-10-19 6:59 ` Josh Poimboeuf
2023-10-19 14:15 ` Borislav Petkov
2023-10-19 14:21 ` Kaplan, David
2023-10-19 14:39 ` Borislav Petkov
2023-10-19 15:20 ` Josh Poimboeuf
2023-10-24 20:19 ` Borislav Petkov
2024-01-03 18:46 ` Borislav Petkov
2024-01-04 13:12 ` Borislav Petkov
2024-01-04 13:24 ` [PATCH -v2] " Borislav Petkov
2024-01-04 13:26 ` Borislav Petkov
2024-02-07 17:50 ` Josh Poimboeuf
2024-02-07 18:53 ` Borislav Petkov
2024-02-07 19:49 ` Josh Poimboeuf
2024-02-12 10:43 ` Borislav Petkov
2024-04-03 17:10 ` Klara Modin [this message]
2024-04-03 17:30 ` Borislav Petkov
2024-04-03 20:26 ` Klara Modin
2024-04-03 20:41 ` Borislav Petkov
2024-04-03 22:25 ` Klara Modin
2024-04-04 14:44 ` Borislav Petkov
2024-04-16 9:27 ` Borislav Petkov
2024-04-17 3:59 ` Klara Modin
2024-04-17 16:20 ` [tip: x86/urgent] x86/retpolines: Enable the default thunk warning only on relevant configs tip-bot2 for Borislav Petkov (AMD)
2023-10-19 7:43 ` [tip: x86/bugs] x86/retpoline: Ensure default return thunk isn't used at runtime Peter Zijlstra
2023-10-19 9:40 ` [tip: x86/bugs] Revert "x86/retpoline: Remove .text..__x86.return_thunk section" tip-bot2 for Borislav Petkov (AMD)
2023-10-19 9:40 ` [tip: x86/bugs] Revert "x86/retpoline: Ensure default return thunk isn't used at runtime" tip-bot2 for Borislav Petkov (AMD)
2024-10-04 19:37 ` Peter Zijlstra
2024-02-12 14:13 ` [tip: x86/bugs] x86/retpoline: Ensure default return thunk isn't used at runtime tip-bot2 for Josh Poimboeuf
2024-02-15 3:20 ` Nathan Chancellor
2024-02-15 8:30 ` Nikolay Borisov
2024-02-15 15:53 ` Borislav Petkov
2024-02-16 5:42 ` Josh Poimboeuf
2024-02-16 21:27 ` Borislav Petkov
2024-02-20 5:57 ` [PATCH] x86/vdso: Fix rethunk patching for vdso-image-{32,64}.o Josh Poimboeuf
2024-02-20 12:31 ` [tip: x86/core] " tip-bot2 for Josh Poimboeuf
2023-10-10 17:52 ` [PATCH 0/3] Ensure default return thunk isn't used at runtime Peter Zijlstra
2023-10-20 11:28 ` Subject: [PATCH] x86/retpoline: Document some thunk handling aspects (was: Re: [PATCH 0/3] Ensure default return thunk isn't used at runtime) Borislav Petkov
2023-10-20 11:37 ` [tip: x86/bugs] x86/retpoline: Document some thunk handling aspects tip-bot2 for Borislav Petkov (AMD)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=78e0d19c-b77a-4169-a80f-2eef91f4a1d6@gmail.com \
--to=klarasmodin@gmail.com \
--cc=David.Kaplan@amd.com \
--cc=bp@alien8.de \
--cc=dhowells@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome