/src/dovecot/src/lib/path-util.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "str.h" |
5 | | #include "path-util.h" |
6 | | |
7 | | #include <fcntl.h> |
8 | | #include <unistd.h> |
9 | | #include <sys/types.h> |
10 | | #include <sys/stat.h> |
11 | | |
12 | 0 | #define PATH_UTIL_MAX_PATH 8*1024 |
13 | 0 | #define PATH_UTIL_MAX_SYMLINKS 80 |
14 | | /* Symlink-hop cap for t_openat_safe(). Lower than the realpath cap: |
15 | | t_openat_safe() refuses absolute and escaping targets outright, so a |
16 | | long chain inside the base dir is the only legitimate use. */ |
17 | 0 | #define PATH_UTIL_OPENAT_SAFE_MAX_SYMLINKS 16 |
18 | | |
19 | | static int t_getcwd_noalloc(char **dir_r, size_t *asize_r, |
20 | | const char **error_r) ATTR_NULL(2) |
21 | 5.04k | { |
22 | | /* @UNSAFE */ |
23 | 5.04k | char *dir; |
24 | 5.04k | size_t asize = 128; |
25 | | |
26 | 5.04k | dir = t_buffer_get(asize); |
27 | 5.04k | while (getcwd(dir, asize) == NULL) { |
28 | 0 | if (errno != ERANGE) { |
29 | 0 | *error_r = t_strdup_printf("getcwd() failed: %m"); |
30 | 0 | return -1; |
31 | 0 | } |
32 | 0 | asize = nearest_power(asize+1); |
33 | 0 | dir = t_buffer_get(asize); |
34 | 0 | } |
35 | 5.04k | if (asize_r != NULL) |
36 | 0 | *asize_r = asize; |
37 | 5.04k | *dir_r = dir; |
38 | 5.04k | return 0; |
39 | 5.04k | } |
40 | | |
41 | | static int path_normalize(const char *path, bool resolve_links, |
42 | | const char **npath_r, const char **error_r) |
43 | 0 | { |
44 | | /* @UNSAFE */ |
45 | 0 | unsigned int link_count = 0; |
46 | 0 | char *npath, *npath_pos; |
47 | 0 | const char *p; |
48 | 0 | size_t asize; |
49 | |
|
50 | 0 | i_assert(path != NULL); |
51 | 0 | i_assert(npath_r != NULL); |
52 | 0 | i_assert(error_r != NULL); |
53 | | |
54 | 0 | if (path[0] != '/') { |
55 | | /* relative; initialize npath with current directory */ |
56 | 0 | if (t_getcwd_noalloc(&npath, &asize, error_r) < 0) |
57 | 0 | return -1; |
58 | 0 | npath_pos = npath + strlen(npath); |
59 | 0 | i_assert(npath[0] == '/'); |
60 | 0 | } else { |
61 | | /* absolute; initialize npath with root */ |
62 | 0 | asize = 128; |
63 | 0 | npath = t_buffer_get(asize); |
64 | 0 | npath[0] = '/'; |
65 | 0 | npath_pos = npath + 1; |
66 | 0 | } |
67 | | |
68 | 0 | p = path; |
69 | 0 | while (*p != '\0') { |
70 | 0 | struct stat st; |
71 | 0 | ptrdiff_t seglen; |
72 | 0 | const char *segend; |
73 | | |
74 | | /* skip duplicate slashes */ |
75 | 0 | while (*p == '/') |
76 | 0 | p++; |
77 | | |
78 | | /* find end of path segment */ |
79 | 0 | for (segend = p; *segend != '\0' && *segend != '/'; segend++); |
80 | |
|
81 | 0 | if (segend == p) |
82 | 0 | break; /* '\0' */ |
83 | 0 | seglen = segend - p; |
84 | 0 | if (seglen == 1 && p[0] == '.') { |
85 | | /* a reference to this segment; nothing to do */ |
86 | 0 | } else if (seglen == 2 && p[0] == '.' && p[1] == '.') { |
87 | | /* a reference to parent segment; back up to previous |
88 | | * slash */ |
89 | 0 | i_assert(npath_pos >= npath); |
90 | 0 | if ((npath_pos - npath) > 1) { |
91 | 0 | if (*(npath_pos-1) == '/') |
92 | 0 | npath_pos--; |
93 | 0 | for (; *(npath_pos-1) != '/'; npath_pos--); |
94 | 0 | } |
95 | 0 | } else { |
96 | | /* allocate space if necessary */ |
97 | 0 | i_assert(npath_pos >= npath); |
98 | 0 | if ((size_t)((npath_pos - npath) + seglen + 1) >= asize) { |
99 | 0 | ptrdiff_t npath_offset = npath_pos - npath; |
100 | 0 | asize = nearest_power(MALLOC_ADD3(npath_offset, seglen, 2)); |
101 | 0 | npath = t_buffer_reget(npath, asize); |
102 | 0 | npath_pos = npath + npath_offset; |
103 | 0 | } |
104 | | |
105 | | /* make sure npath now ends in slash */ |
106 | 0 | i_assert(npath_pos > npath); |
107 | 0 | if (*(npath_pos-1) != '/') { |
108 | 0 | i_assert((size_t)((npath_pos - npath) + 1) < asize); |
109 | 0 | *(npath_pos++) = '/'; |
110 | 0 | } |
111 | | |
112 | | /* copy segment to normalized path */ |
113 | 0 | i_assert(npath_pos >= npath); |
114 | 0 | i_assert((size_t)((npath_pos - npath) + seglen) < asize); |
115 | 0 | memmove(npath_pos, p, seglen); |
116 | 0 | npath_pos += seglen; |
117 | 0 | } |
118 | | |
119 | 0 | if (resolve_links) { |
120 | | /* stat path up to here (segend points to tail) */ |
121 | 0 | *npath_pos = '\0'; |
122 | 0 | if (lstat(npath, &st) < 0) { |
123 | 0 | *error_r = t_strdup_printf("lstat() failed: %m"); |
124 | 0 | return -1; |
125 | 0 | } |
126 | | |
127 | 0 | if (S_ISLNK (st.st_mode)) { |
128 | | /* symlink */ |
129 | 0 | char *npath_link; |
130 | 0 | size_t lsize = 128, tlen = strlen(segend), espace; |
131 | 0 | size_t ltlen = (link_count == 0 ? 0 : tlen); |
132 | 0 | ssize_t ret; |
133 | | |
134 | | /* limit link dereferences */ |
135 | 0 | if (++link_count > PATH_UTIL_MAX_SYMLINKS) { |
136 | 0 | errno = ELOOP; |
137 | 0 | *error_r = "Too many symlink dereferences"; |
138 | 0 | return -1; |
139 | 0 | } |
140 | | |
141 | | /* allocate space for preserving tail of previous symlink and |
142 | | first attempt at reading symlink with room for the tail |
143 | | |
144 | | buffer will look like this: |
145 | | [npath][0][preserved tail][link buffer][room for tail][0] |
146 | | */ |
147 | 0 | espace = ltlen + tlen + 2; |
148 | 0 | i_assert(npath_pos >= npath); |
149 | 0 | if ((size_t)((npath_pos - npath) + espace + lsize) >= asize) { |
150 | 0 | ptrdiff_t npath_offset = npath_pos - npath; |
151 | 0 | asize = nearest_power(MALLOC_ADD3(npath_offset, espace, lsize) + 1); |
152 | 0 | lsize = asize - (npath_offset + espace); |
153 | 0 | npath = t_buffer_reget(npath, asize); |
154 | 0 | npath_pos = npath + npath_offset; |
155 | 0 | } |
156 | |
|
157 | 0 | if (ltlen > 0) { |
158 | | /* preserve tail just after end of npath */ |
159 | 0 | i_assert(npath_pos >= npath); |
160 | 0 | i_assert((size_t)((npath_pos + 1 - npath) + ltlen) < asize); |
161 | 0 | memmove(npath_pos + 1, segend, ltlen); |
162 | 0 | } |
163 | | |
164 | | /* read the symlink after the preserved tail */ |
165 | 0 | for (;;) { |
166 | 0 | npath_link = (npath_pos + 1) + ltlen; |
167 | |
|
168 | 0 | i_assert(npath_link >= npath_pos); |
169 | 0 | i_assert((size_t)((npath_link - npath) + lsize) < asize); |
170 | | |
171 | | /* attempt to read the link */ |
172 | 0 | if ((ret=readlink(npath, npath_link, lsize)) < 0) { |
173 | 0 | *error_r = t_strdup_printf("readlink() failed: %m"); |
174 | 0 | return -1; |
175 | 0 | } |
176 | 0 | if ((size_t)ret < lsize) { |
177 | | /* POSIX doesn't guarantee the presence of a NIL */ |
178 | 0 | npath_link[ret] = '\0'; |
179 | 0 | break; |
180 | 0 | } |
181 | | |
182 | | /* sum of new symlink content length |
183 | | * and path tail length may not |
184 | | exceed maximum */ |
185 | 0 | if ((size_t)(ret + tlen) >= PATH_UTIL_MAX_PATH) { |
186 | 0 | errno = ENAMETOOLONG; |
187 | 0 | *error_r = "Resulting path is too long"; |
188 | 0 | return -1; |
189 | 0 | } |
190 | | |
191 | | /* try again with bigger buffer, |
192 | | we need to allocate more space as well if lsize == ret, |
193 | | because the returned link may have gotten truncated */ |
194 | 0 | espace = ltlen + tlen + 2; |
195 | 0 | i_assert(npath_pos >= npath); |
196 | 0 | if ((size_t)((npath_pos - npath) + espace + lsize) >= asize || |
197 | 0 | lsize == (size_t)ret) { |
198 | 0 | ptrdiff_t npath_offset = npath_pos - npath; |
199 | 0 | asize = nearest_power(MALLOC_ADD3(npath_offset, espace, lsize) + 1); |
200 | 0 | lsize = asize - (npath_offset + espace); |
201 | 0 | npath = t_buffer_reget(npath, asize); |
202 | 0 | npath_pos = npath + npath_offset; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | /* add tail of previous path at end of symlink */ |
207 | 0 | i_assert(npath_link >= npath); |
208 | 0 | if (ltlen > 0) { |
209 | 0 | i_assert(npath_pos >= npath); |
210 | 0 | i_assert((size_t)((npath_pos - npath) + 1 + tlen) < asize); |
211 | 0 | i_assert((size_t)((npath_link - npath) + ret + tlen) < asize); |
212 | 0 | memcpy(npath_link + ret, npath_pos + 1, tlen); |
213 | 0 | } else { |
214 | 0 | i_assert((size_t)((npath_link - npath) + ret + tlen) < asize); |
215 | 0 | memcpy(npath_link + ret, segend, tlen); |
216 | 0 | } |
217 | 0 | *(npath_link+ret+tlen) = '\0'; |
218 | | |
219 | | /* use as new source path */ |
220 | 0 | path = segend = npath_link; |
221 | |
|
222 | 0 | if (path[0] == '/') { |
223 | | /* absolute symlink; start over at root */ |
224 | 0 | npath_pos = npath + 1; |
225 | 0 | } else { |
226 | | /* relative symlink; back up to previous segment */ |
227 | 0 | i_assert(npath_pos >= npath); |
228 | 0 | if ((npath_pos - npath) > 1) { |
229 | 0 | if (*(npath_pos-1) == '/') |
230 | 0 | npath_pos--; |
231 | 0 | for (; *(npath_pos-1) != '/'; npath_pos--); |
232 | 0 | } |
233 | 0 | } |
234 | |
|
235 | 0 | } else if (*segend != '\0' && !S_ISDIR (st.st_mode)) { |
236 | | /* not last segment, but not a directory either */ |
237 | 0 | errno = ENOTDIR; |
238 | 0 | *error_r = t_strdup_printf("Not a directory: %s", npath); |
239 | 0 | return -1; |
240 | 0 | } |
241 | 0 | } |
242 | | |
243 | 0 | p = segend; |
244 | 0 | } |
245 | | |
246 | 0 | i_assert(npath_pos >= npath); |
247 | 0 | i_assert((size_t)(npath_pos - npath) < asize); |
248 | | |
249 | | /* remove any trailing slash */ |
250 | 0 | if ((npath_pos - npath) > 1 && *(npath_pos-1) == '/') |
251 | 0 | npath_pos--; |
252 | 0 | *npath_pos = '\0'; |
253 | |
|
254 | 0 | t_buffer_alloc(npath_pos - npath + 1); |
255 | 0 | *npath_r = npath; |
256 | 0 | return 0; |
257 | 0 | } |
258 | | |
259 | | int t_normpath(const char *path, const char **npath_r, const char **error_r) |
260 | 0 | { |
261 | 0 | return path_normalize(path, FALSE, npath_r, error_r); |
262 | 0 | } |
263 | | |
264 | | int t_normpath_to(const char *path, const char *root, const char **npath_r, |
265 | | const char **error_r) |
266 | 0 | { |
267 | 0 | i_assert(path != NULL); |
268 | 0 | i_assert(root != NULL); |
269 | 0 | i_assert(npath_r != NULL); |
270 | | |
271 | 0 | if (*path == '/') |
272 | 0 | return t_normpath(path, npath_r, error_r); |
273 | | |
274 | 0 | return t_normpath(t_strconcat(root, "/", path, NULL), npath_r, error_r); |
275 | 0 | } |
276 | | |
277 | | int t_normpath_confine(const char *path, const char *unexpanded_path, |
278 | | const char **npath_r, const char **error_r) |
279 | 0 | { |
280 | 0 | const char *npath, *var, *prefix, *nprefix; |
281 | 0 | size_t nprefix_len; |
282 | 0 | bool dir_prefix, inside; |
283 | |
|
284 | 0 | if (t_normpath(path, &npath, error_r) < 0) |
285 | 0 | return -1; |
286 | | |
287 | | /* "~/" is expanded to the home directory before the %variables, so |
288 | | the literal prefix is not part of the expanded path. */ |
289 | 0 | if (unexpanded_path[0] == '~' && |
290 | 0 | (unexpanded_path[1] == '/' || unexpanded_path[1] == '\0')) |
291 | 0 | var = NULL; |
292 | 0 | else |
293 | 0 | var = strstr(unexpanded_path, "%{"); |
294 | 0 | if (var == NULL || var == unexpanded_path) { |
295 | | /* nothing to confine to */ |
296 | 0 | *npath_r = npath; |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | 0 | prefix = t_strdup_until(unexpanded_path, var); |
301 | | /* A prefix ending with '/' names a directory: the path must be that |
302 | | directory or below it. An empty trailing %variable expands to the |
303 | | directory itself, e.g. /var/index/%{user | domain} for a user |
304 | | without a domain. */ |
305 | 0 | dir_prefix = prefix[strlen(prefix)-1] == '/'; |
306 | 0 | if (prefix[0] != '/') |
307 | 0 | nprefix = prefix; |
308 | 0 | else if (dir_prefix) { |
309 | | /* normalize the prefix the same way as the path, so e.g. |
310 | | "/var//mail/%{user}" works */ |
311 | 0 | if (t_normpath(prefix, &nprefix, error_r) < 0) |
312 | 0 | return -1; |
313 | 0 | } else { |
314 | | /* The prefix ends inside a path component, e.g. |
315 | | "/var/mail-%{domain}". Normalize only the directories before |
316 | | it and keep the partial component as it is, so that |
317 | | "/var/mail/..%{user}" doesn't confine to "/var". */ |
318 | 0 | const char *frag = strrchr(prefix, '/') + 1, *ndir; |
319 | |
|
320 | 0 | if (t_normpath(t_strdup_until(prefix, frag), &ndir, |
321 | 0 | error_r) < 0) |
322 | 0 | return -1; |
323 | 0 | nprefix = strcmp(ndir, "/") == 0 ? |
324 | 0 | t_strconcat("/", frag, NULL) : |
325 | 0 | t_strconcat(ndir, "/", frag, NULL); |
326 | 0 | } |
327 | 0 | nprefix_len = strlen(nprefix); |
328 | 0 | if (dir_prefix && strcmp(nprefix, "/") == 0) |
329 | 0 | inside = TRUE; |
330 | 0 | else if (dir_prefix) { |
331 | 0 | inside = strncmp(npath, nprefix, nprefix_len) == 0 && |
332 | 0 | (npath[nprefix_len] == '/' || |
333 | 0 | npath[nprefix_len] == '\0'); |
334 | 0 | } else { |
335 | 0 | inside = strncmp(npath, nprefix, nprefix_len) == 0; |
336 | 0 | } |
337 | 0 | if (!inside) { |
338 | 0 | *error_r = t_strdup_printf("Path %s is outside '%s'", |
339 | 0 | npath, prefix); |
340 | 0 | return -1; |
341 | 0 | } |
342 | 0 | *npath_r = npath; |
343 | 0 | return 0; |
344 | 0 | } |
345 | | |
346 | | int t_realpath(const char *path, const char **npath_r, const char **error_r) |
347 | 0 | { |
348 | 0 | return path_normalize(path, TRUE, npath_r, error_r); |
349 | 0 | } |
350 | | |
351 | | int t_realpath_to(const char *path, const char *root, const char **npath_r, |
352 | | const char **error_r) |
353 | 0 | { |
354 | 0 | i_assert(path != NULL); |
355 | 0 | i_assert(root != NULL); |
356 | 0 | i_assert(npath_r != NULL); |
357 | | |
358 | 0 | if (*path == '/') |
359 | 0 | return t_realpath(path, npath_r, error_r); |
360 | | |
361 | 0 | return t_realpath(t_strconcat(root, "/", path, NULL), npath_r, error_r); |
362 | 0 | } |
363 | | |
364 | | int t_abspath(const char *path, const char **abspath_r, const char **error_r) |
365 | 0 | { |
366 | 0 | i_assert(path != NULL); |
367 | 0 | i_assert(abspath_r != NULL); |
368 | 0 | i_assert(error_r != NULL); |
369 | | |
370 | 0 | if (*path == '/') { |
371 | 0 | *abspath_r = path; |
372 | 0 | return 0; |
373 | 0 | } |
374 | | |
375 | 0 | const char *dir, *error; |
376 | 0 | if (t_get_working_dir(&dir, &error) < 0) { |
377 | 0 | *error_r = t_strconcat("Failed to get working directory: ", |
378 | 0 | error, NULL); |
379 | 0 | return -1; |
380 | 0 | } |
381 | 0 | *abspath_r = t_strconcat(dir, "/", path, NULL); |
382 | 0 | return 0; |
383 | 0 | } |
384 | | |
385 | | const char *t_abspath_to(const char *path, const char *root) |
386 | 0 | { |
387 | 0 | i_assert(path != NULL); |
388 | 0 | i_assert(root != NULL); |
389 | | |
390 | 0 | if (*path == '/') |
391 | 0 | return path; |
392 | | |
393 | 0 | return t_strconcat(root, "/", path, NULL); |
394 | 0 | } |
395 | | |
396 | | int t_get_working_dir(const char **dir_r, const char **error_r) |
397 | 5.04k | { |
398 | 5.04k | char *dir; |
399 | | |
400 | 5.04k | i_assert(dir_r != NULL); |
401 | 5.04k | i_assert(error_r != NULL); |
402 | 5.04k | if (t_getcwd_noalloc(&dir, NULL, error_r) < 0) |
403 | 0 | return -1; |
404 | | |
405 | 5.04k | t_buffer_alloc(strlen(dir) + 1); |
406 | 5.04k | *dir_r = dir; |
407 | 5.04k | return 0; |
408 | 5.04k | } |
409 | | |
410 | | int t_readlink(const char *path, const char **dest_r, const char **error_r) |
411 | 0 | { |
412 | 0 | i_assert(error_r != NULL); |
413 | | |
414 | | /* @UNSAFE */ |
415 | 0 | ssize_t ret; |
416 | 0 | char *dest; |
417 | 0 | size_t size = 128; |
418 | |
|
419 | 0 | dest = t_buffer_get(size); |
420 | 0 | while ((ret = readlink(path, dest, size)) >= (ssize_t)size) { |
421 | 0 | size = nearest_power(size+1); |
422 | 0 | dest = t_buffer_get(size); |
423 | 0 | } |
424 | 0 | if (ret < 0) { |
425 | 0 | *error_r = t_strdup_printf("readlink() failed: %m"); |
426 | 0 | return -1; |
427 | 0 | } |
428 | | |
429 | 0 | dest[ret] = '\0'; |
430 | 0 | t_buffer_alloc(ret + 1); |
431 | 0 | *dest_r = dest; |
432 | 0 | return 0; |
433 | 0 | } |
434 | | |
435 | | /* Iteratively resolve `path` beneath `cur_fd` using openat() with |
436 | | O_NOFOLLOW per component. Symlinks are resolved manually: their targets |
437 | | are validated (non-absolute, no '..' past the base) and the loop |
438 | | restarts with the target spliced into the remaining path. dir_depth |
439 | | tracks how many directory levels we have descended below the original |
440 | | base; '..' is refused when the depth would go negative. |
441 | | */ |
442 | | static int |
443 | | path_openat_safe_walk(int cur_fd, const char *path, int flags, |
444 | | const char **error_r) |
445 | 0 | { |
446 | 0 | bool cur_owned = FALSE; |
447 | 0 | unsigned int symlink_count = 0; |
448 | 0 | unsigned int dir_depth = 0; |
449 | | |
450 | | /* Absolute paths cannot stay beneath the base directory. */ |
451 | 0 | if (*path == '/') { |
452 | 0 | errno = ELOOP; |
453 | 0 | *error_r = "Path is absolute (escapes base directory)"; |
454 | 0 | return -1; |
455 | 0 | } |
456 | | |
457 | 0 | for (;;) { |
458 | 0 | while (*path == '/') |
459 | 0 | path++; |
460 | |
|
461 | 0 | if (*path == '\0') { |
462 | | /* Empty path or trailing slash: open the current |
463 | | directory itself with the requested flags. */ |
464 | 0 | int fd = openat(cur_fd, ".", |
465 | 0 | flags | O_NOFOLLOW | O_CLOEXEC); |
466 | 0 | if (cur_owned) |
467 | 0 | i_close_fd(&cur_fd); |
468 | 0 | if (fd < 0) { |
469 | 0 | *error_r = t_strdup_printf( |
470 | 0 | "openat() failed: %m"); |
471 | 0 | } |
472 | 0 | return fd; |
473 | 0 | } |
474 | | |
475 | 0 | const char *slash = strchr(path, '/'); |
476 | 0 | size_t comp_len = (slash != NULL ? |
477 | 0 | (size_t)(slash - path) : strlen(path)); |
478 | 0 | bool is_last = (slash == NULL); |
479 | |
|
480 | 0 | if (comp_len == 1 && path[0] == '.') { |
481 | 0 | path += comp_len; |
482 | 0 | continue; |
483 | 0 | } |
484 | | |
485 | 0 | if (comp_len == 2 && path[0] == '.' && path[1] == '.') { |
486 | 0 | if (dir_depth == 0) { |
487 | 0 | if (cur_owned) |
488 | 0 | i_close_fd(&cur_fd); |
489 | 0 | errno = ELOOP; |
490 | 0 | *error_r = "Path escapes base directory via '..'"; |
491 | 0 | return -1; |
492 | 0 | } |
493 | 0 | int parent_fd = openat(cur_fd, "..", |
494 | 0 | O_RDONLY | O_DIRECTORY | |
495 | 0 | O_NOFOLLOW | O_CLOEXEC); |
496 | 0 | if (cur_owned) |
497 | 0 | i_close_fd(&cur_fd); |
498 | 0 | if (parent_fd < 0) { |
499 | 0 | *error_r = t_strdup_printf( |
500 | 0 | "openat(..) failed: %m"); |
501 | 0 | return -1; |
502 | 0 | } |
503 | 0 | cur_fd = parent_fd; |
504 | 0 | cur_owned = TRUE; |
505 | 0 | dir_depth--; |
506 | 0 | path += comp_len; |
507 | 0 | continue; |
508 | 0 | } |
509 | | |
510 | 0 | const char *comp = t_strdup_until(path, path + comp_len); |
511 | 0 | int next_fd; |
512 | 0 | if (is_last) { |
513 | 0 | next_fd = openat(cur_fd, comp, |
514 | 0 | flags | O_NOFOLLOW | O_CLOEXEC); |
515 | 0 | } else { |
516 | 0 | next_fd = openat(cur_fd, comp, |
517 | 0 | O_RDONLY | O_DIRECTORY | |
518 | 0 | O_NOFOLLOW | O_CLOEXEC); |
519 | 0 | } |
520 | |
|
521 | 0 | if (next_fd < 0 && (errno == ELOOP || errno == EMLINK)) { |
522 | | /* Component is a symlink. Resolve it ourselves and |
523 | | restart processing with its target spliced into |
524 | | `path`; that way the target is itself walked |
525 | | component-by-component and the boundary check |
526 | | continues to apply. (O_NOFOLLOW on a symlink reports |
527 | | ELOOP on Linux/macOS but EMLINK on the BSDs, so both |
528 | | must be treated as "this is a symlink".) */ |
529 | 0 | if (++symlink_count > |
530 | 0 | PATH_UTIL_OPENAT_SAFE_MAX_SYMLINKS) { |
531 | 0 | if (cur_owned) |
532 | 0 | i_close_fd(&cur_fd); |
533 | 0 | errno = ELOOP; |
534 | 0 | *error_r = "Too many symlink dereferences"; |
535 | 0 | return -1; |
536 | 0 | } |
537 | | |
538 | 0 | char buf[PATH_UTIL_MAX_PATH]; |
539 | 0 | ssize_t llen = readlinkat(cur_fd, comp, |
540 | 0 | buf, sizeof(buf) - 1); |
541 | 0 | if (llen < 0) { |
542 | 0 | if (cur_owned) |
543 | 0 | i_close_fd(&cur_fd); |
544 | 0 | *error_r = t_strdup_printf( |
545 | 0 | "readlinkat(%s) failed: %m", comp); |
546 | 0 | return -1; |
547 | 0 | } |
548 | 0 | buf[llen] = '\0'; |
549 | |
|
550 | 0 | if (buf[0] == '/') { |
551 | 0 | if (cur_owned) |
552 | 0 | i_close_fd(&cur_fd); |
553 | 0 | errno = ELOOP; |
554 | 0 | *error_r = t_strdup_printf( |
555 | 0 | "Symlink '%s' has absolute target " |
556 | 0 | "'%s' (escapes base directory)", |
557 | 0 | comp, buf); |
558 | 0 | return -1; |
559 | 0 | } |
560 | | |
561 | 0 | path = is_last ? t_strdup(buf) : |
562 | 0 | t_strconcat(buf, "/", |
563 | 0 | slash + 1, NULL); |
564 | 0 | continue; |
565 | 0 | } |
566 | | |
567 | 0 | if (next_fd < 0) { |
568 | 0 | if (cur_owned) |
569 | 0 | i_close_fd(&cur_fd); |
570 | 0 | *error_r = t_strdup_printf( |
571 | 0 | "openat(%s) failed: %m", comp); |
572 | 0 | return -1; |
573 | 0 | } |
574 | | |
575 | 0 | if (is_last) { |
576 | 0 | if (cur_owned) |
577 | 0 | i_close_fd(&cur_fd); |
578 | 0 | return next_fd; |
579 | 0 | } |
580 | | |
581 | 0 | if (cur_owned) |
582 | 0 | i_close_fd(&cur_fd); |
583 | 0 | cur_fd = next_fd; |
584 | 0 | cur_owned = TRUE; |
585 | 0 | dir_depth++; |
586 | 0 | path = slash + 1; |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | | int t_openat_safe(int base_fd, const char *path, int flags, |
591 | | const char **error_r) |
592 | 0 | { |
593 | 0 | i_assert(base_fd >= 0); |
594 | 0 | i_assert(path != NULL); |
595 | 0 | i_assert(error_r != NULL); |
596 | | |
597 | 0 | return path_openat_safe_walk(base_fd, path, flags, error_r); |
598 | 0 | } |
599 | | |
600 | | int t_openat_safe_dir(const char *base_dir, const char *path, int flags, |
601 | | const char **error_r) |
602 | 0 | { |
603 | 0 | i_assert(base_dir != NULL); |
604 | 0 | i_assert(path != NULL); |
605 | 0 | i_assert(error_r != NULL); |
606 | | |
607 | 0 | int dir_fd = open(base_dir, |
608 | 0 | O_DIRECTORY | O_RDONLY | O_CLOEXEC | |
609 | 0 | (flags & O_NOFOLLOW)); |
610 | 0 | if (dir_fd < 0) { |
611 | 0 | *error_r = t_strdup_printf("open(%s) failed: %m", base_dir); |
612 | 0 | return -1; |
613 | 0 | } |
614 | 0 | int fd = t_openat_safe(dir_fd, path, flags, error_r); |
615 | 0 | i_close_fd(&dir_fd); |
616 | 0 | return fd; |
617 | 0 | } |
618 | | |
619 | | bool path_has_parent_component(const char *path) |
620 | 0 | { |
621 | 0 | const char *p = path; |
622 | |
|
623 | 0 | while ((p = strstr(p, "..")) != NULL) { |
624 | 0 | if ((p == path || p[-1] == '/') && |
625 | 0 | (p[2] == '\0' || p[2] == '/')) |
626 | 0 | return TRUE; |
627 | 0 | p += 2; |
628 | 0 | } |
629 | 0 | return FALSE; |
630 | 0 | } |
631 | | |
632 | | bool t_binary_abspath(const char **binpath, const char **error_r) |
633 | 0 | { |
634 | 0 | const char *path_env, *const *paths; |
635 | 0 | string_t *path; |
636 | |
|
637 | 0 | if (**binpath == '/') { |
638 | | /* already have absolute path */ |
639 | 0 | return TRUE; |
640 | 0 | } else if (strchr(*binpath, '/') != NULL) { |
641 | | /* relative to current directory */ |
642 | 0 | const char *error; |
643 | 0 | if (t_abspath(*binpath, binpath, &error) < 0) { |
644 | 0 | *error_r = t_strdup_printf("t_abspath(%s) failed: %s", |
645 | 0 | *binpath, error); |
646 | 0 | return FALSE; |
647 | 0 | } |
648 | 0 | return TRUE; |
649 | 0 | } else if ((path_env = getenv("PATH")) != NULL) { |
650 | | /* we have to find our executable from path */ |
651 | 0 | path = t_str_new(256); |
652 | 0 | paths = t_strsplit(path_env, ":"); |
653 | 0 | for (; *paths != NULL; paths++) { |
654 | 0 | str_append(path, *paths); |
655 | 0 | str_append_c(path, '/'); |
656 | 0 | str_append(path, *binpath); |
657 | 0 | if (access(str_c(path), X_OK) == 0) { |
658 | 0 | *binpath = str_c(path); |
659 | 0 | return TRUE; |
660 | 0 | } |
661 | 0 | str_truncate(path, 0); |
662 | 0 | } |
663 | 0 | *error_r = "Could not find the wanted executable from PATH"; |
664 | 0 | return FALSE; |
665 | 0 | } else { |
666 | 0 | *error_r = "PATH environment variable undefined"; |
667 | 0 | return FALSE; |
668 | 0 | } |
669 | 0 | } |