All guides

Pod Security Standards: baseline, restricted and how to enforce them

Kubernetes defines three security levels for pods and ships an admission controller that applies them per namespace. Six labels control it, and a mode with no label falls back to the documented default, which is privileged.

Lines of code on a screen, coloured by syntax

Three levels

The Pod Security Standards define three policies. They are cumulative and range from highly permissive to highly restrictive.

  • Privileged is unrestricted. The standard aims it at system and infrastructure workloads managed by privileged, trusted users.
  • Baseline prevents known privilege escalations and allows the default, minimally specified pod configuration. Among its controls: privileged containers, sharing the host namespaces (hostNetwork, hostPID, hostIPC) and hostPath volumes are disallowed. Capabilities may be added only from a fixed list.
  • Restricted applies the current best practices for hardening pods and gives up some compatibility to do so. It includes everything in baseline. Containers must also set allowPrivilegeEscalation: false, run as non-root, drop ALL capabilities and set a seccomp profile of RuntimeDefault or Localhost. The only capability they may add back is NET_BIND_SERVICE. Volumes are limited to eight types: configMap, csi, downwardAPI, emptyDir, ephemeral, persistentVolumeClaim, projected and secret.

The six namespace labels

Pod Security Admission is the built-in admission controller that enforces the standards. It is stable since Kubernetes v1.25 and reads labels on the namespace. It has three modes:

  • enforce: policy violations cause the pod to be rejected.
  • audit: violations are allowed and add an audit annotation to the event in the audit log.
  • warn: violations are allowed and trigger a user-facing warning.

A namespace can set any or all modes, and a different level for each. Every mode takes a level label and an optional version label. This is the example from the Kubernetes documentation:

YAML

apiVersion: v1
kind: Namespace
metadata:
  name: my-baseline-namespace
  labels:
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: v1.37
    pod-security.kubernetes.io/audit: restricted
    pod-security.kubernetes.io/audit-version: v1.37
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: v1.37

It blocks pods that do not satisfy baseline. It warns and adds an audit annotation for pods that do not meet restricted. The version label pins the policy to the version that shipped with a Kubernetes minor version, and it also accepts latest. The example pins v1.37, the release that page covers today. Set the version you want.

A mode with no label uses the default from the admission controller's configuration. The documented defaults are level privileged and version latest.

The enforce mode is applied to pod objects only. The audit and warn modes are also applied to workload resources that hold a pod template, such as a Deployment or a Job.

Rollout with warn and audit first

Start with a server-side dry run of the level you plan to enforce:

Shell

kubectl label --dry-run=server --overwrite ns --all \
    pod-security.kubernetes.io/enforce=baseline

The checks run against existing pods and no policy is updated. The command returns a warning for each existing pod that is not valid under the proposed level.

Next, set audit and warn on every namespace. The documentation calls this a suitable first step:

Shell

kubectl label --overwrite ns --all \
  pod-security.kubernetes.io/audit=baseline \
  pod-security.kubernetes.io/warn=baseline

This sets no enforce level, so the namespaces that nobody has evaluated can still be told apart. List them:

Shell

kubectl get namespaces --selector='!pod-security.kubernetes.io/enforce'

The documentation describes audit mode as the better option for catching workloads that are not running at the time of the dry run. The audit event carries the annotation pod-security.kubernetes.io/audit-violations, which names the level and the fields that were violated.

When a namespace is ready, enforce a level on it:

Shell

kubectl label --overwrite ns my-existing-namespace \
  pod-security.kubernetes.io/enforce=restricted \
  pod-security.kubernetes.io/enforce-version=v1.37

Keep warn at the same level as enforce. Clients then receive a warning when they create a pod, or a resource with a pod template, that does not pass validation.

Exemptions and cluster defaults

Exemptions are configured statically in the admission controller configuration. That is a file passed to the API server with --admission-control-config-file. The same file holds the defaults that apply when a mode label is not set. This is the documented file with its default values:

YAML

apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
- name: PodSecurity
  configuration:
    apiVersion: pod-security.admission.config.k8s.io/v1
    kind: PodSecurityConfiguration
    defaults:
      enforce: "privileged"
      enforce-version: "latest"
      audit: "privileged"
      audit-version: "latest"
      warn: "privileged"
      warn-version: "latest"
    exemptions:
      usernames: []
      runtimeClasses: []
      namespaces: []

The three exemption lists take authenticated usernames, runtime class names and namespaces. A request that meets an exemption is ignored by the admission controller: all enforce, audit and warn behavior is skipped.

The documentation warns against exempting controller service accounts such as system:serviceaccount:kube-system:replicaset-controller. Doing so implicitly exempts any user who can create the corresponding workload resource.

Limits of the built-in controller

  • It supports the three standard levels only. For more control over specific constraints, the documentation points to a validating admission webhook.
  • It is a non-mutating admission controller. It does not modify pods before it validates them.
  • It works per namespace. Binding different policies to different service accounts or users inside one namespace needs a third-party webhook.
  • The restrictions are applied when pods are created.

The Kubernetes documentation names Kubewarden, Kyverno and OPA Gatekeeper as other ways to enforce policies in the Kubernetes ecosystem.

First steps in an existing cluster

  1. List the namespaces with no enforce label. The documentation treats an unlabeled namespace as one that has yet to be evaluated.
  2. Set audit and warn on all namespaces to the level you want to enforce later, and read the warnings and audit annotations.
  3. Dry-run, then enforce, one namespace at a time.
  4. For each namespace that allows privileged workloads, set up and enforce access controls. The documentation asks for that explicitly.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides