~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/stackleak.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/stackleak.c (Version linux-6.12-rc7) and /kernel/stackleak.c (Version linux-4.15.18)


  1 // SPDX-License-Identifier: GPL-2.0                 1 
  2 /*                                                
  3  * This code fills the used part of the kernel    
  4  * before returning to userspace. It's part of    
  5  * ported from grsecurity/PaX.                    
  6  *                                                
  7  * Author: Alexander Popov <alex.popov@linux.c    
  8  *                                                
  9  * STACKLEAK reduces the information which ker    
 10  * reveal and blocks some uninitialized stack     
 11  */                                               
 12                                                   
 13 #include <linux/stackleak.h>                      
 14 #include <linux/kprobes.h>                        
 15                                                   
 16 #ifdef CONFIG_STACKLEAK_RUNTIME_DISABLE           
 17 #include <linux/jump_label.h>                     
 18 #include <linux/sysctl.h>                         
 19 #include <linux/init.h>                           
 20                                                   
 21 static DEFINE_STATIC_KEY_FALSE(stack_erasing_b    
 22                                                   
 23 #ifdef CONFIG_SYSCTL                              
 24 static int stack_erasing_sysctl(const struct c    
 25                         void __user *buffer, s    
 26 {                                                 
 27         int ret = 0;                              
 28         int state = !static_branch_unlikely(&s    
 29         int prev_state = state;                   
 30         struct ctl_table table_copy = *table;     
 31                                                   
 32         table_copy.data = &state;                 
 33         ret = proc_dointvec_minmax(&table_copy    
 34         state = !!state;                          
 35         if (ret || !write || state == prev_sta    
 36                 return ret;                       
 37                                                   
 38         if (state)                                
 39                 static_branch_disable(&stack_e    
 40         else                                      
 41                 static_branch_enable(&stack_er    
 42                                                   
 43         pr_warn("stackleak: kernel stack erasi    
 44                                         state     
 45         return ret;                               
 46 }                                                 
 47 static struct ctl_table stackleak_sysctls[] =     
 48         {                                         
 49                 .procname       = "stack_erasi    
 50                 .data           = NULL,           
 51                 .maxlen         = sizeof(int),    
 52                 .mode           = 0600,           
 53                 .proc_handler   = stack_erasin    
 54                 .extra1         = SYSCTL_ZERO,    
 55                 .extra2         = SYSCTL_ONE,     
 56         },                                        
 57 };                                                
 58                                                   
 59 static int __init stackleak_sysctls_init(void)    
 60 {                                                 
 61         register_sysctl_init("kernel", stackle    
 62         return 0;                                 
 63 }                                                 
 64 late_initcall(stackleak_sysctls_init);            
 65 #endif /* CONFIG_SYSCTL */                        
 66                                                   
 67 #define skip_erasing()  static_branch_unlikely    
 68 #else                                             
 69 #define skip_erasing()  false                     
 70 #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */     
 71                                                   
 72 #ifndef __stackleak_poison                        
 73 static __always_inline void __stackleak_poison    
 74                                                   
 75                                                   
 76 {                                                 
 77         while (erase_low < erase_high) {          
 78                 *(unsigned long *)erase_low =     
 79                 erase_low += sizeof(unsigned l    
 80         }                                         
 81 }                                                 
 82 #endif                                            
 83                                                   
 84 static __always_inline void __stackleak_erase(    
 85 {                                                 
 86         const unsigned long task_stack_low = s    
 87         const unsigned long task_stack_high =     
 88         unsigned long erase_low, erase_high;      
 89                                                   
 90         erase_low = stackleak_find_top_of_pois    
 91                                                   
 92                                                   
 93 #ifdef CONFIG_STACKLEAK_METRICS                   
 94         current->prev_lowest_stack = erase_low    
 95 #endif                                            
 96                                                   
 97         /*                                        
 98          * Write poison to the task's stack be    
 99          * 'erase_high'.                          
100          *                                        
101          * If we're running on a different sta    
102          * stack) we can erase everything belo    
103          * task stack.                            
104          *                                        
105          * If we're running on the task stack     
106          * stack used by this function and its    
107          * function has a fixed-size stack fra    
108          * doesn't change while we write poiso    
109          */                                       
110         if (on_task_stack)                        
111                 erase_high = current_stack_poi    
112         else                                      
113                 erase_high = task_stack_high;     
114                                                   
115         __stackleak_poison(erase_low, erase_hi    
116                                                   
117         /* Reset the 'lowest_stack' value for     
118         current->lowest_stack = task_stack_hig    
119 }                                                 
120                                                   
121 /*                                                
122  * Erase and poison the portion of the task st    
123  * Can be called from the task stack or an ent    
124  * no longer in use.                              
125  */                                               
126 asmlinkage void noinstr stackleak_erase(void)     
127 {                                                 
128         if (skip_erasing())                       
129                 return;                           
130                                                   
131         __stackleak_erase(on_thread_stack());     
132 }                                                 
133                                                   
134 /*                                                
135  * Erase and poison the portion of the task st    
136  * Can only be called from the task stack.        
137  */                                               
138 asmlinkage void noinstr stackleak_erase_on_tas    
139 {                                                 
140         if (skip_erasing())                       
141                 return;                           
142                                                   
143         __stackleak_erase(true);                  
144 }                                                 
145                                                   
146 /*                                                
147  * Erase and poison the portion of the task st    
148  * Can only be called from a stack other than     
149  */                                               
150 asmlinkage void noinstr stackleak_erase_off_ta    
151 {                                                 
152         if (skip_erasing())                       
153                 return;                           
154                                                   
155         __stackleak_erase(false);                 
156 }                                                 
157                                                   
158 void __used __no_caller_saved_registers noinst    
159 {                                                 
160         unsigned long sp = current_stack_point    
161                                                   
162         /*                                        
163          * Having CONFIG_STACKLEAK_TRACK_MIN_S    
164          * STACKLEAK_SEARCH_DEPTH makes the po    
165          * stackleak_erase() unreliable. Let's    
166          */                                       
167         BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MI    
168                                                   
169         /* 'lowest_stack' should be aligned on    
170         sp = ALIGN(sp, sizeof(unsigned long));    
171         if (sp < current->lowest_stack &&         
172             sp >= stackleak_task_low_bound(cur    
173                 current->lowest_stack = sp;       
174         }                                         
175 }                                                 
176 EXPORT_SYMBOL(stackleak_track_stack);             
177                                                   

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php