Author: markj
Date: Tue Feb  4 21:16:56 2020
New Revision: 357538
URL: https://svnweb.freebsd.org/changeset/base/357538

Log:
  elfcopy: Avoid leaking dst's fd when we fail to copy a file.
  
  We should really create the output file in the same directory as the
  destination file so that rename() works.  This will be done in a future
  change as part of some work to run in capability mode.
  
  CID:          1262523
  MFC after:    1 week
  Sponsored by: The FreeBSD Foundation

Modified:
  head/contrib/elftoolchain/elfcopy/main.c

Modified: head/contrib/elftoolchain/elfcopy/main.c
==============================================================================
--- head/contrib/elftoolchain/elfcopy/main.c    Tue Feb  4 21:16:41 2020        
(r357537)
+++ head/contrib/elftoolchain/elfcopy/main.c    Tue Feb  4 21:16:56 2020        
(r357538)
@@ -587,15 +587,19 @@ copy_from_tempfile(const char *src, const char *dst, i
        if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0)
                return (-1);
 
-       if (elftc_copyfile(infd, tmpfd) < 0)
+       if (elftc_copyfile(infd, tmpfd) < 0) {
+               (void) close(tmpfd);
                return (-1);
+       }
 
        /*
         * Remove the temporary file from the file system
         * namespace, and close its file descriptor.
         */
-       if (unlink(src) < 0)
+       if (unlink(src) < 0) {
+               (void) close(tmpfd);
                return (-1);
+       }
 
        (void) close(infd);
 
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "[email protected]"

Reply via email to