support SKF_NET_OFF and SKF_LL_OFF on skb frags
Subsystems (5)
v3 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.
v2->v3
- do not remove bpf_internal_load_pointer_neg_helper, because it is still used in the sparc32 JIT
v1->v2
- introduce bfp_skb_load_helper_convert_offset to avoid open coding
- selftest: add comment why early demux must be disabled
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
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
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 +++ 5 files changed, 321 insertions(+), 36 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 v3 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.
---
v2->v3
- do not remove bpf_internal_load_pointer_neg_helper, because it is still used in the sparc32 JITv1->v2
- introduce bfp_skb_load_helper_convert_offset to avoid open coding
--- net/core/filter.c | 80 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-)
net/core/filter.c +44 -36
@@ -218,24 +218,36 @@ BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)return 0 ; } +static int bpf_skb_load_helper_convert_offset (const struct sk_buff *skb,int offset) +{ +if (likely (offset >=0 )) +return offset; + +if (offset >=SKF_NET_OFF ) +return offset -SKF_NET_OFF +skb_network_offset (skb); + +if (offset >=SKF_LL_OFF &&skb_mac_header_was_set (skb)) +return offset -SKF_LL_OFF +skb_mac_offset (skb); + +return INT_MIN ; +} + BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *, data, int, headlen, int, offset) { -u8 tmp, *ptr; +u8 tmp;const int len =sizeof (tmp); -if (offset >=0 ) { -if (headlen - offset >= len) -return *(u8 *)(data + offset); -if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) -return tmp; - }else { - ptr =bpf_internal_load_pointer_neg_helper (skb, offset, len); -if (likely (ptr)) -return *(u8 *)ptr; - } + offset =bpf_skb_load_helper_convert_offset (skb, offset); +if (offset ==INT_MIN ) +return -EFAULT ; -return -EFAULT ; +if (headlen - offset >= len) +return *(u8 *)(data + offset); +if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) +return tmp; +else +return -EFAULT ; } BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
@@ -248,21 +260,19 @@ BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb, BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *, data, int, headlen, int, offset) { -__be16 tmp, *ptr; +__be16 tmp;const int len =sizeof (tmp); -if (offset >=0 ) { -if (headlen - offset >= len) -return get_unaligned_be16 (data + offset); -if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) -return be16_to_cpu (tmp); - }else { - ptr =bpf_internal_load_pointer_neg_helper (skb, offset, len); -if (likely (ptr)) -return get_unaligned_be16 (ptr); - } + offset =bpf_skb_load_helper_convert_offset (skb, offset); +if (offset ==INT_MIN ) +return -EFAULT ; -return -EFAULT ; +if (headlen - offset >= len) +return get_unaligned_be16 (data + offset); +if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) +return be16_to_cpu (tmp); +else +return -EFAULT ; } BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
@@ -275,21 +285,19 @@ BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb, BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *, data, int, headlen, int, offset) { -__be32 tmp, *ptr; +__be32 tmp;const int len =sizeof (tmp); -if (likely (offset >=0 )) { -if (headlen - offset >= len) -return get_unaligned_be32 (data + offset); -if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) -return be32_to_cpu (tmp); - }else { - ptr =bpf_internal_load_pointer_neg_helper (skb, offset, len); -if (likely (ptr)) -return get_unaligned_be32 (ptr); - } + offset =bpf_skb_load_helper_convert_offset (skb, offset); +if (offset ==INT_MIN ) +return -EFAULT ; -return -EFAULT ; +if (headlen - offset >= len) +return get_unaligned_be32 (data + offset); +if (!skb_copy_bits (skb, offset, &tmp,sizeof (tmp))) +return be32_to_cpu (tmp); +else +return -EFAULT ; } BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Re: [PATCH bpf v3 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 7 lines
---
v2->v3
- do not removebpf_internal_load_pointer_neg_helper, because it is still used in the sparc32 JITv1->v2
- introducebfp_skb_load_helper_convert_offsetto avoid open coding
PATCH v3 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
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Tue, 8 Apr 2025 09:27:47 -0400 you wrote:
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.[...]
Here is the summary with links:
- [bpf,v3,1/2] bpf: support SKF_NET_OFF and SKF_LL_OFF on skb frags
https://git.kernel.org/bpf/bpf/c/d4bac0288a2b - [bpf,v3,2/2] selftests/net: test sk_filter support for SKF_NET_OFF on frags https://git.kernel.org/bpf/bpf/c/fcd7132cb1f9
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html