* [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-17 9:43 ` netdev-bot+sashiko
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, 1 reply; 8+ 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 related [flat|nested] 8+ messages in thread* Re: [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering
2026-11-15 15:57 ` [PATCH net v4 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
@ 2026-11-17 9:43 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 9:43 UTC (permalink / raw)
To: 2122295973
Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] The fix is incomplete on the passive-open (sk_clone_lock)
allocation path.
- [Low] The patch is tagged `Cc: stable@vger.kernel.org` with Fixes: tags
pointing to v5.8-era commits (2c5ebd001d4f, 96d890daad05).
--
net: mptcp: take token lookup references with acquire ordering
This switches the refcount increments in mptcp_token_get_sock() and
mptcp_token_iter_next() to refcount_inc_not_zero_acquire(). The re-check of
msk->token and sock_net(sk) is then ordered after the reference is taken on
a SLAB_TYPESAFE_BY_RCU socket that may have been recycled. It follows what
commit 5b5d8f351dd9 did for the IPv4/IPv6 established lookups.
> Fixes: 2c5ebd001d4f ("mptcp: refactor token container")
> Fixes: 96d890daad05 ("mptcp: add msk interations helper")
> Cc: stable@vger.kernel.org
[Severity: Low]
Will this build on the stable trees these Fixes: tags reach?
refcount_inc_not_zero_acquire() was added to include/linux/refcount.h by
commit 7f8ceea0c580 in 2025. Both 2c5ebd001d4f and 96d890daad05 date from
v5.8. LTS branches that don't have 7f8ceea0c580 also don't have the helper.
Could the stable tag name the prerequisite, for example:
Cc: stable@vger.kernel.org # depends on 7f8ceea0c580
> diff --git a/net/mptcp/token.c b/net/mptcp/token.c
> index e0f2823f92efb..3429b43a9f7c3 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)) {
[Severity: High]
Does the acquire ordering also cover the passive-open allocation path?
The commit message covers the sock_init_data_uid() path. On the clone
path, every MPTCP-specific store happens after the refcount is already
live. That includes the token the readers re-check:
sk_clone_lock()
sk_clone()
smp_wmb();
refcount_set(&newsk->sk_refcnt, 2);
mptcp_sk_clone_init()
__mptcp_init_sock(nsk);
...
WRITE_ONCE(msk->token, subflow_req->token);
...
mptcp_set_state(nsk, TCP_ESTABLISHED);
WRITE_ONCE(msk->first, ssk);
list_add(&subflow->node, &msk->conn_list);
...
mptcp_copy_inaddrs(nsk, ssk);
...
if (!mptcp_token_accept(subflow_req, msk)) {
The SLAB_TYPESAFE_BY_RCU comment in include/linux/slab.h says "the newly
allocated object has to be fully initialized *before* its refcount gets
initialized". This path doesn't do that.
Take a reader such as mptcp_diag_dump() walking bucket S through
mptcp_token_iter_next(), holding a chain pointer to msk A. A is closed and
freed, and a clone B for a new incoming MPTCP connection reuses its memory
right away.
refcount_inc_not_zero_acquire() then succeeds on B's refcount of 2. The
following READ_ONCE() of the token can return B's new token simply because
that store has already happened. This is a timing issue, not a reordering
one, so strongly ordered CPUs are affected too.
Even with the later patch in this series ("net: mptcp: verify the token
slot in mptcp_token_iter_next()"), this is the only check after the
increment:
token = READ_ONCE(mptcp_sk(sk)->token);
if (!token || (token & token_mask) != slot ||
!net_eq(sock_net(sk), net)) {
If B's token hashes to slot S, B is returned while it is on no token chain
and is still being built in softirq. Its sk_state may still be the
TCP_LISTEN copied from the listener. msk->first, conn_list and the
addresses may not be set yet. On weakly ordered CPUs, the
__mptcp_init_sock() and pm init stores may not be visible either.
That looks like the "or one that is not initialised yet" outcome the commit
message says this change removes.
mptcp_token_get_sock() seems to have the same gap when B's token matches
the token being looked up. One example is a peer that sends an MP_JOIN for
that token right after its MPC ACK, while a reader walks a stale pointer to
a recycled object in the same bucket. The lookup then returns B before
mptcp_token_accept() has hashed it. The later "net: mptcp: reject token 0
in mptcp_token_get_sock()" patch doesn't change this.
The precedent cited in the commit message, __inet_lookup_established(),
also rejects unhashed sockets after the acquire increment:
if (unlikely(!refcount_inc_not_zero_acquire(&sk->sk_refcnt)))
goto out;
if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) ||
!inet_match(net, sk, acookie, ports, dif,
sdif))) {
sk_clone() calls sk_node_init() before it publishes the refcount, so that
check would reject B.
Should both mptcp sites also re-check
hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) after taking the
reference? Alternatively, could the clone path publish its refcount only
after the MPTCP identity is set?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_D0DCF4B132B20B70FB5AE2D9272AA4AEAF07%40qq.com
^ permalink raw reply [flat|nested] 8+ 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-17 9:43 ` netdev-bot+sashiko
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, 1 reply; 8+ 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 related [flat|nested] 8+ messages in thread* Re: [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
2026-11-15 15:57 ` [PATCH net v4 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
@ 2026-11-17 9:43 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 9:43 UTC (permalink / raw)
To: 2122295973
Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] This patch rests on the claim that a freshly allocated msk
"holds [token 0] until it is hashed".
--
net: mptcp: reject token 0 in mptcp_token_get_sock()
This patch makes mptcp_token_get_sock() return NULL early for token 0.
The commit message says token 0 is what a torn down msk holds, and what a
freshly allocated msk holds until it is hashed. The goal is to stop an
MP_JOIN SYN carrying token 0 from matching a dying or half-initialised msk.
> 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.
> diff --git a/net/mptcp/token.c b/net/mptcp/token.c
> index 3429b43a9f7c3..8a603fc8dc059 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;
> +
[Severity: Medium]
The commit message says a freshly allocated msk holds token 0 "until it is
hashed". Does that hold for the passive MP_CAPABLE path?
In mptcp_sk_clone_init(), the real non-zero token is stored well before
the msk is linked into msk_chain:
net/mptcp/protocol.c:mptcp_sk_clone_init() {
...
WRITE_ONCE(msk->token, subflow_req->token);
...
mptcp_copy_inaddrs(nsk, ssk);
mptcp_rcv_space_init(msk, ssk);
msk->rcvq_space.time = mptcp_stamp();
if (!mptcp_token_accept(subflow_req, msk)) {
...
}
Consider a reader in mptcp_token_get_sock() that holds a stale chain
pointer to an msk whose memory is being reused as this clone, and that is
looking up the same token T. It would see token == T, a refcount of 2
from sk_clone(), and a matching netns.
In that case both READ_ONCE(msk->token) != token checks pass and
refcount_inc_not_zero_acquire() succeeds. Nothing checks whether the
socket is hashed, so an unhashed msk that is still being built is
returned. The new !token check doesn't cover this case.
subflow_token_join_request() then uses that msk without holding its lock:
subflow_check_req()
subflow_token_join_request()
mptcp_token_get_sock()
mptcp_pm_get_local_id()
mptcp_pm_is_backup()
mptcp_pm_announced_has_ssk() (different source port)
subflow_req_create_thmac()
mptcp_pm_get_local_id() could read the listener's addresses copied by
sock_copy(), before mptcp_copy_inaddrs() runs. It could then return the
wrong local ID, or add an implicit endpoint that shouldn't exist.
subflow_req_create_thmac() could also run before the keys are set up.
The token store is a plain WRITE_ONCE() with no release ordering, and the
reader has no matching acquire. On weakly ordered CPUs, could
mptcp_pm_announced_has_ssk() see the new token but still see the
pm.anno_list and pm.lock contents copied from the listener?
If mptcp_token_accept() then fails, mptcp_sk_clone_destroy() tears the msk
down while the join request still holds its reference. Because
mptcp_token_destroy() returns early for unhashed sockets, the msk keeps
token T.
The third patch in this series ("net: mptcp: verify the token slot in
mptcp_token_iter_next()") does not touch mptcp_token_get_sock() or the
clone path, so this case is still open at the end of the series.
Would checking that the socket is hashed after taking the reference work
here? Another option is to publish msk->token under the bucket lock just
before hashing. Either would cover this case as well as the token 0
cases.
> rcu_read_lock();
> bucket = token_bucket(token);
>
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_D0DCF4B132B20B70FB5AE2D9272AA4AEAF07%40qq.com
^ permalink raw reply [flat|nested] 8+ 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-17 9:43 ` netdev-bot+sashiko
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, 1 reply; 8+ 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 related [flat|nested] 8+ messages in thread* Re: [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next()
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-17 9:43 ` netdev-bot+sashiko
0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-11-17 9:43 UTC (permalink / raw)
To: 2122295973
Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet, kuba,
pabeni, horms, kuniyu, willemb, linux-kernel, stable
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 (1) · Low: 2
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] Under the new check in mptcp_token_iter_next()
(net/mptcp/token.c:381-386), a socket passes if its token is non-zero…
- [Low] The commit message says the affected callers are "mptcp_diag and
the userspace path manager".
- [Low] The patch uses a plain READ_ONCE() of msk->token as an
"initialisation complete" signal (net/mptcp/token.c:381), but nothing…
Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but the walk in
mptcp_token_iter_next() (net/mptcp/token.c:370-393) does not follow…
--
net: mptcp: verify the token slot in mptcp_token_iter_next()
This patch makes mptcp_token_iter_next() re-read msk->token after it takes
a reference on a socket. The socket is skipped unless the token is
non-zero and hashes to the slot being walked. The goal is to stop the
iterator from returning a recycled msk whose pm state is not initialised.
> 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.
[Severity: Low]
Is "the userspace path manager" the right caller to name here?
Apart from mptcp_diag_dump(), every caller of mptcp_token_iter_next() is
in net/mptcp/pm_kernel.c:
mptcp_nl_add_subflow_or_signal_addr()
mptcp_nl_remove_subflow_and_signal_addr()
mptcp_nl_remove_id_zero_address()
mptcp_nl_flush_addrs_list()
mptcp_pm_nl_set_flags_all()
Each of them skips userspace PM sockets:
if (mptcp_pm_is_userspace(msk))
goto next;
net/mptcp/pm_userspace.c does not use the iterator. It uses
mptcp_token_get_sock(), which already re-checks the token after taking
the reference.
Could the commit message name the in-kernel path manager endpoint
operations instead?
> diff --git a/net/mptcp/token.c b/net/mptcp/token.c
> index 8a603fc8dc059..48470d6e751d6 100644
> --- a/net/mptcp/token.c
> +++ b/net/mptcp/token.c
[ ... ]
> @@ -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)) {
[Severity: Medium]
Does a non-zero token that maps to this slot really show that the msk is
on this chain and fully set up?
On the passive path, the token is published while the refcount is already
non-zero and before the msk is hashed. First, sk_clone_lock() does:
smp_wmb();
refcount_set(&newsk->sk_refcnt, 2);
Then mptcp_sk_clone_init() in net/mptcp/protocol.c does:
WRITE_ONCE(msk->token, subflow_req->token);
...
mptcp_set_state(nsk, TCP_ESTABLISHED);
WRITE_ONCE(msk->first, ssk);
...
list_add(&subflow->node, &msk->conn_list);
...
mptcp_copy_inaddrs(nsk, ssk);
...
if (!mptcp_token_accept(subflow_req, msk)) {
Here is one way to hit it:
- A diag dump or PM netlink walk holds a stale RCU pointer to a freed msk
in slot S.
- That memory is reused by this clone.
- The new token happens to hash to S.
refcount_inc_not_zero_acquire() then succeeds, and the token is non-zero
and matches the slot. So the iterator returns an msk that is not hashed
yet.
Callers then do lockless reads before lock_sock(): sk_state, conn_list,
and the inet addresses via mptcp_local_address(). These race with the
initialisation above.
The failure path looks worse. mptcp_token_accept() can fail, for example
when mptcp_subflow_reqsk_clone()->mptcp_token_move_request() has moved
the request token during reqsk migration. In that case
mptcp_sk_clone_destroy() does:
bh_unlock_sock(nsk);
sock_put(nsk);
sock_set_flag(nsk, SOCK_DEAD);
tcp_orphan_count_inc();
inet_sk(nsk)->inet_num = 0;
inet_csk_destroy_sock(nsk);
A reader blocked in lock_sock() can get the socket right after
bh_unlock_sock(). Examples are mptcp_nl_remove_subflow_and_signal_addr()
and mptcp_nl_flush_addrs_list(). The same applies to lock_sock_fast() in
mptcp_diag_fill_info().
Can that reader then run pm and subflow operations while
inet_csk_destroy_sock()->mptcp_destroy() tears down the msk without the
socket lock?
Would a check tied to hash membership close this window? Another option
would be to assign msk->token inside mptcp_token_accept() under the
bucket lock.
[Severity: Low]
Is a plain READ_ONCE() enough to treat the token as an "initialisation
done" flag?
On the clone path, __mptcp_init_sock()->mptcp_pm_data_init() runs before
WRITE_ONCE(msk->token, ...) in mptcp_sk_clone_init(). There is no release
barrier between them.
On the reader side, refcount_inc_not_zero_acquire() pairs only with the
smp_wmb()/refcount_set() in sk_clone_lock(). That store comes before any
MPTCP init. After the token load there is only a control dependency.
The callers make some lockless reads before taking the socket lock:
mptcp_pm_is_userspace() (reads pm.pm_type)
list_empty(&msk->conn_list)
READ_ONCE(msk->fully_established)
the pm counters in mptcp_diag_fill_info():
info->mptcpi_extra_subflows = READ_ONCE(msk->pm.extra_subflows);
info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
On arm64 or POWER, can these reads be satisfied before the token load?
They would then see the listener values copied by sock_copy() even though
the token check passed.
Would this need smp_store_release()/smp_load_acquire() on msk->token, or
a hash membership check?
> sock_put(sk);
> continue;
> }
[Severity: Medium]
This isn't a bug introduced by this patch, but the walk in
mptcp_token_iter_next() does not follow the nulls-list protocol that the
other msk_chain readers in net/mptcp/token.c use.
mptcp_token_get_sock() restarts when its post-acquire re-check fails, and
when the walk ends on another chain:
if (READ_ONCE(msk->token) != token ||
!net_eq(sock_net(sk), net)) {
sock_put(sk);
goto again;
}
...
if (get_nulls_value(pos) != (token & token_mask))
goto again;
mptcp_token_exists() does the same nulls check.
The msk cache is SLAB_TYPESAFE_BY_RCU. An msk deleted from slot X and
re-added to slot Y can move a lockless walker onto chain Y.
The new (token & token_mask) != slot test detects that hop, but it
responds with continue. The walker then takes and drops a reference on
each entry of chain Y, and stops at Y's nulls marker.
Are the remaining entries of slot X skipped for this dump or PM pass?
Before this patch the X entries were skipped as well, and Y's entries
were returned instead.
Should a failed slot check restart the bucket walk? And should
get_nulls_value(pos) be compared against slot when the walk ends?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_D0DCF4B132B20B70FB5AE2D9272AA4AEAF07%40qq.com
^ permalink raw reply [flat|nested] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ messages in thread