You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.3 KiB

  1. From b56779aed483f0036a32a65e62ab7b5e461b07cc 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. * src/pch.c (do_ed_script): Write ed script to a temporary file instead
  7. of piping it to ed: this will cause ed to abort on invalid commands
  8. instead of rejecting them and carrying on.
  9. * tests/ed-style: New test case.
  10. * tests/Makefile.am (TESTS): Add test case.
  11. ---
  12. src/pch.c | 89 +++++++++++++++++++++++++++++++++++------------
  13. tests/Makefile.am | 1 +
  14. tests/ed-style | 41 ++++++++++++++++++++++
  15. 3 files changed, 108 insertions(+), 23 deletions(-)
  16. create mode 100644 tests/ed-style
  17. --- a/src/pch.c
  18. +++ b/src/pch.c
  19. @@ -33,6 +33,7 @@
  20. # include <io.h>
  21. #endif
  22. #include <safe.h>
  23. +#include <sys/wait.h>
  24. #define INITHUNKMAX 125 /* initial dynamic allocation size */
  25. @@ -2389,22 +2390,28 @@ do_ed_script (char const *inname, char c
  26. static char const editor_program[] = EDITOR_PROGRAM;
  27. file_offset beginning_of_this_line;
  28. - FILE *pipefp = 0;
  29. size_t chars_read;
  30. + FILE *tmpfp = 0;
  31. + char const *tmpname;
  32. + int tmpfd;
  33. + pid_t pid;
  34. +
  35. + if (! dry_run && ! skip_rest_of_patch)
  36. + {
  37. + /* Write ed script to a temporary file. This causes ed to abort on
  38. + invalid commands such as when line numbers or ranges exceed the
  39. + number of available lines. When ed reads from a pipe, it rejects
  40. + invalid commands and treats the next line as a new command, which
  41. + can lead to arbitrary command execution. */
  42. +
  43. + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
  44. + if (tmpfd == -1)
  45. + pfatal ("Can't create temporary file %s", quotearg (tmpname));
  46. + tmpfp = fdopen (tmpfd, "w+b");
  47. + if (! tmpfp)
  48. + pfatal ("Can't open stream for file %s", quotearg (tmpname));
  49. + }
  50. - if (! dry_run && ! skip_rest_of_patch) {
  51. - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  52. - assert (! inerrno);
  53. - *outname_needs_removal = true;
  54. - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  55. - sprintf (buf, "%s %s%s", editor_program,
  56. - verbosity == VERBOSE ? "" : "- ",
  57. - outname);
  58. - fflush (stdout);
  59. - pipefp = popen(buf, binary_transput ? "wb" : "w");
  60. - if (!pipefp)
  61. - pfatal ("Can't open pipe to %s", quotearg (buf));
  62. - }
  63. for (;;) {
  64. char ed_command_letter;
  65. beginning_of_this_line = file_tell (pfp);
  66. @@ -2415,14 +2422,14 @@ do_ed_script (char const *inname, char c
  67. }
  68. ed_command_letter = get_ed_command_letter (buf);
  69. if (ed_command_letter) {
  70. - if (pipefp)
  71. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  72. + if (tmpfp)
  73. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  74. write_fatal ();
  75. if (ed_command_letter != 'd' && ed_command_letter != 's') {
  76. p_pass_comments_through = true;
  77. while ((chars_read = get_line ()) != 0) {
  78. - if (pipefp)
  79. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  80. + if (tmpfp)
  81. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  82. write_fatal ();
  83. if (chars_read == 2 && strEQ (buf, ".\n"))
  84. break;
  85. @@ -2435,13 +2442,49 @@ do_ed_script (char const *inname, char c
  86. break;
  87. }
  88. }
  89. - if (!pipefp)
  90. + if (!tmpfp)
  91. return;
  92. - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
  93. - || fflush (pipefp) != 0)
  94. + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
  95. + || fflush (tmpfp) != 0)
  96. write_fatal ();
  97. - if (pclose (pipefp) != 0)
  98. - fatal ("%s FAILED", editor_program);
  99. +
  100. + if (lseek (tmpfd, 0, SEEK_SET) == -1)
  101. + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
  102. +
  103. + if (! dry_run && ! skip_rest_of_patch) {
  104. + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  105. + *outname_needs_removal = true;
  106. + if (inerrno != ENOENT)
  107. + {
  108. + *outname_needs_removal = true;
  109. + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  110. + }
  111. + sprintf (buf, "%s %s%s", editor_program,
  112. + verbosity == VERBOSE ? "" : "- ",
  113. + outname);
  114. + fflush (stdout);
  115. +
  116. + pid = fork();
  117. + if (pid == -1)
  118. + pfatal ("Can't fork");
  119. + else if (pid == 0)
  120. + {
  121. + dup2 (tmpfd, 0);
  122. + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
  123. + _exit (2);
  124. + }
  125. + else
  126. + {
  127. + int wstatus;
  128. + if (waitpid (pid, &wstatus, 0) == -1
  129. + || ! WIFEXITED (wstatus)
  130. + || WEXITSTATUS (wstatus) != 0)
  131. + fatal ("%s FAILED", editor_program);
  132. + }
  133. + }
  134. +
  135. + fclose (tmpfp);
  136. + safe_unlink (tmpname);
  137. if (ofp)
  138. {
  139. --- a/tests/Makefile.am
  140. +++ b/tests/Makefile.am
  141. @@ -32,6 +32,7 @@ TESTS = \
  142. crlf-handling \
  143. dash-o-append \
  144. deep-directories \
  145. + ed-style \
  146. empty-files \
  147. false-match \
  148. fifo \
  149. --- /dev/null
  150. +++ b/tests/ed-style
  151. @@ -0,0 +1,41 @@
  152. +# Copyright (C) 2018 Free Software Foundation, Inc.
  153. +#
  154. +# Copying and distribution of this file, with or without modification,
  155. +# in any medium, are permitted without royalty provided the copyright
  156. +# notice and this notice are preserved.
  157. +
  158. +. $srcdir/test-lib.sh
  159. +
  160. +require cat
  161. +use_local_patch
  162. +use_tmpdir
  163. +
  164. +# ==============================================================
  165. +
  166. +cat > ed1.diff <<EOF
  167. +0a
  168. +foo
  169. +.
  170. +EOF
  171. +
  172. +check 'patch -e foo -i ed1.diff' <<EOF
  173. +EOF
  174. +
  175. +check 'cat foo' <<EOF
  176. +foo
  177. +EOF
  178. +
  179. +cat > ed2.diff <<EOF
  180. +1337a
  181. +r !echo bar
  182. +,p
  183. +EOF
  184. +
  185. +check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <<EOF
  186. +?
  187. +Status: 2
  188. +EOF
  189. +
  190. +check 'cat foo' <<EOF
  191. +foo
  192. +EOF