Okay, let's dive into the technical details of how KernelCare (and similar live patching technologies like ksplice, kpatch, and livepatch) applies patches to a running Linux kernel without requiring a reboot, focusing on the kernel-level mechanics.
The core challenge is modifying code that is currently residing in memory and potentially being executed by multiple CPUs, without causing crashes or inconsistent states. KernelCare addresses this by using a technique called function hooking or function patching at the binary level.
Here's a breakdown of the process at the kernel level:
-
Patch Generation:
- KernelCare doesn't work directly with source code on the running system. Instead, patches are pre-built by KernelCare Inc. for specific kernel versions and architectures.
- A patch is generated by comparing the vulnerable version of a kernel function (or set of functions) with the fixed version (compiled from the patched kernel source).
- The patch package essentially contains:
- The new, corrected machine code for the function(s) being fixed.
- Metadata indicating which original kernel functions need to be replaced.
- Information about the kernel version, architecture, and symbols needed.
- This patch is distributed as a kind of kernel module or data file specifically designed for the KernelCare agent.
-
Loading the Patch Module:
- The KernelCare user-space agent downloads the appropriate patch package.
- It then instructs a small, persistent KernelCare kernel module (loaded at boot) to apply the patch.
- The patch data is loaded into kernel memory, often structured as a temporary kernel module or a custom data structure managed by the KernelCare core module. This loaded data contains the new function code.
-
Identifying Target Functions:
- The KernelCare core module uses the metadata from the patch to identify the kernel functions that need to be replaced.
- It uses the kernel's symbol table (
kallsyms) to find the exact memory addresses of the original, vulnerable functions in the running kernel's text segment.
-
Preparing for Modification:
- Modifying the kernel's text segment (where the code lives) is inherently risky. It's typically marked read-only in the page tables to prevent accidental corruption.
- To write the patch, the KernelCare module must:
- Temporarily disable interrupts or acquire appropriate kernel locks to prevent context switches or other CPUs from interfering during the modification.
- Temporarily make the memory pages containing the start of the target functions writable. This involves modifying the relevant Page Table Entries (PTEs) for those memory pages. This is often done using low-level kernel functions like
set_memory_rw() (or by temporarily clearing the WP bit in the CPU's CR0 register on x86, though set_memory_rw() is the preferred and safer method).
-
Performing the Jump Insertion (The Core Patching Step):
- For each function being patched, the KernelCare module saves the original bytes at the very beginning of the function (typically the first 5-14 bytes, depending on architecture and the type of jump instruction used). These bytes are needed to potentially unpatch later.
- It then overwrites these original bytes with a jump instruction. This jump instruction is carefully crafted to redirect execution flow directly to the memory address of the new, patched version of the function, which was loaded in step 2.
- On x86, this is typically a
jmp rel32 instruction (a relative jump using a 32-bit displacement) or sometimes a jmp absolute via an indirect jump through a register or memory location, depending on the exact technique and kernel version. The instruction size is critical; it must be small enough to fit within the saved original bytes without overwriting too much of the function's start.
- This write operation (overwriting the start of the function) is performed atomically if possible or with appropriate locking to ensure consistency.
-
Restoring Memory Protection and Flushing Caches:
- Once the jump instruction has been written, the KernelCare module immediately:
- Restores the memory pages containing the modified functions back to read-only permissions by modifying the PTEs again (
set_memory_ro()).
- Flushes the CPU's Instruction Cache (ICache). This is a crucial step. CPUs cache recently executed instructions. Without flushing, a CPU might execute a cached copy of the old instructions at the start of the function instead of fetching the new jump instruction from memory. Flushing ensures the CPU sees the updated code. This often involves architecture-specific instructions (like
wbinvd on x86, though higher-level kernel functions are preferred).
- Re-enables interrupts or releases the locks.
-
Handling Functions Currently in Execution:
- What happens if a CPU was already executing the vulnerable function when the patch was applied?
- The patch is applied at the entry point. Any CPU that enters the function after the jump is in place will be redirected to the new code.
- A CPU that was already inside the function when the patch was applied will typically continue executing the old code path to completion. This is a limitation of this live patching technique. However, for most security vulnerabilities (which are often checks or specific operations at the start or within a code block), allowing existing executions to finish the old way is acceptable, as subsequent calls will use the fixed code. KernelCare's patch design and verification process takes this into account. It does not attempt to migrate executing threads to the new code or wait for all executions to finish, which would be complex and potentially cause delays or deadlocks.
-
Subsequent Function Calls:
- Any process, interrupt handler, or other kernel code that calls the now-patched function will jump to the new code provided by the KernelCare module. The new code performs the corrected operation.
- The new code might call back to the original, unpatched function code if only a small part of the function was changed (e.g., fixing a check at the beginning, then jumping back to the original logic). This requires careful coordination and often involves generating special "trampoline" functions within the patch or adjusting the jump targets. More commonly, the entire vulnerable function is replaced with its fixed version.
-
Unpatching (Reversal):
- If a patch needs to be removed (e.g., before a traditional kernel update), the process is reversed.
- The KernelCare module again makes the memory page writable, writes the original saved bytes back to the function entry point, restores read-only permissions, and flushes the ICache.
- This also requires careful handling to ensure no CPU attempts to execute the patched code during the unpatching process.
In Summary at the Kernel Level:
KernelCare operates by binary patching the running kernel's code in memory. It loads new function code, identifies the original functions using kernel symbols, and then, under carefully controlled conditions (memory writability, cache flushing, interrupt/lock management), overwrites the start of the original functions with a jump instruction that points to the new code. Subsequent calls are redirected, while functions already in execution typically complete using the old code path. This low-level manipulation of kernel memory and execution flow is the core mechanism enabling zero-downtime patching.