xref: /linux-6.15/include/linux/stackleak.h (revision 0cfa2ccd)
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 
189ec79840SMark Rutland /*
199ec79840SMark Rutland  * The lowest address on tsk's stack which we can plausibly erase.
209ec79840SMark Rutland  */
219ec79840SMark Rutland static __always_inline unsigned long
229ec79840SMark Rutland stackleak_task_low_bound(const struct task_struct *tsk)
239ec79840SMark Rutland {
249ec79840SMark Rutland 	/*
259ec79840SMark Rutland 	 * The lowest unsigned long on the task stack contains STACK_END_MAGIC,
269ec79840SMark Rutland 	 * which we must not corrupt.
279ec79840SMark Rutland 	 */
289ec79840SMark Rutland 	return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
299ec79840SMark Rutland }
309ec79840SMark Rutland 
31*0cfa2ccdSMark Rutland /*
32*0cfa2ccdSMark Rutland  * The address immediately after the highest address on tsk's stack which we
33*0cfa2ccdSMark Rutland  * can plausibly erase.
34*0cfa2ccdSMark Rutland  */
35*0cfa2ccdSMark Rutland static __always_inline unsigned long
36*0cfa2ccdSMark Rutland stackleak_task_high_bound(const struct task_struct *tsk)
37*0cfa2ccdSMark Rutland {
38*0cfa2ccdSMark Rutland 	/*
39*0cfa2ccdSMark Rutland 	 * The task's pt_regs lives at the top of the task stack and will be
40*0cfa2ccdSMark Rutland 	 * overwritten by exception entry, so there's no need to erase them.
41*0cfa2ccdSMark Rutland 	 */
42*0cfa2ccdSMark Rutland 	return (unsigned long)task_pt_regs(tsk);
43*0cfa2ccdSMark Rutland }
44*0cfa2ccdSMark Rutland 
45afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t)
46afaef01cSAlexander Popov {
479ec79840SMark Rutland 	t->lowest_stack = stackleak_task_low_bound(t);
48c8d12627SAlexander Popov # ifdef CONFIG_STACKLEAK_METRICS
49c8d12627SAlexander Popov 	t->prev_lowest_stack = t->lowest_stack;
50c8d12627SAlexander Popov # endif
51afaef01cSAlexander Popov }
52964c9dffSAlexander Popov 
53afaef01cSAlexander Popov #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
54afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t) { }
55afaef01cSAlexander Popov #endif
56afaef01cSAlexander Popov 
57afaef01cSAlexander Popov #endif
58