seccomp-bpf Explained: Filtering System Calls
How seccomp-bpf lets a process install a classic BPF filter that allows, blocks or kills on each system call, with the return actions and a working C example.

A process that has been compromised can only do harm through system calls. It cannot touch a file, open a socket or start a program without asking the kernel, and the kernel will say yes if the process has permission. Seccomp-bpf narrows that conversation: before the program does anything else, it installs a small filter that the kernel runs on every system call from then on. A call the filter forbids never reaches the kernel's normal code. This post explains how the filter sees a call, what it can answer, and the traps in writing one, using Linux 6.12 and the seccomp(2) and seccomp_unotify(2) man pages. It is one layer in the complete guide to sandboxing Linux processes, and it builds on Linux capabilities.
Two modes, one that matters
Seccomp began in Linux 2.6.12 as strict mode: a process could call only read, write, _exit and sigreturn, and anything else killed it. That is too blunt for most programs. Linux 3.5 added filter mode, where the process supplies a program in classic BPF (cBPF), and the kernel runs it for every system call. "Seccomp" in practice now means filter mode.
You install a filter with prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) or, since 3.17, the seccomp(SECCOMP_SET_MODE_FILTER, flags, &prog) system call. Filters are inherited by child processes and survive execve(). They can only be added, never removed. A second filter does not replace the first: the kernel runs all of them and takes the most restrictive result.
One precondition is easy to forget. To install a filter, a process must either hold CAP_SYS_ADMIN in its namespace or have set the no_new_privs bit with prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0). The bit stops later execve() calls from gaining privilege through setuid files, which is what makes it safe for an unprivileged process to impose a filter on itself. The post on ambient capabilities and no_new_privs covers the bit in detail.
What the filter sees
The filter's input is a fixed structure, struct seccomp_data, declared in linux/seccomp.h:
struct seccomp_data {
int nr; /* system call number */
__u32 arch; /* AUDIT_ARCH_* value */
__u64 instruction_pointer; /* CPU instruction pointer */
__u64 args[6]; /* up to six system call arguments */
};Three things follow from that layout. The filter sees the call number and the raw argument registers, not decoded strings. A pointer argument is just an address; the filter cannot dereference it, so it cannot allow open for /tmp and deny /etc. That limit is deliberate, because reading user memory from the filter would be racy: another thread could change the string between the check and the use. And the number depends on the architecture, which is why arch is in the structure.
Return values are actions
The filter returns a 32-bit value whose upper bits are an action and lower 16 bits are data. The kernel defines these actions, listed from most to least severe, and when several filters disagree it applies the one that comes first:
| Action | Effect |
|---|---|
SECCOMP_RET_KILL_PROCESS | Kill the whole process (Linux 4.14) |
SECCOMP_RET_KILL_THREAD | Kill only the calling thread |
SECCOMP_RET_TRAP | Send SIGSYS to the thread |
SECCOMP_RET_ERRNO | Fail the call with the errno in the data bits |
SECCOMP_RET_USER_NOTIF | Ask a supervisor process to decide (Linux 5.0) |
SECCOMP_RET_TRACE | Notify an attached ptrace tracer |
SECCOMP_RET_LOG | Allow, and log the call (Linux 4.14) |
SECCOMP_RET_ALLOW | Let the call run |
ERRNO suits programs you want to keep running with a feature missing; KILL_PROCESS suits a sandbox where any forbidden call means something has gone wrong. The supervised case is the subject of the post on SECCOMP_RET_USER_NOTIF.
A first filter, written out
The filter language is classic BPF: a short list of instructions that load from the structure, compare, jump and return. This program blocks getppid and allows everything else. It starts with an architecture check, which is not optional.
#define _GNU_SOURCE
#include <errno.h>
#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
int main(void) {
struct sock_filter filter[] = {
/* A = seccomp_data.arch */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, arch)),
/* if arch == x86-64 skip the next instruction, else kill */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AUDIT_ARCH_X86_64, 1, 0),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
/* A = seccomp_data.nr */
BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
/* if nr == getppid fall through to the deny, else skip it */
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_getppid, 0, 1),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (EPERM & SECCOMP_RET_DATA)),
BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),
};
struct sock_fprog prog = {
.len = sizeof(filter) / sizeof(filter[0]),
.filter = filter,
};
if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
perror("PR_SET_NO_NEW_PRIVS");
return 1;
}
if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
perror("PR_SET_SECCOMP");
return 1;
}
pid_t parent = (pid_t)syscall(SYS_getppid);
printf("getppid() = %d: %s\n", (int)parent, strerror(errno));
printf("getpid() works: %s\n", getpid() > 0 ? "yes" : "no");
return 0;
}The program calls syscall(SYS_getppid) so the number the filter sees is unambiguous. The output on an x86-64 machine:
getppid() = -1: Operation not permitted
getpid() works: yesThe second line shows that getpid still works while getppid fails. The jump distances are the part to check by hand. At instruction 1, a match skips one instruction (the kill at index 2) and lands on index 3; a mismatch falls through to the kill. At instruction 4, a match falls through to the ERRNO return at index 5, and a mismatch skips it to the ALLOW at index 6.
The architecture check is not decoration
The same call number means different things on different architectures. On x86-64 the number 2 is open; on 32-bit x86 it is fork. A 64-bit process can switch to the 32-bit entry points (int 0x80) unless the kernel is built without them, and then the numbers its filter checks would not mean what the author assumed. Checking arch first and killing on a mismatch closes that gap. On x86-64 there is a second wrinkle, the x32 ABI, whose call numbers have bit 30 set. A deny-list filter that does not handle x32 can be bypassed by calling the x32 variant of a call. Allow-lists avoid the problem, because an unknown number is not on the list.
Allow-list or deny-list
A deny-list names calls to block and allows the rest. It is easy to write and it fails open: the next kernel release adds a system call you did not think of, and the filter permits it. An allow-list names what the program needs and denies the rest. It takes more work, but it fails closed. For anything that holds real risk, use the allow-list. The post on recording a program's syscalls with strace shows one way to build it.
Cost
A filter runs on every system call from the process, so its cost scales with the length of the program. Since Linux 5.11, the kernel caches the result for system calls whose filter outcome depends only on the call number and architecture, which makes allowed hot calls close to free on x86-64. Filters that inspect arguments are not cached. We do not quote a figure here, because the cost depends on the kernel and the filter; measure your own with perf before trading safety for speed.


