support SKF_NET_OFF and SKF_LL_OFF on skb frags
Subsystems (6)
v2 cover support SKF_NET_OFF and SKF_LL_OFF on skb frags
From: Willem de Bruijn <[email protected]>
Address a longstanding issue that may lead to missed packets depending on system configuration.
Ensure that reading from packet contents works regardless of skb geometry, also when using the special SKF_.. negative offsets to offset from L2 or L3 header.
Patch 2 is the selftest for the fix.
Willem de Bruijn (2):
bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags selftests/net: test sk_filter support for SKF_NET_OFF on frags
include/linux/filter.h | 3 - kernel/bpf/core.c | 21 -- net/core/filter.c | 80 ++++--- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/skf_net_off.c | 244 +++++++++++++++++++++ tools/testing/selftests/net/skf_net_off.sh | 30 +++ 7 files changed, 321 insertions(+), 60 deletions(-) create mode 100644 tools/testing/selftests/net/skf_net_off.c create mode 100755 tools/testing/selftests/net/skf_net_off.sh
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
PATCH v2 1/2 bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
From: Willem de Bruijn <[email protected]>
Classic BPF socket filters with SKB_NET_OFF and SKB_LL_OFF fail to read when these offsets extend into frags.
This has been observed with iwlwifi and reproduced with tun with IFF_NAPI_FRAGS. The below straightforward socket filter on UDP port, applied to a RAW socket, will silently miss matching packets.
const int offset_proto = offsetof(struct ip6_hdr, ip6_nxt);
const int offset_dport = sizeof(struct ip6_hdr) + offsetof(struct udphdr, dest);
struct sock_filter filter_code[] = {
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_NET_OFF + offset_proto),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 2),
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, SKF_NET_OFF + offset_dport),This is unexpected behavior. Socket filter programs should be consistent regardless of environment. Silent misses are particularly concerning as hard to detect.
Use skb_copy_bits for offsets outside linear, same as done for non-SKF_(LL|NET) offsets.
Offset is always positive after subtracting the reference threshold SKB_(LL|NET)_OFF, so is always >= skb_(mac|network)_offset. The sum of the two is an offset against skb->data, and may be negative, but it cannot point before skb->head, as skb_(mac|network)_offset would too.
This appears to go back to when frag support was introduced to sk_run_filter in linux-2.4.4, before the introduction of git.
The amount of code change and 8/16/32 bit duplication are unfortunate. But any attempt I made to be smarter saved very few LoC while complicating the code.
---
v1->v2
- introduce bfp_skb_load_helper_convert_offset to avoid open coding
--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)
include/linux/filter.h +0 -3
@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;
kernel/bpf/core.c +0 -21
@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ; - }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -} -/* tell bpf programs that include vmlinux.h kernel's PAGE_SIZE */ enum page_size_enum { __PAGE_SIZE =PAGE_SIZE
net/core/filter.c +44 -36
110 lines not on this page · raw message
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Re: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
Quoted 45 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
Classic BPF socket filters with
SKB_NET_OFFandSKB_LL_OFFfail to read when these offsets extend into frags.This has been observed with iwlwifi and reproduced with tun with
IFF_NAPI_FRAGS. The below straightforward socket filter on UDP port, applied to a RAW socket, will silently miss matching packets.const int offset_proto = offsetof(struct ip6_hdr, ip6_nxt); const int offset_dport = sizeof(struct ip6_hdr) + offsetof(struct udphdr, dest); struct sock_filter filter_code[] = { BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4), BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_NET_OFF + offset_proto), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 2), BPF_STMT(BPF_LD + BPF_H + BPF_ABS, SKF_NET_OFF + offset_dport),This is unexpected behavior. Socket filter programs should be consistent regardless of environment. Silent misses are particularly concerning as hard to detect.
Use
skb_copy_bitsfor offsets outside linear, same as done for non-SKF_(LL|NET) offsets.Offset is always positive after subtracting the reference threshold SKB_(LL|NET)_OFF, so is always >= skb_(mac|network)_offset. The sum of the two is an offset against
skb->data, and may be negative, but it cannot point beforeskb->head, as skb_(mac|network)_offset would too.This appears to go back to when frag support was introduced to
sk_run_filterin linux-2.4.4, before the introduction of git.The amount of code change and 8/16/32 bit duplication are unfortunate. But any attempt I made to be smarter saved very few LoC while complicating the code.
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by Matt Moeller <[email protected]>Co-developed-by Maciej Żenczykowski <[email protected]>Signed-off-by Maciej Żenczykowski <[email protected]>Signed-off-by Willem de Bruijn <[email protected]>
Quoted 4 lines
---
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding
Thank you!
Daniel Borkmann
maintainerRe: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
In reply to Willem de BruijnHi Willem,
On 4/4/25 4:23 PM, Willem de Bruijn wrote:
[...]
Quoted 40 lines
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)include/linux/filter.h +0
-3@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;kernel/bpf/core.c +0
-10@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ;
kernel/bpf/core.c line 90
Quoted 10 lines
kernel/bpf/core.c +0
-10- }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -}
Wouldn't this break sparc 32bit JIT which still calls into this?
arch/sparc/net/bpf_jit_asm_32.S :
#define bpf_negative_common(LEN) \
save %sp, -SAVE_SZ, %sp; \
mov %i0, %o0; \
mov r_OFF, %o1; \
SIGN_EXTEND(%o1); \
call bpf_internal_load_pointer_neg_helper; \
mov (LEN), %o2; \
mov %o0, r_TMP; \
cmp %o0, 0; \
BE_PTR(bpf_error); \
restore;Thanks,
Daniel
Re: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
Quoted 60 lines · Daniel Borkmann
Hi Willem,
On 4/4/25 4:23 PM, Willem de Bruijn wrote:
[...]Quoted 40 lines
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)include/linux/filter.h +0
-3@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;kernel/bpf/core.c +0
-10@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ;kernel/bpf/core.c line 90
Quoted 10 lines
kernel/bpf/core.c +0
-10- }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -}Wouldn't this break sparc 32bit JIT which still calls into this?
arch/sparc/net/bpf_jit_asm_32.S:
Quoted 11 lines
#define bpf_negative_common(LEN) \
save %sp, -SAVE_SZ, %sp; \
mov %i0, %o0; \
mov r_OFF, %o1; \
SIGN_EXTEND(%o1); \
call bpf_internal_load_pointer_neg_helper; \
mov (LEN), %o2; \
mov %o0, r_TMP; \
cmp %o0, 0; \
BE_PTR(bpf_error); \
restore;Argh, good catch. Thanks Daniel.
I'll drop the removal of bpf_internal_load_pointer_neg_helper from the patch.
Re: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
Quoted 74 lines · Willem de Bruijn
Quoted 60 lines · Daniel Borkmann
Hi Willem,
On 4/4/25 4:23 PM, Willem de Bruijn wrote:
[...]Quoted 40 lines
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)include/linux/filter.h +0
-3@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;kernel/bpf/core.c +0
-10@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ;kernel/bpf/core.c line 90
Quoted 10 lines
kernel/bpf/core.c +0
-10- }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -}Wouldn't this break sparc 32bit JIT which still calls into this?
arch/sparc/net/bpf_jit_asm_32.S:Quoted 11 lines
#define bpf_negative_common(LEN) \ save %sp, -SAVE_SZ, %sp; \ mov %i0, %o0; \ mov r_OFF, %o1; \ SIGN_EXTEND(%o1); \ call bpf_internal_load_pointer_neg_helper; \ mov (LEN), %o2; \ mov %o0, r_TMP; \ cmp %o0, 0; \ BE_PTR(bpf_error); \ restore;
Quoted 3 lines
Argh, good catch. Thanks Daniel.
I'll drop the removal of
bpf_internal_load_pointer_neg_helperfrom the patch.
add a 'deprecated only used by sparc32 comment'
hopefully someone that knows sparc32 assembly can fix it
--
Maciej Żenczykowski, Kernel Networking Developer @ Google
Daniel Borkmann
maintainerRe: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
Quoted 75 lines · Maciej Żenczykowski
Quoted 73 lines · Willem de Bruijn
Quoted 71 lines · Daniel Borkmann
Hi Willem,
On 4/4/25 4:23 PM, Willem de Bruijn wrote:
[...]Quoted 40 lines
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)include/linux/filter.h +0
-3@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;kernel/bpf/core.c +0
-10@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ;kernel/bpf/core.c line 90
Quoted 10 lines
kernel/bpf/core.c +0
-10- }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -}Wouldn't this break sparc 32bit JIT which still calls into this?
arch/sparc/net/bpf_jit_asm_32.S:#define bpf_negative_common(LEN) \ save %sp, -SAVE_SZ, %sp; \ mov %i0, %o0; \ mov r_OFF, %o1; \ SIGN_EXTEND(%o1); \ call bpf_internal_load_pointer_neg_helper; \ mov (LEN), %o2; \ mov %o0, r_TMP; \ cmp %o0, 0; \ BE_PTR(bpf_error); \ restore;
Quoted 7 lines
Quoted 3 lines
Argh, good catch. Thanks Daniel.
I'll drop the removal of
bpf_internal_load_pointer_neg_helperfrom the patch.add a 'deprecated only used by sparc32 comment'
hopefully someone that knows sparc32 assembly can fix it
Alternatively, the bpf_internal_load_pointer_neg_helper() could be moved entirely over into arch/sparc/net/ so that others won't be tempted to reuse.
Cheers,
Daniel
Re: [PATCH bpf v2 1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
Quoted 78 lines · Daniel Borkmann
Quoted 77 lines · Maciej Żenczykowski
Quoted 75 lines · Willem de Bruijn
Quoted 60 lines · Daniel Borkmann
Hi Willem,
On 4/4/25 4:23 PM, Willem de Bruijn wrote:
[...]Quoted 40 lines
v1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding--- include/linux/filter.h | 3 -- kernel/bpf/core.c | 21 ----------- net/core/filter.c | 80 +++++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 60 deletions(-)include/linux/filter.h +0
-3@@ -1496,9 +1496,6 @@ static inline u16 bpf_anc_helper(const struct sock_filter *ftest) } } -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb, -int k,unsigned int size); -static inline int bpf_tell_extensions (void ) {return SKF_AD_MAX ;kernel/bpf/core.c +0
-10@@ -68,27 +68,6 @@struct bpf_mem_alloc bpf_global_ma;bool bpf_global_ma_set; -/* No hurry in this branch -* -* Exported for the bpf jit load helper. -*/ -void *bpf_internal_load_pointer_neg_helper (const struct sk_buff *skb,int k,unsigned int size) -{ -u8 *ptr =NULL ; - -if (k >=SKF_NET_OFF ) { - ptr =skb_network_header (skb) + k -SKF_NET_OFF ;kernel/bpf/core.c line 90
Quoted 10 lines
kernel/bpf/core.c +0
-10- }else if (k >=SKF_LL_OFF ) { -if (unlikely (!skb_mac_header_was_set (skb))) -return NULL ; - ptr =skb_mac_header (skb) + k -SKF_LL_OFF ; - } -if (ptr >= skb->head && ptr + size <=skb_tail_pointer (skb)) -return ptr; - -return NULL ; -}Wouldn't this break sparc 32bit JIT which still calls into this?
arch/sparc/net/bpf_jit_asm_32.S:Quoted 11 lines
#define bpf_negative_common(LEN) \ save %sp, -SAVE_SZ, %sp; \ mov %i0, %o0; \ mov r_OFF, %o1; \ SIGN_EXTEND(%o1); \ call bpf_internal_load_pointer_neg_helper; \ mov (LEN), %o2; \ mov %o0, r_TMP; \ cmp %o0, 0; \ BE_PTR(bpf_error); \ restore;Argh, good catch. Thanks Daniel.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I'll drop the removal of
bpf_internal_load_pointer_neg_helperfrom the patch.add a 'deprecated only used by sparc32 comment'
hopefully someone that knows sparc32 assembly can fix it
Alternatively, the
bpf_internal_load_pointer_neg_helper()could be moved entirely over intoarch/sparc/net/so that others won't be tempted to reuse.
I'd prefer to keep it as is.
I took a stab, but my Debian has no sparc32 gcc cross compiler anymore, and I was unable to cross compile with clang either.
PATCH v2 2/2 selftests/net: test sk_filter support for SKF_NET_OFF on frags
In reply to Willem de BruijnFrom: Willem de Bruijn <[email protected]>
Verify that a classic BPF linux socket filter correctly matches packet contents. Including when accessing contents in an skb_frag.
1. Open a SOCK_RAW socket with a classic BPF filter on UDP dport 8000.
2. Open a tap device with IFF_NAPI_FRAGS to inject skbs with frags.
3. Send a packet for which the UDP header is in frag[0].
4. Receive this packet to demonstrate that the socket accepted it.
---
v1->v2
- add comment why early demux must be disabled
--- tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 2 + tools/testing/selftests/net/skf_net_off.c | 244 +++++++++++++++++++++ tools/testing/selftests/net/skf_net_off.sh | 30 +++ 4 files changed, 277 insertions(+) create mode 100644 tools/testing/selftests/net/skf_net_off.c create mode 100755 tools/testing/selftests/net/skf_net_off.sh
tools/testing/selftests/net/.gitignore +1 -0
@@ -39,6 +39,7 @@ scm_rights sk_bind_sendto_listen sk_connect_zero_addr sk_so_peek_off +skf_net_off socket so_incoming_cpu so_netns_cookie
tools/testing/selftests/net/Makefile +2 -0
@@ -106,6 +106,8 @@ TEST_PROGS += ipv6_route_update_soft_lockup.sh TEST_PROGS += busy_poll_test.sh TEST_GEN_PROGS += proc_net_pktgen TEST_PROGS += lwt_dst_cache_ref_loop.sh +TEST_PROGS += skf_net_off.sh +TEST_GEN_FILES += skf_net_off # YNL files, must be before "include ..lib.mk" YNL_GEN_FILES := busy_poller netlink-dumps
tools/testing/selftests/net/skf_net_off.c new file +244 -0
244 lines not on this page · raw message
tools/testing/selftests/net/skf_net_off.sh new file +30 -0
@@ -0,0 +1,30 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +readonly NS="ns-$(mktemp -u XXXXXX)" + +cleanup() { + ip netns del $NS +} + +ip netns add $NS +trap cleanup EXIT + +ip -netns $NS link set lo up +ip -netns $NS tuntap add name tap1 mode tap +ip -netns $NS link set tap1 up +ip -netns $NS link set dev tap1 addr 02:00:00:00:00:01 +ip -netns $NS -6 addr add fdab::1 peer fdab::2 dev tap1 nodad +ip netns exec $NS ethtool -K tap1 gro off + +# disable early demux, else udp_v6_early_demux pulls udp header into linear +ip netns exec $NS sysctl -w net.ipv4.ip_early_demux=0 + +echo "no filter" +ip netns exec $NS ./skf_net_off -i tap1 + +echo "filter, linear skb (-f)" +ip netns exec $NS ./skf_net_off -i tap1 -f + +echo "filter, fragmented skb (-f) (-F)" +ip netns exec $NS ./skf_net_off -i tap1 -f -F
Signature · 1 line
2.49.0.504.g3bcea36a83-goog