initcontainer-image-digest
An image reference without a digest resolves through a mutable tag, so registry content can be replaced between pulls and the init container silently starts running attacker-supplied code on the next restart or reschedule.
variables.isWindows || !has(object.spec.initContainers) || object.spec.initContainers.all(ic,
ic.image.matches(".*@sha256:[0-9A-Fa-f]{64}")
)
spec.initContainers[*].image must be pinned by an immutable @sha256 digest.
isWindows
has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"
Replace the mutable tag with an immutable digest reference of the form registry/image@sha256:<64-hex-digest>. Field: spec.initContainers[*].image.
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: initcontainer-image-digest
annotations:
kubeapt.io/uuid: "b6f03623-4d26-40db-a300-f5d2c08cdd6e"
security.kubeapt.io/displayName: "Runs an Image Not Pinned to a Digest"
security.kubeapt.io/description: "An image reference without a digest resolves through a mutable tag, so registry content can be replaced between pulls and the init container silently starts running attacker-supplied code on the next restart or reschedule."
security.kubeapt.io/resource: "InitContainers"
security.kubeapt.io/severity: "Moderate"
security.kubeapt.io/remediation: "Replace the mutable tag with an immutable digest reference of the form registry/image@sha256:<64-hex-digest>. Field: spec.initContainers[*].image."
security.kubeapt.io/product: "Kubernetes"
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups:
- ''
apiVersions:
- v1
operations:
- CREATE
- UPDATE
resources:
- pods
variables:
- name: isWindows
expression: |
has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"
validations:
- expression: |
variables.isWindows || !has(object.spec.initContainers) || object.spec.initContainers.all(ic,
ic.image.matches(".*@sha256:[0-9A-Fa-f]{64}")
)
message: |
spec.initContainers[*].image must be pinned by an immutable @sha256 digest.Save it as initcontainer-image-digest.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-image-digest.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-image-digest -r ./manifestsOr against every namespace in the cluster your kubeconfig points at:
$ kubeapt validate --policy-name initcontainer-image-digest -A