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

b46ebc
diff -up patch-2.7.1/src/pch.c.CVE-2018-1000156 patch-2.7.1/src/pch.c
b46ebc
--- patch-2.7.1/src/pch.c.CVE-2018-1000156	2012-09-22 19:44:33.000000000 +0200
b46ebc
+++ patch-2.7.1/src/pch.c	2018-04-13 22:19:04.509532690 +0200
b46ebc
@@ -31,6 +31,7 @@
b46ebc
 #if HAVE_SETMODE_DOS
b46ebc
 # include <io.h>
b46ebc
 #endif
b46ebc
+#include <sys/wait.h>
b46ebc
 
b46ebc
 #define INITHUNKMAX 125			/* initial dynamic allocation size */
b46ebc
 
b46ebc
@@ -2381,22 +2382,28 @@ do_ed_script (char const *inname, char c
b46ebc
     static char const editor_program[] = EDITOR_PROGRAM;
b46ebc
 
b46ebc
     file_offset beginning_of_this_line;
b46ebc
-    FILE *pipefp = 0;
b46ebc
     size_t chars_read;
b46ebc
+    FILE *tmpfp = 0;
b46ebc
+    char const *tmpname;
b46ebc
+    int tmpfd = -1;
b46ebc
+    pid_t pid;
b46ebc
+
b46ebc
+    if (! dry_run && ! skip_rest_of_patch)
b46ebc
+      {
b46ebc
+       /* Write ed script to a temporary file.  This causes ed to abort on
b46ebc
+          invalid commands such as when line numbers or ranges exceed the
b46ebc
+          number of available lines.  When ed reads from a pipe, it rejects
b46ebc
+          invalid commands and treats the next line as a new command, which
b46ebc
+          can lead to arbitrary command execution.  */
b46ebc
+
b46ebc
+       tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
b46ebc
+       if (tmpfd == -1)
b46ebc
+         pfatal ("Can't create temporary file %s", quotearg (tmpname));
b46ebc
+       tmpfp = fdopen (tmpfd, "w+b");
b46ebc
+       if (! tmpfp)
b46ebc
+         pfatal ("Can't open stream for file %s", quotearg (tmpname));
b46ebc
+      }
b46ebc
 
b46ebc
-    if (! dry_run && ! skip_rest_of_patch) {
b46ebc
-	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
b46ebc
-	assert (! inerrno);
b46ebc
-	*outname_needs_removal = true;
b46ebc
-	copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
b46ebc
-	sprintf (buf, "%s %s%s", editor_program,
b46ebc
-		 verbosity == VERBOSE ? "" : "- ",
b46ebc
-		 outname);
b46ebc
-	fflush (stdout);
b46ebc
-	pipefp = popen(buf, binary_transput ? "wb" : "w");
b46ebc
-	if (!pipefp)
b46ebc
-	  pfatal ("Can't open pipe to %s", quotearg (buf));
b46ebc
-    }
b46ebc
     for (;;) {
b46ebc
 	char ed_command_letter;
b46ebc
 	beginning_of_this_line = file_tell (pfp);
b46ebc
@@ -2407,14 +2414,14 @@ do_ed_script (char const *inname, char c
b46ebc
 	}
b46ebc
 	ed_command_letter = get_ed_command_letter (buf);
b46ebc
 	if (ed_command_letter) {
b46ebc
-	    if (pipefp)
b46ebc
-		if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
b46ebc
+	    if (tmpfp)
b46ebc
+		if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
b46ebc
 		    write_fatal ();
b46ebc
 	    if (ed_command_letter != 'd' && ed_command_letter != 's') {
b46ebc
 	        p_pass_comments_through = true;
b46ebc
 		while ((chars_read = get_line ()) != 0) {
b46ebc
-		    if (pipefp)
b46ebc
-			if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
b46ebc
+		    if (tmpfp)
b46ebc
+			if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
b46ebc
 			    write_fatal ();
b46ebc
 		    if (chars_read == 2  &&  strEQ (buf, ".\n"))
b46ebc
 			break;
b46ebc
@@ -2427,13 +2434,50 @@ do_ed_script (char const *inname, char c
b46ebc
 	    break;
b46ebc
 	}
b46ebc
     }
b46ebc
-    if (!pipefp)
b46ebc
+    if (!tmpfp)
b46ebc
       return;
b46ebc
-    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
b46ebc
-	|| fflush (pipefp) != 0)
b46ebc
+    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
b46ebc
+	|| fflush (tmpfp) != 0)
b46ebc
       write_fatal ();
b46ebc
-    if (pclose (pipefp) != 0)
b46ebc
-      fatal ("%s FAILED", editor_program);
b46ebc
+
b46ebc
+    if (lseek (tmpfd, 0, SEEK_SET) == -1)
b46ebc
+      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
b46ebc
+
b46ebc
+    if (! dry_run && ! skip_rest_of_patch) {
b46ebc
+       int exclusive = *outname_needs_removal ? 0 : O_EXCL;
b46ebc
+       *outname_needs_removal = true;
b46ebc
+       if (inerrno != ENOENT)
b46ebc
+         {
b46ebc
+           *outname_needs_removal = true;
b46ebc
+           copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
b46ebc
+         }
b46ebc
+       sprintf (buf, "%s %s%s", editor_program,
b46ebc
+                verbosity == VERBOSE ? "" : "- ",
b46ebc
+                outname);
b46ebc
+       fflush (stdout);
b46ebc
+
b46ebc
+       pid = fork();
b46ebc
+       if (pid == -1)
b46ebc
+         pfatal ("Can't fork");
b46ebc
+       else if (pid == 0)
b46ebc
+         {
b46ebc
+           dup2 (tmpfd, 0);
b46ebc
+           execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
b46ebc
+           _exit (2);
b46ebc
+         }
b46ebc
+       else
b46ebc
+         {
b46ebc
+           int wstatus;
b46ebc
+           if (waitpid (pid, &wstatus, 0) == -1
b46ebc
+               || ! WIFEXITED (wstatus)
b46ebc
+               || WEXITSTATUS (wstatus) != 0)
b46ebc
+             fatal ("%s FAILED", editor_program);
b46ebc
+         }
b46ebc
+    }
b46ebc
+
b46ebc
+    fclose (tmpfp);
b46ebc
+    unlink (tmpname);
b46ebc
+    free((char*) tmpname);
b46ebc
 
b46ebc
     if (ofp)
b46ebc
       {
b46ebc
diff -up patch-2.7.1/tests/ed-style.CVE-2018-1000156 patch-2.7.1/tests/ed-style
b46ebc
--- patch-2.7.1/tests/ed-style.CVE-2018-1000156	2018-04-13 22:17:57.367258994 +0200
b46ebc
+++ patch-2.7.1/tests/ed-style	2018-04-13 22:17:57.367258994 +0200
b46ebc
@@ -0,0 +1,40 @@
b46ebc
+# Copyright (C) 2018 Free Software Foundation, Inc.
b46ebc
+#
b46ebc
+# Copying and distribution of this file, with or without modification,
b46ebc
+# in any medium, are permitted without royalty provided the copyright
b46ebc
+# notice and this notice are preserved.
b46ebc
+
b46ebc
+. $srcdir/test-lib.sh
b46ebc
+
b46ebc
+require_cat
b46ebc
+use_local_patch
b46ebc
+use_tmpdir
b46ebc
+
b46ebc
+# ==============================================================
b46ebc
+
b46ebc
+cat > ed1.diff <
b46ebc
+0a
b46ebc
+foo
b46ebc
+.
b46ebc
+EOF
b46ebc
+
b46ebc
+check 'patch -e foo -i ed1.diff' <
b46ebc
+EOF
b46ebc
+
b46ebc
+check 'cat foo' <
b46ebc
+foo
b46ebc
+EOF
b46ebc
+
b46ebc
+cat > ed2.diff <
b46ebc
+1337a
b46ebc
+r !echo bar
b46ebc
+,p
b46ebc
+EOF
b46ebc
+
b46ebc
+check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <
b46ebc
+Status: 2
b46ebc
+EOF
b46ebc
+
b46ebc
+check 'cat foo' <
b46ebc
+foo
b46ebc
+EOF
b46ebc
diff -up patch-2.7.1/tests/Makefile.am.CVE-2018-1000156 patch-2.7.1/tests/Makefile.am
b46ebc
--- patch-2.7.1/tests/Makefile.am.CVE-2018-1000156	2018-04-13 22:17:57.360258966 +0200
b46ebc
+++ patch-2.7.1/tests/Makefile.am	2018-04-13 22:17:57.367258994 +0200
b46ebc
@@ -28,6 +28,7 @@ TESTS = \
b46ebc
 	criss-cross \
b46ebc
 	crlf-handling \
b46ebc
 	dash-o-append \
b46ebc
+	ed-style \
b46ebc
 	empty-files \
b46ebc
 	fifo \
b46ebc
 	file-modes \
b46ebc
diff -up patch-2.7.1/tests/Makefile.in.orig patch-2.7.1/tests/Makefile.in
b46ebc
--- patch-2.7.1/tests/Makefile.in.orig	2018-04-13 22:24:23.758796422 +0200
b46ebc
+++ patch-2.7.1/tests/Makefile.in	2018-04-13 22:26:09.860216420 +0200
b46ebc
@@ -1083,6 +1083,7 @@ TESTS = \
b46ebc
 	criss-cross \
b46ebc
 	crlf-handling \
b46ebc
 	dash-o-append \
b46ebc
+	ed-style \
b46ebc
 	empty-files \
b46ebc
 	fifo \
b46ebc
 	file-modes \
b46ebc
@@ -1310,6 +1311,8 @@ crlf-handling.log: crlf-handling
b46ebc
 	@p='crlf-handling'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
b46ebc
 dash-o-append.log: dash-o-append
b46ebc
 	@p='dash-o-append'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
b46ebc
+ed-style.log: ed-style
b46ebc
+	@p='ed-style'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
b46ebc
 empty-files.log: empty-files
b46ebc
 	@p='empty-files'; $(am__check_pre) $(LOG_COMPILE) "$$tst" $(am__check_post)
b46ebc
 fifo.log: fifo