Skip to contentSkip to conversation

xfrm: nat_keepalive: avoid double free on send error

PATCH Applied Ren Wei 3 participants 3 messages Jun 25 – Jun 30

Subsystems (3)
lore
Share & tools
lore

Ren Wei

author
#0 lore raw

PATCH v2 1/1 xfrm: nat_keepalive: avoid double free on send error

From: Qianyu Luo <[email protected]>

nat_keepalive_send() frees the keepalive skb whenever the IPv4 or IPv6 send helper reports an error.

That cleanup is only correct before the skb is handed to the output path. Once ip_build_and_send_pkt() or ip6_xmit() takes ownership, the networking stack may already have consumed the skb before returning an error, so freeing it again is unsafe.

Handle the pre-handoff failure cases inside nat_keepalive_send_ipv4() and nat_keepalive_send_ipv6(), where the caller still owns the skb, and keep nat_keepalive_send() responsible only for family dispatch and the unsupported-family cleanup path.

Fixes f531d13bdfe3 ("xfrm: support sending NAT keepalives in ESP in UDP states")

---
Changes in v2:
- move kfree_skb() after local_unlock_nested_bh() in the IPv6 dst-lookup failure path as suggested in review
- rebase onto latest netdev/net

 net/xfrm/xfrm_nat_keepalive.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
net/xfrm/xfrm_nat_keepalive.c +9 -6
index 458931062a04..eb1b6f67739e 100644
@@ -55,8 +55,10 @@ static int nat_keepalive_send_ipv4(struct sk_buff *skb,
 			   ka->encap_sport, sock_net_uid(net, NULL));
 
 	rt = ip_route_output_key(net, &fl4);
-	if (IS_ERR(rt))
+	if (IS_ERR(rt)) {
+		kfree_skb(skb);
 		return PTR_ERR(rt);
+	}
 
 	skb_dst_set(skb, &rt->dst);
 
@@ -101,6 +103,7 @@ static int nat_keepalive_send_ipv6(struct sk_buff *skb,
 	dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL);
 	if (IS_ERR(dst)) {
 		local_unlock_nested_bh(&nat_keepalive_sk_ipv6.bh_lock);
+		kfree_skb(skb);
 		return PTR_ERR(dst);
 	}
 
@@ -118,7 +121,6 @@ static void nat_keepalive_send(struct nat_keepalive *ka)
 					sizeof(struct ipv6hdr)) +
 				    sizeof(struct udphdr);
 	const u8 nat_ka_payload = 0xFF;
-	int err = -EAFNOSUPPORT;
 	struct sk_buff *skb;
 	struct udphdr *uh;
 
@@ -140,16 +142,17 @@ static void nat_keepalive_send(struct nat_keepalive *ka)
 
 	switch (ka->family) {
 	case AF_INET:
-		err = nat_keepalive_send_ipv4(skb, ka);
+		nat_keepalive_send_ipv4(skb, ka);
 		break;
 #if IS_ENABLED(CONFIG_IPV6)
 	case AF_INET6:
-		err = nat_keepalive_send_ipv6(skb, ka, uh);
+		nat_keepalive_send_ipv6(skb, ka, uh);
 		break;
 #endif
-	}
-	if (err)
+	default:
 		kfree_skb(skb);
+		break;
+	}
 }
 
 struct nat_keepalive_work_ctx {
Signature · 1 line
2.43.7
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Ren Wei

From: Qianyu Luo <[email protected]>

nat_keepalive_send() frees the keepalive skb whenever the IPv4 or IPv6 send helper reports an error.

That cleanup is only correct before the skb is handed to the output path. Once ip_build_and_send_pkt() or ip6_xmit() takes ownership, the networking stack may already have consumed the skb before returning an error, so freeing it again is unsafe.

Quoted 6 lines

Handle the pre-handoff failure cases inside nat_keepalive_send_ipv4() and nat_keepalive_send_ipv6(), where the caller still owns the skb, and keep nat_keepalive_send() responsible only for family dispatch and the unsupported-family cleanup path.

Fixes f531d13bdfe3 ("xfrm: support sending NAT keepalives in ESP in UDP states")

Thanks for the fix!

maintainer Applied
#2 lore raw
Quoted 17 lines · Eyal Birger
Quoted 16 lines · Ren Wei

From: Qianyu Luo <[email protected]>

nat_keepalive_send() frees the keepalive skb whenever the IPv4 or IPv6 send helper reports an error.

That cleanup is only correct before the skb is handed to the output path. Once ip_build_and_send_pkt() or ip6_xmit() takes ownership, the networking stack may already have consumed the skb before returning an error, so freeing it again is unsafe.

Handle the pre-handoff failure cases inside nat_keepalive_send_ipv4() and nat_keepalive_send_ipv6(), where the caller still owns the skb, and keep nat_keepalive_send() responsible only for family dispatch and the unsupported-family cleanup path.

Quoted 5 lines
Quoted 1 line
Fixes f531d13bdfe3 ("xfrm: support sending NAT keepalives in ESP in UDP states")

Thanks for the fix!

Applied, thanks everyone!