Skip to contentSkip to conversation

kpatch: dynamic kernel patching

PATCH In review Josh Poimboeuf 2 participants 5 messages Jul 15, 2014 Documentation

lore
Share & tools
lore
#0 lore raw

v2 cover kpatch: dynamic kernel patching

This is a v2 posting of the kpatch core module, based on 3.16-rc5. There have been many improvements since v1
(https://lkml.org/lkml/2014/5/1/273):

- Dynamic relocation support
- Per-object patching
- Module patching and deferred module patching
- User load/unload hook functions
- Force unsafe flag for skipping activeness safety stack check
- Dump stack on activeness safety error
- Function graph tracer compatibility fix

kpatch enables dynamically patching a running kernel. The kernel piece of it ("core module") is completely self-contained in a GPL module. It compiles and works without needing to change any kernel code. We already have it working fine on Fedora and RHEL with stock kernels. We've also gotten user reports of it working on Ubuntu, Debian and Arch Linux.

This patch set is for the core module, which provides the kernel infrastructure for kpatch. It has a kpatch_register() interface which allows kernel modules ("patch modules") to replace old functions with new ones which are loaded with the modules.

There are also some user space tools [1] which convert source patches to binary patch modules. The user space tools aren't included in this patch set. But it might also make sense to merge them because of how closely they integrate with the core module.

kpatch advantages compared to kGraft:

* 100% self-contained in its own module.

* Doesn't rely on changing all the kthreads.

* Patch is applied atomically using stop_machine(), so it's safer with respect to data semantic changes.

* Patching atomically also makes it much easier to understand and analyze a patch to determine whether it's safe for live patching.

* Already supports many advanced features which kGraft is lacking:
- patched functions can access non-exported symbols, e.g. static variables and functions
- safe unpatching
- module patching (and deferred module patching)
- atomic patch replacement
- supports atomic load/unload user hook functions
- proper duplicate symbol handling
- address verification sanity checks
- sophisticated user space tools for analyzing and converting source patches to binary patch modules
- ability to properly deal with many special sections (__bug_table,

    .data..percpu, etc)

kpatch disadvantages compared to kGraft:

* There is some stop_machine() latency. But we've found that stop_machine() is still pretty fast. We measured ~1ms on an idle system and ~40ms on a heavily loaded 16 CPU system.

Other previously discussed issues:

* Ability to patch functions which are always in use:

Before it was brought up that kpatch can't patch functions which are always on the stack of at least one task. That limitation has now been removed with the new KPATCH_FORCE_UNSAFE macro which allows patch authors to skip the backtrace check for patches which don't change data semantics.

* Freezing/parking of kernel threads:

Currently we don't freeze or park kernel threads. Instead we just put them to sleep. We _could_ do it, but many threads are not freezable or parkable. And I think it would need more discussion anyway. It's definitely not a cure-all because you still have to worry about user threads.

With our current approach, when analyzing whether patches are safe to apply live, we assume that all kernel and user threads will be asleep. We make no assumptions that any threads will be frozen. In general we avoid changing data and data semantics as much as possible, so it shouldn't matter in most cases. Personally I haven't yet run into a case where freezing kernel threads would have made a patch become "safe".

Merge status:

I think the only real disadvantage of kpatch compared to kGraft is the stop_machine() latency. But the latency is quite small and I think it's worth the trade-off to get the advantages listed above.

I think we have the same goals as kGraft, and it would be great if we could find a way to combine our approaches somehow. It seems to me that the two approaches are incompatible because a patch author must know in advance which context a patch will be applied before being able to deem the patch safe. For example a patch which changes data semantics might be safe when applied in the kpatch context but unsafe in the kGraft context. But any ideas about how we can reasonably combine the two approaches are welcome.

Otherwise, if there are no major objections, I think it's in pretty good shape for being merged. We've had a lot of user interest in kpatch, and the number of users of our out-of-tree module on github [1] seems to be growing. It would be very helpful to move it in-tree so that we have a standardized upstream implementation.

Special thanks to the following people who have contributed to this code:

- Seth Jennings
- Masami Hiramatsu
- Jincheng Miao
- Jan Stancek
- Gaetan Trellu

[1] https://github.com/dynup/kpatch

Josh Poimboeuf (2):
kpatch: add TAINT_KPATCH flag
kpatch: add kpatch core module

 Documentation/kpatch.txt        |  209 ++++++++
 Documentation/oops-tracing.txt  |    3 +
 Documentation/sysctl/kernel.txt |    1 +
 MAINTAINERS                     |    9 +
 arch/Kconfig                    |   16 +
 include/linux/kernel.h          |    1 +
 include/linux/kpatch.h          |   95 ++++
 kernel/Makefile                 |    1 +
 kernel/kpatch/Makefile          |    1 +
 kernel/kpatch/kpatch.c          | 1041 +++++++++++++++++++++++++++++++++++++++
 kernel/panic.c                  |    2 +
 11 files changed, 1379 insertions(+)
 create mode 100644 Documentation/kpatch.txt
 create mode 100644 include/linux/kpatch.h
 create mode 100644 kernel/kpatch/Makefile
 create mode 100644 kernel/kpatch/kpatch.c
Signature · 1 line
1.9.3
#1 lore raw

PATCH v2 1/2 kpatch: add TAINT_KPATCH flag

Add a TAINT_KPATCH flag to be set whenever a kpatch patch module successfully replaces a function.

---
 Documentation/oops-tracing.txt  | 3 +++
 Documentation/sysctl/kernel.txt | 1 +
 include/linux/kernel.h          | 1 +
 kernel/panic.c                  | 2 ++
 4 files changed, 7 insertions(+)
Documentation/oops-tracing.txt +3 -0
index e315599..cd89895 100644
@@ -268,6 +268,9 @@ characters, each representing a particular tainted value.
  14: 'E' if an unsigned module has been loaded in a kernel supporting
      module signature.
 
+ 15: 'K' if a kpatch hot patch module has replaced any functions in the
+     running kernel.
+
 The primary reason for the 'Tainted: ' string is to tell kernel
 debuggers if this is a clean kernel or if anything unusual has
 occurred.  Tainting is permanent: even if an offending module is
Documentation/sysctl/kernel.txt +1 -0
index c14374e..1009d22 100644
@@ -826,6 +826,7 @@ can be ORed together:
 4096 - An out-of-tree module has been loaded.
 8192 - An unsigned module has been loaded in a kernel supporting module
        signature.
+16384 - A kpatch module has replaced a function in the running kernel.
 
 ==============================================================
 
include/linux/kernel.h +1 -0
index 4c52907..cdfbe73 100644
@@ -470,6 +470,7 @@ extern enum system_states {
 #define TAINT_FIRMWARE_WORKAROUND	11
 #define TAINT_OOT_MODULE		12
 #define TAINT_UNSIGNED_MODULE		13
+#define TAINT_KPATCH			14
 
 extern const char hex_asc[];
 #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
kernel/panic.c +2 -0
index 62e16ce..e780909 100644
@@ -224,6 +224,7 @@ static const struct tnt tnts[] = {
 	{ TAINT_FIRMWARE_WORKAROUND,	'I', ' ' },
 	{ TAINT_OOT_MODULE,		'O', ' ' },
 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
+	{ TAINT_KPATCH,			'K', ' ' },
 };
 
 /**
@@ -243,6 +244,7 @@ static const struct tnt tnts[] = {
  *  'I' - Working around severe firmware bug.
  *  'O' - Out-of-tree module has been loaded.
  *  'E' - Unsigned module has been loaded.
+ *  'K' - kpatch replaced a function.
  *
  *	The string is overwritten by the next call to print_tainted().
  */
Signature · 1 line
1.9.3
#2 lore raw

PATCH v2 2/2 kpatch: add kpatch core module

In reply to Josh Poimboeuf

Add the kpatch core module. It's a self-contained module with a kernel patching infrastructure that enables patching a running kernel without rebooting or restarting any processes. Kernel modules ("patch modules") can call kpatch_register() to replace old functions with new ones.

Before applying a patch, kpatch checks the stacks of all tasks in stop_machine() to ensure that the patch is applied atomically, to prevent any weird effects from function interface changes or data semantic changes that might occur between old and new versions of the functions running simultaneously. If any of the to-be-patched functions are on the stack, it fails with -EBUSY.

ftrace is used to do the code modification. For each function to be patched, kpatch registers an ftrace ops handler. When called, the handler modifies regs->ip on the stack and returns back to ftrace, which restores the RIP and "returns" to the beginning of the new function.

Other features:

- safe unpatching
- module patching and deferred module patching
- dynamic relocations for accessing non-exported symbols
- atomic patch replacement
- supports atomic load/unload user hook functions
- support for multiple patch modules
- ability to force skip the activeness safety stack check for a given function
- kpatch_[register|unregister] are properly synchronized with kpatch_ftrace_handler() when it runs in NMI context (thanks to Masami for helping with this)

Cc Seth Jennings <[email protected]>
Cc Masami Hiramatsu <[email protected]>
---
 Documentation/kpatch.txt |  209 ++++++++++
 MAINTAINERS              |    9 +
 arch/Kconfig             |   16 +
 include/linux/kpatch.h   |   95 +++++
 kernel/Makefile          |    1 +
 kernel/kpatch/Makefile   |    1 +
 kernel/kpatch/kpatch.c   | 1041 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1372 insertions(+)
 create mode 100644 Documentation/kpatch.txt
 create mode 100644 include/linux/kpatch.h
 create mode 100644 kernel/kpatch/Makefile
 create mode 100644 kernel/kpatch/kpatch.c
Documentation/kpatch.txt new file +209 -0
new file mode 100644
index 0000000..f807bea
@@ -0,0 +1,209 @@
+kpatch: dynamic kernel patching
+===============================
+
+kpatch is a Linux dynamic kernel patching infrastructure which allows you to
+patch a running kernel without rebooting or restarting any processes.  It
+enables sysadmins to apply critical security patches to the kernel immediately,
+without having to wait for long-running tasks to complete, for users to log
+off, or for scheduled reboot windows.  It gives more control over uptime
+without sacrificing security or stability.
+
+How it works
+------------
+
+kpatch works at a function granularity: old functions are replaced with new
+ones.  It has four main components:
+
+- **kpatch-build**: a collection of tools which convert a source diff patch to
+  a patch module.  They work by compiling the kernel both with and without
+  the source patch, comparing the binaries, and generating a patch module
+  which includes new binary versions of the functions to be replaced.
+
+- **patch module**: a kernel module (.ko file) which includes the
+  replacement functions and metadata about the original functions.
+
+- **kpatch core module**: the kernel infrastructure which provides an interface
+  for the patch modules to register new functions for replacement.  It uses the
+  kernel ftrace subsystem to hook into the original function's mcount call
+  instruction, so that a call to the original function is redirected to the
+  replacement function.
+
+- **kpatch utility:** a command-line tool which allows a user to manage a
+  collection of patch modules.  One or more patch modules may be
+  configured to load at boot time, so that a system can remain patched
+  even after a reboot into the same version of the kernel.
+
+
+How to use it
+-------------
+
+Currently, only the core module is in the kernel tree.  The supporting
+The kpatch-build and kpatch utility tools can be found at:
+
+  https://github.com/dynup/kpatch
+
+You can also find directions there for how to create binary patch modules and
+load them into your kernel.
+
+
+Limitations
+-----------
+
+- Patches to functions which are always on the stack of at least one
+  process in the system are not supported.  Examples: schedule(),
+  sys_poll(), sys_select(), sys_read(), sys_nanosleep().  Attempting to
+  apply such a patch will cause the insmod of the patch module to return
+  an error.
+
+- Patches which modify init functions (annotated with `__init`) are not
+  supported.  kpatch-build will return an error if the patch attempts
+  to do so.
+
+- Patches which modify statically allocated data are not supported.
+  kpatch-build will detect that and return an error.  (In the future
+  we will add a facility to support it.  It will probably require the
+  user to write code which runs at patch module loading time which manually
+  updates the data.)
+
+- Patches which change the way a function interacts with dynamically
+  allocated data might be safe, or might not.  It isn't possible for
+  kpatch-build to verify the safety of this kind of patch.  It's up to
+  the user to understand what the patch does, whether the new functions
+  interact with dynamically allocated data in a different way than the
+  old functions did, and whether it would be safe to atomically apply
+  such a patch to a running kernel.
+
+- Patches which modify functions in vdso are not supported.  These run in
+  user-space and ftrace can't hook them.
+
+- Some incompatibilities currently exist between kpatch and usage of ftrace and
+  kprobes.  See the Frequently Asked Questions section for more details.
+
+
+Frequently Asked Questions
+--------------------------
+
+**Q. Isn't this just a virus/rootkit injection framework?**
+
+kpatch uses kernel modules to replace code.  It requires the `CAP_SYS_MODULE`
+capability.  If you already have that capability, then you already have the
+ability to arbitrarily modify the kernel, with or without kpatch.
+
+**Q. How can I detect if somebody has patched the kernel?**
+
+When a patch module is loaded, the `TAINT_KPATCH` flag is set.  To test for it,
+`cat /proc/sys/kernel/tainted` and check to see if the value of 16384 has been
+OR'ed in.
+
+Note that the `TAINT_OOT_MODULE` flag (4096) will also be set, since the patch
+module is built outside the Linux kernel source tree.
+
+If your patch module is unsigned, the `TAINT_UNSIGNED_MODULE` flag (8192) will
+also be set.
+
+**Q. Will it destabilize my system?**
+
+No, as long as the patch is chosen carefully.  See the Limitations section
+above.
+
+**Q. Why does kpatch use ftrace to jump to the replacement function instead of
+adding the jump directly?**
+
+ftrace owns the first "call mcount" instruction of every kernel function.  In
+order to keep compatibility with ftrace, we go through ftrace rather than
+updating the instruction directly.  This approach also ensures that the code
+modification path is reliable, since ftrace has been doing it successfully for
+years.
+
+**Q Is kpatch compatible with \<insert kernel debugging subsystem here\>?**
+
+We aim to be good kernel citizens and maintain compatibility.  A kpatch
+replacement function is no different than a function loaded by any other kernel
+module.  Each replacement function has its own symbol name and kallsyms entry,
+so it looks like a normal function to the kernel.
+
+- **oops stack traces**: Yes.  If the replacement function is involved in an
+  oops, the stack trace will show the function and kernel module name of the
+  replacement function, just like any other kernel module function.  The oops
+  message will also show the taint flag (currently `TAINT_USER`).
+- **kdump/crash**: Yes.  Replacement functions are normal functions, so crash
+  will have no issues.
+- **ftrace**: Yes, but certain uses of ftrace which involve opening the
+  `/sys/kernel/debug/tracing/trace` file or using `trace-cmd record` can result
+  in a tiny window of time where a patch gets temporarily disabled.  Therefore
+  it's a good idea to avoid using ftrace on a patched system until this issue
+  is resolved.
+- **systemtap/kprobes**: Some incompatibilities exist.
+  - If you setup a kprobe module at the beginning of a function before loading
+    a kpatch module, and they both affect the same function, kprobes "wins"
+    until the kprobe has been unregistered.  This is tracked in issue
+    [#47](https://github.com/dynup/kpatch/issues/47).
+  - Setting a kretprobe before loading a kpatch module could be unsafe.  See
+    issue [#67](https://github.com/dynup/kpatch/issues/67).
+- **perf**: Yes.
+- **tracepoints**: Patches to a function which uses tracepoints will result in
+  the tracepoints being effectively disabled as long as the patch is applied.
+
+**Q. Why not use something like kexec instead?**
+
+If you want to avoid a hardware reboot, but are ok with restarting processes,
+kexec is a good alternative.
+
+**Q. If an application can't handle a reboot, it's designed wrong.**
+
+That's a good poi... [system reboots]
+
+**Q. What changes are needed in other upstream projects?**
+
+We hope to make the following changes to other projects:
+
+- kernel:
+	- ftrace improvements to close any windows that would allow a patch to
+	  be inadvertently disabled
+	- hot patch taint flag
+	- possibly the kpatch core module itself
+
+- crash:
+	- point it to where the patch modules and corresponding debug symbols
+	  live on the file system
+
+**Q: Is it possible to register a function that gets called atomically with
+`stop_machine` when the patch module loads and unloads?**
+
+We do have plans to implement something like that.
+
+**Q. What kernels are supported?**
+
+kpatch needs gcc >= 4.8 and Linux >= 3.7 for use of the -mfentry flag.
+
+**Q. Is it possible to remove a patch?**
+
+Yes.  Just run `kpatch unload` which will disable and unload the patch module
+and restore the function to its original state.
+
+**Q. Can you apply multiple patches?**
+
+Yes, but to prevent any unexpected interactions between multiple patch modules,
+it's recommended that you only have a single patch loaded at any given time.
+This can be achieved by combining the new patch with the previous patch using
+`combinediff` before running `kpatch-build`.  You can then the `kpatch replace`
+command to atomically replace the old patch module with the new cumulative one.
+
+**Q. Why did kpatch-build detect a changed function that wasn't touched by the
+source patch?**
+
+There could be a variety of reasons for this, such as:
+
+- The patch changed an inline function.
+- The compiler decided to inline a changed function, resulting in the outer
+  function getting recompiled.  This is common in the case where the inner
+  function is static and is only called once.
+- The function uses a WARN() or WARN_ON() macro.  These macros embed the source
+  code line number (`__LINE__`) into an instruction.  If a function was changed
+  higher up in the file, it will affect the line numbers for all subsequent
+  WARN calls in the file, resulting in recompilation of their functions.  If
+  this happens to you, you can usually just ignore it, as patching a few extra
+  functions isn't typically a problem.  If it becomes a problem for whatever
+  reason, you can change the source patch to redefine the WARN macro for the
+  affected files, such that it hard codes the old line number instead of using
+  `__LINE__`, for example.
MAINTAINERS +9 -0
index e31c874..c04a99c 100644
@@ -5284,6 +5284,15 @@ F:	include/linux/kmemleak.h
 F:	mm/kmemleak.c
 F:	mm/kmemleak-test.c
 
+KPATCH
+M:	Josh Poimboeuf <[email protected]>
+M:	Seth Jennings <[email protected]>
+W:	https://github.com/dynup/kpatch
+S:	Maintained
+F:	Documentation/kpatch.txt
+F:	include/linux/kpatch.h
+F:	kernel/kpatch/*
+
 KPROBES
 M:	Ananth N Mavinakayanahalli <[email protected]>
 M:	Anil S Keshavamurthy <[email protected]>
arch/Kconfig +16 -0
index 97ff872..3726780 100644
@@ -472,6 +472,22 @@ config HAVE_IRQ_EXIT_ON_IRQ_STACK
 	  This spares a stack switch and improves cache usage on softirq
 	  processing.
 
+config KPATCH
+	tristate "kpatch dynamic kernel updating support"
+	default n
+	depends on MODULES
+	depends on FTRACE
+	depends on HAVE_FENTRY
+	depends on SYSFS
+	depends on FUNCTION_TRACER
+	depends on KALLSYMS_ALL
+	help
+	  kpatch is a dynamic kernel patching infrastructure which allows you
+	  to dynamically update the code of a running kernel without rebooting
+	  or restarting any processes.
+
+	  If in doubt, say "N".
+
 #
 # ABI hall of shame
 #
include/linux/kpatch.h new file +95 -0

95 lines not on this page · raw message

kernel/Makefile +1 -0
index f2a8b62..2e56269 100644
@@ -87,6 +87,7 @@ obj-$(CONFIG_RING_BUFFER) += trace/
 obj-$(CONFIG_TRACEPOINTS) += trace/
 obj-$(CONFIG_IRQ_WORK) += irq_work.o
 obj-$(CONFIG_CPU_PM) += cpu_pm.o
+obj-$(CONFIG_KPATCH) += kpatch/
 
 obj-$(CONFIG_PERF_EVENTS) += events/
 
kernel/kpatch/Makefile new file +1 -0
new file mode 100644
index 0000000..800241a
@@ -0,0 +1 @@
+obj-$(CONFIG_KPATCH) := kpatch.o
kernel/kpatch/kpatch.c new file +1041 -0

1,041 lines not on this page · raw message

Signature · 1 line
1.9.3
#3 lore raw
In reply to Josh Poimboeuf

Hello Josh,

being able to patch a live kernel is very interesting feature. I looked through you patch and some questions remained:

In Documentation/kpatch.txt I found no description on how an out of kernel program uses the new code. See https://lkml.org/lkml/2014/7/14/862

Please, create man pages for kpatch_register and kpatch_unregister before moving kpatch into the kernel (if these are the relevant API calls).

I saw some x86 specific code in
https://github.com/dynup/kpatch/tree/master/kpatch-build/insn/asm
How does your code apply to other architectures?

For me the man pages on
https://github.com/dynup/kpatch/
do not yet include all information that a novice user of kpatch would like to receive.

The man pages do not match the format of the other kernel man pages.

The man pages are not linked to each other nor to Documentation/kpatch.txt.

What is the overall status of the complete kpatch solution? Is is still beta?

Should the kpatch and kpatch-build scripts be distributed with the kernel? Or what are your plans?

Best regards

Heinrich Schuchardt

Quoted 147 lines · Josh Poimboeuf

This is a v2 posting of the kpatch core module, based on 3.16-rc5. There have been many improvements since v1
(https://lkml.org/lkml/2014/5/1/273):

- Dynamic relocation support
- Per-object patching
- Module patching and deferred module patching
- User load/unload hook functions
- Force unsafe flag for skipping activeness safety stack check
- Dump stack on activeness safety error
- Function graph tracer compatibility fix

kpatch enables dynamically patching a running kernel. The kernel piece of it ("core module") is completely self-contained in a GPL module. It compiles and works without needing to change any kernel code. We already have it working fine on Fedora and RHEL with stock kernels. We've also gotten user reports of it working on Ubuntu, Debian and Arch Linux.

This patch set is for the core module, which provides the kernel infrastructure for kpatch. It has a kpatch_register() interface which allows kernel modules ("patch modules") to replace old functions with new ones which are loaded with the modules.

There are also some user space tools [1] which convert source patches to binary patch modules. The user space tools aren't included in this patch set. But it might also make sense to merge them because of how closely they integrate with the core module.

kpatch advantages compared to kGraft:

* 100% self-contained in its own module.

* Doesn't rely on changing all the kthreads.

* Patch is applied atomically using stop_machine(), so it's safer with respect to data semantic changes.

* Patching atomically also makes it much easier to understand and analyze a patch to determine whether it's safe for live patching.

* Already supports many advanced features which kGraft is lacking:
- patched functions can access non-exported symbols, e.g. static variables and functions
- safe unpatching
- module patching (and deferred module patching)
- atomic patch replacement
- supports atomic load/unload user hook functions
- proper duplicate symbol handling
- address verification sanity checks
- sophisticated user space tools for analyzing and converting source patches to binary patch modules
- ability to properly deal with many special sections (__bug_table,

     .data..percpu, etc)

kpatch disadvantages compared to kGraft:

* There is some stop_machine() latency. But we've found that stop_machine() is still pretty fast. We measured ~1ms on an idle system and ~40ms on a heavily loaded 16 CPU system.

Other previously discussed issues:

* Ability to patch functions which are always in use:

Before it was brought up that kpatch can't patch functions which are always on the stack of at least one task. That limitation has now been removed with the new KPATCH_FORCE_UNSAFE macro which allows patch authors to skip the backtrace check for patches which don't change data semantics.

* Freezing/parking of kernel threads:

Currently we don't freeze or park kernel threads. Instead we just put them to sleep. We _could_ do it, but many threads are not freezable or parkable. And I think it would need more discussion anyway. It's definitely not a cure-all because you still have to worry about user threads.

With our current approach, when analyzing whether patches are safe to apply live, we assume that all kernel and user threads will be asleep. We make no assumptions that any threads will be frozen. In general we avoid changing data and data semantics as much as possible, so it shouldn't matter in most cases. Personally I haven't yet run into a case where freezing kernel threads would have made a patch become "safe".

Merge status:

I think the only real disadvantage of kpatch compared to kGraft is the stop_machine() latency. But the latency is quite small and I think it's worth the trade-off to get the advantages listed above.

I think we have the same goals as kGraft, and it would be great if we could find a way to combine our approaches somehow. It seems to me that the two approaches are incompatible because a patch author must know in advance which context a patch will be applied before being able to deem the patch safe. For example a patch which changes data semantics might be safe when applied in the kpatch context but unsafe in the kGraft context. But any ideas about how we can reasonably combine the two approaches are welcome.

Otherwise, if there are no major objections, I think it's in pretty good shape for being merged. We've had a lot of user interest in kpatch, and the number of users of our out-of-tree module on github [1] seems to be growing. It would be very helpful to move it in-tree so that we have a standardized upstream implementation.

Special thanks to the following people who have contributed to this code:

- Seth Jennings
- Masami Hiramatsu
- Jincheng Miao
- Jan Stancek
- Gaetan Trellu

[1] https://github.com/dynup/kpatch

Josh Poimboeuf (2):
kpatch: add TAINT_KPATCH flag
kpatch: add kpatch core module

  Documentation/kpatch.txt        |  209 ++++++++
  Documentation/oops-tracing.txt  |    3 +
  Documentation/sysctl/kernel.txt |    1 +
  MAINTAINERS                     |    9 +
  arch/Kconfig                    |   16 +
  include/linux/kernel.h          |    1 +
  include/linux/kpatch.h          |   95 ++++
  kernel/Makefile                 |    1 +
  kernel/kpatch/Makefile          |    1 +
  kernel/kpatch/kpatch.c          | 1041 +++++++++++++++++++++++++++++++++++++++
  kernel/panic.c                  |    2 +
  11 files changed, 1379 insertions(+)

create mode 100644 Documentation/kpatch.txt
create mode 100644 include/linux/kpatch.h
create mode 100644 kernel/kpatch/Makefile
create mode 100644 kernel/kpatch/kpatch.c

#4 lore raw
Quoted 8 lines · Xypron

Hello Josh,

being able to patch a live kernel is very interesting feature. I looked through you patch and some questions remained:

In Documentation/kpatch.txt I found no description on how an out of kernel program uses the new code.
See https://lkml.org/lkml/2014/7/14/862

You're right, these interfaces aren't documented very well. Currently the only user of these interfaces is the kpatch-build tool in our github repository. It builds kernel patch modules which call kpatch_register() and kpatch_unregister().

The interfaces are quite complex. They require an intelligent generation tool to create a kernel module which uses them. This is by necessity, in order to allow dynamic relocations so that patched functions can access unexported kernel symbols.

So I really wonder if it even makes sense to document them. Instead maybe we should also try to put the kpatch-build tool in the Linux kernel tree in tools/kpatch. Then we would just need to document the use of the kpatch-build tool.

Quoted 2 lines

Please, create man pages for kpatch_register and kpatch_unregister before moving kpatch into the kernel (if these are the relevant API calls).

I don't think kernel man pages would be appropriate because these interfaces aren't system calls. They're meant to be called by kernel modules.

Quoted 3 lines

I saw some x86 specific code in
https://github.com/dynup/kpatch/tree/master/kpatch-build/insn/asm
How does your code apply to other architectures?

Right now only x86_64 is supported, as it's the only architecture which has the ftrace features we need. But we do hope to add support for more architectures, probably at least powerpc and s390.

Quoted 4 lines

For me the man pages on
https://github.com/dynup/kpatch/
do not yet include all information that a novice user of kpatch would like to receive.

The end user just needs to know how to use the kpatch-build and kpatch tools. We already have man pages for those.

Quoted 1 line

The man pages do not match the format of the other kernel man pages.

Hm, why do they need to match the format of kernel man pages? They're user space tools.

Quoted 1 line

The man pages are not linked to each other nor to Documentation/kpatch.txt.

True, but it hasn't been merged yet :-) But yes, the kpatch-build and kpatch man pages should probably refer to each other (and to any future kernel documentation).

Quoted 2 lines

What is the overall status of the complete kpatch solution? Is is still beta?

I would say it's still in an experimental phase. But very close to being used more extensively by real users.

Quoted 2 lines

Should the kpatch and kpatch-build scripts be distributed with the kernel? Or what are your plans?

The more I think about it, the more it makes sense to put kpatch-build in the kernel tree, because of the tight dependencies between it and the kpatch core module.

I'm not quite sure whether the kpatch script, which manages the patch modules after they have been built (e.g. loading/unloading, replacing, installing to initramfs), belongs in the kernel tree because it's much less loosely coupled to the kernel interfaces.

Quoted 4 lines

Best regards

Heinrich Schuchardt

Signature · 1 line
Josh