[PATCH 4/5] kbuild: support register zeroing on function exit
Ahmad Fatoum
a.fatoum at pengutronix.de
Mon Nov 25 07:20:23 PST 2024
To prevent information exposure, let's add a config option to zero
registers on function exit.
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
Makefile | 3 +++
lib/Kconfig.hardening | 22 ++++++++++++++++++++++
2 files changed, 25 insertions(+)
diff --git a/Makefile b/Makefile
index a1ee82692564..aa66d0cf3b38 100644
--- a/Makefile
+++ b/Makefile
@@ -703,6 +703,9 @@ KBUILD_CFLAGS += $(CC_AUTO_VAR_INIT_ZERO_ENABLER)
endif
endif
+# Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
+KBUILD_CFLAGS-$(CONFIG_ZERO_CALL_USED_REGS) += -fzero-call-used-regs=used-gpr
+
KBUILD_CFLAGS-$(CONFIG_WERROR) += -Werror
# This warning generated too much noise in a regular build.
diff --git a/lib/Kconfig.hardening b/lib/Kconfig.hardening
index 8e392d4e8e60..34f53b758047 100644
--- a/lib/Kconfig.hardening
+++ b/lib/Kconfig.hardening
@@ -119,6 +119,28 @@ config INIT_ON_FREE_DEFAULT_ON
touching "cold" memory areas. Most cases see 3-5% impact. Some
synthetic workloads have measured as high as 8%.
+config CC_HAS_ZERO_CALL_USED_REGS
+ def_bool $(cc-option,-fzero-call-used-regs=used-gpr)
+ # https://github.com/ClangBuiltLinux/linux/issues/1766
+ # https://github.com/llvm/llvm-project/issues/59242
+ depends on !CC_IS_CLANG || CLANG_VERSION > 150006
+
+config ZERO_CALL_USED_REGS
+ bool "Enable register zeroing on function exit"
+ depends on CC_HAS_ZERO_CALL_USED_REGS
+ help
+ At the end of functions, always zero any caller-used register
+ contents. This helps ensure that temporary values are not
+ leaked beyond the function boundary. This means that register
+ contents are less likely to be available for side channels
+ and information exposures. Additionally, this helps reduce the
+ number of useful ROP gadgets by about 20% (and removes compiler
+ generated "write-what-where" gadgets) in the resulting kernel
+ image. This has a less than 1% performance impact on most
+ workloads. Image size growth depends on architecture, and should
+ be evaluated for suitability. For example, x86_64 grows by less
+ than 1%, and arm64 grows by about 5%.
+
endmenu
config STACK_GUARD_PAGE
--
2.39.5
More information about the barebox
mailing list