← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

preStop TCP Hook Targets an Arbitrary Host

container-prestop-tcpsocket-host

severityLow resourceContainers productKubernetes bundles2

Naming another host in a preStop TCP hook points container shutdown at an off-pod address through a handler the kubelet does not support, so the hook fails and cleanup never runs.

Rejects unless

object.spec.containers.all(c,
  !has(c.lifecycle) ||
  !has(c.lifecycle.preStop) ||
  !has(c.lifecycle.preStop.tcpSocket) ||
  !has(c.lifecycle.preStop.tcpSocket.host) ||
  c.lifecycle.preStop.tcpSocket.host == ""
)

spec.containers[*].lifecycle.preStop.tcpSocket.host must be undefined or empty ("").

Remediation

Remove the host field or set it to an empty string, and prefer an exec or httpGet handler since tcpSocket hooks are unsupported. Field: spec.containers[*].lifecycle.preStop.tcpSocket.host.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: container-prestop-tcpsocket-host
  annotations:
    kubeapt.io/uuid: "6a266584-351e-4543-a289-2860d9ae3296"
    security.kubeapt.io/displayName: "preStop TCP Hook Targets an Arbitrary Host"
    security.kubeapt.io/description: "Naming another host in a preStop TCP hook points container shutdown at an off-pod address through a handler the kubelet does not support, so the hook fails and cleanup never runs."
    security.kubeapt.io/resource: "Containers"
    security.kubeapt.io/severity: "Low"
    security.kubeapt.io/remediation: "Remove the host field or set it to an empty string, and prefer an exec or httpGet handler since tcpSocket hooks are unsupported. Field: spec.containers[*].lifecycle.preStop.tcpSocket.host."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  validations:
  - expression: |
      object.spec.containers.all(c,
        !has(c.lifecycle) ||
        !has(c.lifecycle.preStop) ||
        !has(c.lifecycle.preStop.tcpSocket) ||
        !has(c.lifecycle.preStop.tcpSocket.host) ||
        c.lifecycle.preStop.tcpSocket.host == ""
      )
    message: |
      spec.containers[*].lifecycle.preStop.tcpSocket.host must be undefined or empty ("").

Save it as container-prestop-tcpsocket-host.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./container-prestop-tcpsocket-host.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name container-prestop-tcpsocket-host -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name container-prestop-tcpsocket-host -A