← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Leaves ServiceAccount Token Automounting Enabled

pod-automount-serviceaccount-token

severityModerate resourcePods productKubernetes bundles1

An automounted service account token lands in every container filesystem, so any code execution or path traversal bug hands an attacker a live API credential for enumerating and acting on cluster resources.

Rejects unless

has(object.spec.automountServiceAccountToken) && object.spec.automountServiceAccountToken == false

spec.automountServiceAccountToken must be explicitly set to false.

Remediation

Set spec.automountServiceAccountToken to false on pods that do not call the Kubernetes API, and mount a token only where it is actually needed. Field: spec.automountServiceAccountToken.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: pod-automount-serviceaccount-token
  annotations:
    kubeapt.io/uuid: "75834e82-5933-4ed1-a99c-0c7cf1b6dd23"
    security.kubeapt.io/displayName: "Leaves ServiceAccount Token Automounting Enabled"
    security.kubeapt.io/description: "An automounted service account token lands in every container filesystem, so any code execution or path traversal bug hands an attacker a live API credential for enumerating and acting on cluster resources."
    security.kubeapt.io/resource: "Pods"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Set spec.automountServiceAccountToken to false on pods that do not call the Kubernetes API, and mount a token only where it is actually needed. Field: spec.automountServiceAccountToken."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  validations:
  - expression: |
      has(object.spec.automountServiceAccountToken) && object.spec.automountServiceAccountToken == false
    message: |
      spec.automountServiceAccountToken must be explicitly set to false.

Save it as pod-automount-serviceaccount-token.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 ./pod-automount-serviceaccount-token.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 pod-automount-serviceaccount-token -r ./manifests

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

$ kubeapt validate --policy-name pod-automount-serviceaccount-token -A