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 (CVE-2018-1000156)
5
6* src/pch.c (do_ed_script): Write ed script to a temporary file instead
7of piping it to ed: this will cause ed to abort on invalid commands
8instead of rejecting them and carrying on.
9* tests/ed-style: New test case.
10* tests/Makefile.am (TESTS): Add test case.
11
12Upstream-Status: Backport [http://git.savannah.gnu.org/cgit/patch.git/commit/?id=123eaff0d5d1aebe128295959435b9ca5909c26d]
13CVE: CVE-2018-1000156
14
15Signed-off-by: Jackie Huang <jackie.huang@windriver.com>
16---
17 src/pch.c         | 91 ++++++++++++++++++++++++++++++++++++++++---------------
18 tests/Makefile.am |  1 +
19 tests/ed-style    | 41 +++++++++++++++++++++++++
20 3 files changed, 108 insertions(+), 25 deletions(-)
21 create mode 100644 tests/ed-style
22
23diff --git a/src/pch.c b/src/pch.c
24index 0c5cc26..4fd5a05 100644
25--- a/src/pch.c
26+++ b/src/pch.c
27@@ -33,6 +33,7 @@
28 # include <io.h>
29 #endif
30 #include <safe.h>
31+#include <sys/wait.h>
32
33 #define INITHUNKMAX 125			/* initial dynamic allocation size */
34
35@@ -2389,24 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
36     static char const editor_program[] = EDITOR_PROGRAM;
37
38     file_offset beginning_of_this_line;
39-    FILE *pipefp = 0;
40     size_t chars_read;
41+    FILE *tmpfp = 0;
42+    char const *tmpname;
43+    int tmpfd;
44+    pid_t pid;
45+
46+    if (! dry_run && ! skip_rest_of_patch)
47+      {
48+	/* Write ed script to a temporary file.  This causes ed to abort on
49+	   invalid commands such as when line numbers or ranges exceed the
50+	   number of available lines.  When ed reads from a pipe, it rejects
51+	   invalid commands and treats the next line as a new command, which
52+	   can lead to arbitrary command execution.  */
53+
54+	tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
55+	if (tmpfd == -1)
56+	  pfatal ("Can't create temporary file %s", quotearg (tmpname));
57+	tmpfp = fdopen (tmpfd, "w+b");
58+	if (! tmpfp)
59+	  pfatal ("Can't open stream for file %s", quotearg (tmpname));
60+      }
61
62-    if (! dry_run && ! skip_rest_of_patch) {
63-	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
64-	if (inerrno != ENOENT)
65-	  {
66-	    *outname_needs_removal = true;
67-	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
68-	  }
69-	sprintf (buf, "%s %s%s", editor_program,
70-		 verbosity == VERBOSE ? "" : "- ",
71-		 outname);
72-	fflush (stdout);
73-	pipefp = popen(buf, binary_transput ? "wb" : "w");
74-	if (!pipefp)
75-	  pfatal ("Can't open pipe to %s", quotearg (buf));
76-    }
77     for (;;) {
78 	char ed_command_letter;
79 	beginning_of_this_line = file_tell (pfp);
80@@ -2417,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
81 	}
82 	ed_command_letter = get_ed_command_letter (buf);
83 	if (ed_command_letter) {
84-	    if (pipefp)
85-		if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
86+	    if (tmpfp)
87+		if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
88 		    write_fatal ();
89 	    if (ed_command_letter != 'd' && ed_command_letter != 's') {
90 	        p_pass_comments_through = true;
91 		while ((chars_read = get_line ()) != 0) {
92-		    if (pipefp)
93-			if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
94+		    if (tmpfp)
95+			if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
96 			    write_fatal ();
97 		    if (chars_read == 2  &&  strEQ (buf, ".\n"))
98 			break;
99@@ -2437,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
100 	    break;
101 	}
102     }
103-    if (!pipefp)
104+    if (!tmpfp)
105       return;
106-    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
107-	|| fflush (pipefp) != 0)
108+    if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
109+	|| fflush (tmpfp) != 0)
110       write_fatal ();
111-    if (pclose (pipefp) != 0)
112-      fatal ("%s FAILED", editor_program);
113+
114+    if (lseek (tmpfd, 0, SEEK_SET) == -1)
115+      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
116+
117+    if (! dry_run && ! skip_rest_of_patch) {
118+	int exclusive = *outname_needs_removal ? 0 : O_EXCL;
119+	*outname_needs_removal = true;
120+	if (inerrno != ENOENT)
121+	  {
122+	    *outname_needs_removal = true;
123+	    copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
124+	  }
125+	sprintf (buf, "%s %s%s", editor_program,
126+		 verbosity == VERBOSE ? "" : "- ",
127+		 outname);
128+	fflush (stdout);
129+
130+	pid = fork();
131+	if (pid == -1)
132+	  pfatal ("Can't fork");
133+	else if (pid == 0)
134+	  {
135+	    dup2 (tmpfd, 0);
136+	    execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
137+	    _exit (2);
138+	  }
139+	else
140+	  {
141+	    int wstatus;
142+	    if (waitpid (pid, &wstatus, 0) == -1
143+	        || ! WIFEXITED (wstatus)
144+		|| WEXITSTATUS (wstatus) != 0)
145+	      fatal ("%s FAILED", editor_program);
146+	  }
147+    }
148+
149+    fclose (tmpfp);
150+    safe_unlink (tmpname);
151
152     if (ofp)
153       {
154diff --git a/tests/Makefile.am b/tests/Makefile.am
155index 6b6df63..16f8693 100644
156--- a/tests/Makefile.am
157+++ b/tests/Makefile.am
158@@ -32,6 +32,7 @@ TESTS = \
159 	crlf-handling \
160 	dash-o-append \
161 	deep-directories \
162+	ed-style \
163 	empty-files \
164 	false-match \
165 	fifo \
166diff --git a/tests/ed-style b/tests/ed-style
167new file mode 100644
168index 0000000..d8c0689
169--- /dev/null
170+++ b/tests/ed-style
171@@ -0,0 +1,41 @@
172+# Copyright (C) 2018 Free Software Foundation, Inc.
173+#
174+# Copying and distribution of this file, with or without modification,
175+# in any medium, are permitted without royalty provided the copyright
176+# notice and this notice are preserved.
177+
178+. $srcdir/test-lib.sh
179+
180+require cat
181+use_local_patch
182+use_tmpdir
183+
184+# ==============================================================
185+
186+cat > ed1.diff <<EOF
187+0a
188+foo
189+.
190+EOF
191+
192+check 'patch -e foo -i ed1.diff' <<EOF
193+EOF
194+
195+check 'cat foo' <<EOF
196+foo
197+EOF
198+
199+cat > ed2.diff <<EOF
200+1337a
201+r !echo bar
202+,p
203+EOF
204+
205+check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <<EOF
206+?
207+Status: 2
208+EOF
209+
210+check 'cat foo' <<EOF
211+foo
212+EOF
213--
2142.7.4
215
216