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

Harden eBPF: limit who can load a program

By updated

Improve this page

Loading an eBPF program is a powerful privilege: hardening consists in deciding who may do it, limiting the damage in case of compromise, and monitoring what runs. eBPF is a strength for security, but also an attack surface in its own right. This page gives the concrete settings to reduce it on a server, with the real values verified on Ubuntu 24.04 (kernel 6.8) and AlmaLinux 10.2 VMs, anchored in the kernel documentation. Audience: administrator who operates Linux servers, with or without eBPF tools deployed.

What you will learn

  • Forbid unprivileged eBPF, and understand its three levels
  • Distinguish when the CAP_BPF capability can replace root (agents designed for it) and why bpftrace and BCC remain root tools
  • Harden the JIT compiler against a class of attacks
  • Audit the eBPF programs actually loaded on a machine
  • Situate BPF LSM as an advanced control

Prerequisites

This lesson takes up, on the eBPF side, settings that the lesson Hardening a server with sysctl sets for the whole kernel: read it first, it explains how to make a parameter persistent and why they are applied together. The capabilities (CAP_BPF, CAP_PERFMON) also assume the lesson capabilities, and the lesson the attack surface of eBPF gives the reason for each setting.

Why is eBPF a privilege to control?

An eBPF program runs in the kernel and can observe, even modify, the behavior of the system. Loading it is therefore a sensitive operation. Hardening eBPF does not mean depriving yourself of it: it means applying least privilege to this subsystem, exactly as you would for sudo or an exposed service. Three axes: who can load, with which powers, and how it is monitored.

Lever 1: how do you forbid unprivileged eBPF?

The first setting controls whether an unprivileged user can call bpf(). It is driven by the sysctl kernel.unprivileged_bpf_disabled. The kernel documentation defines three values, summarized in this table:

ValueMeaning (kernel doc)
0Unprivileged calls to bpf() allowed
1Unprivileged calls disabled, with no way back until the next reboot
2Unprivileged calls disabled, but reversible

On the test VM, real value:

Terminal window
sysctl kernel.unprivileged_bpf_disabled
kernel.unprivileged_bpf_disabled = 2

This is the default of recent distributions, measured in the lab on AlmaLinux 10.2 as on Ubuntu 24.04: unprivileged eBPF is disabled. Leaving it that way closes the door to a whole category of exploits, because several historical flaws were exploitable only by an unprivileged caller. The lab verified it with a real bpf(BPF_MAP_CREATE) call made by the nobody account: refused (EPERM) at 2, accepted at 0. The root account succeeds in every case: this setting only protects against ordinary accounts.

The sysctl lesson prescribes 2; the stricter value 1 suits a server that has no need to reopen this door on the fly. "No way back" is observed as is: once 1 is set, even root trying to write 0 or 2 gets sysctl: setting key "kernel.unprivileged_bpf_disabled": Operation not permitted. Only a reboot gives control back, hence the value of testing 1 on a staging machine before production.

Lever 2: why CAP_BPF rather than root?

Historically, loading an eBPF program required CAP_SYS_ADMIN, that is almost full powers. Since kernel 5.8, a dedicated capability, CAP_BPF (often accompanied by CAP_PERFMON for tracing), allows loading without everything else. On the VM, these capabilities do exist:

Terminal window
capsh --print | grep -o 'cap_bpf\|cap_perfmon'
cap_bpf
cap_perfmon

The benefit is direct: an observability tool (profiling agent, collector) that only needs to load probes can run with CAP_BPF and CAP_PERFMON, not as root, if it is designed for it. If the tool is compromised, the attacker does not inherit CAP_SYS_ADMIN. This is least privilege applied to the letter, to be granted for example through the capabilities of a systemd unit (AmbientCapabilities).

Mind the limit. This path holds for agents written to follow it. Measured in the lab on Ubuntu 24.04 and AlmaLinux 10.2, a non-root account holding these two capabilities cannot run bpftrace (ERROR: bpftrace currently only supports running as the root user.), and opensnoop (BCC) fails on opening /sys/kernel/tracing/kprobe_events (Permission denied). Your diagnostic tools therefore remain root tools: least privilege applies to what you deploy continuously. The lab did not replay an agent actually designed to run with these two capabilities: check the agent's documentation before removing root.

Lever 3: which value of bpf_jit_harden on a server?

Once verified, a program is translated into machine code by the JIT (just-in-time compiler). This step has been the target of attacks known as JIT spraying, where an attacker influences the generated code to slip useful instructions into it. The kernel offers a defense, the sysctl net.core.bpf_jit_harden, which "blinds" the constants of the generated code. Documented values:

ValueEffect (kernel doc)
0JIT hardening disabled (kernel default)
1Hardening enabled for unprivileged users
2Hardening enabled for all users

The kernel default is not necessarily that of your machine: in the lab, Ubuntu 24.04 does ship 0, but AlmaLinux 10.2 already ships 1. Neither ships 2, and the value can only be read as root: without sudo, the command answers sysctl: permission denied on key 'net.core.bpf_jit_harden'.

On a server, set 2. This is the value prescribed by the sysctl lesson. The reason lies in the threat: the value 1 protects only the programs loaded by unprivileged accounts, yet that lever is already closed by unprivileged_bpf_disabled. The remaining programs are loaded by root or by a tool holding CAP_BPF, precisely those that an attacker who has taken control of such a tool would try to hijack. With 1, these programs get no hardening; with 2, they all do. The price is a slight performance overhead at load and at run time, acceptable on a server, to be measured on a machine heavily loaded with tracing.

Terminal window
# enable JIT hardening for all users
sudo sysctl -w net.core.bpf_jit_harden=2

To make the setting persistent, write it to a file in /etc/sysctl.d/, then reload the configuration:

Terminal window
echo 'net.core.bpf_jit_harden = 2' | sudo tee /etc/sysctl.d/60-ebpf-hardening.conf
sudo sysctl --system
sudo sysctl net.core.bpf_jit_harden # must print: net.core.bpf_jit_harden = 2

The file name matters. sysctl --system sorts the .conf files of /usr/lib/sysctl.d/ and /etc/sysctl.d/ by name (lexicographic order), across all directories, and the last one that defines a key wins; a file in /etc/sysctl.d/ masks a file of the same name in /usr/lib/sysctl.d/. /etc/sysctl.conf is read last, after all the others (measured on AlmaLinux 10.2, where /etc/sysctl.d/99-sysctl.conf points to it). A forgotten file that sorts after yours (90-debug.conf, for example) therefore rewrites the value at each reload. Before concluding, look for who else defines the key:

Terminal window
sudo grep -rn 'bpf' /usr/lib/sysctl.d/ /etc/sysctl.d/ /etc/sysctl.conf

Each line displayed must carry the value you want, or disappear. Second trap measured in the lab: sysctl --system does not put back a value set by hand with sysctl -w if no file defines the key. A setting that "holds" after a reload is therefore durable only if it is written in a file.

Lever 4: how do you audit what is loaded?

You only harden well what you observe. bpftool lists all the eBPF programs actually present in the kernel, whatever tool loaded them. It is the basis of an audit: knowing what is running, and spotting the abnormal.

Terminal window
sudo bpftool prog show | grep -E '^[0-9]+:' | awk '{print $2}' | sort | uniq -c | sort -rn

Real output on the Ubuntu VM (at rest, only the system):

6 cgroup_skb
6 cgroup_device
1 tracing

Here, everything is explained: the cgroup_skb and cgroup_device are placed by systemd for the network filtering and device access of services, the tracing is internal. A program you cannot explain deserves an investigation. The legitimate list varies from one distribution to another: take your reference on each family of machines, as the lesson on the attack surface explains. On a production machine, regularly compare this list with this known reference; an unexpected kprobe, tracepoint or tracing program can betray an unauthorized tool, even an eBPF rootkit.

Lever 5: what is BPF LSM for?

To go further, the kernel exposes BPF LSM (Linux Security Module written in eBPF): decision points where you can allow or deny operations, including the loading of eBPF programs themselves. You can thus write a policy that allows loading only for certain processes, or logs every attempt. It is the ultimate degree of control over the subsystem.

First reflex: check whether BPF LSM is active, because it is not always by default. The list of loaded security modules can be read here:

Terminal window
cat /sys/kernel/security/lsm

On the test VM (Ubuntu 24.04), the output does not contain bpf:

lockdown,capability,landlock,yama,apparmor

On AlmaLinux 10.2 (kernel 6.12), bpf is already in the list, with no action on your part:

lockdown,capability,yama,selinux,bpf,landlock,ima,evm

The lsm= parameter below is therefore for machines like the test Ubuntu, where bpf is missing.

To enable BPF LSM, add it to the list at kernel boot, through the lsm= parameter (for example in the GRUB configuration), then reboot:

lsm=lockdown,capability,landlock,yama,apparmor,bpf

Once bpf is in the list, you can attach programs to the security hooks to enforce a policy. It is an advanced mechanism, to be reserved for environments that have already applied the previous levers, but it offers a control that no sysctl allows. For runtime detection and blocking in Kubernetes, the tools built on this principle live elsewhere: Tetragon blocks directly in the kernel (not yet translated).

What should you set on a server?

This summary gathers the five levers with the recommended value for a server and the expected effect.

SettingServer recommendationEffect
kernel.unprivileged_bpf_disabled2 (default) or 1Blocks unprivileged eBPF
Tool capabilitiesCAP_BPF + CAP_PERFMON for agents designed for it (not replayed in the lab), not rootLeast privilege
net.core.bpf_jit_harden2Mitigates JIT spraying for all loaders
bpftool prog show auditRegular, compared to a referenceDetects the abnormal
Kernel updatesUp to dateFixes verifier flaws

How do you validate the settings?

After applying these settings, read the two values again and count the loaded programs: the result must match what you expect, with no unexplained program.

Terminal window
sudo sysctl kernel.unprivileged_bpf_disabled net.core.bpf_jit_harden
sudo bpftool prog show | grep -cE '^[0-9]+:' # number of programs, to compare with your reference

Hands-on practice

An audit found eBPF wide open on the lab VM: an ordinary user can call bpf() and the JIT compiler is not hardened. You set kernel.unprivileged_bpf_disabled and net.core.bpf_jit_harden to 2, then you make both settings survive a reload of the sysctl configuration, despite a forgotten debug file that contradicts them. The validation makes a real bpf() call from an unprivileged account and checks that the kernel refuses it.

Key takeaways

  • Loading eBPF is a privilege: apply least privilege to it, like any sensitive power.
  • unprivileged_bpf_disabled=2 (recent default) blocks unprivileged eBPF; 1 locks it until reboot.
  • Run the agents designed for it with CAP_BPF/CAP_PERFMON rather than as full root. This path was not replayed in the lab: bpftrace requires root and the BCC tools fail without it (measured).
  • bpf_jit_harden=2 on a server: the value 1 covers only the unprivileged, already blocked, and leaves root loaders unprotected.
  • Regularly audit the loaded programs with bpftool: the unexplained is suspect.
  • The verifier has had flaws (e.g. CVE-2021-3490): an up-to-date kernel remains an indispensable defense.

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