Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Hu Xinyao
Mon, 30 Nov 2026 05:00
[PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto()
Author: Hu Xinyao
Date: Mon, 30 Nov 2026 05:00
Date: Mon, 30 Nov 2026 05:00
101 lines
12632 bytes
12632 bytes
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
Re: [PATCH bpf] bpf: Handle unset mac_header in bpf_skb_adjust_room() and bpf_skb_change_proto()
Author: "Alexei Starovoitov"
Date: Mon, 30 Nov 2026 05:28
Date: Mon, 30 Nov 2026 05:28
23 lines
12017 bytes
12017 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads