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

TOMOYO Linux Cross Reference
Linux/security/tomoyo/realpath.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ 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.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /security/tomoyo/realpath.c (Version linux-6.11.5) and /security/tomoyo/realpath.c (Version linux-2.4.37.11)


  1 // SPDX-License-Identifier: GPL-2.0                 1 
  2 /*                                                
  3  * security/tomoyo/realpath.c                     
  4  *                                                
  5  * Copyright (C) 2005-2011  NTT DATA CORPORATI    
  6  */                                               
  7                                                   
  8 #include "common.h"                               
  9 #include <linux/magic.h>                          
 10 #include <linux/proc_fs.h>                        
 11                                                   
 12 /**                                               
 13  * tomoyo_encode2 - Encode binary string to as    
 14  *                                                
 15  * @str:     String in binary format.             
 16  * @str_len: Size of @str in byte.                
 17  *                                                
 18  * Returns pointer to @str in ascii format on     
 19  *                                                
 20  * This function uses kzalloc(), so caller mus    
 21  * didn't return NULL.                            
 22  */                                               
 23 char *tomoyo_encode2(const char *str, int str_    
 24 {                                                 
 25         int i;                                    
 26         int len = 0;                              
 27         const char *p = str;                      
 28         char *cp;                                 
 29         char *cp0;                                
 30                                                   
 31         if (!p)                                   
 32                 return NULL;                      
 33         for (i = 0; i < str_len; i++) {           
 34                 const unsigned char c = p[i];     
 35                                                   
 36                 if (c == '\\')                    
 37                         len += 2;                 
 38                 else if (c > ' ' && c < 127)      
 39                         len++;                    
 40                 else                              
 41                         len += 4;                 
 42         }                                         
 43         len++;                                    
 44         /* Reserve space for appending "/". */    
 45         cp = kzalloc(len + 10, GFP_NOFS);         
 46         if (!cp)                                  
 47                 return NULL;                      
 48         cp0 = cp;                                 
 49         p = str;                                  
 50         for (i = 0; i < str_len; i++) {           
 51                 const unsigned char c = p[i];     
 52                                                   
 53                 if (c == '\\') {                  
 54                         *cp++ = '\\';             
 55                         *cp++ = '\\';             
 56                 } else if (c > ' ' && c < 127)    
 57                         *cp++ = c;                
 58                 } else {                          
 59                         *cp++ = '\\';             
 60                         *cp++ = (c >> 6) + '';    
 61                         *cp++ = ((c >> 3) & 7)    
 62                         *cp++ = (c & 7) + '';     
 63                 }                                 
 64         }                                         
 65         return cp0;                               
 66 }                                                 
 67                                                   
 68 /**                                               
 69  * tomoyo_encode - Encode binary string to asc    
 70  *                                                
 71  * @str: String in binary format.                 
 72  *                                                
 73  * Returns pointer to @str in ascii format on     
 74  *                                                
 75  * This function uses kzalloc(), so caller mus    
 76  * didn't return NULL.                            
 77  */                                               
 78 char *tomoyo_encode(const char *str)              
 79 {                                                 
 80         return str ? tomoyo_encode2(str, strle    
 81 }                                                 
 82                                                   
 83 /**                                               
 84  * tomoyo_get_absolute_path - Get the path of     
 85  *                                                
 86  * @path:   Pointer to "struct path".             
 87  * @buffer: Pointer to buffer to return value     
 88  * @buflen: Sizeof @buffer.                       
 89  *                                                
 90  * Returns the buffer on success, an error cod    
 91  *                                                
 92  * If dentry is a directory, trailing '/' is a    
 93  */                                               
 94 static char *tomoyo_get_absolute_path(const st    
 95                                       const in    
 96 {                                                 
 97         char *pos = ERR_PTR(-ENOMEM);             
 98                                                   
 99         if (buflen >= 256) {                      
100                 /* go to whatever namespace ro    
101                 pos = d_absolute_path(path, bu    
102                 if (!IS_ERR(pos) && *pos == '/    
103                         struct inode *inode =     
104                                                   
105                         if (inode && S_ISDIR(i    
106                                 buffer[buflen     
107                                 buffer[buflen     
108                         }                         
109                 }                                 
110         }                                         
111         return pos;                               
112 }                                                 
113                                                   
114 /**                                               
115  * tomoyo_get_dentry_path - Get the path of a     
116  *                                                
117  * @dentry: Pointer to "struct dentry".           
118  * @buffer: Pointer to buffer to return value     
119  * @buflen: Sizeof @buffer.                       
120  *                                                
121  * Returns the buffer on success, an error cod    
122  *                                                
123  * If dentry is a directory, trailing '/' is a    
124  */                                               
125 static char *tomoyo_get_dentry_path(struct den    
126                                     const int     
127 {                                                 
128         char *pos = ERR_PTR(-ENOMEM);             
129                                                   
130         if (buflen >= 256) {                      
131                 pos = dentry_path_raw(dentry,     
132                 if (!IS_ERR(pos) && *pos == '/    
133                         struct inode *inode =     
134                                                   
135                         if (inode && S_ISDIR(i    
136                                 buffer[buflen     
137                                 buffer[buflen     
138                         }                         
139                 }                                 
140         }                                         
141         return pos;                               
142 }                                                 
143                                                   
144 /**                                               
145  * tomoyo_get_local_path - Get the path of a d    
146  *                                                
147  * @dentry: Pointer to "struct dentry".           
148  * @buffer: Pointer to buffer to return value     
149  * @buflen: Sizeof @buffer.                       
150  *                                                
151  * Returns the buffer on success, an error cod    
152  */                                               
153 static char *tomoyo_get_local_path(struct dent    
154                                    const int b    
155 {                                                 
156         struct super_block *sb = dentry->d_sb;    
157         char *pos = tomoyo_get_dentry_path(den    
158                                                   
159         if (IS_ERR(pos))                          
160                 return pos;                       
161         /* Convert from $PID to self if $PID i    
162         if (sb->s_magic == PROC_SUPER_MAGIC &&    
163                 char *ep;                         
164                 const pid_t pid = (pid_t) simp    
165                 struct pid_namespace *proc_pid    
166                                                   
167                 if (*ep == '/' && pid && pid =    
168                     task_tgid_nr_ns(current, p    
169                         pos = ep - 5;             
170                         if (pos < buffer)         
171                                 goto out;         
172                         memmove(pos, "/self",     
173                 }                                 
174                 goto prepend_filesystem_name;     
175         }                                         
176         /* Use filesystem name for unnamed dev    
177         if (!MAJOR(sb->s_dev))                    
178                 goto prepend_filesystem_name;     
179         {                                         
180                 struct inode *inode = d_backin    
181                                                   
182                 /*                                
183                  * Use filesystem name if file    
184                  * operation.                     
185                  */                               
186                 if (!inode->i_op->rename)         
187                         goto prepend_filesyste    
188         }                                         
189         /* Prepend device name. */                
190         {                                         
191                 char name[64];                    
192                 int name_len;                     
193                 const dev_t dev = sb->s_dev;      
194                                                   
195                 name[sizeof(name) - 1] = '\0';    
196                 snprintf(name, sizeof(name) -     
197                          MINOR(dev));             
198                 name_len = strlen(name);          
199                 pos -= name_len;                  
200                 if (pos < buffer)                 
201                         goto out;                 
202                 memmove(pos, name, name_len);     
203                 return pos;                       
204         }                                         
205         /* Prepend filesystem name. */            
206 prepend_filesystem_name:                          
207         {                                         
208                 const char *name = sb->s_type-    
209                 const int name_len = strlen(na    
210                                                   
211                 pos -= name_len + 1;              
212                 if (pos < buffer)                 
213                         goto out;                 
214                 memmove(pos, name, name_len);     
215                 pos[name_len] = ':';              
216         }                                         
217         return pos;                               
218 out:                                              
219         return ERR_PTR(-ENOMEM);                  
220 }                                                 
221                                                   
222 /**                                               
223  * tomoyo_realpath_from_path - Returns realpat    
224  *                                                
225  * @path: Pointer to "struct path".               
226  *                                                
227  * Returns the realpath of the given @path on     
228  *                                                
229  * If dentry is a directory, trailing '/' is a    
230  * Characters out of 0x20 < c < 0x7F range are    
231  * \ooo style octal string.                       
232  * Character \ is converted to \\ string.         
233  *                                                
234  * These functions use kzalloc(), so the calle    
235  * if these functions didn't return NULL.         
236  */                                               
237 char *tomoyo_realpath_from_path(const struct p    
238 {                                                 
239         char *buf = NULL;                         
240         char *name = NULL;                        
241         unsigned int buf_len = PAGE_SIZE / 2;     
242         struct dentry *dentry = path->dentry;     
243         struct super_block *sb = dentry->d_sb;    
244                                                   
245         while (1) {                               
246                 char *pos;                        
247                 struct inode *inode;              
248                                                   
249                 buf_len <<= 1;                    
250                 kfree(buf);                       
251                 buf = kmalloc(buf_len, GFP_NOF    
252                 if (!buf)                         
253                         break;                    
254                 /* To make sure that pos is '\    
255                 buf[buf_len - 1] = '\0';          
256                 /* For "pipe:[\$]" and "socket    
257                 if (dentry->d_op && dentry->d_    
258                         pos = dentry->d_op->d_    
259                         goto encode;              
260                 }                                 
261                 inode = d_backing_inode(sb->s_    
262                 /*                                
263                  * Get local name for filesyst    
264                  */                               
265                 if ((!inode->i_op->rename &&      
266                      !(sb->s_type->fs_flags &     
267                         pos = tomoyo_get_local    
268                                                   
269                 /* Get absolute name for the r    
270                 else {                            
271                         pos = tomoyo_get_absol    
272                         /*                        
273                          * Fall back to local     
274                          * available.             
275                          */                       
276                         if (pos == ERR_PTR(-EI    
277                                 pos = tomoyo_g    
278                                                   
279                 }                                 
280 encode:                                           
281                 if (IS_ERR(pos))                  
282                         continue;                 
283                 name = tomoyo_encode(pos);        
284                 break;                            
285         }                                         
286         kfree(buf);                               
287         if (!name)                                
288                 tomoyo_warn_oom(__func__);        
289         return name;                              
290 }                                                 
291                                                   
292 /**                                               
293  * tomoyo_realpath_nofollow - Get realpath of     
294  *                                                
295  * @pathname: The pathname to solve.              
296  *                                                
297  * Returns the realpath of @pathname on succes    
298  */                                               
299 char *tomoyo_realpath_nofollow(const char *pat    
300 {                                                 
301         struct path path;                         
302                                                   
303         if (pathname && kern_path(pathname, 0,    
304                 char *buf = tomoyo_realpath_fr    
305                                                   
306                 path_put(&path);                  
307                 return buf;                       
308         }                                         
309         return NULL;                              
310 }                                                 
311                                                   

~ [ 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