* [PATCH net 0/1] ipv6: mcast: fix default-interface source filter selection
@ 2026-11-12 15:19 Zihan Xi
2026-11-12 15:19 ` [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters Zihan Xi
0 siblings, 1 reply; 4+ messages in thread
From: Zihan Xi @ 2026-11-12 15:19 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.
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
to a device but previously searched socket memberships with a wildcard
interface index. With the same group joined on multiple interfaces, the
socket filter could be updated on one interface while the aggregate
filter was updated on the route-selected interface. The patch uses the
resolved interface index for both membership lookup and last-source
membership removal.
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 mini_poc mini_poc.c
ip -6 route get ff0e::1234
./mini_poc
The test used a 2 vCPU, 2 GiB x86 QEMU/KVM environment. On the baseline,
the source appeared in socket[lo] while socket[eth0] stayed empty. With
the patch, it appeared in socket[eth0] while socket[lo] stayed empty.
No kernel crash was observed.
------BEGIN PoC------
#define _GNU_SOURCE
#include <arpa/inet.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>
#ifndef GROUP_FILTER_SIZE
#define GROUP_FILTER_SIZE(n) \
(sizeof(struct group_filter) - sizeof(struct sockaddr_storage) + \
(n) * sizeof(struct sockaddr_storage))
#endif
static void set_group(struct sockaddr_storage *ss, const char *addr)
{
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;
sin6->sin6_family = AF_INET6;
if (inet_pton(AF_INET6, addr, &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 = calloc(1, GROUP_FILTER_SIZE(1));
struct sockaddr_in6 *sin6;
socklen_t len = GROUP_FILTER_SIZE(1);
if (!gf) {
perror("calloc");
exit(EXIT_FAILURE);
}
gf->gf_interface = ifindex;
gf->gf_numsrc = 1;
set_group(&gf->gf_group, "ff0e::1234");
if (getsockopt(fd, SOL_IPV6, MCAST_MSFILTER, gf, &len)) {
perror("getsockopt(MCAST_MSFILTER)");
exit(EXIT_FAILURE);
}
printf("%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);
}
int main(void)
{
const char *group = "ff0e::1234";
const char *source = "2001:db8::1";
unsigned int route_if = if_nametoindex("eth0");
unsigned int shadow_if = if_nametoindex("lo");
struct ipv6_mreq mreq = { 0 };
struct group_source_req gsr = { 0 };
int fd;
if (!route_if || !shadow_if) {
fprintf(stderr, "eth0 or lo is unavailable\n");
return EXIT_FAILURE;
}
fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return EXIT_FAILURE;
}
if (inet_pton(AF_INET6, group, &mreq.ipv6mr_multiaddr) != 1) {
perror("inet_pton(group)");
return EXIT_FAILURE;
}
mreq.ipv6mr_interface = route_if;
if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
perror("IPV6_ADD_MEMBERSHIP(eth0)");
return EXIT_FAILURE;
}
mreq.ipv6mr_interface = shadow_if;
if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq))) {
perror("IPV6_ADD_MEMBERSHIP(lo)");
return EXIT_FAILURE;
}
gsr.gsr_interface = 0;
set_group(&gsr.gsr_group, group);
set_group(&gsr.gsr_source, source);
show_filter(fd, route_if, "eth0 before");
show_filter(fd, shadow_if, "lo before");
if (setsockopt(fd, SOL_IPV6, MCAST_BLOCK_SOURCE, &gsr, sizeof(gsr))) {
perror("setsockopt(MCAST_BLOCK_SOURCE)");
return EXIT_FAILURE;
}
show_filter(fd, route_if, "eth0 after");
show_filter(fd, shadow_if, "lo after");
close(fd);
return 0;
}
------END PoC--------
Best regards,
Zihan Xi
Zihan Xi (1):
ipv6: mcast: use resolved interface for source filters
net/ipv6/mcast.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
2026-11-12 15:19 [PATCH net 0/1] ipv6: mcast: fix default-interface source filter selection Zihan Xi
@ 2026-11-12 15:19 ` Zihan Xi
2026-11-19 8:39 ` Paolo Abeni
2026-11-19 8:43 ` Paolo Abeni
0 siblings, 2 replies; 4+ messages in thread
From: Zihan Xi @ 2026-11-12 15:19 UTC (permalink / raw)
To: netdev; +Cc: dsahern, idosch, davem, edumazet, pabeni, horms, vega, zihanx
When no interface is specified, ip6_mc_source() resolves the multicast
route to an interface but searches socket memberships with a wildcard
index. A socket joined to the same group on multiple interfaces can
therefore update its source filter on a different interface from the
interface filter.
Use the resolved interface index for the membership lookup and for
dropping the membership after leaving the last source.
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>
---
net/ipv6/mcast.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index ecef55f26189..63770b3a7bb2 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -362,6 +362,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 +376,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 +388,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 +499,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] 4+ messages in thread
* Re: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
2026-11-12 15:19 ` [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters Zihan Xi
@ 2026-11-19 8:39 ` Paolo Abeni
2026-11-19 8:43 ` Paolo Abeni
1 sibling, 0 replies; 4+ messages in thread
From: Paolo Abeni @ 2026-11-19 8:39 UTC (permalink / raw)
To: zihanx; +Cc: 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.
Full review at: https://netdev-ai.bots.linux.dev/sashiko/
---
--- Patch [1]: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters ---
commit 65be18fe1d53e3d386a91fc23d1c7ba9cc2e7b28
Author: Zihan Xi <zihanx@nebusec.ai>
ipv6: mcast: use resolved interface for source filters
This patch makes ip6_mc_source() match socket memberships against the
ifindex of the device resolved by ip6_mc_find_idev(). Before, it treated
gsr_interface == 0 as a wildcard. The resolved ifindex is also passed to
ipv6_sock_mc_drop() when the last source is removed.
> diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
> index ecef55f261890..63770b3a7bb25 100644
> --- a/net/ipv6/mcast.c
> +++ b/net/ipv6/mcast.c
[ ... ]
> @@ -375,6 +376,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 +388,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;
[Severity: Medium]
[Finding: main-1-0]
[Sources: opus-5-5]
Does this break MCAST_JOIN_SOURCE_GROUP with gsr_interface == 0 when the
socket already has a membership for the group on another interface?
__ipv6_sock_mc_join() still treats ifindex 0 as "any interface". Its
duplicate check also runs before ip6_mc_find_dev() resolves the device:
net/ipv6/mcast.c:__ipv6_sock_mc_join() {
...
for_each_pmc_socklock(np, sk, mc_lst) {
if ((ifindex == 0 || mc_lst->ifindex == ifindex) &&
ipv6_addr_equal(&mc_lst->addr, addr))
return -EADDRINUSE;
}
...
dev = ip6_mc_find_dev(net, addr, ifindex);
...
}
The caller accepts -EADDRINUSE and goes on to call ip6_mc_source():
net/ipv6/ipv6_sockglue.c:do_ipv6_mcast_group_source() {
...
retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
&psin6->sin6_addr,
MCAST_INCLUDE);
/* prior join w/ different source is ok */
if (retv && retv != -EADDRINUSE)
return retv;
...
}
Suppose the socket joined group G on interface Y, and the route for G
resolves to interface X:
do_ipv6_mcast_group_source()
ipv6_sock_mc_join_ssm(sk, 0, G, MCAST_INCLUDE)
__ipv6_sock_mc_join()
returns -EADDRINUSE (matches Y, nothing created on X)
ip6_mc_source()
ip6_mc_find_idev() resolves to X
no pmc with ifindex == X
returns -EINVAL ("must have a prior join")
Before this patch the call returned 0, but it changed the socket filter
on Y while updating the interface filter on X. After this patch it fails.
In this state, a source-specific join with interface 0 can never join on
the resolved interface.
IPv4 does not have this gap. __ip_mc_join_group() calls ip_mc_find_dev()
first, which writes the resolved ifindex into imr. The duplicate check
then uses that ifindex.
MCAST_BLOCK_SOURCE, MCAST_UNBLOCK_SOURCE and MCAST_LEAVE_SOURCE_GROUP
with interface 0 also behave differently when the only membership is on
an interface the route does not resolve to, for example after a route
change.
One case that used to work now fails with -EINVAL: removing the last
INCLUDE source with MCAST_LEAVE_SOURCE_GROUP. Before this patch, that
path reached ipv6_sock_mc_drop(), which cleaned up using the pmc's own
ifindex.
Should __ipv6_sock_mc_join() resolve the device before its -EADDRINUSE
check, as IPv4 does? This is tagged for stable, so could the commit
message also say that these setsockopt() calls can now return -EINVAL?
--
This is an AI-generated review.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
2026-11-12 15:19 ` [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters Zihan Xi
2026-11-19 8:39 ` Paolo Abeni
@ 2026-11-19 8:43 ` Paolo Abeni
1 sibling, 0 replies; 4+ messages in thread
From: Paolo Abeni @ 2026-11-19 8:43 UTC (permalink / raw)
To: Zihan Xi, netdev; +Cc: dsahern, idosch, davem, edumazet, horms, vega
On 11/12/26 16:19, Zihan Xi wrote:
> When no interface is specified, ip6_mc_source() resolves the multicast
> route to an interface but searches socket memberships with a wildcard
> index. A socket joined to the same group on multiple interfaces can
> therefore update its source filter on a different interface from the
> interface filter.
>
> Use the resolved interface index for the membership lookup and for
> dropping the membership after leaving the last source.
>
> 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>
This is changing an established behavior. While the old one is likely
"uncorrect", existing application may depend on it.
This is at best net-next material (with no fixes nor stable tag), but
lacking an end-user report, but I tent to think we are better off
without this change entirely.
/P
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-19 8:43 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-12 15:19 [PATCH net 0/1] ipv6: mcast: fix default-interface source filter selection Zihan Xi
2026-11-12 15:19 ` [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters Zihan Xi
2026-11-19 8:39 ` Paolo Abeni
2026-11-19 8:43 ` Paolo Abeni
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox