diff --git a/packages/system/ingress-nginx/Makefile b/packages/system/ingress-nginx/Makefile index 75dc0392c..cd84bbc39 100644 --- a/packages/system/ingress-nginx/Makefile +++ b/packages/system/ingress-nginx/Makefile @@ -16,3 +16,9 @@ update: patch --no-backup-if-mismatch -p4 < patches/disable-ca-key-rotation.patch rm -f charts/ingress-nginx/templates/controller-deployment.yaml.orig rm -rf charts/ingress-nginx/changelog/ + +# --with-subchart=false: the vendored chart carries upstream's own suites, which +# assert against upstream default values and fail under the overrides in our +# values.yaml. They are upstream's contract, not ours; run only our own. +test: + helm unittest --with-subchart=false . diff --git a/packages/system/ingress-nginx/templates/admission-webhook-networkpolicy.yaml b/packages/system/ingress-nginx/templates/admission-webhook-networkpolicy.yaml new file mode 100644 index 000000000..526e0251e --- /dev/null +++ b/packages/system/ingress-nginx/templates/admission-webhook-networkpolicy.yaml @@ -0,0 +1,69 @@ +{{- if .Values.admissionWebhookPolicy.enabled }} +{{- $inc := index .Values "ingress-nginx" | default dict }} +{{- $webhookPort := dig "controller" "admissionWebhooks" "port" 8443 $inc }} +--- +# The ingress-nginx admission webhook only ever needs to be reachable by the +# API server. Leaving it reachable from every pod in the cluster is avoidable +# attack surface: any flaw in the webhook then becomes exploitable by any +# workload in any namespace, which is precisely what made CVE-2025-1974 +# (IngressNightmare) critical rather than merely serious. +# +# This policy is deny-only and explicitly disables default-deny. A Cilium rule +# that carries an ingress section normally puts the selected endpoints behind a +# default-deny, which here would mean enumerating every port the controller +# serves (80, 443, metrics, the protobuf exporter, any tcp/udp services) and +# silently breaking traffic on the first one forgotten. With +# enableDefaultDeny.ingress=false the rule only subtracts access to the webhook +# port and leaves everything else exactly as it was. +# +# Two legitimate callers are carved out of the deny: +# +# * host-network sources, which are not Cilium endpoints and so are never +# matched by fromEndpoints. This covers a management cluster, where the +# kube-apiserver runs on the host network. +# +# * the konnectivity agent. For a Kamaji-hosted tenant cluster the API server +# lives outside the cluster and reaches in-cluster webhooks through the +# konnectivity tunnel: Kamaji defines only the "cluster" egress selection +# (internal/resources/konnectivity/egress_selector_configuration_resource.go), +# and webhook calls fall under it. The connection therefore leaves the +# tunnel from the agent pod in kube-system, not from the API server address, +# so a fromEntities[kube-apiserver] allow would not match it and every +# Ingress create/update in the tenant cluster would start failing. +apiVersion: cilium.io/v2 +kind: CiliumNetworkPolicy +metadata: + name: ingress-nginx-admission-webhook + namespace: {{ .Release.Namespace }} +spec: + endpointSelector: + matchLabels: + app.kubernetes.io/name: ingress-nginx + app.kubernetes.io/component: controller + enableDefaultDeny: + ingress: false + egress: false + ingressDeny: + # Every pod in the cluster except the konnectivity agent. The namespace key + # is matched with Exists so the rule spans all namespaces: a plain + # fromEndpoints in a namespaced CiliumNetworkPolicy would only ever select + # pods sharing this policy's namespace. + - fromEndpoints: + - matchExpressions: + - key: k8s:io.kubernetes.pod.namespace + operator: Exists + - key: k8s-app + operator: NotIn + values: + - konnectivity-agent + toPorts: + - ports: + - port: {{ $webhookPort | quote }} + protocol: TCP + - fromEntities: + - world + toPorts: + - ports: + - port: {{ $webhookPort | quote }} + protocol: TCP +{{- end }} diff --git a/packages/system/ingress-nginx/tests/admission_webhook_networkpolicy_test.yaml b/packages/system/ingress-nginx/tests/admission_webhook_networkpolicy_test.yaml new file mode 100644 index 000000000..34fd0450b --- /dev/null +++ b/packages/system/ingress-nginx/tests/admission_webhook_networkpolicy_test.yaml @@ -0,0 +1,82 @@ +suite: ingress-nginx admission webhook exposure +release: + name: ingress-nginx + namespace: cozy-ingress-nginx +templates: + - templates/admission-webhook-networkpolicy.yaml +tests: + - it: denies pod traffic to the admission webhook port + # :8443 is the admission webhook. Reachability from arbitrary pods is what + # turned CVE-2025-1974 from a webhook bug into unauthenticated in-cluster + # RCE, so the deny on this port is the whole point of the policy. + asserts: + - equal: + path: spec.ingressDeny[0].toPorts[0].ports[0].port + value: "8443" + - equal: + path: spec.ingressDeny[0].toPorts[0].ports[0].protocol + value: TCP + + - it: spans every namespace rather than only the release namespace + # A bare fromEndpoints in a namespaced CiliumNetworkPolicy selects only + # pods in the policy's own namespace, which would leave the webhook open + # to every other namespace — the exact gap being closed here. + asserts: + - contains: + path: spec.ingressDeny[0].fromEndpoints[0].matchExpressions + content: + key: k8s:io.kubernetes.pod.namespace + operator: Exists + + - it: exempts the konnectivity agent so tenant-cluster admission keeps working + # On a Kamaji-hosted tenant cluster the API server is outside the cluster + # and reaches webhooks through the konnectivity tunnel, so the connection + # originates from the agent pod. Denying it would make every Ingress + # create/update in the tenant cluster fail on the webhook call. + asserts: + - contains: + path: spec.ingressDeny[0].fromEndpoints[0].matchExpressions + content: + key: k8s-app + operator: NotIn + values: + - konnectivity-agent + + - it: denies world traffic to the admission webhook port + asserts: + - contains: + path: spec.ingressDeny[1].fromEntities + content: world + - equal: + path: spec.ingressDeny[1].toPorts[0].ports[0].port + value: "8443" + + - it: does not put the controller behind a default deny + # The controller also serves 80/443, metrics and the protobuf exporter. + # Enabling default-deny here would require enumerating all of them and + # would break traffic on the first one omitted. + asserts: + - equal: + path: spec.enableDefaultDeny.ingress + value: false + - equal: + path: spec.enableDefaultDeny.egress + value: false + - isNull: + path: spec.ingress + + - it: selects only the controller pods + asserts: + - equal: + path: spec.endpointSelector.matchLabels["app.kubernetes.io/component"] + value: controller + - equal: + path: spec.endpointSelector.matchLabels["app.kubernetes.io/name"] + value: ingress-nginx + + - it: can be disabled + set: + admissionWebhookPolicy.enabled: false + asserts: + - hasDocuments: + count: 0 diff --git a/packages/system/ingress-nginx/values.yaml b/packages/system/ingress-nginx/values.yaml index a58aeaa12..c5a1468fd 100644 --- a/packages/system/ingress-nginx/values.yaml +++ b/packages/system/ingress-nginx/values.yaml @@ -102,3 +102,9 @@ ingress-nginx: cpu: 10m memory: 20Mi +# Deny pod-to-webhook traffic on the admission port, leaving it reachable only +# by the API server (directly on a management cluster, or through the +# konnectivity agent on a Kamaji-hosted tenant cluster). Requires Cilium. +admissionWebhookPolicy: + enabled: true +