All of lore.kernel.org
 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

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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.