Skip to content

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

Qihang lkml netdev 7 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest3 patches 0 repliesNew
  2. v73 patches 0 repliesSuperseded
  3. v63 patches 0 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v53 patches2 repliesSuperseded
v43 patches1 replySuperseded
v23 patches0 repliesSuperseded
v12 patches6 repliesSuperseded

Changes in v8 from the cover letter

- rebased onto net main fafbc4051. The drain of @sublist added in v5 is
dropped: "xfrm: iptfs: fix skb leak on error in __input_process_payload()" is in net now and delivers those packets.
- new patch 2: hand the IPTFS drop timer back to the reorder window when a reassembly completes, so a stalled window cannot pin the state (and the tunnel reference added by patch 3) forever; reported by the AI review of v7.
- the former 2/3 (address family before the invalid-state check) is folded into 3/3, because the early drop path in 3/3 needs the family to release the cached tunnel. This gives a clean bisect: no intermediate commit sets the family without using it.
- the sentence in 3/3 that claimed the parked skbs are always released by the drop timer is gone; patch 2 is what makes that true.

Patches in v8 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3xfrm: use skb->dev before dropping the reference held across asyncnet/xfrm/xfrm_input.c+4 -30 replies
2/3xfrm: iptfs: hand the drop timer back to the reorder windownet/xfrm/xfrm_iptfs.c+9 -00 replies
3/3xfrm: hold the tunnel device reference across the input pathnet/ipv4/ip_vti.c net/ipv6/ip6_vti.c net/ipv6/xfrm6_input.c +3 more+91 -160 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v8 yet.

Read the v8 thread

Cover letter [PATCH net v8 0/3] 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 1 makes the two exits that still used skb->dev after the async reference was dropped use it first. Patch 3 sets the address family before the early invalid-state drop, which now has to release the cached tunnel.

Patch 2 is new. The IPTFS drop timer is cancelled when an in-progress reassembly completes, although the same timer ages out the reorder window. A window that is never drained holds its queued packets and, with patch 3, the tunnel reference they carry, so the device could not be unregistered. Patch 2 rearms the timer for the window's own deadline once the reassembly is done, so that a later reassembly does not inherit the earlier expiry.

Patch 2 is deliberately minimal. It only bounds the stale window, which is what 3/3 needs so that the reference it adds cannot pin the device; a wider rework of the IPTFS timer is orthogonal and can land on its own.

Rebased onto net main fafbc4051, which already carries the iptfs skb leak fix on the payload error path. That fix moved the done label ahead of the delivery loop, so the inner packets parsed before a validation failure are now delivered. The drain that the previous revision added to 3/3 is therefore gone.

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 v7:
- rebased onto net main fafbc4051. The drain of @sublist added in v5 is
dropped: "xfrm: iptfs: fix skb leak on error in __input_process_payload()" is in net now and delivers those packets.
- new patch 2: hand the IPTFS drop timer back to the reorder window when a reassembly completes, so a stalled window cannot pin the state (and the tunnel reference added by patch 3) forever; reported by the AI review of v7.
- the former 2/3 (address family before the invalid-state check) is folded into 3/3, because the early drop path in 3/3 needs the family to release the cached tunnel. This gives a clean bisect: no intermediate commit sets the family without using it.
- the sentence in 3/3 that claimed the parked skbs are always released by the drop timer is gone; patch 2 is what makes that true.

Changes since v6:
- patch 1 also drops the skb->dev reference that is held across an async resumption only after gro_cells_receive() in the decaps success exit; that exit still used skb->dev after the reference was dropped (found by the netdev AI review).

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 3 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.

The IPTFS drop-timer/reorder/state-teardown paths, including the window-pin sequence behind patch 2, are covered by code audit, not by dedicated runtime triggers.

v8 was build-tested on fafbc4051: the arm64 objects touched by the series (xfrm, xfrm_iptfs, ip_vti, ip6_vti, xfrm6_input) compile with XFRM, IPTFS, VTI and ESP enabled.

Tooling: the source audit, the patch text and parts of these messages were produced with an AI coding assistant under my direction. I reviewed the result and stand behind every line of it; the build and runtime results above were obtained from earlier revisions of these changes.

Previous versions:
https://lore.kernel.org/netdev/[email protected]/ (v7)
https://lore.kernel.org/netdev/[email protected]/ (v6)
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: use skb->dev before dropping the reference held across async
xfrm: iptfs: hand the drop timer back to the reorder window
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  | 69 +++++++++++++++++++++++++++++++++++++++---
 net/xfrm/xfrm_iptfs.c  | 40 +++++++++++++++---------
 6 files changed, 103 insertions(+), 18 deletions(-)
Signature · 1 line
2.54.0 (Apple Git-157)