Root containers admitted

Allowing root execution and privilege escalation in PodSecurityPolicy can increase the impact of a compromised container.

Description

To prevent root execution in a legacy PodSecurityPolicy (PSP), set runAsUser.rule to MustRunAsNonRoot. The first policy’s RunAsAny permits UID 0.

Potential impact

Running as root, running in privileged mode and gaining extra process privileges are separate permissions. Allowing all three can increase the impact of a compromised container on the host or sensitive files.

Remediation

In a legacy PSP, also set privileged and allowPrivilegeEscalation to false. Kubernetes deprecated PSP in 1.21 and removed it in 1.25. On current clusters, Restricted Pod Security Admission requires non-root execution for Linux Pods outside a Pod user namespace. Use a policy engine such as Gatekeeper or Kyverno to prohibit UID 0 inside a Pod user namespace, enforce group ranges or require a read-only root filesystem.

Examples

The excerpts omit other required PSP fields and authorization to use the policy on a legacy cluster.

Before

yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restricted
spec:
  privileged: true
  allowPrivilegeEscalation: true
  runAsUser:
    rule: "RunAsAny"
  supplementalGroups:
    rule: "RunAsAny"
  fsGroup:
    rule: "MustRunAs"
    ranges:
      - min: 0
        max: 65535

After

yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: restricted
spec:
  privileged: false
  allowPrivilegeEscalation: false
  requiredDropCapabilities:
    - ALL
  runAsUser:
    rule: "MustRunAsNonRoot"
  supplementalGroups:
    rule: "MustRunAs"
    ranges:
      - min: 1
        max: 65535
  fsGroup:
    rule: "MustRunAs"
    ranges:
      - min: 1
        max: 65535
  readOnlyRootFilesystem: true

The second policy also requires dropping all Linux capabilities and limits supplemental groups and the filesystem group to 1–65535. Check that the workload can write to its required volumes with a read-only root filesystem.

References