Skip to main content

More Info:

Security contexts constrain the privileges and access of pods and containers at runtime. Apply appropriate security contexts to all pods and containers.

Risk Level

Medium

Address

Security

Compliance Standards

  • CIS AKS

Triage and Remediation

Remediation

Manual Steps

  1. List pods missing or with weak securityContext
    • Run on: any machine with kubectl access
    • Command:
  2. Gather full specs for candidate pods
    • Run on: any machine with kubectl access
    • For each <namespace> <pod> from step 1:
  3. Review current securityContext settings
    • Open each pod-*.yaml and check at both pod and container levels for (examples of what to look for; you must decide what is appropriate for your workloads):
      • Missing securityContext: blocks
      • Privilege-related fields: privileged, allowPrivilegeEscalation, capabilities.add/drop, runAsUser, runAsGroup, runAsNonRoot, readOnlyRootFilesystem, seLinuxOptions, seccompProfile, procMount, hostPID, hostIPC, hostNetwork, hostPorts.
    • Identify pods/containers that run with unnecessary or overly broad privileges relative to their function.
  4. Decide appropriate securityContext per workload
    • For each pod/container, decide:
      • The minimal user/group IDs (runAsUser, runAsGroup, runAsNonRoot: true)
      • Whether privileged is truly needed; if not, set privileged: false
      • Whether allowPrivilegeEscalation can be disabled: allowPrivilegeEscalation: false
      • Required and removable Linux capabilities (capabilities.drop and capabilities.add)
      • Whether the root filesystem can be read-only: readOnlyRootFilesystem: true
      • Whether host namespaces/ports are truly required (avoid hostPID, hostIPC, hostNetwork, host ports where possible).
    • Use your application and Docker image requirements plus the CIS Docker Benchmark as guidance.
  5. Implement securityContext via manifests (preferred) or live edit
    • If workloads are managed by manifests/Helm/Kustomize:
      • Edit the deployment/statefulset/daemonset/job manifests (not the pods directly) to add/adjust securityContext at pod spec and container spec levels according to step 4. Example pattern (adapt as needed for each workload):
      • Apply changes:
    • If a pod is not managed by higher-level controllers (e.g., a standalone pod for debugging):
      • Edit directly:
      • Add/adjust the securityContext blocks as decided in step 4; save to recreate the pod.
  6. Verify that security contexts are now applied
    • Re-run the evidence command and confirm no workload you intend to secure is listed:
    • Spot-check a few pods:
    • Confirm that the configured securityContext values match your decisions from step 4 and that applications still function as expected.
Problem indicators:
  • POD_SC column is empty (<none> or {}) for pods that should have restricted settings.
  • You see no common baseline (some pods with strict settings, others with none, without a clear reason).
Problem indicators (per container):
  • securityContext is missing (null/[]) or {} for workloads that handle sensitive data or run user code.
  • Known risky patterns, for example:
    • "privileged": true
    • "runAsUser": 0 or "runAsNonRoot": false without a strong justification
    • "allowPrivilegeEscalation": true
    • Broad capabilities like "capAdd": ["ALL"] or many capabilities with no clear need
    • Host access options such as "hostNetwork": true, "hostPID": true, "hostIPC": true at pod spec level.
What to look for in the YAML:
  • Missing securityContext at both .spec.securityContext and .spec.containers[].securityContext.
  • Any of:
    • privileged: true
    • runAsNonRoot: false or absent, combined with runAsUser: 0
    • allowPrivilegeEscalation: true or absent when the container has extra capabilities
    • capabilities.add including unnecessary or broad capabilities (e.g., SYS_ADMIN, NET_ADMIN, ALL)
    • hostNetwork: true, hostPID: true, hostIPC: true without a strict operational reason
    • readOnlyRootFilesystem: false or absent when the container does not need to write to the root FS.
Problem indicators:
  • Any application pod (not a low-level node/cluster agent) appearing in this list without a clear operational need.
  • Cluster-wide patterns where many pods use privileged mode or host namespace access.
Problem indicators:
  • Business‑critical or multi‑tenant workloads listed here; they likely need explicit securityContext settings instead of inheriting defaults.
How to interpret the output
  • In the first table, any row with SECURITY_CONTEXT = MISSING_SC indicates a pod container with no securityContext defined at the container level. These need manual review and likely a securityContext added.
  • In the second and third sections:
    • Lines listed indicate containers of interest.
    • Fields with unset mean that aspect is not configured and should be reviewed (for example, consider setting runAsNonRoot: true, readOnlyRootFilesystem: true, allowPrivilegeEscalation: false, and dropping capabilities where possible).
  • Absence of rows in these sections means all containers have a securityContext and the inspected fields are set, but you still must review whether the chosen values are appropriate and restrictive enough.