seccomp(2)

๐ŸŒธ NAME ๐ŸŒธ

seccomp โ€” operate on Secure Computing state of the process. Yes bestie, this is the seccomp(2) page, and we're gonna get into it. ๐Ÿ’–


๐Ÿซถ LIBRARY ๐Ÿซถ

Standard C library (libc, -lc).


โœจ SYNOPSIS โœจ

#include <linux/seccomp.h>  /* Definition of SECCOMP_* constants */
#include <linux/filter.h>   /* Definition of struct sock_fprog */
#include <linux/audit.h>    /* Definition of AUDIT_* constants */
#include <linux/signal.h>   /* Definition of SIG* constants */
#include <sys/ptrace.h>     /* Definition of PTRACE_* constants */
#include <sys/syscall.h>    /* Definition of SYS_* constants */
#include <unistd.h>

int syscall(SYS_seccomp, unsigned int operation, unsigned int flags,
            void *args);
Okay so huge note: glibc provides no wrapper for seccomp(), so you literally have to use syscall(2). No wrapper, no shortcut, just syscall(2). Periodt. ๐Ÿ’…

๐Ÿ’… DESCRIPTION ๐Ÿ’…

The seccomp() system call operates on the Secure Computing (seccomp) state of the calling process.

Currently, Linux supports the following operation values:

๐Ÿ‘‘ SECCOMP_SET_MODE_STRICT

The only system calls that the calling thread is permitted to make are read(2), write(2), _exit(3) (but not exit_group(2)), and sigreturn(2). Other system calls result in the termination of the calling thread, or termination of the entire process with the SIGKILL signal when there is only one thread. Strict secure computing mode is useful for number-crunching applications that may need to execute untrusted byte code, perhaps obtained by reading from a pipe or socket.

Note that although the calling thread can no longer call sigprocmask(2), it can use sigreturn(2) to block all signals apart from SIGKILL and SIGSTOP. This means that alarm(2) (for example) is not sufficient for restricting the process's execution time. Instead, to reliably terminate the process, SIGKILL must be used. This can be done by using timer_create(2) with SIGEV_SIGNAL and sigev_signo set to SIGKILL, or by using setrlimit(2) to set the hard limit for RLIMIT_CPU.

This operation is available only if the kernel is configured with CONFIG_SECCOMP enabled.

The value of flags must be 0, and args must be NULL.

This operation is functionally identical to the call:

prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);

๐Ÿฆ‹ SECCOMP_SET_MODE_FILTER

The system calls allowed are defined by a pointer to a Berkeley Packet Filter (BPF) passed via args. This argument is a pointer to a struct sock_fprog; it can be designed to filter arbitrary system calls and system call arguments. If the filter is invalid, seccomp() fails, returning EINVAL in errno.

If fork(2) or clone(2) is allowed by the filter, any child processes will be constrained to the same system call filters as the parent. If execve(2) is allowed, the existing filters will be preserved across a call to execve(2).

In order to use the SECCOMP_SET_MODE_FILTER operation, either the calling thread must have the CAP_SYS_ADMIN capability in its user namespace, or the thread must already have the no_new_privs bit set. If that bit was not already set by an ancestor of this thread, the thread must make the following call:

prctl(PR_SET_NO_NEW_PRIVS, 1);

Otherwise, the SECCOMP_SET_MODE_FILTER operation fails and returns EACCES in errno. This requirement ensures that an unprivileged process cannot apply a malicious filter and then invoke a set-user-ID or other privileged program using execve(2), thus potentially compromising that program. (Such a malicious filter might, for example, cause an attempt to use setuid(2) to set the caller's user IDs to nonzero values to instead return 0 without actually making the system call. Thus, the program might be tricked into retaining superuser privileges in circumstances where it is possible to influence it to do dangerous things because it did not actually drop privileges.)

If prctl(2) or seccomp() is allowed by the attached filter, further filters may be added. This will increase evaluation time, but allows for further reduction of the attack surface during execution of a thread.

The SECCOMP_SET_MODE_FILTER operation is available only if the kernel is configured with CONFIG_SECCOMP_FILTER enabled.

When flags is 0, this operation is functionally identical to the call:

prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, args);

The recognized flags are:

  • SECCOMP_FILTER_FLAG_LOG (since Linux 4.14) โ€” All filter return actions except SECCOMP_RET_ALLOW should be logged. An administrator may override this filter flag by preventing specific actions from being logged via the /proc/sys/kernel/seccomp/actions_logged file.
  • SECCOMP_FILTER_FLAG_NEW_LISTENER (since Linux 5.0) โ€” After successfully installing the filter program, return a new user-space notification file descriptor. (The close-on-exec flag is set for the file descriptor.) When the filter returns SECCOMP_RET_USER_NOTIF a notification will be sent to this file descriptor.

At most one seccomp filter using the SECCOMP_FILTER_FLAG_NEW_LISTENER flag can be installed for a thread.

See seccomp_unotify(2) for further details.

  • SECCOMP_FILTER_FLAG_SPEC_ALLOW (since Linux 4.17) โ€” Disable Speculative Store Bypass mitigation.
  • SECCOMP_FILTER_FLAG_TSYNC โ€” When adding a new filter, synchronize all other threads of the calling process to the same seccomp filter tree. A "filter tree" is the ordered list of filters attached to a thread. (Attaching identical filters in separate seccomp() calls results in different filters from this perspective.)

If any thread cannot synchronize to the same filter tree, the call will not attach the new seccomp filter, and will fail, returning the first thread ID found that cannot synchronize. Synchronization will fail if another thread in the same process is in SECCOMP_MODE_STRICT or if it has attached new seccomp filters to itself, diverging from the calling thread's filter tree.

  • SECCOMP_FILTER_FLAG_TSYNC_ESRCH (since Linux 5.7) โ€” Fail with ESRCH instead of returning the thread ID on synchronization failure if SECCOMP_FILTER_FLAG_TSYNC was specified.

If this flag is not set, using SECCOMP_FILTER_FLAG_NEW_LISTENER and SECCOMP_FILTER_FLAG_TSYNC together results in EINVAL.

  • SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV (since Linux 5.19) โ€” Ignore non-fatal signals until the supervisor sends a response.

The SECCOMP_FILTER_FLAG_NEW_LISTENER flag must be set.

โญ SECCOMP_GET_ACTION_AVAIL (since Linux 4.14)

Test to see if an action is supported by the kernel. This operation is helpful to confirm that the kernel knows of a more recently added filter return action since the kernel treats all unknown actions as SECCOMP_RET_KILL_PROCESS.

The value of flags must be 0, and args must be a pointer to an unsigned 32-bit filter return action.

๐ŸŒˆ SECCOMP_GET_NOTIF_SIZES (since Linux 5.0)

Get the sizes of the seccomp user-space notification structures. Since these structures may evolve and grow over time, this command can be used to determine how much memory to allocate for sending and receiving notifications.

The value of flags must be 0, and args must be a pointer to a struct seccomp_notif_sizes, which has the following form:

struct seccomp_notif_sizes
    __u16 seccomp_notif;      /* Size of notification structure */
    __u16 seccomp_notif_resp; /* Size of response structure */
    __u16 seccomp_data;       /* Size of 'struct seccomp_data' */
};

See seccomp_unotify(2) for further details.


๐ŸŒธ Filters ๐ŸŒธ

When adding filters via SECCOMP_SET_MODE_FILTER, args points to a filter program:

struct sock_fprog {
    unsigned short      len;    /* Number of BPF instructions */
    struct sock_filter *filter; /* Pointer to array of
                                   BPF instructions */
};

Each program must contain one or more BPF instructions:

struct sock_filter {            /* Filter block */
    __u16 code;                 /* Actual filter code */
    __u8  jt;                   /* Jump true */
    __u8  jf;                   /* Jump false */
    __u32 k;                    /* Generic multiuse field */
};

When executing the instructions, the BPF program operates on the system call information made available (i.e., use the BPF_ABS addressing mode) as a (read-only) buffer of the following form:

struct seccomp_data {
    int   nr;                   /* System call number */
    __u32 arch;                 /* AUDIT_ARCH_* value
                                   (see <linux/audit.h>) */
    __u64 instruction_pointer;  /* CPU instruction pointer */
    __u64 args[6];              /* Up to 6 system call arguments */
};

Because numbering of system calls varies between architectures and some architectures (e.g., x86-64) allow user-space code to use the calling conventions of multiple architectures (and the convention being used may vary over the life of a process that uses execve(2) to execute binaries that employ the different conventions), it is usually necessary to verify the value of the arch field.

It is strongly recommended to use an allow-list approach whenever possible because such an approach is more robust and simple. A deny-list will have to be updated whenever a potentially dangerous system call is added (or a dangerous flag or option if those are deny-listed), and it is often possible to alter the representation of a value without altering its meaning, leading to a deny-list bypass. See also Caveats below.

The arch field is not unique for all calling conventions. The x86-64 ABI and the x32 ABI both use AUDIT_ARCH_X86_64 as arch, and they run on the same processors. Instead, the mask __X32_SYSCALL_BIT is used on the system call number to tell the two ABIs apart.

okay so picking up right where we left off on the __X32_SYSCALL_BIT situation ๐Ÿ’… because it's giving "policy design is a whole personality"

the rule: a policy has to either deny all syscalls with __X32_SYSCALL_BIT set, or it has to recognize syscalls both with and without __X32_SYSCALL_BIT set. there is no cute little middle ground. and a list of system calls to be denied based on nr that does not also contain nr values with __X32_SYSCALL_BIT set? yeah that gets bypassed by a malicious program that just... sets __X32_SYSCALL_BIT. no cap. ๐Ÿ˜ญ

and there's more lore, unfortunately. kernels prior to Linux 5.4 incorrectly permitted nr in the ranges 512-547, as well as the corresponding non-x32 syscalls ORed with __X32_SYSCALL_BIT. so like, nr == 521 and nr == (101 | __X32_SYSCALL_BIT) would result in invocations of ptrace(2) with potentially confused x32-vs-x86_64 semantics in the kernel ๐Ÿ™ˆ policies that are meant to work on kernels before Linux 5.4 must make sure they deny these system calls or otherwise handle them correctly. on Linux 5.4 and newer, those system calls will fail with the error ENOSYS, without doing anything. slay.


the instruction_pointer field hands you the address of the machine-language instruction that performed the system call. what's that for? well, you can pair it with /proc/pid/maps to do checks based on which region (mapping) of the program made the system call. lowkey useful. (and probably, it is wise to lock down the mmap(2) and mprotect(2) system calls so the program can't subvert those checks. just saying. ๐Ÿ”’)

when you're checking values from args, keep in mind that arguments are often silently truncated before being processed, but after the seccomp check. example: this happens if the i386 ABI is used on an x86-64 kernel โ€” the kernel will normally not look beyond the 32 lowest bits of the arguments, but the values of the full 64-bit registers will be present in the seccomp data. a less surprising example: if the x86-64 ABI is used to perform a system call that takes an argument of type int, the more-significant half of the argument register is ignored by the system call, but visible in the seccomp data. sneak peek energy fr fr ๐Ÿ‘€

a seccomp filter returns a 32-bit value made of two parts:

  • the most significant 16 bits (corresponding to the mask defined by the constant SECCOMP_RET_ACTION_FULL) contain one of the "action" values listed below;
  • the least significant 16-bits (defined by the constant SECCOMP_RET_DATA) are "data" to be associated with this return value.

if multiple filters exist, they all get executed, in reverse order of their addition to the filter tree โ€” that is, the most recently installed filter is executed first. (note that all filters will be called even if one of the earlier filters returns SECCOMP_RET_KILL. this is done to simplify the kernel code and to provide a tiny speed-up in the execution of sets of filters by avoiding a check for this uncommon case.) and the return value for the evaluation of a given system call is the first-seen action value of highest precedence (along with its accompanying data) returned by execution of all of the filters.

in decreasing order of precedence, the action values that may be returned by a seccomp filter are:

SECCOMP_RET_KILL_PROCESS (since Linux 4.14)

this value results in immediate termination of the process, with a core dump ๐Ÿ’€ the system call is not executed. and by contrast with SECCOMP_RET_KILL_THREAD below, all threads in the thread group are terminated. (for a discussion of thread groups, see the description of the CLONE_THREAD flag in clone(2).)

the process terminates as though killed by a SIGSYS signal. even if a signal handler has been registered for SIGSYS, the handler gets ignored in this case and the process always terminates. to a parent process that is waiting on this process (using waitpid(2) or similar), the returned wstatus will indicate that its child was terminated as though by a SIGSYS signal.

SECCOMP_RET_KILL_THREAD (or SECCOMP_RET_KILL)

this value results in immediate termination of the thread that made the system call. the system call is not executed. other threads in the same thread group will continue to execute, so like, the vibe lives on.

the thread terminates as though killed by a SIGSYS signal. see SECCOMP_RET_KILL_PROCESS above.

before Linux 4.11, any process terminated in this way would not trigger a coredump (even though SIGSYS is documented in signal(7) as having a default action of termination with a core dump). since Linux 4.11, a single-threaded process will dump core if terminated in this way. ๐Ÿซถ

with the addition of SECCOMP_RET_KILL_PROCESS in Linux 4.14, SECCOMP_RET_KILL_THREAD was added as a synonym for SECCOMP_RET_KILL, in order to more clearly distinguish the two actions. naming rights, iconic.

note: using SECCOMP_RET_KILL_THREAD to kill a single thread in a multithreaded process is likely to leave the process in a permanently inconsistent and possibly corrupt state. this is the messy part. do not skip past it. ๐Ÿ˜ญ

SECCOMP_RET_TRAP

this value results in the kernel sending a thread-directed SIGSYS signal to the triggering thread. (the system call is not executed.) various fields get set in the siginfo_t structure (see sigaction(2)) associated with that signal:

  • si_signo will contain SIGSYS.
  • si_call_addr will show the address of the system call instruction.
  • si_syscall and si_arch will indicate which system call was attempted.
  • si_code will contain SYS_SECCOMP.
  • si_errno will contain the SECCOMP_RET_DATA portion of the filter return value.

the program counter will be as though the system call happened (i.e., the program counter will not point to the system call instruction). the return value register will contain an architecture-dependent value; if resuming execution, set it to something appropriate for the system call. (the architecture dependency is because replacing it with ENOSYS could overwrite some useful information. so like, respect the register bestie โœจ)

SECCOMP_RET_ERRNO

this value results in the SECCOMP_RET_DATA portion of the filter's return value being passed to user space as the errno value without executing the system call. clean, simple, no drama.

SECCOMP_RET_USER_NOTIF (since Linux 5.0)

forward the system call to an attached user-space supervisor process to allow that process to decide what to do with the system call. if there is no attached supervisor (either because the filter was not installed with the SECCOMP_FILTER_FLAG_NEW_LISTENER flag or because the file descriptor was closed), the filter returns ENOSYS (similar to what happens when a filter returns SECCOMP_RET_TRACE and there is no tracer). see seccomp_unotify(2) for further details. ๐Ÿ“ฌ

note that the supervisor process will not be notified if another filter returns an action value with a precedence greater than SECCOMP_RET_USER_NOTIF.

SECCOMP_RET_TRACE

when returned, this value will cause the kernel to attempt to notify a ptrace(2)-based tracer prior to executing the system call. if there is no tracer present, the system call is not executed and returns a failure status with errno set to ENOSYS.

a tracer will be notified if it requests PTRACE_O_TRACESECCOMP using ptrace(PTRACE_SETOPTIONS). the tracer will be notified of a PTRACE_EVENT_SECCOMP and the SECCOMP_RET_DATA portion of the filter's return value will be available to the tracer via PTRACE_GETEVENTMSG.

the tracer can skip the system call by changing the system call number to -1. alternatively, the tracer can change the system call requested by changing the system call to a valid system call number. if the tracer asks to skip the system call, then the system call will appear to return the value that the tracer puts in the return value register. spycraft behavior fr ๐Ÿ•ต๏ธ

before Linux 4.8, the seccomp check will not be run again after the tracer is notified. (this means that, on older kernels, seccomp-based sandboxes must not allow use of ptrace(2) โ€” even of other sandboxed processes โ€” without extreme care; ptracers can use this mechanism to escape from the seccomp sandbox.) that's an escape hatch, not a feature. handle with care!!

note that a tracer process will not be notified if another filter returns an action value with a precedence greater than SECCOMP_RET_TRACE.

SECCOMP_RET_LOG (since Linux 4.14)

this value results in the system call being executed after the filter return action is logged ๐Ÿ“ an administrator may override the logging of this action via the /proc/sys/kernel/seccomp/actions_logged file.

SECCOMP_RET_ALLOW

this value results in the system call being executed. that's it, that's the whole action, and honestly? it's the most chill one here. allow. โœจ


if an action value other than one of the above is specified, then the filter action is treated as either SECCOMP_RET_KILL_PROCESS (since Linux 4.14) or SECCOMP_RET_KILL_THREAD (in Linux 4.13 and earlier). so like, make a typo and the kernel just goes "lol, kill". ๐Ÿ˜ญ

/proc interfaces

the files in the directory /proc/sys/kernel/seccomp provide additional seccomp information and configuration:

actions_avail (since Linux 4.14)

a read-only ordered list of seccomp filter return actions in string form. the ordering, from left-to-right, is in decreasing order of precedence. the list represents the set of seccomp filter return actions supported by the kernel. basically your kernel's whole roster ๐ŸŽ€

actions_logged (since Linux 4.14)

a read-write ordered list of seccomp filter return actions that are allowed to be logged. writes to the file do not need to be in ordered form but reads from the file will be ordered in the same way as the actions_avail file.

it is important to note that the value of actions_logged does not prevent certain filter return actions from being logged when the audit subsystem is configured to audit a task. if the action is not found in the actions_logged file, the final decision on whether to audit the action for that task is ultimately left up to the audit subsystem to decide for all filter return actions other than SECCOMP_RET_ALLOW.

the "allow" string is not accepted in the actions_logged file as it is not possible to log SECCOMP_RET_ALLOW actions. attempting to write "allow" to the file will fail with the error EINVAL. not me writing the forbidden string ๐Ÿ’€

Audit logging of seccomp actions

since Linux 4.14, the kernel provides the facility to log the actions returned by seccomp filters in the audit log. the kernel makes the decision to log an action based on the action type, whether or not the action is present in the actions_logged file, and whether kernel auditing is enabled (e.g., via the kernel boot option audit=1). the rules are as follows: โณ

  • ๐Ÿ’– If the action is SECCOMP_RET_ALLOW, bestie, it does not get logged. Nothing to see here.
  • ๐ŸŒธ Otherwise, if the action is either SECCOMP_RET_KILL_PROCESS or SECCOMP_RET_KILL_THREAD, and that action shows up in the actions_logged file, then yes โ€” it's logged.
  • ๐Ÿฆ‹ Otherwise, if the filter asked for logging (that's the SECCOMP_FILTER_FLAG_LOG flag) and the action appears in the actions_logged file, it's logged too.
  • โœจ Otherwise, if kernel auditing is turned on and the process is actually being audited (think autrace(8)), then it's logged.
  • ๐ŸŽ€ Otherwise? Not logged. That's the whole vibe check.

๐Ÿ’… RETURN VALUE

On success โ€” slay โ€” seccomp() returns 0.

On error, if SECCOMP_FILTER_FLAG_TSYNC was used, the return value is the ID of the thread that caused the synchronization failure. (And heads up, this ID is a kernel thread ID, the same flavor you get from clone(2) and gettid(2).) iykyk

On other errors, it returns -1, and errno gets set so you know what went wrong. โœจ


๐Ÿ˜ญ ERRORS

seccomp() can fail for the following reasons, bestie, and she will let you know:

  • EACCES โ€” The caller didn't have the CAP_SYS_ADMIN capability in its user namespace, or hadn't set no_new_privs before using SECCOMP_SET_MODE_FILTER. Rude? A little. Correct? Yes.
  • EBUSY โ€” While installing a new filter, the SECCOMP_FILTER_FLAG_NEW_LISTENER flag was specified, but a previous filter had already been installed with that flag. One listener only, babes.
  • EFAULT โ€” args was not a valid address. ๐Ÿซ 
  • EINVAL โ€” operation is unknown, or it's just not supported by this kernel version or configuration.
  • EINVAL โ€” The specified flags are invalid for the given operation.
  • EINVAL โ€” operation included BPF_ABS, but the specified offset wasn't aligned to a 32-bit boundary, or it exceeded sizeof(struct seccomp_data). Alignment really is that girl.
  • EINVAL โ€” A secure computing mode has already been set, and operation differs from the existing setting. Pick a lane.
  • EINVAL โ€” operation specified SECCOMP_SET_MODE_FILTER, but the filter program pointed to by args wasn't valid, or the length of the filter program was zero, or it exceeded BPF_MAXINSNS (4096) instructions. ๐Ÿ˜ญ
  • ENOMEM โ€” Out of memory. Classic.
  • ENOMEM โ€” The total length of all filter programs attached to the calling thread would exceed MAX_INSNS_PER_PATH (32768) instructions. And not me telling you the math: for the purposes of calculating this limit, each already-existing filter program incurs an overhead penalty of 4 instructions. Keep that in mind fr fr.
  • EOPNOTSUPP โ€” operation specified SECCOMP_GET_ACTION_AVAIL, but the kernel doesn't support the filter return action specified by args. ๐Ÿ’€
  • ESRCH โ€” Another thread caused a failure during thread sync, but its ID couldn't be determined. Mysterious energy, honestly.

๐Ÿ‘‘ STANDARDS

Linux.


๐ŸŒˆ HISTORY

Linux 3.17.


๐Ÿงš NOTES

So rather than hand-coding seccomp filters like the example below (in a later part, bestie, don't panic), you may prefer to just use the libseccomp library โ€” it's a whole front-end for generating seccomp filters and honestly it's iconic.

The Seccomp field of the /proc/pid/status file gives you a way to view the seccomp mode of a process; go peek at proc(5) for that.

seccomp() is a superset of what prctl(2)'s PR_SET_SECCOMP operation gives you (and note that the prctl(2) one doesn't support flags at all โ€” it's giving limited).

Since Linux 4.4, the ptrace(2) PTRACE_SECCOMP_GET_FILTER operation can be used to dump a process's seccomp filters. Obsessed.

๐Ÿฆ‹ Architecture support for seccomp BPF

Architecture support for seccomp BPF filtering is available on the following architectures:

  • x86-64, i386, x32 (since Linux 3.5)
  • ARM (since Linux 3.8)
  • s390 (since Linux 3.8)
  • MIPS (since Linux 3.16)
  • ARM-64 (since Linux 3.19)
  • PowerPC (since Linux 4.3)
  • Tile (since Linux 4.3)
  • PA-RISC (since Linux 4.6)

๐Ÿ“ Caveats

There are various subtleties to consider when you're applying seccomp filters to a program, including the following. Pay attention bestie, this part matters:

  • Some traditional system calls have user-space implementations in the vdso(7) on many architectures. Notable examples include clock_gettime(2), gettimeofday(2), and time(2). On such architectures, seccomp filtering for these system calls will have no effect. (However, there are cases where the vdso(7) implementations may fall back to invoking the true system call โ€” and in that case, seccomp filters would see the system call. Sneaky.)
  • Seccomp filtering is based on system call numbers. But applications typically don't directly invoke system calls โ€” they call wrapper functions in the C library, which then invoke the system calls. So you have to be aware of the following:
    • The glibc wrappers for some traditional system calls may actually employ system calls with different names in the kernel. For example, the exit(3) wrapper function actually employs the exit_group(2) system call, and the fork(2) wrapper function actually calls clone(2). Plot twist.
    • The behavior of wrapper functions may vary across architectures, according to the range of system calls provided on those architectures. In other words, the same wrapper function may invoke different system calls on different architectures.
    • Finally, the behavior of wrapper functions can change across glibc versions. For example, in older versions, the glibc wrapper function for open(2) invoked the system call of the same name, but starting in glibc 2.26, the implementation switched to calling openat(2) on all architectures.

The consequence of all of the above is that it may be necessary to filter for a system call other than what you might expect. Various manual pages in Section 2 provide helpful details about the differences between wrapper functions and the underlying system calls, in subsections entitled C library/kernel differences. Go read them, they're doing the lord's work.

Furthermore โ€” and this one's a real one โ€” note that the application of seccomp filters even risks causing bugs in an application, when the filters cause unexpected failures for legitimate operations that the application might need to perform. And such bugs may not easily be discovered when you're testing the seccomp filters, if the bugs occur in rarely used application code paths. ๐Ÿ™ˆ Which is exactly the kind of thing that bites you in prod at 3am. Be careful.

โญ Seccomp-specific BPF details

Note the following BPF details specific to seccomp filters:

  • The BPF_H and BPF_B size modifiers are not supported: all operations must load and store (4-byte) words (BPF_W). No exceptions, no shortcuts.
  • To access the contents of the seccomp_data buffer, use the BPF_ABS addressing mode modifier.
  • The BPF_LEN addressing mode modifier yields an immediate mode operand whose value is the size of the seccomp_data buffer.

๐ŸŒธ EXAMPLES ๐ŸŒธ

okay bestie, this is the part where we actually do the thing. ๐Ÿ’… The program down below? It wants four or more arguments. The first three are:

  • a system call number,
  • a numeric architecture identifier,
  • and an error number.

Then it takes those values and builds a BPF filter that it uses at run time to do these checks:

  • If the program is not running on the specified architecture, the BPF filter makes system calls fail with the error ENOSYS.
  • If the program tries to execute the system call with the specified number, the BPF filter makes that system call fail, with errno set to the specified error number.

The rest of the command-line arguments are the pathname (plus extra arguments) of a program the example program should try to run using execv(3) (that's a library function that uses the execve(2) system call, btw ๐Ÿซถ). Some example runs are below, and they slay.


First we show the architecture we're running on (x86-64), then we build a shell function that looks up system call numbers on that architecture:

$ uname -m;
x86_64
$ syscall_nr() {
    cat /usr/src/linux/arch/x86/syscalls/syscall_64.tbl | \
    awk '$2 != "x32" && $3 == "'$1'" { print $1 }'
};

When the BPF filter rejects a system call (case [2] above, iykyk), it makes the system call fail with the error number you gave it on the command line. In these experiments we're using error number 99:

$ errno 99;
EADDRNOTAVAIL 99 Cannot assign requested address

Next up: we try to run whoami(1), but the BPF filter rejects the execve(2) system call, so the command literally never even executes. no cap ๐Ÿ˜ญ

$ syscall_nr execve;
59
$ ./a.out;
Usage: ./a.out <syscall_nr> <arch> <errno> <prog> [<args>]
Hint for <arch>: AUDIT_ARCH_I386: 0x40000003
                 AUDIT_ARCH_X86_64: 0xC000003E
$ ./a.out 59 0xC000003E 99 /bin/whoami;
execv: Cannot assign requested address

Now the BPF filter rejects the write(2) system call, so even though whoami(1) gets started successfully, it simply cannot write output. it's giving "speak up bestie" but muted ๐Ÿ’€

$ syscall_nr write;
1
$ ./a.out 1 0xC000003E 99 /bin/whoami;

And finally, the BPF filter rejects a system call that whoami(1) doesn't even use, so it executes fine and produces its output. main character behavior tbh ๐Ÿ‘‘

$ syscall_nr preadv;
295
$ ./a.out 295 0xC000003E 99 /bin/whoami;
cecilia

๐ŸŽ€ Program source

here's the whole thing, verbatim, character for character โ€” do not let me catch you retyping this by hand:

#include <linux/audit.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <stdcountof.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>

#define X32_SYSCALL_BIT 0x40000000

static int
install_filter(int syscall_nr, unsigned int t_arch, int f_errno)
{
    unsigned int upper_nr_limit = 0xffffffff;

    /* Assume that AUDIT_ARCH_X86_64 means the normal x86-64 ABI
       (in the x32 ABI, all system calls have bit 30 set in the
       'nr' field, meaning the numbers are >= X32_SYSCALL_BIT).  */
    if (t_arch == AUDIT_ARCH_X86_64)
        upper_nr_limit = X32_SYSCALL_BIT - 1;

    struct sock_filter filter[] = {
        /* [0] Load architecture from 'seccomp_data' buffer into
               accumulator.  */
        BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
                 (offsetof(struct seccomp_data, arch))),

        /* [1] Jump forward 5 instructions if architecture does not
               match 't_arch'.  */
        BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 5),

        /* [2] Load system call number from 'seccomp_data' buffer into
               accumulator.  */
        BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
                 (offsetof(struct seccomp_data, nr))),

        /* [3] Check ABI - only needed for x86-64 in deny-list use
               cases.  Use BPF_JGT instead of checking against the bit
               mask to avoid having to reload the syscall number.  */
        BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, upper_nr_limit, 3, 0),

        /* [4] Jump forward 1 instruction if system call number
               does not match 'syscall_nr'.  */
        BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, syscall_nr, 0, 1),

        /* [5] Matching architecture and system call: don't execute
           the system call, and return 'f_errno' in 'errno'.  */
        BPF_STMT(BPF_RET | BPF_K,
                 SECCOMP_RET_ERRNO | (f_errno & SECCOMP_RET_DATA)),

        /* [6] Destination of system call number mismatch: allow other
               system calls.  */
        BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW),

        /* [7] Destination of architecture mismatch: kill process.  */
        BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL_PROCESS),
    };

    struct sock_fprog prog = {
        .len = countof(filter),
        .filter = filter,
    };

    if (syscall(SYS_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog)) {
        perror("seccomp");
        return 1;
    }

    return 0;
}

int
main(int argc, char *argv[])
{
    if (argc < 5) {
        fprintf(stderr, "Usage: "
                "%s <syscall_nr> <arch> <errno> <prog> [<args>]\n"
                "Hint for <arch>: AUDIT_ARCH_I386: 0x%X\n"
                "                 AUDIT_ARCH_X86_64: 0x%X\n"
                "\n", argv[0], AUDIT_ARCH_I386, AUDIT_ARCH_X86_64);
        exit(EXIT_FAILURE);
    }

    if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
        perror("prctl");
        exit(EXIT_FAILURE);
    }

    if (install_filter(strtol(argv[1], NULL, 0),
                       strtoul(argv[2], NULL, 0),
                       strtol(argv[3], NULL, 0)))
        exit(EXIT_FAILURE);

    execv(argv[4], &argv[4]);
    perror("execv");
    exit(EXIT_FAILURE);
}

๐ŸŒˆ SEE ALSO ๐ŸŒˆ

go read these next bestie, it's a whole syllabus:

Various pages from the libseccomp library, including: scmp_sys_resolver(1), seccomp_export_bpf(3), seccomp_init(3), seccomp_load(3), and seccomp_rule_add(3). ๐Ÿ’–

The kernel source files Documentation/networking/filter.rst and Documentation/userspace-api/seccomp_filter.rst.

McCanne, S. and Jacobson, V. (1992) The BSD Packet Filter: A New Architecture for User-level Packet Capture, Proceedings of the USENIX Winter 1993 Conference http://www.tcpdump.org/papers/bpf-usenix93.pdf

Terence Kelly and Edison Fuh (2025) Sandboxing: Foolproof Boundaries vs. Unbounded Foolishness https://dl.acm.org/doi/pdf/10.1145/3733699


and that's the whole page, babes โœจ this was Linux man-pages 6.20, dated 2026-06-05, and honestly? this seccomp(2) page ate and left no crumbs ๐Ÿ‘‘ go sandbox something responsibly, mwah ๐Ÿซถ๐Ÿ’œ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms