summaryrefslogtreecommitdiffstats
path: root/Makefile
diff options
context:
space:
mode:
authorKees Cook <keescook@chromium.org>2018-02-06 18:37:45 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-02-06 21:32:44 -0500
commit44c6dc940b190cf22b044a784f3e00a7e7f08b2f (patch)
tree6aa4149ad96786c80fa9d7b046d26a373d911093 /Makefile
parent2bc2f688fdf8808de4f36be563ccdb0bde7c0c54 (diff)
Makefile: introduce CONFIG_CC_STACKPROTECTOR_AUTO
Nearly all modern compilers support a stack-protector option, and nearly all modern distributions enable the kernel stack-protector, so enabling this by default in kernel builds would make sense. However, Kconfig does not have knowledge of available compiler features, so it isn't safe to force on, as this would unconditionally break builds for the compilers or architectures that don't have support. Instead, this introduces a new option, CONFIG_CC_STACKPROTECTOR_AUTO, which attempts to discover the best possible stack-protector available, and will allow builds to proceed even if the compiler doesn't support any stack-protector. This option is made the default so that kernels built with modern compilers will be protected-by-default against stack buffer overflows, avoiding things like the recent BlueBorne attack. Selection of a specific stack-protector option remains available, including disabling it. Additionally, tiny.config is adjusted to use CC_STACKPROTECTOR_NONE, since that's the option with the least code size (and it used to be the default, so we have to explicitly choose it there now). Link: http://lkml.kernel.org/r/1510076320-69931-4-git-send-email-keescook@chromium.org Signed-off-by: Kees Cook <keescook@chromium.org> Tested-by: Laura Abbott <labbott@redhat.com> Cc: Masahiro Yamada <yamada.masahiro@socionext.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Josh Triplett <josh@joshtriplett.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile37
1 files changed, 34 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index f0f934097f64..d192dd826cce 100644
--- a/Makefile
+++ b/Makefile
@@ -680,6 +680,10 @@ endif
680# This selects the stack protector compiler flag. Testing it is delayed 680# This selects the stack protector compiler flag. Testing it is delayed
681# until after .config has been reprocessed, in the prepare-compiler-check 681# until after .config has been reprocessed, in the prepare-compiler-check
682# target. 682# target.
683ifdef CONFIG_CC_STACKPROTECTOR_AUTO
684 stackp-flag := $(call cc-option,-fstack-protector-strong,$(call cc-option,-fstack-protector))
685 stackp-name := AUTO
686else
683ifdef CONFIG_CC_STACKPROTECTOR_REGULAR 687ifdef CONFIG_CC_STACKPROTECTOR_REGULAR
684 stackp-flag := -fstack-protector 688 stackp-flag := -fstack-protector
685 stackp-name := REGULAR 689 stackp-name := REGULAR
@@ -688,12 +692,18 @@ ifdef CONFIG_CC_STACKPROTECTOR_STRONG
688 stackp-flag := -fstack-protector-strong 692 stackp-flag := -fstack-protector-strong
689 stackp-name := STRONG 693 stackp-name := STRONG
690else 694else
695 # If either there is no stack protector for this architecture or
696 # CONFIG_CC_STACKPROTECTOR_NONE is selected, we're done, and $(stackp-name)
697 # is empty, skipping all remaining stack protector tests.
698 #
691 # Force off for distro compilers that enable stack protector by default. 699 # Force off for distro compilers that enable stack protector by default.
692 stackp-flag := $(call cc-option, -fno-stack-protector) 700 KBUILD_CFLAGS += $(call cc-option, -fno-stack-protector)
701endif
693endif 702endif
694endif 703endif
695# Find arch-specific stack protector compiler sanity-checking script. 704# Find arch-specific stack protector compiler sanity-checking script.
696ifdef stackp-name 705ifdef stackp-name
706ifneq ($(stackp-flag),)
697 stackp-path := $(srctree)/scripts/gcc-$(SRCARCH)_$(BITS)-has-stack-protector.sh 707 stackp-path := $(srctree)/scripts/gcc-$(SRCARCH)_$(BITS)-has-stack-protector.sh
698 stackp-check := $(wildcard $(stackp-path)) 708 stackp-check := $(wildcard $(stackp-path))
699 # If the wildcard test matches a test script, run it to check functionality. 709 # If the wildcard test matches a test script, run it to check functionality.
@@ -705,9 +715,17 @@ ifdef stackp-name
705 ifndef stackp-broken 715 ifndef stackp-broken
706 # If the stack protector is functional, enable code that depends on it. 716 # If the stack protector is functional, enable code that depends on it.
707 KBUILD_CPPFLAGS += -DCONFIG_CC_STACKPROTECTOR 717 KBUILD_CPPFLAGS += -DCONFIG_CC_STACKPROTECTOR
718 # Either we've already detected the flag (for AUTO) or we'll fail the
719 # build in the prepare-compiler-check rule (for specific flag).
720 KBUILD_CFLAGS += $(stackp-flag)
721 else
722 # We have to make sure stack protector is unconditionally disabled if
723 # the compiler is broken (in case we're going to continue the build in
724 # AUTO mode).
725 KBUILD_CFLAGS += $(call cc-option, -fno-stack-protector)
708 endif 726 endif
709endif 727endif
710KBUILD_CFLAGS += $(stackp-flag) 728endif
711 729
712ifeq ($(cc-name),clang) 730ifeq ($(cc-name),clang)
713KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,) 731KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
@@ -1102,15 +1120,28 @@ PHONY += prepare-compiler-check
1102prepare-compiler-check: FORCE 1120prepare-compiler-check: FORCE
1103# Make sure compiler supports requested stack protector flag. 1121# Make sure compiler supports requested stack protector flag.
1104ifdef stackp-name 1122ifdef stackp-name
1123 # Warn about CONFIG_CC_STACKPROTECTOR_AUTO having found no option.
1124 ifeq ($(stackp-flag),)
1125 @echo CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1126 Compiler does not support any known stack-protector >&2
1127 else
1128 # Fail if specifically requested stack protector is missing.
1105 ifeq ($(call cc-option, $(stackp-flag)),) 1129 ifeq ($(call cc-option, $(stackp-flag)),)
1106 @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \ 1130 @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1107 $(stackp-flag) not supported by compiler >&2 && exit 1 1131 $(stackp-flag) not supported by compiler >&2 && exit 1
1108 endif 1132 endif
1133 endif
1109endif 1134endif
1110# Make sure compiler does not have buggy stack-protector support. 1135# Make sure compiler does not have buggy stack-protector support. If a
1136# specific stack-protector was requested, fail the build, otherwise warn.
1111ifdef stackp-broken 1137ifdef stackp-broken
1138 ifeq ($(stackp-name),AUTO)
1139 @echo CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1140 $(stackp-flag) available but compiler is broken: disabling >&2
1141 else
1112 @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \ 1142 @echo Cannot use CONFIG_CC_STACKPROTECTOR_$(stackp-name): \
1113 $(stackp-flag) available but compiler is broken >&2 && exit 1 1143 $(stackp-flag) available but compiler is broken >&2 && exit 1
1144 endif
1114endif 1145endif
1115 @: 1146 @:
1116 1147