kernel: 5.4: import wireguard backport
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 080-wireguard-0104-wireguard-noise-read-preshared-key-while-taking-lock.patch
1 From 5e29ad069218c486737729f88d15e4fe0ca7eb45 Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Tue, 19 May 2020 22:49:28 -0600
4 Subject: [PATCH 104/124] wireguard: noise: read preshared key while taking
5 lock
6
7 commit bc67d371256f5c47d824e2eec51e46c8d62d022e upstream.
8
9 Prior we read the preshared key after dropping the handshake lock, which
10 isn't an actual crypto issue if it races, but it's still not quite
11 correct. So copy that part of the state into a temporary like we do with
12 the rest of the handshake state variables. Then we can release the lock,
13 operate on the temporary, and zero it out at the end of the function. In
14 performance tests, the impact of this was entirely unnoticable, probably
15 because those bytes are coming from the same cacheline as other things
16 that are being copied out in the same manner.
17
18 Reported-by: Matt Dunwoodie <ncon@noconroy.net>
19 Fixes: e7096c131e51 ("net: WireGuard secure network tunnel")
20 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
21 Signed-off-by: David S. Miller <davem@davemloft.net>
22 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
23 ---
24 drivers/net/wireguard/noise.c | 6 +++++-
25 1 file changed, 5 insertions(+), 1 deletion(-)
26
27 --- a/drivers/net/wireguard/noise.c
28 +++ b/drivers/net/wireguard/noise.c
29 @@ -715,6 +715,7 @@ wg_noise_handshake_consume_response(stru
30 u8 e[NOISE_PUBLIC_KEY_LEN];
31 u8 ephemeral_private[NOISE_PUBLIC_KEY_LEN];
32 u8 static_private[NOISE_PUBLIC_KEY_LEN];
33 + u8 preshared_key[NOISE_SYMMETRIC_KEY_LEN];
34
35 down_read(&wg->static_identity.lock);
36
37 @@ -733,6 +734,8 @@ wg_noise_handshake_consume_response(stru
38 memcpy(chaining_key, handshake->chaining_key, NOISE_HASH_LEN);
39 memcpy(ephemeral_private, handshake->ephemeral_private,
40 NOISE_PUBLIC_KEY_LEN);
41 + memcpy(preshared_key, handshake->preshared_key,
42 + NOISE_SYMMETRIC_KEY_LEN);
43 up_read(&handshake->lock);
44
45 if (state != HANDSHAKE_CREATED_INITIATION)
46 @@ -750,7 +753,7 @@ wg_noise_handshake_consume_response(stru
47 goto fail;
48
49 /* psk */
50 - mix_psk(chaining_key, hash, key, handshake->preshared_key);
51 + mix_psk(chaining_key, hash, key, preshared_key);
52
53 /* {} */
54 if (!message_decrypt(NULL, src->encrypted_nothing,
55 @@ -783,6 +786,7 @@ out:
56 memzero_explicit(chaining_key, NOISE_HASH_LEN);
57 memzero_explicit(ephemeral_private, NOISE_PUBLIC_KEY_LEN);
58 memzero_explicit(static_private, NOISE_PUBLIC_KEY_LEN);
59 + memzero_explicit(preshared_key, NOISE_SYMMETRIC_KEY_LEN);
60 up_read(&wg->static_identity.lock);
61 return ret_peer;
62 }