2019-11-08 20:04:42 -08:00
# Diallow privileged containers
2019-10-23 14:06:03 -07:00
2019-10-23 15:36:37 -07:00
Privileged containers are defined as any container where the container uid 0 is mapped to the host’ s uid 0. A process within a privileged container can get unrestricted host access. With `securityContext.allowPrivilegeEscalation` enabled, a process can gain privileges from its parent.
2019-10-23 14:06:03 -07:00
2019-11-08 20:04:42 -08:00
To disallow privileged containers and privilege escalation, run pod containers with `securityContext.privileged` set to `false` and `securityContext.allowPrivilegeEscalation` set to `false` .
2019-10-23 14:06:03 -07:00
## Policy YAML
2019-11-08 20:04:42 -08:00
[disallow_privileged.yaml ](best_practices/disallow_privileged.yaml )
2019-10-23 14:06:03 -07:00
````yaml
2019-11-13 13:56:20 -08:00
apiVersion: kyverno.io/v1
2019-10-23 14:06:03 -07:00
kind: ClusterPolicy
metadata:
2019-11-08 20:04:42 -08:00
name: disallow-privileged
2019-10-23 14:06:03 -07:00
spec:
rules:
2019-11-08 20:04:42 -08:00
- name: validate-privileged
2019-10-23 14:06:03 -07:00
match:
resources:
kinds:
- Pod
validate:
2019-11-08 20:04:42 -08:00
message: "Privileged mode is not allowed. Set privileged to false"
2019-12-30 16:45:22 -08:00
pattern:
spec:
2019-11-08 20:04:42 -08:00
containers:
2019-12-30 16:45:22 -08:00
- =(securityContext):
# https://github.com/kubernetes/api/blob/7dc09db16fb8ff2eee16c65dc066c85ab3abb7ce/core/v1/types.go#L5707 -L5711
# k8s default to false
=(privileged): false
2019-11-08 20:04:42 -08:00
- name: validate-allowPrivilegeEscalation
match:
resources:
kinds:
- Pod
validate:
message: "Privileged mode is not allowed. Set allowPrivilegeEscalation to false"
2019-12-30 16:45:22 -08:00
pattern:
spec:
2019-10-23 14:06:03 -07:00
containers:
2019-12-30 16:45:22 -08:00
- securityContext:
# https://github.com/kubernetes/api/blob/7dc09db16fb8ff2eee16c65dc066c85ab3abb7ce/core/v1/types.go#L5754
2019-10-23 14:06:03 -07:00
allowPrivilegeEscalation: false
````