Applying updates to an active kernel with Ksplice
Piece of Cake
Uptime is often just as important as updates. But doesn't a kernel patch require a reboot? Ksplice lets you have your cake and eat it too.
Many administrators don't relish the thought of installing a new kernel, so most distributions come with package management and installation tools that simplify the task. After you load and install the new kernel and register it with the bootloader, the reboot should take just a couple of minutes. But a couple of minutes of downtime is too long for some applications.
If you manage a system used for number crunching, such as in weather simulations, or a system that has to manage a large number of network connections, such as a phone server or online shop, you probably would prefer to avoid a reboot.
On the other hand, administrators are also responsible for the security of the systems they manage. Surveys have revealed that developers discover a Linux kernel bug every three weeks on average. The open development model means that patches appear often.
Jeffrey Brian Arnold from the Massachusetts Institute of Technology established in a survey [1] that patches were very simple in most cases. Eighty percent of them comprised fewer than 15 lines of code, and more than half were little more than one-liners. Problems are often caused by "off-by-one errors," in which a developer has simply miscounted a parameter such as an array boundary. Bugs of this type are very easy to resolve. For example, Listing 1 shows a patch for the prctl() system call, which resolves the vulnerability listed as CVE-2006-2451. The problem, for which exploits exist, takes just one line of code to fix.
Listing 1
Patch for CVE-2006-2451
Arnold developed the Ksplice package [2] with the idea of patching the active kernel directly, without the need for a reboot. The program just needs the source code for the current kernel, the configuration data, and the symbol table. The best thing is that you do not even need to prepare the running system to use Ksplice. The program can modify any kernel as of version 2.6.8.
Linux distributions offer the kernel source code, or, if you built your own kernel, you will find it in /usr/src. Distributions typically store the two files config and System.map in /boot. On top of this, Ksplice obviously needs a patch file or one or multiple files with changes. The program builds two new kernels: pre designates a version of the current system, and post designates the kernel after the updates.
Spot the Difference
After building both kernels, Ksplice looks for differences in the object code (see Figure 1). If Ksplice were to analyze the source code directly, it would need to emulate all of the compiler's decisions, which is far too complicated for the task in hand. For this reason, Ksplice uses the GNU BFD library [3] to search for functions in the object code that have changed in the new kernel. Ksplice then adds the new code to modules and inserts trampoline jumps at the start of the original functions that then point to the new versions. When the administrator finally enables the changes, Ksplice loads two kernel modules on the running system that then perform the modifications (see Figure 2).
One critical issue is timing as to when Ksplice is allowed to install the trampoline jumps. Trouble occurs if a kernel thread is currently running one of the functions that needs to be replaced. To avoid this, the program calls stop_machine_run() to stop the thread execution because the function creates a high-priority process for each CPU. The Ksplice module now checks to see whether the change candidate contains threads. If it does, the module waits for a while and then retries. This approach will not work for some functions, such as the scheduler, because the scheduler will always be doing something. In that case, Ksplice gives up; in all other cases, it installs the jump addresses. From now on, the Linux kernel executes the patched version of the functions.
Inner Values
Ksplice has to find the right functions and vector addresses in relocatable code. The program is capable of detecting changes in relative jump addresses where the function itself is not affected by a modification, thanks to the new length of the patched function. The kernel typically enters functions implemented in C at the start only. In contrast, the program has to search for the vector in the case of assembler code.
If the program were to use a different compiler to create the pre kernel, it could lead to incorrect assumptions about the running system. Ksplice relies on substantial logic to analyze the symbol tables, which many distributions store in the /boot/Symbol.map file. On top of this, it builds the two kernels with a number of compiler options that assign a separate ELF text segment to each function to make it easier to identify modified, relative jumps.
Because it handles code like a black box, Ksplice can't detect changes in data structures. For example, if a patch adds a new attribute to a data structure or changes its layout, unpredictable issues will occur. Trampoline jumps will typically pick up function pointers, but there is no guarantee Ksplice will handle more complex pointer arithmetic or "creative" typecasting gracefully in each case.
Off Limits
In the documentation, the author emphasizes that he mainly designed the tool for minor security patches and that it is the system administrator's own responsibility to read, understand, and evaluate the patch before applying it. In other words, you need a great deal of kernel expertise to apply the tool; otherwise, the effect might be far more destructive than a bit of reboot downtime.
Because Ksplice cannot make semantic changes to a running kernel, the administrator's pipe dream of measuring uptime in years is just that because most changes between one kernel release and the next add some new functionality.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.