* [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
[not found] <cover.1788248601.git.zihanx@nebusec.ai>
@ 2026-09-01 12:53 ` Zihan Xi
2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-01 12:53 ` [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold Zihan Xi
1 sibling, 1 reply; 10+ messages in thread
From: Zihan Xi @ 2026-09-01 12:53 UTC (permalink / raw)
To: netdev
Cc: Zihan Xi, linux-kernel, mptcp, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Neal Cardwell,
Kuniyuki Iwashima, Matthieu Baerts, Mat Martineau, Geliang Tang,
Guillaume Nault, Florian Westphal, stable, Vega
inet_diag dumps run request-supplied bytecode through inet_diag_bc_sk().
tcp_diag_dump() currently evaluates socket filters and fills replies while
holding the listener, bind, and ehash bucket locks.
The time spent under a bucket lock can therefore grow with the number of
sockets visited and with per-socket dump work. This defeats the intended
bounded nature of the bucket walk and can cause excessive lock hold times.
Fix this by collecting only referenced sockets while holding each bucket
lock. Move the filtering, bytecode evaluation, and fill work out of the
critical section, and keep a referenced dump cursor so each subsequent
batch resumes after the previous socket instead of rescanning the bucket
head. Validate a cursor against the current listener, bind, or ehash
bucket, and against the table implied by sk_state, before resuming from
it. Bind collection counts TIME_WAIT nodes toward the batch limit and
resumes them via tw_tb2.
Fixes: 5caea4ea7088 ("net: listening_hash get a spinlock per bucket")
Fixes: 91051f003948 ("tcp: Dump bound-only sockets in inet_diag.")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Assisted-by: Codex:gpt-5.4
Signed-off-by: Zihan Xi <zihanx@nebusec.ai>
---
changes in v2:
- Rebased onto net commit e2a6641e3bfd (2026-08-27).
- Added current-bucket cursor validation for listener, bind, and ehash
paths, with safe restart on mismatch.
- Reject listen/ehash cursors unless sk_state still matches the table
being walked, so a reused sk_nulls_node cannot continue under the
wrong bucket lock.
- Count TIME_WAIT bind nodes toward SKARR_SZ and resume them via tw_tb2
instead of skipping them under the bind lock.
- Kept listener and bound-only Fixes tags; dropped 7e3aab4a9cd7 because
that commit only converted the existing ehash dump lock type.
- Sorted new listen/bind/ehash local declarations reverse xmas tree.
- Left INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN to the MPTCP patch.
- Moved SKARR_SZ comment off "bh disabled" and aligned the ehash cursor
continuation indent.
- Refreshed the reviewed PoC and decoded crash-log artifacts.
- v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
include/linux/inet_diag.h | 14 ++
include/net/inet_hashtables.h | 18 ++
net/ipv4/inet_diag.c | 13 ++
net/ipv4/inet_hashtables.c | 18 --
net/ipv4/tcp_diag.c | 338 +++++++++++++++++++++++++---------
5 files changed, 294 insertions(+), 107 deletions(-)
diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
index 704fd415c2b4..6ccd32bc48f9 100644
--- a/include/linux/inet_diag.h
+++ b/include/linux/inet_diag.h
@@ -6,6 +6,7 @@
#include <uapi/linux/inet_diag.h>
struct inet_hashinfo;
+struct sock;
struct inet_diag_handler {
struct module *owner;
@@ -32,12 +33,23 @@ struct inet_diag_handler {
};
struct bpf_sk_storage_diag;
+
+enum inet_diag_dump_cursor_type {
+ INET_DIAG_DUMP_CURSOR_NONE,
+ INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
+ INET_DIAG_DUMP_CURSOR_TCP_BIND,
+ INET_DIAG_DUMP_CURSOR_TCP_EHASH,
+};
+
struct inet_diag_dump_data {
struct nlattr *req_nlas[__INET_DIAG_REQ_MAX];
#define inet_diag_nla_bc req_nlas[INET_DIAG_REQ_BYTECODE]
#define inet_diag_nla_bpf_stgs req_nlas[INET_DIAG_REQ_SK_BPF_STORAGES]
struct bpf_sk_storage_diag *bpf_stg_diag;
+ struct sock *dump_cursor;
+ unsigned int dump_cursor_slot;
+ u8 dump_cursor_type;
bool mark_needed; /* INET_DIAG_BC_MARK_COND present. */
#ifdef CONFIG_SOCK_CGROUP_DATA
bool cgroup_needed; /* INET_DIAG_BC_CGROUP_COND present. */
@@ -53,6 +65,8 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
int inet_diag_bc_sk(const struct inet_diag_dump_data *cb_data, struct sock *sk);
+void inet_diag_dump_clear_cursor(struct inet_diag_dump_data *cb_data);
+
void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk);
static inline size_t inet_diag_msg_attrs_size(void)
diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
index 6e2fe186d0dc..d95639ac70c6 100644
--- a/include/net/inet_hashtables.h
+++ b/include/net/inet_hashtables.h
@@ -188,6 +188,24 @@ inet_lhash2_bucket(struct inet_hashinfo *h, u32 hash)
return &h->lhash2[hash & h->lhash2_mask];
}
+static inline struct inet_listen_hashbucket *
+inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
+{
+ u32 hash;
+
+#if IS_ENABLED(CONFIG_IPV6)
+ if (sk->sk_family == AF_INET6)
+ hash = ipv6_portaddr_hash(sock_net(sk),
+ &sk->sk_v6_rcv_saddr,
+ inet_sk(sk)->inet_num);
+ else
+#endif
+ hash = ipv4_portaddr_hash(sock_net(sk),
+ inet_sk(sk)->inet_rcv_saddr,
+ inet_sk(sk)->inet_num);
+ return inet_lhash2_bucket(h, hash);
+}
+
static inline struct inet_ehash_bucket *inet_ehash_bucket(
struct inet_hashinfo *hashinfo,
unsigned int hash)
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 34b77aa87d0a..41148e880054 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -891,10 +891,23 @@ static int inet_diag_dump_start_compat(struct netlink_callback *cb)
return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
}
+void inet_diag_dump_clear_cursor(struct inet_diag_dump_data *cb_data)
+{
+ if (!cb_data->dump_cursor)
+ return;
+
+ sock_gen_put(cb_data->dump_cursor);
+ cb_data->dump_cursor = NULL;
+ cb_data->dump_cursor_slot = 0;
+ cb_data->dump_cursor_type = INET_DIAG_DUMP_CURSOR_NONE;
+}
+EXPORT_SYMBOL_GPL(inet_diag_dump_clear_cursor);
+
static int inet_diag_dump_done(struct netlink_callback *cb)
{
struct inet_diag_dump_data *cb_data = cb->data;
+ inet_diag_dump_clear_cursor(cb_data);
bpf_sk_storage_diag_free(cb_data->bpf_stg_diag);
kfree(cb->data);
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index ba0faa9ae2bb..1c839fe3d7e0 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -331,24 +331,6 @@ int __inet_inherit_port(const struct sock *sk, struct sock *child)
return -ENOMEM;
}
-static struct inet_listen_hashbucket *
-inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
-{
- u32 hash;
-
-#if IS_ENABLED(CONFIG_IPV6)
- if (sk->sk_family == AF_INET6)
- hash = ipv6_portaddr_hash(sock_net(sk),
- &sk->sk_v6_rcv_saddr,
- inet_sk(sk)->inet_num);
- else
-#endif
- hash = ipv4_portaddr_hash(sock_net(sk),
- inet_sk(sk)->inet_rcv_saddr,
- inet_sk(sk)->inet_num);
- return inet_lhash2_bucket(h, hash);
-}
-
static inline int compute_score(struct sock *sk, const struct net *net,
const unsigned short hnum, const __be32 daddr,
const int dif, const int sdif)
diff --git a/net/ipv4/tcp_diag.c b/net/ipv4/tcp_diag.c
index ba1fdbe9807f..842e13ee79e5 100644
--- a/net/ipv4/tcp_diag.c
+++ b/net/ipv4/tcp_diag.c
@@ -285,6 +285,73 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
net_admin);
}
+/* Process a maximum of SKARR_SZ sockets at a time when walking hash buckets
+ * while holding a bucket lock.
+ */
+#define SKARR_SZ 16
+
+static void tcp_diag_save_cursor(struct inet_diag_dump_data *cb_data, int type,
+ unsigned int slot, struct sock *sk)
+{
+ sock_hold(sk);
+ inet_diag_dump_clear_cursor(cb_data);
+ cb_data->dump_cursor = sk;
+ cb_data->dump_cursor_slot = slot;
+ cb_data->dump_cursor_type = type;
+}
+
+static struct inet_bind2_bucket *tcp_diag_sk_bind2(const struct sock *sk)
+{
+ if (sk->sk_state == TCP_TIME_WAIT)
+ return inet_twsk(sk)->tw_tb2;
+
+ return inet_csk(sk)->icsk_bind2_hash;
+}
+
+static bool tcp_diag_bind_collect_sock(struct sock *sk, struct sock **sk_arr,
+ int *num_arr, int *accum, int num)
+{
+ sock_hold(sk);
+ num_arr[*accum] = num;
+ sk_arr[*accum] = sk;
+
+ return ++*accum == SKARR_SZ;
+}
+
+static bool tcp_diag_bind_collect_owners(struct hlist_head *owners,
+ struct sock **sk_arr, int *num_arr,
+ int *accum, int *num, int s_num)
+{
+ struct sock *sk;
+
+ sk_for_each_bound(sk, owners) {
+ if (*num < s_num) {
+ (*num)++;
+ continue;
+ }
+
+ if (tcp_diag_bind_collect_sock(sk, sk_arr, num_arr, accum, *num))
+ return true;
+ (*num)++;
+ }
+
+ return false;
+}
+
+static bool tcp_diag_bind_collect_owners_continue(struct sock *sk,
+ struct sock **sk_arr,
+ int *num_arr, int *accum,
+ int *num)
+{
+ hlist_for_each_entry_continue(sk, sk_bind_node) {
+ if (tcp_diag_bind_collect_sock(sk, sk_arr, num_arr, accum, *num))
+ return true;
+ (*num)++;
+ }
+
+ return false;
+}
+
static void twsk_build_assert(void)
{
BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
@@ -335,8 +402,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
struct inet_listen_hashbucket *ilb;
struct hlist_nulls_node *node;
+ struct sock *sk_arr[SKARR_SZ];
+ int num_arr[SKARR_SZ];
+ struct sock *cursor;
+ int idx, accum, res;
+ bool use_cursor;
+resume_listen_walk:
num = 0;
+ accum = 0;
ilb = &hashinfo->lhash2[i];
if (hlist_nulls_empty(&ilb->nulls_head)) {
@@ -344,52 +418,81 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
continue;
}
spin_lock(&ilb->lock);
- sk_nulls_for_each(sk, node, &ilb->nulls_head) {
- struct inet_sock *inet = inet_sk(sk);
+ cursor = cb_data->dump_cursor;
+ use_cursor = cursor &&
+ cb_data->dump_cursor_type ==
+ INET_DIAG_DUMP_CURSOR_TCP_LISTEN &&
+ cb_data->dump_cursor_slot == i &&
+ inet_sk_state_load(cursor) == TCP_LISTEN &&
+ !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
+ cursor->sk_nulls_node.pprev != LIST_POISON2 &&
+ inet_lhash2_bucket_sk(hashinfo, cursor) == ilb;
+ node = use_cursor ? cursor->sk_nulls_node.next :
+ ilb->nulls_head.first;
+ if (!use_cursor)
+ s_num = 0;
+ hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
- if (!net_eq(sock_net(sk), net))
- continue;
+ sock_hold(sk);
+ num_arr[accum] = num;
+ sk_arr[accum] = sk;
+ if (++accum == SKARR_SZ)
+ break;
- if (num < s_num) {
- num++;
- continue;
- }
+ ++num;
+ }
+ spin_unlock(&ilb->lock);
+
+ res = 0;
+ for (idx = 0; idx < accum; idx++) {
+ struct inet_sock *inet;
+
+ sk = sk_arr[idx];
+ if (!net_eq(sock_net(sk), net))
+ goto processed_listen_sk;
+ inet = inet_sk(sk);
if (r->sdiag_family != AF_UNSPEC &&
sk->sk_family != r->sdiag_family)
- goto next_listen;
+ goto processed_listen_sk;
if (r->id.idiag_sport != inet->inet_sport &&
r->id.idiag_sport)
- goto next_listen;
-
- if (!inet_diag_bc_sk(cb_data, sk))
- goto next_listen;
+ goto processed_listen_sk;
- if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
- cb, r, NLM_F_MULTI,
- net_admin) < 0) {
- spin_unlock(&ilb->lock);
- goto done;
+ if (res >= 0 && inet_diag_bc_sk(cb_data, sk)) {
+ res = inet_sk_diag_fill(sk, inet_csk(sk),
+ skb, cb, r, NLM_F_MULTI,
+ net_admin);
+ if (res < 0)
+ num = num_arr[idx];
}
+processed_listen_sk:
+ if (res >= 0)
+ tcp_diag_save_cursor(cb_data,
+ INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
+ i, sk);
+ sock_put(sk);
+ }
+ if (res < 0)
+ goto done;
-next_listen:
- ++num;
+ cond_resched();
+
+ if (accum == SKARR_SZ) {
+ s_num = 0;
+ goto resume_listen_walk;
}
- spin_unlock(&ilb->lock);
+ inet_diag_dump_clear_cursor(cb_data);
s_num = 0;
}
skip_listen_ht:
+ inet_diag_dump_clear_cursor(cb_data);
cb->args[0] = 1;
s_i = num = s_num = 0;
}
-/* Process a maximum of SKARR_SZ sockets at a time when walking hash buckets
- * with bh disabled.
- */
-#define SKARR_SZ 16
-
/* Dump bound but inactive (not listening, connecting, etc.) sockets */
if (cb->args[0] == 1) {
if (!(idiag_states & TCPF_BOUND_INACTIVE))
@@ -400,7 +503,9 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
struct inet_bind2_bucket *tb2;
struct sock *sk_arr[SKARR_SZ];
int num_arr[SKARR_SZ];
+ struct sock *cursor;
int idx, accum, res;
+ bool use_cursor;
resume_bind_walk:
num = 0;
@@ -412,34 +517,46 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
continue;
}
spin_lock_bh(&ibb->lock);
- inet_bind_bucket_for_each(tb2, &ibb->chain) {
- if (!net_eq(ib2_net(tb2), net))
- continue;
-
- sk_for_each_bound(sk, &tb2->owners) {
- struct inet_sock *inet = inet_sk(sk);
-
- if (num < s_num)
- goto next_bind;
-
- if (sk->sk_state != TCP_CLOSE ||
- !inet->inet_num)
- goto next_bind;
-
- if (r->sdiag_family != AF_UNSPEC &&
- r->sdiag_family != sk->sk_family)
- goto next_bind;
-
- if (!inet_diag_bc_sk(cb_data, sk))
- goto next_bind;
-
- sock_hold(sk);
- num_arr[accum] = num;
- sk_arr[accum] = sk;
- if (++accum == SKARR_SZ)
+ cursor = cb_data->dump_cursor;
+ use_cursor = cursor &&
+ cb_data->dump_cursor_type ==
+ INET_DIAG_DUMP_CURSOR_TCP_BIND &&
+ cb_data->dump_cursor_slot == i &&
+ !hlist_unhashed(&cursor->sk_bind_node) &&
+ cursor->sk_bind_node.pprev != LIST_POISON2;
+ if (use_cursor) {
+ tb2 = tcp_diag_sk_bind2(cursor);
+ use_cursor = tb2 &&
+ inet_bhashfn_portaddr(hashinfo, cursor,
+ sock_net(cursor),
+ inet_sk(cursor)->inet_num) ==
+ ibb;
+ }
+ if (!use_cursor)
+ s_num = 0;
+ if (use_cursor) {
+ sk = cursor;
+ if (tcp_diag_bind_collect_owners_continue(sk, sk_arr,
+ num_arr,
+ &accum,
+ &num))
+ goto pause_bind_walk;
+ hlist_for_each_entry_continue(tb2, node) {
+ if (tcp_diag_bind_collect_owners(&tb2->owners,
+ sk_arr,
+ num_arr,
+ &accum,
+ &num, 0))
+ goto pause_bind_walk;
+ }
+ } else {
+ inet_bind_bucket_for_each(tb2, &ibb->chain) {
+ if (tcp_diag_bind_collect_owners(&tb2->owners,
+ sk_arr,
+ num_arr,
+ &accum,
+ &num, s_num))
goto pause_bind_walk;
-next_bind:
- num++;
}
}
pause_bind_walk:
@@ -447,15 +564,33 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
res = 0;
for (idx = 0; idx < accum; idx++) {
- if (res >= 0) {
- res = inet_sk_diag_fill(sk_arr[idx],
- NULL, skb, cb,
+ struct inet_sock *inet;
+
+ sk = sk_arr[idx];
+ if (!net_eq(sock_net(sk), net))
+ goto put_bind_sk;
+
+ inet = inet_sk(sk);
+ if (sk->sk_state != TCP_CLOSE || !inet->inet_num)
+ goto put_bind_sk;
+
+ if (r->sdiag_family != AF_UNSPEC &&
+ r->sdiag_family != sk->sk_family)
+ goto put_bind_sk;
+
+ if (res >= 0 && inet_diag_bc_sk(cb_data, sk)) {
+ res = inet_sk_diag_fill(sk, NULL, skb, cb,
r, NLM_F_MULTI,
net_admin);
if (res < 0)
num = num_arr[idx];
}
- sock_put(sk_arr[idx]);
+put_bind_sk:
+ if (res >= 0)
+ tcp_diag_save_cursor(cb_data,
+ INET_DIAG_DUMP_CURSOR_TCP_BIND,
+ i, sk);
+ sock_gen_put(sk);
}
if (res < 0)
goto done;
@@ -463,13 +598,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
cond_resched();
if (accum == SKARR_SZ) {
- s_num = num + 1;
+ s_num = 0;
goto resume_bind_walk;
}
+ inet_diag_dump_clear_cursor(cb_data);
s_num = 0;
}
skip_bind_ht:
+ inet_diag_dump_clear_cursor(cb_data);
cb->args[0] = 2;
s_i = num = s_num = 0;
}
@@ -483,43 +620,35 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
struct hlist_nulls_node *node;
struct sock *sk_arr[SKARR_SZ];
int num_arr[SKARR_SZ];
+ struct sock *cursor;
int idx, accum, res;
+ bool use_cursor;
if (hlist_nulls_empty(&head->chain))
continue;
- if (i > s_i)
+ if (i > s_i) {
+ inet_diag_dump_clear_cursor(cb_data);
s_num = 0;
+ }
next_chunk:
num = 0;
accum = 0;
spin_lock_bh(lock);
- sk_nulls_for_each(sk, node, &head->chain) {
- int state;
-
- if (!net_eq(sock_net(sk), net))
- continue;
- if (num < s_num)
- goto next_normal;
- state = (sk->sk_state == TCP_TIME_WAIT) ?
- READ_ONCE(inet_twsk(sk)->tw_substate) : sk->sk_state;
- if (!(idiag_states & (1 << state)))
- goto next_normal;
- if (r->sdiag_family != AF_UNSPEC &&
- sk->sk_family != r->sdiag_family)
- goto next_normal;
- if (r->id.idiag_sport != htons(READ_ONCE(sk->sk_num)) &&
- r->id.idiag_sport)
- goto next_normal;
- if (r->id.idiag_dport != sk->sk_dport &&
- r->id.idiag_dport)
- goto next_normal;
- twsk_build_assert();
-
- if (!inet_diag_bc_sk(cb_data, sk))
- goto next_normal;
-
+ cursor = cb_data->dump_cursor;
+ use_cursor = cursor &&
+ cb_data->dump_cursor_type ==
+ INET_DIAG_DUMP_CURSOR_TCP_EHASH &&
+ cb_data->dump_cursor_slot == i &&
+ inet_sk_state_load(cursor) != TCP_LISTEN &&
+ !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
+ cursor->sk_nulls_node.pprev != LIST_POISON2 &&
+ inet_ehash_bucket(hashinfo, cursor->sk_hash) == head;
+ node = use_cursor ? cursor->sk_nulls_node.next : head->chain.first;
+ if (!use_cursor)
+ s_num = 0;
+ hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
if (!refcount_inc_not_zero(&sk->sk_refcnt))
goto next_normal;
@@ -534,13 +663,42 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
res = 0;
for (idx = 0; idx < accum; idx++) {
- if (res >= 0) {
- res = sk_diag_fill(sk_arr[idx], skb, cb, r,
- NLM_F_MULTI, net_admin);
+ int state;
+
+ sk = sk_arr[idx];
+ if (!net_eq(sock_net(sk), net))
+ goto put_estab_sk;
+
+ state = (sk->sk_state == TCP_TIME_WAIT) ?
+ READ_ONCE(inet_twsk(sk)->tw_substate) : sk->sk_state;
+ if (!(idiag_states & (1 << state)))
+ goto put_estab_sk;
+
+ if (r->sdiag_family != AF_UNSPEC &&
+ sk->sk_family != r->sdiag_family)
+ goto put_estab_sk;
+
+ if (r->id.idiag_sport != htons(READ_ONCE(sk->sk_num)) &&
+ r->id.idiag_sport)
+ goto put_estab_sk;
+
+ if (r->id.idiag_dport != sk->sk_dport &&
+ r->id.idiag_dport)
+ goto put_estab_sk;
+
+ twsk_build_assert();
+ if (res >= 0 && inet_diag_bc_sk(cb_data, sk)) {
+ res = sk_diag_fill(sk, skb, cb, r, NLM_F_MULTI,
+ net_admin);
if (res < 0)
num = num_arr[idx];
}
- sock_gen_put(sk_arr[idx]);
+put_estab_sk:
+ if (res >= 0)
+ tcp_diag_save_cursor(cb_data,
+ INET_DIAG_DUMP_CURSOR_TCP_EHASH,
+ i, sk);
+ sock_gen_put(sk);
}
if (res < 0)
break;
@@ -548,9 +706,11 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
cond_resched();
if (accum == SKARR_SZ) {
- s_num = num + 1;
+ s_num = 0;
goto next_chunk;
}
+
+ inet_diag_dump_clear_cursor(cb_data);
}
done:
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold
[not found] <cover.1788248601.git.zihanx@nebusec.ai>
2026-09-01 12:53 ` [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump() Zihan Xi
@ 2026-09-01 12:53 ` Zihan Xi
2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-07 7:09 ` Eric Dumazet
1 sibling, 2 replies; 10+ messages in thread
From: Zihan Xi @ 2026-09-01 12:53 UTC (permalink / raw)
To: netdev
Cc: Zihan Xi, linux-kernel, mptcp, David S . Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni, Simon Horman, Neal Cardwell,
Kuniyuki Iwashima, Matthieu Baerts, Mat Martineau, Geliang Tang,
Guillaume Nault, Florian Westphal, stable, Vega
MPTCP listener diag dumping reuses sk_diag_dump(), which executes
inet_diag_bc_sk() before filling the netlink reply. The listener walk in
mptcp_diag_dump_listeners() currently performs that work while holding the
listener bucket lock.
The time spent under the listener bucket lock can therefore grow with the
number of sockets visited and with per-socket dump work. The resume state
also requires later batches to revisit the bucket prefix.
Fix this by collecting only referenced listener sockets while holding the
bucket lock. After dropping it, re-check the listener properties, obtain
the parent MPTCP socket reference, and call sk_diag_dump(). Keep a
referenced cursor so later batches resume after the previous listener
instead of rescanning the bucket head. Validate a cursor against the
current listener bucket and TCP_LISTEN state before resuming from it.
After dropping the lock, read icsk_ulp_data with rcu_dereference().
Fixes: 4fa39b701ce9 ("mptcp: listen diag dump support")
Cc: stable@vger.kernel.org
Reported-by: Vega <vega@nebusec.ai>
Assisted-by: Codex:gpt-5.4
Signed-off-by: Zihan Xi <zihanx@nebusec.ai>
---
changes in v2:
- Rebased onto net commit e2a6641e3bfd (2026-08-27).
- Added current-bucket cursor validation and safe restart.
- Reject MPTCP listener cursors unless the socket is still TCP_LISTEN.
- Read icsk_ulp_data with rcu_dereference() after dropping the listener
lock.
- Moved INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN into this patch.
- Moved MPTCP_DIAG_BULK_SZ below the includes and sorted new local
declarations reverse xmas tree.
- Refreshed the reviewed PoC and decoded crash-log artifacts.
- v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
include/linux/inet_diag.h | 1 +
net/mptcp/mptcp_diag.c | 124 +++++++++++++++++++++++++++-----------
2 files changed, 89 insertions(+), 36 deletions(-)
diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
index 6ccd32bc48f9..4859e77a28c7 100644
--- a/include/linux/inet_diag.h
+++ b/include/linux/inet_diag.h
@@ -39,6 +39,7 @@ enum inet_diag_dump_cursor_type {
INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
INET_DIAG_DUMP_CURSOR_TCP_BIND,
INET_DIAG_DUMP_CURSOR_TCP_EHASH,
+ INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN,
};
struct inet_diag_dump_data {
diff --git a/net/mptcp/mptcp_diag.c b/net/mptcp/mptcp_diag.c
index 136c2d05c0ee..37b33ea5d79d 100644
--- a/net/mptcp/mptcp_diag.c
+++ b/net/mptcp/mptcp_diag.c
@@ -12,6 +12,19 @@
#include <net/netlink.h>
#include "protocol.h"
+/* Process a bounded number of listeners per bucket lock hold. */
+#define MPTCP_DIAG_BULK_SZ 16
+
+static void mptcp_diag_save_cursor(struct inet_diag_dump_data *cb_data,
+ unsigned int slot, struct sock *sk)
+{
+ sock_hold(sk);
+ inet_diag_dump_clear_cursor(cb_data);
+ cb_data->dump_cursor = sk;
+ cb_data->dump_cursor_slot = slot;
+ cb_data->dump_cursor_type = INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN;
+}
+
static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
struct netlink_callback *cb,
const struct inet_diag_req_v2 *req,
@@ -77,6 +90,7 @@ static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callba
bool net_admin)
{
struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
+ struct inet_diag_dump_data *cb_data = cb->data;
struct net *net = sock_net(skb->sk);
struct inet_hashinfo *hinfo;
int i;
@@ -84,64 +98,102 @@ static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callba
hinfo = net->ipv4.tcp_death_row.hashinfo;
for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
+ struct sock *tmp, *sk, *sk_arr[MPTCP_DIAG_BULK_SZ];
struct inet_listen_hashbucket *ilb;
+ int num_arr[MPTCP_DIAG_BULK_SZ];
struct hlist_nulls_node *node;
- struct sock *sk;
- int num = 0;
+ int accum, idx, num, ret;
+ struct sock *cursor;
+ bool use_cursor;
+resume_listen_walk:
+ num = 0;
+ accum = 0;
ilb = &hinfo->lhash2[i];
+ ret = 0;
rcu_read_lock();
spin_lock(&ilb->lock);
- sk_nulls_for_each(sk, node, &ilb->nulls_head) {
- const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
- struct inet_sock *inet = inet_sk(sk);
- int ret;
-
- if (num < diag_ctx->l_num)
- goto next_listen;
-
- if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
- goto next_listen;
-
- sk = ctx->conn;
- if (!sk || !net_eq(sock_net(sk), net))
- goto next_listen;
-
- if (r->sdiag_family != AF_UNSPEC &&
- sk->sk_family != r->sdiag_family)
- goto next_listen;
-
- if (r->id.idiag_sport != inet->inet_sport &&
- r->id.idiag_sport)
+ cursor = cb_data->dump_cursor;
+ use_cursor = cursor &&
+ cb_data->dump_cursor_type ==
+ INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN &&
+ cb_data->dump_cursor_slot == i &&
+ inet_sk_state_load(cursor) == TCP_LISTEN &&
+ !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
+ cursor->sk_nulls_node.pprev != LIST_POISON2 &&
+ inet_lhash2_bucket_sk(hinfo, cursor) == ilb;
+ node = use_cursor ? cursor->sk_nulls_node.next :
+ ilb->nulls_head.first;
+ hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
+ if (!use_cursor && num < diag_ctx->l_num)
goto next_listen;
if (!refcount_inc_not_zero(&sk->sk_refcnt))
goto next_listen;
- ret = sk_diag_dump(sk, skb, cb, r, net_admin);
-
- sock_put(sk);
-
- if (ret < 0) {
- spin_unlock(&ilb->lock);
- rcu_read_unlock();
- diag_ctx->l_slot = i;
- diag_ctx->l_num = num;
- return;
- }
- diag_ctx->l_num = num + 1;
- num = 0;
+ num_arr[accum] = num;
+ sk_arr[accum] = sk;
+ if (++accum == MPTCP_DIAG_BULK_SZ)
+ break;
next_listen:
++num;
}
spin_unlock(&ilb->lock);
rcu_read_unlock();
+ for (idx = 0; idx < accum; idx++) {
+ const struct mptcp_subflow_context *ctx;
+ const struct tcp_ulp_ops *ulp_ops;
+ struct inet_sock *inet;
+
+ sk = sk_arr[idx];
+ rcu_read_lock();
+ ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
+ ulp_ops = READ_ONCE(inet_csk(sk)->icsk_ulp_ops);
+ inet = inet_sk(sk);
+ tmp = ctx ? ctx->conn : NULL;
+ if (!ctx || !ulp_ops || strcmp(ulp_ops->name, "mptcp") ||
+ !tmp || !net_eq(sock_net(tmp), net) ||
+ (r->sdiag_family != AF_UNSPEC &&
+ tmp->sk_family != r->sdiag_family) ||
+ (r->id.idiag_sport != inet->inet_sport &&
+ r->id.idiag_sport) ||
+ !refcount_inc_not_zero(&tmp->sk_refcnt)) {
+ rcu_read_unlock();
+ goto processed_listener_sk;
+ }
+ rcu_read_unlock();
+ if (ret >= 0) {
+ ret = sk_diag_dump(tmp, skb, cb, r, net_admin);
+ if (ret < 0)
+ num = num_arr[idx];
+ }
+ sock_put(tmp);
+processed_listener_sk:
+ if (ret >= 0)
+ mptcp_diag_save_cursor(cb_data, i, sk);
+ sock_put(sk);
+ }
+
+ if (ret < 0) {
+ diag_ctx->l_slot = i;
+ diag_ctx->l_num = num;
+ return;
+ }
+
cond_resched();
+
+ if (accum == MPTCP_DIAG_BULK_SZ) {
+ diag_ctx->l_num = 0;
+ goto resume_listen_walk;
+ }
+
+ inet_diag_dump_clear_cursor(cb_data);
diag_ctx->l_num = 0;
}
+ inet_diag_dump_clear_cursor(cb_data);
diag_ctx->l_num = 0;
diag_ctx->l_slot = i;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
2026-09-01 12:53 ` [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump() Zihan Xi
@ 2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-07 4:48 ` zihan xi
0 siblings, 1 reply; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-09-07 4:22 UTC (permalink / raw)
To: zihanx
Cc: netdev, linux-kernel, mptcp, davem, edumazet, kuba, pabeni, horms,
ncardwell, kuniyu, matttbe, martineau, geliang, gnault, fw,
stable, vega
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider:
Critical: 2 · High: 1 · Medium: 2 · Low: 1
- [Critical] Use-after-free / wild-pointer traversal: the new bind-hash
dump cursor revalidation in tcp_diag_dump() cannot detect that the…
- [Critical] Refcount initialization race: tcp_diag_bind_collect_sock()
calls sock_hold() unconditionally on every node of tb2->owners,…
- [High] Loss of dump forward progress and duplicate output: the patch
deletes every positional (`s_num`) resume path in tcp_diag_dump()…
- [Medium] Lockless, unordered reads of cursor state: the listen and
ehash cursor validations establish "this socket is in the bucket I…
- [Medium] The commit message mischaracterizes the pre-existing defect.
- [Low] Concern that the sibling MPTCP listener dump
(mptcp_diag_dump_listeners()) keeps evaluating request-supplied…
--
tcp: diag: bound bucket lock hold in tcp_diag_dump()
This reworks the three tcp_diag_dump() hash walks so that only referenced
sockets are collected under each bucket lock, and moves the family/port
filters, inet_diag_bc_sk() bytecode evaluation and the fill work outside the
critical section. A referenced dump cursor is added to
struct inet_diag_dump_data so a following batch resumes after the previously
reported socket, with per-table validation of the cursor.
v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
> inet_diag dumps run request-supplied bytecode through inet_diag_bc_sk().
> tcp_diag_dump() currently evaluates socket filters and fills replies while
> holding the listener, bind, and ehash bucket locks.
[Severity: Medium]
Is this description accurate for all three tables?
At the baseline (544d85de4dc2) only the listener walk fills replies under
ilb->lock. The bind walk already collected at most SKARR_SZ = 16 referenced
sockets under spin_lock_bh(&ibb->lock) and only called inet_sk_diag_fill()
after the lock was dropped:
pause_bind_walk:
spin_unlock_bh(&ibb->lock);
res = 0;
for (idx = 0; idx < accum; idx++) {
if (res >= 0) {
res = inet_sk_diag_fill(sk_arr[idx],
NULL, skb, cb,
r, NLM_F_MULTI,
net_admin);
The ehash walk did the same with sk_diag_fill() after spin_unlock_bh(lock).
What remained under the bind and ehash locks was inet_diag_bc_sk() plus the
cheap family/port comparisons, bounded to 16 sockets per lock hold.
Could the changelog be narrowed to the listener walk, or state explicitly
what part of the bind/ehash work it is moving out?
> diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
> index 704fd415c2b49..6ccd32bc48f91 100644
> --- a/include/linux/inet_diag.h
> +++ b/include/linux/inet_diag.h
[ ... ]
> @@ -32,12 +33,23 @@ struct inet_diag_handler {
> };
>
> struct bpf_sk_storage_diag;
> +
> +enum inet_diag_dump_cursor_type {
> + INET_DIAG_DUMP_CURSOR_NONE,
> + INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
> + INET_DIAG_DUMP_CURSOR_TCP_BIND,
> + INET_DIAG_DUMP_CURSOR_TCP_EHASH,
> +};
> +
[Severity: Low]
This isn't a bug, but a note for backporters. The sibling MPTCP listener
dump mptcp_diag_dump_listeners() is converted by the follow-up patch in this
series (INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN, which is why the enum slot and
the exported inet_diag_dump_clear_cursor() appear here), so at the tip of the
series the MPTCP listener walk also drops ilb->lock before calling
sk_diag_dump()/inet_diag_bc_sk().
This patch is Cc: stable, and the commit message body does not mention that
the MPTCP listener walk keeps running request-supplied bytecode under the
same hashinfo->lhash2 bucket lock until the follow-up patch is applied. Only
the v2 changelog, which is stripped on apply, mentions "Left
INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN to the MPTCP patch". Could that
dependency be stated in the commit message?
[ ... ]
> diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
> index 6e2fe186d0dcb..d95639ac70c67 100644
> --- a/include/net/inet_hashtables.h
> +++ b/include/net/inet_hashtables.h
> @@ -188,6 +188,24 @@ inet_lhash2_bucket(struct inet_hashinfo *h, u32 hash)
> return &h->lhash2[hash & h->lhash2_mask];
> }
>
> +static inline struct inet_listen_hashbucket *
> +inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
> +{
> + u32 hash;
> +
> +#if IS_ENABLED(CONFIG_IPV6)
> + if (sk->sk_family == AF_INET6)
> + hash = ipv6_portaddr_hash(sock_net(sk),
> + &sk->sk_v6_rcv_saddr,
> + inet_sk(sk)->inet_num);
> + else
> +#endif
> + hash = ipv4_portaddr_hash(sock_net(sk),
> + inet_sk(sk)->inet_rcv_saddr,
> + inet_sk(sk)->inet_num);
> + return inet_lhash2_bucket(h, hash);
> +}
> +
[ ... ]
> diff --git a/net/ipv4/tcp_diag.c b/net/ipv4/tcp_diag.c
> index ba1fdbe9807fd..842e13ee79e5b 100644
> --- a/net/ipv4/tcp_diag.c
> +++ b/net/ipv4/tcp_diag.c
> @@ -285,6 +285,73 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
> net_admin);
> }
>
[ ... ]
> +static struct inet_bind2_bucket *tcp_diag_sk_bind2(const struct sock *sk)
> +{
> + if (sk->sk_state == TCP_TIME_WAIT)
> + return inet_twsk(sk)->tw_tb2;
> +
> + return inet_csk(sk)->icsk_bind2_hash;
> +}
> +
> +static bool tcp_diag_bind_collect_sock(struct sock *sk, struct sock **sk_arr,
> + int *num_arr, int *accum, int num)
> +{
> + sock_hold(sk);
> + num_arr[*accum] = num;
> + sk_arr[*accum] = sk;
> +
> + return ++*accum == SKARR_SZ;
> +}
> +
> +static bool tcp_diag_bind_collect_owners(struct hlist_head *owners,
> + struct sock **sk_arr, int *num_arr,
> + int *accum, int *num, int s_num)
> +{
> + struct sock *sk;
> +
> + sk_for_each_bound(sk, owners) {
> + if (*num < s_num) {
> + (*num)++;
> + continue;
> + }
> +
> + if (tcp_diag_bind_collect_sock(sk, sk_arr, num_arr, accum, *num))
> + return true;
> + (*num)++;
> + }
> +
> + return false;
> +}
[Severity: Critical]
Can the unconditional sock_hold() in tcp_diag_bind_collect_sock() hit a
TIME_WAIT socket whose tw_refcnt is still 0?
The changelog says "Bind collection counts TIME_WAIT nodes toward the batch
limit", and tcp_diag_bind_collect_owners() no longer filters on
sk->sk_state != TCP_CLOSE before taking the reference, so every node of
tb2->owners now gets a refcount_inc().
inet_twsk_alloc() starts the object at zero on purpose:
/*
* Because we use RCU lookups, we should not set tw_refcnt
* to a non null value before everything is setup for this
* timewait socket.
*/
refcount_set(&tw->tw_refcnt, 0);
and inet_twsk_hashdance_schedule() publishes it into the bind owners list and
drops both bind locks before initializing the count:
sk_add_bind_node((struct sock *)tw, &tw->tw_tb2->owners);
spin_unlock(&bhead2->lock);
spin_unlock(&bhead->lock);
spin_lock(lock);
refcount_set(&tw->tw_refcnt, 3);
A dumper blocked on bhead2->lock acquires it right after that unlock and
immediately walks tb2->owners, so it can observe the published tw with
tw_refcnt == 0. sock_hold() then calls refcount_inc() on zero, which trips
refcount_warn_saturate() and leaves the counter saturated, after which
refcount_set(&tw->tw_refcnt, 3) discards the dump's reference. The later
sock_gen_put() from the fill loop then consumes one of the three structural
references (bhash, ehash, timer).
Would using refcount_inc_not_zero() here, or keeping the TCP_CLOSE check
under the lock, be safer?
> @@ -335,8 +402,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
> struct inet_listen_hashbucket *ilb;
> struct hlist_nulls_node *node;
> + struct sock *sk_arr[SKARR_SZ];
> + int num_arr[SKARR_SZ];
> + struct sock *cursor;
> + int idx, accum, res;
> + bool use_cursor;
>
> +resume_listen_walk:
> num = 0;
> + accum = 0;
> ilb = &hashinfo->lhash2[i];
>
> if (hlist_nulls_empty(&ilb->nulls_head)) {
> @@ -344,52 +418,81 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> continue;
> }
> spin_lock(&ilb->lock);
> - sk_nulls_for_each(sk, node, &ilb->nulls_head) {
> - struct inet_sock *inet = inet_sk(sk);
> + cursor = cb_data->dump_cursor;
> + use_cursor = cursor &&
> + cb_data->dump_cursor_type ==
> + INET_DIAG_DUMP_CURSOR_TCP_LISTEN &&
> + cb_data->dump_cursor_slot == i &&
> + inet_sk_state_load(cursor) == TCP_LISTEN &&
> + !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
> + cursor->sk_nulls_node.pprev != LIST_POISON2 &&
> + inet_lhash2_bucket_sk(hashinfo, cursor) == ilb;
> + node = use_cursor ? cursor->sk_nulls_node.next :
> + ilb->nulls_head.first;
[Severity: Medium]
Is the "cursor is still in this bucket" conclusion sound when every input to
it is read without the lock that publishes it?
In the listener path the recomputation uses sock_net(cursor),
sk_v6_rcv_saddr (a 16-byte non-atomic read) and inet_sk(cursor)->inet_num via
the newly exported inet_lhash2_bucket_sk(), none of which is protected by
ilb->lock, and the pprev test and the bucket computation are separate plain
loads with no barrier between them.
The ehash variant later in this function has the same shape with
cursor->sk_nulls_node.pprev followed by cursor->sk_hash. The insertion side
sets sk->sk_hash before taking the target bucket lock and linking with
__sk_nulls_add_node_rcu(), and inet_csk_listen_start() stores TCP_LISTEN
before hashing into lhash2, so a reader that observes the new linkage is not
guaranteed to observe the matching sk_hash/sk_state store. A socket that
left ehash bucket i and called listen() keeps its old sk_hash, so
inet_sk_state_load(cursor) != TCP_LISTEN can still be true while the lhash2
linkage is already visible.
If validation passes on a stale snapshot, the walk then follows
cursor->sk_nulls_node.next into a chain whose spinlock is not held and
sock_hold()s the entries it finds. On x86 the store order makes the
inconsistent snapshot hard to observe, but is bucket membership something
that can be established from lockless per-socket fields at all?
> + if (!use_cursor)
> + s_num = 0;
> + hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
>
> - if (!net_eq(sock_net(sk), net))
> - continue;
> + sock_hold(sk);
> + num_arr[accum] = num;
> + sk_arr[accum] = sk;
> + if (++accum == SKARR_SZ)
> + break;
>
> - if (num < s_num) {
> - num++;
> - continue;
> - }
> + ++num;
> + }
> + spin_unlock(&ilb->lock);
[Severity: High]
With the num < s_num skip removed from this loop and s_num forced to 0 when
the cursor is rejected, what stops the bucket from being re-dumped from its
head?
All three walks now do:
if (!use_cursor)
s_num = 0;
and the batch continuations replace s_num = num + 1 with s_num = 0:
if (accum == SKARR_SZ) {
s_num = 0;
goto resume_listen_walk;
}
The bind walk and the ehash walk have the same two changes, and the s_num
parameter of tcp_diag_bind_collect_owners() is dead because its only caller
sits in the !use_cursor branch where s_num has just been zeroed.
So whenever cursor validation fails, which the changelog describes as the
"safe restart on mismatch", the walk restarts at ilb->nulls_head.first, at
the head of ibb->chain, or at head->chain.first, and re-emits sockets from
that bucket that were already sent to user space earlier in the same dump.
Before this change s_num = num + 1 guaranteed the restart advanced.
There is a second effect: if all of the re-collected leading SKARR_SZ sockets
are discarded after the lock is dropped (foreign netns, since lhash2 and
ehash are shared across netns, or sdiag_family/port mismatch, or bytecode
rejection), nothing is emitted, res stays 0 and the goto resume_*_walk loop
repeats over the same head with only cond_resched() yielding.
tcp_diag_dump() still loads s_num = num = cb->args[2] on entry and still
stores cb->args[2] = num at the done: label, but num is now a chunk-relative
counter that no resume path consumes. Should that state be dropped, or
should a positional fallback be kept for the mismatch case? Also, since the
walk can now repeat objects within a dump, should NLM_F_DUMP_INTR be set via
a generation counter in netlink_callback.seq?
[ ... ]
> @@ -412,34 +517,46 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> continue;
> }
> spin_lock_bh(&ibb->lock);
> - inet_bind_bucket_for_each(tb2, &ibb->chain) {
> - if (!net_eq(ib2_net(tb2), net))
> - continue;
> -
> - sk_for_each_bound(sk, &tb2->owners) {
> - struct inet_sock *inet = inet_sk(sk);
> -
> - if (num < s_num)
> - goto next_bind;
> -
> - if (sk->sk_state != TCP_CLOSE ||
> - !inet->inet_num)
> - goto next_bind;
> -
> - if (r->sdiag_family != AF_UNSPEC &&
> - r->sdiag_family != sk->sk_family)
> - goto next_bind;
> -
> - if (!inet_diag_bc_sk(cb_data, sk))
> - goto next_bind;
> -
> - sock_hold(sk);
> - num_arr[accum] = num;
> - sk_arr[accum] = sk;
> - if (++accum == SKARR_SZ)
> + cursor = cb_data->dump_cursor;
> + use_cursor = cursor &&
> + cb_data->dump_cursor_type ==
> + INET_DIAG_DUMP_CURSOR_TCP_BIND &&
> + cb_data->dump_cursor_slot == i &&
> + !hlist_unhashed(&cursor->sk_bind_node) &&
> + cursor->sk_bind_node.pprev != LIST_POISON2;
> + if (use_cursor) {
> + tb2 = tcp_diag_sk_bind2(cursor);
> + use_cursor = tb2 &&
> + inet_bhashfn_portaddr(hashinfo, cursor,
> + sock_net(cursor),
> + inet_sk(cursor)->inet_num) ==
> + ibb;
> + }
> + if (!use_cursor)
> + s_num = 0;
> + if (use_cursor) {
> + sk = cursor;
> + if (tcp_diag_bind_collect_owners_continue(sk, sk_arr,
> + num_arr,
> + &accum,
> + &num))
> + goto pause_bind_walk;
> + hlist_for_each_entry_continue(tb2, node) {
> + if (tcp_diag_bind_collect_owners(&tb2->owners,
> + sk_arr,
> + num_arr,
> + &accum,
> + &num, 0))
> + goto pause_bind_walk;
> + }
[Severity: Critical]
Can these two liveness checks ever detect that the cursor left
tb2->owners, and can tb2 here already be freed?
Removal from a bind chain goes through __sk_del_bind_node():
include/net/sock.h:
static inline void __sk_del_bind_node(struct sock *sk)
{
__hlist_del(&sk->sk_bind_node);
}
__hlist_del() only writes *pprev and next->pprev; it leaves the removed
node's own pprev and next untouched. So for a socket that was unlinked,
hlist_unhashed(&cursor->sk_bind_node) is false and
cursor->sk_bind_node.pprev != LIST_POISON2 holds, and both tests pass.
The freed-bucket part comes from __inet_bhash2_update_saddr():
net/ipv4/inet_hashtables.c:
spin_lock(&head2->lock);
__sk_del_bind_node(sk);
inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash);
spin_unlock(&head2->lock);
if (reset)
inet_reset_saddr(sk);
else
inet_update_saddr(sk, saddr, family);
icsk_bind2_hash is never cleared, and inet_bind2_bucket_destroy() frees the
bucket immediately with no grace period when it was the sole owner:
if (hlist_empty(&tb->owners)) {
__hlist_del(&tb->node);
__hlist_del(&tb->bhash_node);
kmem_cache_free(cachep, tb);
return;
}
In the window between that spin_unlock() and the address update, the address
is unchanged, so inet_bhashfn_portaddr() still resolves to ibb, and
tcp_diag_sk_bind2(cursor) returns the dangling icsk_bind2_hash. use_cursor
therefore becomes true.
tcp_diag_bind_collect_owners_continue() then iterates the cursor's stale
sk_bind_node.next, and hlist_for_each_entry_continue(tb2, node) reads
tb2->node.next out of the freed slab object, producing an arbitrary
struct inet_bind2_bucket * whose owners list is walked with sock_hold()
applied to each apparent entry.
Note the dumper holds only hashinfo->bhash2[i].lock and never the primary
bhash head lock, and a cursor is saved for every collected owner including
the ones filtered out afterwards, so a process that dumps with
TCPF_BOUND_INACTIVE while calling connect() on a bound socket in that bucket
can reach this. Could the cursor instead record a validated tb2 identity
that is re-looked-up under ibb->lock, rather than being derived from
icsk_bind2_hash?
> + } else {
> + inet_bind_bucket_for_each(tb2, &ibb->chain) {
> + if (tcp_diag_bind_collect_owners(&tb2->owners,
> + sk_arr,
> + num_arr,
> + &accum,
> + &num, s_num))
> goto pause_bind_walk;
> -next_bind:
> - num++;
> }
> }
> pause_bind_walk:
[ ... ]
> @@ -463,13 +598,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> cond_resched();
>
> if (accum == SKARR_SZ) {
> - s_num = num + 1;
> + s_num = 0;
> goto resume_bind_walk;
> }
>
> + inet_diag_dump_clear_cursor(cb_data);
> s_num = 0;
> }
[ ... ]
> if (accum == SKARR_SZ) {
> - s_num = num + 1;
> + s_num = 0;
> goto next_chunk;
> }
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1788248601.git.zihanx%40nebusec.ai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold
2026-09-01 12:53 ` [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold Zihan Xi
@ 2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-07 4:49 ` zihan xi
2026-09-07 7:09 ` Eric Dumazet
1 sibling, 1 reply; 10+ messages in thread
From: netdev-bot+sashiko @ 2026-09-07 4:22 UTC (permalink / raw)
To: zihanx
Cc: netdev, linux-kernel, mptcp, davem, edumazet, kuba, pabeni, horms,
ncardwell, kuniyu, matttbe, martineau, geliang, gnault, fw,
stable, vega
Thank you for your contribution! Sashiko AI review found 6 potential
issue(s) to consider:
Critical: 0 · High: 1 · Medium: 3 · Low: 2
- [High] Use-after-free introduced by the new deferred processing loop in
mptcp_diag_dump_listeners().
- [Medium] The patch silently changes which MPTCP listeners the dump
reports, and the commit message does not mention it.
- [Medium] mptcp_diag_dump_listeners() mixes two incompatible counting
bases for the resume index diag_ctx->l_num.
- [Medium] mptcp_diag_dump_listeners() performs all
identity/authorization checks on the parent MPTCP socket…
- [Low] Commit message of "mptcp: diag: bound listener bucket lock hold"
carries Fixes: 4fa39b701ce9 + Cc: stable + Reported-by:, but never…
- [Low] In the new post-lock loop of mptcp_diag_dump_listeners()
(net/mptcp/mptcp_diag.c), `tmp = ctx ?
--
mptcp: diag: bound listener bucket lock hold
This reworks mptcp_diag_dump_listeners() so that the listener bucket lock is
only held while collecting up to MPTCP_DIAG_BULK_SZ referenced listener
sockets, with the ULP/property re-checks, the parent msk reference and
sk_diag_dump() moved outside the lock. A referenced dump cursor is kept in
cb->data so later batches resume after the previous listener instead of
rescanning the bucket prefix.
v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
> diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
> index 6ccd32bc48f91..4859e77a28c7a 100644
> --- a/include/linux/inet_diag.h
> +++ b/include/linux/inet_diag.h
> @@ -39,6 +39,7 @@ enum inet_diag_dump_cursor_type {
> INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
> INET_DIAG_DUMP_CURSOR_TCP_BIND,
> INET_DIAG_DUMP_CURSOR_TCP_EHASH,
> + INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN,
> };
[Severity: Low]
Two questions about the changelog for this one.
First, the message says the lock hold time "can therefore grow with the
number of sockets visited", but it never states the observed symptom. Was
this a soft lockup, an RCU stall, or measured contention on ilb->lock? Given
the Fixes: 4fa39b701ce9 and Cc: stable tags plus the Reported-by:, could the
symptom be spelled out?
Second, could the backport dependency be mentioned? enum
inet_diag_dump_cursor_type does not exist in the tree this applies on top of:
git grep inet_diag_dump_cursor_type <base> -> no match
The whole enum, cb_data->dump_cursor, dump_cursor_slot, dump_cursor_type and
inet_diag_dump_clear_cursor() come from the immediately preceding patch of
this series, "tcp: diag: bound bucket lock hold in tcp_diag_dump()". Stable
trees carrying 4fa39b701ce9 cannot take this patch on its own, and nothing in
the message says so.
> diff --git a/net/mptcp/mptcp_diag.c b/net/mptcp/mptcp_diag.c
> index 136c2d05c0eeb..37b33ea5d79de 100644
> --- a/net/mptcp/mptcp_diag.c
> +++ b/net/mptcp/mptcp_diag.c
> @@ -12,6 +12,19 @@
> #include <net/netlink.h>
> #include "protocol.h"
>
> +/* Process a bounded number of listeners per bucket lock hold. */
> +#define MPTCP_DIAG_BULK_SZ 16
> +
> +static void mptcp_diag_save_cursor(struct inet_diag_dump_data *cb_data,
> + unsigned int slot, struct sock *sk)
> +{
> + sock_hold(sk);
> + inet_diag_dump_clear_cursor(cb_data);
> + cb_data->dump_cursor = sk;
> + cb_data->dump_cursor_slot = slot;
> + cb_data->dump_cursor_type = INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN;
> +}
> +
[ ... ]
> @@ -84,64 +98,102 @@ static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callba
> hinfo = net->ipv4.tcp_death_row.hashinfo;
>
> for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
> + struct sock *tmp, *sk, *sk_arr[MPTCP_DIAG_BULK_SZ];
> struct inet_listen_hashbucket *ilb;
> + int num_arr[MPTCP_DIAG_BULK_SZ];
> struct hlist_nulls_node *node;
> - struct sock *sk;
> - int num = 0;
> + int accum, idx, num, ret;
> + struct sock *cursor;
> + bool use_cursor;
>
> +resume_listen_walk:
> + num = 0;
> + accum = 0;
> ilb = &hinfo->lhash2[i];
> + ret = 0;
>
> rcu_read_lock();
> spin_lock(&ilb->lock);
> - sk_nulls_for_each(sk, node, &ilb->nulls_head) {
> - const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
> - struct inet_sock *inet = inet_sk(sk);
> - int ret;
> -
> - if (num < diag_ctx->l_num)
> - goto next_listen;
> -
> - if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
> - goto next_listen;
> -
> - sk = ctx->conn;
> - if (!sk || !net_eq(sock_net(sk), net))
> - goto next_listen;
> -
> - if (r->sdiag_family != AF_UNSPEC &&
> - sk->sk_family != r->sdiag_family)
> - goto next_listen;
> -
> - if (r->id.idiag_sport != inet->inet_sport &&
> - r->id.idiag_sport)
> + cursor = cb_data->dump_cursor;
> + use_cursor = cursor &&
> + cb_data->dump_cursor_type ==
> + INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN &&
> + cb_data->dump_cursor_slot == i &&
> + inet_sk_state_load(cursor) == TCP_LISTEN &&
> + !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
> + cursor->sk_nulls_node.pprev != LIST_POISON2 &&
> + inet_lhash2_bucket_sk(hinfo, cursor) == ilb;
> + node = use_cursor ? cursor->sk_nulls_node.next :
> + ilb->nulls_head.first;
> + hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
> + if (!use_cursor && num < diag_ctx->l_num)
> goto next_listen;
[Severity: Medium]
Are the two resume mechanisms here using the same counting base for
diag_ctx->l_num?
This test counts entries from the bucket head, but at resume_listen_walk num
is reset to 0 and, when use_cursor is true, the walk starts at
cursor->sk_nulls_node.next, i.e. mid-bucket. The values stashed in num_arr[]
and later stored are therefore cursor-relative and bounded by 0..15:
if (ret >= 0) {
ret = sk_diag_dump(tmp, skb, cb, r, net_admin);
if (ret < 0)
num = num_arr[idx];
}
...
if (ret < 0) {
diag_ctx->l_slot = i;
diag_ctx->l_num = num;
return;
}
Walking a bucket with more than 16 matching listeners:
batch 1: 16 listeners dumped, cursor saved on e15
goto resume_listen_walk with diag_ctx->l_num = 0, use_cursor true, so num
now counts from e16
skb fills at idx 5, so diag_ctx->l_num = 5 while the real bucket offset
is 21
the cursor socket is closed before the next recvmsg(), use_cursor becomes
false, and the walk resumes at bucket position 5
Does that re-emit e5..e19, and, if entries were removed in the meantime and
positions shifted, silently drop listeners that were never reported?
The peer implementation of the same cursor protocol from the preceding patch,
tcp_diag_dump(), zeroes the positional counter when the cursor is unusable:
node = use_cursor ? cursor->sk_nulls_node.next : ilb->nulls_head.first;
if (!use_cursor)
s_num = 0;
Should mptcp_diag_dump_listeners() do the same so the two bases are never
mixed? The intra-call restart also forces diag_ctx->l_num = 0 before goto
resume_listen_walk, so a cursor invalidated between lock drops restarts the
bucket at the head within a single dump call.
> if (!refcount_inc_not_zero(&sk->sk_refcnt))
> goto next_listen;
>
> - ret = sk_diag_dump(sk, skb, cb, r, net_admin);
> -
> - sock_put(sk);
> -
> - if (ret < 0) {
> - spin_unlock(&ilb->lock);
> - rcu_read_unlock();
> - diag_ctx->l_slot = i;
> - diag_ctx->l_num = num;
> - return;
> - }
> - diag_ctx->l_num = num + 1;
> - num = 0;
[Severity: Medium]
Removing this "l_num = num + 1; num = 0;" pair changes which listeners the
dump reports, and the changelog does not mention it.
In the pre-patch loop the skip test was "if (num < diag_ctx->l_num)" while
num was reset to 0 after every successful dump and l_num kept growing, so for
a bucket e0..e6 the emitted set was e0, e1, e3, e6:
e0 dumped -> l_num = 1, num = 0 then ++num = 1
e1 dumped -> l_num = 2, num = 1
e2 hits 1 < 2 and is skipped
e3 dumped -> l_num = 3
e4, e5 skipped
The new code keeps num monotonic and applies the skip test only in the
non-cursor fallback, so every collected listener is processed. Is that the
actual user-visible defect being fixed here? ss -Ml output changes (more
listeners reported) on any host with three or more MPTCP listeners hashing
into the same lhash2 bucket, and this seems worth stating in the message,
along with whether it belongs in the same patch as the lock-hold change.
> + num_arr[accum] = num;
> + sk_arr[accum] = sk;
> + if (++accum == MPTCP_DIAG_BULK_SZ)
> + break;
> next_listen:
> ++num;
> }
> spin_unlock(&ilb->lock);
> rcu_read_unlock();
>
> + for (idx = 0; idx < accum; idx++) {
> + const struct mptcp_subflow_context *ctx;
> + const struct tcp_ulp_ops *ulp_ops;
> + struct inet_sock *inet;
> +
> + sk = sk_arr[idx];
> + rcu_read_lock();
> + ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
> + ulp_ops = READ_ONCE(inet_csk(sk)->icsk_ulp_ops);
> + inet = inet_sk(sk);
> + tmp = ctx ? ctx->conn : NULL;
[Severity: High]
Can this read freed memory? The rcu_read_lock() here is entered after
spin_unlock(&ilb->lock) and rcu_read_unlock() above, so it cannot extend a
grace period that already started.
The collection loop only took sk_refcnt on the listener subflow, which does
not keep the ULP context alive:
__tcp_close()
tcp_set_state(sk, TCP_CLOSE)
inet_unhash(sk) /* takes ilb->lock, free once diag drops it */
inet_csk_destroy_sock()
sk->sk_prot->destroy(sk)
tcp_v4_destroy_sock()
tcp_cleanup_ulp()
subflow_ulp_release()
and net/mptcp/subflow.c:subflow_ulp_release() drops the msk reference and
frees the context:
sk = ctx->conn;
if (sk) {
...
sock_put(sk);
}
mptcp_subflow_ops_undo_override(ssk);
if (release)
kfree_rcu(ctx, rcu);
net/ipv4/tcp_ulp.c:tcp_cleanup_ulp() clears only icsk_ulp_ops:
if (icsk->icsk_ulp_ops->release)
icsk->icsk_ulp_ops->release(sk);
module_put(icsk->icsk_ulp_ops->owner);
icsk->icsk_ulp_ops = NULL;
icsk_ulp_data is never unpublished on this path (subflow_ulp_fallback() does
rcu_assign_pointer(icsk->icsk_ulp_data, NULL), this path does not). If
kfree_rcu(ctx) was queued before this loop iteration re-entered
rcu_read_lock(), the grace period does not cover this reader, so
rcu_dereference(inet_csk(sk)->icsk_ulp_data) can return a freed and reused
slab object, and ctx->conn is dereferenced before the !ulp_ops guard is even
evaluated.
The loop is also preemptible for up to 15 preceding sk_diag_dump() /
inet_diag_bc_sk() invocations with request-supplied bytecode, which widens the
window. Does the commit message claim ("After dropping the lock, read
icsk_ulp_data with rcu_dereference()") hold here, or do ctx and the parent msk
reference have to be acquired while ilb->lock is still held, as the pre-patch
code did?
[Severity: Low]
Separately, is it intentional that ctx->conn is loaded before the check that
the ULP is actually "mptcp"? The collection loop under ilb->lock no longer
filters by ULP at all, so listeners with a foreign ULP context now reach this
loop, and the read happens at offsetof(struct mptcp_subflow_context, conn)
inside an object of an unrelated type.
net/xfrm/espintcp.c:espintcp_init_sk() publishes its own object with
rcu_assign_pointer(icsk->icsk_ulp_data, ctx) and, unlike tls_init(), has no
TCP-state restriction, so an unprivileged setsockopt(TCP_ULP, "espintcp")
followed by listen() puts such a socket into lhash2.
struct espintcp_ctx is larger than that offset today, so the read stays inside
the allocation and the loaded value is discarded by the failing strcmp().
Still, the pre-patch code did the strcmp() first and never touched a foreign
context. Could the ctx->conn load be moved after the ULP name check?
> + if (!ctx || !ulp_ops || strcmp(ulp_ops->name, "mptcp") ||
> + !tmp || !net_eq(sock_net(tmp), net) ||
> + (r->sdiag_family != AF_UNSPEC &&
> + tmp->sk_family != r->sdiag_family) ||
> + (r->id.idiag_sport != inet->inet_sport &&
> + r->id.idiag_sport) ||
> + !refcount_inc_not_zero(&tmp->sk_refcnt)) {
> + rcu_read_unlock();
> + goto processed_listener_sk;
> + }
> + rcu_read_unlock();
> + if (ret >= 0) {
> + ret = sk_diag_dump(tmp, skb, cb, r, net_admin);
> + if (ret < 0)
> + num = num_arr[idx];
> + }
> + sock_put(tmp);
[Severity: Medium]
Does this need a re-validation after refcount_inc_not_zero(&tmp->sk_refcnt)?
All the identity and scoping checks on the parent msk (net_eq(sock_net(tmp),
net), sdiag_family, idiag_sport) run before the reference is taken, and
nothing is re-checked afterwards. mptcp_prot uses SLAB_TYPESAFE_BY_RCU:
net/mptcp/protocol.c:
.slab_flags = SLAB_TYPESAFE_BY_RCU,
so rcu_read_lock() only guarantees type stability, not identity. If
subflow_ulp_release() already dropped the last reference via
sock_put(ctx->conn), the object can be recycled as a different MPTCP socket
inside this reader's RCU section, refcount_inc_not_zero() then succeeds, and
sk_diag_dump(tmp, ...) serializes that socket's addresses, ports, uid, cookie
and state into the reply, possibly from another namespace.
The in-tree pattern for this slab, net/mptcp/token.c:mptcp_token_get_sock(),
re-checks after the acquisition:
if (!refcount_inc_not_zero(&sk->sk_refcnt))
goto not_found;
if (READ_ONCE(msk->token) != token ||
!net_eq(sock_net(sk), net)) {
Should the same re-check be done here? Pre-patch the conn read and the msk
refcount acquisition both happened under ilb->lock with the subflow still
hashed, so the reference held by ctx was still alive.
> +processed_listener_sk:
> + if (ret >= 0)
> + mptcp_diag_save_cursor(cb_data, i, sk);
> + sock_put(sk);
> + }
> +
> + if (ret < 0) {
> + diag_ctx->l_slot = i;
> + diag_ctx->l_num = num;
> + return;
> + }
> +
> cond_resched();
> +
> + if (accum == MPTCP_DIAG_BULK_SZ) {
> + diag_ctx->l_num = 0;
> + goto resume_listen_walk;
> + }
> +
> + inet_diag_dump_clear_cursor(cb_data);
> diag_ctx->l_num = 0;
> }
>
> + inet_diag_dump_clear_cursor(cb_data);
> diag_ctx->l_num = 0;
> diag_ctx->l_slot = i;
> }
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1788248601.git.zihanx%40nebusec.ai
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
2026-09-07 4:22 ` netdev-bot+sashiko
@ 2026-09-07 4:48 ` zihan xi
2026-09-07 7:05 ` Eric Dumazet
0 siblings, 1 reply; 10+ messages in thread
From: zihan xi @ 2026-09-07 4:48 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: netdev, linux-kernel, mptcp, davem, edumazet, kuba, pabeni, horms,
ncardwell, kuniyu, matttbe, martineau, geliang, gnault, fw,
stable, vega
On Mon, Sep 7, 2026 at 12:22 PM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 6 potential
> issue(s) to consider:
>
> Critical: 2 · High: 1 · Medium: 2 · Low: 1
>
> - [Critical] Use-after-free / wild-pointer traversal: the new bind-hash
> dump cursor revalidation in tcp_diag_dump() cannot detect that the…
> - [Critical] Refcount initialization race: tcp_diag_bind_collect_sock()
> calls sock_hold() unconditionally on every node of tb2->owners,…
> - [High] Loss of dump forward progress and duplicate output: the patch
> deletes every positional (`s_num`) resume path in tcp_diag_dump()…
> - [Medium] Lockless, unordered reads of cursor state: the listen and
> ehash cursor validations establish "this socket is in the bucket I…
> - [Medium] The commit message mischaracterizes the pre-existing defect.
> - [Low] Concern that the sibling MPTCP listener dump
> (mptcp_diag_dump_listeners()) keeps evaluating request-supplied…
>
> --
>
> tcp: diag: bound bucket lock hold in tcp_diag_dump()
>
> This reworks the three tcp_diag_dump() hash walks so that only referenced
> sockets are collected under each bucket lock, and moves the family/port
> filters, inet_diag_bc_sk() bytecode evaluation and the fill work outside the
> critical section. A referenced dump cursor is added to
> struct inet_diag_dump_data so a following batch resumes after the previously
> reported socket, with per-table validation of the cursor.
>
> v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
>
> > inet_diag dumps run request-supplied bytecode through inet_diag_bc_sk().
> > tcp_diag_dump() currently evaluates socket filters and fills replies while
> > holding the listener, bind, and ehash bucket locks.
>
> [Severity: Medium]
> Is this description accurate for all three tables?
>
> At the baseline (544d85de4dc2) only the listener walk fills replies under
> ilb->lock. The bind walk already collected at most SKARR_SZ = 16 referenced
> sockets under spin_lock_bh(&ibb->lock) and only called inet_sk_diag_fill()
> after the lock was dropped:
>
> pause_bind_walk:
> spin_unlock_bh(&ibb->lock);
>
> res = 0;
> for (idx = 0; idx < accum; idx++) {
> if (res >= 0) {
> res = inet_sk_diag_fill(sk_arr[idx],
> NULL, skb, cb,
> r, NLM_F_MULTI,
> net_admin);
>
> The ehash walk did the same with sk_diag_fill() after spin_unlock_bh(lock).
> What remained under the bind and ehash locks was inet_diag_bc_sk() plus the
> cheap family/port comparisons, bounded to 16 sockets per lock hold.
>
> Could the changelog be narrowed to the listener walk, or state explicitly
> what part of the bind/ehash work it is moving out?
>
> > diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
> > index 704fd415c2b49..6ccd32bc48f91 100644
> > --- a/include/linux/inet_diag.h
> > +++ b/include/linux/inet_diag.h
>
> [ ... ]
>
> > @@ -32,12 +33,23 @@ struct inet_diag_handler {
> > };
> >
> > struct bpf_sk_storage_diag;
> > +
> > +enum inet_diag_dump_cursor_type {
> > + INET_DIAG_DUMP_CURSOR_NONE,
> > + INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
> > + INET_DIAG_DUMP_CURSOR_TCP_BIND,
> > + INET_DIAG_DUMP_CURSOR_TCP_EHASH,
> > +};
> > +
>
> [Severity: Low]
> This isn't a bug, but a note for backporters. The sibling MPTCP listener
> dump mptcp_diag_dump_listeners() is converted by the follow-up patch in this
> series (INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN, which is why the enum slot and
> the exported inet_diag_dump_clear_cursor() appear here), so at the tip of the
> series the MPTCP listener walk also drops ilb->lock before calling
> sk_diag_dump()/inet_diag_bc_sk().
>
> This patch is Cc: stable, and the commit message body does not mention that
> the MPTCP listener walk keeps running request-supplied bytecode under the
> same hashinfo->lhash2 bucket lock until the follow-up patch is applied. Only
> the v2 changelog, which is stripped on apply, mentions "Left
> INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN to the MPTCP patch". Could that
> dependency be stated in the commit message?
>
> [ ... ]
>
> > diff --git a/include/net/inet_hashtables.h b/include/net/inet_hashtables.h
> > index 6e2fe186d0dcb..d95639ac70c67 100644
> > --- a/include/net/inet_hashtables.h
> > +++ b/include/net/inet_hashtables.h
> > @@ -188,6 +188,24 @@ inet_lhash2_bucket(struct inet_hashinfo *h, u32 hash)
> > return &h->lhash2[hash & h->lhash2_mask];
> > }
> >
> > +static inline struct inet_listen_hashbucket *
> > +inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
> > +{
> > + u32 hash;
> > +
> > +#if IS_ENABLED(CONFIG_IPV6)
> > + if (sk->sk_family == AF_INET6)
> > + hash = ipv6_portaddr_hash(sock_net(sk),
> > + &sk->sk_v6_rcv_saddr,
> > + inet_sk(sk)->inet_num);
> > + else
> > +#endif
> > + hash = ipv4_portaddr_hash(sock_net(sk),
> > + inet_sk(sk)->inet_rcv_saddr,
> > + inet_sk(sk)->inet_num);
> > + return inet_lhash2_bucket(h, hash);
> > +}
> > +
>
> [ ... ]
>
> > diff --git a/net/ipv4/tcp_diag.c b/net/ipv4/tcp_diag.c
> > index ba1fdbe9807fd..842e13ee79e5b 100644
> > --- a/net/ipv4/tcp_diag.c
> > +++ b/net/ipv4/tcp_diag.c
> > @@ -285,6 +285,73 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
> > net_admin);
> > }
> >
>
> [ ... ]
>
> > +static struct inet_bind2_bucket *tcp_diag_sk_bind2(const struct sock *sk)
> > +{
> > + if (sk->sk_state == TCP_TIME_WAIT)
> > + return inet_twsk(sk)->tw_tb2;
> > +
> > + return inet_csk(sk)->icsk_bind2_hash;
> > +}
> > +
> > +static bool tcp_diag_bind_collect_sock(struct sock *sk, struct sock **sk_arr,
> > + int *num_arr, int *accum, int num)
> > +{
> > + sock_hold(sk);
> > + num_arr[*accum] = num;
> > + sk_arr[*accum] = sk;
> > +
> > + return ++*accum == SKARR_SZ;
> > +}
> > +
> > +static bool tcp_diag_bind_collect_owners(struct hlist_head *owners,
> > + struct sock **sk_arr, int *num_arr,
> > + int *accum, int *num, int s_num)
> > +{
> > + struct sock *sk;
> > +
> > + sk_for_each_bound(sk, owners) {
> > + if (*num < s_num) {
> > + (*num)++;
> > + continue;
> > + }
> > +
> > + if (tcp_diag_bind_collect_sock(sk, sk_arr, num_arr, accum, *num))
> > + return true;
> > + (*num)++;
> > + }
> > +
> > + return false;
> > +}
>
> [Severity: Critical]
> Can the unconditional sock_hold() in tcp_diag_bind_collect_sock() hit a
> TIME_WAIT socket whose tw_refcnt is still 0?
>
> The changelog says "Bind collection counts TIME_WAIT nodes toward the batch
> limit", and tcp_diag_bind_collect_owners() no longer filters on
> sk->sk_state != TCP_CLOSE before taking the reference, so every node of
> tb2->owners now gets a refcount_inc().
>
> inet_twsk_alloc() starts the object at zero on purpose:
>
> /*
> * Because we use RCU lookups, we should not set tw_refcnt
> * to a non null value before everything is setup for this
> * timewait socket.
> */
> refcount_set(&tw->tw_refcnt, 0);
>
> and inet_twsk_hashdance_schedule() publishes it into the bind owners list and
> drops both bind locks before initializing the count:
>
> sk_add_bind_node((struct sock *)tw, &tw->tw_tb2->owners);
>
> spin_unlock(&bhead2->lock);
> spin_unlock(&bhead->lock);
>
> spin_lock(lock);
>
> refcount_set(&tw->tw_refcnt, 3);
>
> A dumper blocked on bhead2->lock acquires it right after that unlock and
> immediately walks tb2->owners, so it can observe the published tw with
> tw_refcnt == 0. sock_hold() then calls refcount_inc() on zero, which trips
> refcount_warn_saturate() and leaves the counter saturated, after which
> refcount_set(&tw->tw_refcnt, 3) discards the dump's reference. The later
> sock_gen_put() from the fill loop then consumes one of the three structural
> references (bhash, ehash, timer).
>
> Would using refcount_inc_not_zero() here, or keeping the TCP_CLOSE check
> under the lock, be safer?
>
> > @@ -335,8 +402,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> > for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
> > struct inet_listen_hashbucket *ilb;
> > struct hlist_nulls_node *node;
> > + struct sock *sk_arr[SKARR_SZ];
> > + int num_arr[SKARR_SZ];
> > + struct sock *cursor;
> > + int idx, accum, res;
> > + bool use_cursor;
> >
> > +resume_listen_walk:
> > num = 0;
> > + accum = 0;
> > ilb = &hashinfo->lhash2[i];
> >
> > if (hlist_nulls_empty(&ilb->nulls_head)) {
> > @@ -344,52 +418,81 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> > continue;
> > }
> > spin_lock(&ilb->lock);
> > - sk_nulls_for_each(sk, node, &ilb->nulls_head) {
> > - struct inet_sock *inet = inet_sk(sk);
> > + cursor = cb_data->dump_cursor;
> > + use_cursor = cursor &&
> > + cb_data->dump_cursor_type ==
> > + INET_DIAG_DUMP_CURSOR_TCP_LISTEN &&
> > + cb_data->dump_cursor_slot == i &&
> > + inet_sk_state_load(cursor) == TCP_LISTEN &&
> > + !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
> > + cursor->sk_nulls_node.pprev != LIST_POISON2 &&
> > + inet_lhash2_bucket_sk(hashinfo, cursor) == ilb;
> > + node = use_cursor ? cursor->sk_nulls_node.next :
> > + ilb->nulls_head.first;
>
> [Severity: Medium]
> Is the "cursor is still in this bucket" conclusion sound when every input to
> it is read without the lock that publishes it?
>
> In the listener path the recomputation uses sock_net(cursor),
> sk_v6_rcv_saddr (a 16-byte non-atomic read) and inet_sk(cursor)->inet_num via
> the newly exported inet_lhash2_bucket_sk(), none of which is protected by
> ilb->lock, and the pprev test and the bucket computation are separate plain
> loads with no barrier between them.
>
> The ehash variant later in this function has the same shape with
> cursor->sk_nulls_node.pprev followed by cursor->sk_hash. The insertion side
> sets sk->sk_hash before taking the target bucket lock and linking with
> __sk_nulls_add_node_rcu(), and inet_csk_listen_start() stores TCP_LISTEN
> before hashing into lhash2, so a reader that observes the new linkage is not
> guaranteed to observe the matching sk_hash/sk_state store. A socket that
> left ehash bucket i and called listen() keeps its old sk_hash, so
> inet_sk_state_load(cursor) != TCP_LISTEN can still be true while the lhash2
> linkage is already visible.
>
> If validation passes on a stale snapshot, the walk then follows
> cursor->sk_nulls_node.next into a chain whose spinlock is not held and
> sock_hold()s the entries it finds. On x86 the store order makes the
> inconsistent snapshot hard to observe, but is bucket membership something
> that can be established from lockless per-socket fields at all?
>
> > + if (!use_cursor)
> > + s_num = 0;
> > + hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
> >
> > - if (!net_eq(sock_net(sk), net))
> > - continue;
> > + sock_hold(sk);
> > + num_arr[accum] = num;
> > + sk_arr[accum] = sk;
> > + if (++accum == SKARR_SZ)
> > + break;
> >
> > - if (num < s_num) {
> > - num++;
> > - continue;
> > - }
> > + ++num;
> > + }
> > + spin_unlock(&ilb->lock);
>
> [Severity: High]
> With the num < s_num skip removed from this loop and s_num forced to 0 when
> the cursor is rejected, what stops the bucket from being re-dumped from its
> head?
>
> All three walks now do:
>
> if (!use_cursor)
> s_num = 0;
>
> and the batch continuations replace s_num = num + 1 with s_num = 0:
>
> if (accum == SKARR_SZ) {
> s_num = 0;
> goto resume_listen_walk;
> }
>
> The bind walk and the ehash walk have the same two changes, and the s_num
> parameter of tcp_diag_bind_collect_owners() is dead because its only caller
> sits in the !use_cursor branch where s_num has just been zeroed.
>
> So whenever cursor validation fails, which the changelog describes as the
> "safe restart on mismatch", the walk restarts at ilb->nulls_head.first, at
> the head of ibb->chain, or at head->chain.first, and re-emits sockets from
> that bucket that were already sent to user space earlier in the same dump.
> Before this change s_num = num + 1 guaranteed the restart advanced.
>
> There is a second effect: if all of the re-collected leading SKARR_SZ sockets
> are discarded after the lock is dropped (foreign netns, since lhash2 and
> ehash are shared across netns, or sdiag_family/port mismatch, or bytecode
> rejection), nothing is emitted, res stays 0 and the goto resume_*_walk loop
> repeats over the same head with only cond_resched() yielding.
>
> tcp_diag_dump() still loads s_num = num = cb->args[2] on entry and still
> stores cb->args[2] = num at the done: label, but num is now a chunk-relative
> counter that no resume path consumes. Should that state be dropped, or
> should a positional fallback be kept for the mismatch case? Also, since the
> walk can now repeat objects within a dump, should NLM_F_DUMP_INTR be set via
> a generation counter in netlink_callback.seq?
>
> [ ... ]
>
> > @@ -412,34 +517,46 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> > continue;
> > }
> > spin_lock_bh(&ibb->lock);
> > - inet_bind_bucket_for_each(tb2, &ibb->chain) {
> > - if (!net_eq(ib2_net(tb2), net))
> > - continue;
> > -
> > - sk_for_each_bound(sk, &tb2->owners) {
> > - struct inet_sock *inet = inet_sk(sk);
> > -
> > - if (num < s_num)
> > - goto next_bind;
> > -
> > - if (sk->sk_state != TCP_CLOSE ||
> > - !inet->inet_num)
> > - goto next_bind;
> > -
> > - if (r->sdiag_family != AF_UNSPEC &&
> > - r->sdiag_family != sk->sk_family)
> > - goto next_bind;
> > -
> > - if (!inet_diag_bc_sk(cb_data, sk))
> > - goto next_bind;
> > -
> > - sock_hold(sk);
> > - num_arr[accum] = num;
> > - sk_arr[accum] = sk;
> > - if (++accum == SKARR_SZ)
> > + cursor = cb_data->dump_cursor;
> > + use_cursor = cursor &&
> > + cb_data->dump_cursor_type ==
> > + INET_DIAG_DUMP_CURSOR_TCP_BIND &&
> > + cb_data->dump_cursor_slot == i &&
> > + !hlist_unhashed(&cursor->sk_bind_node) &&
> > + cursor->sk_bind_node.pprev != LIST_POISON2;
> > + if (use_cursor) {
> > + tb2 = tcp_diag_sk_bind2(cursor);
> > + use_cursor = tb2 &&
> > + inet_bhashfn_portaddr(hashinfo, cursor,
> > + sock_net(cursor),
> > + inet_sk(cursor)->inet_num) ==
> > + ibb;
> > + }
> > + if (!use_cursor)
> > + s_num = 0;
> > + if (use_cursor) {
> > + sk = cursor;
> > + if (tcp_diag_bind_collect_owners_continue(sk, sk_arr,
> > + num_arr,
> > + &accum,
> > + &num))
> > + goto pause_bind_walk;
> > + hlist_for_each_entry_continue(tb2, node) {
> > + if (tcp_diag_bind_collect_owners(&tb2->owners,
> > + sk_arr,
> > + num_arr,
> > + &accum,
> > + &num, 0))
> > + goto pause_bind_walk;
> > + }
>
> [Severity: Critical]
> Can these two liveness checks ever detect that the cursor left
> tb2->owners, and can tb2 here already be freed?
>
> Removal from a bind chain goes through __sk_del_bind_node():
>
> include/net/sock.h:
> static inline void __sk_del_bind_node(struct sock *sk)
> {
> __hlist_del(&sk->sk_bind_node);
> }
>
> __hlist_del() only writes *pprev and next->pprev; it leaves the removed
> node's own pprev and next untouched. So for a socket that was unlinked,
> hlist_unhashed(&cursor->sk_bind_node) is false and
> cursor->sk_bind_node.pprev != LIST_POISON2 holds, and both tests pass.
>
> The freed-bucket part comes from __inet_bhash2_update_saddr():
>
> net/ipv4/inet_hashtables.c:
> spin_lock(&head2->lock);
> __sk_del_bind_node(sk);
> inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash);
> spin_unlock(&head2->lock);
>
> if (reset)
> inet_reset_saddr(sk);
> else
> inet_update_saddr(sk, saddr, family);
>
> icsk_bind2_hash is never cleared, and inet_bind2_bucket_destroy() frees the
> bucket immediately with no grace period when it was the sole owner:
>
> if (hlist_empty(&tb->owners)) {
> __hlist_del(&tb->node);
> __hlist_del(&tb->bhash_node);
> kmem_cache_free(cachep, tb);
> return;
> }
>
> In the window between that spin_unlock() and the address update, the address
> is unchanged, so inet_bhashfn_portaddr() still resolves to ibb, and
> tcp_diag_sk_bind2(cursor) returns the dangling icsk_bind2_hash. use_cursor
> therefore becomes true.
>
> tcp_diag_bind_collect_owners_continue() then iterates the cursor's stale
> sk_bind_node.next, and hlist_for_each_entry_continue(tb2, node) reads
> tb2->node.next out of the freed slab object, producing an arbitrary
> struct inet_bind2_bucket * whose owners list is walked with sock_hold()
> applied to each apparent entry.
>
> Note the dumper holds only hashinfo->bhash2[i].lock and never the primary
> bhash head lock, and a cursor is saved for every collected owner including
> the ones filtered out afterwards, so a process that dumps with
> TCPF_BOUND_INACTIVE while calling connect() on a bound socket in that bucket
> can reach this. Could the cursor instead record a validated tb2 identity
> that is re-looked-up under ibb->lock, rather than being derived from
> icsk_bind2_hash?
>
> > + } else {
> > + inet_bind_bucket_for_each(tb2, &ibb->chain) {
> > + if (tcp_diag_bind_collect_owners(&tb2->owners,
> > + sk_arr,
> > + num_arr,
> > + &accum,
> > + &num, s_num))
> > goto pause_bind_walk;
> > -next_bind:
> > - num++;
> > }
> > }
> > pause_bind_walk:
>
> [ ... ]
>
> > @@ -463,13 +598,15 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
> > cond_resched();
> >
> > if (accum == SKARR_SZ) {
> > - s_num = num + 1;
> > + s_num = 0;
> > goto resume_bind_walk;
> > }
> >
> > + inet_diag_dump_clear_cursor(cb_data);
> > s_num = 0;
> > }
>
> [ ... ]
>
> > if (accum == SKARR_SZ) {
> > - s_num = num + 1;
> > + s_num = 0;
> > goto next_chunk;
> > }
>
> [ ... ]
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1788248601.git.zihanx%40nebusec.ai
- bind cursor UAF / dangling tb2: valid
- unconditional sock_hold() on unpublished TIME_WAIT: valid
- clearing s_num on cursor mismatch: valid
- lockless cursor-membership checks: valid as a concern
- commit message overstating bind/ehash fill-under-lock: valid
- missing note that MPTCP needs the follow-up patch: valid as a
backport note
- endless re-scan of the same bucket head: not valid as a
guaranteed livelock; duplicate output after a rejected cursor
is valid
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold
2026-09-07 4:22 ` netdev-bot+sashiko
@ 2026-09-07 4:49 ` zihan xi
0 siblings, 0 replies; 10+ messages in thread
From: zihan xi @ 2026-09-07 4:49 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: netdev, linux-kernel, mptcp, davem, edumazet, kuba, pabeni, horms,
ncardwell, kuniyu, matttbe, martineau, geliang, gnault, fw,
stable, vega
On Mon, Sep 7, 2026 at 12:22 PM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 6 potential
> issue(s) to consider:
>
> Critical: 0 · High: 1 · Medium: 3 · Low: 2
>
> - [High] Use-after-free introduced by the new deferred processing loop in
> mptcp_diag_dump_listeners().
> - [Medium] The patch silently changes which MPTCP listeners the dump
> reports, and the commit message does not mention it.
> - [Medium] mptcp_diag_dump_listeners() mixes two incompatible counting
> bases for the resume index diag_ctx->l_num.
> - [Medium] mptcp_diag_dump_listeners() performs all
> identity/authorization checks on the parent MPTCP socket…
> - [Low] Commit message of "mptcp: diag: bound listener bucket lock hold"
> carries Fixes: 4fa39b701ce9 + Cc: stable + Reported-by:, but never…
> - [Low] In the new post-lock loop of mptcp_diag_dump_listeners()
> (net/mptcp/mptcp_diag.c), `tmp = ctx ?
>
> --
>
> mptcp: diag: bound listener bucket lock hold
>
> This reworks mptcp_diag_dump_listeners() so that the listener bucket lock is
> only held while collecting up to MPTCP_DIAG_BULK_SZ referenced listener
> sockets, with the ULP/property re-checks, the parent msk reference and
> sk_diag_dump() moved outside the lock. A referenced dump cursor is kept in
> cb->data so later batches resume after the previous listener instead of
> rescanning the bucket prefix.
>
> v1 Link: https://lore.kernel.org/all/cover.1785307984.git.zihanx@nebusec.ai/
>
> > diff --git a/include/linux/inet_diag.h b/include/linux/inet_diag.h
> > index 6ccd32bc48f91..4859e77a28c7a 100644
> > --- a/include/linux/inet_diag.h
> > +++ b/include/linux/inet_diag.h
> > @@ -39,6 +39,7 @@ enum inet_diag_dump_cursor_type {
> > INET_DIAG_DUMP_CURSOR_TCP_LISTEN,
> > INET_DIAG_DUMP_CURSOR_TCP_BIND,
> > INET_DIAG_DUMP_CURSOR_TCP_EHASH,
> > + INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN,
> > };
>
> [Severity: Low]
> Two questions about the changelog for this one.
>
> First, the message says the lock hold time "can therefore grow with the
> number of sockets visited", but it never states the observed symptom. Was
> this a soft lockup, an RCU stall, or measured contention on ilb->lock? Given
> the Fixes: 4fa39b701ce9 and Cc: stable tags plus the Reported-by:, could the
> symptom be spelled out?
>
> Second, could the backport dependency be mentioned? enum
> inet_diag_dump_cursor_type does not exist in the tree this applies on top of:
>
> git grep inet_diag_dump_cursor_type <base> -> no match
>
> The whole enum, cb_data->dump_cursor, dump_cursor_slot, dump_cursor_type and
> inet_diag_dump_clear_cursor() come from the immediately preceding patch of
> this series, "tcp: diag: bound bucket lock hold in tcp_diag_dump()". Stable
> trees carrying 4fa39b701ce9 cannot take this patch on its own, and nothing in
> the message says so.
>
> > diff --git a/net/mptcp/mptcp_diag.c b/net/mptcp/mptcp_diag.c
> > index 136c2d05c0eeb..37b33ea5d79de 100644
> > --- a/net/mptcp/mptcp_diag.c
> > +++ b/net/mptcp/mptcp_diag.c
> > @@ -12,6 +12,19 @@
> > #include <net/netlink.h>
> > #include "protocol.h"
> >
> > +/* Process a bounded number of listeners per bucket lock hold. */
> > +#define MPTCP_DIAG_BULK_SZ 16
> > +
> > +static void mptcp_diag_save_cursor(struct inet_diag_dump_data *cb_data,
> > + unsigned int slot, struct sock *sk)
> > +{
> > + sock_hold(sk);
> > + inet_diag_dump_clear_cursor(cb_data);
> > + cb_data->dump_cursor = sk;
> > + cb_data->dump_cursor_slot = slot;
> > + cb_data->dump_cursor_type = INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN;
> > +}
> > +
>
> [ ... ]
>
> > @@ -84,64 +98,102 @@ static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callba
> > hinfo = net->ipv4.tcp_death_row.hashinfo;
> >
> > for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
> > + struct sock *tmp, *sk, *sk_arr[MPTCP_DIAG_BULK_SZ];
> > struct inet_listen_hashbucket *ilb;
> > + int num_arr[MPTCP_DIAG_BULK_SZ];
> > struct hlist_nulls_node *node;
> > - struct sock *sk;
> > - int num = 0;
> > + int accum, idx, num, ret;
> > + struct sock *cursor;
> > + bool use_cursor;
> >
> > +resume_listen_walk:
> > + num = 0;
> > + accum = 0;
> > ilb = &hinfo->lhash2[i];
> > + ret = 0;
> >
> > rcu_read_lock();
> > spin_lock(&ilb->lock);
> > - sk_nulls_for_each(sk, node, &ilb->nulls_head) {
> > - const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
> > - struct inet_sock *inet = inet_sk(sk);
> > - int ret;
> > -
> > - if (num < diag_ctx->l_num)
> > - goto next_listen;
> > -
> > - if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
> > - goto next_listen;
> > -
> > - sk = ctx->conn;
> > - if (!sk || !net_eq(sock_net(sk), net))
> > - goto next_listen;
> > -
> > - if (r->sdiag_family != AF_UNSPEC &&
> > - sk->sk_family != r->sdiag_family)
> > - goto next_listen;
> > -
> > - if (r->id.idiag_sport != inet->inet_sport &&
> > - r->id.idiag_sport)
> > + cursor = cb_data->dump_cursor;
> > + use_cursor = cursor &&
> > + cb_data->dump_cursor_type ==
> > + INET_DIAG_DUMP_CURSOR_MPTCP_LISTEN &&
> > + cb_data->dump_cursor_slot == i &&
> > + inet_sk_state_load(cursor) == TCP_LISTEN &&
> > + !hlist_nulls_unhashed(&cursor->sk_nulls_node) &&
> > + cursor->sk_nulls_node.pprev != LIST_POISON2 &&
> > + inet_lhash2_bucket_sk(hinfo, cursor) == ilb;
> > + node = use_cursor ? cursor->sk_nulls_node.next :
> > + ilb->nulls_head.first;
> > + hlist_nulls_for_each_entry_from(sk, node, sk_nulls_node) {
> > + if (!use_cursor && num < diag_ctx->l_num)
> > goto next_listen;
>
> [Severity: Medium]
> Are the two resume mechanisms here using the same counting base for
> diag_ctx->l_num?
>
> This test counts entries from the bucket head, but at resume_listen_walk num
> is reset to 0 and, when use_cursor is true, the walk starts at
> cursor->sk_nulls_node.next, i.e. mid-bucket. The values stashed in num_arr[]
> and later stored are therefore cursor-relative and bounded by 0..15:
>
> if (ret >= 0) {
> ret = sk_diag_dump(tmp, skb, cb, r, net_admin);
> if (ret < 0)
> num = num_arr[idx];
> }
> ...
> if (ret < 0) {
> diag_ctx->l_slot = i;
> diag_ctx->l_num = num;
> return;
> }
>
> Walking a bucket with more than 16 matching listeners:
>
> batch 1: 16 listeners dumped, cursor saved on e15
> goto resume_listen_walk with diag_ctx->l_num = 0, use_cursor true, so num
> now counts from e16
> skb fills at idx 5, so diag_ctx->l_num = 5 while the real bucket offset
> is 21
> the cursor socket is closed before the next recvmsg(), use_cursor becomes
> false, and the walk resumes at bucket position 5
>
> Does that re-emit e5..e19, and, if entries were removed in the meantime and
> positions shifted, silently drop listeners that were never reported?
>
> The peer implementation of the same cursor protocol from the preceding patch,
> tcp_diag_dump(), zeroes the positional counter when the cursor is unusable:
>
> node = use_cursor ? cursor->sk_nulls_node.next : ilb->nulls_head.first;
> if (!use_cursor)
> s_num = 0;
>
> Should mptcp_diag_dump_listeners() do the same so the two bases are never
> mixed? The intra-call restart also forces diag_ctx->l_num = 0 before goto
> resume_listen_walk, so a cursor invalidated between lock drops restarts the
> bucket at the head within a single dump call.
>
> > if (!refcount_inc_not_zero(&sk->sk_refcnt))
> > goto next_listen;
> >
> > - ret = sk_diag_dump(sk, skb, cb, r, net_admin);
> > -
> > - sock_put(sk);
> > -
> > - if (ret < 0) {
> > - spin_unlock(&ilb->lock);
> > - rcu_read_unlock();
> > - diag_ctx->l_slot = i;
> > - diag_ctx->l_num = num;
> > - return;
> > - }
> > - diag_ctx->l_num = num + 1;
> > - num = 0;
>
> [Severity: Medium]
> Removing this "l_num = num + 1; num = 0;" pair changes which listeners the
> dump reports, and the changelog does not mention it.
>
> In the pre-patch loop the skip test was "if (num < diag_ctx->l_num)" while
> num was reset to 0 after every successful dump and l_num kept growing, so for
> a bucket e0..e6 the emitted set was e0, e1, e3, e6:
>
> e0 dumped -> l_num = 1, num = 0 then ++num = 1
> e1 dumped -> l_num = 2, num = 1
> e2 hits 1 < 2 and is skipped
> e3 dumped -> l_num = 3
> e4, e5 skipped
>
> The new code keeps num monotonic and applies the skip test only in the
> non-cursor fallback, so every collected listener is processed. Is that the
> actual user-visible defect being fixed here? ss -Ml output changes (more
> listeners reported) on any host with three or more MPTCP listeners hashing
> into the same lhash2 bucket, and this seems worth stating in the message,
> along with whether it belongs in the same patch as the lock-hold change.
>
> > + num_arr[accum] = num;
> > + sk_arr[accum] = sk;
> > + if (++accum == MPTCP_DIAG_BULK_SZ)
> > + break;
> > next_listen:
> > ++num;
> > }
> > spin_unlock(&ilb->lock);
> > rcu_read_unlock();
> >
> > + for (idx = 0; idx < accum; idx++) {
> > + const struct mptcp_subflow_context *ctx;
> > + const struct tcp_ulp_ops *ulp_ops;
> > + struct inet_sock *inet;
> > +
> > + sk = sk_arr[idx];
> > + rcu_read_lock();
> > + ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
> > + ulp_ops = READ_ONCE(inet_csk(sk)->icsk_ulp_ops);
> > + inet = inet_sk(sk);
> > + tmp = ctx ? ctx->conn : NULL;
>
> [Severity: High]
> Can this read freed memory? The rcu_read_lock() here is entered after
> spin_unlock(&ilb->lock) and rcu_read_unlock() above, so it cannot extend a
> grace period that already started.
>
> The collection loop only took sk_refcnt on the listener subflow, which does
> not keep the ULP context alive:
>
> __tcp_close()
> tcp_set_state(sk, TCP_CLOSE)
> inet_unhash(sk) /* takes ilb->lock, free once diag drops it */
> inet_csk_destroy_sock()
> sk->sk_prot->destroy(sk)
> tcp_v4_destroy_sock()
> tcp_cleanup_ulp()
> subflow_ulp_release()
>
> and net/mptcp/subflow.c:subflow_ulp_release() drops the msk reference and
> frees the context:
>
> sk = ctx->conn;
> if (sk) {
> ...
> sock_put(sk);
> }
> mptcp_subflow_ops_undo_override(ssk);
> if (release)
> kfree_rcu(ctx, rcu);
>
> net/ipv4/tcp_ulp.c:tcp_cleanup_ulp() clears only icsk_ulp_ops:
>
> if (icsk->icsk_ulp_ops->release)
> icsk->icsk_ulp_ops->release(sk);
> module_put(icsk->icsk_ulp_ops->owner);
>
> icsk->icsk_ulp_ops = NULL;
>
> icsk_ulp_data is never unpublished on this path (subflow_ulp_fallback() does
> rcu_assign_pointer(icsk->icsk_ulp_data, NULL), this path does not). If
> kfree_rcu(ctx) was queued before this loop iteration re-entered
> rcu_read_lock(), the grace period does not cover this reader, so
> rcu_dereference(inet_csk(sk)->icsk_ulp_data) can return a freed and reused
> slab object, and ctx->conn is dereferenced before the !ulp_ops guard is even
> evaluated.
>
> The loop is also preemptible for up to 15 preceding sk_diag_dump() /
> inet_diag_bc_sk() invocations with request-supplied bytecode, which widens the
> window. Does the commit message claim ("After dropping the lock, read
> icsk_ulp_data with rcu_dereference()") hold here, or do ctx and the parent msk
> reference have to be acquired while ilb->lock is still held, as the pre-patch
> code did?
>
> [Severity: Low]
> Separately, is it intentional that ctx->conn is loaded before the check that
> the ULP is actually "mptcp"? The collection loop under ilb->lock no longer
> filters by ULP at all, so listeners with a foreign ULP context now reach this
> loop, and the read happens at offsetof(struct mptcp_subflow_context, conn)
> inside an object of an unrelated type.
>
> net/xfrm/espintcp.c:espintcp_init_sk() publishes its own object with
> rcu_assign_pointer(icsk->icsk_ulp_data, ctx) and, unlike tls_init(), has no
> TCP-state restriction, so an unprivileged setsockopt(TCP_ULP, "espintcp")
> followed by listen() puts such a socket into lhash2.
>
> struct espintcp_ctx is larger than that offset today, so the read stays inside
> the allocation and the loaded value is discarded by the failing strcmp().
> Still, the pre-patch code did the strcmp() first and never touched a foreign
> context. Could the ctx->conn load be moved after the ULP name check?
>
> > + if (!ctx || !ulp_ops || strcmp(ulp_ops->name, "mptcp") ||
> > + !tmp || !net_eq(sock_net(tmp), net) ||
> > + (r->sdiag_family != AF_UNSPEC &&
> > + tmp->sk_family != r->sdiag_family) ||
> > + (r->id.idiag_sport != inet->inet_sport &&
> > + r->id.idiag_sport) ||
> > + !refcount_inc_not_zero(&tmp->sk_refcnt)) {
> > + rcu_read_unlock();
> > + goto processed_listener_sk;
> > + }
> > + rcu_read_unlock();
> > + if (ret >= 0) {
> > + ret = sk_diag_dump(tmp, skb, cb, r, net_admin);
> > + if (ret < 0)
> > + num = num_arr[idx];
> > + }
> > + sock_put(tmp);
>
> [Severity: Medium]
> Does this need a re-validation after refcount_inc_not_zero(&tmp->sk_refcnt)?
>
> All the identity and scoping checks on the parent msk (net_eq(sock_net(tmp),
> net), sdiag_family, idiag_sport) run before the reference is taken, and
> nothing is re-checked afterwards. mptcp_prot uses SLAB_TYPESAFE_BY_RCU:
>
> net/mptcp/protocol.c:
> .slab_flags = SLAB_TYPESAFE_BY_RCU,
>
> so rcu_read_lock() only guarantees type stability, not identity. If
> subflow_ulp_release() already dropped the last reference via
> sock_put(ctx->conn), the object can be recycled as a different MPTCP socket
> inside this reader's RCU section, refcount_inc_not_zero() then succeeds, and
> sk_diag_dump(tmp, ...) serializes that socket's addresses, ports, uid, cookie
> and state into the reply, possibly from another namespace.
>
> The in-tree pattern for this slab, net/mptcp/token.c:mptcp_token_get_sock(),
> re-checks after the acquisition:
>
> if (!refcount_inc_not_zero(&sk->sk_refcnt))
> goto not_found;
>
> if (READ_ONCE(msk->token) != token ||
> !net_eq(sock_net(sk), net)) {
>
> Should the same re-check be done here? Pre-patch the conn read and the msk
> refcount acquisition both happened under ilb->lock with the subflow still
> hashed, so the reference held by ctx was still alive.
>
> > +processed_listener_sk:
> > + if (ret >= 0)
> > + mptcp_diag_save_cursor(cb_data, i, sk);
> > + sock_put(sk);
> > + }
> > +
> > + if (ret < 0) {
> > + diag_ctx->l_slot = i;
> > + diag_ctx->l_num = num;
> > + return;
> > + }
> > +
> > cond_resched();
> > +
> > + if (accum == MPTCP_DIAG_BULK_SZ) {
> > + diag_ctx->l_num = 0;
> > + goto resume_listen_walk;
> > + }
> > +
> > + inet_diag_dump_clear_cursor(cb_data);
> > diag_ctx->l_num = 0;
> > }
> >
> > + inet_diag_dump_clear_cursor(cb_data);
> > diag_ctx->l_num = 0;
> > diag_ctx->l_slot = i;
> > }
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1788248601.git.zihanx%40nebusec.ai
- UAF on icsk_ulp_data/ctx after dropping ilb->lock: valid
- mixing cursor-relative and absolute l_num: valid
- dropping "l_num = num + 1; num = 0" changes the reported set:
valid as a behavior change, not a regression
- no re-check after msk refcount_inc_not_zero(): valid for the
lockless path
- changelog/stable dependency on 0001: valid as documentation
- loading ctx->conn before the ULP name check: valid as ordering,
not currently an OOB
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
2026-09-07 4:48 ` zihan xi
@ 2026-09-07 7:05 ` Eric Dumazet
2026-09-07 7:15 ` zihan xi
0 siblings, 1 reply; 10+ messages in thread
From: Eric Dumazet @ 2026-09-07 7:05 UTC (permalink / raw)
To: zihan xi
Cc: netdev-bot+sashiko, netdev, linux-kernel, mptcp, davem, kuba,
pabeni, horms, ncardwell, kuniyu, matttbe, martineau, geliang,
gnault, fw, stable, vega
> - bind cursor UAF / dangling tb2: valid
> - unconditional sock_hold() on unpublished TIME_WAIT: valid
> - clearing s_num on cursor mismatch: valid
> - lockless cursor-membership checks: valid as a concern
> - commit message overstating bind/ehash fill-under-lock: valid
> - missing note that MPTCP needs the follow-up patch: valid as a
> backport note
> - endless re-scan of the same bucket head: not valid as a
> guaranteed livelock; duplicate output after a rejected cursor
> is valid
I just saw your patch today. This is absolutely insane.
Please limit the complexity of a filter to something reasonable.
Absolutely no sane user is using a very complex filter.
Same for UDP.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold
2026-09-01 12:53 ` [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold Zihan Xi
2026-09-07 4:22 ` netdev-bot+sashiko
@ 2026-09-07 7:09 ` Eric Dumazet
1 sibling, 0 replies; 10+ messages in thread
From: Eric Dumazet @ 2026-09-07 7:09 UTC (permalink / raw)
To: Zihan Xi
Cc: netdev, linux-kernel, mptcp, David S . Miller, Jakub Kicinski,
Paolo Abeni, Simon Horman, Neal Cardwell, Kuniyuki Iwashima,
Matthieu Baerts, Mat Martineau, Geliang Tang, Guillaume Nault,
Florian Westphal, stable, Vega
On Tue, Sep 1, 2026 at 2:54 PM Zihan Xi <zihanx@nebusec.ai> wrote:
>
> MPTCP listener diag dumping reuses sk_diag_dump(), which executes
> inet_diag_bc_sk() before filling the netlink reply. The listener walk in
> mptcp_diag_dump_listeners() currently performs that work while holding the
> listener bucket lock.
>
> The time spent under the listener bucket lock can therefore grow with the
> number of sockets visited and with per-socket dump work. The resume state
> also requires later batches to revisit the bucket prefix.
>
> Fix this by collecting only referenced listener sockets while holding the
> bucket lock. After dropping it, re-check the listener properties, obtain
> the parent MPTCP socket reference, and call sk_diag_dump(). Keep a
> referenced cursor so later batches resume after the previous listener
> instead of rescanning the bucket head. Validate a cursor against the
> current listener bucket and TCP_LISTEN state before resuming from it.
> After dropping the lock, read icsk_ulp_data with rcu_dereference().
>
> Fixes: 4fa39b701ce9 ("mptcp: listen diag dump support")
> Cc: stable@vger.kernel.org
> Reported-by: Vega <vega@nebusec.ai>
> Assisted-by: Codex:gpt-5.4
> Signed-off-by: Zihan Xi <zihanx@nebusec.ai>
pw-bot: rejected
Please limit the complexity of filters.
Anyone needing complex filters will implement them in user space.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
2026-09-07 7:05 ` Eric Dumazet
@ 2026-09-07 7:15 ` zihan xi
2026-09-07 7:41 ` zihan xi
0 siblings, 1 reply; 10+ messages in thread
From: zihan xi @ 2026-09-07 7:15 UTC (permalink / raw)
To: Eric Dumazet
Cc: netdev-bot+sashiko, netdev, linux-kernel, mptcp, davem, kuba,
pabeni, horms, ncardwell, kuniyu, matttbe, martineau, geliang,
gnault, fw, stable, vega
On Mon, Sep 7, 2026 at 3:06 PM Eric Dumazet <edumazet@google.com> wrote:
>
> > - bind cursor UAF / dangling tb2: valid
> > - unconditional sock_hold() on unpublished TIME_WAIT: valid
> > - clearing s_num on cursor mismatch: valid
> > - lockless cursor-membership checks: valid as a concern
> > - commit message overstating bind/ehash fill-under-lock: valid
> > - missing note that MPTCP needs the follow-up patch: valid as a
> > backport note
> > - endless re-scan of the same bucket head: not valid as a
> > guaranteed livelock; duplicate output after a rejected cursor
> > is valid
>
> I just saw your patch today. This is absolutely insane.
>
> Please limit the complexity of a filter to something reasonable.
> Absolutely no sane user is using a very complex filter.
>
> Same for UDP.
Thanks for the feedback.
We will drop the dump-cursor rewrite.
A follow-up can cap inet_diag bytecode complexity in
inet_diag_bc_audit() so TCP, UDP and MPTCP dumps share the same limit.
Please let me know if that is the direction you want.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump()
2026-09-07 7:15 ` zihan xi
@ 2026-09-07 7:41 ` zihan xi
0 siblings, 0 replies; 10+ messages in thread
From: zihan xi @ 2026-09-07 7:41 UTC (permalink / raw)
To: Eric Dumazet
Cc: netdev-bot+sashiko, netdev, linux-kernel, mptcp, davem, kuba,
pabeni, horms, ncardwell, kuniyu, matttbe, martineau, geliang,
gnault, fw, stable, vega
On Mon, Sep 7, 2026 at 3:15 PM zihan xi <zihanx@nebusec.ai> wrote:
>
> On Mon, Sep 7, 2026 at 3:06 PM Eric Dumazet <edumazet@google.com> wrote:
> >
> > > - bind cursor UAF / dangling tb2: valid
> > > - unconditional sock_hold() on unpublished TIME_WAIT: valid
> > > - clearing s_num on cursor mismatch: valid
> > > - lockless cursor-membership checks: valid as a concern
> > > - commit message overstating bind/ehash fill-under-lock: valid
> > > - missing note that MPTCP needs the follow-up patch: valid as a
> > > backport note
> > > - endless re-scan of the same bucket head: not valid as a
> > > guaranteed livelock; duplicate output after a rejected cursor
> > > is valid
> >
> > I just saw your patch today. This is absolutely insane.
> >
> > Please limit the complexity of a filter to something reasonable.
> > Absolutely no sane user is using a very complex filter.
> >
> > Same for UDP.
>
> Thanks for the feedback.
>
> We will drop the dump-cursor rewrite.
>
> A follow-up can cap inet_diag bytecode complexity in
> inet_diag_bc_audit() so TCP, UDP and MPTCP dumps share the same limit.
>
> Please let me know if that is the direction you want.
To be clear, this series is dropped. I will send the bytecode cap
as a new series.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-09-07 7:41 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <cover.1788248601.git.zihanx@nebusec.ai>
2026-09-01 12:53 ` [PATCH net v2 1/2] tcp: diag: bound bucket lock hold in tcp_diag_dump() Zihan Xi
2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-07 4:48 ` zihan xi
2026-09-07 7:05 ` Eric Dumazet
2026-09-07 7:15 ` zihan xi
2026-09-07 7:41 ` zihan xi
2026-09-01 12:53 ` [PATCH net v2 2/2] mptcp: diag: bound listener bucket lock hold Zihan Xi
2026-09-07 4:22 ` netdev-bot+sashiko
2026-09-07 4:49 ` zihan xi
2026-09-07 7:09 ` Eric Dumazet
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox