net: mptcp: take token lookup references with acquire ordering
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v5 1/3 net: mptcp: take token lookup references with acquire ordering
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.
--- net/mptcp/token.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/mptcp/token.c +2 -2
@@ -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)) {
Signature · 1 line
2.47.3
PATCH v5 2/3 net: mptcp: reject token 0 in mptcp_token_get_sock()
In reply to a message not in the archiveToken 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.
--- net/mptcp/token.c | 4 ++++ net/mptcp/token_test.c | 51 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+)
net/mptcp/token.c +4 -0
@@ -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);
net/mptcp/token_test.c +51 -0
62 lines not on this page · raw message
Signature · 1 line
2.47.3
kernel test robot
build botRe: [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
kernel test robot noticed the following build errors:
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/[email protected]/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/[email protected]/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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: net/mptcp/mptcp_token_test.ko: symbol 'mptcp_token_exists' undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
[email protected]
AI reviewRe: [PATCH net v5 2/3] net: mptcp: reject token 0 in mptcp_token_get_sock()
In reply to Hang NanThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mptcp: missing EXPORT_SYMBOL_GPL for mptcp_token_exists() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mptcp: missing EXPORT_SYMBOL_GPL for mptcp_token_exists() --
commit 58d873c76950c5892687659dc3383bb38a22332f
Author: Hang Nan <[email protected]>
net: mptcp: reject token 0 in mptcp_token_get_sock()
This commit rejects token 0 in mptcp_token_get_sock() so that lookups cannot match sockets that are being torn down or have not been hashed yet, and adds a KUnit test covering this case.
Quoted 31 lines
net/mptcp/token_test.c +23
-0@@ -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))
net/mptcp/token_test.c static void mptcp_token_test_destroyed(struct kunit *test) · line 167
Quoted 10 lines
net/mptcp/token_test.c +10
-0+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 ;
[Severity: Medium]
Does calling mptcp_token_exists() here break the build when CONFIG_MPTCP_KUNIT_TEST=m?
MPTCP is built-in while mptcp_token_test can be built as a module, and unlike the other token helpers in net/mptcp/token.c, mptcp_token_exists() is not exported under #if IS_MODULE(CONFIG_MPTCP_KUNIT_TEST), causing modpost to fail with an undefined symbol error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/3 net: mptcp: verify the token slot in mptcp_token_iter_next()
In reply to a message not in the archivemptcp_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.
--- net/mptcp/token.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/mptcp/token.c +4 -1
@@ -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 ; }
Signature · 1 line
2.47.3
Re: [PATCH net v5 0/3] mptcp token hash ordering and token 0 lookups
In reply to a message not in the archiveHi 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: [email protected]" plus a sentence pointing at 7f8ceea0c580 ("refcount: provide ops for cases when object's memory can be reused") in the body.
Cheers,
Hang