The symlink placeholder was verified with lstat by name, then unlinked,
replaced and given metadata by re-resolving the same textual path.  A
parent directory swapped for a symlink between those steps redirected
the operations outside the extraction directory.

* src/copyin.c (open_parent_dirfd): New function.  Walk path components
with O_NOFOLLOW and return a directory descriptor.
(struct delayed_link): Record the directory descriptor and basename.
(symlink_placeholder): Create the placeholder relative to the descriptor.
(replace_symlink_placeholders): Verify and finalize with fstatat,
unlinkat, symlinkat, fchownat and fdutimensat on that descriptor.
* tests/symlink-parent-toctou.at: New test.
* tests/Makefile.am (TESTSUITE_AT): Add it.
* tests/testsuite.at: Include it.

Signed-off-by: Mohammed Mustafa <[email protected]>
---
 src/copyin.c                   | 105 +++++++++++++++++++++++++++++----
 tests/Makefile.am              |   3 +-
 tests/symlink-parent-toctou.at |  51 ++++++++++++++++
 tests/testsuite.at             |   2 +
 4 files changed, 147 insertions(+), 14 deletions(-)
 create mode 100644 tests/symlink-parent-toctou.at

diff --git a/src/copyin.c b/src/copyin.c
index 46d5503..6f26d87 100644
--- a/src/copyin.c
+++ b/src/copyin.c
@@ -31,6 +31,7 @@
 # include <fnmatch.h>
 #endif
 #include <hash.h>
+#include <utimens.h>

 #ifndef HAVE_LCHOWN
 # define lchown(f,u,g) 0
@@ -641,6 +642,13 @@ struct delayed_link
     gid_t gid;
     time_t mtime;

+    /* Descriptor for the placeholder's parent directory and the
+       placeholder's name within it.  Keeping both lets finalization
+       resolve the name once instead of re-walking a textual path after
+       the rest of the archive has been extracted.  */
+    int dirfd;
+    char *base;
+
     /* Link source and target names. */
     char *source;
     char target[1];
@@ -667,23 +675,84 @@ dl_compare (void const *a, void const *b)
   return (da->dev == db->dev) & (da->ino == db->ino);
 }

+/* Open a descriptor for the directory holding the final component of
+   PATH.  Each component is opened relative to its parent with O_NOFOLLOW
+   so that a symlink cannot be substituted in any component.  */
+static int
+open_parent_dirfd (const char *path)
+{
+  char *copy = xstrdup (path);
+  char *component = copy;
+  int dirfd = open (".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+
+  if (dirfd < 0)
+    {
+      free (copy);
+      return -1;
+    }
+
+  while (*component)
+    {
+      char *slash = strchr (component, '/');
+      int nextfd;
+
+      if (slash == NULL)
+       break;                  /* The final component is the file itself.  */
+      *slash = '\0';
+      if (*component != '\0')
+       {
+         nextfd = openat (dirfd, component,
+                          O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+         close (dirfd);
+         if (nextfd < 0)
+           {
+             free (copy);
+             return -1;
+           }
+         dirfd = nextfd;
+       }
+      component = slash + 1;
+    }
+
+  free (copy);
+  return dirfd;
+}
+
 static int
 symlink_placeholder (char *oldpath, char *newpath, struct
cpio_file_stat *file_stat)
 {
-  int fd = open (newpath, O_WRONLY | O_CREAT | O_EXCL, 0);
+  int dirfd = open_parent_dirfd (newpath);
+  const char *base = base_name (newpath);
+  int fd;
   struct stat st;
   struct delayed_link *p;
   size_t newlen = strlen (newpath);

+  if (dirfd < 0)
+    {
+      open_error (newpath);
+      return -1;
+    }
+
+  fd = openat (dirfd, base, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0);
+
   if (fd < 0 && create_dir_flag)
     {
       create_all_directories (newpath);
-      fd = open (newpath, O_WRONLY | O_CREAT | O_EXCL, 0);
+      close (dirfd);
+      dirfd = open_parent_dirfd (newpath);
+      if (dirfd < 0)
+       {
+         open_error (newpath);
+         return -1;
+       }
+      fd = openat (dirfd, base, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0);
     }

   if (fd < 0)
     {
       open_error (newpath);
+      close (dirfd);
       return -1;
     }

@@ -691,6 +760,7 @@ symlink_placeholder (char *oldpath, char *newpath,
struct cpio_file_stat *file_s
     {
       stat_error (newpath);
       close (fd);
+      close (dirfd);
       return -1;
     }

@@ -699,6 +769,8 @@ symlink_placeholder (char *oldpath, char *newpath,
struct cpio_file_stat *file_s
   p = xmalloc (sizeof (*p) + strlen (oldpath) + newlen + 1);
   p->dev = st.st_dev;
   p->ino = st.st_ino;
+  p->dirfd = dirfd;
+  p->base = xstrdup (base);

   p->mode = file_stat->c_mode;
   p->uid = file_stat->c_uid;
@@ -734,20 +806,15 @@ replace_symlink_placeholders (void)
       /* Make sure the placeholder file is still there.  If not,
         don't create a link, as the placeholder was probably
         removed by a later extraction.  */
-      if (lstat (dl->target, &st) == 0
+      if (fstatat (dl->dirfd, dl->base, &st, AT_SYMLINK_NOFOLLOW) == 0
          && st.st_dev == dl->dev
          && st.st_ino == dl->ino)
        {
-         if (unlink (dl->target))
+         if (unlinkat (dl->dirfd, dl->base, 0))
            unlink_error (dl->target);
          else
            {
-             int res = UMASKED_SYMLINK (dl->source, dl->target, dl->mode);
-             if (res < 0 && create_dir_flag)
-               {
-                 create_all_directories (dl->target);
-                 res = UMASKED_SYMLINK (dl->source, dl->target, dl->mode);
-               }
+             int res = symlinkat (dl->source, dl->dirfd, dl->base);
              if (res < 0)
                symlink_error (dl->source, dl->target);
              else
@@ -756,15 +823,27 @@ replace_symlink_placeholders (void)
                    {
                      uid_t uid = set_owner_flag ? set_owner : dl->uid;
                      gid_t gid = set_group_flag ? set_group : dl->gid;
-                     if (lchown (dl->target, uid, gid) < 0 && errno != EPERM)
+                     if (fchownat (dl->dirfd, dl->base, uid, gid,
+                                   AT_SYMLINK_NOFOLLOW) < 0 && errno != EPERM)
                        chown_error_details (dl->target, uid, gid);
                    }
                  if (retain_time_flag)
-                   set_file_times (-1, dl->target, dl->mtime, dl->mtime,
-                                   AT_SYMLINK_NOFOLLOW);
+                   {
+                     struct timespec ts[2];
+                     memset (ts, 0, sizeof ts);
+                     ts[0].tv_sec = dl->mtime;
+                     ts[1].tv_sec = dl->mtime;
+                     if (fdutimensat (-1, dl->dirfd, dl->base, ts,
+                                      AT_SYMLINK_NOFOLLOW) < 0
+                         && errno != EROFS)
+                       error (0, errno, _("cannot set times of %s"),
+                              quote (dl->target));
+                   }
                }
            }
        }
+      close (dl->dirfd);
+      free (dl->base);
     }

   hash_free (delayed_link_table);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e5096e5..ae14884 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -58,7 +58,8 @@ TESTSUITE_AT = \
  CVE-2019-14866.at\
  linktime.at\
  linktime01.at\
- symlink-tar-abs.at
+ symlink-tar-abs.at\
+ symlink-parent-toctou.at

 TESTSUITE = $(srcdir)/testsuite

diff --git a/tests/symlink-parent-toctou.at b/tests/symlink-parent-toctou.at
new file mode 100644
index 0000000..80c76ac
--- /dev/null
+++ b/tests/symlink-parent-toctou.at
@@ -0,0 +1,51 @@
+# Symlinked parent components are not followed  -*- Autotest -*-
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# With --no-absolute-filenames, a symlink member is first created as a
+# placeholder and finalized after the rest of the archive is extracted.
+# If a parent component of the member name is a symlink, re-resolving that
+# name at finalization writes through the symlink.  The parent directory
+# must be walked with O_NOFOLLOW and the placeholder resolved relative to
+# the retained descriptor.
+
+AT_SETUP([symlink placeholder parent is not followed])
+
+AT_KEYWORDS([symlink toctou extraction])
+
+AT_CHECK([
+mkdir real
+ln -s real link
+ln -s target real/foo
+printf 'link/foo\n' | cpio -o --format=newc > archive
+],
+[0], [], [ignore])
+
+AT_CHECK([
+mkdir out
+cd out
+mkdir real
+ln -s real link
+],
+[0], [], [])
+
+AT_CHECK([
+cpio -i --directory=out --no-absolute-filenames --format=newc < archive
+],
+[2], [], [ignore])
+
+AT_CHECK([test ! -L out/link/foo], [0], [], [])
+
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 099c1e9..14017ad 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -49,3 +49,5 @@ m4_include([linktime01.at])


 m4_include([symlink-tar-abs.at])
+
+m4_include([symlink-parent-toctou.at])
-- 
2.53.0

Reply via email to