Sub topic 7
Using the default profile and writing a custom one
This chapter assumes you can run containers with docker run, read JSON and simple Kubernetes YAML, and know that a system call is how a process asks the kernel to do something. The previous chapter showed that seccomp filters system calls. Here you will see which profile you already get, how to write your own, and how to apply it.
The default profile
Docker applies a default seccomp profile to every container unless you turn it off. It is an allowlist. It permits most of the roughly 300 system calls Linux offers and blocks a few dozen that ordinary applications rarely need and that expose a lot of kernel attack surface. Examples are kexec_load, reboot, mount (without CAP_SYS_ADMIN), and keyctl.
You get this protection with no flags. You lose it only if you pass --security-opt seccomp=unconfined, so search your scripts and Compose files for that string.
Kubernetes behaves differently. Unless the cluster enables the kubelet's SeccompDefault option, pods run with no seccomp filter at all. You must ask for the runtime's default profile with seccompProfile: {type: RuntimeDefault}, as the hardened pod manifest in this tutorial does.
Writing a custom profile
A custom profile is a JSON file with two main parts:
defaultActionis what happens to any system call not listed.syscallsis a list of rules. Each rule names system calls and gives the action to take on them.
The two actions you will use most are SCMP_ACT_ALLOW and SCMP_ACT_ERRNO. The second one makes the call fail with an error (by default EPERM) instead of killing the process.
This profile allows everything except mkdir and mkdirat. Save it as deny-mkdir.json.
{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"names": ["mkdir", "mkdirat"],
"action": "SCMP_ACT_ERRNO"
}
]
}Pass it with --security-opt:
docker run --rm \
--security-opt seccomp=./deny-mkdir.json \
alpine mkdir /tmp/testThe command fails with Operation not permitted. The name mkdirat is in the list because modern C libraries often call it instead of mkdir. Blocking only one of the two would leave a way around the rule.
flowchart TDReplace that placeholder with the diagram below; the chapter uses this one:
Allowlist or denylist
The profile above is a denylist. Its defaultAction allows everything, so it is weaker than Docker's default profile, which already blocks many calls. Passing seccomp=./deny-mkdir.json replaces the default profile. It does not add to it.
The stricter design is an allowlist: set defaultAction to SCMP_ACT_ERRNO and list only the calls your application needs. This gives the smallest attack surface, but you must find every call the program makes, including those from libraries and the runtime.
| Approach | `defaultAction` | Strength | Cost |
|---|---|---|---|
| Docker default | Allow, with a built-in block list | Good, works for most apps | Allows more than a given app needs |
| Custom denylist | `SCMP_ACT_ALLOW` | Blocks only what you name | Weaker than the default if you start from scratch |
| Custom allowlist | `SCMP_ACT_ERRNO` | Strongest | Breaks easily until the list is complete |
Common errors
- Relative paths. Docker reads the profile on the client, so
./deny-mkdir.jsonresolves relative to where you run the command. A wrong path produces a file-not-found error. - Invalid JSON. A trailing comma makes the container fail to start. Check the file with
python3 -m json.tool deny-mkdir.json. - Missing sibling calls. Blocking
mkdirbut notmkdiratleaves the block incomplete. - Kubernetes custom profiles. Use
type: LocalhostwithlocalhostProfileset to a path relative to the kubelet's seccomp directory, usually/var/lib/kubelet/seccomp. The file must exist on every node that can run the pod.
Exercise
You run a container with --security-opt seccomp=./my.json, where my.json has defaultAction set to SCMP_ACT_ALLOW and blocks only mkdir and mkdirat. Is this container more or less restricted than one started with no seccomp option, and why?
Show answer
Less restricted. The profile replaces Docker's default profile, so the calls the default blocks, such as keyctl or kexec_load, are now allowed. Only mkdir and mkdirat are newly blocked.