← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

postStart TCP Hook Targets an Arbitrary Host

container-poststart-tcpsocket-host

severityLow resourceContainers productKubernetes bundles2

Naming another host in a postStart TCP hook points container start at an off-pod address through a handler the kubelet does not support, so the hook fails and the container is killed.

Rejects unless

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

spec.containers[*].lifecycle.postStart.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.postStart.tcpSocket.host.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: container-poststart-tcpsocket-host
  annotations:
    kubeapt.io/uuid: "11bc67f9-eb75-45b1-a5a2-9df6049e98dd"
    security.kubeapt.io/displayName: "postStart TCP Hook Targets an Arbitrary Host"
    security.kubeapt.io/description: "Naming another host in a postStart TCP hook points container start at an off-pod address through a handler the kubelet does not support, so the hook fails and the container is killed."
    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.postStart.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.postStart) ||
        !has(c.lifecycle.postStart.tcpSocket) ||
        !has(c.lifecycle.postStart.tcpSocket.host) ||
        c.lifecycle.postStart.tcpSocket.host == ""
      )
    message: |
      spec.containers[*].lifecycle.postStart.tcpSocket.host must be undefined or empty ("").

Save it as container-poststart-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-poststart-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-poststart-tcpsocket-host -r ./manifests

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

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