Blame SOURCES/cpio-2.12-improper-input-validation.patch

52ab5b
From: Thomas Habets <habets@google.com>
52ab5b
Subject: [PATCH] Check for size overflow in tar header fields.
52ab5b
52ab5b
This prevents surprising outputs being created, e.g. this cpio tar
52ab5b
output with more than one file:
52ab5b
52ab5b
tar cf suffix.tar AUTHORS
52ab5b
dd if=/dev/zero seek=16G bs=1 count=0 of=suffix.tar
52ab5b
echo suffix.tar | cpio -H tar -o | tar tvf -
52ab5b
52ab5b
-rw-r--r-- 1000/1000       0 2019-08-30 16:40 suffix.tar
52ab5b
-rw-r--r-- thomas/thomas 161 2019-08-30 16:40 AUTHORS
52ab5b
---
52ab5b
 src/copyout.c |  3 +--
52ab5b
 src/extern.h  |  2 +-
52ab5b
 src/tar.c     | 45 ++++++++++++++++++++++++++++++++-------------
52ab5b
 3 files changed, 34 insertions(+), 16 deletions(-)
52ab5b
52ab5b
diff --git a/src/copyout.c b/src/copyout.c
52ab5b
index dcae449..56416ba 100644
52ab5b
--- a/src/copyout.c
52ab5b
+++ b/src/copyout.c
52ab5b
@@ -552,8 +552,7 @@ write_out_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
 	  error (0, 0, _("%s: file name too long"), file_hdr->c_name);
52ab5b
 	  return 1;
52ab5b
 	}
52ab5b
-      write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */
52ab5b
-      return 0;
52ab5b
+      return write_out_tar_header (file_hdr, out_des);
52ab5b
 
52ab5b
     case arf_binary:
52ab5b
       return write_out_binary_header (makedev (file_hdr->c_rdev_maj,
52ab5b
diff --git a/src/extern.h b/src/extern.h
52ab5b
index e27d662..47b477a 100644
52ab5b
--- a/src/extern.h
52ab5b
+++ b/src/extern.h
52ab5b
@@ -145,7 +145,7 @@ int make_path (char *argpath, uid_t owner, gid_t group,
52ab5b
 	       const char *verbose_fmt_string);
52ab5b
 
52ab5b
 /* tar.c */
52ab5b
-void write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des);
52ab5b
+int write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des);
52ab5b
 int null_block (long *block, int size);
52ab5b
 void read_in_tar_header (struct cpio_file_stat *file_hdr, int in_des);
52ab5b
 int otoa (char *s, unsigned long *n);
52ab5b
diff --git a/src/tar.c b/src/tar.c
52ab5b
index e2b5f45..53dc99a 100644
52ab5b
--- a/src/tar.c
52ab5b
+++ b/src/tar.c
52ab5b
@@ -93,8 +93,9 @@ stash_tar_filename (char *prefix, char *filename)
52ab5b
    sprintf (where, "%*lo ", digits - 2, value);
52ab5b
    except that sprintf fills in the trailing NUL and we don't.  */
52ab5b
 
52ab5b
-static void
52ab5b
-to_oct (register long value, register int digits, register char *where)
52ab5b
+static int
52ab5b
+to_oct_or_error (register long value, register int digits, register char *where,
52ab5b
+                 const char *filename, const char *fieldname)
52ab5b
 {
52ab5b
   --digits;			/* Leave the trailing NUL slot alone.  */
52ab5b
 
52ab5b
@@ -105,10 +106,17 @@ to_oct (register long value, register int digits, register char *where)
52ab5b
       value >>= 3;
52ab5b
     }
52ab5b
   while (digits > 0 && value != 0);
52ab5b
+  if (value > 0)
52ab5b
+    {
52ab5b
+      error (0, 0, _("%s: field width not sufficient for storing %s"),
52ab5b
+             filename, fieldname);
52ab5b
+      return 1;
52ab5b
+    }
52ab5b
 
52ab5b
   /* Add leading zeroes, if necessary.  */
52ab5b
   while (digits > 0)
52ab5b
     where[--digits] = '0';
52ab5b
+  return 0;
52ab5b
 }
52ab5b
 
52ab5b
 
52ab5b
@@ -139,7 +147,7 @@ tar_checksum (struct tar_header *tar_hdr)
52ab5b
 /* Write out header FILE_HDR, including the file name, to file
52ab5b
    descriptor OUT_DES.  */
52ab5b
 
52ab5b
-void
52ab5b
+int
52ab5b
 write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
 {
52ab5b
   int name_len;
52ab5b
@@ -168,11 +176,16 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
 
52ab5b
   /* Ustar standard (POSIX.1-1988) requires the mode to contain only 3 octal
52ab5b
      digits */
52ab5b
-  to_oct (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode);
52ab5b
-  to_oct (file_hdr->c_uid, 8, tar_hdr->uid);
52ab5b
-  to_oct (file_hdr->c_gid, 8, tar_hdr->gid);
52ab5b
-  to_oct (file_hdr->c_filesize, 12, tar_hdr->size);
52ab5b
-  to_oct (file_hdr->c_mtime, 12, tar_hdr->mtime);
52ab5b
+  if (to_oct_or_error (file_hdr->c_mode & MODE_ALL, 8, tar_hdr->mode, file_hdr->c_name, _("mode")))
52ab5b
+    return 1;
52ab5b
+  if (to_oct_or_error (file_hdr->c_uid, 8, tar_hdr->uid, file_hdr->c_name, _("uid")))
52ab5b
+    return 1;
52ab5b
+  if (to_oct_or_error (file_hdr->c_gid, 8, tar_hdr->gid, file_hdr->c_name, _("gid")))
52ab5b
+    return 1;
52ab5b
+  if (to_oct_or_error (file_hdr->c_filesize, 12, tar_hdr->size, file_hdr->c_name, _("file size")))
52ab5b
+    return 1;
52ab5b
+  if (to_oct_or_error (file_hdr->c_mtime, 12, tar_hdr->mtime, file_hdr->c_name, _("modification time")))
52ab5b
+    return 1;
52ab5b
 
52ab5b
   switch (file_hdr->c_mode & CP_IFMT)
52ab5b
     {
52ab5b
@@ -184,7 +197,8 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
 	  strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
52ab5b
 		   TARLINKNAMESIZE);
52ab5b
 	  tar_hdr->typeflag = LNKTYPE;
52ab5b
-	  to_oct (0, 12, tar_hdr->size);
52ab5b
+	  if (to_oct_or_error (0, 12, tar_hdr->size, file_hdr->c_name, _("file size")))
52ab5b
+            return 1;
52ab5b
 	}
52ab5b
       else
52ab5b
 	tar_hdr->typeflag = REGTYPE;
52ab5b
@@ -210,7 +224,8 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
 	 than TARLINKNAMESIZE.  */
52ab5b
       strncpy (tar_hdr->linkname, file_hdr->c_tar_linkname,
52ab5b
 	       TARLINKNAMESIZE);
52ab5b
-      to_oct (0, 12, tar_hdr->size);
52ab5b
+      if (to_oct_or_error (0, 12, tar_hdr->size, file_hdr->c_name, _("file size")))
52ab5b
+        return 1;
52ab5b
       break;
52ab5b
 #endif /* CP_IFLNK */
52ab5b
     }
52ab5b
@@ -229,13 +244,17 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
52ab5b
       if (name)
52ab5b
 	strcpy (tar_hdr->gname, name);
52ab5b
 
52ab5b
-      to_oct (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor);
52ab5b
-      to_oct (file_hdr->c_rdev_min, 8, tar_hdr->devminor);
52ab5b
+      if (to_oct_or_error (file_hdr->c_rdev_maj, 8, tar_hdr->devmajor, file_hdr->c_name, _("rdev major")))
52ab5b
+        return 1;
52ab5b
+      if (to_oct_or_error (file_hdr->c_rdev_min, 8, tar_hdr->devminor, file_hdr->c_name, _("rdev minor")))
52ab5b
+        return 1;
52ab5b
     }
52ab5b
 
52ab5b
-  to_oct (tar_checksum (tar_hdr), 8, tar_hdr->chksum);
52ab5b
+  if (to_oct_or_error (tar_checksum (tar_hdr), 8, tar_hdr->chksum, file_hdr->c_name, _("checksum")))
52ab5b
+    return 1;
52ab5b
 
52ab5b
   tape_buffered_write ((char *) &tar_rec, out_des, TARRECORDSIZE);
52ab5b
+  return 0;
52ab5b
 }
52ab5b
 
52ab5b
 /* Return nonzero iff all the bytes in BLOCK are NUL.
52ab5b
-- 
52ab5b
2.26.0
52ab5b