Commit 55f02e44 authored by Eric Dumazet's avatar Eric Dumazet Committed by Khalid Elmously

tcp: md5: add missing memory barriers in tcp_md5_do_add()/tcp_md5_hash_key()

BugLink: https://bugs.launchpad.net/bugs/1888690

[ Upstream commit 6a2febec ]

MD5 keys are read with RCU protection, and tcp_md5_do_add()
might update in-place a prior key.

Normally, typical RCU updates would allocate a new piece
of memory. In this case only key->key and key->keylen might
be updated, and we do not care if an incoming packet could
see the old key, the new one, or some intermediate value,
since changing the key on a live flow is known to be problematic
anyway.

We only want to make sure that in the case key->keylen
is changed, cpus in tcp_md5_hash_key() wont try to use
uninitialized data, or crash because key->keylen was
read twice to feed sg_init_one() and ahash_request_set_crypt()

Fixes: 9ea88a15 ("tcp: md5: check md5 signature without socket lock")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarKamal Mostafa <kamal@canonical.com>
Signed-off-by: default avatarKhalid Elmously <khalid.elmously@canonical.com>
parent 08f894c6
...@@ -3088,9 +3088,12 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data); ...@@ -3088,9 +3088,12 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data);
int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key) int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
{ {
u8 keylen = key->keylen;
struct scatterlist sg; struct scatterlist sg;
sg_init_one(&sg, key->key, key->keylen); smp_rmb(); /* paired with smp_wmb() in tcp_md5_do_add() */
sg_init_one(&sg, key->key, keylen);
return crypto_hash_update(&hp->md5_desc, &sg, key->keylen); return crypto_hash_update(&hp->md5_desc, &sg, key->keylen);
} }
EXPORT_SYMBOL(tcp_md5_hash_key); EXPORT_SYMBOL(tcp_md5_hash_key);
......
...@@ -933,6 +933,9 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, ...@@ -933,6 +933,9 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
if (key) { if (key) {
/* Pre-existing entry - just update that one. */ /* Pre-existing entry - just update that one. */
memcpy(key->key, newkey, newkeylen); memcpy(key->key, newkey, newkeylen);
smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */
key->keylen = newkeylen; key->keylen = newkeylen;
return 0; return 0;
} }
......
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