← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Runs With a Writable Root Filesystem

container-read-only-root-filesystem

severityModerate resourceContainers productKubernetes bundles1

A writable 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.os) &&
 has(object.spec.os.name) &&
 object.spec.os.name.lowerAscii() == "windows") ||
object.spec.containers.all(c,
  has(c.securityContext) &&
  has(c.securityContext.readOnlyRootFilesystem) &&
  c.securityContext.readOnlyRootFilesystem == true
)

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

Remediation

Set readOnlyRootFilesystem to true on every container and mount an emptyDir volume over each path the process genuinely needs to write. Field: spec.containers[*].securityContext.readOnlyRootFilesystem.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: container-read-only-root-filesystem
  annotations:
    kubeapt.io/uuid: "4ed91688-4092-4429-b68d-a29c21f1267e"
    security.kubeapt.io/displayName: "Runs With a Writable Root Filesystem"
    security.kubeapt.io/description: "A writable 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: "Containers"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Set readOnlyRootFilesystem to true on every container and mount an emptyDir volume over each path the process genuinely needs to write. Field: spec.containers[*].securityContext.readOnlyRootFilesystem."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  validations:
  - expression: |
      (has(object.spec.os) &&
       has(object.spec.os.name) &&
       object.spec.os.name.lowerAscii() == "windows") ||
      object.spec.containers.all(c,
        has(c.securityContext) &&
        has(c.securityContext.readOnlyRootFilesystem) &&
        c.securityContext.readOnlyRootFilesystem == true
      )
    message: |
      spec.containers[*].securityContext.readOnlyRootFilesystem must be set to true.

Save it as container-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 ./container-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 container-read-only-root-filesystem -r ./manifests

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

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