]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase-symlinks: Always open a dirfd to the root directory 26341/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 8 Jan 2023 17:19:11 +0000 (18:19 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 7 Feb 2023 15:36:55 +0000 (16:36 +0100)
Instead of special casing "/", let's just always open a dirfd to the
root directory.

src/basic/chase-symlinks.c

index 86a2b7b25cc7788888b9d0345a1281e57a9ba6dc..3a4fdcd1e0247b4e53b359b2b9a7a2cab2ebacf6 100644 (file)
@@ -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;