Blame SOURCES/cpio-2.11-CVE-2014-9112.patch

f0f11f
diff --git a/src/copyin.c b/src/copyin.c
f0f11f
index d505407..db8ee66 100644
f0f11f
--- a/src/copyin.c
f0f11f
+++ b/src/copyin.c
f0f11f
@@ -124,10 +124,30 @@ tape_skip_padding (int in_file_des, off_t offset)
f0f11f
   if (pad != 0)
f0f11f
     tape_toss_input (in_file_des, pad);
f0f11f
 }
f0f11f
-
f0f11f
+
f0f11f
+static char *
f0f11f
+get_link_name (struct cpio_file_stat *file_hdr, int in_file_des)
f0f11f
+{
f0f11f
+  char *link_name;
f0f11f
+  
f0f11f
+  if (file_hdr->c_filesize < 0 || file_hdr->c_filesize > SIZE_MAX-1)
f0f11f
+    {
f0f11f
+      error (0, 0, _("%s: stored filename length is out of range"),
f0f11f
+	     file_hdr->c_name);
f0f11f
+      link_name = NULL;
f0f11f
+    }
f0f11f
+  else
f0f11f
+    {
f0f11f
+      link_name = xmalloc (file_hdr->c_filesize + 1);
f0f11f
+      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
f0f11f
+      link_name[file_hdr->c_filesize] = '\0';
f0f11f
+      tape_skip_padding (in_file_des, file_hdr->c_filesize);
f0f11f
+    }
f0f11f
+  return link_name;
f0f11f
+}
f0f11f
 
f0f11f
 static void
f0f11f
-list_file(struct cpio_file_stat* file_hdr, int in_file_des)
f0f11f
+list_file (struct cpio_file_stat* file_hdr, int in_file_des)
f0f11f
 {
f0f11f
   if (verbose_flag)
f0f11f
     {
f0f11f
@@ -136,21 +156,16 @@ list_file(struct cpio_file_stat* file_hdr, int in_file_des)
f0f11f
 	{
f0f11f
 	  if (archive_format != arf_tar && archive_format != arf_ustar)
f0f11f
 	    {
f0f11f
-	      char *link_name = NULL;	/* Name of hard and symbolic links.  */
f0f11f
-
f0f11f
-	      link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
f0f11f
-	      link_name[file_hdr->c_filesize] = '\0';
f0f11f
-	      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
f0f11f
-	      long_format (file_hdr, link_name);
f0f11f
-	      free (link_name);
f0f11f
-	      tape_skip_padding (in_file_des, file_hdr->c_filesize);
f0f11f
-	      return;
f0f11f
+	      char *link_name = get_link_name (file_hdr, in_file_des);
f0f11f
+	      if (link_name)
f0f11f
+		{
f0f11f
+		  long_format (file_hdr, link_name);
f0f11f
+		  free (link_name);
f0f11f
+		}
f0f11f
 	    }
f0f11f
 	  else
f0f11f
-	    {
f0f11f
-	      long_format (file_hdr, file_hdr->c_tar_linkname);
f0f11f
-	      return;
f0f11f
-	    }
f0f11f
+	    long_format (file_hdr, file_hdr->c_tar_linkname);
f0f11f
+	  return;
f0f11f
 	}
f0f11f
       else
f0f11f
 #endif
f0f11f
@@ -650,10 +665,7 @@ copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
f0f11f
 
f0f11f
   if (archive_format != arf_tar && archive_format != arf_ustar)
f0f11f
     {
f0f11f
-      link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
f0f11f
-      link_name[file_hdr->c_filesize] = '\0';
f0f11f
-      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
f0f11f
-      tape_skip_padding (in_file_des, file_hdr->c_filesize);
f0f11f
+      link_name = get_link_name (file_hdr, in_file_des);
f0f11f
     }
f0f11f
   else
f0f11f
     {
f0f11f
diff --git a/tests/Makefile.am b/tests/Makefile.am
f0f11f
index b3e8e60..cf186da 100644
f0f11f
--- a/tests/Makefile.am
f0f11f
+++ b/tests/Makefile.am
f0f11f
@@ -52,6 +52,8 @@ TESTSUITE_AT = \
f0f11f
  setstat04.at\
f0f11f
  setstat05.at\
f0f11f
  symlink.at\
f0f11f
+ symlink-bad-length.at\
f0f11f
+ symlink-long.at\
f0f11f
  version.at
f0f11f
 
f0f11f
 TESTSUITE = $(srcdir)/testsuite
f0f11f
diff --git a/tests/symlink-bad-length.at b/tests/symlink-bad-length.at
f0f11f
new file mode 100644
f0f11f
index 0000000..cbf4aa7
f0f11f
--- /dev/null
f0f11f
+++ b/tests/symlink-bad-length.at
f0f11f
@@ -0,0 +1,49 @@
f0f11f
+# Process this file with autom4te to create testsuite.  -*- Autotest -*-
f0f11f
+# Copyright (C) 2014 Free Software Foundation, Inc.
f0f11f
+
f0f11f
+# This program is free software; you can redistribute it and/or modify
f0f11f
+# it under the terms of the GNU General Public License as published by
f0f11f
+# the Free Software Foundation; either version 3, or (at your option)
f0f11f
+# any later version.
f0f11f
+
f0f11f
+# This program is distributed in the hope that it will be useful,
f0f11f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f0f11f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f0f11f
+# GNU General Public License for more details.
f0f11f
+
f0f11f
+# You should have received a copy of the GNU General Public License
f0f11f
+# along with this program; if not, write to the Free Software
f0f11f
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
f0f11f
+# 02110-1301 USA.
f0f11f
+
f0f11f
+# Cpio v2.11 did segfault with badly set symlink length.
f0f11f
+# References:
f0f11f
+# http://lists.gnu.org/archive/html/bug-cpio/2014-11/msg00007.html
f0f11f
+
f0f11f
+AT_SETUP([symlink-bad-length])
f0f11f
+AT_KEYWORDS([symlink-long copyout])
f0f11f
+
f0f11f
+AT_DATA([ARCHIVE.base64],
f0f11f
+[x3EjAIBAtIEtJy8nAQAAAHRUYW0FAAAADQBGSUxFAABzb21lIGNvbnRlbnQKAMdxIwBgQ/+hLScv
f0f11f
+JwEAAAB0VEhuBQD/////TElOSwAARklMRcdxAAAAAAAAAAAAAAEAAAAAAAAACwAAAAAAVFJBSUxF
f0f11f
+UiEhIQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
f0f11f
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
f0f11f
+])
f0f11f
+
f0f11f
+AT_CHECK([
f0f11f
+base64 -d ARCHIVE.base64 > ARCHIVE || AT_SKIP_TEST
f0f11f
+cpio -ntv < ARCHIVE
f0f11f
+test $? -eq 2
f0f11f
+],
f0f11f
+[0],
f0f11f
+[-rw-rw-r--   1 10029    10031          13 Nov 25 13:52 FILE
f0f11f
+],[cpio: LINK: stored filename length is out of range
f0f11f
+cpio: premature end of file
f0f11f
+])
f0f11f
+
f0f11f
+AT_CLEANUP
f0f11f
diff --git a/tests/symlink-long.at b/tests/symlink-long.at
f0f11f
new file mode 100644
f0f11f
index 0000000..d3def2d
f0f11f
--- /dev/null
f0f11f
+++ b/tests/symlink-long.at
f0f11f
@@ -0,0 +1,46 @@
f0f11f
+# Process this file with autom4te to create testsuite.  -*- Autotest -*-
f0f11f
+# Copyright (C) 2014 Free Software Foundation, Inc.
f0f11f
+
f0f11f
+# This program is free software; you can redistribute it and/or modify
f0f11f
+# it under the terms of the GNU General Public License as published by
f0f11f
+# the Free Software Foundation; either version 3, or (at your option)
f0f11f
+# any later version.
f0f11f
+
f0f11f
+# This program is distributed in the hope that it will be useful,
f0f11f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f0f11f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f0f11f
+# GNU General Public License for more details.
f0f11f
+
f0f11f
+# You should have received a copy of the GNU General Public License
f0f11f
+# along with this program; if not, write to the Free Software
f0f11f
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
f0f11f
+# 02110-1301 USA.
f0f11f
+
f0f11f
+# Cpio v2.11.90 changed the way symlink name is read from archive.
f0f11f
+# References:
f0f11f
+# http://lists.gnu.org/archive/html/bug-cpio/2014-11/msg00007.html
f0f11f
+
f0f11f
+AT_SETUP([symlink-long])
f0f11f
+AT_KEYWORDS([symlink-long copyout])
f0f11f
+
f0f11f
+AT_CHECK([
f0f11f
+
f0f11f
+# len(dirname) > READBUFSIZE
f0f11f
+dirname=
f0f11f
+for i in {1..52}; do
f0f11f
+    dirname="xxxxxxxxx/$dirname"
f0f11f
+    mkdir "$dirname"
f0f11f
+done
f0f11f
+ln -s "$dirname" x || AT_SKIP_TEST
f0f11f
+
f0f11f
+echo x | cpio -o > ar
f0f11f
+list=`cpio -tv < ar | sed 's|.*-> ||'`
f0f11f
+test "$list" = "$dirname" && echo success || echo fail
f0f11f
+],
f0f11f
+[0],
f0f11f
+[success
f0f11f
+],[2 blocks
f0f11f
+2 blocks
f0f11f
+])
f0f11f
+
f0f11f
+AT_CLEANUP
f0f11f
diff --git a/tests/testsuite.at b/tests/testsuite.at
f0f11f
index 8f3330b..590bdcb 100644
f0f11f
--- a/tests/testsuite.at
f0f11f
+++ b/tests/testsuite.at
f0f11f
@@ -31,6 +31,8 @@ m4_include([version.at])
f0f11f
 
f0f11f
 m4_include([inout.at])
f0f11f
 m4_include([symlink.at])
f0f11f
+m4_include([symlink-bad-length.at])
f0f11f
+m4_include([symlink-long.at])
f0f11f
 m4_include([interdir.at])
f0f11f
 
f0f11f
 m4_include([setstat01.at])