HackTricks
Search…
Pentesting
Seccomp

Basic Information

Seccomp or Secure Computing mode, in summary, is a feature of Linux kernel which can act as syscall filter. Seccomp has 2 modes.
seccomp (short for secure computing mode) is a computer security facility in the Linux kernel. seccomp allows a process to make a one-way transition into a "secure" state where it cannot make any system calls except exit(), sigreturn(), read() and write() to already-open file descriptors. Should it attempt any other system calls, the kernel will terminate the process with SIGKILL or SIGSYS. In this sense, it does not virtualize the system's resources but isolates the process from them entirely.
seccomp mode is enabled via the prctl(2) system call using the PR_SET_SECCOMP argument, or (since Linux kernel 3.17) via the seccomp(2) system call. seccomp mode used to be enabled by writing to a file, /proc/self/seccomp, but this method was removed in favor of prctl(). In some kernel versions, seccomp disables the RDTSC x86 instruction, which returns the number of elapsed processor cycles since power-on, used for high-precision timing.
seccomp-bpf is an extension to seccomp that allows filtering of system calls using a configurable policy implemented using Berkeley Packet Filter rules. It is used by OpenSSH and vsftpd as well as the Google Chrome/Chromium web browsers on Chrome OS and Linux. (In this regard seccomp-bpf achieves similar functionality, but with more flexibility and higher performance, to the older systrace—which seems to be no longer supported for Linux.)

Original/Strict Mode

In this mode Seccomp only allow the syscalls exit(), sigreturn(), read() and write() to already-open file descriptors. If any other syscall is made, the process is killed using SIGKILL
seccomp_strict.c
1
#include <fcntl.h>
2
#include <stdio.h>
3
#include <unistd.h>
4
#include <string.h>
5
#include <linux/seccomp.h>
6
#include <sys/prctl.h>
7
8
//From https://sysdig.com/blog/selinux-seccomp-falco-technical-discussion/
9
//gcc seccomp_strict.c -o seccomp_strict
10
11
int main(int argc, char **argv)
12
{
13
int output = open("output.txt", O_WRONLY);
14
const char *val = "test";
15
16
//enables strict seccomp mode
17
printf("Calling prctl() to set seccomp strict mode...\n");
18
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
19
20
//This is allowed as the file was already opened
21
printf("Writing to an already open file...\n");
22
write(output, val, strlen(val)+1);
23
24
//This isn't allowed
25
printf("Trying to open file for reading...\n");
26
int input = open("output.txt", O_RDONLY);
27
28
printf("You will not see this message--the process will be killed first\n");
29
}
Copied!

Seccomp-bpf

This mode allows filtering of system calls using a configurable policy implemented using Berkeley Packet Filter rules.
seccomp_bpf.c
1
#include <seccomp.h>
2
#include <unistd.h>
3
#include <stdio.h>
4
#include <errno.h>
5
6
//https://security.stackexchange.com/questions/168452/how-is-sandboxing-implemented/175373
7
//gcc seccomp_bpf.c -o seccomp_bpf -lseccomp
8
9
void main(void) {
10
/* initialize the libseccomp context */
11
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
12
13
/* allow exiting */
14
printf("Adding rule : Allow exit_group\n");
15
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
16
17
/* allow getting the current pid */
18
//printf("Adding rule : Allow getpid\n");
19
//seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getpid), 0);
20
21
printf("Adding rule : Deny getpid\n");
22
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(getpid), 0);
23
/* allow changing data segment size, as required by glibc */
24
printf("Adding rule : Allow brk\n");
25
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
26
27
/* allow writing up to 512 bytes to fd 1 */
28
printf("Adding rule : Allow write upto 512 bytes to FD 1\n");
29
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 2,
30
SCMP_A0(SCMP_CMP_EQ, 1),
31
SCMP_A2(SCMP_CMP_LE, 512));
32
33
/* if writing to any other fd, return -EBADF */
34
printf("Adding rule : Deny write to any FD except 1 \n");
35
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(write), 1,
36
SCMP_A0(SCMP_CMP_NE, 1));
37
38
/* load and enforce the filters */
39
printf("Load rules and enforce \n");
40
seccomp_load(ctx);
41
seccomp_release(ctx);
42
//Get the getpid is denied, a weird number will be returned like
43
//this process is -9
44
printf("this process is %d\n", getpid());
45
}
Copied!

Seccomp in Docker

Seccomp-bpf is supported by Docker to restrict the syscalls from the containers effectively decreasing the surface area. You can find the syscalls blocked by default in https://docs.docker.com/engine/security/seccomp/ and the default seccomp profile can be found here https://github.com/moby/moby/blob/master/profiles/seccomp/default.json. You can run a docker container with a different seccomp policy with:
1
docker run --rm \
2
-it \
3
--security-opt seccomp=/path/to/seccomp/profile.json \
4
hello-world
Copied!
If you want for example to forbid a container of executing some syscall like uname you could download the default profile from https://github.com/moby/moby/blob/master/profiles/seccomp/default.json and just remove the uname string from the list. If you want to make sure that some binary doesn't work inside a a docker container you could use strace to list the syscalls the binary is using and then forbid them. In the following example the syscalls of uname are discovered:
1
docker run -it --security-opt seccomp=default.json modified-ubuntu strace uname
Copied!
If you are using Docker just to launch an application, you can profile it with strace and just allow the syscalls it needs

Example Seccomp policy

To illustrate Seccomp feature, let’s create a Seccomp profile disabling “chmod” system call as below.
1
{
2
"defaultAction": "SCMP_ACT_ALLOW",
3
"syscalls": [
4
{
5
"name": "chmod",
6
"action": "SCMP_ACT_ERRNO"
7
}
8
]
9
}
Copied!
In the above profile, we have set default action to “allow” and created a black list to disable “chmod”. To be more secure, we can set default action to drop and create a white list to selectively enable system calls. Following output shows the “chmod” call returning error because its disabled in the seccomp profile
1
$ docker run --rm -it --security-opt seccomp:/home/smakam14/seccomp/profile.json busybox chmod 400 /etc/hosts
2
chmod: /etc/hosts: Operation not permitted
Copied!
Following output shows the “docker inspect” displaying the profile:
1
"SecurityOpt": [
2
"seccomp:{\"defaultAction\":\"SCMP_ACT_ALLOW\",\"syscalls\":[{\"name\":\"chmod\",\"action\":\"SCMP_ACT_ERRNO\"}]}"
3
],
Copied!

Deactivate it in Docker

Launch a container with the flag: --security-opt seccomp=unconfined
Also, note that when Docker (or other CRIs) are used in a Kubernetes cluster, the seccomp filter is disabled by default
Last modified 2d ago