wireguard: account peer and allowedips allocations to memcg
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 wireguard: account peer and allowedips allocations to memcg
Peers, allowed IP trie nodes, the per-peer dst_cache and the per-device hashtables are all allocated in the context of the process that configures the device over netlink, but none of these allocations are charged to that process's memory cgroup: the wg_peer and allowedips_node caches are created without SLAB_ACCOUNT, and the hashtables and dst_cache use plain GFP_KERNEL.
The amount of memory is bounded only by MAX_PEERS_PER_DEVICE (2^20) per device, with no limit on the number of devices or on the number of allowed IPs per peer. Since WG_CMD_SET_DEVICE uses GENL_UNS_ADMIN_PERM, any user who can create a user and network namespace can do this, and thereby escape the memory limits of the cgroup it runs in.
For example, a process in a cgroup with memory.max=64M that adds 100000 peers with 20 /128 allowed IPs each grows Slab by ~790 MiB, yet the cgroup's memory.current stays below 1 MiB and its memory limit is never hit.
Fix this by creating the two caches with SLAB_ACCOUNT and using GFP_KERNEL_ACCOUNT for the hashtables and the dst_cache. The keypair allocations are left alone, as they happen from the handshake worker and are bounded to three per peer.
--- drivers/net/wireguard/allowedips.c | 2 +- drivers/net/wireguard/peer.c | 4 ++-- drivers/net/wireguard/peerlookup.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/net/wireguard/allowedips.c +1 -1
@@ -411,7 +411,7 @@ struct wg_peer *wg_allowedips_lookup_src(struct allowedips *table,int __init wg_allowedips_slab_init (void ) { - node_cache =KMEM_CACHE (allowedips_node,0 ); + node_cache =KMEM_CACHE (allowedips_node,SLAB_ACCOUNT );return node_cache ?0 : -ENOMEM ; }
drivers/net/wireguard/peer.c +2 -2
@@ -33,7 +33,7 @@ struct wg_peer *wg_peer_create(struct wg_device *wg, peer =kmem_cache_zalloc (peer_cache,GFP_KERNEL );if (unlikely (!peer))return ERR_PTR (ret); -if (unlikely (dst_cache_init (&peer->endpoint_cache,GFP_KERNEL ))) +if (unlikely (dst_cache_init (&peer->endpoint_cache,GFP_KERNEL_ACCOUNT ))) goto err; peer->device = wg;
@@ -229,7 +229,7 @@ void wg_peer_put(struct wg_peer *peer)int __init wg_peer_init (void ) { - peer_cache =KMEM_CACHE (wg_peer,0 ); + peer_cache =KMEM_CACHE (wg_peer,SLAB_ACCOUNT );return peer_cache ?0 : -ENOMEM ; }
drivers/net/wireguard/peerlookup.c +2 -2
@@ -21,7 +21,7 @@ static struct hlist_head *pubkey_bucket(struct pubkey_hashtable *table,struct pubkey_hashtable *wg_pubkey_hashtable_alloc (void ) { -struct pubkey_hashtable *table =kvmalloc_obj (*table); +struct pubkey_hashtable *table =kvmalloc_obj (*table,GFP_KERNEL_ACCOUNT );if (!table)return NULL ;
@@ -82,7 +82,7 @@ static struct hlist_head *index_bucket(struct index_hashtable *table,struct index_hashtable *wg_index_hashtable_alloc (void ) { -struct index_hashtable *table =kvmalloc_obj (*table); +struct index_hashtable *table =kvmalloc_obj (*table,GFP_KERNEL_ACCOUNT );if (!table)return NULL ;
Signature · 1 line
2.43.0
Jason A. Donenfeld
maintainerQuoted 18 lines · Alexander Ildiyarov
Peers, allowed IP trie nodes, the per-peer
dst_cacheand the per-device hashtables are all allocated in the context of the process that configures the device over netlink, but none of these allocations are charged to that process's memory cgroup: thewg_peerandallowedips_nodecaches are created withoutSLAB_ACCOUNT, and the hashtables anddst_cacheuse plainGFP_KERNEL.The amount of memory is bounded only by
MAX_PEERS_PER_DEVICE(2^20) per device, with no limit on the number of devices or on the number of allowed IPs per peer. SinceWG_CMD_SET_DEVICEusesGENL_UNS_ADMIN_PERM, any user who can create a user and network namespace can do this, and thereby escape the memory limits of the cgroup it runs in.For example, a process in a cgroup with memory.max=64M that adds 100000 peers with 20 /128 allowed IPs each grows Slab by ~790 MiB, yet the cgroup's memory.current stays below 1 MiB and its memory limit is never hit.
Quoted 4 lines
Fix this by creating the two caches with
SLAB_ACCOUNTand usingGFP_KERNEL_ACCOUNTfor the hashtables and thedst_cache. The keypair allocations are left alone, as they happen from the handshake worker and are bounded to three per peer.
I wasn't aware of this trick, I think mostly because most drivers don't use this. Does any other part of the networking stack do this? Is this a normal thing?
PATCH 2/2 wireguard: noise: recheck replay and flood under write lock on initiation
In reply to a message not in the archivewg_noise_handshake_consume_initiation() checks the initiation's timestamp against latest_timestamp, and last_initiation_consumption against the current time, while holding handshake->lock for reading. It then drops the lock and takes it again for writing to store the new state, without repeating the checks.
Incoming handshake packets are spread round-robin over all online CPUs, so two copies of the same initiation, received back to back, are processed in parallel. Both copies can pass the checks before either of them updates latest_timestamp and last_initiation_consumption, so the replay and flood protection is bypassed. The responder then creates two responses, each with a new ephemeral key, and installs the keypair of the last one, while the initiator accepts whichever response arrives first. When those differ, the session is unusable until the initiator gives up on it and starts a new handshake, and an attacker who can see and duplicate initiations can repeat this for every handshake.
The window is narrow, since the expensive DH computations happen before the check, so it is between up_read() and down_write() only. With netem duplicating every initiation between two namespaces, no double response was observed in 200 handshakes on a 4-CPU VM. With a usleep_range(1000, 2000) inserted before down_write() to widen the window, 30 of 50 duplicated initiations produced two responses, and in 12 of 50 cases the session was unusable after the handshake. With this patch applied, and the same delay, every duplicate was rejected and all 50 sessions worked.
Fix this by doing the checks again once the write lock is held, keeping the read-locked check as a fast path for rejecting replays.
This is the initiation-side counterpart of the response-side race fixed
by:
commit 2a73c9b2c0ca ("wireguard: noise: reject response consumption after intermediate initiation")
--- drivers/net/wireguard/noise.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-)
drivers/net/wireguard/noise.c +22 -7
@@ -580,13 +580,23 @@ wg_noise_handshake_create_initiation(struct message_handshake_initiation *dst,return ret; } +/* Must hold handshake->lock, for reading or writing */ +static bool initiation_is_replay_or_flood (const struct noise_handshake *handshake, +const u8 timestamp[NOISE_TIMESTAMP_LEN ]) +{ +return memcmp (timestamp, handshake->latest_timestamp, +NOISE_TIMESTAMP_LEN ) <=0 || + (s64 )handshake->last_initiation_consumption + +NSEC_PER_SEC /INITIATIONS_PER_SECOND > + (s64 )ktime_get_coarse_boottime_ns (); +} +struct wg_peer *wg_noise_handshake_consume_initiation (struct message_handshake_initiation *src,struct wg_device *wg) {struct wg_peer *peer =NULL , *ret_peer =NULL ;struct noise_handshake *handshake; -bool replay_attack, flood_attack;u8 key[NOISE_SYMMETRIC_KEY_LEN ];u8 chaining_key[NOISE_HASH_LEN ];u8 hash[NOISE_HASH_LEN ];
@@ -594,6 +604,7 @@ wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src,u8 e[NOISE_PUBLIC_KEY_LEN ];u8 t[NOISE_TIMESTAMP_LEN ];u64 initiation_consumption; +bool reject;down_read (&wg->static_identity.lock);if (unlikely (!wg->static_identity.has_identity))
@@ -630,17 +641,21 @@ wg_noise_handshake_consume_initiation(struct message_handshake_initiation *src, goto out;down_read (&handshake->lock); - replay_attack =memcmp (t, handshake->latest_timestamp, -NOISE_TIMESTAMP_LEN ) <=0 ; - flood_attack = (s64 )handshake->last_initiation_consumption + -NSEC_PER_SEC /INITIATIONS_PER_SECOND > - (s64 )ktime_get_coarse_boottime_ns (); + reject =initiation_is_replay_or_flood (handshake, t);up_read (&handshake->lock); -if (replay_attack || flood_attack) +if (reject) goto out;/* Success! Copy everything to peer */ down_write (&handshake->lock); +/* Check again, now that we have an exclusive lock, so that two threads +* processing the same initiation in parallel can't both pass the check +* above before either of them updates the timestamp. +*/ +if (initiation_is_replay_or_flood (handshake, t)) { +up_write (&handshake->lock); + goto out; + }memcpy (handshake->remote_ephemeral, e,NOISE_PUBLIC_KEY_LEN );if (memcmp (t, handshake->latest_timestamp,NOISE_TIMESTAMP_LEN ) >0 )memcpy (handshake->latest_timestamp, t,NOISE_TIMESTAMP_LEN );
Signature · 1 line
2.43.0
Jason A. Donenfeld
maintainerRe: [PATCH net 2/2] wireguard: noise: recheck replay and flood under write lock on initiation
Quoted 27 lines · Alexander Ildiyarov
wg_noise_handshake_consume_initiation()checks the initiation's timestamp againstlatest_timestamp, andlast_initiation_consumptionagainst the current time, while holdinghandshake->lockfor reading. It then drops the lock and takes it again for writing to store the new state, without repeating the checks.Incoming handshake packets are spread round-robin over all online CPUs, so two copies of the same initiation, received back to back, are processed in parallel. Both copies can pass the checks before either of them updates
latest_timestampandlast_initiation_consumption, so the replay and flood protection is bypassed. The responder then creates two responses, each with a new ephemeral key, and installs the keypair of the last one, while the initiator accepts whichever response arrives first. When those differ, the session is unusable until the initiator gives up on it and starts a new handshake, and an attacker who can see and duplicate initiations can repeat this for every handshake.The window is narrow, since the expensive DH computations happen before the check, so it is between
up_read()anddown_write()only. With netem duplicating every initiation between two namespaces, no double response was observed in 200 handshakes on a 4-CPU VM. With ausleep_range(1000, 2000)inserted beforedown_write()to widen the window, 30 of 50 duplicated initiations produced two responses, and in 12 of 50 cases the session was unusable after the handshake. With this patch applied, and the same delay, every duplicate was rejected and all 50 sessions worked.
Quoted 2 lines
Fix this by doing the checks again once the write lock is held, keeping the read-locked check as a fast path for rejecting replays.
The downside, though, is that now it means there's potential to do flood attacks that take the exclusive lock, which is what was trying to be avoided, right?
Re: [PATCH net 2/2] wireguard: noise: recheck replay and flood under write lock on initiation
I don't think it changes which packets take the exclusive lock. Before this patch, every initiation that passed the read-locked check already went on to take down_write() unconditionally to copy the new state into the handshake. With the patch, exactly the same set of packets reaches down_write(). The only difference is that a packet that lost the race now sees the updated latest_timestamp/last_initiation_consumption and bails out right away, instead of overwriting the handshake state and generating a second response. So the time spent under the write lock goes down for those packets, not up.
Everything else is still filtered under the read lock as before. Once the first copy has stored its timestamp and consumption time, later replays, and any initiation within the next 1/INITIATIONS_PER_SECOND, are rejected at the down_read() fast path and never touch the write lock. Only copies of the same initiation that are processed truly in parallel, so at most one per CPU, can get to down_write(), and that was already the case before.
This is the same pattern the response side uses after 2a73c9b2c0ca:
check under the read lock as a fast path, then check again after down_write() before committing.
Thanks,
Alexander
-------- Исходное сообщение --------
Тема: Re: [PATCH net 2/2] wireguard: noise: recheck replay and flood under write lock on initiation Дата: 10.11.2026 16:51
От: "Jason A. Donenfeld" <[email protected]>
Кому: Alexander Ildiyarov <[email protected]>
Quoted 27 lines · Alexander Ildiyarov
wg_noise_handshake_consume_initiation()checks the initiation's timestamp againstlatest_timestamp, andlast_initiation_consumptionagainst the current time, while holdinghandshake->lockfor reading. It then drops the lock and takes it again for writing to store the new state, without repeating the checks.Incoming handshake packets are spread round-robin over all online CPUs, so two copies of the same initiation, received back to back, are processed in parallel. Both copies can pass the checks before either of them updates
latest_timestampandlast_initiation_consumption, so the replay and flood protection is bypassed. The responder then creates two responses, each with a new ephemeral key, and installs the keypair of the last one, while the initiator accepts whichever response arrives first. When those differ, the session is unusable until the initiator gives up on it and starts a new handshake, and an attacker who can see and duplicate initiations can repeat this for every handshake.The window is narrow, since the expensive DH computations happen before the check, so it is between
up_read()anddown_write()only. With netem duplicating every initiation between two namespaces, no double response was observed in 200 handshakes on a 4-CPU VM. With ausleep_range(1000, 2000)inserted beforedown_write()to widen the window, 30 of 50 duplicated initiations produced two responses, and in 12 of 50 cases the session was unusable after the handshake. With this patch applied, and the same delay, every duplicate was rejected and all 50 sessions worked.
Quoted 2 lines
Fix this by doing the checks again once the write lock is held, keeping the read-locked check as a fast path for rejecting replays.
The downside, though, is that now it means there's potential to do flood attacks that take the exclusive lock, which is what was trying to be avoided, right?