]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
chase-symlinks: avoid using stack allocation for potentially huge paths
authorLennart Poettering <lennart@poettering.net>
Tue, 12 Apr 2022 13:41:48 +0000 (15:41 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 12 Apr 2022 15:07:49 +0000 (17:07 +0200)
let's better be safe and use heap allocation for paths which might be
unbounded.

In particular as previously we copied the stack memory to heap anyway,
via a noop path_make_absolute_cwd() call.

src/basic/chase-symlinks.c

index 8558944a69ce96057abf2ef3e09b056c01855120..3e2f2a66221aebefc71da38bcf8de99f549f75c4 100644 (file)
@@ -84,6 +84,10 @@ int chase_symlinks(
         if (isempty(path))
                 return -EINVAL;
 
+        /* We don't support relative paths in combination with a root directory */
+        if (FLAGS_SET(flags, CHASE_PREFIX_ROOT) && !path_is_absolute(path))
+                return -EINVAL;
+
         /* This is a lot like canonicalize_file_name(), but takes an additional "root" parameter, that allows following
          * symlinks relative to a root directory, instead of the root of the host.
          *
@@ -161,17 +165,17 @@ int chase_symlinks(
                 path_simplify(root);
 
                 if (flags & CHASE_PREFIX_ROOT) {
-                        /* We don't support relative paths in combination with a root directory */
-                        if (!path_is_absolute(path))
-                                return -EINVAL;
-
-                        path = prefix_roota(root, path);
+                        buffer = path_join(root, path);
+                        if (!buffer)
+                                return -ENOMEM;
                 }
         }
 
-        r = path_make_absolute_cwd(path, &buffer);
-        if (r < 0)
-                return r;
+        if (!buffer) {
+                r = path_make_absolute_cwd(path, &buffer);
+                if (r < 0)
+                        return r;
+        }
 
         fd = open(root ?: "/", O_CLOEXEC|O_DIRECTORY|O_PATH);
         if (fd < 0)