From nobody Mon Nov 16 18:47:30 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 4jSxgR046lz70Ysh for ; Mon, 16 Nov 2026 18:47:31 +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 4jSxgQ6fXzz4PY3 for ; Mon, 16 Nov 2026 18:47:30 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794854850; 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=EachXYvme8CXkpMCDYzqlarz2RMs4kIyF2ozTLopKrM=; b=Mbizp6fRyArzk+Iyw56X+7QQ5RfjI2wvDWFs7SBbyhzWcs+j6hKTy9tcqQG8qEAfHQnkSV E2rqQm8dnxmFZ3uNwAGUC8V82W9Yt1d6MDlKtuAgeta4IW56hZPacgn16C075MvOSDLZvN iA2EyfGMo/Put4Ken04zOx2/LF6e12opJbyuEaiRt1vlvFTHPsVqF8J2dRaEbKdsGVvc0C 8fAR1cmpKHPQhWwLaTj4HzdDJgKWGSguIhUgDl86LqUXHyqsgatLYkXFHWzU9wQntynMkH Cr5NjxdvxNLul/q15ArnfqoF1c3NdtgY/xP8UWTALSGMQSN75o9zknLXhtGHcg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794854850; b=Ip7R9pL8pBaOIoH23Wd09cF99Asod4AqhGZLRJZLg4pAXXV5xlbxaD5WqBhvFrIk18E8bT OYYTTwz4q25zNWtDTPMgdc8FkClICnhSl00UdALCfuekKBmB6VA1O6wsoBn1I9d6KlDvET P9W+0KWPHeYVxp5Uc+oGe9xop7mcGZI0KxkhbApuXFls2m8Fhwh5QlRUgxtFrbaUIPgs7N fQec+UdNBhNCJGqpNr+JXgCC1b2el2cLRSZZr2eaIGeP3CCeCcPyKpGfzQwcABPqFlMgOZ UbXVxquagb99wUYiZf1bEGvj8Hn9ZKoU3fZWUZ+K3JpQ6YjfFd9FfTnCkPytlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794854850; 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=EachXYvme8CXkpMCDYzqlarz2RMs4kIyF2ozTLopKrM=; b=S9mkyvYy+oJT6fwGgh+vHYiMZIVJ3sbEsX5XPXyc6GDRDPRUS5rDJtfC4SVYh7EbSRc0j0 XnIQjb0ywaPzkdunHxHGdVybfbbmRkPBdSgI1xiDKPZl2ZwicMfO9RPTDepQQ4MGe6dqId brqGN4fgPGhdJJLfxBdOgMz/aEhcqe6Pfy4Fwcb7tic1ResOWg1hyGaU/iFQRQ3+7KPrbV 0Ltok8TH55Q1NS2mwuV3n7HFnYGgEH0hBjjxEqcwbtEOyG+zCJ5lTz5buLE5cSAjVNqXmu IxZ99D8A0KJQnntOUM6PxhTvhnOv4B3Vgkd9IGJD1ZW66Ubt4/pWKwejpb+ilg== 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 4jSxgQ5x2Xz163h for ; Mon, 16 Nov 2026 18:47:30 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 19d31 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 16 Nov 2026 18:47:30 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Maxim Konovalov Subject: git: 1c8725c9e900 - main - ipfw_nat: fix the buffer length check in IP_FW_NAT44_XGETLOG 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: maxim X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 1c8725c9e900da333d376e22133a95193805ded3 Auto-Submitted: auto-generated Date: Mon, 16 Nov 2026 18:47:30 +0000 Message-Id: <6afb4fc2.19d31.7ea29035@gitrepo.freebsd.org> The branch main has been updated by maxim: URL: https://cgit.FreeBSD.org/src/commit/?id=1c8725c9e900da333d376e22133a95193805ded3 commit 1c8725c9e900da333d376e22133a95193805ded3 Author: Maxim Konovalov AuthorDate: 2026-11-12 08:39:57 +0000 Commit: Maxim Konovalov CommitDate: 2026-11-16 18:43:50 +0000 ipfw_nat: fix the buffer length check in IP_FW_NAT44_XGETLOG nat44_get_log() compared the user buffer length against the request header plus another ipfw_obj_header instead of the request header plus the log buffer. A buffer that passed this check but was shorter than the full reply left too little room in the sockopt window, so ipfw_get_sopt_space() returned NULL and the following memcpy() of the log wrote through a NULL pointer, panicking the kernel. The handler was cloned from nat44_get_cfg() in the commit below, under the convention of that time where ucfg->size held the payload only and the ipfw_obj_header was added back in the length check. 9f925e8a920e ("Fix IP_FW_NAT44_LIST_NAT size calculation.") later switched nat44_get_cfg() to count the header in ucfg->size and reduced its check to "valsize < size", but nat44_get_log() was not updated. It kept the stale idiom, whose check counts the header twice, and by coincidence of the struct sizes that never leaves room for the log. Compare against the full reply length and check the result of ipfw_get_sopt_space(). Also report the full reply length, including the ipfw_obj_header, in the size field returned with ENOMEM, as nat44_get_cfg() now does: ipfw(8) retries with that value, and the old one was itself too short to pass the check. Add tests: getlog_short_buffer - every buffer shorter than the reply gets ENOMEM and the reply length; the reply length works. getlog_retry - a retry with the length reported along with ENOMEM succeeds. Without the fix, getlog_short_buffer panics the kernel and getlog_retry fails. Reviewed by: ae Fixes: d6164b77f8b7 ("Make ipfw_nat module use IP_FW3 codes.") Co-Authored-By: Claude Opus 4.8 MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D61587 --- sys/netpfil/ipfw/ip_fw_nat.c | 8 +- tests/sys/netpfil/ipfw/Makefile | 2 + tests/sys/netpfil/ipfw/nat_getlog.c | 226 ++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+), 2 deletions(-) diff --git a/sys/netpfil/ipfw/ip_fw_nat.c b/sys/netpfil/ipfw/ip_fw_nat.c index ab624ce7b430..0ccb2be2343b 100644 --- a/sys/netpfil/ipfw/ip_fw_nat.c +++ b/sys/netpfil/ipfw/ip_fw_nat.c @@ -857,8 +857,8 @@ nat44_get_log(struct ip_fw_chain *chain, ip_fw3_opheader *op3, export_nat_cfg(ptr, ucfg); /* Estimate memory amount */ - ucfg->size = sizeof(struct nat44_cfg_nat) + LIBALIAS_BUF_SIZE; - if (sd->valsize < sz + sizeof(*oh)) { + ucfg->size = sz + LIBALIAS_BUF_SIZE; + if (sd->valsize < ucfg->size) { /* * Submitted buffer size is not enough. * WE've already filled in @ucfg structure with @@ -870,6 +870,10 @@ nat44_get_log(struct ip_fw_chain *chain, ip_fw3_opheader *op3, } pbuf = (void *)ipfw_get_sopt_space(sd, LIBALIAS_BUF_SIZE); + if (pbuf == NULL) { + IPFW_UH_RUNLOCK(chain); + return (ENOMEM); + } memcpy(pbuf, ptr->lib->logDesc, LIBALIAS_BUF_SIZE); IPFW_UH_RUNLOCK(chain); diff --git a/tests/sys/netpfil/ipfw/Makefile b/tests/sys/netpfil/ipfw/Makefile index 8dcb4bcbebfa..131e67b1bd5a 100644 --- a/tests/sys/netpfil/ipfw/Makefile +++ b/tests/sys/netpfil/ipfw/Makefile @@ -10,6 +10,8 @@ ATF_TESTS_SH+= fwd \ table \ unmapped +ATF_TESTS_C+= nat_getlog + ${PACKAGE}FILES+= fwd_inetd.conf \ lookup_inetd.conf diff --git a/tests/sys/netpfil/ipfw/nat_getlog.c b/tests/sys/netpfil/ipfw/nat_getlog.c new file mode 100644 index 000000000000..997673a79b81 --- /dev/null +++ b/tests/sys/netpfil/ipfw/nat_getlog.c @@ -0,0 +1,226 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2026 Maxim Konovalov + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Tests for the IP_FW_NAT44_XGETLOG sockopt handled by ipfw_nat(4). + */ + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#define NAT_NAME "17" +#define NAT_CREATED "nat_created" + +#define NAT_HDRLEN (sizeof(ipfw_obj_header) + sizeof(struct nat44_cfg_nat)) +#define NAT_LOGLEN (NAT_HDRLEN + LIBALIAS_BUF_SIZE) + +static int +nat_socket(void) +{ + int s; + + s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + ATF_REQUIRE_MSG(s >= 0, "socket: %s", strerror(errno)); + return (s); +} + +static ipfw_obj_header * +nat_request(uint16_t opcode, size_t len) +{ + ipfw_obj_header *oh; + struct nat44_cfg_nat *ucfg; + + oh = calloc(1, len); + ATF_REQUIRE(oh != NULL); + oh->opheader.opcode = opcode; + oh->opheader.version = IP_FW3_OPVER; + oh->ntlv.head.length = sizeof(oh->ntlv); + strlcpy(oh->ntlv.name, NAT_NAME, sizeof(oh->ntlv.name)); + if (len >= NAT_HDRLEN) { + ucfg = (struct nat44_cfg_nat *)(oh + 1); + strlcpy(ucfg->name, NAT_NAME, sizeof(ucfg->name)); + } + return (oh); +} + +/* + * Creates a nat instance with logging enabled, which allocates the + * libalias log buffer returned by IP_FW_NAT44_XGETLOG. + */ +static void +nat_create(int s) +{ + ipfw_obj_header *oh; + struct nat44_cfg_nat *ucfg; + socklen_t optlen; + + /* Do not clobber an instance that does not belong to us. */ + oh = nat_request(IP_FW_NAT44_XGETCONFIG, NAT_HDRLEN); + optlen = NAT_HDRLEN; + if (getsockopt(s, IPPROTO_IP, IP_FW3, oh, &optlen) == 0 || + errno != ESRCH) + atf_tc_skip("nat %s already exists", NAT_NAME); + free(oh); + + oh = nat_request(IP_FW_NAT44_XCONFIG, NAT_HDRLEN); + ucfg = (struct nat44_cfg_nat *)(oh + 1); + ucfg->ip.s_addr = htonl(INADDR_LOOPBACK); + ucfg->mode = PKT_ALIAS_LOG; + ATF_REQUIRE_MSG(setsockopt(s, IPPROTO_IP, IP_FW3, oh, NAT_HDRLEN) == 0, + "IP_FW_NAT44_XCONFIG: %s", strerror(errno)); + free(oh); + atf_utils_create_file(NAT_CREATED, "%s", ""); +} + +static void +nat_destroy(void) +{ + ipfw_obj_header *oh; + int s; + + if (access(NAT_CREATED, F_OK) != 0) + return; + s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (s < 0) + return; + oh = nat_request(IP_FW_NAT44_DESTROY, sizeof(*oh)); + (void)setsockopt(s, IPPROTO_IP, IP_FW3, oh, sizeof(*oh)); + free(oh); +} + +/* + * Issues IP_FW_NAT44_XGETLOG with a buffer of len bytes. Returns 0 or + * the errno value, and the reply length and the size reported by the + * kernel in the nat44_cfg_nat header. + */ +static int +nat_getlog(int s, size_t len, size_t *replylen, uint32_t *size) +{ + ipfw_obj_header *oh; + socklen_t optlen; + int error; + + oh = nat_request(IP_FW_NAT44_XGETLOG, len); + optlen = len; + error = 0; + if (getsockopt(s, IPPROTO_IP, IP_FW3, oh, &optlen) != 0) + error = errno; + *replylen = optlen; + *size = ((struct nat44_cfg_nat *)(oh + 1))->size; + free(oh); + return (error); +} + +ATF_TC_WITH_CLEANUP(getlog_short_buffer); +ATF_TC_HEAD(getlog_short_buffer, tc) +{ + atf_tc_set_md_var(tc, "descr", + "A buffer too short for the log gets ENOMEM and the needed size"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.kmods", "ipfw_nat"); +} +ATF_TC_BODY(getlog_short_buffer, tc) +{ + size_t len, replylen; + uint32_t size; + int error, s; + + s = nat_socket(); + nat_create(s); + + /* + * A buffer holding the request header but not the whole log used + * to pass the length check, and some of these lengths left too + * little sockopt buffer for the log, which was then copied to a + * NULL pointer. + */ + for (len = NAT_HDRLEN; len < NAT_LOGLEN; len++) { + error = nat_getlog(s, len, &replylen, &size); + ATF_CHECK_EQ_MSG(ENOMEM, error, "len %zu: error %d", len, + error); + ATF_CHECK_EQ_MSG(NAT_LOGLEN, size, "len %zu: size %u", len, + size); + } + + error = nat_getlog(s, NAT_LOGLEN, &replylen, &size); + ATF_REQUIRE_EQ_MSG(0, error, "len %zu: error %d", NAT_LOGLEN, error); + ATF_REQUIRE_EQ(NAT_LOGLEN, replylen); +} +ATF_TC_CLEANUP(getlog_short_buffer, tc) +{ + nat_destroy(); +} + +ATF_TC_WITH_CLEANUP(getlog_retry); +ATF_TC_HEAD(getlog_retry, tc) +{ + atf_tc_set_md_var(tc, "descr", + "A retry with the size reported along with ENOMEM gets the log"); + atf_tc_set_md_var(tc, "require.user", "root"); + atf_tc_set_md_var(tc, "require.kmods", "ipfw_nat"); +} +ATF_TC_BODY(getlog_retry, tc) +{ + size_t replylen; + uint32_t size; + int error, s; + + s = nat_socket(); + nat_create(s); + + error = nat_getlog(s, NAT_HDRLEN, &replylen, &size); + ATF_REQUIRE_EQ(ENOMEM, error); + ATF_REQUIRE(size >= NAT_HDRLEN); + + error = nat_getlog(s, size, &replylen, &size); + ATF_REQUIRE_EQ_MSG(0, error, "retry with size %u: error %d", size, + error); +} +ATF_TC_CLEANUP(getlog_retry, tc) +{ + nat_destroy(); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, getlog_short_buffer); + ATF_TP_ADD_TC(tp, getlog_retry); + + return (atf_no_error()); +}