Recent rework broke building LKDTM when CONFIG_GCC_PLUGIN_STACKLEAK=n. This patch fixes that breakage. Prior to recent stackleak rework, the LKDTM STACKLEAK_ERASING code could be built when the kernel was not built with stackleak support, and would run a test that would almost certainly fail (or pass by sheer cosmic coincidence), e.g. | # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry STACKLEAK_ERASING | lkdtm: checking unused part of the thread stack (15560 bytes)... | lkdtm: FAIL: the erased part is not found (checked 15560 bytes) | lkdtm: FAIL: the thread stack is NOT properly erased! | lkdtm: This is probably expected, since this kernel (5.18.0-rc2 aarch64) was built *without* CONFIG_GCC_PLUGIN_STACKLEAK=y The recent rework to the test made it more accurate by using helpers which are only defined when CONFIG_GCC_PLUGIN_STACKLEAK=y, and so when building LKDTM when CONFIG_GCC_PLUGIN_STACKLEAK=n, we get a build failure: | drivers/misc/lkdtm/stackleak.c: In function 'check_stackleak_irqoff': | drivers/misc/lkdtm/stackleak.c:30:46: error: implicit declaration of function 'stackleak_task_low_bound' [-Werror=implicit-function-declaration] | 30 | const unsigned long task_stack_low = stackleak_task_low_bound(current); | | ^~~~~~~~~~~~~~~~~~~~~~~~ | drivers/misc/lkdtm/stackleak.c:31:47: error: implicit declaration of function 'stackleak_task_high_bound'; did you mean 'stackleak_task_init'? [-Werror=implicit-function-declaration] | 31 | const unsigned long task_stack_high = stackleak_task_high_bound(current); | | ^~~~~~~~~~~~~~~~~~~~~~~~~ | | stackleak_task_init | drivers/misc/lkdtm/stackleak.c:33:48: error: 'struct task_struct' has no member named 'lowest_stack' | 33 | const unsigned long lowest_sp = current->lowest_stack; | | ^~ | drivers/misc/lkdtm/stackleak.c:74:23: error: implicit declaration of function 'stackleak_find_top_of_poison' [-Werror=implicit-function-declaration] | 74 | poison_high = stackleak_find_top_of_poison(task_stack_low, untracked_high); | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ This patch fixes the issue by not compiling the body of the test when CONFIG_GCC_PLUGIN_STACKLEAK=n, and replacing this with an unconditional XFAIL message. This means the pr_expected_config() in check_stackleak_irqoff() is redundant, and so it is removed. Where an architecture does not support stackleak, the test will log: | # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry STACKLEAK_ERASING | lkdtm: XFAIL: stackleak is not supported on this arch (HAVE_ARCH_STACKLEAK=n) Where an architectures does support stackleak, but this has not been compiled in, the test will log: | # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry STACKLEAK_ERASING | lkdtm: XFAIL: stackleak is not enabled (CONFIG_GCC_PLUGIN_STACKLEAK=n) Where stackleak has been compiled in, the test behaves as usual: | # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT | lkdtm: Performing direct entry STACKLEAK_ERASING | lkdtm: stackleak stack usage: | high offset: 336 bytes | current: 688 bytes | lowest: 1232 bytes | tracked: 1232 bytes | untracked: 672 bytes | poisoned: 14136 bytes | low offset: 8 bytes | lkdtm: OK: the rest of the thread stack is properly erased Fixes: f4cfacd92972cc44 ("lkdtm/stackleak: rework boundary management") Signed-off-by: Mark Rutland <mark.rutland@arm.com> Cc: Alexander Popov <alex.popov@linux.com> Cc: Kees Cook <keescook@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20220506121145.1162908-1-mark.rutland@arm.com
136 lines
4.6 KiB
C
136 lines
4.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* This code tests that the current task stack is properly erased (filled
|
|
* with STACKLEAK_POISON).
|
|
*
|
|
* Authors:
|
|
* Alexander Popov <alex.popov@linux.com>
|
|
* Tycho Andersen <tycho@tycho.ws>
|
|
*/
|
|
|
|
#include "lkdtm.h"
|
|
#include <linux/stackleak.h>
|
|
|
|
#if defined(CONFIG_GCC_PLUGIN_STACKLEAK)
|
|
/*
|
|
* Check that stackleak tracks the lowest stack pointer and erases the stack
|
|
* below this as expected.
|
|
*
|
|
* To prevent the lowest stack pointer changing during the test, IRQs are
|
|
* masked and instrumentation of this function is disabled. We assume that the
|
|
* compiler will create a fixed-size stack frame for this function.
|
|
*
|
|
* Any non-inlined function may make further use of the stack, altering the
|
|
* lowest stack pointer and/or clobbering poison values. To avoid spurious
|
|
* failures we must avoid printing until the end of the test or have already
|
|
* encountered a failure condition.
|
|
*/
|
|
static void noinstr check_stackleak_irqoff(void)
|
|
{
|
|
const unsigned long task_stack_base = (unsigned long)task_stack_page(current);
|
|
const unsigned long task_stack_low = stackleak_task_low_bound(current);
|
|
const unsigned long task_stack_high = stackleak_task_high_bound(current);
|
|
const unsigned long current_sp = current_stack_pointer;
|
|
const unsigned long lowest_sp = current->lowest_stack;
|
|
unsigned long untracked_high;
|
|
unsigned long poison_high, poison_low;
|
|
bool test_failed = false;
|
|
|
|
/*
|
|
* Check that the current and lowest recorded stack pointer values fall
|
|
* within the expected task stack boundaries. These tests should never
|
|
* fail unless the boundaries are incorrect or we're clobbering the
|
|
* STACK_END_MAGIC, and in either casee something is seriously wrong.
|
|
*/
|
|
if (current_sp < task_stack_low || current_sp >= task_stack_high) {
|
|
pr_err("FAIL: current_stack_pointer (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
|
|
current_sp, task_stack_low, task_stack_high - 1);
|
|
test_failed = true;
|
|
goto out;
|
|
}
|
|
if (lowest_sp < task_stack_low || lowest_sp >= task_stack_high) {
|
|
pr_err("FAIL: current->lowest_stack (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n",
|
|
lowest_sp, task_stack_low, task_stack_high - 1);
|
|
test_failed = true;
|
|
goto out;
|
|
}
|
|
|
|
/*
|
|
* Depending on what has run prior to this test, the lowest recorded
|
|
* stack pointer could be above or below the current stack pointer.
|
|
* Start from the lowest of the two.
|
|
*
|
|
* Poison values are naturally-aligned unsigned longs. As the current
|
|
* stack pointer might not be sufficiently aligned, we must align
|
|
* downwards to find the lowest known stack pointer value. This is the
|
|
* high boundary for a portion of the stack which may have been used
|
|
* without being tracked, and has to be scanned for poison.
|
|
*/
|
|
untracked_high = min(current_sp, lowest_sp);
|
|
untracked_high = ALIGN_DOWN(untracked_high, sizeof(unsigned long));
|
|
|
|
/*
|
|
* Find the top of the poison in the same way as the erasing code.
|
|
*/
|
|
poison_high = stackleak_find_top_of_poison(task_stack_low, untracked_high);
|
|
|
|
/*
|
|
* Check whether the poisoned portion of the stack (if any) consists
|
|
* entirely of poison. This verifies the entries that
|
|
* stackleak_find_top_of_poison() should have checked.
|
|
*/
|
|
poison_low = poison_high;
|
|
while (poison_low > task_stack_low) {
|
|
poison_low -= sizeof(unsigned long);
|
|
|
|
if (*(unsigned long *)poison_low == STACKLEAK_POISON)
|
|
continue;
|
|
|
|
pr_err("FAIL: non-poison value %lu bytes below poison boundary: 0x%lx\n",
|
|
poison_high - poison_low, *(unsigned long *)poison_low);
|
|
test_failed = true;
|
|
}
|
|
|
|
pr_info("stackleak stack usage:\n"
|
|
" high offset: %lu bytes\n"
|
|
" current: %lu bytes\n"
|
|
" lowest: %lu bytes\n"
|
|
" tracked: %lu bytes\n"
|
|
" untracked: %lu bytes\n"
|
|
" poisoned: %lu bytes\n"
|
|
" low offset: %lu bytes\n",
|
|
task_stack_base + THREAD_SIZE - task_stack_high,
|
|
task_stack_high - current_sp,
|
|
task_stack_high - lowest_sp,
|
|
task_stack_high - untracked_high,
|
|
untracked_high - poison_high,
|
|
poison_high - task_stack_low,
|
|
task_stack_low - task_stack_base);
|
|
|
|
out:
|
|
if (test_failed) {
|
|
pr_err("FAIL: the thread stack is NOT properly erased!\n");
|
|
} else {
|
|
pr_info("OK: the rest of the thread stack is properly erased\n");
|
|
}
|
|
}
|
|
|
|
void lkdtm_STACKLEAK_ERASING(void)
|
|
{
|
|
unsigned long flags;
|
|
|
|
local_irq_save(flags);
|
|
check_stackleak_irqoff();
|
|
local_irq_restore(flags);
|
|
}
|
|
#else /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
|
|
void lkdtm_STACKLEAK_ERASING(void)
|
|
{
|
|
if (IS_ENABLED(CONFIG_HAVE_ARCH_STACKLEAK)) {
|
|
pr_err("XFAIL: stackleak is not enabled (CONFIG_GCC_PLUGIN_STACKLEAK=n)\n");
|
|
} else {
|
|
pr_err("XFAIL: stackleak is not supported on this arch (HAVE_ARCH_STACKLEAK=n)\n");
|
|
}
|
|
}
|
|
#endif /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
|