From: Chris Bannister <kernel@cbannister.uk>
To: Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
"David S. Miller" <davem@davemloft.net>,
netdev@vger.kernel.org
Cc: regressions@lists.linux.dev, linux-kernel@vger.kernel.org,
Simon Horman <horms@kernel.org>,
Kuniyuki Iwashima <kuniyu@google.com>
Subject: [REGRESSION] rtnetlink: a full RTM_GETLINK racing a netns move can return the device's data from its new netns since v7.2
Date: Sun, 29 Nov 2026 21:57:51 +0000 [thread overview]
Message-ID: <20261129215752.251291-1-kernel@cbannister.uk> (raw)
Hi,
A review of the series merged as aa064a614efc ("rtnetlink: RTNL
avoidance in rtnl_getlink() and rtnl_dump_ifinfo()") found a second
regression from the same commit as the reply-ordering report [1]. A
full RTM_GETLINK that races a netns move of the device can be answered
with the device's data from its new netns.
It bisects to e896e5c0734b ("rtnetlink: do not acquire RTNL in
rtnl_getlink() with RTEXT_FILTER_NAME_ONLY") [2]: clean at its parent
(00888feb6014), reproduces at that commit. v7.3.6 carries the change,
and it is unfixed in mainline as of v7.4-rc4.
#regzbot introduced: e896e5c0734b
The reproducers, kernel configs, run records and the fix patches are in
a bundle [3], and paths below are relative to it.
[1] https://lore.kernel.org/all/20261129214529.4059732-1-kernel@cbannister.uk/
[2] https://lore.kernel.org/all/20260525083542.1565964-4-edumazet@google.com/
[3] https://github.com/zariel/linux-rtnetlink-getlink-regressions
The sections below were drafted by AI tools from the investigation
records. I have reviewed them, and the numbers are from the run
records in the bundle.
Cause
-----
A full RTM_GETLINK is one without RTEXT_FILTER_NAME_ONLY. Since
e896e5c0734b, rtnl_getlink() looks the device up in the target netns
under RCU, and only then takes RTNL to fill the reply. Once it holds
RTNL it checks only netif_is_alive().
A netns move runs under RTNL and leaves reg_state at
NETREG_REGISTERED, so that check passes, and nothing rechecks
dev_net(dev) == tgt_net. A request that found the device and then
waited on RTNL while the device moved fills its reply from the device
in its new netns. The reply carries, for example, the ifindex the
device has there.
Before e896e5c0734b the lookup and the fill shared one RTNL hold, so
the answer was either the device in the target netns or -ENODEV.
Evidence
--------
The evidence comes from vanilla kernel.org builds with taint 0. Every
row below, with its host configuration and run IDs, is in
<bundle>/reports/results.md. The test environment is the one described
in the ordering report.
The Go stress reproducer, <bundle>/nlrace/go-netns/ (-mode netns),
floods full RTM_GETLINK by name in one netns while a dummy device moves
between two, with a different ifindex in each. It counts replies that
carry the device's ifindex in the other netns.
Such replies out of iterations. Pass 1 used a 12 vCPU host with
mitigations on, and pass 2 a 20 vCPU host with mitigations off. Both
ran five concurrent guests per kernel, with no build running:
parent 00888feb6014 (pass 1): 0 / 5,000
e896e5c0734b (pass 1): 6 / 5,000
v7.3.6 (pass 2): 6 / 5,000
v7.4-rc4 (pass 2): 3 / 5,000
v6.18.65 LTS (pass 2): 0 / 5,000
In one guest alone, with the same host, config and binaries:
v7.4-rc4: 47 / 20,000
v7.4-rc4 plus the fix: 0 / 20,000
A v7.4-rc4 kernel with the fix, lockdep and KASAN ran the same
reproducers with no lock-order, KASAN, RCU or might-sleep report, and
taint 0.
Reproducer
----------
nlnetns.c is a single C file that shows the same failure. It is
appended at the end of this mail and is also at
<bundle>/nlrace/c/nlnetns.c:
cc -O2 -Wall -Wextra -pthread -o nlnetns nlnetns.c
unshare -Urn ./nlnetns -t 30 -f 4
It exits 1 on a violation, 0 on none and 125 on a setup error. A
violation is a reply that carries the ifindex from the other netns.
Violations in five such runs per vanilla kernel (taint 0, 20 vCPU
host, mitigations off; the first four kernels as concurrent guests, the
fix in a guest alone):
00888feb6014: 0 in every run
e896e5c0734b: 5,773 to 6,845 per run
v7.3.6: 6,174 to 6,833 per run
v7.4-rc4: 6,014 to 6,851 per run
v7.4-rc4 plus the fix: 0 in every run
As supporting evidence, a bare-metal distribution 7.3.5 kernel gave
1,844 in one run.
Selftest
--------
A deterministic selftest is patch 3 of the fix series, under
<bundle>/kernels/patches/upstream/series/. On vanilla kernels with
taint 0 it fails in 20 of 20 runs on e896e5c0734b and on v7.4-rc4, and
passes in 20 of 20 on 00888feb6014 and on v7.4-rc4 plus the fix, with
no skips.
Architecture and compiler: x86_64, gcc 16.2.0 (from the "Linux
version" lines under <bundle>/evidence/). Kernel configs are under
<bundle>/kernels/configs/.
Questions
---------
A fix series follows these two reports. It has three patches: a fix for
this bug, a fix for the ordering bug, and the selftest. The patches are
also in the bundle.
- Is the extra RTNL acquisition for full requests that name a missing
device acceptable, or is an RCU pre-check preferred?
- Should the name-only fill use a single RCU section, with
dev->ifindex written with WRITE_ONCE() in
__dev_change_net_namespace()?
- The selftest holds RTNL by making the copy_to_user() in SIOCGIFCONF
fault on a userfaultfd page. It is deterministic, but it rests on
dev_ifconf() copying out under RTNL, and I cannot judge whether a
test that depends on that detail is acceptable. Is it, or is there a
better way to hold RTNL from user space?
I am happy to test other approaches.
Disclosure: this investigation was carried out by AI agents under my
direction. Claude Opus 5.5 coordinated it, designed the experiments,
minimised the reproducer and wrote its review fixes and reader
comments; Claude Fable 5.2 found this bug in its review of the series,
designed the offered fixes and tests, wrote nlnetns.c and reviewed the
package; Claude Sonnet 5.5 ran the test host, built the kernels, wrote
the first nlrace.c and drafted this mail; OpenAI gpt-6.2-sol and
gpt-6.2-astra wrote the first harness and Go reproducer, analysed the
source and reviewed the fixes. The reproducers and the offered fix were
run on the kernels listed above.
My own part: I read rtnl_getlink() and __dev_change_net_namespace(),
reviewed nlnetns.c and reproduced this bug with it on my development
machine. I reviewed the fix patches, and read the selftest without
running it. I searched lore for related bugs, reports and patches, and
I reviewed this mail before sending it.
Per-agent detail: <bundle>/reports/provenance.md.
Thanks,
Chris Bannister
---- 8< ---- nlnetns.c ---- 8< ----
// SPDX-License-Identifier: GPL-2.0
/*
* nlnetns: can a full RTM_GETLINK for an ifindex in one network namespace be
* answered with the device as it is in another namespace?
*
* Since e896e5c0734b ("rtnetlink: do not acquire RTNL in rtnl_getlink() with
* RTEXT_FILTER_NAME_ONLY", v7.2), rtnl_getlink() looks the device up in the
* target netns under RCU, and only then takes RTNL to fill a full reply. Once
* it holds RTNL it checks only that the device is still registered. A move to
* another netns (__dev_change_net_namespace()) runs under RTNL and leaves
* reg_state at NETREG_REGISTERED, so a request that found the device and then
* slept on RTNL for the length of a move fills its reply from the device in
* its new netns. Before that commit the lookup and the fill shared one RTNL
* hold.
*
* The program runs in netns A and creates a second netns B. Threads:
* - main, the mover: moves the dummy link nlnetns0 from A to B and back, in
* a loop. Each move passes IFLA_NEW_IFINDEX, so the device has ifindex X
* whenever it is in A and X + 1000 whenever it is in B.
* - flooders (-f): each has its own socket in A, sends a full RTM_GETLINK
* for ifindex X, and reads the answer before it sends the next request.
*
* Violation: an RTM_NEWLINK reply whose ifi_index is not X. Every request
* asks netns A for ifindex X, and the device carries another ifindex only
* while it is in B, so such a reply was filled from the device in B.
*
* Why a correct kernel cannot produce this: listing a device in a netns,
* unlisting it and changing its ifindex all happen under RTNL. When the
* lookup and the fill share one RTNL hold, a device found in A under
* ifindex X is still in A with ifindex X when the reply is filled, and a
* request that arrives while the device is in B finds nothing and gets
* -ENODEV. The private namespaces mean nothing else creates or moves links.
* -ENODEV answers are expected on every kernel and are only counted.
*
* Build: cc -O2 -Wall -Wextra -pthread -o nlnetns nlnetns.c
* Run: unshare -Urn ./nlnetns [-t SECONDS] [-f FLOODERS]
* Exit: 0 no violations, 1 violations, 125 the run gave no verdict (usage,
* setup or netlink failure, or an answer that did not arrive in
* time), so "git bisect run" skips that commit.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <linux/if_link.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <pthread.h>
#include <sched.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <time.h>
#include <unistd.h>
#define DEV "nlnetns0"
#define PEER_OFFSET 1000 /* ifindex in B minus ifindex in A */
#define SKIP 125 /* exit status: no verdict; "git bisect run" skips the commit */
static int ifindex; /* X: the device's ifindex while it is in A */
static atomic_int stop;
static atomic_ulong requests, enodev, foreign;
static void die(const char *what)
{
fprintf(stderr, "nlnetns: %s: %s\n", what, strerror(errno));
exit(SKIP);
}
/* A NETLINK_ROUTE socket in the calling thread's current netns. */
static int nlsock(void)
{
int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
struct timeval tv = { 10, 0 };
struct sockaddr_nl sa = { .nl_family = AF_NETLINK };
if (fd < 0)
die("socket");
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (bind(fd, (void *)&sa, sizeof(sa)))
die("bind");
return fd;
}
static int netns_fd(void)
{
int fd = open("/proc/thread-self/ns/net", O_RDONLY | O_CLOEXEC);
if (fd < 0)
die("open /proc/thread-self/ns/net");
return fd;
}
/* Send one rtnetlink request: ifinfomsg for ifindex, then optional attributes. */
static void request(int fd, int type, int flags, unsigned seq, int idx,
const void *attrs, int alen)
{
struct { struct nlmsghdr h; struct ifinfomsg i; char a[64]; } m = {0};
m.h.nlmsg_type = type;
m.h.nlmsg_flags = NLM_F_REQUEST | flags;
m.h.nlmsg_seq = seq;
m.i.ifi_index = idx;
if (alen)
memcpy(m.a, attrs, alen);
m.h.nlmsg_len = NLMSG_LENGTH(sizeof(m.i)) + alen;
if (send(fd, &m, m.h.nlmsg_len, 0) < 0)
die("send");
}
/* Wait for the ack of a request on fd. */
static void wait_ack(int fd, const char *what)
{
char buf[4096];
struct nlmsghdr *h = (void *)buf;
if (recv(fd, buf, sizeof(buf), 0) < 0)
die(what);
if (h->nlmsg_type != NLMSG_ERROR || ((struct nlmsgerr *)NLMSG_DATA(h))->error) {
errno = h->nlmsg_type == NLMSG_ERROR ? -((struct nlmsgerr *)NLMSG_DATA(h))->error : EPROTO;
die(what);
}
}
/*
* Move the device, known as ifindex idx in the netns of socket fd, to the
* netns behind ns, where it gets ifindex new_idx. The ack arrives after the
* move is complete.
*/
static void move(int fd, int idx, int ns, int new_idx)
{
struct { struct rtattr n; int ns; struct rtattr i; int idx; } a = {
{ RTA_LENGTH(sizeof(int)), IFLA_NET_NS_FD }, ns,
{ RTA_LENGTH(sizeof(int)), IFLA_NEW_IFINDEX }, new_idx };
request(fd, RTM_SETLINK, NLM_F_ACK, 0, idx, &a, sizeof(a));
wait_ack(fd, "move");
}
/*
* Requester: asks netns A for ifindex X with a full RTM_GETLINK (no
* IFLA_EXT_MASK, so not RTEXT_FILTER_NAME_ONLY) and applies the oracle from
* the header comment to the answer. The socket has no group membership and
* one request outstanding, so the next message on it is that answer.
*/
static void *flooder(void *arg)
{
char buf[1 << 15];
struct nlmsghdr *h = (void *)buf;
unsigned seq = 0;
int fd = nlsock();
(void)arg;
while (!atomic_load(&stop)) {
int n;
request(fd, RTM_GETLINK, 0, ++seq, ifindex, NULL, 0);
n = recv(fd, buf, sizeof(buf), 0);
if (n < 0)
die("recv GETLINK answer");
if (!NLMSG_OK(h, n) || h->nlmsg_seq != seq) {
errno = EPROTO;
die("GETLINK answer");
}
atomic_fetch_add(&requests, 1);
if (h->nlmsg_type == NLMSG_ERROR) {
errno = -((struct nlmsgerr *)NLMSG_DATA(h))->error;
if (errno != ENODEV)
die("GETLINK");
atomic_fetch_add(&enodev, 1);
} else if (h->nlmsg_type == RTM_NEWLINK) {
int got = ((struct ifinfomsg *)NLMSG_DATA(h))->ifi_index;
if (got != ifindex && atomic_fetch_add(&foreign, 1) < 5)
printf("GETLINK for ifindex %d answered with ifindex %d (the device in the other netns)\n",
ifindex, got);
} else {
errno = EPROTO;
die("GETLINK answer type");
}
}
close(fd);
return NULL;
}
static double since(struct timespec *t)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (now.tv_sec - t->tv_sec) * 1e3 + (now.tv_nsec - t->tv_nsec) / 1e6;
}
int main(int argc, char **argv)
{
int opt, nflood = 4, seconds = 30, moves = 0;
int ns_a, ns_b, ctl_a, ctl_b;
struct timespec start;
struct utsname u;
pthread_t *fl;
while ((opt = getopt(argc, argv, "t:f:")) != -1) {
if (opt == 't') seconds = atoi(optarg);
else if (opt == 'f') nflood = atoi(optarg);
else { fprintf(stderr, "usage: %s [-t SECONDS] [-f FLOODERS]\n", argv[0]); return SKIP; }
}
if (nflood < 1)
nflood = 1;
/*
* Create netns B and return to A. A socket stays in the netns it was
* created in, so ctl_b is opened while this thread is in B. This runs
* before any thread exists, so every thread starts in A.
*/
ns_a = netns_fd();
if (unshare(CLONE_NEWNET))
die("unshare(CLONE_NEWNET)");
ns_b = netns_fd();
ctl_b = nlsock();
if (setns(ns_a, CLONE_NEWNET))
die("setns back to the first netns");
ctl_a = nlsock();
/* Create the dummy link in A and find its index there. */
{
struct { struct rtattr n; char name[12]; struct rtattr li; struct rtattr k; char kind[8]; } a = {
{ RTA_LENGTH(sizeof(DEV)), IFLA_IFNAME }, DEV,
{ RTA_LENGTH(RTA_LENGTH(6)), IFLA_LINKINFO | NLA_F_NESTED },
{ RTA_LENGTH(6), IFLA_INFO_KIND }, "dummy" };
request(ctl_a, RTM_NEWLINK, NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL, 0, 0, &a, sizeof(a));
wait_ack(ctl_a, "create " DEV);
}
if (!(ifindex = if_nametoindex(DEV)))
die("if_nametoindex");
if (!(fl = calloc(nflood, sizeof(*fl))))
die("calloc");
for (int i = 0; i < nflood; i++)
if ((errno = pthread_create(&fl[i], NULL, flooder, NULL)))
die("pthread_create");
clock_gettime(CLOCK_MONOTONIC, &start);
while (since(&start) < seconds * 1e3) {
move(ctl_a, ifindex, ns_b, ifindex + PEER_OFFSET);
move(ctl_b, ifindex + PEER_OFFSET, ns_a, ifindex);
moves += 2;
}
/* The device is back in A. Every flooder request gets an answer, so the joins end. */
atomic_store(&stop, 1);
for (int i = 0; i < nflood; i++)
pthread_join(fl[i], NULL);
request(ctl_a, RTM_DELLINK, NLM_F_ACK, 0, ifindex, NULL, 0);
wait_ack(ctl_a, "delete " DEV);
free(fl);
uname(&u);
printf("%s: %d moves, %lu requests (%lu ENODEV), %lu violations\n", u.release, moves,
atomic_load(&requests), atomic_load(&enodev), atomic_load(&foreign));
return atomic_load(&foreign) ? 1 : 0;
}
reply other threads:[~2026-11-29 21:58 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=20261129215752.251291-1-kernel@cbannister.uk \
--to=kernel@cbannister.uk \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=regressions@lists.linux.dev \
--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