Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tcp: md5: refine tcp_md5_do_add()/tcp_md5_hash_key() barriers

My prior fix went a bit too far, according to Herbert and Mathieu.

Since we accept that concurrent TCP MD5 lookups might see inconsistent
keys, we can use READ_ONCE()/WRITE_ONCE() instead of smp_rmb()/smp_wmb()

Clearing all key->key[] is needed to avoid possible KMSAN reports,
if key->keylen is increased. Since tcp_md5_do_add() is not fast path,
using __GFP_ZERO to clear all struct tcp_md5sig_key is simpler.

data_race() was added in linux-5.8 and will prevent KCSAN reports,
this can safely be removed in stable backports, if data_race() is
not yet backported.

v2: use data_race() both in tcp_md5_hash_key() and tcp_md5_do_add()

Fixes: 6a2febec338d ("tcp: md5: add missing memory barriers in tcp_md5_do_add()/tcp_md5_hash_key()")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Marco Elver <elver@google.com>
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Eric Dumazet and committed by
David S. Miller
e6ced831 1e82a62f

+18 -9
+4 -4
net/ipv4/tcp.c
··· 4033 4033 4034 4034 int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key) 4035 4035 { 4036 - u8 keylen = key->keylen; 4036 + u8 keylen = READ_ONCE(key->keylen); /* paired with WRITE_ONCE() in tcp_md5_do_add */ 4037 4037 struct scatterlist sg; 4038 - 4039 - smp_rmb(); /* paired with smp_wmb() in tcp_md5_do_add() */ 4040 4038 4041 4039 sg_init_one(&sg, key->key, keylen); 4042 4040 ahash_request_set_crypt(hp->md5_req, &sg, NULL, keylen); 4043 - return crypto_ahash_update(hp->md5_req); 4041 + 4042 + /* We use data_race() because tcp_md5_do_add() might change key->key under us */ 4043 + return data_race(crypto_ahash_update(hp->md5_req)); 4044 4044 } 4045 4045 EXPORT_SYMBOL(tcp_md5_hash_key); 4046 4046
+14 -5
net/ipv4/tcp_ipv4.c
··· 1111 1111 1112 1112 key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen, l3index); 1113 1113 if (key) { 1114 - /* Pre-existing entry - just update that one. */ 1115 - memcpy(key->key, newkey, newkeylen); 1114 + /* Pre-existing entry - just update that one. 1115 + * Note that the key might be used concurrently. 1116 + * data_race() is telling kcsan that we do not care of 1117 + * key mismatches, since changing MD5 key on live flows 1118 + * can lead to packet drops. 1119 + */ 1120 + data_race(memcpy(key->key, newkey, newkeylen)); 1116 1121 1117 - smp_wmb(); /* pairs with smp_rmb() in tcp_md5_hash_key() */ 1122 + /* Pairs with READ_ONCE() in tcp_md5_hash_key(). 1123 + * Also note that a reader could catch new key->keylen value 1124 + * but old key->key[], this is the reason we use __GFP_ZERO 1125 + * at sock_kmalloc() time below these lines. 1126 + */ 1127 + WRITE_ONCE(key->keylen, newkeylen); 1118 1128 1119 - key->keylen = newkeylen; 1120 1129 return 0; 1121 1130 } 1122 1131 ··· 1141 1132 rcu_assign_pointer(tp->md5sig_info, md5sig); 1142 1133 } 1143 1134 1144 - key = sock_kmalloc(sk, sizeof(*key), gfp); 1135 + key = sock_kmalloc(sk, sizeof(*key), gfp | __GFP_ZERO); 1145 1136 if (!key) 1146 1137 return -ENOMEM; 1147 1138 if (!tcp_alloc_md5sig_pool()) {