tools: patch: Fix build by not modifing Makefile.am
[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 --- a/src/pch.c
20 +++ b/src/pch.c
21 @@ -33,6 +33,7 @@
22 # include <io.h>
23 #endif
24 #include <safe.h>
25 +#include <sys/wait.h>
26
27 #define INITHUNKMAX 125 /* initial dynamic allocation size */
28
29 @@ -2388,22 +2389,28 @@ do_ed_script (char const *inname, char c
30 static char const editor_program[] = EDITOR_PROGRAM;
31
32 file_offset beginning_of_this_line;
33 - FILE *pipefp = 0;
34 size_t chars_read;
35 + FILE *tmpfp = 0;
36 + char const *tmpname;
37 + int tmpfd;
38 + pid_t pid;
39 +
40 + if (! dry_run && ! skip_rest_of_patch)
41 + {
42 + /* Write ed script to a temporary file. This causes ed to abort on
43 + invalid commands such as when line numbers or ranges exceed the
44 + number of available lines. When ed reads from a pipe, it rejects
45 + invalid commands and treats the next line as a new command, which
46 + can lead to arbitrary command execution. */
47 +
48 + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
49 + if (tmpfd == -1)
50 + pfatal ("Can't create temporary file %s", quotearg (tmpname));
51 + tmpfp = fdopen (tmpfd, "w+b");
52 + if (! tmpfp)
53 + pfatal ("Can't open stream for file %s", quotearg (tmpname));
54 + }
55
56 - if (! dry_run && ! skip_rest_of_patch) {
57 - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
58 - assert (! inerrno);
59 - *outname_needs_removal = true;
60 - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
61 - sprintf (buf, "%s %s%s", editor_program,
62 - verbosity == VERBOSE ? "" : "- ",
63 - outname);
64 - fflush (stdout);
65 - pipefp = popen(buf, binary_transput ? "wb" : "w");
66 - if (!pipefp)
67 - pfatal ("Can't open pipe to %s", quotearg (buf));
68 - }
69 for (;;) {
70 char ed_command_letter;
71 beginning_of_this_line = file_tell (pfp);
72 @@ -2414,14 +2421,14 @@ do_ed_script (char const *inname, char c
73 }
74 ed_command_letter = get_ed_command_letter (buf);
75 if (ed_command_letter) {
76 - if (pipefp)
77 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
78 + if (tmpfp)
79 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
80 write_fatal ();
81 if (ed_command_letter != 'd' && ed_command_letter != 's') {
82 p_pass_comments_through = true;
83 while ((chars_read = get_line ()) != 0) {
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 (chars_read == 2 && strEQ (buf, ".\n"))
90 break;
91 @@ -2434,13 +2441,49 @@ do_ed_script (char const *inname, char c
92 break;
93 }
94 }
95 - if (!pipefp)
96 + if (!tmpfp)
97 return;
98 - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
99 - || fflush (pipefp) != 0)
100 + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
101 + || fflush (tmpfp) != 0)
102 write_fatal ();
103 - if (pclose (pipefp) != 0)
104 - fatal ("%s FAILED", editor_program);
105 +
106 + if (lseek (tmpfd, 0, SEEK_SET) == -1)
107 + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
108 +
109 + if (! dry_run && ! skip_rest_of_patch) {
110 + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
111 + *outname_needs_removal = true;
112 + if (inerrno != ENOENT)
113 + {
114 + *outname_needs_removal = true;
115 + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
116 + }
117 + sprintf (buf, "%s %s%s", editor_program,
118 + verbosity == VERBOSE ? "" : "- ",
119 + outname);
120 + fflush (stdout);
121 +
122 + pid = fork();
123 + if (pid == -1)
124 + pfatal ("Can't fork");
125 + else if (pid == 0)
126 + {
127 + dup2 (tmpfd, 0);
128 + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
129 + _exit (2);
130 + }
131 + else
132 + {
133 + int wstatus;
134 + if (waitpid (pid, &wstatus, 0) == -1
135 + || ! WIFEXITED (wstatus)
136 + || WEXITSTATUS (wstatus) != 0)
137 + fatal ("%s FAILED", editor_program);
138 + }
139 + }
140 +
141 + fclose (tmpfp);
142 + safe_unlink (tmpname);
143
144 if (ofp)
145 {