b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 |
| 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 | 1 file changed, 66 insertions(+), 23 deletions(-) |
| 15 | |
| 16 | --- a/src/pch.c |
| 17 | +++ b/src/pch.c |
| 18 | @@ -33,6 +33,7 @@ |
| 19 | # include <io.h> |
| 20 | #endif |
| 21 | #include <safe.h> |
| 22 | +#include <sys/wait.h> |
| 23 | |
| 24 | #define INITHUNKMAX 125 /* initial dynamic allocation size */ |
| 25 | |
| 26 | @@ -2389,22 +2390,28 @@ do_ed_script (char const *inname, char c |
| 27 | static char const editor_program[] = EDITOR_PROGRAM; |
| 28 | |
| 29 | file_offset beginning_of_this_line; |
| 30 | - FILE *pipefp = 0; |
| 31 | size_t chars_read; |
| 32 | + FILE *tmpfp = 0; |
| 33 | + char const *tmpname; |
| 34 | + int tmpfd; |
| 35 | + pid_t pid; |
| 36 | + |
| 37 | + if (! dry_run && ! skip_rest_of_patch) |
| 38 | + { |
| 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. */ |
| 44 | + |
| 45 | + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0); |
| 46 | + if (tmpfd == -1) |
| 47 | + pfatal ("Can't create temporary file %s", quotearg (tmpname)); |
| 48 | + tmpfp = fdopen (tmpfd, "w+b"); |
| 49 | + if (! tmpfp) |
| 50 | + pfatal ("Can't open stream for file %s", quotearg (tmpname)); |
| 51 | + } |
| 52 | |
| 53 | - if (! dry_run && ! skip_rest_of_patch) { |
| 54 | - int exclusive = *outname_needs_removal ? 0 : O_EXCL; |
| 55 | - assert (! inerrno); |
| 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 ? "" : "- ", |
| 60 | - outname); |
| 61 | - fflush (stdout); |
| 62 | - pipefp = popen(buf, binary_transput ? "wb" : "w"); |
| 63 | - if (!pipefp) |
| 64 | - pfatal ("Can't open pipe to %s", quotearg (buf)); |
| 65 | - } |
| 66 | for (;;) { |
| 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 |
| 70 | } |
| 71 | ed_command_letter = get_ed_command_letter (buf); |
| 72 | if (ed_command_letter) { |
| 73 | - if (pipefp) |
| 74 | - if (! fwrite (buf, sizeof *buf, chars_read, pipefp)) |
| 75 | + if (tmpfp) |
| 76 | + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp)) |
| 77 | write_fatal (); |
| 78 | if (ed_command_letter != 'd' && ed_command_letter != 's') { |
| 79 | p_pass_comments_through = true; |
| 80 | while ((chars_read = get_line ()) != 0) { |
| 81 | - if (pipefp) |
| 82 | - if (! fwrite (buf, sizeof *buf, chars_read, pipefp)) |
| 83 | + if (tmpfp) |
| 84 | + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp)) |
| 85 | write_fatal (); |
| 86 | if (chars_read == 2 && strEQ (buf, ".\n")) |
| 87 | break; |
| 88 | @@ -2435,13 +2442,49 @@ do_ed_script (char const *inname, char c |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | - if (!pipefp) |
| 93 | + if (!tmpfp) |
| 94 | return; |
| 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) |
| 99 | write_fatal (); |
| 100 | - if (pclose (pipefp) != 0) |
| 101 | - fatal ("%s FAILED", editor_program); |
| 102 | + |
| 103 | + if (lseek (tmpfd, 0, SEEK_SET) == -1) |
| 104 | + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname)); |
| 105 | + |
| 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) |
| 110 | + { |
| 111 | + *outname_needs_removal = true; |
| 112 | + copy_file (inname, outname, 0, exclusive, instat.st_mode, true); |
| 113 | + } |
| 114 | + sprintf (buf, "%s %s%s", editor_program, |
| 115 | + verbosity == VERBOSE ? "" : "- ", |
| 116 | + outname); |
| 117 | + fflush (stdout); |
| 118 | + |
| 119 | + pid = fork(); |
| 120 | + if (pid == -1) |
| 121 | + pfatal ("Can't fork"); |
| 122 | + else if (pid == 0) |
| 123 | + { |
| 124 | + dup2 (tmpfd, 0); |
| 125 | + execl ("/bin/sh", "sh", "-c", buf, (char *) 0); |
| 126 | + _exit (2); |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + int wstatus; |
| 131 | + if (waitpid (pid, &wstatus, 0) == -1 |
| 132 | + || ! WIFEXITED (wstatus) |
| 133 | + || WEXITSTATUS (wstatus) != 0) |
| 134 | + fatal ("%s FAILED", editor_program); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + fclose (tmpfp); |
| 139 | + safe_unlink (tmpname); |
| 140 | |
| 141 | if (ofp) |
| 142 | { |