Skip to contentSkip to conversation

mptcp: pm: Fix undefined behavior in mptcp_remove_anno_list_by_saddr()

PATCH In review Thorsten Blum 3 participants 4 messages Mar 25, 2025 1 message not in the archive

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

PATCH 1/1 mptcp: pm: Fix undefined behavior in mptcp_remove_anno_list_by_saddr()

Commit e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c") removed a necessary if-check, leading to undefined behavior because the freed pointer is subsequently returned from the function.

Reintroduce the if-check to fix this and add a local return variable to prevent further checkpatch warnings, which originally led to the removal of the if-check.

Fixes e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c")
---
 net/mptcp/pm.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/mptcp/pm.c +7 -2
index 18b19dbccbba..5a6d5e4897dd 100644
@@ -151,10 +151,15 @@ bool mptcp_remove_anno_list_by_saddr(struct mptcp_sock *msk,
 				     const struct mptcp_addr_info *addr)
 {
 	struct mptcp_pm_add_entry *entry;
+	bool ret = false;
 
 	entry = mptcp_pm_del_add_timer(msk, addr, false);
-	kfree(entry);
-	return entry;
+	if (entry) {
+		ret = true;
+		kfree(entry);
+	}
+
+	return ret;
 }
 
 bool mptcp_pm_sport_in_anno_list(struct mptcp_sock *msk, const struct sock *sk)

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#2 lore raw
Quoted 10 lines · On Tue, 25 Mar 2025 12:33:11 +0100 Thorsten Blum wrote:
Quoted 9 lines · Thorsten Blum

Commit e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c") removed a necessary if-check, leading to undefined behavior because the freed pointer is subsequently returned from the function.

Reintroduce the if-check to fix this and add a local return variable to prevent further checkpatch warnings, which originally led to the removal of the if-check.

Quoted 7 lines
Quoted 3 lines
Fixes e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c")

---

Never mind, technically it's not actually undefined behavior because of the implicit bool conversion, but returning a freed pointer still seems confusing.

CCing the list back in.

Signature · 1 line
pw-bot: cr
#3 lore raw
Quoted 15 lines · Jakub Kicinski
Quoted 14 lines · On Tue, 25 Mar 2025 12:33:11 +0100 Thorsten Blum wrote:
Quoted 12 lines · Thorsten Blum

Commit e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c") removed a necessary if-check, leading to undefined behavior because the freed pointer is subsequently returned from the function.

Reintroduce the if-check to fix this and add a local return variable to prevent further checkpatch warnings, which originally led to the removal of the if-check.

Fixes e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c")

---

Quoted 5 lines
Quoted 3 lines

Never mind, technically it's not actually undefined behavior because of the implicit bool conversion, but returning a freed pointer still seems confusing.

CCing the list back in.

Thanks!

The change imo still makes sense, but the commit message should be updated. I'll submit a new patch for after the merge window.

maintainer
#4 lore raw

Hi Thorsten,

Quoted 20 lines · Thorsten Blum
Quoted 19 lines · Jakub Kicinski
Quoted 17 lines · On Tue, 25 Mar 2025 12:33:11 +0100 Thorsten Blum wrote:
Quoted 5 lines · Thorsten Blum

Commit e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c") removed a necessary if-check, leading to undefined behavior because the freed pointer is subsequently returned from the function.

Quoted 7 lines

Reintroduce the if-check to fix this and add a local return variable to prevent further checkpatch warnings, which originally led to the removal of the if-check.

Fixes e4c28e3d5c090 ("mptcp: pm: move generic PM helpers to pm.c")

---

Never mind, technically it's not actually undefined behavior because of the implicit bool conversion, but returning a freed pointer still seems confusing.

Quoted 6 lines
Quoted 1 line

CCing the list back in.

Thanks!

The change imo still makes sense, but the commit message should be updated.

Yes, I think it still makes sense, and I understand the confusion. Another reason to change that is to avoid future issues when kfree() will be able to set variables to NULL [1].

Instead of re-introducing the if-statement, why not assigning 'ret' to 'entry'?

  entry = mptcp_pm_del_add_timer(...);
  ret = entry; // or assign it at the previous line? ret = entry = (...)
  kfree(entry);

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

Quoted 1 line

I'll submit a new patch for after the merge window.

Thank you!

An alternative if you want to send it before is to rebase it on top of the MPTCP "export" branch, and to send it only to the MPTCP ML. I can apply the new version in our tree, and send it to Netdev later with other patches.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.