Skip to contentSkip to conversation

mptcp: pm: fix reachable extra_subflows guards

PATCH Superseded Tao Cui 4 participants 8 messages Sep 3

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

v2 cover mptcp: pm: fix reachable extra_subflows guards

From: Tao Cui <[email protected]>

Hi,

Following up on issue #629, here is a fix series for the issues reported against e99c1ca871 ("mptcp: pm: add WARN_ON_ONCE guards on extra_subflows underflow").

Patch 1 bounds the userspace PM admission at U8_MAX, so the u8 can no longer be wrapped by a peer establishing more than 255 subflows.

Patch 2 addresses the disconnect() vs MP_JOIN race: as discussed, the decrement sites now check the msk state first and skip the accounting when the msk is already in TCP_CLOSE, as the state is set before mptcp_pm_data_reset() clears the counters, and once the msk is closed the accounting is not relevant anymore. No new lock is involved. The guards themselves are downgraded to a clamp with a rate-limited pr_warn(), as the warn was reachable and turned into a remotely triggerable panic on panic_on_warn kernels.

Patch 3, from Quanye, applies the same bound to the Netlink subflow creation path, with a goto to the error cleanup as suggested during the review.

Changes since v1 [1]:

- Patch 1: re-use "ret" instead of a new variable, drop the comment, Co-developed-by added for Quanye, who sent the same fix independently
- issue reference: Link: on patches 1 and 2, Closes: on the last one
- New patch 3 from Quanye for the Netlink path, per the discussion with Matt [2]

Validated with the virtme CI (mptcp selftests), and with a stress repro comparing the base kernel and the patched one: on the base kernel the server accepts 256 MP_JOINs and the counter wraps to 0, with the patches admission stops at 255 and the leftover imbalance only shows up as a rate-limited warning.

[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Quanye Yang (1):
mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

Tao Cui (2):
mptcp: pm: bound extra_subflows admission on userspace PM
mptcp: pm: skip extra_subflows accounting on disconnected msk

 net/mptcp/pm.c           | 17 ++++++++++++++---
 net/mptcp/pm_userspace.c | 10 +++++++++-
 net/mptcp/protocol.h     | 14 ++++++++++++--
 3 files changed, 35 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 mptcp: pm: bound extra_subflows admission on userspace PM

From: Tao Cui <[email protected]>

mptcp_pm_allow_new_subflow() increments the u8 extra_subflows counter for every accepted MP_JOIN on sockets using the userspace PM, without any limit. A peer establishing more than 255 live subflows wraps the counter back to 0, which then makes the underflow guards warn on the next subflow close, and permanently corrupts mptcpi_subflows_total reported to userspace.

Refuse new MP_JOINs once the counter has reached U8_MAX, so that it cannot wrap anymore.

Fixes e99c1ca89071 ("mptcp: pm: add WARN_ON_ONCE guards on extra_subflows underflow")
---
 net/mptcp/pm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
net/mptcp/pm.c +4 -2
index 8b68868255c5..07cdcdb54b15 100644
@@ -563,9 +563,11 @@ bool mptcp_pm_allow_new_subflow(struct mptcp_sock *msk)
 	if (mptcp_pm_is_userspace(msk)) {
 		if (mptcp_userspace_pm_active(msk)) {
 			spin_lock_bh(&pm->lock);
-			pm->extra_subflows++;
+			ret = pm->extra_subflows < U8_MAX;
+			if (ret)
+				pm->extra_subflows++;
 			spin_unlock_bh(&pm->lock);
-			return true;
+			return ret;
 		}
 		return false;
 	}
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/3 mptcp: pm: skip extra_subflows accounting on disconnected msk

In reply to Tao Cui

From: Tao Cui <[email protected]>

The WARN_ON_ONCE() guards added to the extra_subflows decrement sites turn out to be reachable:

mptcp_pm_data_reset() zeroes the counter with only the msk socket lock held, while an MP_JOIN subflow can still sit in msk->join_list, its reference already accounted by mptcp_pm_allow_new_subflow() under pm->lock. If the socket gets disconnected(AF_UNSPEC) in that window, mptcp_pm_data_reset() zeroes the counter, and the join list is flushed later at release_sock() time: the leftover subflow then reaches mptcp_pm_subflow_check_next() (or __mptcp_pm_close_subflow() for kernel PM sockets) with the counter already at 0, firing the warning. On panic_on_warn kernels this is a remotely triggerable panic, which is worse than the silent wrap the guards replaced.

Skip the PM accounting when the msk is already in TCP_CLOSE: in the scenario above the state is set before the counters are cleared, and once the msk is closed the accounting is not relevant anymore. Keep a clamp and a rate-limited pr_warn() on the decrement sites instead, to leave a trace of any imbalance we would still not know about.

Fixes e99c1ca89071 ("mptcp: pm: add WARN_ON_ONCE guards on extra_subflows underflow")
---
 net/mptcp/pm.c       | 11 ++++++++++-
 net/mptcp/protocol.h | 14 ++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)
net/mptcp/pm.c +10 -1
index 07cdcdb54b15..cccf5319cce0 100644
@@ -671,9 +671,18 @@ void mptcp_pm_subflow_check_next(struct mptcp_sock *msk,
 	update_subflows = subflow->request_join || subflow->mp_join;
 	if (mptcp_pm_is_userspace(msk)) {
 		if (update_subflows) {
+			/* The PM counters have already been cleared if the
+			 * msk got disconnected while this subflow was still
+			 * queued in the join list
+			 */
+			if (inet_sk_state_load(sk) == TCP_CLOSE)
+				return;
 			spin_lock_bh(&pm->lock);
-			if (!WARN_ON_ONCE(pm->extra_subflows == 0))
+			if (likely(pm->extra_subflows))
 				pm->extra_subflows--;
+			else
+				pr_warn_ratelimited("extra_subflows underflow, msk=%p\n",
+						    msk);
 			spin_unlock_bh(&pm->lock);
 		}
 		return;
net/mptcp/protocol.h +12 -2
index 87ccb84e9927..07821551499c 100644
@@ -1252,8 +1252,18 @@ u8 mptcp_pm_get_limit_extra_subflows(const struct mptcp_sock *msk);
 /* called under PM lock */
 static inline void __mptcp_pm_close_subflow(struct mptcp_sock *msk)
 {
-	if (!WARN_ON_ONCE(msk->pm.extra_subflows == 0) &&
-	    --msk->pm.extra_subflows < mptcp_pm_get_limit_extra_subflows(msk))
+	/* The PM counters have already been cleared if the msk got
+	 * disconnected while this subflow was still queued in the
+	 * join list
+	 */
+	if (inet_sk_state_load((struct sock *)msk) == TCP_CLOSE)
+		return;
+
+	if (unlikely(msk->pm.extra_subflows == 0)) {
+		pr_warn_ratelimited("extra_subflows underflow, msk=%p\n", msk);
+		return;
+	}
+	if (--msk->pm.extra_subflows < mptcp_pm_get_limit_extra_subflows(msk))
 		WRITE_ONCE(msk->pm.accept_subflow, true);
 }
 
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v2 3/3 mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

In reply to Tao Cui

From: Quanye Yang <[email protected]>

mptcp_pm_nl_subflow_create_doit() also increments the u8 extra_subflows counter without any limit: a client with CAP_NET_ADMIN can create more than 255 extra subflows via Netlink and wrap the counter back to 0, with the same effects as the MP_JOIN path now bounded separately:
the underflow guards warn on the next subflow close, and mptcpi_subflows_total reported to userspace is corrupted.

Refuse the subflow creation with -ENOSPC once the counter has reached U8_MAX, and clean up the local address entry in that case as well.

---
 net/mptcp/pm_userspace.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/mptcp/pm_userspace.c +9 -1
index b94fbb483bf9..3371e17d01b9 100644
@@ -417,6 +417,12 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	local.ifindex = entry.ifindex;
 
 	spin_lock_bh(&msk->pm.lock);
+	if (msk->pm.extra_subflows == U8_MAX) {
+		spin_unlock_bh(&msk->pm.lock);
+		GENL_SET_ERR_MSG(info, "too many extra subflows");
+		err = -ENOSPC;
+		goto delete_addr;
+	}
 	msk->pm.extra_subflows++;
 	spin_unlock_bh(&msk->pm.lock);
 
@@ -424,9 +430,11 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	err = __mptcp_subflow_connect(sk, &local, &addr_r);
 	release_sock(sk);
 
-	if (err) {
+	if (err)
 		GENL_SET_ERR_MSG_FMT(info, "connect error: %d", err);
 
+delete_addr:
+	if (err) {
 		spin_lock_bh(&msk->pm.lock);
 		mptcp_userspace_pm_delete_local_addr(msk, &entry);
 		spin_unlock_bh(&msk->pm.lock);
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH mptcp-next v2 3/3] mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the error path unconditionally deletes and frees a local address entry, even if the address was pre-existing and shared among multiple subflows. --

commit 147f86e2aec9bdeead6dcc987672ad229b2286ec
Author: Quanye Yang <[email protected]>

mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

mptcp_pm_nl_subflow_create_doit() also increments the u8 extra_subflows counter without any limit: a client with CAP_NET_ADMIN can create more than 255 extra subflows via Netlink and wrap the counter back to 0, with the same effects as the MP_JOIN path now bounded separately:
the underflow guards warn on the next subflow close, and mptcpi_subflows_total reported to userspace is corrupted.

Quoted 19 lines
net/mptcp/pm_userspace.c +6 -0
index f723a134356f3..bede75a2e2586 100644
@@ -427,6 +427,12 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	local.ifindex = entry.ifindex;
 
 	spin_lock_bh(&msk->pm.lock);
+	if (msk->pm.extra_subflows == U8_MAX) {
+		spin_unlock_bh(&msk->pm.lock);
+		GENL_SET_ERR_MSG(info, "too many extra subflows");
+		err = -ENOSPC;
+		goto delete_addr;
+	}
 	msk->pm.extra_subflows++;
 	spin_unlock_bh(&msk->pm.lock);
 
@@ -434,9 +440,11 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	err = __mptcp_subflow_connect(sk, &local, &addr_r);

net/mptcp/pm_userspace.c int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) · line 449

Quoted 10 lines
net/mptcp/pm_userspace.c +3 -1
 	release_sock(sk);
 
-	if (err) {
+	if (err)
 		GENL_SET_ERR_MSG_FMT(info, "connect error: %d", err);
 
+delete_addr:
+	if (err) {
 		spin_lock_bh(&msk->pm.lock);
 		mptcp_userspace_pm_delete_local_addr(msk, &entry);

[Severity: High]
This is a pre-existing issue, but does this error path incorrectly delete a shared local address entry if the address was pre-existing?

When mptcp_userspace_pm_append_new_local_addr() is called earlier in mptcp_pm_nl_subflow_create_doit(), it might match an already existing local address instead of appending a new one.

If the newly added extra_subflows limit is hit (or if __mptcp_subflow_connect() fails), the code jumps to this delete_addr error path.

Because mptcp_userspace_pm_append_new_local_addr() did not allocate a new entry but simply reused the existing one, unconditionally calling mptcp_userspace_pm_delete_local_addr() prematurely frees the shared address and decrements the local_addr_used counter, potentially corrupting the state for other subflows relying on it.

Quoted 2 lines
net/mptcp/pm_userspace.c +0 -0
 		spin_unlock_bh(&msk->pm.lock);

}

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#5 lore raw

Re: [PATCH mptcp-next v2 3/3] mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

Hello,

Quoted 62 lines · On 03/09/2026 15:27, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the error path unconditionally deletes and frees a local address entry, even if the address was pre-existing and shared among multiple subflows. --

commit 147f86e2aec9bdeead6dcc987672ad229b2286ec
Author: Quanye Yang <[email protected]>

mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

mptcp_pm_nl_subflow_create_doit() also increments the u8 extra_subflows counter without any limit: a client with CAP_NET_ADMIN can create more than 255 extra subflows via Netlink and wrap the counter back to 0, with the same effects as the MP_JOIN path now bounded separately:
the underflow guards warn on the next subflow close, and mptcpi_subflows_total reported to userspace is corrupted.

Quoted 19 lines
net/mptcp/pm_userspace.c +6 -0
index f723a134356f3..bede75a2e2586 100644
@@ -427,6 +427,12 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	local.ifindex = entry.ifindex;
 
 	spin_lock_bh(&msk->pm.lock);
+	if (msk->pm.extra_subflows == U8_MAX) {
+		spin_unlock_bh(&msk->pm.lock);
+		GENL_SET_ERR_MSG(info, "too many extra subflows");
+		err = -ENOSPC;
+		goto delete_addr;
+	}
 	msk->pm.extra_subflows++;
 	spin_unlock_bh(&msk->pm.lock);
 
@@ -434,9 +440,11 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	err = __mptcp_subflow_connect(sk, &local, &addr_r);

net/mptcp/pm_userspace.c int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) · line 449

Quoted 10 lines
net/mptcp/pm_userspace.c +3 -1
 	release_sock(sk);
 
-	if (err) {
+	if (err)
 		GENL_SET_ERR_MSG_FMT(info, "connect error: %d", err);
 
+delete_addr:
+	if (err) {
 		spin_lock_bh(&msk->pm.lock);
 		mptcp_userspace_pm_delete_local_addr(msk, &entry);

[Severity: High]
This is a pre-existing issue, but does this error path incorrectly delete a shared local address entry if the address was pre-existing?

When mptcp_userspace_pm_append_new_local_addr() is called earlier in mptcp_pm_nl_subflow_create_doit(), it might match an already existing local address instead of appending a new one.

If the newly added extra_subflows limit is hit (or if __mptcp_subflow_connect() fails), the code jumps to this delete_addr error path.

Quoted 5 lines

Because mptcp_userspace_pm_append_new_local_addr() did not allocate a new entry but simply reused the existing one, unconditionally calling mptcp_userspace_pm_delete_local_addr() prematurely frees the shared address and decrements the local_addr_used counter, potentially corrupting the state for other subflows relying on it.

Correct, but there are pending patches from Geliang fixing that.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
maintainer
#6 lore raw

Re: [PATCH mptcp-next v2 3/3] mptcp: pm: userspace: cap extra_subflows on Netlink subflow creation

In reply to Tao Cui

Hi Tao, Quanye,

Quoted 9 lines · Tao Cui

From: Quanye Yang <[email protected]>

mptcp_pm_nl_subflow_create_doit() also increments the u8 extra_subflows counter without any limit: a client with CAP_NET_ADMIN can create more than 255 extra subflows via Netlink and wrap the counter back to 0, with the same effects as the MP_JOIN path now bounded separately:
the underflow guards warn on the next subflow close, and mptcpi_subflows_total reported to userspace is corrupted.

Quoted 7 lines

Refuse the subflow creation with -ENOSPC once the counter has reached U8_MAX, and clean up the local address entry in that case as well.

(no empty line here)

Quoted 24 lines
---
 net/mptcp/pm_userspace.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
net/mptcp/pm_userspace.c +6 -0
index b94fbb483bf9..3371e17d01b9 100644
@@ -417,6 +417,12 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	local.ifindex = entry.ifindex;
 
 	spin_lock_bh(&msk->pm.lock);
+	if (msk->pm.extra_subflows == U8_MAX) {
+		spin_unlock_bh(&msk->pm.lock);
+		GENL_SET_ERR_MSG(info, "too many extra subflows");
+		err = -ENOSPC;
+		goto delete_addr;
+	}
 	msk->pm.extra_subflows++;
 	spin_unlock_bh(&msk->pm.lock);
 

net/mptcp/pm_userspace.c int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) · line 436

Quoted 8 lines
net/mptcp/pm_userspace.c +2 -1
@@ -424,9 +430,11 @@ int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info)
 	err = __mptcp_subflow_connect(sk, &local, &addr_r);
 	release_sock(sk);
 
-	if (err) {
+	if (err)
 		GENL_SET_ERR_MSG_FMT(info, "connect error: %d", err);
 
+delete_addr:

Can you not have the label inside the if-statement?

    if (err) {
        GENL_SET_ERR_MSG_FMT(info, "connect error: %d", err);

delete_addr:

        (...)

net/mptcp/pm_userspace.c int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) · line 439

Quoted 3 lines
net/mptcp/pm_userspace.c +1 -0
+	if (err) {
 		spin_lock_bh(&msk->pm.lock);
 		mptcp_userspace_pm_delete_local_addr(msk, &entry);

Maybe you can add something in the commit message for the reviewers:

The same error path is followed after mptcp_userspace_pm_append_new_local_addr, which should probably not delete the local address unconditionally, but this is a pre-existing issue that will be addressed separately.

net/mptcp/pm_userspace.c int mptcp_pm_nl_subflow_create_doit(struct sk_buff *skb, struct genl_info *info) · line 440

Quoted 1 line
net/mptcp/pm_userspace.c +0 -0
 		spin_unlock_bh(&msk->pm.lock);

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#7 lore raw
In reply to Tao Cui

Hi Quanye,

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! ✅
- Perf:
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/33761727542

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

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)