Skip to main content

More Info:

Security contexts restrict privileges, capabilities, volumes, and root access for pods and containers. Apply restrictive policies broadly and scope privileged access to specific service accounts and namespaces.

Risk Level

Low

Address

Security

Compliance Standards

  • CIS OKE

Triage and Remediation

Remediation

Manual Steps

  1. Identify pods and namespaces with missing or weak securityContext
    • On any machine with kubectl access:
  2. Review securityContext details for suspicious pods
    • For each namespace/pod pair from step 1, inspect full spec:
    • Manually check for:
      • securityContext.privileged: true
      • allowPrivilegeEscalation: true or unset
      • runAsUser: 0 or runAsNonRoot: false/unset
      • Use of hostNetwork, hostPID, hostIPC, and hostPath volumes
      • Absence of capabilities.drop: ["ALL"]
  3. Decide which workloads truly need elevated privileges and scope them
    • Classify each privileged/high‑risk pod as:
      • System-critical (needs elevated privileges) – e.g. CNI, kube-proxy, node/logging/monitoring agents. Prefer to run these in a dedicated namespace like kube-system or another clearly named “system” namespace.
      • Business application (should be restricted) – typical app workloads that should run non-root and non-privileged.
    • For system-critical pods:
      • Ensure they use dedicated service accounts and namespaces.
      • Plan to bind any future privileged policies only to those service accounts/namespaces.
  4. Define or tighten restrictive security policies for normal workloads
    • On any machine with kubectl access, create a baseline “restricted” policy object aligned with the remediation (adapted to your cluster’s API support; PodSecurityPolicy is deprecated in newer versions, so you may instead implement equivalent Pod Security Admission or OPA/Gatekeeper):
    • Bind this policy only to non-privileged service accounts/namespaces via RBAC (cluster roles/rolebindings) after confirming PodSecurityPolicy is enabled in your cluster.
  5. Refactor pod specs for non-privileged workloads
    • For each business/application workload identified in step 3, update the Deployment/DaemonSet/StatefulSet (not the live pod) to add safe security contexts, for example:
      • Under spec.template.spec.securityContext and each container’s securityContext, add fields such as:
      • Remove privileged: true, hostPath volumes, hostNetwork: true, hostPID: true, hostIPC: true unless strictly required.
    • Save to trigger a rolling restart.
  6. Verify improved posture and monitor regressions
    • Re-run discovery to ensure no remaining obviously insecure pods:
    • Optionally, enforce admission controls (Pod Security Admission “restricted” level or equivalent policy engine) to prevent future pods without appropriate security contexts from being admitted.
Look for non-system namespaces hosting your workloads (not kube-system, kube-public, kube-node-lease).
Red flags in the output:
  • SC_POD=<none> and SC_CONT=<none> (no securityContext at all on pod or containers).
  • Service account is generic (e.g., default) and used by many pods that may need different privilege levels.

In the YAML, look for:Pod-level (under .spec.securityContext):
  • Missing entirely (no securityContext: block).
  • fsGroup / supplementalGroups with values including 0.
  • runAsUser: 0 or runAsGroup: 0 without strong need.
Container-level (for each entry under .spec.containers[] and .spec.initContainers[]):
  • securityContext missing completely.
  • privileged: true.
  • allowPrivilegeEscalation: true or not set.
  • runAsUser: 0 or runAsNonRoot: false or not set.
  • capabilities.add with broad capabilities (or no capabilities.drop).
  • readOnlyRootFilesystem: false or not set.
Host-level access (still under each container):
  • hostNetwork: true, hostPID: true, or hostIPC: true (these are at pod spec level).
  • Volumes with hostPath: under .spec.volumes[] without strong justification.
These indicate pods/containers lack restrictive security contexts or have excessive privileges compared to the benchmark example.
Concerning lines:
  • true in the privileged position.
  • true or <no value> in the allowPrivilegeEscalation position for general application workloads.

Concerning lines:
  • 0 for runAsUser.
  • false or <no value> for runAsNonRoot on general workloads.

Concerning output:
  • Any hostPath object (not null) used by regular application pods, instead of more controlled volume types (configMap, secret, PVC, etc.), unless clearly justified (e.g., logging agents, node-level tooling).

Then for suspicious bindings (especially those granting broad or cluster-admin-like roles), inspect them:
Concerning situations:
  • Service accounts outside tightly controlled namespaces (e.g., not kube-system) bound to highly privileged roles.
  • Generic service accounts (default) having elevated roles, which may be used by pods lacking restrictive security contexts.

Concerning output:
  • Workload namespaces without pod-security.kubernetes.io/enforce labels.
  • Enforce levels weaker than restricted for general application namespaces (e.g., enforce=privileged or missing).
Explanation of problematic output to review manually:
  • Section “Namespaces with pods missing ANY securityContext”:
    • Any listed <namespace> <pod> means that pod and all its containers lack explicit securityContext. These are candidates to harden by adding non-root, dropping capabilities, read-only root filesystem, etc.
  • Section “Pods/containers with risky securityContext fields”:
    • Lines beginning with:
      • PRIVILEGED|... – containers running with securityContext.privileged: true (highest risk; should be tightly scoped to specific service accounts/namespaces like kube-system if truly required).
      • ALLOW_PRIV_ESC|... – containers that allow privilege escalation.
      • ROOT_USER|... – containers explicitly running as root (runAsUser: 0 or runAsNonRoot: false).
      • RW_ROOTFS|... – containers with readOnlyRootFilesystem: false (less restrictive; often acceptable but should be intentional).
  • Section “Pods using host features or hostPath volumes”:
    • Any line indicates a pod using hostNetwork, hostIPC, hostPID, or hostPath volumes, which breaks the isolation the benchmark’s example policy expects. These should be limited to well‑understood system workloads in tightly controlled namespaces.
  • Section “Per-namespace securityContext usage summary”:
    • For each namespace:
      • podSC=false|containerSC=false – all pods in that namespace lack explicit securityContext; namespace is a prime target for enforcing more restrictive defaults.
      • Namespaces with many false values should be prioritized for adding restrictive policies and explicit pod/container securityContext.
Use this script to identify where securityContext is missing or too permissive, then update manifests and apply namespace/service-account–scoped restrictive policies accordingly.