Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lkdtm/stackleak: prevent unexpected stack usage

The lkdtm_STACKLEAK_ERASING() test is instrumentable and runs with IRQs
unmasked, so it's possible for unrelated code to clobber the task stack
and/or manipulate current->lowest_stack while the test is running,
resulting in spurious failures.

The regular stackleak erasing code is non-instrumentable and runs with
IRQs masked, preventing similar issues.

Make the body of the test non-instrumentable, and run it with IRQs
masked, avoiding such spurious failures.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Alexander Popov <alex.popov@linux.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220427173128.2603085-11-mark.rutland@arm.com

authored by

Mark Rutland and committed by
Kees Cook
f03a5093 72b61896

+23 -1
+23 -1
drivers/misc/lkdtm/stackleak.c
··· 11 11 #include "lkdtm.h" 12 12 #include <linux/stackleak.h> 13 13 14 - void lkdtm_STACKLEAK_ERASING(void) 14 + /* 15 + * Check that stackleak tracks the lowest stack pointer and erases the stack 16 + * below this as expected. 17 + * 18 + * To prevent the lowest stack pointer changing during the test, IRQs are 19 + * masked and instrumentation of this function is disabled. We assume that the 20 + * compiler will create a fixed-size stack frame for this function. 21 + * 22 + * Any non-inlined function may make further use of the stack, altering the 23 + * lowest stack pointer and/or clobbering poison values. To avoid spurious 24 + * failures we must avoid printing until the end of the test or have already 25 + * encountered a failure condition. 26 + */ 27 + static void noinstr check_stackleak_irqoff(void) 15 28 { 16 29 const unsigned long task_stack_base = (unsigned long)task_stack_page(current); 17 30 const unsigned long task_stack_low = stackleak_task_low_bound(current); ··· 93 80 } else { 94 81 pr_info("OK: the rest of the thread stack is properly erased\n"); 95 82 } 83 + } 84 + 85 + void lkdtm_STACKLEAK_ERASING(void) 86 + { 87 + unsigned long flags; 88 + 89 + local_irq_save(flags); 90 + check_stackleak_irqoff(); 91 + local_irq_restore(flags); 96 92 }