* [PATCH net v2 1/2] wireguard: peer: free packets left on the per-peer queues on removal
2026-11-26 9:47 [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it Andrii Pasichnyk
@ 2026-11-26 9:47 ` Andrii Pasichnyk
2026-11-26 9:47 ` [PATCH net v2 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk
2026-11-26 9:52 ` [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it netdev-bot+sinfo
2 siblings, 0 replies; 4+ messages in thread
From: Andrii Pasichnyk @ 2026-11-26 9:47 UTC (permalink / raw)
To: Jason A . Donenfeld
Cc: wireguard, netdev, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Shuah Khan, linux-kselftest,
linux-kernel
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 whatever is left on both per-peer queues once nothing can feed them
any more: receive entries are single packets, transmit entries lists.
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
212 removals. 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 | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c
index 1cb502a93..3e08898ce 100644
--- a/drivers/net/wireguard/peer.c
+++ b/drivers/net/wireguard/peer.c
@@ -91,6 +91,25 @@ static void peer_make_dead(struct wg_peer *peer)
/* The caller must now synchronize_net() for this to take effect. */
}
+/* Each queue entry holds a keypair and a peer reference. Transmit entries
+ * are lists of packets, receive entries single packets.
+ */
+static void peer_purge_queue(struct wg_peer *peer, struct prev_queue *queue,
+ bool lists)
+{
+ struct sk_buff *first;
+
+ while ((first = wg_prev_queue_peek(queue)) != NULL) {
+ wg_prev_queue_drop_peeked(queue);
+ wg_noise_keypair_put(PACKET_CB(first)->keypair, false);
+ wg_peer_put(peer);
+ if (lists)
+ kfree_skb_list(first);
+ else
+ dev_kfree_skb(first);
+ }
+}
+
static void peer_remove_after_dead(struct wg_peer *peer)
{
WARN_ON(!peer->is_dead);
@@ -122,6 +141,11 @@ static void peer_remove_after_dead(struct wg_peer *peer)
* here from process context.
*/
netif_napi_del(&peer->napi);
+ /* A NAPI being disabled completes after at most one more poll, which
+ * may leave packets on rx_queue that still hold references.
+ */
+ peer_purge_queue(peer, &peer->rx_queue, false);
+ peer_purge_queue(peer, &peer->tx_queue, true);
/* Ensure any workstructs we own (like transmit_handshake_work or
* clear_peer_work) no longer are in use.
base-commit: e509726660dd06ce107437aecf7c106c9d3fc1e7
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH net v2 2/2] wireguard: selftests: remove a peer under load
2026-11-26 9:47 [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it Andrii Pasichnyk
2026-11-26 9:47 ` [PATCH net v2 1/2] wireguard: peer: free packets left on the per-peer queues on removal Andrii Pasichnyk
@ 2026-11-26 9:47 ` Andrii Pasichnyk
2026-11-26 9:52 ` [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it netdev-bot+sinfo
2 siblings, 0 replies; 4+ messages in thread
From: Andrii Pasichnyk @ 2026-11-26 9:47 UTC (permalink / raw)
To: Jason A . Donenfeld
Cc: wireguard, netdev, Andrew Lunn, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Shuah Khan, linux-kselftest,
linux-kernel
Remove a peer while a UDP flood keeps its receive queue busy, re-add it,
and repeat ten times. Packets left on the per-peer queues keep the peer
and its keypair alive, which the created/destroyed object check at the
end of netns.sh reports as "merely created".
The ping after the re-add makes the new peer handshake at once. Without
it the other side keeps using the old keypair until its own timers start
a handshake, nothing flows in the meantime, and only the first removal
happens under load.
netns.sh sets message_cost to 0, so every removal under load logs up to
about 1000 "Packet has unallowed src IP" lines. Ten of them overflow the
256 KB kernel log buffer; the object check then reads an incomplete log,
reports peers as "merely destroyed" and can miss real leaks. Raise
CONFIG_LOG_BUF_SHIFT to 20.
The loop adds about 13 seconds to a run. Without the previous patch it
fails in every run with 1 vCPU.
Signed-off-by: Andrii Pasichnyk <apasichnik9@gmail.com>
---
tools/testing/selftests/wireguard/netns.sh | 14 ++++++++++++++
.../testing/selftests/wireguard/qemu/kernel.config | 2 +-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/wireguard/netns.sh b/tools/testing/selftests/wireguard/netns.sh
index a8f550aec..8303802c0 100755
--- a/tools/testing/selftests/wireguard/netns.sh
+++ b/tools/testing/selftests/wireguard/netns.sh
@@ -207,6 +207,20 @@ n0 iptables -F INPUT
ip1 link set wg0 mtu $orig_mtu
ip2 link set wg0 mtu $orig_mtu
+# Remove a peer while a UDP flood keeps its receive queue busy, then re-add it;
+# packets left on the per-peer queues would keep it from being destroyed,
+# which the created/destroyed check at the end catches.
+for ((i = 0; i < 10; ++i)); do
+ n1 iperf3 -s -1 -B 192.168.241.1 &
+ pids=( $! ); waitiperf $netns1 $!
+ n2 iperf3 -Z -t 2 -b 0 -u -l 64 -P 4 -c 192.168.241.1 >/dev/null 2>&1 &
+ pids+=( $! ); sleep 1
+ n1 wg set wg0 peer "$pub2" remove
+ kill "${pids[@]}" 2>/dev/null || true; wait "${pids[@]}" || true
+ n1 wg set wg0 peer "$pub2" preshared-key <(echo "$psk") allowed-ips 192.168.241.2/32,fd00::2/128 endpoint 127.0.0.1:2
+ n1 ping -c 1 -W 1 192.168.241.2
+done
+
# Test using IPv4 that roaming works
ip0 -4 addr del 127.0.0.1/8 dev lo
ip0 -4 addr add 127.212.121.99/8 dev lo
diff --git a/tools/testing/selftests/wireguard/qemu/kernel.config b/tools/testing/selftests/wireguard/qemu/kernel.config
index 368fd9d60..c69a408a5 100644
--- a/tools/testing/selftests/wireguard/qemu/kernel.config
+++ b/tools/testing/selftests/wireguard/qemu/kernel.config
@@ -67,7 +67,7 @@ CONFIG_SYSCTL=y
CONFIG_SYSFS=y
CONFIG_TMPFS=y
CONFIG_CONSOLE_LOGLEVEL_DEFAULT=15
-CONFIG_LOG_BUF_SHIFT=18
+CONFIG_LOG_BUF_SHIFT=20
CONFIG_PRINTK_TIME=y
CONFIG_BLK_DEV_INITRD=y
CONFIG_LEGACY_VSYSCALL_NONE=y
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it
2026-11-26 9:47 [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it Andrii Pasichnyk
2026-11-26 9:47 ` [PATCH net v2 1/2] wireguard: peer: free packets left on the per-peer queues on removal Andrii Pasichnyk
2026-11-26 9:47 ` [PATCH net v2 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk
@ 2026-11-26 9:52 ` netdev-bot+sinfo
2 siblings, 0 replies; 4+ messages in thread
From: netdev-bot+sinfo @ 2026-11-26 9:52 UTC (permalink / raw)
To: Andrii Pasichnyk
Cc: Jason A . Donenfeld, wireguard, netdev, Andrew Lunn,
David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Shuah Khan, linux-kselftest, linux-kernel
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during
development, syzbot report, manual code inspection, LLM or static
analysis tool scan.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 4+ messages in thread