020-CVE-2018-1000156.patch 4.3 KB

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