* [PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto()
@ 2026-11-30 5:00 Hu Xinyao
2026-11-30 5:28 ` Alexei Starovoitov
0 siblings, 1 reply; 2+ messages in thread
From: Hu Xinyao @ 2026-11-30 5:00 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Stanislav Fomichev, Willem de Bruijn, Chris Mason, bpf, netdev,
linux-kernel
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@kylinsec.com.cn>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto()
2026-11-30 5:00 [PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto() Hu Xinyao
@ 2026-11-30 5:28 ` Alexei Starovoitov
0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2026-11-30 5:28 UTC (permalink / raw)
To: Hu Xinyao, Daniel Borkmann, Andrii Nakryiko
Cc: Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
Stanislav Fomichev, Willem de Bruijn, Chris Mason, bpf, netdev,
linux-kernel
On Sun, Nov 29, 2026 at 09:00 PM Hu Xinyao <huxinyao@kylinsec.com.cn> wrote:
> 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.
No. Both drivers call skb_reset_mac_header() before the skb gets to tc:
driver where
--------- ------------------------------------------------
tun __virtio_net_hdr_to_skb(), from tun_get_user()
wireguard wg_reset_packet(), from wg_packet_rx_poll()
skb->data is at the network header at that point.
So skb_mac_header_len() returns 0 and both helpers work.
> Found during a GLM-5.4-assisted code audit and verified manually against
> mainline; compile-tested only (not triggered at runtime).
It cannot be triggered on tun or wireguard.
Pls don't send fixes for bugs that you didn't reproduce.
pw-bot: cr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 5:28 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 5:00 [PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto() Hu Xinyao
2026-11-30 5:28 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox