mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v5 1/3] net: mptcp: take token lookup references with acquire ordering
       [not found] <20261116145829.96849-1-2122295973@qq.com>
@ 2026-11-16 14:58 ` Hang Nan
  2026-11-16 14:58 ` [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-16 14:58 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 v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
       [not found] <20261116145829.96849-1-2122295973@qq.com>
  2026-11-16 14:58 ` [PATCH net v5 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
@ 2026-11-16 14:58 ` Hang Nan
  2026-11-17  6:44   ` kernel test robot
  2026-11-16 14:58 ` [PATCH net v5 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
  2026-11-20 12:17 ` [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
  3 siblings, 1 reply; 7+ messages in thread
From: Hang Nan @ 2026-11-16 14:58 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.  The new KUnit case builds the
state such a socket presents to a reader - hashed, carrying token 0 - and
checks that the lookup does not return it; it fails without the guard.

Fixes: 2c5ebd001d4f ("mptcp: refactor token container")
Cc: stable@vger.kernel.org
Signed-off-by: Hang Nan <2122295973@qq.com>
---
 net/mptcp/token.c      |  4 ++++
 net/mptcp/token_test.c | 51 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 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);
 
diff --git a/net/mptcp/token_test.c b/net/mptcp/token_test.c
index be9acce8a567..2418d392ff7b 100644
--- a/net/mptcp/token_test.c
+++ b/net/mptcp/token_test.c
@@ -132,11 +132,62 @@ static void mptcp_token_test_destroyed(struct kunit *test)
 	mptcp_token_destroy(msk);
 }
 
+static void mptcp_token_test_zero(struct kunit *test)
+{
+	struct mptcp_subflow_request_sock *req = build_req_sock(test);
+	struct mptcp_sock *msk = build_msk(test);
+	struct mptcp_sock *null_msk = NULL;
+	u32 token = 0;
+	int i;
+
+	/* An empty chain does not answer a lookup for token 0 either. */
+	KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, 0));
+
+	/* Build the state a socket being torn down, or one that has just been
+	 * allocated, presents to a reader: hashed and carrying token 0.  A
+	 * lookup for token 0 only reaches a socket that hashes to bucket 0,
+	 * and the token a connection gets is random, so retry until one lands
+	 * there; mptcp_token_exists() reports that without going through the
+	 * guarded lookup.
+	 */
+	for (i = 0; i < (1 << 20); i++) {
+		/* the token is derived from local_key, so vary it */
+		mptcp_subflow_rsk((struct request_sock *)req)->local_key = i + 1;
+
+		if (mptcp_token_new_request((struct request_sock *)req))
+			continue;
+
+		token = req->token;
+		msk->token = token;
+		if (!mptcp_token_accept(req, msk))
+			continue;
+
+		WRITE_ONCE(msk->token, 0);
+		if (mptcp_token_exists(0))
+			break;
+
+		WRITE_ONCE(msk->token, token);
+		mptcp_token_destroy(msk);
+		token = 0;
+	}
+	KUNIT_ASSERT_LT(test, i, 1 << 20);
+	KUNIT_ASSERT_NE(test, token, 0);
+
+	/* msk is hashed in bucket 0 with token 0: the lookup must not
+	 * return it, even though both of its checks would now compare 0
+	 * with 0.
+	 */
+	KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, 0));
+
+	mptcp_token_destroy(msk);
+}
+
 static struct kunit_case mptcp_token_test_cases[] = {
 	KUNIT_CASE(mptcp_token_test_req_basic),
 	KUNIT_CASE(mptcp_token_test_msk_basic),
 	KUNIT_CASE(mptcp_token_test_accept),
 	KUNIT_CASE(mptcp_token_test_destroyed),
+	KUNIT_CASE(mptcp_token_test_zero),
 	{}
 };
 
-- 
2.47.3


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

* [PATCH net v5 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next()
       [not found] <20261116145829.96849-1-2122295973@qq.com>
  2026-11-16 14:58 ` [PATCH net v5 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
  2026-11-16 14:58 ` [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
@ 2026-11-16 14:58 ` Hang Nan
  2026-11-20 12:17 ` [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
  3 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-16 14:58 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 v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
  2026-11-16 14:58 ` [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
@ 2026-11-17  6:44   ` kernel test robot
  0 siblings, 0 replies; 7+ messages in thread
From: kernel test robot @ 2026-11-17  6:44 UTC (permalink / raw)
  To: Hang Nan, netdev
  Cc: oe-kbuild-all, mptcp, matttbe, martineau, geliang, davem,
	edumazet, kuba, pabeni, horms, kuniyu, willemb, linux-kernel,
	stable, 2122295973

Hi Hang,

kernel test robot noticed the following build errors:

[auto build test ERROR on net/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Hang-Nan/net-mptcp-reject-token-0-in-mptcp_token_get_sock/20261116-225828
base:   net/main
patch link:    https://lore.kernel.org/r/tencent_46FC62D4DE8FB8E6C90049F5AB28BDAA8C05%40qq.com
patch subject: [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
config: x86_64-rhel-9.4-kunit (https://download.01.org/0day-ci/archive/20261117/202611171407.G0WgWRA6-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261117/202611171407.G0WgWRA6-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202611171407.G0WgWRA6-lkp@intel.com/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: net/mptcp/mptcp_token_test.ko: symbol 'mptcp_token_exists' undefined!

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

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

* Re: [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups
       [not found] <20261116145829.96849-1-2122295973@qq.com>
                   ` (2 preceding siblings ...)
  2026-11-16 14:58 ` [PATCH net v5 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
@ 2026-11-20 12:17 ` Hang Nan
  3 siblings, 0 replies; 7+ messages in thread
From: Hang Nan @ 2026-11-20 12:17 UTC (permalink / raw)
  To: matttbe
  Cc: netdev, mptcp, martineau, geliang, davem, edumazet, kuba, pabeni,
	horms, kuniyu, willemb, linux-kernel, stable, 2122295973

Hi Matt,

thanks, all points taken.  v6 is prepared but not sent: I will wait for
Netdev Sashiko to finish, as you asked.

- Threading: I see the same thing you do on lore.  The patches are sent
  with git send-email, which sets In-Reply-To to the Message-ID it assigned
  to the cover letter, but our relay rewrites Message-ID on the way out
  (the tencent_*... IDs on lore are not the ones the client wrote), so the
  children end up pointing at an ID that never existed publicly.  I have
  not found a way to make that relay preserve the header; if you have a
  suggestion that works with such a relay I would take it, otherwise I will
  send the next version in a way that does not depend on the rewritten ID.

- Prefix: switched to "mptcp: " in v6.

- KUnit test: dropped.  Beyond your point that a !token guard is not worth
  50 lines, it also caused the build failure the test robot reported - the
  case used mptcp_token_exists(), which is not exported, so
  mptcp_token_test.ko failed at modpost when the test is built as a module.
  The pre-existing token tests pass with the series: pass:4 fail:0.

- For backports I kept the code changes as three separate commits (ordering,
  hash-membership check, token 0), so none of them carries test code.

- token 0: v6 says explicitly that no connection gets token 0 from this
  implementation, and that the RFC 8684 token field does allow zero, so the
  rejection is a Linux-side property, not a protocol one.

Two things I folded in from the Sashiko review of the same series, which
you may want to look at when you do the full review:

- [High] The acquire ordering does not cover the passive-open path, where
  sk_clone() publishes the refcount before mptcp_sk_clone_init() sets the
  token.  v6 adds a separate patch that drops a socket which is no longer
  on the chain after the increment, the way __inet_lookup_established()
  does.

- [Low] The stable tag now names the dependency:
  "Cc: stable@vger.kernel.org" plus a sentence pointing at
  7f8ceea0c580 ("refcount: provide ops for cases when object's memory can
  be reused") in the body.

Cheers,
Hang


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

* Re: [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups
  2026-11-16 14:58 Hang Nan
@ 2026-11-18 10:50 ` Matthieu Baerts
  0 siblings, 0 replies; 7+ messages in thread
From: Matthieu Baerts @ 2026-11-18 10:50 UTC (permalink / raw)
  To: Hang Nan
  Cc: netdev, mptcp, martineau, geliang, davem, edumazet, kuba, pabeni,
	horms, kuniyu, willemb, linux-kernel, stable

Hi Hang Nan,

On 16/11/2026 15:58, Hang Nan wrote:
> 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.

I didn't do a full review of this series because of the build failures,
but here are just a couple of quick notes:

- The patches you send are not linked to the cover-letter, see the
  In-Reply-To [1] pointing to a different Message-ID than the
  cover-letter [2]. Your messages ID are probably modified when sent.

- Please wait for Netdev Sashiko [3] before sending new versions.

- Use 'mptcp:' prefix instead of 'net: mptcp:'

- Split the new kunit tests from the modification in the code to ease
  backports.

- (Honestly, a test for a !token guard is probably not worth 50 lines.
   The first KUNIT_EXPECT_PTR_EQ on the empty table, or no new test at
   all seems more reasonable.)

- (I agree that the Token 0 is never assigned... in Linux: the RFC8684
   accepts it, but the Linux implementation don't. Maybe mention that in
   the commit message to avoid confusions.)

[1] https://lore.kernel.org/tencent_BB8DA72524E32FD374B8B2950B247B62E207@qq.com
[2] https://lore.kernel.org/tencent_50E9A78F95855D54DA67136F3D8520017F0A@qq.com
[3] https://netdev-ai.bots.linux.dev/sashiko/

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.


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

* [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups
@ 2026-11-16 14:58 Hang Nan
  2026-11-18 10:50 ` Matthieu Baerts
  0 siblings, 1 reply; 7+ messages in thread
From: Hang Nan @ 2026-11-16 14:58 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.

None of the three has a runtime trigger.  Patch 1 is argued at the
instruction level (on arm64 the CAS becomes CASA; on x86-64 both variants
compile to the same lock cmpxchg) and KCSAN cannot observe it, since it
does not model reordering of marked accesses.  Patches 2 and 3 are static
arguments; patch 2 now carries a KUnit case that fails without the guard,
which is as far as the state can be constructed without the race.

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).

v4 -> v5:
 - Add a KUnit case for the token 0 guard, as asked by netdev-bot+sinfo.


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 ++++++++---
 net/mptcp/token_test.c | 51 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 3 deletions(-)

-- 
2.47.3


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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20261116145829.96849-1-2122295973@qq.com>
2026-11-16 14:58 ` [PATCH net v5 1/3] net: mptcp: take token lookup references with acquire ordering Hang Nan
2026-11-16 14:58 ` [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() Hang Nan
2026-11-17  6:44   ` kernel test robot
2026-11-16 14:58 ` [PATCH net v5 3/3] net: mptcp: verify the token slot in mptcp_token_iter_next() Hang Nan
2026-11-20 12:17 ` [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups Hang Nan
2026-11-16 14:58 Hang Nan
2026-11-18 10:50 ` Matthieu Baerts

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®