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

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