* [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering
[not found] <20261115155747.178858-1-2122295973@qq.com>
@ 2026-11-15 15:57 ` Hang Nan
2026-11-15 15:57 ` [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-15 15:57 UTC (permalink / raw)
To: netdev
Cc: mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable, 2122295973
mptcp_token_get_sock() and mptcp_token_iter_next() take a reference on a
socket found on an RCU-protected hash chain and then re-read state to
decide whether that socket is the one they were looking for. The
re-validation is only meaningful if it is ordered after the reference
acquisition.
The sockets come from mptcp_prot.slab, which is SLAB_TYPESAFE_BY_RCU, so
the memory a reader still points at can be freed and reallocated as a new
mptcp socket while the reader is between the chain walk and the
increment. The new owner publishes itself in sock_init_data_uid() with
smp_wmb();
refcount_set(&sk->sk_refcnt, 1);
and initialises the rest of the socket only afterwards. A reader whose
increment does not carry acquire ordering can have the re-read of
msk->token and sock_net(sk) satisfied before the increment, that is
against the old object's contents, while the increment itself succeeds on
the new one. The lookup would then hand back a socket that belongs to a
different connection, or one that is not initialised yet.
refcount_inc_not_zero() has not provided that ordering since
commit fb041bb7c0a9 ("locking/refcount: Consolidate implementations of refcount_t"),
which removed the ordering the previous atomic_add_unless() based
implementation gave; refcount.h names this exact case as the reason the
acquire variants exist.
commit 5b5d8f351dd9 ("net: restore refcount ordering for lockless socket access")
restored the ordering for the IPv4/IPv6 established lookups,
inet_twsk_purge() and bpf_sk_assign(). These two mptcp sites have the
same shape and are converted as well. There is no behaviour change on
strongly ordered CPUs.
Fixes: 2c5ebd001d4f ("mptcp: refactor token container")
Fixes: 96d890daad05 ("mptcp: add msk interations helper")
Cc: stable@vger.kernel.org
Signed-off-by: Hang Nan <2122295973@qq.com>
---
net/mptcp/token.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/mptcp/token.c b/net/mptcp/token.c
index e0f2823f92ef..3429b43a9f7c 100644
--- a/net/mptcp/token.c
+++ b/net/mptcp/token.c
@@ -309,7 +309,7 @@ struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token)
!net_eq(sock_net(sk), net))
continue;
- if (!refcount_inc_not_zero(&sk->sk_refcnt))
+ if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt))
goto not_found;
if (READ_ONCE(msk->token) != token ||
@@ -370,7 +370,7 @@ struct mptcp_sock *mptcp_token_iter_next(const struct net *net, long *s_slot,
if (num <= *s_num)
continue;
- if (!refcount_inc_not_zero(&sk->sk_refcnt))
+ if (!refcount_inc_not_zero_acquire(&sk->sk_refcnt))
continue;
if (!net_eq(sock_net(sk), net)) {
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
[not found] <20261115155747.178858-1-2122295973@qq.com>
2026-11-15 15:57 ` [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
@ 2026-11-15 15:57 ` Hang Nan
2026-11-15 15:57 ` [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-15 15:57 UTC (permalink / raw)
To: netdev
Cc: mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable, 2122295973
Token 0 is never assigned to a connection: __token_bucket_busy() treats
it as busy. It is, however, the value mptcp_token_destroy() stores into a
socket that is being torn down, and the value a freshly allocated msk
holds until it is hashed.
Both validations in mptcp_token_get_sock() compare the socket's token
with the requested one, so for a request for token 0 they cannot tell a
match apart from a socket whose token has just been cleared. With
SLAB_TYPESAFE_BY_RCU the same is true for a socket whose memory has
already been reused by a new msk that has not been hashed yet.
The token comes from the wire: mptcp_parse_option() copies the MP_JOIN
SYN token without validating it. A lookup for token 0 can therefore
return a dying or half-initialised msk, which subflow_token_join_request()
then hands to mptcp_pm_get_local_id() and, when the subflow uses a
different source port, to mptcp_pm_announced_has_ssk(), which walks
msk->pm.anno_list. That list is only initialised later, from
mptcp_pm_data_init().
Reject token 0 before walking the chain.
Fixes: 2c5ebd001d4f ("mptcp: refactor token container")
Cc: stable@vger.kernel.org
Signed-off-by: Hang Nan <2122295973@qq.com>
---
net/mptcp/token.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/net/mptcp/token.c b/net/mptcp/token.c
index 3429b43a9f7c..8a603fc8dc05 100644
--- a/net/mptcp/token.c
+++ b/net/mptcp/token.c
@@ -299,6 +299,10 @@ struct mptcp_sock *mptcp_token_get_sock(struct net *net, u32 token)
struct mptcp_sock *msk;
struct sock *sk;
+ /* Token 0 is never assigned, but is what a torn down socket holds. */
+ if (!token)
+ return NULL;
+
rcu_read_lock();
bucket = token_bucket(token);
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next()
[not found] <20261115155747.178858-1-2122295973@qq.com>
2026-11-15 15:57 ` [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
2026-11-15 15:57 ` [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
@ 2026-11-15 15:57 ` Hang Nan
2026-11-16 12:20 ` [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
2026-11-16 14:58 ` Hang Nan
4 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-15 15:57 UTC (permalink / raw)
To: netdev
Cc: mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable, 2122295973
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@vger.kernel.org
Signed-off-by: Hang Nan <2122295973@qq.com>
---
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
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups
[not found] <20261115155747.178858-1-2122295973@qq.com>
` (2 preceding siblings ...)
2026-11-15 15:57 ` [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
@ 2026-11-16 12:20 ` Hang Nan
2026-11-16 14:58 ` Hang Nan
4 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-16 12:20 UTC (permalink / raw)
To: netdev-bot+sinfo
Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet,
kuba, pabeni, horms, kuniyu, willemb, linux-kernel, stable,
2122295973
Thanks for the prompt. Honest answer up front: none of the three patches
has a runtime trigger. All three are theoretical, and this is what I
should have put in the commit messages.
1/3 net: mptcp: take token lookup references with acquire ordering
Theoretical. There is no syzbot report, no KCSAN splat and no
reproducer. KCSAN cannot observe this class of defect at all:
Documentation/dev-tools/kcsan.rst is explicit that watchpoints are
only set up for plain accesses ("KCSAN never sets up a watchpoint on
marked accesses") and that "reordering of marked accesses is not
modeled", and the reordering in question involves a marked refcount
operation. On x86-64 there is nothing to observe anyway: both
variants compile to the same lock cmpxchg. The evidence is
instruction-level: on arm64 the CAS becomes CASA
(88a37c02 cas -> 88e17c43 casa), which I can attach if wanted. What
I did confirm is only reachability of the lookup path from the wire
(mptcp_parse_option() -> subflow_token_join_request() ->
mptcp_token_get_sock()), which is a static reading, not a runtime
demonstration of the reordering.
2/3 net: mptcp: reject token 0 in mptcp_token_get_sock()
Theoretical, and found by static analysis while addressing a review
comment on 1/3, not by fuzzing. The crash path I describe
(subflow_check_req() -> mptcp_pm_announced_has_ssk() walking
msk->pm.anno_list before mptcp_pm_data_init() has run) is argued from
code; I have not triggered it and have no stack trace or KASAN
report for it. What is verified is the input side: the token is
copied from the MP_JOIN option without validation, and 0 is never
assigned to a live connection, so a lookup for 0 relies entirely on
the two comparisons that cannot distinguish it.
3/3 net: mptcp: verify the token slot in mptcp_token_iter_next()
Theoretical as well. Both consumers (mptcp_diag and the userspace
path manager) are netlink paths that need privileges in the target
network namespace, so this is not a remote attack surface in the
first place.
For 2/3 I will add a KUnit case to net/mptcp/token_test.c
(mptcp_token_get_sock(&init_net, 0) must return NULL) in the next
revision, so at least the guard has a regression test. I am not
reposting only for this message; if reviewers want the above folded into
the cover letter, I will include it with the next respin.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups
[not found] <20261115155747.178858-1-2122295973@qq.com>
` (3 preceding siblings ...)
2026-11-16 12:20 ` [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
@ 2026-11-16 14:58 ` Hang Nan
4 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-16 14:58 UTC (permalink / raw)
To: netdev-bot+sinfo
Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet,
kuba, pabeni, horms, kuniyu, willemb, linux-kernel, stable,
2122295973
Thanks. The missing information is now in the cover letter of v5, which I
am posting for a different reason as well (a new KUnit case, see below),
so this is not a repost just to answer this mail.
Short version, per patch:
1/3 acquire ordering: theoretical. No syzbot report, no KCSAN splat, no
reproducer. KCSAN cannot observe it (it does not model reordering of
marked accesses), and on x86-64 both variants compile to the same
lock cmpxchg, so there is nothing to observe on that hardware. The
evidence is instruction level: on arm64 the CAS becomes CASA.
2/3 token 0: theoretical, found by static analysis. It now carries a
KUnit case, which is the closest thing to a trigger that exists
without winning the race: the test builds the state a dying or
freshly allocated socket presents to a reader (hashed in bucket 0,
carrying token 0) and asserts that the lookup does not return it.
# Subtest: mptcp-token
ok 1 mptcp_token_test_req_basic
ok 2 mptcp_token_test_msk_basic
ok 3 mptcp_token_test_accept
ok 4 mptcp_token_test_destroyed
ok 5 mptcp_token_test_zero
# mptcp-token: pass:5 fail:0 skip:0 total:5
With the guard reverted, the same kernel reports
# mptcp_token_test_zero: EXPECTATION FAILED at net/mptcp/token_test.c:180
not ok 5 mptcp_token_test_zero
# mptcp-token: pass:4 fail:1 skip:0 total:5
so the case does fail without the fix; it is not a test that passes
either way.
3/3 iterator slot check: theoretical as well, and the consumers are
netlink paths that need privileges in the target network namespace.
Both runs are a defconfig x86-64 kernel with CONFIG_MPTCP=y and
CONFIG_MPTCP_KUNIT_TEST=y, booted under qemu with
kunit.filter_glob=mptcp-token.
^ permalink raw reply [flat|nested] 7+ messages in thread