mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrii Pasichnyk <apasichnik9@gmail.com>
To: "Jason A . Donenfeld" <Jason@zx2c4.com>
Cc: wireguard@lists.zx2c4.com, netdev@vger.kernel.org,
	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>,
	Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal
Date: Fri, 27 Nov 2026 12:22:42 +0200	[thread overview]
Message-ID: <20261127102243.49405-2-apasichnik9@gmail.com> (raw)
In-Reply-To: <20261127102243.49405-1-apasichnik9@gmail.com>

peer_remove_after_dead() flushes the crypt workqueue and then disables
the peer's NAPI. Once a disable is pending, __napi_poll() completes the
instance after the poll returns even if it used its whole budget, so a
peer removed while more than one budget of decrypted packets waits on
rx_queue keeps the rest there. Each entry holds a keypair and a peer
reference, so the peer is never released; the WARN_ON in rcu_release()
that checks for leftovers is never reached either.

Free what is left on rx_queue once the NAPI is gone and nothing reads
the queue any more. tx_queue needs no such step: transmit_packet_work
drains it without a budget, and its entries keep the peer alive until
then.

Hit on a single-CPU MIPS router (Realtek RTL8196E) in everyday use as a
WireGuard gateway, then reproduced with the WireGuard selftest VM (x86
KVM): remove a peer while a UDP flood keeps its receive queue busy,
re-add it, bring the session back up, repeat, then run the selftest's
created/destroyed object check. With 1 vCPU the unpatched kernel leaks a
peer and its keypair in 25-40% of such removals ("wg0: Peer 11: merely
created"); with this patch, 0 in 210. The next patch adds the loop to
netns.sh.

Fixes: e7096c131e51 ("net: WireGuard secure network tunnel")
Signed-off-by: Andrii Pasichnyk <apasichnik9@gmail.com>
---
 drivers/net/wireguard/peer.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c
index 1cb502a93..6bafe5724 100644
--- a/drivers/net/wireguard/peer.c
+++ b/drivers/net/wireguard/peer.c
@@ -91,6 +91,19 @@ static void peer_make_dead(struct wg_peer *peer)
 	/* The caller must now synchronize_net() for this to take effect. */
 }
 
+/* Each receive queue entry holds a keypair and a peer reference. */
+static void peer_purge_rx_queue(struct wg_peer *peer)
+{
+	struct sk_buff *skb;
+
+	while ((skb = wg_prev_queue_peek(&peer->rx_queue)) != NULL) {
+		wg_prev_queue_drop_peeked(&peer->rx_queue);
+		wg_noise_keypair_put(PACKET_CB(skb)->keypair, false);
+		wg_peer_put(peer);
+		dev_kfree_skb(skb);
+	}
+}
+
 static void peer_remove_after_dead(struct wg_peer *peer)
 {
 	WARN_ON(!peer->is_dead);
@@ -108,8 +121,9 @@ static void peer_remove_after_dead(struct wg_peer *peer)
 	/* The transition between packet encryption/decryption queues isn't
 	 * guarded by is_dead, but each reference's life is strictly bounded by
 	 * two generations: once for parallel crypto and once for serial
-	 * ingestion, so we can simply flush twice, and be sure that we no
-	 * longer have references inside these queues.
+	 * ingestion, so we can simply flush twice, and be sure that no crypto
+	 * worker still holds a packet of this peer. The per-peer queues may
+	 * still hold packets after that; see below.
 	 */
 
 	/* a) For encrypt/decrypt. */
@@ -122,6 +136,13 @@ static void peer_remove_after_dead(struct wg_peer *peer)
 	 * here from process context.
 	 */
 	netif_napi_del(&peer->napi);
+	/* A NAPI that is being disabled completes after the poll in progress,
+	 * even if that poll used its whole budget, so rx_queue may still hold
+	 * packets, and nothing reads it any more. tx_queue is left alone: a
+	 * transmit_packet_work queued during the second flush may still be
+	 * draining it, and its entries keep the peer alive until it has.
+	 */
+	peer_purge_rx_queue(peer);
 
 	/* Ensure any workstructs we own (like transmit_handshake_work or
 	 * clear_peer_work) no longer are in use.
-- 
2.43.0


  reply	other threads:[~2026-11-27 10:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 10:22 [PATCH net v3 0/2] wireguard: free receive queue packets on peer removal, and test it Andrii Pasichnyk
2026-11-27 10:22 ` Andrii Pasichnyk [this message]
2026-11-27 10:22 ` [PATCH net v3 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk

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=20261127102243.49405-2-apasichnik9@gmail.com \
    --to=apasichnik9@gmail.com \
    --cc=Jason@zx2c4.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=wireguard@lists.zx2c4.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®