From: xietangxin <xietangxin@h-partners.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
Jason Wang <jasowangio@gmail.com>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
"David S . Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
huyizhen <huyizhen2@huawei.com>,
gaoxingwang <gaoxingwang1@huawei.com>,
xietangxin <xietangxin@h-partners.com>, <netdev@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: [PATCH net] net: tun: fix race condition between tun_attach and tun_get_user with XDP
Date: Thu, 17 Sep 2026 15:41:49 +0800 [thread overview]
Message-ID: <20260917074149.1799437-1-xietangxin@h-partners.com> (raw)
Concurrent TUNSETIFF ioctls and write() operations on the same
file descriptor can trigger a WARN_ONCE in netif_get_rxqueue():
virt_tun0 received packet on queue 2, but number of RX queues is 2
WARNING: CPU: 2 PID: 5989 at net/core/dev.c:5494
Call trace:
bpf_prog_run_generic_xdp
netif_receive_generic_xdp
do_xdp_generic
tun_get_user
tun_chr_write_iter
Because tfile->tun is published before tun_set_real_num_queues()
updates dev->real_num_rx_queues. Concurrent tun_get_user() can
observe the newly attached tun file and process packets with
the new queue_index before dev->real_num_rx_queues is updated.
CPU 0 (tun_attach) CPU 1 (tun_get_user)
tfile->queue_index = 2
rcu_assign_pointer(tfile->tun)
skb_record_rx_queue(skb,tfile->queue_index)
do_xdp_generic()
netif_receive_generic_xdp()
bpf_prog_run_generic_xdp()
netif_get_rxqueue()
// index(2) >= real_num_rx_queues(2)
WARN_ONCE
tun_set_real_num_queues()
dev->real_num_rx_queues = 3
Fix this by increase tun->numqueues and call tun_set_real_num_queues()
prior to publishing tfile->tun. This ensures dev->real_num_rx_queues
is updated before any concurrent packet processing path can observe
the attached tun file.
Fixes: 3fe260e00cd0 ("net: tun: record RX queue in skb before do_xdp_generic()")
Cc: stable@vger.kernel.org
Signed-off-by: xietangxin <xietangxin@h-partners.com>
---
drivers/net/tun.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index 5a302709a68a..2fb8df036d8c 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -807,15 +807,16 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
* refcnt.
*/
+ WRITE_ONCE(tun->numqueues, tun->numqueues + 1);
+ tun_set_real_num_queues(tun);
+
/* Publish tfile->tun and tun->tfiles only after we've fully
* initialized tfile; otherwise we risk using half-initialized
* object.
*/
if (publish_tun)
rcu_assign_pointer(tfile->tun, tun);
- rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
- WRITE_ONCE(tun->numqueues, tun->numqueues + 1);
- tun_set_real_num_queues(tun);
+ rcu_assign_pointer(tun->tfiles[tun->numqueues - 1], tfile);
out:
return err;
}
--
2.43.0
next reply other threads:[~2026-09-17 7:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-17 7:41 xietangxin [this message]
2026-09-20 22:02 ` [PATCH net] net: tun: fix race condition between tun_attach and tun_get_user with XDP netdev-bot+sashiko
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=20260917074149.1799437-1-xietangxin@h-partners.com \
--to=xietangxin@h-partners.com \
--cc=andrew+netdev@lunn.ch \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=gaoxingwang1@huawei.com \
--cc=huyizhen2@huawei.com \
--cc=jasowangio@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=willemdebruijn.kernel@gmail.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