From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 4/5] hardening: support register zeroing on function exit
Date: Mon, 2 Dec 2024 09:18:14 +0100 [thread overview]
Message-ID: <20241202081815.3475994-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20241202081815.3475994-1-a.fatoum@pengutronix.de>
To prevent information exposure, let's add a config option to zero
registers on function exit.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Makefile | 3 +++
lib/Kconfig.hardening | 22 ++++++++++++++++++++++
2 files changed, 25 insertions(+)
diff --git a/Makefile b/Makefile
index bbb5d9409ea8..bc75c6335177 100644
--- a/Makefile
+++ b/Makefile
@@ -705,6 +705,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 84aaab1693e0..6457d24c7382 100644
--- a/lib/Kconfig.hardening
+++ b/lib/Kconfig.hardening
@@ -107,6 +107,28 @@ config INIT_ON_FREE_DEFAULT_ON
wiped immediately, making live forensics or cold boot memory
attacks unable to recover freed memory contents.
+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
next prev parent reply other threads:[~2024-12-02 8:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-02 8:18 [PATCH v2 0/5] malloc: add options to zero-initialize buffers Ahmad Fatoum
2024-12-02 8:18 ` [PATCH v2 1/5] dlmalloc: add aliases with dl as prefix Ahmad Fatoum
2024-12-02 8:18 ` [PATCH v2 2/5] hardening: support zeroing all malloc buffers by default Ahmad Fatoum
2024-12-02 8:18 ` [PATCH v2 3/5] hardening: support initializing stack variables " Ahmad Fatoum
2024-12-02 8:18 ` Ahmad Fatoum [this message]
2024-12-02 8:18 ` [PATCH v2 5/5] tlsf: panic in asserts if CONFIG_BUG_ON_DATA_CORRUPTION=y Ahmad Fatoum
2024-12-02 11:09 ` [PATCH v2 0/5] malloc: add options to zero-initialize buffers Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241202081815.3475994-5-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox