Writing · Kubernetes, supply chain, security
Scaling Kyverno to zero makes it delete its own webhooks. That, and four more things I only found because every defence in my supply chain has an attack that must get through without it.
supply-chain-fortress ships one small Go service, and the whole path from its source to a running pod. The release workflow builds the image from pinned inputs, signs it keyless through GitHub’s OIDC token, attaches an SBOM and SLSA provenance, and Kubernetes runs it only if all of that checks out: Kyverno verifies the signature and the SBOM, and a built-in ValidatingAdmissionPolicy refuses images from any other registry.
None of that is new. What I wanted to know is whether it actually holds, so every defence in the repository comes with an attack that CI runs on every pull request. This post is about five things those attacks found that I would not have found by reading documentation, starting with the one I least expected: setting Kyverno’s failurePolicy to Fail does not stop pods getting in when Kyverno is gone.
Three checks per attack
Each attack runs three ways, the same method I used for my Raft tests. First the genuine image must be admitted, because a policy that refuses everything passes every attack test. Then the attack must be refused, and the refusal has to carry the message of the defence meant to stop it, not just any error. Last comes the control: the same attack in a namespace where that one defence is missing, which must be admitted. If the control is refused too, the test proves nothing about the defence.
Every pod goes through real admission with kubectl apply --dry-run=server on a kind cluster, against the production policy files. A small script renders test copies that change only lines marked # render:, such as the registry address and the signer, so the tests cannot drift into testing a lookalike.
failurePolicy: Fail, admitted anyway
The test I wrote for “Kyverno is down” was simple. Scale Kyverno’s admission controller to zero, wait for its pod to go, and submit an unsigned image. With failurePolicy: Fail the API server should refuse what it cannot check; the control namespace, with Ignore, should let it in.
FAIL a09 enforced admitted Kyverno down: unsigned image
FAIL p09 enforced admitted Kyverno down: the genuine image is refused too
ok c09 fail-open admitted control: failurePolicy Ignore lets the unsigned image inBoth namespaces admitted it. The debugging step that dumps webhook configurations on failure printed nothing at all: there were none left. The reason is in Kyverno’s webhook server. When it shuts down, it asks whether it is “going down”, and if so it deletes every webhook configuration it manages:
func (c *runtime) IsGoingDown() bool {
deployment, err := c.getDeployment()
...
if deployment.GetDeletionTimestamp() != nil {
return true
}
return *deployment.Spec.Replicas == 0
}
func (s *server) cleanup(ctx context.Context) {
if s.runtime.IsGoingDown() {
...
deleteVwc() // every ValidatingWebhookConfiguration Kyverno manages
deleteMwc()
}
}That is a deliberate choice, and a reasonable one: a policy engine that fails closed and then gets uninstalled would otherwise leave behind webhooks that block every pod in the cluster. But it changes what failurePolicy: Fail means. It covers a Kyverno that is deployed and cannot answer. It does not cover a Kyverno that was scaled to zero, because by the time anything is admitted there is no webhook left to fail. No error, no event, and pods are admitted unchecked.
Scaling a deployment to zero is not exotic. It is what people do during an incident (“is Kyverno the reason deploys are slow?”), what a script with --all does, and what an autoscaler or a GitOps tool can do by mistake. Anyone with deployments/scale in the kyverno namespace can switch off every image check in the cluster, and the person who does it may not know they have.
A guard Kyverno cannot switch off
Kyverno cannot protect itself from this: when it is gone, its rules are gone. Kubernetes’ built-in ValidatingAdmissionPolicy is evaluated inside the API server, so it does not depend on Kyverno being up. The guard refuses deleting Kyverno’s admission controller and scaling it below one replica:
matchConstraints:
resourceRules:
- apiGroups: [apps]
apiVersions: [v1]
operations: [UPDATE, DELETE]
resources: [deployments, deployments/scale]
resourceNames: [kyverno-admission-controller]
validations:
- expression: request.operation != 'DELETE'
message: Kyverno's admission controller cannot be deleted
- expression: >-
request.operation == 'DELETE' ||
(has(object.spec.replicas) && object.spec.replicas >= 1)
message: Kyverno's admission controller cannot be scaled to zero ...Taking Kyverno down on purpose now means deleting the guard’s binding first, which is a deliberate, audited step rather than a side effect of kubectl scale. The control for this attack does exactly that, and it is the most convincing line in the test output:
ok a14 kyverno denied scale Kyverno to zero (an incident, a script, an autoscaler)
ok a14d kyverno denied delete Kyverno's Deployment
ok c14 enforced admitted control: no guard, Kyverno scaled to zero (0 webhooks left): unsigned image admittedScaling is not the only way to remove the webhooks. Anyone allowed to delete webhook configurations can delete Kyverno’s directly, and Kyverno can do nothing about it: the API server never sends requests for webhook configurations to admission webhooks, so a webhook cannot lock itself in place. Built-in policies are not exempt. A second ValidatingAdmissionPolicy refuses any change to a configuration labelled webhook.kyverno.io/managed-by unless it comes from Kyverno’s own service account, which still needs to manage them:
ok a15 cluster denied delete Kyverno's webhook configurations
ok ok6 cluster admitted Kyverno itself may still delete them
ok c15 cluster admitted control: no guard, a cluster admin deletes Kyverno's webhook configurationsNone of this stops a determined cluster admin, who can delete the guards first. It turns switching Kyverno off from a side effect into a two-step, deliberate act that shows up in the audit log.
The guard’s own bug
The first version of the guard let kubectl scale --replicas=0 straight through. Deleting the deployment was refused; scaling it was not. Its expression was:
request.operation == 'DELETE' || !has(object.spec.replicas) || object.spec.replicas >= 1The idea was that a missing replicas means “use the default”, which is one. That is true of a Deployment. But kubectl scale does not send a Deployment; it sends a Scale object to the /scale subresource, and in Scale the field is a plain integer tagged omitempty. Zero is the one value that is never serialised. So the request that asks for zero replicas arrives with no replicas at all, and !has() waved it through. Without the attack test, this guard would have been decoration.
Unplugging Kyverno is harder than it looks
With scaling ruled out, the test for failurePolicy needed a Kyverno that is deployed but cannot answer. My first attempt pointed its Service at a label no pod has. The endpoints emptied, and Kyverno kept answering: the unsigned image was refused by Kyverno itself, and the genuine one admitted. The API server keeps an open connection to the webhook, and an established connection does not care that the Service no longer selects the pod behind it.
What works is freezing the process. The test sends SIGSTOP to Kyverno from the kind node, so the webhook hangs until its 15 second timeout. The liveness probe would restart it after 35 to 65 seconds, so the three requests run at the same time and the whole test fits inside one timeout:
ok a09 enforced denied Kyverno hung: unsigned image
ok p09 enforced denied Kyverno hung: the genuine image is refused too
ok c09 fail-open admitted control: Kyverno hung, failurePolicy Ignore lets the unsigned image inThe middle line is the price of failing closed, and I wanted it in the output on purpose. While Kyverno is hung, nothing deploys, including your own fix.
Closing one hole opened another
An earlier attack showed that kubectl debug gets past most published policies. It adds an ephemeral container to a pod that is already running, through the pods/ephemeralcontainers subresource, and a policy that matches only pods never sees it. So the policy matched both. Then I added a test that puts the unsigned image in a Deployment, expecting Kyverno’s autogen to refuse it at kubectl apply. The Deployment was admitted.
// CanAutoGen returns false if ... the matching logic matches more than
// one resource and it's not pods
if len(rule.Resources) != 1 || rule.Resources[0] != "pods" {
return false
}Autogen, the feature that writes the same checks for Deployments, Jobs and the other pod controllers, only runs when a policy matches exactly pods. Adding the subresource turned it off, without a warning. The pods themselves would still be refused, but only as a ReplicaSet that quietly never scales up, which is a worse failure to debug than an error at kubectl apply. The fix is two policies: one that matches only pods and gets autogen, and one for the debug subresource.
Who checks the checker
Kyverno decides what runs, so a backdoored Kyverno would make every other check meaningless. The cluster now installs it only from two digests, after verifying them. Kyverno’s v1.19.2 images have no plain cosign signature; what they carry is SLSA provenance signed by slsa-github-generator’s reusable workflow. That detail matters, because the certificate’s subject is the generator’s workflow, which is the same for every project that calls it. Who actually built the image is in two other claims: the calling repository and its ref.
The tests check this against real images. Another project’s image with genuine provenance from the same generator passes a check on the subject alone, and is refused once the repository is pinned. A genuine but older Kyverno release passes a check on the repository, and is refused once the tag is pinned. A modified copy of Kyverno carrying Kyverno’s real provenance document, re-signed by my own CI, passes “signed by a GitHub workflow” and is refused by the full check.
Provenance alone is still not enough. Kyverno’s other images, the CLI and the background controller, have the same repository and tag in their provenance. A check by provenance would accept either of them in place of the admission controller, so the digests are pinned as well, and the cluster setup fails if any Kyverno pod runs anything else.
What I am keeping
- Read failure settings as claims, then test them.
failurePolicy: Failis true for one way of being down and false for the most common one. - Test the guard too. The first version of the guard against scale-to-zero did not guard against scale-to-zero, because of how a zero is serialised.
- A fix can switch off a neighbouring feature without a word. Matching one more subresource disabled autogen. Only a test of the neighbour shows it.
- A shared signer proves nothing by itself. When a reusable workflow signs for everyone, check who called it, and from which ref.
All 31 cluster checks, eight checks on Kyverno’s own images, and five workflow attacks run on every pull request at github.com/Kage7047/supply-chain-fortress. To verify a released image yourself:
cosign verify ghcr.io/kage7047/supply-chain-fortress/app:<commit sha> \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
--certificate-identity https://github.com/Kage7047/supply-chain-fortress/.github/workflows/release.yml@refs/heads/main