Skip to content
Hands-on IT IT Drills

Day 14: Case Study CVE-2021-25741 – subPath Escape

Goal: You triage a CVE against your version and add a policy as an extra protective wall. · Duration: approx. 55 min · Prerequisite: Day 13

Briefing

CVE-2021-25741 was a real container escape: Using volumeMounts.subPath with a symbolic link, you could mount a file of the node into the container – read it and overwrite it, without privileged or hostPath. It was fixed in the kubelet (among others in 1.22.1, 1.21.5, 1.20.11, 1.19.16). Today you learn two things: triage – am I affected at all? – and, as defense in depth, switching off subPath with a policy.

Drill 1: Triage – am I affected?

The first question for any CVE is the version. Read it and compare it against the fixed versions:

kubectl version -o json | python3 -c '
import sys,json,re
v=json.load(sys.stdin)["serverVersion"]["gitVersion"]
m=re.match(r"v(\d+)\.(\d+)", v)
maj,mi=int(m.group(1)),int(m.group(2))
fixed = (maj,mi) >= (1,22)   # CVE-2021-25741 u.a. in 1.22.1 behoben
print(f"Cluster: {v}")
print("CVE-2021-25741:", "gepatcht" if fixed else "BETROFFEN - upgraden!")'

Expected:

Cluster: v1.36.6+rke2r1
CVE-2021-25741: gepatcht

Our cluster is patched – the code fix in the kubelet closes the actual hole. But “patched” does not mean “subPath cannot be abused”: New kubelet bugs with the same pattern keep showing up. That is why you add the second protective wall.

Save your progress? Sign up for free

Drill 2: Defense in depth – forbid subPath

Most workloads do not need subPath. Forbid it, and this entire class of bugs is ruled out:

kubectl create namespace cve2

kubectl apply -f - <<'EOF'
apiVersion: policies.kyverno.io/v1
kind: ValidatingPolicy
metadata: {name: no-subpath}
spec:
  validationActions: ["Deny"]
  matchConstraints:
    resourceRules:
    - apiGroups: [""]
      apiVersions: ["v1"]
      operations: ["CREATE","UPDATE"]
      resources: ["pods"]
  matchConditions:
  - name: only-cve2
    expression: "request.namespace == 'cve2'"
  validations:
  - expression: "object.spec.containers.all(c, !has(c.volumeMounts) ||
      c.volumeMounts.all(m, !has(m.subPath) && !has(m.subPathExpr)))"
    message: "volumeMounts.subPath ist verboten (CVE-2021-25741)"
EOF

Save your progress? Sign up for free

Drill 3: Proof – subPath is rejected

kubectl -n cve2 apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: sp, namespace: cve2}
spec:
  containers:
  - name: c
    image: docker.io/library/busybox:1.36
    command: ["sleep","infinity"]
    volumeMounts: [{name: v, mountPath: /data, subPath: sub}]
  volumes: [{name: v, emptyDir: {}}]
EOF

Expected:

Error from server: error when creating "STDIN": admission webhook
"vpol.validate.kyverno.svc-...-no-subpath" denied the request:
Policy no-subpath failed: volumeMounts.subPath ist verboten (CVE-2021-25741)

A pod without subPath runs normally:

kubectl -n cve2 run ok --image=docker.io/library/busybox:1.36 --command -- sleep infinity
kubectl -n cve2 get pod ok -o jsonpath='{.status.phase}'; echo

Expected: Pending/Running

Clean up:

kubectl delete validatingpolicy no-subpath
kubectl delete namespace cve2

Save your progress? Sign up for free

Checkpoint

Your triage routine works – it reports the version and a verdict:

kubectl version -o json | python3 -c '
import sys,json,re
v=json.load(sys.stdin)["serverVersion"]["gitVersion"]
maj,mi=map(int,re.match(r"v(\d+)\.(\d+)",v).groups())
print(v, "->", "gepatcht" if (maj,mi)>=(1,22) else "BETROFFEN")'

Expected: v1.36.6+rke2r1 -> gepatcht

Challenge

Manual triage does not scale. There are tools that check the cluster against the CIS Benchmark and known CVEs. Name two and say what each one covers.

Hint: One checks the configuration of the components (CIS), the other scans images and cluster resources for known vulnerabilities.

Solution

  • kube-bench checks nodes and components against the CIS Kubernetes Benchmark (API server flags, kubelet config, file permissions) – it tells you whether the hardening is in place.
  • Trivy (trivy k8s) scans images and cluster resources for known CVEs and misconfigurations – it tells you whether something vulnerable is running.

Both belong in CI or a regular scan; manual triage is only for the quick single question “does this new CVE affect me?”.

Save your progress? Sign up for free

Enterprise Note

  • Patch discipline is the foundation: subscribe to the Kubernetes security announcements, keep versions within the supported window, practice upgrades (your Talos/CKA knowledge helps here).
  • Defense in depth on top of the patch: switch off dangerous, rarely used fields (subPath, hostPath, hostPort) with a policy – then the next CVE of the same class hits nothing.
  • Run kube-bench (CIS) and Trivy (CVEs) regularly and in CI, and collect the results centrally.
  • A CVE response needs both: a policy as a protective wall right away, then the patch in the maintenance window.