xref: /linux-6.15/include/linux/stackleak.h (revision 9ec79840)
1afaef01cSAlexander Popov /* SPDX-License-Identifier: GPL-2.0 */
2afaef01cSAlexander Popov #ifndef _LINUX_STACKLEAK_H
3afaef01cSAlexander Popov #define _LINUX_STACKLEAK_H
4afaef01cSAlexander Popov 
5afaef01cSAlexander Popov #include <linux/sched.h>
6afaef01cSAlexander Popov #include <linux/sched/task_stack.h>
7afaef01cSAlexander Popov 
8afaef01cSAlexander Popov /*
9afaef01cSAlexander Popov  * Check that the poison value points to the unused hole in the
10afaef01cSAlexander Popov  * virtual memory map for your platform.
11afaef01cSAlexander Popov  */
12afaef01cSAlexander Popov #define STACKLEAK_POISON -0xBEEF
13afaef01cSAlexander Popov #define STACKLEAK_SEARCH_DEPTH 128
14afaef01cSAlexander Popov 
15afaef01cSAlexander Popov #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
16afaef01cSAlexander Popov #include <asm/stacktrace.h>
17afaef01cSAlexander Popov 
18*9ec79840SMark Rutland /*
19*9ec79840SMark Rutland  * The lowest address on tsk's stack which we can plausibly erase.
20*9ec79840SMark Rutland  */
21*9ec79840SMark Rutland static __always_inline unsigned long
22*9ec79840SMark Rutland stackleak_task_low_bound(const struct task_struct *tsk)
23*9ec79840SMark Rutland {
24*9ec79840SMark Rutland 	/*
25*9ec79840SMark Rutland 	 * The lowest unsigned long on the task stack contains STACK_END_MAGIC,
26*9ec79840SMark Rutland 	 * which we must not corrupt.
27*9ec79840SMark Rutland 	 */
28*9ec79840SMark Rutland 	return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
29*9ec79840SMark Rutland }
30*9ec79840SMark Rutland 
31afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t)
32afaef01cSAlexander Popov {
33*9ec79840SMark Rutland 	t->lowest_stack = stackleak_task_low_bound(t);
34c8d12627SAlexander Popov # ifdef CONFIG_STACKLEAK_METRICS
35c8d12627SAlexander Popov 	t->prev_lowest_stack = t->lowest_stack;
36c8d12627SAlexander Popov # endif
37afaef01cSAlexander Popov }
38964c9dffSAlexander Popov 
39afaef01cSAlexander Popov #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
40afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t) { }
41afaef01cSAlexander Popov #endif
42afaef01cSAlexander Popov 
43afaef01cSAlexander Popov #endif
44