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

inet: Avoid ehash lookup race in inet_twsk_hashdance_schedule()

Since ehash lookups are lockless, if another CPU is converting sk to tw
concurrently, fetching the newly inserted tw with tw->tw_refcnt == 0 cause
lookup failure.

The call trace map is drawn as follows:
CPU 0 CPU 1
----- -----
inet_twsk_hashdance_schedule()
spin_lock()
inet_twsk_add_node_rcu(tw, ...)
__inet_lookup_established()
(find tw, failure due to tw_refcnt = 0)
__sk_nulls_del_node_init_rcu(sk)
refcount_set(&tw->tw_refcnt, 3)
spin_unlock()

By replacing sk with tw atomically via hlist_nulls_replace_init_rcu() after
setting tw_refcnt, we ensure that tw is either fully initialized or not
visible to other CPUs, eliminating the race.

It's worth noting that we held lock_sock() before the replacement, so
there's no need to check if sk is hashed. Thanks to Kuniyuki Iwashima!

Fixes: 3ab5aee7fe84 ("net: Convert TCP & DCCP hash tables to use RCU / hlist_nulls")
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20251015020236.431822-4-xuanqiang.luo@linux.dev
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Xuanqiang Luo and committed by
Jakub Kicinski
b8ec80b1 1532ed0d

+12 -23
+12 -23
net/ipv4/inet_timewait_sock.c
··· 88 88 } 89 89 EXPORT_SYMBOL_GPL(inet_twsk_put); 90 90 91 - static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, 92 - struct hlist_nulls_head *list) 93 - { 94 - hlist_nulls_add_head_rcu(&tw->tw_node, list); 95 - } 96 - 97 91 static void inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo) 98 92 { 99 93 __inet_twsk_schedule(tw, timeo, false); ··· 107 113 { 108 114 const struct inet_sock *inet = inet_sk(sk); 109 115 const struct inet_connection_sock *icsk = inet_csk(sk); 110 - struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); 111 116 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 112 117 struct inet_bind_hashbucket *bhead, *bhead2; 113 118 114 - /* Step 1: Put TW into bind hash. Original socket stays there too. 115 - Note, that any socket with inet->num != 0 MUST be bound in 116 - binding cache, even if it is closed. 119 + /* Put TW into bind hash. Original socket stays there too. 120 + * Note, that any socket with inet->num != 0 MUST be bound in 121 + * binding cache, even if it is closed. 117 122 */ 118 123 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num, 119 124 hashinfo->bhash_size)]; ··· 134 141 135 142 spin_lock(lock); 136 143 137 - /* Step 2: Hash TW into tcp ehash chain */ 138 - inet_twsk_add_node_rcu(tw, &ehead->chain); 139 - 140 - /* Step 3: Remove SK from hash chain */ 141 - if (__sk_nulls_del_node_init_rcu(sk)) 142 - sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 143 - 144 - 145 - /* Ensure above writes are committed into memory before updating the 146 - * refcount. 147 - * Provides ordering vs later refcount_inc(). 148 - */ 149 - smp_wmb(); 150 144 /* tw_refcnt is set to 3 because we have : 151 145 * - one reference for bhash chain. 152 146 * - one reference for ehash chain. ··· 142 162 * so we are not allowed to use tw anymore. 143 163 */ 144 164 refcount_set(&tw->tw_refcnt, 3); 165 + 166 + /* Ensure tw_refcnt has been set before tw is published. 167 + * smp_wmb() provides the necessary memory barrier to enforce this 168 + * ordering. 169 + */ 170 + smp_wmb(); 171 + 172 + hlist_nulls_replace_init_rcu(&sk->sk_nulls_node, &tw->tw_node); 173 + sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 145 174 146 175 inet_twsk_schedule(tw, timeo); 147 176