Skip to contentSkip to conversation

mptcp: pm: fix use-after-free in userspace_pm_get_local_id()

PATCH In review [email protected] 3 participants 4 messages Jul 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mptcp: pm: fix use-after-free in userspace_pm_get_local_id()

From: Xuanqiang Luo <[email protected]>

mptcp_userspace_pm_get_local_id() looks up an address entry with pm.lock held, but drops the lock before reading its ID. A concurrent subflow destroy command can remove and free the entry in between, resulting in a use-after-free while processing an MP_JOIN SYN.

Read the ID while holding pm.lock, then use the copied value after unlocking.

Fixes f012d796a6de ("mptcp: check addrs list in userspace_pm_get_local_id")

---
The race window is narrow. It was reproduced only with a locally constructed stress test that repeatedly overlaps an MP_JOIN SYN with a MPTCP_PM_CMD_SUBFLOW_DESTROY request.

However, the KASAN report below confirms that the race is reachable:

[  666.319362] ==================================================================
[  666.319376] BUG: KASAN: slab-use-after-free in mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[  666.319386] Read of size 1 at addr ffff888124845610 by task swapper/0/0
...
[  666.319401] Call Trace:
[  666.319405]  <IRQ>
[  666.319408]  dump_stack_lvl+0x53/0x70
[  666.319412]  print_address_description.constprop.0+0x2c/0x3b0
[  666.319418]  print_report+0xbe/0x2b0
[  666.319421]  ? mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[  666.319423]  kasan_report+0xce/0x100
[  666.319426]  ? mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[  666.319429]  mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[  666.319433]  mptcp_pm_get_local_id+0x371/0x440
...
[  666.319821] Allocated by task 45539:
[  666.319844]  kasan_save_stack+0x33/0x60
[  666.319855]  kasan_save_track+0x14/0x30
[  666.319858]  __kasan_kmalloc+0x8f/0xa0
[  666.319863]  __kmalloc_noprof+0x1e7/0x520
[  666.319867]  sock_kmalloc+0xdf/0x130
[  666.319885]  sock_kmemdup+0x1b/0x40
[  666.319888]  mptcp_userspace_pm_append_new_local_addr+0x261/0x500
[  666.319910]  mptcp_pm_nl_announce_doit+0x16a/0x610
...
[  666.319967] Freed by task 45560:
[  666.319988]  kasan_save_stack+0x33/0x60
[  666.319991]  kasan_save_track+0x14/0x30
[  666.319994]  kasan_save_free_info+0x3b/0x60
[  666.319998]  __kasan_slab_free+0x43/0x70
[  666.320000]  kfree+0x166/0x440
[  666.320003]  sock_kfree_s+0x1d/0x50
[  666.320007]  mptcp_userspace_pm_delete_local_addr.isra.0+0x157/0x200
[  666.320011]  mptcp_pm_nl_subflow_destroy_doit+0x51d/0xea0
 net/mptcp/pm_userspace.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/mptcp/pm_userspace.c +5 -2
index d100867e9202f..27fa8dc757b4f 100644
@@ -132,12 +132,15 @@ int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
 	__be16 msk_sport =  ((struct inet_sock *)
 			     inet_sk((struct sock *)msk))->inet_sport;
 	struct mptcp_pm_addr_entry *entry;
+	int id = -1;
 
 	spin_lock_bh(&msk->pm.lock);
 	entry = mptcp_userspace_pm_lookup_addr(msk, &skc->addr);
-	spin_unlock_bh(&msk->pm.lock);
 	if (entry)
-		return entry->addr.id;
+		id = entry->addr.id;
+	spin_unlock_bh(&msk->pm.lock);
+	if (id >= 0)
+		return id;
 
 	if (skc->addr.port == msk_sport)
 		skc->addr.port = 0;
Signature · 1 line
2.43.0
#1 lore raw

July 13, 2026 at 3:47 PM, [email protected] mailto:[email protected] wrote:

Hi xuanqiang,

Thanks for the patch.

But AFAIK, geliang has fixed this in [1], and your test verified this issues.

@Matt, it seems that [1](geliang's patch) can be merged.

[1]https://patchwork.kernel.org/project/mptcp/patch/4e50adfde3b80f433e13b86919596be229045edc.1782799876.git.tanggeliang@kylinos.cn/

Thanks
Gang

Quoted 84 lines

From: Xuanqiang Luo <[email protected]>

mptcp_userspace_pm_get_local_id() looks up an address entry with pm.lock held, but drops the lock before reading its ID. A concurrent subflow destroy command can remove and free the entry in between, resulting in a use-after-free while processing an MP_JOIN SYN.

Read the ID while holding pm.lock, then use the copied value after unlocking.

Fixes f012d796a6de ("mptcp: check addrs list in userspace_pm_get_local_id")

---
The race window is narrow. It was reproduced only with a locally constructed stress test that repeatedly overlaps an MP_JOIN SYN with a MPTCP_PM_CMD_SUBFLOW_DESTROY request.

However, the KASAN report below confirms that the race is reachable:

[ 666.319362] ==================================================================
[ 666.319376] BUG: KASAN: slab-use-after-free in mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[ 666.319386] Read of size 1 at addr ffff888124845610 by task swapper/0/0
...
[ 666.319401] Call Trace:
[ 666.319405] <IRQ>
[ 666.319408] dump_stack_lvl+0x53/0x70
[ 666.319412] print_address_description.constprop.0+0x2c/0x3b0
[ 666.319418] print_report+0xbe/0x2b0
[ 666.319421] ? mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[ 666.319423] kasan_report+0xce/0x100
[ 666.319426] ? mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[ 666.319429] mptcp_userspace_pm_get_local_id+0x1dc/0x1f0
[ 666.319433] mptcp_pm_get_local_id+0x371/0x440
...
[ 666.319821] Allocated by task 45539:
[ 666.319844] kasan_save_stack+0x33/0x60
[ 666.319855] kasan_save_track+0x14/0x30
[ 666.319858] __kasan_kmalloc+0x8f/0xa0
[ 666.319863] __kmalloc_noprof+0x1e7/0x520
[ 666.319867] sock_kmalloc+0xdf/0x130
[ 666.319885] sock_kmemdup+0x1b/0x40
[ 666.319888] mptcp_userspace_pm_append_new_local_addr+0x261/0x500
[ 666.319910] mptcp_pm_nl_announce_doit+0x16a/0x610
...
[ 666.319967] Freed by task 45560:
[ 666.319988] kasan_save_stack+0x33/0x60
[ 666.319991] kasan_save_track+0x14/0x30
[ 666.319994] kasan_save_free_info+0x3b/0x60
[ 666.319998] __kasan_slab_free+0x43/0x70
[ 666.320000] kfree+0x166/0x440
[ 666.320003] sock_kfree_s+0x1d/0x50
[ 666.320007] mptcp_userspace_pm_delete_local_addr.isra.0+0x157/0x200
[ 666.320011] mptcp_pm_nl_subflow_destroy_doit+0x51d/0xea0
 net/mptcp/pm_userspace.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
net/mptcp/pm_userspace.c +5 -2
index d100867e9202f..27fa8dc757b4f 100644
@@ -132,12 +132,15 @@ int mptcp_userspace_pm_get_local_id(struct mptcp_sock *msk,
 __be16 msk_sport = ((struct inet_sock *)
 inet_sk((struct sock *)msk))->inet_sport;
 struct mptcp_pm_addr_entry *entry;
+ int id = -1;
 
 spin_lock_bh(&msk->pm.lock);
 entry = mptcp_userspace_pm_lookup_addr(msk, &skc->addr);
- spin_unlock_bh(&msk->pm.lock);
 if (entry)
- return entry->addr.id;
+ id = entry->addr.id;
+ spin_unlock_bh(&msk->pm.lock);
+ if (id >= 0)
+ return id;
 
 if (skc->addr.port == msk_sport)
 skc->addr.port = 0;
Signature · 1 line
2.43.0
#2 lore raw
Quoted 7 lines · 在 2026/7/13 16:03, [email protected] 写道:

July 13, 2026 at 3:47 PM, [email protected] mailto:[email protected] wrote:

Hi xuanqiang,

Thanks for the patch.

Quoted 8 lines

But AFAIK, geliang has fixed this in [1], and your test verified this issues.

@Matt, it seems that [1](geliang's patch) can be merged.

[1]https://patchwork.kernel.org/project/mptcp/patch/4e50adfde3b80f433e13b86919596be229045edc.1782799876.git.tanggeliang@kylinos.cn/

Thanks
Gang

I see it now.
Indeed, I missed the pending patch;
Thank you for pointing it out!

Thanks
Xuanqiang

#3 lore raw
In reply to [email protected]

Hi Xuanqiang,

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/29234124591

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

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)