RFC: introduce CONFIG_INIT_ALL_MEMORY
Subsystems (5)
v3 cover RFC: introduce CONFIG_INIT_ALL_MEMORY
This patch is a part of a bigger initiative to allow initializing heap/stack memory in the Linux kernels by default.
The rationale behind doing so is to reduce the severity of bugs caused by using uninitialized memory.
Over the last two years KMSAN (https://github.com/google/kmsan/) has found more than a hundred bugs running in a really moderate setup (orders of magnitude less CPU/months than KASAN). Some of those bugs led to information leaks if uninitialized memory was copied to the userspace, other could cause DoS because of subverted control flow.
A lot more bugs remain uncovered, so we want to provide the distros and OS vendors with a last resort measure to mitigate such bugs.
Our plan is to introduce configuration flags to force initialization of stack and heap variables with a fixed pattern.
This is going to render information leaks inefficient (as we'll only leak pattern data) and make uses of uninitialized values in conditions more deterministic and discoverable.
The stack instrumentation part is based on Clang's -ftrivial-auto-var-init (see https://reviews.llvm.org/D54604 ; there's also a GCC feature request for a similar flag: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87210)
or GCC's -fplugin-arg-structleak_plugin-byref-all
The heap initialization part is compiler-agnostic and is based on the existing CONFIG_SLUB_DEBUG and CONFIG_PAGE_POISONING.
Alexander Potapenko (2):
initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
initmem: introduce CONFIG_INIT_ALL_HEAP
Makefile | 3 ++- mm/page_poison.c | 5 +++++ mm/slub.c | 2 ++ scripts/Makefile.initmem | 10 ++++++++++ security/Kconfig | 1 + security/Kconfig.initmem | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.initmem create mode 100644 security/Kconfig.initmem
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
PATCH v3 1/2 initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
CONFIG_INIT_ALL_MEMORY is going to be an umbrella config for options that force heap and stack initialization.
The rationale behind doing so is to reduce the severity of bugs caused by using uninitialized memory.
CONFIG_INIT_ALL_STACK turns on stack initialization based on -ftrivial-auto-var-init in Clang builds and on -fplugin-arg-structleak_plugin-byref-all in GCC builds.
-ftrivial-auto-var-init is a Clang flag that 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) 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.
The proposed config builds the kernel with
-ftrivial-auto-var-init=pattern.
Developers have the possibility to opt-out of this feature on a per-variable basis by using __attribute__((uninitialized)).
For GCC builds, CONFIG_INIT_ALL_STACK is simply wired up to CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. No opt-out is possible at the moment.
---
v2:
- addressed Kees Cook's comments: added GCC support
v3: addressed Masahiro Yamada's comments:
- dropped per-file opt-out mechanism
- fixed GCC_PLUGINS dependencies
--- Makefile | 3 ++- scripts/Makefile.initmem | 10 ++++++++++ security/Kconfig | 1 + security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.initmem create mode 100644 security/Kconfig.initmem
Makefile +2 -1
@@ -448,7 +448,7 @@ export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_INITMEM export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
@@ -840,6 +840,7 @@ KBUILD_ARFLAGS := $(call ar-option,D) include scripts/Makefile.kasan include scripts/Makefile.extrawarn include scripts/Makefile.ubsan +include scripts/Makefile.initmem # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the # last assignments
scripts/Makefile.initmem new file +10 -0
@@ -0,0 +1,10 @@ +ifdef CONFIG_INIT_ALL_STACK + +# Clang's -ftrivial-auto-var-init=pattern flag initializes the +# uninitialized parts of local variables (including fields and padding) +# with a fixed pattern (0xAA in most cases). +ifdef CONFIG_CC_HAS_AUTO_VAR_INIT + CFLAGS_INITMEM := -ftrivial-auto-var-init=pattern +endif + +endif
security/Kconfig +1 -0
@@ -230,6 +230,7 @@ config STATIC_USERMODEHELPER_PATH If you wish for all usermode helper programs to be disabled, specify an empty string here (i.e. ""). +source "security/Kconfig.initmem" source "security/selinux/Kconfig" source "security/smack/Kconfig" source "security/tomoyo/Kconfig"
security/Kconfig.initmem new file +29 -0
@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n + help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic. + +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack" + depends on INIT_ALL_MEMORY + depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). + +endif # INIT_ALL_MEMORY +endmenu
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
PATCH v3 2/2 initmem: introduce CONFIG_INIT_ALL_HEAP
In reply to Alexander PotapenkoThis config option enables CONFIG_SLUB_DEBUG and CONFIG_PAGE_POISONING without the need to pass any boot parameters.
No performance optimizations are done at the moment to reduce double initialization of memory regions.
---
v3:
- addressed comments by Masahiro Yamada (Kconfig fixes)
--- mm/page_poison.c | 5 +++++ mm/slub.c | 2 ++ security/Kconfig.initmem | 11 +++++++++++ 3 files changed, 18 insertions(+)
mm/page_poison.c +5 -0
@@ -12,9 +12,14 @@ static bool want_page_poisoning __read_mostly;static int __init early_page_poison_param (char *buf) { +#ifdef CONFIG_INIT_ALL_HEAP + want_page_poisoning = true; +return 0 ; +#else if (!buf)return -EINVAL ;return strtobool (buf, &want_page_poisoning); +#endif }early_param ("page_poison" , early_page_poison_param);
mm/slub.c +2 -0
@@ -1287,6 +1287,8 @@ static int __init setup_slub_debug(char *str)if (*str ==',' ) slub_debug_slabs = str +1 ; out: +if (IS_ENABLED (CONFIG_INIT_ALL_HEAP )) + slub_debug |=SLAB_POISON ;return 1 ; }
security/Kconfig.initmem +11 -0
@@ -13,6 +13,17 @@ config INIT_ALL_MEMORY if INIT_ALL_MEMORY +config INIT_ALL_HEAP + bool "Initialize all heap" + depends on INIT_ALL_MEMORY + select PAGE_POISONING + select PAGE_POISONING_NO_SANITY + select PAGE_POISONING_ZERO + select SLUB_DEBUG if SLUB + default y + help + Enable page poisoning and SLUB poisoning by default. + config INIT_ALL_STACK bool "Initialize all stack" depends on INIT_ALL_MEMORY
Signature · 1 line
2.21.0.392.gf8f6787159e-goog
Re: [PATCH v3 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP
Quoted 5 lines · Alexander Potapenko
This config option enables
CONFIG_SLUB_DEBUGandCONFIG_PAGE_POISONINGwithout the need to pass any boot parameters.No performance optimizations are done at the moment to reduce double initialization of memory regions.
[...]
Quoted 24 lines
mm/page_poison.c +5
-0@@ -12,9 +12,14 @@ static bool want_page_poisoning __read_mostly;static int __init early_page_poison_param (char *buf) { +#ifdef CONFIG_INIT_ALL_HEAP + want_page_poisoning = true; +return 0 ; +#else if (!buf)return -EINVAL ;return strtobool (buf, &want_page_poisoning); +#endif }early_param ("page_poison" , early_page_poison_param);mm/slub.c +0
-0
mm/slub.c static int __init setup_slub_debug(char *str) · line 1293
Quoted 7 lines
mm/slub.c +2
-0@@ -1287,6 +1287,8 @@ static int __init setup_slub_debug(char *str)if (*str ==',' ) slub_debug_slabs = str +1 ; out: +if (IS_ENABLED (CONFIG_INIT_ALL_HEAP )) + slub_debug |=SLAB_POISON ;return 1 ; }
I don't understand how this is supposed to work. As far as I can tell, the "slub_debug |= SLAB_POISON;" only happens if you actually pass in a "slub_debug" boot parameter? Same thing for "want_page_poisoning = true;".
Also, didn't Laura suggest in
https://www.openwall.com/lists/kernel-hardening/2019/04/08/4 that a different approach might be more sensible to reduce the performance hit?
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
In reply to Alexander PotapenkoQuoted 14 lines · Alexander Potapenko
security/Kconfig.initmem new file +8
-0@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n
security/Kconfig.initmem line 18
Quoted 10 lines
security/Kconfig.initmem new file +10
-0+ help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic. + +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack" + depends on INIT_ALL_MEMORY
This "depends on" is redundant with the "if INIT_ALL_MEMORY" above it.
Similar comment for patch 2/2 and INIT_ALL_HEAP.
Quoted 11 lines
security/Kconfig.initmem new file +11
-0+ depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). + +endif # INIT_ALL_MEMORY +endmenu
Signature · 1 line
~Randy
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
Quoted 20 lines · Randy Dunlap
Quoted 18 lines · Alexander Potapenko
security/Kconfig.initmem new file +12
-0@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n + help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic.
Quoted 8 lines
security/Kconfig.initmem line 18
Quoted 6 lines
security/Kconfig.initmem new file +6
-0+ +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack" + depends on INIT_ALL_MEMORYThis "depends on" is redundant with the "if
INIT_ALL_MEMORY" above it.
Ack, thank you!
Quoted 17 lines
Similar comment for patch 2/2 and
INIT_ALL_HEAP.security/Kconfig.initmem line 29
Quoted 11 lines
security/Kconfig.initmem new file +11
-0+ depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). + +endif # INIT_ALL_MEMORY +endmenu--
~Randy
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
In reply to Alexander PotapenkoQuoted 87 lines · Alexander Potapenko
CONFIG_INIT_ALL_MEMORYis going to be an umbrella config for options that force heap and stack initialization.
The rationale behind doing so is to reduce the severity of bugs caused by using uninitialized memory.
CONFIG_INIT_ALL_STACKturns on stack initialization based on(quoted diff) +0
-3-ftrivial-auto-var-init in Clang builds and on -fplugin-arg-structleak_plugin-byref-all in GCC builds. -ftrivial-auto-var-init is a Clang flag that provides trivialinitializers 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) 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.
The proposed config builds the kernel with
-ftrivial-auto-var-init=pattern.Developers have the possibility to opt-out of this feature on a per-variable basis by using __attribute__((uninitialized)).
For GCC builds,
CONFIG_INIT_ALL_STACKis simply wired up toCONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. No opt-out is possible at the moment.Signed-off-by Alexander Potapenko <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>---
v2:
- addressed Kees Cook's comments: added GCC support
v3: addressed Masahiro Yamada's comments:
- dropped per-file opt-out mechanism
- fixedGCC_PLUGINSdependencies--- Makefile | 3 ++- scripts/Makefile.initmem | 10 ++++++++++ security/Kconfig | 1 + security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.initmem create mode 100644 security/Kconfig.initmemMakefile +2
-1@@ -448,7 +448,7 @@ export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_INITMEM export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL@@ -840,6 +840,7 @@ KBUILD_ARFLAGS := $(call ar-option,D) include scripts/Makefile.kasan include scripts/Makefile.extrawarn include scripts/Makefile.ubsan +include scripts/Makefile.initmem # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the # last assignmentsscripts/Makefile.initmem new file +0
-0
scripts/Makefile.initmem line 10
Quoted 10 lines
scripts/Makefile.initmem new file +10
-0@@ -0,0 +1,10 @@ +ifdef CONFIG_INIT_ALL_STACK + +# Clang's -ftrivial-auto-var-init=pattern flag initializes the +# uninitialized parts of local variables (including fields and padding) +# with a fixed pattern (0xAA in most cases). +ifdef CONFIG_CC_HAS_AUTO_VAR_INIT + CFLAGS_INITMEM := -ftrivial-auto-var-init=pattern +endif + +endif
Now that you dropped per-file control,
why is this necessary?
Also, CFLAGS_INITMEM is not wired up to anything, so this patch has no effect.
I think you can add the following to top Makefile, and that's it.
ifdef CONFIG_AUTO_VAR_INITKBUILD_CFLAGS += -ftrivial-auto-var-init=pattern endif
Quoted 35 lines
security/Kconfig +1
-0@@ -230,6 +230,7 @@ config STATIC_USERMODEHELPER_PATH If you wish for all usermode helper programs to be disabled, specify an empty string here (i.e. ""). +source "security/Kconfig.initmem" source "security/selinux/Kconfig" source "security/smack/Kconfig" source "security/tomoyo/Kconfig"security/Kconfig.initmem new file +17
-0@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n + help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic. + +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack"
security/Kconfig.initmem line 27
Quoted 10 lines
security/Kconfig.initmem new file +10
-0+ depends on INIT_ALL_MEMORY + depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). +
I am not sure about the benefit of these umbrella CONFIGs since these make 'depends on' and 'select' complicated. I will leave it to Kees, though.
Quoted 5 lines
security/Kconfig.initmem new file +2
-1+endif # INIT_ALL_MEMORY +endmenu --2.21.0.392.gf8f6787159e-goog
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
Quoted 96 lines · Masahiro Yamada
Quoted 94 lines · Alexander Potapenko
CONFIG_INIT_ALL_MEMORYis going to be an umbrella config for options that force heap and stack initialization.
The rationale behind doing so is to reduce the severity of bugs caused by using uninitialized memory.
CONFIG_INIT_ALL_STACKturns on stack initialization based on(quoted diff) +0
-3-ftrivial-auto-var-init in Clang builds and on -fplugin-arg-structleak_plugin-byref-all in GCC builds. -ftrivial-auto-var-init is a Clang flag that provides trivialinitializers 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) 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.
The proposed config builds the kernel with
-ftrivial-auto-var-init=pattern.Developers have the possibility to opt-out of this feature on a per-variable basis by using __attribute__((uninitialized)).
For GCC builds,
CONFIG_INIT_ALL_STACKis simply wired up toCONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. No opt-out is possible at the moment.Signed-off-by Alexander Potapenko <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>---
v2:
- addressed Kees Cook's comments: added GCC support
v3: addressed Masahiro Yamada's comments:
- dropped per-file opt-out mechanism
- fixedGCC_PLUGINSdependencies--- Makefile | 3 ++- scripts/Makefile.initmem | 10 ++++++++++ security/Kconfig | 1 + security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.initmem create mode 100644 security/Kconfig.initmemMakefile +2
-1@@ -448,7 +448,7 @@ export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_INITMEM export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL@@ -840,6 +840,7 @@ KBUILD_ARFLAGS := $(call ar-option,D) include scripts/Makefile.kasan include scripts/Makefile.extrawarn include scripts/Makefile.ubsan +include scripts/Makefile.initmem # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the # last assignmentsscripts/Makefile.initmem new file +7
-0@@ -0,0 +1,10 @@ +ifdef CONFIG_INIT_ALL_STACK + +# Clang's -ftrivial-auto-var-init=pattern flag initializes the +# uninitialized parts of local variables (including fields and padding) +# with a fixed pattern (0xAA in most cases). +ifdef CONFIG_CC_HAS_AUTO_VAR_INIT + CFLAGS_INITMEM := -ftrivial-auto-var-init=pattern
Quoted 8 lines
scripts/Makefile.initmem line 10
Quoted 3 lines
scripts/Makefile.initmem new file +3
-0+endif + +endifNow that you dropped per-file control,
why is this necessary?
Also,CFLAGS_INITMEMis not wired up to anything, so this patch has no effect.
There's CFLAGS_INITMEM in the top Makefile, so I suppose it should be working. But I agree there's no point in adding extra files for a single build flag.
Quoted 73 lines
I think you can add the following to top
Makefile, and that's it.ifdef
CONFIG_AUTO_VAR_INITKBUILD_CFLAGS+= -ftrivial-auto-var-init=pattern endifQuoted 35 lines
security/Kconfig +1
-0@@ -230,6 +230,7 @@ config STATIC_USERMODEHELPER_PATH If you wish for all usermode helper programs to be disabled, specify an empty string here (i.e. ""). +source "security/Kconfig.initmem" source "security/selinux/Kconfig" source "security/smack/Kconfig" source "security/tomoyo/Kconfig"security/Kconfig.initmem new file +17
-0@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n + help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic. + +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack"security/Kconfig.initmem line 27
Quoted 10 lines
security/Kconfig.initmem new file +10
-0+ depends on INIT_ALL_MEMORY + depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). +I am not sure about the benefit of these umbrella CONFIGs since these make 'depends on' and 'select' complicated. I will leave it to Kees, though.
Quoted 5 lines
security/Kconfig.initmem new file +2
-1+endif # INIT_ALL_MEMORY +endmenu --2.21.0.392.gf8f6787159e-goog
--
Best Regards
Masahiro Yamada
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
Quoted 107 lines · Alexander Potapenko
Quoted 96 lines · Masahiro Yamada
Quoted 94 lines · Alexander Potapenko
CONFIG_INIT_ALL_MEMORYis going to be an umbrella config for options that force heap and stack initialization.
The rationale behind doing so is to reduce the severity of bugs caused by using uninitialized memory.
CONFIG_INIT_ALL_STACKturns on stack initialization based on(quoted diff) +0
-3-ftrivial-auto-var-init in Clang builds and on -fplugin-arg-structleak_plugin-byref-all in GCC builds. -ftrivial-auto-var-init is a Clang flag that provides trivialinitializers 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) 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.
The proposed config builds the kernel with
-ftrivial-auto-var-init=pattern.Developers have the possibility to opt-out of this feature on a per-variable basis by using __attribute__((uninitialized)).
For GCC builds,
CONFIG_INIT_ALL_STACKis simply wired up toCONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. No opt-out is possible at the moment.Signed-off-by Alexander Potapenko <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>---
v2:
- addressed Kees Cook's comments: added GCC support
v3: addressed Masahiro Yamada's comments:
- dropped per-file opt-out mechanism
- fixedGCC_PLUGINSdependencies--- Makefile | 3 ++- scripts/Makefile.initmem | 10 ++++++++++ security/Kconfig | 1 + security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.initmem create mode 100644 security/Kconfig.initmemMakefile +2
-1@@ -448,7 +448,7 @@ export HOSTCXX KBUILD_HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE -export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN +export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE CFLAGS_UBSAN CFLAGS_INITMEM export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL@@ -840,6 +840,7 @@ KBUILD_ARFLAGS := $(call ar-option,D) include scripts/Makefile.kasan include scripts/Makefile.extrawarn include scripts/Makefile.ubsan +include scripts/Makefile.initmem # Add any arch overrides and user supplied CPPFLAGS, AFLAGS and CFLAGS as the # last assignmentsscripts/Makefile.initmem new file +7
-0@@ -0,0 +1,10 @@ +ifdef CONFIG_INIT_ALL_STACK + +# Clang's -ftrivial-auto-var-init=pattern flag initializes the +# uninitialized parts of local variables (including fields and padding) +# with a fixed pattern (0xAA in most cases). +ifdef CONFIG_CC_HAS_AUTO_VAR_INIT + CFLAGS_INITMEM := -ftrivial-auto-var-init=patternQuoted 8 lines
scripts/Makefile.initmem line 10
Quoted 3 lines
scripts/Makefile.initmem new file +3
-0+endif + +endifNow that you dropped per-file control,
why is this necessary?
Also,CFLAGS_INITMEMis not wired up to anything, so this patch has no effect.
Quoted 1 line
There's
CFLAGS_INITMEMin the topMakefile, so I suppose it should be working.
Ah, now I see. Yes, I was too hasty with this patchset.
Quoted 88 lines
But I agree there's no point in adding extra files for a single build flag.
Quoted 63 lines
I think you can add the following to top
Makefile, and that's it.ifdef
CONFIG_AUTO_VAR_INITKBUILD_CFLAGS+= -ftrivial-auto-var-init=pattern endifQuoted 35 lines
security/Kconfig +1
-0@@ -230,6 +230,7 @@ config STATIC_USERMODEHELPER_PATH If you wish for all usermode helper programs to be disabled, specify an empty string here (i.e. ""). +source "security/Kconfig.initmem" source "security/selinux/Kconfig" source "security/smack/Kconfig" source "security/tomoyo/Kconfig"security/Kconfig.initmem new file +17
-0@@ -0,0 +1,29 @@ +menu "Initialize all memory" + +config CC_HAS_AUTO_VAR_INIT + def_bool $(cc-option,-ftrivial-auto-var-init=pattern) + +config INIT_ALL_MEMORY + bool "Initialize all memory" + default n + help + Enforce memory initialization to mitigate infoleaks and make + the control-flow bugs depending on uninitialized values more + deterministic. + +if INIT_ALL_MEMORY + +config INIT_ALL_STACK + bool "Initialize all stack"security/Kconfig.initmem line 27
Quoted 10 lines
security/Kconfig.initmem new file +10
-0+ depends on INIT_ALL_MEMORY + depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y + help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). +I am not sure about the benefit of these umbrella CONFIGs since these make 'depends on' and 'select' complicated. I will leave it to Kees, though.
Quoted 10 lines
Quoted 5 lines
security/Kconfig.initmem new file +2
-1+endif # INIT_ALL_MEMORY +endmenu --2.21.0.392.gf8f6787159e-goog
--
Best Regards
Masahiro Yamada--
Alexander Potapenko
Software EngineerGoogle Germany GmbH
Erika-Mann-Straße, 33
80636 MünchenGeschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH v3 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP
In reply to Alexander PotapenkoQuoted 35 lines · Alexander Potapenko
This config option enables
CONFIG_SLUB_DEBUGandCONFIG_PAGE_POISONINGwithout the need to pass any boot parameters.No performance optimizations are done at the moment to reduce double initialization of memory regions.
Signed-off-by Alexander Potapenko <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>---
v3:
- addressed comments by Masahiro Yamada (Kconfigfixes)--- mm/page_poison.c | 5 +++++ mm/slub.c | 2 ++ security/Kconfig.initmem | 11 +++++++++++ 3 files changed, 18 insertions(+)mm/page_poison.c +0
-0@@ -12,9 +12,14 @@ static bool want_page_poisoning __read_mostly;static int __init early_page_poison_param (char *buf) {
mm/page_poison.c static bool want_page_poisoning __read_mostly; · line 24
Quoted 10 lines
mm/page_poison.c +5
-0+#ifdef CONFIG_INIT_ALL_HEAP + want_page_poisoning = true; +return 0 ; +#else if (!buf)return -EINVAL ;return strtobool (buf, &want_page_poisoning); +#endif }early_param ("page_poison" , early_page_poison_param);
IIUC this is only called if page_poison is passed on the command line, so want_page_poisoning won't be set automatically unless that's passed.
Presumably you want to initialize it at definition with:
static bool want_page_poisoning __read_mostly = IS_ENABLED(CONFIG_INIT_ALL_HEAP);
with:
#ifndef CONFIG_INIT_ALL_HEAP
static int __init early_page_poison_param(char *buf)
{
...
}early_param("page_poison", early_page_poison_param); #endif
... so that it can't be disabled?
Thanks,
Mark.
Re: [PATCH v3 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP
Quoted 51 lines · Mark Rutland
Quoted 35 lines · Alexander Potapenko
This config option enables
CONFIG_SLUB_DEBUGandCONFIG_PAGE_POISONINGwithout the need to pass any boot parameters.No performance optimizations are done at the moment to reduce double initialization of memory regions.
Signed-off-by Alexander Potapenko <[email protected]>Cc Masahiro Yamada <[email protected]>Cc James Morris <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kostya Serebryany <[email protected]>Cc Dmitry Vyukov <[email protected]>Cc Kees Cook <[email protected]>Cc Sandeep Patil <[email protected]>---
v3:
- addressed comments by Masahiro Yamada (Kconfigfixes)--- mm/page_poison.c | 5 +++++ mm/slub.c | 2 ++ security/Kconfig.initmem | 11 +++++++++++ 3 files changed, 18 insertions(+)mm/page_poison.c +0
-0@@ -12,9 +12,14 @@ static bool want_page_poisoning __read_mostly;static int __init early_page_poison_param (char *buf) {mm/page_poison.c static bool want_page_poisoning __read_mostly; · line 24
Quoted 10 lines
mm/page_poison.c +5
-0+#ifdef CONFIG_INIT_ALL_HEAP + want_page_poisoning = true; +return 0 ; +#else if (!buf)return -EINVAL ;return strtobool (buf, &want_page_poisoning); +#endif }early_param ("page_poison" , early_page_poison_param);IIUC this is only called if
page_poisonis passed on the command line, sowant_page_poisoningwon't be set automatically unless that's passed.
Quoted 3 lines
Presumably you want to initialize it at definition with:
static bool
want_page_poisoning__read_mostly=IS_ENABLED(CONFIG_INIT_ALL_HEAP);
Yes, I've misunderstood how boot parameters work.
Thanks for the suggestions!
We'd better look into wiping allocations without POISON_SLAB though.
Quoted 14 lines
with:
#ifndef
CONFIG_INIT_ALL_HEAP
static int__initearly_page_poison_param(char *buf){ ... }
early_param("page_poison", early_page_poison_param); #endif... so that it can't be disabled?
Thanks,
Mark.
Signature · 10 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg
Re: [PATCH v3 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK
In reply to Masahiro YamadaQuoted 8 lines · Masahiro Yamada
Quoted 8 lines
security/Kconfig.initmem new file +8
-0+config INIT_ALL_STACK + bool "Initialize all stack" + depends on INIT_ALL_MEMORY + depends on CC_HAS_AUTO_VAR_INIT || (HAVE_GCC_PLUGINS && PLUGIN_HOSTCC != "") + select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK if !CC_HAS_AUTO_VAR_INIT + select GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if !CC_HAS_AUTO_VAR_INIT + default y
Quoted 8 lines
security/Kconfig.initmem line 27
Quoted 4 lines
security/Kconfig.initmem new file +4
-0+ help + Initialize uninitialized stack data with a fixed pattern + (0x00 in GCC, 0xAA in Clang). +I am not sure about the benefit of these umbrella CONFIGs since these make 'depends on' and 'select' complicated. I will leave it to Kees, though.
Yeah, I think this could use some more cleanup to get a single config that represents that if gcc plugins are available (instead of having it split across two configs now). Additionally I think this needs refactoring against the current menu choices for GCC_PLUGIN_STRUCTLEAK. Let me send a proposed patch in a few hours...
Signature · 1 line
Kees Cook