Blame SOURCES/patch-2.7.6-dont-leak-temporary-file-on-failed-ed-style-patch.patch

796689
commit 19599883ffb6a450d2884f081f8ecf68edbed7ee
796689
Author: Jean Delvare <jdelvare@suse.de>
796689
Date:   Thu May 3 14:31:55 2018 +0200
796689
796689
    Don't leak temporary file on failed ed-style patch
796689
    
796689
    Now that we write ed-style patches to a temporary file before we
796689
    apply them, we need to ensure that the temporary file is removed
796689
    before we leave, even on fatal error.
796689
    
796689
    * src/pch.c (do_ed_script): Use global TMPEDNAME instead of local
796689
      tmpname. Don't unlink the file directly, instead tag it for removal
796689
      at exit time.
796689
    * src/patch.c (cleanup): Unlink TMPEDNAME at exit.
796689
    
796689
    This closes bug #53820:
796689
    https://savannah.gnu.org/bugs/index.php?53820
796689
    
796689
    Fixes: 123eaff0d5d1 ("Fix arbitrary command execution in ed-style patches (CVE-2018-1000156)")
796689
796689
diff --git a/src/common.h b/src/common.h
796689
index 904a3f8..53c5e32 100644
796689
--- a/src/common.h
796689
+++ b/src/common.h
796689
@@ -94,10 +94,12 @@ XTERN char const *origsuff;
796689
 XTERN char const * TMPINNAME;
796689
 XTERN char const * TMPOUTNAME;
796689
 XTERN char const * TMPPATNAME;
796689
+XTERN char const * TMPEDNAME;
796689
 
796689
 XTERN bool TMPINNAME_needs_removal;
796689
 XTERN bool TMPOUTNAME_needs_removal;
796689
 XTERN bool TMPPATNAME_needs_removal;
796689
+XTERN bool TMPEDNAME_needs_removal;
796689
 
796689
 #ifdef DEBUGGING
796689
 XTERN int debug;
796689
diff --git a/src/patch.c b/src/patch.c
796689
index 3fcaec5..9146597 100644
796689
--- a/src/patch.c
796689
+++ b/src/patch.c
796689
@@ -1999,6 +1999,7 @@ cleanup (void)
796689
   remove_if_needed (TMPINNAME, &TMPINNAME_needs_removal);
796689
   remove_if_needed (TMPOUTNAME, &TMPOUTNAME_needs_removal);
796689
   remove_if_needed (TMPPATNAME, &TMPPATNAME_needs_removal);
796689
+  remove_if_needed (TMPEDNAME, &TMPEDNAME_needs_removal);
796689
   remove_if_needed (TMPREJNAME, &TMPREJNAME_needs_removal);
796689
   output_files (NULL);
796689
 }
796689
diff --git a/src/pch.c b/src/pch.c
796689
index 79a3c99..1bb3153 100644
796689
--- a/src/pch.c
796689
+++ b/src/pch.c
796689
@@ -2396,7 +2396,6 @@ do_ed_script (char const *inname, char const *outname,
796689
     file_offset beginning_of_this_line;
796689
     size_t chars_read;
796689
     FILE *tmpfp = 0;
796689
-    char const *tmpname;
796689
     int tmpfd = -1; /* placate gcc's -Wmaybe-uninitialized */
796689
     int exclusive = *outname_needs_removal ? 0 : O_EXCL;
796689
     char const **ed_argv;
796689
@@ -2411,12 +2410,13 @@ do_ed_script (char const *inname, char const *outname,
796689
 	   invalid commands and treats the next line as a new command, which
796689
 	   can lead to arbitrary command execution.  */
796689
 
796689
-	tmpfd = make_tempfile (&tmpname, 'e', NULL, O_RDWR | O_BINARY, 0);
796689
+	tmpfd = make_tempfile (&TMPEDNAME, 'e', NULL, O_RDWR | O_BINARY, 0);
796689
 	if (tmpfd == -1)
796689
-	  pfatal ("Can't create temporary file %s", quotearg (tmpname));
796689
+	  pfatal ("Can't create temporary file %s", quotearg (TMPEDNAME));
796689
+	TMPEDNAME_needs_removal = true;
796689
 	tmpfp = fdopen (tmpfd, "w+b");
796689
 	if (! tmpfp)
796689
-	  pfatal ("Can't open stream for file %s", quotearg (tmpname));
796689
+	  pfatal ("Can't open stream for file %s", quotearg (TMPEDNAME));
796689
       }
796689
 
796689
     for (;;) {
796689
@@ -2457,7 +2457,7 @@ do_ed_script (char const *inname, char const *outname,
796689
       write_fatal ();
796689
 
796689
     if (lseek (tmpfd, 0, SEEK_SET) == -1)
796689
-      pfatal ("Can't rewind to the beginning of file %s", quotearg (tmpname));
796689
+      pfatal ("Can't rewind to the beginning of file %s", quotearg (TMPEDNAME));
796689
 
796689
     if (inerrno != ENOENT)
796689
       {
796689
@@ -2484,7 +2484,6 @@ do_ed_script (char const *inname, char const *outname,
796689
       pfatal ("Failed to duplicate standard input");
796689
 
796689
     fclose (tmpfp);
796689
-    safe_unlink (tmpname);
796689
 
796689
     if (ofp)
796689
       {