Live Patching Kernel Vulnerabilities on CentOS Stream 9 with kpatch: Security Updates Without Reboots

CentOS tutorial - IT technology blog
CentOS tutorial - IT technology blog

The “Maintenance Window” Headache and the kpatch Solution

A few years ago, when I was managing a server cluster for a bank, every time a critical Kernel vulnerability (CVE) notification arrived, it was a major stressor. The process back then was grueling: notifying clients, scheduling maintenance a week in advance, and waking up at 2 AM to run dnf update and reboot. Just one web server failing to come back up after a restart meant an all-nighter spent troubleshooting.

For systems requiring 24/7 uptime, rebooting just to patch a minor Kernel bug is a waste. It consumes resources and introduces unnecessary risks. That’s why live patching was born. On CentOS Stream 9, we have kpatch. This tool allows you to inject patches directly into the Kernel while the system is running. Since I started using kpatch for critical nodes, I’ve cut down on manual labor and no longer lose sleep over scheduled reboots.

How does kpatch work?

In simple terms, kpatch replaces faulty functions in the running Kernel with safer versions. It uses the ftrace mechanism to redirect function calls from the old version to the new one. This process happens in just a few milliseconds—so fast that running applications don’t even notice the change.

Why should you consider kpatch for Production systems?

  • Absolute Uptime: Servers continue to handle traffic normally without interrupting the user experience.
  • Zero-day Vulnerability Management: When a flaw like CVE-2024-1086 (privilege escalation) surfaces, you can patch it immediately instead of waiting until the end of the month.
  • Instant Rollback: If a patch causes conflicts, you can remove it instantly without needing a reboot.

Installing kpatch on CentOS Stream 9

First, check your current Kernel version. Note that kpatch works most reliably with official Kernels from the system repository.

uname -r

Next, install the kpatch toolset and the dnf plugin. This plugin is extremely useful as it helps dnf automatically find the patch that best matches your running Kernel version.

sudo dnf install kpatch kpatch-dnf

Once installed, check the status with the command:

kpatch list

If the list is empty, don’t worry. This is normal because we haven’t loaded any patches into the system yet.

How to Find and Apply “Live” Kernel Patches

Red Hat and CentOS Stream typically package security patches with names starting with kpatch-patch. To see if there are any patches available for your machine, use the search command:

dnf search kpatch-patch

Assuming your server is running Kernel 5.14.0-427.el9.x86_64, you can let dnf automatically calculate and install the corresponding patch with a single command:

sudo dnf install "kpatch-patch = $(uname -r)"

Pro Tip: Not every Kernel version has a patch available immediately. Typically, live patches are only released for security vulnerabilities rated as Important or Critical.

Confirming the Patch is Active

After installing the kpatch-patch-... package, the system will automatically activate it. You should double-check to be sure:

sudo kpatch list

If you see the status reported as loaded, congratulations. The vulnerability has been closed without the server stopping for even a second.

Auto-loading on Boot

Even though the main goal is to avoid reboots, if the server must restart due to hardware reasons, you’ll want the patch applied immediately. Ensure the kpatch service is enabled:

sudo systemctl enable --now kpatch

Testing and Monitoring Post-Patching

When managing systems, don’t “blindly trust” a command. You need to verify if the patch is truly stable.

1. Inspect System Logs

All kpatch actions are recorded in dmesg. You can quickly check with:

dmesg | grep kpatch

Seeing the line kpatch: loaded patch ... appear means you’re 99% good to go.

2. Managing kpatch Modules

At its core, kpatch is a special kernel module. You can list them using the familiar lsmod command:

lsmod | grep kpatch

3. Troubleshooting (Unload)

In the rare event that a patch causes issues (e.g., unusually high CPU load), you can remove it immediately:

# Get the patch name from kpatch list
sudo kpatch unload kpatch_patch_5_14_0_427

Real-world Tips for Implementing Live Patching

After running kpatch for a long time, here are a few pieces of advice:

  • Don’t forget Kernel upgrades: kpatch is a temporary fix. In the long run, you should still schedule reboots to run on a completely new Kernel version to benefit from other performance improvements.
  • Always test on Staging: Never push a patch directly to Production. Test it on a node with an equivalent configuration to ensure no conflicts with your specific applications.
  • Technical Limitations: kpatch cannot patch changes that are too deep within Kernel data structures. In those cases, a reboot is mandatory.
  • Manage the Number of Patches: Loading 5-7 patches simultaneously for one Kernel can make the system complex. Clean up and update to a new Kernel when the opportunity arises.

In summary, kpatch is an incredibly useful tool in a Linux administrator’s toolkit. It turns stressful on-call nights into relaxed evenings. If you are running CentOS Stream 9, install it today to make your system management more professional.

Share: