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

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