Skip to contentSkip to conversation

RFC: introduce CONFIG_INIT_ALL_MEMORY

PATCH Superseded Alexander Potapenko 4 participants 14 messages Mar 8, 2019 – Apr 18, 2019

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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 ++++++++++
 scripts/Makefile.lib     |  6 ++++++
 security/Kconfig         |  1 +
 security/Kconfig.initmem | 40 ++++++++++++++++++++++++++++++++++++++++
 7 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 scripts/Makefile.initmem
 create mode 100644 security/Kconfig.initmem
Signature · 1 line
2.21.0.360.g471c308f928-goog
#1 lore raw

PATCH v2 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-file (by using the INIT_ALL_MEMORY_ Makefile prefix) or per-variable (by using __attribute__((uninitialized))) basis.

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.

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

---
 Makefile                 |  3 ++-
 scripts/Makefile.initmem | 10 ++++++++++
 scripts/Makefile.lib     |  6 ++++++
 security/Kconfig         |  1 +
 security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++
 5 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 scripts/Makefile.initmem
 create mode 100644 security/Kconfig.initmem
Makefile +2 -1
index f070e0d65186..028ca37878fd 100644
@@ -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
new file mode 100644
index 000000000000..a6253d78fe35
@@ -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
scripts/Makefile.lib +6 -0
index 12b88d09c3a4..53d18fd15c79 100644
@@ -131,6 +131,12 @@ _c_flags += $(if $(patsubst n%,, \
 		$(CFLAGS_UBSAN))
 endif
 
+ifeq ($(CONFIG_INIT_ALL_MEMORY),y)
+_c_flags += $(if $(patsubst n%,, \
+		$(INIT_ALL_MEMORY_$(basetarget).o)$(INIT_ALL_MEMORY)y), \
+		$(CFLAGS_INITMEM))
+endif
+
 ifeq ($(CONFIG_KCOV),y)
 _c_flags += $(if $(patsubst n%,, \
 	$(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
security/Kconfig +1 -0
index e4fe2f3c2c65..cc12a39424dd 100644
@@ -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
new file mode 100644
index 000000000000..27aec394365e
@@ -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
+	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.360.g471c308f928-goog
#2 lore raw

Re: [PATCH v2 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK

Quoted 46 lines · Alexander Potapenko

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

(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 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-file (by using the INIT_ALL_MEMORY_ Makefile prefix) or per-variable (by using __attribute__((uninitialized))) basis.

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.

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]>
Quoted 3 lines

---
v2:
- addressed Kees Cook's comments: added GCC support

A friendly ping.
Please let me know if I should wait for any upcoming changes to CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL.

Quoted 114 lines
---
 Makefile                 |  3 ++-
 scripts/Makefile.initmem | 10 ++++++++++
 scripts/Makefile.lib     |  6 ++++++
 security/Kconfig         |  1 +
 security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++
 5 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 scripts/Makefile.initmem
 create mode 100644 security/Kconfig.initmem
Makefile +2 -1
index f070e0d65186..028ca37878fd 100644
@@ -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
new file mode 100644
index 000000000000..a6253d78fe35
@@ -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
scripts/Makefile.lib +6 -0
index 12b88d09c3a4..53d18fd15c79 100644
@@ -131,6 +131,12 @@ _c_flags += $(if $(patsubst n%,, \
                $(CFLAGS_UBSAN))
 endif
 
+ifeq ($(CONFIG_INIT_ALL_MEMORY),y)
+_c_flags += $(if $(patsubst n%,, \
+               $(INIT_ALL_MEMORY_$(basetarget).o)$(INIT_ALL_MEMORY)y), \
+               $(CFLAGS_INITMEM))
+endif
+
 ifeq ($(CONFIG_KCOV),y)
 _c_flags += $(if $(patsubst n%,, \
        $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
security/Kconfig +1 -0
index e4fe2f3c2c65..cc12a39424dd 100644
@@ -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 -1
new file mode 100644
index 000000000000..27aec394365e
@@ -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
+       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
--

2.21.0.360.g471c308f928-goog

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
#3 lore raw

Re: [PATCH v2 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK

Quoted 3 lines · Alexander Potapenko

A friendly ping.
Please let me know if I should wait for any upcoming changes to CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL.

Hi! Sorry for the delay -- between the merge window and travel, I'm still a couple weeks behind. I should be able to look more closely at this late next week.

Signature · 1 line
Kees Cook
#4 lore raw

Re: [PATCH v2 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK

In reply to Alexander Potapenko
Quoted 21 lines · Alexander Potapenko

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

(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 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;
Quoted 7 lines

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-file (by using the INIT_ALL_MEMORY_ Makefile prefix)

Do you have a plan to use this per-file prefix? If so, could you elaborate which parts should opt out?

Quoted 120 lines

or per-variable
(by using __attribute__((uninitialized))) basis.

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.

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

---
 Makefile                 |  3 ++-
 scripts/Makefile.initmem | 10 ++++++++++
 scripts/Makefile.lib     |  6 ++++++
 security/Kconfig         |  1 +
 security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++
 5 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 scripts/Makefile.initmem
 create mode 100644 security/Kconfig.initmem
Makefile +2 -1
index f070e0d65186..028ca37878fd 100644
@@ -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
new file mode 100644
index 000000000000..a6253d78fe35
@@ -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
scripts/Makefile.lib +6 -0
index 12b88d09c3a4..53d18fd15c79 100644
@@ -131,6 +131,12 @@ _c_flags += $(if $(patsubst n%,, \
                $(CFLAGS_UBSAN))
 endif
 
+ifeq ($(CONFIG_INIT_ALL_MEMORY),y)
+_c_flags += $(if $(patsubst n%,, \
+               $(INIT_ALL_MEMORY_$(basetarget).o)$(INIT_ALL_MEMORY)y), \
+               $(CFLAGS_INITMEM))
+endif
+
 ifeq ($(CONFIG_KCOV),y)
 _c_flags += $(if $(patsubst n%,, \
        $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
security/Kconfig +1 -0
index e4fe2f3c2c65..cc12a39424dd 100644
@@ -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 +16 -0
new file mode 100644
index 000000000000..27aec394365e
@@ -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

security/Kconfig.initmem line 26

Quoted 10 lines
security/Kconfig.initmem new file +10 -0
+       bool "Initialize all stack"
+       depends on INIT_ALL_MEMORY
+       depends on CC_HAS_AUTO_VAR_INIT || HAVE_GCC_PLUGINS
+       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).

Ugh, the logic is getting complicated.

When I enabled INIT_ALL_MEMORY, I saw this:

WARNING: unmet direct dependencies detected for GCC_PLUGINS
Depends on [n]: HAVE_GCC_PLUGINS [=y] && PLUGIN_HOSTCC [=]!= Selected by [y]:
- INIT_ALL_STACK [=y] && INIT_ALL_MEMORY [=y] &&
(CC_HAS_AUTO_VAR_INIT [=n] || HAVE_GCC_PLUGINS [=y]) && !CC_HAS_AUTO_VAR_INIT [=n]

Quoted 5 lines
security/Kconfig.initmem new file +2 -1
+endif # INIT_ALL_MEMORY
+endmenu
--

2.21.0.360.g471c308f928-goog

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw

Re: [PATCH v2 1/2] initmem: introduce CONFIG_INIT_ALL_MEMORY and CONFIG_INIT_ALL_STACK

Quoted 25 lines · Masahiro Yamada
Quoted 23 lines · Alexander Potapenko

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

(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 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.

Quoted 8 lines
Quoted 5 lines

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-file (by using the INIT_ALL_MEMORY_ Makefile prefix)

Do you have a plan to use this per-file prefix? If so, could you elaborate which parts should opt out?

I've added that when I started experimenting with auto-initialization, but so far I haven't encountered a use-case for that, especially since there's __attribute__((uninitialized)).
I'd better remove this part for now till we actually need it.

Quoted 125 lines
Quoted 125 lines

or per-variable
(by using __attribute__((uninitialized))) basis.

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.

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

---
 Makefile                 |  3 ++-
 scripts/Makefile.initmem | 10 ++++++++++
 scripts/Makefile.lib     |  6 ++++++
 security/Kconfig         |  1 +
 security/Kconfig.initmem | 29 +++++++++++++++++++++++++++++
 5 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 scripts/Makefile.initmem
 create mode 100644 security/Kconfig.initmem
Makefile +2 -1
index f070e0d65186..028ca37878fd 100644
@@ -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
new file mode 100644
index 000000000000..a6253d78fe35
@@ -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
scripts/Makefile.lib +6 -0
index 12b88d09c3a4..53d18fd15c79 100644
@@ -131,6 +131,12 @@ _c_flags += $(if $(patsubst n%,, \
                $(CFLAGS_UBSAN))
 endif
 
+ifeq ($(CONFIG_INIT_ALL_MEMORY),y)
+_c_flags += $(if $(patsubst n%,, \
+               $(INIT_ALL_MEMORY_$(basetarget).o)$(INIT_ALL_MEMORY)y), \
+               $(CFLAGS_INITMEM))
+endif
+
 ifeq ($(CONFIG_KCOV),y)
 _c_flags += $(if $(patsubst n%,, \
        $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
security/Kconfig +1 -0
index e4fe2f3c2c65..cc12a39424dd 100644
@@ -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 +20 -0
new file mode 100644
index 000000000000..27aec394365e
@@ -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
+       select GCC_PLUGINS if !CC_HAS_AUTO_VAR_INIT
Quoted 8 lines

security/Kconfig.initmem line 26

Quoted 6 lines
security/Kconfig.initmem new file +6 -0
+       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).

Ugh, the logic is getting complicated.

Not sure what's the best strategy here.
GCC provides only the 0x00 initialization.
Clang provides both, but 0x00 is hidden behind the -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang flag, which may be gone at some point.
Overall, initializing locals with 0xAA pattern is better, as it is more likely to cause early detectable crashes when using those values.

Quoted 9 lines

When I enabled INIT_ALL_MEMORY, I saw this:

WARNING: unmet direct dependencies detected for GCC_PLUGINS
Depends on [n]: HAVE_GCC_PLUGINS [=y] && PLUGIN_HOSTCC [=]!= Selected by [y]:
- INIT_ALL_STACK [=y] && INIT_ALL_MEMORY [=y] &&
(CC_HAS_AUTO_VAR_INIT [=n] || HAVE_GCC_PLUGINS [=y]) && !CC_HAS_AUTO_VAR_INIT [=n]

Yes, we need to check for PLUGIN_HOSTCC as well. I'll update the patch. FWIW you need gcc-*-plugin-dev installed in order to use these plugins.

Quoted 13 lines
Quoted 8 lines
security/Kconfig.initmem new file +2 -1
+endif # INIT_ALL_MEMORY
+endmenu
--

2.21.0.360.g471c308f928-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
#6 lore raw

PATCH v2 2/2 initmem: introduce CONFIG_INIT_ALL_HEAP

In reply to Alexander Potapenko

This 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.

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]>
---
 mm/page_poison.c         |  5 +++++
 mm/slub.c                |  2 ++
 security/Kconfig.initmem | 11 +++++++++++
 3 files changed, 18 insertions(+)
mm/page_poison.c +5 -0
index 21d4f97cb49b..a1985f33f635 100644
@@ -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
index 1b08fbcb7e61..00e0197d3f35 100644
@@ -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
index 27aec394365e..5ce49663777a 100644
@@ -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 CONFIG_PAGE_POISONING
+	select CONFIG_PAGE_POISONING_NO_SANITY
+	select CONFIG_PAGE_POISONING_ZERO
+	select CONFIG_SLUB_DEBUG
+	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.360.g471c308f928-goog
#7 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

Quoted 6 lines · Alexander Potapenko
security/Kconfig.initmem +0 -0
index 27aec394365e..5ce49663777a 100644

security/Kconfig.initmem config INIT_ALL_MEMORY · line 22

Quoted 10 lines
security/Kconfig.initmem +7 -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 CONFIG_PAGE_POISONING
+       select CONFIG_PAGE_POISONING_NO_SANITY
+       select CONFIG_PAGE_POISONING_ZERO
+       select CONFIG_SLUB_DEBUG

This should like follows (no CONFIG_ prefix):

select PAGE_POISONING
select PAGE_POISONING_NO_SANITY
select PAGE_POISONING_ZERO
select SLUB_DEBUG

But, again, this causes unmet dependency if SLUB=n

Quoted 10 lines
security/Kconfig.initmem +4 -1
+       default y
+       help
+         Enable page poisoning and slub poisoning by default.
+
 config INIT_ALL_STACK
        bool "Initialize all stack"
        depends on INIT_ALL_MEMORY
--

2.21.0.360.g471c308f928-goog

--
Best Regards
Masahiro Yamada

#8 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

Quoted 21 lines · Masahiro Yamada
Quoted 6 lines · Alexander Potapenko
security/Kconfig.initmem +0 -0
index 27aec394365e..5ce49663777a 100644

security/Kconfig.initmem config INIT_ALL_MEMORY · line 22

Quoted 10 lines
security/Kconfig.initmem +7 -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 CONFIG_PAGE_POISONING
+       select CONFIG_PAGE_POISONING_NO_SANITY
+       select CONFIG_PAGE_POISONING_ZERO
+       select CONFIG_SLUB_DEBUG

This should like follows (no CONFIG_ prefix):

Quoted 4 lines

select PAGE_POISONING
select PAGE_POISONING_NO_SANITY
select PAGE_POISONING_ZERO
select SLUB_DEBUG

Thanks!

Quoted 1 line

But, again, this causes unmet dependency if SLUB=n

select SLUB_DEBUG if SLUB
seems to help. Guess it's better than making CONFIG_INIT_ALL_HEAP depend on SLUB.

Quoted 19 lines
Quoted 12 lines
security/Kconfig.initmem +4 -1
+       default y
+       help
+         Enable page poisoning and slub poisoning by default.
+
 config INIT_ALL_STACK
        bool "Initialize all stack"
        depends on INIT_ALL_MEMORY
--
Quoted 2 lines

2.21.0.360.g471c308f928-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
#9 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

In reply to Alexander Potapenko
Quoted 48 lines · Alexander Potapenko

This 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.

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]>
---
  mm/page_poison.c         |  5 +++++
  mm/slub.c                |  2 ++
  security/Kconfig.initmem | 11 +++++++++++
  3 files changed, 18 insertions(+)
mm/page_poison.c +5 -0
index 21d4f97cb49b..a1985f33f635 100644
@@ -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
index 1b08fbcb7e61..00e0197d3f35 100644

mm/slub.c static int __init setup_slub_debug(char *str) · line 1294

Quoted 8 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've looked at doing something similar in the past (failing to find the thread this morning...) and while this will work, it has pretty serious performance issues. It's not actually the poisoning which is expensive but that turning on debugging removes the cpu slab which has significant performance penalties.

I'd rather go back to the proposal of just poisoning the slab at alloc/free without using SLAB_POISON.

Thanks,
Laura

Quoted 23 lines
security/Kconfig.initmem +11 -0
index 27aec394365e..5ce49663777a 100644
@@ -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 CONFIG_PAGE_POISONING
+	select CONFIG_PAGE_POISONING_NO_SANITY
+	select CONFIG_PAGE_POISONING_ZERO
+	select CONFIG_SLUB_DEBUG
+	default y
+	help
+	  Enable page poisoning and slub poisoning by default.
+
  config INIT_ALL_STACK
  	bool "Initialize all stack"
  	depends on INIT_ALL_MEMORY
#10 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

Quoted 8 lines · Laura Abbott

I've looked at doing something similar in the past (failing to find the thread this morning...) and while this will work, it has pretty serious performance issues. It's not actually the poisoning which is expensive but that turning on debugging removes the cpu slab which has significant performance penalties.

I'd rather go back to the proposal of just poisoning the slab at alloc/free without using SLAB_POISON.

I still agree this would make the most sense. Fundamentally it's not a debugging feature.

Signature · 1 line
Kees Cook
#11 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

Quoted 8 lines · Kees Cook
Quoted 6 lines · Laura Abbott

I've looked at doing something similar in the past (failing to find the thread this morning...) and while this will work, it has pretty serious performance issues. It's not actually the poisoning which is expensive but that turning on debugging removes the cpu slab which has significant performance penalties.

Quoted 5 lines
Quoted 2 lines

I'd rather go back to the proposal of just poisoning the slab at alloc/free without using SLAB_POISON.

I still agree this would make the most sense. Fundamentally it's not a debugging feature.

Wasn't it you who suggested using SLAB_POISON in the first round of comments? :)

I actually have a working implementation that piggybacks on existing __GFP_ZERO code to initialize page_alloc() and SLUB allocations:
https://github.com/google/kmsan/commit/4907af529ad525378a0728435c96d3812f71e594
https://github.com/google/kmsan/commit/69618a9668bcf27700cc5da42eebf8ab50d1f56a

I'd better cook a patch based on that.

There's also some overhead when allocations are initialized twice (in page_alloc() and kmalloc()) or thrice (page_alloc(), kmalloc() and e.g. sock_alloc_send_pskb())
We can introduce another GFP flag explicitly telling the allocator to not initialize the memory chunk if we know it'll be initialized by a higher level allocator
(something along the lines of
https://github.com/google/kmsan/commit/4fc8cff79d868c29688c8a4186e504fda362f6fd)

Quoted 2 lines

--
Kees Cook

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
#12 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

Quoted 10 lines · Alexander Potapenko
Quoted 8 lines · Kees Cook
Quoted 6 lines · Laura Abbott

I've looked at doing something similar in the past (failing to find the thread this morning...) and while this will work, it has pretty serious performance issues. It's not actually the poisoning which is expensive but that turning on debugging removes the cpu slab which has significant performance penalties.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

I'd rather go back to the proposal of just poisoning the slab at alloc/free without using SLAB_POISON.

I still agree this would make the most sense. Fundamentally it's not a debugging feature.

Wasn't it you who suggested using SLAB_POISON in the first round of comments? :)

Sure, if we want to use what we have right now, that's the way to go. Optimally, I'd like to see it done the way Laura mentioned, but that's a long road to convince the heap maintainers, etc.

Quoted 6 lines

I actually have a working implementation that piggybacks on existing __GFP_ZERO code to initialize page_alloc() and SLUB allocations:
https://github.com/google/kmsan/commit/4907af529ad525378a0728435c96d3812f71e594
https://github.com/google/kmsan/commit/69618a9668bcf27700cc5da42eebf8ab50d1f56a

I'd better cook a patch based on that.

I think it's still better to zero at free (this reduces the lifetime of the data in memory and should make some use-after-tree bugs stand out more), but we'll need to do something like what you have here for doing memory tagging anyway, so we likely need both.

Quoted 8 lines

There's also some overhead when allocations are initialized twice (in page_alloc() and kmalloc()) or thrice (page_alloc(), kmalloc() and e.g. sock_alloc_send_pskb())
We can introduce another GFP flag explicitly telling the allocator to not initialize the memory chunk if we know it'll be initialized by a higher level allocator
(something along the lines of
https://github.com/google/kmsan/commit/4fc8cff79d868c29688c8a4186e504fda362f6fd)

Agreed.

Signature · 1 line
Kees Cook
#13 lore raw

Re: [PATCH v2 2/2] initmem: introduce CONFIG_INIT_ALL_HEAP

In reply to Laura Abbott
Quoted 65 lines · Laura Abbott
Quoted 48 lines · Alexander Potapenko

This 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.

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]>
---
  mm/page_poison.c         |  5 +++++
  mm/slub.c                |  2 ++
  security/Kconfig.initmem | 11 +++++++++++
  3 files changed, 18 insertions(+)
mm/page_poison.c +5 -0
index 21d4f97cb49b..a1985f33f635 100644
@@ -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
index 1b08fbcb7e61..00e0197d3f35 100644

mm/slub.c static int __init setup_slub_debug(char *str) · line 1294

Quoted 8 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've looked at doing something similar in the past (failing to find the thread this morning...) and while this will work, it has pretty serious performance issues. It's not actually the poisoning which is expensive but that turning on debugging removes the cpu slab which has significant performance penalties.

Quoted 2 lines

I'd rather go back to the proposal of just poisoning the slab at alloc/free without using SLAB_POISON.

Hi Laura,

May I wonder what were the performance numbers you were seeing? I've found this patch:
https://www.openwall.com/lists/kernel-hardening/2016/01/26/1, but that's around 100% slowdown.

Quoted 28 lines

Thanks,
Laura

Quoted 23 lines
security/Kconfig.initmem +11 -0
index 27aec394365e..5ce49663777a 100644
@@ -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 CONFIG_PAGE_POISONING
+     select CONFIG_PAGE_POISONING_NO_SANITY
+     select CONFIG_PAGE_POISONING_ZERO
+     select CONFIG_SLUB_DEBUG
+     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 · 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