Skip to content

Commit 54cea3e

Browse files
Stringyclaude
andcommitted
Fix BPF verifier rejection in push__bytebuf on older kernels
Widen len_to_read to unsigned long and mask with & 0xFFFF before bpf_probe_read calls. Older verifiers lose uint16_t bounds through 32-bit register moves. Reverts per-file snaplen masks as they addressed the wrong level. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0aacbba commit 54cea3e

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

driver/modern_bpf/helpers/base/push_data.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,22 @@ static __always_inline uint16_t push__bytebuf(uint8_t *data,
264264
unsigned long bytebuf_pointer,
265265
uint16_t len_to_read,
266266
enum read_memory mem) {
267+
/* Older BPF verifiers (RHEL 8 4.18, COS 6.6) lose uint16_t bounds
268+
* through 32-bit register moves (w2 = w8), causing "R2 unbounded
269+
* memory access". Widen to unsigned long and mask to force clang
270+
* to emit an AND instruction the verifier can track.
271+
*/
272+
unsigned long safe_len = len_to_read;
273+
safe_len &= 0xFFFF;
267274
if(mem == KERNEL) {
268275
if(bpf_probe_read_kernel(&data[SAFE_ACCESS(*payload_pos)],
269-
len_to_read,
276+
safe_len,
270277
(void *)bytebuf_pointer) != 0) {
271278
return 0;
272279
}
273280
} else {
274281
if(bpf_probe_read_user(&data[SAFE_ACCESS(*payload_pos)],
275-
len_to_read,
282+
safe_len,
276283
(void *)bytebuf_pointer) != 0) {
277284
return 0;
278285
}

driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvfrom.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ int BPF_PROG(recvfrom_x, struct pt_regs *regs, long ret) {
4242
uint16_t snaplen = maps__get_snaplen();
4343
apply_dynamic_snaplen(regs, &snaplen, &snaplen_args);
4444
if(snaplen > ret) {
45-
snaplen = ret & 0xFFFF;
45+
snaplen = ret;
4646
}
4747

4848
/* Parameter 2: data (type: PT_BYTEBUF) */

driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmsg.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int BPF_PROG(recvmsg_x, struct pt_regs *regs, long ret) {
4646
uint16_t snaplen = maps__get_snaplen();
4747
apply_dynamic_snaplen(regs, &snaplen, &snaplen_args);
4848
if(snaplen > ret) {
49-
snaplen = ret & 0xFFFF;
49+
snaplen = ret;
5050
}
5151

5252
/* Parameter 3: data (type: PT_BYTEBUF) */

driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/sendmsg.bpf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int BPF_PROG(sendmsg_x, struct pt_regs *regs, long ret) {
5959
};
6060
apply_dynamic_snaplen(regs, &snaplen, &snaplen_args);
6161
if(ret > 0 && snaplen > ret) {
62-
snaplen = ret & 0xFFFF;
62+
snaplen = ret;
6363
}
6464

6565
unsigned long iov_pointer = (unsigned long)msghdr.msg_iov;

0 commit comments

Comments
 (0)