Skip to contentSkip to conversation

wireguard: peer: free packets left on the per-peer queues on removal

PATCH In review Andrii Pasichnyk 3 participants 7 messages Oct 3 – Nov 10

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wireguard: peer: free packets left on the per-peer queues 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 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, 1 vCPU, net-next):
remove a peer while a UDP flood keeps its receive queue busy, re-add it, repeat, then run the selftest's created/destroyed object check. Over 2140 removals the unpatched kernel leaked a peer and its keypair 5 times ("wg0: Peer 27: merely created"); with this patch, 0 times in another 2140.

Fixes e7096c131e51 ("net: WireGuard secure network tunnel")
Assisted-by LLM
---
 drivers/net/wireguard/peer.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/net/wireguard/peer.c +24 -0
index 1cb502a..3e08898 100644
@@ -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.
Signature · 1 line
2.53.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

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. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

#2 lore raw
Quoted 3 lines · On Sat, 3 Oct 2026 20:19:40 +0000 netdev-bot wrote:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

It was found by an LLM-assisted review of the peer removal path, while I was working on a WireGuard change for single-CPU systems; it was not hit in production. I then checked the mechanism by hand against __napi_poll() and the WireGuard history, and confirmed it with a stress test in the WireGuard selftest VM: remove a peer while a UDP flood keeps its receive queue busy, re-add it, repeat, then run the selftest's created/destroyed object check. Unpatched net-next leaked a peer and its keypair 5 times in 2140 removals; with the patch, 0 times in another 2140.

Thanks,
Andrii

author
#3 lore raw
In reply to Andrii Pasichnyk

From: Andrii Pasichnyk <[email protected]>

Hi Jason,

Gentle ping on this one. Patchwork shows it as awaiting upstream, so I assume it should go through your tree. It still applies to net and net-next unchanged; if you would rather have it taken through net directly, or want anything changed, please let me know.

Thanks,
Andrii

#4 lore raw
Quoted 8 lines · pasichdev

From: Andrii Pasichnyk <[email protected]>

Hi Jason,

Gentle ping on this one. Patchwork shows it as awaiting upstream, so I assume it should go through your tree. It still applies to net and net-next unchanged; if you would rather have it taken through net directly, or want anything changed, please let me know.

Thanks for the poke. I got bogged down with some other wireguard patches and this slipped past. I'll take a look in the coming week.

Jason

#5 lore raw
In reply to Andrii Pasichnyk
Quoted 6 lines · Andrii Pasichnyk

Reproduced with the WireGuard selftest VM (x86 KVM, 1 vCPU, net-next):
remove a peer while a UDP flood keeps its receive queue busy, re-add it, repeat, then run the selftest's created/destroyed object check. Over 2140 removals the unpatched kernel leaked a peer and its keypair 5 times ("wg0: Peer 27: merely created"); with this patch, 0 times in another 2140.

I'd be interested in seeing your adjustment to netns.sh so that I can test this myself and validate various approaches. Do you have something really minimal? Also, is this a test case that could be implemented minimally enough that we could integrate this into netns.sh permanently?

#6 lore raw
Quoted 4 lines · Jason A. Donenfeld

I'd be interested in seeing your adjustment to netns.sh so that I can test this myself and validate various approaches. Do you have something really minimal? Also, is this a test case that could be implemented minimally enough that we could integrate this into netns.sh permanently?

Here is a minimal version. It relies on the created/destroyed object check netns.sh already does at the end:

tools/testing/selftests/wireguard/netns.sh +14 -0
index a8f550aec..8303802c0 100755
@@ -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
index 368fd9d60..c69a408a5 100644
@@ -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

Two details matter:

- The ping after the re-add. Without it the re-added peer has no session while the other side keeps using the old keypair, so nothing flows until the next handshake and only the first removal happens under load. My original reproducer had this flaw, so the "5 in 2140" in the commit message undercounts: most of those removals were idle.

- The log buffer. With message_cost=0 each removal under load logs up to ~1000 "Packet has unallowed src IP" lines, which overflow the default 256 KB buffer; the object check then reports false "merely destroyed" errors and can miss real leaks. 1 MB is enough for 10 iterations. Without the kernel.config change, 2 iterations fit in the default buffer, but caught the leak in only 2 of 6 runs.

Results on net-next 66974df65a0b, x86 KVM:

unpatched, 1 vCPU: failed in all 3 runs; 25-40% of loaded removals
leak a peer and its keypair
unpatched, 2 vCPUs: failed (1 run)
unpatched, 4 vCPUs: passed (1 run)
patched, 1 and 4 vCPUs: passed; 0 leaks in 212 loaded removals,
including a 200-iteration run

A leak shows as "Error: wg0: Peer N: merely created" plus a matching "Keypair M: merely created". One gotcha when switching NR_CPUS: the qemu Makefile does not regenerate the kernel .config, so delete build/<arch>/kernel/.config first.

So yes, I think it can go into netns.sh permanently: 10 iterations add about 13 seconds and caught the bug on every 1-vCPU run of an unpatched kernel. I can send it as a separate patch, or fold it into a v2 of the fix, whichever you prefer.

Thanks,
Andrii