Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177726594
D61731.1795140267.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None
D61731.1795140267.diff
View Options
diff --git a/sys/netpfil/ipfw/ip_fw_nat.c b/sys/netpfil/ipfw/ip_fw_nat.c
--- a/sys/netpfil/ipfw/ip_fw_nat.c
+++ b/sys/netpfil/ipfw/ip_fw_nat.c
@@ -189,6 +189,10 @@
}
}
+/*
+ * The redirs and spool entries in buf must have been validated with
+ * nat44_check_cfg().
+ */
static int
add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
{
@@ -559,6 +563,34 @@
free_nat_instance(ptr);
}
+/*
+ * Redirs and their spool entries are interleaved. Check that all the
+ * redirs and the spool entries each of them claims fit into the
+ * request, so that add_redir_spool_cfg() cannot read past its end.
+ */
+static int
+nat44_check_cfg(const char *buf, size_t len, uint32_t redir_cnt)
+{
+ const struct nat44_cfg_redir *r;
+ size_t size;
+ uint32_t i;
+
+ for (i = 0; i < redir_cnt; i++) {
+ if (len < sizeof(*r))
+ return (EINVAL);
+ r = (const struct nat44_cfg_redir *)buf;
+ buf += sizeof(*r);
+ len -= sizeof(*r);
+ if (r->spool_cnt > len / sizeof(struct nat44_cfg_spool))
+ return (EINVAL);
+ size = r->spool_cnt * sizeof(struct nat44_cfg_spool);
+ buf += size;
+ len -= size;
+ }
+
+ return (0);
+}
+
/*
* Creates/configure nat44 instance
* Data layout (v0)(current):
@@ -596,8 +628,9 @@
return (EINVAL);
read = sizeof(*oh) + sizeof(*ucfg);
- /* Check number of redirs */
- if (sd->valsize < read + ucfg->redir_cnt*sizeof(struct nat44_cfg_redir))
+ /* Check that all redirs and their spool entries were passed in */
+ if (nat44_check_cfg((const char *)(ucfg + 1), sd->valsize - read,
+ ucfg->redir_cnt) != 0)
return (EINVAL);
nat44_config(chain, ucfg);
@@ -964,7 +997,8 @@
ucfg->mode = cfg->mode;
ucfg->redir_cnt = cfg->redir_cnt;
- if (len < sizeof(*cfg) + cfg->redir_cnt * sizeof(*rdir)) {
+ if (cfg->redir_cnt < 0 ||
+ (len - sizeof(*cfg)) / sizeof(*rdir) < (size_t)cfg->redir_cnt) {
error = EINVAL;
goto out;
}
@@ -988,6 +1022,16 @@
rdir++;
}
+ /*
+ * Spool entries are not converted, so any spool_cnt has to fit
+ * into the zeroed tail of the conversion buffer.
+ */
+ if (nat44_check_cfg((const char *)(ucfg + 1),
+ len2 - sizeof(*ucfg), ucfg->redir_cnt) != 0) {
+ error = EINVAL;
+ goto out;
+ }
+
nat44_config(&V_layer3_chain, ucfg);
out:
diff --git a/tests/sys/netpfil/ipfw/Makefile b/tests/sys/netpfil/ipfw/Makefile
--- a/tests/sys/netpfil/ipfw/Makefile
+++ b/tests/sys/netpfil/ipfw/Makefile
@@ -2,6 +2,8 @@
TESTSDIR= ${TESTSBASE}/sys/netpfil/ipfw
+ATF_TESTS_C+= nat44_config
+
ATF_TESTS_SH+= fwd \
divert \
ipv6-flow-id \
diff --git a/tests/sys/netpfil/ipfw/nat44_config.c b/tests/sys/netpfil/ipfw/nat44_config.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/nat44_config.c
@@ -0,0 +1,337 @@
+/*-
+ * 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.
+ */
+
+/*
+ * Length checks of IP_FW_NAT44_XCONFIG requests.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/ip_fw.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <atf-c.h>
+
+#define NAT_KMODS "ipfw ipfw_nat"
+
+struct req {
+ char *buf;
+ size_t len;
+ struct nat44_cfg_nat *ucfg;
+};
+
+static int
+fw_socket(void)
+{
+ int s;
+
+ s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+ ATF_REQUIRE_MSG(s >= 0, "socket: %s", strerror(errno));
+ return (s);
+}
+
+static void
+fill_oh(ipfw_obj_header *oh, uint16_t op, const char *name)
+{
+ oh->opheader.opcode = op;
+ oh->opheader.version = IP_FW3_OPVER;
+ oh->ntlv.head.type = IPFW_TLV_EACTION_NAME(1);
+ oh->ntlv.head.length = sizeof(oh->ntlv);
+ oh->ntlv.idx = 1;
+ strlcpy(oh->ntlv.name, name, sizeof(oh->ntlv.name));
+}
+
+/* Start an XCONFIG request for the instance, with room for extra bytes. */
+static void
+req_init(struct req *rq, const char *name, uint32_t redir_cnt, size_t extra)
+{
+ ipfw_obj_header *oh;
+
+ rq->len = sizeof(*oh) + sizeof(*rq->ucfg);
+ rq->buf = calloc(1, rq->len + extra);
+ ATF_REQUIRE(rq->buf != NULL);
+ oh = (ipfw_obj_header *)(void *)rq->buf;
+ fill_oh(oh, IP_FW_NAT44_XCONFIG, name);
+ rq->ucfg = (struct nat44_cfg_nat *)(oh + 1);
+ strlcpy(rq->ucfg->name, name, sizeof(rq->ucfg->name));
+ rq->ucfg->redir_cnt = redir_cnt;
+}
+
+/* Append a redirect_addr redir that claims spool_cnt spool entries. */
+static void
+req_add_redir(struct req *rq, uint16_t spool_cnt)
+{
+ struct nat44_cfg_redir *r;
+
+ r = (struct nat44_cfg_redir *)(void *)(rq->buf + rq->len);
+ r->mode = NAT44_REDIR_ADDR;
+ inet_pton(AF_INET, "192.0.2.1", &r->laddr);
+ inet_pton(AF_INET, "198.51.100.1", &r->paddr);
+ r->pport_cnt = 1;
+ r->spool_cnt = spool_cnt;
+ rq->len += sizeof(*r);
+}
+
+/* Append one spool entry. */
+static void
+req_add_spool(struct req *rq, const char *addr, uint16_t port)
+{
+ struct nat44_cfg_spool *sp;
+
+ sp = (struct nat44_cfg_spool *)(void *)(rq->buf + rq->len);
+ inet_pton(AF_INET, addr, &sp->addr);
+ sp->port = port;
+ rq->len += sizeof(*sp);
+}
+
+/* Send the request; return 0 or errno. */
+static int
+req_send(int s, struct req *rq)
+{
+ int rv;
+
+ rv = setsockopt(s, IPPROTO_IP, IP_FW3, rq->buf, rq->len);
+ free(rq->buf);
+ rq->buf = NULL;
+ return (rv == 0 ? 0 : errno);
+}
+
+static void
+nat_destroy(int s, const char *name)
+{
+ ipfw_obj_header oh;
+
+ memset(&oh, 0, sizeof(oh));
+ fill_oh(&oh, IP_FW_NAT44_DESTROY, name);
+ (void)setsockopt(s, IPPROTO_IP, IP_FW3, &oh, sizeof(oh));
+}
+
+static void
+nat_head(atf_tc_t *tc, const char *descr)
+{
+ atf_tc_set_md_var(tc, "descr", descr);
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "require.kmods", NAT_KMODS);
+}
+
+ATF_TC(spool_missing);
+ATF_TC_HEAD(spool_missing, tc)
+{
+ nat_head(tc, "A redir whose spool entries are not in the request "
+ "is rejected");
+}
+ATF_TC_BODY(spool_missing, tc)
+{
+ struct req rq;
+ int error, s;
+
+ s = fw_socket();
+ req_init(&rq, "101", 1, sizeof(struct nat44_cfg_redir));
+ req_add_redir(&rq, 16);
+ error = req_send(s, &rq);
+ nat_destroy(s, "101");
+ ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+}
+
+ATF_TC(spool_short);
+ATF_TC_HEAD(spool_short, tc)
+{
+ nat_head(tc, "A request one spool entry short of the last redir "
+ "is rejected");
+}
+ATF_TC_BODY(spool_short, tc)
+{
+ struct req rq;
+ int error, s;
+
+ s = fw_socket();
+ req_init(&rq, "102", 2, 2 * sizeof(struct nat44_cfg_redir) +
+ 2 * sizeof(struct nat44_cfg_spool));
+ req_add_redir(&rq, 1);
+ req_add_spool(&rq, "192.0.2.10", 0);
+ req_add_redir(&rq, 2);
+ req_add_spool(&rq, "192.0.2.20", 0);
+ error = req_send(s, &rq);
+ nat_destroy(s, "102");
+ ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+}
+
+ATF_TC(redir_cnt_overflow);
+ATF_TC_HEAD(redir_cnt_overflow, tc)
+{
+ nat_head(tc, "A redir_cnt that wraps the request size computation "
+ "on 32-bit platforms is rejected");
+}
+ATF_TC_BODY(redir_cnt_overflow, tc)
+{
+ struct req rq;
+ int error, s;
+
+ s = fw_socket();
+ /* 0x08000000 * sizeof(struct nat44_cfg_redir) == 2^32 */
+ req_init(&rq, "103", 0x08000000, sizeof(struct nat44_cfg_redir));
+ req_add_redir(&rq, 0);
+ error = req_send(s, &rq);
+ nat_destroy(s, "103");
+ ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+}
+
+/* Configure the instance with one redirect_addr redir and two servers. */
+static void
+nat_config_lsnat(int s, const char *name)
+{
+ struct req rq;
+ int error;
+
+ req_init(&rq, name, 1, sizeof(struct nat44_cfg_redir) +
+ 2 * sizeof(struct nat44_cfg_spool));
+ req_add_redir(&rq, 2);
+ req_add_spool(&rq, "192.0.2.10", 0);
+ req_add_spool(&rq, "192.0.2.11", 0);
+ error = req_send(s, &rq);
+ ATF_REQUIRE_EQ_MSG(0, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+}
+
+/* Read the instance back and check it is what nat_config_lsnat() set. */
+static void
+nat_check_lsnat(int s, const char *name)
+{
+ struct nat44_cfg_nat *ucfg;
+ struct nat44_cfg_redir *r;
+ struct nat44_cfg_spool *sp;
+ ipfw_obj_header *oh;
+ socklen_t sz;
+ char *buf;
+ in_addr_t a1, a2;
+ int seen;
+
+ /* Room for more than expected, so that extra entries show up. */
+ sz = sizeof(*oh) + sizeof(*ucfg) + 4 * sizeof(*r) + 8 * sizeof(*sp);
+ buf = calloc(1, sz);
+ ATF_REQUIRE(buf != NULL);
+ oh = (ipfw_obj_header *)(void *)buf;
+ fill_oh(oh, IP_FW_NAT44_XGETCONFIG, name);
+ ucfg = (struct nat44_cfg_nat *)(oh + 1);
+ strlcpy(ucfg->name, name, sizeof(ucfg->name));
+ ATF_REQUIRE_MSG(getsockopt(s, IPPROTO_IP, IP_FW3, buf, &sz) == 0,
+ "XGETCONFIG: %s", strerror(errno));
+
+ r = (struct nat44_cfg_redir *)(ucfg + 1);
+ sp = (struct nat44_cfg_spool *)(r + 1);
+ ATF_REQUIRE_EQ(1, ucfg->redir_cnt);
+ ATF_CHECK_EQ(sizeof(*oh) + sizeof(*ucfg) + sizeof(*r) +
+ 2 * sizeof(*sp), ucfg->size);
+ ATF_REQUIRE_EQ(2, r->spool_cnt);
+ /* The kernel keeps spool entries in reverse order. */
+ a1 = inet_addr("192.0.2.10");
+ a2 = inet_addr("192.0.2.11");
+ seen = 0;
+ for (int i = 0; i < 2; i++) {
+ if (sp[i].addr.s_addr == a1)
+ seen |= 1;
+ else if (sp[i].addr.s_addr == a2)
+ seen |= 2;
+ }
+ ATF_CHECK_EQ_MSG(3, seen, "spool entries did not round-trip");
+ free(buf);
+}
+
+ATF_TC(spool_roundtrip);
+ATF_TC_HEAD(spool_roundtrip, tc)
+{
+ nat_head(tc, "A complete LSNAT redir is accepted and read back "
+ "unchanged");
+}
+ATF_TC_BODY(spool_roundtrip, tc)
+{
+ int s;
+
+ s = fw_socket();
+ nat_config_lsnat(s, "104");
+ nat_check_lsnat(s, "104");
+ nat_destroy(s, "104");
+}
+
+ATF_TC(reject_keeps_config);
+ATF_TC_HEAD(reject_keeps_config, tc)
+{
+ nat_head(tc, "A truncated request for an existing instance is "
+ "rejected and leaves its configuration unchanged");
+}
+ATF_TC_BODY(reject_keeps_config, tc)
+{
+ struct req rq;
+ int error, s;
+
+ s = fw_socket();
+ nat_config_lsnat(s, "105");
+
+ /* The last of two spool entries is missing. */
+ req_init(&rq, "105", 1, sizeof(struct nat44_cfg_redir) +
+ sizeof(struct nat44_cfg_spool));
+ req_add_redir(&rq, 2);
+ req_add_spool(&rq, "192.0.2.20", 0);
+ error = req_send(s, &rq);
+ ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+
+ /* The second redir, after the first one's spool, is one byte short. */
+ req_init(&rq, "105", 2, 2 * sizeof(struct nat44_cfg_redir) +
+ sizeof(struct nat44_cfg_spool));
+ req_add_redir(&rq, 1);
+ req_add_spool(&rq, "192.0.2.20", 0);
+ req_add_redir(&rq, 0);
+ rq.len--;
+ error = req_send(s, &rq);
+ ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+ strerror(error));
+
+ nat_check_lsnat(s, "105");
+ nat_destroy(s, "105");
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, spool_missing);
+ ATF_TP_ADD_TC(tp, spool_short);
+ ATF_TP_ADD_TC(tp, redir_cnt_overflow);
+ ATF_TP_ADD_TC(tp, spool_roundtrip);
+ ATF_TP_ADD_TC(tp, reject_keeps_config);
+
+ return (atf_no_error());
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 20, 2:04 AM (12 h, 35 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41857373
Default Alt Text
D61731.1795140267.diff (11 KB)
Attached To
Mode
D61731: ipfw_nat: check spool entries in NAT44 config requests
Attached
Detach File
Event Timeline