wireguard: free receive queue packets on peer removal, and test it
Subsystems (4)
v3 cover wireguard: free receive queue packets on peer removal, and test it
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/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
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
Signature · 1 line
2.43.0
PATCH v3 1/2 wireguard: peer: free packets left on the receive queue on removal
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.
--- drivers/net/wireguard/peer.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
drivers/net/wireguard/peer.c +23 -2
@@ -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.
Signature · 1 line
2.43.0
PATCH v3 2/2 wireguard: selftests: remove a peer under load
In reply to Andrii PasichnykRemove 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.
--- tools/testing/selftests/wireguard/netns.sh | 14 ++++++++++++++ .../testing/selftests/wireguard/qemu/kernel.config | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-)
tools/testing/selftests/wireguard/netns.sh +14 -0
@@ -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
tools/testing/selftests/wireguard/qemu/kernel.config +1 -1
@@ -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
Signature · 1 line
2.43.0