Day 12: Admission Gaps and Policy Bypasses
Goal: You find two gaps in a “finished” policy – audit mode and too narrow a scope – and close both. · Duration: approx. 70 min · Prerequisite: Day 11
Briefing
Having a policy does not mean being protected. The two most common fallacies: the policy runs in Audit mode (it reports, but does not block), and it covers only part of the cluster (another namespace bypasses it). Today you build a “no-privileged” policy with exactly these gaps, demonstrate the bypass, and seal it.
Drill 1: The gap – policy in audit mode
kubectl create namespace gaps
kubectl apply -f - <<'EOF'
apiVersion: policies.kyverno.io/v1
kind: ValidatingPolicy
metadata: {name: no-privileged}
spec:
validationActions: ["Audit"]
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE"]
resources: ["pods"]
matchConditions:
- name: only-gaps
expression: "request.namespace == 'gaps'"
validations:
- expression: "object.spec.containers.all(c, !has(c.securityContext) ||
!has(c.securityContext.privileged) || c.securityContext.privileged == false)"
message: "privileged ist verboten"
EOFSave your progress? Sign up for free
Drill 2: Exploit – Audit does not block
kubectl -n gaps run p --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinity
kubectl -n gaps get pod p -o jsonpath='{.status.phase}'; echoExpected: Pending/Running – the privileged
Pod runs. Audit only writes a PolicyReport; admission lets
it through. Remove it:
kubectl -n gaps delete pod pSave your progress? Sign up for free
Drill 3: Fix 1 – switch to Deny
Change validationActions to Deny (rest of
the policy identical):
kubectl patch validatingpolicy no-privileged --type=json \
-p='[{"op":"replace","path":"/spec/validationActions","value":["Deny"]}]'Proof in gaps:
kubectl -n gaps run p --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinityExpected:
Error from server: admission webhook "vpol.validate.kyverno.svc-...-no-privileged"
denied the request: Policy no-privileged failed: privileged ist verboten
Save your progress? Sign up for free
Drill 4: The second gap – too narrow a scope
The policy applies only to gaps. Every other namespace
is open:
kubectl create namespace gaps2
kubectl -n gaps2 run p --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinity
kubectl -n gaps2 get pod p -o jsonpath='{.status.phase}'; echoExpected: Pending/Running – the same
privileged Pod, just one namespace over, and the “protection” does not
kick in. This is exactly how an attacker bypasses namespace-bound
policies: they simply create resources in a namespace that is not
covered.
kubectl -n gaps2 delete pod pSave your progress? Sign up for free
Drill 5: Fix 2 – cluster-wide, excluding only system namespaces
Instead of including one namespace, you exclude the few trusted system namespaces and cover the rest:
kubectl patch validatingpolicy no-privileged --type=json -p='[{"op":"replace",
"path":"/spec/matchConditions","value":[{"name":"not-system",
"expression":"!(request.namespace in [\"kube-system\",\"kube-public\",\"kube-node-lease\",\"kyverno\"])"}]}]'Proof – privileged is now rejected everywhere:
kubectl -n gaps2 run p --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinity
kubectl -n attacker run pv --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinityExpected (both):
Error from server: admission webhook "..." denied the request:
Policy no-privileged failed: privileged ist verboten
Clean up:
kubectl delete validatingpolicy no-privileged
kubectl delete namespace gaps gaps2Save your progress? Sign up for free
Checkpoint
After re-applying the final policy (Deny + not-system), there is no uncovered namespace left to find – test any new one:
kubectl create namespace gaps3
kubectl -n gaps3 run p --image=docker.io/library/busybox:1.36 --privileged \
--command -- sleep infinity 2>&1 | grep -o "privileged ist verboten"
kubectl delete namespace gaps3Expected: privileged ist verboten
Challenge
There is a third, subtle gap: the policy only matches
operations: ["CREATE"]. Could an attacker create an allowed
resource and escalate it via UPDATE? Check for Pods whether
securityContext.privileged can be changed afterwards – and
what that means for the choice of operations.
Hint: Try to set a running, compliant Pod to
privileged: true with kubectl patch.
Solution
kubectl create namespace gaps4
kubectl -n gaps4 run q --image=docker.io/library/busybox:1.36 --command -- sleep infinity
kubectl -n gaps4 patch pod q --type=json \
-p='[{"op":"add","path":"/spec/containers/0/securityContext","value":{"privileged":true}}]'
kubectl delete namespace gaps4Expected: Kubernetes itself rejects the patch – the Pod
securityContext is largely immutable after creation
(Forbidden: pod updates may not change fields other than …).
For Pods, this immutability saves you. For resources that can
be changed (Deployments, many CRDs), you must always include
UPDATE – otherwise the later patch is the gap.
Save your progress? Sign up for free
Enterprise Note
- Policies through their lifecycle: first
Audit, review violations (PolicyReports), thenDeny– but the end state of production guardrails is alwaysDeny. - Scope by exclusion (system namespaces) instead of inclusion; new namespaces are then covered automatically.
- Complete
operations:CREATEandUPDATE(andCONNECTwhere needed). - Consider Kyverno availability: if the webhook fails,
failurePolicydecides whether to “reject when in doubt” (Fail, safe) or “let through” (Ignore). For security policies useFail– for operations and HA, see the course “Kyverno & NetworkPolicies”. - Evaluate PolicyReports centrally (Policy Reporter) and alert on new violations.