Netdev List
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	David Ahern <dsahern@kernel.org>
Cc: Kuniyuki Iwashima <kuniyu@google.com>,
	Jiayuan Chen <jiayuan.chen@linux.dev>,
	Ido Schimmel <idosch@nvidia.com>,
	Willem de Bruijn <willemb@google.com>,
	Simon Horman <horms@kernel.org>,
	netdev@vger.kernel.org
Subject: [PATCH net] ipv6: anycast: delete the anycast route under idev->lock
Date: Tue, 24 Nov 2026 22:32:26 +0900	[thread overview]
Message-ID: <179552107133.1345245.9532670970901182620@gmail.com> (raw)

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.")
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
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(-)

diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
index be6dac8a8566..d66d0154ac3a 100644
--- a/net/ipv6/anycast.c
+++ b/net/ipv6/anycast.c
@@ -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);
-- 
2.43.0


                 reply	other threads:[~2026-11-24 13:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179552107133.1345245.9532670970901182620@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=jiayuan.chen@linux.dev \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox