[PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload

From: Chris Mason

Date: Sat Dec 05 2026 - 07:59:08 EST


A decap with bpf_skb_adjust_room() corrupts a non-GSO packet if ESP still
has to encrypt it.

The transport header has to stay at the ESP header until esp_xmit() runs.
bpf_skb_net_shrink() checks for ESP on GSO packets, but not on others:

sch_handle_egress()
bpf_skb_net_shrink() /* decap of IP | IP | ESP | UDP */
bpf_skb_clear_encap() /* transport_header: ESP -> UDP */
validate_xmit_xfrm()
esp_xmit() /* writes the SPI over the UDP header */

The fix used here is to check for a pending ESP offload on non-GSO
packets too.

Fixes: ec20dee2f2c4 ("bpf: Clear decap state on skb_adjust_room shrink path")
Fixes: 33be492c2e23 ("bpf: Use the inner transport header after a decap")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
---
net/core/filter.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index 892e20291283d..001837ff24eb6 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3880,9 +3880,15 @@ static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
shinfo->gso_type |= SKB_GSO_DODGY;
shinfo->gso_segs = 0;
} else {
- /* For non-GSO packets, clear encapsulation if decap flags are set */
- if (flags & (BPF_F_ADJ_ROOM_DECAP_L4_MASK |
- BPF_F_ADJ_ROOM_DECAP_IPXIP_MASK))
+ struct xfrm_offload *xo = xfrm_offload(skb);
+
+ /*
+ * For non-GSO packets, clear encapsulation if decap flags are
+ * set, unless ESP still has to encrypt the packet.
+ */
+ if ((flags & (BPF_F_ADJ_ROOM_DECAP_L4_MASK |
+ BPF_F_ADJ_ROOM_DECAP_IPXIP_MASK)) &&
+ (!xo || (xo->flags & CRYPTO_DONE)))
bpf_skb_clear_encap(skb, off);
}