Fix issue with alternatives/paravirt patches
Subsystems (2)
v2 cover Fix issue with alternatives/paravirt patches
Hi,
A few months ago, Chris Arges reported a bug involving alternatives/paravirt patching that was discussed here [1] and here [2]. To briefly summarize the bug, patch modules that contained .altinstructions or .parainstructions sections would break because these alternative/paravirt patches would be applied first by the module loader (see x86 module_finalize()), then livepatch would later clobber these patches when applying per-object relocations. This lead to crashes and unpredictable behavior.
One conclusion we reached from our last discussion was that we will need to introduce some arch-specific code to address this problem. This patchset presents a possible fix for the bug by adding a new arch-specific arch_klp_init_object_loaded() function that by default does nothing but can be overridden by different arches.
To fix this issue for x86, since we can access a patch module's Elf sections through mod->klp_info, we can simply delay the calls to apply_paravirt() and apply_alternatives() to arch_klp_init_object_loaded(), which is called after relocations have been written for an object. In addition, for patch modules, .parainstructions and .altinstructions are prefixed by ".klp.arch.${objname}" so that the module loader ignores them and livepatch can apply them manually.
Currently for kpatch, we don't support including jump table sections in the patch module, and supporting .smp_locks is currently broken, so we don't consider those sections (for now).
I did some light testing with some patches to kvm and verified that the original issue reported in [2] was fixed.
Based on linux-next.
v1 here:
http://lkml.kernel.org/g/[email protected]
v2:
- add BUILD_BUG_ON() check in arch_klp_init_object_loaded (x86)
[1] http://thread.gmane.org/gmane.linux.kernel/2185604/
[2] https://github.com/dynup/kpatch/issues/580
Jessica Yu (2):
livepatch: use arch_klp_init_object_loaded() to finish arch-specific tasks livepatch/x86: apply alternatives and paravirt patches after relocations
arch/x86/kernel/Makefile | 1 + arch/x86/kernel/livepatch.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/livepatch.h | 3 +++ kernel/livepatch/core.c | 12 +++++++-- 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 arch/x86/kernel/livepatch.c
Signature · 1 line
2.5.5
PATCH v2 1/2 livepatch: use arch_klp_init_object_loaded() to finish arch-specific tasks
Introduce arch_klp_init_object_loaded() to complete any additional arch-specific tasks during patching. Architecture code may override this function.
--- include/linux/livepatch.h | 3 +++ kernel/livepatch/core.c | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
include/linux/livepatch.h +3 -0
@@ -116,6 +116,9 @@ int klp_unregister_patch(struct klp_patch *);int klp_enable_patch (struct klp_patch *);int klp_disable_patch (struct klp_patch *); +void arch_klp_init_object_loaded (struct klp_patch *patch, +struct klp_object *obj); +/* Called from the module loader during module coming/going states */ int klp_module_coming (struct module *mod);void klp_module_going (struct module *mod);
kernel/livepatch/core.c +10 -2
@@ -274,7 +274,6 @@ static int klp_write_object_relocations(struct module *pmod, objname =klp_is_module (obj) ? obj->name :"vmlinux" ; -module_disable_ro (pmod);/* For each klp relocation section */ for (i =1 ; i < pmod->klp_info->hdr.e_shnum; i++) { sec = pmod->klp_info->sechdrs + i;
@@ -309,7 +308,6 @@ static int klp_write_object_relocations(struct module *pmod,break ; } -module_enable_ro (pmod);return ret; }
@@ -763,6 +761,12 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func) func->old_sympos ? func->old_sympos : 1); } +/* Arches may override this to finish any remaining arch-specific tasks */ +void __weak arch_klp_init_object_loaded (struct klp_patch *patch, +struct klp_object *obj) +{ +} +/* parts of the initialization that is done only when the object is loaded */ static int klp_init_object_loaded (struct klp_patch *patch,struct klp_object *obj)
@@ -770,10 +774,14 @@ static int klp_init_object_loaded(struct klp_patch *patch,struct klp_func *func;int ret; +module_disable_ro (patch->mod); ret =klp_write_object_relocations (patch->mod, obj);if (ret)return ret; +arch_klp_init_object_loaded (patch, obj); +module_enable_ro (patch->mod); +klp_for_each_func (obj, func) { ret = klp_find_object_symbol(obj->name, func->old_name, func->old_sympos,
Signature · 1 line
2.5.5
Miroslav Benes
maintainerRe: [PATCH v2 1/2] livepatch: use arch_klp_init_object_loaded() to finish arch-specific tasks
Quoted 58 lines · Jessica Yu
Introduce
arch_klp_init_object_loaded()to complete any additional arch-specific tasks during patching. Architecture code may override this function.Signed-off-by Jessica Yu <[email protected]>--- include/linux/livepatch.h | 3 +++ kernel/livepatch/core.c | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)include/linux/livepatch.h +3
-0@@ -116,6 +116,9 @@ int klp_unregister_patch(struct klp_patch *);int klp_enable_patch (struct klp_patch *);int klp_disable_patch (struct klp_patch *); +void arch_klp_init_object_loaded (struct klp_patch *patch, +struct klp_object *obj); +/* Called from the module loader during module coming/going states */ int klp_module_coming (struct module *mod);void klp_module_going (struct module *mod);kernel/livepatch/core.c +6
-2@@ -274,7 +274,6 @@ static int klp_write_object_relocations(struct module *pmod, objname =klp_is_module (obj) ? obj->name :"vmlinux" ; -module_disable_ro (pmod);/* For each klp relocation section */ for (i =1 ; i < pmod->klp_info->hdr.e_shnum; i++) { sec = pmod->klp_info->sechdrs + i;@@ -309,7 +308,6 @@ static int klp_write_object_relocations(struct module *pmod,break ; } -module_enable_ro (pmod);return ret; }@@ -763,6 +761,12 @@ static int klp_init_func(struct klp_object *obj, struct klp_func *func) func->old_sympos ? func->old_sympos : 1); } +/* Arches may override this to finish any remaining arch-specific tasks */ +void __weak arch_klp_init_object_loaded (struct klp_patch *patch, +struct klp_object *obj) +{ +} +/* parts of the initialization that is done only when the object is loaded */ static int klp_init_object_loaded (struct klp_patch *patch,struct klp_object *obj)
kernel/livepatch/core.c static int klp_init_object_loaded(struct klp_patch *patch, · line 780
Quoted 7 lines
kernel/livepatch/core.c +1
-0@@ -770,10 +774,14 @@ static int klp_init_object_loaded(struct klp_patch *patch,struct klp_func *func;int ret; +module_disable_ro (patch->mod); ret =klp_write_object_relocations (patch->mod, obj);if (ret)return ret;
We need to call module_enable_ro(patch->mod) here before returning back.
Miroslav
Quoted 10 lines
kernel/livepatch/core.c +3
-0+arch_klp_init_object_loaded (patch, obj); +module_enable_ro (patch->mod); +klp_for_each_func (obj, func) { ret = klp_find_object_symbol(obj->name, func->old_name, func->old_sympos,Signature · 1 line
2.5.5
PATCH v2 2/2 livepatch/x86: apply alternatives and paravirt patches after relocations
In reply to Jessica YuImplement arch_klp_init_object_loaded() for x86, which applies alternatives/paravirt patches. This fixes the order in which relocations and alternatives/paravirt patches are applied.
Previously, if a patch module had alternatives or paravirt patches, these were applied first by the module loader before livepatch can apply per-object relocations. The (buggy) sequence of events was:
(1) Load patch module
(2) Apply alternatives and paravirt patches to patch module
* Note that these are applied to the new functions in the patch module
(3) Apply per-object relocations to patch module when target module loads.
* This clobbers what was written in step 2
This lead to crashes and corruption in general, since livepatch would overwrite or step on previously applied alternative/paravirt patches. The correct sequence of events should be:
(1) Load patch module
(2) Apply per-object relocations to patch module
(3) Apply alternatives and paravirt patches to patch module
This is fixed by delaying paravirt/alternatives patching until after relocations are applied. Any .altinstructions or .parainstructions sections in a patch module are prefixed with ".klp.arch.${objname}" and applied in arch_klp_init_object_loaded().
--- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/livepatch.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 arch/x86/kernel/livepatch.c
arch/x86/kernel/Makefile +1 -0
@@ -83,6 +83,7 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o obj-y += apic/ obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o +obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o obj-$(CONFIG_X86_TSC) += trace_clock.o
arch/x86/kernel/livepatch.c new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.5.5
Miroslav Benes
maintainerRe: [PATCH v2 2/2] livepatch/x86: apply alternatives and paravirt patches after relocations
Quoted 18 lines · Jessica Yu
Implement
arch_klp_init_object_loaded()for x86, which applies alternatives/paravirt patches. This fixes the order in which relocations and alternatives/paravirt patches are applied.Previously, if a patch module had alternatives or paravirt patches, these were applied first by the module loader before livepatch can apply per-object relocations. The (buggy) sequence of events was:
(1) Load patch module
(2) Apply alternatives and paravirt patches to patch module
* Note that these are applied to the new functions in the patch module
(3) Apply per-object relocations to patch module when target module loads.
* This clobbers what was written in step 2This lead to crashes and corruption in general, since livepatch would overwrite or step on previously applied alternative/paravirt patches. The correct sequence of events should be:
Quoted 8 lines
(1) Load patch module
(2) Apply per-object relocations to patch module
(3) Apply alternatives and paravirt patches to patch moduleThis is fixed by delaying paravirt/alternatives patching until after relocations are applied. Any .altinstructions or .parainstructions sections in a patch module are prefixed with ".klp.arch.${objname}" and applied in
arch_klp_init_object_loaded().
Would it make sense to update our documentation and include this info?
Quoted 36 lines
Signed-off-by Jessica Yu <[email protected]>--- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/livepatch.c | 65 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 arch/x86/kernel/livepatch.carch/x86/kernel/Makefile +1
-0@@ -83,6 +83,7 @@ obj-$(CONFIG_X86_MPPARSE) += mpparse.o obj-y += apic/ obj-$(CONFIG_X86_REBOOTFIXUPS) += reboot_fixups_32.o obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o +obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o obj-$(CONFIG_X86_TSC) += trace_clock.oarch/x86/kernel/livepatch.c new file +11
-0@@ -0,0 +1,65 @@ +/* +* livepatch.c - x86-specific Kernel Live Patching Core +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
arch/x86/kernel/livepatch.c line 21
Quoted 10 lines
arch/x86/kernel/livepatch.c new file +10
-0+* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +#include <linux/module.h> +#include <linux/kallsyms.h> +#include <linux/livepatch.h> +#include <asm/text-patching.h>
It seems I don't have such header file here and it causes a build error. Shouldn't it be <asm/alternative.h>?
Quoted 34 lines
arch/x86/kernel/livepatch.c new file +34
-0+ +/* Apply per-object alternatives. Based on x86 module_finalize() */ +void arch_klp_init_object_loaded (struct klp_patch *patch, +struct klp_object *obj) +{ +int cnt; +struct klp_modinfo *info; +Elf_Shdr *s, *alt =NULL , *para =NULL ; +void *aseg, *pseg; +const char *objname; +char sec_objname[MODULE_NAME_LEN ]; +char secname[KSYM_NAME_LEN ]; + + info = patch->mod->klp_info; + objname = obj->name ? obj->name :"vmlinux" ; + +/* See livepatch core code for BUILD_BUG_ON() explanation */ +BUILD_BUG_ON (MODULE_NAME_LEN <56 ||KSYM_NAME_LEN !=128 ); + +for (s = info->sechdrs; s < info->sechdrs + info->hdr.e_shnum; s++) { +/* Apply per-object .klp.arch sections */ + cnt =sscanf (info->secstrings + s->sh_name, +".klp.arch.%55[^.].%127s" , + sec_objname, secname); +if (cnt !=2 ) +continue ; +if (strcmp (sec_objname, objname)) +continue ; +if (!strcmp ("altinstructions" , secname)) + alt = s; +if (!strcmp ("parainstructions" , secname)) + para = s; + } +
arch/x86/kernel/livepatch.c line 65
Quoted 10 lines
arch/x86/kernel/livepatch.c new file +10
-0+if (alt) { + aseg = (void *) alt->sh_addr; +apply_alternatives (aseg, aseg + alt->sh_size); + } + +if (para) { + pseg = (void *) para->sh_addr; +apply_paravirt (pseg, pseg + para->sh_size); + } +}
Otherwise it looks good.
Regards,
Miroslav
Petr Mladek
maintainerRe: [PATCH v2 2/2] livepatch/x86: apply alternatives and paravirt patches after relocations
Quoted 6 lines · Miroslav Benes
Quoted 4 lines · Jessica Yu
Implement
arch_klp_init_object_loaded()for x86, which applies alternatives/paravirt patches. This fixes the order in which relocations and alternatives/paravirt patches are applied.
Quoted 12 lines
arch/x86/kernel/livepatch.c line 21
Quoted 9 lines
arch/x86/kernel/livepatch.c new file +4
-0+#include <linux/module.h> +#include <linux/kallsyms.h> +#include <linux/livepatch.h> +#include <asm/text-patching.h> It seems I don't have such header file here and it causes a build error. Shouldn't it be <
asm/alternative.h>?
It is in linux-next, see 35de5b0692aaa ("x86/asm: Stop depending on ptrace.h in alternative.h").
I agree with the other Mirek's comments. Otherwise, it looks good.
Thanks for working on it,
Petr
Re: [PATCH v2 2/2] livepatch/x86: apply alternatives and paravirt patches after relocations
In reply to Miroslav BenesQuoted 7 lines · Miroslav Benes
arch/x86/kernel/livepatch.c line 21
Quoted 4 lines
arch/x86/kernel/livepatch.c new file +4
-0+#include <linux/module.h> +#include <linux/kallsyms.h> +#include <linux/livepatch.h> +#include <asm/text-patching.h> It seems I don't have such header file here and it causes a build error. Shouldn't it be <
asm/alternative.h>?
I've now refreshed our for-next branch so that it's based on v4.7 to allow for it being used as a up-to-date basis for development.
(there have been no KLP patches this merge window, which made for-next also quite silent).
Thanks,
Signature · 2 lines
Jiri Kosina SUSE Labs
Quoted 24 lines · Jessica Yu
Hi,
A few months ago, Chris Arges reported a bug involving alternatives/paravirt patching that was discussed here [1] and here [2]. To briefly summarize the bug, patch modules that contained .altinstructions or .parainstructions sections would break because these alternative/paravirt patches would be applied first by the module loader (see x86
module_finalize()), then livepatch would later clobber these patches when applying per-object relocations. This lead to crashes and unpredictable behavior.One conclusion we reached from our last discussion was that we will need to introduce some arch-specific code to address this problem. This patchset presents a possible fix for the bug by adding a new arch-specific
arch_klp_init_object_loaded()function that by default does nothing but can be overridden by different arches.To fix this issue for x86, since we can access a patch module's Elf sections through
mod->klp_info, we can simply delay the calls toapply_paravirt()andapply_alternatives()toarch_klp_init_object_loaded(), which is called after relocations have been written for an object. In addition, for patch modules, .parainstructions and .altinstructions are prefixed by ".klp.arch.${objname}" so that the module loader ignores them and livepatch can apply them manually.
Quoted 8 lines
Currently for kpatch, we don't support including jump table sections in the patch module, and supporting .smp_locks is currently broken, so we don't consider those sections (for now).
I did some light testing with some patches to kvm and verified that the original issue reported in [2] was fixed.
Based on linux-next.
For the series:
Signature · 1 line
Josh