Skip to contentSkip to conversation

x86/rust: use `-Zindirect-branch-cs-prefix` for RETPOLINE builds

PATCH Applied Miguel Ojeda 3 participants 5 messages Oct 18 – Nov 17

Subsystems (4)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 x86/rust: use `-Zindirect-branch-cs-prefix` for RETPOLINE builds

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:
     <TASK>
     ? _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 <cc> 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.

---
 arch/x86/Makefile | 1 +
 rust/Makefile     | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
arch/x86/Makefile +1 -0
index 8af6b80cffdd..01c848855f99 100644
@@ -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
rust/Makefile +1 -1
index da1a7409d984..35868a47e18b 100644
@@ -167,7 +167,7 @@ pin_init-flags := \
 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)
 
 quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
       cmd_rustdoc = \

base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907

Signature · 1 line
2.56.0
#1 lore raw
Quoted 65 lines · Miguel Ojeda

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:
     <TASK>
     ? _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 <cc> 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.

Quoted 7 lines

Not sure if Fixes: tags may be wanted here, since this was simply a matter of not having added the support after we got it into the upstream compiler, but two candidates are:

Fixes 97e59672a9d2 ("x86/ibt: Add paranoid FineIBT mode")
Fixes 4a1e02b15ac1 ("x86,retpoline: Optimize patch_retpoline()")

I hope this helps.

Cheers,
Miguel

#2 lore raw
In reply to Miguel Ojeda
Quoted 9 lines · Miguel Ojeda

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:

Any comments on this?

If it helps, then I am happy to take it via one of the Rust trees with an Acked-by from x86.

Thanks!

Cheers,
Miguel

maintainer Acked-by
#3 lore raw
Quoted 13 lines · Miguel Ojeda
Quoted 9 lines · Miguel Ojeda

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:

Any comments on this?

Quoted 7 lines

If it helps, then I am happy to take it via one of the Rust trees with an Acked-by from x86.

Thanks!

Cheers,
Miguel

LGTM.

Signature · 1 line
Small device. Typos and formatting crap
Applied
#4 lore raw
In reply to Miguel Ojeda
Quoted 65 lines · Miguel Ojeda

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:
     <TASK>
     ? _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 <cc> 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.

Quoted 7 lines

Applied to rust-fixes -- thanks everyone!

Cheers,
Miguel