← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Runs With a Writable Root Filesystem

initcontainer-read-only-root-filesystem

severityModerate resourceInitContainers productKubernetes bundles1

A writable init 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") ||
!has(object.spec.initContainers) || object.spec.initContainers.all(ic,
  has(ic.securityContext) &&
  has(ic.securityContext.readOnlyRootFilesystem) &&
  ic.securityContext.readOnlyRootFilesystem == true
)

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

Remediation

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

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: initcontainer-read-only-root-filesystem
  annotations:
    kubeapt.io/uuid: "67aa9a32-8729-4511-b824-61f891c4e41b"
    security.kubeapt.io/displayName: "Runs With a Writable Root Filesystem"
    security.kubeapt.io/description: "A writable init 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: "InitContainers"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Set readOnlyRootFilesystem to true on every init container and mount an emptyDir volume over each path the process genuinely needs to write. Field: spec.initContainers[*].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") ||
      !has(object.spec.initContainers) || object.spec.initContainers.all(ic,
        has(ic.securityContext) &&
        has(ic.securityContext.readOnlyRootFilesystem) &&
        ic.securityContext.readOnlyRootFilesystem == true
      )
    message: |
      spec.initContainers[*].securityContext.readOnlyRootFilesystem must be set to true.

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

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

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