Skip to contentSkip to conversation

kbuild: Provide way to actually disable stack protector

PATCH In review Kees Cook 2 participants 5 messages Jun 22, 2020 – Jun 26, 2020

Subsystems (4)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 kbuild: Provide way to actually disable stack protector

Some builds of GCC enable stack protector by default. Simply removing the arguments is not sufficient to disable stack protector, as the stack protector for those GCC builds must be explicitly disabled. (Removing the arguments is left as-is just to be sure there are no ordering problems. If -fno-stack-protector ended up _before_ -fstack-protector, it would not disable it: GCC uses whichever -f... comes last on the command line.)

Fixes 20355e5f73a7 ("x86/entry: Exclude low level entry code from sanitizing")
---
 Makefile                          | 4 +++-
 arch/Kconfig                      | 3 ---
 arch/arm/boot/compressed/Makefile | 4 ++--
 arch/x86/entry/Makefile           | 3 +++
 4 files changed, 8 insertions(+), 6 deletions(-)
Makefile +3 -1
index ac2c61c37a73..b46e91bf0b0e 100644
@@ -762,7 +762,9 @@ ifneq ($(CONFIG_FRAME_WARN),0)
 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
 endif
 
-stackp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
+DISABLE_STACKPROTECTOR := $(call cc-option,-fno-stack-protector)
+export DISABLE_STACKPROTECTOR
+stackp-flags-y                                    := $(DISABLE_STACKPROTECTOR)
 stackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
 stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 
arch/Kconfig +0 -3
index 8cc35dc556c7..1ea61290900a 100644
@@ -478,9 +478,6 @@ config HAVE_STACKPROTECTOR
 	  An arch should select this symbol if:
 	  - it has implemented a stack canary (e.g. __stack_chk_guard)
 
-config CC_HAS_STACKPROTECTOR_NONE
-	def_bool $(cc-option,-fno-stack-protector)
-
 config STACKPROTECTOR
 	bool "Stack Protector buffer overflow detection"
 	depends on HAVE_STACKPROTECTOR
arch/arm/boot/compressed/Makefile +2 -2
index 00602a6fba04..3693bac525d2 100644
@@ -84,9 +84,9 @@ endif
 
 # -fstack-protector-strong triggers protection checks in this code,
 # but it is being used too early to link to meaningful stack_chk logic.
-nossp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
 $(foreach o, $(libfdt_objs) atags_to_fdt.o, \
-	$(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt $(nossp-flags-y)))
+	$(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt \
+			      $(DISABLE_STACKPROTECTOR)))
 
 # These were previously generated C files. When you are building the kernel
 # with O=, make sure to remove the stale files in the output tree. Otherwise,
arch/x86/entry/Makefile +3 -0
index b7a5790d8d63..79902decc3d1 100644
@@ -10,6 +10,9 @@ KCOV_INSTRUMENT := n
 CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
+CFLAGS_common.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_32.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_64.o += $(DISABLE_STACKPROTECTOR)
 
 CFLAGS_syscall_64.o		+= $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o		+= $(call cc-option,-Wno-override-init,)
Signature · 5 lines
2.25.1


-- 
Kees Cook
#1 lore raw
Quoted 67 lines · Kees Cook

Some builds of GCC enable stack protector by default. Simply removing the arguments is not sufficient to disable stack protector, as the stack protector for those GCC builds must be explicitly disabled. (Removing the arguments is left as-is just to be sure there are no ordering problems. If -fno-stack-protector ended up _before_ -fstack-protector, it would not disable it: GCC uses whichever -f... comes last on the command line.)

Fixes 20355e5f73a7 ("x86/entry: Exclude low level entry code from sanitizing")
---
 Makefile                          | 4 +++-
 arch/Kconfig                      | 3 ---
 arch/arm/boot/compressed/Makefile | 4 ++--
 arch/x86/entry/Makefile           | 3 +++
 4 files changed, 8 insertions(+), 6 deletions(-)
Makefile +3 -1
index ac2c61c37a73..b46e91bf0b0e 100644
@@ -762,7 +762,9 @@ ifneq ($(CONFIG_FRAME_WARN),0)
 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
 endif
 
-stackp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
+DISABLE_STACKPROTECTOR := $(call cc-option,-fno-stack-protector)
+export DISABLE_STACKPROTECTOR
+stackp-flags-y                                    := $(DISABLE_STACKPROTECTOR)
 stackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
 stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 
arch/Kconfig +0 -3
index 8cc35dc556c7..1ea61290900a 100644
@@ -478,9 +478,6 @@ config HAVE_STACKPROTECTOR
          An arch should select this symbol if:
          - it has implemented a stack canary (e.g. __stack_chk_guard)
 
-config CC_HAS_STACKPROTECTOR_NONE
-       def_bool $(cc-option,-fno-stack-protector)
-
 config STACKPROTECTOR
        bool "Stack Protector buffer overflow detection"
        depends on HAVE_STACKPROTECTOR
arch/arm/boot/compressed/Makefile +2 -2
index 00602a6fba04..3693bac525d2 100644
@@ -84,9 +84,9 @@ endif
 
 # -fstack-protector-strong triggers protection checks in this code,
 # but it is being used too early to link to meaningful stack_chk logic.
-nossp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
 $(foreach o, $(libfdt_objs) atags_to_fdt.o, \
-       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt $(nossp-flags-y)))
+       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt \
+                             $(DISABLE_STACKPROTECTOR)))
 
 # These were previously generated C files. When you are building the kernel
 # with O=, make sure to remove the stale files in the output tree. Otherwise,
arch/x86/entry/Makefile +0 -0
index b7a5790d8d63..79902decc3d1 100644

arch/x86/entry/Makefile KCOV_INSTRUMENT := n · line 15

Quoted 6 lines
arch/x86/entry/Makefile +3 -0
@@ -10,6 +10,9 @@ KCOV_INSTRUMENT := n
 CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
+CFLAGS_common.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_32.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_64.o += $(DISABLE_STACKPROTECTOR)

There is one more c file in this directory.

Is it OK to not patch syscall_x32.c ?

arch/x86/entry/Makefile KCOV_INSTRUMENT := n · line 17

Quoted 3 lines
arch/x86/entry/Makefile +0 -0
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)

This patch is ugly.

I'd rather want to fix this by one-liner.

arch/x86/entry/Makefile +2 -3
index b7a5790d8d63..0d41eb91aaea 100644
@@ -11,6 +11,8 @@ CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
-fstack-protector -fstack-protector-
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 
+ccflags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) += -fno-stack-protector
+
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)
 obj-y                          := entry_$(BITS).o thunk_$(BITS).o

syscall_$(BITS).o

Signature · 2 lines
Best Regards
Masahiro Yamada
author
#2 lore raw
Quoted 77 lines · Masahiro Yamada
Quoted 67 lines · Kees Cook

Some builds of GCC enable stack protector by default. Simply removing the arguments is not sufficient to disable stack protector, as the stack protector for those GCC builds must be explicitly disabled. (Removing the arguments is left as-is just to be sure there are no ordering problems. If -fno-stack-protector ended up _before_ -fstack-protector, it would not disable it: GCC uses whichever -f... comes last on the command line.)

Fixes 20355e5f73a7 ("x86/entry: Exclude low level entry code from sanitizing")
---
 Makefile                          | 4 +++-
 arch/Kconfig                      | 3 ---
 arch/arm/boot/compressed/Makefile | 4 ++--
 arch/x86/entry/Makefile           | 3 +++
 4 files changed, 8 insertions(+), 6 deletions(-)
Makefile +3 -1
index ac2c61c37a73..b46e91bf0b0e 100644
@@ -762,7 +762,9 @@ ifneq ($(CONFIG_FRAME_WARN),0)
 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
 endif
 
-stackp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
+DISABLE_STACKPROTECTOR := $(call cc-option,-fno-stack-protector)
+export DISABLE_STACKPROTECTOR
+stackp-flags-y                                    := $(DISABLE_STACKPROTECTOR)
 stackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
 stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 
arch/Kconfig +0 -3
index 8cc35dc556c7..1ea61290900a 100644
@@ -478,9 +478,6 @@ config HAVE_STACKPROTECTOR
          An arch should select this symbol if:
          - it has implemented a stack canary (e.g. __stack_chk_guard)
 
-config CC_HAS_STACKPROTECTOR_NONE
-       def_bool $(cc-option,-fno-stack-protector)
-
 config STACKPROTECTOR
        bool "Stack Protector buffer overflow detection"
        depends on HAVE_STACKPROTECTOR
arch/arm/boot/compressed/Makefile +2 -2
index 00602a6fba04..3693bac525d2 100644
@@ -84,9 +84,9 @@ endif
 
 # -fstack-protector-strong triggers protection checks in this code,
 # but it is being used too early to link to meaningful stack_chk logic.
-nossp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
 $(foreach o, $(libfdt_objs) atags_to_fdt.o, \
-       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt $(nossp-flags-y)))
+       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt \
+                             $(DISABLE_STACKPROTECTOR)))
 
 # These were previously generated C files. When you are building the kernel
 # with O=, make sure to remove the stale files in the output tree. Otherwise,
arch/x86/entry/Makefile +0 -0
index b7a5790d8d63..79902decc3d1 100644

arch/x86/entry/Makefile KCOV_INSTRUMENT := n · line 15

Quoted 6 lines
arch/x86/entry/Makefile +3 -0
@@ -10,6 +10,9 @@ KCOV_INSTRUMENT := n
 CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
+CFLAGS_common.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_32.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_64.o += $(DISABLE_STACKPROTECTOR)

There is one more c file in this directory.

Quoted 1 line

Is it OK to not patch syscall_x32.c ?

Good question. Peter? (It seems all the syscall_*.c files are just a table, not code -- why do they need any instrumentation changes?)

Quoted 9 lines

arch/x86/entry/Makefile KCOV_INSTRUMENT := n · line 17

Quoted 5 lines
arch/x86/entry/Makefile +0 -0
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)
Quoted 3 lines

This patch is ugly.

I'd rather want to fix this by one-liner.

Why not a global export to assist? This isn't the only place it's needed (see the arm64 chunk...)

Quoted 9 lines
arch/x86/entry/Makefile +0 -0
index b7a5790d8d63..0d41eb91aaea 100644

arch/x86/entry/Makefile CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) · line 15

Quoted 8 lines
arch/x86/entry/Makefile +2 -3
@@ -11,6 +11,8 @@ CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
-fstack-protector -fstack-protector-
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 
+ccflags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) += -fno-stack-protector
+

Order matters here -- when is ccflags-y applied?

Quoted 11 lines
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)
 obj-y                          := entry_$(BITS).o thunk_$(BITS).o

syscall_$(BITS).o

Signature · 2 lines
Best Regards
Masahiro Yamada
Signature · 1 line
Kees Cook
#3 lore raw
Quoted 93 lines · Kees Cook
Quoted 77 lines · Masahiro Yamada
Quoted 67 lines · Kees Cook

Some builds of GCC enable stack protector by default. Simply removing the arguments is not sufficient to disable stack protector, as the stack protector for those GCC builds must be explicitly disabled. (Removing the arguments is left as-is just to be sure there are no ordering problems. If -fno-stack-protector ended up _before_ -fstack-protector, it would not disable it: GCC uses whichever -f... comes last on the command line.)

Fixes 20355e5f73a7 ("x86/entry: Exclude low level entry code from sanitizing")
---
 Makefile                          | 4 +++-
 arch/Kconfig                      | 3 ---
 arch/arm/boot/compressed/Makefile | 4 ++--
 arch/x86/entry/Makefile           | 3 +++
 4 files changed, 8 insertions(+), 6 deletions(-)
Makefile +3 -1
index ac2c61c37a73..b46e91bf0b0e 100644
@@ -762,7 +762,9 @@ ifneq ($(CONFIG_FRAME_WARN),0)
 KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
 endif
 
-stackp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
+DISABLE_STACKPROTECTOR := $(call cc-option,-fno-stack-protector)
+export DISABLE_STACKPROTECTOR
+stackp-flags-y                                    := $(DISABLE_STACKPROTECTOR)
 stackp-flags-$(CONFIG_STACKPROTECTOR)             := -fstack-protector
 stackp-flags-$(CONFIG_STACKPROTECTOR_STRONG)      := -fstack-protector-strong
 
arch/Kconfig +0 -3
index 8cc35dc556c7..1ea61290900a 100644
@@ -478,9 +478,6 @@ config HAVE_STACKPROTECTOR
          An arch should select this symbol if:
          - it has implemented a stack canary (e.g. __stack_chk_guard)
 
-config CC_HAS_STACKPROTECTOR_NONE
-       def_bool $(cc-option,-fno-stack-protector)
-
 config STACKPROTECTOR
        bool "Stack Protector buffer overflow detection"
        depends on HAVE_STACKPROTECTOR
arch/arm/boot/compressed/Makefile +2 -2
index 00602a6fba04..3693bac525d2 100644
@@ -84,9 +84,9 @@ endif
 
 # -fstack-protector-strong triggers protection checks in this code,
 # but it is being used too early to link to meaningful stack_chk logic.
-nossp-flags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) := -fno-stack-protector
 $(foreach o, $(libfdt_objs) atags_to_fdt.o, \
-       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt $(nossp-flags-y)))
+       $(eval CFLAGS_$(o) := -I $(srctree)/scripts/dtc/libfdt \
+                             $(DISABLE_STACKPROTECTOR)))
 
 # These were previously generated C files. When you are building the kernel
 # with O=, make sure to remove the stale files in the output tree. Otherwise,
arch/x86/entry/Makefile +0 -0
index b7a5790d8d63..79902decc3d1 100644

arch/x86/entry/Makefile KCOV_INSTRUMENT := n · line 15

Quoted 6 lines
arch/x86/entry/Makefile +3 -0
@@ -10,6 +10,9 @@ KCOV_INSTRUMENT := n
 CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector -fstack-protector-strong
+CFLAGS_common.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_32.o += $(DISABLE_STACKPROTECTOR)
+CFLAGS_syscall_64.o += $(DISABLE_STACKPROTECTOR)

There is one more c file in this directory.

Quoted 1 line

Is it OK to not patch syscall_x32.c ?

Good question. Peter? (It seems all the syscall_*.c files are just a table, not code -- why do they need any instrumentation changes?)

Quoted 9 lines
Quoted 5 lines
arch/x86/entry/Makefile +0 -0
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)
Quoted 6 lines
Quoted 3 lines

This patch is ugly.

I'd rather want to fix this by one-liner.

Why not a global export to assist? This isn't the only place it's needed (see the arm64 chunk...)

Is it useful when we know
DISABLE_STACKPROTECTOR = -fno-stack-protector ?

I'd rather want to apply this patch
https://patchwork.kernel.org/patch/11628493/
and hard-code -fno-stack-protector where necessary.

Quoted 16 lines
Quoted 16 lines
arch/x86/entry/Makefile +0 -3
index b7a5790d8d63..0d41eb91aaea 100644
@@ -11,6 +11,8 @@ CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE)
-fstack-protector -fstack-protector-
 CFLAGS_REMOVE_syscall_32.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 CFLAGS_REMOVE_syscall_64.o = $(CC_FLAGS_FTRACE) -fstack-protector
-fstack-protector-strong
 
Quoted 4 lines

arch/x86/entry/Makefile CFLAGS_REMOVE_common.o = $(CC_FLAGS_FTRACE) · line 15

Quoted 2 lines
arch/x86/entry/Makefile +2 -0
+ccflags-$(CONFIG_CC_HAS_STACKPROTECTOR_NONE) += -fno-stack-protector
+

Order matters here -- when is ccflags-y applied?

cc-flags-y comes after KBUILD_CFLAGS
so that -fno-stack-protector can negate -fstack-protector(-strong)

Quoted 15 lines
Quoted 4 lines
 CFLAGS_syscall_64.o            += $(call cc-option,-Wno-override-init,)
 CFLAGS_syscall_32.o            += $(call cc-option,-Wno-override-init,)
 obj-y                          := entry_$(BITS).o thunk_$(BITS).o
Quoted 8 lines

syscall_$(BITS).o

--
Best Regards
Masahiro Yamada

--
Kees Cook

Signature · 2 lines
Best Regards
Masahiro Yamada
author
#4 lore raw
Quoted 7 lines · Masahiro Yamada
Quoted 6 lines · Kees Cook
Quoted 1 line · Masahiro Yamada

Is it OK to not patch syscall_x32.c ?

Good question. Peter? (It seems all the syscall_*.c files are just a table, not code -- why do they need any instrumentation changes?)

I'd still like to know the answer to this one...

Quoted 2 lines

Is it useful when we know
DISABLE_STACKPROTECTOR = -fno-stack-protector ?

I'm fine with that. My point was the using _REMOVE isn't going to work for some compiler builds.

Quoted 3 lines

I'd rather want to apply this patch
https://patchwork.kernel.org/patch/11628493/
and hard-code -fno-stack-protector where necessary.

That's fine. I will send a separate fix for arch/x86/entry/Makefile.

Quoted 2 lines

cc-flags-y comes after KBUILD_CFLAGS
so that -fno-stack-protector can negate -fstack-protector(-strong)

Okay, good.

Signature · 1 line
Kees Cook