mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] tap: don't take consumer_lock in tap_sock_destruct()
@ 2026-11-29 15:59 Nguyen Ngoc Thang
  0 siblings, 0 replies; only message in thread
From: Nguyen Ngoc Thang @ 2026-11-29 15:59 UTC (permalink / raw)
  To: Willem de Bruijn, Jason Wang
  Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Jason A . Donenfeld, netdev, linux-kernel,
	syzbot+fc3b1b923a16043b9d3c

tap_sock_destruct() runs when the last reference to the queue socket
is dropped. That is not necessarily tap_release(): skbs written to the
tap fd are charged to the socket, so the final sock_put() can come
from sock_wfree() anywhere in the transmit path, with BH disabled and
arbitrary locks held.

syzbot hit this with a macvtap -> veth -> mirred -> wireguard setup.
The wireguard tx worker sends the encrypted skb to a loopback endpoint
while holding peer->endpoint_lock (SOFTIRQ-safe). loopback_xmit()
orphans the skb, the tap socket goes away, and ptr_ring_cleanup()
takes q->ring.consumer_lock, which is SOFTIRQ-unsafe:

  WARNING: SOFTIRQ-safe -> SOFTIRQ-unsafe lock order detected
  ...
   tap_sock_destruct+0x40/0x3f8 drivers/net/tap.c:444
   __sk_free+0x264/0x328 net/core/sock.c:2459
   sock_wfree+0x234/0x6fc net/core/sock.c
   loopback_xmit+0x144/0x6cc drivers/net/loopback.c:81
   ...
   send4+0x8cc/0xcf4 drivers/net/wireguard/socket.c:85
   wg_socket_send_skb_to_peer+0xc0/0x19c drivers/net/wireguard/socket.c:174
   wg_packet_tx_worker+0x188/0x75c drivers/net/wireguard/send.c:276

The lock is not needed there. The refcount is zero and tap_put_queue()
has already unpublished the queue and waited for an RCU grace period,
so neither tap_handle_frame() nor a reader can touch the ring anymore.
Drain it with __ptr_ring_consume() and free it without locking.

Fixes: 362899b8725b ("macvtap: switch to use skb array")
Reported-by: syzbot+fc3b1b923a16043b9d3c@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=fc3b1b923a16043b9d3c
Signed-off-by: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.com>
---
Tested in QEMU (x86_64, PROVE_LOCKING) with the syzbot C reproducer:
without the patch the lockdep splat fires on the first run; with it,
3 boots x 3 reproducer runs are clean and debug_locks stays 1.

 drivers/net/tap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 2f19fb07bb2d4..bcb37679c5a1f 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -440,8 +440,14 @@ static void tap_sock_write_space(struct sock *sk)
 static void tap_sock_destruct(struct sock *sk)
 {
 	struct tap_queue *q = container_of(sk, struct tap_queue, sk);
+	struct sk_buff *skb;
 
-	ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
+	/* Last reference is gone and the ring is private. This may run from
+	 * sock_wfree() in the xmit path, so don't take consumer_lock.
+	 */
+	while ((skb = __ptr_ring_consume(&q->ring)))
+		kfree_skb(skb);
+	ptr_ring_cleanup(&q->ring, NULL);
 }
 
 static int tap_open(struct inode *inode, struct file *file)
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-29 15:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-29 15:59 [PATCH net] tap: don't take consumer_lock in tap_sock_destruct() Nguyen Ngoc Thang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®