Security

Pod Security

1.Standards vs admission

Pod Security Standards (PSS) define three levels. Pod Security Admission (PSA) enforces them per namespace.

  • PSA is the built-in PodSecurity validating admission plugin. GA since v1.25, on by default.
  • It runs after mutating admission, so it judges the final Pod. See Admission Control.
  • Pods comply through securityContext and, since v1.35, hostUsers: false.
  • PodSecurityPolicy : removed in v1.25. Use PSA, a third-party admission plugin, or both.
Privileged no restrictions Baseline a default Pod passes bans : host namespaces, privileged, hostPath, hostPort, extra caps, Unconfined seccomp or AppArmor, Unmasked procMount, unsafe sysctls, probe host field, HostProcess, SELinux Restricted best practice adds : allowPrivilegeEscalation false, runAsNonRoot, runAsUser not 0, seccomp RuntimeDefault or Localhost, drop ALL, volume allow-list

A Pod that passes restricted also passes baseline.

2.One label per mode

Label the namespace pod-security.kubernetes.io/<MODE>: <LEVEL>. The modes are independent.

ModeOn a violationChecks
enforceRejects the Pod (403)Pods only
auditAudit annotation, allowedWorkloads + Pods
warnClient warning, allowedWorkloads + Pods

Common rollout : enforce baseline, warn and audit restricted.

Versions, defaults, exemptions
  • -versionv1.37 or latest (default). Pin enforce-version so an upgrade cannot start rejecting Pods.
  • No labelsCluster defaults from PodSecurityConfiguration. Built in : privileged / latest.
  • Exemptusernames, runtimeClasses, namespaces skip all three modes. Avoid controller service accounts.

3.Deployment path vs Pod path

Namespace labels level + version per mode reads kubectl apply a Deployment kube-apiserver Mutating admission final object PodSecurity validating store etcd if allowed warn audit Warning to the client Audit log annotation warn, audit : Deployment and Pod ReplicaSet controller creates the Pod enforce fails : 403 Forbidden, no Pod ReplicaSet controller kubectl apply a Deployment a Pod kube-apiserver Mutating admission final object PodSecurity validating Namespace labels Warning to client Audit log annotation etcd if allowed warn, audit : Deployment and Pod red : enforce fails, 403, no Pod

enforce checks Pods only : a bad Deployment is stored, its Pods are not.

4.The checks, field by field

Baseline bansAllowed
hostNetwork / PID / IPCunset or false
privilegedunset or false
hostPath volumesnot used
ports[*].hostPortunset or 0
appArmorProfile.typeunset, RuntimeDefault, Localhost
seccompProfile.typeanything but Unconfined
Restricted addsAllowed
allowPrivilegeEscalationfalse
capabilitiesdrop ALL, add only NET_BIND_SERVICE
runAsNonRoottrue (pod, or every container)
runAsUsernon-zero or unset
seccompProfile.typeset : RuntimeDefault or Localhost

Restricted volumes : configMap, csi, downwardAPI, emptyDir, ephemeral, persistentVolumeClaim, projected, secret. A rejection names the check, for example allowPrivilegeEscalation != false or runAsNonRoot != true.

In short : PSS defines three levels and PSA enforces them per namespace through labels. enforce rejects Pods, warn and audit only report.

1.A Pod that passes restricted

apiVersion: v1
kind: Pod
metadata: {name: app, namespace: team-a}
spec:
  securityContext:        # pod level
    runAsNonRoot: true
    runAsUser: 10001
    runAsGroup: 10001
    fsGroup: 10001
    seccompProfile: {type: RuntimeDefault}
  containers:
  - name: app
    image: busybox:1.28
    command: ["sh", "-c", "sleep 1h"]
    securityContext:      # container level
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities: {drop: ["ALL"]}
    volumeMounts: [{name: tmp, mountPath: /tmp}]
  volumes: [{name: tmp, emptyDir: {}}]  # scratch

2.securityContext, two levels

spec.securityContext sets defaults for every container. containers[*].securityContext wins when both set a field.

FieldLevelIf unset
runAsUserbothimage USER
runAsNonRootbothno check
seccompProfilebothUnconfined *
appArmorProfilebothRuntimeDefault **
fsGrouppodowners untouched
allowPrivilegeEscalationcontainertrue
readOnlyRootFilesystemcontainerfalse
capabilitiescontainerruntime default
privilegedcontainerfalse

* RuntimeDefault if kubelet seccompDefault: true (GA v1.27, off by default). ** on AppArmor nodes. fsGroup, supplementalGroups, sysctls : pod only.

3.User namespaces : hostUsers: false

  • GA in v1.36. Root in the Pod maps to an unprivileged host UID range, 65536 IDs per Pod by default.
  • PSA (policy v1.35+) skips runAsNonRoot and runAsUser : root inside passes restricted.
  • Not with hostNetwork, hostPID, hostIPC or volumeDevices.
  • Nodes : Linux 6.3+, containerd 2.0+ or CRI-O 1.25+, runc 1.2+ or crun 1.9+.
Pod user namespace process, UID 0 root inside the Pod capabilities work inside it only Volume idmap mount in-Pod owners, disk unchanged uid_map : 0 to 833617920 count 65536 Node : host UID 833617920 unprivileged, a breakout lands here

4.Commands

CommandDoes
kubectl label --overwrite ns team-a pod-security.kubernetes.io/enforce=restricted pod-security.kubernetes.io/enforce-version=v1.37Enforce restricted, pinned to v1.37
kubectl label --dry-run=server --overwrite ns <ns> pod-security.kubernetes.io/enforce=baselineRunning Pods that would fail
kubectl apply --dry-run=server -n <ns> -f <file>Runs admission, stores nothing
kubectl debug -it <pod> --image=<nonroot-image> --target=<container> --profile=restrictedRestricted-safe debug container
kubectl exec <pod> -- cat /proc/self/uid_mapExpect 0, a high host UID, 65536

5.Gotchas & Good to know

Gotchas
  • enforce never blocks a Deployment. Read the warnings and the ReplicaSet events.
  • New enforce labels do not evict running Pods. Dry-run the label first.
  • runAsNonRoot with an image USER of root, or a name : CreateContainerConfigError. Set a numeric runAsUser.
Good to know
  • An explicit appArmorProfile RuntimeDefault fails on nodes without AppArmor.
  • PSA checks debug containers too : use --profile=restricted.
  • hostPID sees node processes, hostNetwork Pods may skip NetworkPolicy, privileged or hostPath reach the node's Secrets.
In short : Share settings through the pod securityContext, use a numeric non-root runAsUser, and hostUsers: false maps root in the Pod to an unprivileged UID on the node.