Skip to contentSkip to conversation

mptcp: fix ADD_ADDR retransmission socket leaks

PATCH Applied Karl Mehltretter 2 participants 4 messages Sep 3 – Sep 4

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

cover mptcp: fix ADD_ADDR retransmission socket leaks

Backport the two ordered ADD_ADDR retransmission timer fixes missing from 5.10.y. Patch 1 makes every terminal path release the socket reference held by the timer. Patch 2 uses sock_put() so the last reference frees the socket, while avoiding a timer self-deadlock during destruction.

Both fixes carry Cc: stable and are present in 5.15.y and every newer supported LTS. The original 11-patch cover groups patches 3-5 as the v5.10 ADD_ADDR timer data-race and leak fixes. Patch 3 is already present as d9b272a85fe6 ("mptcp: pm: ADD_ADDR rtx: fix potential data-race"); this series supplies the missing ordered patches 4 and 5:
https://patch.msgid.link/[email protected]

In v5.10 the timer code is in pm_netlink.c and uses fixed retransmission delays; the adaptations retain those details. Patch 1 also routes v5.10's unreachable ID 0 guard through the reference drop.

Tested on v5.10.269 with KASAN under QEMU. A targeted A/B forced the tracked MPTCP socket to TCP_CLOSE at timer entry: the vulnerable kernel ran the callback without destroying the socket, while the fixed kernel destroyed it. Both completed an identical MPTCP transfer without a KASAN report, Oops, or warning.

The fixed kernel also passed all 15 tests in pm_netlink.sh, a 16 MiB bidirectional transfer with a real ADD_ADDR/MP_JOIN second subflow, and an 8 MiB curl --mptcp transfer. All payload comparisons were byte-exact.

Matthieu Baerts (NGI0) (2):
mptcp: pm: ADD_ADDR rtx: always decrease sk refcount
mptcp: pm: ADD_ADDR rtx: free sk if last

 net/mptcp/pm_netlink.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 mptcp: pm: ADD_ADDR rtx: always decrease sk refcount

From: Matthieu Baerts (NGI0) <[email protected]>

[ Upstream commit 9634cb35af17019baec21ca648516ce376fa10e6 ]

When an ADD_ADDR is retransmitted, the sk is held in sk_reset_timer(). It should then be released in all cases at the end.

Some (unlikely) checks were returning directly instead of calling sock_put() to decrease the refcount. Jump to a new 'exit' label to call __sock_put() (which will become sock_put() in the next commit) to fix this potential leak.

While at it, drop the '!msk' check which cannot happen because it is never reset, and explicitly mark the remaining one as "unlikely".

[ Karl Mehltretter: applied to pm_netlink.c; retained v5.10's ID 0 guard
and routed it through the common refcount drop. ]

Assisted-by LLM
---
 net/mptcp/pm_netlink.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
net/mptcp/pm_netlink.c +4 -6
index d4d56f0af3e0a..aee3a8dd9287a 100644
@@ -217,14 +217,11 @@ static void mptcp_pm_add_timer(struct timer_list *timer)
 
 	pr_debug("msk=%p\n", msk);
 
-	if (!msk)
-		return;
-
-	if (inet_sk_state_load(sk) == TCP_CLOSE)
-		return;
+	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
+		goto exit;
 
 	if (!entry->addr.id)
-		return;
+		goto exit;
 
 	bh_lock_sock(sk);
 	if (sock_owned_by_user(sk)) {
@@ -253,6 +250,7 @@ static void mptcp_pm_add_timer(struct timer_list *timer)
 
 out:
 	bh_unlock_sock(sk);
+exit:
 	__sock_put(sk);
 }
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 mptcp: pm: ADD_ADDR rtx: free sk if last

In reply to Karl Mehltretter

From: Matthieu Baerts (NGI0) <[email protected]>

[ Upstream commit b7b9a461569734d33d3259d58d2507adfac107ed ]

When an ADD_ADDR is retransmitted, the sk is held in sk_reset_timer(), and released at the end.

If at that moment, it was the last reference being held, the sk would not be freed. sock_put() should then be called instead of __sock_put().

But that's not enough: if it is the last reference, sock_put() will call sk_free(), which will end up calling sk_stop_timer_sync() on the same timer, and waiting indefinitely to finish. So it is needed to mark that the timer is done at the end of the timer handler when it has not been rescheduled, not to call sk_stop_timer_sync() on "itself".

[ Karl Mehltretter: applied to pm_netlink.c and retained v5.10's fixed
retransmission delays while centralizing timer rearming. ]

Assisted-by LLM
---
 net/mptcp/pm_netlink.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
net/mptcp/pm_netlink.c +17 -9
index aee3a8dd9287a..89570f83f96b0 100644
@@ -34,6 +34,7 @@ struct mptcp_pm_add_entry {
 	struct timer_list	add_timer;
 	struct mptcp_sock	*sock;
 	u8			retrans_times;
+	bool			timer_done;
 	struct rcu_head		rcu;
 };
 
@@ -214,24 +215,25 @@ static void mptcp_pm_add_timer(struct timer_list *timer)
 	struct mptcp_pm_add_entry *entry = from_timer(entry, timer, add_timer);
 	struct mptcp_sock *msk = entry->sock;
 	struct sock *sk = (struct sock *)msk;
+	unsigned int timeout = 0;
 
 	pr_debug("msk=%p\n", msk);
 
+	bh_lock_sock(sk);
 	if (unlikely(inet_sk_state_load(sk) == TCP_CLOSE))
-		goto exit;
+		goto out;
 
 	if (!entry->addr.id)
-		goto exit;
+		goto out;
 
-	bh_lock_sock(sk);
 	if (sock_owned_by_user(sk)) {
 		/* Try again later. */
-		sk_reset_timer(sk, timer, jiffies + HZ / 20);
+		timeout = HZ / 20;
 		goto out;
 	}
 
 	if (mptcp_pm_should_add_signal(msk)) {
-		sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX / 8);
+		timeout = TCP_RTO_MAX / 8;
 		goto out;
 	}
 
@@ -244,14 +246,18 @@ static void mptcp_pm_add_timer(struct timer_list *timer)
 	}
 
 	if (entry->retrans_times < ADD_ADDR_RETRANS_MAX)
-		sk_reset_timer(sk, timer, jiffies + TCP_RTO_MAX);
+		timeout = TCP_RTO_MAX;
 
 	spin_unlock_bh(&msk->pm.lock);
 
 out:
+	if (timeout)
+		sk_reset_timer(sk, timer, jiffies + timeout);
+	else
+		/* if sock_put calls sk_free: avoid waiting for this timer */
+		entry->timer_done = true;
 	bh_unlock_sock(sk);
-exit:
-	__sock_put(sk);
+	sock_put(sk);
 }
 
 struct mptcp_pm_add_entry *
@@ -304,6 +310,7 @@ static bool mptcp_pm_alloc_anno_list(struct mptcp_sock *msk,
 	add_entry->retrans_times = 0;
 
 	timer_setup(&add_entry->add_timer, mptcp_pm_add_timer, 0);
+	add_entry->timer_done = false;
 	sk_reset_timer(sk, &add_entry->add_timer, jiffies + TCP_RTO_MAX);
 
 	return true;
@@ -322,7 +329,8 @@ void mptcp_pm_free_anno_list(struct mptcp_sock *msk)
 	spin_unlock_bh(&msk->pm.lock);
 
 	list_for_each_entry_safe(entry, tmp, &free_list, list) {
-		sk_stop_timer_sync(sk, &entry->add_timer);
+		if (!entry->timer_done)
+			sk_stop_timer_sync(sk, &entry->add_timer);
 		kfree_rcu(entry, rcu);
 	}
 }
Signature · 1 line
2.53.0
maintainer Applied
#3 lore raw
In reply to Karl Mehltretter
Quoted 5 lines

Backport the two ordered ADD_ADDR retransmission timer fixes missing from 5.10.y. Patch 1 makes every terminal path release the socket reference held by the timer. Patch 2 uses sock_put() so the last reference frees the socket, while avoiding a timer self-deadlock during destruction.

Queued the series for 5.10, thanks.

Signature · 2 lines
Thanks,
Sasha