From 8eaa5a937ce44dedd4ab3fda2e2aac56390e5365 Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Fri, 15 Sep 2023 00:05:06 +0000 Subject: [PATCH] rust: Enable KCFI support when available On newer clang, a flag is available which makes KCFI compatibility with rustc possible without significant contortions. Signed-off-by: Matthew Maurer --- Makefile | 15 +++++++++++++-- init/Kconfig | 1 + rust/Makefile | 2 +- scripts/generate_rust_target.rs | 1 + 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 690cde550acd3b..5e60e371a0da31 100644 --- a/Makefile +++ b/Makefile @@ -967,8 +967,19 @@ export CC_FLAGS_LTO endif ifdef CONFIG_CFI_CLANG -CC_FLAGS_CFI := -fsanitize=kcfi -KBUILD_CFLAGS += $(CC_FLAGS_CFI) +CC_FLAGS_CFI := -fsanitize=kcfi +ifdef CONFIG_RUST +# If Rust is enabled, this flag is required to support cross-language +# integer types. +# This addresses the problem that on e.g. i686, int != long, and Rust +# maps both to i32. +# See https://rcvalle.com/docs/rust-cfi-design-doc.pdf for details. +CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers +RS_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers +KBUILD_RSFLAGS += RS_FLAGS_CFI +export RS_FLAGS_CFI +endif +KBUILD_CFLAGS += $(CC_FLAGS_CFI) export CC_FLAGS_CFI endif diff --git a/init/Kconfig b/init/Kconfig index 9ffb103fc927bc..e88925e655398b 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1889,6 +1889,7 @@ config RUST depends on !GCC_PLUGINS depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE + depends on !CFI_CLANG || $(cc-option,-fsanitize=kcfi,-fsanitize-cfi-icall-experimental-normalize-integers) select CONSTRUCTORS help Enables Rust support in the kernel. diff --git a/rust/Makefile b/rust/Makefile index 543b37f6c77f12..662b61b9c57035 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -364,7 +364,7 @@ $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE quiet_cmd_exports = EXPORTS $@ cmd_exports = \ $(NM) -p --defined-only $< \ - | awk '/ (T|R|D) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ + | awk '$$2~/(T|R|D)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE $(call if_changed,exports) diff --git a/scripts/generate_rust_target.rs b/scripts/generate_rust_target.rs index e780ded38a3c83..1a35570f53b4a6 100644 --- a/scripts/generate_rust_target.rs +++ b/scripts/generate_rust_target.rs @@ -179,6 +179,7 @@ fn main() { } ts.push("features", features); ts.push("llvm-target", "x86_64-linux-gnu"); + ts.push("supported-sanitizers", ["kcfi"]); ts.push("target-pointer-width", "64"); } else { panic!("Unsupported architecture");