Skip to contentSkip to conversation

Rust KCFI support

PATCH Superseded Alice Ryhl 5 participants 20 messages Jul 30, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alice Ryhl

author reviewer
#0 lore raw

cover Rust KCFI support

The control flow integrity (kCFI) sanitizer is an important sanitizer that is often used in production. This patch series makes it possible to use kCFI and Rust together.

The second patch in this series depends on the next version of [1], which Miguel will send soon. It also depends on [2].

---
Alice Ryhl (1):
cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Matthew Maurer (1):
rust: cfi: add support for CFI_CLANG with Rust

 Makefile                        | 10 ++++++++++
 arch/Kconfig                    | 11 +++++++++++
 init/Kconfig                    |  2 +-
 rust/Makefile                   |  2 +-
 scripts/generate_rust_target.rs |  1 +
 5 files changed, 24 insertions(+), 2 deletions(-)

---
base-commit: 8718bc07faa6ddf4f7335a12e4cdd4ffc796bbd8
change-id: 20240725-kcfi-c592898e2bfb

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author reviewer
#1 lore raw

PATCH 1/2 cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

---
 Makefile     |  3 +++
 arch/Kconfig | 11 +++++++++++
 2 files changed, 14 insertions(+)
Makefile +3 -0
index 2b5f9f098b6f..484c6900337e 100644
@@ -952,6 +952,9 @@ endif
 
 ifdef CONFIG_CFI_CLANG
 CC_FLAGS_CFI	:= -fsanitize=kcfi
+ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS
+	CC_FLAGS_CFI	+= -fsanitize-cfi-icall-experimental-normalize-integers
+endif
 KBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
 export CC_FLAGS_CFI
 endif
arch/Kconfig +11 -0
index 975dd22a2dbd..f6ecb15cb8ba 100644
@@ -826,6 +826,17 @@ config CFI_CLANG
 
 	    https://clang.llvm.org/docs/ControlFlowIntegrity.html
 
+config CFI_ICALL_NORMALIZE_INTEGERS
+	bool "Normalize CFI tags for integers"
+	depends on CFI_CLANG
+	depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
+	help
+	  This option normalizes the CFI tags for integer types so that all
+	  integer types of the same size and signedness receive the same CFI
+	  tag.
+
+	  This option is necessary for using CFI with Rust. If unsure, say N.
+
 config CFI_PERMISSIVE
 	bool "Use CFI in permissive mode"
 	depends on CFI_CLANG
Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
author reviewer
#2 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Quoted 6 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

Quoted 6 lines

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

This should say signedness where it says alignment.

Alice

#3 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

In reply to Alice Ryhl
Quoted 12 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

To be clear, any code compiled with this is incompatible with code compiled without this, as the function signatures will differ, right?

Specifically, it will map things like 'unsigned long long' and 'unsigned long' -- which are both u64 on LP64 targets to the same 'type', right?

I suppose it has been decided the security impact of this change is minimal?

All in all, there is very little actual information provided here.

Quoted 46 lines
---
 Makefile     |  3 +++
 arch/Kconfig | 11 +++++++++++
 2 files changed, 14 insertions(+)
Makefile +3 -0
index 2b5f9f098b6f..484c6900337e 100644
@@ -952,6 +952,9 @@ endif
 
 ifdef CONFIG_CFI_CLANG
 CC_FLAGS_CFI	:= -fsanitize=kcfi
+ifdef CONFIG_CFI_ICALL_NORMALIZE_INTEGERS
+	CC_FLAGS_CFI	+= -fsanitize-cfi-icall-experimental-normalize-integers
+endif
 KBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
 export CC_FLAGS_CFI
 endif
arch/Kconfig +11 -0
index 975dd22a2dbd..f6ecb15cb8ba 100644
@@ -826,6 +826,17 @@ config CFI_CLANG
 
 	    https://clang.llvm.org/docs/ControlFlowIntegrity.html
 
+config CFI_ICALL_NORMALIZE_INTEGERS
+	bool "Normalize CFI tags for integers"
+	depends on CFI_CLANG
+	depends on $(cc-option,-fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers)
+	help
+	  This option normalizes the CFI tags for integer types so that all
+	  integer types of the same size and signedness receive the same CFI
+	  tag.
+
+	  This option is necessary for using CFI with Rust. If unsure, say N.
+
 config CFI_PERMISSIVE
 	bool "Use CFI in permissive mode"
 	depends on CFI_CLANG
Signature · 2 lines
-- 
2.46.0.rc1.232.g9752f9e123-goog
#4 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Quoted 17 lines · Peter Zijlstra
Quoted 12 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

Quoted 8 lines

To be clear, any code compiled with this is incompatible with code compiled without this, as the function signatures will differ, right?

Specifically, it will map things like 'unsigned long long' and 'unsigned long' -- which are both u64 on LP64 targets to the same 'type', right?

I suppose it has been decided the security impact of this change is minimal?

I looked into this last year, and integer normalization reduced the number of unique type hashes in the kernel by ~1%, which should be fine.

Sami

#5 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Quoted 24 lines · Sami Tolvanen
Quoted 23 lines · Peter Zijlstra
Quoted 12 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

To be clear, any code compiled with this is incompatible with code compiled without this, as the function signatures will differ, right?

Specifically, it will map things like 'unsigned long long' and 'unsigned long' -- which are both u64 on LP64 targets to the same 'type', right?

Quoted 6 lines
Quoted 2 lines

I suppose it has been decided the security impact of this change is minimal?

I looked into this last year, and integer normalization reduced the number of unique type hashes in the kernel by ~1%, which should be fine.

Right, I didn't actually expect it to matter much either, but it would've made good Changelog content.

author reviewer
#6 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

In reply to Peter Zijlstra
Quoted 14 lines · Peter Zijlstra
Quoted 12 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 5 lines
Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

To be clear, any code compiled with this is incompatible with code compiled without this, as the function signatures will differ, right?

That's correct.

Quoted 2 lines

Specifically, it will map things like 'unsigned long long' and 'unsigned long' -- which are both u64 on LP64 targets to the same 'type', right?

That's correct.

Quoted 2 lines

I suppose it has been decided the security impact of this change is minimal?

Yeah, see Sami's response.

Quoted 1 line

All in all, there is very little actual information provided here.

I'll make sure to include this info in the commit message of v2.

Alice

#7 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

In reply to Alice Ryhl
Quoted 13 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

Is there any case where we would want CFI_ICALL_NORMALIZE_INTEGERS when Rust is not enabled, then? If not, is the idea here to make this an explicit extra question in the config before enabling Rust? Or why wouldn't it be done automatically?

Thanks!

Cheers,
Miguel

#8 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Quoted 14 lines · Miguel Ojeda
Quoted 13 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 7 lines
Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

Is there any case where we would want CFI_ICALL_NORMALIZE_INTEGERS when Rust is not enabled, then? If not, is the idea here to make this an explicit extra question in the config before enabling Rust? Or why wouldn't it be done automatically?

I suspect CFI_ICALL_NORMALIZE_INTEGERS breaks ABI, then again, Linux doesn't promise or preserve ABI except for the SYSCALL layer. So yeah, meh.

author reviewer
#9 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

In reply to Miguel Ojeda
Quoted 15 lines · Miguel Ojeda
Quoted 13 lines · Alice Ryhl

Introduce a Kconfig option for enabling the experimental option to normalize integer types. This ensures that integer types of the same size and signedness are considered compatible by the Control Flow Integrity sanitizer.

This option exists for compatibility with Rust, as C and Rust do not have the same set of integer types. There are cases where C has two different integer types of the same size and alignment, but Rust only has one integer type of that size and alignment. When Rust calls into C functions using such types in their signature, this results in CFI failures.

Quoted 7 lines
Quoted 2 lines

This patch introduces a dedicated option for this because it is undesirable to have CONFIG_RUST affect CC_FLAGS in this way.

Is there any case where we would want CFI_ICALL_NORMALIZE_INTEGERS when Rust is not enabled, then? If not, is the idea here to make this an explicit extra question in the config before enabling Rust? Or why wouldn't it be done automatically?

I'm adding this flag to make the bringup process for RUST easier.

I'm working on enabling RUST in a new branch. We're eventually going to have both RUST and CFI_ICALL_NORMALIZE_INTEGERS enabled in our build, but the path to getting there is complex and we would like to turn on CFI_ICALL_NORMALIZE_INTEGERS first, and then turn on RUST later. Both options are non-trivial to turn on and I want to disentangle them.

Alice

#10 lore raw

Re: [PATCH 1/2] cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Quoted 9 lines · Alice Ryhl

I'm adding this flag to make the bringup process for RUST easier.

I'm working on enabling RUST in a new branch. We're eventually going to have both RUST and CFI_ICALL_NORMALIZE_INTEGERS enabled in our build, but the path to getting there is complex and we would like to turn on CFI_ICALL_NORMALIZE_INTEGERS first, and then turn on RUST later. Both options are non-trivial to turn on and I want to disentangle them.

Would it be useful for other users/distros to do that two-stage approach as well?

In other words, if the intended end state is that everybody should enable this if Rust is enabled, and nobody should enable it if they don't care about Rust, then we should add this only if you think others will also need to do this step by step. The option or commit message could ideally explain more about this need/use case.

Cheers,
Miguel

author reviewer
#11 lore raw

PATCH 2/2 rust: cfi: add support for CFI_CLANG with Rust

In reply to Alice Ryhl

From: Matthew Maurer <[email protected]>

Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI.

---
 Makefile                        | 7 +++++++
 init/Kconfig                    | 2 +-
 rust/Makefile                   | 2 +-
 scripts/generate_rust_target.rs | 1 +
 4 files changed, 10 insertions(+), 2 deletions(-)
Makefile +7 -0
index 484c6900337e..8d7d52f57c63 100644
@@ -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 depends
+	# on CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
+	RS_FLAGS_CFI   := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
+	KBUILD_RUSTFLAGS += $(RS_FLAGS_CFI)
+	export RS_FLAGS_CFI
+endif
 KBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
 export CC_FLAGS_CFI
 endif
init/Kconfig +1 -1
index b0238c4b6e79..d0d3442d1756 100644
@@ -1905,11 +1905,11 @@ 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 && CFI_ICALL_NORMALIZE_INTEGERS
 	help
 	  Enables Rust support in the kernel.
 
rust/Makefile +1 -1
index f6b9bb946609..a2c9a3e03a23 100644
@@ -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) / {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)
scripts/generate_rust_target.rs +1 -0
index c31657380bf9..9b184099278a 100644
@@ -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
Signature · 1 line
2.46.0.rc1.232.g9752f9e123-goog
#12 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 6 lines · Alice Ryhl

From: Matthew Maurer <[email protected]>

Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI.

I am assuming -- because I have to, because you're not actually saying anyting -- that this is fully compatible with the C version and all the fun and games we play with rewriting the function prologue for FineIBT and the like also work?

Quoted 74 lines
---
 Makefile                        | 7 +++++++
 init/Kconfig                    | 2 +-
 rust/Makefile                   | 2 +-
 scripts/generate_rust_target.rs | 1 +
 4 files changed, 10 insertions(+), 2 deletions(-)
Makefile +7 -0
index 484c6900337e..8d7d52f57c63 100644
@@ -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 depends
+	# on CONFIG_CFI_ICALL_NORMALIZE_INTEGERS.
+	RS_FLAGS_CFI   := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers
+	KBUILD_RUSTFLAGS += $(RS_FLAGS_CFI)
+	export RS_FLAGS_CFI
+endif
 KBUILD_CFLAGS	+= $(CC_FLAGS_CFI)
 export CC_FLAGS_CFI
 endif
init/Kconfig +1 -1
index b0238c4b6e79..d0d3442d1756 100644
@@ -1905,11 +1905,11 @@ 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 && CFI_ICALL_NORMALIZE_INTEGERS
 	help
 	  Enables Rust support in the kernel.
 
rust/Makefile +1 -1
index f6b9bb946609..a2c9a3e03a23 100644
@@ -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) / {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)
scripts/generate_rust_target.rs +1 -0
index c31657380bf9..9b184099278a 100644
@@ -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
Signature · 2 lines
-- 
2.46.0.rc1.232.g9752f9e123-goog
#13 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Alice Ryhl

From: Matthew Maurer <[email protected]>

Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI.

Quoted 4 lines

I am assuming -- because I have to, because you're not actually saying anyting -- that this is fully compatible with the C version and all the fun and games we play with rewriting the function prologue for FineIBT and the like also work?

Rust uses the same LLVM backend for the actual code generation, so it should be fully compatible.

Sami

#14 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 10 lines · Sami Tolvanen
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Alice Ryhl

From: Matthew Maurer <[email protected]>

Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI.

Quoted 7 lines
Quoted 4 lines

I am assuming -- because I have to, because you're not actually saying anyting -- that this is fully compatible with the C version and all the fun and games we play with rewriting the function prologue for FineIBT and the like also work?

Rust uses the same LLVM backend for the actual code generation, so it should be fully compatible.

Yes, but we also combine that with -fpatchable-function-entry= for a very specific effect, and I don't think I see the Rust thingy do that.

author reviewer
#15 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 17 lines · Peter Zijlstra
Quoted 15 lines · Sami Tolvanen
Quoted 13 lines · Peter Zijlstra
Quoted 6 lines · Alice Ryhl

From: Matthew Maurer <[email protected]>

Make it possible to use the Control Flow Integrity (CFI) sanitizer when Rust is enabled. Enabling CFI with Rust requires that CFI is configured to normalize integer types so that all integer types of the same size and signedness are compatible under CFI.

I am assuming -- because I have to, because you're not actually saying anyting -- that this is fully compatible with the C version and all the fun and games we play with rewriting the function prologue for FineIBT and the like also work?

Quoted 5 lines
Quoted 2 lines

Rust uses the same LLVM backend for the actual code generation, so it should be fully compatible.

Yes, but we also combine that with -fpatchable-function-entry= for a very specific effect, and I don't think I see the Rust thingy do that.

Oh, you're right. I missed this because we're not using FineIBT, and when this patch was originally written we had not yet implemented a Rust equivalent to -fpatchable-function-entry=. However, that flag is now available as of rustc version 1.80.0, so it shouldn't be an issue. I'll fix this for v2 of this series.

Thanks for catching this!

As for whether it works other than that, I've been using this on my personal phone together with Rust Binder for several months, so it's well tested on arm64.

Alice

#16 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

In reply to Alice Ryhl
Quoted 2 lines · Alice Ryhl

+ RS_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers

Before I forget: this should probably be RUSTC_... for consistency with the rest (and, in this case, these are flags, so it makes sense they target the particular compiler).

Cheers,
Miguel

author reviewer
#17 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 8 lines · Miguel Ojeda
Quoted 2 lines · Alice Ryhl

+ RS_FLAGS_CFI := -Zsanitizer=kcfi -Zsanitizer-cfi-normalize-integers

Before I forget: this should probably be RUSTC_... for consistency with the rest (and, in this case, these are flags, so it makes sense they target the particular compiler).

Hmm. It seems like the existing variables containing rustc flags just use RUST not RUSTC in the name?

Alice

#18 lore raw

Re: [PATCH 2/2] rust: cfi: add support for CFI_CLANG with Rust

Quoted 3 lines · Alice Ryhl

Hmm. It seems like the existing variables containing rustc flags just use RUST not RUSTC in the name?

If you mean things like RUSTFLAGS, it was because the C side uses CFLAGS, but for the equivalent of CC_FLAGS or CLANG_FLAGS, I would say it should be RUSTC_FLAGS.

Cheers,
Miguel

reviewer Reviewed-by on the v1 series
#19 lore raw
In reply to Alice Ryhl
Quoted 10 lines · On Tue, 30 Jul 2024 09:40:10 +0000 Alice Ryhl <[email protected]> wrote:

The control flow integrity (kCFI) sanitizer is an important sanitizer that is often used in production. This patch series makes it possible to use kCFI and Rust together.

The second patch in this series depends on the next version of [1], which Miguel will send soon. It also depends on [2].

Both patches:

Quoted 18 lines

---
Alice Ryhl (1):
cfi: add CONFIG_CFI_ICALL_NORMALIZE_INTEGERS

Matthew Maurer (1):
rust: cfi: add support for CFI_CLANG with Rust

 Makefile                        | 10 ++++++++++
 arch/Kconfig                    | 11 +++++++++++
 init/Kconfig                    |  2 +-
 rust/Makefile                   |  2 +-
 scripts/generate_rust_target.rs |  1 +
 5 files changed, 24 insertions(+), 2 deletions(-)

---
base-commit: 8718bc07faa6ddf4f7335a12e4cdd4ffc796bbd8
change-id: 20240725-kcfi-c592898e2bfb

Best regards,