0003-Fix-arbitrary-command-execution-in-ed-style-patches-.patch 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: [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. [baruch: drop test hunks to avoid autoreconf]
  12. Signed-off-by: Baruch Siach <baruch@tkos.co.il>
  13. ---
  14. Upstream status: commit 123eaff0d5d1
  15. src/pch.c | 91 ++++++++++++++++++++++++++++++++++++++++---------------
  16. tests/Makefile.am | 1 +
  17. tests/ed-style | 41 +++++++++++++++++++++++++
  18. 3 files changed, 108 insertions(+), 25 deletions(-)
  19. create mode 100644 tests/ed-style
  20. diff --git a/src/pch.c b/src/pch.c
  21. index 0c5cc2623079..4fd5a05a6f5c 100644
  22. --- a/src/pch.c
  23. +++ b/src/pch.c
  24. @@ -33,6 +33,7 @@
  25. # include <io.h>
  26. #endif
  27. #include <safe.h>
  28. +#include <sys/wait.h>
  29. #define INITHUNKMAX 125 /* initial dynamic allocation size */
  30. @@ -2389,24 +2390,28 @@ do_ed_script (char const *inname, char const *outname,
  31. static char const editor_program[] = EDITOR_PROGRAM;
  32. file_offset beginning_of_this_line;
  33. - FILE *pipefp = 0;
  34. size_t chars_read;
  35. + FILE *tmpfp = 0;
  36. + char const *tmpname;
  37. + int tmpfd;
  38. + pid_t pid;
  39. +
  40. + if (! dry_run && ! skip_rest_of_patch)
  41. + {
  42. + /* Write ed script to a temporary file. This causes ed to abort on
  43. + invalid commands such as when line numbers or ranges exceed the
  44. + number of available lines. When ed reads from a pipe, it rejects
  45. + invalid commands and treats the next line as a new command, which
  46. + can lead to arbitrary command execution. */
  47. +
  48. + tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
  49. + if (tmpfd == -1)
  50. + pfatal ("Can't create temporary file %s", quotearg (tmpname));
  51. + tmpfp = fdopen (tmpfd, "w+b");
  52. + if (! tmpfp)
  53. + pfatal ("Can't open stream for file %s", quotearg (tmpname));
  54. + }
  55. - if (! dry_run && ! skip_rest_of_patch) {
  56. - int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  57. - if (inerrno != ENOENT)
  58. - {
  59. - *outname_needs_removal = true;
  60. - copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  61. - }
  62. - sprintf (buf, "%s %s%s", editor_program,
  63. - verbosity == VERBOSE ? "" : "- ",
  64. - outname);
  65. - fflush (stdout);
  66. - pipefp = popen(buf, binary_transput ? "wb" : "w");
  67. - if (!pipefp)
  68. - pfatal ("Can't open pipe to %s", quotearg (buf));
  69. - }
  70. for (;;) {
  71. char ed_command_letter;
  72. beginning_of_this_line = file_tell (pfp);
  73. @@ -2417,14 +2422,14 @@ do_ed_script (char const *inname, char const *outname,
  74. }
  75. ed_command_letter = get_ed_command_letter (buf);
  76. if (ed_command_letter) {
  77. - if (pipefp)
  78. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  79. + if (tmpfp)
  80. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  81. write_fatal ();
  82. if (ed_command_letter != 'd' && ed_command_letter != 's') {
  83. p_pass_comments_through = true;
  84. while ((chars_read = get_line ()) != 0) {
  85. - if (pipefp)
  86. - if (! fwrite (buf, sizeof *buf, chars_read, pipefp))
  87. + if (tmpfp)
  88. + if (! fwrite (buf, sizeof *buf, chars_read, tmpfp))
  89. write_fatal ();
  90. if (chars_read == 2 && strEQ (buf, ".\n"))
  91. break;
  92. @@ -2437,13 +2442,49 @@ do_ed_script (char const *inname, char const *outname,
  93. break;
  94. }
  95. }
  96. - if (!pipefp)
  97. + if (!tmpfp)
  98. return;
  99. - if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, pipefp) == 0
  100. - || fflush (pipefp) != 0)
  101. + if (fwrite ("w\nq\n", sizeof (char), (size_t) 4, tmpfp) == 0
  102. + || fflush (tmpfp) != 0)
  103. write_fatal ();
  104. - if (pclose (pipefp) != 0)
  105. - fatal ("%s FAILED", editor_program);
  106. +
  107. + if (lseek (tmpfd, 0, SEEK_SET) == -1)
  108. + pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
  109. +
  110. + if (! dry_run && ! skip_rest_of_patch) {
  111. + int exclusive = *outname_needs_removal ? 0 : O_EXCL;
  112. + *outname_needs_removal = true;
  113. + if (inerrno != ENOENT)
  114. + {
  115. + *outname_needs_removal = true;
  116. + copy_file (inname, outname, 0, exclusive, instat.st_mode, true);
  117. + }
  118. + sprintf (buf, "%s %s%s", editor_program,
  119. + verbosity == VERBOSE ? "" : "- ",
  120. + outname);
  121. + fflush (stdout);
  122. +
  123. + pid = fork();
  124. + if (pid == -1)
  125. + pfatal ("Can't fork");
  126. + else if (pid == 0)
  127. + {
  128. + dup2 (tmpfd, 0);
  129. + execl ("/bin/sh", "sh", "-c", buf, (char *) 0);
  130. + _exit (2);
  131. + }
  132. + else
  133. + {
  134. + int wstatus;
  135. + if (waitpid (pid, &wstatus, 0) == -1
  136. + || ! WIFEXITED (wstatus)
  137. + || WEXITSTATUS (wstatus) != 0)
  138. + fatal ("%s FAILED", editor_program);
  139. + }
  140. + }
  141. +
  142. + fclose (tmpfp);
  143. + safe_unlink (tmpname);
  144. if (ofp)
  145. {
  146. --
  147. 2.16.3