FIELD NOTESKUBERNETES · DEVOPS

Pod Security Admission: a new namespace admits a pod that owns the node

An unlabelled namespace admits a privileged pod that can read the cluster CA key. The labels that stop it, the order to apply them in, and the Deployment that passes CI with no pods.

By Captain Spitt McGee3 min readPublished
ON THIS PAGE
The namespace shop before and after Pod Security labels. Unlabelled, a privileged escape pod mounts the node's root filesystem and reads /etc/kubernetes/pki/ca.key. With enforce=baseline and warn=restricted, Pod Security Admission refuses a new escape pod, a pod admitted earlier keeps running, and a Deployment with a privileged template is accepted while its ReplicaSet logs FailedCreate and no pods start. Drawn in archhy.

A namespace you have just created will admit a pod that mounts the node's root filesystem and runs privileged, and from inside it you can read the cluster's certificate authority key. Kubernetes ships the fix, Pod Security Admission, but every namespace you create starts at its most permissive level, and a label raises it. The labels take one line each. What catches people is what enforcement leaves alone. Pods already running stay running. A Deployment whose pods it refuses still applies cleanly, then never gets any pods.

What the default lets in#

On a fresh kind cluster, in a namespace called shop with no labels beyond its name, this pod is accepted without a word:

YAML
apiVersion: v1
kind: Pod
metadata:
  name: escape
  namespace: shop
spec:
  containers:
  - name: shell
    image: busybox
    command: ["sleep", "3600"]
    securityContext:
      privileged: true
    volumeMounts:
    - name: host
      mountPath: /host
  volumes:
  - name: host
    hostPath:
      path: /
Shell
kubectl -n shop exec escape -- ls /host/etc/kubernetes/pki/ca.key

That prints the path, because the file is there. On a kubeadm-built node that is the key that signs every certificate in the cluster, so anyone who can create a pod in shop can mint themselves an administrator. Managed clusters keep the control plane off your nodes, so the prize there is smaller: the kubelet's credentials and every other pod's secrets on that node. Either way, permission to create a pod is root on a node, and RBAC that says "developers may deploy to shop" has quietly granted it.

The Pod Security Standards define three levels. privileged allows everything, and it is what an unlabelled namespace gets. baseline blocks the known escalations: privileged containers, host namespaces, hostPath volumes, added capabilities. restricted also requires a non-root user, all capabilities dropped, privilege escalation disabled and a seccomp profile. Pod Security Admission applies a level in three modes: enforce rejects the pod, warn prints a warning to whoever applied it, and audit records it in the audit log.

Ask before you enforce#

A server-side dry run of the label lists every running pod that would break it, and changes nothing:

Shell
kubectl label --dry-run=server --overwrite ns shop \
  pod-security.kubernetes.io/enforce=restricted

With the escape pod and a plain kubectl create deployment web --image=nginx running, both fail restricted. The stock nginx pod fails on four counts: privilege escalation, capabilities, running as root and seccomp. Try baseline and only the escape pod is named. Most real workloads pass baseline and fail restricted, because their manifests never set a security context and their images run as root.

So enforce baseline now and warn on restricted, which blocks the escapes and tells every deployer what the stricter level would refuse:

Shell
kubectl label --overwrite ns shop \
  pod-security.kubernetes.io/enforce=baseline \
  pod-security.kubernetes.io/warn=restricted

Where enforcement fails quietly#

The label is not retroactive. The escape pod was still running after enforce=baseline went on, and the label command's only protest was a warning. Enforcement happens at admission, so a pod already admitted stays until something deletes it. Re-create it and it is refused, naming the hostPath volume and the privileged flag.

And enforce applies to pods, not to the Deployments that make them. A Deployment with a privileged template is accepted, kubectl apply prints a warning and exits zero, and the Deployment sits at 0/1 with no pods. The refusal is on the ReplicaSet, where nobody looks:

Shell
kubectl -n shop get events --field-selector reason=FailedCreate

A pipeline that checks the exit code of kubectl apply reports success. Before you enforce anything, make it wait on kubectl rollout status deployment/web --timeout=2m, which exits non-zero when the pods never arrive. Otherwise the first blocked release passes CI and is noticed when someone asks why the old version is still serving.

The diagram#

The same namespace before and after the label, with the escape pod, the pod that outlives the label and the Deployment that never gets pods: open it in archhy and make a copy to mark up your own namespaces.

Getting to restricted#

A workload passes restricted with four settings and an image that does not need root:

YAML
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
  - name: web
    image: nginxinc/nginx-unprivileged
    ports:
    - containerPort: 8080
    securityContext:
      allowPrivilegeEscalation: false
      capabilities:
        drop: ["ALL"]

The image matters as much as the YAML: stock nginx runs as root and binds port 80, so it cannot run under runAsNonRoot, and the unprivileged build listens on 8080 instead. Move namespaces to enforce=restricted one at a time. Warnings only fire when someone applies, so silence proves nothing: a namespace is ready when the server-side dry run of enforce=restricted above names no pods in it.

Labels protect only the namespaces someone remembered to label, and the one nobody labelled is where the escape pod will run. If you control the API server, as on a kubeadm, K3s or kind cluster, set a cluster-wide default in the admission configuration, so an unlabelled namespace gets baseline rather than privileged. On a managed cluster you usually cannot, so make the labels part of whatever creates namespaces, and treat a namespace without them as a finding.