e72bf6d
diff -up patch-2.7.1/NEWS.CVE-2015-1196 patch-2.7.1/NEWS
e72bf6d
diff -up patch-2.7.1/src/pch.c.CVE-2015-1196 patch-2.7.1/src/pch.c
e72bf6d
--- patch-2.7.1/src/pch.c.CVE-2015-1196	2015-01-20 12:23:34.808516117 +0000
e72bf6d
+++ patch-2.7.1/src/pch.c	2015-01-20 12:24:15.763652714 +0000
e72bf6d
@@ -454,6 +454,60 @@ name_is_valid (char const *name)
e72bf6d
   return is_valid;
e72bf6d
 }
e72bf6d
 
e72bf6d
+bool
e72bf6d
+symlink_target_is_valid (char const *target, char const *to)
e72bf6d
+{
e72bf6d
+  bool is_valid;
e72bf6d
+
e72bf6d
+  if (IS_ABSOLUTE_FILE_NAME (to))
e72bf6d
+    is_valid = true;
e72bf6d
+  else if (IS_ABSOLUTE_FILE_NAME (target))
e72bf6d
+    is_valid = false;
e72bf6d
+  else
e72bf6d
+    {
e72bf6d
+      unsigned int depth = 0;
e72bf6d
+      char const *t;
e72bf6d
+
e72bf6d
+      is_valid = true;
e72bf6d
+      t = to;
e72bf6d
+      while (*t)
e72bf6d
+	{
e72bf6d
+	  while (*t && ! ISSLASH (*t))
e72bf6d
+	    t++;
e72bf6d
+	  if (ISSLASH (*t))
e72bf6d
+	    {
e72bf6d
+	      while (ISSLASH (*t))
e72bf6d
+		t++;
e72bf6d
+	      depth++;
e72bf6d
+	    }
e72bf6d
+	}
e72bf6d
+
e72bf6d
+      t = target;
e72bf6d
+      while (*t)
e72bf6d
+	{
e72bf6d
+	  if (*t == '.' && *++t == '.' && (! *++t || ISSLASH (*t)))
e72bf6d
+	    {
e72bf6d
+	      if (! depth--)
e72bf6d
+		{
e72bf6d
+		  is_valid = false;
e72bf6d
+		  break;
e72bf6d
+		}
e72bf6d
+	    }
e72bf6d
+	  else
e72bf6d
+	    {
e72bf6d
+	      while (*t && ! ISSLASH (*t))
e72bf6d
+		t++;
e72bf6d
+	      depth++;
e72bf6d
+	    }
e72bf6d
+	  while (ISSLASH (*t))
e72bf6d
+	    t++;
e72bf6d
+	}
e72bf6d
+    }
e72bf6d
+
e72bf6d
+  /* Allow any symlink target if we are in the filesystem root.  */
e72bf6d
+  return is_valid || cwd_is_root (to);
e72bf6d
+}
e72bf6d
+
e72bf6d
 /* Determine what kind of diff is in the remaining part of the patch file. */
e72bf6d
 
e72bf6d
 static enum diff
e72bf6d
diff -up patch-2.7.1/src/pch.h.CVE-2015-1196 patch-2.7.1/src/pch.h
e72bf6d
--- patch-2.7.1/src/pch.h.CVE-2015-1196	2012-09-22 18:37:21.000000000 +0100
e72bf6d
+++ patch-2.7.1/src/pch.h	2015-01-20 12:24:15.763652714 +0000
e72bf6d
@@ -37,6 +37,7 @@ bool pch_write_line (lin, FILE *);
e72bf6d
 bool there_is_another_patch (bool, mode_t *);
e72bf6d
 char *pfetch (lin) _GL_ATTRIBUTE_PURE;
e72bf6d
 char pch_char (lin) _GL_ATTRIBUTE_PURE;
e72bf6d
+bool symlink_target_is_valid (char const *, char const *);
e72bf6d
 int another_hunk (enum diff, bool);
e72bf6d
 int pch_says_nonexistent (bool) _GL_ATTRIBUTE_PURE;
e72bf6d
 size_t pch_line_len (lin) _GL_ATTRIBUTE_PURE;
e72bf6d
diff -up patch-2.7.1/src/util.c.CVE-2015-1196 patch-2.7.1/src/util.c
e72bf6d
--- patch-2.7.1/src/util.c.CVE-2015-1196	2015-01-20 12:23:34.808516117 +0000
e72bf6d
+++ patch-2.7.1/src/util.c	2015-01-20 12:24:15.764652717 +0000
e72bf6d
@@ -478,6 +478,13 @@ move_file (char const *from, bool *from_
e72bf6d
 	    read_fatal ();
e72bf6d
 	  buffer[size] = 0;
e72bf6d
 
e72bf6d
+	  if (! symlink_target_is_valid (buffer, to))
e72bf6d
+	    {
e72bf6d
+	      fprintf (stderr, "symbolic link target '%s' is invalid\n",
e72bf6d
+		       buffer);
e72bf6d
+	      fatal_exit (0);
e72bf6d
+	    }
e72bf6d
+
e72bf6d
 	  if (! backup)
e72bf6d
 	    {
e72bf6d
 	      if (unlink (to) == 0)
e72bf6d
diff -up patch-2.7.1/tests/symlinks.CVE-2015-1196 patch-2.7.1/tests/symlinks
e72bf6d
--- patch-2.7.1/tests/symlinks.CVE-2015-1196	2012-09-19 02:18:42.000000000 +0100
e72bf6d
+++ patch-2.7.1/tests/symlinks	2015-01-20 12:24:15.764652717 +0000
e72bf6d
@@ -146,6 +146,59 @@ ncheck 'test ! -L symlink'
e72bf6d
 
e72bf6d
 # --------------------------------------------------------------
e72bf6d
 
e72bf6d
+# Patch should not create symlinks which point outside the working directory.
e72bf6d
+
e72bf6d
+cat > symlink-target.diff <
e72bf6d
+diff --git a/dir/foo b/dir/foo
e72bf6d
+new file mode 120000
e72bf6d
+index 0000000..cad2309
e72bf6d
+--- /dev/null
e72bf6d
++++ b/dir/foo
e72bf6d
+@@ -0,0 +1 @@
e72bf6d
++../foo
e72bf6d
+\ No newline at end of file
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+check 'patch -p1 < symlink-target.diff || echo "Status: $?"' <
e72bf6d
+patching symbolic link dir/foo
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+cat > bad-symlink-target1.diff <
e72bf6d
+diff --git a/bar b/bar
e72bf6d
+new file mode 120000
e72bf6d
+index 0000000..cad2309
e72bf6d
+--- /dev/null
e72bf6d
++++ b/bar
e72bf6d
+@@ -0,0 +1 @@
e72bf6d
++/bar
e72bf6d
+\ No newline at end of file
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+check 'patch -p1 < bad-symlink-target1.diff || echo "Status: $?"' <
e72bf6d
+patching symbolic link bar
e72bf6d
+symbolic link target '/bar' is invalid
e72bf6d
+Status: 2
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+cat > bad-symlink-target2.diff <
e72bf6d
+diff --git a/baz b/baz
e72bf6d
+new file mode 120000
e72bf6d
+index 0000000..cad2309
e72bf6d
+--- /dev/null
e72bf6d
++++ b/baz
e72bf6d
+@@ -0,0 +1 @@
e72bf6d
++../baz
e72bf6d
+\ No newline at end of file
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+check 'patch -p1 < bad-symlink-target2.diff || echo "Status: $?"' <
e72bf6d
+patching symbolic link baz
e72bf6d
+symbolic link target '../baz' is invalid
e72bf6d
+Status: 2
e72bf6d
+EOF
e72bf6d
+
e72bf6d
+# --------------------------------------------------------------
e72bf6d
+
e72bf6d
 # The backup file of a new symlink is an empty regular file.
e72bf6d
 
e72bf6d
 check 'patch -p1 --backup < create-symlink.diff || echo "Status: $?"' <