Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2 0/1] ipv6: mcast: unify default interface resolution
@ 2026-11-15 11:17 Zihan Xi
  2026-11-15 11:17 ` [PATCH net v2 1/1] ipv6: mcast: pass resolved interface to source update Zihan Xi
  0 siblings, 1 reply; 3+ messages in thread
From: Zihan Xi @ 2026-11-15 11:17 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, idosch, davem, edumazet, pabeni, horms, vega, zihanx

Hi Linux kernel maintainers,

We found an issue in net/ipv6/mcast.c. The bug is reachable by an
unprivileged process using the IPv6 multicast source-filter socket API.
The relevant details are provided below.

---- details below ----

An LLM was used to help create this patch.

Changes in v2:
- Resolve the default interface before duplicate membership checks. This
  also scopes ordinary ASM IPV6_ADD_MEMBERSHIP and MCAST_JOIN_GROUP
  duplicate checks to the route-selected interface, allowing the same
  group to be joined on separate links.
- Return the resolved ifindex from the SSM join helper and pass it to
  ip6_mc_source(), avoiding a second route lookup in the same operation.
  This follows the IPv4 group-source path's resolved-ifindex handoff.
- Clarify that source-filter operations target the resolved interface;
  callers can specify an ifindex to operate on a different membership.

Link: https://lore.kernel.org/all/cover.1794492711.git.zihanx@nebusec.ai/

This bug is tracked at: https://bugtracker.nebusec.ai/f/Z8p (Z8p)

Bug details:

When gsr_interface is zero, ip6_mc_source() resolves the multicast route
but previously treated zero as a wildcard when finding the socket
membership. With the same group joined on multiple interfaces, the
socket filter could be updated on one interface while the aggregate
filter was updated on another. The v1 review also found that the SSM
join helper performed its wildcard duplicate check before resolving the
default interface, causing MCAST_JOIN_SOURCE_GROUP to return EINVAL when
only another interface had the group joined. Resolving the interface in
both join and source-filter stages separately also allowed a concurrent
route change to make those stages choose different devices.

This revision checks and stores memberships against the concrete
resolved interface, then carries that same ifindex from the SSM join
helper into the source-filter update. Thus both stages use one route
selection. Source-filter operations with interface zero are scoped to
that resolved interface; if no membership exists there, they may return
EINVAL. Specify an interface index to target a membership on another
interface.

Reproducer:

Build and run as an unprivileged user in a Linux guest where ff0e::1234
routes through eth0 and lo is available:

    gcc -O2 -Wall -Wextra -o ssm-route-interface-test ssm-route-interface-test.c
    ip -6 route get ff0e::1234
    ./ssm-route-interface-test

The test first joins ff0e::1234 on lo, then performs a source-specific
join with gsr_interface zero. On v1 this returned EINVAL and created no
membership on the route-selected interface. With v2 it succeeded; the
source appeared in that interface's INCLUDE filter, and source leave
removed only that membership. It also verifies default ASM joins,
duplicate EADDRINUSE results, default BLOCK/UNBLOCK and explicit
interface filtering. It was run as uid 1001 (test_user) in a 2 vCPU,
2 GiB x86 QEMU/KVM guest.

For a route-switch case, create/enable dummy0 and prefer its local
multicast route over eth0. As root in the guest:

    ip link show dummy0 || ip link add dummy0 type dummy
    ip link set dummy0 up
    ip -6 route replace ff00::/8 dev dummy0 metric 1 table local
    ip -6 route get ff0e::1234

Then run the test as an unprivileged user:

    su -s /bin/sh test_user -c './ssm-route-interface-test route-switched-dummy'

The test begins with the group joined on eth0 after the route has moved
to dummy0. The SSM join, source leave, ASM join, and source filters
follow dummy0; explicit eth0 filtering remains on eth0. No kernel crash
was observed.

------BEGIN PoC------

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

static void set_addr(struct sockaddr_storage *ss, const char *address)
{
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;

	sin6->sin6_family = AF_INET6;
	if (inet_pton(AF_INET6, address, &sin6->sin6_addr) != 1) {
		perror("inet_pton");
		exit(EXIT_FAILURE);
	}
}

static void show_filter(int fd, unsigned int ifindex, const char *ifname)
{
	struct group_filter *gf;
	socklen_t len = sizeof(*gf) + sizeof(struct sockaddr_storage);
	struct sockaddr_in6 *sin6;

	/* Allow the kernel to return one source address. */
	gf = calloc(1, len);
	if (!gf) {
		perror("calloc");
		exit(EXIT_FAILURE);
	}
	gf->gf_interface = ifindex;
	gf->gf_numsrc = 1;
	set_addr(&gf->gf_group, "ff0e::1234");
	if (getsockopt(fd, SOL_IPV6, MCAST_MSFILTER, gf, &len) < 0) {
		printf("filter[%s]: %s\n", ifname, strerror(errno));
		free(gf);
		return;
	}
	printf("filter[%s]: mode=%s sources=%u", ifname,
	       gf->gf_fmode == MCAST_INCLUDE ? "INCLUDE" : "EXCLUDE",
	       gf->gf_numsrc);
	if (gf->gf_numsrc) {
		char source[INET6_ADDRSTRLEN];

		sin6 = (struct sockaddr_in6 *)&gf->gf_slist[0];
		inet_ntop(AF_INET6, &sin6->sin6_addr, source, sizeof(source));
		printf(" first-source=%s", source);
	}
	putchar('\n');
	free(gf);
}

static int check_filter(int fd, unsigned int ifindex, const char *ifname,
			int present, uint32_t mode, unsigned int count,
			const char *expected_source)
{
	struct group_filter *gf;
	struct sockaddr_in6 *sin6;
	socklen_t len = sizeof(*gf) + sizeof(struct sockaddr_storage);
	struct in6_addr expected;
	int ret = -1;

	gf = calloc(1, len);
	if (!gf) {
		perror("calloc");
		return -1;
	}
	gf->gf_interface = ifindex;
	gf->gf_numsrc = 1;
	set_addr(&gf->gf_group, "ff0e::1234");
	if (getsockopt(fd, SOL_IPV6, MCAST_MSFILTER, gf, &len) < 0) {
		if (!present && errno == EADDRNOTAVAIL) {
			printf("check[%s]: no membership as expected\n", ifname);
			ret = 0;
		} else {
			fprintf(stderr, "check[%s]: getsockopt failed: %s\n",
				ifname, strerror(errno));
		}
		goto out;
	}
	if (!present) {
		fprintf(stderr, "check[%s]: unexpected membership\n", ifname);
		goto out;
	}
	if (gf->gf_fmode != mode || gf->gf_numsrc != count) {
		fprintf(stderr,
			"check[%s]: expected mode=%u sources=%u, got mode=%u sources=%u\n",
			ifname, mode, count, gf->gf_fmode, gf->gf_numsrc);
		goto out;
	}
	if (expected_source) {
		if (inet_pton(AF_INET6, expected_source, &expected) != 1 || !count) {
			fprintf(stderr, "check[%s]: invalid/missing expected source %s\n",
				ifname, expected_source);
			goto out;
		}
		sin6 = (struct sockaddr_in6 *)&gf->gf_slist[0];
		if (memcmp(&sin6->sin6_addr, &expected, sizeof(expected))) {
			fprintf(stderr, "check[%s]: expected source %s\n",
				ifname, expected_source);
			goto out;
		}
	}
	printf("check[%s]: mode=%s sources=%u as expected\n", ifname,
	       mode == MCAST_INCLUDE ? "INCLUDE" : "EXCLUDE", count);
	ret = 0;
out:
	free(gf);
	return ret;
}

int main(int argc, char **argv)
{
	struct in6_addr group;
	struct ipv6_mreq mreq = { 0 };
	struct group_req greq = { 0 };
	struct group_source_req gsr = { 0 };
	const char *route_name = "eth0";
	const char *old_name = "lo";
	unsigned int route_if, old_if;
	int fd, ret;

	if (argc > 2) {
		fprintf(stderr, "usage: %s [route-switched|route-switched-dummy]\n",
			argv[0]);
		return EXIT_FAILURE;
	}
	if (argc == 2 && !strcmp(argv[1], "route-switched")) {
		route_name = "lo";
		old_name = "eth0";
	} else if (argc == 2 && !strcmp(argv[1], "route-switched-dummy")) {
		route_name = "dummy0";
		old_name = "eth0";
	}
	route_if = if_nametoindex(route_name);
	old_if = if_nametoindex(old_name);
	if (!route_if || !old_if) {
		fprintf(stderr, "%s or %s is unavailable\n", route_name, old_name);
		return EXIT_FAILURE;
	}
	if (inet_pton(AF_INET6, "ff0e::1234", &group) != 1) {
		perror("inet_pton");
		return EXIT_FAILURE;
	}
	fd = socket(AF_INET6, SOCK_DGRAM, 0);
	if (fd < 0) {
		perror("socket");
		return EXIT_FAILURE;
	}
	/* Keep the prior membership off the interface selected by the route. */
	mreq.ipv6mr_multiaddr = group;
	mreq.ipv6mr_interface = old_if;
	if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
		perror("IPV6_ADD_MEMBERSHIP(prior interface)");
		return EXIT_FAILURE;
	}
	gsr.gsr_interface = 0;
	set_addr(&gsr.gsr_group, "ff0e::1234");
	set_addr(&gsr.gsr_source, "2001:db8::2");

	printf("route interface: %s(%u); prior membership: %s(%u)\n",
	       route_name, route_if, old_name, old_if);
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, MCAST_JOIN_SOURCE_GROUP,
			 &gsr, sizeof(gsr));
	printf("MCAST_JOIN_SOURCE_GROUP(ifindex=0): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (!ret && (check_filter(fd, route_if, route_name, 1, MCAST_INCLUDE,
				 1, "2001:db8::2") ||
		     check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)))
		ret = -1;

	if (ret == 0) {
		errno = 0;
		ret = setsockopt(fd, SOL_IPV6, MCAST_LEAVE_SOURCE_GROUP,
				 &gsr, sizeof(gsr));
		printf("MCAST_LEAVE_SOURCE_GROUP(ifindex=0): ret=%d errno=%d (%s)\n",
		       ret, errno, ret ? strerror(errno) : "success");
		show_filter(fd, route_if, route_name);
		show_filter(fd, old_if, old_name);
		if (!ret && (check_filter(fd, route_if, route_name, 0, 0, 0, NULL) ||
			     check_filter(fd, old_if, old_name, 1,
					  MCAST_EXCLUDE, 0, NULL)))
			ret = -1;
	}
	if (ret)
		goto out;

	/* Generic ASM join can add the route interface beside the old one. */
	greq.gr_interface = 0;
	set_addr(&greq.gr_group, "ff0e::1234");
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, MCAST_JOIN_GROUP, &greq, sizeof(greq));
	printf("MCAST_JOIN_GROUP(ifindex=0): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* Remove that membership explicitly, then test IPV6_ADD_MEMBERSHIP. */
	greq.gr_interface = route_if;
	ret = setsockopt(fd, SOL_IPV6, MCAST_LEAVE_GROUP, &greq, sizeof(greq));
	printf("MCAST_LEAVE_GROUP(explicit route): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	if (check_filter(fd, route_if, route_name, 0, 0, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* ASM joins with interface zero are scoped to the resolved route. */
	mreq.ipv6mr_interface = 0;
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP,
			 &mreq, sizeof(mreq));
	printf("IPV6_ADD_MEMBERSHIP(ifindex=0): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* A second default ASM join duplicates the route interface only. */
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP,
			 &mreq, sizeof(mreq));
	printf("IPV6_ADD_MEMBERSHIP(ifindex=0, duplicate): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret == 0 || errno != EADDRINUSE) {
		fprintf(stderr, "default ASM duplicate was not rejected\n");
		ret = -1;
		goto out;
	}
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* The generic MCAST_JOIN_GROUP API shares the same duplicate check. */
	greq.gr_interface = 0;
	set_addr(&greq.gr_group, "ff0e::1234");
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, MCAST_JOIN_GROUP, &greq, sizeof(greq));
	printf("MCAST_JOIN_GROUP(ifindex=0, duplicate): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret == 0 || errno != EADDRINUSE) {
		fprintf(stderr, "generic default ASM duplicate was not rejected\n");
		ret = -1;
		goto out;
	}

	/* Block and unblock a source on the route-selected interface. */
	gsr.gsr_interface = 0;
	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, MCAST_BLOCK_SOURCE, &gsr, sizeof(gsr));
	printf("MCAST_BLOCK_SOURCE(ifindex=0): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE,
			 1, "2001:db8::2") ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	errno = 0;
	ret = setsockopt(fd, SOL_IPV6, MCAST_UNBLOCK_SOURCE, &gsr, sizeof(gsr));
	printf("MCAST_UNBLOCK_SOURCE(ifindex=0): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* Explicit interface selection continues to target the old membership. */
	gsr.gsr_interface = old_if;
	ret = setsockopt(fd, SOL_IPV6, MCAST_BLOCK_SOURCE, &gsr, sizeof(gsr));
	printf("MCAST_BLOCK_SOURCE(explicit %s): ret=%d errno=%d (%s)\n",
	       old_name, ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE,
			 1, "2001:db8::2")) {
		ret = -1;
		goto out;
	}
	ret = setsockopt(fd, SOL_IPV6, MCAST_UNBLOCK_SOURCE, &gsr, sizeof(gsr));
	printf("MCAST_UNBLOCK_SOURCE(explicit %s): ret=%d errno=%d (%s)\n",
	       old_name, ret, errno, ret ? strerror(errno) : "success");
	if (ret)
		goto out;
	if (check_filter(fd, route_if, route_name, 1, MCAST_EXCLUDE, 0, NULL) ||
	    check_filter(fd, old_if, old_name, 1, MCAST_EXCLUDE, 0, NULL)) {
		ret = -1;
		goto out;
	}

	/* Remove only the ASM membership created on the route-selected device. */
	mreq.ipv6mr_interface = route_if;
	ret = setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP,
			 &mreq, sizeof(mreq));
	printf("IPV6_DROP_MEMBERSHIP(route interface): ret=%d errno=%d (%s)\n",
	       ret, errno, ret ? strerror(errno) : "success");
	show_filter(fd, route_if, route_name);
	show_filter(fd, old_if, old_name);
	if (!ret && (check_filter(fd, route_if, route_name, 0, 0, 0, NULL) ||
		     check_filter(fd, old_if, old_name, 1,
				  MCAST_EXCLUDE, 0, NULL)))
		ret = -1;

out:
	close(fd);
	return ret ? EXIT_FAILURE : EXIT_SUCCESS;
}
------END PoC--------

Best regards,
Zihan Xi


Zihan Xi (1):
  ipv6: mcast: pass resolved interface to source update

 include/net/ipv6.h       |  3 ++-
 net/ipv6/ipv6_sockglue.c |  5 ++++-
 net/ipv6/mcast.c         | 39 ++++++++++++++++++++++++---------------
 3 files changed, 30 insertions(+), 17 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH net v2 1/1] ipv6: mcast: pass resolved interface to source update
  2026-11-15 11:17 [PATCH net v2 0/1] ipv6: mcast: unify default interface resolution Zihan Xi
@ 2026-11-15 11:17 ` Zihan Xi
  2026-11-20 23:56   ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Zihan Xi @ 2026-11-15 11:17 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, idosch, davem, edumazet, pabeni, horms, vega, zihanx

When gsr_interface is zero, the SSM join helper resolves the default
interface before checking for an existing membership. ip6_mc_source()
then resolves the route again. A route change between these lookups can
leave a membership on the first device while the source update fails on
the second.

Return the resolved ifindex from ipv6_sock_mc_join_ssm() and pass it to
the source update. This follows the IPv4 group-source path, which writes
the resolved interface back to the request before updating the source
filter. Both membership lookup and source update now use one route
selection.

The resolved-interface duplicate check also applies to ordinary ASM
IPV6_ADD_MEMBERSHIP and MCAST_JOIN_GROUP calls with ifindex zero. Such
joins check duplicates on the route-selected interface, allowing the
same group to be joined on another interface.

Source-filter operations with no membership on the resolved interface
may return -EINVAL; specify its ifindex to operate on a membership
elsewhere.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Signed-off-by: Zihan Xi <zihanx@nebusec.ai>
---
 include/net/ipv6.h       |  3 ++-
 net/ipv6/ipv6_sockglue.c |  5 ++++-
 net/ipv6/mcast.c         | 39 ++++++++++++++++++++++++---------------
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/include/net/ipv6.h b/include/net/ipv6.h
index 3de07e738538..aa5f130eb9be 100644
--- a/include/net/ipv6.h
+++ b/include/net/ipv6.h
@@ -1230,7 +1230,8 @@ void ipv6_sysctl_unregister(void);
 int ipv6_sock_mc_join(struct sock *sk, int ifindex,
 		      const struct in6_addr *addr);
 int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
-			  const struct in6_addr *addr, unsigned int mode);
+			  const struct in6_addr *addr, unsigned int mode,
+			  int *resolved_ifindex);
 int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
 		      const struct in6_addr *addr);
 
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index 4b3536571c98..5253ddd6a248 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -166,15 +166,18 @@ static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
 		add = 0;
 	} else if (optname == MCAST_JOIN_SOURCE_GROUP) {
 		struct sockaddr_in6 *psin6;
+		int resolved_ifindex;
 		int retv;
 
 		psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
 		retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
 					     &psin6->sin6_addr,
-					     MCAST_INCLUDE);
+					     MCAST_INCLUDE,
+					     &resolved_ifindex);
 		/* prior join w/ different source is ok */
 		if (retv && retv != -EADDRINUSE)
 			return retv;
+		greqs.gsr_interface = resolved_ifindex;
 		omode = MCAST_INCLUDE;
 		add = 1;
 	} else /* MCAST_LEAVE_SOURCE_GROUP */ {
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index ecef55f26189..3f2a431ea0c8 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -196,7 +196,8 @@ static struct net_device *ip6_mc_find_dev(struct net *net,
  *	socket join on multicast group
  */
 static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
-			       const struct in6_addr *addr, unsigned int mode)
+			       const struct in6_addr *addr, unsigned int mode,
+			       int *resolved_ifindex)
 {
 	struct ipv6_pinfo *np = inet6_sk(sk);
 	struct ipv6_mc_socklist *mc_lst;
@@ -207,25 +208,30 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
 	if (!ipv6_addr_is_multicast(addr))
 		return -EINVAL;
 
+	dev = ip6_mc_find_dev(net, addr, ifindex);
+	if (!dev)
+		return -ENODEV;
+	ifindex = dev->ifindex;
+	if (resolved_ifindex)
+		*resolved_ifindex = ifindex;
+
 	for_each_pmc_socklock(np, sk, mc_lst) {
-		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
-		    ipv6_addr_equal(&mc_lst->addr, addr))
+		if (mc_lst->ifindex == ifindex &&
+		    ipv6_addr_equal(&mc_lst->addr, addr)) {
+			dev_put(dev);
 			return -EADDRINUSE;
+		}
 	}
 
 	mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
-	if (!mc_lst)
+	if (!mc_lst) {
+		dev_put(dev);
 		return -ENOMEM;
+	}
 
 	mc_lst->next = NULL;
 	mc_lst->addr = *addr;
 
-	dev = ip6_mc_find_dev(net, addr, ifindex);
-	if (!dev) {
-		sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
-		return -ENODEV;
-	}
-
 	mc_lst->ifindex = dev->ifindex;
 	mc_lst->sfmode = mode;
 	RCU_INIT_POINTER(mc_lst->sflist, NULL);
@@ -249,14 +255,15 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
 
 int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
 {
-	return __ipv6_sock_mc_join(sk, ifindex, addr, MCAST_EXCLUDE);
+	return __ipv6_sock_mc_join(sk, ifindex, addr, MCAST_EXCLUDE, NULL);
 }
 EXPORT_SYMBOL(ipv6_sock_mc_join);
 
 int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
-			  const struct in6_addr *addr, unsigned int mode)
+			  const struct in6_addr *addr, unsigned int mode,
+			  int *resolved_ifindex)
 {
-	return __ipv6_sock_mc_join(sk, ifindex, addr, mode);
+	return __ipv6_sock_mc_join(sk, ifindex, addr, mode, resolved_ifindex);
 }
 
 /*
@@ -362,6 +369,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
 	struct net *net = sock_net(sk);
 	struct ipv6_mc_socklist *pmc;
 	struct inet6_dev *idev;
+	int ifindex;
 	int leavegroup = 0;
 	int i, j;
 	int err;
@@ -375,6 +383,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
 	idev = ip6_mc_find_idev(net, group, pgsr->gsr_interface);
 	if (!idev)
 		return -ENODEV;
+	ifindex = idev->dev->ifindex;
 
 	mutex_lock(&idev->mc_lock);
 
@@ -386,7 +395,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
 	err = -EADDRNOTAVAIL;
 
 	for_each_pmc_socklock(inet6, sk, pmc) {
-		if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
+		if (pmc->ifindex != ifindex)
 			continue;
 		if (ipv6_addr_equal(&pmc->addr, group))
 			break;
@@ -497,7 +506,7 @@ int ip6_mc_source(int add, int omode, struct sock *sk,
 	mutex_unlock(&idev->mc_lock);
 	in6_dev_put(idev);
 	if (leavegroup)
-		err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
+		err = ipv6_sock_mc_drop(sk, ifindex, group);
 	return err;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2 1/1] ipv6: mcast: pass resolved interface to source update
  2026-11-15 11:17 ` [PATCH net v2 1/1] ipv6: mcast: pass resolved interface to source update Zihan Xi
@ 2026-11-20 23:56   ` Jakub Kicinski
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2026-11-20 23:56 UTC (permalink / raw)
  To: zihanx
  Cc: Jakub Kicinski, netdev, dsahern, idosch, davem, edumazet, pabeni,
	horms, vega

This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
---
ipv6: mcast: pass resolved interface to source update

This patch changes __ipv6_sock_mc_join() to resolve the device before the
duplicate membership check and to return the resolved ifindex to
do_ipv6_mcast_group_source(). ip6_mc_source() now matches memberships only
on the resolved interface and passes that ifindex to ipv6_sock_mc_drop().
The intent is for the membership lookup and the source update to share one
route selection, as in the IPv4 group-source path.

> When gsr_interface is zero, the SSM join helper resolves the default
> interface before checking for an existing membership. ip6_mc_source()
> then resolves the route again. A route change between these lookups can
> leave a membership on the first device while the source update fails on
> the second.

[Severity: Medium]
Is this an accurate description of the pre-patch behaviour? It looks like
no route change is needed, and the source update doesn't fail.

Before this patch, ip6_mc_source() took idev from the route when
gsr_interface was 0. Its membership loop, however, skipped the interface
check:

	for_each_pmc_socklock(inet6, sk, pmc) {
		if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface)
			continue;

The old __ipv6_sock_mc_join() also matched any interface for ifindex 0 and
returned -EADDRINUSE. do_ipv6_mcast_group_source() treats that as success.

Take a socket with a membership on device A, while the route for the group
points at B. A MCAST_BLOCK_SOURCE, MCAST_JOIN_SOURCE_GROUP or
MCAST_UNBLOCK_SOURCE call with gsr_interface 0 then updates the
sflist/sfmode of the A membership.

The ip6_mc_add_src()/ip6_mc_del_src() calls run on B's ifmcaddr6 instead.
This covers both the mode-switch pair and the per-source calls. The
syscall still returns 0.

If another socket has joined the group on B, B's per-source
sf_count[MCAST_EXCLUDE] goes up. ipv6_chk_mcast_addr() then drops that
source for every listener on B:

	rv = READ_ONCE(psf->sf_count[MCAST_INCLUDE]) ||
		READ_ONCE(psf->sf_count[MCAST_EXCLUDE]) !=
		READ_ONCE(mc->mca_sfcount[MCAST_EXCLUDE]);

The mode-switch path can also lower B's mca_sfcount[MCAST_EXCLUDE] and flip
B's filter mode. Nothing undoes this later, because ip6_mc_leave_src() runs
against A, not B.

The ip6_mc_source() change in this patch does fix this.

Could the commit message say that an unprivileged socket can reliably
change another device's source filter state, affecting other sockets?
Right now it reads like a timing-dependent failure. The difference matters
for stable and security triage.

> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index ecef55f261890..3f2a431ea0c81 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c

[ ... ]

> @@ -207,25 +208,30 @@ static int __ipv6_sock_mc_join(struct sock *sk, int ifindex,
>  	if (!ipv6_addr_is_multicast(addr))
>  		return -EINVAL;
>  
> +	dev = ip6_mc_find_dev(net, addr, ifindex);
> +	if (!dev)
> +		return -ENODEV;
> +	ifindex = dev->ifindex;
> +	if (resolved_ifindex)
> +		*resolved_ifindex = ifindex;
> +
>  	for_each_pmc_socklock(np, sk, mc_lst) {
> -		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
> -		    ipv6_addr_equal(&mc_lst->addr, addr))
> +		if (mc_lst->ifindex == ifindex &&
> +		    ipv6_addr_equal(&mc_lst->addr, addr)) {
> +			dev_put(dev);
>  			return -EADDRINUSE;
> +		}
>  	}

[Severity: Medium]
With this change, join treats ifindex 0 as the route-selected device.
ipv6_sock_mc_drop() is unchanged and still treats 0 as a wildcard:

net/ipv6/mcast.c:ipv6_sock_mc_drop() {
    ...
		if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
		    ipv6_addr_equal(&mc_lst->addr, addr)) {
    ...
}

Is this asymmetry intended? Joins with ifindex 0 can now create several
memberships for the same group. A drop with ifindex 0 removes whichever
matching entry comes first in the list (the most recent join), on any
interface.

For example, using IPV6_ADD_MEMBERSHIP/IPV6_DROP_MEMBERSHIP or
MCAST_JOIN_GROUP/MCAST_LEAVE_GROUP through ipv6_mcast_join_leave():

  join(G, 0)  -> membership on A
  route for G moves to B
  join(G, 0)  -> membership on B (previously -EADDRINUSE)
  route for G moves back to A
  drop(G, 0)  -> removes the B membership, not A

Some of this ambiguity already existed when an explicit-ifindex join was
mixed with an ifindex 0 join. Now ifindex 0 joins alone can produce it.

An application written for the old -EADDRINUSE behaviour sends one leave.
It then keeps receiving G on the other interface until the socket is
closed.

The IPv4 path mentioned in the commit message resolves ifindex 0 on leave
too. ip_mc_leave_group() calls ip_mc_find_dev(), which fills in
imr->imr_ifindex, and then matches that exact interface:

	ifindex = imr->imr_ifindex;
	...
		if (ifindex) {
			if (iml->multi.imr_ifindex != ifindex)
				continue;

The leavegroup path in ip6_mc_msfilter() also still passes the raw
gf_interface, which can be 0. ip6_mc_source() now passes the resolved
ifindex instead:

	err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group);

Should ipv6_sock_mc_drop() resolve ifindex 0 the same way? Then join,
source update and drop would all use one interface, as the commit message
describes.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 23:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 11:17 [PATCH net v2 0/1] ipv6: mcast: unify default interface resolution Zihan Xi
2026-11-15 11:17 ` [PATCH net v2 1/1] ipv6: mcast: pass resolved interface to source update Zihan Xi
2026-11-20 23:56   ` Jakub Kicinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox