mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hang Nan <2122295973@qq.com>
To: netdev@vger.kernel.org
Cc: mptcp@lists.linux.dev, matttbe@kernel.org, martineau@kernel.org,
	geliang@kernel.org, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	kuniyu@google.com, willemb@google.com,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	2122295973@qq.com
Subject: [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
Date: Mon, 16 Nov 2026 22:58:28 +0800	[thread overview]
Message-ID: <tencent_46FC62D4DE8FB8E6C90049F5AB28BDAA8C05@qq.com> (raw)
In-Reply-To: <20261116145829.96849-1-2122295973@qq.com>

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


  parent reply	other threads:[~2026-11-16 14:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2026-11-17  6:44   ` [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock() 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_46FC62D4DE8FB8E6C90049F5AB28BDAA8C05@qq.com \
    --to=2122295973@qq.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=geliang@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martineau@kernel.org \
    --cc=matttbe@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®