← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Runs an Image Not Pinned to a Digest

initcontainer-image-digest

severityModerate resourceInitContainers productKubernetes bundles1

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.

Rejects unless

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.

Variables

isWindows

has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"

Remediation

Replace the mutable tag with an immutable digest reference of the form registry/image@sha256:<64-hex-digest>. Field: spec.initContainers[*].image.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

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.

Validate with kubeapt

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

$ kubeapt validate -p ./initcontainer-image-digest.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-image-digest -r ./manifests

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

$ kubeapt validate --policy-name initcontainer-image-digest -A