Skip to contentSkip to conversation

xfrm: fix vti tunnel device use-after-free across async crypto resumption

PATCH Superseded Qihang 1 participant 4 messages Oct 22

Subsystems (4)

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Qihang

author
#0 lore raw

v6 cover xfrm: fix vti tunnel device use-after-free across async crypto resumption

This replaces the earlier vti/vti6 series and the reference-transfer rework.

vti_input() and vti6_input_proto() cache the tunnel pointer in the skb control buffer without taking a reference. With async crypto, the receive callback runs from crypto completion and may run after the tunnel device has been torn down, causing a use-after-free when the callback dereferences the cached tunnel.

The input hook takes a reference on the tunnel device when caching the tunnel. A successful receive callback leaves the cached tunnel in place; xfrm_input() releases the reference on every exit, including after delivery and on drop paths. The error callback only updates the error counters, so the common drop path releases the tunnel reference after the skb has been freed. IPTFS, which can free or consume the outer skb itself, releases the reference through xfrm_input_drop() or xfrm_input_consume().

Patch 3 relies on the first two patches: the receive callback must run before the async skb->dev reference is dropped, and the address family must be available on the early invalid-state drop path.

The issue was found by source audit of the VTI input path (LLM-assisted review): the tunnel pointer cached in the skb control buffer is dereferenced by the receive callback, which with async crypto can run after the device has been torn down, and nothing keeps the device alive across that window. It was confirmed with a KASAN reproducer on arm64 (async ESP; deleting the VTI device inside the crypto window gives "slab-use-after-free in vti_rcv_cb").

Changes since v5:
- cover letter only: record how the issue was found.

Changes since v4:
- IPTFS: when an inner packet fails validation, __input_process_payload() can reach the drop path before the inner packets queued for delivery are sent. Drop those queued packets as well so the outer skb's tunnel device reference is not leaked (found by the Sashiko review of v4 3/3).

Changes since v3:
- the cached pointer is always a tunnel; the address family selects its type, and a non-NULL tunnel slot denotes one held device reference
- xfrm_input() takes the tunnel device out of the skb before gro_cells_receive()/transport_finish(), which may consume the skb
- the device reference is dropped only after the skb has been freed, including on error paths
- IPTFS passes the state's address family explicitly instead of inferring it from the secpath; consumed skbs keep consume_skb() semantics and drop paths keep their skb drop reason
- drop the xfrmi changes: it re-looks up the interface and does not participate in the reference transfer
- merge the reference and IPTFS changes into one patch so the series remains bisectable
- the gro_cells NAPI window is pre-existing and is documented, not changed

Runtime testing (aarch64 QEMU; KASAN for UAF detection):
- VTI async success and error: RTM_DELLINK completes without a KASAN report; the unfixed tree reports slab-use-after-free in vti_rcv_cb.
- Async resume with the SA deleted during the crypto window: the reference is released and the device is removed; with patch 2 reverted, xfrm_input_take_tunnel_dev() warns and unregister_netdevice() waits for vti0.
- IPTFS outer-skb free: RTM_DELLINK completes; with the !first_skb xfrm_input_drop() reverted to kfree_skb(), the reference leaks and unregister_netdevice() waits for vti0.
- IPTFS aggregated payload with a valid first inner packet followed by an inner packet with an invalid IPv4 header length: RTM_DELLINK completes; without the sublist drain, the tunnel device reference leaks and unregister_netdevice() waits for vti0 (Usage count = 2).

The IPTFS timer/reorder/state-teardown paths are covered by code audit, not by dedicated runtime triggers.

Previous versions:
https://lore.kernel.org/netdev/[email protected]/ (v5)
https://lore.kernel.org/netdev/[email protected]/ (v4)
https://lore.kernel.org/netdev/[email protected]/ (v2)
https://lore.kernel.org/netdev/[email protected]/ (v1)

Qihang (3):
xfrm: run the receive callback before dropping the skb->dev reference
xfrm: set the address family before the early invalid-state drop
xfrm: hold the tunnel device reference across the input path

 net/ipv4/ip_vti.c      |  3 ++
 net/ipv6/ip6_vti.c     |  2 ++
 net/ipv6/xfrm6_input.c |  3 ++
 net/xfrm/xfrm_inout.h  |  4 +++
 net/xfrm/xfrm_input.c  | 63 +++++++++++++++++++++++++++++++++++++++++-
 net/xfrm/xfrm_iptfs.c  | 42 ++++++++++++++++++----------
 6 files changed, 105 insertions(+), 19 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)
author
#1 lore raw

PATCH v6 1/3 xfrm: run the receive callback before dropping the skb->dev reference

The drop path in xfrm_input() releases the skb->dev reference that is held across an async resumption before it runs the receive callbacks. The callbacks may still dereference skb->dev, xfrmi_rcv_cb() reads skb->dev->ifindex, so they can race a concurrent teardown of the device. Run the callbacks first and drop the reference afterwards, like the success path does.

The early drop for a state that is no longer valid releases the reference on its own; let the drop label handle that reference too.

Fixes 1c428b038400 ("xfrm: hold dev ref until after transport_finish NF_HOOK")
---
 net/xfrm/xfrm_input.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_input.c +2 -2
index 5ed87d51392a..94de4f698c33 100644
@@ -496,7 +496,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 					       LINUX_MIB_XFRMINSTATEINVALID);
 
 			if (encap_type == -1)
-				dev_put(dev);
+				async = 1;
 			goto drop;
 		}
 
@@ -779,9 +779,9 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 drop_unlock:
 	spin_unlock(&x->lock);
 drop:
+	xfrm_rcv_cb(skb, family, xfrm_proto, -1);
 	if (async)
 		dev_put(dev);
-	xfrm_rcv_cb(skb, family, xfrm_proto, -1);
 	kfree_skb(skb);
 	return 0;
 }
Signature · 1 line
2.54.0 (Apple Git-157)
author
#2 lore raw

PATCH v6 2/3 xfrm: set the address family before the early invalid-state drop

In reply to Qihang

The early drop in xfrm_input() for a state that is no longer valid runs while family is still AF_UNSPEC, so xfrm_rcv_cb() cannot find the afinfo and the receive callback is never invoked for such a packet. Set the family from the state before the check so that the drop path dispatches the callback like every other drop does.

This is also a prerequisite for releasing the tunnel device reference on that drop path in the next patch: without a family, the release cannot tell an ip_tunnel from an ip6_tnl.

Fixes 4ce3dbe397d7 ("xfrm: Fix xfrm_input() to verify state is valid when (encap_type < 0)")
---
 net/xfrm/xfrm_input.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/xfrm/xfrm_input.c +1 -2
index 94de4f698c33..517369ba9efe 100644
@@ -487,6 +487,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 				      encap_type == UDP_ENCAP_ESPINUDP))) {
 		x = xfrm_input_state(skb);
 		xfrm_proto = x->type ? x->type->proto : nexthdr;
+		family = x->props.family;
 
 		if (unlikely(x->km.state != XFRM_STATE_VALID)) {
 			if (x->km.state == XFRM_STATE_ACQ)
@@ -500,8 +501,6 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 			goto drop;
 		}
 
-		family = x->props.family;
-
 		/* An encap_type of -2 indicates reconstructed inner packet */
 		if (encap_type == -2)
 			goto resume_decapped;
Signature · 1 line
2.54.0 (Apple Git-157)
author
#3 lore raw

PATCH v6 3/3 xfrm: hold the tunnel device reference across the input path

In reply to Qihang

vti_input() and vti6_input_proto() cache the tunnel pointer in the skb control buffer without taking a reference. With async crypto the receive callback runs from the crypto completion, after a possible device teardown, and dereferences the freed tunnel device:

    BUG: KASAN: slab-use-after-free in vti_rcv_cb

Take a reference on the tunnel device when the pointer is cached. The tunnel is the device's private area, so the reference keeps the tunnel structure alive as well. xfrm6_rcv_spi() takes the same reference for the tunnel it is given; it is a separate entry point, so vti6 does not hold the reference twice.

A successful vti/vti6 callback leaves the cached tunnel in place for xfrm_input() to release after delivery. A callback that fails only updates the error counters; the common drop path releases the reference after the skb has been freed. The helper takes the device out of the skb before gro_cells_receive() and transport_finish(), which may already consume the skb. The device reference is therefore always dropped after the skb is gone.

IPTFS consumes the outer skb itself and frees it directly, so it must release the reference as well. The IPTFS state carries the address family the tunnel was resolved for, and the caller passes it in. Add xfrm_input_drop() and xfrm_input_consume() for the outer-skb free paths in __input_process_payload(), iptfs_input_ordered(), iptfs_input(), iptfs_drop_timer() and iptfs_destroy_state(). Only the outer skb carries the cached tunnel; the inner skbs built by iptfs_alloc_skb() have a zeroed control buffer, so they do not release it. The drop paths keep their skb drop reason and the consumed skb keeps consume_skb() semantics. An inner packet that fails validation reaches the drop path before the queued inner packets are delivered; drop those as well so the outer skb's reference is not leaked.

The reference is released right after gro_cells_receive(); the NAPI that processes the queued skb later still uses skb->dev without a reference. That window is pre-existing and is not changed here.

This depends on the previous two patches: the receive callbacks must run before the async dev_put(), and the early drop must know the address family to release the reference.

Fixes df3893c176e9 ("vti: Update the ipv4 side to use it's own receive hook.")
Fixes fa9ad96d4905 ("vti6: Update the ipv6 side to use its own receive hook.")

---
v5:
- __input_process_payload(): drop the inner packets queued on the delivery list when an inner packet fails validation and jumps to the drop label, so the outer skb's tunnel device reference is released (found by the Sashiko review).

 net/ipv4/ip_vti.c      |  3 ++
 net/ipv6/ip6_vti.c     |  2 ++
 net/ipv6/xfrm6_input.c |  3 ++
 net/xfrm/xfrm_inout.h  |  4 +++
 net/xfrm/xfrm_input.c  | 63 +++++++++++++++++++++++++++++++++++++++++-
 net/xfrm/xfrm_iptfs.c  | 42 ++++++++++++++++++----------
 6 files changed, 102 insertions(+), 15 deletions(-)
net/ipv4/ip_vti.c +3 -0
index 3b80929994a0..0277812852e7 100644
@@ -63,6 +63,9 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
 
 		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = tunnel;
 
+		/* Keep the tunnel alive until xfrm_input() is done with the skb. */
+		dev_hold(tunnel->dev);
+
 		if (update_skb_dev)
 			skb->dev = tunnel->dev;
 
net/ipv6/ip6_vti.c +2 -0
index ab94b3a4ba9c..6715bf10bc85 100644
@@ -319,6 +319,8 @@ static int vti6_input_proto(struct sk_buff *skb, int nexthdr, __be32 spi,
 			goto discard;
 		}
 
+		/* Keep the tunnel alive until xfrm_input() is done with the skb. */
+		dev_hold(t->dev);
 		rcu_read_unlock();
 
 		XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
net/ipv6/xfrm6_input.c +3 -0
index 07edef258984..d27bc24f1b16 100644
@@ -15,6 +15,7 @@
 #include <linux/netfilter.h>
 #include <linux/netfilter_ipv6.h>
 #include <net/ipv6.h>
+#include <net/ip6_tunnel.h>
 #include <net/xfrm.h>
 #include <net/protocol.h>
 #include <net/gro.h>
@@ -23,6 +24,8 @@ int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
 		  struct ip6_tnl *t)
 {
 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = t;
+	if (t)
+		dev_hold(t->dev);
 	XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
 	return xfrm_input(skb, nexthdr, spi, 0);
net/xfrm/xfrm_inout.h +4 -0
index efc5e6b2e87b..8bd05f62ad56 100644
@@ -6,6 +6,10 @@
 #ifndef XFRM_INOUT_H
 #define XFRM_INOUT_H 1
 
+void xfrm_input_drop(struct sk_buff *skb, unsigned int family,
+		     enum skb_drop_reason reason);
+void xfrm_input_consume(struct sk_buff *skb, unsigned int family);
+
 static inline void xfrm4_extract_header(struct sk_buff *skb)
 {
 	const struct iphdr *iph = ip_hdr(skb);
net/xfrm/xfrm_input.c +62 -1

102 lines not on this page · raw message

net/xfrm/xfrm_iptfs.c +28 -14

83 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)