Skip to contentSkip to conversation

mptcp: hold msk reference when cloning request sockets

PATCH In review David Lee 4 participants 5 messages Aug 4 – Aug 5

Subsystems (5)
lore
Share & tools
lore

David Lee

author
#0 lore raw

PATCH 1/1 mptcp: hold msk reference when cloning request sockets

From: Kyle Zeng <[email protected]>

An MP_JOIN request owns the reference stored in subflow_req->msk. inet_reqsk_clone() byte-copies that pointer when migrating a request, but does not acquire a reference for the clone. The original and cloned request destructors can consequently drop the same reference, leaving one request with a dangling msk pointer.

Let cloned MPTCP requests take their own msk reference. The source request still owns its reference while it is being cloned, so sock_hold() is safe. The clone's normal destructor balances the new reference on both successful and failed migration paths.

Fixes c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")
Assisted-by Codex:gpt-5.6-sol Codex:gpt-5.5-cyber

---
Bug found and triaged by OpenAI Security Research and validated by Trail of Bits.

Trail of Bits has a reproducer for this bug that triggers a KASAN use-after-free and can share if needed.

 include/net/mptcp.h             |  5 +++++
 net/ipv4/inet_connection_sock.c | 10 ++++++++--
 net/mptcp/subflow.c             |  8 ++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)
include/net/mptcp.h +5 -0
index 71b9fc5a5..51d4b02f5 100644
@@ -223,6 +223,7 @@ int mptcp_subflow_init_cookie_req(struct request_sock *req,
 struct request_sock *mptcp_subflow_reqsk_alloc(const struct request_sock_ops *ops,
 					       struct sock *sk_listener,
 					       bool attach_listener);
+void mptcp_subflow_reqsk_clone(struct request_sock *req);
 
 __be32 mptcp_get_reset_option(const struct sk_buff *skb);
 
@@ -309,6 +310,10 @@ static inline struct request_sock *mptcp_subflow_reqsk_alloc(const struct reques
 	return NULL;
 }
 
+static inline void mptcp_subflow_reqsk_clone(struct request_sock *req)
+{
+}
+
 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)  { return htonl(0u); }
 
 static inline void mptcp_active_detect_blackhole(struct sock *sk, bool expired) { }
net/ipv4/inet_connection_sock.c +8 -2
index 56902bba5..f3ef9c15a 100644
@@ -17,6 +17,7 @@
 #include <net/inet_timewait_sock.h>
 #include <net/ip.h>
 #include <net/route.h>
+#include <net/mptcp.h>
 #include <net/tcp_states.h>
 #include <net/xfrm.h>
 #include <net/tcp.h>
@@ -946,8 +947,13 @@ static struct request_sock *inet_reqsk_clone(struct request_sock *req,
 	/* We need not acquire fastopenq->lock
 	 * because the child socket is locked in inet_csk_listen_stop().
 	 */
-	if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(nreq)->tfo_listener)
-		rcu_assign_pointer(tcp_sk(nreq->sk)->fastopen_rsk, nreq);
+	if (sk->sk_protocol == IPPROTO_TCP) {
+		if (tcp_rsk(nreq)->tfo_listener)
+			rcu_assign_pointer(tcp_sk(nreq->sk)->fastopen_rsk, nreq);
+
+		if (rsk_is_mptcp(req))
+			mptcp_subflow_reqsk_clone(nreq);
+	}
 
 	return nreq;
 }
net/mptcp/subflow.c +8 -0
index 8e386899c..1b6aa5e8c 100644
@@ -47,6 +47,14 @@ static void subflow_req_destructor(struct request_sock *req)
 	mptcp_token_destroy_request(req);
 }
 
+void mptcp_subflow_reqsk_clone(struct request_sock *req)
+{
+	struct mptcp_subflow_request_sock *subflow_req = mptcp_subflow_rsk(req);
+
+	if (subflow_req->msk)
+		sock_hold((struct sock *)subflow_req->msk);
+}
+
 static void subflow_generate_hmac(u64 key1, u64 key2, u32 nonce1, u32 nonce2,
 				  void *hmac)
 {
Signature · 1 line
2.47.3
#1 lore raw

Hi Kyle,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/30899232462

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/0a0e78978315
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1139981

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)

maintainer
#2 lore raw
In reply to David Lee

Hi David, Kyle,

(+cc Vega's people)

Quoted 8 lines · David Lee

From: Kyle Zeng <[email protected]>

An MP_JOIN request owns the reference stored in subflow_req->msk. inet_reqsk_clone() byte-copies that pointer when migrating a request, but does not acquire a reference for the clone. The original and cloned request destructors can consequently drop the same reference, leaving one request with a dangling msk pointer.

Quoted 6 lines

Let cloned MPTCP requests take their own msk reference. The source request still owns its reference while it is being cloned, so sock_hold() is safe. The clone's normal destructor balances the new reference on both successful and failed migration paths.

Fixes c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")

Thank you for this patch. It looks like it is similar to this one sent a few months ago, but where changes have been requested:

https://lore.kernel.org/40fd38e7a368e5b7bc9bc83364a32241f977d53f.1778404619.git.caoruide123@gmail.com

Do you mind checking what they did, and explaining the different approach, please?

It seems there are two issues the Vega's team tried to solve: one with MP_JOIN requests (what you are trying to fix here) and with MP_CAPABLE. For me, it is fine to split that in two patches. I also don't mind who is writing the final patch(es), as long as credits are given. If you, David/Kyle, are doing that, it might be OK to add a:

@Vega's team: OK with that? Or are you actively working on a v3?

One last thing: it looks like your patch is conflicting with this one:

https://lore.kernel.org/netdev/[email protected]/

Quoted 11 lines
Assisted-by Codex:gpt-5.6-sol Codex:gpt-5.5-cyber

---
Bug found and triaged by OpenAI Security Research and validated by Trail of Bits.

Trail of Bits has a reproducer for this bug that triggers a KASAN use-after-free and can share if needed.

I think it would be good to include the (decoded) KASAN warning in the commit message. Regarding the reproducer, if it is with packetdrill, I think it is always useful to share it. For others, don't hesitate to share it to the same people: it can help better understanding the issue. Maybe remove the mailing list when doing that if it is security related?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#3 lore raw
Quoted 34 lines · Matthieu Baerts

Hi David, Kyle,

(+cc Vega's people)

Quoted 8 lines · David Lee

From: Kyle Zeng <[email protected]>

An MP_JOIN request owns the reference stored in subflow_req->msk. inet_reqsk_clone() byte-copies that pointer when migrating a request, but does not acquire a reference for the clone. The original and cloned request destructors can consequently drop the same reference, leaving one request with a dangling msk pointer.

Quoted 6 lines

Let cloned MPTCP requests take their own msk reference. The source request still owns its reference while it is being cloned, so sock_hold() is safe. The clone's normal destructor balances the new reference on both successful and failed migration paths.

Fixes c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")

Thank you for this patch. It looks like it is similar to this one sent a few months ago, but where changes have been requested:

https://lore.kernel.org/40fd38e7a368e5b7bc9bc83364a32241f977d53f.1778404619.git.caoruide123@gmail.com

Do you mind checking what they did, and explaining the different approach, please?

It seems there are two issues the Vega's team tried to solve: one with MP_JOIN requests (what you are trying to fix here) and with MP_CAPABLE. For me, it is fine to split that in two patches. I also don't mind who is writing the final patch(es), as long as credits are given. If you, David/Kyle, are doing that, it might be OK to add a:

Quoted 4 lines

@Vega's team: OK with that? Or are you actively working on a v3?

We seem to have lost track of this patch, and we sincerely apologize.

Our patch addresses two issues: one involving MP_JOIN requests and the other involving MP_CAPABLE.

I reviewed Kyle and David’s fix for the MP_JOIN issue, and it looks reasonable to me.

Ruide and I also do not have a preference regarding whose patch is accepted.

If David/Kyle' patch is accepted, please add:

Btw, Kyle and David, would you be open to collaborating with us on Linux kernel patches? Over the past few months, our team has fixed more than 100 high-severity vulnerabilities in the kernel. We’ve learned a great deal from maintainers’ feedback and have built up extensive experience. We’d love to help improve kernel security while minimizing the burden on maintainers as much as possible.

Quoted 28 lines

One last thing: it looks like your patch is conflicting with this one:

https://lore.kernel.org/netdev/[email protected]/

Quoted 11 lines
Assisted-by Codex:gpt-5.6-sol Codex:gpt-5.5-cyber

---
Bug found and triaged by OpenAI Security Research and validated by Trail of Bits.

Trail of Bits has a reproducer for this bug that triggers a KASAN use-after-free and can share if needed.

I think it would be good to include the (decoded) KASAN warning in the commit message. Regarding the reproducer, if it is with packetdrill, I think it is always useful to share it. For others, don't hesitate to share it to the same people: it can help better understanding the issue. Maybe remove the mailing list when doing that if it is security related?

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

maintainer
#4 lore raw

Hi Yuan,

Thank you for your reply.

Quoted 40 lines · Yuan Tan
Quoted 34 lines · Matthieu Baerts

Hi David, Kyle,

(+cc Vega's people)

Quoted 8 lines · David Lee

From: Kyle Zeng <[email protected]>

An MP_JOIN request owns the reference stored in subflow_req->msk. inet_reqsk_clone() byte-copies that pointer when migrating a request, but does not acquire a reference for the clone. The original and cloned request destructors can consequently drop the same reference, leaving one request with a dangling msk pointer.

Quoted 6 lines

Let cloned MPTCP requests take their own msk reference. The source request still owns its reference while it is being cloned, so sock_hold() is safe. The clone's normal destructor balances the new reference on both successful and failed migration paths.

Fixes c905dee62232 ("tcp: Migrate TCP_NEW_SYN_RECV requests at retransmitting SYN+ACKs.")

Thank you for this patch. It looks like it is similar to this one sent a few months ago, but where changes have been requested:

https://lore.kernel.org/40fd38e7a368e5b7bc9bc83364a32241f977d53f.1778404619.git.caoruide123@gmail.com

Do you mind checking what they did, and explaining the different approach, please?

It seems there are two issues the Vega's team tried to solve: one with MP_JOIN requests (what you are trying to fix here) and with MP_CAPABLE. For me, it is fine to split that in two patches. I also don't mind who is writing the final patch(es), as long as credits are given. If you, David/Kyle, are doing that, it might be OK to add a:

Quoted 4 lines

@Vega's team: OK with that? Or are you actively working on a v3?

Quoted 7 lines

We seem to have lost track of this patch, and we sincerely apologize.

Our patch addresses two issues: one involving MP_JOIN requests and the other involving MP_CAPABLE.

I reviewed Kyle and David’s fix for the MP_JOIN issue, and it looks reasonable to me.

Please note that Clashiko reported an issue. A new version *might* be required:

https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260804095051.715355-1-david.lee%40trailofbits.com

Note that both Clashiko and Sashiko are mentioning the other existing issue with MP_CAPABLE, that the Vega's team tried to fix in their v2:

https://sashiko.dev/#/patchset/20260804095051.715355-1-david.lee%40trailofbits.com

I think the fix can be done in two parts.

Quoted 7 lines

Ruide and I also do not have a preference regarding whose patch is accepted.

If David/Kyle' patch is accepted, please add:

If a new version is required, please sync on who is doing what. Let's wait for Kyle and David's reply. (In case of "timeout" (after a few days), feel free to work on the v3.)

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.