← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Runs With a Writable Root Filesystem

ephemeralcontainer-read-only-root-filesystem

severityModerate resourceEphemeralContainers productKubernetes bundles1

A writable ephemeral container root filesystem lets an intruder drop tooling, overwrite application binaries, and rewrite configuration in place, leaving a foothold in the running pod that nothing in the image would reveal.

Rejects unless

!has(object.spec.ephemeralContainers) || object.spec.ephemeralContainers.all(ec,
  has(ec.securityContext) &&
  has(ec.securityContext.readOnlyRootFilesystem) &&
  ec.securityContext.readOnlyRootFilesystem == true
)

spec.ephemeralContainers[*].securityContext.readOnlyRootFilesystem must be set to true.

Remediation

Set readOnlyRootFilesystem to true on every ephemeral container and mount an existing pod volume over any path the debug tooling must write, since a running pod cannot gain new volumes. Field: spec.ephemeralContainers[*].securityContext.readOnlyRootFilesystem.

Applies to

  • pods/ephemeralcontainers · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: ephemeralcontainer-read-only-root-filesystem
  annotations:
    kubeapt.io/uuid: "d1b97c40-d989-4e75-b984-dab87225265c"
    security.kubeapt.io/displayName: "Runs With a Writable Root Filesystem"
    security.kubeapt.io/description: "A writable ephemeral container root filesystem lets an intruder drop tooling, overwrite application binaries, and rewrite configuration in place, leaving a foothold in the running pod that nothing in the image would reveal."
    security.kubeapt.io/resource: "EphemeralContainers"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Set readOnlyRootFilesystem to true on every ephemeral container and mount an existing pod volume over any path the debug tooling must write, since a running pod cannot gain new volumes. Field: spec.ephemeralContainers[*].securityContext.readOnlyRootFilesystem."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods/ephemeralcontainers
  validations:
  - expression: |
      !has(object.spec.ephemeralContainers) || object.spec.ephemeralContainers.all(ec,
        has(ec.securityContext) &&
        has(ec.securityContext.readOnlyRootFilesystem) &&
        ec.securityContext.readOnlyRootFilesystem == true
      )
    message: |
      spec.ephemeralContainers[*].securityContext.readOnlyRootFilesystem must be set to true.

Save it as ephemeralcontainer-read-only-root-filesystem.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./ephemeralcontainer-read-only-root-filesystem.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name ephemeralcontainer-read-only-root-filesystem -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name ephemeralcontainer-read-only-root-filesystem -A