Skip to contentSkip to conversation

ipv6: anycast: delete the anycast route under idev->lock

PATCH Applied Kyuwon Cho 4 participants 4 messages Nov 24 – Nov 30

Subsystems (3)
lore
Share & tools
lore

Kyuwon Cho

author
#0 lore raw

PATCH 1/1 ipv6: anycast: delete the anycast route under idev->lock

IPV6_LEAVE_ANYCAST no longer holds RTNL, so a join of the same address can run between the two halves of its last leave. __ipv6_dev_ac_dec() unlinks the aca from idev->ac_list under idev->lock but deletes its anycast route only after dropping the lock. A join in that window finds no aca and creates a new one, and its ip6_ins_rt() fails with -EEXIST because the old route is still in the table. __ipv6_dev_ac_inc() ignores that error, the leave then deletes the old route, and the address stays joined without an anycast route in the local table.

Delete the route before dropping idev->lock.

Fixes f7fdf13bf103 ("ipv6: anycast: Don't hold RTNL for IPV6_LEAVE_ANYCAST and IPV6_ADDRFORM.")

---
The issue was found by an LLM-guided test generator that we are developing as a research prototype. A reproducer, which runs as any
user: one thread drops the last membership of an anycast address on lo while another joins it again on a second socket, and the main thread then looks for the anycast route in /proc/net/ipv6_route.

#define _GNU_SOURCE
#include <arpa/inet.h>
#include <net/if.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>

struct in6_ifreq { struct in6_addr addr; unsigned int prefixlen; int ifindex; };

static struct ipv6_mreq mr = { .ipv6mr_interface = 1 }; /* lo */
static pthread_barrier_t bar;
static int sl, sj, joined;

/* one thread drops the last membership while the other joins again */ static void *race(void *join)

{
	for (;;) {
		pthread_barrier_wait(&bar);
		if (join)
			joined = !setsockopt(sj, IPPROTO_IPV6, IPV6_JOIN_ANYCAST, &mr, sizeof(mr));
		else
			setsockopt(sl, IPPROTO_IPV6, IPV6_LEAVE_ANYCAST, &mr, sizeof(mr));
		pthread_barrier_wait(&bar);
	}
	return NULL;
}

/* the anycast route of 2001:db8::100 in table local */ static int route(void)

{
	char line[256];
	int n = 0;
	FILE *f = fopen("/proc/net/ipv6_route", "r");

	while (fgets(line, sizeof(line), f))
		n += !strncmp(line, "20010db8000000000000000000000100 80", 35);
	fclose(f);
	return n;
}

int main(void)

{
	struct ifreq up = { .ifr_name = "lo", .ifr_flags = IFF_UP };
	struct in6_ifreq ifr = { .prefixlen = 64, .ifindex = 1 };
	int s, lost = 0;
	pthread_t t;

	/* CAP_NET_ADMIN for IPV6_JOIN_ANYCAST */
	if (unshare(CLONE_NEWUSER | CLONE_NEWNET))
		return perror("unshare"), 1;
	s = socket(AF_INET6, SOCK_DGRAM, 0);
	inet_pton(AF_INET6, "2001:db8::1", &ifr.addr);
	if (ioctl(s, SIOCSIFFLAGS, &up) || ioctl(s, SIOCSIFADDR, &ifr))
		return perror("lo"), 1;
	inet_pton(AF_INET6, "2001:db8::100", &mr.ipv6mr_multiaddr);

	pthread_barrier_init(&bar, NULL, 3);
	pthread_create(&t, NULL, race, NULL);
	pthread_create(&t, NULL, race, "join");
	for (int i = 0; i < 10000; i++) {
		sl = socket(AF_INET6, SOCK_DGRAM, 0);
		sj = socket(AF_INET6, SOCK_DGRAM, 0);
		setsockopt(sl, IPPROTO_IPV6, IPV6_JOIN_ANYCAST, &mr, sizeof(mr));
		pthread_barrier_wait(&bar);
		pthread_barrier_wait(&bar);
		lost += joined && !route();
		close(sj);
		close(sl);
	}
	printf("joined without an anycast route: %d of 10000\n", lost);
	return 0;
}

Without the patch, a few of the 10000 joins leave the address without its route; with it, none do, and lockdep stays quiet.

The patch was written with LLM assistance; we reviewed and tested it.

 net/ipv6/anycast.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/ipv6/anycast.c +1 -2
index be6dac8a8566..d66d0154ac3a 100644
@@ -415,12 +415,11 @@ int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
 		rcu_assign_pointer(prev_aca->aca_next, aca->aca_next);
 	else
 		rcu_assign_pointer(idev->ac_list, aca->aca_next);
+	ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
 	write_unlock_bh(&idev->lock);
 	ipv6_del_acaddr_hash(aca);
 	addrconf_leave_solict(idev, &aca->aca_addr);
 
-	ip6_del_rt(dev_net(idev->dev), aca->aca_rt, false);
-
 	inet6_ifacaddr_notify(idev->dev, aca, RTM_DELANYCAST);
 
 	aca_put(aca);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kyuwon Cho

IPV6_LEAVE_ANYCAST no longer holds RTNL, so a join of the same address can run between the two halves of its last leave. __ipv6_dev_ac_dec() unlinks the aca from idev->ac_list under idev->lock but deletes its anycast route only after dropping the lock. A join in that window finds no aca and creates a new one, and its ip6_ins_rt() fails with -EEXIST because the old route is still in the table. __ipv6_dev_ac_inc() ignores that error, the leave then deletes the old route, and the address stays joined without an anycast route in the local table.

Quoted 4 lines

Delete the route before dropping idev->lock.

Fixes f7fdf13bf103 ("ipv6: anycast: Don't hold RTNL for IPV6_LEAVE_ANYCAST and IPV6_ADDRFORM.")

Note that:

"Avoid having more than 15 patches, across all series, outstanding for review on the mailing list for a single tree. In other words, a maximum of 15 patches under review on net, and a maximum of 15 patches under review on net-next."

https://docs.kernel.org/next/process/maintainer-netdev.html#limit-patches-outstanding-on-mailing-list

And according to patchwork you already have 17 patches under review for net.

maintainer
#2 lore raw
Quoted 24 lines · Ido Schimmel
Quoted 9 lines · Kyuwon Cho

IPV6_LEAVE_ANYCAST no longer holds RTNL, so a join of the same address can run between the two halves of its last leave. __ipv6_dev_ac_dec() unlinks the aca from idev->ac_list under idev->lock but deletes its anycast route only after dropping the lock. A join in that window finds no aca and creates a new one, and its ip6_ins_rt() fails with -EEXIST because the old route is still in the table. __ipv6_dev_ac_inc() ignores that error, the leave then deletes the old route, and the address stays joined without an anycast route in the local table.

Quoted 4 lines

Delete the route before dropping idev->lock.

Fixes f7fdf13bf103 ("ipv6: anycast: Don't hold RTNL for IPV6_LEAVE_ANYCAST and IPV6_ADDRFORM.")

Note that:

"Avoid having more than 15 patches, across all series, outstanding for review on the mailing list for a single tree. In other words, a maximum of 15 patches under review on net, and a maximum of 15 patches under review on net-next."

Quoted 4 lines

https://docs.kernel.org/next/process/maintainer-netdev.html#limit-patches-outstanding-on-mailing-list

And according to patchwork you already have 17 patches under review for net.

To augment such statement, the maximum limit is intended for established contributors with a consistent record of reviewing effort spent on the ML.

@Kyuwon Cho: you should limit yourself to no more than an handful of patches in flight.

Thanks,

Paolo

#3 lore raw
In reply to Kyuwon Cho
This patch was applied to netdev/net.git (main) by Willem de Bruijn <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Willem de Bruijn <[email protected]>:

Quoted 10 lines · On Tue, 24 Nov 2026 22:32:26 +0900 you wrote:

IPV6_LEAVE_ANYCAST no longer holds RTNL, so a join of the same address can run between the two halves of its last leave. __ipv6_dev_ac_dec() unlinks the aca from idev->ac_list under idev->lock but deletes its anycast route only after dropping the lock. A join in that window finds no aca and creates a new one, and its ip6_ins_rt() fails with -EEXIST because the old route is still in the table. __ipv6_dev_ac_inc() ignores that error, the leave then deletes the old route, and the address stays joined without an anycast route in the local table.

[...]

Here is the summary with links:
- [net] ipv6: anycast: delete the anycast route under idev->lock

    https://git.kernel.org/netdev/net/c/b5275fd42e75

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html