Restrict Automount SA Token
Description
Kubernetes automatically mounts ServiceAccount
credentials in each Pod. The ServiceAccount
may be assigned roles allowing Pods to access API resources. Blocking this ability is an extension of the least privilege best practice and should be followed if Pods do not need to speak to the API server to function. This policy ensures that mounting of these ServiceAccount tokens is blocked.
Risks
In Kubernetes, there are two key security layers: authentication and authorization (authz). Authentication ensures that the pod or user is who they claim to be, while authorization determines what actions they’re allowed to perform. When a pod is created, it automatically mounts a service account token, which allows it to interact with the Kubernetes API server for authentication purposes. However, by default, the pod doesn’t have permission (via authorization) to do much with that access.
The problem arises when an attacker gains control of a pod. Even though the attacker might not have initial permissions (due to the security of the authz layer), if they manage to bypass authorization, they can exploit the service account token to escalate privileges and perform unauthorized actions across the cluster. For example, an attacker could perform API requests like creating or deleting pods.
Kyverno Policy
Refer to the Nirmata curated policies - restrict-automount-sa-token.
Resource Example
Below is an example of a Deployment
resource that has automountServiceAccountToken
set to false
.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: busybox
name: gooddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
automountServiceAccountToken: false
containers:
- name: busybox
image: busybox:1.35
Below is another example of a CronJob
resource enforcing this policy.
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
name: goodcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox:1.35
automountServiceAccountToken: false
restartPolicy: OnFailure