Skip to content

Lesson 4 of 13 · 14m

eBPF Is a Verifier, Not Magic

What the verifier actually proves, why CO-RE survives kernel upgrades, and the per-hit cost nobody budgets for.

eBPF gets described as "safe, zero-overhead dynamic instrumentation," and two of those three words need asterisks. Here is what actually happens when you attach a probe: your BPF program is loaded via the bpf() syscall, the verifier walks every possible execution path of it, a JIT compiles it to native code, and it's attached to a kprobe, tracepoint, uprobe, or perf event. On every hit, the kernel runs your code, and results reach userspace through a ring buffer or a map. The "safety" is the verifier; the overhead is per-hit and entirely measurable.

The verifier is the price of admission

The verifier exists so the kernel can run your program without trusting you. It rejects anything it can't prove safe: unbounded loops (bounded for loops were only allowed starting in kernel 5.3), more than about a million instructions, more than 512 bytes of stack, calls to helpers not allowlisted for your program type, pointer arithmetic it can't bound. This is why BPF C reads like someone removed the fun parts — the compiler is not being petty, it's proving termination and memory safety to a skeptical theorem-checker.

The verifier itself is code, and code has bugs. CVE-2021-3490 was a bounds-tracking error in ALU32 operations that let a local user escalate privileges through a crafted BPF program; the fix landed in stable kernels in June 2021. Keep kernel.unprivileged_bpf_disabled=1 — the default on Debian and Ubuntu — and since kernel 5.8 you can grant CAP_BPF plus CAP_PERFMON instead of handing out CAP_SYS_ADMIN.

CO-RE is why your binary survives kernel upgrades

Kernel structs change between versions; a BPF program compiled against 5.15's task_struct layout reads garbage on 6.5. BTF (BPF Type Format) solves this: the kernel publishes its own type metadata at /sys/kernel/btf/vmlinux, and libbpf relocates field offsets at load time. That's Compile Once — Run Everywhere, and it's why modern eBPF tools ship one binary instead of a compiler toolchain per kernel. Check CONFIG_DEBUG_INFO_BTF — effectively test -f /sys/kernel/btf/vmlinux — before assuming a CO-RE tool will run.

Per-hit cost, honestly accounted

There is no zero overhead. A kprobe or tracepoint hit costs the probe dispatch plus your program's instructions — hundreds of nanoseconds to a few microseconds depending on what you emit. Multiply by hit rate: a probe on vfs_read or malloc-adjacent hot paths sees millions of hits per second and will visibly tax the workload. A probe on a once-per-request event is a rounding error. Measure the hit rate first — funccount (BCC) or a counting bpftrace one-liner — before attaching anything expensive:

bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args.filename)); }'

Prefer tracepoints over kprobes where both exist: tracepoints are a stable kernel ABI with named arguments (args.filename), while kprobes attach to internal functions whose names and signatures drift — do_sys_open became do_sys_openat2 — breaking probes across kernel upgrades.

The evidence it works

Meta's Katran — its layer-4 load balancer, open-sourced in May 2018 — runs on XDP/eBPF at facebook.com's edge. Cilium graduated from the CNCF in October 2023 on the same machinery. Pixie auto-instruments Kubernetes services with eBPF and no code changes. None of these claim the instrumentation is free; they claim it's cheaper than the alternatives, and they publish the overhead numbers to prove it.

Practice

  • Confirm /sys/kernel/btf/vmlinux exists before deploying CO-RE tools.
  • Keep kernel.unprivileged_bpf_disabled=1; grant CAP_BPF/CAP_PERFMON, not root.
  • Measure hit rate first; prefer tracepoints over kprobes; bound your map sizes.
  • Prefer BPF_MAP_TYPE_RINGBUF (kernel 5.8+) over the older perf buffer for event streams.

eBPF's real claim isn't "free." It's that the kernel is already sitting on the data, and the verifier lets you read it without a rootkit.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.