Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 2 | /* |
| 3 | * security/tomoyo/realpath.c |
| 4 | * |
Tetsuo Handa | 0f2a55d | 2011-07-14 14:46:51 +0900 | [diff] [blame] | 5 | * Copyright (C) 2005-2011 NTT DATA CORPORATION |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 8 | #include "common.h" |
Al Viro | d10577a | 2011-12-07 13:06:11 -0500 | [diff] [blame] | 9 | #include <linux/magic.h> |
Alexey Gladkov | c59f415 | 2020-04-23 22:03:10 +0200 | [diff] [blame] | 10 | #include <linux/proc_fs.h> |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 11 | |
| 12 | /** |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 13 | * tomoyo_encode2 - Encode binary string to ascii string. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 14 | * |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 15 | * @str: String in binary format. |
| 16 | * @str_len: Size of @str in byte. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 17 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 18 | * Returns pointer to @str in ascii format on success, NULL otherwise. |
| 19 | * |
| 20 | * This function uses kzalloc(), so caller must kfree() if this function |
| 21 | * didn't return NULL. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 22 | */ |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 23 | char *tomoyo_encode2(const char *str, int str_len) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 24 | { |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 25 | int i; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 26 | int len = 0; |
| 27 | const char *p = str; |
| 28 | char *cp; |
| 29 | char *cp0; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 30 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 31 | if (!p) |
| 32 | return NULL; |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 33 | for (i = 0; i < str_len; i++) { |
| 34 | const unsigned char c = p[i]; |
| 35 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 36 | if (c == '\\') |
| 37 | len += 2; |
| 38 | else if (c > ' ' && c < 127) |
| 39 | len++; |
| 40 | else |
| 41 | len += 4; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 42 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 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; |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 50 | for (i = 0; i < str_len; i++) { |
| 51 | const unsigned char c = p[i]; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 52 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 53 | if (c == '\\') { |
| 54 | *cp++ = '\\'; |
| 55 | *cp++ = '\\'; |
| 56 | } else if (c > ' ' && c < 127) { |
| 57 | *cp++ = c; |
| 58 | } else { |
| 59 | *cp++ = '\\'; |
| 60 | *cp++ = (c >> 6) + '0'; |
| 61 | *cp++ = ((c >> 3) & 7) + '0'; |
| 62 | *cp++ = (c & 7) + '0'; |
Eric W. Biederman | a4054b6 | 2009-11-20 09:12:22 -0800 | [diff] [blame] | 63 | } |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 64 | } |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 65 | return cp0; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Tetsuo Handa | 059d84d | 2011-09-10 15:23:54 +0900 | [diff] [blame] | 69 | * tomoyo_encode - Encode binary string to ascii string. |
| 70 | * |
| 71 | * @str: String in binary format. |
| 72 | * |
| 73 | * Returns pointer to @str in ascii format on success, NULL otherwise. |
| 74 | * |
| 75 | * This function uses kzalloc(), so caller must kfree() if this function |
| 76 | * didn't return NULL. |
| 77 | */ |
| 78 | char *tomoyo_encode(const char *str) |
| 79 | { |
| 80 | return str ? tomoyo_encode2(str, strlen(str)) : NULL; |
| 81 | } |
| 82 | |
| 83 | /** |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 84 | * tomoyo_get_absolute_path - Get the path of a dentry but ignores chroot'ed root. |
| 85 | * |
| 86 | * @path: Pointer to "struct path". |
| 87 | * @buffer: Pointer to buffer to return value in. |
| 88 | * @buflen: Sizeof @buffer. |
| 89 | * |
| 90 | * Returns the buffer on success, an error code otherwise. |
| 91 | * |
| 92 | * If dentry is a directory, trailing '/' is appended. |
| 93 | */ |
Al Viro | 2247386 | 2015-03-08 19:24:30 -0400 | [diff] [blame] | 94 | static char *tomoyo_get_absolute_path(const struct path *path, char * const buffer, |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 95 | const int buflen) |
| 96 | { |
| 97 | char *pos = ERR_PTR(-ENOMEM); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 98 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 99 | if (buflen >= 256) { |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 100 | /* go to whatever namespace root we are under */ |
Al Viro | 02125a8 | 2011-12-05 08:43:34 -0500 | [diff] [blame] | 101 | pos = d_absolute_path(path, buffer, buflen - 1); |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 102 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 103 | struct inode *inode = d_backing_inode(path->dentry); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 104 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 105 | if (inode && S_ISDIR(inode->i_mode)) { |
| 106 | buffer[buflen - 2] = '/'; |
| 107 | buffer[buflen - 1] = '\0'; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return pos; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * tomoyo_get_dentry_path - Get the path of a dentry. |
| 116 | * |
| 117 | * @dentry: Pointer to "struct dentry". |
| 118 | * @buffer: Pointer to buffer to return value in. |
| 119 | * @buflen: Sizeof @buffer. |
| 120 | * |
| 121 | * Returns the buffer on success, an error code otherwise. |
| 122 | * |
| 123 | * If dentry is a directory, trailing '/' is appended. |
| 124 | */ |
| 125 | static char *tomoyo_get_dentry_path(struct dentry *dentry, char * const buffer, |
| 126 | const int buflen) |
| 127 | { |
| 128 | char *pos = ERR_PTR(-ENOMEM); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 129 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 130 | if (buflen >= 256) { |
| 131 | pos = dentry_path_raw(dentry, buffer, buflen - 1); |
| 132 | if (!IS_ERR(pos) && *pos == '/' && pos[1]) { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 133 | struct inode *inode = d_backing_inode(dentry); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 134 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 135 | if (inode && S_ISDIR(inode->i_mode)) { |
| 136 | buffer[buflen - 2] = '/'; |
| 137 | buffer[buflen - 1] = '\0'; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return pos; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * tomoyo_get_local_path - Get the path of a dentry. |
| 146 | * |
| 147 | * @dentry: Pointer to "struct dentry". |
| 148 | * @buffer: Pointer to buffer to return value in. |
| 149 | * @buflen: Sizeof @buffer. |
| 150 | * |
| 151 | * Returns the buffer on success, an error code otherwise. |
| 152 | */ |
| 153 | static char *tomoyo_get_local_path(struct dentry *dentry, char * const buffer, |
| 154 | const int buflen) |
| 155 | { |
| 156 | struct super_block *sb = dentry->d_sb; |
| 157 | char *pos = tomoyo_get_dentry_path(dentry, buffer, buflen); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 158 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 159 | if (IS_ERR(pos)) |
| 160 | return pos; |
| 161 | /* Convert from $PID to self if $PID is current thread. */ |
| 162 | if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') { |
| 163 | char *ep; |
| 164 | const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10); |
Alexey Gladkov | 9d78ede | 2020-05-18 20:07:38 +0200 | [diff] [blame] | 165 | struct pid_namespace *proc_pidns = proc_pid_ns(sb); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 166 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 167 | if (*ep == '/' && pid && pid == |
Alexey Gladkov | c59f415 | 2020-04-23 22:03:10 +0200 | [diff] [blame] | 168 | task_tgid_nr_ns(current, proc_pidns)) { |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 169 | pos = ep - 5; |
| 170 | if (pos < buffer) |
| 171 | goto out; |
| 172 | memmove(pos, "/self", 5); |
| 173 | } |
| 174 | goto prepend_filesystem_name; |
| 175 | } |
| 176 | /* Use filesystem name for unnamed devices. */ |
| 177 | if (!MAJOR(sb->s_dev)) |
| 178 | goto prepend_filesystem_name; |
| 179 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 180 | struct inode *inode = d_backing_inode(sb->s_root); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 181 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 182 | /* |
| 183 | * Use filesystem name if filesystem does not support rename() |
| 184 | * operation. |
| 185 | */ |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 186 | if (!inode->i_op->rename) |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 187 | goto prepend_filesystem_name; |
| 188 | } |
| 189 | /* Prepend device name. */ |
| 190 | { |
| 191 | char name[64]; |
| 192 | int name_len; |
| 193 | const dev_t dev = sb->s_dev; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 194 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 195 | name[sizeof(name) - 1] = '\0'; |
| 196 | snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev), |
| 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->name; |
| 209 | const int name_len = strlen(name); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 210 | |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 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 | /** |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 223 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
| 224 | * |
| 225 | * @path: Pointer to "struct path". |
| 226 | * |
| 227 | * Returns the realpath of the given @path on success, NULL otherwise. |
| 228 | * |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 229 | * If dentry is a directory, trailing '/' is appended. |
| 230 | * Characters out of 0x20 < c < 0x7F range are converted to |
| 231 | * \ooo style octal string. |
| 232 | * Character \ is converted to \\ string. |
| 233 | * |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 234 | * These functions use kzalloc(), so the caller must call kfree() |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 235 | * if these functions didn't return NULL. |
| 236 | */ |
Al Viro | 2247386 | 2015-03-08 19:24:30 -0400 | [diff] [blame] | 237 | char *tomoyo_realpath_from_path(const struct path *path) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 238 | { |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 239 | char *buf = NULL; |
| 240 | char *name = NULL; |
| 241 | unsigned int buf_len = PAGE_SIZE / 2; |
| 242 | struct dentry *dentry = path->dentry; |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 243 | struct super_block *sb; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 244 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 245 | if (!dentry) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 246 | return NULL; |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 247 | sb = dentry->d_sb; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 248 | while (1) { |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 249 | char *pos; |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 250 | struct inode *inode; |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 251 | |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 252 | buf_len <<= 1; |
| 253 | kfree(buf); |
| 254 | buf = kmalloc(buf_len, GFP_NOFS); |
| 255 | if (!buf) |
| 256 | break; |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 257 | /* To make sure that pos is '\0' terminated. */ |
| 258 | buf[buf_len - 1] = '\0'; |
Tetsuo Handa | 6f7c413 | 2019-11-25 10:46:51 +0900 | [diff] [blame] | 259 | /* For "pipe:[\$]" and "socket:[\$]". */ |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 260 | if (dentry->d_op && dentry->d_op->d_dname) { |
| 261 | pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1); |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 262 | goto encode; |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 263 | } |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 264 | inode = d_backing_inode(sb->s_root); |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 265 | /* |
| 266 | * Get local name for filesystems without rename() operation |
| 267 | * or dentry without vfsmount. |
| 268 | */ |
Tetsuo Handa | 8fe7a26 | 2014-08-20 14:14:04 +0900 | [diff] [blame] | 269 | if (!path->mnt || |
Tetsuo Handa | 27df4b4 | 2019-02-27 23:19:24 +0900 | [diff] [blame] | 270 | (!inode->i_op->rename && |
| 271 | !(sb->s_type->fs_flags & FS_REQUIRES_DEV))) |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 272 | pos = tomoyo_get_local_path(path->dentry, buf, |
| 273 | buf_len - 1); |
| 274 | /* Get absolute name for the rest. */ |
Tetsuo Handa | 1418a3e | 2011-12-08 21:24:06 +0900 | [diff] [blame] | 275 | else { |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 276 | pos = tomoyo_get_absolute_path(path, buf, buf_len - 1); |
Tetsuo Handa | 1418a3e | 2011-12-08 21:24:06 +0900 | [diff] [blame] | 277 | /* |
| 278 | * Fall back to local name if absolute name is not |
| 279 | * available. |
| 280 | */ |
| 281 | if (pos == ERR_PTR(-EINVAL)) |
| 282 | pos = tomoyo_get_local_path(path->dentry, buf, |
| 283 | buf_len - 1); |
| 284 | } |
Tetsuo Handa | 5625f2e | 2011-06-26 23:20:23 +0900 | [diff] [blame] | 285 | encode: |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 286 | if (IS_ERR(pos)) |
| 287 | continue; |
| 288 | name = tomoyo_encode(pos); |
| 289 | break; |
| 290 | } |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 291 | kfree(buf); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 292 | if (!name) |
| 293 | tomoyo_warn_oom(__func__); |
Tetsuo Handa | c8c57e8 | 2010-06-03 20:36:43 +0900 | [diff] [blame] | 294 | return name; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 298 | * tomoyo_realpath_nofollow - Get realpath of a pathname. |
| 299 | * |
| 300 | * @pathname: The pathname to solve. |
| 301 | * |
| 302 | * Returns the realpath of @pathname on success, NULL otherwise. |
| 303 | */ |
| 304 | char *tomoyo_realpath_nofollow(const char *pathname) |
| 305 | { |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 306 | struct path path; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 307 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 308 | if (pathname && kern_path(pathname, 0, &path) == 0) { |
| 309 | char *buf = tomoyo_realpath_from_path(&path); |
Tetsuo Handa | cdcf672 | 2019-01-24 18:37:35 +0900 | [diff] [blame] | 310 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 311 | path_put(&path); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 312 | return buf; |
| 313 | } |
| 314 | return NULL; |
| 315 | } |