CVE-2018-1000156.patch 4.2 KB

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