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

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