1 From 123eaff0d5d1aebe128295959435b9ca5909c26d 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: Fix arbitrary command execution in ed-style patches
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.
13 src/pch.c | 91 ++++++++++++++++++++++++++++++++++++++++---------------
14 tests/Makefile.am | 1 +
15 tests/ed-style | 41 +++++++++++++++++++++++++
16 3 files changed, 108 insertions(+), 25 deletions(-)
17 create mode 100644 tests/ed-style
19 diff --git a/src/pch.c b/src/pch.c
20 index 0c5cc26..4fd5a05 100644
27 +#include <sys/wait.h>
29 #define INITHUNKMAX 125 /* initial dynamic allocation size */
31 @@ -2389,24 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
32 static char const editor_program[] = EDITOR_PROGRAM;
34 file_offset beginning_of_this_line;
38 + char const *tmpname;
42 + if (! dry_run && ! skip_rest_of_patch)
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. */
50 + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
52 + pfatal ("Can't create temporary file %s", quotearg (tmpname));
53 + tmpfp = fdopen (tmpfd, "w+b");
55 + pfatal ("Can't open stream for file %s", quotearg (tmpname));
58 - if (! dry_run && ! skip_rest_of_patch) {
59 - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
60 - if (inerrno != ENOENT)
62 - *outname_needs_removal = true;
63 - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
65 - sprintf (buf, "%s %s%s", editor_program,
66 - verbosity == VERBOSE ? "" : "- ",
69 - pipefp = popen(buf, binary_transput ? "wb" : "w");
71 - pfatal ("Can't open pipe to %s", quotearg (buf));
74 char ed_command_letter;
75 beginning_of_this_line = file_tell (pfp);
76 @@ -2417,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
78 ed_command_letter = get_ed_command_letter (buf);
79 if (ed_command_letter) {
81 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
83 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
85 if (ed_command_letter != 'd' && ed_command_letter != 's') {
86 p_pass_comments_through = true;
87 while ((chars_read = get_line ()) != 0) {
89 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
91 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
93 if (chars_read == 2 && strEQ (buf, ".\n"))
95 @@ -2437,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
102 - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
103 - || fflush (pipefp) != 0)
104 + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
105 + || fflush (tmpfp) != 0)
107 - if (pclose (pipefp) != 0)
108 - fatal ("%s FAILED", editor_program);
110 + if (lseek (tmpfd, 0, SEEK_SET) == -1)
111 + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
113 + if (! dry_run && ! skip_rest_of_patch) {
114 + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
115 + *outname_needs_removal = true;
116 + if (inerrno != ENOENT)
118 + *outname_needs_removal = true;
119 + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
121 + sprintf (buf, "%s %s%s", editor_program,
122 + verbosity == VERBOSE ? "" : "- ",
128 + pfatal ("Can't fork");
132 + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
138 + if (waitpid (pid, &wstatus, 0) == -1
139 + || ! WIFEXITED (wstatus)
140 + || WEXITSTATUS (wstatus) != 0)
141 + fatal ("%s FAILED", editor_program);
146 + safe_unlink (tmpname);
150 diff --git a/tests/Makefile.am b/tests/Makefile.am
151 index 6b6df63..16f8693 100644
152 --- a/tests/Makefile.am
153 +++ b/tests/Makefile.am
154 @@ -32,6 +32,7 @@ TESTS = \
162 diff --git a/tests/ed-style b/tests/ed-style
164 index 0000000..d8c0689
168 +# Copyright (C) 2018 Free Software Foundation, Inc.
170 +# Copying and distribution of this file, with or without modification,
171 +# in any medium, are permitted without royalty provided the copyright
172 +# notice and this notice are preserved.
174 +. $srcdir/test-lib.sh
180 +# ==============================================================
182 +cat > ed1.diff <<EOF
188 +check 'patch -e foo -i ed1.diff' <<EOF
191 +check 'cat foo' <<EOF
195 +cat > ed2.diff <<EOF
201 +check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <<EOF
206 +check 'cat foo' <<EOF