Commit 0843a4ce462d9a691a3b5f38a465b129b69fdd5f

Sebastien marie 2020-10-31T04:54:20

use fchmod(2) instead of chmod(2). it is less racy, and we just opened the descriptor. ok naddy@ stsp@

diff --git a/lib/fetch.c b/lib/fetch.c
index 0e60b21..718d957 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -498,6 +498,10 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		free(path);
 		if (err)
 			goto done;
+		if (fchmod(packfd, GOT_DEFAULT_FILE_MODE) != 0) {
+			err = got_error_from_errno2("fchmod", tmppackpath);
+			goto done;
+		}
 	}
 	if (list_refs_only) {
 		idxfd = got_opentempfd();
@@ -515,6 +519,10 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 		free(path);
 		if (err)
 			goto done;
+		if (fchmod(idxfd, GOT_DEFAULT_FILE_MODE) != 0) {
+			err = got_error_from_errno2("fchmod", tmpidxpath);
+			goto done;
+		}
 	}
 	nidxfd = dup(idxfd);
 	if (nidxfd == -1) {
@@ -800,15 +808,6 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	free(tmpidxpath);
 	tmpidxpath = NULL;
 
-	if (chmod(packpath, GOT_DEFAULT_FILE_MODE) != 0) {
-		err = got_error_from_errno2("chmod", packpath);
-		goto done;
-	}
-	if (chmod(idxpath, GOT_DEFAULT_FILE_MODE) != 0) {
-		err = got_error_from_errno2("chmod", idxpath);
-		goto done;
-	}
-
 done:
 	if (tmppackpath && unlink(tmppackpath) == -1 && err == NULL)
 		err = got_error_from_errno2("unlink", tmppackpath);