← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Grants Write Access to ServiceAccounts

clusterrole-serviceaccounts-write

severityHigh resourceClusterRoles productKubernetes bundles1

Creating or replacing ServiceAccounts anywhere in the cluster lets an attacker re-create an identity that existing bindings already point at and take over its tokens, inheriting every permission bound to that name.

Rejects unless

!has(object.rules) || object.rules.all(r,
  !(has(r.apiGroups) && has(r.resources) && has(r.verbs) &&
    r.apiGroups.exists(ag, ag == "") &&
    r.resources.exists(res, res == "serviceaccounts") &&
    r.verbs.exists(v, v in ["create","update","patch","delete"]))
)

ClusterRoles must not grant create/update/patch/delete on ServiceAccounts without strict scoping.

Remediation

Remove create, update, patch and delete from every rule covering serviceaccounts in the core API group, keeping get, list and watch only where a workload needs them. Fields: rules[*].apiGroups, rules[*].resources, rules[*].verbs.

Applies to

  • clusterroles · rbac.authorization.k8s.io/v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: clusterrole-serviceaccounts-write
  annotations:
    kubeapt.io/uuid: "c2ceea3b-e377-4be5-ba9d-0b86ffdb3030"
    security.kubeapt.io/displayName: "Grants Write Access to ServiceAccounts"
    security.kubeapt.io/description: "Creating or replacing ServiceAccounts anywhere in the cluster lets an attacker re-create an identity that existing bindings already point at and take over its tokens, inheriting every permission bound to that name."
    security.kubeapt.io/resource: "ClusterRoles"
    security.kubeapt.io/severity: "High"
    security.kubeapt.io/remediation: "Remove create, update, patch and delete from every rule covering serviceaccounts in the core API group, keeping get, list and watch only where a workload needs them. Fields: rules[*].apiGroups, rules[*].resources, rules[*].verbs."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - rbac.authorization.k8s.io
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - clusterroles
  validations:
  - expression: |
      !has(object.rules) || object.rules.all(r,
        !(has(r.apiGroups) && has(r.resources) && has(r.verbs) &&
          r.apiGroups.exists(ag, ag == "") &&
          r.resources.exists(res, res == "serviceaccounts") &&
          r.verbs.exists(v, v in ["create","update","patch","delete"]))
      )
    message: |
      ClusterRoles must not grant create/update/patch/delete on ServiceAccounts without strict scoping.

Save it as clusterrole-serviceaccounts-write.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 ./clusterrole-serviceaccounts-write.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 clusterrole-serviceaccounts-write -r ./manifests

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

$ kubeapt validate --policy-name clusterrole-serviceaccounts-write -A