initcontainer-read-only-root-filesystem
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.
(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.
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.
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.
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.yamlOr 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 ./manifestsOr against every namespace in the cluster your kubeconfig points at:
$ kubeapt validate --policy-name initcontainer-read-only-root-filesystem -A