The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH bpf 0/2] bpf: fix skb->transport_header after a decap
@ 2026-12-05 12:56 Chris Mason
  2026-12-05 12:57 ` [PATCH bpf 1/2] bpf: Don't use an unset inner transport header Chris Mason
  2026-12-05 12:58 ` [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload Chris Mason
  0 siblings, 2 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-05 12:56 UTC (permalink / raw)
  To: ast, daniel, andrii, bpf, netdev
  Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, john.fastabend, sdf, davem, edumazet, kuba, pabeni,
	willemb, horms, nhudson, linux-kernel, mason

Hi everyone,

These are AI generated fixes for two bugs in my commit 33be492c2e23 ("bpf: Use
the inner transport header after a decap").  A decap with bpf_skb_adjust_room()
can move skb->transport_header to the wrong place.

- Patch 1: nobody has set the inner transport header, and its offset has
  wrapped.  The kernel drops valid GSO packets then.
- Patch 2: ESP still has to encrypt the packet.  It gets corrupted or dropped,
  and a very short one can hit a BUG().

AI generated tests reproduce both bugs on v7.4-rc5, and pass with the patches.
7.3.7 has both bugs too.

This is on top of v7.4-rc5.

Chris Mason (2):
  bpf: Don't use an unset inner transport header
  bpf: Keep skb->transport_header for ESP offload

 net/core/filter.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)


base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH bpf 1/2] bpf: Don't use an unset inner transport header
  2026-12-05 12:56 [PATCH bpf 0/2] bpf: fix skb->transport_header after a decap Chris Mason
@ 2026-12-05 12:57 ` Chris Mason
  2026-12-05 12:58 ` [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload Chris Mason
  1 sibling, 0 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-05 12:57 UTC (permalink / raw)
  To: ast, daniel, andrii, bpf, netdev
  Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, john.fastabend, sdf, davem, edumazet, kuba, pabeni,
	willemb, horms, nhudson, linux-kernel, mason

A decap with bpf_skb_adjust_room() corrupts skb->transport_header if an
unset skb->inner_transport_header has wrapped around.

An unset inner transport header is in front of skb->data, so
bpf_skb_net_hdr_pop() resets it. But its offset wraps around if the skb is
copied to a buffer with less headroom:

    tcp4_gro_complete()             /* fraglist: inner_transport_header
                                       stays 0 */
    skb_pp_cow_data()               /* 64 bytes less headroom */
      skb_headers_offset_update()   /* inner_transport_header = 65472 */
    bpf_skb_net_shrink()
      bpf_skb_net_hdr_pop()         /* doesn't reset it */
      bpf_skb_clear_encap()         /* copies it to transport_header */
    qdisc_pkt_len_segs_init()       /* SKB_DROP_REASON_SKB_BAD_GSO */

The fix used here is to treat a wrapped offset as one in front of
skb->data.

Fixes: 33be492c2e23 ("bpf: Use the inner transport header after a decap")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
---
 net/core/filter.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2be65c0..892e20291283d 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3456,9 +3456,12 @@ static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
 			/* It was in the removed bytes or before skb->data. */
 			skb_set_transport_header(skb, off);
 		}
-		/* Do not leave the inner transport header in front of @off. */
+		/*
+		 * Do not leave the inner transport header in front of @off. If
+		 * nobody has set it, its offset can have wrapped.
+		 */
 		if (skb->encapsulation &&
-		    skb_inner_transport_offset(skb) < (int)off)
+		    (s16)skb_inner_transport_offset(skb) < (int)off)
 			skb_set_inner_transport_header(skb, off);
 	}
 


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload
  2026-12-05 12:56 [PATCH bpf 0/2] bpf: fix skb->transport_header after a decap Chris Mason
  2026-12-05 12:57 ` [PATCH bpf 1/2] bpf: Don't use an unset inner transport header Chris Mason
@ 2026-12-05 12:58 ` Chris Mason
  1 sibling, 0 replies; 3+ messages in thread
From: Chris Mason @ 2026-12-05 12:58 UTC (permalink / raw)
  To: ast, daniel, andrii, bpf, netdev
  Cc: eddyz87, memxor, martin.lau, song, yonghong.song, jolsa, emil,
	ihor.solodrai, john.fastabend, sdf, davem, edumazet, kuba, pabeni,
	willemb, horms, nhudson, linux-kernel, mason

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@vger.kernel.org
Signed-off-by: Chris Mason <mason@kernel.org>
---
 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);
 	}
 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05 12:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 12:56 [PATCH bpf 0/2] bpf: fix skb->transport_header after a decap Chris Mason
2026-12-05 12:57 ` [PATCH bpf 1/2] bpf: Don't use an unset inner transport header Chris Mason
2026-12-05 12:58 ` [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload Chris Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox