ipv6: mcast: fix default-interface source filter selection
Subsystems (3)
cover ipv6: mcast: fix default-interface source filter selection
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_pocThe 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(-)
Signature · 1 line
2.43.0
PATCH 1/1 ipv6: mcast: use resolved interface for source filters
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.
--- net/ipv6/mcast.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/ipv6/mcast.c +4 -2
@@ -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; }
Signature · 1 line
2.43.0
Paolo Abeni
maintainerRe: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
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 <[email protected]>
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.
Quoted 4 lines
net/ipv6/mcast.c +0
-0
[ ... ]
Quoted 9 lines
net/ipv6/mcast.c +1
-0@@ -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);
net/ipv6/mcast.c int ip6_mc_source(int add, int omode, struct sock *sk, · line 394
Quoted 8 lines
net/ipv6/mcast.c +1
-1@@ -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?
Signature · 1 line
This is an AI-generated review.
Paolo Abeni
maintainerRe: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
In reply to Zihan XiQuoted 6 lines · Zihan Xi
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.
Quoted 7 lines
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")Reported-by Vega <[email protected]>Signed-off-by Zihan Xi <[email protected]>
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
Simon Horman
reviewerRe: [PATCH net 1/1] ipv6: mcast: use resolved interface for source filters
In reply to Zihan XiQuoted 10 lines · Zihan Xi
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")
Hi Zihan,
I see that Paolo has responded, and his comments should stand. But I had been looking into this patch and I think it's worth sharing two points, in case they turn out to be useful in future.
There is an AI-generated review of this patch available at
https://sashiko.dev/#/patchset/cover.1794738094.git.zihanx%40nebusec.ai
It states:
Furthermore, the duplicate membership check with ifindex == 0 in ipv6_sock_mc_join(), the -EADDRINUSE handling in MCAST_JOIN_SOURCE_GROUP, and the leavegroup handling in ip6_mc_source() were introduced later by c9e3e8b6958e ("[IPV6]: multicast join and misc"), while ipv6_sock_mc_join_ssm() was introduced by c7ea20c9da5b ("ipv6/mcast: init as INCLUDE when join SSM INCLUDE group"). Should the Fixes: tag point to those commits instead?
I would lean towards using c9e3e8b6958e as the Fixes tag as it seems to me that is the commit that that first introduced this bug.
Quoted 12 lines
--- net/ipv6/mcast.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)net/ipv6/mcast.c +0
-0
net/ipv6/mcast.c int ip6_mc_source(int add, int omode, struct sock *sk, · line 368
Quoted 7 lines
net/ipv6/mcast.c +1
-0@@ -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;
For reference, please preserve reverse xmas tree - longest line to shortest - ordering for local variable declarations in Networking code.