Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Rootless and Security

SELinux, Seccomp and Capabilities in Podman

Harden containers with the layers Podman uses: Linux capabilities, seccomp profiles, SELinux labels with :z and :Z, no-new-privileges, read-only roots and --privileged.

A container is not a virtual machine: it shares the host kernel. What keeps it contained is a stack of kernel features that each restrict something. Rootless mode and user namespaces (the previous lessons) are the first layer. This lesson covers the other layers Podman applies by default and how to adjust them: Linux capabilities, seccomp filters, SELinux (or AppArmor) labels, and a few simple options such as --read-only and no-new-privileges.

The guiding rule is least privilege: the defaults are sensible, and your job is usually to remove more, not add. Every time you are tempted to write --privileged, find the one thing that is actually needed.

Capabilities: splitting up root

Traditionally root can do everything. Linux splits that power into about 40 capabilities, such as binding low ports (NET_BIND_SERVICE), changing file owners (CHOWN), loading kernel modules (SYS_MODULE) or changing the system time (SYS_TIME). A container starts with a reduced default set of roughly a dozen of them (CHOWN, DAC_OVERRIDE, FOWNER, FSETID, KILL, NET_BIND_SERVICE, SETFCAP, SETGID, SETPCAP, SETUID, SYS_CHROOT and a few more); dangerous ones like SYS_ADMIN and SYS_MODULE are not included.

Terminal
podman run --rm docker.io/library/alpine:latest sh -c 'grep Cap /proc/self/status'
podman run --rm --cap-drop=all --cap-add=NET_BIND_SERVICE docker.io/library/nginx:alpine nginx -t

The first command prints the capability bit masks of the process. Use capsh --decode=MASK (if installed) to read them. Options:

Option Effect
--cap-drop=NAME remove one capability
--cap-drop=all remove all of them
--cap-add=NAME add one
--privileged give all capabilities, disable seccomp and SELinux confinement, expose host devices

A good pattern is --cap-drop=all first, then add only what the program proves it needs. In rootless mode, capabilities apply inside the user namespace: the container's root can do privileged things to things the namespace owns, but nothing to the host itself, so a rootless --privileged is much less dangerous than a rootful one (although still wide open for what your user can do).

Here is a stand-in that applies add/drop rules to a default set, in the order Podman does: drop first, then add. It is JavaScript with a shortened default list.

JS Stand-in: effective capability set

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Seccomp: filtering system calls

Programs ask the kernel for things through system calls. A seccomp profile is a list of calls that are allowed, blocked or answered with an error. Podman applies a default profile (shipped in /usr/share/containers/seccomp.json) that blocks calls a normal application never needs, such as reboot, kexec_load or many rarely used ones that have been exploited in the past. If a program is blocked, it usually fails with "Operation not permitted" or is killed with a "bad system call" signal (SIGSYS).

Terminal
podman run --rm --security-opt seccomp=./my-profile.json IMAGE
podman run --rm --security-opt seccomp=unconfined IMAGE      # no filtering: for debugging only

A tiny custom profile in JSON looks like this:

JSON
{
  "defaultAction": "SCMP_ACT_ERRNO",
  "architectures": ["SCMP_ARCH_X86_64"],
  "syscalls": [
    { "names": ["read", "write", "exit", "exit_group", "brk", "mmap", "openat", "close", "fstat"], "action": "SCMP_ACT_ALLOW" }
  ]
}

That allow-list is far too small for a real program, but shows the structure: a default action, and rules with names and actions. Start from the default profile and remove calls, rather than writing one from nothing; tools like strace -c show which calls your program makes.

SELinux: labels on everything

On Fedora, RHEL and similar systems, SELinux adds mandatory access control. Every process and file has a label. A container's processes run with the type container_t, which may touch only files labelled container_file_t. That protects the host even if a container escapes into the file system, because your home directory and system files have different labels.

The catch is when you share a host folder with a container (a bind mount): its files carry your home directory's label, which the container may not read. The mount options :z and :Z ask Podman to relabel the folder:

Option Label applied Use
:z shared container_file_t several containers use the same folder
:Z private label with a unique category pair for this container one container only
Terminal
podman run --rm -v ./data:/data:Z docker.io/library/alpine:latest touch /data/ok
ls -Z ./data

ls -Z prints the SELinux label of each file. A volume that fails with "Permission denied" even though the owner looks right is the classic sign of a missing :z/:Z.

Never use :z or :Z on system folders such as /home, /etc or /usr. Podman relabels the whole tree recursively, which can break the system. Use a dedicated folder for container data.

Other SELinux options:

Terminal
podman run --security-opt label=disable IMAGE         # turn off SELinux separation for this container
podman run --security-opt label=type:container_t IMAGE

Disabling is a last resort. Debian and Ubuntu use AppArmor instead; Podman applies a default profile there, adjustable with --security-opt apparmor=PROFILE (or unconfined).

Small switches that help a lot

Terminal
podman run --rm \
  --read-only --tmpfs /tmp \
  --security-opt no-new-privileges \
  --cap-drop=all \
  --user 10001 \
  docker.io/library/alpine:latest id
  • --read-only: the root file system cannot be modified; give writable paths through --tmpfs or volumes.
  • no-new-privileges: even setuid programs cannot gain more rights than their parent. This blocks a class of local privilege escalations.
  • --user: run as a non-root user inside the container.
  • --pids-limit and --memory: stop resource exhaustion (see run options).

Which layer stops what?

flowchart TB
    A["Program in the container"] --> B["User namespace: no real root on the host"]
    B --> C["Capabilities: only some root powers"]
    C --> D["Seccomp: only some system calls"]
    D --> E["SELinux or AppArmor: only some files and objects"]
    E --> F["Host kernel and files"]

Each layer works even if another is misconfigured. Do not disable one without a clear reason.

Debugging a permission error

  1. Run as the same user without the container: does it work? If yes, the container is the difference.
  2. ls -Z and sudo ausearch -m avc -ts recent (or journalctl -t setroubleshoot) show SELinux denials.
  3. Try :Z on the bind mount. If that fixes it, the cause was labels.
  4. Try --cap-add of the specific capability named in the error, not --privileged.
  5. Run with --security-opt seccomp=unconfined only to test whether seccomp is blocking; then write a minimal profile.
JS Stand-in: seccomp-style rule evaluation

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does --cap-drop=all --cap-add=NET_BIND_SERVICE do?

    Dropping all and adding back what is needed is the least-privilege pattern.
  2. 2. What is the difference between :z and :Z on a bind mount?

    Both relabel the host folder for SELinux. Lowercase gives a shared label, uppercase a private one.
  3. 3. A program fails with 'bad system call'. Which layer is probably involved?

    Seccomp blocks system calls outside its profile, and a blocked call can end the process with SIGSYS.
  4. 4. Why is :Z on /home a bad idea?

    Relabeling changes the SELinux labels of every file below the folder. Use a dedicated data folder.
  5. 5. What does no-new-privileges prevent?

    It sets a kernel flag so that execve cannot grant more rights than the process already has.

Summary

  • Containers share the host kernel; capabilities, seccomp and SELinux/AppArmor restrict what they can do, on top of user namespaces.
  • Start from --cap-drop=all and add only what is needed; avoid --privileged.
  • Seccomp filters system calls; the default profile blocks dangerous ones, and unconfined is for tests only.
  • On SELinux hosts, bind mounts need :z or :Z; never apply them to system folders.
  • --read-only, --tmpfs, no-new-privileges and a non-root --user are cheap, effective hardening.

Next: Secrets and Image Trust