e72bf6d
diff -up patch-2.7.1/src/pch.c.CVE-2015-1196 patch-2.7.1/src/pch.c
0885656
--- patch-2.7.1/src/pch.c.CVE-2015-1196	2012-09-22 18:44:33.000000000 +0100
0885656
+++ patch-2.7.1/src/pch.c	2015-01-20 13:29:14.304859557 +0000
0885656
@@ -387,29 +387,6 @@ skip_hex_digits (char const *str)
0885656
   return s == str ? NULL : s;
e72bf6d
 }
e72bf6d
 
0885656
-/* Check if we are in the root of a particular filesystem namespace ("/" on
0885656
-   UNIX or a particular drive's root on DOS-like systems).  */
0885656
-static bool
0885656
-cwd_is_root (char const *name)
0885656
-{
0885656
-  unsigned int prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
0885656
-  char root[prefix_len + 2];
0885656
-  struct stat st;
0885656
-  dev_t root_dev;
0885656
-  ino_t root_ino;
0885656
-
0885656
-  memcpy (root, name, prefix_len);
0885656
-  root[prefix_len] = '/';
0885656
-  root[prefix_len + 1] = 0;
0885656
-  if (stat (root, &st))
0885656
-    return false;
0885656
-  root_dev = st.st_dev;
0885656
-  root_ino = st.st_ino;
0885656
-  if (stat (".", &st))
0885656
-    return false;
0885656
-  return root_dev == st.st_dev && root_ino == st.st_ino;
0885656
-}
0885656
-
0885656
 static bool
0885656
 name_is_valid (char const *name)
0885656
 {
0885656
diff -up patch-2.7.1/src/util.c.CVE-2015-1196 patch-2.7.1/src/util.c
0885656
--- patch-2.7.1/src/util.c.CVE-2015-1196	2012-09-22 21:09:10.000000000 +0100
0885656
+++ patch-2.7.1/src/util.c	2015-01-20 13:29:14.305859561 +0000
0885656
@@ -422,6 +422,60 @@ create_backup (char const *to, const str
0885656
     }
0885656
 }
0885656
 
0885656
+static 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
+
0885656
 /* Move a file FROM (where *FROM_NEEDS_REMOVAL is nonzero if FROM
0885656
    needs removal when cleaning up at the end of execution, and where
0885656
    *FROMST is FROM's status if known),
0885656
@@ -465,6 +519,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)
0885656
@@ -1660,3 +1721,26 @@ int stat_file (char const *filename, str
0885656
 
0885656
   return xstat (filename, st) == 0 ? 0 : errno;
0885656
 }
0885656
+
0885656
+/* Check if we are in the root of a particular filesystem namespace ("/" on
0885656
+   UNIX or a particular drive's root on DOS-like systems).  */
0885656
+bool
0885656
+cwd_is_root (char const *name)
0885656
+{
0885656
+  unsigned int prefix_len = FILE_SYSTEM_PREFIX_LEN (name);
0885656
+  char root[prefix_len + 2];
0885656
+  struct stat st;
0885656
+  dev_t root_dev;
0885656
+  ino_t root_ino;
0885656
+
0885656
+  memcpy (root, name, prefix_len);
0885656
+  root[prefix_len] = '/';
0885656
+  root[prefix_len + 1] = 0;
0885656
+  if (stat (root, &st))
0885656
+    return false;
0885656
+  root_dev = st.st_dev;
0885656
+  root_ino = st.st_ino;
0885656
+  if (stat (".", &st))
0885656
+    return false;
0885656
+  return root_dev == st.st_dev && root_ino == st.st_ino;
0885656
+}
0885656
diff -up patch-2.7.1/src/util.h.CVE-2015-1196 patch-2.7.1/src/util.h
0885656
--- patch-2.7.1/src/util.h.CVE-2015-1196	2012-09-21 21:21:16.000000000 +0100
0885656
+++ patch-2.7.1/src/util.h	2015-01-20 13:29:14.306859564 +0000
0885656
@@ -69,6 +69,7 @@ enum file_id_type lookup_file_id (struct
0885656
 void set_queued_output (struct stat const *, bool);
0885656
 bool has_queued_output (struct stat const *);
0885656
 int stat_file (char const *, struct stat *);
0885656
+bool cwd_is_root (char const *);
0885656
 
0885656
 enum file_attributes {
0885656
   FA_TIMES = 1,
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
0885656
+++ patch-2.7.1/tests/symlinks	2015-01-20 13:29:14.306859564 +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: $?"' <