Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
audit: don't leave behind tmp files
Baptiste Daroussin committed 1 month ago
commit 35f78fc97505d16f4d03c328c4b6e69c01a0d913
parent 2acedb1
3 files changed +14 -14
modified libpkg/fetch.c
@@ -101,7 +101,7 @@ static struct fetcher fetchers[] = {

int
pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
-
	time_t t)
+
	time_t t, int *outfd)
{
	int fd = -1;
	int retcode = EPKG_FATAL;
@@ -116,6 +116,10 @@ pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
		return(EPKG_FATAL);
	}

+
	/* Unlink immediately so the temp file is cleaned up even if the
+
	 * process is killed by a signal during the fetch (issue #1988) */
+
	unlink(dest);
+

	fi.url = url;
	fi.mtime = t;
	retcode = pkg_fetch_file_to_fd(repo, fd, &fi, false);
@@ -134,11 +138,14 @@ pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
		futimens(fd, ts);
	}

-
	close(fd);
+
	if (retcode != EPKG_OK) {
+
		close(fd);
+
		return (retcode);
+
	}

-
	/* Remove local file if fetch failed */
-
	if (retcode != EPKG_OK)
-
		unlink(dest);
+
	/* Rewind so the caller can read from the beginning */
+
	lseek(fd, 0, SEEK_SET);
+
	*outfd = fd;

	return (retcode);
}
modified libpkg/pkg.h.in
@@ -1256,7 +1256,7 @@ int pkg_fetch_file(struct pkg_repo *repo, const char *url, char *dest, time_t t,
 * Fetch a file to temporary destination
 */
int pkg_fetch_file_tmp(struct pkg_repo *repo, const char *url, char *dest,
-
	time_t t);
+
	time_t t, int *outfd);

/**
 * Get cached name of a package
modified libpkg/pkg_audit.c
@@ -237,7 +237,7 @@ pkg_audit_fetch(const char *src, const char *dest)
			t = st.st_mtime;
	}

-
	switch (pkg_fetch_file_tmp(NULL, src, tmp, t)) {
+
	switch (pkg_fetch_file_tmp(NULL, src, tmp, t, &fd)) {
	case EPKG_OK:
		break;
	case EPKG_UPTODATE:
@@ -248,11 +248,6 @@ pkg_audit_fetch(const char *src, const char *dest)
		pkg_emit_error("cannot fetch vulnxml file");
		goto cleanup;
	}
-

-
	/* Open input fd */
-
	fd = open(tmp, O_RDONLY);
-
	if (fd == -1)
-
		goto cleanup;
	/* Open out fd */
	if (dest != NULL) {
		outfd = open(dest, O_RDWR|O_CREAT|O_TRUNC,
@@ -278,8 +273,6 @@ pkg_audit_fetch(const char *src, const char *dest)
	futimens(outfd, ts);

cleanup:
-
	unlink(tmp);
-

	if (fd != -1)
		close(fd);
	if (outfd != -1)