Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b9cfa87

Browse files
committedAug 18, 2024
x86/rust: support MITIGATION_RETPOLINE
Support `MITIGATION_RETPOLINE` 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 Rust, 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]. These should be eventually enabled via `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated flag) [3]. Cc: Daniel Borkmann <daniel@iogearbox.net> Link: llvm/llvm-project@c58f216 [1] Link: https://godbolt.org/z/G4YPr58qG [2] Link: rust-lang/rust#116852 [3] Reviewed-by: Gary Guo <gary@garyguo.net> Tested-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Benno Lossin <benno.lossin@proton.me> Link: #945 Link: https://lore.kernel.org/r/20240725183325.122827-3-ojeda@kernel.org Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 1c1ff5f commit b9cfa87

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed
 

‎arch/x86/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ ifdef CONFIG_MITIGATION_RETPOLINE
220220
KBUILD_CFLAGS += $(RETPOLINE_CFLAGS)
221221
# Additionally, avoid generating expensive indirect jumps which
222222
# are subject to retpolines for small number of switch cases.
223-
# clang turns off jump table generation by default when under
223+
# LLVM turns off jump table generation by default when under
224224
# retpoline builds, however, gcc does not for x86. This has
225225
# only been fixed starting from gcc stable version 8.4.0 and
226226
# onwards, but not for older ones. See gcc bug #86952.

‎scripts/generate_rust_target.rs

+7
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,14 @@ fn main() {
164164
);
165165
let mut features = "-mmx,+soft-float".to_string();
166166
if cfg.has("MITIGATION_RETPOLINE") {
167+
// The kernel uses `-mretpoline-external-thunk` (for Clang), which Clang maps to the
168+
// target feature of the same name plus the other two target features in
169+
// `clang/lib/Driver/ToolChains/Arch/X86.cpp`. These should be eventually enabled via
170+
// `-Ctarget-feature` when `rustc` starts recognizing them (or via a new dedicated
171+
// flag); see https://github.com/rust-lang/rust/issues/116852.
167172
features += ",+retpoline-external-thunk";
173+
features += ",+retpoline-indirect-branches";
174+
features += ",+retpoline-indirect-calls";
168175
}
169176
ts.push("features", features);
170177
ts.push("llvm-target", "x86_64-linux-gnu");

0 commit comments

Comments
 (0)
Failed to load comments.