Review Kubernetes Pod and container security contexts

Specify the execution identity, permissions and filesystem protections each workload needs.

Description

Use the Pod’s securityContext for a shared execution user. Set privilege escalation, root filesystem access and Linux capabilities on each container.

Potential impact

An omitted or empty context leaves the workload dependent on image, runtime and admission policy defaults, which may grant more access than intended.

Remediation

Check whether a container overrides the Pod user. Verify its effective UID and writable paths in the deployed Pod, then test startup and normal operation.

Examples

Replace the example image with one that supports UID 1000. Mount a volume with suitable permissions wherever the application needs to write.

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
    - name: app
      image: images.my-company.example/app:v4

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  securityContext:
    runAsUser: 1000
  containers:
    - name: app
      image: images.my-company.example/app:v4
      securityContext:
        allowPrivilegeEscalation: false
        readOnlyRootFilesystem: true

The Pod selects UID 1000. The app container restricts privilege escalation and makes its root filesystem read-only.

References