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