Blame README.md

Packit c71e3f
kpatch: dynamic kernel patching
Packit c71e3f
===============================
Packit c71e3f
Packit c71e3f
kpatch is a Linux dynamic kernel patching infrastructure which allows you to
Packit c71e3f
patch a running kernel without rebooting or restarting any processes.  It
Packit c71e3f
enables sysadmins to apply critical security patches to the kernel immediately,
Packit c71e3f
without having to wait for long-running tasks to complete, for users to log
Packit c71e3f
off, or for scheduled reboot windows.  It gives more control over uptime
Packit c71e3f
without sacrificing security or stability.
Packit c71e3f
Packit c71e3f
**WARNING: Use with caution!  Kernel crashes, spontaneous reboots, and data loss
Packit c71e3f
may occur!**
Packit c71e3f
Packit c71e3f
Here's a video of kpatch in action:
Packit c71e3f
Packit c71e3f
[![kpatch video](https://img.youtube.com/vi/juyQ5TsJRTA/0.jpg)](https://www.youtube.com/watch?v=juyQ5TsJRTA)
Packit c71e3f
Packit c71e3f
And a few more:
Packit c71e3f
Packit c71e3f
- https://www.youtube.com/watch?v=rN0sFjrJQfU
Packit c71e3f
- https://www.youtube.com/watch?v=Mftc80KyjA4
Packit c71e3f
Packit c71e3f
Installation
Packit c71e3f
------------
Packit c71e3f
Packit c71e3f
### Prerequisites
Packit c71e3f
Packit c71e3f
#### Fedora
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
UNAME=$(uname -r)
Packit c71e3f
sudo dnf install gcc kernel-devel-${UNAME%.*} elfutils elfutils-devel
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
sudo dnf install pesign yum-utils openssl wget numactl-devel
Packit c71e3f
sudo dnf builddep kernel-${UNAME%.*}
Packit c71e3f
sudo dnf debuginfo-install kernel-${UNAME%.*}
Packit c71e3f
Packit c71e3f
# optional, but highly recommended
Packit c71e3f
sudo dnf install ccache
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
Packit c71e3f
# optional, for kpatch-test
Packit c71e3f
sudo dnf install patchutils
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
#### RHEL 7
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
UNAME=$(uname -r)
Packit c71e3f
sudo yum install gcc kernel-devel-${UNAME%.*} elfutils elfutils-devel
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
sudo yum-config-manager --enable rhel-7-server-optional-rpms
Packit c71e3f
sudo yum install pesign yum-utils zlib-devel \
Packit c71e3f
  binutils-devel newt-devel python-devel perl-ExtUtils-Embed \
Packit c71e3f
  audit-libs-devel numactl-devel pciutils-devel bison ncurses-devel
Packit c71e3f
Packit c71e3f
sudo yum-builddep kernel-${UNAME%.*}
Packit c71e3f
sudo debuginfo-install kernel-${UNAME%.*}
Packit c71e3f
Packit c71e3f
# optional, but highly recommended
Packit c71e3f
sudo yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/c/ccache-3.3.4-1.el7.x86_64.rpm
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
Packit c71e3f
# optional, for kpatch-test
Packit c71e3f
sudo yum install patchutils
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
#### CentOS 7
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
UNAME=$(uname -r)
Packit c71e3f
sudo yum install gcc kernel-devel-${UNAME%.*} elfutils elfutils-devel
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
sudo yum install pesign yum-utils zlib-devel \
Packit c71e3f
  binutils-devel newt-devel python-devel perl-ExtUtils-Embed \
Packit c71e3f
  audit-libs audit-libs-devel numactl-devel pciutils-devel bison
Packit c71e3f
Packit c71e3f
# enable CentOS 7 debug repo
Packit c71e3f
sudo yum-config-manager --enable debug
Packit c71e3f
Packit c71e3f
sudo yum-builddep kernel-${UNAME%.*}
Packit c71e3f
sudo debuginfo-install kernel-${UNAME%.*}
Packit c71e3f
Packit c71e3f
# optional, but highly recommended - enable EPEL 7
Packit c71e3f
sudo yum install ccache
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
Packit c71e3f
# optional, for kpatch-test
Packit c71e3f
sudo yum install patchutils
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
#### Oracle Linux 7
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
UNAME=$(uname -r)
Packit c71e3f
sudo yum install gcc kernel-devel-${UNAME%.*} elfutils elfutils-devel
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
sudo yum install pesign yum-utils zlib-devel \
Packit c71e3f
  binutils-devel newt-devel python-devel perl-ExtUtils-Embed \
Packit c71e3f
  audit-libs numactl-devel pciutils-devel bison
Packit c71e3f
Packit c71e3f
# enable ol7_optional_latest repo
Packit c71e3f
sudo yum-config-manager --enable ol7_optional_latest
Packit c71e3f
Packit c71e3f
sudo yum-builddep kernel-${UNAME%.*}
Packit c71e3f
Packit c71e3f
# manually install kernel debuginfo packages
Packit c71e3f
rpm -ivh https://oss.oracle.com/ol7/debuginfo/kernel-debuginfo-$(uname -r).rpm
Packit c71e3f
rpm -ivh https://oss.oracle.com/ol7/debuginfo/kernel-debuginfo-common-x86_64-$(uname -r).rpm
Packit c71e3f
Packit c71e3f
# optional, but highly recommended - enable EPEL 7
Packit c71e3f
sudo yum install ccache
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
Packit c71e3f
# optional, for kpatch-test
Packit c71e3f
sudo yum install patchutils
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
#### Ubuntu 14.04
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
apt-get install make gcc libelf-dev
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
apt-get install dpkg-dev devscripts
Packit c71e3f
apt-get build-dep linux
Packit c71e3f
Packit c71e3f
# optional, but highly recommended
Packit c71e3f
apt-get install ccache
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install kernel debug symbols:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
# Add ddebs repository
Packit c71e3f
codename=$(lsb_release -sc)
Packit c71e3f
sudo tee /etc/apt/sources.list.d/ddebs.list << EOF
Packit c71e3f
deb http://ddebs.ubuntu.com/ ${codename} main restricted universe multiverse
Packit c71e3f
deb http://ddebs.ubuntu.com/ ${codename}-security main restricted universe multiverse
Packit c71e3f
deb http://ddebs.ubuntu.com/ ${codename}-updates main restricted universe multiverse
Packit c71e3f
deb http://ddebs.ubuntu.com/ ${codename}-proposed main restricted universe multiverse
Packit c71e3f
EOF
Packit c71e3f
Packit c71e3f
# add APT key
Packit c71e3f
wget -Nq http://ddebs.ubuntu.com/dbgsym-release-key.asc -O- | sudo apt-key add -
Packit c71e3f
apt-get update && apt-get install linux-image-$(uname -r)-dbgsym
Packit c71e3f
```
Packit c71e3f
If there are no packages published yet to the codename-security pocket, the
Packit c71e3f
apt update may report a "404 Not Found" error, as well as a complaint about
Packit c71e3f
disabling the repository by default.  This message may be ignored (see issue
Packit c71e3f
#710).
Packit c71e3f
Packit c71e3f
#### Debian 9 (Stretch)
Packit c71e3f
Packit c71e3f
Since Stretch the stock kernel can be used without changes, however the
Packit c71e3f
version of kpatch in Stretch is too old so you still need to build it
Packit c71e3f
manually. Follow the instructions for Debian Jessie (next section) but skip
Packit c71e3f
building a custom kernel/rebooting.
Packit c71e3f
Packit c71e3f
#### Debian 8 (Jessie)
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install the dependencies for compiling kpatch:
Packit c71e3f
Packit c71e3f
    apt-get install make gcc libelf-dev build-essential
Packit c71e3f
Packit c71e3f
Install and prepare the kernel sources:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
apt-get install linux-source-$(uname -r)
Packit c71e3f
cd /usr/src && tar xvf linux-source-$(uname -r).tar.xz && ln -s linux-source-$(uname -r) linux && cd linux
Packit c71e3f
cp /boot/config-$(uname -r) .config
Packit c71e3f
for OPTION in CONFIG_KALLSYMS_ALL CONFIG_FUNCTION_TRACER ; do sed -i "s/# $OPTION is not set/$OPTION=y/g" .config ; done
Packit c71e3f
sed -i "s/^SUBLEVEL.*/SUBLEVEL =/" Makefile
Packit c71e3f
make -j`getconf _NPROCESSORS_CONF` deb-pkg KDEB_PKGVERSION=$(uname -r).9-1
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the kernel packages and reboot
Packit c71e3f
Packit c71e3f
    dpkg -i /usr/src/*.deb
Packit c71e3f
    reboot
Packit c71e3f
Packit c71e3f
Install the dependencies for the "kpatch-build" command:
Packit c71e3f
Packit c71e3f
    apt-get install dpkg-dev
Packit c71e3f
    apt-get build-dep linux
Packit c71e3f
Packit c71e3f
    # optional, but highly recommended
Packit c71e3f
    apt-get install ccache
Packit c71e3f
    ccache --max-size=5G
Packit c71e3f
Packit c71e3f
#### Debian 7 (Lenny)
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Add backports repositories:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
echo "deb http://http.debian.net/debian wheezy-backports main" > /etc/apt/sources.list.d/wheezy-backports.list
Packit c71e3f
echo "deb http://packages.incloudus.com backports-incloudus main" > /etc/apt/sources.list.d/incloudus.list
Packit c71e3f
wget http://packages.incloudus.com/incloudus/incloudus.pub -O- | apt-key add -
Packit c71e3f
aptitude update
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install the linux kernel, symbols and gcc 4.9:
Packit c71e3f
Packit c71e3f
    aptitude install -t wheezy-backports -y initramfs-tools
Packit c71e3f
    aptitude install -y gcc gcc-4.9 g++-4.9 linux-image-3.14 linux-image-3.14-dbg
Packit c71e3f
Packit c71e3f
Configure gcc 4.9 as the default gcc compiler:
Packit c71e3f
Packit c71e3f
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 20
Packit c71e3f
    update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 50
Packit c71e3f
    update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 20
Packit c71e3f
    update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 50
Packit c71e3f
Packit c71e3f
Install kpatch and these dependencies:
Packit c71e3f
Packit c71e3f
    aptitude install kpatch
Packit c71e3f
Packit c71e3f
Configure ccache (installed by kpatch package):
Packit c71e3f
Packit c71e3f
    ccache --max-size=5G
Packit c71e3f
Packit c71e3f
#### Gentoo
Packit c71e3f
Packit c71e3f
*NOTE: You'll need about 15GB of free disk space for the kpatch-build cache in
Packit c71e3f
`~/.kpatch` and for ccache.*
Packit c71e3f
Packit c71e3f
Install Kpatch and Kpatch dependencies:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
emerge --ask sys-kernel/kpatch
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Install ccache (optional):
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
emerge --ask dev-util/ccache
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
Configure ccache:
Packit c71e3f
Packit c71e3f
```bash
Packit c71e3f
ccache --max-size=5G
Packit c71e3f
```
Packit c71e3f
Packit c71e3f
### Build
Packit c71e3f
Packit c71e3f
Compile kpatch:
Packit c71e3f
Packit c71e3f
    make
Packit c71e3f
Packit c71e3f
Packit c71e3f
### Install
Packit c71e3f
Packit c71e3f
OPTIONAL: Install kpatch to `/usr/local`:
Packit c71e3f
Packit c71e3f
    sudo make install
Packit c71e3f
Packit c71e3f
Alternatively, the kpatch and kpatch-build scripts can be run directly from the
Packit c71e3f
git tree.
Packit c71e3f
Packit c71e3f
Packit c71e3f
Quick start
Packit c71e3f
-----------
Packit c71e3f
Packit c71e3f
> NOTE: While kpatch is designed to work with any recent Linux
Packit c71e3f
kernel on any distribution, the `kpatch-build` command has **ONLY** been tested
Packit c71e3f
and confirmed to work on Fedora 20 and later, RHEL 7, Oracle Linux 7, CentOS 7 and Ubuntu 14.04.
Packit c71e3f
Packit c71e3f
First, make a source code patch against the kernel tree using diff, git, or
Packit c71e3f
quilt.
Packit c71e3f
Packit c71e3f
As a contrived example, let's patch /proc/meminfo to show VmallocChunk in ALL
Packit c71e3f
CAPS so we can see it better:
Packit c71e3f
Packit c71e3f
    $ cat meminfo-string.patch
Packit c71e3f
    Index: src/fs/proc/meminfo.c
Packit c71e3f
    ===================================================================
Packit c71e3f
    --- src.orig/fs/proc/meminfo.c
Packit c71e3f
    +++ src/fs/proc/meminfo.c
Packit c71e3f
    @@ -95,7 +95,7 @@ static int meminfo_proc_show(struct seq_
Packit c71e3f
     		"Committed_AS:   %8lu kB\n"
Packit c71e3f
     		"VmallocTotal:   %8lu kB\n"
Packit c71e3f
     		"VmallocUsed:    %8lu kB\n"
Packit c71e3f
    -		"VmallocChunk:   %8lu kB\n"
Packit c71e3f
    +		"VMALLOCCHUNK:   %8lu kB\n"
Packit c71e3f
     #ifdef CONFIG_MEMORY_FAILURE
Packit c71e3f
     		"HardwareCorrupted: %5lu kB\n"
Packit c71e3f
     #endif
Packit c71e3f
Packit c71e3f
Build the patch module:
Packit c71e3f
Packit c71e3f
    $ kpatch-build -t vmlinux meminfo-string.patch
Packit c71e3f
    Using cache at /home/jpoimboe/.kpatch/3.13.10-200.fc20.x86_64/src
Packit c71e3f
    Testing patch file
Packit c71e3f
    checking file fs/proc/meminfo.c
Packit c71e3f
    Building original kernel
Packit c71e3f
    Building patched kernel
Packit c71e3f
    Detecting changed objects
Packit c71e3f
    Rebuilding changed objects
Packit c71e3f
    Extracting new and modified ELF sections
Packit c71e3f
    meminfo.o: changed function: meminfo_proc_show
Packit c71e3f
    Building patch module: kpatch-meminfo-string.ko
Packit c71e3f
    SUCCESS
Packit c71e3f
Packit c71e3f
> NOTE: The `-t vmlinux` option is used to tell `kpatch-build` to only look for
Packit c71e3f
> changes in the `vmlinux` base kernel image, which is much faster than also
Packit c71e3f
> compiling all the kernel modules.  If your patch affects a kernel module, you
Packit c71e3f
> can either omit this option to build everything, and have `kpatch-build`
Packit c71e3f
> detect which modules changed, or you can specify the affected kernel build
Packit c71e3f
> targets with multiple `-t` options.
Packit c71e3f
Packit c71e3f
That outputs a patch module named `kpatch-meminfo-string.ko` in the current
Packit c71e3f
directory.  Now apply it to the running kernel:
Packit c71e3f
Packit c71e3f
    $ sudo kpatch load kpatch-meminfo-string.ko
Packit c71e3f
    loading core module: /usr/local/lib/modules/3.13.10-200.fc20.x86_64/kpatch/kpatch.ko
Packit c71e3f
    loading patch module: kpatch-meminfo-string.ko
Packit c71e3f
Packit c71e3f
Done!  The kernel is now patched.
Packit c71e3f
Packit c71e3f
    $ grep -i chunk /proc/meminfo
Packit c71e3f
    VMALLOCCHUNK:   34359337092 kB
Packit c71e3f
Packit c71e3f
Packit c71e3f
Patch Author Guide
Packit c71e3f
------------------
Packit c71e3f
Packit c71e3f
Unfortunately, live patching isn't always as easy as the previous example, and
Packit c71e3f
can have some major pitfalls if you're not careful.  To learn more about how to
Packit c71e3f
properly create live patches, see the [Patch Author
Packit c71e3f
Guide](doc/patch-author-guide.md).
Packit c71e3f
Packit c71e3f
Packit c71e3f
How it works
Packit c71e3f
------------
Packit c71e3f
Packit c71e3f
kpatch works at a function granularity: old functions are replaced with new
Packit c71e3f
ones.  It has four main components:
Packit c71e3f
Packit c71e3f
- **kpatch-build**: a collection of tools which convert a source diff patch to
Packit c71e3f
  a patch module.  They work by compiling the kernel both with and without
Packit c71e3f
  the source patch, comparing the binaries, and generating a patch module
Packit c71e3f
  which includes new binary versions of the functions to be replaced.
Packit c71e3f
Packit c71e3f
- **patch module**: a kernel module (.ko file) which includes the
Packit c71e3f
  replacement functions and metadata about the original functions.
Packit c71e3f
Packit c71e3f
- **kpatch core module**: a kernel module (.ko file) which provides an
Packit c71e3f
  interface for the patch modules to register new functions for
Packit c71e3f
  replacement.  It uses the kernel ftrace subsystem to hook into the original
Packit c71e3f
  function's mcount call instruction, so that a call to the original function
Packit c71e3f
  is redirected to the replacement function.
Packit c71e3f
Packit c71e3f
- **kpatch utility:** a command-line tool which allows a user to manage a
Packit c71e3f
  collection of patch modules.  One or more patch modules may be
Packit c71e3f
  configured to load at boot time, so that a system can remain patched
Packit c71e3f
  even after a reboot into the same version of the kernel.
Packit c71e3f
Packit c71e3f
Packit c71e3f
### kpatch-build
Packit c71e3f
Packit c71e3f
The "kpatch-build" command converts a source-level diff patch file to a kernel
Packit c71e3f
patch module.  Most of its work is performed by the kpatch-build script
Packit c71e3f
which uses a utility named `create-diff-object` to compare changed objects.
Packit c71e3f
Packit c71e3f
The primary steps in kpatch-build are:
Packit c71e3f
- Build the unstripped vmlinux for the kernel
Packit c71e3f
- Patch the source tree
Packit c71e3f
- Rebuild vmlinux and monitor which objects are being rebuilt.
Packit c71e3f
  These are the "changed objects".
Packit c71e3f
- Recompile each changed object with `-ffunction-sections -fdata-sections`,
Packit c71e3f
  resulting in the changed patched objects
Packit c71e3f
- Unpatch the source tree
Packit c71e3f
- Recompile each changed object with `-ffunction-sections -fdata-sections`,
Packit c71e3f
  resulting in the changed original objects
Packit c71e3f
- For every changed object, use `create-diff-object` to do the following:
Packit c71e3f
	* Analyze each original/patched object pair for patchability
Packit c71e3f
	* Add `.kpatch.funcs` and `.rela.kpatch.funcs` sections to the output object.
Packit c71e3f
	The kpatch core module uses this to determine the list of functions
Packit c71e3f
	that need to be redirected using ftrace.
Packit c71e3f
	* Add `.kpatch.dynrelas` and `.rela.kpatch.dynrelas` sections to the output object.
Packit c71e3f
	This will be used to resolve references to non-included local
Packit c71e3f
	and non-exported global symbols. These relocations will be resolved by the kpatch core module.
Packit c71e3f
	* Generate the resulting output object containing the new and modified sections
Packit c71e3f
- Link all the output objects into a cumulative object
Packit c71e3f
- Generate the patch module
Packit c71e3f
Packit c71e3f
Packit c71e3f
### Patching
Packit c71e3f
Packit c71e3f
The patch modules register with the core module (`kpatch.ko`).
Packit c71e3f
They provide information about original functions that need to be replaced, and
Packit c71e3f
corresponding function pointers to the replacement functions.
Packit c71e3f
Packit c71e3f
The core module registers a handler function with ftrace.  The
Packit c71e3f
handler function is called by ftrace immediately before the original
Packit c71e3f
function begins executing.  This occurs with the help of the reserved mcount
Packit c71e3f
call at the beginning of every function, created by the gcc `-mfentry` flag.
Packit c71e3f
The ftrace handler then modifies the return instruction pointer (IP)
Packit c71e3f
address on the stack and returns to ftrace, which then restores the original
Packit c71e3f
function's arguments and stack, and "returns" to the new function.
Packit c71e3f
Packit c71e3f
Packit c71e3f
Limitations
Packit c71e3f
-----------
Packit c71e3f
Packit c71e3f
- Patches which modify init functions (annotated with `__init`) are not
Packit c71e3f
  supported.  kpatch-build will return an error if the patch attempts
Packit c71e3f
  to do so.
Packit c71e3f
Packit c71e3f
- Patches which modify statically allocated data are not supported.
Packit c71e3f
  kpatch-build will detect that and return an error.  (In the future
Packit c71e3f
  we will add a facility to support it.  It will probably require the
Packit c71e3f
  user to write code which runs at patch module loading time which manually
Packit c71e3f
  updates the data.)
Packit c71e3f
Packit c71e3f
- Patches which change the way a function interacts with dynamically
Packit c71e3f
  allocated data might be safe, or might not.  It isn't possible for
Packit c71e3f
  kpatch-build to verify the safety of this kind of patch.  It's up to
Packit c71e3f
  the user to understand what the patch does, whether the new functions
Packit c71e3f
  interact with dynamically allocated data in a different way than the
Packit c71e3f
  old functions did, and whether it would be safe to atomically apply
Packit c71e3f
  such a patch to a running kernel.
Packit c71e3f
Packit c71e3f
- Patches which modify functions in vdso are not supported.  These run in
Packit c71e3f
  user-space and ftrace can't hook them.
Packit c71e3f
Packit c71e3f
- Patches which modify functions that are missing a `fentry` call are not
Packit c71e3f
  supported.  This includes any `lib-y` targets that are archived into a
Packit c71e3f
  `lib.a` library for later linking (for example, `lib/string.o`).
Packit c71e3f
Packit c71e3f
- Some incompatibilities currently exist between kpatch and usage of ftrace and
Packit c71e3f
  kprobes.  See the Frequently Asked Questions section for more details.
Packit c71e3f
Packit c71e3f
Packit c71e3f
Frequently Asked Questions
Packit c71e3f
--------------------------
Packit c71e3f
Packit c71e3f
**Q. What's the relationship between kpatch and the upstream Linux live kernel
Packit c71e3f
patching component (livepatch)?**
Packit c71e3f
Packit c71e3f
Starting with Linux 4.0, the Linux kernel has livepatch, which is a new
Packit c71e3f
converged live kernel patching framework.  Livepatch is similar in
Packit c71e3f
functionality to the kpatch core module, though it doesn't yet have all the
Packit c71e3f
features that kpatch does.
Packit c71e3f
Packit c71e3f
kpatch-build already works with both livepatch and kpatch.  If your kernel has
Packit c71e3f
CONFIG\_LIVEPATCH enabled, it detects that and builds a patch module in the
Packit c71e3f
livepatch format.  Otherwise it builds a kpatch patch module.
Packit c71e3f
Packit c71e3f
The kpatch script also supports both patch module formats.
Packit c71e3f
Packit c71e3f
**Q. Isn't this just a virus/rootkit injection framework?**
Packit c71e3f
Packit c71e3f
kpatch uses kernel modules to replace code.  It requires the `CAP_SYS_MODULE`
Packit c71e3f
capability.  If you already have that capability, then you already have the
Packit c71e3f
ability to arbitrarily modify the kernel, with or without kpatch.
Packit c71e3f
Packit c71e3f
**Q. How can I detect if somebody has patched the kernel?**
Packit c71e3f
Packit c71e3f
When a patch module is loaded, the `TAINT_USER` or `TAINT_LIVEPATCH` flag is
Packit c71e3f
set.  (The latter flag was introduced in Linux version 4.0.)  To test for
Packit c71e3f
these flags, `cat /proc/sys/kernel/tainted` and check to see if the value of
Packit c71e3f
`TAINT_USER` (64) or `TAINT_LIVEPATCH` (32768) has been OR'ed in.
Packit c71e3f
Packit c71e3f
Note that the `TAINT_OOT_MODULE` flag (4096) will also be set, since the patch
Packit c71e3f
module is built outside the Linux kernel source tree.
Packit c71e3f
Packit c71e3f
If your patch module is unsigned, the `TAINT_FORCED_MODULE` flag (2) will also
Packit c71e3f
be set.  Starting with Linux 3.15, this will be changed to the more specific
Packit c71e3f
`TAINT_UNSIGNED_MODULE` (8192).
Packit c71e3f
Packit c71e3f
Linux versions starting with 4.9 also support a per-module `TAINT_LIVEPATCH`
Packit c71e3f
taint flag. This can be checked by verifying the output of
Packit c71e3f
`cat /sys/module/<kpatch module>/taint` -- a 'K' character indicates the
Packit c71e3f
presence of `TAINT_LIVEPATCH`.
Packit c71e3f
Packit c71e3f
**Q. Will it destabilize my system?**
Packit c71e3f
Packit c71e3f
No, as long as the patch is chosen carefully.  See the Limitations section
Packit c71e3f
above.
Packit c71e3f
Packit c71e3f
**Q. Why does kpatch use ftrace to jump to the replacement function instead of
Packit c71e3f
adding the jump directly?**
Packit c71e3f
Packit c71e3f
ftrace owns the first "call mcount" instruction of every kernel function.  In
Packit c71e3f
order to keep compatibility with ftrace, we go through ftrace rather than
Packit c71e3f
updating the instruction directly.  This approach also ensures that the code
Packit c71e3f
modification path is reliable, since ftrace has been doing it successfully for
Packit c71e3f
years.
Packit c71e3f
Packit c71e3f
**Q. Is kpatch compatible with \<insert kernel debugging subsystem here\>?**
Packit c71e3f
Packit c71e3f
We aim to be good kernel citizens and maintain compatibility.  A kpatch
Packit c71e3f
replacement function is no different than a function loaded by any other kernel
Packit c71e3f
module.  Each replacement function has its own symbol name and kallsyms entry,
Packit c71e3f
so it looks like a normal function to the kernel.
Packit c71e3f
Packit c71e3f
- **oops stack traces**: Yes.  If the replacement function is involved in an
Packit c71e3f
  oops, the stack trace will show the function and kernel module name of the
Packit c71e3f
  replacement function, just like any other kernel module function.  The oops
Packit c71e3f
  message will also show the taint flag (see the FAQ "How can I detect if
Packit c71e3f
  somebody has patched the kernel" for specifics).
Packit c71e3f
- **kdump/crash**: Yes.  Replacement functions are normal functions, so crash
Packit c71e3f
  will have no issues.
Packit c71e3f
- **ftrace**: Yes, but certain uses of ftrace which involve opening the
Packit c71e3f
  `/sys/kernel/debug/tracing/trace` file or using `trace-cmd record` can result
Packit c71e3f
  in a tiny window of time where a patch gets temporarily disabled.  Therefore
Packit c71e3f
  it's a good idea to avoid using ftrace on a patched system until this issue
Packit c71e3f
  is resolved.
Packit c71e3f
- **systemtap/kprobes**: Some incompatibilities exist.
Packit c71e3f
  - If you setup a kprobe module at the beginning of a function before loading
Packit c71e3f
    a kpatch module, and they both affect the same function, kprobes "wins"
Packit c71e3f
    until the kprobe has been unregistered.  This is tracked in issue
Packit c71e3f
    [#47](https://github.com/dynup/kpatch/issues/47).
Packit c71e3f
  - Setting a kretprobe before loading a kpatch module could be unsafe.  See
Packit c71e3f
    issue [#67](https://github.com/dynup/kpatch/issues/67).
Packit c71e3f
- **perf**: Yes.
Packit c71e3f
- **tracepoints**: Patches to a function which uses tracepoints will result in
Packit c71e3f
  the tracepoints being effectively disabled as long as the patch is applied.
Packit c71e3f
Packit c71e3f
**Q. Why not use something like kexec instead?**
Packit c71e3f
Packit c71e3f
If you want to avoid a hardware reboot, but are ok with restarting processes,
Packit c71e3f
kexec is a good alternative.
Packit c71e3f
Packit c71e3f
**Q. If an application can't handle a reboot, it's designed wrong.**
Packit c71e3f
Packit c71e3f
That's a good poi... [system reboots]
Packit c71e3f
Packit c71e3f
**Q. What changes are needed in other upstream projects?**
Packit c71e3f
Packit c71e3f
We hope to make the following changes to other projects:
Packit c71e3f
Packit c71e3f
- kernel:
Packit c71e3f
	- ftrace improvements to close any windows that would allow a patch to
Packit c71e3f
	  be inadvertently disabled
Packit c71e3f
Packit c71e3f
**Q. Is it possible to register a function that gets called atomically with
Packit c71e3f
`stop_machine` when the patch module loads and unloads?**
Packit c71e3f
Packit c71e3f
We do have plans to implement something like that.
Packit c71e3f
Packit c71e3f
**Q. What kernels are supported?**
Packit c71e3f
Packit c71e3f
kpatch needs gcc >= 4.8 and Linux >= 3.9.
Packit c71e3f
Packit c71e3f
**Q. Is it possible to remove a patch?**
Packit c71e3f
Packit c71e3f
Yes.  Just run `kpatch unload` which will disable and unload the patch module
Packit c71e3f
and restore the function to its original state.
Packit c71e3f
Packit c71e3f
**Q. Can you apply multiple patches?**
Packit c71e3f
Packit c71e3f
Yes, but to prevent any unexpected interactions between multiple patch modules,
Packit c71e3f
it's recommended that patch upgrades are cumulative, so that each patch is a
Packit c71e3f
superset of the previous patch.  This can be achieved by combining the new
Packit c71e3f
patch with the previous patch using `combinediff` before running
Packit c71e3f
`kpatch-build`.
Packit c71e3f
Packit c71e3f
**Q. Why did kpatch-build detect a changed function that wasn't touched by the
Packit c71e3f
source patch?**
Packit c71e3f
Packit c71e3f
There could be a variety of reasons for this, such as:
Packit c71e3f
Packit c71e3f
- The patch changed an inline function.
Packit c71e3f
- The compiler decided to inline a changed function, resulting in the outer
Packit c71e3f
  function getting recompiled.  This is common in the case where the inner
Packit c71e3f
  function is static and is only called once.
Packit c71e3f
Packit c71e3f
**Q. How do I patch a function which is always on the stack of at least one
Packit c71e3f
task, such as schedule(), sys_poll(), sys_select(), sys_read(),
Packit c71e3f
sys_nanosleep(), etc?**
Packit c71e3f
Packit c71e3f
- If you're sure it would be safe for the old function and the new function to
Packit c71e3f
  run simultaneously, use the `KPATCH_FORCE_UNSAFE` macro to skip the
Packit c71e3f
  activeness safety check for the function.  See `kmod/patch/kpatch-macros.h`
Packit c71e3f
  for more details.
Packit c71e3f
Packit c71e3f
**Q. Are patching of kernel modules supported?**
Packit c71e3f
Packit c71e3f
- Yes.
Packit c71e3f
Packit c71e3f
**Q. Can you patch out-of-tree modules?**
Packit c71e3f
Packit c71e3f
- Yes, though it's currently a bit of a manual process.  See this
Packit c71e3f
  [message](https://www.redhat.com/archives/kpatch/2015-June/msg00004.html) on
Packit c71e3f
  the kpatch mailing list for more information.
Packit c71e3f
Packit c71e3f
Packit c71e3f
Get involved
Packit c71e3f
------------
Packit c71e3f
Packit c71e3f
If you have questions or feedback, join the #kpatch IRC channel on freenode and
Packit c71e3f
say hi.  We also have a [mailing list](https://www.redhat.com/mailman/listinfo/kpatch).
Packit c71e3f
Packit c71e3f
Contributions are very welcome.  Feel free to open issues or PRs on github.
Packit c71e3f
For big PRs, it's a good idea to discuss them first in github issues or on the
Packit c71e3f
[mailing list](https://www.redhat.com/mailman/listinfo/kpatch) before you write
Packit c71e3f
a lot of code.
Packit c71e3f
Packit c71e3f
Packit c71e3f
License
Packit c71e3f
-------
Packit c71e3f
Packit c71e3f
kpatch is under the GPLv2 license.
Packit c71e3f
Packit c71e3f
This program is free software; you can redistribute it and/or
Packit c71e3f
modify it under the terms of the GNU General Public License
Packit c71e3f
as published by the Free Software Foundation; either version 2
Packit c71e3f
of the License, or (at your option) any later version.
Packit c71e3f
Packit c71e3f
This program is distributed in the hope that it will be useful,
Packit c71e3f
but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c71e3f
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit c71e3f
GNU General Public License for more details.
Packit c71e3f
Packit c71e3f
You should have received a copy of the GNU General Public License
Packit c71e3f
along with this program; if not, write to the Free Software
Packit c71e3f
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.