[PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG

Conor Dooley posted 1 patch 1 day, 7 hours ago
arch/Kconfig | 1 +
1 file changed, 1 insertion(+)
[PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG
Posted by Conor Dooley 1 day, 7 hours ago
From: Conor Dooley <conor.dooley@microchip.com>

The kernel uses the standard rustc targets for non-x86 targets, and out
of those only 64-bit arm's target has kcfi support enabled. For x86, the
custom 64-bit target enables kcfi.

The HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC config option that allows
CFI_CLANG to be used in combination with RUST does not check whether the
rustc target supports kcfi. This breaks the build on riscv (and
presumably 32-bit arm) when CFI_CLANG and RUST are enabled at the same
time.

Ordinarily, a rustc-option check would be used to detect target support
but unfortunately rustc-option filters out the target for reasons given
in commit 46e24a545cdb4 ("rust: kasan/kbuild: fix missing flags on first
build"). As a result, if the host supports kcfi but the target does not,
e.g. when building for riscv on x86_64, the build would remain broken.

Instead, make HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC depend on the only
two architectures where the target used supports it to fix the build.

CC: stable@vger.kernel.org
Fixes: ca627e636551e ("rust: cfi: add support for CFI_CLANG with Rust")
Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
---
CC: Paul Walmsley <paul.walmsley@sifive.com>
CC: Palmer Dabbelt <palmer@dabbelt.com>
CC: Alexandre Ghiti <alex@ghiti.fr>
CC: Miguel Ojeda <ojeda@kernel.org>
CC: Alex Gaynor <alex.gaynor@gmail.com>
CC: Boqun Feng <boqun.feng@gmail.com>
CC: Gary Guo <gary@garyguo.net>
CC: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
CC: Benno Lossin <lossin@kernel.org>
CC: Andreas Hindborg <a.hindborg@kernel.org>
CC: Alice Ryhl <aliceryhl@google.com>
CC: Trevor Gross <tmgross@umich.edu>
CC: Danilo Krummrich <dakr@kernel.org>
CC: Kees Cook <kees@kernel.org>
CC: Sami Tolvanen <samitolvanen@google.com>
CC: Matthew Maurer <mmaurer@google.com>
CC: "Peter Zijlstra (Intel)" <peterz@infradead.org>
CC: linux-kernel@vger.kernel.org
CC: linux-riscv@lists.infradead.org
CC: rust-for-linux@vger.kernel.org
---
 arch/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/Kconfig b/arch/Kconfig
index d1b4ffd6e0856..880cddff5eda7 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -917,6 +917,7 @@ config HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC
 	def_bool y
 	depends on HAVE_CFI_ICALL_NORMALIZE_INTEGERS_CLANG
 	depends on RUSTC_VERSION >= 107900
+	depends on ARM64 || X86_64
 	# With GCOV/KASAN we need this fix: https://github.com/rust-lang/rust/pull/129373
 	depends on (RUSTC_LLVM_VERSION >= 190103 && RUSTC_VERSION >= 108200) || \
 		(!GCOV_KERNEL && !KASAN_GENERIC && !KASAN_SW_TAGS)
-- 
2.47.2

Re: [PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG
Posted by Kees Cook 17 hours ago
On Mon, Sep 08, 2025 at 02:12:35PM +0100, Conor Dooley wrote:
> From: Conor Dooley <conor.dooley@microchip.com>
> 
> The kernel uses the standard rustc targets for non-x86 targets, and out
> of those only 64-bit arm's target has kcfi support enabled. For x86, the
> custom 64-bit target enables kcfi.
> 
> The HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC config option that allows
> CFI_CLANG to be used in combination with RUST does not check whether the
> rustc target supports kcfi. This breaks the build on riscv (and
> presumably 32-bit arm) when CFI_CLANG and RUST are enabled at the same
> time.
> 
> Ordinarily, a rustc-option check would be used to detect target support
> but unfortunately rustc-option filters out the target for reasons given
> in commit 46e24a545cdb4 ("rust: kasan/kbuild: fix missing flags on first
> build"). As a result, if the host supports kcfi but the target does not,
> e.g. when building for riscv on x86_64, the build would remain broken.
> 
> Instead, make HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC depend on the only
> two architectures where the target used supports it to fix the build.

I'm generally fine with this, but normally we do arch-specific stuff
only in arch/$arch/Kconfig, and expose some kind of
ARCH_HAS_CFI_ICALL_NORMALIZE_INTEGERS that would get tested here. Should
we do that here too?

-Kees

> 
> CC: stable@vger.kernel.org
> Fixes: ca627e636551e ("rust: cfi: add support for CFI_CLANG with Rust")
> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
> ---
> CC: Paul Walmsley <paul.walmsley@sifive.com>
> CC: Palmer Dabbelt <palmer@dabbelt.com>
> CC: Alexandre Ghiti <alex@ghiti.fr>
> CC: Miguel Ojeda <ojeda@kernel.org>
> CC: Alex Gaynor <alex.gaynor@gmail.com>
> CC: Boqun Feng <boqun.feng@gmail.com>
> CC: Gary Guo <gary@garyguo.net>
> CC: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
> CC: Benno Lossin <lossin@kernel.org>
> CC: Andreas Hindborg <a.hindborg@kernel.org>
> CC: Alice Ryhl <aliceryhl@google.com>
> CC: Trevor Gross <tmgross@umich.edu>
> CC: Danilo Krummrich <dakr@kernel.org>
> CC: Kees Cook <kees@kernel.org>
> CC: Sami Tolvanen <samitolvanen@google.com>
> CC: Matthew Maurer <mmaurer@google.com>
> CC: "Peter Zijlstra (Intel)" <peterz@infradead.org>
> CC: linux-kernel@vger.kernel.org
> CC: linux-riscv@lists.infradead.org
> CC: rust-for-linux@vger.kernel.org
> ---
>  arch/Kconfig | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/Kconfig b/arch/Kconfig
> index d1b4ffd6e0856..880cddff5eda7 100644
> --- a/arch/Kconfig
> +++ b/arch/Kconfig
> @@ -917,6 +917,7 @@ config HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC
>  	def_bool y
>  	depends on HAVE_CFI_ICALL_NORMALIZE_INTEGERS_CLANG
>  	depends on RUSTC_VERSION >= 107900
> +	depends on ARM64 || X86_64
>  	# With GCOV/KASAN we need this fix: https://github.com/rust-lang/rust/pull/129373
>  	depends on (RUSTC_LLVM_VERSION >= 190103 && RUSTC_VERSION >= 108200) || \
>  		(!GCOV_KERNEL && !KASAN_GENERIC && !KASAN_SW_TAGS)
> -- 
> 2.47.2
> 

-- 
Kees Cook
Re: [PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG
Posted by Alice Ryhl 7 hours ago
On Mon, Sep 08, 2025 at 08:11:48PM -0700, Kees Cook wrote:
> On Mon, Sep 08, 2025 at 02:12:35PM +0100, Conor Dooley wrote:
> > From: Conor Dooley <conor.dooley@microchip.com>
> > 
> > The kernel uses the standard rustc targets for non-x86 targets, and out
> > of those only 64-bit arm's target has kcfi support enabled. For x86, the
> > custom 64-bit target enables kcfi.
> > 
> > The HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC config option that allows
> > CFI_CLANG to be used in combination with RUST does not check whether the
> > rustc target supports kcfi. This breaks the build on riscv (and
> > presumably 32-bit arm) when CFI_CLANG and RUST are enabled at the same
> > time.
> > 
> > Ordinarily, a rustc-option check would be used to detect target support
> > but unfortunately rustc-option filters out the target for reasons given
> > in commit 46e24a545cdb4 ("rust: kasan/kbuild: fix missing flags on first
> > build"). As a result, if the host supports kcfi but the target does not,
> > e.g. when building for riscv on x86_64, the build would remain broken.
> > 
> > Instead, make HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC depend on the only
> > two architectures where the target used supports it to fix the build.
> 
> I'm generally fine with this, but normally we do arch-specific stuff
> only in arch/$arch/Kconfig, and expose some kind of
> ARCH_HAS_CFI_ICALL_NORMALIZE_INTEGERS that would get tested here. Should
> we do that here too?

I'm thinking in this case it makes sense to keep this patch simple as
it's a fix. Once rustc supports cfi on riscv (which should really just
be changing the target to list it as supported), we can reorganize it to
match what you're describing at that point.

> > CC: stable@vger.kernel.org
> > Fixes: ca627e636551e ("rust: cfi: add support for CFI_CLANG with Rust")
> > Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
> > ---
> > CC: Paul Walmsley <paul.walmsley@sifive.com>
> > CC: Palmer Dabbelt <palmer@dabbelt.com>
> > CC: Alexandre Ghiti <alex@ghiti.fr>
> > CC: Miguel Ojeda <ojeda@kernel.org>
> > CC: Alex Gaynor <alex.gaynor@gmail.com>
> > CC: Boqun Feng <boqun.feng@gmail.com>
> > CC: Gary Guo <gary@garyguo.net>
> > CC: "Björn Roy Baron" <bjorn3_gh@protonmail.com>
> > CC: Benno Lossin <lossin@kernel.org>
> > CC: Andreas Hindborg <a.hindborg@kernel.org>
> > CC: Alice Ryhl <aliceryhl@google.com>
> > CC: Trevor Gross <tmgross@umich.edu>
> > CC: Danilo Krummrich <dakr@kernel.org>
> > CC: Kees Cook <kees@kernel.org>
> > CC: Sami Tolvanen <samitolvanen@google.com>
> > CC: Matthew Maurer <mmaurer@google.com>
> > CC: "Peter Zijlstra (Intel)" <peterz@infradead.org>
> > CC: linux-kernel@vger.kernel.org
> > CC: linux-riscv@lists.infradead.org
> > CC: rust-for-linux@vger.kernel.org
> > ---
> >  arch/Kconfig | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index d1b4ffd6e0856..880cddff5eda7 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -917,6 +917,7 @@ config HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC
> >  	def_bool y
> >  	depends on HAVE_CFI_ICALL_NORMALIZE_INTEGERS_CLANG
> >  	depends on RUSTC_VERSION >= 107900
> > +	depends on ARM64 || X86_64
> >  	# With GCOV/KASAN we need this fix: https://github.com/rust-lang/rust/pull/129373
> >  	depends on (RUSTC_LLVM_VERSION >= 190103 && RUSTC_VERSION >= 108200) || \
> >  		(!GCOV_KERNEL && !KASAN_GENERIC && !KASAN_SW_TAGS)
> > -- 
> > 2.47.2
> > 
> 
> -- 
> Kees Cook
Re: [PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG
Posted by Miguel Ojeda 1 day, 6 hours ago
On Mon, Sep 8, 2025 at 3:13 PM Conor Dooley <conor@kernel.org> wrote:
>
> From: Conor Dooley <conor.dooley@microchip.com>
>
> The kernel uses the standard rustc targets for non-x86 targets, and out
> of those only 64-bit arm's target has kcfi support enabled. For x86, the
> custom 64-bit target enables kcfi.
>
> The HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC config option that allows
> CFI_CLANG to be used in combination with RUST does not check whether the
> rustc target supports kcfi. This breaks the build on riscv (and
> presumably 32-bit arm) when CFI_CLANG and RUST are enabled at the same
> time.
>
> Ordinarily, a rustc-option check would be used to detect target support
> but unfortunately rustc-option filters out the target for reasons given
> in commit 46e24a545cdb4 ("rust: kasan/kbuild: fix missing flags on first
> build"). As a result, if the host supports kcfi but the target does not,
> e.g. when building for riscv on x86_64, the build would remain broken.
>
> Instead, make HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC depend on the only
> two architectures where the target used supports it to fix the build.
>
> CC: stable@vger.kernel.org
> Fixes: ca627e636551e ("rust: cfi: add support for CFI_CLANG with Rust")
> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>

If you are taking this through RISC-V:

Acked-by: Miguel Ojeda <ojeda@kernel.org>

Cheers,
Miguel
Re: [PATCH v1] rust: cfi: only 64-bit arm and x86 support CFI_CLANG
Posted by Alice Ryhl 1 day, 7 hours ago
On Mon, Sep 8, 2025 at 3:13 PM Conor Dooley <conor@kernel.org> wrote:
>
> From: Conor Dooley <conor.dooley@microchip.com>
>
> The kernel uses the standard rustc targets for non-x86 targets, and out
> of those only 64-bit arm's target has kcfi support enabled. For x86, the
> custom 64-bit target enables kcfi.
>
> The HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC config option that allows
> CFI_CLANG to be used in combination with RUST does not check whether the
> rustc target supports kcfi. This breaks the build on riscv (and
> presumably 32-bit arm) when CFI_CLANG and RUST are enabled at the same
> time.
>
> Ordinarily, a rustc-option check would be used to detect target support
> but unfortunately rustc-option filters out the target for reasons given
> in commit 46e24a545cdb4 ("rust: kasan/kbuild: fix missing flags on first
> build"). As a result, if the host supports kcfi but the target does not,
> e.g. when building for riscv on x86_64, the build would remain broken.
>
> Instead, make HAVE_CFI_ICALL_NORMALIZE_INTEGERS_RUSTC depend on the only
> two architectures where the target used supports it to fix the build.
>
> CC: stable@vger.kernel.org
> Fixes: ca627e636551e ("rust: cfi: add support for CFI_CLANG with Rust")
> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>