bpf: Fix verifier WARN on narrow read of bpf_tcp_sock u64 fields
Subsystems (6)
PATCH v2 1/2 bpf: Fix verifier WARN on narrow read of bpf_tcp_sock u64 fields
A BPF program can read the middle of a u64 field in struct bpf_tcp_sock:
tp = bpf_tcp_sock(listener); /* +0x54 is in the middle of the u64 bytes_received */ unsigned hi = *(unsigned *)((char *)tp + 0x54);
bpf_tcp_sock_is_valid_access() allows this 4-byte read (the default case only checks size == 4), but bpf_tcp_sock_convert_ctx_access() only knows the field starts and has no idea how to handle such an offset. It emits no instruction, cnt stays 0 and the fixup pass trips verifier_bug():
verifier bug: error during ctx access conversion (0)
Just disallow the narrower access by matching the whole u64 range of the two fields, so it is turned down at check time:
invalid tcp_sock access off=84 size=4
---
v1 -> v2: Add a selftest (Alexei).
v1: https://lore.kernel.org/bpf/[email protected]/T/#t
--- net/core/filter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/filter.c +2 -2
@@ -7600,8 +7600,8 @@ bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,return false;switch (off) { -case offsetof(struct bpf_tcp_sock , bytes_received): -case offsetof(struct bpf_tcp_sock , bytes_acked): + case bpf_ctx_range(struct bpf_tcp_sock, bytes_received): + case bpf_ctx_range(struct bpf_tcp_sock, bytes_acked):return size ==sizeof (__u64);default :return size ==sizeof (__u32);
Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/bpf: Test narrow read of bpf_tcp_sock u64 fields
Add a test that reads the two u64 fields bytes_received and bytes_acked at offset + 4 with a u32 load, to make sure such a narrower access is rejected with "invalid tcp_sock access".
--- .../selftests/bpf/progs/verifier_sock.c | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+)
tools/testing/selftests/bpf/progs/verifier_sock.c +48 -0
@@ -611,6 +611,54 @@ l1_%=: r0 = *(u64*)(r0 + %[bpf_tcp_sock_bytes_acked__end]);\ : __clobber_all); } +SEC("cgroup/skb") +__description("bpf_tcp_sock(skb->sk): narrow load of tp->bytes_received") +__failure __msg("invalid tcp_sock access") +__failure_unpriv +__naked void skb_sk_tp_bytes_received_narrow (void ) +{ + asmvolatile (" \ +r1 = *(u64*)(r1 + %[__sk_buff_sk]); \ +if r1 != 0 goto l0_%=; \ +r0 = 0; \ +exit; \ +l0_%=: call %[bpf_tcp_sock]; \ +if r0 != 0 goto l1_%=; \ +exit; \ +l1_%=: r0 = *(u32*)(r0 + %[off]); \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_tcp_sock), +__imm_const (__sk_buff_sk, offsetof(struct __sk_buff , sk)), +__imm_const (off, offsetof(struct bpf_tcp_sock , bytes_received) +4 ) + : __clobber_all); +} + +SEC("cgroup/skb") +__description("bpf_tcp_sock(skb->sk): narrow load of tp->bytes_acked") +__failure __msg("invalid tcp_sock access") +__failure_unpriv +__naked void skb_sk_tp_bytes_acked_narrow (void ) +{ + asmvolatile (" \ +r1 = *(u64*)(r1 + %[__sk_buff_sk]); \ +if r1 != 0 goto l0_%=; \ +r0 = 0; \ +exit; \ +l0_%=: call %[bpf_tcp_sock]; \ +if r0 != 0 goto l1_%=; \ +exit; \ +l1_%=: r0 = *(u32*)(r0 + %[off]); \ +r0 = 0; \ +exit; \ +" : + : __imm(bpf_tcp_sock), +__imm_const (__sk_buff_sk, offsetof(struct __sk_buff , sk)), +__imm_const (off, offsetof(struct bpf_tcp_sock , bytes_acked) +4 ) + : __clobber_all); +} + SEC("cgroup/skb") __description("bpf_tcp_sock(bpf_sk_fullsock(skb->sk)): tp->snd_cwnd") __success __success_unpriv __retval(0)
Signature · 1 line
2.43.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 6 lines · On Sat, 17 Oct 2026 22:38:12 +0800 you wrote:
A BPF program can read the middle of a u64 field in struct
bpf_tcp_sock:tp = bpf_tcp_sock(listener); /* +0x54 is in the middle of the u64 bytes_received */ unsigned hi = *(unsigned *)((char *)tp + 0x54);
Quoted 6 lines
bpf_tcp_sock_is_valid_access()allows this 4-byte read (the default case only checks size == 4), butbpf_tcp_sock_convert_ctx_access()only knows the field starts and has no idea how to handle such an offset. It emits no instruction, cnt stays 0 and the fixup pass tripsverifier_bug():[...]
Here is the summary with links:
- [bpf,v2,1/2] bpf: Fix verifier WARN on narrow read of bpf_tcp_sock u64 fields
https://git.kernel.org/bpf/bpf-next/c/af873495cbcb - [bpf,v2,2/2] selftests/bpf: Test narrow read of bpf_tcp_sock u64 fields https://git.kernel.org/bpf/bpf-next/c/5256161e3573
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html