Skip to content

Commit 6a2febe

Browse files
edumazetdavem330
authored andcommitted
tcp: md5: add missing memory barriers in tcp_md5_do_add()/tcp_md5_hash_key()
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>
1 parent 28541f3 commit 6a2febe

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎net/ipv4/tcp.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,10 +4033,13 @@ EXPORT_SYMBOL(tcp_md5_hash_skb_data);
40334033

40344034
int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
40354035
{
4036+
u8 keylen = key->keylen;
40364037
struct scatterlist sg;
40374038

4038-
sg_init_one(&sg, key->key, key->keylen);
4039-
ahash_request_set_crypt(hp->md5_req, &sg, NULL, key->keylen);
4039+
smp_rmb(); /* paired with smp_wmb() in tcp_md5_do_add() */
4040+
4041+
sg_init_one(&sg, key->key, keylen);
4042+
ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen);
40404043
return crypto_ahash_update(hp->md5_req);
40414044
}
40424045
EXPORT_SYMBOL(tcp_md5_hash_key);

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,9 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
11131113
if (key) {
11141114
/* Pre-existing entry - just update that one. */
11151115
memcpy(key->key, newkey, newkeylen);
1116+
1117+
smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */
1118+
11161119
key->keylen = newkeylen;
11171120
return 0;
11181121
}

0 commit comments

Comments
 (0)