Patching The OpenBSD Kernel


To address OpenBSD 3.7 reliability fix 002, I recently had to apply a source code patch to the OpenBSD kernel. This required me to familiarize myself with the patch tools, and the kernel configuration and build process. Patching the source code and compiling a new kernel turned out to be super easy to do, and the following article will outline the process.

Patching and building a kernel

OpenBSD patches are provided as universal diffs, and are available on the OpenBSD website. Each patch can be applied to the OpenBSD kernel source code with the patch utility. The following steps show how to apply patch 002_getsockopt.patch to the OpenBSD kernel source that resides in the directory /usr/src:

$ cd /usr/src

$ patch -p0 < 002_getsockopt.patch

Once a patch is applied to the kernel source, a new kernel will need to be compiled and installed in order for the path functionality to take effect . This can be accomplished by changing to the hardware architecture specific “conf” directory, executing the config script, and running make to compile the new kernel. The following steps show how to configure and build a kernel for the SPARC64 architecture:

$ cd /usr/src/sys/arch/sparc64/conf

$ config GENERIC

$ cd /usr/src/sys/arch/sparc64/compile/GENERIC/

$ make depend

$ make

This example used the GENERIC kernel configuration, which contains typical settings for the hardware architecture you are attempting to build on. If you need to add or remove support for a specific feature, you can copy the GENERIC file to something more specific, adjust the configuration to meet your needs, and then run config with the new file name.
The following example shows how to customize the SPARC64 kernel configuration:

$ cd /usr/src/sys/arch/sparc64/conf

$ cp GENERIC SOMETHINGSPECIFIC

< edit SOMETHINGSPECIFIC >

$ config SOMETHINGSPECIFIC

$ cd /usr/src/sys/arch/sparc64/compile/SOMETHINGSPECIFIC/

$ make depend

$ make

After the kernel compiles successfully, a file with the name bsd will be created in the compile directory. This is the OpenBSD kernel, and will need to be copied to the base of the root file system once the previous kernel has been backed up:

$ cp /bsd /bsd.old

$ cp /usr/src/sys/arch/sparc64/compile/GENERIC/bsd /bsd

It is important to back up the old kernel prior to installing a new one. This will allow you to recover if a problem is detected with the new kernel, and it also provides a way to migrate back to a safe environmnet if odd behavior is encountered. If you have questions or comments on the article, please feel free to E-mail the author.

References

The following references were used while writing this article: