Skip to contentSkip to conversation

ipv6: xfrm6: release dst on error in xfrm6_rcv_encap()

PATCH Applied Ren Wei 3 participants 4 messages Apr 12 – Apr 21 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Ren Wei

author
#1 lore raw

PATCH 1/1 ipv6: xfrm6: release dst on error in xfrm6_rcv_encap()

In reply to a message not in the archive

From: Yilin Zhu <[email protected]>

xfrm6_rcv_encap() performs an IPv6 route lookup when the skb does not already have a dst attached. ip6_route_input_lookup() returns a referenced dst entry even when the lookup resolves to an error route.

If dst->error is set, xfrm6_rcv_encap() drops the skb without attaching the dst to the skb and without releasing the reference returned by the lookup. Repeated packets hitting this path therefore leak dst entries.

Release the dst before jumping to the drop path.

Fixes 0146dca70b87 ("xfrm: add support for UDPv6 encapsulation of ESP")
---
 net/ipv6/xfrm6_protocol.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv6/xfrm6_protocol.c +3 -1
index ea2f805d3b01..9b586fcec485 100644
@@ -88,8 +88,10 @@ int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
 
 		dst = ip6_route_input_lookup(dev_net(skb->dev), skb->dev, &fl6,
 					     skb, flags);
-		if (dst->error)
+		if (dst->error) {
+			dst_release(dst);
 			goto drop;
+		}
 		skb_dst_set(skb, dst);
 	}
 
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#2 lore raw
Quoted 22 lines · Ren Wei

From: Yilin Zhu <[email protected]>

xfrm6_rcv_encap() performs an IPv6 route lookup when the skb does not already have a dst attached. ip6_route_input_lookup() returns a referenced dst entry even when the lookup resolves to an error route.

If dst->error is set, xfrm6_rcv_encap() drops the skb without attaching the dst to the skb and without releasing the reference returned by the lookup. Repeated packets hitting this path therefore leak dst entries.

Release the dst before jumping to the drop path.

Fixes 0146dca70b87 ("xfrm: add support for UDPv6 encapsulation of ESP")
Quoted 3 lines
---
 net/ipv6/xfrm6_protocol.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
reviewer
#3 lore raw
In reply to Ren Wei

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
ipv6: xfrm6: release dst on error in xfrm6_rcv_encap()

This commit fixes a dst entry leak in xfrm6_rcv_encap() by ensuring the dst is released when ip6_route_input_lookup() returns an error route.

net/ipv6/xfrm6_protocol.c int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi, · line 90

Quoted 8 lines
net/ipv6/xfrm6_protocol.c +0 -0
index ea2f805d3b014..9b586fcec4850 100644
@@ -88,8 +88,10 @@ int xfrm6_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
 
 		dst = ip6_route_input_lookup(dev_net(skb->dev), skb->dev, &fl6,
 					     skb, flags);

This isn't a bug introduced by this patch, but is it possible for skb->dev to be NULL here?

When ESP packets are delivered over TCP, they are processed asynchronously via handle_esp() in net/xfrm/espintcp.c. This dynamically resolves skb->dev using dev_get_by_index_rcu(). If the interface was removed before this runs, it returns NULL.

Because TCP receive paths drop the skb's dst entry, skb_dst(skb) is NULL. When handle_esp() passes the skb to xfrm6_rcv_encap(), it enters the !skb_dst(skb) block and unconditionally accesses skb->dev->ifindex and calls dev_net(skb->dev).

Could this result in a NULL pointer dereference if the interface is torn down while packets are being processed asynchronously?

Quoted 8 lines
net/ipv6/xfrm6_protocol.c +3 -1
-		if (dst->error)
+		if (dst->error) {
+			dst_release(dst);
 			goto drop;
+		}
 		skb_dst_set(skb, dst);
 	}
 
maintainer Applied
#4 lore raw
In reply to Ren Wei
Quoted 12 lines · Ren Wei

From: Yilin Zhu <[email protected]>

xfrm6_rcv_encap() performs an IPv6 route lookup when the skb does not already have a dst attached. ip6_route_input_lookup() returns a referenced dst entry even when the lookup resolves to an error route.

If dst->error is set, xfrm6_rcv_encap() drops the skb without attaching the dst to the skb and without releasing the reference returned by the lookup. Repeated packets hitting this path therefore leak dst entries.

Release the dst before jumping to the drop path.

Quoted 10 lines
Fixes 0146dca70b87 ("xfrm: add support for UDPv6 encapsulation of ESP")

Applied to the ipsec tree, thanks a lot!