All Policies

Disallow hostPorts Range (Alternate)

Access to host ports allows potential snooping of network traffic and should not be allowed by requiring host ports be undefined (recommended) or at minimum restricted to a known list. This policy ensures the `hostPort` field, if defined, is set to either a port in the specified range or to a value of zero. This policy is mutually exclusive of the disallow-host-ports policy. Note that Kubernetes Pod Security Admission does not support the host port range rule.

Policy Definition

/pod-security/baseline/disallow-host-ports-range/disallow-host-ports-range.yaml

 1apiVersion: kyverno.io/v1
 2kind: ClusterPolicy
 3metadata:
 4  name: disallow-host-ports-range
 5  annotations:
 6    policies.kyverno.io/title: Disallow hostPorts Range (Alternate)
 7    policies.kyverno.io/category: Pod Security Standards (Baseline)
 8    policies.kyverno.io/severity: medium
 9    policies.kyverno.io/subject: Pod
10    kyverno.io/kyverno-version: 1.6.0
11    policies.kyverno.io/minversion: 1.6.0
12    kyverno.io/kubernetes-version: "1.22-1.23"
13    policies.kyverno.io/description: >-
14      Access to host ports allows potential snooping of network traffic and should not be
15      allowed by requiring host ports be undefined (recommended) or at minimum restricted to a known list.
16      This policy ensures the `hostPort` field, if defined, is set to either a port in the specified range
17      or to a value of zero. This policy is mutually exclusive of the disallow-host-ports policy.
18      Note that Kubernetes Pod Security Admission does not support the host port range rule.
19spec:
20  validationFailureAction: Audit
21  background: true
22  rules:
23    - name: host-port-range
24      match:
25        any:
26        - resources:
27            kinds:
28              - Pod
29      preconditions:
30        all:
31        - key: "{{ request.operation }}"
32          operator: NotEquals
33          value: DELETE
34      validate:
35        message: >-
36          The only permitted hostPorts are in the range 5000-6000 or 0.
37        deny:
38          conditions:
39            all:
40            - key: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].ports[].hostPort }}"
41              operator: AnyNotIn
42              value: 5000-6000
43            - key: "{{ request.object.spec.[ephemeralContainers, initContainers, containers][].ports[].hostPort }}"
44              operator: AnyNotIn
45              value:
46              - 0