Skip to content
systemcoding.dev
Networking

Fix 'invalid access to packet' Verifier Errors in XDP

Decode the eBPF verifier message invalid access to packet in an XDP program, read the off, size and r values, and add the missing data_end bounds check.

7 min read
Summarize in AI
Flat illustration of network packets crossing between two hosts, with the C, Linux and Wireshark logos.

You compile an XDP program, run the loader, and the kernel answers with a wall of text that ends in invalid access to packet. The program is rejected before it ever sees a frame, and nothing about the message mentions your source line. This post starts from that literal error, explains what each number means, and walks through the four mistakes that cause it, with a fixed version of each. It uses Linux 6.12, clang 18 and libbpf 1.5. The cause is nearly always the same: the verifier cannot prove that a read stays inside the packet.

The error text

When a load fails, libbpf prints the verifier log. The relevant lines look like this:

Text
libbpf: prog 'parse_udp': BPF program load failed: Permission denied
libbpf: prog 'parse_udp': -- BEGIN PROG LOAD LOG --
0: R1=ctx() R10=fp0
; void *data_end = (void *)(long)ctx->data_end; @ udp.bpf.c:9
0: (61) r2 = *(u32 *)(r1 +4)          ; R1=ctx() R2_w=pkt_end()
; void *data = (void *)(long)ctx->data; @ udp.bpf.c:8
1: (61) r1 = *(u32 *)(r1 +0)          ; R1_w=pkt(r=0)
; return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS; @ udp.bpf.c:21
2: (69) r1 = *(u16 *)(r1 +36)
invalid access to packet, off=36 size=2, R1(id=0,off=36,r=0)
R1 offset is outside of the packet
processed 3 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --

The line to read is the one that starts with invalid access to packet. The exact surrounding text differs between kernel versions, and the register annotations changed format around Linux 6.x, but the core message has been stable for years. It is defined in the verifier source, kernel/bpf/verifier.c, in the function that checks packet access.

Reading off, size and r

The fields describe one memory access the program attempted:

  • off=36: the offset, in bytes from the start of the packet, where the read begins.

  • size=2: how many bytes the instruction reads.

  • R1(id=0,off=36,r=0): the register holding the pointer. Its own fixed offset is 36, and r=0 is the verified range: the number of bytes from the start of the packet that the verifier has proven are inside data_end.

The access is valid only if off + size <= r. Here 36 + 2 = 38 is greater than 0, so the verifier refuses. A read at offset 36 of size 2 is the UDP destination port in an untagged IPv4 packet with a 20-byte IP header: 14 bytes of Ethernet plus 20 of IP plus 2 for the source port. The log has pointed at the exact field, and the fix is to establish a range that covers it before the read.

The r value is how you debug the rest. After a correct if ((void *)(udp + 1) > data_end) return XDP_PASS; the verifier raises r to 42 on the fall-through path, which covers offsets up to the end of the UDP header.

Cause 1: no bounds check at all

The simplest version, which produced the log above:

bad_nocheck.bpf.cc
SEC("xdp")
int parse_udp(struct xdp_md *ctx)
{
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;

    struct ethhdr *eth = data;
    struct iphdr *ip = (void *)(eth + 1);
    struct udphdr *udp = (void *)(ip + 1);

    return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS;
}

data_end is computed but never compared with anything. The fix compares each header's end with data_end before using it:

fix_nocheck.bpf.cc
SEC("xdp")
int parse_udp(struct xdp_md *ctx)
{
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;

    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return XDP_PASS;

    struct iphdr *ip = (void *)(eth + 1);
    if ((void *)(ip + 1) > data_end)
        return XDP_PASS;

    struct udphdr *udp = (void *)(ip + 1);
    if ((void *)(udp + 1) > data_end)
        return XDP_PASS;

    return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS;
}

This version loads. It assumes a 20-byte IP header, which a later cause revisits.

Cause 2: checking the wrong pointer

A check against a pointer that is not the one you dereference proves nothing about the other. This variant checks ip but then reads through udp, which the verifier tracks as a separate range:

bad_wrongptr.bpf.cc
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
    return XDP_PASS;

struct udphdr *udp = (void *)(ip + 1);
return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS;   /* UDP header unchecked */

The check covered the IP header, so r reaches 34, and the read at offset 36 of size 2 exceeds it, giving invalid access to packet, off=36 size=2, R1(id=0,off=36,r=34). The giveaway is that r is non-zero but too small. Add a check that covers the UDP header, as in the fixed program above.

Cause 3: a variable offset the verifier cannot bound

IPv4 header length is variable, so the UDP header position is computed from ihl. If the compiler or the code lets the verifier lose track of the bound, the message changes:

bad_varoff.bpf.cc
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
    return XDP_PASS;

struct udphdr *udp = (void *)ip + ip->ihl * 4;
return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS;

This fails because the pointer arithmetic result is never compared with data_end. The fix has two parts: establish bounds on the computed value, and check the final pointer.

fix_varoff.bpf.cc
struct iphdr *ip = (void *)(eth + 1);
if ((void *)(ip + 1) > data_end)
    return XDP_PASS;

__u32 ip_len = ip->ihl * 4;
if (ip_len < sizeof(*ip))
    return XDP_PASS;

struct udphdr *udp = (void *)ip + ip_len;
if ((void *)(udp + 1) > data_end)
    return XDP_PASS;

return udp->dest == bpf_htons(9999) ? XDP_DROP : XDP_PASS;

ip_len is bounded below by the explicit comparison and above by the four-bit width of ihl, so the verifier knows ip_len is between 20 and 60 and can track the pointer. If a variant of this fails with math between pkt pointer and register with unbounded min value is not allowed, the offset lacks a lower bound; clamp it with an explicit comparison.

Cause 4: the compiler moved the check

Sometimes the source has the check but the compiled code does not use the checked pointer. Optimization can reorder or recompute the address so the verifier sees an access through a pointer different from the one it compared. Symptoms are a log where the check clearly exists in source and still r is smaller than expected. Two things help. Use the same pointer variable for the check and the access, rather than recomputing (void *)ip + 20 in both places. And, if the compiler splits the logic across a function call, mark small helper functions static __always_inline, because the verifier checks each non-inlined function separately in its own context and a bounds proof does not carry across the call boundary in the way you might assume.

helper.bpf.cc
static __always_inline struct udphdr *parse_udp(void *data, void *data_end)
{
    struct ethhdr *eth = data;
    if ((void *)(eth + 1) > data_end)
        return NULL;
    if (eth->h_proto != bpf_htons(ETH_P_IP))
        return NULL;

    struct iphdr *ip = (void *)(eth + 1);
    if ((void *)(ip + 1) > data_end)
        return NULL;
    if (ip->protocol != IPPROTO_UDP)
        return NULL;

    struct udphdr *udp = (void *)(ip + 1);
    if ((void *)(udp + 1) > data_end)
        return NULL;
    return udp;
}

The caller then tests the returned pointer for null before using it, just as with a map lookup.

Getting more detail from the log

If the default log is truncated, load with a bigger buffer and a higher log level. In libbpf, set .kernel_log_level = 2 in bpf_object_open_opts, or use bpftool -d prog load to print the debug log. The annotated source lines (; ... @ file.c:21) appear only when the object was compiled with -g, so keep it on. Read the log from the failing instruction upward and look for the last place the pointer's r was raised.

A habit that prevents the error

Most of these failures disappear if you write parsers in one fixed shape. Declare data and data_end at the top. For each header, in order, take the pointer from the end of the previous header, test the new header's end against data_end immediately, and only then read from it. Never compute a pointer, test it later and read through a differently computed one. Return early on failure so the success path stays unindented and the verifier's range only ever grows along it.

Keep the checks adjacent to the pointers they protect, and prefer a small inline parsing helper per protocol over one long function. A long function with many branches also risks hitting the verifier's complexity limit, which produces a different error about the number of instructions processed. Splitting the work into static __always_inline helpers keeps each proof short and obvious, and it makes the log easier to read when something does go wrong.

When none of this explains it

If the access is inside a loop, the verifier may be pruning states and losing a bound that held on every iteration. Restructure the loop to a fixed count with the check inside it, or use bpf_loop where available. If the access happens in a function called with a pointer argument, pass the end pointer too and check inside the callee.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.