role-serviceaccounts-write
Creating or replacing ServiceAccounts in the namespace 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.
!has(object.rules) || object.rules.all(r,
!(r.apiGroups.exists(ag, ag == "") &&
r.resources.exists(res, res == "serviceaccounts") &&
r.verbs.exists(v, v in ["create","update","patch","delete"]))
)
Roles must not grant create/update/patch/delete on ServiceAccounts without strict scoping.
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.
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: role-serviceaccounts-write
annotations:
kubeapt.io/uuid: "00f4dd66-8570-4413-a279-b00a8969ab51"
security.kubeapt.io/displayName: "Grants Write Access to ServiceAccounts"
security.kubeapt.io/description: "Creating or replacing ServiceAccounts in the namespace 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: "Roles"
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:
- roles
validations:
- expression: |
!has(object.rules) || object.rules.all(r,
!(r.apiGroups.exists(ag, ag == "") &&
r.resources.exists(res, res == "serviceaccounts") &&
r.verbs.exists(v, v in ["create","update","patch","delete"]))
)
message: |
Roles must not grant create/update/patch/delete on ServiceAccounts without strict scoping.Save it as role-serviceaccounts-write.yaml — the commands below assume that name.
Check a workload against this one policy, before anything is installed on a cluster.
$ kubeapt validate -p ./role-serviceaccounts-write.yaml -r ./workload.yamlOr without the file, from the published policy set — -r takes a directory too:
$ kubeapt policies download && kubeapt validate --policy-name role-serviceaccounts-write -r ./manifestsOr against every namespace in the cluster your kubeconfig points at:
$ kubeapt validate --policy-name role-serviceaccounts-write -A