xref: /linux-6.15/include/linux/stackleak.h (revision 77cf2b6d)
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 
310cfa2ccdSMark Rutland /*
320cfa2ccdSMark Rutland  * The address immediately after the highest address on tsk's stack which we
330cfa2ccdSMark Rutland  * can plausibly erase.
340cfa2ccdSMark Rutland  */
350cfa2ccdSMark Rutland static __always_inline unsigned long
360cfa2ccdSMark Rutland stackleak_task_high_bound(const struct task_struct *tsk)
370cfa2ccdSMark Rutland {
380cfa2ccdSMark Rutland 	/*
390cfa2ccdSMark Rutland 	 * The task's pt_regs lives at the top of the task stack and will be
400cfa2ccdSMark Rutland 	 * overwritten by exception entry, so there's no need to erase them.
410cfa2ccdSMark Rutland 	 */
420cfa2ccdSMark Rutland 	return (unsigned long)task_pt_regs(tsk);
430cfa2ccdSMark Rutland }
440cfa2ccdSMark Rutland 
45*77cf2b6dSMark Rutland /*
46*77cf2b6dSMark Rutland  * Find the address immediately above the poisoned region of the stack, where
47*77cf2b6dSMark Rutland  * that region falls between 'low' (inclusive) and 'high' (exclusive).
48*77cf2b6dSMark Rutland  */
49*77cf2b6dSMark Rutland static __always_inline unsigned long
50*77cf2b6dSMark Rutland stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
51*77cf2b6dSMark Rutland {
52*77cf2b6dSMark Rutland 	const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
53*77cf2b6dSMark Rutland 	unsigned int poison_count = 0;
54*77cf2b6dSMark Rutland 	unsigned long poison_high = high;
55*77cf2b6dSMark Rutland 	unsigned long sp = high;
56*77cf2b6dSMark Rutland 
57*77cf2b6dSMark Rutland 	while (sp > low && poison_count < depth) {
58*77cf2b6dSMark Rutland 		sp -= sizeof(unsigned long);
59*77cf2b6dSMark Rutland 
60*77cf2b6dSMark Rutland 		if (*(unsigned long *)sp == STACKLEAK_POISON) {
61*77cf2b6dSMark Rutland 			poison_count++;
62*77cf2b6dSMark Rutland 		} else {
63*77cf2b6dSMark Rutland 			poison_count = 0;
64*77cf2b6dSMark Rutland 			poison_high = sp;
65*77cf2b6dSMark Rutland 		}
66*77cf2b6dSMark Rutland 	}
67*77cf2b6dSMark Rutland 
68*77cf2b6dSMark Rutland 	return poison_high;
69*77cf2b6dSMark Rutland }
70*77cf2b6dSMark Rutland 
71afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t)
72afaef01cSAlexander Popov {
739ec79840SMark Rutland 	t->lowest_stack = stackleak_task_low_bound(t);
74c8d12627SAlexander Popov # ifdef CONFIG_STACKLEAK_METRICS
75c8d12627SAlexander Popov 	t->prev_lowest_stack = t->lowest_stack;
76c8d12627SAlexander Popov # endif
77afaef01cSAlexander Popov }
78964c9dffSAlexander Popov 
79afaef01cSAlexander Popov #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
80afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t) { }
81afaef01cSAlexander Popov #endif
82afaef01cSAlexander Popov 
83afaef01cSAlexander Popov #endif
84