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
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 | 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
19 diff --git a/src/pch.c b/src/pch.c
20 index ff9ed2c..8150493 100644
27 +#include <sys/wait.h>
29 #define INITHUNKMAX 125 /* initial dynamic allocation size */
31 @@ -2388,22 +2389,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;
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 ? "" : "- ",
67 - pipefp = popen(buf, binary_transput ? "wb" : "w");
69 - pfatal ("Can't open pipe to %s", quotearg (buf));
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,
76 ed_command_letter = get_ed_command_letter (buf);
77 if (ed_command_letter) {
79 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
81 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
83 if (ed_command_letter != 'd' && ed_command_letter != 's') {
84 p_pass_comments_through = true;
85 while ((chars_read = get_line ()) != 0) {
87 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
89 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
91 if (chars_read == 2 && strEQ (buf, ".\n"))
93 @@ -2434,13 +2441,49 @@ do_ed_script (char const *inname, char const *outname,
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)
105 - if (pclose (pipefp) != 0)
106 - fatal ("%s FAILED", editor_program);
108 + if (lseek (tmpfd, 0, SEEK_SET) == -1)
109 + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
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)
116 + *outname_needs_removal = true;
117 + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
119 + sprintf (buf, "%s %s%s", editor_program,
120 + verbosity == VERBOSE ? "" : "- ",
126 + pfatal ("Can't fork");
130 + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
136 + if (waitpid (pid, &wstatus, 0) == -1
137 + || ! WIFEXITED (wstatus)
138 + || WEXITSTATUS (wstatus) != 0)
139 + fatal ("%s FAILED", editor_program);
144 + safe_unlink (tmpname);
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 = \
160 diff --git a/tests/ed-style b/tests/ed-style
162 index 0000000..d8c0689
166 +# Copyright (C) 2018 Free Software Foundation, Inc.
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.
172 +. $srcdir/test-lib.sh
178 +# ==============================================================
180 +cat > ed1.diff <<EOF
186 +check 'patch -e foo -i ed1.diff' <<EOF
189 +check 'cat foo' <<EOF
193 +cat > ed2.diff <<EOF
199 +check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <<EOF
204 +check 'cat foo' <<EOF