iovisor / iovisor/bcc

BPF_SNPRINTF is not working

Open
#4,057 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
22.7k
Forks
4.1k
Avg merge
10d 4h
Merged PRs (30d)
3

Description

When I use BPF_SNPRINTF on my xdp.c, it reports the following error. If I don’t use BPF_SNPRINTF, the error is gone. Any hints will be appreaciated:
```

go run -exec sudo ./pkg ens160
2022/06/17 05:04:56 loading objects: field XdpProgFunc: program xdp_prog_func: load program: permission denied: ; int xdp_prog_func(struct xdp_md *ctx)
0: (b7) r0 = 0
; void *data_end = (void *)(long)ctx->data_end;
1: (61) r9 = *(u32 *)(r1 +4)
; void *data = (void *)(long)ctx->data;
2: (61) r6 = *(u32 *)(r1 +0)
; if (data + sizeof(struct ethhdr) > data_end)
3: (bf) r1 = r6
4: (07) r1 += 14
; if (data + sizeof(struct ethhdr) > data_end)
5: (2d) if r1 > r9 goto pc+693
R0_w=inv0 R1_w=pkt(id=0,off=14,r=14,imm=0) R6_w=pkt(id=0,off=0,r=14,imm=0) R9_w=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (ethh->h_proto != bpf_htons(ETH_P_IP))
6: (71) r1 = *(u8 *)(r6 +12)
7: (71) r2 = *(u8 *)(r6 +13)
8: (67) r2 <<= 8
9: (4f) r2 |= r1
10: (b7) r0 = 2
; if (ethh->h_proto != bpf_htons(ETH_P_IP))
11: (55) if r2 != 0x8 goto pc+687
R0_w=inv2 R1_w=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R2_w=inv8 R6_w=pkt(id=0,off=0,r=14,imm=0) R9_w=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end)
12: (bf) r7 = r6
13: (07) r7 += 34
14: (b7) r0 = 0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) > data_end)
15: (2d) if r7 > r9 goto pc+683
R0=inv0 R1=inv(id=0,umax_value=255,var_off=(0x0; 0xff)) R2=inv8 R6=pkt(id=0,off=0,r=34,imm=0) R7=pkt(id=0,off=34,r=34,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (iph->protocol != IPPROTO_TCP)
16: (71) r1 = *(u8 *)(r6 +23)
17: (b7) r0 = 2
; if (iph->protocol != IPPROTO_TCP)
18: (55) if r1 != 0x6 goto pc+680
R0_w=inv2 R1_w=inv6 R2=inv8 R6=pkt(id=0,off=0,r=34,imm=0) R7=pkt(id=0,off=34,r=34,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr) > data_end)
19: (bf) r1 = r6
20: (07) r1 += 54
21: (b7) r0 = 0
; if (data + sizeof(struct ethhdr) + sizeof(struct iphdr) + sizeof(struct tcphdr) > data_end)
22: (2d) if r1 > r9 goto pc+676
R0_w=inv0 R1_w=pkt(id=0,off=54,r=54,imm=0) R2=inv8 R6=pkt(id=0,off=0,r=54,imm=0) R7=pkt(id=0,off=34,r=54,imm=0) R9=pkt_end(id=0,off=0,imm=0) R10=fp0
23: (b7) r1 = 0
; char ip_str[18] = {};
24: (6b) *(u16 *)(r10 -8) = r1
last_idx 24 first_idx 15
regs=2 stack=0 before 23: (b7) r1 = 0
25: (7b) *(u64 *)(r10 -16) = r1
26: (7b) *(u64 *)(r10 -24) = r1
27: (bf) r1 = r10
28: (07) r1 += -24
29: (bf) r4 = r10
30: (07) r4 += -40
; BPF_SNPRINTF(ip_str, sizeof(ip_str), "hello");
31: (b7) r2 = 18
32: (18) r3 = 0xffff8a264ae74923
34: (b7) r5 = 0
35: (85) call bpf_snprintf#165
last_idx 35 first_idx 15
regs=4 stack=0 before 34: (b7) r5 = 0
regs=4 stack=0 before 32: (18) r3 = 0xffff8a264ae74923
regs=4 stack=0 before 31: (b7) r2 = 18
R3 does not point to a readonly map'
processed 35 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 1
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with `go run -exec sudo ./pkg ens160` and inspect the verifier output around the `BPF_SNPRINTF` call in `xdp.c`, especially instruction 32. Compare the call’s arguments with the verifier’s readonly-map requirement and confirm a minimal working invocation; done means the XDP program loads successfully or the incompatibility is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, go, linux
Domain
devtools, networking, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.