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