git: f417e2a5036e - main - pkg-serve: stat the opened descriptor instead of the path

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 11:56:49 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=f417e2a5036ed2d2beff7ed77db5883d726d2c42

commit f417e2a5036ed2d2beff7ed77db5883d726d2c42
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-11-17 11:47:42 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-11-17 11:55:19 +0000

    pkg-serve: stat the opened descriptor instead of the path
    
    Open first and fstat() that fd, so the file checked is the one served.
    O_NONBLOCK avoids blocking on a FIFO.
---
 libexec/pkg-serve/pkg-serve.c             | 27 +++++++++++++++++++--------
 libexec/pkg-serve/tests/pkg_serve_test.sh | 20 ++++++++++++++++++++
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/libexec/pkg-serve/pkg-serve.c b/libexec/pkg-serve/pkg-serve.c
index ebfacc3e3e00..cbe3c7689a93 100644
--- a/libexec/pkg-serve/pkg-serve.c
+++ b/libexec/pkg-serve/pkg-serve.c
@@ -65,7 +65,7 @@ int
 main(int argc, char *argv[])
 {
 	struct stat st;
-	cap_rights_t rights;
+	cap_rights_t rights, ffdrights;
 	char line[MAXLINE];
 	char *file, *age;
 	size_t r, toread;
@@ -85,8 +85,7 @@ main(int argc, char *argv[])
 	if ((fd = open(basedir, O_DIRECTORY | O_RDONLY | O_CLOEXEC)) < 0)
 		err(EXIT_FAILURE, "open(%s)", basedir);
 
-	cap_rights_init(&rights, CAP_READ, CAP_FSTATAT, CAP_LOOKUP,
-	    CAP_FCNTL);
+	cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_LOOKUP);
 	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
 		err(EXIT_FAILURE, "cap_rights_limit");
 
@@ -158,28 +157,40 @@ main(int argc, char *argv[])
 			continue;
 		}
 
-		if (fstatat(fd, file, &st, AT_RESOLVE_BENEATH) == -1) {
+		ffd = openat(fd, file,
+		    O_RDONLY | O_NONBLOCK | O_RESOLVE_BENEATH | O_CLOEXEC);
+		if (ffd == -1) {
+			printf("ko: file not found\n");
+			fflush(stdout);
+			continue;
+		}
+
+		if (fstat(ffd, &st) == -1) {
+			close(ffd);
 			printf("ko: file not found\n");
 			fflush(stdout);
 			continue;
 		}
 
 		if (!S_ISREG(st.st_mode)) {
+			close(ffd);
 			printf("ko: not a file\n");
 			fflush(stdout);
 			continue;
 		}
 
 		if (st.st_mtime <= mtime) {
+			close(ffd);
 			printf("ok: 0\n");
 			fflush(stdout);
 			continue;
 		}
 
-		if ((ffd = openat(fd, file, O_RDONLY | O_RESOLVE_BENEATH)) == -1) {
-			printf("ko: file not found\n");
-			fflush(stdout);
-			continue;
+		/* Only reading is needed from now on. */
+		cap_rights_init(&ffdrights, CAP_READ);
+		if (cap_rights_limit(ffd, &ffdrights) < 0 && errno != ENOSYS) {
+			close(ffd);
+			err(EXIT_FAILURE, "cap_rights_limit");
 		}
 
 		printf("ok: %" PRIdMAX "\n", (intmax_t)st.st_size);
diff --git a/libexec/pkg-serve/tests/pkg_serve_test.sh b/libexec/pkg-serve/tests/pkg_serve_test.sh
index 9c872f3ecdb4..60ab92e2114c 100644
--- a/libexec/pkg-serve/tests/pkg_serve_test.sh
+++ b/libexec/pkg-serve/tests/pkg_serve_test.sh
@@ -152,6 +152,25 @@ get_empty_arg_body()
 	    atf_fail "expected bad command get"
 }
 
+atf_test_case get_fifo
+get_fifo_head()
+{
+	atf_set "descr" "Requesting a FIFO returns ko without blocking"
+}
+get_fifo_body()
+{
+	mkdir repo
+	mkfifo repo/fifo
+	output=$(printf 'get fifo 0\nquit\n' | timeout 10 "${PKG_SERVE}" repo)
+	case "$output" in
+	*"ko: not a file"*)
+		;;
+	*)
+		atf_fail "expected 'not a file', got: ${output}"
+		;;
+	esac
+}
+
 atf_test_case path_traversal
 path_traversal_head()
 {
@@ -256,6 +275,7 @@ atf_init_test_cases()
 	atf_add_test_case get_bad_age
 	atf_add_test_case get_empty_arg
 	atf_add_test_case command_too_long
+	atf_add_test_case get_fifo
 	atf_add_test_case path_traversal
 	atf_add_test_case get_subdir_file
 	atf_add_test_case multiple_gets