6c1aee8
commit 123eaff0d5d1aebe128295959435b9ca5909c26d
6c1aee8
Author: Andreas Gruenbacher <agruen@gnu.org>
6c1aee8
Date:   Fri Apr 6 12:14:49 2018 +0200
6c1aee8
6c1aee8
    Fix arbitrary command execution in ed-style patches (CVE-2018-1000156)
6c1aee8
    
6c1aee8
    * src/pch.c (do_ed_script): Write ed script to a temporary file instead
6c1aee8
    of piping it to ed: this will cause ed to abort on invalid commands
6c1aee8
    instead of rejecting them and carrying on.
6c1aee8
    * tests/ed-style: New test case.
6c1aee8
    * tests/Makefile.am (TESTS): Add test case.
6c1aee8
6c1aee8
diff --git a/src/pch.c b/src/pch.c
6c1aee8
index 0c5cc26..4fd5a05 100644
6c1aee8
--- a/src/pch.c
6c1aee8
+++ b/src/pch.c
3aad410
@@ -33,6 +33,7 @@
3aad410
 # include <io.h>
3aad410
 #endif
3aad410
 #include <safe.h>
3aad410
+#include <sys/wait.h>
3aad410
 
3aad410
 #define INITHUNKMAX 125			/* initial dynamic allocation size */
3aad410
 
6c1aee8
@@ -2389,24 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
3aad410
     static char const editor_program[] = EDITOR_PROGRAM;
3aad410
 
3aad410
     file_offset beginning_of_this_line;
3aad410
-    FILE *pipefp = 0;
3aad410
     size_t chars_read;
3aad410
+    FILE *tmpfp = 0;
3aad410
+    char const *tmpname;
6c1aee8
+    int tmpfd;
3aad410
+    pid_t pid;
3aad410
+
3aad410
+    if (! dry_run && ! skip_rest_of_patch)
3aad410
+      {
6c1aee8
+	/* Write ed script to a temporary file.  This causes ed to abort on
6c1aee8
+	   invalid commands such as when line numbers or ranges exceed the
6c1aee8
+	   number of available lines.  When ed reads from a pipe, it rejects
6c1aee8
+	   invalid commands and treats the next line as a new command, which
6c1aee8
+	   can lead to arbitrary command execution.  */
3aad410
+
6c1aee8
+	tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
6c1aee8
+	if (tmpfd == -1)
6c1aee8
+	  pfatal ("Can't create temporary file %s", quotearg (tmpname));
6c1aee8
+	tmpfp = fdopen (tmpfd, "w+b");
6c1aee8
+	if (! tmpfp)
6c1aee8
+	  pfatal ("Can't open stream for file %s", quotearg (tmpname));
3aad410
+      }
3aad410
 
3aad410
-    if (! dry_run && ! skip_rest_of_patch) {
3aad410
-	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
6c1aee8
-	if (inerrno != ENOENT)
6c1aee8
-	  {
6c1aee8
-	    *outname_needs_removal = true;
6c1aee8
-	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
6c1aee8
-	  }
3aad410
-	sprintf (buf, "%s %s%s", editor_program,
3aad410
-		 verbosity == VERBOSE ? "" : "- ",
3aad410
-		 outname);
3aad410
-	fflush (stdout);
3aad410
-	pipefp = popen(buf, binary_transput ? "wb" : "w");
3aad410
-	if (!pipefp)
3aad410
-	  pfatal ("Can't open pipe to %s", quotearg (buf));
3aad410
-    }
3aad410
     for (;;) {
3aad410
 	char ed_command_letter;
3aad410
 	beginning_of_this_line = file_tell (pfp);
6c1aee8
@@ -2417,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
3aad410
 	}
3aad410
 	ed_command_letter = get_ed_command_letter (buf);
3aad410
 	if (ed_command_letter) {
3aad410
-	    if (pipefp)
3aad410
-		if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
3aad410
+	    if (tmpfp)
3aad410
+		if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
3aad410
 		    write_fatal ();
3aad410
 	    if (ed_command_letter != 'd' && ed_command_letter != 's') {
3aad410
 	        p_pass_comments_through = true;
3aad410
 		while ((chars_read = get_line ()) != 0) {
3aad410
-		    if (pipefp)
3aad410
-			if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
3aad410
+		    if (tmpfp)
3aad410
+			if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
3aad410
 			    write_fatal ();
3aad410
 		    if (chars_read == 2  &&  strEQ (buf, ".\n"))
3aad410
 			break;
6c1aee8
@@ -2437,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
3aad410
 	    break;
3aad410
 	}
3aad410
     }
3aad410
-    if (!pipefp)
3aad410
+    if (!tmpfp)
3aad410
       return;
3aad410
-    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
3aad410
-	|| fflush (pipefp) != 0)
3aad410
+    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
3aad410
+	|| fflush (tmpfp) != 0)
3aad410
       write_fatal ();
3aad410
-    if (pclose (pipefp) != 0)
3aad410
-      fatal ("%s FAILED", editor_program);
3aad410
+
3aad410
+    if (lseek (tmpfd, 0, SEEK_SET) == -1)
3aad410
+      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
3aad410
+
3aad410
+    if (! dry_run && ! skip_rest_of_patch) {
6c1aee8
+	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
6c1aee8
+	*outname_needs_removal = true;
6c1aee8
+	if (inerrno != ENOENT)
6c1aee8
+	  {
6c1aee8
+	    *outname_needs_removal = true;
6c1aee8
+	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
6c1aee8
+	  }
6c1aee8
+	sprintf (buf, "%s %s%s", editor_program,
6c1aee8
+		 verbosity == VERBOSE ? "" : "- ",
6c1aee8
+		 outname);
6c1aee8
+	fflush (stdout);
3aad410
+
6c1aee8
+	pid = fork();
6c1aee8
+	if (pid == -1)
6c1aee8
+	  pfatal ("Can't fork");
6c1aee8
+	else if (pid == 0)
6c1aee8
+	  {
6c1aee8
+	    dup2 (tmpfd, 0);
6c1aee8
+	    execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
6c1aee8
+	    _exit (2);
6c1aee8
+	  }
6c1aee8
+	else
6c1aee8
+	  {
6c1aee8
+	    int wstatus;
6c1aee8
+	    if (waitpid (pid, &wstatus, 0) == -1
6c1aee8
+	        || ! WIFEXITED (wstatus)
6c1aee8
+		|| WEXITSTATUS (wstatus) != 0)
6c1aee8
+	      fatal ("%s FAILED", editor_program);
6c1aee8
+	  }
3aad410
+    }
3aad410
+
3aad410
+    fclose (tmpfp);
6c1aee8
+    safe_unlink (tmpname);
3aad410
 
3aad410
     if (ofp)
3aad410
       {
6c1aee8
diff --git a/tests/Makefile.am b/tests/Makefile.am
6c1aee8
index 6b6df63..16f8693 100644
6c1aee8
--- a/tests/Makefile.am
6c1aee8
+++ b/tests/Makefile.am
6c1aee8
@@ -32,6 +32,7 @@ TESTS = \
6c1aee8
 	crlf-handling \
6c1aee8
 	dash-o-append \
6c1aee8
 	deep-directories \
6c1aee8
+	ed-style \
6c1aee8
 	empty-files \
6c1aee8
 	false-match \
6c1aee8
 	fifo \
6c1aee8
diff --git a/tests/ed-style b/tests/ed-style
6c1aee8
new file mode 100644
6c1aee8
index 0000000..d8c0689
6c1aee8
--- /dev/null
6c1aee8
+++ b/tests/ed-style
6c1aee8
@@ -0,0 +1,41 @@
3aad410
+# Copyright (C) 2018 Free Software Foundation, Inc.
3aad410
+#
3aad410
+# Copying and distribution of this file, with or without modification,
3aad410
+# in any medium, are permitted without royalty provided the copyright
3aad410
+# notice and this notice are preserved.
3aad410
+
3aad410
+. $srcdir/test-lib.sh
3aad410
+
6c1aee8
+require cat
3aad410
+use_local_patch
3aad410
+use_tmpdir
3aad410
+
3aad410
+# ==============================================================
3aad410
+
3aad410
+cat > ed1.diff <
3aad410
+0a
3aad410
+foo
3aad410
+.
3aad410
+EOF
3aad410
+
3aad410
+check 'patch -e foo -i ed1.diff' <
3aad410
+EOF
3aad410
+
3aad410
+check 'cat foo' <
3aad410
+foo
3aad410
+EOF
3aad410
+
3aad410
+cat > ed2.diff <
3aad410
+1337a
3aad410
+r !echo bar
3aad410
+,p
3aad410
+EOF
3aad410
+
6c1aee8
+check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <
6c1aee8
+?
3aad410
+Status: 2
3aad410
+EOF
3aad410
+
3aad410
+check 'cat foo' <
3aad410
+foo
3aad410
+EOF