git: 309650be43aa - main - pkg-serve: bound the command line length

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

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

commit 309650be43aa3f335f977320332bebbf7aa0b6ec
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-11-17 11:34:08 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-11-17 11:55:18 +0000

    pkg-serve: bound the command line length
    
    getline(3) grows its buffer without limit, so a client could make the
    process consume unbounded memory.  Read commands with a bounded
    reader, drain and reject lines longer than MAXLINE with
    "ko: command too long", and document the limit.
---
 libexec/pkg-serve/pkg-serve.8             |  5 +++-
 libexec/pkg-serve/pkg-serve.c             | 40 ++++++++++++++++++++++++++-----
 libexec/pkg-serve/tests/pkg_serve_test.sh | 33 +++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/libexec/pkg-serve/pkg-serve.8 b/libexec/pkg-serve/pkg-serve.8
index 2d6826d45c88..05d23f970978 100644
--- a/libexec/pkg-serve/pkg-serve.8
+++ b/libexec/pkg-serve/pkg-serve.8
@@ -2,7 +2,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.Dd March 17, 2026
+.Dd November 17, 2026
 .Dt PKG-SERVE 8
 .Os
 .Sh NAME
@@ -32,6 +32,9 @@ enters a Capsicum sandbox, restricting filesystem access to
 .Ar basedir .
 .Sh PROTOCOL
 The protocol is line-oriented.
+Command lines are limited to 4096 bytes; a longer line is discarded and
+answered with
+.Dq Li "ko: command too long" .
 Upon connection, the server sends a greeting:
 .Bd -literal -offset indent
 ok: pkg-serve <version>
diff --git a/libexec/pkg-serve/pkg-serve.c b/libexec/pkg-serve/pkg-serve.c
index 56770ef37f88..ebfacc3e3e00 100644
--- a/libexec/pkg-serve/pkg-serve.c
+++ b/libexec/pkg-serve/pkg-serve.c
@@ -27,6 +27,7 @@
 
 #define VERSION "0.1"
 #define BUFSZ 32768
+#define MAXLINE 4096
 
 static void
 usage(void)
@@ -35,14 +36,39 @@ usage(void)
 	exit(EXIT_FAILURE);
 }
 
+static ssize_t
+readcmd(char *buf, size_t size, FILE *fp)
+{
+	size_t len;
+	int c;
+
+	len = 0;
+	while ((c = fgetc(fp)) != EOF && c != '\n') {
+		if (len + 1 >= size) {
+			/* Line too long: discard the rest of it. */
+			while ((c = fgetc(fp)) != EOF && c != '\n')
+				;
+			buf[0] = '\0';
+			return (-2);
+		}
+		buf[len++] = (char)c;
+	}
+
+	if (c == EOF && len == 0)
+		return (-1);
+
+	buf[len] = '\0';
+	return ((ssize_t)len);
+}
+
 int
 main(int argc, char *argv[])
 {
 	struct stat st;
 	cap_rights_t rights;
-	char *line = NULL;
+	char line[MAXLINE];
 	char *file, *age;
-	size_t linecap = 0, r, toread;
+	size_t r, toread;
 	ssize_t linelen;
 	off_t remaining;
 	time_t mtime;
@@ -70,10 +96,12 @@ main(int argc, char *argv[])
 	printf("ok: pkg-serve " VERSION "\n");
 	fflush(stdout);
 
-	while ((linelen = getline(&line, &linecap, stdin)) > 0) {
-		/* trim newline */
-		if (linelen > 0 && line[linelen - 1] == '\n')
-			line[--linelen] = '\0';
+	while ((linelen = readcmd(line, sizeof(line), stdin)) != -1) {
+		if (linelen == -2) {
+			printf("ko: command too long\n");
+			fflush(stdout);
+			continue;
+		}
 
 		if (linelen == 0)
 			continue;
diff --git a/libexec/pkg-serve/tests/pkg_serve_test.sh b/libexec/pkg-serve/tests/pkg_serve_test.sh
index fc9cf2101369..9c872f3ecdb4 100644
--- a/libexec/pkg-serve/tests/pkg_serve_test.sh
+++ b/libexec/pkg-serve/tests/pkg_serve_test.sh
@@ -211,6 +211,38 @@ bad_basedir_body()
 	    "${PKG_SERVE}" /nonexistent/path
 }
 
+atf_test_case command_too_long
+command_too_long_head()
+{
+	atf_set "descr" "Overlong command line is rejected and does not desync the stream"
+}
+command_too_long_body()
+{
+	mkdir repo
+	echo "testcontent" > repo/test.pkg
+	# A line far beyond the per-command limit must be discarded, and the
+	# commands that follow it on the same stream must still be processed.
+	output=$(awk 'BEGIN {
+	    for (i = 0; i < 100000; i++)
+		printf "A"
+	    printf "\nget test.pkg 0\nquit\n"
+	}' | "${PKG_SERVE}" repo)
+	case "$output" in
+	*"ko: command too long"*)
+		;;
+	*)
+		atf_fail "expected 'command too long', got: ${output}"
+		;;
+	esac
+	case "$output" in
+	*"ok: 12"*"testcontent"*)
+		;;
+	*)
+		atf_fail "server did not recover after overlong line: ${output}"
+		;;
+	esac
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case greeting
@@ -223,6 +255,7 @@ atf_init_test_cases()
 	atf_add_test_case get_missing_age
 	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 path_traversal
 	atf_add_test_case get_subdir_file
 	atf_add_test_case multiple_gets