Hot-Patching in the Agentic CVE Age

· wilkens.io ·

8 min read Original article ↗

The window between a kernel CVE going public and a working exploit can be measured in hours, with AI making exploit development faster. Vendor patch cadence does not always keep up. Supported kernels may get a fix quickly, especially after coordinated disclosure. Kernels a few releases behind may wait longer or get nothing, and the machines running them are exactly the ones nobody wanted to reboot in the first place.

This gap is where many of us currently live. We should be better at this, I know. For suitable fixes, hot-patching can close a specific hole on a running kernel the same day. The tooling I use is public at github.com/nwilkens/linux-hotpatch.

The options when you cannot wait

When a CVE lands and you cannot wait for the vendor's timeline, you have four options.

  1. Mitigate. Turn the feature off. A sysctl, a module blacklist, a firewall rule at the edge. Cheapest, fastest, though sometimes impossible.

  2. Isolate. Move the exposure: put the host behind something that strips the trigger, or move the workload. Buys time.

  3. Reboot with the patch. The right long-term answer and often not available this week. Long-uptime hosts tend to be long-uptime for reasons.

  4. Hot-patch. Replace the vulnerable functions in the running kernel with fixed ones. For suitable patches, this avoids a reboot and can preserve service availability. It targets one bug and needs a tested rollback plan.

Hot-patching is often the last resort because a patch may not exist for your particular host, or your support agreement may not include it. If you want a vendor to handle this, I recommend TuxCare, provided your kernel is covered.

Linux has shipped livepatch support for years, several vendors offer services built on it, and open-source tooling such as kpatch can build a patch module from a suitable upstream fix.

None of this is tied to one distribution. The only distribution-specific question is where to get the exact source, debug symbols, and build flags for the running kernel, and that lives in one backend file. Our first backend is Ubuntu because that is where the host that started this was running; a Fedora or Debian backend is the same four functions over different package tools.

What a livepatch can carry

A livepatch module uses ftrace to redirect calls from an old function to a new one compiled from the fixed source. The kernel's consistency model switches each task over at a safe point, to avoid exposing tasks to an inconsistent mix of old and new code. That mechanism defines what fits:

  • Changes to function bodies: a missing bounds check, a wrong free, a reordered lock. Many memory-safety fixes fall into this category, though each still needs review.
  • New static helpers and string constants.

And what does not:

  • Changes to the layout of a struct already in memory.
  • Changes to init code, which has already run.
  • Changes to exported symbols or build files.
  • Functions the tracer cannot see, such as those marked notrace.

The first job on any new CVE is sorting the fix into one of these buckets. That is a reading task, and reading is what agents are good at.

The agentic workflow

We treat the agent as the operator and the human as the approver. The repository ships a runbook the agent reads on start. The steps, and what each one produces:

Establish exposure. Before anything is built, the agent answers: which service on this host reaches the vulnerable code, from where, with what privileges, gated by which configuration. It writes a short risk statement. Half the time this step ends the work, because the code path is not reachable here, or because a sysctl closes it for free.

Resolve the fix. Given a CVE ID, the tool reads the kernel CNA record, which lists the fixing commit on every stable branch, downloads each one starting with the branch nearest the running kernel, and keeps the first that applies to the exact source tree. The agent reads the commit message as the primary source, not the advisory.

Judge suitability. The tool prints the functions the patch changes, found by reading the source rather than trusting diff hunk headers, whether each file is built into vmlinux or a module, whether that module is loaded, and flags for the constructs listed above. The agent decides livepatch or reboot and says why. A second model is a cheap second opinion here, and disagreement means stop and show the human both views.

Build and verify. The build rebuilds the kernel from the exact source package and the vendor's exact make flags, extracts the changed functions, and links the module. Then it checks the module against the running kernel: the source came through the distribution's signed index, the debug vmlinux matches the running kernel's build-id, every symbol version the module imports matches the kernel's export table, and the module records the build-id it was built against so it can be loaded on identical hosts and nowhere else.

Write the test. The agent writes a small userspace program that reaches the patched code without harming anything. Not an exploit. The smallest thing that makes the kernel execute the changed function: open a socket with the right option, issue the right ioctl, touch the right file. It runs before loading, so a failure after loading means something.

Stop and report. Module name, replaced functions, every check and its result, the test plan, the rollback command. Then wait.

Load on a human's word. The human reads the report and says load. The tool inserts the module, waits for the transition, and the agent runs the test and watches dmesg. The rollback command disables the patch and, once the reverse transition completes safely, removes the module.

Where this goes wrong

Three lessons from doing this for real, each of which the tooling now encodes.

Reproducing the vendor's build is the hard part. The compiler must be the one that built the kernel, down to the packaging revision, or you must read the changelog and prove the difference cannot touch code generation. Distribution build systems pass flags on the make command line that never appear in the shipped config, and without them the rebuilt objects do not match the shipped kernel and the patch tooling cannot line them up. Expect the first build to fail and expect the reason to be a flag.

Checks that pass without checking. A verification step that errors and is ignored is worse than no step, because it produces confidence. We found one where a userspace tool could not parse a newer kernel's symbol version table, printed an error, and the build carried on to success. Every check in the pipeline now fails the build when it cannot run.

Approval is a specific thing. A green build is not approval. A scheduled job firing is not approval. The agent's own earlier message saying it will ask is not approval. The runbook says this in plain words because an agent under time pressure will otherwise find a reason to proceed. The human types the word, in the conversation, after reading the report.

The economics

A livepatch build is a full kernel compile. On a four-core VM that is over an hour; on a sixteen-core build host it is minutes. Build once per kernel build, not per host: the module loads anywhere the identical kernel package runs, and the build-id check refuses everything else. For a fleet, the pattern is one build host per kernel version you still run, and a copy step.

The agent's time is the other cost, and it is mostly spent on the first CVE against a given kernel: finding where the vendor still keeps the source, getting the build to match, learning the local quirks. The second CVE on the same kernel is an hour of mostly waiting.

What it does not fix

A livepatch closes one hole. A manually loaded patch does not persist across a reboot. It does not make an unsupported kernel supported, and it does not reduce the backlog of every other fix that kernel is missing. It buys time to schedule the reboot you still owe. If your kernel is supported, use your vendor's livepatch service; it has people and test farms behind it. If it is not, this is how to stop the bleeding today.

The point

The exploit side of this industry already runs at machine speed. The defence side can too, for the class of fixes that fit a livepatch, without pretending the machine should make the final call. Put the playbook in a file the agent reads. Make every check fail loudly. Keep the human on the switch. For a suitable fix on a kernel you already know how to build, the next bug you read about at breakfast can be an afternoon's work, not a wait for the next change window.