Skip to content

Lesson 5 of 13 · 12m

The securityContext Fields That Matter

runAsNonRoot, allowPrivilegeEscalation, drop ALL capabilities, readOnlyRootFilesystem, and RuntimeDefault seccomp — what each field actually blocks.

Five fields carry the whole load

securityContext has maybe thirty knobs across pod and container level. Five of them decide whether a container escape is plausible or a punchline:

securityContext:
  runAsNonRoot: true
  allowPrivilegeEscalation: false
  readOnlyRootFilesystem: true
  capabilities:
    drop: ["ALL"]
  seccompProfile:
    type: RuntimeDefault

Each one closes a different door. None of them is decorative.

runAsNonRoot: true tells the kubelet to refuse starting a container that would resolve to UID 0. It checks the image's USER (which must be numeric — USER app with no passwd entry fails open to root on some runtimes) and any explicit runAsUser. The failure mode you will actually see: Error: container has runAsNonRoot and image will run as root. Fix it in the image (USER 1000) or pin runAsUser: 1000 in the pod. Note the asymmetry — this is a kubelet check, not an admission check, so it fails at pod start, not at deploy apply.

allowPrivilegeEscalation: false sets the kernel's no_new_privs bit: no process in the container can gain more privilege than it started with, no matter what setuid binaries or capability games it plays. Without it, a setuid-root binary — or a kernel bug reachable from inside — can raise effective privilege even when runAsNonRoot held.

capabilities.drop: ["ALL"] strips the Linux capability set. Capabilities are real kernel powers, not config metadata: CAP_NET_ADMIN rewrites iptables and reroutes traffic, CAP_SYS_ADMIN is root-by-another-name, CAP_DAC_READ_SEARCH reads files ignoring permission bits, CAP_SYS_PTRACE inspects any process's memory. The restricted standard's pattern is drop-everything then re-add NET_BIND_SERVICE only if you must bind :80.

readOnlyRootFilesystem: true makes the image's filesystem immutable — an attacker can't write a poisoned binary, drop a webshell, or modify app config on disk. The cost: anything the app writes (/tmp, caches) needs an explicit emptyDir or ephemeral volume mount:

volumes:
  - name: scratch
    emptyDir: {}

emptyDir is per-pod, node-disk (or memory-backed via medium: Memory), and destroyed with the pod — the right kind of scratch space. Apps that silently write to their own rootfs are the common breakage; fix them by giving scratch space, not by turning this off. Distroless images pair naturally with the whole posture: no shell, no package manager, nothing for an attacker to apt install their toolkit with.

seccompProfile.type: RuntimeDefault applies the container runtime's default syscall filter — containerd and CRI-O ship a profile built on Docker's, which allows the ~300 syscalls ordinary processes use and blocks the escape-primitive set: mount, kexec_load, open_by_handle_at, init_module, ptrace, keyctl-family abuse. Custom Localhost profiles exist for workloads that need tighter (loaded from files on the node's filesystem), but RuntimeDefault is free: it almost never breaks an application, which is why the restricted standard requires seccomp at all.

The privileged: true confession

One flag undoes all of it. privileged: true grants all capabilities, all host devices, disables seccomp, and drops device cgroup limits. A privileged pod is root on the node wearing a container costume — it can read /dev/sda, load kernel modules, and see every other container's traffic. Pod Security's baseline level exists largely to make this flag rejectable.

The evidence for why this matters: Siloscape, disclosed by Unit 42 in June 2021, was the first malware built specifically for Kubernetes — it compromised Windows containers, escaped to the node, and opened a backdoor into the cluster. Its escape path ran through exactly the privileges this lesson's five fields remove: host access, elevated caps, writable everything.

The practice

Ship the five-field block as your org's pod spec default — template it, lint it, enforce restricted PSA where you control the images. Each field has a stated cost (scratch volumes, fixed USER, DNS port juggling); pay those. They're configuration costs, not risk costs.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.