Sub topic 13
Preventing privilege escalation: escalate, bind, and impersonate
This chapter assumes you can write Roles, ClusterRoles, and bindings, and that you know kubectl auth can-i. It covers three checks that stop a user from gaining more access than they were given.
The problem
RBAC objects are API resources. If a user can create Roles and RoleBindings, they could write themselves a Role with every permission and bind it to themselves. The API server blocks this. Permission to edit RBAC objects does not include permission to hand out access you don't hold.
The two built-in checks
The API server applies these checks when someone writes an RBAC object:
- Creating or updating a Role or ClusterRole. The user must already hold every permission in the rules they write, at the same scope. Alternatively, they must have the
escalateverb onrolesorclusterroles. - Creating or updating a binding. The user must already hold every permission in the referenced role. Alternatively, they must have the
bindverb on that role.
These checks apply on top of the normal create and update permissions. Both are needed.
The escalate and bind verbs
escalate and bind bypass the checks above, so grant them rarely. The bind verb can be limited to named roles with resourceNames:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pod-reader-binder
rules:
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["rolebindings"]
verbs: ["create", "get", "list"]
- apiGroups: ["rbac.authorization.k8s.io"]
resources: ["clusterroles"]
verbs: ["bind"]
resourceNames: ["pod-reader"]A user with this ClusterRole can create RoleBindings that reference only a ClusterRole named pod-reader, even if they can't read pods themselves. They can't bind cluster-admin.
The impersonate verb
The impersonate verb lets a user act as another identity with --as and --as-group. The API server then evaluates the request with that identity's permissions. Impersonating a powerful user is equivalent to having their access. You can grant it on users, groups, serviceaccounts, and userextras/<key>. Use resourceNames to limit which identities can be impersonated:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: impersonate-jane
rules:
- apiGroups: [""]
resources: ["users"]
verbs: ["impersonate"]
resourceNames: ["jane"]kubectl get pods --as jane
kubectl auth can-i list secrets --as janeImpersonation is also how administrators test other identities' permissions, so give it only to administrators.
Related risks
Some permissions are escalation paths even without these verbs:
| Permission | Why it is risky |
|---|---|
| `create` on `pods` in a namespace | The pod can run as any service account in that namespace and mount its secrets. |
| `get` or `list` on `secrets` | Reveals service account tokens and credentials. |
| `escalate` or `bind` with no `resourceNames` | Allows granting any permission, including `cluster-admin`. |
| `impersonate` on `groups` | Allows acting as members of a group such as `system:masters`. |
Practical steps
- Audit who holds the three verbs by listing ClusterRoles and Roles that mention
escalate,bind, orimpersonate. - Always set
resourceNameswhen grantingbindorimpersonate. - Avoid wildcards on RBAC resources.
- Give
createonrolebindingsonly to people who need to delegate access.
Exercise
A user has create on rolebindings in the namespace dev but holds no other permissions. They try to bind the ClusterRole edit to themselves in dev and get Forbidden. Why, and what is the narrowest change that lets them bind only view?
Show answer
The API server requires the user to hold all permissions in the referenced role, or to have the bind verb on it. They hold neither for edit. The narrowest fix is a rule granting bind on clusterroles with resourceNames: ["view"]. This lets them create bindings to view only.