git: 42d358453d05 - main - dummynet: validate object length before fixed-size copies in do_config()

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 16:28:42 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=42d358453d056e70bd112c63247e89c1ecf575d5

commit 42d358453d056e70bd112c63247e89c1ecf575d5
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-18 10:50:14 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-18 16:28:22 +0000

    dummynet: validate object length before fixed-size copies in do_config()
    
    do_config() checks that each command's o.len fits in the remaining
    buffer, but the DN_LINK, DN_PROFILE, DN_SCH and DN_FS cases then copy a
    fixed sizeof(struct) out of the command, which can be larger than o.len.
    A command with o.len == sizeof(struct dn_id) reads past the end of the
    sockopt buffer (DN_PROFILE copies about 4 KB).
    
    Reject a command whose o.len is shorter than the structure it carries.
    Add a test.
    
    Reviewed by:    kp
    MFC after:      1 week
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D61440
---
 sys/netpfil/ipfw/ip_dummynet.c                  |  16 ++++
 tests/sys/netpfil/common/Makefile               |   2 +
 tests/sys/netpfil/common/dummynet_config_test.c | 119 ++++++++++++++++++++++++
 3 files changed, 137 insertions(+)

diff --git a/sys/netpfil/ipfw/ip_dummynet.c b/sys/netpfil/ipfw/ip_dummynet.c
index f834fb2b7ba6..7c0f4bd10cbd 100644
--- a/sys/netpfil/ipfw/ip_dummynet.c
+++ b/sys/netpfil/ipfw/ip_dummynet.c
@@ -2100,6 +2100,10 @@ do_config(void *p, size_t l)
 				err = ENOMEM;
 				break;
 			}
+			if (o.len < sizeof(dn->link)) {
+				err = EINVAL;
+				break;
+			}
 			memcpy(&dn->link, (char *)p + off, sizeof(dn->link));
 			err = config_link(&dn->link, arg);
 			break;
@@ -2110,6 +2114,10 @@ do_config(void *p, size_t l)
 				err = ENOMEM;
 				break;
 			}
+			if (o.len < sizeof(dn->profile)) {
+				err = EINVAL;
+				break;
+			}
 			memcpy(&dn->profile, (char *)p + off,
 			    sizeof(dn->profile));
 			err = config_profile(&dn->profile, arg);
@@ -2121,6 +2129,10 @@ do_config(void *p, size_t l)
 				err = ENOMEM;
 				break;
 			}
+			if (o.len < sizeof(dn->sched)) {
+				err = EINVAL;
+				break;
+			}
 			memcpy(&dn->sched, (char *)p + off,
 			    sizeof(dn->sched));
 			err = config_sched(&dn->sched, arg);
@@ -2132,6 +2144,10 @@ do_config(void *p, size_t l)
 				err = ENOMEM;
 				break;
 			}
+			if (o.len < sizeof(dn->fs)) {
+				err = EINVAL;
+				break;
+			}
 			memcpy(&dn->fs, (char *)p + off, sizeof(dn->fs));
 			err = (NULL == config_fs(&dn->fs, arg, 0));
 			break;
diff --git a/tests/sys/netpfil/common/Makefile b/tests/sys/netpfil/common/Makefile
index 0938bd9d9c7e..721ec14a86d9 100644
--- a/tests/sys/netpfil/common/Makefile
+++ b/tests/sys/netpfil/common/Makefile
@@ -4,6 +4,8 @@ TESTSDIR=	${TESTSBASE}/sys/netpfil/common
 BINDIR=		${TESTSDIR}
 
 
+ATF_TESTS_C+=	dummynet_config_test
+
 ATF_TESTS_SH+=	\
 		dummynet \
 		pass_block \
diff --git a/tests/sys/netpfil/common/dummynet_config_test.c b/tests/sys/netpfil/common/dummynet_config_test.c
new file mode 100644
index 000000000000..ecde680c5b8b
--- /dev/null
+++ b/tests/sys/netpfil/common/dummynet_config_test.c
@@ -0,0 +1,119 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * 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 ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/ip_fw.h>
+#include <netinet/ip_dummynet.h>
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+/*
+ * do_config() validates that each command's declared length fits in the
+ * supplied buffer, but the DN_LINK, DN_PROFILE, DN_SCH and DN_FS cases
+ * copy a fixed sizeof(struct) that is larger than struct dn_id.  A command
+ * whose oid.len is only sizeof(struct dn_id) must be rejected rather than
+ * read beyond the buffer.
+ */
+static void
+check_short_object(uint8_t type)
+{
+	struct dn_id o;
+	int s, rv;
+
+	s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+	ATF_REQUIRE_MSG(s >= 0, "raw socket: %s", strerror(errno));
+
+	memset(&o, 0, sizeof(o));
+	o.len = sizeof(o);
+	o.type = type;
+	o.id = DN_API_VERSION;
+
+	rv = setsockopt(s, IPPROTO_IP, IP_DUMMYNET3, &o, sizeof(o));
+	ATF_CHECK_MSG(rv == -1 && errno == EINVAL,
+	    "type %u: expected EINVAL, got rv=%d errno=%d", type, rv, errno);
+
+	close(s);
+}
+
+static void
+require_dummynet(atf_tc_t *tc)
+{
+	atf_tc_set_md_var(tc, "require.user", "root");
+	atf_tc_set_md_var(tc, "require.kmods", "dummynet");
+}
+
+ATF_TC(dn_link_short);
+ATF_TC_HEAD(dn_link_short, tc)
+{
+	require_dummynet(tc);
+}
+ATF_TC_BODY(dn_link_short, tc)
+{
+	check_short_object(DN_LINK);
+}
+
+ATF_TC(dn_profile_short);
+ATF_TC_HEAD(dn_profile_short, tc)
+{
+	require_dummynet(tc);
+}
+ATF_TC_BODY(dn_profile_short, tc)
+{
+	check_short_object(DN_PROFILE);
+}
+
+ATF_TC(dn_sch_short);
+ATF_TC_HEAD(dn_sch_short, tc)
+{
+	require_dummynet(tc);
+}
+ATF_TC_BODY(dn_sch_short, tc)
+{
+	check_short_object(DN_SCH);
+}
+
+ATF_TC(dn_fs_short);
+ATF_TC_HEAD(dn_fs_short, tc)
+{
+	require_dummynet(tc);
+}
+ATF_TC_BODY(dn_fs_short, tc)
+{
+	check_short_object(DN_FS);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, dn_link_short);
+	ATF_TP_ADD_TC(tp, dn_profile_short);
+	ATF_TP_ADD_TC(tp, dn_sch_short);
+	ATF_TP_ADD_TC(tp, dn_fs_short);
+
+	return (atf_no_error());
+}