Skip to contentSkip to conversation

net: mctp: take ownership of skb in mctp_local_output

PATCH Applied Jeremy Kerr 3 participants 3 messages Feb 20, 2024 – Feb 23, 2024

Subsystems (3)
lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

PATCH v2 1/1 net: mctp: take ownership of skb in mctp_local_output

Currently, mctp_local_output only takes ownership of skb on success, and we may leak an skb if mctp_local_output fails in specific states; the skb ownership isn't transferred until the actual output routing occurs.

Instead, make mctp_local_output free the skb on all error paths up to the route action, so it always consumes the passed skb.

Fixes 833ef3b91de6 ("mctp: Populate socket implementation")

---
v2:
- retain EINVAL return code in !rt && !ifindex case. Based on feedback from Simon Horman <[email protected]>.

---
 include/net/mctp.h |  1 +
 net/mctp/route.c   | 10 ++++++++--
 2 files changed, 9 insertions(+), 2 deletions(-)
include/net/mctp.h +1 -0
index da86e106c91d..2bff5f47ce82 100644
@@ -249,6 +249,7 @@ struct mctp_route {
 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
 				     mctp_eid_t daddr);
 
+/* always takes ownership of skb */
 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
 
net/mctp/route.c +8 -2
index 7a47a58aa54b..d0c43812bec3 100644
@@ -888,7 +888,7 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		dev = dev_get_by_index_rcu(sock_net(sk), cb->ifindex);
 		if (!dev) {
 			rcu_read_unlock();
-			return rc;
+			goto out_free;
 		}
 		rt->dev = __mctp_dev_get(dev);
 		rcu_read_unlock();
@@ -903,7 +903,8 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		rt->mtu = 0;
 
 	} else {
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out_free;
 	}
 
 	spin_lock_irqsave(&rt->dev->addrs_lock, flags);
@@ -966,12 +967,17 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		rc = mctp_do_fragment_route(rt, skb, mtu, tag);
 	}
 
+	/* route output functions consume the skb, even on error */
+	skb = NULL;
+
 out_release:
 	if (!ext_rt)
 		mctp_route_release(rt);
 
 	mctp_dev_put(tmp_rt.dev);
 
+out_free:
+	kfree_skb(skb);
 	return rc;
 }
 
Signature · 1 line
2.39.2
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Jeremy Kerr

Currently, mctp_local_output only takes ownership of skb on success, and we may leak an skb if mctp_local_output fails in specific states; the skb ownership isn't transferred until the actual output routing occurs.

Instead, make mctp_local_output free the skb on all error paths up to the route action, so it always consumes the passed skb.

Quoted 7 lines
Fixes 833ef3b91de6 ("mctp: Populate socket implementation")

---
v2:
- retain EINVAL return code in !rt && !ifindex case. Based on feedback from Simon Horman <[email protected]>.

Hi Jeremy,

Thanks for the update.
This one looks good to me.

#2 lore raw
In reply to Jeremy Kerr
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Tue, 20 Feb 2024 16:10:53 +0800 you wrote:

Currently, mctp_local_output only takes ownership of skb on success, and we may leak an skb if mctp_local_output fails in specific states; the skb ownership isn't transferred until the actual output routing occurs.

Instead, make mctp_local_output free the skb on all error paths up to the route action, so it always consumes the passed skb.

[...]

Here is the summary with links:
- [net-next,v2] net: mctp: take ownership of skb in mctp_local_output

    https://git.kernel.org/netdev/net/c/3773d65ae515

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html