Message ID | 20240801-kcfi-v2b-2-c93caed3d121@google.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [v2b] rust: cfi: add support for CFI_CLANG with Rust | expand |
On Mon, Aug 19, 2024 at 9:55 AM Alice Ryhl <aliceryhl@google.com> wrote: > > +PADDING_RUSTFLAGS := -Zpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES) > +KBUILD_RUSTFLAGS += $(PADDING_RUSTFLAGS) > +export PADDING_RUSTFLAGS It is possible to have CALL_PADDING && !FINEIBT, which means one can trigger a build error if the compiler is not recent enough. Should we test for CALL_PADDING here? > + depends on !FINEIBT || RUSTC_VERSION >= 108000 Cheers, Miguel
On Mon, Sep 16, 2024 at 4:07 PM Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote: > > It is possible to have CALL_PADDING && !FINEIBT, which means one can > trigger a build error if the compiler is not recent enough. Should we > test for CALL_PADDING here? Alice confirmed offline that she agrees, so I just changed the requirement in `rust-next`. [ Replaced `!FINEIBT` requirement with `!CALL_PADDING` to prevent a build error on older Rust compilers. Fixed typo. - Miguel ] Cheers, Miguel
On Mon, Sep 16, 2024 at 6:14 PM Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote: > > [ Replaced `!FINEIBT` requirement with `!CALL_PADDING` to prevent > a build error on older Rust compilers. Fixed typo. - Miguel ] I also noticed we need Rust 1.81.0 for the flag rather than 1.80.0 -- I will send a patch later and/or rebase. Cheers, Miguel
diff --git a/Makefile b/Makefile index 484c6900337e..2dc39a23005d 100644 --- a/Makefile +++ b/Makefile @@ -955,6 +955,13 @@ CC_FLAGS_CFI := -fsanitize=kcfi ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS CC_FLAGS_CFI += -fsanitize-cfi-icall-experimental-normalize-integers endif +ifdef CONFIG_RUST + # Always pass -Zsanitizer-cfi-normalize-integers as CONFIG_RUST selects + # CONFIG_CFI_ICALL_NORMALIZE_INTEGERS. + RUSTC_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers + KBUILD_RUSTFLAGS += $(RUSTC_FLAGS_CFI) + export RUSTC_FLAGS_CFI +endif KBUILD_CFLAGS += $(CC_FLAGS_CFI) export CC_FLAGS_CFI endif diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 801fd85c3ef6..e9b2ee3c8a71 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -237,6 +237,10 @@ ifdef CONFIG_CALL_PADDING PADDING_CFLAGS := -fpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES) KBUILD_CFLAGS += $(PADDING_CFLAGS) export PADDING_CFLAGS + +PADDING_RUSTFLAGS := -Zpatchable-function-entry=$(CONFIG_FUNCTION_PADDING_BYTES),$(CONFIG_FUNCTION_PADDING_BYTES) +KBUILD_RUSTFLAGS += $(PADDING_RUSTFLAGS) +export PADDING_RUSTFLAGS endif KBUILD_LDFLAGS += -m elf_$(UTS_MACHINE) diff --git a/init/Kconfig b/init/Kconfig index b0238c4b6e79..306af56a22df 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1905,11 +1905,13 @@ config RUST bool "Rust support" depends on HAVE_RUST depends on RUST_IS_AVAILABLE - depends on !CFI_CLANG depends on !MODVERSIONS depends on !GCC_PLUGINS depends on !RANDSTRUCT depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE + depends on !CFI_CLANG || RUSTC_VERSION >= 107900 && $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers) + select CFI_ICALL_NORMALIZE_INTEGERS if CFI_CLANG + depends on !FINEIBT || RUSTC_VERSION >= 108000 help Enables Rust support in the kernel. diff --git a/rust/Makefile b/rust/Makefile index 26b16c036fe3..53a17d22f5cd 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -305,7 +305,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|B) / {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@ + | awk '$$2~/(T|R|D|B)/ && $$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 c31657380bf9..9b184099278a 100644 --- a/scripts/generate_rust_target.rs +++ b/scripts/generate_rust_target.rs @@ -192,6 +192,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 if cfg.has("X86_32") { // This only works on UML, as i386 otherwise needs regparm support in rustc