Skip to content

Commit 5b5d3be

Browse files
committed
Merge tag 'var-init-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull automatic variable initialization updates from Kees Cook: "This adds the "zero" init option from Clang, which is being used widely in production builds of Android and Chrome OS (though it also keeps the "pattern" init, which is better for debug builds). - Introduce CONFIG_INIT_STACK_ALL_ZERO (Alexander Potapenko)" * tag 'var-init-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: security: allow using Clang's zero initialization for stack variables
2 parents 3e4a12a + f0fe00d commit 5b5d3be

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,20 @@ KBUILD_CFLAGS += -fomit-frame-pointer
803803
endif
804804
endif
805805

806-
# Initialize all stack variables with a pattern, if desired.
807-
ifdef CONFIG_INIT_STACK_ALL
806+
# Initialize all stack variables with a 0xAA pattern.
807+
ifdef CONFIG_INIT_STACK_ALL_PATTERN
808808
KBUILD_CFLAGS += -ftrivial-auto-var-init=pattern
809809
endif
810810

811+
# Initialize all stack variables with a zero value.
812+
ifdef CONFIG_INIT_STACK_ALL_ZERO
813+
# Future support for zero initialization is still being debated, see
814+
# https://bugs.llvm.org/show_bug.cgi?id=45497. These flags are subject to being
815+
# renamed or dropped.
816+
KBUILD_CFLAGS += -ftrivial-auto-var-init=zero
817+
KBUILD_CFLAGS += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
818+
endif
819+
811820
DEBUG_CFLAGS := $(call cc-option, -fno-var-tracking-assignments)
812821

813822
ifdef CONFIG_DEBUG_INFO

init/main.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,16 @@ static void __init report_meminit(void)
779779
{
780780
const char *stack;
781781

782-
if (IS_ENABLED(CONFIG_INIT_STACK_ALL))
783-
stack = "all";
782+
if (IS_ENABLED(CONFIG_INIT_STACK_ALL_PATTERN))
783+
stack = "all(pattern)";
784+
else if (IS_ENABLED(CONFIG_INIT_STACK_ALL_ZERO))
785+
stack = "all(zero)";
784786
else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL))
785-
stack = "byref_all";
787+
stack = "byref_all(zero)";
786788
else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF))
787-
stack = "byref";
789+
stack = "byref(zero)";
788790
else if (IS_ENABLED(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER))
789-
stack = "__user";
791+
stack = "__user(zero)";
790792
else
791793
stack = "off";
792794

security/Kconfig.hardening

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ config GCC_PLUGIN_STRUCTLEAK
1919

2020
menu "Memory initialization"
2121

22-
config CC_HAS_AUTO_VAR_INIT
22+
config CC_HAS_AUTO_VAR_INIT_PATTERN
2323
def_bool $(cc-option,-ftrivial-auto-var-init=pattern)
2424

25+
config CC_HAS_AUTO_VAR_INIT_ZERO
26+
def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang)
27+
2528
choice
2629
prompt "Initialize kernel stack variables at function entry"
2730
default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
28-
default INIT_STACK_ALL if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT
31+
default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
2932
default INIT_STACK_NONE
3033
help
3134
This option enables initialization of stack variables at
@@ -88,16 +91,34 @@ choice
8891
of uninitialized stack variable exploits and information
8992
exposures.
9093

91-
config INIT_STACK_ALL
94+
config INIT_STACK_ALL_PATTERN
9295
bool "0xAA-init everything on the stack (strongest)"
93-
depends on CC_HAS_AUTO_VAR_INIT
96+
depends on CC_HAS_AUTO_VAR_INIT_PATTERN
9497
help
9598
Initializes everything on the stack with a 0xAA
9699
pattern. This is intended to eliminate all classes
97100
of uninitialized stack variable exploits and information
98101
exposures, even variables that were warned to have been
99102
left uninitialized.
100103

104+
Pattern initialization is known to provoke many existing bugs
105+
related to uninitialized locals, e.g. pointers receive
106+
non-NULL values, buffer sizes and indices are very big.
107+
108+
config INIT_STACK_ALL_ZERO
109+
bool "zero-init everything on the stack (strongest and safest)"
110+
depends on CC_HAS_AUTO_VAR_INIT_ZERO
111+
help
112+
Initializes everything on the stack with a zero
113+
value. This is intended to eliminate all classes
114+
of uninitialized stack variable exploits and information
115+
exposures, even variables that were warned to have been
116+
left uninitialized.
117+
118+
Zero initialization provides safe defaults for strings,
119+
pointers, indices and sizes, and is therefore
120+
more suitable as a security mitigation measure.
121+
101122
endchoice
102123

103124
config GCC_PLUGIN_STRUCTLEAK_VERBOSE

0 commit comments

Comments
 (0)