mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nguyen Ngoc Thang <ngocthang2710.1999@gmail.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@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	syzbot+fc3b1b923a16043b9d3c@syzkaller.appspotmail.com
Subject: [PATCH net] tap: don't take consumer_lock in tap_sock_destruct()
Date: Sun, 29 Nov 2026 22:59:30 +0700	[thread overview]
Message-ID: <20261129155930.234768-1-ngocthang2710.1999@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-29 15:59 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=20261129155930.234768-1-ngocthang2710.1999@gmail.com \
    --to=ngocthang2710.1999@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=syzbot+fc3b1b923a16043b9d3c@syzkaller.appspotmail.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

all inboxes | Powered by JetHome®