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
PodSecurityvalidating 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
securityContextand, since v1.35,hostUsers: false. - PodSecurityPolicy : removed in v1.25. Use PSA, a third-party admission plugin, or both.
A Pod that passes restricted also passes baseline.
2.One label per mode
Label the namespace pod-security.. The modes are independent.
| Mode | On a violation | Checks |
|---|---|---|
| enforce | Rejects the Pod (403) | Pods only |
| audit | Audit annotation, allowed | Workloads + Pods |
| warn | Client warning, allowed | Workloads + Pods |
Common rollout : enforce baseline, warn and audit restricted.
- -version
v1.37orlatest(default). Pinenforce-versionso 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
enforce checks Pods only : a bad Deployment is stored, its Pods are not.
4.The checks, field by field
| Baseline bans | Allowed |
|---|---|
| hostNetwork / PID / IPC | unset or false |
| privileged | unset or false |
| hostPath volumes | not used |
| ports[*].hostPort | unset or 0 |
| appArmorProfile.type | unset, RuntimeDefault, Localhost |
| seccompProfile.type | anything but Unconfined |
| Restricted adds | Allowed |
|---|---|
| allowPrivilegeEscalation | false |
| capabilities | drop ALL, add only NET_BIND_SERVICE |
| runAsNonRoot | true (pod, or every container) |
| runAsUser | non-zero or unset |
| seccompProfile.type | set : 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.
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. sets defaults for every container. containers[*]. wins when both set a field.
| Field | Level | If unset |
|---|---|---|
| runAsUser | both | image USER |
| runAsNonRoot | both | no check |
| seccompProfile | both | Unconfined * |
| appArmorProfile | both | Runtime |
| fsGroup | pod | owners untouched |
| allowPrivilegeEscalation | container | true |
| readOnlyRootFilesystem | container | false |
| capabilities | container | runtime default |
| privileged | container | false |
* 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+.
4.Commands
| Command | Does |
|---|---|
kubectl label --overwrite ns team-a pod-security. | Enforce restricted, pinned to v1.37 |
kubectl label --dry-run=server --overwrite ns <ns> pod-security. | Running 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=restricted | Restricted-safe debug container |
kubectl exec <pod> -- cat / | Expect 0, a high host UID, 65536 |
5.Gotchas & Good to know
- 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.
- 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.