Skip to content

Commit

Permalink
elfcopy: Avoid leaking dst's fd when we fail to copy a file.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markjdb committed Feb 4, 2020
1 parent 1a79d65 commit 87649ab
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions contrib/elftoolchain/elfcopy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 87649ab

Please sign in to comment.