From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796228566; b=AxseCPmBfiWyWEgnqx0sJ/7uA2U+hTqD+rrWHHzBVyOUnjuQkOpnxZ4WDumKp8cttWOs TlGFgfIt+DiiBzteYtM4EZV6NZCEWKZ+X9wtFMbAbv76Y/n+aLNI/hnGK3Mr8Ctn91sOi zK6deS3vhsrHPfRO5bKtShCkYCSzwoOxNDs21k5k0R5sgalq7gRkgiCbcnZtTH+DWgxTd cs7u9dybqsBTTtgxb6ghiodIuBmhbldTLuz94VHQ85asg4JnWob1t7dUy+oGMhvmUUTkU cuOkYqgS+kOu2HiHTKnYjXmpds9uo67o7hRQZmCc+HrPfgFY7nWNSTe3Jtvs+6PT9ZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796228566; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=ByxE5vg5fl3ZWKoInZCOxm3BPjslre2s+R2Zyc9AfLs=; b=Rz10x+U+K4FdYyB8NTnYAUOSDAfAYk4RBHdUW9ocbLLz5J0HHTrGzR+zrDR1nBP+ltas vCbWTchi7DYUj31UDyJAJn4KkWtlUd8eqHYCG6WSb9lZrrKGDv2BVgFmGBs7fo1utmHIi vkr0UQkWaQ+7IRhgkA4cypGQz0Jz7JU80PgKutm3kV1iyCkOGV/X5GNo2asMT/Gp6L+s7 IptVhEw4nsVnMmeJx1ZjdpZ86QLbwLCp09Y3k2OmQ/p/mCosmnUTH8CiXtO4UtVm7VS+Q 2f9p0iR3ebDtYyOIFQNCWFN8c5C5sN2Liqg9U8lzppfYxi1cqWBBlpOY5Fslp14qBCg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CFgsY6uC; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E750606A31A for ; Wed, 2 Dec 2026 16:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=CFgsY6uC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02D9E1F000C1; Wed, 2 Dec 2026 16:22:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796228566; bh=ByxE5vg5fl3ZWKoInZCOxm3BPjslre2s+R2Zyc9AfLs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CFgsY6uCJ0aIAXWSmDnyi/ZQxIunjTZsEAdgRhOBYBKJlwCIyZbslu+zE81L0j6RK tLEwM8ocAGmqjkMLvhTHvn0s3zVt+3eoM/XAsk9XS+/WoIJF6g5YQj4sqBZmDSfnAi WTWcTzZLc2DDlove4ivaIQrYdBQH3ReNZGSXhEG79DTO17kxIQPS0y6VDGNi4UucxO UlYLamJimFfGDoyP8ZTgTgYvdXmuyX3hafDjAmjxTAc4x0/BsjAnB6d8X5OEzgE/aA VQSWAIv//Uj766A6yFvDIBm++vfSFptzBXvaTthcQzYVmhwqF/H39yLsabZxN6Ccxf FR0HGlIkanjDg== From: Miguel Ojeda To: stable@vger.kernel.org Cc: Miguel Ojeda , Kent Overstreet , Borislav Petkov Subject: [PATCH 6.18.y] x86/rust: use `-Zindirect-branch-cs-prefix` for RETPOLINE builds Date: Wed, 2 Dec 2026 17:22:30 +0100 Message-ID: <20261202162230.20170-1-ojeda@kernel.org> In-Reply-To: <2026120231-waltz-poncho-8000@gregkh> References: <2026120231-waltz-poncho-8000@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The C compiler is asked to add a `cs` prefix to the `call` and `jmp` instructions that go through the indirect thunks for the `r8`-`r15` registers, which makes them 6 bytes long instead of 5. That gives `patch_retpoline()` enough room to inline them, e.g. as `lfence; call *%r11` for `spectre_v2=retpoline,lfence`. However, Rust code does not get the prefix, e.g. for a `CONFIG_MITIGATION_RETPOLINE=y` build: $ objdump -dr rust/kernel.o ... e2b: e8 00 00 00 00 call e30 <...> e2c: R_X86_64_PLT32 __x86_indirect_thunk_r11-0x4 Moreover, since commit 4a1e02b15ac1 ("x86,retpoline: Optimize patch_retpoline()"), `emit_indirect()` assumes there is enough room for the inlined sequence -- e.g. with `spectre_v2=retpoline,lfence`, the kernel warns and dies while patching a site inside `core`: WARNING: arch/x86/kernel/alternative.c:736 at apply_retpolines+0x2ee/0x400, CPU#0: swapper/0/0 ... Call Trace: ? _RNvMs6_NtNtCshS2CUL9MEDn_4core3fmt8buildersNtB5_9DebugList21finish_non_exhaustive+0x5b/0xd0 ... alternative_instructions+0x1d/0x60 arch_cpu_finalize_init+0x5a/0xb0 start_kernel+0x1f4/0x280 Similarly, paranoid FineIBT rewrites each kCFI call site into a fixed 20-byte sequence, i.e. the 14 bytes of the kCFI check plus a 6-byte call. For a 5-byte call, that overwrites the first byte of the next instruction, corrupting Rust code: SMP alternatives: Using paranoid FineIBT CFI ... Oops: double fault: 0000 [#1] ... RIP: 0010:_RNvNtCsdVypMZsLRAh_4core3fmt5write+0x1e9/0x210 Code: ... b8 68 81 16 9f 41 3b 43 f5 2e 4d 8d 5b f0 75 fd 41 ff e3 c0 eb 02 b0 01 ... ... Kernel panic - not syncing: Fatal exception in interrupt Non-paranoid FineIBT is not affected, since it only rewrites the kCFI check and leaves the call itself to `apply_retpolines()`. We added support for `-Zindirect-branch-cs-prefix` [1][2] in Rust 1.91.0 [3]. Thus pass it for `RETPOLINE` builds when possible, which makes all sites indeed get the prefix: e3c: 2e e8 00 00 00 00 cs call e42 <...> e3e: R_X86_64_PLT32 __x86_indirect_thunk_r11-0x4 The kernel above boots fine with `spectre_v2=retpoline,lfence`, inlining the Rust sites as well. For older Rust compilers, set the LLVM module flag instead, which has the same code-generation effect. This means that every Rust version that the kernel supports gets the prefix, that the kernel boots fine with `spectre_v2=retpoline,lfence`, and that paranoid FineIBT no longer corrupts. In addition, add it to our target modifier workaround for `rustdoc`. Cc: stable@vger.kernel.org # 6.18.y+ Link: https://github.com/rust-lang/rust/issues/116852 [1] Link: https://github.com/rust-lang/compiler-team/issues/899 [2] Link: https://github.com/rust-lang/rust/pull/140740 [3] Reported-by: Kent Overstreet Acked-by: Borislav Petkov Link: https://patch.msgid.link/20261018175457.87803-1-ojeda@kernel.org Signed-off-by: Miguel Ojeda (cherry picked from commit 1eaa31fa41125db78a50abef7f0a35aa22af40d0) --- arch/x86/Makefile | 1 + rust/Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 9f3553263f7b..82c75562aa35 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -21,6 +21,7 @@ RETPOLINE_CFLAGS := -mretpoline-external-thunk RETPOLINE_VDSO_CFLAGS := -mretpoline endif RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix) +RETPOLINE_RUSTFLAGS += $(if $(call rustc-min-version,109100),-Zindirect-branch-cs-prefix,-Zllvm_module_flag=indirect_branch_cs_prefix:u32:1:override) ifdef CONFIG_MITIGATION_RETHUNK RETHUNK_CFLAGS := -mfunction-return=thunk-extern diff --git a/rust/Makefile b/rust/Makefile index d6429e68949f..6f4002a3e77a 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -70,7 +70,7 @@ core-edition := $(if $(call rustc-min-version,108700),2024,2021) rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18) # Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465). -doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer) +doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer$(comma)indirect-branch-cs-prefix) # `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only # since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust base-commit: 2c7bb1d0212c23234a9f85123eb7a36eecc76776 -- 2.56.1