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

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