mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ 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] 6+ 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
  2026-11-16 12:20 ` [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
  3 siblings, 0 replies; 6+ 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] 6+ 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
  3 siblings, 0 replies; 6+ 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] 6+ 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
  3 siblings, 0 replies; 6+ 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] 6+ messages in thread

* Re: [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups
  2026-11-15 15:57 Hang Nan
@ 2026-11-15 20:12 ` netdev-bot+sinfo
  0 siblings, 0 replies; 6+ messages in thread
From: netdev-bot+sinfo @ 2026-11-15 20:12 UTC (permalink / raw)
  To: Hang Nan
  Cc: netdev, mptcp, matttbe, martineau, geliang, davem, edumazet,
	kuba, pabeni, horms, kuniyu, willemb, linux-kernel, stable

Hi!

This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:

 - Whether the issue was actually triggered, or is only theoretical
   (e.g. found by LLM and not proven at runtime). If it was triggered
   please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
-- 
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups
@ 2026-11-15 15:57 Hang Nan
  2026-11-15 20:12 ` netdev-bot+sinfo
  0 siblings, 1 reply; 6+ 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

From: NanHang <2122295973@qq.com>

This series restores acquire ordering for the mptcp token lookup and fixes
two pre-existing defects in that hash table which ordering alone cannot
cover.

Patch 1 converts mptcp_token_get_sock() and mptcp_token_iter_next() to
refcount_inc_not_zero_acquire().  These sockets live in a
SLAB_TYPESAFE_BY_RCU slab, so the re-check of msk->token and sock_net()
after the increment only rejects a recycled socket if those loads are
ordered after the increment; without the acquire they can be satisfied
before it, against the previous owner's contents.

Patch 2 rejects token 0 in mptcp_token_get_sock().  Token 0 is never
assigned, but it is what mptcp_token_destroy() stores into a socket being
torn down, and what a freshly allocated msk holds until it is hashed.
For a lookup for token 0 both validations compare 0 with 0 and accept
such a socket; the token is copied from the wire by mptcp_parse_option(),
so a peer can ask for it.  The join path then uses the result, including
msk->pm.anno_list, which is initialised only later.

Patch 3 makes mptcp_token_iter_next() re-read the token after the
increment and drop the socket unless it is non-zero and still hashes to
the slot being walked, so a socket recycled in the same network namespace
is not handed to mptcp_diag or the userspace path manager before it is
initialised.

v1 -> v2:
 - Point the Fixes: tags at the commits that introduced each lockless
   lookup instead of at the refcount consolidation (sashiko review).

v2 -> v3:
 - Rewrite the rationale of the ordering patch: the acquire pairs with
   the SLAB_TYPESAFE_BY_RCU reuse window published by
   sock_init_data_uid(), not with mptcp_token_destroy() (sashiko review).
 - Add the token 0 and iterator patches, reported by the same review.

v3 -> v4:
 - Drop "net: core: take the reuseport migration reference with acquire
   ordering".  reuseport_migrate_sock() does not validate anything after
   the increment, and the listener state that could be raced is re-checked
   under queue->rskq_lock in inet_csk_reqsk_queue_add(), so the acquire
   orders nothing there (sashiko review).


Hang Nan (3):
  net: mptcp: take token lookup references with acquire ordering
  net: mptcp: reject token 0 in mptcp_token_get_sock()
  net: mptcp: verify the token slot in mptcp_token_iter_next()

 net/mptcp/token.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-16 12:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [PATCH net v4 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
2026-11-16 12:20 ` [PATCH net v4 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
2026-11-15 15:57 Hang Nan
2026-11-15 20:12 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®