Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Feb 2020 21:16:57 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r357538 - head/contrib/elftoolchain/elfcopy
Message-ID:  <202002042116.014LGvU4019147@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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);
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202002042116.014LGvU4019147>