* [PATCH net v3 0/2] wireguard: free receive queue packets on peer removal, and test it
@ 2026-11-27 10:22 Andrii Pasichnyk
2026-11-27 10:22 ` [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal Andrii Pasichnyk
2026-11-27 10:22 ` [PATCH net v3 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk
0 siblings, 2 replies; 5+ messages in thread
From: Andrii Pasichnyk @ 2026-11-27 10:22 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
Removing a peer while its receive queue is busy can leave packets on
the per-peer receive queue. Each of them holds a keypair and a peer
reference, so the peer and the keypair are never freed. Patch 1 frees
what is left; patch 2 adds the test Jason asked for in the v1 thread to
netns.sh.
Tested on net e9e45fcb6 with the WireGuard selftest VM (x86_64 KVM):
both patches, 4 vCPUs: pass
both patches, NR_CPUS=1: pass
both patches, NR_CPUS=1, 200 removals: pass, nothing leaked
patch 2 only, NR_CPUS=1: fail, a peer and a keypair
"merely created"
Changes in v3:
- Patch 1: purge rx_queue only. The tx_queue purge in v2 could run
beside a transmit_packet_work that was queued during the second
flush, as the Sashiko review pointed out, and it was not needed: the
leak is on rx_queue. The older comment in peer_remove_after_dead() is
updated, the subject follows, and the commit message says how the bug
was found.
- Patch 2: CONFIG_LOG_BUF_SHIFT is 21 instead of 20, which covers the
worst case of the loop; the commit message says that NR_CPUS=1 is the
configuration that checks for the leak.
- Rebased on current net.
v2: https://lore.kernel.org/netdev/20261126094722.73025-1-apasichnik9@gmail.com/
v1: https://lore.kernel.org/netdev/20261003201625.4572-1-apasichnik9@gmail.com/
Andrii Pasichnyk (2):
wireguard: peer: free packets left on the receive queue on removal
wireguard: selftests: remove a peer under load
drivers/net/wireguard/peer.c | 25 +++++++++++++++++--
tools/testing/selftests/wireguard/netns.sh | 14 +++++++++++
.../selftests/wireguard/qemu/kernel.config | 2 +-
3 files changed, 38 insertions(+), 3 deletions(-)
base-commit: e9e45fcb6d39bee9325e6a7dca72bd14e82760f3
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal 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 2026-11-28 10:25 ` netdev-bot+sashiko 2026-11-27 10:22 ` [PATCH net v3 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk 1 sibling, 1 reply; 5+ messages in thread From: Andrii Pasichnyk @ 2026-11-27 10:22 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 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal 2026-11-27 10:22 ` [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal Andrii Pasichnyk @ 2026-11-28 10:25 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-11-28 10:25 UTC (permalink / raw) To: apasichnik9 Cc: Jason, wireguard, netdev, andrew+netdev, davem, edumazet, kuba, pabeni, shuah, linux-kselftest, linux-kernel Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 0 · Low: 2 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] The patch skips purging tx_queue. - [Low] The new comment in peer_remove_after_dead() says a transmit_packet_work queued during the second flush of packet_crypt_wq… - [Low] The commit message describes the bug only as a refcount/memory leak ('leaks a peer and its keypair'). -- wireguard: peer: free packets left on the receive queue on removal This commit adds peer_purge_rx_queue() and calls it from peer_remove_after_dead() after netif_napi_del(). It frees packets left on rx_queue when a NAPI that is being disabled completes without draining the queue, and drops each entry's keypair and peer references. tx_queue is intentionally left alone. > 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. [Severity: Low] This isn't a bug in the code, but could the message also mention the other effects of the leak? A stranded rx_queue entry pins the peer, so kref_release() never runs. As a result, rcu_release() never calls memzero_explicit(peer, sizeof(*peer)). That call clears the remaining handshake key material, such as the preshared key and the precomputed static-static DH value. The pinned keypairs also never reach kfree_sensitive() in keypair_free_rcu(), so the session keys stay in slab memory as well. peer->handshake.entry is only removed from index_hashtable in kref_release(), and peer_remove_after_dead() doesn't call wg_noise_handshake_clear(). The removed peer therefore stays reachable through wg_index_hashtable_lookup(). That contradicts this comment in peer_remove_after_dead(): and in wg_cookie_message_consume, the lookup eventually gets a peer with a refcount of zero, so no new reference is taken. As written, the message describes only a memory leak. The Fixes: tag should be enough for stable selection. > diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c > index 1cb502a932e07..6bafe572469ca 100644 > --- a/drivers/net/wireguard/peer.c > +++ b/drivers/net/wireguard/peer.c [ ... ] > @@ -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. > + */ [Severity: High] Do tx_queue entries really keep the peer alive until draining is done? The commit message makes the same claim: tx_queue needs no such step: transmit_packet_work drains it without a budget, and its entries keep the peer alive until then. If transmit_packet_work is still running after peer_remove_after_dead() drops the base reference, the put for the last entry in wg_packet_tx_worker() can be the final one: drivers/net/wireguard/send.c:wg_packet_tx_worker() { while ((first = wg_prev_queue_peek(&peer->tx_queue)) != NULL && ... wg_noise_keypair_put(keypair, false); wg_peer_put(peer); if (need_resched()) cond_resched(); } } That final put goes through kref_release()->call_rcu(&peer->rcu, rcu_release). The worker runs in process context outside any RCU read-side section, so a grace period can complete during cond_resched(), or while the worker is preempted under PREEMPT_RCU. rcu_release() then runs memzero_explicit() and kmem_cache_free() on the peer. After that, the worker evaluates wg_prev_queue_peek(&peer->tx_queue) again on freed memory. Couldn't this lead to a NULL dereference on the zeroed object, or to writes into a reused slab object via wg_prev_queue_dequeue()? The new comment itself says the work can outlive the flushes. The packet_crypt_wq encrypt workers share one ring across peers. An encrypt worker queued after the first flush_workqueue() began, for example by another peer's traffic, can take this peer's packet. It can then reach this point after the second flush has advanced the flush colour: drivers/net/wireguard/queueing.h:wg_queue_enqueue_per_peer_tx() { ... queue_work_on(wg_cpumask_choose_online(&peer->serial_work_cpu, peer->internal_id), peer->device->packet_crypt_wq, &peer->transmit_packet_work); } The second flush does not wait for that work. rcu_release() also peeks tx_queue in its WARN_ON() while the worker may still be in its loop. That makes it a second consumer of a single-consumer queue. wg_packet_tx_worker() predates this patch, but the new comment relies on this lifetime guarantee to skip tx_queue. Does something need to wait for transmit_packet_work before the final wg_peer_put() in peer_remove_after_dead()? > + peer_purge_rx_queue(peer); > > /* Ensure any workstructs we own (like transmit_handshake_work or > * clear_peer_work) no longer are in use. [Severity: Low] This isn't a bug, but the new comment contradicts two comments that this patch leaves unchanged in peer_remove_after_dead(). The marker before the second flush says it covers the send side: /* b.1) For send (but not receive, since that's napi). */ flush_workqueue(peer->device->packet_crypt_wq); The block before the final wg_peer_put() lists which contexts may still use the peer after the flushes: /* After the above flushes, a peer might still be active in a few * different contexts: 1) from xmit(), before hitting is_dead and * returning, 2) from wg_packet_consume_data(), before hitting is_dead * and returning, 3) from wg_receive_handshake_packet() after a point ... That list does not include a wg_packet_tx_worker() that is still running, even though the new comment says one can be. Should these two comments be updated to match? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127102243.49405-1-apasichnik9%40gmail.com ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net v3 2/2] wireguard: selftests: remove a peer under load 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 ` [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal Andrii Pasichnyk @ 2026-11-27 10:22 ` Andrii Pasichnyk 2026-11-28 10:25 ` netdev-bot+sashiko 1 sibling, 1 reply; 5+ messages in thread From: Andrii Pasichnyk @ 2026-11-27 10:22 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 receive queue 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 1024 "Packet has unallowed src IP" lines, one per queued packet. 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. In the worst case the loop logs about 1 MB, so raise CONFIG_LOG_BUF_SHIFT to 21. The loop adds about 13 seconds to a run. Without the previous patch it fails in every run with NR_CPUS=1. With the default four CPUs the leak is hit much less often, so NR_CPUS=1 is the configuration that checks for it. 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..1adf7cb6e 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=21 CONFIG_PRINTK_TIME=y CONFIG_BLK_DEV_INITRD=y CONFIG_LEGACY_VSYSCALL_NONE=y -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net v3 2/2] wireguard: selftests: remove a peer under load 2026-11-27 10:22 ` [PATCH net v3 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk @ 2026-11-28 10:25 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-11-28 10:25 UTC (permalink / raw) To: apasichnik9 Cc: Jason, wireguard, netdev, andrew+netdev, davem, edumazet, kuba, pabeni, shuah, linux-kselftest, linux-kernel Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin. Low priority / nit picks for your consideration: - [Low] With the in-tree default VM configuration, the new loop in tools/testing/selftests/wireguard/netns.sh (lines 210-222) will… -- wireguard: selftests: remove a peer under load This adds a loop to netns.sh that removes a peer ten times while a UDP flood is running, and re-adds it after each removal. The goal is to catch packets left on the per-peer receive queue that keep the peer alive. It also raises CONFIG_LOG_BUF_SHIFT to 21 in the qemu kernel.config so the created/destroyed object check sees the whole log. > diff --git a/tools/testing/selftests/wireguard/netns.sh b/tools/testing/selftests/wireguard/netns.sh > index a8f550aecb35d..8303802c02f27 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 [Severity: Low] Will this loop catch a regression of the rx_queue purge in the default selftest configuration? The qemu Makefile defaults to a 4-CPU guest: tools/testing/selftests/wireguard/qemu/Makefile: NR_CPUS ?= 4 ... -smp $(NR_CPUS) \ ... printf 'CONFIG_NR_CPUS=$(NR_CPUS)\n... The commit message says that with four CPUs "the leak is hit much less often, so NR_CPUS=1 is the configuration that checks for it". The leak needs rx_queue to still hold more than one NAPI budget of packets at napi_disable(). With several CPUs, the decrypt workers and NAPI usually drain the queue first. Nothing in the tree records this. No Makefile target runs netns.sh with NR_CPUS=1. The comment above also says the leak is something "which the created/destroyed check at the end catches", with no mention of the single-CPU requirement. As a result, a plain "make -C tools/testing/selftests/wireguard/qemu" run, including in CI, would usually pass on a kernel where peer_remove_after_dead() no longer calls peer_purge_rx_queue(). Could the comment or the qemu Makefile mention NR_CPUS=1? Alternatively, could a single-CPU run be added so the check is exercised by default? > + 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 > + [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127102243.49405-1-apasichnik9%40gmail.com ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-28 10:25 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 ` [PATCH net v3 1/2] wireguard: peer: free packets left on the receive queue on removal Andrii Pasichnyk 2026-11-28 10:25 ` netdev-bot+sashiko 2026-11-27 10:22 ` [PATCH net v3 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk 2026-11-28 10:25 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox