In stackleak_task_init(), stackleak_track_stack(), and __stackleak_erase(), we open-code skipping the STACK_END_MAGIC at the bottom of the stack. Each case is implemented slightly differently, and only the __stackleak_erase() case is commented. In stackleak_task_init() and stackleak_track_stack() we unconditionally add sizeof(unsigned long) to the lowest stack address. In stackleak_task_init() we use end_of_stack() for this, and in stackleak_track_stack() we use task_stack_page(). In __stackleak_erase() we handle this by detecting if `kstack_ptr` has hit the stack end boundary, and if so, conditionally moving it above the magic. This patch adds a new stackleak_task_low_bound() helper which is used in all three cases, which unconditionally adds sizeof(unsigned long) to the lowest address on the task stack, with commentary as to why. This uses end_of_stack() as stackleak_task_init() did prior to this patch, as this is consistent with the code in kernel/fork.c which initializes the STACK_END_MAGIC value. In __stackleak_erase() we no longer need to check whether we've spilled into the STACK_END_MAGIC value, as stackleak_track_stack() ensures that `current->lowest_stack` stops immediately above this, and similarly the poison scan will stop immediately above this. For stackleak_task_init() and stackleak_track_stack() this results in no change to code generation. For __stackleak_erase() the generated assembly is slightly simpler and shorter.
Unified · Split
| @@ -15,9 +15,22 @@ · 14 unmodified lines | ||
| 15 | 15 | #ifdef CONFIG_GCC_PLUGIN_STACKLEAK |
| 16 | 16 | #include <asm/stacktrace.h> |
| 17 | 17 | |
| 18 | +/* | |
| 19 | + * The lowest address on tsk's stack which we can plausibly erase. | |
| 20 | + */ | |
| 21 | +static __always_inline unsigned long | |
| 22 | +stackleak_task_low_bound(const struct task_struct *tsk) | |
| 23 | +{ | |
| 24 | + /* | |
| 25 | + * The lowest unsigned long on the task stack contains STACK_END_MAGIC, | |
| 26 | + * which we must not corrupt. | |
| 27 | + */ | |
| 28 | + return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long); | |
| 29 | +} | |
| 30 | + | |
| 18 | 31 | static inline void stackleak_task_init(struct task_struct *t) |
| 19 | 32 | { |
| 20 | − t->lowest_stack = (unsigned long)end_of_stack(t) + sizeof(unsigned long); | |
| 33 | + t->lowest_stack = stackleak_task_low_bound(t); | |
| 21 | 34 | # ifdef CONFIG_STACKLEAK_METRICS |
| 22 | 35 | t->prev_lowest_stack = t->lowest_stack; |
| 23 | 36 | # endif |
| @@ -72,9 +72,11 @@ late_initcall(stackleak_sysctls_init); · 71 unmodified lines | ||
| 72 | 72 | |
| 73 | 73 | static __always_inline void __stackleak_erase(void) |
| 74 | 74 | { |
| 75 | + const unsigned long task_stack_low = stackleak_task_low_bound(current); | |
| 76 | + | |
| 75 | 77 | /* It would be nice not to have 'kstack_ptr' and 'boundary' on stack */ |
| 76 | 78 | unsigned long kstack_ptr = current->lowest_stack; |
| 77 | − unsigned long boundary = (unsigned long)end_of_stack(current); | |
| 79 | + unsigned long boundary = task_stack_low; | |
| 78 | 80 | unsigned int poison_count = 0; |
| 79 | 81 | const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long); |
| 80 | 82 | |
| @@ -88,13 +90,6 @@ static __always_inline void __stackleak_erase(void) · 7 unmodified lines | ||
| 88 | 90 | kstack_ptr -= sizeof(unsigned long); |
| 89 | 91 | } |
| 90 | 92 | |
| 91 | − /* | |
| 92 | − * One 'long int' at the bottom of the thread stack is reserved and | |
| 93 | − * should not be poisoned (see CONFIG_SCHED_STACK_END_CHECK=y). | |
| 94 | − */ | |
| 95 | − if (kstack_ptr == boundary) | |
| 96 | − kstack_ptr += sizeof(unsigned long); | |
| 97 | − | |
| 98 | 93 | #ifdef CONFIG_STACKLEAK_METRICS |
| 99 | 94 | current->prev_lowest_stack = kstack_ptr; |
| 100 | 95 | #endif |
| @@ -140,8 +135,7 @@ void __used __no_caller_saved_registers noinstr stackleak_track_stack(void) · 39 unmodified lines | ||
| 140 | 135 | /* 'lowest_stack' should be aligned on the register width boundary */ |
| 141 | 136 | sp = ALIGN(sp, sizeof(unsigned long)); |
| 142 | 137 | if (sp < current->lowest_stack && |
| 143 | − sp >= (unsigned long)task_stack_page(current) + | |
| 144 | − sizeof(unsigned long)) { | |
| 138 | + sp >= stackleak_task_low_bound(current)) { | |
| 145 | 139 | current->lowest_stack = sp; |
| 146 | 140 | } |
| 147 | 141 | } |