Blame SOURCES/patch-CVE-2018-1000156.patch

572a74
diff -up patch-2.7.6/src/pch.c.CVE-2018-1000156 patch-2.7.6/src/pch.c
572a74
--- patch-2.7.6/src/pch.c.CVE-2018-1000156	2018-06-19 10:10:41.407826617 +0200
572a74
+++ patch-2.7.6/src/pch.c	2018-06-19 10:11:01.200927524 +0200
572a74
@@ -33,6 +33,7 @@
572a74
 # include <io.h>
572a74
 #endif
572a74
 #include <safe.h>
572a74
+#include <sys/wait.h>
572a74
 
572a74
 #define INITHUNKMAX 125			/* initial dynamic allocation size */
572a74
 
572a74
@@ -2389,22 +2390,28 @@ do_ed_script (char const *inname, char c
572a74
     static char const editor_program[] = EDITOR_PROGRAM;
572a74
 
572a74
     file_offset beginning_of_this_line;
572a74
-    FILE *pipefp = 0;
572a74
     size_t chars_read;
572a74
+    FILE *tmpfp = 0;
572a74
+    char const *tmpname;
572a74
+    int tmpfd;
572a74
+    pid_t pid;
572a74
+
572a74
+    if (! dry_run && ! skip_rest_of_patch)
572a74
+      {
572a74
+	/* Write ed script to a temporary file.  This causes ed to abort on
572a74
+	   invalid commands such as when line numbers or ranges exceed the
572a74
+	   number of available lines.  When ed reads from a pipe, it rejects
572a74
+	   invalid commands and treats the next line as a new command, which
572a74
+	   can lead to arbitrary command execution.  */
572a74
+
572a74
+	tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
572a74
+	if (tmpfd == -1)
572a74
+	  pfatal ("Can't create temporary file %s", quotearg (tmpname));
572a74
+	tmpfp = fdopen (tmpfd, "w+b");
572a74
+	if (! tmpfp)
572a74
+	  pfatal ("Can't open stream for file %s", quotearg (tmpname));
572a74
+      }
572a74
 
572a74
-    if (! dry_run && ! skip_rest_of_patch) {
572a74
-	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
572a74
-	assert (! inerrno);
572a74
-	*outname_needs_removal = true;
572a74
-	copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
572a74
-	sprintf (buf, "%s %s%s", editor_program,
572a74
-		 verbosity == VERBOSE ? "" : "- ",
572a74
-		 outname);
572a74
-	fflush (stdout);
572a74
-	pipefp = popen(buf, binary_transput ? "wb" : "w");
572a74
-	if (!pipefp)
572a74
-	  pfatal ("Can't open pipe to %s", quotearg (buf));
572a74
-    }
572a74
     for (;;) {
572a74
 	char ed_command_letter;
572a74
 	beginning_of_this_line = file_tell (pfp);
572a74
@@ -2415,14 +2422,14 @@ do_ed_script (char const *inname, char c
572a74
 	}
572a74
 	ed_command_letter = get_ed_command_letter (buf);
572a74
 	if (ed_command_letter) {
572a74
-	    if (pipefp)
572a74
-		if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
572a74
+	    if (tmpfp)
572a74
+		if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
572a74
 		    write_fatal ();
572a74
 	    if (ed_command_letter != 'd' && ed_command_letter != 's') {
572a74
 	        p_pass_comments_through = true;
572a74
 		while ((chars_read = get_line ()) != 0) {
572a74
-		    if (pipefp)
572a74
-			if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
572a74
+		    if (tmpfp)
572a74
+			if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
572a74
 			    write_fatal ();
572a74
 		    if (chars_read == 2  &&  strEQ (buf, ".\n"))
572a74
 			break;
572a74
@@ -2435,13 +2442,50 @@ do_ed_script (char const *inname, char c
572a74
 	    break;
572a74
 	}
572a74
     }
572a74
-    if (!pipefp)
572a74
+    if (!tmpfp)
572a74
       return;
572a74
-    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
572a74
-	|| fflush (pipefp) != 0)
572a74
+    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
572a74
+	|| fflush (tmpfp) != 0)
572a74
       write_fatal ();
572a74
-    if (pclose (pipefp) != 0)
572a74
-      fatal ("%s FAILED", editor_program);
572a74
+
572a74
+    if (lseek (tmpfd, 0, SEEK_SET) == -1)
572a74
+      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
572a74
+
572a74
+    if (! dry_run && ! skip_rest_of_patch) {
572a74
+	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
572a74
+	*outname_needs_removal = true;
572a74
+	if (inerrno != ENOENT)
572a74
+	  {
572a74
+	    *outname_needs_removal = true;
572a74
+	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
572a74
+	  }
572a74
+	sprintf (buf, "%s %s%s", editor_program,
572a74
+		 verbosity == VERBOSE ? "" : "- ",
572a74
+		 outname);
572a74
+	fflush (stdout);
572a74
+
572a74
+	pid = fork();
572a74
+	if (pid == -1)
572a74
+	  pfatal ("Can't fork");
572a74
+	else if (pid == 0)
572a74
+	  {
572a74
+	    dup2 (tmpfd, 0);
572a74
+	    execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
572a74
+	    _exit (2);
572a74
+	  }
572a74
+	else
572a74
+	  {
572a74
+	    int wstatus;
572a74
+	    if (waitpid (pid, &wstatus, 0) == -1
572a74
+	        || ! WIFEXITED (wstatus)
572a74
+		|| WEXITSTATUS (wstatus) != 0)
572a74
+	      fatal ("%s FAILED", editor_program);
572a74
+	  }
572a74
+    }
572a74
+
572a74
+    fclose (tmpfp);
572a74
+    safe_unlink (tmpname);
572a74
+    free((char*) tmpname);
572a74
 
572a74
     if (ofp)
572a74
       {
572a74
diff -up patch-2.7.6/tests/ed-style.CVE-2018-1000156 patch-2.7.6/tests/ed-style
572a74
--- patch-2.7.6/tests/ed-style.CVE-2018-1000156	2018-06-19 10:10:41.409826627 +0200
572a74
+++ patch-2.7.6/tests/ed-style	2018-06-19 11:28:43.354641294 +0200
572a74
@@ -0,0 +1,40 @@
572a74
+# Copyright (C) 2018 Free Software Foundation, Inc.
572a74
+#
572a74
+# Copying and distribution of this file, with or without modification,
572a74
+# in any medium, are permitted without royalty provided the copyright
572a74
+# notice and this notice are preserved.
572a74
+
572a74
+. $srcdir/test-lib.sh
572a74
+
572a74
+require cat
572a74
+use_local_patch
572a74
+use_tmpdir
572a74
+
572a74
+# ==============================================================
572a74
+
572a74
+cat > ed1.diff <
572a74
+0a
572a74
+foo
572a74
+.
572a74
+EOF
572a74
+
572a74
+check 'patch -e foo -i ed1.diff' <
572a74
+EOF
572a74
+
572a74
+check 'cat foo' <
572a74
+foo
572a74
+EOF
572a74
+
572a74
+cat > ed2.diff <
572a74
+1337a
572a74
+r !echo bar
572a74
+,p
572a74
+EOF
572a74
+
572a74
+check 'patch -e foo -i ed2.diff > /dev/null 2> /dev/null || echo "Status: $?"' <
572a74
+Status: 2
572a74
+EOF
572a74
+
572a74
+check 'cat foo' <
572a74
+foo
572a74
+EOF
572a74
diff -up patch-2.7.6/tests/Makefile.am.CVE-2018-1000156 patch-2.7.6/tests/Makefile.am
572a74
--- patch-2.7.6/tests/Makefile.am.CVE-2018-1000156	2018-02-03 13:41:49.000000000 +0100
572a74
+++ patch-2.7.6/tests/Makefile.am	2018-06-19 10:10:41.409826627 +0200
572a74
@@ -32,6 +32,7 @@ TESTS = \
572a74
 	crlf-handling \
572a74
 	dash-o-append \
572a74
 	deep-directories \
572a74
+	ed-style \
572a74
 	empty-files \
572a74
 	false-match \
572a74
 	fifo \
572a74
diff -up patch-2.7.6/tests/Makefile.in.CVE-2018-1000156 patch-2.7.6/tests/Makefile.in
572a74
--- patch-2.7.6/tests/Makefile.in.CVE-2018-1000156	2018-02-03 14:33:56.000000000 +0100
572a74
+++ patch-2.7.6/tests/Makefile.in	2018-06-19 10:10:41.409826627 +0200
572a74
@@ -1308,6 +1308,7 @@ TESTS = \
572a74
 	crlf-handling \
572a74
 	dash-o-append \
572a74
 	deep-directories \
572a74
+	ed-style \
572a74
 	empty-files \
572a74
 	false-match \
572a74
 	fifo \
572a74
@@ -1645,6 +1646,13 @@ empty-files.log: empty-files
572a74
 	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
572a74
 	--log-file $$b.log --trs-file $$b.trs \
572a74
 	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
572a74
+	"$$tst" $(AM_TESTS_FD_REDIRECT)
572a74
+ed-style.log: empty-files
572a74
+	@p='ed-style'; \
572a74
+	b='ed-style'; \
572a74
+	$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
572a74
+	--log-file $$b.log --trs-file $$b.trs \
572a74
+	$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
572a74
 	"$$tst" $(AM_TESTS_FD_REDIRECT)
572a74
 false-match.log: false-match
572a74
 	@p='false-match'; \