All Policies

Ensure Read Only hostPath in CEL expressions

Pods which are allowed to mount hostPath volumes in read/write mode pose a security risk even if confined to a "safe" file system on the host and may escape those confines (see https://blog.aquasec.com/kubernetes-security-pod-escape-log-mounts). The only true way to ensure safety is to enforce that all Pods mounting hostPath volumes do so in read only mode. This policy checks all containers for any hostPath volumes and ensures they are explicitly mounted in readOnly mode.

Policy Definition

/other-cel/ensure-readonly-hostpath/ensure-readonly-hostpath.yaml

1apiVersion: kyverno.io/v1 2kind: ClusterPolicy 3metadata: 4 name: ensure-readonly-hostpath 5 annotations: 6 policies.kyverno.io/title: Ensure Read Only hostPath in CEL expressions 7 policies.kyverno.io/category: Other in CEL 8 policies.kyverno.io/severity: medium 9 policies.kyverno.io/minversion: 1.11.0 10 kyverno.io/kyverno-version: 1.11.0 11 kyverno.io/kubernetes-version: "1.26-1.27" 12 policies.kyverno.io/subject: Pod 13 policies.kyverno.io/description: >- 14 Pods which are allowed to mount hostPath volumes in read/write mode pose a security risk 15 even if confined to a "safe" file system on the host and may escape those confines (see 16 https://blog.aquasec.com/kubernetes-security-pod-escape-log-mounts). The only true way 17 to ensure safety is to enforce that all Pods mounting hostPath volumes do so in read only 18 mode. This policy checks all containers for any hostPath volumes and ensures they are 19 explicitly mounted in readOnly mode. 20spec: 21 background: false 22 validationFailureAction: Audit 23 rules: 24 - name: ensure-hostpaths-readonly 25 match: 26 any: 27 - resources: 28 kinds: 29 - Pod 30 operations: 31 - CREATE 32 - UPDATE 33 validate: 34 cel: 35 variables: 36 - name: allContainers 37 expression: "object.spec.containers + object.spec.?initContainers.orValue([]) + object.spec.?ephemeralContainers.orValue([])" 38 - name: hostPathVolumes 39 expression: "object.spec.?volumes.orValue([]).filter(volume, has(volume.hostPath))" 40 expressions: 41 - expression: >- 42 variables.hostPathVolumes.all(hostPath, variables.allContainers.all(container, 43 container.volumeMounts.orValue([]).all(volume, (hostPath.name != volume.name) || volume.?readOnly.orValue(false) == true))) 44 message: All hostPath volumes must be mounted as readOnly. 45
yaml