Commit c78a0b4a authored by Jason A. Donenfeld's avatar Jason A. Donenfeld Committed by David S. Miller

wireguard: queueing: preserve flow hash across packet scrubbing

It's important that we clear most header fields during encapsulation and
decapsulation, because the packet is substantially changed, and we don't
want any info leak or logic bug due to an accidental correlation. But,
for encapsulation, it's wrong to clear skb->hash, since it's used by
fq_codel and flow dissection in general. Without it, classification does
not proceed as usual. This change might make it easier to estimate the
number of innerflows by examining clustering of out of order packets,
but this shouldn't open up anything that can't already be inferred
otherwise (e.g. syn packet size inference), and fq_codel can be disabled
anyway.

Furthermore, it might be the case that the hash isn't used or queried at
all until after wireguard transmits the encrypted UDP packet, which
means skb->hash might still be zero at this point, and thus no hash
taken over the inner packet data. In order to address this situation, we
force a calculation of skb->hash before encrypting packet data.

Of course this means that fq_codel might transmit packets slightly more
out of order than usual. Toke did some testing on beefy machines with
high quantities of parallel flows and found that increasing the
reply-attack counter to 8192 takes care of the most pathological cases
pretty well.
Reported-by: default avatarDave Taht <dave.taht@gmail.com>
Reviewed-and-tested-by: default avatarToke Høiland-Jørgensen <toke@toke.dk>
Fixes: e7096c13 ("net: WireGuard secure network tunnel")
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent bc67d371
...@@ -32,7 +32,7 @@ enum cookie_values { ...@@ -32,7 +32,7 @@ enum cookie_values {
}; };
enum counter_values { enum counter_values {
COUNTER_BITS_TOTAL = 2048, COUNTER_BITS_TOTAL = 8192,
COUNTER_REDUNDANT_BITS = BITS_PER_LONG, COUNTER_REDUNDANT_BITS = BITS_PER_LONG,
COUNTER_WINDOW_SIZE = COUNTER_BITS_TOTAL - COUNTER_REDUNDANT_BITS COUNTER_WINDOW_SIZE = COUNTER_BITS_TOTAL - COUNTER_REDUNDANT_BITS
}; };
......
...@@ -87,12 +87,20 @@ static inline bool wg_check_packet_protocol(struct sk_buff *skb) ...@@ -87,12 +87,20 @@ static inline bool wg_check_packet_protocol(struct sk_buff *skb)
return real_protocol && skb->protocol == real_protocol; return real_protocol && skb->protocol == real_protocol;
} }
static inline void wg_reset_packet(struct sk_buff *skb) static inline void wg_reset_packet(struct sk_buff *skb, bool encapsulating)
{ {
u8 l4_hash = skb->l4_hash;
u8 sw_hash = skb->sw_hash;
u32 hash = skb->hash;
skb_scrub_packet(skb, true); skb_scrub_packet(skb, true);
memset(&skb->headers_start, 0, memset(&skb->headers_start, 0,
offsetof(struct sk_buff, headers_end) - offsetof(struct sk_buff, headers_end) -
offsetof(struct sk_buff, headers_start)); offsetof(struct sk_buff, headers_start));
if (encapsulating) {
skb->l4_hash = l4_hash;
skb->sw_hash = sw_hash;
skb->hash = hash;
}
skb->queue_mapping = 0; skb->queue_mapping = 0;
skb->nohdr = 0; skb->nohdr = 0;
skb->peeked = 0; skb->peeked = 0;
......
...@@ -484,7 +484,7 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget) ...@@ -484,7 +484,7 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget)
if (unlikely(wg_socket_endpoint_from_skb(&endpoint, skb))) if (unlikely(wg_socket_endpoint_from_skb(&endpoint, skb)))
goto next; goto next;
wg_reset_packet(skb); wg_reset_packet(skb, false);
wg_packet_consume_data_done(peer, skb, &endpoint); wg_packet_consume_data_done(peer, skb, &endpoint);
free = false; free = false;
......
...@@ -167,6 +167,11 @@ static bool encrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair) ...@@ -167,6 +167,11 @@ static bool encrypt_packet(struct sk_buff *skb, struct noise_keypair *keypair)
struct sk_buff *trailer; struct sk_buff *trailer;
int num_frags; int num_frags;
/* Force hash calculation before encryption so that flow analysis is
* consistent over the inner packet.
*/
skb_get_hash(skb);
/* Calculate lengths. */ /* Calculate lengths. */
padding_len = calculate_skb_padding(skb); padding_len = calculate_skb_padding(skb);
trailer_len = padding_len + noise_encrypted_len(0); trailer_len = padding_len + noise_encrypted_len(0);
...@@ -295,7 +300,7 @@ void wg_packet_encrypt_worker(struct work_struct *work) ...@@ -295,7 +300,7 @@ void wg_packet_encrypt_worker(struct work_struct *work)
skb_list_walk_safe(first, skb, next) { skb_list_walk_safe(first, skb, next) {
if (likely(encrypt_packet(skb, if (likely(encrypt_packet(skb,
PACKET_CB(first)->keypair))) { PACKET_CB(first)->keypair))) {
wg_reset_packet(skb); wg_reset_packet(skb, true);
} else { } else {
state = PACKET_STATE_DEAD; state = PACKET_STATE_DEAD;
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment