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
25 +#include <sys/wait.h>
27 #define INITHUNKMAX 125 /* initial dynamic allocation size */
29 @@ -2388,22 +2389,28 @@ do_ed_script (char const *inname, char c
30 static char const editor_program[] = EDITOR_PROGRAM;
32 file_offset beginning_of_this_line;
36 + char const *tmpname;
40 + if (! dry_run && ! skip_rest_of_patch)
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. */
48 + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
50 + pfatal ("Can't create temporary file %s", quotearg (tmpname));
51 + tmpfp = fdopen (tmpfd, "w+b");
53 + pfatal ("Can't open stream for file %s", quotearg (tmpname));
56 - if (! dry_run && ! skip_rest_of_patch) {
57 - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
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 ? "" : "- ",
65 - pipefp = popen(buf, binary_transput ? "wb" : "w");
67 - pfatal ("Can't open pipe to %s", quotearg (buf));
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
74 ed_command_letter = get_ed_command_letter (buf);
75 if (ed_command_letter) {
77 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
79 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
81 if (ed_command_letter != 'd' && ed_command_letter != 's') {
82 p_pass_comments_through = true;
83 while ((chars_read = get_line ()) != 0) {
85 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
87 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
89 if (chars_read == 2 && strEQ (buf, ".\n"))
91 @@ -2434,13 +2441,49 @@ do_ed_script (char const *inname, char c
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)
103 - if (pclose (pipefp) != 0)
104 - fatal ("%s FAILED", editor_program);
106 + if (lseek (tmpfd, 0, SEEK_SET) == -1)
107 + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
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)
114 + *outname_needs_removal = true;
115 + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
117 + sprintf (buf, "%s %s%s", editor_program,
118 + verbosity == VERBOSE ? "" : "- ",
124 + pfatal ("Can't fork");
128 + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
134 + if (waitpid (pid, &wstatus, 0) == -1
135 + || ! WIFEXITED (wstatus)
136 + || WEXITSTATUS (wstatus) != 0)
137 + fatal ("%s FAILED", editor_program);
142 + safe_unlink (tmpname);