Custom Seccomp Profiles in Docker: Block Risky Syscalls and Stop Container Escapes

Security tutorial - IT technology blog
Security tutorial - IT technology blog

Context & Why

At 2:15 AM, our central SIEM flagged an alert: an anomalous process had executed inside a public-facing Node.js container. An attacker had chained a remote code execution bug in an unpatched dependency to spawn a reverse shell. But they didn't stop there. Within 90 seconds, they pulled down a compiled C exploit targeting a known kernel vulnerability (CVE-2022-0185), aiming to abuse unshare() and user namespaces to break out onto the host node.

The exploit failed immediately with EPERM (Operation not permitted). We got lucky—because we had planned for it. Dropping Linux capabilities like CAP_SYS_ADMIN and running as a non-root user (UID 10001) are solid first steps. Still, they aren't enough on their own. Every container on a host shares the exact same Linux kernel.

When an intruder compromises an application, their shortest path to host root runs through kernel system calls. Modern 64-bit Linux kernels expose over 450 distinct syscalls. By default, Docker applies a built-in seccomp filter that blocks roughly 60 of the most hazardous ones. The problem? That default filter prioritizes broad backward compatibility over strict isolation.

As a result, Docker's baseline profile leaves syscalls like keyctl, bpf, and ptrace available to workloads that never need them. A stateless API or static Nginx proxy has no business probing kernel tracing interfaces or manipulating kernel rings. Writing a custom seccomp profile closes this gap, blocking attack primitives before an exploit payload can execute.

Installation & Prerequisites

Modern enterprise distributions—including Ubuntu 22.04/24.04, Debian 12, and RHEL 9—ship with seccomp enabled by default. Before building custom rules, confirm that your host kernel and Docker engine have seccomp support active.

Run this check against your active kernel config:

grep -i seccomp /boot/config-$(uname -r)

You should see both flags compiled in:

CONFIG_SECCOMP=y
CONFIG_SECCOMP_FILTER=y

Next, ensure the Docker daemon detects host-level seccomp support:

docker info --format '{{json .SecurityOptions}}'

The output must include name=seccomp and specify the builtin profile:

["name=seccomp,profile=builtin","name=cgroupns"]

To profile your application's system calls during staging runs, install strace and the audit utilities on your test host:

# Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y strace auditd libseccomp-dev

# RHEL / Rocky Linux / AlmaLinux 9
sudo dnf install -y strace audit libseccomp-devel

Configuration

A Docker seccomp profile is a JSON file structured around three main fields: defaultAction, targeted CPU architectures, and an array of granular syscalls rules.

Writing an allowlist containing hundreds of calls from scratch is prone to errors. A better approach takes two steps: profile your container under synthetic load, then craft an enforced policy based on real data.

Step 1: Trace Application Syscalls in Staging

Spin up your container with strace attached, mount a local directory for logs, and drive typical traffic through it. For example, trace an Nginx container while sending 5,000 requests using an HTTP benchmarking tool:

docker run --rm -it \
  --cap-add SYS_PTRACE \
  -v $(pwd)/trace-logs:/logs \
  --entrypoint strace \
  nginx:1.27-alpine -f -o /logs/nginx-trace.log nginx -g 'daemon off;'

Hit the service with health checks, file uploads, and edge-case requests. Once you stop the container, extract the list of invoked syscalls from the raw log:

awk -F'(' '{print $1}' trace-logs/nginx-trace.log | tr -d ' ' | sort -u | grep -E '^[a-z0-9_]+$'

For an Nginx web proxy serving static assets, the output usually hovers around 40 to 55 syscalls, including epoll_wait, newfstatat, accept4, and writev.

Step 2: Generate the Hardened Seccomp Profile

Grab Docker's default seccomp configuration to examine its syntax:

curl -sSL -o default-seccomp.json \
  https://raw.githubusercontent.com/moby/moby/master/profiles/seccomp/default.json

Create your custom file at /etc/docker/seccomp-hardened.json. We will set defaultAction to SCMP_ACT_ERRNO, returning an explicit EPERM (errno code 1). Legitimate application calls are explicitly allowed. Dangerous calls frequently used in container breakouts—such as unshare, ptrace, and bpf—are set to SCMP_ACT_KILL_PROCESS to immediately terminate rogue execution threads:

{
  "defaultAction": "SCMP_ACT_ERRNO",
  "defaultErrnoRet": 1,
  "architectures": [
    "SCMP_ARCH_X86_64",
    "SCMP_ARCH_X86",
    "SCMP_ARCH_AARCH64"
  ],
  "syscalls": [
    {
      "names": [
        "accept4",
        "access",
        "bind",
        "close",
        "connect",
        "epoll_create1",
        "epoll_ctl",
        "epoll_pwait",
        "epoll_wait",
        "exit",
        "exit_group",
        "fcntl",
        "fstat",
        "futex",
        "getpid",
        "getuid",
        "ioctl",
        "listen",
        "mmap",
        "mprotect",
        "munmap",
        "nanosleep",
        "newfstatat",
        "openat",
        "poll",
        "read",
        "recvfrom",
        "restart_syscall",
        "rt_sigaction",
        "rt_sigprocmask",
        "rt_sigreturn",
        "sched_yield",
        "sendto",
        "set_robust_list",
        "setsockopt",
        "socket",
        "write",
        "writev"
      ],
      "action": "SCMP_ACT_ALLOW"
    },
    {
      "names": [
        "bpf",
        "keyctl",
        "mount",
        "pivot_root",
        "ptrace",
        "reboot",
        "swapon",
        "swapoff",
        "umount2",
        "unshare"
      ],
      "action": "SCMP_ACT_KILL_PROCESS"
    }
  ]
}

Step 3: Attach the Profile to Your Workloads

Apply the profile via the Docker CLI using the --security-opt argument:

docker run -d \
  --name hardened-nginx \
  --security-opt seccomp=/etc/docker/seccomp-hardened.json \
  --security-opt no-new-privileges:true \
  -p 8080:80 \
  nginx:1.27-alpine

For services defined in Docker Compose, add the path directly to your docker-compose.yml file:

services:
  api:
    image: node:20-alpine
    restart: unless-stopped
    ports:
      - "3000:3000"
    security_opt:
      - seccomp:/etc/docker/seccomp-hardened.json
      - no-new-privileges:true

Verification & Troubleshooting

Applying strict syscall rules straight to production without auditing can break applications unexpectedly. Always test that legitimate paths work while forbidden calls get intercepted.

Step 1: Test with Prohibited Syscalls

Run a disposable test container with your profile and attempt to call unshare, which is set to kill unauthorized processes:

docker run --rm -it \
  --security-opt seccomp=/etc/docker/seccomp-hardened.json \
  alpine:3.20 unshare --user /bin/sh

Because the filter matches SCMP_ACT_KILL_PROCESS, the kernel drops the hammer immediately with a SIGSYS signal:

Bad system call (core dumped)

The offending process was terminated before it could configure a new user namespace. The exploit stopped dead in its tracks.

Step 2: Safe Auditing with SCMP_ACT_LOG

Worried that you missed an obscure syscall needed by a background cron job? Change defaultAction to SCMP_ACT_LOG for a week in your staging environment:

{
  "defaultAction": "SCMP_ACT_LOG",
  "architectures": [
    "SCMP_ARCH_X86_64"
  ],
  "syscalls": [
    ...
  ]
}

In audit mode, unlisted calls succeed, but the kernel logs every violation. You can catch missing dependencies without causing production downtime.

Step 3: Track Kernel Violation Logs

Use journalctl or dmesg on the host to monitor seccomp audit notices in real time:

sudo journalctl -k -g "SECCOMP" --follow

When an unauthorized call triggers, an audit entry appears with the process name and raw syscall identifier:

audit: type=1326 audit(1701234567.890:42): auid=4294967295 uid=10001 gid=10001 ses=4294967295 \
  pid=45231 comm="exploit" exe="/tmp/exploit" sig=31 arch=c000003e syscall=272 compat=0

Look at syscall=272. Translate that number back into a human-readable name using ausyscall:

ausyscall x86_64 272

Output:

unshare

If the blocked call is an authorized operational script, add it to your profile's allowlist. If it is an unexpected call, you have just stopped an exploit from reaching your host kernel.

Share: