Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

What is eBPF? Programs, hooks and the verifier explained

By updated

Improve this page

eBPF lets you run your own programs inside the Linux kernel, triggered by events, without recompiling the kernel or loading a module. It is a virtual machine built into the kernel: you give it a small program, it checks that the program is safe, then runs it every time the chosen event occurs. This page explains the complete mental model, without code: what an eBPF program is, where it attaches, how it communicates with user space, and why the verifier is the piece that makes all this acceptable.

What you will learn

  • Define eBPF without reducing it to networking
  • Locate where an eBPF program runs and what triggers it
  • Choose the program type that matches an administrator's need
  • Understand the role of the verifier, of maps and of JIT compilation
  • Distinguish eBPF from a kernel module and from a classic agent

Where does the name come from?

eBPF stands for extended Berkeley Packet Filter. The original BPF, born in 1992, was a mini-language for filtering network packets: it is what, behind tcpdump, decides which packets to capture without sending each one up to user space. The idea was already there: run a small program in the kernel, as close as possible to the event.

Around 2014, this mechanism was generalized. The language became richer, the virtual machine more complete, and above all the trigger points opened up far beyond networking: system calls, kernel functions, performance events. This extended version, eBPF, changed everything. The "e" prefix is now often dropped: "BPF" is used for the modern technology.

What problem does eBPF solve?

To understand why eBPF has taken such a place, you need to see the dilemma it settles. Before it, observing or modifying the behavior of the kernel in fine detail forced a choice between two bad options.

Writing a kernel module gives all the powers, but a bug crashes the whole machine (kernel panic), and a badly written module is an entry point for an attacker. Going through a user-space agent is safe, but slow and partly blind: every piece of information must cross the boundary between the kernel and user space, which is expensive, and many events are not visible from outside.

eBPF offers a third way: the power of the kernel with the safety of a sandbox. The program runs in the kernel, in contact with the event, but it is proven harmless before being loaded. This combination did not exist before, and the following table sets it against the two other approaches.

ApproachPowerSafetyCost
Kernel moduletotala bug crashes the systemrecompilation, high risk
User-space agentlimited, partial viewgoodexpensive boundary crossings
eBPFclose to the kernelverified before loadinglow, in-kernel execution

How does an eBPF program work?

The life cycle of an eBPF program fits in four steps. Understanding them is understanding eBPF.

1. You write a program and compile it

The program is usually written in a restricted C, then compiled not into ordinary machine code but into eBPF bytecode: an instruction set specific to the kernel's virtual machine. This bytecode is the format the kernel knows how to verify and run.

You will not always write this C yourself. Tools such as bpftrace let you describe what you want to trace in a much simpler language, and take care of producing the bytecode. But underneath, it is still an eBPF program.

2. The verifier proves that it is safe

This is the key piece, and what distinguishes eBPF from everything that came before. Before loading the program, the kernel hands it to a verifier. This component analyzes all the possible execution paths and rejects the program if it cannot prove that it is safe.

The verifier notably guarantees that the program always terminates (no infinite loop that would freeze the kernel), that it only accesses authorized memory (no wild reads or writes), and that it does not leak sensitive kernel information. A program that fails this proof is never loaded.

3. The kernel compiles it to native code and attaches it to a hook

Once verified, the kernel translates the bytecode into native machine code thanks to a JIT (Just-In-Time) compiler. The program then runs at kernel speed, not at the speed of an interpreter.

The program is then attached to a trigger point, a hook. The hook is the place and the moment where your code will run: a kernel function, an observation point, the arrival of a packet, a security decision. The section "Which types of eBPF programs exist?" below details the families of hooks and their use.

4. It communicates with user space through maps

An eBPF program does not live in isolation: it must pass on what it observes to a user-space program that will display or use it. This bridge is the maps: data structures (hash tables, arrays, queues) shared between the kernel program and user space.

The eBPF program writes into the map (for example, it increments a system call counter per process), and the user-space tool reads the map to present the result. Maps are also the way to configure a program from outside while it runs. The lesson on maps is devoted to them.

Which types of eBPF programs exist?

The type of an eBPF program determines where it can attach and what it is allowed to do. For an administrator, seven families are enough to find one's way: each answers a different operations question, and the type name is displayed directly in the output of bpftool prog show.

TypeAttaches toWhat an administrator does with it
kprobeEntry or exit of any kernel functionDiagnose kernel behavior (disk reads, system calls); unstable, function names change
uprobeA function of a program in user spaceObserve an application or a library without its source code
tracepointA stable observation point provided by the kernelTrace reliably from one version to the next (process launch, file opening)
XDPThe very start of packet processing, at the network card levelFilter or drop a packet flow before it costs CPU (protection against denial of service)
TC (traffic control)Traffic control, on the way in or out of an interfaceClassify, limit or redirect traffic; this is where part of Cilium attaches
cgroupA control group (cgroup) of processesFilter the network or device access of a service; this is what systemd sets for its units
LSM (Linux Security Module)The security decision points of the kernelAllow or deny an action, for example the loading of another eBPF program

The table groups the types by purpose. The first three rows serve to observe without changing anything, which is the field of bpftrace. The XDP, TC and cgroup rows serve to act on the network and on the resources of a group of processes. The LSM row serves to decide whether an action is allowed, which makes it the tool of advanced hardening, covered in the lesson on hardening eBPF.

What can eBPF be used for?

The same mechanism, grafting safe code onto a kernel event, comes in three main uses that structure the module.

Observe. Count the system calls of a process, measure the latency of a function, trace the opened files, all without modifying the application or installing an agent in the container. This is the field of bpftrace and of the BCC tools (see BCC and libbpf), covered in this module, and of Inspektor Gadget for Kubernetes (not yet translated).

Route and filter the network. Process packets as early as possible, decide routing by identity rather than by IP address, apply network policies up to the application level. This is what Cilium does by replacing iptables with eBPF in Kubernetes (not yet translated).

Secure at runtime. Detect in real time that a process opens a suspicious shell or reads a sensitive file, and, with Tetragon, block the action directly in the kernel before it completes. The field of Falco and of Tracee is detection (Tracee and Tetragon are not yet translated).

Key takeaways

  • eBPF runs your programs in the Linux kernel, triggered by events, without a module or recompilation.
  • It is not only a networking tool: system calls, functions, packets, security decisions, almost anything is a possible attachment point.
  • The verifier proves that a program is safe before loading it: no infinite loop, no forbidden memory access.
  • The type of a program fixes its attachment: kprobe, uprobe and tracepoint to observe, XDP, TC and cgroup for the network and resources, LSM to decide.
  • Maps link the kernel program to user space: they carry the observed data and the configuration.
  • eBPF serves to observe, route the network and secure at runtime, but remains a sensitive privilege.

Knowledge check

Check that the essentials of this lesson are in place. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer