1From 123eaff0d5d1aebe128295959435b9ca5909c26d Mon Sep 17 00:00:00 2001
2From: Andreas Gruenbacher <agruen@gnu.org>
3Date: Fri, 6 Apr 2018 12:14:49 +0200
4Subject: [PATCH] Fix arbitrary command execution in ed-style patches
5 (CVE-2018-1000156)
6
7* src/pch.c (do_ed_script): Write ed script to a temporary file instead
8of piping it to ed: this will cause ed to abort on invalid commands
9instead of rejecting them and carrying on.
10* tests/ed-style: New test case.
11* tests/Makefile.am (TESTS): Add test case.
12
13[baruch: drop test hunks to avoid autoreconf]
14Signed-off-by: Baruch Siach <baruch@tkos.co.il>
15---
16Upstream status: commit 123eaff0d5d1
17
18 src/pch.c         | 91 ++++++++++++++++++++++++++++++++++++++++---------------
19 tests/Makefile.am |  1 +
20 tests/ed-style    | 41 +++++++++++++++++++++++++
21 3 files changed, 108 insertions(+), 25 deletions(-)
22 create mode 100644 tests/ed-style
23
24diff --git a/src/pch.c b/src/pch.c
25index 0c5cc2623079..4fd5a05a6f5c 100644
26--- a/src/pch.c
27+++ b/src/pch.c
28@@ -33,6 +33,7 @@
29 # include <io.h>
30 #endif
31 #include <safe.h>
32+#include <sys/wait.h>
33
34 #define INITHUNKMAX 125			/* initial dynamic allocation size */
35
36@@ -2389,24 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
37     static char const editor_program[] = EDITOR_PROGRAM;
38
39     file_offset beginning_of_this_line;
40-    FILE *pipefp = 0;
41     size_t chars_read;
42+    FILE *tmpfp = 0;
43+    char const *tmpname;
44+    int tmpfd;
45+    pid_t pid;
46+
47+    if (! dry_run && ! skip_rest_of_patch)
48+      {
49+	/* Write ed script to a temporary file.  This causes ed to abort on
50+	   invalid commands such as when line numbers or ranges exceed the
51+	   number of available lines.  When ed reads from a pipe, it rejects
52+	   invalid commands and treats the next line as a new command, which
53+	   can lead to arbitrary command execution.  */
54+
55+	tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
56+	if (tmpfd == -1)
57+	  pfatal ("Can't create temporary file %s", quotearg (tmpname));
58+	tmpfp = fdopen (tmpfd, "w+b");
59+	if (! tmpfp)
60+	  pfatal ("Can't open stream for file %s", quotearg (tmpname));
61+      }
62
63-    if (! dry_run && ! skip_rest_of_patch) {
64-	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
65-	if (inerrno != ENOENT)
66-	  {
67-	    *outname_needs_removal = true;
68-	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
69-	  }
70-	sprintf (buf, "%s %s%s", editor_program,
71-		 verbosity == VERBOSE ? "" : "- ",
72-		 outname);
73-	fflush (stdout);
74-	pipefp = popen(buf, binary_transput ? "wb" : "w");
75-	if (!pipefp)
76-	  pfatal ("Can't open pipe to %s", quotearg (buf));
77-    }
78     for (;;) {
79 	char ed_command_letter;
80 	beginning_of_this_line = file_tell (pfp);
81@@ -2417,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
82 	}
83 	ed_command_letter = get_ed_command_letter (buf);
84 	if (ed_command_letter) {
85-	    if (pipefp)
86-		if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
87+	    if (tmpfp)
88+		if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
89 		    write_fatal ();
90 	    if (ed_command_letter != 'd' && ed_command_letter != 's') {
91 	        p_pass_comments_through = true;
92 		while ((chars_read = get_line ()) != 0) {
93-		    if (pipefp)
94-			if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
95+		    if (tmpfp)
96+			if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
97 			    write_fatal ();
98 		    if (chars_read == 2  &&  strEQ (buf, ".\n"))
99 			break;
100@@ -2437,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
101 	    break;
102 	}
103     }
104-    if (!pipefp)
105+    if (!tmpfp)
106       return;
107-    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
108-	|| fflush (pipefp) != 0)
109+    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
110+	|| fflush (tmpfp) != 0)
111       write_fatal ();
112-    if (pclose (pipefp) != 0)
113-      fatal ("%s FAILED", editor_program);
114+
115+    if (lseek (tmpfd, 0, SEEK_SET) == -1)
116+      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
117+
118+    if (! dry_run && ! skip_rest_of_patch) {
119+	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
120+	*outname_needs_removal = true;
121+	if (inerrno != ENOENT)
122+	  {
123+	    *outname_needs_removal = true;
124+	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
125+	  }
126+	sprintf (buf, "%s %s%s", editor_program,
127+		 verbosity == VERBOSE ? "" : "- ",
128+		 outname);
129+	fflush (stdout);
130+
131+	pid = fork();
132+	if (pid == -1)
133+	  pfatal ("Can't fork");
134+	else if (pid == 0)
135+	  {
136+	    dup2 (tmpfd, 0);
137+	    execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
138+	    _exit (2);
139+	  }
140+	else
141+	  {
142+	    int wstatus;
143+	    if (waitpid (pid, &wstatus, 0) == -1
144+	        || ! WIFEXITED (wstatus)
145+		|| WEXITSTATUS (wstatus) != 0)
146+	      fatal ("%s FAILED", editor_program);
147+	  }
148+    }
149+
150+    fclose (tmpfp);
151+    safe_unlink (tmpname);
152
153     if (ofp)
154       {
155--
1562.16.3
157
158