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.

209 lines
5.6 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. diff --git a/src/pch.c b/src/pch.c
  18. index bc6278c..4fd5a05 100644
  19. --- a/src/pch.c
  20. +++ b/src/pch.c
  21. @@ -33,6 +33,7 @@
  22. # include <io.h>
  23. #endif
  24. #include <safe.h>
  25. +#include <sys/wait.h>
  26. #define INITHUNKMAX 125 /* initial dynamic allocation size */
  27. @@ -2389,22 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
  28. static char const editor_program[] = EDITOR_PROGRAM;
  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. - if (! dry_run && ! skip_rest_of_patch) {
  53. - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  54. - assert (! inerrno);
  55. - *outname_needs_removal = true;
  56. - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  57. - sprintf (buf, "%s %s%s", editor_program,
  58. - verbosity == VERBOSE ? "" : "- ",
  59. - outname);
  60. - fflush (stdout);
  61. - pipefp = popen(buf, binary_transput ? "wb" : "w");
  62. - if (!pipefp)
  63. - pfatal ("Can't open pipe to %s", quotearg (buf));
  64. - }
  65. for (;;) {
  66. char ed_command_letter;
  67. beginning_of_this_line = file_tell (pfp);
  68. @@ -2415,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
  69. }
  70. ed_command_letter = get_ed_command_letter (buf);
  71. if (ed_command_letter) {
  72. - if (pipefp)
  73. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  74. + if (tmpfp)
  75. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  76. write_fatal ();
  77. if (ed_command_letter != 'd' && ed_command_letter != 's') {
  78. p_pass_comments_through = true;
  79. while ((chars_read = get_line ()) != 0) {
  80. - if (pipefp)
  81. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  82. + if (tmpfp)
  83. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  84. write_fatal ();
  85. if (chars_read == 2 && strEQ (buf, ".\n"))
  86. break;
  87. @@ -2435,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
  88. break;
  89. }
  90. }
  91. - if (!pipefp)
  92. + if (!tmpfp)
  93. return;
  94. - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
  95. - || fflush (pipefp) != 0)
  96. + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
  97. + || fflush (tmpfp) != 0)
  98. write_fatal ();
  99. - if (pclose (pipefp) != 0)
  100. - fatal ("%s FAILED", editor_program);
  101. +
  102. + if (lseek (tmpfd, 0, SEEK_SET) == -1)
  103. + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
  104. +
  105. + if (! dry_run && ! skip_rest_of_patch) {
  106. + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  107. + *outname_needs_removal = true;
  108. + if (inerrno != ENOENT)
  109. + {
  110. + *outname_needs_removal = true;
  111. + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  112. + }
  113. + sprintf (buf, "%s %s%s", editor_program,
  114. + verbosity == VERBOSE ? "" : "- ",
  115. + outname);
  116. + fflush (stdout);
  117. +
  118. + pid = fork();
  119. + if (pid == -1)
  120. + pfatal ("Can't fork");
  121. + else if (pid == 0)
  122. + {
  123. + dup2 (tmpfd, 0);
  124. + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
  125. + _exit (2);
  126. + }
  127. + else
  128. + {
  129. + int wstatus;
  130. + if (waitpid (pid, &wstatus, 0) == -1
  131. + || ! WIFEXITED (wstatus)
  132. + || WEXITSTATUS (wstatus) != 0)
  133. + fatal ("%s FAILED", editor_program);
  134. + }
  135. + }
  136. +
  137. + fclose (tmpfp);
  138. + safe_unlink (tmpname);
  139. if (ofp)
  140. {
  141. diff --git a/tests/Makefile.am b/tests/Makefile.am
  142. index 6b6df63..16f8693 100644
  143. --- a/tests/Makefile.am
  144. +++ b/tests/Makefile.am
  145. @@ -32,6 +32,7 @@ TESTS = \
  146. crlf-handling \
  147. dash-o-append \
  148. deep-directories \
  149. + ed-style \
  150. empty-files \
  151. false-match \
  152. fifo \
  153. diff --git a/tests/ed-style b/tests/ed-style
  154. new file mode 100644
  155. index 0000000..d8c0689
  156. --- /dev/null
  157. +++ b/tests/ed-style
  158. @@ -0,0 +1,41 @@
  159. +# Copyright (C) 2018 Free Software Foundation, Inc.
  160. +#
  161. +# Copying and distribution of this file, with or without modification,
  162. +# in any medium, are permitted without royalty provided the copyright
  163. +# notice and this notice are preserved.
  164. +
  165. +. $srcdir/test-lib.sh
  166. +
  167. +require cat
  168. +use_local_patch
  169. +use_tmpdir
  170. +
  171. +# ==============================================================
  172. +
  173. +cat > ed1.diff <<EOF
  174. +0a
  175. +foo
  176. +.
  177. +EOF
  178. +
  179. +check 'patch -e foo -i ed1.diff' <<EOF
  180. +EOF
  181. +
  182. +check 'cat foo' <<EOF
  183. +foo
  184. +EOF
  185. +
  186. +cat > ed2.diff <<EOF
  187. +1337a
  188. +r !echo bar
  189. +,p
  190. +EOF
  191. +
  192. +check 'patch -e foo -i ed2.diff 2> /dev/null || echo "Status: $?"' <<EOF
  193. +?
  194. +Status: 2
  195. +EOF
  196. +
  197. +check 'cat foo' <<EOF
  198. +foo
  199. +EOF
  200. --
  201. 2.19.1