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
This commit is contained in:
Mark Johnston 2020-02-04 21:16:56 +00:00
parent f2530c80db
commit 640ff6ed84
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=357538

View File

@ -587,15 +587,19 @@ copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd,
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);