[PATCH net v3 4/4] net: mptcp: verify the token slot in mptcp_token_iter_next()
From: Hang Nan
Date: Sat Nov 14 2026 - 08:16:22 EST
mptcp_token_iter_next() takes a reference on a socket found on the chain
of the slot it is walking and then re-checks only net_eq(). A socket
that was freed and whose memory has been reused by a new msk in the same
network namespace passes that check while it is still zeroed, and the
callers - mptcp_diag and the userspace path manager - then use mptcp_pm
state that has not been initialised yet.
The token of a socket on a chain always hashes to the slot being walked,
and token 0 is never assigned, so re-read the token after the acquisition
and drop the socket unless it is non-zero and still maps to this slot.
Fixes: 96d890daad05 ("mptcp: add msk interations helper")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hang Nan <2122295973@xxxxxx>
---
net/mptcp/token.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/net/mptcp/token.c b/net/mptcp/token.c
index 8a603fc8dc05..48470d6e751d 100644
--- a/net/mptcp/token.c
+++ b/net/mptcp/token.c
@@ -355,6 +355,7 @@ struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
struct mptcp_sock *ret = NULL;
struct hlist_nulls_node *pos;
int slot, num = 0;
+ u32 token;
for (slot = *s_slot; slot <= token_mask; *s_num = 0, slot++) {
struct token_bucket *bucket = &token_hash[slot];
@@ -377,7 +378,9 @@ struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt))
continue;
- if (!net_eq(sock_net(sk), net)) {
+ token = READ_ONCE(mptcp_sk(sk)->token);
+ if (!token || (token & token_mask) != slot ||
+ !net_eq(sock_net(sk), net)) {
sock_put(sk);
continue;
}
--
2.47.3