Skip to contentSkip to conversation

Add the randstruct gcc plugin

PATCH In review Michael Leibowitz 3 participants 9 messages Oct 21, 2016 – Oct 28, 2016

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add the randstruct gcc plugin

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future. The UAPI checks are retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

---
 Documentation/dontdiff                        |   2 +
 arch/Kconfig                                  |  36 ++
 include/linux/compiler-gcc.h                  |   5 +
 include/linux/compiler.h                      |   8 +
 include/linux/vermagic.h                      |   9 +-
 kernel/module.c                               |  27 +
 scripts/Makefile.gcc-plugins                  |  14 +-
 scripts/gcc-plugins/.gitignore                |   1 +
 scripts/gcc-plugins/Makefile                  |   8 +
 scripts/gcc-plugins/gen-random-seed.sh        |   8 +
 scripts/gcc-plugins/randomize_layout_plugin.c | 837 ++++++++++++++++++++++++++
 11 files changed, 952 insertions(+), 3 deletions(-)
 create mode 100644 scripts/gcc-plugins/.gitignore
 create mode 100755 scripts/gcc-plugins/gen-random-seed.sh
 create mode 100644 scripts/gcc-plugins/randomize_layout_plugin.c
Documentation/dontdiff +2 -0
index 5385cba..23b8d5e 100644
@@ -206,6 +206,8 @@ r200_reg_safe.h
 r300_reg_safe.h
 r420_reg_safe.h
 r600_reg_safe.h
+randomize_layout_hash.h
+randomize_layout_seed.h
 recordmcount
 relocs
 rlim_names.h
arch/Kconfig +36 -0
index fd6e971..4fc8d51 100644
@@ -383,6 +383,42 @@ config GCC_PLUGIN_SANCOV
 	  gcc-4.5 on). It is based on the commit "Add fuzzing coverage support"
 	  by Dmitry Vyukov <[email protected]>.
 
+config GCC_PLUGIN_RANDSTRUCT
+	bool "Randomize layout of sensitive kernel structures"
+	depends on GCC_PLUGINS
+	select MODVERSIONS if MODULES
+	help
+	  If you say Y here, the layouts of structures explicitly
+	  marked by __randomize_layout will be randomized at
+	  compile-time.  This can introduce the requirement of an
+	  additional infoleak vulnerability for exploits targeting
+	  these structure types.
+
+	  Enabling this feature will introduce some performance impact, slightly
+	  increase memory usage, and prevent the use of forensic tools like
+	  Volatility against the system (unless the kernel source tree isn't
+	  cleaned after kernel installation).
+
+	  The seed used for compilation is located at
+	  scripts/gcc-plgins/randomize_layout_seed.h.  It remains
+	  after a make clean to allow for external modules to be
+	  compiled with the existing seed and will be removed by a
+	  make mrproper or make distclean.
+
+          Note that the implementation requires gcc 4.6.4. or newer.  You may need
+	  to install the supporting headers explicitly in addition to the normal
+	  gcc package.
+
+config GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+	bool "Use cacheline-aware structure randomization"
+	depends on GCC_PLUGIN_RANDSTRUCT
+	depends on GCC_PLUGINS
+	help
+	  If you say Y here, the RANDSTRUCT randomization will make a best effort
+	  at restricting randomization to cacheline-sized groups of elements.  It
+	  will further not randomize bitfields in structures.  This reduces the
+	  performance hit of RANDSTRUCT at the cost of weakened randomization.
+
 config HAVE_CC_STACKPROTECTOR
 	bool
 	help
include/linux/compiler-gcc.h +5 -0
index 573c5a1..9c1ac02 100644
@@ -202,6 +202,11 @@
 /* Mark a function definition as prohibited from being cloned. */
 #define __noclone	__attribute__((__noclone__, __optimize__("no-tracer")))
 
+#ifdef RANDSTRUCT_PLUGIN
+#define __randomize_layout __attribute__((randomize_layout))
+#define __no_randomize_layout __attribute__((no_randomize_layout))
+#endif
+
 #endif /* GCC_VERSION >= 40500 */
 
 #if GCC_VERSION >= 40600
include/linux/compiler.h +8 -0
index 6685698..eb789e6 100644
@@ -388,6 +388,14 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
 
 #endif /* __KERNEL__ */
 
+#ifndef __randomize_layout
+# define __randomize_layout
+#endif
+
+#ifndef __no_randomize_layout
+# define __no_randomize_layout
+#endif
+
 /*
  * From the GCC manual:
  *
include/linux/vermagic.h +8 -1
index 6f8fbcf..af6c03f 100644
@@ -24,10 +24,17 @@
 #ifndef MODULE_ARCH_VERMAGIC
 #define MODULE_ARCH_VERMAGIC ""
 #endif
+#ifdef RANDSTRUCT_PLUGIN
+#include <generated/randomize_layout_hash.h>
+#define MODULE_RANDSTRUCT_PLUGIN "RANDSTRUCT_PLUGIN_" RANDSTRUCT_HASHED_SEED
+#else
+#define MODULE_RANDSTRUCT_PLUGIN
+#endif
 
 #define VERMAGIC_STRING 						\
 	UTS_RELEASE " "							\
 	MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT 			\
 	MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS	\
-	MODULE_ARCH_VERMAGIC
+	MODULE_ARCH_VERMAGIC						\
+	MODULE_RANDSTRUCT_PLUGIN
 
kernel/module.c +27 -0
index 529efae..70b17db 100644
@@ -1299,12 +1299,31 @@ static int check_version(Elf_Shdr *sechdrs,
 		goto bad_version;
 	}
 
+#ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+	/*
+	 * avoid potentially printing jibberish on attempted load
+	 * of a module randomized with a different seed
+	 */
+	pr_warn("no symbol version for %s\n", symname);
+#else
+	pr_warn("%s: no symbol version for %s\n", mod->name, symname);
+#endif
+
 	pr_warn("%s: no symbol version for %s\n", mod->name, symname);
 	return 0;
 
 bad_version:
+#ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+	/*
+	 * avoid potentially printing jibberish on attempted load
+	 * of a module randomized with a different seed
+	 */
+	pr_warn("attempted module disagrees about version of symbol %s\n",
+	       symname);
+#else
 	pr_warn("%s: disagrees about version of symbol %s\n",
 	       mod->name, symname);
+#endif
 	return 0;
 }
 
@@ -2924,7 +2943,15 @@ static struct module *setup_load_info(struct load_info *info, int flags)
 	mod = (void *)info->sechdrs[info->index.mod].sh_addr;
 
 	if (info->index.sym == 0) {
+#ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+		/*
+		 * avoid potentially printing jibberish on attempted load
+		 * of a module randomized with a different seed
+		 */
+		pr_warn("module has no symbols (stripped?)\n");
+#else
 		pr_warn("%s: module has no symbols (stripped?)\n", mod->name);
+#endif
 		return ERR_PTR(-ENOEXEC);
 	}
 
scripts/Makefile.gcc-plugins +12 -2
index 61f0e6d..94bfffe 100644
@@ -19,9 +19,19 @@ ifdef CONFIG_GCC_PLUGINS
     endif
   endif
 
-  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+  RANDSTRUCT_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so
+  ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+    gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)		+= randomize_layout_plugin.so
 
-  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
+    GCC_PLUGIN_RANDSTRUCT_CFLAGS := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so -DRANDSTRUCT_PLUGIN
+    ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+      GCC_PLUGIN_RANDSTRUCT_CFLAGS = -fplugin-arg-randomize_layout_plugin-performance-mode
+    endif
+  endif
+
+  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) $(GCC_PLUGIN_RANDSTRUCT_CFLAGS)
+
+  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN RANDSTRUCT_PLUGIN
 
   ifneq ($(PLUGINCC),)
     # SANCOV_PLUGIN can be only in CFLAGS_KCOV because avoid duplication.
scripts/gcc-plugins/.gitignore new file +1 -0
new file mode 100644
index 0000000..de92ed9
@@ -0,0 +1 @@
+randomize_layout_seed.h
scripts/gcc-plugins/Makefile +8 -0
index 8b29dc1..44e69ed 100644
@@ -18,6 +18,14 @@ endif
 
 export HOSTLIBS
 
+$(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h
+quiet_cmd_create_randomize_layout_seed = GENSEED  $@
+cmd_create_randomize_layout_seed = \
+  $(CONFIG_SHELL) $(srctree)/$(src)/gen-random-seed.sh $@ $(objtree)/include/generated/randomize_layout_hash.h
+$(objtree)/$(obj)/randomize_layout_seed.h: FORCE
+	$(call if_changed,create_randomize_layout_seed)
+targets = randomize_layout_seed.h randomize_layout_hash.h
+
 $(HOSTLIBS)-y := $(foreach p,$(GCC_PLUGIN),$(if $(findstring /,$(p)),,$(p)))
 always := $($(HOSTLIBS)-y)
 
scripts/gcc-plugins/gen-random-seed.sh new file +8 -0
new file mode 100755
index 0000000..7514850
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+if [ ! -f "$1" ]; then
+	SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'`
+	echo "const char *randstruct_seed = \"$SEED\";" > "$1"
+	HASH=`echo -n "$SEED" | sha256sum | cut -d" " -f1 | tr -d ' \n'`
+	echo "#define RANDSTRUCT_HASHED_SEED \"$HASH\"" > "$2"
+fi
scripts/gcc-plugins/randomize_layout_plugin.c new file +837 -0

837 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw
Quoted 1 line · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile

"allows for the randomization of"... :)

Quoted 10 lines

time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future.

Quoted 5 lines

The UAPI checks are
retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

In the earlier thread, I suggested a series that looked like this:

- plugin itself (minus all-function-pointers)
- opt-in markings
- C99 changes
- out-out markings
- all-function-pointers plugin logic

It sounds like this is the first step of that idea -- what was changed to remove the auto-marking logic?

(Also, this needs extensive 80-char wrapping done... see how close you can get it to be scripts/checkpatch.pl clean...)

Thanks for the update!

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#2 lore raw
In reply to Michael Leibowitz
Quoted 4 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

How are you testing this, btw? I manually marked struct cred with __randomize_layout, and "pahole vmlinux" shows it is not randomized...

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#3 lore raw
Quoted 9 lines · Kees Cook
Quoted 4 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

How are you testing this, btw? I manually marked struct cred with __randomize_layout, and "pahole vmlinux" shows it is not randomized...

I made a module that defined several fields and printed their layout in memory. It was out of order with it marked. I haven't tried it on the cred struct. I will do so.

Cheers

Quoted 6 lines

-Kees

--
Kees Cook
Nexus Security

Signature · 1 line
Michael Leibowitz
#4 lore raw
In reply to Michael Leibowitz
Quoted 22 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future. The UAPI checks are retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

scripts/Makefile.gcc-plugins +0 -0
index 61f0e6d..94bfffe 100644
@@ -19,9 +19,19 @@ ifdef CONFIG_GCC_PLUGINS
     endif
   endif

scripts/Makefile.gcc-plugins ifdef CONFIG_GCC_PLUGINS · line 28

Quoted 10 lines
scripts/Makefile.gcc-plugins +6 -2
 
-  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+  RANDSTRUCT_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so
+  ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+    gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)         += randomize_layout_plugin.so
 
-  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
+    GCC_PLUGIN_RANDSTRUCT_CFLAGS := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so -DRANDSTRUCT_PLUGIN
+    ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+      GCC_PLUGIN_RANDSTRUCT_CFLAGS = -fplugin-arg-randomize_layout_plugin-performance-mode

Found it: this "=" should be "+=", though really the whole section should just be updated to the new style of plugin handling:

gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) +=
-fplugin-arg-randomize_layout_plugin-performance-mode

and the export of RANDSTRUCT_PLUGIN

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#5 lore raw
Quoted 38 lines · Kees Cook
Quoted 22 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future. The UAPI checks are retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

scripts/Makefile.gcc-plugins +0 -0
index 61f0e6d..94bfffe 100644
@@ -19,9 +19,19 @@ ifdef CONFIG_GCC_PLUGINS
     endif
   endif

scripts/Makefile.gcc-plugins ifdef CONFIG_GCC_PLUGINS · line 28

Quoted 10 lines
scripts/Makefile.gcc-plugins +6 -2
 
-  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+  RANDSTRUCT_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so
+  ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+    gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)         += randomize_layout_plugin.so
 
-  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
+    GCC_PLUGIN_RANDSTRUCT_CFLAGS := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so -DRANDSTRUCT_PLUGIN
+    ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+      GCC_PLUGIN_RANDSTRUCT_CFLAGS = -fplugin-arg-randomize_layout_plugin-performance-mode

Found it: this "=" should be "+=", though really the whole section should just be updated to the new style of plugin handling:

Quoted 6 lines

gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) +=
-fplugin-arg-randomize_layout_plugin-performance-mode

and the export of RANDSTRUCT_PLUGIN

Also, it looks like "pahole" (or the DWARF record) is lying. :P Printing out offsetof()s shows things clearly randomized.

For the next version: can you update to the latest version of this plugin? What you sent seems out of date compared to current grsecurity. Disabling is_pure_ops_struct() with a "return 0" looks to be sufficient to stop the automatic randomization.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#6 lore raw
Quoted 46 lines · Kees Cook
Quoted 38 lines · Kees Cook
Quoted 22 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future. The UAPI checks are retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

scripts/Makefile.gcc-plugins +0 -0
index 61f0e6d..94bfffe 100644
@@ -19,9 +19,19 @@ ifdef CONFIG_GCC_PLUGINS
     endif
   endif

scripts/Makefile.gcc-plugins ifdef CONFIG_GCC_PLUGINS · line 28

Quoted 10 lines
scripts/Makefile.gcc-plugins +6 -2
 
-  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+  RANDSTRUCT_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so
+  ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+    gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)         += randomize_layout_plugin.so
 
-  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
+    GCC_PLUGIN_RANDSTRUCT_CFLAGS := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so -DRANDSTRUCT_PLUGIN
+    ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+      GCC_PLUGIN_RANDSTRUCT_CFLAGS = -fplugin-arg-randomize_layout_plugin-performance-mode

Found it: this "=" should be "+=", though really the whole section should just be updated to the new style of plugin handling:

Quoted 6 lines

gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) +=
-fplugin-arg-randomize_layout_plugin-performance-mode

and the export of RANDSTRUCT_PLUGIN

Quoted 7 lines

Also, it looks like "pahole" (or the DWARF record) is lying. :P Printing out offsetof()s shows things clearly randomized.

For the next version: can you update to the latest version of this plugin? What you sent seems out of date compared to current grsecurity. Disabling is_pure_ops_struct() with a "return 0" looks to be sufficient to stop the automatic randomization.

I've built out a more complete series here:
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=kspp/gcc-plugin/randstruct

This includes all the initializer fixes and opt-in/opt-out markings, an updated plugin, etc.

It still needs improved comments, changelogs, and some better understanding as to why the DWARF information appears broken. If you can work from this tree, that'd be great!

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#7 lore raw

Just curious:

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=cd7686ee26b839ad5e0467ac2162b360bf1fa673

"These were found using allyesconfig on x86, arm, and arm64."

Who found them, and whose code is all that?

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=6dd5fff4a626c8db8800d76f5096f6efcd2a675f

Who decided on these, whose code is it?

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=7169767228cd6d117a0908997d9a3b38bfb6a1b9

Ditto -- did you spend the hours to figure the ARM hunk out? I only see your name there.

Show the world you can do something more than plagiarize code. This is the third time I'm having to warn you guys collectively about this. How hard is it to not plagiarize?

-Brad

Quoted 70 lines · Kees Cook
Quoted 46 lines · Kees Cook
Quoted 38 lines · Kees Cook
Quoted 22 lines · Michael Leibowitz

This plugin randomizes the layout of certain structures at compile time. This introduces two defines __randomize_layout and __no_randomize_layout. Which, in turn, tell the compiler to either try to randomize or not to randomize the struct in question.

This feature is ported over from grsecurity. The implementation is nearly identical to the original code written by the PaX Team and Spender. To make integration simpler, this version only supports explicit marking of structures. However, it retains the __no_randomize_layout support for the future. The UAPI checks are retained as well. Structures that are to be randomized are required to use the C99 designated initializer form.

scripts/Makefile.gcc-plugins +0 -0
index 61f0e6d..94bfffe 100644
@@ -19,9 +19,19 @@ ifdef CONFIG_GCC_PLUGINS
     endif
   endif

scripts/Makefile.gcc-plugins ifdef CONFIG_GCC_PLUGINS · line 28

Quoted 10 lines
scripts/Makefile.gcc-plugins +6 -2
 
-  GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
+  RANDSTRUCT_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so
+  ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT
+    gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)         += randomize_layout_plugin.so
 
-  export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR SANCOV_PLUGIN
+    GCC_PLUGIN_RANDSTRUCT_CFLAGS := -fplugin=$(objtree)/scripts/gcc-plugins/randomize_layout_plugin.so -DRANDSTRUCT_PLUGIN
+    ifdef CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
+      GCC_PLUGIN_RANDSTRUCT_CFLAGS = -fplugin-arg-randomize_layout_plugin-performance-mode

Found it: this "=" should be "+=", though really the whole section should just be updated to the new style of plugin handling:

Quoted 6 lines

gcc-plugin-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += randomize_layout_plugin.so
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) += -DRANDSTRUCT_PLUGIN
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) +=
-fplugin-arg-randomize_layout_plugin-performance-mode

and the export of RANDSTRUCT_PLUGIN

Quoted 7 lines

Also, it looks like "pahole" (or the DWARF record) is lying. :P Printing out offsetof()s shows things clearly randomized.

For the next version: can you update to the latest version of this plugin? What you sent seems out of date compared to current grsecurity. Disabling is_pure_ops_struct() with a "return 0" looks to be sufficient to stop the automatic randomization.

I've built out a more complete series here:
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=kspp/gcc-plugin/randstruct

This includes all the initializer fixes and opt-in/opt-out markings, an updated plugin, etc.

It still needs improved comments, changelogs, and some better understanding as to why the DWARF information appears broken. If you can work from this tree, that'd be great!

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#8 lore raw
Quoted 12 lines · Brad Spengler

Just curious:

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=cd7686ee26b839ad5e0467ac2162b360bf1fa673

"These were found using allyesconfig on x86, arm, and arm64."

Who found them, and whose code is all that?

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=6dd5fff4a626c8db8800d76f5096f6efcd2a675f

Who decided on these, whose code is it?

Quoted 8 lines

http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/commit/?h=kspp/gcc-plugin/randstruct&id=7169767228cd6d117a0908997d9a3b38bfb6a1b9

Ditto -- did you spend the hours to figure the ARM hunk out? I only see your name there.

Show the world you can do something more than plagiarize code. This is the third time I'm having to warn you guys collectively about this. How hard is it to not plagiarize?

I already said the commit logs needed work. I think I have a reasonable track record giving you and PaX Team credit for things -- this isn't the final version by a long shot. I wanted to give Michael something to work from before the weekend got here.

-Kees

Signature · 2 lines
Kees Cook
Nexus Security