Skip to content

Commit

Permalink
rust: Enable KCFI support when available
Browse files Browse the repository at this point in the history
On newer clang, a flag is available which makes KCFI compatibility with
rustc possible without significant contortions.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
  • Loading branch information
maurer committed Oct 30, 2023
1 parent 49654b3 commit 8eaa5a9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Makefile
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions init/Kconfig
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion rust/Makefile
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions scripts/generate_rust_target.rs
Expand Up @@ -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");
Expand Down

0 comments on commit 8eaa5a9

Please sign in to comment.