From accc26a0e543c9f9e2e5adc1ad6c5a2b9d64670d Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Sun, 8 Jan 2023 18:19:11 +0100 Subject: [PATCH] chase-symlinks: Always open a dirfd to the root directory Instead of special casing "/", let's just always open a dirfd to the root directory. --- src/basic/chase-symlinks.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/basic/chase-symlinks.c b/src/basic/chase-symlinks.c index 86a2b7b25cc..3a4fdcd1e02 100644 --- a/src/basic/chase-symlinks.c +++ b/src/basic/chase-symlinks.c @@ -464,30 +464,26 @@ int chase_symlinks( return r; } - if (root) { - path = path_startswith(absolute, root); - if (!path) - return log_full_errno(flags & CHASE_WARN ? LOG_WARNING : LOG_DEBUG, - SYNTHETIC_ERRNO(ECHRNG), - "Specified path '%s' is outside of specified root directory '%s', refusing to resolve.", - absolute, root); - - fd = open(root, O_CLOEXEC|O_DIRECTORY|O_PATH); - if (fd < 0) - return -errno; + path = path_startswith(absolute, empty_to_root(root)); + if (!path) + return log_full_errno(flags & CHASE_WARN ? LOG_WARNING : LOG_DEBUG, + SYNTHETIC_ERRNO(ECHRNG), + "Specified path '%s' is outside of specified root directory '%s', refusing to resolve.", + absolute, empty_to_root(root)); + + fd = open(empty_to_root(root), O_CLOEXEC|O_DIRECTORY|O_PATH); + if (fd < 0) + return -errno; - flags |= CHASE_AT_RESOLVE_IN_ROOT; - } else { - path = absolute; - fd = AT_FDCWD; - } + flags |= CHASE_AT_RESOLVE_IN_ROOT; + flags &= ~CHASE_PREFIX_ROOT; - r = chase_symlinks_at(fd, path, flags & ~CHASE_PREFIX_ROOT, ret_path ? &p : NULL, ret_fd ? &pfd : NULL); + r = chase_symlinks_at(fd, path, flags, ret_path ? &p : NULL, ret_fd ? &pfd : NULL); if (r < 0) return r; if (ret_path) { - char *q = path_join(root, p); + char *q = path_join(empty_to_root(root), p); if (!q) return -ENOMEM; -- 2.47.3