xref: /linux-6.15/include/linux/stackleak.h (revision 65120498)
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>
17*65120498SArnd Bergmann #include <linux/linkage.h>
18afaef01cSAlexander Popov 
199ec79840SMark Rutland /*
209ec79840SMark Rutland  * The lowest address on tsk's stack which we can plausibly erase.
219ec79840SMark Rutland  */
229ec79840SMark Rutland static __always_inline unsigned long
stackleak_task_low_bound(const struct task_struct * tsk)239ec79840SMark Rutland stackleak_task_low_bound(const struct task_struct *tsk)
249ec79840SMark Rutland {
259ec79840SMark Rutland 	/*
269ec79840SMark Rutland 	 * The lowest unsigned long on the task stack contains STACK_END_MAGIC,
279ec79840SMark Rutland 	 * which we must not corrupt.
289ec79840SMark Rutland 	 */
299ec79840SMark Rutland 	return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
309ec79840SMark Rutland }
319ec79840SMark Rutland 
320cfa2ccdSMark Rutland /*
330cfa2ccdSMark Rutland  * The address immediately after the highest address on tsk's stack which we
340cfa2ccdSMark Rutland  * can plausibly erase.
350cfa2ccdSMark Rutland  */
360cfa2ccdSMark Rutland static __always_inline unsigned long
stackleak_task_high_bound(const struct task_struct * tsk)370cfa2ccdSMark Rutland stackleak_task_high_bound(const struct task_struct *tsk)
380cfa2ccdSMark Rutland {
390cfa2ccdSMark Rutland 	/*
400cfa2ccdSMark Rutland 	 * The task's pt_regs lives at the top of the task stack and will be
410cfa2ccdSMark Rutland 	 * overwritten by exception entry, so there's no need to erase them.
420cfa2ccdSMark Rutland 	 */
430cfa2ccdSMark Rutland 	return (unsigned long)task_pt_regs(tsk);
440cfa2ccdSMark Rutland }
450cfa2ccdSMark Rutland 
4677cf2b6dSMark Rutland /*
4777cf2b6dSMark Rutland  * Find the address immediately above the poisoned region of the stack, where
4877cf2b6dSMark Rutland  * that region falls between 'low' (inclusive) and 'high' (exclusive).
4977cf2b6dSMark Rutland  */
5077cf2b6dSMark Rutland static __always_inline unsigned long
stackleak_find_top_of_poison(const unsigned long low,const unsigned long high)5177cf2b6dSMark Rutland stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
5277cf2b6dSMark Rutland {
5377cf2b6dSMark Rutland 	const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
5477cf2b6dSMark Rutland 	unsigned int poison_count = 0;
5577cf2b6dSMark Rutland 	unsigned long poison_high = high;
5677cf2b6dSMark Rutland 	unsigned long sp = high;
5777cf2b6dSMark Rutland 
5877cf2b6dSMark Rutland 	while (sp > low && poison_count < depth) {
5977cf2b6dSMark Rutland 		sp -= sizeof(unsigned long);
6077cf2b6dSMark Rutland 
6177cf2b6dSMark Rutland 		if (*(unsigned long *)sp == STACKLEAK_POISON) {
6277cf2b6dSMark Rutland 			poison_count++;
6377cf2b6dSMark Rutland 		} else {
6477cf2b6dSMark Rutland 			poison_count = 0;
6577cf2b6dSMark Rutland 			poison_high = sp;
6677cf2b6dSMark Rutland 		}
6777cf2b6dSMark Rutland 	}
6877cf2b6dSMark Rutland 
6977cf2b6dSMark Rutland 	return poison_high;
7077cf2b6dSMark Rutland }
7177cf2b6dSMark Rutland 
stackleak_task_init(struct task_struct * t)72afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t)
73afaef01cSAlexander Popov {
749ec79840SMark Rutland 	t->lowest_stack = stackleak_task_low_bound(t);
75c8d12627SAlexander Popov # ifdef CONFIG_STACKLEAK_METRICS
76c8d12627SAlexander Popov 	t->prev_lowest_stack = t->lowest_stack;
77c8d12627SAlexander Popov # endif
78afaef01cSAlexander Popov }
79964c9dffSAlexander Popov 
80*65120498SArnd Bergmann asmlinkage void noinstr stackleak_erase(void);
81*65120498SArnd Bergmann asmlinkage void noinstr stackleak_erase_on_task_stack(void);
82*65120498SArnd Bergmann asmlinkage void noinstr stackleak_erase_off_task_stack(void);
83*65120498SArnd Bergmann void __no_caller_saved_registers noinstr stackleak_track_stack(void);
84*65120498SArnd Bergmann 
85afaef01cSAlexander Popov #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
stackleak_task_init(struct task_struct * t)86afaef01cSAlexander Popov static inline void stackleak_task_init(struct task_struct *t) { }
87afaef01cSAlexander Popov #endif
88afaef01cSAlexander Popov 
89afaef01cSAlexander Popov #endif
90