eBPF and Tetragon — security decisions belong in the kernel
Say an attacker exploits an upload bug in a web app behind nginx and gets a shell. The payload runs bash, then id, whoami and cat /etc/passwd. Your security tooling will probably tell you about it. The question is when.
Most Linux security tools report what a process did. eBPF lets you decide what a process is allowed to do, inside the kernel, at the moment it tries. Tetragon packages that so you can use it without writing kernel code.
Userspace agents hear about the attack after it happens
Linux is split into two parts:
- The kernel controls hardware, memory, files and the network, and runs with full privileges.
- Userspace is everything else: nginx, bash, your security agent. Userspace programs can't touch files or the network directly.
When a program needs something done, it makes a syscall, a request to the kernel. Three matter for this post:
execve()starts a new program. When nginx runsbash, that is anexecve.open()opens a file and returns a file descriptor, the handle a process uses to read or write it.write()andsendto()write data to a file or send it over a network socket.
In our attack, nginx calls execve() to start bash. A security agent in userspace learns about that call through events the kernel forwards to it: audit records, copied data, a stream of logs. Each event is copied across the kernel/userspace boundary, which is expensive on a busy host. The agent reads the event after the kernel has already allowed the call. You can alert on the shell. You can't prevent it.
eBPF runs your code inside the kernel, at the moment of the syscall
eBPF lets you load small programs into the running kernel without a reboot or a custom kernel build. You attach a program to a hook: a specific kernel event such as a syscall, a function call, a network packet or a security checkpoint. Every time the event fires, the kernel runs your program as part of its own code path, before the syscall continues.
Running your own code in the kernel is dangerous. Kernel code has unrestricted access to memory, and one null pointer dereference or out-of-bounds write crashes the whole machine. eBPF handles this with the verifier. Before the kernel accepts a program, the verifier simulates every possible execution path and checks that:
- every memory access stays within proven bounds
- no variable is read before it is initialized
- every loop is provably bounded
If any check fails, the program doesn't load. A program that could crash the kernel never runs.
eBPF programs are compiled to bytecode rather than native machine code — that's what makes static verification possible. After verification the kernel JIT-compiles the bytecode to native CPU instructions, so it runs at near-native speed.
An eBPF program has two things it can do when an event fires. It can observe: write a filtered event to a BPF map, a fixed-size queue in kernel memory that Tetragon reads from userspace. Tetragon uses a perf buffer on older versions and a BPF ring buffer by default on kernel 5.11+ with recent versions. For this post the difference doesn't matter. Or it can block: return an error or send SIGKILL before the syscall completes, so the operation never finishes.
Because the program runs at the hook, it acts before the syscall returns to the process. For our attack, a program attached to execve sees nginx starting bash and can kill it before bash executes a single instruction.
Tetragon: eBPF programs you configure instead of write
Writing eBPF programs by hand is kernel programming. Tetragon, an open-source project from Cilium, ships them ready to use and loads them based on configuration. It runs on any Linux host, as a systemd service on VMs and bare metal or as a Kubernetes DaemonSet (one pod per node). The Tetragon agent loads your policies into the kernel as eBPF programs, then reads the events they produce.
With no configuration, Tetragon already emits a process_exec event for every program started on the host, with the full parent process tree. On Kubernetes it adds pod, namespace and container metadata. This is what it logs for our attack (abbreviated):
{
"process_exec": {
"process": {
"binary": "/bin/bash",
"arguments": "-c 'id; whoami; cat /etc/passwd'",
"cwd": "/var/www/html",
"uid": 33,
"parent": {
"binary": "/usr/sbin/nginx",
"pid": 19580
},
"pod": {
"namespace": "production",
"name": "frontend-685769df8-912bc",
"container": { "name": "nginx" }
}
}
}
}
The parent is nginx, the child is a shell, and the working directory is the webroot. A shell under a web server is a reliable detection signal. But this is still observation: the event describes a shell that has already started.
To act on it, you write a TracingPolicy, a YAML file or a Kubernetes CRD. A policy has four parts.
Hook. Where the eBPF program attaches. The three types you will use most:
- kprobes attach to almost any kernel function by name. They are flexible, but function names and signatures can change between kernel versions, which breaks policies.
- tracepoints are hooks the kernel maintainers deliberately expose. They cover less but stay stable across updates.
- LSM hooks attach to Linux Security Module checkpoints, the places where the kernel makes its security decisions. Tetragon supports them on kernel 5.7+ with
CONFIG_BPF_LSM=y.
Arguments. Which values from the call to capture, such as a file path or the binary being executed.
Selectors. Filters that run inside the eBPF program. Events that don't match are dropped in the kernel and never reach userspace.
Actions. What happens on a match:
Postsends the event to userspace. Observe only.Sigkillsends SIGKILL, the signal that terminates a process and can't be caught or ignored.Overridemakes the syscall return an error code, as if the kernel had rejected it itself.
Blocking the shell before it starts
For our attack the hook is security_bprm_check, a kernel function called during execve after the kernel has found the target binary and before the new program runs:
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: block-nginx-shell-spawn
spec:
kprobes:
- call: "security_bprm_check"
syscall: false
message: "Blocked shell spawned by nginx"
args:
- index: 0
type: "linux_binprm"
selectors:
- matchBinaries:
- operator: "In"
values:
- "/usr/sbin/nginx"
followChildren: true
matchArgs:
- index: 0
operator: "Equal"
values:
- "/bin/sh"
- "/bin/bash"
- "/usr/bin/sh"
- "/usr/bin/bash"
matchActions:
- action: Sigkill
Read it as a sentence: if the process calling execve is nginx or one of its children, and the target is a shell, kill it. The shell never executes an instruction. The policy match is still logged as a process_kprobe event with the action taken, so you stopped the attack and kept the evidence.
- Use
Equaland list both/binand/usr/binpaths. APrefixmatch on/bin/shwould also match/bin/shutdown. On many distros/binis a symlink to/usr/bin, so check which path yourexecvecalls actually report on your hosts. - Run the policy with
Postfirst. If your app legitimately shells out,Sigkillwill break it, and the observe-mode events tell you before it does.
Sigkill sends the signal via bpf_send_signal. The process is killed at the next kernel-to-userspace transition — for execve that means before the new program ever starts. For file access, Override is cleaner: it makes the syscall return an error immediately, before any data is written. Override requires CONFIG_BPF_KPROBE_OVERRIDE in the kernel config, and only works on functions whitelisted for error injection — check /sys/kernel/debug/error_injection/list on your host.
What this doesn't cover
| eBPF is a poor fit when | Why |
|---|---|
| You need application context: HTTP routes, SQL queries, user sessions | Kernel hooks see syscalls and sockets. App-level context needs uprobes (hooks on userspace functions) or in-app instrumentation. |
| Traffic is encrypted before it reaches the kernel | On write() or sendto() you see ciphertext, unless you hook the TLS library in userspace. |
| Your kernel is old | LSM hooks need 5.7+. Older kernels have limited eBPF support, so check what your distro ships before planning around it. |
| The attacker has root on the host | Root can detach or unload eBPF programs. Kernel lockdown and restricted BPF capabilities raise the bar, but don't assume enforcement survives a root compromise. |
| The host is under heavy load | Every hook runs your program on the event path. eBPF programs that capture broadly — logging every syscall or every file open — add measurable overhead on busy systems. |
The shell never runs
Back to the attack from the start. nginx calls execve() to start bash. The kernel reaches security_bprm_check and runs the eBPF program the verifier already approved. The program sees that the caller is nginx and the target is a shell, and sends SIGKILL. bash never runs id, whoami or cat /etc/passwd, and Tetragon still logs the attempt.
That is the whole mechanism. A hook is the point where the kernel is about to act. A verified program runs there. A policy says what to do when it matches. Other hooks, other selectors and Override for file access are the same loop pointed at a different event. If you can describe an action that is never legitimate on your hosts, you can write the policy for it, and you now know what happens between the syscall and the block.