Netdev List
 help / color / mirror / Atom feed
* [PATCH net] wireguard: flush endpoint dst cache when a peer is removed
@ 2026-11-06  8:15 Jun Yang
  2026-11-10 16:56 ` Jason A. Donenfeld
  0 siblings, 1 reply; 3+ messages in thread
From: Jun Yang @ 2026-11-06  8:15 UTC (permalink / raw)
  To: Jason, andrew+netdev
  Cc: davem, edumazet, kuba, pabeni, wireguard, netdev, Jun Yang,
	stable, TencentOS Corvus AI

From: Jun Yang <junvyyang@tencent.com>

peer_make_dead() removes a peer from wg->peer_list, but a netlink device
dump can keep it alive through ctx->next_peer. If the WireGuard device
has moved to another network namespace, wg_netns_exit_rtnl() skips the
removed peer when flushing endpoint caches for the original namespace.
A cached IPv6 RTF_CACHE dst can then survive ip6_route_net_exit(), and
its later release from rcu_release() accesses the destroyed per-netns
dst counter.

Mark the peer dead and flush its endpoint cache under endpoint_lock.
Bypass the cache for dead peers in the send path so an in-flight sender
cannot repopulate it after the flush.

Fixes: 20ae1d6aa159 ("wireguard: device: reset peer src endpoint when netns exits")
Cc: stable@kernel.org
Reported-by: TencentOS Corvus AI <corvus@tencent.com>
Signed-off-by: Jun Yang <junvyyang@tencent.com>
---
A KASAN reproducer for this issue is available if requested.

 drivers/net/wireguard/peer.c   | 5 ++++-
 drivers/net/wireguard/socket.c | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c
index 1cb502a932e0..c8d9396b1f13 100644
--- a/drivers/net/wireguard/peer.c
+++ b/drivers/net/wireguard/peer.c
@@ -85,8 +85,11 @@ static void peer_make_dead(struct wg_peer *peer)
 				     &peer->device->device_update_lock);
 	wg_pubkey_hashtable_remove(peer->device->peer_hashtable, peer);
 
-	/* Mark as dead, so that we don't allow jumping contexts after. */
+	/* Mark as dead and flush the cache before the peer's netns exits. */
+	write_lock_bh(&peer->endpoint_lock);
 	WRITE_ONCE(peer->is_dead, true);
+	dst_cache_reset_now(&peer->endpoint_cache);
+	write_unlock_bh(&peer->endpoint_lock);
 
 	/* The caller must now synchronize_net() for this to take effect. */
 }
diff --git a/drivers/net/wireguard/socket.c b/drivers/net/wireguard/socket.c
index 0028ef17dc71..0cd916d480b2 100644
--- a/drivers/net/wireguard/socket.c
+++ b/drivers/net/wireguard/socket.c
@@ -167,15 +167,17 @@ out:
 int wg_socket_send_skb_to_peer(struct wg_peer *peer, struct sk_buff *skb, u8 ds)
 {
 	size_t skb_len = skb->len;
+	struct dst_cache *cache;
 	int ret = -EAFNOSUPPORT;
 
 	read_lock_bh(&peer->endpoint_lock);
+	cache = READ_ONCE(peer->is_dead) ? NULL : &peer->endpoint_cache;
 	if (peer->endpoint.addr.sa_family == AF_INET)
 		ret = send4(peer->device, skb, &peer->endpoint, ds,
-			    &peer->endpoint_cache);
+			    cache);
 	else if (peer->endpoint.addr.sa_family == AF_INET6)
 		ret = send6(peer->device, skb, &peer->endpoint, ds,
-			    &peer->endpoint_cache);
+			    cache);
 	else
 		dev_kfree_skb(skb);
 	if (likely(!ret))
-- 
2.43.7


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net] wireguard: flush endpoint dst cache when a peer is removed
  2026-11-06  8:15 [PATCH net] wireguard: flush endpoint dst cache when a peer is removed Jun Yang
@ 2026-11-10 16:56 ` Jason A. Donenfeld
  2026-12-01 11:27   ` Jun Yang
  0 siblings, 1 reply; 3+ messages in thread
From: Jason A. Donenfeld @ 2026-11-10 16:56 UTC (permalink / raw)
  To: Jun Yang
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, wireguard, netdev,
	Jun Yang, stable, TencentOS Corvus AI

On Fri, Nov 06, 2026 at 04:15:17PM +0800, Jun Yang wrote:
> peer_make_dead() removes a peer from wg->peer_list, but a netlink device
> dump can keep it alive through ctx->next_peer. If the WireGuard device
> has moved to another network namespace, wg_netns_exit_rtnl() skips the
> removed peer when flushing endpoint caches for the original namespace.
> A cached IPv6 RTF_CACHE dst can then survive ip6_route_net_exit(), and
> its later release from rcu_release() accesses the destroyed per-netns
> dst counter.

Can you explain this better in your own words? I'd rather you do the
work of interpreting the LLM-generated output and making it
comprehensible. If you understand the bug, write up your understanding
of it.

> Mark the peer dead and flush its endpoint cache under endpoint_lock.
> Bypass the cache for dead peers in the send path so an in-flight sender
> cannot repopulate it after the flush.
> 
> Fixes: 20ae1d6aa159 ("wireguard: device: reset peer src endpoint when netns exits")
> Cc: stable@kernel.org
> Reported-by: TencentOS Corvus AI <corvus@tencent.com>
> Signed-off-by: Jun Yang <junvyyang@tencent.com>
> ---
> A KASAN reproducer for this issue is available if requested.

Yes, please.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net] wireguard: flush endpoint dst cache when a peer is removed
  2026-11-10 16:56 ` Jason A. Donenfeld
@ 2026-12-01 11:27   ` Jun Yang
  0 siblings, 0 replies; 3+ messages in thread
From: Jun Yang @ 2026-12-01 11:27 UTC (permalink / raw)
  To: jason
  Cc: andrew+netdev, corvus, davem, edumazet, junvyyang, juny24602,
	kuba, netdev, pabeni, stable, wireguard

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2075 bytes --]

Here is my proposed text for the commit message:

When a WireGuard device is moved to another network namespace, the 
endpoint_cache of a peer may still hold routes belonging to that 
original namespace.

peer_make_dead() removes a peer from wg->peer_list
but fails to clear peer->endpoint_cache. If a netlink query still holds a
reference to the peer, the peer remains alive, and its endpoint_cache may
continue to hold an IPv6 route (marked with the RTF_CACHE flag) from the
original namespace. When the original namespace exits, wg_netns_exit_rtnl()
only cleans up the endpoint_cache for peers still present in wg->peer_list.
Since the peer in question has already been removed from the list,
wg_netns_exit_rtnl() does not release its endpoint_cache reference to the
route. Subsequently, ip6_route_net_exit() destroys the IPv6 route counter
for the original namespace.

When the peer is eventually reclaimed, rcu_release() calls
dst_cache_destroy(&peer->endpoint_cache). dst_cache_destroy() invokes
dst_release(dst) for the cached route. If this is the final reference to
the dst object, dst_release() calls dst_count_dec()—attempting to decrement
the IPv6 route counter of the original namespace—which triggers a
Use-After-Free.

By acquiring the endpoint_lock (write lock) within peer_make_dead(), we can
mark the peer as is_dead and immediately clear its endpoint_cache. Any
ongoing send operations that already hold the read lock will complete, and
their newly cached routes will also be cleared. Subsequent send operations
that acquire the read lock will observe the is_dead flag and bypass the
cache. Thus, even if a netlink query retains a reference to the peer, the
peer’s endpoint_cache will have already released its references to the
routes.

If this text looks good to you, I will proceed to submit a v2 patch;
otherwise, I am happy to make further adjustments.

I previously sent you the PoC code privately—I am not sure if you received
it, but I can resend it if necessary.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01 11:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-06  8:15 [PATCH net] wireguard: flush endpoint dst cache when a peer is removed Jun Yang
2026-11-10 16:56 ` Jason A. Donenfeld
2026-12-01 11:27   ` Jun Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox