x86/rust: support RETPOLINE
Miguel Ojeda
maintainerPATCH 1/4 x86/rust: support RETPOLINE
Support the RETPOLINE speculation mitigation by enabling the target features that Clang does.
The existing target feature being enabled was a leftover from our old rust branch, and it is not enough: the target feature retpoline-external-thunk only implies retpoline-indirect-calls, but not retpoline-indirect-branches (see LLVM's X86.td), unlike Clang's flag of the same name -mretpoline-external-thunk which does imply both (see Clang's lib/Driver/ToolChains/Arch/X86.cpp).
Without this, objtool would complain if enabled for individual object files (like it is planned in the future), e.g.
rust/core.o: warning: objtool:
_R...escape_default+0x13: indirect jump found in RETPOLINE build
In addition, change the comment to note that LLVM is the one disabling jump tables when retpoline is enabled, thus we do not need to use -Zno-jump-tables for Rust here -- see commit c58f2166ab39 ("Introduce the "retpoline" x86 mitigation technique ...") [1]:
The goal is simple: avoid generating code which contains an indirect branch that could have its prediction poisoned by an attacker. In many cases, the compiler can simply use directed conditional branches and a small search tree. LLVM already has support for lowering switches in this way and the first step of this patch is to disable jump-table lowering of switches and introduce a pass to rewrite explicit indirectbr sequences into a switch over integers.
As well as a live example at [2].
--- arch/x86/Makefile | 2 +- scripts/generate_rust_target.rs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/Makefile +1 -1
@@ -195,7 +195,7 @@ ifdef CONFIG_RETPOLINE KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) # Additionally, avoid generating expensive indirect jumps which # are subject to retpolines for small number of switch cases. - # clang turns off jump table generation by default when under + # LLVM turns off jump table generation by default when under # retpoline builds, however, gcc does not for x86. This has # only been fixed starting from gcc stable version 8.4.0 and # onwards, but not for older ones. See gcc bug #86952.
scripts/generate_rust_target.rs +7 -0
@@ -156,7 +156,14 @@ fn main() { );let mut features ="-3dnow,-3dnowa,-mmx,+soft-float" .to_string ();if cfg.has ("RETPOLINE" ) { +// The kernel uses `-mretpoline-external-thunk` (for Clang), which Clang maps to the +// target feature of the same name plus the other two target features in +// `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via +// `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated +// flag); see https://github.com/rust-lang/rust/issues/116852. features +=",+retpoline-external-thunk" ; + features +=",+retpoline-indirect-branches" ; + features +=",+retpoline-indirect-calls" ; } ts.push ("features" , features); ts.push ("llvm-target" ,"x86_64-linux-gnu" );
base-commit: 58720809f52779dc0f08e53e54b014209d13eebb
Signature · 1 line
2.42.0
Miguel Ojeda
maintainerPATCH 2/4 x86/rust: support SLS
Support the SLS speculation mitigation by enabling the target features that Clang does.
Without this, objtool would complain if enabled for individual object files (like it is planned in the future), e.g.
rust/core.o: warning: objtool:
_R...next_up+0x44: missing int3 after ret
--- scripts/generate_rust_target.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
scripts/generate_rust_target.rs +8 -0
@@ -165,6 +165,14 @@ fn main() { features +=",+retpoline-indirect-branches" ; features +=",+retpoline-indirect-calls" ; } +if cfg.has ("SLS" ) { +// The kernel uses `-mharden-sls=all`, which Clang maps to both these target features in +// `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via +// `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated +// flag); see https://github.com/rust-lang/rust/issues/116851. + features +=",+harden-sls-ijmp" ; + features +=",+harden-sls-ret" ; + } ts.push ("features" , features); ts.push ("llvm-target" ,"x86_64-linux-gnu" ); ts.push ("target-pointer-width" ,"64" );
Signature · 1 line
2.42.0
Miguel Ojeda
maintainerPATCH 3/4 x86/rust: depend on !RETHUNK
In reply to Miguel OjedaThe Rust compiler does not support the equivalent of -mfunction-return=thunk-extern yet [1]. Thus, currently, objtool warns about it, e.g.:
samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
'naked' return found in RETHUNK build
The support in rustc for -Zfunction-return has been submitted and is being reviewed [2]. It adds the needed LLVM function attributes and, with it, I got a RETHUNK kernel build with Rust enabled that does not print the objtool related warnings, boots in QEMU and can load a kernel loadable module.
In any case, until proper/complete support is added to rustc, make it a hard restriction until the mitigation is in place.
This may have an impact for developers that may not need/care about the mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology preview" [3]), but given we are getting closer to having the first actual in-tree Rust kernel users, it seems like the right time to disallow it. This should also avoid confusion [4].
--- init/Kconfig | 1 + 1 file changed, 1 insertion(+)
init/Kconfig +1 -0
@@ -1889,6 +1889,7 @@ config RUST depends on !GCC_PLUGINS depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE + depends on !RETHUNK select CONSTRUCTORS help Enables Rust support in the kernel.
Signature · 1 line
2.42.0
Re: [PATCH 3/4] x86/rust: depend on !RETHUNK
Quoted 13 lines · Miguel Ojeda
The Rust compiler does not support the equivalent of
-mfunction-return=thunk-externyet [1]. Thus, currently,objtoolwarns about it, e.g.:
samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
'naked' return found in RETHUNK buildThe support in
rustcfor-Zfunction-returnhas been submitted and is being reviewed [2]. It adds the needed LLVM function attributes and, with it, I got a RETHUNK kernel build with Rust enabled that does not print theobjtoolrelated warnings, boots in QEMU and can load a kernel loadable module.
Quoted 8 lines
In any case, until proper/complete support is added to
rustc, make it a hard restriction until the mitigation is in place.This may have an impact for developers that may not need/care about the mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology preview" [3]), but given we are getting closer to having the first actual in-tree Rust kernel users, it seems like the right time to disallow it. This should also avoid confusion [4].
I'll mention that I've tested boots, both in bare metal and QEMU, with RUST=y and RETHUNK=y and they were alright regardless of objtool warnings. Although, if you had an issue in the past then I'd like to know about it.
Re: [PATCH 3/4] x86/rust: depend on !RETHUNK
Quoted 5 lines · Martin Rodriguez Reboredo
I'll mention that I've tested boots, both in bare metal and QEMU, with
RUST=yandRETHUNK=yand they were alright regardless ofobjtoolwarnings. Although, if you had an issue in the past then I'd like to know about it.
These are mitigations -- things do functionally work if they are not applied, but you would be vulnerable.
In other words, it is not like e.g. IBT where you could have noticed it breaking by running it normally if you happened to have a supported platform.
Cheers,
Miguel
Martin Rodriguez Reboredo
Acked-byRe: [PATCH 3/4] x86/rust: depend on !RETHUNK
Quoted 11 lines · Miguel Ojeda
Quoted 5 lines · Martin Rodriguez Reboredo
I'll mention that I've tested boots, both in bare metal and QEMU, with
RUST=yandRETHUNK=yand they were alright regardless ofobjtoolwarnings. Although, if you had an issue in the past then I'd like to know about it.These are mitigations -- things do functionally work if they are not applied, but you would be vulnerable.
Quoted 6 lines
In other words, it is not like e.g. IBT where you could have noticed it breaking by running it normally if you happened to have a supported platform.
Cheers,
Miguel
'Guess you have a point...
Re: [PATCH 3/4] x86/rust: depend on !RETHUNK
In reply to Miguel OjedaQuoted 4 lines · Miguel Ojeda
The Rust compiler does not support the equivalent of
-mfunction-return=thunk-externyet [1]. Thus, currently,objtoolwarns about it, e.g.:
I just realized I did not Cc Andrea when I sent this, which was my intention -- done here, sorry!
Cheers,
Miguel
Miguel Ojeda
maintainerRFC PATCH 4/4 x86/rust: support RETHUNK
In reply to Miguel OjedaWhen support for -Zfunction-return lands in Rust [1], this patch may be used to enable RETHUNK support on top of the previous patch.
---
Note that this patch is not meant to be applied now, it is only here to show what will be needed to drop the depends on from the previous patch.
arch/x86/Makefile | 5 +++++ init/Kconfig | 1 - 2 files changed, 5 insertions(+), 1 deletion(-)
arch/x86/Makefile +5 -0
@@ -24,11 +24,15 @@ RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix) ifdef CONFIG_RETHUNK RETHUNK_CFLAGS := -mfunction-return=thunk-extern +RETHUNK_RUSTFLAGS := -Zfunction-return=thunk-extern RETPOLINE_CFLAGS += $(RETHUNK_CFLAGS) +RETPOLINE_RUSTFLAGS += $(RETHUNK_RUSTFLAGS) endif export RETHUNK_CFLAGS +export RETHUNK_RUSTFLAGS export RETPOLINE_CFLAGS +export RETPOLINE_RUSTFLAGS export RETPOLINE_VDSO_CFLAGS # For gcc stack alignment is specified with -mpreferred-stack-boundary,
@@ -193,6 +197,7 @@ KBUILD_CFLAGS += -fno-asynchronous-unwind-tables # Avoid indirect branches in kernel to deal with Spectre ifdef CONFIG_RETPOLINE KBUILD_CFLAGS += $(RETPOLINE_CFLAGS) + KBUILD_RUSTFLAGS += $(RETPOLINE_RUSTFLAGS) # Additionally, avoid generating expensive indirect jumps which # are subject to retpolines for small number of switch cases. # LLVM turns off jump table generation by default when under
init/Kconfig +0 -1
@@ -1889,7 +1889,6 @@ config RUST depends on !GCC_PLUGINS depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE - depends on !RETHUNK select CONSTRUCTORS help Enables Rust support in the kernel.
--
2.42.0
Quoted 3 lines · Miguel Ojeda
Support the
RETPOLINEspeculation mitigation by enabling the target features that Clang does.
x86: do you plan/want to pick this series up?
Thanks!
Cheers,
Miguel