1 From b3a0ca3deed00334f9feece43f76776b6a168e47 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 1 file changed, 66 insertions(+), 23 deletions(-)
22 +#include <sys/wait.h>
24 #define INITHUNKMAX 125 /* initial dynamic allocation size */
26 @@ -2389,22 +2390,28 @@ do_ed_script (char const *inname, char c
27 static char const editor_program[] = EDITOR_PROGRAM;
29 file_offset beginning_of_this_line;
33 + char const *tmpname;
37 + if (! dry_run && ! skip_rest_of_patch)
39 + /* Write ed script to a temporary file. This causes ed to abort on
40 + invalid commands such as when line numbers or ranges exceed the
41 + number of available lines. When ed reads from a pipe, it rejects
42 + invalid commands and treats the next line as a new command, which
43 + can lead to arbitrary command execution. */
45 + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
47 + pfatal ("Can't create temporary file %s", quotearg (tmpname));
48 + tmpfp = fdopen (tmpfd, "w+b");
50 + pfatal ("Can't open stream for file %s", quotearg (tmpname));
53 - if (! dry_run && ! skip_rest_of_patch) {
54 - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
56 - *outname_needs_removal = true;
57 - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
58 - sprintf (buf, "%s %s%s", editor_program,
59 - verbosity == VERBOSE ? "" : "- ",
62 - pipefp = popen(buf, binary_transput ? "wb" : "w");
64 - pfatal ("Can't open pipe to %s", quotearg (buf));
67 char ed_command_letter;
68 beginning_of_this_line = file_tell (pfp);
69 @@ -2415,14 +2422,14 @@ do_ed_script (char const *inname, char c
71 ed_command_letter = get_ed_command_letter (buf);
72 if (ed_command_letter) {
74 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
76 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
78 if (ed_command_letter != 'd' && ed_command_letter != 's') {
79 p_pass_comments_through = true;
80 while ((chars_read = get_line ()) != 0) {
82 - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
84 + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
86 if (chars_read == 2 && strEQ (buf, ".\n"))
88 @@ -2435,13 +2442,49 @@ do_ed_script (char const *inname, char c
95 - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
96 - || fflush (pipefp) != 0)
97 + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
98 + || fflush (tmpfp) != 0)
100 - if (pclose (pipefp) != 0)
101 - fatal ("%s FAILED", editor_program);
103 + if (lseek (tmpfd, 0, SEEK_SET) == -1)
104 + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
106 + if (! dry_run && ! skip_rest_of_patch) {
107 + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
108 + *outname_needs_removal = true;
109 + if (inerrno != ENOENT)
111 + *outname_needs_removal = true;
112 + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
114 + sprintf (buf, "%s %s%s", editor_program,
115 + verbosity == VERBOSE ? "" : "- ",
121 + pfatal ("Can't fork");
125 + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
131 + if (waitpid (pid, &wstatus, 0) == -1
132 + || ! WIFEXITED (wstatus)
133 + || WEXITSTATUS (wstatus) != 0)
134 + fatal ("%s FAILED", editor_program);
139 + safe_unlink (tmpname);