Fix_arbitrary_command_execution_in_ed-style_patches.patch 5.7 KB

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