MPXK: Intel MPX for in-kernel use
Subsystems (5)
cover MPXK: Intel MPX for in-kernel use
This patch series adds experimental Intel MPX support for in-kernel code. Intel MPX provides runtime pointer bounds checking via hardware, operating system support and compiler instrumentation[1,2,3]. Intel MPX provides new registers and instructions for pointer bounds checking. Briefly, it keeps track of pointer bounds by propagating them through stack/registers and storing them in a hardware address in-memory data-structure.
While the hardware is designed for both ring 0 and 3, current software (kernel and compiler) only support MPX in user space. This patch-set introduces MPXK, which provides a way to use MPX in kernel space. As with vanilla MPX this protection is modular, i.e. it can be enabled only for specific subsystems or even translation units. This allows MPXK to be enabled selectively on for instance only known troublesome subsystems. This patch-set in itself enable MPX only for the accompanying LKDTM tests.
Our work build on existing GCC MPX instrumentation but modifies it via a gcc-plugin to better accommodate kernel use. The main difference to vanilla MPX being that we do not use the BNDSTX and BNDLDX instructions that can cause excessive memory use. The vanilla BNDSTX+BNDLDX storage requires, on 64bit systems, a 2GB directory for addressing and several 4MB tables for the bounds data, by contrast MPXK requires no extra memory (although the instrumentation increases kernel and stack size). MPXK instead relies on existing in-kernel metadata to determine bounds when necessary; This is however not always possible, in which case MPXK cannot check bounds.
That brings us to the limitations and known problems of MPXK, some of which can potentially be mitigated in future iterations:
- Vanilla MPX loads bounds based on the linear address of pointers, whereas MPXK instead uses the pointer's value. This means that if a corrupted pointer value points into another object the load will be based on that, in this case incorrect, object. This attack however already assumes a memory attack to corrupt the pointer, and would also be missed vanilla MPX BNDLDX due to compatibility reasons.
- The bound loading is limited to kmalloc allocated pointers, this only affects situations where bounds need to be dynamically loaded. In common cases bounds are propagated via hardware registers and the stack. This is something we believe can be improved by utilizing other information available to the kernel.
- We currently do not support support MPXK in modules (which means the accompanying lkdtm tests must be compiled into the kernel).
- Providing modular support requires handling pointers originating or passed via non-protected code, such pointer are treated as non-bounded to ensure compatibility.
In addition to these fundamental issues there are several other aspects that still need work, including several potential performance improvements. At present we all still working on both PoC exploit code and performance measurements, any suggestions on meaningful measurements or appropriate exploits are happily taken.
Feedback on the general approach and details are appreciated.
Best Regards,
-hans liljestrand
[1]: https://01.org/blogs/2016/intel-mpx-linux
[2]: https://lwn.net/Articles/582712/
[3]: https://gcc.gnu.org/wiki/Intel%20MPX%20support%20in%20the%20GCC%20compiler
Hans Liljestrand (5):
x86: add CONFIG_X86_INTEL_MPX_KERNEL to Kconfig
gcc-plugins: adds MPXK gcc plugin
x86: add mpxk-wrappers
x86: MPXK base
lkdtm: Add kernel MPX testing
arch/x86/Kconfig | 19 ++++ arch/x86/include/asm/mpxk.h | 18 ++++ arch/x86/kernel/traps.c | 44 +++++++- arch/x86/lib/Makefile | 5 + arch/x86/lib/mpxk-wrappers.c | 157 +++++++++++++++++++++++++++ arch/x86/lib/mpxk.c | 69 ++++++++++++ drivers/misc/Makefile | 7 ++ drivers/misc/lkdtm.h | 7 ++ drivers/misc/lkdtm_core.c | 6 ++ drivers/misc/lkdtm_mpxk.c | 115 ++++++++++++++++++++ drivers/misc/lkdtm_mpxk.h | 11 ++ drivers/misc/lkdtm_mpxk_base.c | 65 ++++++++++++ include/asm-generic/mpxk.h | 20 ++++ init/main.c | 2 + scripts/Makefile.gcc-plugins | 17 +++ scripts/gcc-plugins/Makefile | 6 ++ scripts/gcc-plugins/mpxk.c | 171 ++++++++++++++++++++++++++++++ scripts/gcc-plugins/mpxk.h | 60 +++++++++++ scripts/gcc-plugins/mpxk_builtins.c | 102 ++++++++++++++++++ scripts/gcc-plugins/mpxk_builtins.def | 41 +++++++ scripts/gcc-plugins/mpxk_pass_bnd_store.c | 147 +++++++++++++++++++++++++ scripts/gcc-plugins/mpxk_pass_cfun_args.c | 98 +++++++++++++++++ scripts/gcc-plugins/mpxk_pass_sweeper.c | 107 +++++++++++++++++++ scripts/gcc-plugins/mpxk_pass_wrappers.c | 128 ++++++++++++++++++++++ 24 files changed, 1421 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/mpxk.h create mode 100644 arch/x86/lib/mpxk-wrappers.c create mode 100644 arch/x86/lib/mpxk.c create mode 100644 drivers/misc/lkdtm_mpxk.c create mode 100644 drivers/misc/lkdtm_mpxk.h create mode 100644 drivers/misc/lkdtm_mpxk_base.c create mode 100644 include/asm-generic/mpxk.h create mode 100644 scripts/gcc-plugins/mpxk.c create mode 100644 scripts/gcc-plugins/mpxk.h create mode 100644 scripts/gcc-plugins/mpxk_builtins.c create mode 100644 scripts/gcc-plugins/mpxk_builtins.def create mode 100644 scripts/gcc-plugins/mpxk_pass_bnd_store.c create mode 100644 scripts/gcc-plugins/mpxk_pass_cfun_args.c create mode 100644 scripts/gcc-plugins/mpxk_pass_sweeper.c create mode 100644 scripts/gcc-plugins/mpxk_pass_wrappers.c
Signature · 1 line
2.11.0
RFC PATCH 1/5 x86: add CONFIG_X86_INTEL_MPX_KERNEL to Kconfig
Add CONFIG_X86_INTEL_MPX_KERNEL for future kernel-space support for Intel MPX. Currently depends on CPU_SUP_INTEL.
--- arch/x86/Kconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/Kconfig +19 -0
@@ -1771,6 +1771,25 @@ config X86_INTEL_MPX If unsure, say N. +config X86_INTEL_MPX_KERNEL + prompt "Intel MPX for kernel" + def_bool n + depends on CPU_SUP_INTEL + select CONSTRUCTORS + select GCC_PLUGINS + ---help--- + MPX provides hardware features that can be used in + conjunction with compiler-instrumented code to check + memory references. It is designed to detect buffer + overflow or underflow bugs. + + This option enables MPXK, which is a slightly modified + MPX instrumentation for in-kernel code. This + protection is modular and even when enabled covers + only code that explicitly use this feature. + + If unsure, say N + config X86_INTEL_MEMORY_PROTECTION_KEYS prompt "Intel Memory Protection Keys" def_bool y
Signature · 1 line
2.11.0
[kernel-hardening] Re: [RFC PATCH 1/5] x86: add CONFIG_X86_INTEL_MPX_KERNEL to Kconfig
Quoted 14 lines · Hans Liljestrand
Add
CONFIG_X86_INTEL_MPX_KERNELfor future kernel-space support for Intel MPX. Currently depends onCPU_SUP_INTEL.Signed-off-by Hans Liljestrand <[email protected]>Signed-off-by Elena Reshetova <[email protected]>--- arch/x86/Kconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/Kconfig +0
-0
arch/x86/Kconfig config X86_INTEL_MPX · line 1779
Quoted 9 lines
arch/x86/Kconfig +6
-0@@ -1771,6 +1771,25 @@ config X86_INTEL_MPX If unsure, say N. +config X86_INTEL_MPX_KERNEL + prompt "Intel MPX for kernel" + def_bool n + depends on CPU_SUP_INTEL + select CONSTRUCTORS + select GCC_PLUGINS
GCC_PLUGINS should be a "depends" here, so that when we finally get compile-support-testing hooked up to Kconfig we won't get some nasty surprises.
arch/x86/Kconfig config X86_INTEL_MPX · line 1791
Quoted 12 lines
arch/x86/Kconfig +12
-0+ ---help--- + MPX provides hardware features that can be used in + conjunction with compiler-instrumented code to check + memory references. It is designed to detect buffer + overflow or underflow bugs. + + This option enables MPXK, which is a slightly modified + MPX instrumentation for in-kernel code. This + protection is modular and even when enabled covers + only code that explicitly use this feature. + + If unsure, say N
I think this Kconfig should live in whichever patch actually starts adding things (maybe patch 2?)
-Kees
Quoted 7 lines
arch/x86/Kconfig +1
-1+ config X86_INTEL_MEMORY_PROTECTION_KEYS prompt "Intel Memory Protection Keys" def_bool y --2.11.0
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 1/5] x86: add CONFIG_X86_INTEL_MPX_KERNEL to Kconfig
Quoted 19 lines · Kees Cook
Quoted 17 lines · Hans Liljestrand
Add
CONFIG_X86_INTEL_MPX_KERNELfor future kernel-space support for Intel MPX. Currently depends onCPU_SUP_INTEL.Signed-off-by Hans Liljestrand <[email protected]>Signed-off-by Elena Reshetova <[email protected]>--- arch/x86/Kconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/Kconfig +0
-0@@ -1771,6 +1771,25 @@ config X86_INTEL_MPX If unsure, say N.
Quoted 10 lines
arch/x86/Kconfig config X86_INTEL_MPX · line 1779
Quoted 6 lines
arch/x86/Kconfig +6
-0+config X86_INTEL_MPX_KERNEL + prompt "Intel MPX for kernel" + def_bool n + depends on CPU_SUP_INTEL + select CONSTRUCTORS + select GCC_PLUGINS
GCC_PLUGINSshould be a "depends" here, so that when we finally get compile-support-testing hooked up toKconfigwe won't get some nasty surprises.
Okay, sounds good. Also realized the CONSTRUCTORS thing is an old leftover, we don't use those anymore.
Quoted 16 lines
arch/x86/Kconfig config X86_INTEL_MPX · line 1791
Quoted 13 lines
arch/x86/Kconfig +12
-0+ ---help--- + MPX provides hardware features that can be used in + conjunction with compiler-instrumented code to check + memory references. It is designed to detect buffer + overflow or underflow bugs. + + This option enables MPXK, which is a slightly modified + MPX instrumentation for in-kernel code. This + protection is modular and even when enabled covers + only code that explicitly use this feature. + + If unsure, say NI think this
Kconfigshould live in whichever patch actually starts adding things (maybe patch 2?)
Ok, thanks!
-hans
Quoted 16 lines
-Kees
Quoted 7 lines
arch/x86/Kconfig +1
-1+ config X86_INTEL_MEMORY_PROTECTION_KEYS prompt "Intel Memory Protection Keys" def_bool y --2.11.0
Signature · 2 lines
Kees Cook Pixel Security
RFC PATCH 2/5 gcc-plugins: adds MPXK gcc plugin
In reply to Hans LiljestrandAdds a gcc-plugin that modifies vanilla GCC MPX instrumentation for in-kernel use. Also adds MPXK_CFLAGS Makefile variable that can be used to selectively enable MPXK for specific kernel objects. This plugin is not enabled kernel-wide and must be explicitly enabled in the appropriate Makefile.
The purpose of the plugin is to replace BNDSTX+BNDLDX bound storage with our own mpxk_load_bounds function and to replace memory mainpulating function with corresponding wrapper function. The plugin accomplishes this via the following tasks:
- It adds MPXK-wrappers functions for memory altering functions, e.g. kmalloc, memcpy, etc. This is needed both to check bounds for input argument pointers and properly set bounds for returned pointers. This is done by the mpxk_wrappers compiler pass. (This could potentially be done, for better performance, by direct instrumentation at the call site.)
- Replace BNDLDX calls with mpxk_load_bounds function calls. This includes two separate cases: free-standing loads handled by the mpxk_bnd_store pass, and loads in function prologues that are handled by the mpxk_cfun_args pass. These are needed for situation where the compile time instrumentation cannot determine a way to statically propagate the bounds via the stack or registers.
- The final mpxk_pass_sweeper removes any remaining BNDLDX/BNDSTX calls.
--- scripts/Makefile.gcc-plugins | 17 +++ scripts/gcc-plugins/Makefile | 6 ++ scripts/gcc-plugins/mpxk.c | 171 ++++++++++++++++++++++++++++++ scripts/gcc-plugins/mpxk.h | 60 +++++++++++ scripts/gcc-plugins/mpxk_builtins.c | 102 ++++++++++++++++++ scripts/gcc-plugins/mpxk_builtins.def | 41 +++++++ scripts/gcc-plugins/mpxk_pass_bnd_store.c | 147 +++++++++++++++++++++++++ scripts/gcc-plugins/mpxk_pass_cfun_args.c | 98 +++++++++++++++++ scripts/gcc-plugins/mpxk_pass_sweeper.c | 107 +++++++++++++++++++ scripts/gcc-plugins/mpxk_pass_wrappers.c | 128 ++++++++++++++++++++++ 10 files changed, 877 insertions(+) create mode 100644 scripts/gcc-plugins/mpxk.c create mode 100644 scripts/gcc-plugins/mpxk.h create mode 100644 scripts/gcc-plugins/mpxk_builtins.c create mode 100644 scripts/gcc-plugins/mpxk_builtins.def create mode 100644 scripts/gcc-plugins/mpxk_pass_bnd_store.c create mode 100644 scripts/gcc-plugins/mpxk_pass_cfun_args.c create mode 100644 scripts/gcc-plugins/mpxk_pass_sweeper.c create mode 100644 scripts/gcc-plugins/mpxk_pass_wrappers.c
scripts/Makefile.gcc-plugins +17 -0
@@ -29,7 +29,24 @@ ifdef CONFIG_GCC_PLUGINS gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK_VERBOSE) += -fplugin-arg-structleak_plugin-verbose gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STRUCTLEAK) += -DSTRUCTLEAK_PLUGIN + ifdef CONFIG_X86_INTEL_MPX_KERNEL + MPXK_PLUGIN := -fplugin=$(objtree)/scripts/gcc-plugins/mpxk.so + gcc-plugin-$(CONFIG_X86_INTEL_MPX_KERNEL) += mpxk.so + + MPXK_CFLAGS := -mmpx -fcheck-pointer-bounds $(MPXK_PLUGIN) + MPXK_CFLAGS += -fno-chkp-store-bounds + MPXK_CFLAGS += -fno-chkp-use-wrappers + + MPXK_LIB_CFLAGS := $(MPXK_CFLAGS) + MPXK_LIB_CFLAGS += -fno-chkp-narrow-bounds + MPXK_LIB_CFLAGS += -fno-chkp-check-read + MPXK_LIB_CFLAGS += -fno-chkp-check-write + + export MPXK_PLUGIN MPXK_LIB_CFLAGS MPXK_CFLAGS + endif + GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y)) + GCC_PLUGINS_CFLAGS := $(filter-out $(MPXK_PLUGIN), $(GCC_PLUGINS_CFLAGS)) export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR export SANCOV_PLUGIN DISABLE_LATENT_ENTROPY_PLUGIN
scripts/gcc-plugins/Makefile +6 -0
@@ -23,6 +23,12 @@ always := $($(HOSTLIBS)-y) $(foreach p,$($(HOSTLIBS)-y:%.so=%),$(eval $(p)-objs := $(p).o)) +mpxk-objs += mpxk_builtins.o +mpxk-objs += mpxk_pass_wrappers.o +mpxk-objs += mpxk_pass_bnd_store.o +mpxk-objs += mpxk_pass_cfun_args.o +mpxk-objs += mpxk_pass_sweeper.o + subdir-y := $(GCC_PLUGIN_SUBDIR) subdir- += $(GCC_PLUGIN_SUBDIR)
scripts/gcc-plugins/mpxk.c new file +171 -0
171 lines not on this page · raw message
scripts/gcc-plugins/mpxk.h new file +60 -0
60 lines not on this page · raw message
scripts/gcc-plugins/mpxk_builtins.c new file +102 -0
102 lines not on this page · raw message
scripts/gcc-plugins/mpxk_builtins.def new file +41 -0
41 lines not on this page · raw message
scripts/gcc-plugins/mpxk_pass_bnd_store.c new file +147 -0
147 lines not on this page · raw message
scripts/gcc-plugins/mpxk_pass_cfun_args.c new file +98 -0
98 lines not on this page · raw message
scripts/gcc-plugins/mpxk_pass_sweeper.c new file +107 -0
107 lines not on this page · raw message
scripts/gcc-plugins/mpxk_pass_wrappers.c new file +128 -0
128 lines not on this page · raw message
Signature · 1 line
2.11.0
[kernel-hardening] Re: [RFC PATCH 2/5] gcc-plugins: adds MPXK gcc plugin
Quoted 18 lines · Hans Liljestrand
Adds a gcc-plugin that modifies vanilla GCC MPX instrumentation for in-kernel use. Also adds
MPXK_CFLAGSMakefilevariable that can be used to selectively enable MPXK for specific kernel objects. This plugin is not enabled kernel-wide and must be explicitly enabled in the appropriateMakefile.The purpose of the plugin is to replace BNDSTX+BNDLDX bound storage with our own
mpxk_load_boundsfunction and to replace memory mainpulating function with corresponding wrapper function. The plugin accomplishes this via the following tasks:- It adds MPXK-wrappers functions for memory altering functions, e.g. kmalloc, memcpy, etc. This is needed both to check bounds for input argument pointers and properly set bounds for returned pointers. This is done by the
mpxk_wrapperscompiler pass. (This could potentially be done, for better performance, by direct instrumentation at the call site.)
Quoted 8 lines
- Replace BNDLDX calls with
mpxk_load_boundsfunction calls. This includes two separate cases: free-standing loads handled by thempxk_bnd_storepass, and loads in function prologues that are handled by thempxk_cfun_argspass. These are needed for situation where the compile time instrumentation cannot determine a way to statically propagate the bounds via the stack or registers.- The final
mpxk_pass_sweeperremoves any remaining BNDLDX/BNDSTX calls.
Since this creates multiple files for a single plugin, can you instead relocate these into a subdirectory (which should be supported by the build system already).
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 2/5] gcc-plugins: adds MPXK gcc plugin
Quoted 27 lines · Kees Cook
Quoted 25 lines · Hans Liljestrand
Adds a gcc-plugin that modifies vanilla GCC MPX instrumentation for in-kernel use. Also adds
MPXK_CFLAGSMakefilevariable that can be used to selectively enable MPXK for specific kernel objects. This plugin is not enabled kernel-wide and must be explicitly enabled in the appropriateMakefile.The purpose of the plugin is to replace BNDSTX+BNDLDX bound storage with our own
mpxk_load_boundsfunction and to replace memory mainpulating function with corresponding wrapper function. The plugin accomplishes this via the following tasks:- It adds MPXK-wrappers functions for memory altering functions, e.g. kmalloc, memcpy, etc. This is needed both to check bounds for input argument pointers and properly set bounds for returned pointers. This is done by the
mpxk_wrapperscompiler pass. (This could potentially be done, for better performance, by direct instrumentation at the call site.)- Replace BNDLDX calls with
mpxk_load_boundsfunction calls. This includes two separate cases: free-standing loads handled by thempxk_bnd_storepass, and loads in function prologues that are handled by thempxk_cfun_argspass. These are needed for situation where the compile time instrumentation cannot determine a way to statically propagate the bounds via the stack or registers.
Quoted 5 lines
Quoted 1 line
- The final
mpxk_pass_sweeperremoves any remaining BNDLDX/BNDSTX calls.Since this creates multiple files for a single plugin, can you instead relocate these into a subdirectory (which should be supported by the build system already).
Thanks. I was a bit unsure how to organize these in the first place, but a subdirectory sounds like a good idea. I'll also look at consolidating the non-pass files into one.
Regards,
-hans
Quoted 6 lines
-Kees
Signature · 2 lines
Kees Cook Pixel Security
RFC PATCH 3/5 x86: add mpxk-wrappers
In reply to Hans LiljestrandThis adds actual implementation for mpxk-wrapper functions. The wrapper function are used by the instrumentation to update and check pointer bounds on functions that alter memory, e.g. kmalloc and memcpy. The kmalloc wrapper function for instance simply executes kmalloc, associates bounds with the returned pointer, and returns both. Other wrapper functions, such as for memcpy, also check the bounds of incoming arguments.
For future work these wrappers could potentially be replaced by direct instrumentation without the need to incur the cost of calling the wrapper function. In this scenario every kmalloc would simply be preceded by an appropriate mkbnd instruction, and memcpy preceded by bndcu+bndcl instructions.
The wrappers are added by the MPXK gcc-plugin, and as such work on preprocessed code. This introduces another problem with our implementation since macros might actually be used to direct "base functions" into specific implementations (e.g. memcpy might be a macro pointing to __memcpy). One solution is covering all possibilities, but this might introduce unwanted code bloat.
--- arch/x86/lib/mpxk-wrappers.c | 157 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 arch/x86/lib/mpxk-wrappers.c
arch/x86/lib/mpxk-wrappers.c new file +157 -0
@@ -0,0 +1,157 @@ +/* +* arch/x86/lib/mpxk-wrappers.h +* +* Copyright (C) 2017 Aalto University +*/ +#include <linux/page-flags.h> +#include <linux/vmalloc.h> +#include <linux/slab.h> +#include <linux/mm.h> +#include <asm/page.h> +#include <asm/pgtable_64.h> + +#define MPXK_BUILTIN_DEF (r, f, ...) extern r f(__VA_ARGS__); +#define MPXK_WRAPPER_DEF (r, f, ...) extern r mpxk_wrapper_##f(__VA_ARGS__); + +#include "../../../../scripts/gcc-plugins/mpxk_builtins.def" + +#undef MPXK_WRAPPER_DEF +#undef MPXK_BUILTIN_DEF + +void *mpxk_wrapper_kmalloc (size_t s,gfp_t f) +{ +void *p =kmalloc (s, f); + +if (p) +return __bnd_set_ptr_bounds (p, s); +return __bnd_null_ptr_bounds (p); +} + +void *mpxk_wrapper_krealloc (void *p,size_t s,gfp_t f) +{ +if (!p) +return mpxk_wrapper_kmalloc (s, f); + +__bnd_chk_ptr_lbounds (p); + p =krealloc (p, s, f); + +if (p) +return __bnd_set_ptr_bounds (p, s); +return __bnd_null_ptr_bounds (p); +} + +void *mpxk_wrapper_memmove (void *d,const void *s,size_t c) +{ +if (c ==0 ) +return d; + +__bnd_chk_ptr_bounds (d, c); +__bnd_chk_ptr_bounds (s, c); + +return memmove (d, s, c); +} + +void *mpxk_wrapper_memcpy (void *d,const void *s,size_t c) +{ +if (c ==0 ) +return d; + +__bnd_chk_ptr_bounds (d, c); +__bnd_chk_ptr_bounds (s, c); + +return memcpy (d, s, c); +} + +/* Because the MPXK gcc-plugin works on preprocessed code it cannot properly +* handle macro expanded calls such as potential memcpy -> __memcpy changes. +* This probably needs to be solved in some cleaner way, and probably also +* affects other function besides memcpy. +*/ +void *mpxk_wrapper___inline_memcpy (void *d,const void *s,size_t c) +{ +if (c ==0 ) +return d; + +__bnd_chk_ptr_bounds (d, c); +__bnd_chk_ptr_bounds (s, c); + +return __inline_memcpy (d, s, c); +} + +void *mpxk_wrapper___memcpy (void *d,const void *s,size_t c) +{ +if (c ==0 ) +return d; + +__bnd_chk_ptr_bounds (d, c); +__bnd_chk_ptr_bounds (s, c); + +return __memcpy (d, s, c); +} + +void *mpxk_wrapper_memset (void *s,int c,size_t l) +{ +if (l >0 ) { +__bnd_chk_ptr_bounds (s, l); +memset (s, c, l); + } +return s; +} + +char *mpxk_wrapper_strcat (char *d,const char *s) +{ +size_t ds =mpxk_wrapper_strlen (d); +size_t ss =mpxk_wrapper_strlen (s); + +__bnd_chk_ptr_bounds (d, ds + ss +1 ); +__bnd_chk_ptr_bounds (s, ss +1 ); + +return strcat (d, s); +} + +char *mpxk_wrapper_strncat (char *d,const char *s,size_t c) +{ +size_t ds =mpxk_wrapper_strlen (d); +size_t ss =mpxk_wrapper_strnlen (s, c); + +__bnd_chk_ptr_bounds (d, ds + ss +1 ); +__bnd_chk_ptr_bounds (s, (ss < c ? ss +1 : ss)); + +return strncat (d, s, c); +} + +char *mpxk_wrapper_strcpy (char *d,const char *s) +{ +size_t ss =mpxk_wrapper_strlen (s); + +__bnd_chk_ptr_bounds (d, ss +1 ); +__bnd_chk_ptr_bounds (s, ss +1 ); + +return strcpy (d, s); +} + +char *mpxk_wrapper_strncpy (char *d,const char *s,size_t c) +{ +size_t ss =strnlen (s, c); + +__bnd_chk_ptr_bounds (d, c); +__bnd_chk_ptr_bounds (s, (ss < c ? ss +1 : ss)); + +return strncpy (d, s, c); +} + +size_t mpxk_wrapper_strlen (const char *s) +{ +const size_t l =strlen (s); + +__bnd_chk_ptr_bounds (s, l +1 ); +return l; +} + +size_t mpxk_wrapper_strnlen (const char *s,size_t c) +{ +const size_t l =strnlen (s, c); + +__bnd_chk_ptr_bounds (s, l +1 ); +return l; +}
Signature · 1 line
2.11.0
[kernel-hardening] Re: [RFC PATCH 3/5] x86: add mpxk-wrappers
Quoted 13 lines · Hans Liljestrand
This adds actual implementation for mpxk-wrapper functions. The wrapper function are used by the instrumentation to update and check pointer bounds on functions that alter memory, e.g. kmalloc and memcpy. The kmalloc wrapper function for instance simply executes kmalloc, associates bounds with the returned pointer, and returns both. Other wrapper functions, such as for memcpy, also check the bounds of incoming arguments.
For future work these wrappers could potentially be replaced by direct instrumentation without the need to incur the cost of calling the wrapper function. In this scenario every kmalloc would simply be preceded by an appropriate mkbnd instruction, and memcpy preceded by bndcu+bndcl instructions.
Quoted 6 lines
The wrappers are added by the MPXK gcc-plugin, and as such work on preprocessed code. This introduces another problem with our implementation since macros might actually be used to direct "base functions" into specific implementations (e.g. memcpy might be a macro pointing to
__memcpy). One solution is covering all possibilities, but this might introduce unwanted code bloat.
I'd be curious to see how (if?) this interacts with CONFIG_FORTIFY. It seems that currently the MPXK checks would be similar to the inter-object checks as they exist (e.g. checking the size of the whole kmalloc allocation), but it wouldn't work on intra-object copies (i.e. bounds checking a field within an object).
More directly, I'd be curious to see coverage and performance comparisons between FORTIFY and MPXK.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 3/5] x86: add mpxk-wrappers
Quoted 22 lines · Kees Cook
Quoted 13 lines · Hans Liljestrand
This adds actual implementation for mpxk-wrapper functions. The wrapper function are used by the instrumentation to update and check pointer bounds on functions that alter memory, e.g. kmalloc and memcpy. The kmalloc wrapper function for instance simply executes kmalloc, associates bounds with the returned pointer, and returns both. Other wrapper functions, such as for memcpy, also check the bounds of incoming arguments.
For future work these wrappers could potentially be replaced by direct instrumentation without the need to incur the cost of calling the wrapper function. In this scenario every kmalloc would simply be preceded by an appropriate mkbnd instruction, and memcpy preceded by bndcu+bndcl instructions.
Quoted 6 lines
The wrappers are added by the MPXK gcc-plugin, and as such work on preprocessed code. This introduces another problem with our implementation since macros might actually be used to direct "base functions" into specific implementations (e.g. memcpy might be a macro pointing to
__memcpy). One solution is covering all possibilities, but this might introduce unwanted code bloat.
Quoted 5 lines
I'd be curious to see how (if?) this interacts with
CONFIG_FORTIFY. It seems that currently the MPXK checks would be similar to the inter-object checks as they exist (e.g. checking the size of the whole kmalloc allocation), but it wouldn't work on intra-object copies (i.e. bounds checking a field within an object).
Do you mean the FORTIFY_SOURCE thing (could not find anything directly on CONFIG_FORTIFY). Based on a quick look FORTIFY_SOURCE is localized, and essentially just provides the equivalent os our "wrappers", but I could be wildly off?
MPXK on the other hand also checks other pointer dereferences and propagates pointer bounds into non-wrapper functions. The wrappers themselves also do not load any bounds, instead the calling function passes in the pointer bounds via the bound registers (although the caller might, depending on the situation have loaded the bounds based on the kmalloc allocation).
Regarding inter-objects thing MPXK is a bit fuzzy, which is one of the weaknesses of our approach in general. The mpxk_load_bounds functions uses the whole kmalloc allocation, but MPX also uses narrowing, which (in some cases) narrows pointer bounds to intra-object pointers (excluding first fields and pointers into arrays, which all retain the bounds of the whole structure). The narrowing is provided by vanilla GCC MPX instrumentation, not specific to our MPXK code. As a guarantee we therefore cannot say more than that we enforce the whole kmalloc area (provided kmalloc was used).
Quoted 3 lines
More directly, I'd be curious to see coverage and performance comparisons between FORTIFY and MPXK.
Based on the above we have seen KASAN as the more direct comparison, or would you disagree? In the any case, we would also like to get some actual comparisons done.
Can you recommend any "standard" way of doing measurements, either coverage or performance, for this kind of protections?
Thanks,
-hans
Quoted 6 lines
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 3/5] x86: add mpxk-wrappers
Quoted 4 lines · Hans Liljestrand
Do you mean the
FORTIFY_SOURCEthing (could not find anything directly onCONFIG_FORTIFY). Based on a quick lookFORTIFY_SOURCEis localized, and essentially just provides the equivalent os our "wrappers", but I could be wildly off?
Yup, I mean this:
https://git.kernel.org/linus/6974f0c4555e285ab217cee58b6e874f776ff409
Quoted 5 lines
MPXK on the other hand also checks other pointer dereferences and propagates pointer bounds into non-wrapper functions. The wrappers themselves also do not load any bounds, instead the calling function passes in the pointer bounds via the bound registers (although the caller might, depending on the situation have loaded the bounds based on the kmalloc allocation).
What are the code patterns that MPXK protects? It sounds like I'm not quite seeing what MPXK provides beyond the CONFIG_FORTIFY_SOURCE checks (both need to know the bounds already...)
Quoted 2 lines
Can you recommend any "standard" way of doing measurements, either coverage or performance, for this kind of protections?
I say, for build coverage (possibly via some compile-time warning), how many, e.g., memcpy()s are protected (i.e. bounds are known). And then similarly, during runtime (possibly via kprobe) how many memcpy()s are protected?
For performance, it's trickier, and needs a meaningful workload. I've used hackbench and kernel build times...
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 3/5] x86: add mpxk-wrappers
Quoted 10 lines · Kees Cook
Quoted 4 lines · Hans Liljestrand
Do you mean the
FORTIFY_SOURCEthing (could not find anything directly onCONFIG_FORTIFY). Based on a quick lookFORTIFY_SOURCEis localized, and essentially just provides the equivalent os our "wrappers", but I could be wildly off?Yup, I mean this:
https://git.kernel.org/linus/6974f0c4555e285ab217cee58b6e874f776ff409
Okay, thanks!
Quoted 7 lines
Quoted 6 lines
MPXK on the other hand also checks other pointer dereferences and propagates pointer bounds into non-wrapper functions. The wrappers themselves also do not load any bounds, instead the calling function passes in the pointer bounds via the bound registers (although the caller might, depending on the situation have loaded the bounds based on the kmalloc allocation).
Quoted 5 lines
What are the code patterns that MPXK protects? It sounds like I'm not quite seeing what MPXK provides beyond the
CONFIG_FORTIFY_SOURCEchecks (both need to know the bounds already...)From what I can tell FORTIFY needs to know the during compile time and is only
applied on specific function. This is not the case with MPXK, which works more like KASAN in that it enforces runtime bounds for any pointer access.
As a concrete example of the runtime instrumentation:
noinline void try_write(char *ptr, int i)
{
ptr[i] = '\0';
}becomes (where the bnd0 is set by the caller along with function args):
0000000000000000 <try_write>:
0: e8 00 00 00 00 callq 5 <try_write+0x5> 5: 4c 8d 54 24 08 lea 0x8(%rsp),%r10 a: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp e: 48 63 f6 movslq %esi,%rsi 11: 48 01 f7 add %rsi,%rdi 14: 41 ff 72 f8 pushq -0x8(%r10) 18: 55 push %rbp 19: 48 89 e5 mov %rsp,%rbp 1c: 41 52 push %r10 1e: 48 83 ec 08 sub $0x8,%rsp 22: f3 0f 1a 07 bndcl (%rdi),%bnd0 # <- bound checks 26: f2 0f 1a 07 bndcu (%rdi),%bnd0 # <- bound checks 2a: c6 07 00 movb $0x0,(%rdi) 2d: 48 83 c4 08 add $0x8,%rsp 31: 41 5a pop %r10 33: 5d pop %rbp 34: 49 8d 62 f8 lea -0x8(%r10),%rsp 38: f2 c3 bnd retq 3a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
This applies to any MPXK compiled function and pointer dereference, not only wrappers. The caller of course needs to know (at runtime) the bounds, which were set by the instrumentation either compile-time or runtime, potentially in the kmalloc wrapper (based on the size argument and returned via the bnd0 register). The memcpy & friends are wrapped so the checks can be done explicitly before letting the functions do their thing. The wrappers serve, IIUC, a similar purpose to the KASAN's direct modifications to the same functions.
KASAN uses shadow memory to keep track of valid memory regions and then checks pointer dereferences against that. MPXK by contrast tracks the bounds along in-memory (either on stack or in .data section) or in MPX hardware bound registers (using regular MPX GCC instrumentation). This is not possible when pointers are stashed away in a shared array for example, so in those cases the bounds are stored elsewhere. Vanilla MPX uses a separate data structure for this, whereas MPXK attempts to determine bounds based on the kmalloc allocation area without explicit bound stores, shadow memory or fat-pointers.
The main difference to KASAN/vanilla-MPX is that MPXK does not use any extra memory and we therefore hope MPXK will be more feasible to actually use runtime. In support of that idea MPXK is also designed to be modular, i.e. you can just apply it on specific parts of the code (hence also wrappers instead of KASAN-like direct changes to memcpy, etc implementations).
Quoted 8 lines
Quoted 3 lines
Can you recommend any "standard" way of doing measurements, either coverage or performance, for this kind of protections?
I say, for build coverage (possibly via some compile-time warning), how many, e.g., memcpy()s are protected (i.e. bounds are known). And then similarly, during runtime (possibly via kprobe) how many memcpy()s are protected?
Yes, this sounds good. Although it would not cover non-wrapped pointer dereferences. None the less, this should give us an estimate of how much our bound load limitations actually affects MPXK coverage. When considering only these functions I think our worst case should be similar to what CONFIG_FORTIFY already does.
Thanks for the suggestion!
Quoted 3 lines
For performance, it's trickier, and needs a meaningful workload. I've used hackbench and kernel build times...
The meaningful workload thing is further complicated because we haven't envisioned this for kernel-wide deployment. But I guess we will have to do something like this to get comparable numbers. I will have to take a look at hackbench.
Thanks,
-hans
Quoted 6 lines
-Kees
Signature · 2 lines
Kees Cook Pixel Security
RFC PATCH 4/5 x86: MPXK base
In reply to Hans LiljestrandEnable and add needed support functionality for ring 0 MPX. MPXK is enabled in init/main.c by setting the BNDCFGS MSR registers. This also includes the mpxk_load_bounds implementation and error handling code for MPX errors, i.e. bound violations.
--- arch/x86/include/asm/mpxk.h | 18 ++++++++++++ arch/x86/kernel/traps.c | 44 ++++++++++++++++++++++++++++- arch/x86/lib/Makefile | 5 ++++ arch/x86/lib/mpxk.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ include/asm-generic/mpxk.h | 20 +++++++++++++ init/main.c | 2 ++ 6 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/mpxk.h create mode 100644 arch/x86/lib/mpxk.c create mode 100644 include/asm-generic/mpxk.h
arch/x86/include/asm/mpxk.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* arch/x86/include/asm/mpxk.h +* +* Copyright (C) 2017 Aalto University +*/ +#ifndef _X86_INCLUDE_ASM_MPXK_H_ +#define _X86_INCLUDE_ASM_MPXK_H_ + +#ifndef CONFIG_X86_INTEL_MPX_KERNEL +/* Use the generic header that provides empty definitions */ +#include <asm-generic/mpxk.h> +#else /*CONFIG_X86_INTEL_MPX_KERNEL */ + +extern void mpxk_enable_mpx (void ); +extern void mpxk_print_bounds (const char *str,const void *ptr); + +#endif /*CONFIG_X86_INTEL_MPX_KERNEL */ +#endif /* _X86_INCLUDE_ASM_MPXK_H_ */
arch/x86/kernel/traps.c +43 -1
56 lines not on this page · raw message
arch/x86/lib/Makefile +5 -0
@@ -46,3 +46,8 @@ else lib-y += copy_user_64.o lib-y += cmpxchg16b_emu.o endif + +lib-$(CONFIG_X86_INTEL_MPX_KERNEL) += mpxk.o +lib-$(CONFIG_X86_INTEL_MPX_KERNEL) += mpxk-wrappers.o +CFLAGS_mpxk.o += $(MPXK_LIB_CFLAGS) +CFLAGS_mpxk-wrappers.o += $(MPXK_LIB_CFLAGS)
arch/x86/lib/mpxk.c new file +69 -0
69 lines not on this page · raw message
include/asm-generic/mpxk.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* include/asm-generic/mpxk.h +* +* Copyright (C) 2017 Aalto University +*/ +#ifndef _ASM_MPXK_H_ +#define _ASM_MPXK_H_ + +#include <asm/mpx.h> +#include <linux/types.h> + +static inline void mpxk_enable_mpx (void ) +{} + +static inline void mpxk_print_bounds (const char *str,const void *ptr) +{ +pr_info ("%s: MPXK disabled, no bounds for pointer %pK\n" , str, ptr); +} + +#endif /* _ASM_MPXK_H_ */
init/main.c +2 -0
@@ -94,6 +94,7 @@#include <asm/setup.h> #include <asm/sections.h> #include <asm/cacheflush.h> +#include <asm/mpxk.h> static int kernel_init (void *);
@@ -883,6 +884,7 @@ static void __init do_basic_setup(void)driver_init ();init_irq_proc ();do_ctors (); +mpxk_enable_mpx ();usermodehelper_enable ();do_initcalls (); }
Signature · 1 line
2.11.0
[kernel-hardening] Re: [RFC PATCH 4/5] x86: MPXK base
Quoted 4 lines · Hans Liljestrand
Enable and add needed support functionality for ring 0 MPX. MPXK is enabled in
init/main.cby setting the BNDCFGS MSR registers. This also includes thempxk_load_boundsimplementation and error handling code for MPX errors, i.e. bound violations.
Maybe I missed it somewhere else, but this seems like there is no CPU feature flag testing. I'd expect runtime alternatives or something to disable this dynamically if the CPU didn't support it.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 4/5] x86: MPXK base
Quoted 10 lines · Kees Cook
Quoted 4 lines · Hans Liljestrand
Enable and add needed support functionality for ring 0 MPX. MPXK is enabled in
init/main.cby setting the BNDCFGS MSR registers. This also includes thempxk_load_boundsimplementation and error handling code for MPX errors, i.e. bound violations.Maybe I missed it somewhere else, but this seems like there is no CPU feature flag testing. I'd expect runtime alternatives or something to disable this dynamically if the CPU didn't support it.
No, it seems I missed it :)
(although I am sure I had a test in there at some point)
Thanks for catching this!
-hans
Quoted 6 lines
-Kees
Signature · 2 lines
Kees Cook Pixel Security
RFC PATCH 5/5 lkdtm: Add kernel MPX testing
In reply to Hans LiljestrandTests MPXK functionality via lkdtm test cases. Currently tests basic bound propagation and instrumentation for memcpy and kmalloc.
--- drivers/misc/Makefile | 7 +++ drivers/misc/lkdtm.h | 7 +++ drivers/misc/lkdtm_core.c | 6 +++ drivers/misc/lkdtm_mpxk.c | 115 +++++++++++++++++++++++++++++++++++++++++ drivers/misc/lkdtm_mpxk.h | 11 ++++ drivers/misc/lkdtm_mpxk_base.c | 65 +++++++++++++++++++++++ 6 files changed, 211 insertions(+) create mode 100644 drivers/misc/lkdtm_mpxk.c create mode 100644 drivers/misc/lkdtm_mpxk.h create mode 100644 drivers/misc/lkdtm_mpxk_base.c
drivers/misc/Makefile +7 -0
@@ -64,6 +64,13 @@ lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o KCOV_INSTRUMENT_lkdtm_rodata.o := n +ifdef CONFIG_X86_INTEL_MPX_KERNEL + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk.o + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk_base.o + CFLAGS_lkdtm_mpxk.o += $(MPXK_CFLAGS) + CFLAGS_lkdtm_mpxk_base.o += $(MPXK_CFLAGS) +endif + OBJCOPYFLAGS := OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \ --set-section-flags .text=alloc,readonly \
drivers/misc/lkdtm.h +7 -0
@@ -29,6 +29,13 @@ void lkdtm_CORRUPT_LIST_ADD(void);void lkdtm_CORRUPT_LIST_DEL (void );void lkdtm_CORRUPT_USER_DS (void ); +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +void lkdtm_MPXK_LOAD_BOUNDS (void ); +void lkdtm_MPXK_FUNCTION_ARGS (void ); +void lkdtm_MPXK_KMALLOC (void ); +void lkdtm_MPXK_MEMCPY (void ); +#endif +/* lkdtm_heap.c */ void lkdtm_OVERWRITE_ALLOCATION (void );void lkdtm_WRITE_AFTER_FREE (void );
drivers/misc/lkdtm_core.c +6 -0
@@ -235,6 +235,12 @@ struct crashtype crashtypes[] = {CRASHTYPE (USERCOPY_STACK_FRAME_FROM ),CRASHTYPE (USERCOPY_STACK_BEYOND ),CRASHTYPE (USERCOPY_KERNEL ), +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +CRASHTYPE (MPXK_LOAD_BOUNDS ), +CRASHTYPE (MPXK_FUNCTION_ARGS ), +CRASHTYPE (MPXK_KMALLOC ), +CRASHTYPE (MPXK_MEMCPY ) +#endif };
drivers/misc/lkdtm_mpxk.c new file +115 -0
115 lines not on this page · raw message
drivers/misc/lkdtm_mpxk.h new file +11 -0
@@ -0,0 +1,11 @@ +#undef pr_fmt +#include "lkdtm.h" +#include <asm/mpxk.h> + +/* #define SOFT_TEST */ + +void try_write (void *ptr,int i); +void mpxk_write_arr_i (char **arr,int i,int j); +noinline void mpxk_write_10_i(int i, int j, +void *s0,void *s1,void *s2,void *s3,void *s4, +void *s5,void *s6,void *s7,void *s8,void *s9);
drivers/misc/lkdtm_mpxk_base.c new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.11.0
[kernel-hardening] Re: [RFC PATCH 5/5] lkdtm: Add kernel MPX testing
Quoted 5 lines · Hans Liljestrand
Tests MPXK functionality via lkdtm test cases. Currently tests basic bound propagation and instrumentation for memcpy and kmalloc.
Signed-off-by Hans Liljestrand <[email protected]>Signed-off-by Elena Reshetova <[email protected]>
Quoted 7 lines
--- drivers/misc/Makefile | 7 +++ drivers/misc/lkdtm.h | 7 +++ drivers/misc/lkdtm_core.c | 6 +++ drivers/misc/lkdtm_mpxk.c | 115 +++++++++++++++++++++++++++++++++++++++++ drivers/misc/lkdtm_mpxk.h | 11 ++++ drivers/misc/lkdtm_mpxk_base.c | 65 +++++++++++++++++++++++
Organizationally speaking, I'd prefer this was all in one file: lkdtm_mpxk.c
Quoted 10 lines
6 files changed, 211 insertions(+) create mode 100644 drivers/misc/lkdtm_mpxk.c create mode 100644 drivers/misc/lkdtm_mpxk.h create mode 100644 drivers/misc/lkdtm_mpxk_base.cdrivers/misc/Makefile +0
-0
drivers/misc/Makefile lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o · line 72
Quoted 9 lines
drivers/misc/Makefile +6
-0@@ -64,6 +64,13 @@ lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o KCOV_INSTRUMENT_lkdtm_rodata.o := n +ifdef CONFIG_X86_INTEL_MPX_KERNEL + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk.o + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk_base.o + CFLAGS_lkdtm_mpxk.o += $(MPXK_CFLAGS) + CFLAGS_lkdtm_mpxk_base.o += $(MPXK_CFLAGS) +endif
Operationally, I'd like to find a way for these tests to be added unconditionally (i.e. built with either CONFIG_X86_INTEL_MPX_KERNEL enabled or disabled.) They would, obviously, all fail without MPXK.
Quoted 9 lines
drivers/misc/Makefile +1
-0+ OBJCOPYFLAGS := OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \ --set-section-flags .text=alloc,readonly \drivers/misc/lkdtm.h +0
-0
drivers/misc/lkdtm.h void lkdtm_CORRUPT_LIST_ADD(void); · line 38
Quoted 10 lines
drivers/misc/lkdtm.h +7
-0@@ -29,6 +29,13 @@ void lkdtm_CORRUPT_LIST_ADD(void);void lkdtm_CORRUPT_LIST_DEL (void );void lkdtm_CORRUPT_USER_DS (void ); +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +void lkdtm_MPXK_LOAD_BOUNDS (void ); +void lkdtm_MPXK_FUNCTION_ARGS (void ); +void lkdtm_MPXK_KMALLOC (void ); +void lkdtm_MPXK_MEMCPY (void ); +#endif +
It would allow dropping all these ifdefs too.
Quoted 59 lines
drivers/misc/lkdtm.h +0
-0/* lkdtm_heap.c */ void lkdtm_OVERWRITE_ALLOCATION (void );void lkdtm_WRITE_AFTER_FREE (void );drivers/misc/lkdtm_core.c +6
-0@@ -235,6 +235,12 @@ struct crashtype crashtypes[] = {CRASHTYPE (USERCOPY_STACK_FRAME_FROM ),CRASHTYPE (USERCOPY_STACK_BEYOND ),CRASHTYPE (USERCOPY_KERNEL ), +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +CRASHTYPE (MPXK_LOAD_BOUNDS ), +CRASHTYPE (MPXK_FUNCTION_ARGS ), +CRASHTYPE (MPXK_KMALLOC ), +CRASHTYPE (MPXK_MEMCPY ) +#endif };drivers/misc/lkdtm_mpxk.c new file +33
-0@@ -0,0 +1,115 @@ +#undef pr_fmt +#include "lkdtm_mpxk.h" +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/time.h> +#include <linux/slab.h> +#include <linux/printk.h> +#include <asm/mpxk.h> + +/** lkdtm_MPXK_LOAD_BOUNDS - test mpxk_bound_load +* +* Tests mpxk_load_bounds function by passing pointers into function via an +* array. The bounds for the array itself are passed via the bnd0 register, but +* MPX cannot do that for the internal pointers, hence it uses BNDSTX+BNDLDX. +* MPXK therefore must use mpxk_load_bounds to retrieve the bounds inside the +* called function. +*/ +void lkdtm_MPXK_LOAD_BOUNDS (void ) +{ +int i; +char *arr[10 ]; + +for (i =0 ; i <10 ; i++) + arr[i] =kmalloc (16 ,GFP_KERNEL ); + +pr_info ("attempting good ptr write\n" ); +mpxk_write_arr_i (arr,2 ,0 ); + +/* This could succeed because mpxk_load_bounds retrieved the size based +* on the pointer value via ksize, which in turn doesn't necessarily +* return the exact size that was passed into kmalloc. The size is none +* the less guaranteed to be "safe" in that it will not be reserved +* elsewhere.
drivers/misc/lkdtm_mpxk.c line 43
Quoted 10 lines
drivers/misc/lkdtm_mpxk.c new file +10
-0+*/ +pr_info ("attempting exact (+1) bad ptr write (can succeed)" ); +mpxk_write_arr_i (arr,4 ,16 ); + +pr_info ("attempting real bad ptr write (should be caught)\n" ); +mpxk_write_arr_i (arr,5 ,1024 ); + +for (i =0 ; i <10 ; i++) +kfree (arr[i]); +}
Instead of these mpxk_write_arr_i() wrappers, I'd prefer just seeing a direct use of the standard kernel APIs or memory accesses. The "SOFT TEST" stuff doesn't make sense to me here -- lkdtm should either pass loudly (with WARN, BUG, etc) or fail quietly.
-Kees
Signature · 2 lines
Kees Cook Pixel Security
[kernel-hardening] Re: [RFC PATCH 5/5] lkdtm: Add kernel MPX testing
Quoted 15 lines · Kees Cook
Quoted 5 lines · Hans Liljestrand
Tests MPXK functionality via lkdtm test cases. Currently tests basic bound propagation and instrumentation for memcpy and kmalloc.
Signed-off-by Hans Liljestrand <[email protected]>Signed-off-by Elena Reshetova <[email protected]>Quoted 7 lines
--- drivers/misc/Makefile | 7 +++ drivers/misc/lkdtm.h | 7 +++ drivers/misc/lkdtm_core.c | 6 +++ drivers/misc/lkdtm_mpxk.c | 115 +++++++++++++++++++++++++++++++++++++++++ drivers/misc/lkdtm_mpxk.h | 11 ++++ drivers/misc/lkdtm_mpxk_base.c | 65 +++++++++++++++++++++++
Quoted 1 line
Organizationally speaking, I'd prefer this was all in one file:
lkdtm_mpxk.c
Okay. I originally separated these to make sure we don't run into issues with linking, but since that hasn't proven to be an issue in these kinds of cases there's no reason to put them all into one.
Quoted 14 lines
Quoted 14 lines
6 files changed, 211 insertions(+) create mode 100644 drivers/misc/lkdtm_mpxk.c create mode 100644 drivers/misc/lkdtm_mpxk.h create mode 100644 drivers/misc/lkdtm_mpxk_base.cdrivers/misc/Makefile +0
-0@@ -64,6 +64,13 @@ lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o KCOV_INSTRUMENT_lkdtm_rodata.o := n
Quoted 10 lines
drivers/misc/Makefile lkdtm-$(CONFIG_LKDTM) += lkdtm_usercopy.o · line 72
Quoted 6 lines
drivers/misc/Makefile +6
-0+ifdef CONFIG_X86_INTEL_MPX_KERNEL + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk.o + lkdtm-$(CONFIG_LKDTM) += lkdtm_mpxk_base.o + CFLAGS_lkdtm_mpxk.o += $(MPXK_CFLAGS) + CFLAGS_lkdtm_mpxk_base.o += $(MPXK_CFLAGS) +endifOperationally, I'd like to find a way for these tests to be added unconditionally (i.e. built with either
CONFIG_X86_INTEL_MPX_KERNELenabled or disabled.) They would, obviously, all fail without MPXK.
Sure. The compiler intrinsic functions probably will not get recognized, but I can instead conditionally replace them with with nop macros (in lkdtm_mpxk.c).
Quoted 87 lines
Quoted 10 lines
drivers/misc/Makefile +1
-0+ OBJCOPYFLAGS := OBJCOPYFLAGS_lkdtm_rodata_objcopy.o := \ --set-section-flags .text=alloc,readonly \drivers/misc/lkdtm.h +0
-0drivers/misc/lkdtm.h void lkdtm_CORRUPT_LIST_ADD(void); · line 38
Quoted 10 lines
drivers/misc/lkdtm.h +7
-0@@ -29,6 +29,13 @@ void lkdtm_CORRUPT_LIST_ADD(void);void lkdtm_CORRUPT_LIST_DEL (void );void lkdtm_CORRUPT_USER_DS (void ); +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +void lkdtm_MPXK_LOAD_BOUNDS (void ); +void lkdtm_MPXK_FUNCTION_ARGS (void ); +void lkdtm_MPXK_KMALLOC (void ); +void lkdtm_MPXK_MEMCPY (void ); +#endif +It would allow dropping all these ifdefs too.
Quoted 64 lines
drivers/misc/lkdtm.h +0
-0/* lkdtm_heap.c */ void lkdtm_OVERWRITE_ALLOCATION (void );void lkdtm_WRITE_AFTER_FREE (void );drivers/misc/lkdtm_core.c +6
-0@@ -235,6 +235,12 @@ struct crashtype crashtypes[] = {CRASHTYPE (USERCOPY_STACK_FRAME_FROM ),CRASHTYPE (USERCOPY_STACK_BEYOND ),CRASHTYPE (USERCOPY_KERNEL ), +#ifdef CONFIG_X86_INTEL_MPX_KERNEL +CRASHTYPE (MPXK_LOAD_BOUNDS ), +CRASHTYPE (MPXK_FUNCTION_ARGS ), +CRASHTYPE (MPXK_KMALLOC ), +CRASHTYPE (MPXK_MEMCPY ) +#endif };drivers/misc/lkdtm_mpxk.c new file +38
-0@@ -0,0 +1,115 @@ +#undef pr_fmt +#include "lkdtm_mpxk.h" +#include <linux/list.h> +#include <linux/sched.h> +#include <linux/time.h> +#include <linux/slab.h> +#include <linux/printk.h> +#include <asm/mpxk.h> + +/** lkdtm_MPXK_LOAD_BOUNDS - test mpxk_bound_load +* +* Tests mpxk_load_bounds function by passing pointers into function via an +* array. The bounds for the array itself are passed via the bnd0 register, but +* MPX cannot do that for the internal pointers, hence it uses BNDSTX+BNDLDX. +* MPXK therefore must use mpxk_load_bounds to retrieve the bounds inside the +* called function. +*/ +void lkdtm_MPXK_LOAD_BOUNDS (void ) +{ +int i; +char *arr[10 ]; + +for (i =0 ; i <10 ; i++) + arr[i] =kmalloc (16 ,GFP_KERNEL ); + +pr_info ("attempting good ptr write\n" ); +mpxk_write_arr_i (arr,2 ,0 ); + +/* This could succeed because mpxk_load_bounds retrieved the size based +* on the pointer value via ksize, which in turn doesn't necessarily +* return the exact size that was passed into kmalloc. The size is none +* the less guaranteed to be "safe" in that it will not be reserved +* elsewhere. +*/ +pr_info ("attempting exact (+1) bad ptr write (can succeed)" ); +mpxk_write_arr_i (arr,4 ,16 ); + +pr_info ("attempting real bad ptr write (should be caught)\n" );
Quoted 8 lines
drivers/misc/lkdtm_mpxk.c line 43
Quoted 5 lines
drivers/misc/lkdtm_mpxk.c new file +5
-0+mpxk_write_arr_i (arr,5 ,1024 ); + +for (i =0 ; i <10 ; i++) +kfree (arr[i]); +}Instead of these
mpxk_write_arr_i()wrappers, I'd prefer just seeing a direct use of the standard kernel APIs or memory accesses.
I'm not sure if I understand what you mean by this?
The reason I use the awkward wrapper is that I try to force the compiler to not optimize away the specific compile time instrumentation I want to test. If I just inline the above tests the compiler might optimize in a way that eliminates the mpxk_load_bounds call. By forcing the pointers in via an array I make sure the "test-site" cannot get the bounds from anywhere else.
That said, I agree that the mpxk_write_arr_i stuff looks horrible and that this could probably be done in some better way. Any advice on how to do that without having the compiler potentially mess things up?
Quoted 3 lines
The "SOFT
TEST" stuff doesn't make sense to me here -- lkdtm should either pass loudly (with WARN, BUG, etc) or fail quietly.
Yes sorry about the SOFT_TEST stuff, that is a remnant of my own testing. Will remove!
Thanks again!
-hans
Quoted 6 lines
-Kees
Signature · 2 lines
Kees Cook Pixel Security