Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_STACKLEAK_H
0003 #define _LINUX_STACKLEAK_H
0004 
0005 #include <linux/sched.h>
0006 #include <linux/sched/task_stack.h>
0007 
0008 /*
0009  * Check that the poison value points to the unused hole in the
0010  * virtual memory map for your platform.
0011  */
0012 #define STACKLEAK_POISON -0xBEEF
0013 #define STACKLEAK_SEARCH_DEPTH 128
0014 
0015 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
0016 #include <asm/stacktrace.h>
0017 
0018 /*
0019  * The lowest address on tsk's stack which we can plausibly erase.
0020  */
0021 static __always_inline unsigned long
0022 stackleak_task_low_bound(const struct task_struct *tsk)
0023 {
0024     /*
0025      * The lowest unsigned long on the task stack contains STACK_END_MAGIC,
0026      * which we must not corrupt.
0027      */
0028     return (unsigned long)end_of_stack(tsk) + sizeof(unsigned long);
0029 }
0030 
0031 /*
0032  * The address immediately after the highest address on tsk's stack which we
0033  * can plausibly erase.
0034  */
0035 static __always_inline unsigned long
0036 stackleak_task_high_bound(const struct task_struct *tsk)
0037 {
0038     /*
0039      * The task's pt_regs lives at the top of the task stack and will be
0040      * overwritten by exception entry, so there's no need to erase them.
0041      */
0042     return (unsigned long)task_pt_regs(tsk);
0043 }
0044 
0045 /*
0046  * Find the address immediately above the poisoned region of the stack, where
0047  * that region falls between 'low' (inclusive) and 'high' (exclusive).
0048  */
0049 static __always_inline unsigned long
0050 stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
0051 {
0052     const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
0053     unsigned int poison_count = 0;
0054     unsigned long poison_high = high;
0055     unsigned long sp = high;
0056 
0057     while (sp > low && poison_count < depth) {
0058         sp -= sizeof(unsigned long);
0059 
0060         if (*(unsigned long *)sp == STACKLEAK_POISON) {
0061             poison_count++;
0062         } else {
0063             poison_count = 0;
0064             poison_high = sp;
0065         }
0066     }
0067 
0068     return poison_high;
0069 }
0070 
0071 static inline void stackleak_task_init(struct task_struct *t)
0072 {
0073     t->lowest_stack = stackleak_task_low_bound(t);
0074 # ifdef CONFIG_STACKLEAK_METRICS
0075     t->prev_lowest_stack = t->lowest_stack;
0076 # endif
0077 }
0078 
0079 #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
0080 static inline void stackleak_task_init(struct task_struct *t) { }
0081 #endif
0082 
0083 #endif