Message ID | 20200604134957.505389-2-alex.popov@linux.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Improvements of the stackleak gcc plugin | expand |
On Thu, Jun 4, 2020 at 3:51 PM Alexander Popov <alex.popov@linux.com> wrote: > Some time ago Variable Length Arrays (VLA) were removed from the kernel. > The kernel is built with '-Wvla'. Let's exclude alloca() from the > instrumentation logic and make it simpler. The build-time assertion > against alloca() is added instead. [...] > + /* Variable Length Arrays are forbidden in the kernel */ > + gcc_assert(!is_alloca(stmt)); There is a patch series from Elena and Kees on the kernel-hardening list that deliberately uses __builtin_alloca() in the syscall entry path to randomize the stack pointer per-syscall - see <https://lore.kernel.org/kernel-hardening/20200406231606.37619-4-keescook@chromium.org/>.
On 04.06.2020 17:01, Jann Horn wrote: > On Thu, Jun 4, 2020 at 3:51 PM Alexander Popov <alex.popov@linux.com> wrote: >> Some time ago Variable Length Arrays (VLA) were removed from the kernel. >> The kernel is built with '-Wvla'. Let's exclude alloca() from the >> instrumentation logic and make it simpler. The build-time assertion >> against alloca() is added instead. > [...] >> + /* Variable Length Arrays are forbidden in the kernel */ >> + gcc_assert(!is_alloca(stmt)); > > There is a patch series from Elena and Kees on the kernel-hardening > list that deliberately uses __builtin_alloca() in the syscall entry > path to randomize the stack pointer per-syscall - see > <https://lore.kernel.org/kernel-hardening/20200406231606.37619-4-keescook@chromium.org/>. Thanks, Jann. At first glance, leaving alloca() handling in stackleak instrumentation logic would allow to integrate stackleak and this version of random_kstack_offset. Kees, Elena, did you try random_kstack_offset with upstream stackleak? It looks to me that without stackleak erasing random_kstack_offset can be weaker. I mean, if next syscall has a bigger stack randomization gap, the data on thread stack from the previous syscall is not overwritten and can be used. Am I right? Another aspect: CONFIG_STACKLEAK_METRICS can be used for guessing kernel stack offset, which is bad. It should be disabled if random_kstack_offset is on. Best regards, Alexander
On Thu, Jun 04, 2020 at 06:23:38PM +0300, Alexander Popov wrote: > On 04.06.2020 17:01, Jann Horn wrote: > > On Thu, Jun 4, 2020 at 3:51 PM Alexander Popov <alex.popov@linux.com> wrote: > >> Some time ago Variable Length Arrays (VLA) were removed from the kernel. > >> The kernel is built with '-Wvla'. Let's exclude alloca() from the > >> instrumentation logic and make it simpler. The build-time assertion > >> against alloca() is added instead. > > [...] > >> + /* Variable Length Arrays are forbidden in the kernel */ > >> + gcc_assert(!is_alloca(stmt)); > > > > There is a patch series from Elena and Kees on the kernel-hardening > > list that deliberately uses __builtin_alloca() in the syscall entry > > path to randomize the stack pointer per-syscall - see > > <https://lore.kernel.org/kernel-hardening/20200406231606.37619-4-keescook@chromium.org/>. > > Thanks, Jann. > > At first glance, leaving alloca() handling in stackleak instrumentation logic > would allow to integrate stackleak and this version of random_kstack_offset. Right, it seems there would be a need for this coverage to remain, otherwise the depth of stack erasure might be incorrect. It doesn't seem like the other patches strictly depend on alloca() support being removed, though? > Kees, Elena, did you try random_kstack_offset with upstream stackleak? I didn't try that combination yet, no. It seemed there would likely still be further discussion about the offset series first (though the thread has been silent -- I'll rebase and resend it after rc2). > It looks to me that without stackleak erasing random_kstack_offset can be > weaker. I mean, if next syscall has a bigger stack randomization gap, the data > on thread stack from the previous syscall is not overwritten and can be used. Am > I right? That's correct. I think the combination is needed, but I don't think they need to be strictly tied together. > Another aspect: CONFIG_STACKLEAK_METRICS can be used for guessing kernel stack > offset, which is bad. It should be disabled if random_kstack_offset is on. Agreed.
On 09.06.2020 21:39, Kees Cook wrote: > On Thu, Jun 04, 2020 at 06:23:38PM +0300, Alexander Popov wrote: >> On 04.06.2020 17:01, Jann Horn wrote: >>> On Thu, Jun 4, 2020 at 3:51 PM Alexander Popov <alex.popov@linux.com> wrote: >>>> Some time ago Variable Length Arrays (VLA) were removed from the kernel. >>>> The kernel is built with '-Wvla'. Let's exclude alloca() from the >>>> instrumentation logic and make it simpler. The build-time assertion >>>> against alloca() is added instead. >>> [...] >>>> + /* Variable Length Arrays are forbidden in the kernel */ >>>> + gcc_assert(!is_alloca(stmt)); >>> >>> There is a patch series from Elena and Kees on the kernel-hardening >>> list that deliberately uses __builtin_alloca() in the syscall entry >>> path to randomize the stack pointer per-syscall - see >>> <https://lore.kernel.org/kernel-hardening/20200406231606.37619-4-keescook@chromium.org/>. >> >> Thanks, Jann. >> >> At first glance, leaving alloca() handling in stackleak instrumentation logic >> would allow to integrate stackleak and this version of random_kstack_offset. > > Right, it seems there would be a need for this coverage to remain, > otherwise the depth of stack erasure might be incorrect. > > It doesn't seem like the other patches strictly depend on alloca() > support being removed, though? Ok, I will leave alloca() support, reorganize the patch series and send v2. >> Kees, Elena, did you try random_kstack_offset with upstream stackleak? > > I didn't try that combination yet, no. It seemed there would likely > still be further discussion about the offset series first (though the > thread has been silent -- I'll rebase and resend it after rc2). Ok, please add me to CC list. Best regards, Alexander >> It looks to me that without stackleak erasing random_kstack_offset can be >> weaker. I mean, if next syscall has a bigger stack randomization gap, the data >> on thread stack from the previous syscall is not overwritten and can be used. Am >> I right? > > That's correct. I think the combination is needed, but I don't think > they need to be strictly tied together. > >> Another aspect: CONFIG_STACKLEAK_METRICS can be used for guessing kernel stack >> offset, which is bad. It should be disabled if random_kstack_offset is on. > > Agreed.
diff --git a/scripts/gcc-plugins/stackleak_plugin.c b/scripts/gcc-plugins/stackleak_plugin.c index cc75eeba0be1..1ecfe50d0bf5 100644 --- a/scripts/gcc-plugins/stackleak_plugin.c +++ b/scripts/gcc-plugins/stackleak_plugin.c @@ -9,10 +9,9 @@ * any of the gcc libraries * * This gcc plugin is needed for tracking the lowest border of the kernel stack. - * It instruments the kernel code inserting stackleak_track_stack() calls: - * - after alloca(); - * - for the functions with a stack frame size greater than or equal - * to the "track-min-size" plugin parameter. + * It instruments the kernel code inserting stackleak_track_stack() calls + * for the functions with a stack frame size greater than or equal to + * the "track-min-size" plugin parameter. * * This plugin is ported from grsecurity/PaX. For more information see: * https://grsecurity.net/ @@ -46,7 +45,7 @@ static struct plugin_info stackleak_plugin_info = { "disable\t\tdo not activate the plugin\n" }; -static void stackleak_add_track_stack(gimple_stmt_iterator *gsi, bool after) +static void stackleak_add_track_stack(gimple_stmt_iterator *gsi) { gimple stmt; gcall *stackleak_track_stack; @@ -56,12 +55,7 @@ static void stackleak_add_track_stack(gimple_stmt_iterator *gsi, bool after) /* Insert call to void stackleak_track_stack(void) */ stmt = gimple_build_call(track_function_decl, 0); stackleak_track_stack = as_a_gcall(stmt); - if (after) { - gsi_insert_after(gsi, stackleak_track_stack, - GSI_CONTINUE_LINKING); - } else { - gsi_insert_before(gsi, stackleak_track_stack, GSI_SAME_STMT); - } + gsi_insert_before(gsi, stackleak_track_stack, GSI_SAME_STMT); /* Update the cgraph */ bb = gimple_bb(stackleak_track_stack); @@ -87,14 +81,13 @@ static bool is_alloca(gimple stmt) /* * Work with the GIMPLE representation of the code. Insert the - * stackleak_track_stack() call after alloca() and into the beginning - * of the function if it is not instrumented. + * stackleak_track_stack() call into the beginning of the function. */ static unsigned int stackleak_instrument_execute(void) { basic_block bb, entry_bb; - bool prologue_instrumented = false, is_leaf = true; - gimple_stmt_iterator gsi; + bool is_leaf = true; + gimple_stmt_iterator gsi = { 0 }; /* * ENTRY_BLOCK_PTR is a basic block which represents possible entry @@ -111,27 +104,17 @@ static unsigned int stackleak_instrument_execute(void) */ FOR_EACH_BB_FN(bb, cfun) { for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) { - gimple stmt; - - stmt = gsi_stmt(gsi); + gimple stmt = gsi_stmt(gsi); /* Leaf function is a function which makes no calls */ if (is_gimple_call(stmt)) is_leaf = false; - if (!is_alloca(stmt)) - continue; - - /* Insert stackleak_track_stack() call after alloca() */ - stackleak_add_track_stack(&gsi, true); - if (bb == entry_bb) - prologue_instrumented = true; + /* Variable Length Arrays are forbidden in the kernel */ + gcc_assert(!is_alloca(stmt)); } } - if (prologue_instrumented) - return 0; - /* * Special cases to skip the instrumentation. * @@ -168,7 +151,7 @@ static unsigned int stackleak_instrument_execute(void) bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)); } gsi = gsi_after_labels(bb); - stackleak_add_track_stack(&gsi, false); + stackleak_add_track_stack(&gsi); return 0; } @@ -185,12 +168,20 @@ static bool large_stack_frame(void) /* * Work with the RTL representation of the code. * Remove the unneeded stackleak_track_stack() calls from the functions - * which don't call alloca() and don't have a large enough stack frame size. + * that don't have a large enough stack frame size. */ static unsigned int stackleak_cleanup_execute(void) { rtx_insn *insn, *next; + /* + * gcc before version 7 called allocate_dynamic_stack_space() from + * expand_stack_vars() for runtime alignment of constant-sized stack + * variables. That caused cfun->calls_alloca to be set for functions + * that don't use alloca(). + * For more info see gcc commit 7072df0aae0c59ae437e. + * Let's leave such functions instrumented. + */ if (cfun->calls_alloca) return 0;
Some time ago Variable Length Arrays (VLA) were removed from the kernel. The kernel is built with '-Wvla'. Let's exclude alloca() from the instrumentation logic and make it simpler. The build-time assertion against alloca() is added instead. Unfortunately, for that assertion we can't simply check cfun->calls_alloca during RTL phase. It turned out that gcc before version 7 called allocate_dynamic_stack_space() from expand_stack_vars() for runtime alignment of constant-sized stack variables. That caused cfun->calls_alloca to be set for functions that don't use alloca(). Signed-off-by: Alexander Popov <alex.popov@linux.com> --- scripts/gcc-plugins/stackleak_plugin.c | 51 +++++++++++--------------- 1 file changed, 21 insertions(+), 30 deletions(-)