Skip to contentSkip to conversation

wireguard: flush endpoint dst cache when a peer is removed

PATCH In review Jun Yang 2 participants 2 messages Nov 6 – Nov 10

Subsystems (3)
lore
Share & tools
lore

Jun Yang

author
#0 lore raw

PATCH 1/1 wireguard: flush endpoint dst cache when a peer is removed

From: Jun Yang <[email protected]>

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")

---
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(-)
drivers/net/wireguard/peer.c +4 -1
index 1cb502a932e0..c8d9396b1f13 100644
@@ -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. */
 }
drivers/net/wireguard/socket.c +4 -2
index 0028ef17dc71..0cd916d480b2 100644
@@ -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))
Signature · 1 line
2.43.7
#1 lore raw
Quoted 7 lines · Jun Yang

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.

Quoted 10 lines

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")

---
A KASAN reproducer for this issue is available if requested.

Yes, please.