68f90be
diff -up patch-2.6.1/Makefile.in.CVE-2010-4651 patch-2.6.1/Makefile.in
4b3160c
--- patch-2.6.1/Makefile.in.CVE-2010-4651	2009-12-30 12:56:30.000000000 +0000
4b3160c
+++ patch-2.6.1/Makefile.in	2011-02-10 12:29:32.926361705 +0000
68f90be
@@ -192,6 +192,7 @@ installcheck::
68f90be
 TESTS = \
68f90be
 	tests/asymmetric-hunks \
68f90be
 	tests/backup-prefix-suffix \
68f90be
+	tests/bad-filenames \
68f90be
 	tests/corrupt-reject-files \
68f90be
 	tests/create-delete \
68f90be
 	tests/crlf-handling \
4b3160c
diff -up patch-2.6.1/src/common.h.CVE-2010-4651 patch-2.6.1/src/common.h
4b3160c
--- patch-2.6.1/src/common.h.CVE-2010-4651	2011-02-10 12:30:29.142797627 +0000
4b3160c
+++ patch-2.6.1/src/common.h	2011-02-10 12:30:33.566989729 +0000
4b3160c
@@ -169,6 +169,7 @@ XTERN char *revision;			/* prerequisite 
4b3160c
 #endif
68f90be
 
4b3160c
 void fatal_exit (int) __attribute__ ((noreturn));
4b3160c
+void validate_target_name (char const *n);
68f90be
 
4b3160c
 #include <errno.h>
4b3160c
 #if !STDC_HEADERS && !defined errno
4b3160c
diff -up patch-2.6.1/src/patch.c.CVE-2010-4651 patch-2.6.1/src/patch.c
4b3160c
--- patch-2.6.1/src/patch.c.CVE-2010-4651	2011-02-10 12:30:20.721432124 +0000
4b3160c
+++ patch-2.6.1/src/patch.c	2011-02-10 12:30:33.567989772 +0000
4b3160c
@@ -34,6 +34,7 @@
4b3160c
 #include <util.h>
4b3160c
 #include <version.h>
4b3160c
 #include <xalloc.h>
4b3160c
+#include <dirname.h>
4b3160c
 
4b3160c
 /* procedures */
4b3160c
 
4b3160c
@@ -916,6 +917,26 @@ numeric_string (char const *string,
4b3160c
   return value;
68f90be
 }
68f90be
 
4b3160c
+void
68f90be
+validate_target_name (char const *n)
68f90be
+{
68f90be
+  char const *p = n;
4b3160c
+  if (explicit_inname)
4b3160c
+    return;
68f90be
+  if (IS_ABSOLUTE_FILE_NAME (p))
68f90be
+    fatal ("rejecting absolute target file name: %s", quotearg (p));
68f90be
+  while (*p)
68f90be
+    {
68f90be
+      if (*p == '.' && *++p == '.' && ( ! *++p || ISSLASH (*p)))
68f90be
+	fatal ("rejecting target file name with \"..\" component: %s",
68f90be
+	       quotearg (n));
68f90be
+      while (*p && ! ISSLASH (*p))
68f90be
+	p++;
68f90be
+      while (ISSLASH (*p))
68f90be
+	p++;
68f90be
+    }
68f90be
+}
68f90be
+
4b3160c
 /* Attempt to find the right place to apply this hunk of patch. */
4b3160c
 
4b3160c
 static LINENUM
4b3160c
diff -up patch-2.6.1/src/pch.c.CVE-2010-4651 patch-2.6.1/src/pch.c
4b3160c
--- patch-2.6.1/src/pch.c.CVE-2010-4651	2009-12-30 12:56:30.000000000 +0000
4b3160c
+++ patch-2.6.1/src/pch.c	2011-02-10 12:30:33.573990033 +0000
4b3160c
@@ -3,7 +3,7 @@
4b3160c
 /* Copyright (C) 1986, 1987, 1988 Larry Wall
4b3160c
 
4b3160c
    Copyright (C) 1990, 1991, 1992, 1993, 1997, 1998, 1999, 2000, 2001,
4b3160c
-   2002, 2003, 2006, 2009 Free Software Foundation, Inc.
4b3160c
+   2002, 2003, 2006, 2009, 2011 Free Software Foundation, Inc.
4b3160c
 
4b3160c
    This program is free software; you can redistribute it and/or modify
4b3160c
    it under the terms of the GNU General Public License as published by
4b3160c
@@ -199,6 +199,8 @@ maybe_reverse (char const *name, bool no
68f90be
 {
68f90be
   bool looks_reversed = (! is_empty) < p_says_nonexistent[reverse ^ is_empty];
68f90be
 
68f90be
+  validate_target_name (name);
68f90be
+
68f90be
   if (looks_reversed)
68f90be
     reverse ^=
68f90be
       ok_to_reverse ("The next patch%s would %s the file %s,\nwhich %s!",
4b3160c
@@ -725,6 +727,7 @@ intuit_diff_type (bool need_header)
68f90be
 	inerrno = stat_errno[i];
68f90be
 	invc = version_controlled[i];
68f90be
 	instat = st[i];
68f90be
+	validate_target_name (inname);
68f90be
       }
68f90be
 
68f90be
     return retval;
68f90be
diff -up patch-2.6.1/tests/bad-filenames.CVE-2010-4651 patch-2.6.1/tests/bad-filenames
4b3160c
--- patch-2.6.1/tests/bad-filenames.CVE-2010-4651	2011-02-10 12:29:32.931361921 +0000
4b3160c
+++ patch-2.6.1/tests/bad-filenames	2011-02-10 12:30:33.576990163 +0000
4b3160c
@@ -0,0 +1,71 @@
68f90be
+# Copyright (C) 2011 Free Software Foundation, Inc.
68f90be
+#
68f90be
+# Copying and distribution of this file, with or without modification,
68f90be
+# in any medium, are permitted without royalty provided the copyright
68f90be
+# notice and this notice are preserved.
68f90be
+
68f90be
+. $srcdir/test-lib.sh
68f90be
+
68f90be
+use_local_patch
68f90be
+use_tmpdir
68f90be
+
68f90be
+# ================================================================
68f90be
+
68f90be
+emit_2()
68f90be
+{
68f90be
+cat <
68f90be
+--- $1
68f90be
++++ $2
68f90be
+@@ -0,0 +1 @@
68f90be
++x
68f90be
+EOF
68f90be
+}
68f90be
+
68f90be
+emit_patch() { emit_2 /dev/null "$1"; }
68f90be
+
68f90be
+# Ensure that patch rejects an output file name that is absolute
68f90be
+# or that contains a ".." component.
68f90be
+
68f90be
+check 'emit_patch /absolute/path | patch -p0; echo status: $?' <
68f90be
+$PATCH: **** rejecting absolute target file name: /absolute/path
68f90be
+status: 2
68f90be
+EOF
68f90be
+
68f90be
+check 'emit_patch a/../z | patch -p0; echo status: $?' <
68f90be
+$PATCH: **** rejecting target file name with ".." component: a/../z
68f90be
+status: 2
68f90be
+EOF
68f90be
+
68f90be
+check 'emit_patch a/../z | patch -p1; echo status: $?' <
68f90be
+$PATCH: **** rejecting target file name with ".." component: ../z
68f90be
+status: 2
68f90be
+EOF
68f90be
+
68f90be
+check 'emit_patch a/.. | patch -p0; echo status: $?' <
68f90be
+$PATCH: **** rejecting target file name with ".." component: a/..
68f90be
+status: 2
68f90be
+EOF
68f90be
+
68f90be
+check 'emit_patch ../z | patch -p0; echo status: $?' <
68f90be
+$PATCH: **** rejecting target file name with ".." component: ../z
68f90be
+status: 2
68f90be
+EOF
68f90be
+
68f90be
+check 'emit_2 /abs/path target | patch -p0; echo status: $?' <
68f90be
+patching file target
68f90be
+status: 0
68f90be
+EOF
68f90be
+
68f90be
+echo x > target
68f90be
+check 'emit_2 /abs/path target | patch -R -p0; echo status: $?' <
68f90be
+patching file target
68f90be
+status: 0
68f90be
+EOF
4b3160c
+
4b3160c
+# Do not validate any file name from the input when the target
4b3160c
+# is specified on the command line:
4b3160c
+touch abs
4b3160c
+check 'emit_patch /absolute/path | patch `pwd`/abs; echo status: $?' <
4b3160c
+patching file `pwd`/abs
4b3160c
+status: 0
4b3160c
+EOF