[PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()

From: Hang Nan

Date: Mon Nov 16 2026 - 09:59:13 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Hang Nan <2122295973@xxxxxx>
---
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