Skip to content

Commit 709a972

Browse files
committed
security: Implement Clang's stack initialization
CONFIG_INIT_STACK_ALL turns on stack initialization based on -ftrivial-auto-var-init in Clang builds, which has greater coverage than CONFIG_GCC_PLUGINS_STRUCTLEAK_BYREF_ALL. -ftrivial-auto-var-init Clang option provides trivial initializers for uninitialized local variables, variable fields and padding. It has three possible values: pattern - uninitialized locals are filled with a fixed pattern (mostly 0xAA on 64-bit platforms, see https://reviews.llvm.org/D54604 for more details, but 0x000000AA for 32-bit pointers) likely to cause crashes when uninitialized value is used; zero (it's still debated whether this flag makes it to the official Clang release) - uninitialized locals are filled with zeroes; uninitialized (default) - uninitialized locals are left intact. This patch uses only the "pattern" mode when CONFIG_INIT_STACK_ALL is enabled. Developers have the possibility to opt-out of this feature on a per-variable basis by using __attribute__((uninitialized)), but such use should be well justified in comments. Co-developed-by: Alexander Potapenko <glider@google.com> Signed-off-by: Alexander Potapenko <glider@google.com> Signed-off-by: Kees Cook <keescook@chromium.org> Tested-by: Alexander Potapenko <glider@google.com> Acked-by: Masahiro Yamada <yamada.masahiro@socionext.com>
1 parent b6a6a37 commit 709a972

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@ KBUILD_CFLAGS += -fomit-frame-pointer
745745
endif
746746
endif
747747

748+
# Initialize all stack variables with a pattern, if desired.
749+
ifdef CONFIG_INIT_STACK_ALL
750+
KBUILD_CFLAGS += -ftrivial-auto-var-init=pattern
751+
endif
752+
748753
DEBUG_CFLAGS := $(call cc-option, -fno-var-tracking-assignments)
749754

750755
ifdef CONFIG_DEBUG_INFO

‎security/Kconfig.hardening‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ config GCC_PLUGIN_STRUCTLEAK
1818

1919
menu "Memory initialization"
2020

21+
config CC_HAS_AUTO_VAR_INIT
22+
def_bool $(cc-option,-ftrivial-auto-var-init=pattern)
23+
2124
choice
2225
prompt "Initialize kernel stack variables at function entry"
2326
default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
27+
default INIT_STACK_ALL if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT
2428
default INIT_STACK_NONE
2529
help
2630
This option enables initialization of stack variables at
@@ -76,6 +80,16 @@ choice
7680
of uninitialized stack variable exploits and information
7781
exposures.
7882

83+
config INIT_STACK_ALL
84+
bool "0xAA-init everything on the stack (strongest)"
85+
depends on CC_HAS_AUTO_VAR_INIT
86+
help
87+
Initializes everything on the stack with a 0xAA
88+
pattern. This is intended to eliminate all classes
89+
of uninitialized stack variable exploits and information
90+
exposures, even variables that were warned to have been
91+
left uninitialized.
92+
7993
endchoice
8094

8195
config GCC_PLUGIN_STRUCTLEAK_VERBOSE

0 commit comments

Comments
 (0)