X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fbasic%2Ffs-util.c;h=4d494650cb916cfcd3fa697cb2884449d44f331a;hb=8622c1b84e198fb2d0626e622dfeaaa446e38f3a;hp=b97edb552c067c7b10b0c805e3aa7ccd8e2efad1;hpb=7b987da65ad4eacb30034519653a61457b688352;p=elogind.git diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index b97edb552..4d494650c 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -236,7 +236,7 @@ int readlink_and_canonicalize(const char *p, const char *root, char **ret) { if (r < 0) return r; - r = chase_symlinks(t, root, &s); + r = chase_symlinks(t, root, 0, &s); if (r < 0) /* If we can't follow up, then let's return the original string, slightly cleaned up. */ *ret = path_kill_slashes(t); @@ -603,10 +603,11 @@ int inotify_add_watch_fd(int fd, int what, uint32_t mask) { return r; } -int chase_symlinks(const char *path, const char *_root, char **ret) { +int chase_symlinks(const char *path, const char *original_root, unsigned flags, char **ret) { _cleanup_free_ char *buffer = NULL, *done = NULL, *root = NULL; _cleanup_close_ int fd = -1; unsigned max_follow = 32; /* how many symlinks to follow before giving up and returning ELOOP */ + bool exists = true; char *todo; int r; @@ -616,8 +617,9 @@ int chase_symlinks(const char *path, const char *_root, char **ret) { * symlinks relative to a root directory, instead of the root of the host. * * Note that "root" primarily matters if we encounter an absolute symlink. It is also used when following - * relative symlinks to ensure they cannot be used to "escape" the root directory. The path parameter passed - * shall *not* be prefixed by it. + * relative symlinks to ensure they cannot be used to "escape" the root directory. The path parameter passed is + * assumed to be already prefixed by it, except if the CHASE_PREFIX_ROOT flag is set, in which case it is first + * prefixed accordingly. * * Algorithmically this operates on two path buffers: "done" are the components of the path we already * processed and resolved symlinks, "." and ".." of. "todo" are the components of the path we still need to @@ -634,16 +636,19 @@ int chase_symlinks(const char *path, const char *_root, char **ret) { * Note: there's also chase_symlinks_prefix() (see below), which as first step prefixes the passed path by the * passed root. */ - r = path_make_absolute_cwd(path, &buffer); - if (r < 0) - return r; - - if (_root) { - r = path_make_absolute_cwd(_root, &root); + if (original_root) { + r = path_make_absolute_cwd(original_root, &root); if (r < 0) return r; + + if (flags & CHASE_PREFIX_ROOT) + path = prefix_roota(root, path); } + r = path_make_absolute_cwd(path, &buffer); + if (r < 0) + return r; + fd = open("/", O_CLOEXEC|O_NOFOLLOW|O_PATH); if (fd < 0) return -errno; @@ -708,8 +713,25 @@ int chase_symlinks(const char *path, const char *_root, char **ret) { /* Otherwise let's see what this is. */ child = openat(fd, first + n, O_CLOEXEC|O_NOFOLLOW|O_PATH); - if (child < 0) + if (child < 0) { + + if (errno == ENOENT && + (flags & CHASE_NONEXISTENT) && + (isempty(todo) || path_is_safe(todo))) { + + /* If CHASE_NONEXISTENT is set, and the path does not exist, then that's OK, return + * what we got so far. But don't allow this if the remaining path contains "../ or "./" + * or something else weird. */ + + if (!strextend(&done, first, todo, NULL)) + return -ENOMEM; + + exists = false; + break; + } + return -errno; + } if (fstat(child, &st) < 0) return -errno; @@ -794,16 +816,6 @@ int chase_symlinks(const char *path, const char *_root, char **ret) { *ret = done; done = NULL; - return 0; + return exists; } #endif // 0 - -int chase_symlinks_prefix(const char *path, const char *root, char **ret) { - const char *t; - - /* Same as chase_symlinks(), but prefixes 'path' by 'root' first. */ - - t = prefix_roota(root, path); - - return chase_symlinks(t, root, ret); -}