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