From nobody Tue Nov 17 11:56:48 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jTNW5098Gz71JtT for ; Tue, 17 Nov 2026 11:56:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jTNW44cmBz4HRh for ; Tue, 17 Nov 2026 11:56:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794916608; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Oe4xSbh0/yjs/kgpiHCp7Bzm4TM3JF5sSSfSn+f2YRA=; b=oOF1kg89rAzXI3OpiXxfmZwzsVxD5/FYeoCrUyHFONYzichN6eBwtLbWq5TNqS7WgIuyAv 49N1inMpYJMyIxB9hE//+VUCFDyeAcH9wljOCA4cZAsbRptgjpe8fhZBmB9eZDDM/7XwmK +mLZ5y2nxZMLe4YysoWtanPQdj1HfwxQAo10a+Xc4ZMcACTxOxe56rFI6HquGcMZ+7azR3 9TlStQYdSByTUcvQHL2kc4fyRuMCJ7QnF34XHY3FA8o0frIQnrC3HG3vjBy+cHLREDowBl URZrmHE4UpRzuZaRbZMV320B2qj/xKnwe0MhVGTWxduojq2tw3/13jDLdIF8fw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794916608; b=PrfO9g09GTE4IcPtyhQH4s9lc86NtfsXdWPWM78f6MNtnH4kNAx/TCoQLxtiO9dAvxuleU jM5ga7QNmIsFHvc9pDDpt96ugxTjCI2BbqF80jHPkkNwgX1JxXvezlTEzVcwo8eAYJVnxM 0K5rjfRAry6vJRNXmaZ4tVIEGrRcRZpZSkTnQ4ySb5OzwHV9NNXSc6Oo9kcrkFEhZWn0RV ItduHh8emTpH2nVVsWAN3vNFhRKgYQ6+qpqP/VvCyfd+tmMDOxzwIONeM0RDR7vf18ahL8 ibMGq2hv402s9oo4MzdhoBipwyaMKCiSA5JnmDx0I3ykqU7u54nToDaG6B7Yow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794916608; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Oe4xSbh0/yjs/kgpiHCp7Bzm4TM3JF5sSSfSn+f2YRA=; b=B8vsZZiI/NF0sr5B1f2ofM82MKuvLduHfZNoSREn90zh6mxI4/0qxodPjH84ENxUwr/bux 5WNHPXpBnrCOcKjqYqM5dMU0YBwRnePYBfuoxyaCnuJS0LOpOimn8OVQTHexWV2gI8OLYD 1ya0jCOPzF28CK8Uorjf0b/gdtCu4OWJ5w/ylwr6H08ikZFnF4iYFDF/KtzSgnUFUiEVDG LO8/+fuyt83uHhs9NP2yAZM27akIWv5tQtdZD5cFOMjUBUggv0iaY6ZVJU/NgWMZm/ESPr hnEbGcIC6ArKXvT6OHtX9iM/CFSS0wo7IzAcYudrJ5qeBwG4h4f3LI1ms+eSfg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jTNW43cyrzgXy for ; Tue, 17 Nov 2026 11:56:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1e825 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 17 Nov 2026 11:56:48 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 309650be43aa - main - pkg-serve: bound the command line length List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 309650be43aa3f335f977320332bebbf7aa0b6ec Auto-Submitted: auto-generated Date: Tue, 17 Nov 2026 11:56:48 +0000 Message-Id: <6afc4100.1e825.70681947@gitrepo.freebsd.org> The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=309650be43aa3f335f977320332bebbf7aa0b6ec commit 309650be43aa3f335f977320332bebbf7aa0b6ec Author: Baptiste Daroussin AuthorDate: 2026-11-17 11:34:08 +0000 Commit: Baptiste Daroussin 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 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