mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
xdp_md.data/data_end/data_meta and __sk_buff.data/data_end/data_meta are u32 in C but packet pointers to the verifier, which rewrites their load into a pointer load and prohibits 32-bit arithmetic on them. The signedness rules ranked them as u32, so ctx.data - k lowered to a 32-bit add (w0 += -1) and was rejected; C code avoids this with a cast through (void *)(long). A binary operation with a packet-pointer operand now bypasses the usual arithmetic conversions: the pointer is used at 64 bits, an offset added to or subtracted from it is taken as an unsigned 16-bit value (truncated, zero-extended) because the verifier only tracks packet ranges up to MAX_PACKET_OFF (0xffff), and the result is a 64-bit value. Pointer minus pointer is an ordinary 64-bit length. A constant offset outside 0..65535, any other operator, and offset minus pointer are compile errors rather than code the verifier would reject. Local inference agrees (64-bit unsigned). Only direct field reads are recognised; a local copied from one is an integer again, and comparisons between packet pointers are the next site of the same rule. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
25 lines
545 B
Python
25 lines
545 B
Python
# A runtime offset added to a packet pointer is taken as an unsigned 16-bit
|
|
# value (masked to 0xffff, zero-extended), so the verifier can bound the
|
|
# packet range; the addition itself is 64-bit.
|
|
from ctypes import c_int64
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from pythonbpf.helper import random
|
|
from vmlinux import struct_xdp_md
|
|
|
|
|
|
@bpf
|
|
@section("xdp")
|
|
def prog(ctx: struct_xdp_md) -> c_int64:
|
|
off = random()
|
|
d = ctx.data + off
|
|
return c_int64(d)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|