Skip to content

Commit

Permalink
tcp: md5: add missing memory barriers in tcp_md5_do_add()/tcp_md5_has…
Browse files Browse the repository at this point in the history
…h_key()

[ Upstream commit 6a2febe ]

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: 9ea88a1 ("tcp: md5: check md5 signature without socket lock")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Eric Dumazet authored and gregkh committed Jul 22, 2020
1 parent e5ba276 commit d9e26f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions net/ipv4/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3207,10 +3207,13 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data);

int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
{
u8 keylen = key->keylen;
struct scatterlist sg;

sg_init_one(&sg, key->key, key->keylen);
ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen);
smp_rmb(); /* paired with smp_wmb() in tcp_md5_do_add() */

sg_init_one(&sg, key->key, keylen);
ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen);
return crypto_ahash_update(hp->md5_req);
}
EXPORT_SYMBOL(tcp_md5_hash_key);
Expand Down
3 changes: 3 additions & 0 deletions net/ipv4/tcp_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
if (key) {
/* Pre-existing entry - just update that one. */
memcpy(key->key, newkey, newkeylen);

smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */

key->keylen = newkeylen;
return 0;
}
Expand Down

0 comments on commit d9e26f6

Please sign in to comment.