Skip to contentSkip to conversation

Kernel Live Patching

PATCH Superseded Seth Jennings 13 participants 73 messages Nov 6, 2014 – Dec 1, 2014

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Kernel Live Patching

This patchset implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions. It represents the greatest common functionality set between kpatch [1] and kGraft [2] and can accept patches built using either method. This solution was discussed in the Live Patching Mini-conference at LPC 2014 [3].

The model consists of a live patching "core" that provides an interface for other "patch" kernel modules to register patches with the core.

Patch modules contain the new function code and create an lp_patch structure containing the required data about what functions to patch, where the new code for each patched function resides, and in which kernel object (vmlinux or module) the function to be patch resides. The patch module then invokes the lp_register_patch() function to register with the core module, then lp_enable_patch() to have to core module redirect the execution paths using ftrace.

An example patch module can be found here:
https://github.com/spartacus06/livepatch/blob/master/patch/patch.c

The live patching core creates a sysfs hierarchy for user-level access to live patching information. The hierarchy is structured like this:

/sys/kernel/livepatch /sys/kernel/livepatch/<patch> /sys/kernel/livepatch/<patch>/enabled /sys/kernel/livepatch/<patch>/<object> /sys/kernel/livepatch/<patch>/<object>/<func> /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr

The new_addr attribute provides the location of the new version of the function within the patch module. The old_addr attribute provides the location of the old function. The old function is located using one of two methods: it is either provided by the patch module (only possible for a function in vmlinux) or kallsyms lookup. Symbol ambiguity results in a failure.

The core holds a reference on any kernel module that is patched to ensure it does not unload while we are redirecting calls from it. Also, the core takes a reference on the patch module itself to keep it from unloading. This is because, without a mechanism to ensure that no thread is currently executing in the patched function, we can not determine whether it is safe to unload the patch module. For this reason, unloading patch modules is currently not allowed.

The core is able to release its reference on patched modules by disabling all patches that patch a function in that module. Disabling patches can be done like this:

echo 0 > /sys/kernel/livepatch/<patch>/enabled

Patches can also be re-enabled, however, the core with retake any reference on a kernel module that contains a patched function.

If a patch module contains a patch for a module that is not currently loaded, there is nothing to patch so the core does nothing for that object. However, the core registers a module notifier so that if the module is ever loaded, it is immediately patched.

kpatch and kGraft each have their own mechanisms for ensuring system consistency during the patching process. This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied for now.

[1] https://github.com/dynup/kpatch
[2] https://git.kernel.org/cgit/linux/kernel/git/jirislaby/kgraft.git/
[3] https://etherpad.fr/p/LPC2014_LivePatching

Seth Jennings (2):
kernel: add TAINT_LIVEPATCH
kernel: add support for live patching

 Documentation/oops-tracing.txt  |    2 +
 Documentation/sysctl/kernel.txt |    1 +
 MAINTAINERS                     |   10 +
 arch/x86/Kconfig                |    2 +
 include/linux/kernel.h          |    1 +
 include/linux/livepatch.h       |   45 ++
 kernel/Makefile                 |    1 +
 kernel/livepatch/Kconfig        |   11 +
 kernel/livepatch/Makefile       |    3 +
 kernel/livepatch/core.c         | 1020 +++++++++++++++++++++++++++++++++++++++
 kernel/panic.c                  |    2 +
 11 files changed, 1098 insertions(+)
 create mode 100644 include/linux/livepatch.h
 create mode 100644 kernel/livepatch/Kconfig
 create mode 100644 kernel/livepatch/Makefile
 create mode 100644 kernel/livepatch/core.c
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/2 kernel: add TAINT_LIVEPATCH

This adds a new taint flag to indicate when the kernel or a kernel module has been live patched. This will provide a clean indication in bug reports that live patching was used.

Additionally, if the crash occurs in a live patched function, the live patch module will appear beside the patched function in the backtrace.

---
 Documentation/oops-tracing.txt  | 2 ++
 Documentation/sysctl/kernel.txt | 1 +
 include/linux/kernel.h          | 1 +
 kernel/panic.c                  | 2 ++
 4 files changed, 6 insertions(+)
Documentation/oops-tracing.txt +2 -0
index beefb9f..f3ac05c 100644
@@ -270,6 +270,8 @@ characters, each representing a particular tainted value.
 
  15: 'L' if a soft lockup has previously occurred on the system.
 
+ 16: 'K' if the kernel has been live patched.
+
 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 d7fc4ab..085f73b 100644
@@ -831,6 +831,7 @@ can be ORed together:
 8192 - An unsigned module has been loaded in a kernel supporting module
        signature.
 16384 - A soft lockup has previously occurred on the system.
+32768 - The kernel has been live patched.
 
 ==============================================================
 
include/linux/kernel.h +1 -0
index 446d76a..a6aa2df 100644
@@ -473,6 +473,7 @@ extern enum system_states {
 #define TAINT_OOT_MODULE		12
 #define TAINT_UNSIGNED_MODULE		13
 #define TAINT_SOFTLOCKUP		14
+#define TAINT_LIVEPATCH			15
 
 extern const char hex_asc[];
 #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
kernel/panic.c +2 -0
index d09dc5c..46bca3d 100644
@@ -225,6 +225,7 @@ static const struct tnt tnts[] = {
 	{ TAINT_OOT_MODULE,		'O', ' ' },
 	{ TAINT_UNSIGNED_MODULE,	'E', ' ' },
 	{ TAINT_SOFTLOCKUP,		'L', ' ' },
+	{ TAINT_LIVEPATCH,		'K', ' ' },
 };
 
 /**
@@ -244,6 +245,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' - Kernel has been live patched.
  *
  *	The string is overwritten by the next call to print_tainted().
  */
Signature · 1 line
1.9.3
#2 lore raw

Re: [PATCH 1/2] kernel: add TAINT_LIVEPATCH

Quoted 45 lines · Seth Jennings

This adds a new taint flag to indicate when the kernel or a kernel module has been live patched. This will provide a clean indication in bug reports that live patching was used.

Additionally, if the crash occurs in a live patched function, the live patch module will appear beside the patched function in the backtrace.

---
 Documentation/oops-tracing.txt  | 2 ++
 Documentation/sysctl/kernel.txt | 1 +
 include/linux/kernel.h          | 1 +
 kernel/panic.c                  | 2 ++
 4 files changed, 6 insertions(+)
Documentation/oops-tracing.txt +2 -0
index beefb9f..f3ac05c 100644
@@ -270,6 +270,8 @@ characters, each representing a particular tainted value.
 
  15: 'L' if a soft lockup has previously occurred on the system.
 
+ 16: 'K' if the kernel has been live patched.
+
 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 d7fc4ab..085f73b 100644
@@ -831,6 +831,7 @@ can be ORed together:
 8192 - An unsigned module has been loaded in a kernel supporting module
        signature.
 16384 - A soft lockup has previously occurred on the system.
+32768 - The kernel has been live patched.
 
 ==============================================================
 
include/linux/kernel.h +0 -0
index 446d76a..a6aa2df 100644

include/linux/kernel.h extern enum system_states { · line 479

Quoted 7 lines
include/linux/kernel.h +1 -0
@@ -473,6 +473,7 @@ extern enum system_states {
 #define TAINT_OOT_MODULE		12
 #define TAINT_UNSIGNED_MODULE		13
 #define TAINT_SOFTLOCKUP		14
+#define TAINT_LIVEPATCH			15
 
 extern const char hex_asc[];
 #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]

Note, this conflicts with a taint value that others are proposing for something else, so be aware you might run into problems when you hit linux-next.

thanks,

greg k-h

#3 lore raw

Re: [PATCH 1/2] kernel: add TAINT_LIVEPATCH

Quoted 51 lines · Greg KH
Quoted 50 lines · Seth Jennings

This adds a new taint flag to indicate when the kernel or a kernel module has been live patched. This will provide a clean indication in bug reports that live patching was used.

Additionally, if the crash occurs in a live patched function, the live patch module will appear beside the patched function in the backtrace.

---
 Documentation/oops-tracing.txt  | 2 ++
 Documentation/sysctl/kernel.txt | 1 +
 include/linux/kernel.h          | 1 +
 kernel/panic.c                  | 2 ++
 4 files changed, 6 insertions(+)
Documentation/oops-tracing.txt +2 -0
index beefb9f..f3ac05c 100644
@@ -270,6 +270,8 @@ characters, each representing a particular tainted value.
 
  15: 'L' if a soft lockup has previously occurred on the system.
 
+ 16: 'K' if the kernel has been live patched.
+
 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 d7fc4ab..085f73b 100644
@@ -831,6 +831,7 @@ can be ORed together:
 8192 - An unsigned module has been loaded in a kernel supporting module
        signature.
 16384 - A soft lockup has previously occurred on the system.
+32768 - The kernel has been live patched.
 
 ==============================================================
 
include/linux/kernel.h +1 -0
index 446d76a..a6aa2df 100644
@@ -473,6 +473,7 @@ extern enum system_states {
 #define TAINT_OOT_MODULE		12
 #define TAINT_UNSIGNED_MODULE		13
 #define TAINT_SOFTLOCKUP		14
+#define TAINT_LIVEPATCH			15
 
Quoted 6 lines

include/linux/kernel.h extern enum system_states { · line 479

Quoted 2 lines
include/linux/kernel.h +0 -0
 extern const char hex_asc[];
 #define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]

Note, this conflicts with a taint value that others are proposing for something else, so be aware you might run into problems when you hit linux-next.

Thanks for the notice. I'll continue rebasing the patchset against the latest -next and if the other proposal(s) gets in first, I'll change.

Seth

Quoted 4 lines

thanks,

greg k-h

#4 lore raw

PATCH 2/2 kernel: add support for live patching

In reply to Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

---
 MAINTAINERS               |   10 +
 arch/x86/Kconfig          |    2 +
 include/linux/livepatch.h |   45 ++
 kernel/Makefile           |    1 +
 kernel/livepatch/Kconfig  |   11 +
 kernel/livepatch/Makefile |    3 +
 kernel/livepatch/core.c   | 1020 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1092 insertions(+)
 create mode 100644 include/linux/livepatch.h
 create mode 100644 kernel/livepatch/Kconfig
 create mode 100644 kernel/livepatch/Makefile
 create mode 100644 kernel/livepatch/core.c
MAINTAINERS +10 -0
index f98019e..02d1af7 100644
@@ -5671,6 +5671,16 @@ F:	Documentation/misc-devices/lis3lv02d
 F:	drivers/misc/lis3lv02d/
 F:	drivers/platform/x86/hp_accel.c
 
+LIVE PATCHING
+M:	Josh Poimboeuf <[email protected]>
+M:	Seth Jennings <[email protected]>
+M:	Jiri Kosina <[email protected]>
+M:	Vojtech Pavlik <[email protected]>
+S:	Maintained
+F:	kernel/livepatch/
+F:	include/linux/livepatch.h
+L:	[email protected]
+
 LLC (802.2)
 M:	Arnaldo Carvalho de Melo <[email protected]>
 S:	Maintained
arch/x86/Kconfig +2 -0
index 9cd2578..fb0bb59 100644
@@ -1982,6 +1982,8 @@ config CMDLINE_OVERRIDE
 	  This is used to work around broken boot loaders.  This should
 	  be set to 'N' under normal conditions.
 
+source "kernel/livepatch/Kconfig"
+
 endmenu
 
 config ARCH_ENABLE_MEMORY_HOTPLUG
include/linux/livepatch.h new file +45 -0

45 lines not on this page · raw message

kernel/Makefile +1 -0
index a59481a..616994f 100644
@@ -26,6 +26,7 @@ obj-y += power/
 obj-y += printk/
 obj-y += irq/
 obj-y += rcu/
+obj-y += livepatch/
 
 obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o
 obj-$(CONFIG_FREEZER) += freezer.o
kernel/livepatch/Kconfig new file +11 -0
new file mode 100644
index 0000000..312ed81
@@ -0,0 +1,11 @@
+config LIVE_PATCHING
+	tristate "Live Kernel Patching"
+	depends on DYNAMIC_FTRACE_WITH_REGS && MODULES && SYSFS && KALLSYMS_ALL
+	default m
+	help
+	  Say Y here if you want to support live kernel patching.
+	  This setting has no runtime impact until a live-patch
+	  kernel module that uses the live-patch interface provided
+	  by this option is loaded, resulting in calls to patched
+	  functions being redirected to the new function code contained
+	  in the live-patch module.
kernel/livepatch/Makefile new file +3 -0
new file mode 100644
index 0000000..7c1f008
@@ -0,0 +1,3 @@
+obj-$(CONFIG_LIVE_PATCHING) += livepatch.o
+
+livepatch-objs := core.o
kernel/livepatch/core.c new file +1020 -0

1,020 lines not on this page · raw message

Signature · 1 line
1.9.3
#5 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 7 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

Quoted 6 lines

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

Thanks a lot for having started the work on this!

We will be reviewing it carefully in the coming days and will getting back to you (I was surprised to see that that diffstat indicates that it's actually more code than our whole kgraft implementation including the consistency model :) ).

I have one questions right away though.

kernel/livepatch/core.c line 238

Quoted 8 lines
kernel/livepatch/core.c new file +8 -0
+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/
+
+/*
+ * external symbols are located outside the parent object (where the parent
+ * object is either vmlinux or the kmod being patched).
+ */

I have no ideas what dynrela is, and quickly reading the source doesn't really help too much.

Could you please provide some explanation / pointer to some documentation, explaining what exactly it is, and why should it be part of the common infrastructure?

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#6 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 16 lines · Jiri Kosina
Quoted 7 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

Quoted 6 lines

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

Quoted 6 lines

Thanks a lot for having started the work on this!

We will be reviewing it carefully in the coming days and will getting back to you (I was surprised to see that that diffstat indicates that it's actually more code than our whole kgraft implementation including the consistency model :) ).

The structure allocation and sysfs stuff is a lot of (mundane) code. Lots of boring error path handling too.

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Quoted 15 lines

I have one questions right away though.

kernel/livepatch/core.c line 238

Quoted 8 lines
kernel/livepatch/core.c new file +8 -0
+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/
+
+/*
+ * external symbols are located outside the parent object (where the parent
+ * object is either vmlinux or the kmod being patched).
+ */

I have no ideas what dynrela is, and quickly reading the source doesn't really help too much.

Quoted 3 lines

Could you please provide some explanation / pointer to some documentation, explaining what exactly it is, and why should it be part of the common infrastructure?

Yes, I should explain it.

This is something that is currently only used in the kpatch approach. It allows the patching core to do dynamic relocations on the new function code, similar to what the kernel module linker does, but this works for non-exported symbols as well.

This is so the patch module doesn't have to do a kallsyms lookup on every non-exported symbol that the new functions use.

The fields of the dynrela structure are those of a normal ELF rela entry, except for the "external" field, which conveys information about where the core module should go looking for the symbol referenced in the dynrela entry.

Josh was under the impression that Vojtech was ok with putting the dynrela stuff in the core. Is that not correct (misunderstanding)?

Thanks,
Seth

Quoted 6 lines

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#7 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 25 lines · Seth Jennings
Quoted 13 lines · Jiri Kosina

kernel/livepatch/core.c line 238

Quoted 8 lines · Seth Jennings
kernel/livepatch/core.c new file +8 -0
+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/
+
+/*
+ * external symbols are located outside the parent object (where the parent
+ * object is either vmlinux or the kmod being patched).
+ */

I have no ideas what dynrela is, and quickly reading the source doesn't really help too much.

Quoted 3 lines

Could you please provide some explanation / pointer to some documentation, explaining what exactly it is, and why should it be part of the common infrastructure?

Yes, I should explain it.

This is something that is currently only used in the kpatch approach. It allows the patching core to do dynamic relocations on the new function code, similar to what the kernel module linker does, but this works for non-exported symbols as well.

Quoted 7 lines

This is so the patch module doesn't have to do a kallsyms lookup on every non-exported symbol that the new functions use.

The fields of the dynrela structure are those of a normal ELF rela entry, except for the "external" field, which conveys information about where the core module should go looking for the symbol referenced in the dynrela entry.

BTW, use of the dynrelas is optional, but highly recommended. The kGraft approach of manually doing a kallsyms lookup for each non-exported symbol is inherently dangerous because of duplicate symbols.

Signature · 1 line
Josh
#8 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 10 lines · Seth Jennings

Yes, I should explain it.

This is something that is currently only used in the kpatch approach. It allows the patching core to do dynamic relocations on the new function code, similar to what the kernel module linker does, but this works for non-exported symbols as well.

This is so the patch module doesn't have to do a kallsyms lookup on every non-exported symbol that the new functions use.

Quoted 7 lines

The fields of the dynrela structure are those of a normal ELF rela entry, except for the "external" field, which conveys information about where the core module should go looking for the symbol referenced in the dynrela entry.

Josh was under the impression that Vojtech was ok with putting the dynrela stuff in the core. Is that not correct (misunderstanding)?

Yes, that is correct, as obviously the kpatch way of generating patches by extracting code from a compiled kernel would not be viable without it.

For our own kGraft usage we're choosing to compile patches from C source, and there we can simply replace the function calls by calls via pointer looked up through kallsyms.

However, kGraft also has tools to create patches in an automated way, where the individual functions are extracted from the compiled patched kernel using a modified objopy and this is hitting exactly the same issue of having to do relocation of unexported symbols if any are referenced.

So no objection to the idea. We'll have to look more into the code to comment on the implementation of the dynrela stuff.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#9 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 9 lines · Seth Jennings
Quoted 6 lines

Thanks a lot for having started the work on this!

We will be reviewing it carefully in the coming days and will getting back to you (I was surprised to see that that diffstat indicates that it's actually more code than our whole kgraft implementation including the consistency model :) ).

The structure allocation and sysfs stuff is a lot of (mundane) code. Lots of boring error path handling too.

Also, lpc_create_object(), lpc_create_func(), lpc_create_patch(), lpc_create_objects(), lpc_create_funcs(), ... they all are pretty much alike, and are asking for some kind of unification ... perhaps iterator for generic structure initialization?

I am not also really fully convinced that we need the patch->object->funcs abstraction hierarchy (which also contributes to the structure allocation being rather a spaghetti copy/paste code) ... wouldn't patch->funcs be suffcient, with the "object" being made just a property of the function, for example?

Quoted 2 lines

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Which is still seem to me to be a ratio worth thinking about improving :)

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#10 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 9 lines · Jiri Kosina
Quoted 7 lines · Seth Jennings
Quoted 6 lines

Thanks a lot for having started the work on this!

We will be reviewing it carefully in the coming days and will getting back to you (I was surprised to see that that diffstat indicates that it's actually more code than our whole kgraft implementation including the consistency model :) ).

Quoted 7 lines
Quoted 2 lines

The structure allocation and sysfs stuff is a lot of (mundane) code. Lots of boring error path handling too.

Also, lpc_create_object(), lpc_create_func(), lpc_create_patch(), lpc_create_objects(), lpc_create_funcs(), ... they all are pretty much alike, and are asking for some kind of unification ... perhaps iterator for generic structure initialization?

The allocation and initialization code is very simple and straightforward. I really don't see a problem there.

Can you give an example of what you mean by "iterator for generic structure initialization"?

Quoted 10 lines

I am not also really fully convinced that we need the patch->object->funcs abstraction hierarchy (which also contributes to the structure allocation being rather a spaghetti copy/paste code) ... wouldn't patch->funcs be suffcient, with the "object" being made just a property of the function, for example?

Quoted 2 lines

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Which is still seem to me to be a ratio worth thinking about improving :)

Yes, this code doesn't have a consistency model, but it does have some other non-kGraft things like dynamic relocations, deferred module patching, and a unified API. There's really no point in comparing lines of code.

Signature · 1 line
Josh
#11 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 7 lines · Josh Poimboeuf
Quoted 4 lines

Also, lpc_create_object(), lpc_create_func(), lpc_create_patch(), lpc_create_objects(), lpc_create_funcs(), ... they all are pretty much alike, and are asking for some kind of unification ... perhaps iterator for generic structure initialization?

The allocation and initialization code is very simple and straightforward. I really don't see a problem there.

This really boils down to the question I had in previous mail, whether three-level hierarchy (patch->object->funcs), which is why there is a lot of very alike initialization code, is not a bit over-designed.

Quoted 6 lines
Quoted 6 lines

I am not also really fully convinced that we need the patch->object->funcs abstraction hierarchy (which also contributes to the structure allocation being rather a spaghetti copy/paste code) ... wouldn't patch->funcs be suffcient, with the "object" being made just a property of the function, for example?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Which is still seem to me to be a ratio worth thinking about improving :)

Yes, this code doesn't have a consistency model, but it does have some other non-kGraft things like dynamic relocations,

BTW we need to put those into arch/x86/ as they are unfortunately not generic. But more on this later independently.

Quoted 1 line

deferred module patching,

FWIW kgraft supports that as well.

Quoted 1 line

and a unified API. There's really no point in comparing lines of code.

Oh, sure, I didn't mean that this is any kind of metrics that should be taken too seriously at all. I was just expressing my surprise that unification of the API would bring so much code that it makes the result comparably sized to "the whole thing" :)

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#12 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 7 lines · Jiri Kosina
Quoted 5 lines · Josh Poimboeuf
Quoted 4 lines

Also, lpc_create_object(), lpc_create_func(), lpc_create_patch(), lpc_create_objects(), lpc_create_funcs(), ... they all are pretty much alike, and are asking for some kind of unification ... perhaps iterator for generic structure initialization?

Quoted 6 lines
Quoted 2 lines

The allocation and initialization code is very simple and straightforward. I really don't see a problem there.

This really boils down to the question I had in previous mail, whether three-level hierarchy (patch->object->funcs), which is why there is a lot of very alike initialization code, is not a bit over-designed.

Oh sorry, I missed that point :-) See below.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

I am not also really fully convinced that we need the patch->object->funcs abstraction hierarchy (which also contributes to the structure allocation being rather a spaghetti copy/paste code) ... wouldn't patch->funcs be suffcient, with the "object" being made just a property of the function, for example?

The patched object represents the module being patched (or "vmlinux"). It is much more than a property of the function. Multiple functions can be patched in the same object. There are several things we do on a per-object basis, including try_module_get(), deferred module patching (patching from the module notifier), and dynamic relocations.

Quoted 29 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Which is still seem to me to be a ratio worth thinking about improving :)

Yes, this code doesn't have a consistency model, but it does have some other non-kGraft things like dynamic relocations,

BTW we need to put those into arch/x86/ as they are unfortunately not generic. But more on this later independently.

Quoted 1 line

deferred module patching,

FWIW kgraft supports that as well.

Quoted 1 line

and a unified API. There's really no point in comparing lines of code.

Oh, sure, I didn't mean that this is any kind of metrics that should be taken too seriously at all. I was just expressing my surprise that unification of the API would bring so much code that it makes the result comparably sized to "the whole thing" :)

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
Signature · 1 line
Josh
#13 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Jiri Kosina
Quoted 7 lines · Jiri Kosina
Quoted 5 lines · Josh Poimboeuf
Quoted 4 lines

Also, lpc_create_object(), lpc_create_func(), lpc_create_patch(), lpc_create_objects(), lpc_create_funcs(), ... they all are pretty much alike, and are asking for some kind of unification ... perhaps iterator for generic structure initialization?

Quoted 6 lines
Quoted 2 lines

The allocation and initialization code is very simple and straightforward. I really don't see a problem there.

This really boils down to the question I had in previous mail, whether three-level hierarchy (patch->object->funcs), which is why there is a lot of very alike initialization code, is not a bit over-designed.

It might right now, but we coded ourselves into a corner a couple of times in kpatch using optimal, but inflexible data structures and sharing those data structures with the API. This structure layout will give us flexibility to make changes without having to gut everything. I see flexibility and modularity being important going forward as we are both looking to extend the abilities.

Additionally it allows the sysfs directories to correlate to data structures and we can use the kobject ref count to cleanly do object cleanup (i.e. kobject_put() with release handlers for each ktype).

As Josh said, we do have operations that apply to each level. I think your point is that we could do away with the object level, but we have operations that happen on a per-object basis. lpc_enable_object() isn't just a for loop for registering the functions with ftrace. It also does the dynamic relocations. I'm sure we will find other things in the future. It is also nice to have a function that can be called from both lpc_enable_patch() and lp_module_notify() to enable the object in a common way.

Thanks,
Seth

Quoted 35 lines
Quoted 7 lines
Quoted 7 lines

I am not also really fully convinced that we need the patch->object->funcs abstraction hierarchy (which also contributes to the structure allocation being rather a spaghetti copy/paste code) ... wouldn't patch->funcs be suffcient, with the "object" being made just a property of the function, for example?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Plus, I show that kernel/kgraft.c + kernel/kgraft_files.c is 906+193=1099. I'd say they are about the same size :)

Which is still seem to me to be a ratio worth thinking about improving :)

Yes, this code doesn't have a consistency model, but it does have some other non-kGraft things like dynamic relocations,

BTW we need to put those into arch/x86/ as they are unfortunately not generic. But more on this later independently.

Quoted 1 line

deferred module patching,

FWIW kgraft supports that as well.

Quoted 1 line

and a unified API. There's really no point in comparing lines of code.

Oh, sure, I didn't mean that this is any kind of metrics that should be taken too seriously at all. I was just expressing my surprise that unification of the API would bring so much code that it makes the result comparably sized to "the whole thing" :)

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#14 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 3 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

Hi,

nice! So we have something to start with. Brilliant!

I have some comments below now. Yet, it obviously needs deeper review which will take more time.

include/linux/livepatch.h line 2

Quoted 5 lines
include/linux/livepatch.h new file +2 -0
@@ -0,0 +1,45 @@
+#ifndef _LIVEPATCH_H_
+#define _LIVEPATCH_H_

This should follow the linux kernel naming: LINUX_LIVEPATCH_H

include/linux/livepatch.h line 6

Quoted 3 lines
include/linux/livepatch.h new file +3 -0
+#include <linux/module.h>
+
+struct lp_func {

I am not much happy with "lp" which effectively means parallel printer support. What about lip?

Quoted 22 lines
include/linux/livepatch.h new file +21 -0
+	const char *old_name; /* function to be patched */
+	void *new_func; /* replacement function in patch module */
+	/*
+	 * The old_addr field is optional and can be used to resolve
+	 * duplicate symbol names in the vmlinux object.  If this
+	 * information is not present, the symbol is located by name
+	 * with kallsyms. If the name is not unique and old_addr is
+	 * not provided, the patch application fails as there is no
+	 * way to resolve the ambiguity.
+	 */
+	unsigned long old_addr;
+};
 
+struct lp_dynrela {
+	unsigned long dest;
+	unsigned long src;
+	unsigned long type;
+	const char *name;
+	int addend;
+	int external;
+};
+

include/linux/livepatch.h line 38

Quoted 10 lines
include/linux/livepatch.h new file +10 -0
+struct lp_object {
+	const char *name; /* "vmlinux" or module name */
+	struct lp_func *funcs;
+	struct lp_dynrela *dynrelas;
+};
+
+struct lp_patch {
+	struct module *mod; /* module containing the patch */
+	struct lp_object *objs;
+};

Please document all the structures and all its members. And use kernel-doc format for that. (You can take an inspiration in kgraft.)

Quoted 6 lines
include/linux/livepatch.h new file +6 -0
+int lp_register_patch(struct lp_patch *);
+int lp_unregister_patch(struct lp_patch *);
+int lp_enable_patch(struct lp_patch *);
+int lp_disable_patch(struct lp_patch *);
+
+#endif /* _LIVEPATCH_H_ */

...

Quoted 12 lines
kernel/livepatch/Makefile new file +3 -0
@@ -0,0 +1,3 @@
+obj-$(CONFIG_LIVE_PATCHING) += livepatch.o
+
+livepatch-objs := core.o
kernel/livepatch/core.c new file +0 -0
new file mode 100644
index 0000000..b32dbb5
@@ -0,0 +1,1020 @@

...

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+/*************************************
+ * Core structures
+ ************************************/
+
+/*

kernel/livepatch/core.c line 48

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+ * lp_ structs vs lpc_ structs
+ *
+ * For each element (patch, object, func) in the live-patching code,
+ * there are two types with two different prefixes: lp_ and lpc_.
+ *
+ * Structures used by the live-patch modules to register with this core module
+ * are prefixed with lp_ (live patching).  These structures are part of the
+ * registration API and are defined in livepatch.h.  The structures used
+ * internally by this core module are prefixed with lpc_ (live patching core).
+ */

I am not sure if the separation and the allocations/kobj handling are worth it. It makes the code really less understandable. Can we have just struct lip_function (don't unnecessarily abbreviate), lip_objectfile (object is too generic, like Java object) and lip_patch containing all the needed information? It would clean up the code a lot. (Yes, we would have profited from c++ here.)

Quoted 1 line

+static DEFINE_SEMAPHORE(lpc_mutex);

Ugh, those are deprecated. Use mutex. (Or am I missing the need of recursive locking?)

kernel/livepatch/core.c line 55

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+static LIST_HEAD(lpc_patches);
+
+enum lpc_state {
+	DISABLED,
+	ENABLED

These are too generic names. This is prone to conflicts in the tree.

kernel/livepatch/core.c line 64

Quoted 9 lines
kernel/livepatch/core.c new file +9 -0
+};
+
+struct lpc_func {
+	struct list_head list;
+	struct kobject kobj;
+	struct ftrace_ops fops;
+	enum lpc_state state;
+
+	const char *old_name;

So you do lpc_func->old_name = lp_func->old_name.

Why? Duplication is always bad and introduces errors. The same for the other members here and there. Well, lip_function would solve that.

Quoted 21 lines
kernel/livepatch/core.c new file +21 -0
+	unsigned long new_addr;
+	unsigned long old_addr;
+};
+
+struct lpc_object {
+	struct list_head list;
+	struct kobject kobj;
+	struct module *mod; /* module associated with object */
+	enum lpc_state state;
+
+	const char *name;
+	struct list_head funcs;
+	struct lp_dynrela *dynrelas;
+};
+
+struct lpc_patch {
+	struct list_head list;
+	struct kobject kobj;
+	struct lp_patch *userpatch; /* for correlation during unregister */
+	enum lpc_state state;
+

kernel/livepatch/core.c line 95

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+	struct module *mod;
+	struct list_head objs;
+};
+
+/*******************************************
+ * Helpers
+ *******************************************/
+
+/* sets obj->mod if object is not vmlinux and module was found */
+static bool is_object_loaded(struct lpc_object *obj)

Always prefix function names. We try to avoid kallsyms duplicates ;).

kernel/livepatch/core.c line 105

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+{
+	struct module *mod;
+
+	if (!strcmp(obj->name, "vmlinux"))
+		return 1;
+
+	mutex_lock(&module_mutex);
+	mod = find_module(obj->name);
+	mutex_unlock(&module_mutex);
+	obj->mod = mod;

This is racy. Mod can be already gone now, right?.

Quoted 20 lines
kernel/livepatch/core.c new file +20 -0
+
+	return !!mod;
+}
+
+/************************************
+ * kallsyms
+ ***********************************/
+
+struct lpc_find_arg {
+	const char *objname;
+	const char *name;
+	unsigned long addr;
+	/*
+	 * If count == 0, the symbol was not found. If count == 1, a unique
+	 * match was found and addr is set.  If count > 1, there is
+	 * unresolvable ambiguity among "count" number of symbols with the same
+	 * name in the same object.
+	 */
+	unsigned long count;
+};

...

kernel/livepatch/core.c line 153

Quoted 2 lines
kernel/livepatch/core.c new file +2 -0
+static int lpc_find_symbol(const char *objname, const char *name,
+			   unsigned long *addr)

The first two params can be const, right?

Quoted 26 lines
kernel/livepatch/core.c new file +26 -0
+{
+	struct lpc_find_arg args = {
+		.objname = objname,
+		.name = name,
+		.addr = 0,
+		.count = 0
+	};
+
+	if (objname && !strcmp(objname, "vmlinux"))
+		args.objname = NULL;
+
+	kallsyms_on_each_symbol(lpc_find_callback, &args);
+
+	if (args.count == 0)
+		pr_err("symbol '%s' not found in symbol table\n", name);
+	else if (args.count > 1)
+		pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
+		       args.count, name, objname);
+	else {
+		*addr = args.addr;
+		return 0;
+	}
+
+	*addr = 0;
+	return -EINVAL;
+}

...

Quoted 3 lines

+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/

I am skipping this now (see Jiri's e-mail).

Quoted 8 lines
kernel/livepatch/core.c new file +8 -0
+/***********************************
+ * ftrace registration
+ **********************************/
+
+static void lpc_ftrace_handler(unsigned long ip, unsigned long parent_ip,
+			       struct ftrace_ops *ops, struct pt_regs *regs)
+{
+	struct lpc_func *func = ops->private;

kernel/livepatch/core.c line 360

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+
+	regs->ip = func->new_addr;
+}
+
+static int lpc_enable_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(!func->old_addr);
+	BUG_ON(func->state != DISABLED);

No BUGs please, just return appropriately. Possibly with WARN_ON.

Quoted 12 lines
kernel/livepatch/core.c new file +12 -0
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
+	if (ret) {
+		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = register_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to register ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
+	} else

kernel/livepatch/core.c line 382

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+		func->state = ENABLED;
+
+	return ret;
+}
+
+static int lpc_unregister_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(func->state != ENABLED);

Detto.

Quoted 20 lines
kernel/livepatch/core.c new file +19 -0
+	if (!func->old_addr)
+		/* parent object is not loaded */
+		return 0;
+	ret = unregister_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
+	if (ret)
+		pr_warn("function unregister succeeded but failed to clear the filter\n");
+	func->state = DISABLED;
+
+	return 0;
+}
 
+/******************************
+ * enable/disable
+ ******************************/

...

kernel/livepatch/core.c

Quoted 2 lines
kernel/livepatch/core.c new file +2 -0
+/* must be called with lpc_mutex held */
+static int lpc_enable_patch(struct lpc_patch *patch)

The question I want to raise here is whether we need two-state
registration: register+enable. We don't in kGraft. Why do you?

kernel/livepatch/core.c line 512

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+{
+	struct lpc_object *obj;
+	int ret;
+
+	BUG_ON(patch->state != DISABLED);

No bugs...

Quoted 38 lines
kernel/livepatch/core.c new file +38 -0
+
+	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
+	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
+
+	pr_notice("enabling patch '%s'\n", patch->mod->name);
+
+	list_for_each_entry(obj, &patch->objs, list) {
+		if (!is_object_loaded(obj))
+			continue;
+		ret = lpc_enable_object(patch->mod, obj);
+		if (ret)
+			goto unregister;
+	}
+	patch->state = ENABLED;
+	return 0;
+
+unregister:
+	WARN_ON(lpc_disable_patch(patch));
+	return ret;
+}
+
+int lp_enable_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+	int ret;
+
+	down(&lpc_mutex);
+	patch = lpc_find_patch(userpatch);
+	if (!patch) {
+		ret = -ENODEV;
+		goto out;
+	}
+	ret = lpc_enable_patch(patch);
+out:
+	up(&lpc_mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_enable_patch);

...

kernel/livepatch/core.c line 940

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+/************************************
+ * register/unregister
+ ***********************************/
+
+int lp_register_patch(struct lp_patch *userpatch)

This and other guys forming the interface should be documented.

Quoted 15 lines
kernel/livepatch/core.c new file +15 -0
+{
+	int ret;
+
+	if (!userpatch || !userpatch->mod || !userpatch->objs)
+		return -EINVAL;
+
+	/*
+	 * A reference is taken on the patch module to prevent it from being
+	 * unloaded.  Right now, we don't allow patch modules to unload since
+	 * there is currently no method to determine if a thread is still
+	 * running in the patched code contained in the patch module once
+	 * the ftrace registration is successful.
+	 */
+	if (!try_module_get(userpatch->mod))
+		return -ENODEV;

kernel/livepatch/core.c line 965

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+
+	down(&lpc_mutex);
+	ret = lpc_create_patch(userpatch);
+	up(&lpc_mutex);
+	if (ret)
+		module_put(userpatch->mod);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_register_patch);

...

Thanks for the work!

Signature · 2 lines
js
suse labs
#15 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 12 lines · Jiri Slaby
Quoted 3 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

Hi,

nice! So we have something to start with. Brilliant!

I have some comments below now. Yet, it obviously needs deeper review which will take more time.

Quoted 7 lines

include/linux/livepatch.h line 2

Quoted 5 lines
include/linux/livepatch.h new file +2 -0
@@ -0,0 +1,45 @@
+#ifndef _LIVEPATCH_H_
+#define _LIVEPATCH_H_

This should follow the linux kernel naming: LINUX_LIVEPATCH_H

Didn't realize that was the convention. Just to be sure, you meant _LINUX_LIVEPATCH_H right (with the leading underscore)?

Quoted 8 lines

include/linux/livepatch.h line 6

Quoted 5 lines
include/linux/livepatch.h new file +3 -0
+#include <linux/module.h>
+
+struct lp_func {

I am not much happy with "lp" which effectively means parallel printer support. What about lip?

Not sure how much clearer lip is. It isn't for me :-/ I'm not opposed to changing it. I was just trying to keep the name short since it is used many times. Reducing the prefix from something like "livepatch_" to "lp_" seemed to be the shortest and most straightforward way.

Quoted 28 lines
Quoted 28 lines
include/linux/livepatch.h new file +26 -0
+	const char *old_name; /* function to be patched */
+	void *new_func; /* replacement function in patch module */
+	/*
+	 * The old_addr field is optional and can be used to resolve
+	 * duplicate symbol names in the vmlinux object.  If this
+	 * information is not present, the symbol is located by name
+	 * with kallsyms. If the name is not unique and old_addr is
+	 * not provided, the patch application fails as there is no
+	 * way to resolve the ambiguity.
+	 */
+	unsigned long old_addr;
+};
 
+struct lp_dynrela {
+	unsigned long dest;
+	unsigned long src;
+	unsigned long type;
+	const char *name;
+	int addend;
+	int external;
+};
+
+struct lp_object {
+	const char *name; /* "vmlinux" or module name */
+	struct lp_func *funcs;
+	struct lp_dynrela *dynrelas;
+};
Quoted 8 lines

include/linux/livepatch.h line 38

Quoted 5 lines
include/linux/livepatch.h new file +5 -0
+
+struct lp_patch {
+	struct module *mod; /* module containing the patch */
+	struct lp_object *objs;
+};

Please document all the structures and all its members. And use kernel-doc format for that. (You can take an inspiration in kgraft.)

Sure.

Quoted 39 lines
Quoted 7 lines
include/linux/livepatch.h new file +6 -0
+int lp_register_patch(struct lp_patch *);
+int lp_unregister_patch(struct lp_patch *);
+int lp_enable_patch(struct lp_patch *);
+int lp_disable_patch(struct lp_patch *);
+
+#endif /* _LIVEPATCH_H_ */

...

Quoted 12 lines
kernel/livepatch/Makefile new file +3 -0
@@ -0,0 +1,3 @@
+obj-$(CONFIG_LIVE_PATCHING) += livepatch.o
+
+livepatch-objs := core.o
kernel/livepatch/core.c new file +0 -0
new file mode 100644
index 0000000..b32dbb5
@@ -0,0 +1,1020 @@

...

Quoted 14 lines
kernel/livepatch/core.c new file +14 -0
+/*************************************
+ * Core structures
+ ************************************/
+
+/*
+ * lp_ structs vs lpc_ structs
+ *
+ * For each element (patch, object, func) in the live-patching code,
+ * there are two types with two different prefixes: lp_ and lpc_.
+ *
+ * Structures used by the live-patch modules to register with this core module
+ * are prefixed with lp_ (live patching).  These structures are part of the
+ * registration API and are defined in livepatch.h.  The structures used
+ * internally by this core module are prefixed with lpc_ (live patching core).
Quoted 8 lines
Quoted 1 line

+ */

I am not sure if the separation and the allocations/kobj handling are worth it. It makes the code really less understandable. Can we have just struct lip_function (don't unnecessarily abbreviate), lip_objectfile (object is too generic, like Java object) and lip_patch containing all the needed information? It would clean up the code a lot. (Yes, we would have profited from c++ here.)

I looked at doing this and this is actually what we did in kpatch. We made one structure that had "private" members that the user wasn't suppose to access that were only used in the core. This was messy though. Every time you wanted to add a "private" field to the struct so the core could do something new, you were changing the API to the patch modules as well. While copying the data into an internal structure does add code and opportunity for errors, that functionality is localized into functions that are specifically tasked with taking care of that. So the risk is minimized and we gain flexibility within the core and more self-documenting API structures.

Quoted 5 lines
Quoted 2 lines

+static DEFINE_SEMAPHORE(lpc_mutex);

Ugh, those are deprecated. Use mutex. (Or am I missing the need of recursive locking?)

Sure.

Quoted 8 lines

kernel/livepatch/core.c line 55

Quoted 6 lines
kernel/livepatch/core.c new file +5 -0
+static LIST_HEAD(lpc_patches);
+
+enum lpc_state {
+	DISABLED,
+	ENABLED

These are too generic names. This is prone to conflicts in the tree.

Add LPC_ prefix good enough?

Quoted 13 lines

kernel/livepatch/core.c line 64

Quoted 10 lines
kernel/livepatch/core.c new file +9 -0
+};
+
+struct lpc_func {
+	struct list_head list;
+	struct kobject kobj;
+	struct ftrace_ops fops;
+	enum lpc_state state;
+
+	const char *old_name;

So you do lpc_func->old_name = lp_func->old_name.

Quoted 2 lines

Why? Duplication is always bad and introduces errors. The same for the other members here and there. Well, lip_function would solve that.

See earlier comment.

Quoted 26 lines
Quoted 26 lines
kernel/livepatch/core.c new file +25 -0
+	unsigned long new_addr;
+	unsigned long old_addr;
+};
+
+struct lpc_object {
+	struct list_head list;
+	struct kobject kobj;
+	struct module *mod; /* module associated with object */
+	enum lpc_state state;
+
+	const char *name;
+	struct list_head funcs;
+	struct lp_dynrela *dynrelas;
+};
+
+struct lpc_patch {
+	struct list_head list;
+	struct kobject kobj;
+	struct lp_patch *userpatch; /* for correlation during unregister */
+	enum lpc_state state;
+
+	struct module *mod;
+	struct list_head objs;
+};
+
Quoted 8 lines

kernel/livepatch/core.c line 95

Quoted 6 lines
kernel/livepatch/core.c new file +6 -0
+/*******************************************
+ * Helpers
+ *******************************************/
+
+/* sets obj->mod if object is not vmlinux and module was found */
+static bool is_object_loaded(struct lpc_object *obj)

Always prefix function names. We try to avoid kallsyms duplicates ;).

Sure.

Quoted 13 lines

kernel/livepatch/core.c line 105

Quoted 11 lines
kernel/livepatch/core.c new file +10 -0
+{
+	struct module *mod;
+
+	if (!strcmp(obj->name, "vmlinux"))
+		return 1;
+
+	mutex_lock(&module_mutex);
+	mod = find_module(obj->name);
+	mutex_unlock(&module_mutex);
+	obj->mod = mod;

This is racy. Mod can be already gone now, right?.

Yes, we should take a ref on the module before releasing the module_mutex.

Quoted 24 lines
Quoted 21 lines
kernel/livepatch/core.c new file +20 -0
+
+	return !!mod;
+}
+
+/************************************
+ * kallsyms
+ ***********************************/
+
+struct lpc_find_arg {
+	const char *objname;
+	const char *name;
+	unsigned long addr;
+	/*
+	 * If count == 0, the symbol was not found. If count == 1, a unique
+	 * match was found and addr is set.  If count > 1, there is
+	 * unresolvable ambiguity among "count" number of symbols with the same
+	 * name in the same object.
+	 */
+	unsigned long count;
+};

...

Quoted 4 lines

kernel/livepatch/core.c line 153

Quoted 2 lines
kernel/livepatch/core.c new file +2 -0
+static int lpc_find_symbol(const char *objname, const char *name,
+			   unsigned long *addr)

The first two params can be const, right?

Not following. The first two params _are_ const. If you are saying they should be... I agree! :)

Quoted 48 lines
Quoted 27 lines
kernel/livepatch/core.c new file +26 -0
+{
+	struct lpc_find_arg args = {
+		.objname = objname,
+		.name = name,
+		.addr = 0,
+		.count = 0
+	};
+
+	if (objname && !strcmp(objname, "vmlinux"))
+		args.objname = NULL;
+
+	kallsyms_on_each_symbol(lpc_find_callback, &args);
+
+	if (args.count == 0)
+		pr_err("symbol '%s' not found in symbol table\n", name);
+	else if (args.count > 1)
+		pr_err("unresolvable ambiguity (%lu matches) on symbol '%s' in object '%s'\n",
+		       args.count, name, objname);
+	else {
+		*addr = args.addr;
+		return 0;
+	}
+
+	*addr = 0;
+	return -EINVAL;
+}

...

Quoted 3 lines

+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/

I am skipping this now (see Jiri's e-mail).

Quoted 12 lines
kernel/livepatch/core.c new file +12 -0
+/***********************************
+ * ftrace registration
+ **********************************/
+
+static void lpc_ftrace_handler(unsigned long ip, unsigned long parent_ip,
+			       struct ftrace_ops *ops, struct pt_regs *regs)
+{
+	struct lpc_func *func = ops->private;
+
+	regs->ip = func->new_addr;
+}
+
Quoted 8 lines

kernel/livepatch/core.c line 360

Quoted 6 lines
kernel/livepatch/core.c new file +6 -0
+static int lpc_enable_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(!func->old_addr);
+	BUG_ON(func->state != DISABLED);

No BUGs please, just return appropriately. Possibly with WARN_ON.

Sure.

Quoted 17 lines
Quoted 17 lines
kernel/livepatch/core.c new file +16 -0
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
+	if (ret) {
+		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = register_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to register ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
+	} else
+		func->state = ENABLED;
+
+	return ret;
+}
Quoted 8 lines

kernel/livepatch/core.c line 382

Quoted 6 lines
kernel/livepatch/core.c new file +6 -0
+
+static int lpc_unregister_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(func->state != ENABLED);

Detto.

Ok.

Quoted 22 lines
Quoted 11 lines
kernel/livepatch/core.c new file +10 -0
+	if (!func->old_addr)
+		/* parent object is not loaded */
+		return 0;
+	ret = unregister_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to unregister ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);

kernel/livepatch/core.c line 454

Quoted 10 lines
kernel/livepatch/core.c new file +9 -0
+	if (ret)
+		pr_warn("function unregister succeeded but failed to clear the filter\n");
+	func->state = DISABLED;
+
+	return 0;
+}
 
+/******************************
+ * enable/disable
+ ******************************/
Quoted 7 lines

...

kernel/livepatch/core.c

Quoted 2 lines
kernel/livepatch/core.c new file +2 -0
+/* must be called with lpc_mutex held */
+static int lpc_enable_patch(struct lpc_patch *patch)

The question I want to raise here is whether we need two-state
registration: register+enable. We don't in kGraft. Why do you?

We actually don't in kpatch either and this was a late change for this patchset. The thinking was that, while the patch modules would normally call lpc_register_patch() and lpc_enable_patch() in the same way all the time, breaking them up created more symmetric code and gives more flexibility to the API.

Josh might like to elaborate here.

Quoted 8 lines

kernel/livepatch/core.c line 512

Quoted 6 lines
kernel/livepatch/core.c new file +5 -0
+{
+	struct lpc_object *obj;
+	int ret;
+
+	BUG_ON(patch->state != DISABLED);

No bugs...

Ok.

Quoted 42 lines
Quoted 39 lines
kernel/livepatch/core.c new file +38 -0
+
+	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
+	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
+
+	pr_notice("enabling patch '%s'\n", patch->mod->name);
+
+	list_for_each_entry(obj, &patch->objs, list) {
+		if (!is_object_loaded(obj))
+			continue;
+		ret = lpc_enable_object(patch->mod, obj);
+		if (ret)
+			goto unregister;
+	}
+	patch->state = ENABLED;
+	return 0;
+
+unregister:
+	WARN_ON(lpc_disable_patch(patch));
+	return ret;
+}
+
+int lp_enable_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+	int ret;
+
+	down(&lpc_mutex);
+	patch = lpc_find_patch(userpatch);
+	if (!patch) {
+		ret = -ENODEV;
+		goto out;
+	}
+	ret = lpc_enable_patch(patch);
+out:
+	up(&lpc_mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_enable_patch);

...

Quoted 7 lines

kernel/livepatch/core.c line 940

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+/************************************
+ * register/unregister
+ ***********************************/
+
+int lp_register_patch(struct lp_patch *userpatch)

This and other guys forming the interface should be documented.

Yes.

Thanks,
Seth

Quoted 35 lines
Quoted 16 lines
kernel/livepatch/core.c new file +15 -0
+{
+	int ret;
+
+	if (!userpatch || !userpatch->mod || !userpatch->objs)
+		return -EINVAL;
+
+	/*
+	 * A reference is taken on the patch module to prevent it from being
+	 * unloaded.  Right now, we don't allow patch modules to unload since
+	 * there is currently no method to determine if a thread is still
+	 * running in the patched code contained in the patch module once
+	 * the ftrace registration is successful.
+	 */
+	if (!try_module_get(userpatch->mod))
+		return -ENODEV;

kernel/livepatch/core.c line 965

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+
+	down(&lpc_mutex);
+	ret = lpc_create_patch(userpatch);
+	up(&lpc_mutex);
+	if (ret)
+		module_put(userpatch->mod);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_register_patch);

...

Thanks for the work!

Signature · 2 lines
js
suse labs
#16 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 8 lines · Seth Jennings
Quoted 6 lines · Jiri Slaby

Review

Quoted 2 lines · Seth Jennings
(quoted diff) +2 -0
+/* must be called with lpc_mutex held */
+static int lpc_enable_patch(struct lpc_patch *patch)

The question I want to raise here is whether we need two-state
registration: register+enable. We don't in kGraft. Why do you?

Quoted 7 lines

We actually don't in kpatch either and this was a late change for this patchset. The thinking was that, while the patch modules would normally call lpc_register_patch() and lpc_enable_patch() in the same way all the time, breaking them up created more symmetric code and gives more flexibility to the API.

Josh might like to elaborate here.

Yes, this was my brilliant idea :-) I like it because it makes the register/unregister interfaces more symmetrical.

We already have to separate disable and unregister so that a patch can be disabled from sysfs, so it makes sense IMO to likewise separate register and enable.

The downside is an extra function call. The upside is it makes the code cleaner, and the API easier to understand and more flexible.

Signature · 1 line
Josh
#17 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 25 lines · Seth Jennings
Quoted 15 lines · Jiri Slaby
Quoted 14 lines · Seth Jennings
kernel/livepatch/core.c new file +14 -0
+/*************************************
+ * Core structures
+ ************************************/
+
+/*
+ * lp_ structs vs lpc_ structs
+ *
+ * For each element (patch, object, func) in the live-patching code,
+ * there are two types with two different prefixes: lp_ and lpc_.
+ *
+ * Structures used by the live-patch modules to register with this core module
+ * are prefixed with lp_ (live patching).  These structures are part of the
+ * registration API and are defined in livepatch.h.  The structures used
+ * internally by this core module are prefixed with lpc_ (live patching core).
Quoted 8 lines
Quoted 1 line

+ */

I am not sure if the separation and the allocations/kobj handling are worth it. It makes the code really less understandable. Can we have just struct lip_function (don't unnecessarily abbreviate), lip_objectfile (object is too generic, like Java object) and lip_patch containing all the needed information? It would clean up the code a lot. (Yes, we would have profited from c++ here.)

Quoted 10 lines

I looked at doing this and this is actually what we did in kpatch. We made one structure that had "private" members that the user wasn't suppose to access that were only used in the core. This was messy though. Every time you wanted to add a "private" field to the struct so the core could do something new, you were changing the API to the patch modules as well. While copying the data into an internal structure does add code and opportunity for errors, that functionality is localized into functions that are specifically tasked with taking care of that. So the risk is minimized and we gain flexibility within the core and more self-documenting API structures.

I am not sure if the modified API is really such a big limit. The modules initialize the needed members using ".member = value". Also we do not need to take care of API/ABI backward compatibility because there is very strict dependency between patches and the patched kernel.

Well, I have to think more about it.

Best Regards,
Petr

#18 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 37 lines · Petr Mladek
Quoted 25 lines · Seth Jennings
Quoted 15 lines · Jiri Slaby
Quoted 14 lines · Seth Jennings
kernel/livepatch/core.c new file +14 -0
+/*************************************
+ * Core structures
+ ************************************/
+
+/*
+ * lp_ structs vs lpc_ structs
+ *
+ * For each element (patch, object, func) in the live-patching code,
+ * there are two types with two different prefixes: lp_ and lpc_.
+ *
+ * Structures used by the live-patch modules to register with this core module
+ * are prefixed with lp_ (live patching).  These structures are part of the
+ * registration API and are defined in livepatch.h.  The structures used
+ * internally by this core module are prefixed with lpc_ (live patching core).
Quoted 8 lines
Quoted 1 line

+ */

I am not sure if the separation and the allocations/kobj handling are worth it. It makes the code really less understandable. Can we have just struct lip_function (don't unnecessarily abbreviate), lip_objectfile (object is too generic, like Java object) and lip_patch containing all the needed information? It would clean up the code a lot. (Yes, we would have profited from c++ here.)

Quoted 10 lines

I looked at doing this and this is actually what we did in kpatch. We made one structure that had "private" members that the user wasn't suppose to access that were only used in the core. This was messy though. Every time you wanted to add a "private" field to the struct so the core could do something new, you were changing the API to the patch modules as well. While copying the data into an internal structure does add code and opportunity for errors, that functionality is localized into functions that are specifically tasked with taking care of that. So the risk is minimized and we gain flexibility within the core and more self-documenting API structures.

Quoted 5 lines

I am not sure if the modified API is really such a big limit. The modules initialize the needed members using ".member = value". Also we do not need to take care of API/ABI backward compatibility because there is very strict dependency between patches and the patched kernel.

Our patch module generation tool (kpatch-build) relies on the API as well, so we should try to keep the API as stable as possible. At least until we can put kpatch-build (or something like it) into the kernel tree.

Signature · 1 line
Josh
#19 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Jiri Slaby
Quoted 21 lines · Jiri Slaby
Quoted 3 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

Hi,

nice! So we have something to start with. Brilliant!

I have some comments below now. Yet, it obviously needs deeper review which will take more time.

include/linux/livepatch.h line 2

Quoted 5 lines
include/linux/livepatch.h new file +2 -0
@@ -0,0 +1,45 @@
+#ifndef _LIVEPATCH_H_
+#define _LIVEPATCH_H_

This should follow the linux kernel naming: LINUX_LIVEPATCH_H

Quoted 6 lines

include/linux/livepatch.h line 6

Quoted 3 lines
include/linux/livepatch.h new file +3 -0
+#include <linux/module.h>
+
+struct lp_func {

I am not much happy with "lp" which effectively means parallel printer support. What about lip?

What about "patch_"?

It is not so big subsystem that additional typing would matter much...

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#20 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 22 lines · Pavel Machek
Quoted 21 lines · Jiri Slaby
Quoted 3 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

Hi,

nice! So we have something to start with. Brilliant!

I have some comments below now. Yet, it obviously needs deeper review which will take more time.

include/linux/livepatch.h line 2

Quoted 5 lines
include/linux/livepatch.h new file +2 -0
@@ -0,0 +1,45 @@
+#ifndef _LIVEPATCH_H_
+#define _LIVEPATCH_H_

This should follow the linux kernel naming: LINUX_LIVEPATCH_H

Quoted 8 lines
Quoted 6 lines

include/linux/livepatch.h line 6

Quoted 3 lines
include/linux/livepatch.h new file +3 -0
+#include <linux/module.h>
+
+struct lp_func {

I am not much happy with "lp" which effectively means parallel printer support. What about lip?

What about "patch_"?

Hey Pavel,

We are on v4 of this patchset:
https://lkml.org/lkml/2014/11/25/868

We ended up going with klp_ (kernel live patching).

Thanks,
Seth

Quoted 10 lines

It is not so big subsystem that additional typing would matter much...

Signature · 6 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
--
To unsubscribe from this list: send the line "unsubscribe live-patching" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#21 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings

kernel/livepatch/Kconfig line 4

Quoted 7 lines · On Thu, 6 Nov 2014 08:39:08 -0600 Seth Jennings <[email protected]> wrote:
kernel/livepatch/Kconfig new file +4 -0
@@ -0,0 +1,11 @@
+config LIVE_PATCHING
+	tristate "Live Kernel Patching"
+	depends on DYNAMIC_FTRACE_WITH_REGS && MODULES && SYSFS && KALLSYMS_ALL
+	default m

Nuke this default. This should be default 'n', which is what kconfig defaults to when none is mentioned.

-- Steve

Quoted 7 lines
kernel/livepatch/Kconfig new file +7 -0
+	help
+	  Say Y here if you want to support live kernel patching.
+	  This setting has no runtime impact until a live-patch
+	  kernel module that uses the live-patch interface provided
+	  by this option is loaded, resulting in calls to patched
+	  functions being redirected to the new function code contained
+	  in the live-patch module.
#22 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 13 lines · Steven Rostedt

kernel/livepatch/Kconfig line 4

Quoted 7 lines · On Thu, 6 Nov 2014 08:39:08 -0600 Seth Jennings <[email protected]> wrote:
kernel/livepatch/Kconfig new file +4 -0
@@ -0,0 +1,11 @@
+config LIVE_PATCHING
+	tristate "Live Kernel Patching"
+	depends on DYNAMIC_FTRACE_WITH_REGS && MODULES && SYSFS && KALLSYMS_ALL
+	default m

Nuke this default. This should be default 'n', which is what kconfig defaults to when none is mentioned.

Ok.

Thanks,
Seth

Quoted 10 lines

-- Steve

Quoted 7 lines
kernel/livepatch/Kconfig new file +7 -0
+	help
+	  Say Y here if you want to support live kernel patching.
+	  This setting has no runtime impact until a live-patch
+	  kernel module that uses the live-patch interface provided
+	  by this option is loaded, resulting in calls to patched
+	  functions being redirected to the new function code contained
+	  in the live-patch module.
#23 lore raw

module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+/******************************
+ * module notifier
+ *****************************/
+

kernel/livepatch/core.c line 565

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+			    void *data)
+{
+	struct module *mod = data;
+	struct lpc_patch *patch;
+	struct lpc_object *obj;
+	int ret = 0;
+
+	if (action != MODULE_STATE_COMING)
+		return 0;

IMHO, we should handle also MODULE_STATE_GOING. We should unregister the ftrace handlers and update the state of the affected objects (ENABLED -> DISABLED)

Quoted 11 lines
kernel/livepatch/core.c new file +11 -0
+	down(&lpc_mutex);
+
+	list_for_each_entry(patch, &lpc_patches, list) {
+		if (patch->state == DISABLED)
+			continue;
+		list_for_each_entry(obj, &patch->objs, list) {
+			if (strcmp(obj->name, mod->name))
+				continue;
+			pr_notice("load of module '%s' detected, applying patch '%s'\n",
+				  mod->name, patch->mod->name);
+			obj->mod = mod;

kernel/livepatch/core.c line 587

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+			ret = lpc_enable_object(patch->mod, obj);
+			if (ret)
+				goto out;
+			break;
+		}
+	}
+
+	up(&lpc_mutex);
+	return 0;
+out:

I would name this err_our or so to make it clear that it is used when something fails.

kernel/livepatch/core.c line 596

Quoted 9 lines
kernel/livepatch/core.c new file +9 -0
+	up(&lpc_mutex);
+	WARN("failed to apply patch '%s' to module '%s'\n",
+		patch->mod->name, mod->name);
+	return 0;
+}
+
+static struct notifier_block lp_module_nb = {
+	.notifier_call = lp_module_notify,
+	.priority = INT_MIN, /* called last */

The handler for MODULE_STATE_COMMING would need have higger priority, if we want to cleanly unregister the ftrace handlers.

Best Regards,
Petr

#24 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 27 lines · Petr Mladek
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+/******************************
+ * module notifier
+ *****************************/
+
+static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+			    void *data)
+{
+	struct module *mod = data;
+	struct lpc_patch *patch;
+	struct lpc_object *obj;
Quoted 8 lines

kernel/livepatch/core.c line 565

Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	int ret = 0;
+
+	if (action != MODULE_STATE_COMING)
+		return 0;

IMHO, we should handle also MODULE_STATE_GOING. We should unregister the ftrace handlers and update the state of the affected objects (ENABLED -> DISABLED)

The mechanism we use to avoid this right now is taking a reference on patched module. We only release that reference after the patch is disabled, which unregisters all the patched functions from ftrace.

However, your comment reminded me of an idea I had to use MODULE_STATE_GOING and let the lpc_mutex protect against races. I think it could be cleaner, but I haven't fleshed the idea out fully.

Quoted 17 lines
Quoted 17 lines
kernel/livepatch/core.c new file +16 -0
+	down(&lpc_mutex);
+
+	list_for_each_entry(patch, &lpc_patches, list) {
+		if (patch->state == DISABLED)
+			continue;
+		list_for_each_entry(obj, &patch->objs, list) {
+			if (strcmp(obj->name, mod->name))
+				continue;
+			pr_notice("load of module '%s' detected, applying patch '%s'\n",
+				  mod->name, patch->mod->name);
+			obj->mod = mod;
+			ret = lpc_enable_object(patch->mod, obj);
+			if (ret)
+				goto out;
+			break;
+		}
Quoted 8 lines

kernel/livepatch/core.c line 546

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+	}
+
+	up(&lpc_mutex);
+	return 0;
+out:

I would name this err_our or so to make it clear that it is used when something fails.

Just "err" good?

Quoted 13 lines

kernel/livepatch/core.c line 596

Quoted 10 lines
kernel/livepatch/core.c new file +9 -0
+	up(&lpc_mutex);
+	WARN("failed to apply patch '%s' to module '%s'\n",
+		patch->mod->name, mod->name);
+	return 0;
+}
+
+static struct notifier_block lp_module_nb = {
+	.notifier_call = lp_module_notify,
+	.priority = INT_MIN, /* called last */

The handler for MODULE_STATE_COMMING would need have higger priority, if we want to cleanly unregister the ftrace handlers.

Yes, we might need two handlers at different priorities if we decide to go that direction: one for MODULE_STATE_GOING at high/max and one for MODULE_STATE_COMING at low/min.

Thanks,
Seth

Quoted 3 lines

Best Regards,
Petr

#25 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 37 lines · Seth Jennings
Quoted 27 lines · Petr Mladek
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+/******************************
+ * module notifier
+ *****************************/
+
+static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+			    void *data)
+{
+	struct module *mod = data;
+	struct lpc_patch *patch;
+	struct lpc_object *obj;
Quoted 8 lines

kernel/livepatch/core.c line 565

Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	int ret = 0;
+
+	if (action != MODULE_STATE_COMING)
+		return 0;

IMHO, we should handle also MODULE_STATE_GOING. We should unregister the ftrace handlers and update the state of the affected objects (ENABLED -> DISABLED)

Quoted 3 lines

The mechanism we use to avoid this right now is taking a reference on patched module. We only release that reference after the patch is disabled, which unregisters all the patched functions from ftrace.

I see. This was actually another thing that I noticed and wanted to investigate :-) I think that we should not force users to disable the entire patch if they want to remove some module.

Quoted 3 lines

However, your comment reminded me of an idea I had to use MODULE_STATE_GOING and let the lpc_mutex protect against races. I think it could be cleaner, but I haven't fleshed the idea out fully.

AFAIK, the going module is not longer used when the notifier is called. Therefore we could remove the patch fast way even when patching would require the slow path otherwise.

Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
kernel/livepatch/core.c new file +16 -0
+	down(&lpc_mutex);
+
+	list_for_each_entry(patch, &lpc_patches, list) {
+		if (patch->state == DISABLED)
+			continue;
+		list_for_each_entry(obj, &patch->objs, list) {
+			if (strcmp(obj->name, mod->name))
+				continue;
+			pr_notice("load of module '%s' detected, applying patch '%s'\n",
+				  mod->name, patch->mod->name);
+			obj->mod = mod;
+			ret = lpc_enable_object(patch->mod, obj);
+			if (ret)
+				goto out;
+			break;
+		}
Quoted 8 lines

kernel/livepatch/core.c line 546

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+	}
+
+	up(&lpc_mutex);
+	return 0;
+out:

I would name this err_our or so to make it clear that it is used when something fails.

Quoted 1 line

Just "err" good?

Fine with me.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	up(&lpc_mutex);
+	WARN("failed to apply patch '%s' to module '%s'\n",
+		patch->mod->name, mod->name);
+	return 0;
Quoted 8 lines

kernel/livepatch/core.c line 596

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+}
+
+static struct notifier_block lp_module_nb = {
+	.notifier_call = lp_module_notify,
+	.priority = INT_MIN, /* called last */

The handler for MODULE_STATE_COMMING would need have higger priority, if we want to cleanly unregister the ftrace handlers.

Quoted 3 lines

Yes, we might need two handlers at different priorities if we decide to go that direction: one for MODULE_STATE_GOING at high/max and one for MODULE_STATE_COMING at low/min.

kGraft has notifier only for the going state. The initialization is called directly from load_module() after ftrace_module_init() and complete_formation() before it is executed by parse_args().

I need to investigate if the notifier is more elegant and safe or not.

Best Regards,
Petr

#26 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 38 lines · Petr Mladek
Quoted 37 lines · Seth Jennings
Quoted 35 lines · Petr Mladek
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+/******************************
+ * module notifier
+ *****************************/
+

kernel/livepatch/core.c line 565

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+			    void *data)
+{
+	struct module *mod = data;
+	struct lpc_patch *patch;
+	struct lpc_object *obj;
+	int ret = 0;
+
+	if (action != MODULE_STATE_COMING)
+		return 0;

IMHO, we should handle also MODULE_STATE_GOING. We should unregister the ftrace handlers and update the state of the affected objects (ENABLED -> DISABLED)

Quoted 7 lines
Quoted 3 lines

The mechanism we use to avoid this right now is taking a reference on patched module. We only release that reference after the patch is disabled, which unregisters all the patched functions from ftrace.

I see. This was actually another thing that I noticed and wanted to investigate :-) I think that we should not force users to disable the entire patch if they want to remove some module.

I agree that would be better.

Quoted 9 lines
Quoted 5 lines

However, your comment reminded me of an idea I had to use MODULE_STATE_GOING and let the lpc_mutex protect against races. I think it could be cleaner, but I haven't fleshed the idea out fully.

AFAIK, the going module is not longer used when the notifier is called. Therefore we could remove the patch fast way even when patching would require the slow path otherwise.

Yes (Josh just brought this to my attention) is that the notifiers are call with GOING _after_ the module's exit function is called.

Thanks,
Seth

Quoted 57 lines
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
kernel/livepatch/core.c new file +16 -0
+	down(&lpc_mutex);
+
+	list_for_each_entry(patch, &lpc_patches, list) {
+		if (patch->state == DISABLED)
+			continue;
+		list_for_each_entry(obj, &patch->objs, list) {
+			if (strcmp(obj->name, mod->name))
+				continue;
+			pr_notice("load of module '%s' detected, applying patch '%s'\n",
+				  mod->name, patch->mod->name);
+			obj->mod = mod;
+			ret = lpc_enable_object(patch->mod, obj);
+			if (ret)
+				goto out;
+			break;
+		}
Quoted 8 lines

kernel/livepatch/core.c line 546

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+	}
+
+	up(&lpc_mutex);
+	return 0;
+out:

I would name this err_our or so to make it clear that it is used when something fails.

Quoted 1 line

Just "err" good?

Fine with me.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	up(&lpc_mutex);
+	WARN("failed to apply patch '%s' to module '%s'\n",
+		patch->mod->name, mod->name);
+	return 0;
Quoted 8 lines

kernel/livepatch/core.c line 596

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+}
+
+static struct notifier_block lp_module_nb = {
+	.notifier_call = lp_module_notify,
+	.priority = INT_MIN, /* called last */

The handler for MODULE_STATE_COMMING would need have higger priority, if we want to cleanly unregister the ftrace handlers.

Quoted 3 lines

Yes, we might need two handlers at different priorities if we decide to go that direction: one for MODULE_STATE_GOING at high/max and one for MODULE_STATE_COMING at low/min.

kGraft has notifier only for the going state. The initialization is called directly from load_module() after ftrace_module_init() and complete_formation() before it is executed by parse_args().

I need to investigate if the notifier is more elegant and safe or not.

Best Regards,
Petr

#27 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

In reply to Petr Mladek
Quoted 3 lines · Petr Mladek
Quoted 2 lines · Seth Jennings
Quoted 1 line · Petr Mladek

On Thu 2014-11-06 08:39:08, Seth Jennings wrote:

[...]

Quoted 17 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	up(&lpc_mutex);
+	WARN("failed to apply patch '%s' to module '%s'\n",
+		patch->mod->name, mod->name);
+	return 0;
Quoted 8 lines

kernel/livepatch/core.c line 596

Quoted 5 lines
kernel/livepatch/core.c new file +5 -0
+}
+
+static struct notifier_block lp_module_nb = {
+	.notifier_call = lp_module_notify,
+	.priority = INT_MIN, /* called last */

The handler for MODULE_STATE_COMMING would need have higger priority, if we want to cleanly unregister the ftrace handlers.

Quoted 3 lines

Yes, we might need two handlers at different priorities if we decide to go that direction: one for MODULE_STATE_GOING at high/max and one for MODULE_STATE_COMING at low/min.

Quoted 5 lines

kGraft has notifier only for the going state. The initialization is called directly from load_module() after ftrace_module_init() and complete_formation() before it is executed by parse_args().

I need to investigate if the notifier is more elegant and safe or not.

I looked it up and having a COMING notifier with priority INT_MIN is effectively the same as having a call between complete_formation() and parse_args() since the notifiers are called as the last thing in complete_formation().

I think I've found a clean way to avoid the ref taking on the patched modules using only the notifier and lpc_mutex. It will be in v2 (hopefully out in the next couple of days).

Thanks,
Seth

Quoted 3 lines

Best Regards,
Petr

#28 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 1 line · Seth Jennings

It will be in v2 (hopefully out in the next couple of days).

FWIW we are also working on a few patches on top of v1 to back some of the proposals we've made during the first round of review, so maybe it might make sense to wait with v2 a little bit more, so that it incorporates as much v1 feedback as possible ... ?

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#29 lore raw

Re: module notifier: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 8 lines · Jiri Kosina
Quoted 1 line · Seth Jennings

It will be in v2 (hopefully out in the next couple of days).

FWIW we are also working on a few patches on top of v1 to back some of the proposals we've made during the first round of review, so maybe it might make sense to wait with v2 a little bit more, so that it incorporates as much v1 feedback as possible ... ?

What proposals in particular? I've already made many of the changes that we agreed upon.

Thanks,
Seth

Quoted 6 lines

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#30 lore raw

more patches for the same func: was Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 23 lines
kernel/livepatch/core.c new file +23 -0
+static int lpc_enable_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(!func->old_addr);
+	BUG_ON(func->state != DISABLED);
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
+	if (ret) {
+		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = register_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to register ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
+	} else
+		func->state = ENABLED;
+
+	return ret;
+}
+

[...]

Quoted 46 lines
kernel/livepatch/core.c new file +46 -0
+/* caller must ensure that obj->mod is set if object is a module */
+static int lpc_enable_object(struct module *pmod, struct lpc_object *obj)
+{
+	struct lpc_func *func;
+	int ret;
+
+	if (obj->mod && !try_module_get(obj->mod))
+		return -ENODEV;
+
+	if (obj->dynrelas) {
+		ret = lpc_write_object_relocations(pmod, obj);
+		if (ret)
+			goto unregister;
+	}
+	list_for_each_entry(func, &obj->funcs, list) {
+		ret = lpc_find_verify_func_addr(func, obj->name);
+		if (ret)
+			goto unregister;
+
+		ret = lpc_enable_func(func);
+		if (ret)
+			goto unregister;
+	}
+	obj->state = ENABLED;
+
+	return 0;
+unregister:
+	WARN_ON(lpc_unregister_object(obj));
+	return ret;
+}
+
+/******************************
+ * enable/disable
+ ******************************/
+
+/* must be called with lpc_mutex held */
+static struct lpc_patch *lpc_find_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+
+	list_for_each_entry(patch, &lpc_patches, list)
+		if (patch->userpatch == userpatch)
+			return patch;
+
+	return NULL;
+}

[...]

Quoted 36 lines
kernel/livepatch/core.c new file +36 -0
+
+/* must be called with lpc_mutex held */
+static int lpc_enable_patch(struct lpc_patch *patch)
+{
+	struct lpc_object *obj;
+	int ret;
+
+	BUG_ON(patch->state != DISABLED);
+
+	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
+	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
+
+	pr_notice("enabling patch '%s'\n", patch->mod->name);
+
+	list_for_each_entry(obj, &patch->objs, list) {
+		if (!is_object_loaded(obj))
+			continue;
+		ret = lpc_enable_object(patch->mod, obj);
+		if (ret)
+			goto unregister;
+	}
+	patch->state = ENABLED;
+	return 0;
+
+unregister:
+	WARN_ON(lpc_disable_patch(patch));
+	return ret;
+}
+
+int lp_enable_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+	int ret;
+
+	down(&lpc_mutex);
+	patch = lpc_find_patch(userpatch);

kernel/livepatch/core.c line 550

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+	if (!patch) {
+		ret = -ENODEV;
+		goto out;
+	}
+	ret = lpc_enable_patch(patch);
+out:
+	up(&lpc_mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_enable_patch);

AFAIK, this does not handle correctly the situation when there are more patches for the same symbol. IMHO, the first registered ftrace function wins. It means that later patches are ignored.

In kGraft, we detect this situation and do the following:

add_new_ftrace_function()
/* old one still might be used at this stage */ if (old_function)

      remove_old_ftrace_function();

/* the new one is used from now on */

Similar problem is when a patch is disabled. We need to know if it was actually used. If not, we are done. If it is active, we need to look if there is an older patch for the the same symbol and enable the other ftrace function instead.

Best Regards,
Petr

PS: We should probably decide on the used structures before we start coding fixes for this particular problems. I have similar concern about the complexity as my colleagues have. But I need to think more about it. Let's discuss it in the other thread.

#31 lore raw

Re: more patches for the same func: was Re: [PATCH 2/2] kernel: add support for live patching

Quoted 134 lines · Petr Mladek
Quoted 13 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 23 lines
kernel/livepatch/core.c new file +23 -0
+static int lpc_enable_func(struct lpc_func *func)
+{
+	int ret;
+
+	BUG_ON(!func->old_addr);
+	BUG_ON(func->state != DISABLED);
+	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
+	if (ret) {
+		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
+		       func->old_name, ret);
+		return ret;
+	}
+	ret = register_ftrace_function(&func->fops);
+	if (ret) {
+		pr_err("failed to register ftrace handler for function '%s' (%d)\n",
+		       func->old_name, ret);
+		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
+	} else
+		func->state = ENABLED;
+
+	return ret;
+}
+

[...]

Quoted 46 lines
kernel/livepatch/core.c new file +46 -0
+/* caller must ensure that obj->mod is set if object is a module */
+static int lpc_enable_object(struct module *pmod, struct lpc_object *obj)
+{
+	struct lpc_func *func;
+	int ret;
+
+	if (obj->mod && !try_module_get(obj->mod))
+		return -ENODEV;
+
+	if (obj->dynrelas) {
+		ret = lpc_write_object_relocations(pmod, obj);
+		if (ret)
+			goto unregister;
+	}
+	list_for_each_entry(func, &obj->funcs, list) {
+		ret = lpc_find_verify_func_addr(func, obj->name);
+		if (ret)
+			goto unregister;
+
+		ret = lpc_enable_func(func);
+		if (ret)
+			goto unregister;
+	}
+	obj->state = ENABLED;
+
+	return 0;
+unregister:
+	WARN_ON(lpc_unregister_object(obj));
+	return ret;
+}
+
+/******************************
+ * enable/disable
+ ******************************/
+
+/* must be called with lpc_mutex held */
+static struct lpc_patch *lpc_find_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+
+	list_for_each_entry(patch, &lpc_patches, list)
+		if (patch->userpatch == userpatch)
+			return patch;
+
+	return NULL;
+}

[...]

Quoted 42 lines
kernel/livepatch/core.c new file +42 -0
+
+/* must be called with lpc_mutex held */
+static int lpc_enable_patch(struct lpc_patch *patch)
+{
+	struct lpc_object *obj;
+	int ret;
+
+	BUG_ON(patch->state != DISABLED);
+
+	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
+	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
+
+	pr_notice("enabling patch '%s'\n", patch->mod->name);
+
+	list_for_each_entry(obj, &patch->objs, list) {
+		if (!is_object_loaded(obj))
+			continue;
+		ret = lpc_enable_object(patch->mod, obj);
+		if (ret)
+			goto unregister;
+	}
+	patch->state = ENABLED;
+	return 0;
+
+unregister:
+	WARN_ON(lpc_disable_patch(patch));
+	return ret;
+}
+
+int lp_enable_patch(struct lp_patch *userpatch)
+{
+	struct lpc_patch *patch;
+	int ret;
+
+	down(&lpc_mutex);
+	patch = lpc_find_patch(userpatch);
+	if (!patch) {
+		ret = -ENODEV;
+		goto out;
+	}
+	ret = lpc_enable_patch(patch);
+out:
Quoted 8 lines

kernel/livepatch/core.c line 550

Quoted 4 lines
kernel/livepatch/core.c new file +4 -0
+	up(&lpc_mutex);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(lp_enable_patch);

AFAIK, this does not handle correctly the situation when there are more patches for the same symbol. IMHO, the first registered ftrace function wins. It means that later patches are ignored.

Yeah, good point. With kpatch we had a single ftrace_ops which was shared for all patched functions, so we didn't have this problem. From the ftrace handler, we would just get the most recent version of the function from our hash table.

With livepatch we decided to change to the model of one ftrace ops per patched function. Which I think is probably a good idea, but it's hard to say for sure without knowing our consistency model yet.

Quoted 9 lines

In kGraft, we detect this situation and do the following:

   add_new_ftrace_function()
   /* old one still might be used at this stage */
   if (old_function)
      remove_old_ftrace_function();
   /* the new one is used from now on */
Quoted 4 lines

Similar problem is when a patch is disabled. We need to know if it was actually used. If not, we are done. If it is active, we need to look if there is an older patch for the the same symbol and enable the other ftrace function instead.

Yeah, maybe we should do something similar for livepatch.

Quoted 9 lines

Best Regards,
Petr

PS: We should probably decide on the used structures before we start coding fixes for this particular problems. I have similar concern about the complexity as my colleagues have. But I need to think more about it. Let's discuss it in the other thread.

Sounds good :-)

Signature · 1 line
Josh
#32 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 14 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

kernel/livepatch/core.c line 610

Quoted 12 lines
kernel/livepatch/core.c new file +12 -0
+/********************************************
+ * Sysfs Interface
+ *******************************************/
+/*
+ * /sys/kernel/livepatch
+ * /sys/kernel/livepatch/<patch>
+ * /sys/kernel/livepatch/<patch>/enabled
+ * /sys/kernel/livepatch/<patch>/<object>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr
+ */

Letting anyone read new_addr and old_addr is a kASLR leak, and I would argue that showing this information to non-root at all is probably a bad idea.

Can you make new_addr and old_addr have mode 0600 and /sys/kernel/livepatch itself have mode 0500? For the latter, an admin who wants unprivileged users to be able to see it can easily chmod it.

--Andy

#33 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 35 lines · Andy Lutomirski
Quoted 14 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

kernel/livepatch/core.c line 610

Quoted 12 lines
kernel/livepatch/core.c new file +12 -0
+/********************************************
+ * Sysfs Interface
+ *******************************************/
+/*
+ * /sys/kernel/livepatch
+ * /sys/kernel/livepatch/<patch>
+ * /sys/kernel/livepatch/<patch>/enabled
+ * /sys/kernel/livepatch/<patch>/<object>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr
+ */

Letting anyone read new_addr and old_addr is a kASLR leak, and I would argue that showing this information to non-root at all is probably a bad idea.

Quoted 3 lines

Can you make new_addr and old_addr have mode 0600 and /sys/kernel/livepatch itself have mode 0500? For the latter, an admin who wants unprivileged users to be able to see it can easily chmod it.

Good call. Will do.

Thanks,
Seth

Quoted 6 lines

--Andy
--
To unsubscribe from this list: send the line "unsubscribe live-patching" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#34 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Andy Lutomirski
Quoted 18 lines · Andy Lutomirski
Quoted 14 lines · Seth Jennings

This commit introduces code for the live patching core. It implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions.

It represents the greatest common functionality set between kpatch and kgraft and can accept patches built using either method.

This first version does not implement any consistency mechanism that ensures that old and new code do not run together. In practice, ~90% of CVEs are safe to apply in this way, since they simply add a conditional check. However, any function change that can not execute safely with the old version of the function can _not_ be safely applied in this version.

[...]

Quoted 16 lines

kernel/livepatch/core.c line 610

Quoted 12 lines
kernel/livepatch/core.c new file +12 -0
+/********************************************
+ * Sysfs Interface
+ *******************************************/
+/*
+ * /sys/kernel/livepatch
+ * /sys/kernel/livepatch/<patch>
+ * /sys/kernel/livepatch/<patch>/enabled
+ * /sys/kernel/livepatch/<patch>/<object>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr
+ * /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr
+ */

Letting anyone read new_addr and old_addr is a kASLR leak, and I would argue that showing this information to non-root at all is probably a bad idea.

Also worth noting that this live patching implementation currently doesn't support kASLR, as there is a method for the patch module to supply the old_addr, determined at generation time by pulling from vmlinux/System.map/etc, for a particular function to resolve symbol ambiguity in a kallsyms lookup. Obviously, this old_addr would be wrong for a kernel using kASLR.

Thanks,
Seth

Quoted 10 lines

Can you make new_addr and old_addr have mode 0600 and /sys/kernel/livepatch itself have mode 0500? For the latter, an admin who wants unprivileged users to be able to see it can easily chmod it.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe live-patching" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#35 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings
Quoted 5 lines · Seth Jennings
kernel/livepatch/core.c new file +0 -0
new file mode 100644
index 0000000..b32dbb5

[ ... snip ... ]

Quoted 65 lines
kernel/livepatch/core.c new file +65 -0
+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/
+
+/*
+ * external symbols are located outside the parent object (where the parent
+ * object is either vmlinux or the kmod being patched).
+ */
+static int lpc_find_external_symbol(struct module *pmod, const char *name,
+					unsigned long *addr)
+{
+	const struct kernel_symbol *sym;
+
+	/* first, check if it's an exported symbol */
+	preempt_disable();
+	sym = find_symbol(name, NULL, NULL, true, true);
+	preempt_enable();
+	if (sym) {
+		*addr = sym->value;
+		return 0;
+	}
+
+	/* otherwise check if it's in another .o within the patch module */
+	return lpc_find_symbol(pmod->name, name, addr);
+}
+
+static int lpc_write_object_relocations(struct module *pmod,
+					struct lpc_object *obj)
+{
+	int ret, size, readonly = 0, numpages;
+	struct lp_dynrela *dynrela;
+	u64 loc, val;
+	unsigned long core = (unsigned long)pmod->module_core;
+	unsigned long core_ro_size = pmod->core_ro_size;
+	unsigned long core_size = pmod->core_size;
+
+	for (dynrela = obj->dynrelas; dynrela->name; dynrela++) {
+		if (!strcmp(obj->name, "vmlinux")) {
+			ret = lpc_verify_vmlinux_symbol(dynrela->name,
+							dynrela->src);
+			if (ret)
+				return ret;
+		} else {
+			/* module, dynrela->src needs to be discovered */
+			if (dynrela->external)
+				ret = lpc_find_external_symbol(pmod,
+							       dynrela->name,
+							       &dynrela->src);
+			else
+				ret = lpc_find_symbol(obj->mod->name,
+						      dynrela->name,
+						      &dynrela->src);
+			if (ret)
+				return -EINVAL;
+		}
+
+		switch (dynrela->type) {
+		case R_X86_64_NONE:
+			continue;
+		case R_X86_64_PC32:
+			loc = dynrela->dest;
+			val = (u32)(dynrela->src + dynrela->addend -
+				    dynrela->dest);
+			size = 4;
+			break;

kernel/livepatch/core.c line 305

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+		case R_X86_64_32S:
+			loc = dynrela->dest;
+			val = (s32)dynrela->src + dynrela->addend;
+			size = 4;
+			break;
+		case R_X86_64_64:
+			loc = dynrela->dest;
+			val = dynrela->src;
+			size = 8;
+			break;

This is x86-specific, so it definitely needs to go to arch/x86. The only hard precondition for arch to support live patching is ftrace with regs saving (we are currently in parallel working on extending the set of architectures that support this), so we shouldn't introduce any x86-isms into the generic code.

It seems to me that what basically needs to be done is to teach apply_relocate_add() about this kind of relocations and apply them as needed.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#36 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 92 lines · Jiri Kosina
Quoted 5 lines · Seth Jennings
kernel/livepatch/core.c new file +0 -0
new file mode 100644
index 0000000..b32dbb5

[ ... snip ... ]

Quoted 65 lines
kernel/livepatch/core.c new file +65 -0
+/****************************************
+ * dynamic relocations (load-time linker)
+ ****************************************/
+
+/*
+ * external symbols are located outside the parent object (where the parent
+ * object is either vmlinux or the kmod being patched).
+ */
+static int lpc_find_external_symbol(struct module *pmod, const char *name,
+					unsigned long *addr)
+{
+	const struct kernel_symbol *sym;
+
+	/* first, check if it's an exported symbol */
+	preempt_disable();
+	sym = find_symbol(name, NULL, NULL, true, true);
+	preempt_enable();
+	if (sym) {
+		*addr = sym->value;
+		return 0;
+	}
+
+	/* otherwise check if it's in another .o within the patch module */
+	return lpc_find_symbol(pmod->name, name, addr);
+}
+
+static int lpc_write_object_relocations(struct module *pmod,
+					struct lpc_object *obj)
+{
+	int ret, size, readonly = 0, numpages;
+	struct lp_dynrela *dynrela;
+	u64 loc, val;
+	unsigned long core = (unsigned long)pmod->module_core;
+	unsigned long core_ro_size = pmod->core_ro_size;
+	unsigned long core_size = pmod->core_size;
+
+	for (dynrela = obj->dynrelas; dynrela->name; dynrela++) {
+		if (!strcmp(obj->name, "vmlinux")) {
+			ret = lpc_verify_vmlinux_symbol(dynrela->name,
+							dynrela->src);
+			if (ret)
+				return ret;
+		} else {
+			/* module, dynrela->src needs to be discovered */
+			if (dynrela->external)
+				ret = lpc_find_external_symbol(pmod,
+							       dynrela->name,
+							       &dynrela->src);
+			else
+				ret = lpc_find_symbol(obj->mod->name,
+						      dynrela->name,
+						      &dynrela->src);
+			if (ret)
+				return -EINVAL;
+		}
+
+		switch (dynrela->type) {
+		case R_X86_64_NONE:
+			continue;
+		case R_X86_64_PC32:
+			loc = dynrela->dest;
+			val = (u32)(dynrela->src + dynrela->addend -
+				    dynrela->dest);
+			size = 4;
+			break;

kernel/livepatch/core.c line 305

Quoted 10 lines
kernel/livepatch/core.c new file +10 -0
+		case R_X86_64_32S:
+			loc = dynrela->dest;
+			val = (s32)dynrela->src + dynrela->addend;
+			size = 4;
+			break;
+		case R_X86_64_64:
+			loc = dynrela->dest;
+			val = dynrela->src;
+			size = 8;
+			break;

This is x86-specific, so it definitely needs to go to arch/x86. The only hard precondition for arch to support live patching is ftrace with regs saving (we are currently in parallel working on extending the set of architectures that support this), so we shouldn't introduce any x86-isms into the generic code.

Quoted 3 lines

It seems to me that what basically needs to be done is to teach apply_relocate_add() about this kind of relocations and apply them as needed.

Agreed.

Signature · 1 line
Josh
maintainer
#37 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Seth Jennings

Hi,

thank you for the first version of the united live patching core.

The patch below implements some of our review objections. Changes are described in the commit log. It simplifies the hierarchy of data structures, removes data duplication (lp_ and lpc_ structures) and simplifies sysfs directory.

I did not try to repair other stuff (races, function names, function prefix, api symmetry etc.). It should serve as a demonstration of our point of view.

There are some problems with this. try_module_get and module_put may be called several times for each kernel module where some function is patched in. This should be fixed with module going notifier as suggested by Petr.

The modified core was tested with modified testing live patch originally from Seth's github. It worked as expected.

Please take a look at these changes, so we can discuss them in more detail.

Best regards,
--
Miroslav Benes
SUSE Labs

----

Quoted 1 line

From f659a18a630de27b47d375119d793e28ee50da04 Mon Sep 17 00:00:00 2001

From: Miroslav Benes <[email protected]>
Date: Thu, 13 Nov 2014 10:25:48 +0100
Subject: [PATCH] lpc: simplification of structure and sysfs hierarchy

Original code has several issues this patch tries to remove.

First, there is only lpc_func structure for patched function and lpc_patch for the patch as a whole. Therefore lpc_object structure as middle step of hierarchy is removed. Patched function is still associated with some object (vmlinux or module) through obj_name. Dynrelas are now in lpc_patch structure and object identifier (obj_name) is in the lpc_dynrela to preserve the connection.

Second, sysfs structure is simplified. We do not need to propagate old_addr and new_addr. So, there is subdirectory for each patch (patching module) which includes original enabled attribute and new one funcs attribute which lists the patched functions.

Third, data duplication (lp_ and lpc_ structures) is removed. lpc_ structures are now in the header file and made available for the user. This allows us to remove almost all the functions for structure allocation in the original code.

---
 include/linux/livepatch.h |  46 ++--
 kernel/livepatch/core.c   | 575 +++++++++++++---------------------------------
 2 files changed, 191 insertions(+), 430 deletions(-)
include/linux/livepatch.h +32 -14
index c7a415b..db5ba00 100644
@@ -2,10 +2,23 @@
 #define _LIVEPATCH_H_
 
 #include <linux/module.h>
+#include <linux/ftrace.h>
 
-struct lp_func {
+enum lpc_state {
+	LPC_DISABLED,
+	LPC_ENABLED
+};
+
+struct lpc_func {
+	/* internal */
+	struct ftrace_ops fops;
+	enum lpc_state state;
+	struct module *mod; /* module associated with patched function */
+	unsigned long new_addr; /* replacement function in patch module */
+
+	/* external */
 	const char *old_name; /* function to be patched */
-	void *new_func; /* replacement function in patch module */
+	void *new_func;
 	/*
 	 * The old_addr field is optional and can be used to resolve
 	 * duplicate symbol names in the vmlinux object.  If this
@@ -15,31 +28,36 @@ struct lp_func {
 	 * way to resolve the ambiguity.
 	 */
 	unsigned long old_addr;
+
+	const char *obj_name; /* "vmlinux" or module name */
 };
 
-struct lp_dynrela {
+struct lpc_dynrela {
 	unsigned long dest;
 	unsigned long src;
 	unsigned long type;
 	const char *name;
+	const char *obj_name;
 	int addend;
 	int external;
 };
 
-struct lp_object {
-	const char *name; /* "vmlinux" or module name */
-	struct lp_func *funcs;
-	struct lp_dynrela *dynrelas;
-};
+struct lpc_patch {
+	/* internal */
+	struct list_head list;
+	struct kobject kobj;
+	enum lpc_state state;
 
-struct lp_patch {
+	/* external */
 	struct module *mod; /* module containing the patch */
-	struct lp_object *objs;
+	struct lpc_dynrela *dynrelas;
+	struct lpc_func funcs[];
 };
 
-int lp_register_patch(struct lp_patch *);
-int lp_unregister_patch(struct lp_patch *);
-int lp_enable_patch(struct lp_patch *);
-int lp_disable_patch(struct lp_patch *);
+
+extern int lpc_register_patch(struct lpc_patch *);
+extern int lpc_unregister_patch(struct lpc_patch *);
+extern int lpc_enable_patch(struct lpc_patch *);
+extern int lpc_disable_patch(struct lpc_patch *);
 
 #endif /* _LIVEPATCH_H_ */
kernel/livepatch/core.c +159 -416

844 lines not on this page · raw message

Signature · 1 line
2.1.2
#38 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 19 lines · Miroslav Benes

Hi,

thank you for the first version of the united live patching core.

The patch below implements some of our review objections. Changes are described in the commit log. It simplifies the hierarchy of data structures, removes data duplication (lp_ and lpc_ structures) and simplifies sysfs directory.

I did not try to repair other stuff (races, function names, function prefix, api symmetry etc.). It should serve as a demonstration of our point of view.

There are some problems with this. try_module_get and module_put may be called several times for each kernel module where some function is patched in. This should be fixed with module going notifier as suggested by Petr.

Quoted 5 lines

The modified core was tested with modified testing live patch originally from Seth's github. It worked as expected.

Please take a look at these changes, so we can discuss them in more detail.

Hi Miroslav,

Thanks for the code suggestions.

This is a single patch with three major changes, which makes it hard to discuss each individual change on its own merits.

Also, Seth has already made a lot of changes already based on previous comments, and is very close to having a v2 patch. Because this patch is so big, there are a lot of conflicts.

Can you wait for the v2 patch set and then post your own patch set against it, with the patches split out so we can discuss them individually?

Signature · 1 line
Josh
#39 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

In reply to Miroslav Benes
Quoted 19 lines · Miroslav Benes

Hi,

thank you for the first version of the united live patching core.

The patch below implements some of our review objections. Changes are described in the commit log. It simplifies the hierarchy of data structures, removes data duplication (lp_ and lpc_ structures) and simplifies sysfs directory.

I did not try to repair other stuff (races, function names, function prefix, api symmetry etc.). It should serve as a demonstration of our point of view.

There are some problems with this. try_module_get and module_put may be called several times for each kernel module where some function is patched in. This should be fixed with module going notifier as suggested by Petr.

Quoted 5 lines

The modified core was tested with modified testing live patch originally from Seth's github. It worked as expected.

Please take a look at these changes, so we can discuss them in more detail.

Thanks Miroslav.

The functional changes are a little hard to break out from the formatting changes like s/disable/unregister and s/lp_/lpc_/ or adding LPC_ prefix to the enum, most (all?) of which I have included for v2.

A problem with getting rid of the object layer is that there are operations we do that are object-level operations. For example, module lookup and deferred module patching. Also, the dynamic relocations need to be associated with an object, not a patch, as not all relocations will be able to be applied at patch load time for patches that apply to modules that aren't loaded. I understand that you can walk the patch-level dynrela table and skip dynrela entries that don't match the target object, but why do that when you can cleanly express the relationship with a data structure hierarchy?

One example is the call to is_object_loaded() (renamed and reworked in v2 btw) per function rather than per object. That is duplicate work and information that could be more cleanly expressed through an object layer.

I also understand that sysfs/kobject stuff adds code length. However, the new "funcs" attribute is procfs style, not sysfs style. sysfs attribute should convey _one_ value.

Quoted 1 line

From Documenation/filesystems/sysfs.txt:

==========
Attributes should be ASCII text files, preferably with only one value per file. It is noted that it may not be efficient to contain only one value per file, so it is socially acceptable to express an array of values of the same type.

Mixing types, expressing multiple lines of data, and doing fancy formatting of data is heavily frowned upon. Doing these things may get you publicly humiliated and your code rewritten without notice.
==========

Also the function list would have object ambiguity. If there was a patched function my_func() in both vmlinux and a module, it would just appear on the list twice. You can fix this by using the mod:func syntax like kallsyms, but it isn't as clean as expressing it in a hierarchy.

As far as the unification of the API structures with the internal structures I have two points. First is that, IMHO, we should assume that the structures coming from the user are const. In kpatch, for example, we pass through some structures that are not created in the code, but by the patch generation tool and stored in an ELF section (read-only). Additionally, I am really against exposing the internal fields. Commenting them as "internal" is just messy and we have to change the .h file every time when want to add a field for internal use.

It seems that the primary purpose of this patch is to reduce the lines of code. However, I think that the object layer of the data structure cleanly expresses the object<->function relationship and makes code like the deferred patching much more straightforward since you already have the functions/dynrelas organized by object. You don't have to do the nasty "if (strcmp(func->obj_name, objname)) continue;" business over the entire patch every time.

Be advised, I have also done away with the new_addr/old_addr attributes for v2 and replaced the patched module ref'ing with a combination of a GOING notifier with lpc_mutex for protection.

Thanks,
Seth

Quoted 981 lines

Best regards,
--
Miroslav Benes
SUSE Labs

----
From f659a18a630de27b47d375119d793e28ee50da04 Mon Sep 17 00:00:00 2001
From: Miroslav Benes <[email protected]>
Date: Thu, 13 Nov 2014 10:25:48 +0100
Subject: [PATCH] lpc: simplification of structure and sysfs hierarchy

Original code has several issues this patch tries to remove.

First, there is only lpc_func structure for patched function and lpc_patch for the patch as a whole. Therefore lpc_object structure as middle step of hierarchy is removed. Patched function is still associated with some object (vmlinux or module) through obj_name. Dynrelas are now in lpc_patch structure and object identifier (obj_name) is in the lpc_dynrela to preserve the connection.

Second, sysfs structure is simplified. We do not need to propagate old_addr and new_addr. So, there is subdirectory for each patch (patching module) which includes original enabled attribute and new one funcs attribute which lists the patched functions.

Third, data duplication (lp_ and lpc_ structures) is removed. lpc_ structures are now in the header file and made available for the user. This allows us to remove almost all the functions for structure allocation in the original code.

---
 include/linux/livepatch.h |  46 ++--
 kernel/livepatch/core.c   | 575 +++++++++++++---------------------------------
 2 files changed, 191 insertions(+), 430 deletions(-)
include/linux/livepatch.h +32 -14
index c7a415b..db5ba00 100644
@@ -2,10 +2,23 @@
 #define _LIVEPATCH_H_
 
 #include <linux/module.h>
+#include <linux/ftrace.h>
 
-struct lp_func {
+enum lpc_state {
+	LPC_DISABLED,
+	LPC_ENABLED
+};
+
+struct lpc_func {
+	/* internal */
+	struct ftrace_ops fops;
+	enum lpc_state state;
+	struct module *mod; /* module associated with patched function */
+	unsigned long new_addr; /* replacement function in patch module */
+
+	/* external */
 	const char *old_name; /* function to be patched */
-	void *new_func; /* replacement function in patch module */
+	void *new_func;
 	/*
 	 * The old_addr field is optional and can be used to resolve
 	 * duplicate symbol names in the vmlinux object.  If this
@@ -15,31 +28,36 @@ struct lp_func {
 	 * way to resolve the ambiguity.
 	 */
 	unsigned long old_addr;
+
+	const char *obj_name; /* "vmlinux" or module name */
 };
 
-struct lp_dynrela {
+struct lpc_dynrela {
 	unsigned long dest;
 	unsigned long src;
 	unsigned long type;
 	const char *name;
+	const char *obj_name;
 	int addend;
 	int external;
 };
 
-struct lp_object {
-	const char *name; /* "vmlinux" or module name */
-	struct lp_func *funcs;
-	struct lp_dynrela *dynrelas;
-};
+struct lpc_patch {
+	/* internal */
+	struct list_head list;
+	struct kobject kobj;
+	enum lpc_state state;
 
-struct lp_patch {
+	/* external */
 	struct module *mod; /* module containing the patch */
-	struct lp_object *objs;
+	struct lpc_dynrela *dynrelas;
+	struct lpc_func funcs[];
 };
 
-int lp_register_patch(struct lp_patch *);
-int lp_unregister_patch(struct lp_patch *);
-int lp_enable_patch(struct lp_patch *);
-int lp_disable_patch(struct lp_patch *);
+
+extern int lpc_register_patch(struct lpc_patch *);
+extern int lpc_unregister_patch(struct lpc_patch *);
+extern int lpc_enable_patch(struct lpc_patch *);
+extern int lpc_disable_patch(struct lpc_patch *);
 
 #endif /* _LIVEPATCH_H_ */
kernel/livepatch/core.c +159 -416
index b32dbb5..feecc22 100644
@@ -31,78 +31,32 @@
 
 #include <linux/livepatch.h>
 
+#define lpc_for_each_patch_func(p, pf)   \
+        for (pf = p->funcs; pf->old_name; pf++)
+
 /*************************************
  * Core structures
  ************************************/
 
-/*
- * lp_ structs vs lpc_ structs
- *
- * For each element (patch, object, func) in the live-patching code,
- * there are two types with two different prefixes: lp_ and lpc_.
- *
- * Structures used by the live-patch modules to register with this core module
- * are prefixed with lp_ (live patching).  These structures are part of the
- * registration API and are defined in livepatch.h.  The structures used
- * internally by this core module are prefixed with lpc_ (live patching core).
- */
-
 static DEFINE_SEMAPHORE(lpc_mutex);
 static LIST_HEAD(lpc_patches);
 
-enum lpc_state {
-	DISABLED,
-	ENABLED
-};
-
-struct lpc_func {
-	struct list_head list;
-	struct kobject kobj;
-	struct ftrace_ops fops;
-	enum lpc_state state;
-
-	const char *old_name;
-	unsigned long new_addr;
-	unsigned long old_addr;
-};
-
-struct lpc_object {
-	struct list_head list;
-	struct kobject kobj;
-	struct module *mod; /* module associated with object */
-	enum lpc_state state;
-
-	const char *name;
-	struct list_head funcs;
-	struct lp_dynrela *dynrelas;
-};
-
-struct lpc_patch {
-	struct list_head list;
-	struct kobject kobj;
-	struct lp_patch *userpatch; /* for correlation during unregister */
-	enum lpc_state state;
-
-	struct module *mod;
-	struct list_head objs;
-};
-
 /*******************************************
  * Helpers
  *******************************************/
 
-/* sets obj->mod if object is not vmlinux and module was found */
-static bool is_object_loaded(struct lpc_object *obj)
+/* sets patch_func->mod if object is not vmlinux and module was found */
+static bool is_object_loaded(struct lpc_func *patch_func)
 {
 	struct module *mod;
 
-	if (!strcmp(obj->name, "vmlinux"))
+	if (!strcmp(patch_func->obj_name, "vmlinux"))
 		return 1;
 
 	mutex_lock(&module_mutex);
-	mod = find_module(obj->name);
+	mod = find_module(patch_func->obj_name);
 	mutex_unlock(&module_mutex);
-	obj->mod = mod;
+	patch_func->mod = mod;
 
 	return !!mod;
 }
@@ -254,18 +208,18 @@ static int lpc_find_external_symbol(struct module *pmod, const char *name,
 	return lpc_find_symbol(pmod->name, name, addr);
 }
 
-static int lpc_write_object_relocations(struct module *pmod,
-					struct lpc_object *obj)
+static int lpc_write_relocations(struct module *pmod,
+		struct lpc_dynrela *patch_dynrelas)
 {
 	int ret, size, readonly = 0, numpages;
-	struct lp_dynrela *dynrela;
+	struct lpc_dynrela *dynrela;
 	u64 loc, val;
 	unsigned long core = (unsigned long)pmod->module_core;
 	unsigned long core_ro_size = pmod->core_ro_size;
 	unsigned long core_size = pmod->core_size;
 
-	for (dynrela = obj->dynrelas; dynrela->name; dynrela++) {
-		if (!strcmp(obj->name, "vmlinux")) {
+	for (dynrela = patch_dynrelas; dynrela->name; dynrela++) {
+		if (!strcmp(dynrela->obj_name, "vmlinux")) {
 			ret = lpc_verify_vmlinux_symbol(dynrela->name,
 							dynrela->src);
 			if (ret)
@@ -277,7 +231,7 @@ static int lpc_write_object_relocations(struct module *pmod,
 							       dynrela->name,
 							       &dynrela->src);
 			else
-				ret = lpc_find_symbol(obj->mod->name,
+				ret = lpc_find_symbol(dynrela->obj_name,
 						      dynrela->name,
 						      &dynrela->src);
 			if (ret)
@@ -357,7 +311,7 @@ static int lpc_enable_func(struct lpc_func *func)
 	int ret;
 
 	BUG_ON(!func->old_addr);
-	BUG_ON(func->state != DISABLED);
+	BUG_ON(func->state != LPC_DISABLED);
 	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
 	if (ret) {
 		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
@@ -370,16 +324,16 @@ static int lpc_enable_func(struct lpc_func *func)
 		       func->old_name, ret);
 		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
 	} else
-		func->state = ENABLED;
+		func->state = LPC_ENABLED;
 
 	return ret;
 }
 
-static int lpc_unregister_func(struct lpc_func *func)
+static int lpc_disable_func(struct lpc_func *func)
 {
 	int ret;
 
-	BUG_ON(func->state != ENABLED);
+	BUG_ON(func->state != LPC_ENABLED);
 	if (!func->old_addr)
 		/* parent object is not loaded */
 		return 0;
@@ -392,173 +346,131 @@ static int lpc_unregister_func(struct lpc_func *func)
 	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
 	if (ret)
 		pr_warn("function unregister succeeded but failed to clear the filter\n");
-	func->state = DISABLED;
+	func->state = LPC_DISABLED;
 
 	return 0;
 }
 
-static int lpc_unregister_object(struct lpc_object *obj)
-{
-	struct lpc_func *func;
-	int ret;
-
-	list_for_each_entry(func, &obj->funcs, list) {
-		if (func->state != ENABLED)
-			continue;
-		ret = lpc_unregister_func(func);
-		if (ret)
-			return ret;
-		if (strcmp(obj->name, "vmlinux"))
-			func->old_addr = 0;
-	}
-	if (obj->mod)
-		module_put(obj->mod);
-	obj->state = DISABLED;
-
-	return 0;
-}
-
-/* caller must ensure that obj->mod is set if object is a module */
-static int lpc_enable_object(struct module *pmod, struct lpc_object *obj)
-{
-	struct lpc_func *func;
-	int ret;
-
-	if (obj->mod && !try_module_get(obj->mod))
-		return -ENODEV;
-
-	if (obj->dynrelas) {
-		ret = lpc_write_object_relocations(pmod, obj);
-		if (ret)
-			goto unregister;
-	}
-	list_for_each_entry(func, &obj->funcs, list) {
-		ret = lpc_find_verify_func_addr(func, obj->name);
-		if (ret)
-			goto unregister;
-
-		ret = lpc_enable_func(func);
-		if (ret)
-			goto unregister;
-	}
-	obj->state = ENABLED;
-
-	return 0;
-unregister:
-	WARN_ON(lpc_unregister_object(obj));
-	return ret;
-}
-
 /******************************
  * enable/disable
  ******************************/
 
 /* must be called with lpc_mutex held */
-static struct lpc_patch *lpc_find_patch(struct lp_patch *userpatch)
-{
-	struct lpc_patch *patch;
-
-	list_for_each_entry(patch, &lpc_patches, list)
-		if (patch->userpatch == userpatch)
-			return patch;
-
-	return NULL;
-}
-
-/* must be called with lpc_mutex held */
-static int lpc_disable_patch(struct lpc_patch *patch)
+static int __lpc_disable_patch(struct lpc_patch *patch)
 {
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret;
 
 	pr_notice("disabling patch '%s'\n", patch->mod->name);
 
-	list_for_each_entry(obj, &patch->objs, list) {
-		if (obj->state != ENABLED)
+	lpc_for_each_patch_func(patch, patch_func) {
+		if (patch_func->state != LPC_ENABLED)
 			continue;
-		ret = lpc_unregister_object(obj);
-		if (ret)
+		ret = lpc_disable_func(patch_func);
+		if (ret) {
+			pr_err("lpc: cannot disable function %s\n",
+				patch_func->old_name);
 			return ret;
+		}
+
+		if (strcmp(patch_func->obj_name, "vmlinux"))
+			patch_func->old_addr = 0;
+		if (patch_func->mod)
+			module_put(patch_func->mod);
 	}
-	patch->state = DISABLED;
+	patch->state = LPC_DISABLED;
 
 	return 0;
 }
 
-int lp_disable_patch(struct lp_patch *userpatch)
+int lpc_disable_patch(struct lpc_patch *patch)
 {
-	struct lpc_patch *patch;
 	int ret;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	ret = lpc_disable_patch(patch);
-out:
+	ret = __lpc_disable_patch(patch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_disable_patch);
+EXPORT_SYMBOL_GPL(lpc_disable_patch);
+
+static int lpc_verify_enable_func(struct lpc_func *patch_func)
+{
+	int ret;
+
+	if (patch_func->mod && !try_module_get(patch_func->mod))
+		return -ENODEV;
+
+	ret = lpc_find_verify_func_addr(patch_func, patch_func->obj_name);
+	if (ret)
+		return ret;
+
+	ret = lpc_enable_func(patch_func);
+	if (ret)
+		return ret;
+
+	return 0;
+}
 
 /* must be called with lpc_mutex held */
-static int lpc_enable_patch(struct lpc_patch *patch)
+static int __lpc_enable_patch(struct lpc_patch *patch)
 {
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret;
 
-	BUG_ON(patch->state != DISABLED);
+	BUG_ON(patch->state != LPC_DISABLED);
 
 	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
 	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
 
 	pr_notice("enabling patch '%s'\n", patch->mod->name);
 
-	list_for_each_entry(obj, &patch->objs, list) {
-		if (!is_object_loaded(obj))
+	if (patch->dynrelas) {
+		ret = lpc_write_relocations(patch->mod, patch->dynrelas);
+		if (ret)
+			goto err;
+	}
+
+	lpc_for_each_patch_func(patch, patch_func) {
+		if (!is_object_loaded(patch_func))
 			continue;
-		ret = lpc_enable_object(patch->mod, obj);
+
+		ret = lpc_verify_enable_func(patch_func);
 		if (ret)
-			goto unregister;
+			goto err;
 	}
-	patch->state = ENABLED;
+	patch->state = LPC_ENABLED;
+
 	return 0;
 
-unregister:
-	WARN_ON(lpc_disable_patch(patch));
+err:
+	WARN_ON(__lpc_disable_patch(patch));
 	return ret;
 }
 
-int lp_enable_patch(struct lp_patch *userpatch)
+int lpc_enable_patch(struct lpc_patch *patch)
 {
-	struct lpc_patch *patch;
 	int ret;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	ret = lpc_enable_patch(patch);
-out:
+	ret = __lpc_enable_patch(patch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_enable_patch);
+EXPORT_SYMBOL_GPL(lpc_enable_patch);
 
 /******************************
  * module notifier
  *****************************/
 
-static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+static int lpc_module_notify(struct notifier_block *nb, unsigned long action,
 			    void *data)
 {
 	struct module *mod = data;
 	struct lpc_patch *patch;
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret = 0;
 
 	if (action != MODULE_STATE_COMING)
@@ -567,32 +479,42 @@ static int lp_module_notify(struct notifier_block *nb, unsigned long action,
 	down(&lpc_mutex);
 
 	list_for_each_entry(patch, &lpc_patches, list) {
-		if (patch->state == DISABLED)
+		if (patch->state == LPC_DISABLED)
 			continue;
-		list_for_each_entry(obj, &patch->objs, list) {
-			if (strcmp(obj->name, mod->name))
+
+		if (patch->dynrelas) {
+			ret = lpc_write_relocations(patch->mod,
+				patch->dynrelas);
+			if (ret)
+				goto err;
+		}
+
+		lpc_for_each_patch_func(patch, patch_func) {
+			if (strcmp(patch_func->obj_name, mod->name))
 				continue;
+
 			pr_notice("load of module '%s' detected, applying patch '%s'\n",
 				  mod->name, patch->mod->name);
-			obj->mod = mod;
-			ret = lpc_enable_object(patch->mod, obj);
+			patch_func->mod = mod;
+
+			ret = lpc_verify_enable_func(patch_func);
 			if (ret)
-				goto out;
-			break;
+				goto err;
 		}
 	}
 
 	up(&lpc_mutex);
 	return 0;
-out:
+
+err:
 	up(&lpc_mutex);
 	WARN("failed to apply patch '%s' to module '%s'\n",
 		patch->mod->name, mod->name);
 	return 0;
 }
 
-static struct notifier_block lp_module_nb = {
-	.notifier_call = lp_module_notify,
+static struct notifier_block lpc_module_nb = {
+	.notifier_call = lpc_module_notify,
 	.priority = INT_MIN, /* called last */
 };
 
@@ -603,10 +525,7 @@ static struct notifier_block lp_module_nb = {
  * /sys/kernel/livepatch
  * /sys/kernel/livepatch/<patch>
  * /sys/kernel/livepatch/<patch>/enabled
- * /sys/kernel/livepatch/<patch>/<object>
- * /sys/kernel/livepatch/<patch>/<object>/<func>
- * /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr
- * /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr
+ * /sys/kernel/livepatch/<patch>/funcs
  */
 
 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -620,7 +539,7 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
 	if (ret)
 		return -EINVAL;
 
-	if (val != DISABLED && val != ENABLED)
+	if (val != LPC_DISABLED && val != LPC_ENABLED)
 		return -EINVAL;
 
 	patch = container_of(kobj, struct lpc_patch, kobj);
@@ -632,12 +551,12 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
 		goto out;
 	}
 
-	if (val == ENABLED) {
-		ret = lpc_enable_patch(patch);
+	if (val == LPC_ENABLED) {
+		ret = __lpc_enable_patch(patch);
 		if (ret)
 			goto out;
 	} else {
-		ret = lpc_disable_patch(patch);
+		ret = __lpc_disable_patch(patch);
 		if (ret)
 			goto out;
 	}
@@ -657,40 +576,35 @@ static ssize_t enabled_show(struct kobject *kobj,
 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
 }
 
-static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
-static struct attribute *lpc_patch_attrs[] = {
-	&enabled_kobj_attr.attr,
-	NULL
-};
-
-static ssize_t new_addr_show(struct kobject *kobj,
+static ssize_t funcs_show(struct kobject *kobj,
 			     struct kobj_attribute *attr, char *buf)
 {
-	struct lpc_func *func;
-
-	func = container_of(kobj, struct lpc_func, kobj);
-	return snprintf(buf, PAGE_SIZE-1, "0x%016lx\n", func->new_addr);
-}
+	struct lpc_patch *patch;
+	const struct lpc_func *patch_func;
+	ssize_t size;
 
-static struct kobj_attribute new_addr_kobj_attr = __ATTR_RO(new_addr);
+	size = snprintf(buf, PAGE_SIZE, "Functions:\n");
 
-static ssize_t old_addr_show(struct kobject *kobj,
-			     struct kobj_attribute *attr, char *buf)
-{
-	struct lpc_func *func;
+	patch = container_of(kobj, struct lpc_patch, kobj);
+	lpc_for_each_patch_func(patch, patch_func)
+		size += snprintf(buf + size, PAGE_SIZE - size, "%s\n",
+				patch_func->old_name);
 
-	func = container_of(kobj, struct lpc_func, kobj);
-	return snprintf(buf, PAGE_SIZE-1, "0x%016lx\n", func->old_addr);
+        return size;
 }
 
-static struct kobj_attribute old_addr_kobj_attr = __ATTR_RO(old_addr);
-
-static struct attribute *lpc_func_attrs[] = {
-	&new_addr_kobj_attr.attr,
-	&old_addr_kobj_attr.attr,
+static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
+static struct kobj_attribute funcs_kobj_attr = __ATTR_RO(funcs);
+static struct attribute *lpc_patch_attrs[] = {
+	&enabled_kobj_attr.attr,
+	&funcs_kobj_attr.attr,
 	NULL
 };
 
+static struct attribute_group lpc_patch_sysfs_group = {
+	.attrs = lpc_patch_attrs,
+};
+
 static struct kobject *lpc_root_kobj;
 
 static int lpc_create_root_kobj(void)
@@ -720,228 +634,67 @@ static void lpc_kobj_release_patch(struct kobject *kobj)
 static struct kobj_type lpc_ktype_patch = {
 	.release = lpc_kobj_release_patch,
 	.sysfs_ops = &kobj_sysfs_ops,
-	.default_attrs = lpc_patch_attrs
-};
-
-static void lpc_kobj_release_object(struct kobject *kobj)
-{
-	struct lpc_object *obj;
-
-	obj = container_of(kobj, struct lpc_object, kobj);
-	if (!list_empty(&obj->list))
-		list_del(&obj->list);
-	kfree(obj);
-}
-
-static struct kobj_type lpc_ktype_object = {
-	.release	= lpc_kobj_release_object,
-	.sysfs_ops	= &kobj_sysfs_ops,
-};
-
-static void lpc_kobj_release_func(struct kobject *kobj)
-{
-	struct lpc_func *func;
-
-	func = container_of(kobj, struct lpc_func, kobj);
-	if (!list_empty(&func->list))
-		list_del(&func->list);
-	kfree(func);
-}
-
-static struct kobj_type lpc_ktype_func = {
-	.release	= lpc_kobj_release_func,
-	.sysfs_ops	= &kobj_sysfs_ops,
-	.default_attrs = lpc_func_attrs
 };
 
 /*********************************
- * structure allocation
+ * structure init and free
  ********************************/
 
-static void lpc_free_funcs(struct lpc_object *obj)
-{
-	struct lpc_func *func, *funcsafe;
-
-	list_for_each_entry_safe(func, funcsafe, &obj->funcs, list)
-		kobject_put(&func->kobj);
-}
-
-static void lpc_free_objects(struct lpc_patch *patch)
-{
-	struct lpc_object *obj, *objsafe;
-
-	list_for_each_entry_safe(obj, objsafe, &patch->objs, list) {
-		lpc_free_funcs(obj);
-		kobject_put(&obj->kobj);
-	}
-}
-
 static void lpc_free_patch(struct lpc_patch *patch)
 {
-	lpc_free_objects(patch);
+	sysfs_remove_group(&patch->kobj, &lpc_patch_sysfs_group);
 	kobject_put(&patch->kobj);
 }
 
-static struct lpc_func *lpc_create_func(struct kobject *root,
-					struct lp_func *userfunc)
+static int lpc_init_patch(struct lpc_patch *patch)
 {
-	struct lpc_func *func;
 	struct ftrace_ops *ops;
+	struct lpc_func *patch_func;
 	int ret;
 
-	/* alloc */
-	func = kzalloc(sizeof(*func), GFP_KERNEL);
-	if (!func)
-		return NULL;
-
 	/* init */
-	INIT_LIST_HEAD(&func->list);
-	func->old_name = userfunc->old_name;
-	func->new_addr = (unsigned long)userfunc->new_func;
-	func->old_addr = userfunc->old_addr;
-	func->state = DISABLED;
-	ops = &func->fops;
-	ops->private = func;
-	ops->func = lpc_ftrace_handler;
-	ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
-
-	/* sysfs */
-	ret = kobject_init_and_add(&func->kobj, &lpc_ktype_func,
-				   root, func->old_name);
-	if (ret) {
-		kfree(func);
-		return NULL;
-	}
-
-	return func;
-}
-
-static int lpc_create_funcs(struct lpc_object *obj,
-			    struct lp_func *userfuncs)
-{
-	struct lp_func *userfunc;
-	struct lpc_func *func;
-
-	if (!userfuncs)
-		return -EINVAL;
-
-	for (userfunc = userfuncs; userfunc->old_name; userfunc++) {
-		func = lpc_create_func(&obj->kobj, userfunc);
-		if (!func)
-			goto free;
-		list_add(&func->list, &obj->funcs);
-	}
-	return 0;
-free:
-	lpc_free_funcs(obj);
-	return -ENOMEM;
-}
-
-static struct lpc_object *lpc_create_object(struct kobject *root,
-					    struct lp_object *userobj)
-{
-	struct lpc_object *obj;
-	int ret;
-
-	/* alloc */
-	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
-	if (!obj)
-		return NULL;
-
-	/* init */
-	INIT_LIST_HEAD(&obj->list);
-	obj->name = userobj->name;
-	obj->dynrelas = userobj->dynrelas;
-	obj->state = DISABLED;
-	/* obj->mod set by is_object_loaded() */
-	INIT_LIST_HEAD(&obj->funcs);
-
-	/* sysfs */
-	ret = kobject_init_and_add(&obj->kobj, &lpc_ktype_object,
-				   root, obj->name);
-	if (ret) {
-		kfree(obj);
-		return NULL;
-	}
-
-	/* create functions */
-	ret = lpc_create_funcs(obj, userobj->funcs);
-	if (ret) {
-		kobject_put(&obj->kobj);
-		return NULL;
-	}
-
-	return obj;
-}
-
-static int lpc_create_objects(struct lpc_patch *patch,
-			      struct lp_object *userobjs)
-{
-	struct lp_object *userobj;
-	struct lpc_object *obj;
-
-	if (!userobjs)
-		return -EINVAL;
-
-	for (userobj = userobjs; userobj->name; userobj++) {
-		obj = lpc_create_object(&patch->kobj, userobj);
-		if (!obj)
-			goto free;
-		list_add(&obj->list, &patch->objs);
-	}
-	return 0;
-free:
-	lpc_free_objects(patch);
-	return -ENOMEM;
-}
-
-static int lpc_create_patch(struct lp_patch *userpatch)
-{
-	struct lpc_patch *patch;
-	int ret;
-
-	/* alloc */
-	patch = kzalloc(sizeof(*patch), GFP_KERNEL);
-	if (!patch)
-		return -ENOMEM;
-
-	/* init */
-	INIT_LIST_HEAD(&patch->list);
-	patch->userpatch = userpatch;
-	patch->mod = userpatch->mod;
-	patch->state = DISABLED;
-	INIT_LIST_HEAD(&patch->objs);
+	patch->state = LPC_DISABLED;
 
 	/* sysfs */
 	ret = kobject_init_and_add(&patch->kobj, &lpc_ktype_patch,
 				   lpc_root_kobj, patch->mod->name);
-	if (ret) {
-		kfree(patch);
-		return ret;
-	}
+	if (ret)
+		goto err_root;
 
-	/* create objects */
-	ret = lpc_create_objects(patch, userpatch->objs);
-	if (ret) {
-		kobject_put(&patch->kobj);
-		return ret;
+	/* create functions */
+	lpc_for_each_patch_func(patch, patch_func) {
+		patch_func->new_addr = (unsigned long)patch_func->new_func;
+		patch_func->state = LPC_DISABLED;
+		ops = &patch_func->fops;
+		ops->private = patch_func;
+		ops->func = lpc_ftrace_handler;
+		ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
 	}
 
+	ret = sysfs_create_group(&patch->kobj, &lpc_patch_sysfs_group);
+	if (ret)
+		goto err_patch;
+
 	/* add to global list of patches */
 	list_add(&patch->list, &lpc_patches);
 
 	return 0;
+
+err_patch:
+	kobject_put(&patch->kobj);
+err_root:
+	return ret;
 }
 
 /************************************
  * register/unregister
  ***********************************/
 
-int lp_register_patch(struct lp_patch *userpatch)
+int lpc_register_patch(struct lpc_patch *userpatch)
 {
 	int ret;
 
-	if (!userpatch || !userpatch->mod || !userpatch->objs)
+	if (!userpatch || !userpatch->mod || !userpatch->funcs)
 		return -EINVAL;
 
 	/*
@@ -955,36 +708,26 @@ int lp_register_patch(struct lp_patch *userpatch)
 		return -ENODEV;
 
 	down(&lpc_mutex);
-	ret = lpc_create_patch(userpatch);
+	ret = lpc_init_patch(userpatch);
 	up(&lpc_mutex);
 	if (ret)
 		module_put(userpatch->mod);
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_register_patch);
+EXPORT_SYMBOL_GPL(lpc_register_patch);
 
-int lp_unregister_patch(struct lp_patch *userpatch)
+int lpc_unregister_patch(struct lpc_patch *userpatch)
 {
-	struct lpc_patch *patch;
 	int ret = 0;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	if (patch->state == ENABLED) {
-		ret = -EINVAL;
-		goto out;
-	}
-	lpc_free_patch(patch);
-out:
+	lpc_free_patch(userpatch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_unregister_patch);
+EXPORT_SYMBOL_GPL(lpc_unregister_patch);
 
 /************************************
  * entry/exit
@@ -994,7 +737,7 @@ static int lpc_init(void)
 {
 	int ret;
 
-	ret = register_module_notifier(&lp_module_nb);
+	ret = register_module_notifier(&lpc_module_nb);
 	if (ret)
 		return ret;
 
@@ -1004,14 +747,14 @@ static int lpc_init(void)
 
 	return 0;
 unregister:
-	unregister_module_notifier(&lp_module_nb);
+	unregister_module_notifier(&lpc_module_nb);
 	return ret;
 }
 
 static void lpc_exit(void)
 {
 	lpc_remove_root_kobj();
-	unregister_module_notifier(&lp_module_nb);
+	unregister_module_notifier(&lpc_module_nb);
 }
 
 module_init(lpc_init);
Signature · 1 line
2.1.2
maintainer
#40 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 42 lines · Seth Jennings
Quoted 19 lines · Miroslav Benes

Hi,

thank you for the first version of the united live patching core.

The patch below implements some of our review objections. Changes are described in the commit log. It simplifies the hierarchy of data structures, removes data duplication (lp_ and lpc_ structures) and simplifies sysfs directory.

I did not try to repair other stuff (races, function names, function prefix, api symmetry etc.). It should serve as a demonstration of our point of view.

There are some problems with this. try_module_get and module_put may be called several times for each kernel module where some function is patched in. This should be fixed with module going notifier as suggested by Petr.

Quoted 5 lines

The modified core was tested with modified testing live patch originally from Seth's github. It worked as expected.

Please take a look at these changes, so we can discuss them in more detail.

Thanks Miroslav.

The functional changes are a little hard to break out from the formatting changes like s/disable/unregister and s/lp_/lpc_/ or adding LPC_ prefix to the enum, most (all?) of which I have included for v2.

A problem with getting rid of the object layer is that there are operations we do that are object-level operations. For example, module lookup and deferred module patching. Also, the dynamic relocations need to be associated with an object, not a patch, as not all relocations will be able to be applied at patch load time for patches that apply to modules that aren't loaded. I understand that you can walk the patch-level dynrela table and skip dynrela entries that don't match the target object, but why do that when you can cleanly express the relationship with a data structure hierarchy?

Quoted 4 lines

One example is the call to is_object_loaded() (renamed and reworked in v2 btw) per function rather than per object. That is duplicate work and information that could be more cleanly expressed through an object layer.

I understand your arguments as I had thought about this before. It is true that some operations which are connected with the object level could be duplicated in our approach. However the list of patched functions and especially objects (vmlinux or modules) would be always relatively short. Two-level hierarchy (functions and patches) is in my opinion more compact and easier to maintain. I do not think that object-level outweighs this. Let us think about it some more...

Quoted 11 lines

I also understand that sysfs/kobject stuff adds code length. However, the new "funcs" attribute is procfs style, not sysfs style. sysfs attribute should convey _one_ value.

Quoted 1 line

From Documenation/filesystems/sysfs.txt:

==========
Attributes should be ASCII text files, preferably with only one value per file. It is noted that it may not be efficient to contain only one value per file, so it is socially acceptable to express an array of values of the same type.

Quoted 4 lines

Mixing types, expressing multiple lines of data, and doing fancy formatting of data is heavily frowned upon. Doing these things may get you publicly humiliated and your code rewritten without notice.
==========

Ah, you are right. My mistake. Thank you.

Quoted 4 lines

Also the function list would have object ambiguity. If there was a patched function my_func() in both vmlinux and a module, it would just appear on the list twice. You can fix this by using the mod:func syntax like kallsyms, but it isn't as clean as expressing it in a hierarchy.

Yes, using mod:func or check against module name (and not only function name) would be necessary. Ambiguity would be also the problem in sysfs directory tree without object level. But it is doable.

Quoted 8 lines

As far as the unification of the API structures with the internal structures I have two points. First is that, IMHO, we should assume that the structures coming from the user are const. In kpatch, for example, we pass through some structures that are not created in the code, but by the patch generation tool and stored in an ELF section (read-only). Additionally, I am really against exposing the internal fields. Commenting them as "internal" is just messy and we have to change the .h file every time when want to add a field for internal use.

Changing the header file and thus API between different kernel releases is not a problem in my opinion. First live patching module would be created against specific kernel version (so the correct API is known). Second we would like to add userspace tool for automatic patch generation to upstream sometime in the future. API would be of "no importance" there as the situation in perf now (if I understand it correctly).

Quoted 7 lines

It seems that the primary purpose of this patch is to reduce the lines of code. However, I think that the object layer of the data structure cleanly expresses the object<->function relationship and makes code like the deferred patching much more straightforward since you already have the functions/dynrelas organized by object. You don't have to do the nasty "if (strcmp(func->obj_name, objname)) continue;" business over the entire patch every time.

The primary purpose was to show our point of view. I do not pretend that there are no problems, but there are also some benefits.

Quoted 3 lines

Be advised, I have also done away with the new_addr/old_addr attributes for v2 and replaced the patched module ref'ing with a combination of a GOING notifier with lpc_mutex for protection.

Great. I'll wait for v2 and resend our patch based on that, rebased and split to several patches. We can continue the discussion afterwards. Is it ok?

Thank you

---
Miroslav Benes
SUSE Labs

Quoted 975 lines
Quoted 974 lines

----
From f659a18a630de27b47d375119d793e28ee50da04 Mon Sep 17 00:00:00 2001
From: Miroslav Benes <[email protected]>
Date: Thu, 13 Nov 2014 10:25:48 +0100
Subject: [PATCH] lpc: simplification of structure and sysfs hierarchy

Original code has several issues this patch tries to remove.

First, there is only lpc_func structure for patched function and lpc_patch for the patch as a whole. Therefore lpc_object structure as middle step of hierarchy is removed. Patched function is still associated with some object (vmlinux or module) through obj_name. Dynrelas are now in lpc_patch structure and object identifier (obj_name) is in the lpc_dynrela to preserve the connection.

Second, sysfs structure is simplified. We do not need to propagate old_addr and new_addr. So, there is subdirectory for each patch (patching module) which includes original enabled attribute and new one funcs attribute which lists the patched functions.

Third, data duplication (lp_ and lpc_ structures) is removed. lpc_ structures are now in the header file and made available for the user. This allows us to remove almost all the functions for structure allocation in the original code.

---
 include/linux/livepatch.h |  46 ++--
 kernel/livepatch/core.c   | 575 +++++++++++++---------------------------------
 2 files changed, 191 insertions(+), 430 deletions(-)
include/linux/livepatch.h +32 -14
index c7a415b..db5ba00 100644
@@ -2,10 +2,23 @@
 #define _LIVEPATCH_H_
 
 #include <linux/module.h>
+#include <linux/ftrace.h>
 
-struct lp_func {
+enum lpc_state {
+	LPC_DISABLED,
+	LPC_ENABLED
+};
+
+struct lpc_func {
+	/* internal */
+	struct ftrace_ops fops;
+	enum lpc_state state;
+	struct module *mod; /* module associated with patched function */
+	unsigned long new_addr; /* replacement function in patch module */
+
+	/* external */
 	const char *old_name; /* function to be patched */
-	void *new_func; /* replacement function in patch module */
+	void *new_func;
 	/*
 	 * The old_addr field is optional and can be used to resolve
 	 * duplicate symbol names in the vmlinux object.  If this
@@ -15,31 +28,36 @@ struct lp_func {
 	 * way to resolve the ambiguity.
 	 */
 	unsigned long old_addr;
+
+	const char *obj_name; /* "vmlinux" or module name */
 };
 
-struct lp_dynrela {
+struct lpc_dynrela {
 	unsigned long dest;
 	unsigned long src;
 	unsigned long type;
 	const char *name;
+	const char *obj_name;
 	int addend;
 	int external;
 };
 
-struct lp_object {
-	const char *name; /* "vmlinux" or module name */
-	struct lp_func *funcs;
-	struct lp_dynrela *dynrelas;
-};
+struct lpc_patch {
+	/* internal */
+	struct list_head list;
+	struct kobject kobj;
+	enum lpc_state state;
 
-struct lp_patch {
+	/* external */
 	struct module *mod; /* module containing the patch */
-	struct lp_object *objs;
+	struct lpc_dynrela *dynrelas;
+	struct lpc_func funcs[];
 };
 
-int lp_register_patch(struct lp_patch *);
-int lp_unregister_patch(struct lp_patch *);
-int lp_enable_patch(struct lp_patch *);
-int lp_disable_patch(struct lp_patch *);
+
+extern int lpc_register_patch(struct lpc_patch *);
+extern int lpc_unregister_patch(struct lpc_patch *);
+extern int lpc_enable_patch(struct lpc_patch *);
+extern int lpc_disable_patch(struct lpc_patch *);
 
 #endif /* _LIVEPATCH_H_ */
kernel/livepatch/core.c +159 -416
index b32dbb5..feecc22 100644
@@ -31,78 +31,32 @@
 
 #include <linux/livepatch.h>
 
+#define lpc_for_each_patch_func(p, pf)   \
+        for (pf = p->funcs; pf->old_name; pf++)
+
 /*************************************
  * Core structures
  ************************************/
 
-/*
- * lp_ structs vs lpc_ structs
- *
- * For each element (patch, object, func) in the live-patching code,
- * there are two types with two different prefixes: lp_ and lpc_.
- *
- * Structures used by the live-patch modules to register with this core module
- * are prefixed with lp_ (live patching).  These structures are part of the
- * registration API and are defined in livepatch.h.  The structures used
- * internally by this core module are prefixed with lpc_ (live patching core).
- */
-
 static DEFINE_SEMAPHORE(lpc_mutex);
 static LIST_HEAD(lpc_patches);
 
-enum lpc_state {
-	DISABLED,
-	ENABLED
-};
-
-struct lpc_func {
-	struct list_head list;
-	struct kobject kobj;
-	struct ftrace_ops fops;
-	enum lpc_state state;
-
-	const char *old_name;
-	unsigned long new_addr;
-	unsigned long old_addr;
-};
-
-struct lpc_object {
-	struct list_head list;
-	struct kobject kobj;
-	struct module *mod; /* module associated with object */
-	enum lpc_state state;
-
-	const char *name;
-	struct list_head funcs;
-	struct lp_dynrela *dynrelas;
-};
-
-struct lpc_patch {
-	struct list_head list;
-	struct kobject kobj;
-	struct lp_patch *userpatch; /* for correlation during unregister */
-	enum lpc_state state;
-
-	struct module *mod;
-	struct list_head objs;
-};
-
 /*******************************************
  * Helpers
  *******************************************/
 
-/* sets obj->mod if object is not vmlinux and module was found */
-static bool is_object_loaded(struct lpc_object *obj)
+/* sets patch_func->mod if object is not vmlinux and module was found */
+static bool is_object_loaded(struct lpc_func *patch_func)
 {
 	struct module *mod;
 
-	if (!strcmp(obj->name, "vmlinux"))
+	if (!strcmp(patch_func->obj_name, "vmlinux"))
 		return 1;
 
 	mutex_lock(&module_mutex);
-	mod = find_module(obj->name);
+	mod = find_module(patch_func->obj_name);
 	mutex_unlock(&module_mutex);
-	obj->mod = mod;
+	patch_func->mod = mod;
 
 	return !!mod;
 }
@@ -254,18 +208,18 @@ static int lpc_find_external_symbol(struct module *pmod, const char *name,
 	return lpc_find_symbol(pmod->name, name, addr);
 }
 
-static int lpc_write_object_relocations(struct module *pmod,
-					struct lpc_object *obj)
+static int lpc_write_relocations(struct module *pmod,
+		struct lpc_dynrela *patch_dynrelas)
 {
 	int ret, size, readonly = 0, numpages;
-	struct lp_dynrela *dynrela;
+	struct lpc_dynrela *dynrela;
 	u64 loc, val;
 	unsigned long core = (unsigned long)pmod->module_core;
 	unsigned long core_ro_size = pmod->core_ro_size;
 	unsigned long core_size = pmod->core_size;
 
-	for (dynrela = obj->dynrelas; dynrela->name; dynrela++) {
-		if (!strcmp(obj->name, "vmlinux")) {
+	for (dynrela = patch_dynrelas; dynrela->name; dynrela++) {
+		if (!strcmp(dynrela->obj_name, "vmlinux")) {
 			ret = lpc_verify_vmlinux_symbol(dynrela->name,
 							dynrela->src);
 			if (ret)
@@ -277,7 +231,7 @@ static int lpc_write_object_relocations(struct module *pmod,
 							       dynrela->name,
 							       &dynrela->src);
 			else
-				ret = lpc_find_symbol(obj->mod->name,
+				ret = lpc_find_symbol(dynrela->obj_name,
 						      dynrela->name,
 						      &dynrela->src);
 			if (ret)
@@ -357,7 +311,7 @@ static int lpc_enable_func(struct lpc_func *func)
 	int ret;
 
 	BUG_ON(!func->old_addr);
-	BUG_ON(func->state != DISABLED);
+	BUG_ON(func->state != LPC_DISABLED);
 	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 0, 0);
 	if (ret) {
 		pr_err("failed to set ftrace filter for function '%s' (%d)\n",
@@ -370,16 +324,16 @@ static int lpc_enable_func(struct lpc_func *func)
 		       func->old_name, ret);
 		ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
 	} else
-		func->state = ENABLED;
+		func->state = LPC_ENABLED;
 
 	return ret;
 }
 
-static int lpc_unregister_func(struct lpc_func *func)
+static int lpc_disable_func(struct lpc_func *func)
 {
 	int ret;
 
-	BUG_ON(func->state != ENABLED);
+	BUG_ON(func->state != LPC_ENABLED);
 	if (!func->old_addr)
 		/* parent object is not loaded */
 		return 0;
@@ -392,173 +346,131 @@ static int lpc_unregister_func(struct lpc_func *func)
 	ret = ftrace_set_filter_ip(&func->fops, func->old_addr, 1, 0);
 	if (ret)
 		pr_warn("function unregister succeeded but failed to clear the filter\n");
-	func->state = DISABLED;
+	func->state = LPC_DISABLED;
 
 	return 0;
 }
 
-static int lpc_unregister_object(struct lpc_object *obj)
-{
-	struct lpc_func *func;
-	int ret;
-
-	list_for_each_entry(func, &obj->funcs, list) {
-		if (func->state != ENABLED)
-			continue;
-		ret = lpc_unregister_func(func);
-		if (ret)
-			return ret;
-		if (strcmp(obj->name, "vmlinux"))
-			func->old_addr = 0;
-	}
-	if (obj->mod)
-		module_put(obj->mod);
-	obj->state = DISABLED;
-
-	return 0;
-}
-
-/* caller must ensure that obj->mod is set if object is a module */
-static int lpc_enable_object(struct module *pmod, struct lpc_object *obj)
-{
-	struct lpc_func *func;
-	int ret;
-
-	if (obj->mod && !try_module_get(obj->mod))
-		return -ENODEV;
-
-	if (obj->dynrelas) {
-		ret = lpc_write_object_relocations(pmod, obj);
-		if (ret)
-			goto unregister;
-	}
-	list_for_each_entry(func, &obj->funcs, list) {
-		ret = lpc_find_verify_func_addr(func, obj->name);
-		if (ret)
-			goto unregister;
-
-		ret = lpc_enable_func(func);
-		if (ret)
-			goto unregister;
-	}
-	obj->state = ENABLED;
-
-	return 0;
-unregister:
-	WARN_ON(lpc_unregister_object(obj));
-	return ret;
-}
-
 /******************************
  * enable/disable
  ******************************/
 
 /* must be called with lpc_mutex held */
-static struct lpc_patch *lpc_find_patch(struct lp_patch *userpatch)
-{
-	struct lpc_patch *patch;
-
-	list_for_each_entry(patch, &lpc_patches, list)
-		if (patch->userpatch == userpatch)
-			return patch;
-
-	return NULL;
-}
-
-/* must be called with lpc_mutex held */
-static int lpc_disable_patch(struct lpc_patch *patch)
+static int __lpc_disable_patch(struct lpc_patch *patch)
 {
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret;
 
 	pr_notice("disabling patch '%s'\n", patch->mod->name);
 
-	list_for_each_entry(obj, &patch->objs, list) {
-		if (obj->state != ENABLED)
+	lpc_for_each_patch_func(patch, patch_func) {
+		if (patch_func->state != LPC_ENABLED)
 			continue;
-		ret = lpc_unregister_object(obj);
-		if (ret)
+		ret = lpc_disable_func(patch_func);
+		if (ret) {
+			pr_err("lpc: cannot disable function %s\n",
+				patch_func->old_name);
 			return ret;
+		}
+
+		if (strcmp(patch_func->obj_name, "vmlinux"))
+			patch_func->old_addr = 0;
+		if (patch_func->mod)
+			module_put(patch_func->mod);
 	}
-	patch->state = DISABLED;
+	patch->state = LPC_DISABLED;
 
 	return 0;
 }
 
-int lp_disable_patch(struct lp_patch *userpatch)
+int lpc_disable_patch(struct lpc_patch *patch)
 {
-	struct lpc_patch *patch;
 	int ret;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	ret = lpc_disable_patch(patch);
-out:
+	ret = __lpc_disable_patch(patch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_disable_patch);
+EXPORT_SYMBOL_GPL(lpc_disable_patch);
+
+static int lpc_verify_enable_func(struct lpc_func *patch_func)
+{
+	int ret;
+
+	if (patch_func->mod && !try_module_get(patch_func->mod))
+		return -ENODEV;
+
+	ret = lpc_find_verify_func_addr(patch_func, patch_func->obj_name);
+	if (ret)
+		return ret;
+
+	ret = lpc_enable_func(patch_func);
+	if (ret)
+		return ret;
+
+	return 0;
+}
 
 /* must be called with lpc_mutex held */
-static int lpc_enable_patch(struct lpc_patch *patch)
+static int __lpc_enable_patch(struct lpc_patch *patch)
 {
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret;
 
-	BUG_ON(patch->state != DISABLED);
+	BUG_ON(patch->state != LPC_DISABLED);
 
 	pr_notice_once("tainting kernel with TAINT_LIVEPATCH\n");
 	add_taint(TAINT_LIVEPATCH, LOCKDEP_STILL_OK);
 
 	pr_notice("enabling patch '%s'\n", patch->mod->name);
 
-	list_for_each_entry(obj, &patch->objs, list) {
-		if (!is_object_loaded(obj))
+	if (patch->dynrelas) {
+		ret = lpc_write_relocations(patch->mod, patch->dynrelas);
+		if (ret)
+			goto err;
+	}
+
+	lpc_for_each_patch_func(patch, patch_func) {
+		if (!is_object_loaded(patch_func))
 			continue;
-		ret = lpc_enable_object(patch->mod, obj);
+
+		ret = lpc_verify_enable_func(patch_func);
 		if (ret)
-			goto unregister;
+			goto err;
 	}
-	patch->state = ENABLED;
+	patch->state = LPC_ENABLED;
+
 	return 0;
 
-unregister:
-	WARN_ON(lpc_disable_patch(patch));
+err:
+	WARN_ON(__lpc_disable_patch(patch));
 	return ret;
 }
 
-int lp_enable_patch(struct lp_patch *userpatch)
+int lpc_enable_patch(struct lpc_patch *patch)
 {
-	struct lpc_patch *patch;
 	int ret;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	ret = lpc_enable_patch(patch);
-out:
+	ret = __lpc_enable_patch(patch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_enable_patch);
+EXPORT_SYMBOL_GPL(lpc_enable_patch);
 
 /******************************
  * module notifier
  *****************************/
 
-static int lp_module_notify(struct notifier_block *nb, unsigned long action,
+static int lpc_module_notify(struct notifier_block *nb, unsigned long action,
 			    void *data)
 {
 	struct module *mod = data;
 	struct lpc_patch *patch;
-	struct lpc_object *obj;
+	struct lpc_func *patch_func;
 	int ret = 0;
 
 	if (action != MODULE_STATE_COMING)
@@ -567,32 +479,42 @@ static int lp_module_notify(struct notifier_block *nb, unsigned long action,
 	down(&lpc_mutex);
 
 	list_for_each_entry(patch, &lpc_patches, list) {
-		if (patch->state == DISABLED)
+		if (patch->state == LPC_DISABLED)
 			continue;
-		list_for_each_entry(obj, &patch->objs, list) {
-			if (strcmp(obj->name, mod->name))
+
+		if (patch->dynrelas) {
+			ret = lpc_write_relocations(patch->mod,
+				patch->dynrelas);
+			if (ret)
+				goto err;
+		}
+
+		lpc_for_each_patch_func(patch, patch_func) {
+			if (strcmp(patch_func->obj_name, mod->name))
 				continue;
+
 			pr_notice("load of module '%s' detected, applying patch '%s'\n",
 				  mod->name, patch->mod->name);
-			obj->mod = mod;
-			ret = lpc_enable_object(patch->mod, obj);
+			patch_func->mod = mod;
+
+			ret = lpc_verify_enable_func(patch_func);
 			if (ret)
-				goto out;
-			break;
+				goto err;
 		}
 	}
 
 	up(&lpc_mutex);
 	return 0;
-out:
+
+err:
 	up(&lpc_mutex);
 	WARN("failed to apply patch '%s' to module '%s'\n",
 		patch->mod->name, mod->name);
 	return 0;
 }
 
-static struct notifier_block lp_module_nb = {
-	.notifier_call = lp_module_notify,
+static struct notifier_block lpc_module_nb = {
+	.notifier_call = lpc_module_notify,
 	.priority = INT_MIN, /* called last */
 };
 
@@ -603,10 +525,7 @@ static struct notifier_block lp_module_nb = {
  * /sys/kernel/livepatch
  * /sys/kernel/livepatch/<patch>
  * /sys/kernel/livepatch/<patch>/enabled
- * /sys/kernel/livepatch/<patch>/<object>
- * /sys/kernel/livepatch/<patch>/<object>/<func>
- * /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr
- * /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr
+ * /sys/kernel/livepatch/<patch>/funcs
  */
 
 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
@@ -620,7 +539,7 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
 	if (ret)
 		return -EINVAL;
 
-	if (val != DISABLED && val != ENABLED)
+	if (val != LPC_DISABLED && val != LPC_ENABLED)
 		return -EINVAL;
 
 	patch = container_of(kobj, struct lpc_patch, kobj);
@@ -632,12 +551,12 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
 		goto out;
 	}
 
-	if (val == ENABLED) {
-		ret = lpc_enable_patch(patch);
+	if (val == LPC_ENABLED) {
+		ret = __lpc_enable_patch(patch);
 		if (ret)
 			goto out;
 	} else {
-		ret = lpc_disable_patch(patch);
+		ret = __lpc_disable_patch(patch);
 		if (ret)
 			goto out;
 	}
@@ -657,40 +576,35 @@ static ssize_t enabled_show(struct kobject *kobj,
 	return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->state);
 }
 
-static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
-static struct attribute *lpc_patch_attrs[] = {
-	&enabled_kobj_attr.attr,
-	NULL
-};
-
-static ssize_t new_addr_show(struct kobject *kobj,
+static ssize_t funcs_show(struct kobject *kobj,
 			     struct kobj_attribute *attr, char *buf)
 {
-	struct lpc_func *func;
-
-	func = container_of(kobj, struct lpc_func, kobj);
-	return snprintf(buf, PAGE_SIZE-1, "0x%016lx\n", func->new_addr);
-}
+	struct lpc_patch *patch;
+	const struct lpc_func *patch_func;
+	ssize_t size;
 
-static struct kobj_attribute new_addr_kobj_attr = __ATTR_RO(new_addr);
+	size = snprintf(buf, PAGE_SIZE, "Functions:\n");
 
-static ssize_t old_addr_show(struct kobject *kobj,
-			     struct kobj_attribute *attr, char *buf)
-{
-	struct lpc_func *func;
+	patch = container_of(kobj, struct lpc_patch, kobj);
+	lpc_for_each_patch_func(patch, patch_func)
+		size += snprintf(buf + size, PAGE_SIZE - size, "%s\n",
+				patch_func->old_name);
 
-	func = container_of(kobj, struct lpc_func, kobj);
-	return snprintf(buf, PAGE_SIZE-1, "0x%016lx\n", func->old_addr);
+        return size;
 }
 
-static struct kobj_attribute old_addr_kobj_attr = __ATTR_RO(old_addr);
-
-static struct attribute *lpc_func_attrs[] = {
-	&new_addr_kobj_attr.attr,
-	&old_addr_kobj_attr.attr,
+static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
+static struct kobj_attribute funcs_kobj_attr = __ATTR_RO(funcs);
+static struct attribute *lpc_patch_attrs[] = {
+	&enabled_kobj_attr.attr,
+	&funcs_kobj_attr.attr,
 	NULL
 };
 
+static struct attribute_group lpc_patch_sysfs_group = {
+	.attrs = lpc_patch_attrs,
+};
+
 static struct kobject *lpc_root_kobj;
 
 static int lpc_create_root_kobj(void)
@@ -720,228 +634,67 @@ static void lpc_kobj_release_patch(struct kobject *kobj)
 static struct kobj_type lpc_ktype_patch = {
 	.release = lpc_kobj_release_patch,
 	.sysfs_ops = &kobj_sysfs_ops,
-	.default_attrs = lpc_patch_attrs
-};
-
-static void lpc_kobj_release_object(struct kobject *kobj)
-{
-	struct lpc_object *obj;
-
-	obj = container_of(kobj, struct lpc_object, kobj);
-	if (!list_empty(&obj->list))
-		list_del(&obj->list);
-	kfree(obj);
-}
-
-static struct kobj_type lpc_ktype_object = {
-	.release	= lpc_kobj_release_object,
-	.sysfs_ops	= &kobj_sysfs_ops,
-};
-
-static void lpc_kobj_release_func(struct kobject *kobj)
-{
-	struct lpc_func *func;
-
-	func = container_of(kobj, struct lpc_func, kobj);
-	if (!list_empty(&func->list))
-		list_del(&func->list);
-	kfree(func);
-}
-
-static struct kobj_type lpc_ktype_func = {
-	.release	= lpc_kobj_release_func,
-	.sysfs_ops	= &kobj_sysfs_ops,
-	.default_attrs = lpc_func_attrs
 };
 
 /*********************************
- * structure allocation
+ * structure init and free
  ********************************/
 
-static void lpc_free_funcs(struct lpc_object *obj)
-{
-	struct lpc_func *func, *funcsafe;
-
-	list_for_each_entry_safe(func, funcsafe, &obj->funcs, list)
-		kobject_put(&func->kobj);
-}
-
-static void lpc_free_objects(struct lpc_patch *patch)
-{
-	struct lpc_object *obj, *objsafe;
-
-	list_for_each_entry_safe(obj, objsafe, &patch->objs, list) {
-		lpc_free_funcs(obj);
-		kobject_put(&obj->kobj);
-	}
-}
-
 static void lpc_free_patch(struct lpc_patch *patch)
 {
-	lpc_free_objects(patch);
+	sysfs_remove_group(&patch->kobj, &lpc_patch_sysfs_group);
 	kobject_put(&patch->kobj);
 }
 
-static struct lpc_func *lpc_create_func(struct kobject *root,
-					struct lp_func *userfunc)
+static int lpc_init_patch(struct lpc_patch *patch)
 {
-	struct lpc_func *func;
 	struct ftrace_ops *ops;
+	struct lpc_func *patch_func;
 	int ret;
 
-	/* alloc */
-	func = kzalloc(sizeof(*func), GFP_KERNEL);
-	if (!func)
-		return NULL;
-
 	/* init */
-	INIT_LIST_HEAD(&func->list);
-	func->old_name = userfunc->old_name;
-	func->new_addr = (unsigned long)userfunc->new_func;
-	func->old_addr = userfunc->old_addr;
-	func->state = DISABLED;
-	ops = &func->fops;
-	ops->private = func;
-	ops->func = lpc_ftrace_handler;
-	ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
-
-	/* sysfs */
-	ret = kobject_init_and_add(&func->kobj, &lpc_ktype_func,
-				   root, func->old_name);
-	if (ret) {
-		kfree(func);
-		return NULL;
-	}
-
-	return func;
-}
-
-static int lpc_create_funcs(struct lpc_object *obj,
-			    struct lp_func *userfuncs)
-{
-	struct lp_func *userfunc;
-	struct lpc_func *func;
-
-	if (!userfuncs)
-		return -EINVAL;
-
-	for (userfunc = userfuncs; userfunc->old_name; userfunc++) {
-		func = lpc_create_func(&obj->kobj, userfunc);
-		if (!func)
-			goto free;
-		list_add(&func->list, &obj->funcs);
-	}
-	return 0;
-free:
-	lpc_free_funcs(obj);
-	return -ENOMEM;
-}
-
-static struct lpc_object *lpc_create_object(struct kobject *root,
-					    struct lp_object *userobj)
-{
-	struct lpc_object *obj;
-	int ret;
-
-	/* alloc */
-	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
-	if (!obj)
-		return NULL;
-
-	/* init */
-	INIT_LIST_HEAD(&obj->list);
-	obj->name = userobj->name;
-	obj->dynrelas = userobj->dynrelas;
-	obj->state = DISABLED;
-	/* obj->mod set by is_object_loaded() */
-	INIT_LIST_HEAD(&obj->funcs);
-
-	/* sysfs */
-	ret = kobject_init_and_add(&obj->kobj, &lpc_ktype_object,
-				   root, obj->name);
-	if (ret) {
-		kfree(obj);
-		return NULL;
-	}
-
-	/* create functions */
-	ret = lpc_create_funcs(obj, userobj->funcs);
-	if (ret) {
-		kobject_put(&obj->kobj);
-		return NULL;
-	}
-
-	return obj;
-}
-
-static int lpc_create_objects(struct lpc_patch *patch,
-			      struct lp_object *userobjs)
-{
-	struct lp_object *userobj;
-	struct lpc_object *obj;
-
-	if (!userobjs)
-		return -EINVAL;
-
-	for (userobj = userobjs; userobj->name; userobj++) {
-		obj = lpc_create_object(&patch->kobj, userobj);
-		if (!obj)
-			goto free;
-		list_add(&obj->list, &patch->objs);
-	}
-	return 0;
-free:
-	lpc_free_objects(patch);
-	return -ENOMEM;
-}
-
-static int lpc_create_patch(struct lp_patch *userpatch)
-{
-	struct lpc_patch *patch;
-	int ret;
-
-	/* alloc */
-	patch = kzalloc(sizeof(*patch), GFP_KERNEL);
-	if (!patch)
-		return -ENOMEM;
-
-	/* init */
-	INIT_LIST_HEAD(&patch->list);
-	patch->userpatch = userpatch;
-	patch->mod = userpatch->mod;
-	patch->state = DISABLED;
-	INIT_LIST_HEAD(&patch->objs);
+	patch->state = LPC_DISABLED;
 
 	/* sysfs */
 	ret = kobject_init_and_add(&patch->kobj, &lpc_ktype_patch,
 				   lpc_root_kobj, patch->mod->name);
-	if (ret) {
-		kfree(patch);
-		return ret;
-	}
+	if (ret)
+		goto err_root;
 
-	/* create objects */
-	ret = lpc_create_objects(patch, userpatch->objs);
-	if (ret) {
-		kobject_put(&patch->kobj);
-		return ret;
+	/* create functions */
+	lpc_for_each_patch_func(patch, patch_func) {
+		patch_func->new_addr = (unsigned long)patch_func->new_func;
+		patch_func->state = LPC_DISABLED;
+		ops = &patch_func->fops;
+		ops->private = patch_func;
+		ops->func = lpc_ftrace_handler;
+		ops->flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_DYNAMIC;
 	}
 
+	ret = sysfs_create_group(&patch->kobj, &lpc_patch_sysfs_group);
+	if (ret)
+		goto err_patch;
+
 	/* add to global list of patches */
 	list_add(&patch->list, &lpc_patches);
 
 	return 0;
+
+err_patch:
+	kobject_put(&patch->kobj);
+err_root:
+	return ret;
 }
 
 /************************************
  * register/unregister
  ***********************************/
 
-int lp_register_patch(struct lp_patch *userpatch)
+int lpc_register_patch(struct lpc_patch *userpatch)
 {
 	int ret;
 
-	if (!userpatch || !userpatch->mod || !userpatch->objs)
+	if (!userpatch || !userpatch->mod || !userpatch->funcs)
 		return -EINVAL;
 
 	/*
@@ -955,36 +708,26 @@ int lp_register_patch(struct lp_patch *userpatch)
 		return -ENODEV;
 
 	down(&lpc_mutex);
-	ret = lpc_create_patch(userpatch);
+	ret = lpc_init_patch(userpatch);
 	up(&lpc_mutex);
 	if (ret)
 		module_put(userpatch->mod);
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_register_patch);
+EXPORT_SYMBOL_GPL(lpc_register_patch);
 
-int lp_unregister_patch(struct lp_patch *userpatch)
+int lpc_unregister_patch(struct lpc_patch *userpatch)
 {
-	struct lpc_patch *patch;
 	int ret = 0;
 
 	down(&lpc_mutex);
-	patch = lpc_find_patch(userpatch);
-	if (!patch) {
-		ret = -ENODEV;
-		goto out;
-	}
-	if (patch->state == ENABLED) {
-		ret = -EINVAL;
-		goto out;
-	}
-	lpc_free_patch(patch);
-out:
+	lpc_free_patch(userpatch);
 	up(&lpc_mutex);
+
 	return ret;
 }
-EXPORT_SYMBOL_GPL(lp_unregister_patch);
+EXPORT_SYMBOL_GPL(lpc_unregister_patch);
 
 /************************************
  * entry/exit
@@ -994,7 +737,7 @@ static int lpc_init(void)
 {
 	int ret;
 
-	ret = register_module_notifier(&lp_module_nb);
+	ret = register_module_notifier(&lpc_module_nb);
 	if (ret)
 		return ret;
 
@@ -1004,14 +747,14 @@ static int lpc_init(void)
 
 	return 0;
 unregister:
-	unregister_module_notifier(&lp_module_nb);
+	unregister_module_notifier(&lpc_module_nb);
 	return ret;
 }
 
 static void lpc_exit(void)
 {
 	lpc_remove_root_kobj();
-	unregister_module_notifier(&lp_module_nb);
+	unregister_module_notifier(&lpc_module_nb);
 }
 
 module_init(lpc_init);
Signature · 1 line
2.1.2
#41 lore raw

Re: [PATCH 2/2] kernel: add support for live patching

Quoted 93 lines · Miroslav Benes
Quoted 42 lines · Seth Jennings
Quoted 19 lines · Miroslav Benes

Hi,

thank you for the first version of the united live patching core.

The patch below implements some of our review objections. Changes are described in the commit log. It simplifies the hierarchy of data structures, removes data duplication (lp_ and lpc_ structures) and simplifies sysfs directory.

I did not try to repair other stuff (races, function names, function prefix, api symmetry etc.). It should serve as a demonstration of our point of view.

There are some problems with this. try_module_get and module_put may be called several times for each kernel module where some function is patched in. This should be fixed with module going notifier as suggested by Petr.

Quoted 5 lines

The modified core was tested with modified testing live patch originally from Seth's github. It worked as expected.

Please take a look at these changes, so we can discuss them in more detail.

Thanks Miroslav.

The functional changes are a little hard to break out from the formatting changes like s/disable/unregister and s/lp_/lpc_/ or adding LPC_ prefix to the enum, most (all?) of which I have included for v2.

A problem with getting rid of the object layer is that there are operations we do that are object-level operations. For example, module lookup and deferred module patching. Also, the dynamic relocations need to be associated with an object, not a patch, as not all relocations will be able to be applied at patch load time for patches that apply to modules that aren't loaded. I understand that you can walk the patch-level dynrela table and skip dynrela entries that don't match the target object, but why do that when you can cleanly express the relationship with a data structure hierarchy?

Quoted 4 lines

One example is the call to is_object_loaded() (renamed and reworked in v2 btw) per function rather than per object. That is duplicate work and information that could be more cleanly expressed through an object layer.

I understand your arguments as I had thought about this before. It is true that some operations which are connected with the object level could be duplicated in our approach. However the list of patched functions and especially objects (vmlinux or modules) would be always relatively short. Two-level hierarchy (functions and patches) is in my opinion more compact and easier to maintain. I do not think that object-level outweighs this. Let us think about it some more...

Quoted 11 lines

I also understand that sysfs/kobject stuff adds code length. However, the new "funcs" attribute is procfs style, not sysfs style. sysfs attribute should convey _one_ value.

Quoted 1 line

From Documenation/filesystems/sysfs.txt:

==========
Attributes should be ASCII text files, preferably with only one value per file. It is noted that it may not be efficient to contain only one value per file, so it is socially acceptable to express an array of values of the same type.

Quoted 4 lines

Mixing types, expressing multiple lines of data, and doing fancy formatting of data is heavily frowned upon. Doing these things may get you publicly humiliated and your code rewritten without notice.
==========

Ah, you are right. My mistake. Thank you.

Quoted 4 lines

Also the function list would have object ambiguity. If there was a patched function my_func() in both vmlinux and a module, it would just appear on the list twice. You can fix this by using the mod:func syntax like kallsyms, but it isn't as clean as expressing it in a hierarchy.

Yes, using mod:func or check against module name (and not only function name) would be necessary. Ambiguity would be also the problem in sysfs directory tree without object level. But it is doable.

Quoted 8 lines

As far as the unification of the API structures with the internal structures I have two points. First is that, IMHO, we should assume that the structures coming from the user are const. In kpatch, for example, we pass through some structures that are not created in the code, but by the patch generation tool and stored in an ELF section (read-only). Additionally, I am really against exposing the internal fields. Commenting them as "internal" is just messy and we have to change the .h file every time when want to add a field for internal use.

Quoted 14 lines

Changing the header file and thus API between different kernel releases is not a problem in my opinion. First live patching module would be created against specific kernel version (so the correct API is known). Second we would like to add userspace tool for automatic patch generation to upstream sometime in the future. API would be of "no importance" there as the situation in perf now (if I understand it correctly).

Quoted 7 lines

It seems that the primary purpose of this patch is to reduce the lines of code. However, I think that the object layer of the data structure cleanly expresses the object<->function relationship and makes code like the deferred patching much more straightforward since you already have the functions/dynrelas organized by object. You don't have to do the nasty "if (strcmp(func->obj_name, objname)) continue;" business over the entire patch every time.

I think that it was not primary about the number of lines but about the complexity.

Quoted 2 lines

The primary purpose was to show our point of view. I do not pretend that there are no problems, but there are also some benefits.

As discussed above, there were three main points where we had different opinion.

1. The sysfs structure. It has to stay more complex as proposed in the original patch. We have somehow missed the rule about one value per file.

Well, we might want to show the status instead of the addresses. Note that particular functions might be obsolete by another patch and it would be nice to show this in the sysfs tree.

2. The three level structure (patch -> object -> func) vs. the two level one (patch -> func). I have personally neutral feeling about it.

The tree level structure looks cleaner, for example the problem with functions of the same name in different modules. It allows to optimize the structures and some operations, for example, it reduces the number of checks for loaded module,
the functions for coming and going module are at one place.

But it creates more spaghetti code, for example, the call create_patch() -> create_objects() -> create_object() -> create_funcs -> create_func(). Also the more structures create more complex abstraction that might be harder to keep in head, understand, and operate with.

I think that we should compare the number of operations with all functions or just with the object. Then we could see if it is really worth it.

Also we might take inspiration in some similar existing stuff, for example with ftrace, kprobe, kallsyms.

3. The separation of public and internal structures. I would really like to avoid the duplication.

If I get it correctly that main motivation is the API stability but I think that it is not a real problem here.

The separation creates extra code (copying). Also it creates a confusion because there are many variables and functions of the same name that differ only in one character (lp vs lpc).

From my point of view, this is not worth having.

Quoted 3 lines
Quoted 3 lines

Be advised, I have also done away with the new_addr/old_addr attributes for v2 and replaced the patched module ref'ing with a combination of a GOING notifier with lpc_mutex for protection.

Great.

Quoted 3 lines

Great. I'll wait for v2 and resend our patch based on that, rebased and split to several patches. We can continue the discussion afterwards. Is it ok?

Sounds like a good plan.

Best Regards,
Petr

#43 lore raw
Quoted 7 lines · Christoph Hellwig
Quoted 2 lines · Seth Jennings

An example patch module can be found here:
https://github.com/spartacus06/livepatch/blob/master/patch/patch.c

Please include the generator for this patch in the kernel tree. Providing interfaces for out of tree modules (or generators) is not how the kernel internal APIs work.

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

So I believe including one or more human produced examples makes most sense.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#44 lore raw
Quoted 5 lines · Vojtech Pavlik

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

#45 lore raw
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Vojtech Pavlik

I don't think this specific example was generated.

So there are two ways to use this live patching API: using a generated module (e.g., using the kpatch-build tool) or manually compiling a module via kbuild.

Vojtech's right, the provided example was not generated. Maybe it belongs in samples/livepatch?

Quoted 9 lines
Quoted 4 lines

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

I agree that we should also put kpatch-build (or some converged kpatch/kGraft-build tool) into the kernel tree, because of the tight interdependencies between it and the kernel. I think it would make development much easier. Otherwise, for example, it may end up having a lot of #ifdef hacks based on what kernel version it's targeting.

Signature · 1 line
Josh
#46 lore raw
Quoted 11 lines · On Thu, 6 Nov 2014 13:34:33 -0600 Josh Poimboeuf <[email protected]> wrote:
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Vojtech Pavlik

I don't think this specific example was generated.

So there are two ways to use this live patching API: using a generated module (e.g., using the kpatch-build tool) or manually compiling a module via kbuild.

Vojtech's right, the provided example was not generated. Maybe it belongs in samples/livepatch?

I understand that there is two methods in doing this. Is it possible to create a "simple generator" that only does the simple case. Perhaps can detect non simple cases where it rejects the change and tells the user they need to reboot.

Something that isn't really related to either kpatch or kGraft, but can be used for testing purposes?

-- Steve

#47 lore raw
Quoted 15 lines · Steven Rostedt
Quoted 11 lines · On Thu, 6 Nov 2014 13:34:33 -0600 Josh Poimboeuf <[email protected]> wrote:
Quoted 2 lines · Christoph Hellwig
Quoted 1 line · Vojtech Pavlik

I don't think this specific example was generated.

So there are two ways to use this live patching API: using a generated module (e.g., using the kpatch-build tool) or manually compiling a module via kbuild.

Vojtech's right, the provided example was not generated. Maybe it belongs in samples/livepatch?

Quoted 7 lines

I understand that there is two methods in doing this. Is it possible to create a "simple generator" that only does the simple case. Perhaps can detect non simple cases where it rejects the change and tells the user they need to reboot.

Something that isn't really related to either kpatch or kGraft, but can be used for testing purposes?

For basic testing, a generator isn't needed. You can just use kbuild to compile a kmod from a human-created source file, a la kGraft. For example:

https://github.com/spartacus06/livepatch/blob/master/patch/patch.c

Signature · 1 line
Josh
#48 lore raw
In reply to Steven Rostedt
Quoted 4 lines · Steven Rostedt

I understand that there is two methods in doing this. Is it possible to create a "simple generator" that only does the simple case. Perhaps can detect non simple cases where it rejects the change and tells the user they need to reboot.

Especially the complicated case needs to be in tree, otherwise we're almost guaranteed to break it constantly.

#49 lore raw
In reply to Josh Poimboeuf
Quoted 5 lines · Josh Poimboeuf

I agree that we should also put kpatch-build (or some converged kpatch/kGraft-build tool) into the kernel tree, because of the tight interdependencies between it and the kernel. I think it would make development much easier. Otherwise, for example, it may end up having a lot of #ifdef hacks based on what kernel version it's targeting.

Exactly.

#50 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Vojtech Pavlik

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

A generator is not required for using the infrastructure and is merely a means of preparing the live patch with less effort.

I'm not opposed at all to adding the generator(s) eventually.

However, given that their use is optional, I would prefer not to have to wait on finishing and cleaning up the generator(s) to include of the in-kernel live patching infrastructure.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#51 lore raw
Quoted 5 lines · Vojtech Pavlik

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

So agree on one method and get it into shape, just like we do for other kernel subsystems.

#52 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 5 lines · Vojtech Pavlik

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

So agree on one method and get it into shape, just like we do for other kernel subsystems.

That's our goal (and it sounds like everybody's on board with that).

But we have two different implementations, all the way up the stack. I think we have to work on combining and stabilizing one thing at a time. We want to do this in stages:

1. Define a common core module with an API that can be used by all three approaches (manual, kpatch generator, kGraft generator)

2. Add consistency model(s) (e.g. kpatch stop_machine, kGraft per-task consistency, Masami's per task ref counting)

3. Add combined patch module generation tool

Signature · 1 line
Josh
#53 lore raw
Quoted 2 lines · Josh Poimboeuf

2. Add consistency model(s) (e.g. kpatch stop_machine, kGraft per-task consistency, Masami's per task ref counting)

I have given some thought to the consistency models and how they differ and how they potentially could be unified.

I have to thank Masami, because his rewrite of the kpatch model based on refcounting is what brought it closer to the kGraft model and thus allowed me to find the parallels.

Let me start by defining the properties of the patching consistency model. First, what entity the execution must be outside of to be able to make the switch, ordered from weakest to strongest:

LEAVE_FUNCTION
- execution has to leave a patched function to switch to the new implementation

LEAVE_PATCHED_SET
- execution has to leave the set of patched functions to switch to the new implementation

LEAVE_KERNEL
- execution has to leave the entire kernel to switch to the new implementation

Then, what entity the switch happens for. Again, from weakest to strongest:

SWITCH_FUNCTION
- the switch to the new implementation happens on a per-function basis

SWITCH_THREAD
- the switch to the new implementation is per-thread.

SWITCH_KERNEL
- the switch to the new implementation happens at once for the whole kernel

Now with those definitions:

livepatch (null model), as is, is LEAVE_FUNCTION and SWITCH_FUNCTION

kpatch, masami-refcounting and Ksplice are LEAVE_PATCHED_SET and SWITCH_KERNEL

kGraft is LEAVE_KERNEL and SWITCH_THREAD

CRIU/kexec is LEAVE_KERNEL and SWITCH_KERNEL

By blending kGraft and masami-refcounting, we could create a consistency engine capable of almost any combination of these properties and thus all the consistency models.

However, I'm currently thinking that the most interesting model is LEAVE_PATCHED_SET and SWITCH_THREAD, as it is reliable, fast converging, doesn't require annotating kernel threads nor fails with frequent sleepers like futexes.

It provides the least consistency that is required to be able to change the calling convention of functions and still allows for semantic dependencies.

What do you think?

----------------------------------------------------------------------------

PS.: Livepatch's null model isn't in fact the weakest possible, as it still guarantees executing complete intact functions, this thanks to ftrace. That is much more than what would direct overwriting of the function in memory achieve.

This is also the reason why Ksplice is locked to a very specific consistency model. Ksplice can patch only when the kernel is stopped and the model is built from that.

masami-refcounting, kpatch, kGraft, livepatch have a lot more freedom, thanks to ftrace, into what the consistency model should look like.

PPS.: I haven't included any handling of changed data structures in this, that's another set of properties.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#54 lore raw
Quoted 43 lines · Vojtech Pavlik
Quoted 2 lines · Josh Poimboeuf

2. Add consistency model(s) (e.g. kpatch stop_machine, kGraft per-task consistency, Masami's per task ref counting)

I have given some thought to the consistency models and how they differ and how they potentially could be unified.

I have to thank Masami, because his rewrite of the kpatch model based on refcounting is what brought it closer to the kGraft model and thus allowed me to find the parallels.

Let me start by defining the properties of the patching consistency model. First, what entity the execution must be outside of to be able to make the switch, ordered from weakest to strongest:

LEAVE_FUNCTION
- execution has to leave a patched function to switch to the new implementation

LEAVE_PATCHED_SET
- execution has to leave the set of patched functions to switch to the new implementation

LEAVE_KERNEL
- execution has to leave the entire kernel to switch to the new implementation

Then, what entity the switch happens for. Again, from weakest to strongest:

SWITCH_FUNCTION
- the switch to the new implementation happens on a per-function basis

SWITCH_THREAD
- the switch to the new implementation is per-thread.

SWITCH_KERNEL
- the switch to the new implementation happens at once for the whole kernel

Now with those definitions:

Quoted 7 lines

livepatch (null model), as is, is LEAVE_FUNCTION and SWITCH_FUNCTION

kpatch, masami-refcounting and Ksplice are LEAVE_PATCHED_SET and SWITCH_KERNEL

kGraft is LEAVE_KERNEL and SWITCH_THREAD

CRIU/kexec is LEAVE_KERNEL and SWITCH_KERNEL

Thanks, nice analysis!

Quoted 3 lines

By blending kGraft and masami-refcounting, we could create a consistency engine capable of almost any combination of these properties and thus all the consistency models.

Can you elaborate on what this would look like?

Quoted 11 lines

However, I'm currently thinking that the most interesting model is LEAVE_PATCHED_SET and SWITCH_THREAD, as it is reliable, fast converging, doesn't require annotating kernel threads nor fails with frequent sleepers like futexes.

It provides the least consistency that is required to be able to change the calling convention of functions and still allows for semantic dependencies.

What do you think?

The big problem with SWITCH_THREAD is that it adds the possibility that old functions can run simultaneously with new ones. When you change data or data semantics, which is roughly 10% of security patches, it creates some serious headaches:

- It makes patch safety analysis much harder by doubling the number of permutations of scenarios you have to consider. In addition to considering newfunc/olddata and newfunc/newdata, you also have to consider oldfunc/olddata and oldfunc/newdata.

- It requires two patches instead of one. The first patch is needed to modify the old functions to be able to deal with new data. After the first patch has been fully applied, then you apply the second patch which can start creating new versions of data.

On the other hand, SWITCH_KERNEL doesn't have those problems. It does have the problem you mentioned, roughly 2% of the time, where it can't patch functions which are always in use. But in that case we can skip the backtrace check ~90% of the time. So it's really maybe something like 0.2% of patches which can't be patched with SWITCH_KERNEL. But even then I think we could overcome that by getting creative, e.g. using the multiple patch approach.

So my perspective is that SWITCH_THREAD causes big headaches 10% of the time, whereas SWITCH_KERNEL causes small headaches 1.8% of the time, and big headaches 0.2% of the time :-)

Quoted 20 lines

----------------------------------------------------------------------------

PS.: Livepatch's null model isn't in fact the weakest possible, as it still guarantees executing complete intact functions, this thanks to ftrace. That is much more than what would direct overwriting of the function in memory achieve.

This is also the reason why Ksplice is locked to a very specific consistency model. Ksplice can patch only when the kernel is stopped and the model is built from that.

masami-refcounting, kpatch, kGraft, livepatch have a lot more freedom, thanks to ftrace, into what the consistency model should look like.

PPS.: I haven't included any handling of changed data structures in this, that's another set of properties.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
Signature · 1 line
Josh
#55 lore raw
Quoted 18 lines · Josh Poimboeuf
Quoted 10 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

Now with those definitions:

Quoted 7 lines

livepatch (null model), as is, is LEAVE_FUNCTION and SWITCH_FUNCTION

kpatch, masami-refcounting and Ksplice are LEAVE_PATCHED_SET and SWITCH_KERNEL

kGraft is LEAVE_KERNEL and SWITCH_THREAD

CRIU/kexec is LEAVE_KERNEL and SWITCH_KERNEL

Quoted 7 lines

Thanks, nice analysis!

Quoted 3 lines

By blending kGraft and masami-refcounting, we could create a consistency engine capable of almost any combination of these properties and thus all the consistency models.

Can you elaborate on what this would look like?

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

A patch would have flags which specify a combination of the above properties that are needed for successful patching of that specific patch.

Quoted 10 lines

The big problem with SWITCH_THREAD is that it adds the possibility that old functions can run simultaneously with new ones. When you change data or data semantics, which is roughly 10% of security patches, it creates some serious headaches:

- It makes patch safety analysis much harder by doubling the number of permutations of scenarios you have to consider. In addition to considering newfunc/olddata and newfunc/newdata, you also have to consider oldfunc/olddata and oldfunc/newdata.

Quoted 4 lines

- It requires two patches instead of one. The first patch is needed to modify the old functions to be able to deal with new data. After the first patch has been fully applied, then you apply the second patch which can start creating new versions of data.

For data layout an semantic changes, there are two approaches:

1) TRANSFORM_WORLD

Stop the world, transform everything, resume. This is what Ksplice does and what could work for kpatch, would be rather interesting (but possible) for masami-refcounting and doesn't work at all for the per-thread kGraft.

It allows to deallocate structures, allocate new ones, basically rebuild the data structures of the kernel. No shadowing or using of padding is needed.

The nice part is that the patch can stay pretty much the original patch that fixes the bug when applied to normal kernel sources.

The most tricky part with this approach is writing the additional transformation code. Finding all instances of a changed data structure. It fails if only semantics are changed, but that is easily fixed by making sure there is always a layout change for any semantic change. All instances of a specific data structure can be found, worst case with some compiler help: No function can have pointers or instances of the structure on the stack, or registers, as that would include it in the patched set. So all have to be either global, or referenced by a globally-rooted tree, linked list or any other structure.

This one is also possible to revert, if a reverse-transforming function is provided.

masami-refcounting can be made to work with this by spinning in every function entry ftrace/kprobe callback after a universe flip and calling stop_kernel from the function exit callback that flipped the switch.

2) TRANSFORM_ON_ACCESS

This requires structure versioning and/or shadowing. All 'new' functions are written with this in mind and can both handle the old and new data formats and transform the data to the new format. When universe transition is completed for the whole system, a single flag is flipped for the functions to start transforming.

The advantage is to not have to look up every single instance of the structure and not having to make sure you found them all.

The disadvantages are that the patch now looks very different to what goes into the kernel sources, that you never know whether the conversion is complete and reverting the patch is tough, although can be helped by keeping track of transformed functions at a cost of maintaining another data structure for that.

It works with any of the approaches (except null model) and while it needs two steps (patch, then enable conversion), it doesn't require two rounds of patching. Also, you don't have to consider oldfunc/newdata as that will never happen. oldfunc/olddata obviously works, so you only have to look at newfunc/olddata and newfunc/newdata as the transformation goes on.

I don't see either of these as really that much simpler. But I do see value in offering both.

Quoted 4 lines

On the other hand, SWITCH_KERNEL doesn't have those problems. It does have the problem you mentioned, roughly 2% of the time, where it can't patch functions which are always in use. But in that case we can skip the backtrace check ~90% of the time.

An interesting bit is that when you skip the backtrace check you're actually reverting to LEAVE_FUNCION SWITCH_FUNCTION, forfeiting all consistency and not LEAVE_FUNCTION SWITCH_KERNEL as one would expect.

Hence for those 2% of cases (going with your number, because it's a guess anyway) LEAVE_PATCHED_SET SWITCH_THREAD would in fact be a safer option.

Quoted 8 lines

So it's really maybe something
like 0.2% of patches which can't be patched with SWITCH_KERNEL. But even then I think we could overcome that by getting creative, e.g. using the multiple patch approach.

So my perspective is that SWITCH_THREAD causes big headaches 10% of the time, whereas SWITCH_KERNEL causes small headaches 1.8% of the time, and big headaches 0.2% of the time :-)

My preferred way would be to go with SWITCH_THREAD for the simpler stuff and do a SWITCH_KERNEL for the 10% of complex patches. This because (LEAVE_PATCHED_SET) SWITCH_THREAD finishes much quicker. But I'm biased there. ;)

It seems more and more to me that we will actually want the more powerful engine coping with the various options.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#56 lore raw
Quoted 35 lines · Vojtech Pavlik
Quoted 18 lines · Josh Poimboeuf
Quoted 10 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

Now with those definitions:

Quoted 7 lines

livepatch (null model), as is, is LEAVE_FUNCTION and SWITCH_FUNCTION

kpatch, masami-refcounting and Ksplice are LEAVE_PATCHED_SET and SWITCH_KERNEL

kGraft is LEAVE_KERNEL and SWITCH_THREAD

CRIU/kexec is LEAVE_KERNEL and SWITCH_KERNEL

Quoted 7 lines

Thanks, nice analysis!

Quoted 3 lines

By blending kGraft and masami-refcounting, we could create a consistency engine capable of almost any combination of these properties and thus all the consistency models.

Can you elaborate on what this would look like?

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Quoted 3 lines

A patch would have flags which specify a combination of the above properties that are needed for successful patching of that specific patch.

Would it really be necessary to support all possible permutations? I think that would add a lot of complexity to the code. Especially if we have to support LEAVE_KERNEL, which adds a lot of interdependencies with the rest of the kernel (kthreads, syscall, irq, etc).

It seems to me that the two most interesting options are:
- LEAVE_PATCHED_SET + SWITCH_THREAD (Masami-kGraft)
- LEAVE_PATCHED_SET + SWITCH_KERNEL (kpatch and/or Masami-kpatch)

I do see some appeal for the choose-your-own-consistency-model idea. But it wouldn't be practical for cumulative patch upgrades, which I think we both agreed at LPC seems to be safer than incremental patching. If you only ever have one patch module loaded at any given time, you only get one consistency model anyway.

In order for multiple consistency models to be practical, I think we'd need to figure out how to make incremental patching safe.

Quoted 42 lines
Quoted 10 lines

The big problem with SWITCH_THREAD is that it adds the possibility that old functions can run simultaneously with new ones. When you change data or data semantics, which is roughly 10% of security patches, it creates some serious headaches:

- It makes patch safety analysis much harder by doubling the number of permutations of scenarios you have to consider. In addition to considering newfunc/olddata and newfunc/newdata, you also have to consider oldfunc/olddata and oldfunc/newdata.

Quoted 4 lines

- It requires two patches instead of one. The first patch is needed to modify the old functions to be able to deal with new data. After the first patch has been fully applied, then you apply the second patch which can start creating new versions of data.

For data layout an semantic changes, there are two approaches:

1) TRANSFORM_WORLD

Stop the world, transform everything, resume. This is what Ksplice does and what could work for kpatch, would be rather interesting (but possible) for masami-refcounting and doesn't work at all for the per-thread kGraft.

It allows to deallocate structures, allocate new ones, basically rebuild the data structures of the kernel. No shadowing or using of padding is needed.

The nice part is that the patch can stay pretty much the original patch that fixes the bug when applied to normal kernel sources.

The most tricky part with this approach is writing the additional transformation code. Finding all instances of a changed data structure. It fails if only semantics are changed, but that is easily fixed by making sure there is always a layout change for any semantic change. All instances of a specific data structure can be found, worst case with some compiler help: No function can have pointers or instances of the structure on the stack, or registers, as that would include it in the patched set. So all have to be either global, or referenced by a globally-rooted tree, linked list or any other structure.

Quoted 6 lines

This one is also possible to revert, if a reverse-transforming function is provided.

masami-refcounting can be made to work with this by spinning in every function entry ftrace/kprobe callback after a universe flip and calling stop_kernel from the function exit callback that flipped the switch.

I'm kind of surprised to hear that Ksplice does this. I had considered this approach, but it sounds really tricky, if not impossible in many cases.

Ahem, this would be an opportune time for a Ksplice person to chime in with their experiences...

Quoted 7 lines

2) TRANSFORM_ON_ACCESS

This requires structure versioning and/or shadowing. All 'new' functions are written with this in mind and can both handle the old and new data formats and transform the data to the new format. When universe transition is completed for the whole system, a single flag is flipped for the functions to start transforming.

This is a variation on what we've been doing with kpatch, using shadow data fields to add data and/or version metadata to structures, with a few differences:

First, we haven't been transforming existing data. All existing data structures stay at v1. Only new data structures are created as v2. I suppose it depends on the nature of the patch as to whether it's safe to convert existing data.

Second, we don't need to flip a flag, because with SWITCH_KERNEL the system universe transition happens instantly.

Quoted 5 lines

The advantage is to not have to look up every single instance of the structure and not having to make sure you found them all.

The disadvantages are that the patch now looks very different to what goes into the kernel sources,

In my experience, s/very different/slightly different/.

Quoted 4 lines

that you never know whether the conversion
is complete and reverting the patch is tough, although can be helped by keeping track of transformed functions at a cost of maintaining another data structure for that.

True, and we might even want to prevent or discourage such patches from being disabled somehow.

Quoted 6 lines

It works with any of the approaches (except null model) and while it needs two steps (patch, then enable conversion), it doesn't require two rounds of patching. Also, you don't have to consider oldfunc/newdata as that will never happen. oldfunc/olddata obviously works, so you only have to look at newfunc/olddata and newfunc/newdata as the transformation goes on.

Makes sense, I hadn't thought of the flag flipping approach for SWITCH_THREAD. It's definitely a lot better than requiring an intermediate patch.

Quoted 11 lines

I don't see either of these as really that much simpler. But I do see value in offering both.

Quoted 4 lines

On the other hand, SWITCH_KERNEL doesn't have those problems. It does have the problem you mentioned, roughly 2% of the time, where it can't patch functions which are always in use. But in that case we can skip the backtrace check ~90% of the time.

An interesting bit is that when you skip the backtrace check you're actually reverting to LEAVE_FUNCION SWITCH_FUNCTION, forfeiting all consistency and not LEAVE_FUNCTION SWITCH_KERNEL as one would expect.

Hm, if we used stop machine (or ref counting), but without the backtrace check, wouldn't it be LEAVE_FUNCTION SWITCH_KERNEL?

Quoted 9 lines

Hence for those 2% of cases (going with your number, because it's a guess anyway) LEAVE_PATCHED_SET SWITCH_THREAD would in fact be a safer option.

Quoted 5 lines

So it's really maybe something
like 0.2% of patches which can't be patched with SWITCH_KERNEL. But even then I think we could overcome that by getting creative, e.g. using the multiple patch approach.

Quoted 6 lines
Quoted 3 lines

So my perspective is that SWITCH_THREAD causes big headaches 10% of the time, whereas SWITCH_KERNEL causes small headaches 1.8% of the time, and big headaches 0.2% of the time :-)

My preferred way would be to go with SWITCH_THREAD for the simpler stuff and do a SWITCH_KERNEL for the 10% of complex patches.

You said above that neither SWITCH_KERNEL nor SWITCH_THREAD is much simpler than the other for the 10% case. So why would you use SWITCH_KERNEL here?

Quoted 2 lines

This because (LEAVE_PATCHED_SET) SWITCH_THREAD finishes much quicker. But I'm biased there. ;)

Why would LEAVE_PATCHED_SET SWITCH_THREAD finish much quicker than LEAVE_PATCHED_SET SWITCH_KERNEL? Wouldn't they be about the same?

Signature · 1 line
Josh
#57 lore raw
Quoted 24 lines · Josh Poimboeuf
Quoted 19 lines · Vojtech Pavlik
Quoted 8 lines · Josh Poimboeuf
Quoted 8 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Quoted 3 lines

A patch would have flags which specify a combination of the above properties that are needed for successful patching of that specific patch.

Quoted 8 lines

Would it really be necessary to support all possible permutations? I think that would add a lot of complexity to the code. Especially if we have to support LEAVE_KERNEL, which adds a lot of interdependencies with the rest of the kernel (kthreads, syscall, irq, etc).

It seems to me that the two most interesting options are:
- LEAVE_PATCHED_SET + SWITCH_THREAD (Masami-kGraft)
- LEAVE_PATCHED_SET + SWITCH_KERNEL (kpatch and/or Masami-kpatch)

I agree here.

In fact LEAVE_KERNEL can be approximated by extending the patched set as required to include functions which are not changed per se, but are "contaminated" by propagation of semantic changes in the calling convention, and/or data format.

This applies to cases like this (needs LEAVE_KERNEL or extending patched set beyond changed functions):

-----------------------------------------------------------

	int bar() {
		[...]
-		return x;
+		return x + 1;
	}

	foo() {
		int ret = bar();
		do {
			wait_interruptible();
		} while (ret == bar());
	}

-----------------------------------------------------------

Or like this (needs SWITCH_KERNEL so won't work with kGraft, but also extending patched set, will not work with kpatch as it stands today):

-----------------------------------------------------------

	void lock_a()
	{
-		spin_lock(&x);
+		spin_lock(&y);
	}
	void lock_b()
	{
-		spin_lock(&y);
+		spin_lock(&x);
	}
	void unlock_a()
	}
-		spin_unlock(&x);
+		spin_unlock(&y);
	}
	void unlock_b()
	{
-		spin_unlock(&y);
+		spin_unlock(&x);
	}

	void foo()
	{
		lock_a();
		lock_b();
		[...]
		unlock_b();
		unlock_a();
	}

-----------------------------------------------------------

So once we can extend the patched set as needed (manually, after review), we can handle all the cases that LEAVE_KERNEL offers, making LEAVE_KERNEL unnecessary.

It'd be nice if we wouldn't require actually patching those functions, only include them in the set we have to leave to proceed.

The remaining question is the performance impact of using a large set with refcounting. LEAVE_KERNEL's impact as implemented in kGraft is beyond being measurable, it's about 16 added instructions for each thread that get executed.

Quoted 8 lines

I do see some appeal for the choose-your-own-consistency-model idea. But it wouldn't be practical for cumulative patch upgrades, which I think we both agreed at LPC seems to be safer than incremental patching. If you only ever have one patch module loaded at any given time, you only get one consistency model anyway.

In order for multiple consistency models to be practical, I think we'd need to figure out how to make incremental patching safe.

I believe we have to get incremental patching working anyway as it is a valid usecase for many users, just not for major distributions.

And we may want to take a look at how to mark parts of a cumulative patch with different consistency models, when we combine eg. the recent futex CVE patch (not working with SWITCH_KERNEL) and anything requiring SWITCH kernel.

Quoted 7 lines
Quoted 3 lines

For data layout an semantic changes, there are two approaches:

1) TRANSFORM_WORLD

I'm kind of surprised to hear that Ksplice does this. I had considered this approach, but it sounds really tricky, if not impossible in many cases.

By the way, the ability to do this is basically the only advantage of actually stopping the kernel.

Quoted 2 lines

Ahem, this would be an opportune time for a Ksplice person to chime in with their experiences...

Indeed. :)

Quoted 10 lines
Quoted 1 line

2) TRANSFORM_ON_ACCESS

This is a variation on what we've been doing with kpatch, using shadow data fields to add data and/or version metadata to structures, with a few differences:

First, we haven't been transforming existing data. All existing data structures stay at v1. Only new data structures are created as v2. I suppose it depends on the nature of the patch as to whether it's safe to convert existing data.

Also it depends on whether existing data do contain enough information to actually avoid the security issue the patch is fixing. You may want to transform the data structures as soon as possible.

Quoted 2 lines

Second, we don't need to flip a flag, because with SWITCH_KERNEL the system universe transition happens instantly.

Indeed. I wanted to point out that it works even with the SWITCH_THREAD and using the patching_complete flag that already is used in kGraft for other purposes.

Quoted 7 lines
Quoted 5 lines

The advantage is to not have to look up every single instance of the structure and not having to make sure you found them all.

The disadvantages are that the patch now looks very different to what goes into the kernel sources,

In my experience, s/very different/slightly different/.

Not the same, anyway. :)

Quoted 7 lines
Quoted 4 lines

that you never know whether the conversion is complete and reverting the patch is tough, although can be helped by keeping track of transformed functions at a cost of maintaining another data structure for that.

True, and we might even want to prevent or discourage such patches from being disabled somehow.

Yes.

Quoted 6 lines
Quoted 3 lines

An interesting bit is that when you skip the backtrace check you're actually reverting to LEAVE_FUNCION SWITCH_FUNCTION, forfeiting all consistency and not LEAVE_FUNCTION SWITCH_KERNEL as one would expect.

Hm, if we used stop machine (or ref counting), but without the backtrace check, wouldn't it be LEAVE_FUNCTION SWITCH_KERNEL?

No; if you don't check the backtrace, any of the patched functions can be on the stack and old code can still be executed after you resume the kernel again. Look at this:

--------------------------------------------------------------

	baz() {
	}
	bar() {
-		[...]
+		[...]
		baz();
-		[...]
+		[...]
	}
	foo() {
		bar();
	}

--------------------------------------------------------------

Now we stop_kernel on baz(). We don't check the stack. We patch bar(). We resume, and baz() happily returns into bar(), executing old code. At the same time, another CPU can call bar(), getting new code.

Stack checking at stop_kernel() time is required to keep the SWITCH_KERNEL part of the model.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

So my perspective is that SWITCH_THREAD causes big headaches 10% of the time, whereas SWITCH_KERNEL causes small headaches 1.8% of the time, and big headaches 0.2% of the time :-)

My preferred way would be to go with SWITCH_THREAD for the simpler stuff and do a SWITCH_KERNEL for the 10% of complex patches.

You said above that neither SWITCH_KERNEL nor SWITCH_THREAD is much simpler than the other for the 10% case. So why would you use SWITCH_KERNEL here?

I think I was referring to the two data transformation methods and one not being simpler than the other.

And since we're both looking at the TRANSFORM_ON_ACCESS model, there isn't much of a difference using SWITCH_THREAD or SWITCH_KERNEL for the data format modification cases indeed. It works the same.

But there are a few (probably much less than 10%) cases like the locking one I used above, where SWITCH_THREAD just isn't going to cut it and for those I would need SWITCH_KERNEL or get very creative with refactoring the patch to do things differently.

Quoted 5 lines
Quoted 2 lines

This because (LEAVE_PATCHED_SET) SWITCH_THREAD finishes much quicker. But I'm biased there. ;)

Why would LEAVE_PATCHED_SET SWITCH_THREAD finish much quicker than LEAVE_PATCHED_SET SWITCH_KERNEL? Wouldn't they be about the same?

Because with LEAVE_PATCHED_SET SWITCH_THREAD you're waiting for each thread to leave the patched set and when they each have done that at least once, you're done. Even if some are already back within the set.

With LEAVE_PATCHED_SET SWITCH_KERNEL, you have to find the perfect moment when all of the threads are outside of the patched set at the same time. Depending on how often the functions are used and how large the set is, reaching that moment will get harder.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#58 lore raw
Quoted 60 lines · Vojtech Pavlik
Quoted 24 lines · Josh Poimboeuf
Quoted 19 lines · Vojtech Pavlik
Quoted 8 lines · Josh Poimboeuf
Quoted 8 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Quoted 3 lines

A patch would have flags which specify a combination of the above properties that are needed for successful patching of that specific patch.

Quoted 8 lines

Would it really be necessary to support all possible permutations? I think that would add a lot of complexity to the code. Especially if we have to support LEAVE_KERNEL, which adds a lot of interdependencies with the rest of the kernel (kthreads, syscall, irq, etc).

It seems to me that the two most interesting options are:
- LEAVE_PATCHED_SET + SWITCH_THREAD (Masami-kGraft)
- LEAVE_PATCHED_SET + SWITCH_KERNEL (kpatch and/or Masami-kpatch)

I agree here.

In fact LEAVE_KERNEL can be approximated by extending the patched set as required to include functions which are not changed per se, but are "contaminated" by propagation of semantic changes in the calling convention, and/or data format.

This applies to cases like this (needs LEAVE_KERNEL or extending patched set beyond changed functions):

-----------------------------------------------------------

	int bar() {
		[...]
(quoted diff) +1 -1
-		return x;
+		return x + 1;
	}

	foo() {
		int ret = bar();
		do {
			wait_interruptible();
		} while (ret == bar());
	}
Quoted 1 line

-----------------------------------------------------------

Agreed. Though I think this is quite rare anyway. Do you know of any real world examples of this pattern in the kernel?

Quoted 34 lines

Or like this (needs SWITCH_KERNEL so won't work with kGraft, but also extending patched set, will not work with kpatch as it stands today):

-----------------------------------------------------------

	void lock_a()
	{
(quoted diff) +1 -1
-		spin_lock(&x);
+		spin_lock(&y);
	}
	void lock_b()
	{
(quoted diff) +1 -1
-		spin_lock(&y);
+		spin_lock(&x);
	}
	void unlock_a()
	}
(quoted diff) +1 -1
-		spin_unlock(&x);
+		spin_unlock(&y);
	}
	void unlock_b()
	{
(quoted diff) +1 -1
-		spin_unlock(&y);
+		spin_unlock(&x);
	}

	void foo()
	{
		lock_a();
		lock_b();
		[...]
		unlock_b();
		unlock_a();
	}
Quoted 1 line

-----------------------------------------------------------

Another way to handle this type of locking semantic change for either kpatch or kGraft is to use shadow data to add a "v2" shadow field to the lock's containing struct, which is set whenever the struct is allocated in the new universe. Then you can use that field to determine which version of locking semantics to use.

Quoted 3 lines

So once we can extend the patched set as needed (manually, after review), we can handle all the cases that LEAVE_KERNEL offers, making LEAVE_KERNEL unnecessary.

Agreed.

Quoted 2 lines

It'd be nice if we wouldn't require actually patching those functions, only include them in the set we have to leave to proceed.

Maybe, let's wait and see how common this problem turns out to be :-)

Quoted 4 lines

The remaining question is the performance impact of using a large set with refcounting. LEAVE_KERNEL's impact as implemented in kGraft is beyond being measurable, it's about 16 added instructions for each thread that get executed.

Sure, but I don't see this being much of an issue.

Quoted 11 lines
Quoted 8 lines

I do see some appeal for the choose-your-own-consistency-model idea. But it wouldn't be practical for cumulative patch upgrades, which I think we both agreed at LPC seems to be safer than incremental patching. If you only ever have one patch module loaded at any given time, you only get one consistency model anyway.

In order for multiple consistency models to be practical, I think we'd need to figure out how to make incremental patching safe.

I believe we have to get incremental patching working anyway as it is a valid usecase for many users, just not for major distributions.

Well, we do already have it "working", but it's not safe enough for serious use because we aren't properly dealing with build and runtime dependencies between patch modules. It may be tricky to get right.

But yeah, if you're _very_ careful to analyze any dependencies between the patches, an occasional incremental patch might be do-able.

Quoted 4 lines

And we may want to take a look at how to mark parts of a cumulative patch with different consistency models, when we combine eg. the recent futex CVE patch (not working with SWITCH_KERNEL) and anything requiring SWITCH kernel.

Yeah, interesting idea. But again I think we'd still need to be very careful with the dependencies.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

For data layout an semantic changes, there are two approaches:

1) TRANSFORM_WORLD

I'm kind of surprised to hear that Ksplice does this. I had considered this approach, but it sounds really tricky, if not impossible in many cases.

By the way, the ability to do this is basically the only advantage of actually stopping the kernel.

I pretty much agree.

Quoted 11 lines
Quoted 2 lines

Ahem, this would be an opportune time for a Ksplice person to chime in with their experiences...

Indeed. :)

Quoted 6 lines
Quoted 1 line

2) TRANSFORM_ON_ACCESS

This is a variation on what we've been doing with kpatch, using shadow data fields to add data and/or version metadata to structures, with a few differences:

Quoted 8 lines
Quoted 4 lines

First, we haven't been transforming existing data. All existing data structures stay at v1. Only new data structures are created as v2. I suppose it depends on the nature of the patch as to whether it's safe to convert existing data.

Also it depends on whether existing data do contain enough information to actually avoid the security issue the patch is fixing. You may want to transform the data structures as soon as possible.

Agreed, it's something that needs to be considered on a per patch basis.

Quoted 53 lines
Quoted 2 lines

Second, we don't need to flip a flag, because with SWITCH_KERNEL the system universe transition happens instantly.

Indeed. I wanted to point out that it works even with the SWITCH_THREAD and using the patching_complete flag that already is used in kGraft for other purposes.

Quoted 7 lines
Quoted 5 lines

The advantage is to not have to look up every single instance of the structure and not having to make sure you found them all.

The disadvantages are that the patch now looks very different to what goes into the kernel sources,

In my experience, s/very different/slightly different/.

Not the same, anyway. :)

Quoted 7 lines
Quoted 4 lines

that you never know whether the conversion is complete and reverting the patch is tough, although can be helped by keeping track of transformed functions at a cost of maintaining another data structure for that.

True, and we might even want to prevent or discourage such patches from being disabled somehow.

Yes.

Quoted 6 lines
Quoted 3 lines

An interesting bit is that when you skip the backtrace check you're actually reverting to LEAVE_FUNCION SWITCH_FUNCTION, forfeiting all consistency and not LEAVE_FUNCTION SWITCH_KERNEL as one would expect.

Hm, if we used stop machine (or ref counting), but without the backtrace check, wouldn't it be LEAVE_FUNCTION SWITCH_KERNEL?

No; if you don't check the backtrace, any of the patched functions can be on the stack and old code can still be executed after you resume the kernel again. Look at this:

--------------------------------------------------------------

	baz() {
	}
	bar() {
(quoted diff) +1 -1
-		[...]
+		[...]
		baz();
(quoted diff) +1 -1
-		[...]
+		[...]
	}
	foo() {
		bar();
	}
Quoted 8 lines

--------------------------------------------------------------

Now we stop_kernel on baz(). We don't check the stack. We patch bar(). We resume, and baz() happily returns into bar(), executing old code. At the same time, another CPU can call bar(), getting new code.

Stack checking at stop_kernel() time is required to keep the SWITCH_KERNEL part of the model.

Ok, I think I get what you're saying. In my mind it's kind of a hybrid of SWITCH_KERNEL and SWITCH_FUNCTION, since SWITCH_KERNEL would still be used for other functions in the patch. In that case we'd be forfeiting consistency just for those skipped functions in the list.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

So my perspective is that SWITCH_THREAD causes big headaches 10% of the time, whereas SWITCH_KERNEL causes small headaches 1.8% of the time, and big headaches 0.2% of the time :-)

My preferred way would be to go with SWITCH_THREAD for the simpler stuff and do a SWITCH_KERNEL for the 10% of complex patches.

You said above that neither SWITCH_KERNEL nor SWITCH_THREAD is much simpler than the other for the 10% case. So why would you use SWITCH_KERNEL here?

Quoted 6 lines

I think I was referring to the two data transformation methods and one not being simpler than the other.

And since we're both looking at the TRANSFORM_ON_ACCESS model, there isn't much of a difference using SWITCH_THREAD or SWITCH_KERNEL for the data format modification cases indeed. It works the same.

Well, not exactly the same, since SWITCH_THREAD needs the flag for creation/transformation, but yeah, close enough :-)

Quoted 4 lines

But there are a few (probably much less than 10%) cases like the locking one I used above, where SWITCH_THREAD just isn't going to cut it and for those I would need SWITCH_KERNEL or get very creative with refactoring the patch to do things differently.

I'm not opposed to having both if necessary. But I think the code would be _much_ simpler if we could agree on a single consistency model that can be used in all cases. Plus there wouldn't be such a strong requirement to get incremental patching to work safely (which will add more complexity).

I actually agree with you that LEAVE_PATCHED_SET + SWITCH_THREAD is pretty nice.

So I'd like to hear more about cases where you think we _need_ SWITCH_KERNEL. As I mentioned above, I think many of those cases can be solved by using data structure versioning with shadow data fields.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

This because (LEAVE_PATCHED_SET) SWITCH_THREAD finishes much quicker. But I'm biased there. ;)

Why would LEAVE_PATCHED_SET SWITCH_THREAD finish much quicker than LEAVE_PATCHED_SET SWITCH_KERNEL? Wouldn't they be about the same?

Because with LEAVE_PATCHED_SET SWITCH_THREAD you're waiting for each thread to leave the patched set and when they each have done that at least once, you're done. Even if some are already back within the set.

Ok, so you're talking about the case when you're trying to patch a function which is always active. Agreed :-)

Quoted 4 lines

With LEAVE_PATCHED_SET SWITCH_KERNEL, you have to find the perfect moment when all of the threads are outside of the patched set at the same time. Depending on how often the functions are used and how large the set is, reaching that moment will get harder.

Yeah, I think this is the biggest drawback of SWITCH_KERNEL. More likely to fail (or never succeed).

Signature · 1 line
Josh
#59 lore raw
Quoted 23 lines · Josh Poimboeuf
Quoted 23 lines

In fact LEAVE_KERNEL can be approximated by extending the patched set as required to include functions which are not changed per se, but are "contaminated" by propagation of semantic changes in the calling convention, and/or data format.

This applies to cases like this (needs LEAVE_KERNEL or extending patched set beyond changed functions):

-----------------------------------------------------------

	int bar() {
		[...]
(quoted diff) +1 -1
-		return x;
+		return x + 1;
	}

	foo() {
		int ret = bar();
		do {
			wait_interruptible();
		} while (ret == bar());
	}
Quoted 4 lines
Quoted 1 line

-----------------------------------------------------------

Agreed. Though I think this is quite rare anyway. Do you know of any real world examples of this pattern in the kernel?

No, I do not. All of the examples I presented are entirely synthetic corner cases designed to show the weaknesses of the various models.

Quoted 26 lines
Quoted 26 lines

Or like this (needs SWITCH_KERNEL so won't work with kGraft, but also extending patched set, will not work with kpatch as it stands today):

-----------------------------------------------------------

	void lock_a()
	{
(quoted diff) +1 -1
-		spin_lock(&x);
+		spin_lock(&y);
	}
	void lock_b()
	{
(quoted diff) +1 -1
-		spin_lock(&y);
+		spin_lock(&x);
	}
	void unlock_a()
	}
(quoted diff) +1 -1
-		spin_unlock(&x);
+		spin_unlock(&y);
	}
	void unlock_b()
	{
(quoted diff) +1 -1
-		spin_unlock(&y);
+		spin_unlock(&x);
	}
Quoted 15 lines
Quoted 9 lines
	void foo()
	{
		lock_a();
		lock_b();
		[...]
		unlock_b();
		unlock_a();
	}

-----------------------------------------------------------

Another way to handle this type of locking semantic change for either kpatch or kGraft is to use shadow data to add a "v2" shadow field to the lock's containing struct, which is set whenever the struct is allocated in the new universe. Then you can use that field to determine which version of locking semantics to use.

Agreed, this way of handling the locking changes makes the locking changes viable even for the SWITCH_THREAD case then.

And if you'd want to convert existing structs, then you also need to make the shadow operations atomic, but that's possible.

Also, if the lock is not a member of any struct, you'd have to shadow the lock itself, again, possible.

Quoted 6 lines
Quoted 2 lines

I believe we have to get incremental patching working anyway as it is a valid usecase for many users, just not for major distributions.

Well, we do already have it "working", but it's not safe enough for serious use because we aren't properly dealing with build and runtime dependencies between patch modules. It may be tricky to get right.

Similar for kGraft, incremental also works, and kGraft doesn't have an issue with build time dependencies for obvious reasons. At runtime it gets tricky, particularly if you start removing some of the patches.

Quoted 2 lines

But yeah, if you're _very_ careful to analyze any dependencies between the patches, an occasional incremental patch might be do-able.

I don't have an issue with not offering incremental patching initially, as we currently do not have a practical use for it.

Quoted 7 lines
Quoted 4 lines

And we may want to take a look at how to mark parts of a cumulative patch with different consistency models, when we combine eg. the recent futex CVE patch (not working with SWITCH_KERNEL) and anything requiring SWITCH kernel.

Yeah, interesting idea. But again I think we'd still need to be very careful with the dependencies.

Yes.

Quoted 11 lines
Quoted 6 lines

Now we stop_kernel on baz(). We don't check the stack. We patch bar(). We resume, and baz() happily returns into bar(), executing old code. At the same time, another CPU can call bar(), getting new code.

Stack checking at stop_kernel() time is required to keep the SWITCH_KERNEL part of the model.

Ok, I think I get what you're saying. In my mind it's kind of a hybrid of SWITCH_KERNEL and SWITCH_FUNCTION, since SWITCH_KERNEL would still be used for other functions in the patch. In that case we'd be forfeiting consistency just for those skipped functions in the list.

So you would not be skipping the stack checking entirely, just allowing certain functions from the patched set to be on the stack while you switch to the new universe.

That indeed would make it a mixed SWITCH_FUNCTION/SWITCH_KERNEL situation.

The big caveat in such a situation is that you must not change the calling convention or semantics of any function called directly from the function you skipped the stack check for. As doing so would crash the kernel when an old call calls a new function.

Quoted 5 lines
Quoted 4 lines

But there are a few (probably much less than 10%) cases like the locking one I used above, where SWITCH_THREAD just isn't going to cut it and for those I would need SWITCH_KERNEL or get very creative with refactoring the patch to do things differently.

Quoted 8 lines

I'm not opposed to having both if necessary. But I think the code would be _much_ simpler if we could agree on a single consistency model that can be used in all cases. Plus there wouldn't be such a strong requirement to get incremental patching to work safely (which will add more complexity).

I actually agree with you that LEAVE_PATCHED_SET + SWITCH_THREAD is pretty nice.

Cool! Do you see it as the next step consistency model we would focus on implementing in livepatch after the null model is complete and upstream?

(That wouldn't preclude extending it or implementing more models later.)

Quoted 3 lines

So I'd like to hear more about cases where you think we _need_ SWITCH_KERNEL. As I mentioned above, I think many of those cases can be solved by using data structure versioning with shadow data fields.

I have tried, but so far I can't find a situation whether we would absolutely need SWITCH_KERNEL, assuming we have LEAVE_PATCHED_SET + SWITCH_THREAD + TRANSFORM_ON_ACCESS.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Why would LEAVE_PATCHED_SET SWITCH_THREAD finish much quicker than LEAVE_PATCHED_SET SWITCH_KERNEL? Wouldn't they be about the same?

Because with LEAVE_PATCHED_SET SWITCH_THREAD you're waiting for each thread to leave the patched set and when they each have done that at least once, you're done. Even if some are already back within the set.

Ok, so you're talking about the case when you're trying to patch a function which is always active. Agreed :-)

Yes.

Quoted 7 lines
Quoted 4 lines

With LEAVE_PATCHED_SET SWITCH_KERNEL, you have to find the perfect moment when all of the threads are outside of the patched set at the same time. Depending on how often the functions are used and how large the set is, reaching that moment will get harder.

Yeah, I think this is the biggest drawback of SWITCH_KERNEL. More likely to fail (or never succeed).

If some threads are sleeping in a loop inside the patched set:

With SWITCH_THREAD you can wake (eg. by a signal) the threads from userspace as a last resort and that will complete your patching.

With SWITCH_KERNEL you'd have somehow to wake them at the same time hoping they also leave the patched set together. That's unlikely to happen when many threads are involved.

In addition the "in progress" behavior is nicer for SWITCH_THREAD, as any new thread will already be running patched code. With SWITCH_KERNEL, you're waiting with applying the fix until the perfect moment.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#60 lore raw
Quoted 12 lines · Vojtech Pavlik
Quoted 11 lines · Josh Poimboeuf
Quoted 4 lines

But there are a few (probably much less than 10%) cases like the locking one I used above, where SWITCH_THREAD just isn't going to cut it and for those I would need SWITCH_KERNEL or get very creative with refactoring the patch to do things differently.

I'm not opposed to having both if necessary. But I think the code would be _much_ simpler if we could agree on a single consistency model that can be used in all cases. Plus there wouldn't be such a strong requirement to get incremental patching to work safely (which will add more complexity).

Quoted 5 lines
Quoted 2 lines

I actually agree with you that LEAVE_PATCHED_SET + SWITCH_THREAD is pretty nice.

Cool! Do you see it as the next step consistency model we would focus on implementing in livepatch after the null model is complete and upstream?

Yeah, I'm thinking so. None of the consistency models are perfect, but I think this is a nice hybrid of the kGraft and kpatch models. It allows us to apply the greatest percentage of patches with the highest success rate, while keeping the code complexity at a reasonable level.

Signature · 1 line
Josh
#61 lore raw
In reply to Vojtech Pavlik

Hi,

Quoted 14 lines · Vojtech Pavlik
Quoted 12 lines · Josh Poimboeuf
Quoted 12 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

Now with those definitions:

livepatch (null model), as is, is LEAVE_FUNCTION and SWITCH_FUNCTION

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

kpatch, masami-refcounting and Ksplice are LEAVE_PATCHED_SET and SWITCH_KERNEL

kGraft is LEAVE_KERNEL and SWITCH_THREAD

CRIU/kexec is LEAVE_KERNEL and SWITCH_KERNEL

Thanks, nice analysis!

Hmm, I doubt this can cover all. what I'm thinking is a combination of LEAVE_KERNEL and SWITCH_KERNEL by using my refcounting and kGraft's per-thread "new universe" flagging(*). It switches all threads but not change entire kernel as kexec does.

So, I think the patch may be classified by following four types

PATCH_FUNCTION - Patching per function. This ignores context, just
change the function.
User must ensure that the new function can co-exist with old functions on the same context (e.g. recursive call can cause inconsistency).

PATCH_THREAD - Patching per thread. If a thread leave the kernel,
changes are applied for that thread.
User must ensure that the new functions can co-exist with old functions per-thread. Inter-thread shared data acquisition(locks) should not be involved.

PATCH_KERNEL - Patching all threads. This wait for all threads leave the
all target functions.
User must ensure that the new functions can co-exist with old functions on a thread (note that if there is a loop, old one can be called first n times, and new one can be called afterwords).(**)

RENEW_KERNEL - Renew entire kernel and reset internally. No patch limitation,
but involving kernel resetting. This may take a time.

(*) Instead of checking stacks, at first, wait for all threads leaving the kernel once, after that, wait for refcount becomes zero and switch all the patched functions.

(**) For the loops, if it is a simple loop or some simple lock calls, we can wait for all threads leave the caller function to avoid inconsistency by using refcounting.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

By blending kGraft and masami-refcounting, we could create a consistency engine capable of almost any combination of these properties and thus all the consistency models.

Quoted 8 lines
Quoted 1 line

Can you elaborate on what this would look like?

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Ah, that's similar thing what I'd like to try next :)

Sorry, here is an off-topic talk.
I think a problem of kGraft's LEAVE_KERNEL work is that the sleeping processes. To ensure all the threads are changing to new universe, we need to wakeup all the threads, or we need stack-dumping to find someone is sleeping on the target functions. What would the kGraft do for this issue?

Quoted 3 lines

A patch would have flags which specify a combination of the above properties that are needed for successful patching of that specific patch.

Agreed.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#62 lore raw
Quoted 4 lines · Masami Hiramatsu

Hmm, I doubt this can cover all. what I'm thinking is a combination of LEAVE_KERNEL and SWITCH_KERNEL by using my refcounting and kGraft's per-thread "new universe" flagging(*). It switches all threads but not change entire kernel as kexec does.

While your approach described below indeed forces all threads to leave kernel once, to initialize the reference counts, this can be considered a preparatory phase before the actual patching begins.

The actual consistency model remains unchanged from what kpatch offers today, which guarantees that at the time of switch, no execution thread is inside the set of patched functions and that the switch happens at once for all threads. Hence I'd still classify the consistency model offered as LEAVE_PATCHED_SET SWITCH_KERNEL.

Quoted 14 lines

So, I think the patch may be classified by following four types

PATCH_FUNCTION - Patching per function. This ignores context, just
change the function.
User must ensure that the new function can co-exist with old functions on the same context (e.g. recursive call can cause inconsistency).

PATCH_THREAD - Patching per thread. If a thread leave the kernel,
changes are applied for that thread.
User must ensure that the new functions can co-exist with old functions per-thread. Inter-thread shared data acquisition(locks) should not be involved.

Quoted 6 lines

PATCH_KERNEL - Patching all threads. This wait for all threads leave the
all target functions.
User must ensure that the new functions can co-exist with old functions on a thread (note that if there is a loop, old one can be called first n times, and new one can be called afterwords).(**)

Yes, but only when the function calling it is not included in the patched set, which is only a problem for semantic changes accompanied by no change in the function prototyppe. This can be avoided by changing the prototype deliberately.

Quoted 2 lines

RENEW_KERNEL - Renew entire kernel and reset internally. No patch limitation,
but involving kernel resetting. This may take a time.

And involves recording the userspace-kernel interface state exactly. The interface is fairly large, so this can become hairy.

Quoted 3 lines

(*) Instead of checking stacks, at first, wait for all threads leaving the kernel once, after that, wait for refcount becomes zero and switch all the patched functions.

This is a very beautiful idea.

It does away with both the stack parsing and the kernel stopping, achieving kGraft's goals, while preserving kpatch's consistency model.

Sadly, it combines the disadvantages of both kpatch and kGraft: From kpatch it takes the inability to patch functions where threads are sleeping often and as such never leave them at once. From kGraft it takes the need to annotate kernel threads and wake sleepers from userspace.

So while it is beautiful, it's less practical than either kpatch or kGraft alone.

Quoted 3 lines

(**) For the loops, if it is a simple loop or some simple lock calls, we can wait for all threads leave the caller function to avoid inconsistency by using refcounting.

Yes, this is what I call 'extending the patched set'. You can do that either by deliberately changing the prototype of the patched function being called, which causes the calling function to be considered different, or just add it to the set of functions considered manually.

Quoted 8 lines
Quoted 6 lines

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Ah, that's similar thing what I'd like to try next :)

Cool.

Quoted 5 lines

Sorry, here is an off-topic talk. I think a problem of kGraft's LEAVE_KERNEL work is that the sleeping processes. To ensure all the threads are changing to new universe, we need to wakeup all the threads, or we need stack-dumping to find someone is sleeping on the target functions. What would the kGraft do for this issue?

Yes, kGraft uses an userspace helper to find such sleeper and wake them by sending a SIGUSR1 or SIGSTOP/SIGCONT. It's one of the disadvantages of kGraft that sleeper threads have to be handled possibly case by case.

Also, kernel threads are problematic for kGraft (as you may have seen in earlier kGraft upstream submissions) as they never leave the kernel.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#63 lore raw
Quoted 11 lines · Vojtech Pavlik
Quoted 4 lines · Masami Hiramatsu

Hmm, I doubt this can cover all. what I'm thinking is a combination of LEAVE_KERNEL and SWITCH_KERNEL by using my refcounting and kGraft's per-thread "new universe" flagging(*). It switches all threads but not change entire kernel as kexec does.

While your approach described below indeed forces all threads to leave kernel once, to initialize the reference counts, this can be considered a preparatory phase before the actual patching begins.

Quoted 5 lines

The actual consistency model remains unchanged from what kpatch offers today, which guarantees that at the time of switch, no execution thread is inside the set of patched functions and that the switch happens at once for all threads. Hence I'd still classify the consistency model offered as LEAVE_PATCHED_SET SWITCH_KERNEL.

Right. Consistency model is still same as kpatch. Btw, I think we can just use the difference of consistency for classifying the patches, since we have these classes, only limited combination is meaningful.

Quoted 7 lines
Quoted 7 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

How about the below combination of consistent flags?

<flags>
CONSISTENT_IN_THREAD - patching is consistent in a thread. CONSISTENT_IN_TIME - patching is atomically done.

<combination>
(none) - the 'null' mode? same as LEAVE_FUNCTION & SWITCH_FUNCTION

CONSISTENT_IN_THREAD - kGraft mode. same as LEAVE_KERNEL & SWITCH_THREAD

CONSISTENT_IN_TIME - kpatch mode. same as LEAVE_PATCHED_SET & SWITCH_KERNEL

CONSISTENT_IN_THREAD|CONSISTENT_IN_TIME - CRIU mode. same as LEAVE_KERNEL & SWITCH_KERNEL

So, each patch requires consistency constrains flag and livepatch tool chooses the mode based on the flag.

Quoted 21 lines
Quoted 14 lines

So, I think the patch may be classified by following four types

PATCH_FUNCTION - Patching per function. This ignores context, just
change the function.
User must ensure that the new function can co-exist with old functions on the same context (e.g. recursive call can cause inconsistency).

PATCH_THREAD - Patching per thread. If a thread leave the kernel,
changes are applied for that thread.
User must ensure that the new functions can co-exist with old functions per-thread. Inter-thread shared data acquisition(locks) should not be involved.

Quoted 6 lines

PATCH_KERNEL - Patching all threads. This wait for all threads leave the
all target functions.
User must ensure that the new functions can co-exist with old functions on a thread (note that if there is a loop, old one can be called first n times, and new one can be called afterwords).(**)

Quoted 4 lines

Yes, but only when the function calling it is not included in the patched set, which is only a problem for semantic changes accompanied by no change in the function prototyppe. This can be avoided by changing the prototype deliberately.

Hmm, but what would you think about following simple case?

----
int func(int a) {
return a + 1;
}

...
b = 0;
for (i = 0; i < 10; i++)

    b = func(b);

...
----
----
int func(int a) {
return a + 2; /* Changed */
}

...
b = 0;
for (i = 0; i < 10; i++)

    b = func(b);

...
----

So, after the patch, "b" will be in a range of 10 to 20, not 10 or 20. Of course CONSISTENT_IN_THREAD can ensure it should be 10 or 20 :)

Quoted 13 lines
Quoted 3 lines

RENEW_KERNEL - Renew entire kernel and reset internally. No patch limitation,
but involving kernel resetting. This may take a time.

And involves recording the userspace-kernel interface state exactly. The interface is fairly large, so this can become hairy.

Quoted 3 lines

(*) Instead of checking stacks, at first, wait for all threads leaving the kernel once, after that, wait for refcount becomes zero and switch all the patched functions.

This is a very beautiful idea.

Quoted 8 lines

It does away with both the stack parsing and the kernel stopping, achieving kGraft's goals, while preserving kpatch's consistency model.

Sadly, it combines the disadvantages of both kpatch and kGraft: From kpatch it takes the inability to patch functions where threads are sleeping often and as such never leave them at once. From kGraft it takes the need to annotate kernel threads and wake sleepers from userspace.

But how frequently the former case happens? It seems very very rare. And if we aim to enable both kpatch mode and kGraft mode in the kernel, anyway we'll have something for the latter cases.

Quoted 3 lines

So while it is beautiful, it's less practical than either kpatch or kGraft alone.

Ah, sorry for confusing, I don't tend to integrate kpatch and kGraft. Actually, it is just about modifying kpatch, since it may shorten stack-checking time.
This means that does not change the consistency model.
We certainly need both of kGraft mode and kpatch mode.

Quoted 8 lines
Quoted 3 lines

(**) For the loops, if it is a simple loop or some simple lock calls, we can wait for all threads leave the caller function to avoid inconsistency by using refcounting.

Yes, this is what I call 'extending the patched set'. You can do that either by deliberately changing the prototype of the patched function being called, which causes the calling function to be considered different, or just add it to the set of functions considered manually.

I'd prefer latter one :) or just gives hints of watching targets.

Quoted 17 lines
Quoted 8 lines
Quoted 6 lines

There would be the refcounting engine, counting entries/exits of the area of interest (nothing for LEAVE_FUNCTION, patched functions for LEAVE_PATCHED_SET - same as Masami's work now, or syscall entry/exit for LEAVE_KERNEL), and it'd do the counting either per thread, flagging a thread as 'new universe' when the count goes to zero, or flipping a 'new universe' switch for the whole kernel when the count goes down to zero.

Ah, that's similar thing what I'd like to try next :)

Cool.

Quoted 5 lines

Sorry, here is an off-topic talk. I think a problem of kGraft's LEAVE_KERNEL work is that the sleeping processes. To ensure all the threads are changing to new universe, we need to wakeup all the threads, or we need stack-dumping to find someone is sleeping on the target functions. What would the kGraft do for this issue?

Quoted 5 lines

Yes, kGraft uses an userspace helper to find such sleeper and wake them by sending a SIGUSR1 or SIGSTOP/SIGCONT. It's one of the disadvantages of kGraft that sleeper threads have to be handled possibly case by case. Also, kernel threads are problematic for kGraft (as you may have seen in earlier kGraft upstream submissions) as they never leave the kernel.

Ah, I see. Perhaps, you can use stack-checking for sleeping threads and context-switch hooking for kernel threads as kpatch does :) Of course this will have a downside that patching can fail, but can avoid such problems.

Thank you!

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#64 lore raw
Quoted 19 lines · Masami Hiramatsu

Right. Consistency model is still same as kpatch. Btw, I think we can just use the difference of consistency for classifying the patches, since we have these classes, only limited combination is meaningful.

Quoted 7 lines
Quoted 7 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

How about the below combination of consistent flags?

<flags>
CONSISTENT_IN_THREAD - patching is consistent in a thread. CONSISTENT_IN_TIME - patching is atomically done.

Quoted 8 lines

<combination>
(none) - the 'null' mode? same as LEAVE_FUNCTION & SWITCH_FUNCTION

CONSISTENT_IN_THREAD - kGraft mode. same as LEAVE_KERNEL & SWITCH_THREAD

CONSISTENT_IN_TIME - kpatch mode. same as LEAVE_PATCHED_SET & SWITCH_KERNEL

CONSISTENT_IN_THREAD|CONSISTENT_IN_TIME - CRIU mode. same as LEAVE_KERNEL & SWITCH_KERNEL

The reason I tried to parametrize the consistency model in a more flexible and fine-grained manner than just describing the existing solutions was for the purpose of exploring whether any of the remaining combinations make sense.

It allowed me to look at what value we're getting from the consistency
models: Most importantly the ability to change function prototypes and still make calls work.

For this, the minimum requirements are LEAVE_PATCHED_SET (what kpatch does) and SWITCH_THREAD (which is what kGraft does).

Both kpatch and kGraft do more, but:

I was able to show that LEAVE_KERNEL is unnecessary and any cases where it is beneficial can be augmented by just increasing the patched set.

I believe at this point that SWITCH_KERNEL is unnecessary and that data or locking changes - the major benefit of switching at once can be done by shadowing/versioning of data structures, which is what both kpatch and kGraft had planned to do anyway.

I haven't shown yet whether the strongest consistency (LEAVE_KERNEL + SWITCH_KERNEL) is possible at all. CRIU is close, but not necessarily doing quite that. It might be possible to just force processes to sleep at syscall entry one by one until all are asleep. Also the benefits of doing that are still unclear.

The goal is to find a consistency model that is best suited for the goals of both kpatch and kGraft: Reliably apply simple to mid-complexity kernel patches.

Quoted 50 lines

So, each patch requires consistency constrains flag and livepatch tool chooses the mode based on the flag.

Quoted 21 lines
Quoted 14 lines

So, I think the patch may be classified by following four types

PATCH_FUNCTION - Patching per function. This ignores context, just
change the function.
User must ensure that the new function can co-exist with old functions on the same context (e.g. recursive call can cause inconsistency).

PATCH_THREAD - Patching per thread. If a thread leave the kernel,
changes are applied for that thread.
User must ensure that the new functions can co-exist with old functions per-thread. Inter-thread shared data acquisition(locks) should not be involved.

Quoted 6 lines

PATCH_KERNEL - Patching all threads. This wait for all threads leave the
all target functions.
User must ensure that the new functions can co-exist with old functions on a thread (note that if there is a loop, old one can be called first n times, and new one can be called afterwords).(**)

Quoted 4 lines

Yes, but only when the function calling it is not included in the patched set, which is only a problem for semantic changes accompanied by no change in the function prototyppe. This can be avoided by changing the prototype deliberately.

Hmm, but what would you think about following simple case?

----
int func(int a) {
return a + 1;
}

...
b = 0;
for (i = 0; i < 10; i++)

    b = func(b);

...
----
----
int func(int a) {
return a + 2; /* Changed */
}

...
b = 0;
for (i = 0; i < 10; i++)

Quoted 6 lines
    b = func(b);

...
----

So, after the patch, "b" will be in a range of 10 to 20, not 10 or 20. Of course CONSISTENT_IN_THREAD can ensure it should be 10 or 20 :)

If you force a prototype change, eg by changing func() to an unsigned int, or simply add a parameter, the place where it is called from will also be changed and will be included in the patched set. (Or you can just include it manually in the set.)

Then, you can be sure that the place which calls func() is not on the stack when patching. This way, in your classification, PATCH_KERNEL can be as good as PATCH_THREAD. In my classification, I'm saying that LEAVE_PATCHED_SET is as good as LEAVE_KERNEL.

Quoted 15 lines
Quoted 6 lines
Quoted 3 lines

(*) Instead of checking stacks, at first, wait for all threads leaving the kernel once, after that, wait for refcount becomes zero and switch all the patched functions.

This is a very beautiful idea.

Quoted 8 lines

It does away with both the stack parsing and the kernel stopping, achieving kGraft's goals, while preserving kpatch's consistency model.

Sadly, it combines the disadvantages of both kpatch and kGraft: From kpatch it takes the inability to patch functions where threads are sleeping often and as such never leave them at once. From kGraft it takes the need to annotate kernel threads and wake sleepers from userspace.

Quoted 3 lines

But how frequently the former case happens? It seems very very rare. And if we aim to enable both kpatch mode and kGraft mode in the kernel, anyway we'll have something for the latter cases.

The kpatch problem case isn't that rare. It just happened with a CVE in futexes recently. It will happen if you try to patch anything that is on the stack when a TTY or TCP read is waiting for data as another example.

The kGraft problem case will happen when you load a 3rd party module with a non-annotated kernel thread. Or a different problem will happen when you have an application sleeping that will exit when receiving any signal.

Both the cases can be handled with tricks and workarounds. But it'd be much nicer to have a patching engine that is reliable.

Quoted 8 lines
Quoted 2 lines

So while it is beautiful, it's less practical than either kpatch or kGraft alone.

Ah, sorry for confusing, I don't tend to integrate kpatch and kGraft. Actually, it is just about modifying kpatch, since it may shorten stack-checking time.
This means that does not change the consistency model.
We certainly need both of kGraft mode and kpatch mode.

What I'm proposing is a LEAVE_PATCHED_SET + SWITCH_THREAD mode. It's less consistency, but it is enough. And it is more reliable (likely to succeed in finite time) than either kpatch or kGraft.

It'd be mostly based on your refcounting code, including stack checking (when a process sleeps, counter gets set based on number of patched functions on the stack), possibly including setting the counter to 0 on syscall entry/exit, but it'd make the switch per-thread like kGraft does, not for the whole system, when the respective counters reach zero.

This handles the frequent sleeper case, it doesn't need annotated kernel thread main loops, it will not need the user to wake up every process in the system unless it sleeps in a patched function.

And it can handle all the patches that kpatch and kGraft can (it needs shadowing for some).

Quoted 6 lines
Quoted 4 lines

Yes, this is what I call 'extending the patched set'. You can do that either by deliberately changing the prototype of the patched function being called, which causes the calling function to be considered different, or just add it to the set of functions considered manually.

I'd prefer latter one :) or just gives hints of watching targets.

Me too.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#65 lore raw
Quoted 25 lines · Vojtech Pavlik
Quoted 24 lines · Masami Hiramatsu

Right. Consistency model is still same as kpatch. Btw, I think we can just use the difference of consistency for classifying the patches, since we have these classes, only limited combination is meaningful.

Quoted 7 lines
Quoted 7 lines

LEAVE_FUNCTION
LEAVE_PATCHED_SET
LEAVE_KERNEL

SWITCH_FUNCTION
SWITCH_THREAD
SWITCH_KERNEL

How about the below combination of consistent flags?

<flags>
CONSISTENT_IN_THREAD - patching is consistent in a thread. CONSISTENT_IN_TIME - patching is atomically done.

<combination>
(none) - the 'null' mode? same as LEAVE_FUNCTION & SWITCH_FUNCTION

CONSISTENT_IN_THREAD - kGraft mode. same as LEAVE_KERNEL & SWITCH_THREAD

Quoted 8 lines
Quoted 3 lines

CONSISTENT_IN_TIME - kpatch mode. same as LEAVE_PATCHED_SET & SWITCH_KERNEL

CONSISTENT_IN_THREAD|CONSISTENT_IN_TIME - CRIU mode. same as LEAVE_KERNEL & SWITCH_KERNEL

The reason I tried to parametrize the consistency model in a more flexible and fine-grained manner than just describing the existing solutions was for the purpose of exploring whether any of the remaining combinations make sense.

I see. I don't mind the implementation of how to check the execution path. I just considers that we need classify consistency requirements when checking the "patch" itself (maybe by manual at first).

And since your classification seemed mixing the consistency and switching timings, I thought we'd better split them into the consistency requirement flags and implementation of safeness checking :)

Even if you can use refcounting with per-thread patching, it still switches per-thread basis, inconsistent among threads.

Quoted 17 lines

It allowed me to look at what value we're getting from the consistency
models: Most importantly the ability to change function prototypes and still make calls work.

For this, the minimum requirements are LEAVE_PATCHED_SET (what kpatch does) and SWITCH_THREAD (which is what kGraft does).

Both kpatch and kGraft do more, but:

I was able to show that LEAVE_KERNEL is unnecessary and any cases where it is beneficial can be augmented by just increasing the patched set.

I believe at this point that SWITCH_KERNEL is unnecessary and that data or locking changes - the major benefit of switching at once can be done by shadowing/versioning of data structures, which is what both kpatch and kGraft had planned to do anyway.

Quoted 5 lines

I haven't shown yet whether the strongest consistency (LEAVE_KERNEL + SWITCH_KERNEL) is possible at all. CRIU is close, but not necessarily doing quite that. It might be possible to just force processes to sleep at syscall entry one by one until all are asleep. Also the benefits of doing that are still unclear.

Of course, that is what kernel/freezer.c does :)
So, if you need to patch with the strongest consistency, you can freeze them all.

Quoted 4 lines

The goal is to find a consistency model that is best suited for the goals of both kpatch and kGraft: Reliably apply simple to mid-complexity kernel patches.

Same as me. I just sorted out the possible consistency requirements. And I've thought that the key was "consistent in a context of each thread" or "consistent at the moment among all threads but not in a context" or "consistent in contexts of all threads". What would you think, any other consistency model is there?

Quoted 54 lines
Quoted 54 lines

So, each patch requires consistency constrains flag and livepatch tool chooses the mode based on the flag.

Quoted 21 lines
Quoted 14 lines

So, I think the patch may be classified by following four types

PATCH_FUNCTION - Patching per function. This ignores context, just
change the function.
User must ensure that the new function can co-exist with old functions on the same context (e.g. recursive call can cause inconsistency).

PATCH_THREAD - Patching per thread. If a thread leave the kernel,
changes are applied for that thread.
User must ensure that the new functions can co-exist with old functions per-thread. Inter-thread shared data acquisition(locks) should not be involved.

Quoted 6 lines

PATCH_KERNEL - Patching all threads. This wait for all threads leave the
all target functions.
User must ensure that the new functions can co-exist with old functions on a thread (note that if there is a loop, old one can be called first n times, and new one can be called afterwords).(**)

Quoted 4 lines

Yes, but only when the function calling it is not included in the patched set, which is only a problem for semantic changes accompanied by no change in the function prototyppe. This can be avoided by changing the prototype deliberately.

Hmm, but what would you think about following simple case?

----
int func(int a) {
return a + 1;
}

...
b = 0;
for (i = 0; i < 10; i++)

    b = func(b);

...
----
----
int func(int a) {
return a + 2; /* Changed */
}

...
b = 0;
for (i = 0; i < 10; i++)

    b = func(b);

...
----

Quoted 7 lines
Quoted 2 lines

So, after the patch, "b" will be in a range of 10 to 20, not 10 or 20. Of course CONSISTENT_IN_THREAD can ensure it should be 10 or 20 :)

If you force a prototype change, eg by changing func() to an unsigned int, or simply add a parameter, the place where it is called from will also be changed and will be included in the patched set. (Or you can just include it manually in the set.)

Yes.

Quoted 4 lines

Then, you can be sure that the place which calls func() is not on the stack when patching. This way, in your classification, PATCH_KERNEL can be as good as PATCH_THREAD. In my classification, I'm saying that LEAVE_PATCHED_SET is as good as LEAVE_KERNEL.

OK, but again, to be sure that, we need to dump stack for each kernel as I did.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 3 lines

(*) Instead of checking stacks, at first, wait for all threads leaving the kernel once, after that, wait for refcount becomes zero and switch all the patched functions.

This is a very beautiful idea.

It does away with both the stack parsing and the kernel stopping, achieving kGraft's goals, while preserving kpatch's consistency model.

Sadly, it combines the disadvantages of both kpatch and kGraft: From kpatch it takes the inability to patch functions where threads are sleeping often and as such never leave them at once. From kGraft it takes the need to annotate kernel threads and wake sleepers from userspace.

Quoted 7 lines
Quoted 3 lines

But how frequently the former case happens? It seems very very rare. And if we aim to enable both kpatch mode and kGraft mode in the kernel, anyway we'll have something for the latter cases.

The kpatch problem case isn't that rare. It just happened with a CVE in futexes recently. It will happen if you try to patch anything that is on the stack when a TTY or TCP read is waiting for data as another example.

Oh, I see. this should be solved then... perhaps, we can freeze those tasks and thaw it again.

Quoted 4 lines

The kGraft problem case will happen when you load a 3rd party module with a non-annotated kernel thread. Or a different problem will happen when you have an application sleeping that will exit when receiving any signal.

Ah, yes. especially latter case is serious. maybe freezer can handle this too...

Quoted 12 lines

Both the cases can be handled with tricks and workarounds. But it'd be much nicer to have a patching engine that is reliable.

Quoted 8 lines
Quoted 2 lines

So while it is beautiful, it's less practical than either kpatch or kGraft alone.

Ah, sorry for confusing, I don't tend to integrate kpatch and kGraft. Actually, it is just about modifying kpatch, since it may shorten stack-checking time.
This means that does not change the consistency model.
We certainly need both of kGraft mode and kpatch mode.

Quoted 3 lines

What I'm proposing is a LEAVE_PATCHED_SET + SWITCH_THREAD mode. It's less consistency, but it is enough. And it is more reliable (likely to succeed in finite time) than either kpatch or kGraft.

Yeah, that is actual merge of kpatch and kGraft, and also can avoid stop_machine (yes, that is important for me :)).

Quoted 6 lines

It'd be mostly based on your refcounting code, including stack checking (when a process sleeps, counter gets set based on number of patched functions on the stack), possibly including setting the counter to 0 on syscall entry/exit, but it'd make the switch per-thread like kGraft does, not for the whole system, when the respective counters reach zero.

I'm not sure what happens if a process sleeps on the patched-set? If we switch the other threads, when this sleeping thread wakes up that will see the old functions (and old data). So I think we need both SWITCH_THREAD and SWITCH_KERNEL options in that case. What I'm thinking is to merge the code (technique) of both and allow to choose the "switch-timing" based on the patch's consistency requirement.

Quoted 8 lines

This handles the frequent sleeper case, it doesn't need annotated kernel thread main loops, it will not need the user to wake up every process in the system unless it sleeps in a patched function.

And it can handle all the patches that kpatch and kGraft can (it needs shadowing for some).

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Yes, this is what I call 'extending the patched set'. You can do that either by deliberately changing the prototype of the patched function being called, which causes the calling function to be considered different, or just add it to the set of functions considered manually.

I'd prefer latter one :) or just gives hints of watching targets.

Me too.

Anyway, I'd like to support for this effort from kernel side. At least I have to solve ftrace regs conflict by IPMODIFY flag and a headache kretprobe failure case by sharing per-thread retstack with ftrace-callgraph.

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#66 lore raw
Quoted 7 lines · Masami Hiramatsu

I see. I don't mind the implementation of how to check the execution path. I just considers that we need classify consistency requirements when checking the "patch" itself (maybe by manual at first).

And since your classification seemed mixing the consistency and switching timings, I thought we'd better split them into the consistency requirement flags and implementation of safeness checking :)

That makes sense. Although what classes of patches actually need what kind of consistency is an open debate still.

Quoted 2 lines

Even if you can use refcounting with per-thread patching, it still switches per-thread basis, inconsistent among threads.

Yes.

Quoted 9 lines
Quoted 5 lines

I haven't shown yet whether the strongest consistency (LEAVE_KERNEL + SWITCH_KERNEL) is possible at all. CRIU is close, but not necessarily doing quite that. It might be possible to just force processes to sleep at syscall entry one by one until all are asleep. Also the benefits of doing that are still unclear.

Of course, that is what kernel/freezer.c does :)
So, if you need to patch with the strongest consistency, you can freeze them all.

That'd be really cool. (Pun intended.)

I'd have to look deeper into freezer, but does it really stop the threads at syscall entry? And not anywhere where they sleep? How would it handle sleepers?

For LEAVE_KERNEL + SWITCH_KERNEL we'd have to freeze them when no kernel function is on the stack at all.

Quoted 9 lines
Quoted 3 lines

The goal is to find a consistency model that is best suited for the goals of both kpatch and kGraft: Reliably apply simple to mid-complexity kernel patches.

Same as me. I just sorted out the possible consistency requirements. And I've thought that the key was "consistent in a context of each thread" or "consistent at the moment among all threads but not in a context" or "consistent in contexts of all threads". What would you think, any other consistency model is there?

I'm looking for the simplest solution that allows modification to function prototypes. Because that's the most important value that both kGraft and kpatch consistency models offer.

Quoted 7 lines
Quoted 4 lines

If you force a prototype change, eg by changing func() to an unsigned int, or simply add a parameter, the place where it is called from will also be changed and will be included in the patched set. (Or you can just include it manually in the set.)

Yes.

Quoted 7 lines
Quoted 4 lines

Then, you can be sure that the place which calls func() is not on the stack when patching. This way, in your classification, PATCH_KERNEL can be as good as PATCH_THREAD. In my classification, I'm saying that LEAVE_PATCHED_SET is as good as LEAVE_KERNEL.

OK, but again, to be sure that, we need to dump stack for each kernel as I did.

Or pass through the userspace to initialize the counters as you proposed.

But I'd really like to avoid stack analysis or having to force every thread through the kernel/userspace boundary.

I have one more, rather trivial idea how things could be done, I'll be sending an email with it shortly.

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

But how frequently the former case happens? It seems very very rare. And if we aim to enable both kpatch mode and kGraft mode in the kernel, anyway we'll have something for the latter cases.

The kpatch problem case isn't that rare. It just happened with a CVE in futexes recently. It will happen if you try to patch anything that is on the stack when a TTY or TCP read is waiting for data as another example.

Oh, I see. this should be solved then... perhaps, we can freeze those tasks and thaw it again.

Quoted 7 lines
Quoted 4 lines

The kGraft problem case will happen when you load a 3rd party module with a non-annotated kernel thread. Or a different problem will happen when you have an application sleeping that will exit when receiving any signal.

Ah, yes. especially latter case is serious. maybe freezer can handle this too...

I should look into it then.

Quoted 6 lines
Quoted 3 lines

What I'm proposing is a LEAVE_PATCHED_SET + SWITCH_THREAD mode. It's less consistency, but it is enough. And it is more reliable (likely to succeed in finite time) than either kpatch or kGraft.

Yeah, that is actual merge of kpatch and kGraft, and also can avoid stop_machine (yes, that is important for me :)).

Good. Same here.

Quoted 8 lines
Quoted 6 lines

It'd be mostly based on your refcounting code, including stack checking (when a process sleeps, counter gets set based on number of patched functions on the stack), possibly including setting the counter to 0 on syscall entry/exit, but it'd make the switch per-thread like kGraft does, not for the whole system, when the respective counters reach zero.

I'm not sure what happens if a process sleeps on the patched-set?

Then the patching process will be stuck until it is woken up somehow. But it's still much better to only have to care about processes sleeping on the patched-set than about processes sleeping anywhere (kGraft).

Quoted 2 lines

If we switch the other threads, when this sleeping thread wakes up that will see the old functions (and old data).

Yes, until the patching process is complete, data must be kept in the old format, even by new functions.

Quoted 2 lines

So I think we need both SWITCH_THREAD and SWITCH_KERNEL options in that case.

With data shadowing that's not required. It still may be worth having it.

Quoted 3 lines

What I'm thinking is to merge the code (technique) of both and allow to choose the "switch-timing" based on the patch's consistency requirement.

That's what I'm thinking about, too. But I'm also thinking, "this will be complex, is it really needed"?

Quoted 4 lines

Anyway, I'd like to support for this effort from kernel side. At least I have to solve ftrace regs conflict by IPMODIFY flag and a headache kretprobe failure case by sharing per-thread retstack with ftrace-callgraph.

While I don't know enough about the IPMODIFY flag, I wholeheartedly support sharing the return stacks between kprobes and ftrace graph caller.

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#67 lore raw
Quoted 14 lines · Vojtech Pavlik
Quoted 8 lines · Masami Hiramatsu
Quoted 6 lines

It'd be mostly based on your refcounting code, including stack checking (when a process sleeps, counter gets set based on number of patched functions on the stack), possibly including setting the counter to 0 on syscall entry/exit, but it'd make the switch per-thread like kGraft does, not for the whole system, when the respective counters reach zero.

I'm not sure what happens if a process sleeps on the patched-set?

Then the patching process will be stuck until it is woken up somehow. But it's still much better to only have to care about processes sleeping on the patched-set than about processes sleeping anywhere (kGraft).

Quoted 5 lines
Quoted 2 lines

If we switch the other threads, when this sleeping thread wakes up that will see the old functions (and old data).

Yes, until the patching process is complete, data must be kept in the old format, even by new functions.

I am not sure if I am able to follow all the ideas. Anyway, the above sentence triggered some warning bells in my head ;-)

Would it mean waiting for two safe switch points, please? One to switch functions and the second to switch the data structures?

The later condition looks pretty complicated to me. It would mean to make sure that the old structures won't be stored anywhere and nobody would want to use them later. It won't be enough to check the stack because some function might be called later that would access a saved pointer pointing to the old structure.

Quoted 5 lines
Quoted 2 lines

So I think we need both SWITCH_THREAD and SWITCH_KERNEL options in that case.

With data shadowing that's not required. It still may be worth having it.

I am not 100% sure what the shadowing means. If it means that the new function will be able to read and write both versions of the structure, it looks more easily doable to me.

Best Regards,
Petr

#68 lore raw
Quoted 18 lines · Petr Mladek
Quoted 17 lines · Vojtech Pavlik
Quoted 8 lines · Masami Hiramatsu
Quoted 6 lines

It'd be mostly based on your refcounting code, including stack checking (when a process sleeps, counter gets set based on number of patched functions on the stack), possibly including setting the counter to 0 on syscall entry/exit, but it'd make the switch per-thread like kGraft does, not for the whole system, when the respective counters reach zero.

I'm not sure what happens if a process sleeps on the patched-set?

Then the patching process will be stuck until it is woken up somehow. But it's still much better to only have to care about processes sleeping on the patched-set than about processes sleeping anywhere (kGraft).

Quoted 2 lines

If we switch the other threads, when this sleeping thread wakes up that will see the old functions (and old data).

Quoted 8 lines
Quoted 2 lines

Yes, until the patching process is complete, data must be kept in the old format, even by new functions.

I am not sure if I am able to follow all the ideas. Anyway, the above sentence triggered some warning bells in my head ;-)

Would it mean waiting for two safe switch points, please? One to switch functions and the second to switch the data structures?

Yes, though the first switch "point" doesn't have to be instantaneous. When changing data, new functions must be able to understand both old and new data. The transition to the new functions can happen gradually, as long as you don't start creating new data (or migrating existing data) until after the patching is complete. That way you'll never have an old function trying to access new data.

Quoted 5 lines

The later condition looks pretty complicated to me. It would mean to make sure that the old structures won't be stored anywhere and nobody would want to use them later. It won't be enough to check the stack because some function might be called later that would access a saved pointer pointing to the old structure.

We don't need to necessarily get rid of all old data, because new functions can handle both old and new data.

Quoted 11 lines
Quoted 7 lines
Quoted 4 lines

So I think we need both SWITCH_THREAD and SWITCH_KERNEL options in that case.

With data shadowing that's not required. It still may be worth having it.

I am not 100% sure what the shadowing means. If it means that the new function will be able to read and write both versions of the structure, it looks more easily doable to me.

Shadowing is a way of adding virtual fields to existing data structures at runtime. That enables the new functions to easily determine whether they're dealing with v1 or v2 of a given data structure, as well as access any new fields in v2.

Signature · 1 line
Josh
#69 lore raw
In reply to Vojtech Pavlik
Quoted 14 lines · Vojtech Pavlik
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Vojtech Pavlik

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

Quoted 5 lines

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

What generator does kGraft have? Is that the one that generates the source patch, or is there one that generates a binary patch module?

Signature · 1 line
Josh
#70 lore raw
Quoted 15 lines · Josh Poimboeuf
Quoted 14 lines · Vojtech Pavlik
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Vojtech Pavlik

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

Quoted 8 lines
Quoted 5 lines

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

What generator does kGraft have? Is that the one that generates the source patch, or is there one that generates a binary patch module?

The generator for kGraft:

* extracts a list of changed functions from a patch (rather naïvely so far)
* uses DWARF debuginfo of the old kernel to handle things like inlining and create a complete list of functions that need to be replaced
* compiles the kernel with -fdata-sections -ffunction-sections
* uses a modified objcopy to extract functions from the kernel into a single .o file
* creates a stub .c file that references those functions
* compiles the .c and links with the .o to build a .ko

The main difference is in that the kGraft generator doesn't try to compare the old and new binary objects, but rather works with function lists and the DWARF info of the old code and extracts new functions from the new binary.

However, as I said before, we have found enough trouble around eg. IPA-SRA and other optimizations that make any automated approach fragile and in our view more effort than benefit. Hence, we're intend to use the manual way of creating live patches until proven that we were wrong in this assessment. :)

Signature · 2 lines
Vojtech Pavlik
Director SUSE Labs
#71 lore raw
Quoted 36 lines · Vojtech Pavlik
Quoted 15 lines · Josh Poimboeuf
Quoted 14 lines · Vojtech Pavlik
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines · Vojtech Pavlik

I don't think this specific example was generated.

I also don't think including the whole kpatch automation into the kernel tree is a viable development model for it. (Same would apply for kGraft automation.)

Why? We (IMHO incorrectly) used the argument of tight coupling to put perf into the kernel tree. Generating kernel live patches is way more integrated that it absolutely has to go into the tree to be able to do proper development on it in an integrated fashion.

Quoted 8 lines
Quoted 5 lines

One reason is that there are currently at least two generators using very different methods of generation (in addition to the option of doing the patch module by hand), and neither of them are currently in a state where they would be ready for inclusion into the kernel (although the kpatch one is clearly closer to that).

What generator does kGraft have? Is that the one that generates the source patch, or is there one that generates a binary patch module?

The generator for kGraft:

* extracts a list of changed functions from a patch (rather naïvely so far)
* uses DWARF debuginfo of the old kernel to handle things like inlining and create a complete list of functions that need to be replaced
* compiles the kernel with -fdata-sections -ffunction-sections
* uses a modified objcopy to extract functions from the kernel into a single .o file
* creates a stub .c file that references those functions
* compiles the .c and links with the .o to build a .ko

Quoted 4 lines

The main difference is in that the kGraft generator doesn't try to compare the old and new binary objects, but rather works with function lists and the DWARF info of the old code and extracts new functions from the new binary.

Thanks, interesting. Sounds like we're mostly on the same page here.

Quoted 6 lines

However, as I said before, we have found enough trouble around eg. IPA-SRA and other optimizations that make any automated approach fragile and in our view more effort than benefit. Hence, we're intend to use the manual way of creating live patches until proven that we were wrong in this assessment. :)

Yeah. We've already put in a lot of effort to support the gcc optimizations like IPA-SRA, partial inlining, static variable renaming, etc. And also added support for many kernel special sections.

For now, at least, it works very well, and we find that generation is _much_ easier and less error-prone than the manual approach. So in our experience, the benefits far outweigh the effort.

But I do agree that it's fragile, and at the mercy of any future gcc optimization features. Which is why I like our current approach of supporting the manual approach as well. The manual approach isn't optimal, but it is a nice backup solution for us in case something causes the generator to break.

Signature · 1 line
Josh
#72 lore raw
In reply to Seth Jennings
Quoted 23 lines · Seth Jennings

This patchset implements an ftrace-based mechanism and kernel interface for doing live patching of kernel and kernel module functions. It represents the greatest common functionality set between kpatch [1] and kGraft [2] and can accept patches built using either method. This solution was discussed in the Live Patching Mini-conference at LPC 2014 [3].

The model consists of a live patching "core" that provides an interface for other "patch" kernel modules to register patches with the core.

Patch modules contain the new function code and create an lp_patch structure containing the required data about what functions to patch, where the new code for each patched function resides, and in which kernel object (vmlinux or module) the function to be patch resides. The patch module then invokes the lp_register_patch() function to register with the core module, then lp_enable_patch() to have to core module redirect the execution paths using ftrace.

An example patch module can be found here:
https://github.com/spartacus06/livepatch/blob/master/patch/patch.c

The live patching core creates a sysfs hierarchy for user-level access to live patching information. The hierarchy is structured like this:

Quoted 7 lines

/sys/kernel/livepatch /sys/kernel/livepatch/<patch> /sys/kernel/livepatch/<patch>/enabled /sys/kernel/livepatch/<patch>/<object> /sys/kernel/livepatch/<patch>/<object>/<func> /sys/kernel/livepatch/<patch>/<object>/<func>/new_addr /sys/kernel/livepatch/<patch>/<object>/<func>/old_addr

You are creating sysfs attributes with no Documentation/ABI/ entries, please fix that in future patches.

thanks,

greg k-h