Skip to main content

More Info:

Apply Security Context to Your Pods and Containers

Risk Level

Medium

Address

Security

Compliance Standards

  • APRA CPS 234 (Australia)
  • BSI C5 (Germany)
  • Brazil LGPD
  • CCPA / CPRA (California)
  • CIS Critical Security Controls v8
  • CIS EKS
  • CMMC 2.0
  • CSA Cloud Controls Matrix v4
  • DPDPA
  • Digital Operational Resilience Act (EU)
  • Essential 8
  • ISO/IEC 27017
  • ISO/IEC 27018
  • ISO/IEC 27701
  • KSA PDPL
  • MAS Technology Risk Management (Singapore)
  • MITRE ATT&CK (Cloud)
  • NIS2 Directive
  • NIST SP 800-171
  • NYDFS 23 NYCRR 500
  • SWIFT Customer Security Controls Framework
  • Sarbanes-Oxley IT General Controls
  • UK NCSC Cyber Assessment Framework

Triage and Remediation

Remediation

Manual Steps

  1. Identify pods and namespaces lacking securityContext
    • On any machine with kubectl access:
    • Flag pods/containers where .spec.securityContext is <nil> and any container .securityContext is missing or empty for review.
  2. Review current privilege-related settings on suspect workloads
    • For each flagged pod, inspect full spec:
    • Look for: securityContext.privileged, allowPrivilegeEscalation, runAsUser, runAsNonRoot, capabilities, hostNetwork, hostPID, hostIPC, hostPath/other volume types, fsGroup, supplementalGroups.
    • Decide, with the application owner, whether the workload truly requires any privileged or host-level access.
  3. Define or update a restrictive baseline policy per namespace
    • For namespaces that should be locked down (everything except core infra like kube-system), create or adjust a baseline manifest reflecting the benchmark’s restrictive intent, for example (edit names/labels/namespaces as needed):
    • For existing namespaces, apply labels directly:
  4. Harden pod specs to meet the restrictive intent
    • For each non-privileged workload, update its Deployment/StatefulSet/Job manifest (never edit bare pods that are not managed by a controller) to add container-level securityContext such as:
    • Apply updated manifests from your IaC/manifest repo using kubectl from any machine with access:
  5. Handle necessary exceptions in tightly scoped namespaces
    • For workloads that require broader access (e.g., logging agents, CNI, storage drivers), ensure they are:
      • Deployed into dedicated namespaces (e.g., kube-system or a specific infra-logging namespace).
      • Bound to specific service accounts with RBAC tailored to their needs:
    • Loosen securityContext only for these pods and document the justification per workload.
  6. Re-verify posture after changes
    • Confirm that most pods now carry non-empty securityContext fields and that no unexpected privileged/host-level settings exist:
    • Spot-check a sample of critical namespaces (default, app namespaces) with kubectl get pod <pod> -n <ns> -o yaml to ensure configurations align with your restrictive baseline and the benchmark’s intent.

Using kubectl

Run these from any machine with kubectl access.

1. List pods that declare no securityContext at all

Problem indication: Any application namespace (e.g. not kube-system, kube-public, kube-node-lease) showing pods here means those pods have no explicit security context and should be reviewed and likely hardened.

2. Find containers that can run as root or with no user restriction

Problem indication: Application workloads here are effectively allowed to run as root (UID 0) or have no restriction to non‑root. Compare against your policy; in line with the benchmark, you typically want runAsNonRoot: true or a non‑0 runAsUser.

3. Find containers allowed to escalate privileges

Problem indication: Non‑system workloads in this list allow privilege escalation. The benchmark recommends allowPrivilegeEscalation: false as a default.

4. Find privileged containers and host‑level access

Problem indication: Any application pod here has elevated privileges (privileged, extra capabilities, or host namespace access). Only tightly‑controlled system or daemon pods should appear, and even those should be explicitly justified.

5. Identify containers without restricted capabilities / read‑only root FS

Problem indication: For most workloads aligned with the sample restrictive policy, you expect drop: ["ALL"] and readOnlyRootFilesystem: true (or a conscious exception). Entries here are candidates for hardening.

6. Review volume types that may bypass restrictions

Problem indication: While persistent volumes are generally allowed, widespread hostPath or other direct node‑level mounts in application namespaces often signal pods that should be tightly reviewed and potentially restricted or moved under a privileged policy only where strictly necessary.

7. Check for Pod-level securityContext defaults

Problem indication: When inspecting individual pod specs, look for:
  • securityContext at pod and container level missing or overly permissive.
  • Absence of fields that align with your restrictive baseline, such as:
    • runAsNonRoot: true or non‑0 runAsUser
    • allowPrivilegeEscalation: false
    • readOnlyRootFilesystem: true
    • capabilities: { drop: ["ALL"] }
    • No privileged: true, no host namespaces unless strictly required.
Use the above commands to shortlist pods, then inspect those pods’ full specs with:
A human must decide which pods are legitimate exceptions (e.g. certain kube-system components, logging agents) and where manifests or higher‑level policy (PSPs, Pod Security admission, or other policy engines) should be tightened.

Additional Reading: