#include <linux/seccomp.h>#include <linux/filter.h>#include <linux/audit.h>#include <linux/signal.h>#include <sys/ptrace.h>int seccomp(unsigned int operation, unsigned int flagsvoid *" args );
Currently, Linux supports the following operation values:
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);
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 caller must have the CAP_SYS_ADMIN capability, 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 will fail and return 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 non-zero 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(2) 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:
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.
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 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 */ };
A seccomp filter returns a 32-bit value consisting of two parts: the most significant 16 bits (corresponding to the mask defined by the constant SECCOMP_RET_ACTION) 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 are all executed, in reverse order of their addition to the filter tree (i.e., the most recently installed filter is executed first). The return value for the evaluation of a given system call is the first-seen SECCOMP_RET_ACTION value of highest precedence (along with its accompanying data) returned by execution of all of the filters.
In decreasing order of precedence, the values that may be returned by a seccomp filter are:
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.
The seccomp check will not be run again after the tracer is notified. (This means that 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.)
seccomp() provides a superset of the functionality provided by the prctl(2) PR_SET_SECCOMP operation (which does not support flags).
The remaining command-line arguments specify the pathname and additional arguments of a program that the example program should attempt to execute using execve(3) (a library function that employs the execve(2) system call). Some example runs of the program are shown below.
First, we display the architecture that we are running on (x86-64) and then construct a shell function that looks up system call numbers on this 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), it causes the system call to fail with the error number specified on the command line. In the experiments shown here, we’ll use error number 99:
$ errno 99 EADDRNOTAVAIL 99 Cannot assign requested address
In the following example, we attempt to run the command whoami(1) , but the BPF filter rejects the execve(2) system call, so that the command is not even executed:
$ 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
In the next example, the BPF filter rejects the write(2) system call, so that, although it is successfully started, the whoami(1) command is not able to write output:
$ syscall_nr write 1 $ ./a.out 1 0xC000003E 99 /bin/whoami
In the final example, the BPF filter rejects a system call that is not used by the whoami(1) command, so it is able to successfully execute and produce output:
$ syscall_nr preadv 295 $ ./a.out 295 0xC000003E 99 /bin/whoami cecilia
#include <errno.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <linux/audit.h> #include <linux/filter.h> #include <linux/seccomp.h> #include <sys/prctl.h> static int install_filter(int syscall_nr, int t_arch, int f_errno) { 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 4 instructions if architecture does not match ’t_arch’ */ BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, t_arch, 0, 4), /* [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] 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), /* [4] 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)), /* [5] Destination of system call number mismatch: allow other system calls */ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), /* [6] Destination of architecture mismatch: kill process */ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL), }; struct sock_fprog prog = { .len = (unsigned short) (sizeof(filter) / sizeof(filter[0])), .filter = filter, }; if (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), strtol(argv[2], NULL, 0), strtol(argv[3], NULL, 0))) exit(EXIT_FAILURE); execv(argv[4], &argv[4]); perror("execv"); exit(EXIT_FAILURE); }
The kernel source files Documentation/networking/filter.txt and Documentation/prctl/seccomp_filter.txt.
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