Review container protection against root execution

Review the image and security context together to prevent unnecessary root execution.

Description

Check the UID that actually runs the container. runAsNonRoot: false permits UID 0. Container settings override Pod defaults, and the image supplies the user if neither specifies one.

Potential impact

Root can modify more files and processes inside the container. Host mounts and additional capabilities can extend that access.

Remediation

Set runAsNonRoot: true and choose a nonzero runAsUser that works with the image. Check app and init containers for overrides. Grant that UID access to the files and volumes it needs, restrict host access and capabilities, and test startup.

Examples

Before

yaml
apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-2
spec:
  securityContext:
    runAsUser: 1000
    runAsNonRoot: false
  containers:
    - name: sec-ctx-demo-2
      image: gcr.io/google-samples/node-hello:1.0
      securityContext:
        runAsUser: 0
        allowPrivilegeEscalation: false
        runAsNonRoot: false

The container overrides the Pod’s UID 1000 with UID 0. allowPrivilegeEscalation: false leaves that UID in effect.

After

yaml
apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-2
spec:
  securityContext:
    runAsUser: 10000
    runAsNonRoot: true
  containers:
    - name: sec-ctx-demo-2
      image: gcr.io/google-samples/node-hello:1.0
      securityContext:
        runAsUser: 10100
        allowPrivilegeEscalation: false
        runAsNonRoot: true

The container runs as UID 10100.

References