git: 0504bd31d6c3 - main - netinet6: check the length of the destination address

From: Maxim Konovalov <maxim_at_FreeBSD.org>
Date: Tue, 24 Nov 2026 22:13:16 UTC
The branch main has been updated by maxim:

URL: https://cgit.FreeBSD.org/src/commit/?id=0504bd31d6c33f47a6c9ec8c2044b6f6fba3f478

commit 0504bd31d6c33f47a6c9ec8c2044b6f6fba3f478
Author:     Maxim Konovalov <maxim@FreeBSD.org>
AuthorDate: 2026-11-19 17:56:16 +0000
Commit:     Maxim Konovalov <maxim@FreeBSD.org>
CommitDate: 2026-11-24 22:12:27 +0000

    netinet6: check the length of the destination address
    
    in6_validate_ifra() requires ifra_addr to be a struct sockaddr_in6,
    but it accepts an AF_INET6 ifra_dstaddr with any sin6_len, and
    in6_notify_ifa() stores that address in ia_dstaddr as given.
    
    Reject an AF_INET6 destination address whose sin6_len is not
    sizeof(struct sockaddr_in6) with EINVAL, as in_aifaddr() does for
    IPv4.  ifconfig(8), the netlink address handler and the kernel's own
    callers already pass the right length.
    
    Add tests/sys/netinet6/in6_control, whose cases run in a VNET jail:
    - aifaddr_dstaddr: SIOCAIFADDR_IN6 stores a valid destination
      address, and getifaddrs(3) reports it;
    - aifaddr_dstaddr_badlen: SIOCAIFADDR_IN6 rejects a destination
      address with sin6_len 255.
    aifaddr_dstaddr_badlen fails without the fix.
    
    Reviewed by:    glebius, markj
    MFC after:      2 weeks
    Co-Authored-By: Claude Opus 5.5
    Differential Revision:  https://reviews.freebsd.org/D61850
---
 sys/netinet6/in6.c               |   6 +-
 tests/sys/netinet6/Makefile      |   5 +-
 tests/sys/netinet6/in6_control.c | 201 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 210 insertions(+), 2 deletions(-)

diff --git a/sys/netinet6/in6.c b/sys/netinet6/in6.c
index cb0c25ec78d2..742bcaa2ecfc 100644
--- a/sys/netinet6/in6.c
+++ b/sys/netinet6/in6.c
@@ -852,12 +852,16 @@ in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
 		return (EAFNOSUPPORT);
 
 	/*
-	 * Validate address
+	 * Validate addresses
 	 */
 	if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
 	    ifra->ifra_addr.sin6_family != AF_INET6)
 		return (EINVAL);
 
+	if (ifra->ifra_dstaddr.sin6_family == AF_INET6 &&
+	    ifra->ifra_dstaddr.sin6_len != sizeof(struct sockaddr_in6))
+		return (EINVAL);
+
 	/*
 	 * validate ifra_prefixmask.  don't check sin6_family, netmask
 	 * does not carry fields other than sin6_len.
diff --git a/tests/sys/netinet6/Makefile b/tests/sys/netinet6/Makefile
index 88db13600f55..7c1036439d9f 100644
--- a/tests/sys/netinet6/Makefile
+++ b/tests/sys/netinet6/Makefile
@@ -4,7 +4,8 @@ TESTSDIR=	${TESTSBASE}/sys/netinet6
 BINDIR=		${TESTSDIR}
 FILESDIR=	${TESTSDIR}
 
-ATF_TESTS_C=		udp6
+ATF_TESTS_C=		in6_control \
+			udp6
 ATF_TESTS_PYTEST=	test_ip6_output.py
 ATF_TESTS_SH=		exthdr \
 			mld \
@@ -25,6 +26,8 @@ TEST_METADATA.exthdr+=	execenv="jail"		\
 			execenv_jail_params="vnet allow.raw_sockets"
 TEST_METADATA.forward6+= execenv="jail"		\
 			execenv_jail_params="vnet allow.raw_sockets"
+TEST_METADATA.in6_control+= execenv="jail"	\
+			execenv_jail_params="vnet"
 TEST_METADATA.ndp+=	execenv="jail"		\
 			execenv_jail_params="vnet allow.raw_sockets"
 TEST_METADATA.output6+=	execenv="jail"		\
diff --git a/tests/sys/netinet6/in6_control.c b/tests/sys/netinet6/in6_control.c
new file mode 100644
index 000000000000..4acbab3071c3
--- /dev/null
+++ b/tests/sys/netinet6/in6_control.c
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2026 Maxim Konovalov <maxim@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * Argument checks of the IPv6 address ioctls handled by in6_control().
+ */
+
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/sysctl.h>
+
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet6/in6_var.h>
+#include <netinet6/nd6.h>
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <ifaddrs.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+#define	IFNAME	"lo0"
+#define	ADDR	"2001:db8::1"
+#define	DSTADDR	"2001:db8::2"
+#define	MASK128	"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
+#define	BADLEN	255
+
+/*
+ * The test cases change lo0, so run them only in a VNET jail of their
+ * own, as the Makefile sets up, never in the host's network stack.
+ */
+static void
+require_vnet_jail(void)
+{
+	size_t len;
+	int vnet;
+
+	len = sizeof(vnet);
+	if (sysctlbyname("security.jail.vnet", &vnet, &len, NULL, 0) != 0 ||
+	    vnet != 1)
+		atf_tc_skip("not running in a VNET jail of its own");
+}
+
+static void
+sin6_init(struct sockaddr_in6 *sin6, const char *addr, int len)
+{
+	memset(sin6, 0, sizeof(*sin6));
+	sin6->sin6_family = AF_INET6;
+	sin6->sin6_len = len;
+	ATF_REQUIRE(inet_pton(AF_INET6, addr, &sin6->sin6_addr) == 1);
+}
+
+/*
+ * Fill in a request to add ADDR/128 to IFNAME with DSTADDR as the
+ * destination address, whose sin6_len is set to dstlen.
+ */
+static void
+aliasreq_init(struct in6_aliasreq *ifra, int dstlen)
+{
+	memset(ifra, 0, sizeof(*ifra));
+	strlcpy(ifra->ifra_name, IFNAME, sizeof(ifra->ifra_name));
+	sin6_init(&ifra->ifra_addr, ADDR, sizeof(struct sockaddr_in6));
+	sin6_init(&ifra->ifra_dstaddr, DSTADDR, dstlen);
+	sin6_init(&ifra->ifra_prefixmask, MASK128,
+	    sizeof(struct sockaddr_in6));
+	ifra->ifra_flags = IN6_IFF_NODAD;
+	ifra->ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
+	ifra->ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
+}
+
+static void
+delete_addr(int s)
+{
+	struct in6_ifreq ifr;
+
+	memset(&ifr, 0, sizeof(ifr));
+	strlcpy(ifr.ifr_name, IFNAME, sizeof(ifr.ifr_name));
+	sin6_init(&ifr.ifr_addr, ADDR, sizeof(struct sockaddr_in6));
+	ATF_REQUIRE_MSG(ioctl(s, SIOCDIFADDR_IN6, &ifr) == 0,
+	    "SIOCDIFADDR_IN6: %s", strerror(errno));
+}
+
+/*
+ * Return the getifaddrs(3) entry of the IPv6 address addr on IFNAME,
+ * or NULL if there is none.
+ */
+static struct ifaddrs *
+find_addr(struct ifaddrs *ifap, const struct in6_addr *addr)
+{
+	struct sockaddr_in6 sin6;
+	struct ifaddrs *ifa;
+
+	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
+		if (strcmp(ifa->ifa_name, IFNAME) != 0 ||
+		    ifa->ifa_addr == NULL ||
+		    ifa->ifa_addr->sa_family != AF_INET6 ||
+		    ifa->ifa_addr->sa_len != sizeof(sin6))
+			continue;
+		memcpy(&sin6, ifa->ifa_addr, sizeof(sin6));
+		if (IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr, addr))
+			return (ifa);
+	}
+	return (NULL);
+}
+
+ATF_TC(aifaddr_dstaddr);
+ATF_TC_HEAD(aifaddr_dstaddr, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "SIOCAIFADDR_IN6 stores a valid destination address");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(aifaddr_dstaddr, tc)
+{
+	struct in6_aliasreq ifra;
+	struct sockaddr_in6 dst;
+	struct ifaddrs *ifa, *ifap;
+	int s;
+
+	require_vnet_jail();
+	ATF_REQUIRE((s = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0);
+
+	aliasreq_init(&ifra, sizeof(struct sockaddr_in6));
+	ATF_REQUIRE_MSG(ioctl(s, SIOCAIFADDR_IN6, &ifra) == 0,
+	    "SIOCAIFADDR_IN6: %s", strerror(errno));
+
+	ATF_REQUIRE_MSG(getifaddrs(&ifap) == 0, "getifaddrs: %s",
+	    strerror(errno));
+	ifa = find_addr(ifap, &ifra.ifra_addr.sin6_addr);
+	ATF_REQUIRE_MSG(ifa != NULL, "%s not found on %s", ADDR, IFNAME);
+	ATF_REQUIRE_MSG(ifa->ifa_dstaddr != NULL, "no destination address");
+	ATF_REQUIRE_MSG(ifa->ifa_dstaddr->sa_family == AF_INET6,
+	    "destination sa_family %u", ifa->ifa_dstaddr->sa_family);
+	ATF_REQUIRE_MSG(ifa->ifa_dstaddr->sa_len == sizeof(dst),
+	    "destination sa_len %u", ifa->ifa_dstaddr->sa_len);
+	memcpy(&dst, ifa->ifa_dstaddr, sizeof(dst));
+	ATF_REQUIRE_MSG(IN6_ARE_ADDR_EQUAL(&dst.sin6_addr,
+	    &ifra.ifra_dstaddr.sin6_addr), "destination is not %s", DSTADDR);
+	freeifaddrs(ifap);
+
+	delete_addr(s);
+	close(s);
+}
+
+ATF_TC(aifaddr_dstaddr_badlen);
+ATF_TC_HEAD(aifaddr_dstaddr_badlen, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "SIOCAIFADDR_IN6 rejects a destination address with a bad length");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(aifaddr_dstaddr_badlen, tc)
+{
+	struct in6_aliasreq ifra;
+	struct ifaddrs *ifap;
+	int error, rv, s;
+
+	require_vnet_jail();
+	ATF_REQUIRE((s = socket(AF_INET6, SOCK_DGRAM, 0)) >= 0);
+
+	aliasreq_init(&ifra, BADLEN);
+	rv = ioctl(s, SIOCAIFADDR_IN6, &ifra);
+	if (rv == 0) {
+		/*
+		 * The address was stored.  getifaddrs(3) then fails in
+		 * this VNET until the address is deleted.
+		 */
+		error = 0;
+		if (getifaddrs(&ifap) == 0)
+			freeifaddrs(ifap);
+		else
+			error = errno;
+		delete_addr(s);
+		atf_tc_fail("SIOCAIFADDR_IN6 accepted sin6_len %d; "
+		    "getifaddrs: %s", BADLEN,
+		    error == 0 ? "ok" : strerror(error));
+	}
+	ATF_REQUIRE_ERRNO(EINVAL, rv == -1);
+
+	ATF_REQUIRE_MSG(getifaddrs(&ifap) == 0, "getifaddrs: %s",
+	    strerror(errno));
+	freeifaddrs(ifap);
+	close(s);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, aifaddr_dstaddr);
+	ATF_TP_ADD_TC(tp, aifaddr_dstaddr_badlen);
+
+	return (atf_no_error());
+}