Regular-file extraction used lstat/unlink followed by separate pathname
open and mkdir operations.  This left a time-of-check/time-of-use race and
allowed symlinks in parent directories or replacement targets to redirect
archive writes.

Walk archive paths relative to directory descriptors with openat(), reject
symlinked components with O_NOFOLLOW and O_DIRECTORY, and use descriptor-
based creation for regular files and deferred extraction paths.

Signed-off-by: Mohammed Mustafa <[email protected]>
---
 src/copyin.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 89 insertions(+), 11 deletions(-)

diff --git a/src/copyin.c b/src/copyin.c
index 46d5503..e4c91aa 100644
--- a/src/copyin.c
+++ b/src/copyin.c
@@ -40,6 +40,82 @@
 static void copyin_regular_file(struct cpio_file_stat* file_hdr,
                                int in_file_des);

+#if defined(HAVE_OPENAT) || (defined(__linux__) && defined(HAVE_UNISTD_H))
+static int
+secure_open_output (const char *path, int flags, mode_t mode)
+{
+  char *copy;
+  char *component;
+  char *slash;
+  int dirfd;
+  int fd;
+  int is_absolute;
+
+  copy = xstrdup (path);
+  is_absolute = copy[0] == '/';
+  dirfd = open (is_absolute ? "/" : ".",
+               O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+  if (dirfd < 0)
+    {
+      free (copy);
+      return -1;
+    }
+
+  component = copy;
+  if (is_absolute)
+    {
+      while (*component == '/')
+       ++component;
+    }
+
+  while (*component)
+    {
+      while (*component == '/')
+       ++component;
+      if (*component == '\0')
+       break;
+
+      slash = component;
+      while (*slash && *slash != '/')
+       ++slash;
+      if (*slash)
+       *slash++ = '\0';
+
+      if (*slash == '\0' && slash == component + strlen (component))
+       {
+         fd = openat (dirfd, component, flags | O_NOFOLLOW | O_CLOEXEC, mode);
+         close (dirfd);
+         free (copy);
+         return fd;
+       }
+
+      {
+       int nextfd = openat (dirfd, component,
+                            O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
+       if (nextfd < 0 && errno == ENOENT && create_dir_flag
+           && mkdirat (dirfd, component, 0777) == 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;
+    }
+
+  close (dirfd);
+  free (copy);
+  return -1;
+}
+#else
+#define secure_open_output(path, flags, mode) open (path, flags, mode)
+#endif
+
 void
 warn_junk_bytes (long bytes_skipped)
 {
@@ -370,14 +446,15 @@ create_final_defers (void)
        {
          continue;
        }
-      out_file_des = open (d->header.c_name,
-                          O_CREAT | O_WRONLY | O_BINARY, 0600);
+      out_file_des = secure_open_output (d->header.c_name,
+                                        O_CREAT | O_WRONLY | O_TRUNC | 
O_BINARY,
+                                        0600);
       if (out_file_des < 0 && create_dir_flag)
        {
          create_all_directories (d->header.c_name);
-         out_file_des = open (d->header.c_name,
-                              O_CREAT | O_WRONLY | O_BINARY,
-                              0600);
+         out_file_des = secure_open_output (d->header.c_name,
+                                            O_CREAT | O_WRONLY | O_TRUNC | 
O_BINARY,
+                                            0600);
        }
       if (out_file_des < 0)
        {
@@ -479,15 +556,16 @@ copyin_regular_file (struct cpio_file_stat*
file_hdr, int in_file_des)
        }

       /* If not linked, copy the contents of the file.  */
-      out_file_des = open (file_hdr->c_name,
-                          O_CREAT | O_WRONLY | O_BINARY, 0600);
+      out_file_des = secure_open_output (file_hdr->c_name,
+                                        O_CREAT | O_WRONLY | O_TRUNC | 
O_BINARY,
+                                        0600);

       if (out_file_des < 0 && create_dir_flag)
        {
          create_all_directories (file_hdr->c_name);
-         out_file_des = open (file_hdr->c_name,
-                              O_CREAT | O_WRONLY | O_BINARY,
-                              0600);
+         out_file_des = secure_open_output (file_hdr->c_name,
+                                            O_CREAT | O_WRONLY | O_TRUNC | 
O_BINARY,
+                                            0600);
        }

       if (out_file_des < 0)
@@ -670,7 +748,7 @@ dl_compare (void const *a, void const *b)
 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 fd = secure_open_output (newpath, O_WRONLY | O_CREAT | O_EXCL, 0);
   struct stat st;
   struct delayed_link *p;
   size_t newlen = strlen (newpath);

-- 
 2.53.0

Reply via email to