git: 1c8725c9e900 - main - ipfw_nat: fix the buffer length check in IP_FW_NAT44_XGETLOG
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 16 Nov 2026 18:47:30 UTC
The branch main has been updated by maxim:
URL: https://cgit.FreeBSD.org/src/commit/?id=1c8725c9e900da333d376e22133a95193805ded3
commit 1c8725c9e900da333d376e22133a95193805ded3
Author: Maxim Konovalov <maxim@FreeBSD.org>
AuthorDate: 2026-11-12 08:39:57 +0000
Commit: Maxim Konovalov <maxim@FreeBSD.org>
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 <maxim@FreeBSD.org>
+ *
+ * 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 <sys/types.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/ip_fw.h>
+
+#include <alias.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#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());
+}