Commit ce4a2620 authored by Alice Ryhl's avatar Alice Ryhl Committed by Miguel Ojeda
Browse files

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.

The security impact of this flag is minimal. When Sami Tolvanen looked
into it, he found that integer normalization reduced the number of
unique type hashes in the kernel by ~1%, which is acceptable.

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 signedness, but Rust only
has one integer type of that size and signedness. When Rust calls into
C functions using such types in their signature, this results in CFI
failures. One example is 'unsigned long long' and 'unsigned long' which
are both 64-bit on LP64 targets, so on those targets this flag will give
both types the same CFI tag.

This flag changes the ABI heavily. It is not applied automatically when
CONFIG_RUST is turned on to make sure that the CONFIG_RUST option does
not change the ABI of C code. For example, some build may need to make
other changes atomically with toggling this flag. Having it be a
separate option makes it possible to first turn on normalized integer
tags, and then later turn on CONFIG_RUST.

Similarly, when turning on CONFIG_RUST in a build, you may need a few
attempts where the RUST=y commit gets reverted a few times. It is
inconvenient if reverting RUST=y also requires reverting the changes you
made to support normalized integer tags.

To avoid having this flag impact builds that don't care about this, the
next patch in this series will make CONFIG_RUST turn on this option
using `select` rather than `depends on`.

Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
Reviewed-by: default avatarSami Tolvanen <samitolvanen@google.com>
Tested-by: default avatarGatlin Newhouse <gatlin.newhouse@gmail.com>
Acked-by: default avatarKees Cook <kees@kernel.org>
Acked-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240801-kcfi-v2-1-c93caed3d121@google.com


Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
parent d077242d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -954,6 +954,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
+16 −0
Original line number Diff line number Diff line
@@ -826,6 +826,22 @@ 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.

	  The option is separate from CONFIG_RUST because it affects the ABI.
	  When working with build systems that care about the ABI, it is
	  convenient to be able to turn on this flag first, before Rust is
	  turned on.

	  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