Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hardening: Avoid harmless Clang option under CONFIG_INIT_STACK_ALL_ZERO

Currently under Clang, CC_HAS_AUTO_VAR_INIT_ZERO requires an extra
-enable flag compared to CC_HAS_AUTO_VAR_INIT_PATTERN. GCC 12[1] will
not, and will happily ignore the Clang-specific flag. However, its
presence on the command-line is both cumbersome and confusing. Due to
GCC's tolerant behavior, though, we can continue to use a single Kconfig
cc-option test for the feature on both compilers, but then drop the
Clang-specific option in the Makefile.

In other words, this patch does not change anything other than making the
compiler command line shorter once GCC supports -ftrivial-auto-var-init=zero.

[1] https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=a25e0b5e6ac8a77a71c229e0a7b744603365b0e9

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: llvm@lists.linux.dev
Fixes: dcb7c0b9461c ("hardening: Clarify Kconfig text for auto-var-init")
Suggested-by: Will Deacon <will@kernel.org>
Link: https://lore.kernel.org/lkml/20210914102837.6172-1-will@kernel.org/
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Acked-by: Will Deacon <will@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>

+7 -4
+3 -3
Makefile
··· 831 831 832 832 # Initialize all stack variables with a zero value. 833 833 ifdef CONFIG_INIT_STACK_ALL_ZERO 834 - # Future support for zero initialization is still being debated, see 835 - # https://bugs.llvm.org/show_bug.cgi?id=45497. These flags are subject to being 836 - # renamed or dropped. 837 834 KBUILD_CFLAGS += -ftrivial-auto-var-init=zero 835 + ifdef CONFIG_CC_IS_CLANG 836 + # https://bugs.llvm.org/show_bug.cgi?id=45497 838 837 KBUILD_CFLAGS += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 838 + endif 839 839 endif 840 840 841 841 # While VLAs have been removed, GCC produces unreachable stack probes
+4 -1
security/Kconfig.hardening
··· 23 23 def_bool $(cc-option,-ftrivial-auto-var-init=pattern) 24 24 25 25 config CC_HAS_AUTO_VAR_INIT_ZERO 26 + # GCC ignores the -enable flag, so we can test for the feature with 27 + # a single invocation using the flag, but drop it as appropriate in 28 + # the Makefile, depending on the presence of Clang. 26 29 def_bool $(cc-option,-ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang) 27 30 28 31 choice 29 32 prompt "Initialize kernel stack variables at function entry" 30 33 default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS 31 34 default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN 32 - default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_PATTERN 35 + default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_ZERO 33 36 default INIT_STACK_NONE 34 37 help 35 38 This option enables initialization of stack variables at