Skip to content

Commit 77cf2b6

Browse files
mrutland-armkees
authored andcommitted
stackleak: rework poison scanning
Currently we over-estimate the region of stack which must be erased. To determine the region to be erased, we scan downwards for a contiguous block of poison values (or the low bound of the stack). There are a few minor problems with this today: * When we find a block of poison values, we include this block within the region to erase. As this is included within the region to erase, this causes us to redundantly overwrite 'STACKLEAK_SEARCH_DEPTH' (128) bytes with poison. * As the loop condition checks 'poison_count <= depth', it will run an additional iteration after finding the contiguous block of poison, decrementing 'erase_low' once more than necessary. As this is included within the region to erase, this causes us to redundantly overwrite an additional unsigned long with poison. * As we always decrement 'erase_low' after checking an element on the stack, we always include the element below this within the region to erase. As this is included within the region to erase, this causes us to redundantly overwrite an additional unsigned long with poison. Note that this is not a functional problem. As the loop condition checks 'erase_low > task_stack_low', we'll never clobber the STACK_END_MAGIC. As we always decrement 'erase_low' after this, we'll never fail to erase the element immediately above the STACK_END_MAGIC. In total, this can cause us to erase `128 + 2 * sizeof(unsigned long)` bytes more than necessary, which is unfortunate. This patch reworks the logic to find the address immediately above the poisoned region, by finding the lowest non-poisoned address. This is factored into a stackleak_find_top_of_poison() helper both for clarity and so that this can be shared with the LKDTM test in subsequent patches. 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: Kees Cook <keescook@chromium.org> Signed-off-by: Kees Cook <keescook@chromium.org> Link: https://lore.kernel.org/r/20220427173128.2603085-8-mark.rutland@arm.com
1 parent 0cfa2cc commit 77cf2b6

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

include/linux/stackleak.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ stackleak_task_high_bound(const struct task_struct *tsk)
4242
return (unsigned long)task_pt_regs(tsk);
4343
}
4444

45+
/*
46+
* Find the address immediately above the poisoned region of the stack, where
47+
* that region falls between 'low' (inclusive) and 'high' (exclusive).
48+
*/
49+
static __always_inline unsigned long
50+
stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
51+
{
52+
const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
53+
unsigned int poison_count = 0;
54+
unsigned long poison_high = high;
55+
unsigned long sp = high;
56+
57+
while (sp > low && poison_count < depth) {
58+
sp -= sizeof(unsigned long);
59+
60+
if (*(unsigned long *)sp == STACKLEAK_POISON) {
61+
poison_count++;
62+
} else {
63+
poison_count = 0;
64+
poison_high = sp;
65+
}
66+
}
67+
68+
return poison_high;
69+
}
70+
4571
static inline void stackleak_task_init(struct task_struct *t)
4672
{
4773
t->lowest_stack = stackleak_task_low_bound(t);

kernel/stackleak.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,10 @@ static __always_inline void __stackleak_erase(void)
7474
{
7575
const unsigned long task_stack_low = stackleak_task_low_bound(current);
7676
const unsigned long task_stack_high = stackleak_task_high_bound(current);
77-
unsigned long erase_low = current->lowest_stack;
78-
unsigned long erase_high;
79-
unsigned int poison_count = 0;
80-
const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
81-
82-
/* Search for the poison value in the kernel stack */
83-
while (erase_low > task_stack_low && poison_count <= depth) {
84-
if (*(unsigned long *)erase_low == STACKLEAK_POISON)
85-
poison_count++;
86-
else
87-
poison_count = 0;
88-
89-
erase_low -= sizeof(unsigned long);
90-
}
77+
unsigned long erase_low, erase_high;
78+
79+
erase_low = stackleak_find_top_of_poison(task_stack_low,
80+
current->lowest_stack);
9181

9282
#ifdef CONFIG_STACKLEAK_METRICS
9383
current->prev_lowest_stack = erase_low;

0 commit comments

Comments
 (0)