[PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto()

From: Hu Xinyao

Date: Mon Nov 30 2026 - 00:00:35 EST


On L3 interfaces such as IFF_TUN and wireguard, ingress packets enter TC
BPF programs with skb->mac_header unset (~0U) and skb->data at the
network header.

bpf_skb_adjust_room() and bpf_skb_change_proto() call
skb_mac_header_len() unconditionally, triggering DEBUG_NET_WARN_ON_ONCE()
and wrapping the u32 offset to ~4GiB (which causes
bpf_skb_generic_{push,pop}() to fail with -ENOMEM since commit
ee36c9aa44ef ("bpf: Check the offset in bpf_skb_generic_{push,pop}()")).
In addition, bpf_skb_net_hdr_{push,pop}() adjust skb->mac_header even
when it is unset, and bpf_skb_net_grow() computes mac_len from the unset
header during encapsulation.

Use a helper that returns 0 when !skb_mac_header_was_set(skb) and only
update skb->mac_header in bpf_skb_net_hdr_{push,pop}() when it is set.

Found during a GLM-5.4-assisted code audit and verified manually against
mainline; compile-tested only (not triggered at runtime).

Fixes: 6578171a7ff0 ("bpf: add bpf_skb_change_proto helper")
Fixes: 2be7e212d541 ("bpf: add bpf_skb_adjust_room helper")
Signed-off-by: Hu Xinyao <huxinyao@xxxxxxxxxxxxxxx>
---
net/core/filter.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/net/core/filter.c b/net/core/filter.c
index ea20fd2..978f4c0 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -3419,6 +3419,11 @@ static bool bpf_skb_trans_hdr_moves(const struct sk_buff *skb, u32 off)
(u32)skb_transport_offset(skb) < off;
}

+static u32 bpf_skb_mac_header_len(const struct sk_buff *skb)
+{
+ return skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0;
+}
+
static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
{
bool trans_moves = bpf_skb_trans_hdr_moves(skb, off);
@@ -3430,7 +3435,8 @@ static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
*/
ret = bpf_skb_generic_push(skb, off, len);
if (likely(!ret)) {
- skb->mac_header -= len;
+ if (skb_mac_header_was_set(skb))
+ skb->mac_header -= len;
skb->network_header -= len;
if (trans_moves)
skb->transport_header -= len;
@@ -3447,7 +3453,8 @@ static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
/* Same here, __skb_push()/__skb_pull() pair not needed. */
ret = bpf_skb_generic_pop(skb, off, len);
if (likely(!ret)) {
- skb->mac_header += len;
+ if (skb_mac_header_was_set(skb))
+ skb->mac_header += len;
skb->network_header += len;
if (trans_moves) {
skb->transport_header += len;
@@ -3469,7 +3476,7 @@ static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
{
const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
const u8 meta_len = skb_metadata_len(skb);
- u32 off = skb_mac_header_len(skb);
+ u32 off = bpf_skb_mac_header_len(skb);
int ret;

ret = skb_cow(skb, meta_len + len_diff);
@@ -3500,7 +3507,7 @@ static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
{
const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
- u32 off = skb_mac_header_len(skb);
+ u32 off = bpf_skb_mac_header_len(skb);
int ret;

ret = skb_unclone(skb, GFP_ATOMIC);
@@ -3696,7 +3703,7 @@ static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
if (skb->encapsulation)
return -EALREADY;

- mac_len = skb->network_header - skb->mac_header;
+ mac_len = bpf_skb_mac_header_len(skb);
inner_net = skb->network_header;
if (inner_mac_len > len_diff)
return -EINVAL;
@@ -3948,7 +3955,7 @@ BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
proto != htons(ETH_P_IPV6)))
return -ENOTSUPP;

- off = skb_mac_header_len(skb);
+ off = bpf_skb_mac_header_len(skb);
switch (mode) {
case BPF_ADJ_ROOM_NET:
off += bpf_skb_net_base_len(skb);
--
2.43.0