Skip to content

Commit 67072c3

Browse files
committed
Merge branch 'tcp-rds-fix-use-after-free-around-kernel-tcp-reqsk'
Kuniyuki Iwashima says: ==================== tcp/rds: Fix use-after-free around kernel TCP reqsk. syzkaller reported an warning of netns ref tracker for RDS TCP listener, which commit 740ea3c ("tcp: Clean up kernel listener's reqsk in inet_twsk_purge()") fixed for per-netns ehash. This series fixes the bug in the partial fix and fixes the reported bug in the global ehash. v4: https://lore.kernel.org/netdev/20240307232151.55963-1-kuniyu@amazon.com/ v3: https://lore.kernel.org/netdev/20240307224423.53315-1-kuniyu@amazon.com/ v2: https://lore.kernel.org/netdev/20240227011041.97375-1-kuniyu@amazon.com/ v1: https://lore.kernel.org/netdev/20240223172448.94084-1-kuniyu@amazon.com/ ==================== Link: https://lore.kernel.org/r/20240308200122.64357-1-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 9187210 + 2a750d6 commit 67072c3

2 files changed

Lines changed: 19 additions & 26 deletions

File tree

net/ipv4/inet_timewait_sock.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
263263
}
264264
EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
265265

266+
/* Remove all non full sockets (TIME_WAIT and NEW_SYN_RECV) for dead netns */
266267
void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
267268
{
268-
struct inet_timewait_sock *tw;
269-
struct sock *sk;
270269
struct hlist_nulls_node *node;
271270
unsigned int slot;
271+
struct sock *sk;
272272

273273
for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
274274
struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
@@ -277,38 +277,35 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
277277
rcu_read_lock();
278278
restart:
279279
sk_nulls_for_each_rcu(sk, node, &head->chain) {
280-
if (sk->sk_state != TCP_TIME_WAIT) {
281-
/* A kernel listener socket might not hold refcnt for net,
282-
* so reqsk_timer_handler() could be fired after net is
283-
* freed. Userspace listener and reqsk never exist here.
284-
*/
285-
if (unlikely(sk->sk_state == TCP_NEW_SYN_RECV &&
286-
hashinfo->pernet)) {
287-
struct request_sock *req = inet_reqsk(sk);
288-
289-
inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
290-
}
280+
int state = inet_sk_state_load(sk);
291281

282+
if ((1 << state) & ~(TCPF_TIME_WAIT |
283+
TCPF_NEW_SYN_RECV))
292284
continue;
293-
}
294285

295-
tw = inet_twsk(sk);
296-
if ((tw->tw_family != family) ||
297-
refcount_read(&twsk_net(tw)->ns.count))
286+
if (sk->sk_family != family ||
287+
refcount_read(&sock_net(sk)->ns.count))
298288
continue;
299289

300-
if (unlikely(!refcount_inc_not_zero(&tw->tw_refcnt)))
290+
if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
301291
continue;
302292

303-
if (unlikely((tw->tw_family != family) ||
304-
refcount_read(&twsk_net(tw)->ns.count))) {
305-
inet_twsk_put(tw);
293+
if (unlikely(sk->sk_family != family ||
294+
refcount_read(&sock_net(sk)->ns.count))) {
295+
sock_gen_put(sk);
306296
goto restart;
307297
}
308298

309299
rcu_read_unlock();
310300
local_bh_disable();
311-
inet_twsk_deschedule_put(tw);
301+
if (state == TCP_TIME_WAIT) {
302+
inet_twsk_deschedule_put(inet_twsk(sk));
303+
} else {
304+
struct request_sock *req = inet_reqsk(sk);
305+
306+
inet_csk_reqsk_queue_drop_and_put(req->rsk_listener,
307+
req);
308+
}
312309
local_bh_enable();
313310
goto restart_rcu;
314311
}

net/ipv4/tcp_minisocks.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,6 @@ void tcp_twsk_purge(struct list_head *net_exit_list, int family)
398398
/* Even if tw_refcount == 1, we must clean up kernel reqsk */
399399
inet_twsk_purge(net->ipv4.tcp_death_row.hashinfo, family);
400400
} else if (!purged_once) {
401-
/* The last refcount is decremented in tcp_sk_exit_batch() */
402-
if (refcount_read(&net->ipv4.tcp_death_row.tw_refcount) == 1)
403-
continue;
404-
405401
inet_twsk_purge(&tcp_hashinfo, family);
406402
purged_once = true;
407403
}

0 commit comments

Comments
 (0)