From nobody Tue Nov 17 17:05:07 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 4jTWLr28MNz71RHs for ; Tue, 17 Nov 2026 17:05:08 +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 4jTWLq5Yjrz4GtK for ; Tue, 17 Nov 2026 17:05:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794935107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9QB7K9HGZsyTjbEg6b7zCfmKvjVBrQu2cPMXleOZ0gM=; b=juIAhXv/7UN4fpV9IzjqIahM2tS2dgW8cL5tUaahrstzKUJyWl+X5gRiRHg5OUzZyNDVAg hYtE8ogx5m1at/Q5NxJePK/CX18BmdvOl2mK5dUbYusmOU80L1UtlXKgaNpub7GBV4NxeS zO49gD29Ahp6jP2Mp6+QHGuvgUaZPMqvRlDdnZqAYtK6TwFBJJ3JFVR9GCTf310KfyJvyZ /8q/P6AWybOuwTtyAOP+xSCrr5n2DXIaeNWerMBOsCs9on3MaRnaCbl98kSJu1czCob7cZ 6Pis9snUlif82gtZ+sWpm3kpG3b0IhcaeU/y3bfKKiO5yOIPSe290H281Ng7uA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794935107; b=k8nfy2N/j0DajlrwnOFUOAMwtpEVoT7vA3NliWHVJYzG/+0kfpdrHPanrdT9sF9o5s23I4 /QrwLEASUQdKoHkeRKA8yVlJDJIhG04Syt16VC3y+cZLeprUKph6SpMmV24hUFn8Gaop4Z 9n/Z0TpZ3rx1e6OsqOIPo40YpV+eFYSFEfHbCqGUKO5EyiFgcgCHCK3ajM05OCQaGnyumU /LN2J7VV98mmIwvDjUN00ld+ueptwmaSPlzJehcUfjoPjMXGPxMDPuPtCaj23rrFvGr15g R4E+zcR5cdcJLWM7u1plJUUqopgacjsvAUaIFlGlUf42Oz8Q/1+XUDhttsb51Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794935107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9QB7K9HGZsyTjbEg6b7zCfmKvjVBrQu2cPMXleOZ0gM=; b=j2Agx7HqoGREMWacC1a1JkNZym+B6j+Ko+PqIcfZwKt9Ok3rKVYS8vMrCU4f32OR65ARaV EgU4xxx9oi15w8MxyNo6uUXnqqXEPcdJmqEYSQch3sI+KORcI5SMQ1Hwg7hAI1KLx8HKQW /YFc+gghGowvHhtDwNJz9j+EA3yEO4iH3EvZjFCCtfdgn5e5fAF8uIyKMsQ6fggHhUj3lA OumaZMTqK8L8BdynJb+1rqZWjhOo+VvRg4BPQ4p3UMDo9fIjC3ZkiPIg1DMwT1sSUVmT3N SpyXgcfshhYaf+Ihw2PzjiVF0u0awxWOSDYwPyw6ZtJO4tMOEFjWbU6h3EezXQ== 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 4jTWLq4xLqzrS9 for ; Tue, 17 Nov 2026 17:05:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1cee9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 17 Nov 2026 17:05:07 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Cc: R. Christian McDonald From: Mark Johnston Subject: git: aedfc347d081 - releng/14.5 - netlink: Make room for the unfinished message when growing the buffer 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.5 X-Git-Reftype: branch X-Git-Commit: aedfc347d08168d22b429081b9f05e42f558f3c1 Auto-Submitted: auto-generated Date: Tue, 17 Nov 2026 17:05:07 +0000 Message-Id: <6afc8943.1cee9.591b31c6@gitrepo.freebsd.org> The branch releng/14.5 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=aedfc347d08168d22b429081b9f05e42f558f3c1 commit aedfc347d08168d22b429081b9f05e42f558f3c1 Author: R. Christian McDonald AuthorDate: 2026-10-11 20:46:56 +0000 Commit: Mark Johnston CommitDate: 2026-11-16 21:17:12 +0000 netlink: Make room for the unfinished message when growing the buffer When a message outgrows the writer's buffer, _nlmsg_refill_buffer() allocates a larger one and moves the unfinished message there. The new buffer was sized for the data about to be added alone, so with enough of the message already written the caller wrote past its end and left it with datalen > buflen. A 20-byte message plus an attribute of 8176 bytes got a buffer of 8192 bytes for 8200 bytes of data, and reading it panicked an INVARIANTS kernel in nl_soreceive(). Size the new buffer for both, and add a ktest for it. Approved by: so Security: FreeBSD-SA-26:77.netlink Security: CVE-2026-127747 Reviewed by: glebius Fixes: 7e5bf68495cc ("netlink: add netlink support") MFC after: 1 week Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D60541 (cherry picked from commit 5180fce1cf7ec7430ccf2adb88358602e5ccee1e) (cherry picked from commit dc9f234163826314a72c7560fcd6a866c13b8120) --- sys/netlink/ktest_netlink_message_writer.c | 94 ++++++++++++++++++++++++ sys/netlink/netlink_message_writer.c | 18 +++-- tests/sys/netlink/test_netlink_message_writer.py | 23 ++++++ 3 files changed, 127 insertions(+), 8 deletions(-) diff --git a/sys/netlink/ktest_netlink_message_writer.c b/sys/netlink/ktest_netlink_message_writer.c index 4f894d5e070a..4058b24eba64 100644 --- a/sys/netlink/ktest_netlink_message_writer.c +++ b/sys/netlink/ktest_netlink_message_writer.c @@ -145,6 +145,94 @@ test_mbuf_chain_allocation(struct ktest_test_context *ctx) return (0); } +struct test_nlbuf_refill_attrs { + uint32_t size; + uint32_t completed; + uint32_t unfinished; + uint32_t attr_len; +}; + +#define _OUT(_field) offsetof(struct test_nlbuf_refill_attrs, _field) +static const struct nlattr_parser nla_p_nlbuf_refill[] = { + { .type = 1, .off = _OUT(size), .cb = nlattr_get_uint32 }, + { .type = 2, .off = _OUT(completed), .cb = nlattr_get_uint32 }, + { .type = 3, .off = _OUT(unfinished), .cb = nlattr_get_uint32 }, + { .type = 4, .off = _OUT(attr_len), .cb = nlattr_get_uint32 }, +}; +#undef _OUT +NL_DECLARE_ATTR_PARSER(nlbuf_refill_parser, nla_p_nlbuf_refill); + +static int +test_nlbuf_refill_parser(struct ktest_test_context *ctx, struct nlattr *nla) +{ + struct test_nlbuf_refill_attrs *attrs; + + attrs = npt_alloc(ctx->npt, sizeof(*attrs)); + ctx->arg = attrs; + if (attrs != NULL) + return (nl_parse_nested(nla, &nlbuf_refill_parser, ctx->npt, + attrs)); + return (ENOMEM); +} + +static bool +test_nlbuf_discard(struct nl_writer *nw, void *buf, int buflen, int count) +{ + nw->offset = 0; + return (true); +} + +/* + * Add an attribute that does not fit into the buffer to an unfinished + * message, optionally preceded by a completed one. + */ +static int +test_nlbuf_writer_refill(struct ktest_test_context *ctx) +{ + struct test_nlbuf_refill_attrs *attrs = ctx->arg; + struct nl_writer nw = {}; + void *data; + u_int expected; + int error = 0; + + if (attrs == NULL || attrs->unfinished < sizeof(struct nlmsghdr) || + (attrs->completed != 0 && + attrs->completed < sizeof(struct nlmsghdr))) + return (EINVAL); + if (!nlmsg_get_buf_type_wrapper(&nw, attrs->size, NS_WRITER_TYPE_BUF, true)) + return (EINVAL); + nw.cb = test_nlbuf_discard; + data = malloc(attrs->attr_len, M_TEMP, M_WAITOK | M_ZERO); + + if (attrs->completed != 0) { + if (!nlmsg_add(&nw, 0, 0, NLMSG_NOOP, 0, 0) || + nlmsg_reserve_data(&nw, attrs->completed - + sizeof(struct nlmsghdr), void *) == NULL || + !nlmsg_end(&nw)) { + error = EINVAL; + goto out; + } + } + if (!nlmsg_add(&nw, 0, 0, NLMSG_NOOP, 0, 0) || + nlmsg_reserve_data(&nw, attrs->unfinished - + sizeof(struct nlmsghdr), void *) == NULL || + !nlattr_add(&nw, 1, attrs->attr_len, data)) { + error = EINVAL; + goto out; + } + + expected = NETLINK_ALIGN(attrs->unfinished) + + NLA_ALIGN(attrs->attr_len + sizeof(struct nlattr)); + KTEST_LOG(ctx, "message of %u bytes in a buffer of %u, expected %u", + nw.offset, nw.alloc_len, expected); + if (nw.offset > nw.alloc_len || nw.offset != expected) + error = EINVAL; +out: + free(data, M_TEMP); + + return (error); +} + static const struct ktest_test_info tests[] = { { .name = "test_mbuf_writer_allocation", @@ -158,5 +246,11 @@ static const struct ktest_test_info tests[] = { .func = &test_mbuf_chain_allocation, .parse = &test_mbuf_parser, }, + { + .name = "test_nlbuf_writer_refill", + .desc = "test growing the buffer under an unfinished message", + .func = &test_nlbuf_writer_refill, + .parse = &test_nlbuf_refill_parser, + }, }; KTEST_MODULE_DECLARE(ktest_netlink_message_writer, tests); diff --git a/sys/netlink/netlink_message_writer.c b/sys/netlink/netlink_message_writer.c index 8443cc59d9e0..5682b9d10106 100644 --- a/sys/netlink/netlink_message_writer.c +++ b/sys/netlink/netlink_message_writer.c @@ -603,7 +603,7 @@ bool _nlmsg_refill_buffer(struct nl_writer *nw, int required_len) { struct nl_writer ns_new = {}; - int completed_len, new_len; + size_t completed_len, new_len, last_len; if (nw->enomem) return (false); @@ -611,8 +611,10 @@ _nlmsg_refill_buffer(struct nl_writer *nw, int required_len) NL_LOG(LOG_DEBUG3, "no space at offset %d/%d (want %d), trying to reclaim", nw->offset, nw->alloc_len, required_len); - /* Calculated new buffer size and allocate it s*/ - completed_len = (nw->hdr != NULL) ? (char *)nw->hdr - nw->data : nw->offset; + /* Calculate new buffer size and allocate it. */ + completed_len = (nw->hdr != NULL) ? + (char *)nw->hdr - nw->data : nw->offset; + last_len = nw->offset - completed_len; if (completed_len > 0 && required_len < NLMBUFSIZE) { /* We already ran out of space, use the largest effective size */ new_len = max(nw->alloc_len, NLMBUFSIZE); @@ -621,9 +623,10 @@ _nlmsg_refill_buffer(struct nl_writer *nw, int required_len) new_len = NLMBUFSIZE; else new_len = nw->alloc_len * 2; - while (new_len < required_len) - new_len *= 2; } + /* The unfinished message is copied over and needs room as well. */ + while (new_len < last_len + required_len) + new_len *= 2; bool waitok = (nw->malloc_flag == M_WAITOK); bool is_linux = (nw->writer_type == NS_WRITER_TYPE_LBUF); if (!nlmsg_get_buf(&ns_new, new_len, waitok, is_linux)) { @@ -639,15 +642,14 @@ _nlmsg_refill_buffer(struct nl_writer *nw, int required_len) nlmsg_set_callback(&ns_new); ns_new.arg = nw->arg; - /* Copy last (unfinished) header to the new storage */ - int last_len = nw->offset - completed_len; + /* Copy last (unfinished) header to the new storage. */ if (last_len > 0) { memcpy(ns_new.data, nw->hdr, last_len); ns_new.hdr = (struct nlmsghdr *)ns_new.data; ns_new.offset = last_len; } - NL_LOG(LOG_DEBUG2, "completed: %d bytes, copied: %d bytes", completed_len, last_len); + NL_LOG(LOG_DEBUG2, "completed: %zu bytes, copied: %zu bytes", completed_len, last_len); /* Flush completed headers & switch to the new nw */ nlmsg_flush(nw); diff --git a/tests/sys/netlink/test_netlink_message_writer.py b/tests/sys/netlink/test_netlink_message_writer.py index df1768129b11..f3485eb9c313 100644 --- a/tests/sys/netlink/test_netlink_message_writer.py +++ b/tests/sys/netlink/test_netlink_message_writer.py @@ -17,6 +17,8 @@ MJUMPAGESIZE = mmap.PAGESIZE MJUM9BYTES = 9 * 1024 MJUM16BYTES = 16 * 1024 +NLMSG_SMALL = 128 +NLMSG_LARGE = 2048 class TestNetlinkMessageWriter(BaseKernelTest): KTEST_MODULE_NAME = "ktest_netlink_message_writer" @@ -77,3 +79,24 @@ class TestNetlinkMessageWriter(BaseKernelTest): NlAttrU32(5, malloc_flags), ] self.runtest(test_meta) + + @pytest.mark.parametrize( + "sz", + [ + # buffer size, completed message, unfinished message, attribute + pytest.param([NLMSG_SMALL, 0, 20, 112], id="small"), + pytest.param([NLMSG_SMALL, 0, 20, 8176], id="large_attr"), + pytest.param([NLMSG_LARGE, 0, 2000, 4092], id="large_msg"), + pytest.param([NLMSG_LARGE, 100, 1900, 196], id="after_completed"), + ], + ) + def test_nlbuf_writer_refill(self, sz): + """override to parametrize""" + + test_meta = [ + NlAttrU32(1, sz[0]), # size + NlAttrU32(2, sz[1]), # completed + NlAttrU32(3, sz[2]), # unfinished + NlAttrU32(4, sz[3]), # attr_len + ] + self.runtest(test_meta)