* [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; 5+ 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] 5+ 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 13:30 ` sashiko-bot
2026-12-05 12:58 ` [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload Chris Mason
1 sibling, 1 reply; 5+ 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] 5+ 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; 5+ 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] 5+ messages in thread
* Re: [PATCH bpf 1/2] bpf: Don't use an unset inner transport header
2026-12-05 12:57 ` [PATCH bpf 1/2] bpf: Don't use an unset inner transport header Chris Mason
@ 2026-12-05 13:30 ` sashiko-bot
2026-12-05 14:32 ` Chris Mason
0 siblings, 1 reply; 5+ messages in thread
From: sashiko-bot @ 2026-12-05 13:30 UTC (permalink / raw)
To: Chris Mason, bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] bpf: s16 cast in bpf_skb_net_hdr_pop() truncates inner
transport offset
- [Medium] bpf: bpf_skb_net_hdr_pop() misplaces inner_transport_header
and masks GRO bug
--
commit 1a1076a6002794e99517c8507268d7aa9c97eab5
Author: Chris Mason <mason@kernel.org>
bpf: Don't use an unset inner transport header
This commit casts skb_inner_transport_offset(skb) to s16 in
bpf_skb_net_hdr_pop() so that an unset inner_transport_header whose u16
offset wrapped after a headroom reduction is still reset to off.
> 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);
> }
[Severity: Medium]
Can casting skb_inner_transport_offset(skb) to s16 clobber valid offsets
>= 32768 and fail to reset offsets < -32768?
virtio_net_hdr_tnl_to_skb() can set inner_transport_header from csum_start
up to U16_MAX - 1 via skb_partial_csum_set(). Under the s16 cast, offsets
>= 32768 sign-extend negative and are overwritten with off.
If skb->encapsulation stays set (for example in bpf_skb_proto_6_to_4()),
that places inner_transport_header behind the outer transport_header,
underflowing thlen in skb_gso_transport_seglen().
In the other direction, RX allows headroom up to SKB_MAX_RX_HEADROOM
(32767) and bpf_skb_generic_pop() pulls up to 4095 bytes before this
check. An unset inner_transport_header at 0 then has an offset below
-32768, which wraps positive under s16 and skips the reset.
[Severity: Medium]
Does resetting a wrapped inner_transport_header to off misplace it when
skb->encapsulation stays set, while leaving the GRO bug unfixed?
When bpf_skb_net_hdr_pop() is not followed by bpf_skb_clear_encap() (for
instance in bpf_skb_adjust_room() without decap flags, or when decapping
only the outer layer of a nested tunnel), off points to the network header
in BPF_ADJ_ROOM_MAC mode.
qdisc_pkt_len_segs_init() then reads the IP header as a TCP header at
skb_inner_transport_offset(skb), dropping packets with
SKB_DROP_REASON_SKB_BAD_GSO or miscomputing gso_segs and pkt_len.
This also leaves the root cause in place: the fraglist early return in
tcp{4,6}_gro_complete() and udp{4,6}_gro_complete() omits setting
inner_transport_header, so non-BPF forwarded encapsulated fraglist-GRO
packets are still dropped in qdisc_pkt_len_segs_init().
Fixing fraglist GRO completion to set inner_transport_header and avoiding
u16 underflow of unset inner headers in skb_headers_offset_update() would
handle both BPF and non-BPF paths without the s16 heuristic.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205012513.15379-1-mason@kernel.org?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf 1/2] bpf: Don't use an unset inner transport header
2026-12-05 13:30 ` sashiko-bot
@ 2026-12-05 14:32 ` Chris Mason
0 siblings, 0 replies; 5+ messages in thread
From: Chris Mason @ 2026-12-05 14:32 UTC (permalink / raw)
To: sashiko-reviews; +Cc: ast, daniel, andrii, bpf, netdev
On Sat, Dec 5, 2026, at 8:30 AM, sashiko-bot@kernel.org wrote:
> Can casting skb_inner_transport_offset(skb) to s16 clobber valid offsets
> >= 32768 and fail to reset offsets < -32768?
>
> virtio_net_hdr_tnl_to_skb() can set inner_transport_header from csum_start
> up to U16_MAX - 1 via skb_partial_csum_set(). Under the s16 cast, offsets
> >= 32768 sign-extend negative and are overwritten with off.
That takes a made-up virtio header. Every segment repeats all of the headers,
so with 32 KiB of them, none would fit an Ethernet MTU.
> An unset inner_transport_header at 0 then has an offset below
> -32768, which wraps positive under s16 and skips the reset.
The decap still gets this one right. bpf_skb_clear_encap() takes the larger
of the two headers, and that's the outer one then.
> Does resetting a wrapped inner_transport_header to off misplace it when
> skb->encapsulation stays set, while leaving the GRO bug unfixed?
It looks like this doesn't make anything worse. A wrapped offset is about
65,000 bytes into the packet, so qdisc_pkt_len_segs_init() already drops it
without the patch.
The GRO bug is real, and Julius Bairaktaris has posted a fix for it:
https://lore.kernel.org/all/bf33ad6cb30391be0c7f80b4714fd96e9874ebd3.1795884313.git.julius@bairaktaris.de/
-chris
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 14:33 UTC | newest]
Thread overview: 5+ 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 13:30 ` sashiko-bot
2026-12-05 14:32 ` Chris Mason
2026-12-05 12:58 ` [PATCH bpf 2/2] bpf: Keep skb->transport_header for ESP offload Chris Mason
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.