livepatch: Clear relocation targets on a module removal
Subsystems (5)
Song Liu
maintainerPATCH v5 1/1 livepatch: Clear relocation targets on a module removal
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc 00000000ba0302e9, val ffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'
The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When apply_relocate_add() tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.
On ppc64le, we have a similar issue:
module_64: livepatch_nfsd: Expected nop after call, got e8410018 at e_show+0x60/0x548 [livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'
He also proposed three different solutions. We could remove the error check in apply_relocate_add() introduced by commit eda9cec4c9a1 ("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.
We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.
---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.
Changes v3 = v4:
1. Reuse __apply_relocate_add to make it more reliable in long term. (Josh Poimboeuf)
2. Add back ppc64 logic from v2, with changes to match current code. (Josh Poimboeuf)
Changes v2 => v3:
1. Rewrite x86 changes to match current code style.
2. Remove powerpc changes as there is no test coverage in v3.
3. Only keep 1/3 of v2.
v2: https://lore.kernel.org/all/[email protected]/T/#u
--- arch/powerpc/kernel/module_64.c | 49 +++++++++++++++ arch/s390/kernel/module.c | 8 +++ arch/x86/kernel/module.c | 102 +++++++++++++++++++++++--------- include/linux/moduleloader.h | 7 +++ kernel/livepatch/core.c | 41 ++++++++++++- 5 files changed, 179 insertions(+), 28 deletions(-)
arch/powerpc/kernel/module_64.c +49 -0
@@ -739,6 +739,55 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +unsigned int i; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +const char *symname; +u32 *instruction; + +pr_debug ("Clearing ADD relocate section %u to %u\n" , relsec, + sechdrs[relsec].sh_info); + +for (i =0 ; i < sechdrs[relsec].sh_size /sizeof (*rela); i++) { + location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + + rela[i].r_offset; + sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + +ELF64_R_SYM (rela[i].r_info); + symname = me->core_kallsyms.strtab + + sym->st_name; + +if (ELF64_R_TYPE (rela[i].r_info) !=R_PPC_REL24 ) +continue ; +/* +* reverse the operations in apply_relocate_add() for case +* R_PPC_REL24. +*/ +if (sym->st_shndx !=SHN_UNDEF && + sym->st_shndx !=SHN_LIVEPATCH ) +continue ; + + instruction = (u32 *)location; +if (is_mprofile_ftrace_call (symname)) +continue ; + +if (!instr_is_relative_link_branch (ppc_inst (*instruction))) +continue ; + + instruction +=1 ; + *instruction =PPC_RAW_NOP (); + } + +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE int module_trampoline_target (struct module *mod,unsigned long addr,unsigned long *target)
arch/s390/kernel/module.c +8 -0
@@ -500,6 +500,14 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, }#endif /* CONFIG_FUNCTION_TRACER */ +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs,const char *strtab, +unsigned int symindex,unsigned int relsec, +struct module *me) +{ +} +#endif +int module_finalize (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,struct module *me)
arch/x86/kernel/module.c +75 -27
147 lines not on this page · raw message
include/linux/moduleloader.h +7 -0
@@ -72,6 +72,13 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int symindex, unsigned int relsec, struct module *mod); +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me); +#endif #else static inline int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
kernel/livepatch/core.c +40 -1
53 lines not on this page · raw message
Signature · 1 line
2.30.2
Le 30/08/2022 à 20:53, Song Liu a écrit :
Quoted 40 lines
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'He also proposed three different solutions. We could remove the error check in
apply_relocate_add()introduced by commiteda9cec4c9a1("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on
x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.Reported-by Josh Poimboeuf <[email protected]>Signed-off-by Miroslav Benes <[email protected]>Signed-off-by Song Liu <[email protected]>
Quoted 6 lines
---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.
Not completely it seems.
CC kernel/livepatch/core.okernel/livepatch/core.c: In function 'klp_clear_object_relocations':kernel/livepatch/core.c:352:50: error: passing argument 1 of 'clear_relocate_add' from incompatible pointer type [-Werror=incompatible-pointer-types]
352 | clear_relocate_add(pmod->klp_info->sechdrs,
| ~~~~~~~~~~~~~~^~~~~~~~~
| |
| Elf32_Shdr * {aka struct elf32_shdr *}
In file included from kernel/livepatch/core.c:19:./include/linux/moduleloader.h:76:37: note: expected 'Elf64_Shdr *' {aka 'struct elf64_shdr *'} but argument is of type 'Elf32_Shdr *' {aka 'struct elf32_shdr *'}
76 | void clear_relocate_add(Elf64_Shdr *sechdrs,
| ~~~~~~~~~~~~^~~~~~~
cc1: some warnings being treated as errors
Fixup:
include/linux/moduleloader.h +1 -1
@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,
But then the link fails.
LD .tmp_vmlinux.kallsyms1
powerpc64-linux-ld: kernel/livepatch/core.o: in function `klp_cleanup_module_patches_limited':
core.c:(.text+0xdb4): undefined reference to `clear_relocate_add'
Christophe
Quoted 260 lines
Changes v3 = v4:
1. Reuse__apply_relocate_addto make it more reliable in long term. (Josh Poimboeuf)
2. Add back ppc64 logic from v2, with changes to match current code. (Josh Poimboeuf)Changes v2 => v3:
1. Rewrite x86 changes to match current code style.
2. Remove powerpc changes as there is no test coverage in v3.
3. Only keep 1/3 of v2.v2: https://lore.kernel.org/all/[email protected]/T/#u
--- arch/powerpc/kernel/module_64.c | 49 +++++++++++++++ arch/s390/kernel/module.c | 8 +++ arch/x86/kernel/module.c | 102 +++++++++++++++++++++++--------- include/linux/moduleloader.h | 7 +++ kernel/livepatch/core.c | 41 ++++++++++++- 5 files changed, 179 insertions(+), 28 deletions(-)arch/powerpc/kernel/module_64.c +49
-0@@ -739,6 +739,55 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +unsigned int i; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +const char *symname; +u32 *instruction; + +pr_debug ("Clearing ADD relocate section %u to %u\n" , relsec, + sechdrs[relsec].sh_info); + +for (i =0 ; i < sechdrs[relsec].sh_size /sizeof (*rela); i++) { + location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + + rela[i].r_offset; + sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + +ELF64_R_SYM (rela[i].r_info); + symname = me->core_kallsyms.strtab + + sym->st_name; + +if (ELF64_R_TYPE (rela[i].r_info) !=R_PPC_REL24 ) +continue ; +/* +* reverse the operations in apply_relocate_add() for case +* R_PPC_REL24. +*/ +if (sym->st_shndx !=SHN_UNDEF && + sym->st_shndx !=SHN_LIVEPATCH ) +continue ; + + instruction = (u32 *)location; +if (is_mprofile_ftrace_call (symname)) +continue ; + +if (!instr_is_relative_link_branch (ppc_inst (*instruction))) +continue ; + + instruction +=1 ; + *instruction =PPC_RAW_NOP (); + } + +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE int module_trampoline_target (struct module *mod,unsigned long addr,unsigned long *target)arch/s390/kernel/module.c +8
-0@@ -500,6 +500,14 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, }#endif /* CONFIG_FUNCTION_TRACER */ +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs,const char *strtab, +unsigned int symindex,unsigned int relsec, +struct module *me) +{ +} +#endif +int module_finalize (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,struct module *me)arch/x86/kernel/module.c +75
-27@@ -128,18 +128,20 @@ int apply_relocate(Elf32_Shdr *sechdrs, return 0; }#else /*X86_64*/ -static int __apply_relocate_add (Elf64_Shdr *sechdrs, +static int __apply_clear_relocate_add (Elf64_Shdr *sechdrs,const char *strtab,unsigned int symindex,unsigned int relsec,struct module *me, -void *(*write)(void *dest,const void *src,size_t len)) +void *(*write)(void *dest,const void *src,size_t len), +bool clear) {unsigned int i;Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;Elf64_Sym *sym;void *loc;u64 val; +u64 zero =0ULL ;DEBUGP ("Applying relocate section %u to %u\n" , relsec, sechdrs[relsec].sh_info);@@ -163,40 +165,60 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs,case R_X86_64_NONE :break ;case R_X86_64_64 : -if (*(u64 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,8 ); +if (!clear) { +if (*(u64 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,8 ); + }else { +write (loc, &zero,8 ); + }break ;case R_X86_64_32 : -if (*(u32 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,4 ); -if (val != *(u32 *)loc) - goto overflow; +if (!clear) { +if (*(u32 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,4 ); +if (val != *(u32 *)loc) + goto overflow; + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_32S : -if (*(s32 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,4 ); -if ((s64 )val != *(s32 *)loc) - goto overflow; +if (!clear) { +if (*(s32 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,4 ); +if ((s64 )val != *(s32 *)loc) + goto overflow; + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_PC32 :case R_X86_64_PLT32 : -if (*(u32 *)loc !=0 ) - goto invalid_relocation; - val -= (u64 )loc; -write (loc, &val,4 ); +if (!clear) { +if (*(u32 *)loc !=0 ) + goto invalid_relocation; + val -= (u64 )loc; +write (loc, &val,4 );#if 0 -if ((s64 )val != *(s32 *)loc) - goto overflow; +if ((s64 )val != *(s32 *)loc) + goto overflow;#endif + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_PC64 : -if (*(u64 *)loc !=0 ) - goto invalid_relocation; - val -= (u64 )loc; -write (loc, &val,8 ); +if (!clear) { +if (*(u64 *)loc !=0 ) + goto invalid_relocation; + val -= (u64 )loc; +write (loc, &val,8 ); + }else { +write (loc, &zero,8 ); + }break ;default : pr_err("%s: Unknown rela relocation: %llu\n",@@ -234,8 +256,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,mutex_lock (&text_mutex); } - ret =__apply_relocate_add (sechdrs, strtab, symindex, relsec, me, - write); + ret =__apply_clear_relocate_add (sechdrs, strtab, symindex, relsec, me, + write, false/* clear */ );if (!early) {text_poke_sync ();@@ -245,6 +267,32 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return ret; } +#ifdef CONFIG_LIVEPATCH + +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +bool early = me->state ==MODULE_STATE_UNFORMED ; +void *(*write)(void *,const void *,size_t ) = memcpy; + +if (!early) { + write = text_poke; +mutex_lock (&text_mutex); + } + +__apply_clear_relocate_add (sechdrs, strtab, symindex, relsec, me, + write, true/* clear */ ); + +if (!early) { +text_poke_sync (); +mutex_unlock (&text_mutex); + } +} +#endif +#endif int module_finalize(const Elf_Ehdr *hdr,include/linux/moduleloader.h +0
-0
include/linux/moduleloader.h int apply_relocate_add(Elf_Shdr *sechdrs, · line 76
Quoted 5 lines
include/linux/moduleloader.h +2
-0@@ -72,6 +72,13 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int symindex, unsigned int relsec, struct module *mod); +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add(Elf64_Shdr *sechdrs,
Should be Elf_Shdr, otherwise I get :
Quoted 67 lines
include/linux/moduleloader.h +5
-0+const char *strtab, + unsigned int symindex, + unsigned int relsec, + struct module *me); +#endif #else static inline int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,kernel/livepatch/core.c +40
-1@@ -316,6 +316,45 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,return apply_relocate_add (sechdrs, strtab, symndx, secndx, pmod); } +static void klp_clear_object_relocations (struct module *pmod, +struct klp_object *obj) +{ +int i, cnt; +const char *objname, *secname; +char sec_objname[MODULE_NAME_LEN ]; +Elf_Shdr *sec; + + objname =klp_is_module (obj) ? obj->name :"vmlinux" ; + +/* For each klp relocation section */ +for (i =1 ; i < pmod->klp_info->hdr.e_shnum; i++) { + sec = pmod->klp_info->sechdrs + i; + secname = pmod->klp_info->secstrings + sec->sh_name; +if (!(sec->sh_flags &SHF_RELA_LIVEPATCH )) +continue ; + +/* +* Format: .klp.rela.sec_objname.section_name +* See comment in klp_resolve_symbols() for an explanation +* of the selected field width value. +*/ + secname = pmod->klp_info->secstrings + sec->sh_name; + cnt =sscanf (secname,".klp.rela.%55[^.]" , sec_objname); +if (cnt !=1 ) { +pr_err ("section %s has an incorrectly formatted name\n" , + secname); +continue ; + } + +if (strcmp (objname, sec_objname)) +continue ; + +clear_relocate_add (pmod->klp_info->sechdrs, + pmod->core_kallsyms.strtab, + pmod->klp_info->symndx, i, pmod); + } +} +/* * Sysfs Interface * @@ -1154,7 +1193,7 @@ static void klp_cleanup_module_patches_limited(struct module *mod,klp_unpatch_object (obj);klp_post_unpatch_callback (obj); - +klp_clear_object_relocations (patch->mod, obj);klp_free_object_loaded (obj);break ; }
Song Liu
maintainerQuoted 89 lines · Christophe Leroy
Quoted 40 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'He also proposed three different solutions. We could remove the error check in
apply_relocate_add()introduced by commiteda9cec4c9a1("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on
x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.Reported-by Josh Poimboeuf <[email protected]>Signed-off-by Miroslav Benes <[email protected]>Signed-off-by Song Liu <[email protected]>Quoted 6 lines
---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.Not completely it seems.
CC
kernel/livepatch/core.okernel/livepatch/core.c: In function 'klp_clear_object_relocations':kernel/livepatch/core.c:352:50: error: passing argument 1 of 'clear_relocate_add' from incompatible pointer type [-Werror=incompatible-pointer-types]352 | clear_relocate_add(pmod->klp_info->sechdrs, | ~~~~~~~~~~~~~~^~~~~~~~~ | | | Elf32_Shdr *{aka struct
elf32_shdr*}
In file included fromkernel/livepatch/core.c:19:./include/linux/moduleloader.h:76:37: note: expected 'Elf64_Shdr*' {aka 'structelf64_shdr*'} but argument is of type 'Elf32_Shdr*' {aka 'structelf32_shdr*'}
76 | void clear_relocate_add(Elf64_Shdr *sechdrs,
| ~~~~~~~~~~~~^~~~~~~
cc1: some warnings being treated as errorsFixup:
include/linux/moduleloader.h +1
-1@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,
Quoted 6 lines
But then the link fails.
LD .tmp_vmlinux.kallsyms1
powerpc64-linux-ld:kernel/livepatch/core.o: in function `klp_cleanup_module_patches_limited':
core.c:(.text+0xdb4): undefined reference to `clear_relocate_add'
Hmm.. I am not seeing either error. Could you please share your .config file?
Thanks,
Song
Le 31/08/2022 à 19:05, Song Liu a écrit :
Quoted 91 lines
Quoted 89 lines · Christophe Leroy
Quoted 40 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'He also proposed three different solutions. We could remove the error check in
apply_relocate_add()introduced by commiteda9cec4c9a1("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on
x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.Reported-by Josh Poimboeuf <[email protected]>Signed-off-by Miroslav Benes <[email protected]>Signed-off-by Song Liu <[email protected]>Quoted 6 lines
---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.Not completely it seems.
CC
kernel/livepatch/core.okernel/livepatch/core.c: In function 'klp_clear_object_relocations':kernel/livepatch/core.c:352:50: error: passing argument 1 of 'clear_relocate_add' from incompatible pointer type [-Werror=incompatible-pointer-types]352 | clear_relocate_add(pmod->klp_info->sechdrs, | ~~~~~~~~~~~~~~^~~~~~~~~ | | | Elf32_Shdr *{aka struct
elf32_shdr*}
In file included fromkernel/livepatch/core.c:19:./include/linux/moduleloader.h:76:37: note: expected 'Elf64_Shdr*' {aka 'structelf64_shdr*'} but argument is of type 'Elf32_Shdr*' {aka 'structelf32_shdr*'}
76 | void clear_relocate_add(Elf64_Shdr *sechdrs,
| ~~~~~~~~~~~~^~~~~~~
cc1: some warnings being treated as errorsFixup:
include/linux/moduleloader.h +1
-1@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,
Quoted 9 lines
Quoted 6 lines
But then the link fails.
LD .tmp_vmlinux.kallsyms1
powerpc64-linux-ld:kernel/livepatch/core.o: in function `klp_cleanup_module_patches_limited':
core.c:(.text+0xdb4): undefined reference to `clear_relocate_add'Hmm.. I am not seeing either error. Could you please share your .config file?
defconfig follows:
# CONFIG_LOCALVERSION_AUTO is not setCONFIG_SYSVIPC=yCONFIG_POSIX_MQUEUE=yCONFIG_NO_HZ=yCONFIG_HIGH_RES_TIMERS=yCONFIG_IKCONFIG=yCONFIG_IKCONFIG_PROC=yCONFIG_LOG_BUF_SHIFT=14CONFIG_BLK_DEV_INITRD=yCONFIG_KALLSYMS_ALL=yCONFIG_PROFILING=yCONFIG_ALTIVEC=y
# CONFIG_PPC_CHRP is not setCONFIG_CPU_FREQ=yCONFIG_CPU_FREQ_GOV_POWERSAVE=yCONFIG_CPU_FREQ_GOV_USERSPACE=yCONFIG_CPU_FREQ_PMAC=yCONFIG_GEN_RTC=yCONFIG_HIGHMEM=yCONFIG_HIBERNATION=yCONFIG_PM_DEBUG=yCONFIG_APM_EMULATION=yCONFIG_LIVEPATCH=yCONFIG_MODULES=yCONFIG_MODULE_UNLOAD=yCONFIG_MODULE_FORCE_UNLOAD=yCONFIG_PARTITION_ADVANCED=yCONFIG_BINFMT_MISC=m
# CONFIG_COMPAT_BRK is not setCONFIG_NET=yCONFIG_PACKET=yCONFIG_UNIX=yCONFIG_XFRM_USER=yCONFIG_NET_KEY=yCONFIG_INET=yCONFIG_IP_MULTICAST=yCONFIG_SYN_COOKIES=yCONFIG_INET_AH=yCONFIG_INET_ESP=y
# CONFIG_IPV6 is not setCONFIG_NETFILTER=yCONFIG_NF_CONNTRACK=mCONFIG_NF_CONNTRACK_FTP=mCONFIG_NF_CONNTRACK_IRC=mCONFIG_NF_CONNTRACK_TFTP=mCONFIG_NF_CT_NETLINK=mCONFIG_NETFILTER_XT_TARGET_CLASSIFY=mCONFIG_NETFILTER_XT_TARGET_MARK=mCONFIG_NETFILTER_XT_TARGET_NFLOG=mCONFIG_NETFILTER_XT_TARGET_NFQUEUE=mCONFIG_NETFILTER_XT_TARGET_TRACE=mCONFIG_NETFILTER_XT_TARGET_TCPMSS=mCONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=mCONFIG_NETFILTER_XT_MATCH_COMMENT=mCONFIG_NETFILTER_XT_MATCH_CONNLIMIT=mCONFIG_NETFILTER_XT_MATCH_CONNTRACK=mCONFIG_NETFILTER_XT_MATCH_DSCP=mCONFIG_NETFILTER_XT_MATCH_ESP=mCONFIG_NETFILTER_XT_MATCH_HELPER=mCONFIG_NETFILTER_XT_MATCH_IPRANGE=mCONFIG_NETFILTER_XT_MATCH_LENGTH=mCONFIG_NETFILTER_XT_MATCH_LIMIT=mCONFIG_NETFILTER_XT_MATCH_MAC=mCONFIG_NETFILTER_XT_MATCH_MARK=mCONFIG_NETFILTER_XT_MATCH_MULTIPORT=mCONFIG_NETFILTER_XT_MATCH_OWNER=mCONFIG_NETFILTER_XT_MATCH_POLICY=mCONFIG_NETFILTER_XT_MATCH_PKTTYPE=mCONFIG_NETFILTER_XT_MATCH_RATEEST=mCONFIG_NETFILTER_XT_MATCH_REALM=mCONFIG_NETFILTER_XT_MATCH_RECENT=mCONFIG_NETFILTER_XT_MATCH_SCTP=mCONFIG_NETFILTER_XT_MATCH_STRING=mCONFIG_NETFILTER_XT_MATCH_TCPMSS=mCONFIG_NETFILTER_XT_MATCH_TIME=mCONFIG_NETFILTER_XT_MATCH_U32=mCONFIG_IP_NF_IPTABLES=mCONFIG_IP_NF_MATCH_AH=mCONFIG_IP_NF_MATCH_ECN=mCONFIG_IP_NF_MATCH_TTL=mCONFIG_IP_NF_FILTER=mCONFIG_IP_NF_TARGET_REJECT=mCONFIG_IP_NF_MANGLE=mCONFIG_IP_NF_TARGET_ECN=mCONFIG_IP_NF_TARGET_TTL=mCONFIG_IP_NF_RAW=mCONFIG_IP_NF_ARPTABLES=mCONFIG_IP_NF_ARPFILTER=mCONFIG_IP_NF_ARP_MANGLE=mCONFIG_IP_DCCP=mCONFIG_BT=mCONFIG_BT_RFCOMM=mCONFIG_BT_RFCOMM_TTY=yCONFIG_BT_BNEP=mCONFIG_BT_BNEP_MC_FILTER=yCONFIG_BT_BNEP_PROTO_FILTER=yCONFIG_BT_HIDP=mCONFIG_BT_HCIBCM203X=mCONFIG_BT_HCIBFUSB=mCONFIG_CFG80211=mCONFIG_MAC80211=mCONFIG_MAC80211_LEDS=yCONFIG_PCCARD=mCONFIG_YENTA=m
# CONFIG_STANDALONE is not setCONFIG_CONNECTOR=yCONFIG_MAC_FLOPPY=mCONFIG_BLK_DEV_LOOP=yCONFIG_BLK_DEV_RAM=yCONFIG_BLK_DEV_SD=yCONFIG_CHR_DEV_ST=yCONFIG_BLK_DEV_SR=yCONFIG_CHR_DEV_SG=yCONFIG_SCSI_CONSTANTS=yCONFIG_SCSI_FC_ATTRS=yCONFIG_SCSI_AIC7XXX=mCONFIG_AIC7XXX_CMDS_PER_DEVICE=253CONFIG_AIC7XXX_RESET_DELAY_MS=15000CONFIG_SCSI_SYM53C8XX_2=yCONFIG_SCSI_SYM53C8XX_DMA_ADDRESSING_MODE=0CONFIG_SCSI_MESH=yCONFIG_SCSI_MAC53C94=yCONFIG_ATA=yCONFIG_PATA_MACIO=yCONFIG_PATA_PDC2027X=yCONFIG_PATA_WINBOND=yCONFIG_PATA_PCMCIA=mCONFIG_ATA_GENERIC=yCONFIG_MD=yCONFIG_BLK_DEV_MD=mCONFIG_MD_LINEAR=mCONFIG_MD_RAID0=mCONFIG_MD_RAID1=mCONFIG_MD_RAID10=mCONFIG_MD_MULTIPATH=mCONFIG_MD_FAULTY=mCONFIG_BLK_DEV_DM=mCONFIG_DM_CRYPT=mCONFIG_DM_SNAPSHOT=mCONFIG_DM_MIRROR=mCONFIG_DM_ZERO=mCONFIG_ADB=yCONFIG_ADB_CUDA=yCONFIG_ADB_PMU=yCONFIG_ADB_PMU_LED=yCONFIG_ADB_PMU_LED_DISK=yCONFIG_PMAC_APM_EMU=mCONFIG_PMAC_MEDIABAY=yCONFIG_PMAC_BACKLIGHT=yCONFIG_PMAC_BACKLIGHT_LEGACY=yCONFIG_INPUT_ADBHID=yCONFIG_MAC_EMUMOUSEBTN=yCONFIG_THERM_WINDTUNNEL=mCONFIG_THERM_ADT746X=mCONFIG_PMAC_RACKMETER=mCONFIG_NETDEVICES=yCONFIG_DUMMY=mCONFIG_TUN=mCONFIG_PCNET32=yCONFIG_MACE=yCONFIG_BMAC=yCONFIG_SUNGEM=yCONFIG_PPP=yCONFIG_PPP_BSDCOMP=mCONFIG_PPP_DEFLATE=yCONFIG_PPP_MULTILINK=yCONFIG_PPP_ASYNC=yCONFIG_PPP_SYNC_TTY=mCONFIG_USB_USBNET=m
# CONFIG_USB_NET_CDC_SUBSET is not setCONFIG_B43=mCONFIG_B43LEGACY=mCONFIG_P54_COMMON=mCONFIG_INPUT_EVDEV=y
# CONFIG_KEYBOARD_ATKBD is not set
# CONFIG_MOUSE_PS2 is not setCONFIG_MOUSE_APPLETOUCH=y
# CONFIG_SERIO_I8042 is not set
# CONFIG_SERIO_SERPORT is not setCONFIG_SERIAL_8250=mCONFIG_SERIAL_PMACZILOG=mCONFIG_SERIAL_PMACZILOG_TTYS=yCONFIG_I2C_CHARDEV=mCONFIG_APM_POWER=yCONFIG_BATTERY_PMU=yCONFIG_HWMON=mCONFIG_AGP=mCONFIG_AGP_UNINORTH=mCONFIG_DRM=mCONFIG_DRM_RADEON=mCONFIG_DRM_LEGACY=yCONFIG_DRM_R128=mCONFIG_FB=yCONFIG_FB_OF=yCONFIG_FB_CONTROL=yCONFIG_FB_PLATINUM=yCONFIG_FB_VALKYRIE=yCONFIG_FB_CT65550=yCONFIG_FB_IMSTT=yCONFIG_FB_NVIDIA=yCONFIG_FB_NVIDIA_I2C=yCONFIG_FB_MATROX=yCONFIG_FB_MATROX_MILLENIUM=yCONFIG_FB_MATROX_MYSTIQUE=yCONFIG_FB_RADEON=yCONFIG_FB_ATY128=yCONFIG_FB_ATY=yCONFIG_FB_ATY_CT=yCONFIG_FB_ATY_GX=yCONFIG_FB_3DFX=y
# CONFIG_VGA_CONSOLE is not setCONFIG_LOGO=yCONFIG_SOUND=mCONFIG_SND=mCONFIG_SND_OSSEMUL=yCONFIG_SND_MIXER_OSS=mCONFIG_SND_PCM_OSS=mCONFIG_SND_SEQUENCER=mCONFIG_SND_SEQ_DUMMY=mCONFIG_SND_SEQUENCER_OSS=mCONFIG_SND_DUMMY=mCONFIG_SND_POWERMAC=mCONFIG_SND_AOA=mCONFIG_SND_AOA_FABRIC_LAYOUT=mCONFIG_SND_AOA_ONYX=mCONFIG_SND_AOA_TAS=mCONFIG_SND_AOA_TOONIE=mCONFIG_SND_USB_AUDIO=mCONFIG_HID_GYRATION=yCONFIG_HID_NTRIG=yCONFIG_HID_PANTHERLORD=yCONFIG_HID_PETALYNX=yCONFIG_HID_SAMSUNG=yCONFIG_HID_SONY=yCONFIG_HID_SUNPLUS=yCONFIG_HID_TOPSEED=yCONFIG_USB_DYNAMIC_MINORS=yCONFIG_USB_MON=yCONFIG_USB_EHCI_HCD=mCONFIG_USB_EHCI_ROOT_HUB_TT=y
# CONFIG_USB_EHCI_HCD_PPC_OF is not setCONFIG_USB_OHCI_HCD=yCONFIG_USB_ACM=mCONFIG_USB_PRINTER=mCONFIG_USB_STORAGE=mCONFIG_USB_STORAGE_ONETOUCH=mCONFIG_USB_SERIAL=mCONFIG_USB_SERIAL_VISOR=mCONFIG_USB_SERIAL_IPAQ=mCONFIG_USB_SERIAL_KEYSPAN_PDA=mCONFIG_USB_SERIAL_KEYSPAN=mCONFIG_USB_APPLEDISPLAY=mCONFIG_LEDS_TRIGGER_DEFAULT_ON=yCONFIG_EXT2_FS=yCONFIG_EXT4_FS=yCONFIG_EXT4_FS_POSIX_ACL=yCONFIG_AUTOFS4_FS=mCONFIG_FUSE_FS=mCONFIG_ISO9660_FS=yCONFIG_JOLIET=yCONFIG_ZISOFS=yCONFIG_UDF_FS=mCONFIG_MSDOS_FS=mCONFIG_VFAT_FS=mCONFIG_PROC_KCORE=yCONFIG_TMPFS=yCONFIG_HFS_FS=mCONFIG_HFSPLUS_FS=mCONFIG_NFS_FS=yCONFIG_NFS_V3_ACL=yCONFIG_NFS_V4=yCONFIG_NFSD=mCONFIG_NFSD_V3_ACL=yCONFIG_NFSD_V4=yCONFIG_NLS_CODEPAGE_437=mCONFIG_NLS_ISO8859_1=mCONFIG_CRYPTO_PCBC=mCONFIG_CRYPTO_MD4=mCONFIG_CRYPTO_WP512=mCONFIG_CRYPTO_BLOWFISH=mCONFIG_CRYPTO_CAST5=mCONFIG_CRYPTO_CAST6=mCONFIG_CRYPTO_SERPENT=mCONFIG_CRYPTO_TWOFISH=mCONFIG_CRYPTO_DEFLATE=mCONFIG_CRC_T10DIF=yCONFIG_DEBUG_KERNEL=yCONFIG_MAGIC_SYSRQ=yCONFIG_DETECT_HUNG_TASK=yCONFIG_FUNCTION_TRACER=yCONFIG_XMON=yCONFIG_XMON_DEFAULT=yCONFIG_BOOTX_TEXT=y
Joe Lawrence
maintainerQuoted 100 lines · Song Liu
Quoted 89 lines · Christophe Leroy
Quoted 40 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'He also proposed three different solutions. We could remove the error check in
apply_relocate_add()introduced by commiteda9cec4c9a1("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on
x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.Reported-by Josh Poimboeuf <[email protected]>Signed-off-by Miroslav Benes <[email protected]>Signed-off-by Song Liu <[email protected]>Quoted 6 lines
---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.Not completely it seems.
CC
kernel/livepatch/core.okernel/livepatch/core.c: In function 'klp_clear_object_relocations':kernel/livepatch/core.c:352:50: error: passing argument 1 of 'clear_relocate_add' from incompatible pointer type [-Werror=incompatible-pointer-types]352 | clear_relocate_add(pmod->klp_info->sechdrs, | ~~~~~~~~~~~~~~^~~~~~~~~ | | | Elf32_Shdr *{aka struct
elf32_shdr*}
In file included fromkernel/livepatch/core.c:19:./include/linux/moduleloader.h:76:37: note: expected 'Elf64_Shdr*' {aka 'structelf64_shdr*'} but argument is of type 'Elf32_Shdr*' {aka 'structelf32_shdr*'}
76 | void clear_relocate_add(Elf64_Shdr *sechdrs,
| ~~~~~~~~~~~~^~~~~~~
cc1: some warnings being treated as errorsFixup:
include/linux/moduleloader.h +1
-1@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,Quoted 6 lines
But then the link fails.
LD .tmp_vmlinux.kallsyms1
powerpc64-linux-ld:kernel/livepatch/core.o: in function `klp_cleanup_module_patches_limited':
core.c:(.text+0xdb4): undefined reference to `clear_relocate_add'Hmm.. I am not seeing either error. Could you please share your .config file?
Quoted 3 lines
Thanks,
Song
If it's any help, I see the same build error Christophe reported using the 'cross-dev' script that's in my klp-convert-tree [1].
$ BUILD_ARCHES="ppc32" ./cross-dev config $ BUILD_ARCHES="ppc32" ./cross-dev build -j$(nproc)
(The kernel will be built in /tmp/klp-convert-ppc32 btw.)
Applying the header file fix results in the same linker error, too.
[1] https://github.com/joe-lawrence/klp-convert-tree/tree/klp-convert-v7-devel+song
-- Joe
Joe Lawrence
maintainerQuoted 381 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'He also proposed three different solutions. We could remove the error check in
apply_relocate_add()introduced by commiteda9cec4c9a1("x86/module: Detect and skip invalid relocations"). However the check is useful for detecting corrupted modules.We could also deny the patched modules to be removed. If it proved to be a major drawback for users, we could still implement a different approach. The solution would also complicate the existing code a lot.
We thus decided to reverse the relocation patching (clear all relocation targets on
x86_64). The solution is not
universal and is too much arch-specific, but it may prove to be simpler in the end.Reported-by Josh Poimboeuf <[email protected]>Signed-off-by Miroslav Benes <[email protected]>Signed-off-by Song Liu <[email protected]>---
NOTE: powerpc code has not be tested.
Changes v4 = v5:
1. Fix compile with powerpc.Changes v3 = v4:
1. Reuse__apply_relocate_addto make it more reliable in long term. (Josh Poimboeuf)
2. Add back ppc64 logic from v2, with changes to match current code. (Josh Poimboeuf)Changes v2 => v3:
1. Rewrite x86 changes to match current code style.
2. Remove powerpc changes as there is no test coverage in v3.
3. Only keep 1/3 of v2.v2: https://lore.kernel.org/all/[email protected]/T/#u
--- arch/powerpc/kernel/module_64.c | 49 +++++++++++++++ arch/s390/kernel/module.c | 8 +++ arch/x86/kernel/module.c | 102 +++++++++++++++++++++++--------- include/linux/moduleloader.h | 7 +++ kernel/livepatch/core.c | 41 ++++++++++++- 5 files changed, 179 insertions(+), 28 deletions(-)arch/powerpc/kernel/module_64.c +49
-0@@ -739,6 +739,55 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +unsigned int i; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +const char *symname; +u32 *instruction; + +pr_debug ("Clearing ADD relocate section %u to %u\n" , relsec, + sechdrs[relsec].sh_info); + +for (i =0 ; i < sechdrs[relsec].sh_size /sizeof (*rela); i++) { + location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr + + rela[i].r_offset; + sym = (Elf64_Sym *)sechdrs[symindex].sh_addr + +ELF64_R_SYM (rela[i].r_info); + symname = me->core_kallsyms.strtab + + sym->st_name; + +if (ELF64_R_TYPE (rela[i].r_info) !=R_PPC_REL24 ) +continue ; +/* +* reverse the operations in apply_relocate_add() for case +* R_PPC_REL24. +*/ +if (sym->st_shndx !=SHN_UNDEF && + sym->st_shndx !=SHN_LIVEPATCH ) +continue ; + + instruction = (u32 *)location; +if (is_mprofile_ftrace_call (symname)) +continue ; + +if (!instr_is_relative_link_branch (ppc_inst (*instruction))) +continue ; + + instruction +=1 ; + *instruction =PPC_RAW_NOP (); + } + +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE int module_trampoline_target (struct module *mod,unsigned long addr,unsigned long *target)arch/s390/kernel/module.c +8
-0@@ -500,6 +500,14 @@ static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, }#endif /* CONFIG_FUNCTION_TRACER */ +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs,const char *strtab, +unsigned int symindex,unsigned int relsec, +struct module *me) +{ +} +#endif +int module_finalize (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,struct module *me)arch/x86/kernel/module.c +75
-27@@ -128,18 +128,20 @@ int apply_relocate(Elf32_Shdr *sechdrs, return 0; }#else /*X86_64*/ -static int __apply_relocate_add (Elf64_Shdr *sechdrs, +static int __apply_clear_relocate_add (Elf64_Shdr *sechdrs,const char *strtab,unsigned int symindex,unsigned int relsec,struct module *me, -void *(*write)(void *dest,const void *src,size_t len)) +void *(*write)(void *dest,const void *src,size_t len), +bool clear) {unsigned int i;Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;Elf64_Sym *sym;void *loc;u64 val; +u64 zero =0ULL ;DEBUGP ("Applying relocate section %u to %u\n" , relsec, sechdrs[relsec].sh_info);@@ -163,40 +165,60 @@ static int __apply_relocate_add(Elf64_Shdr *sechdrs,case R_X86_64_NONE :break ;case R_X86_64_64 : -if (*(u64 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,8 ); +if (!clear) { +if (*(u64 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,8 ); + }else { +write (loc, &zero,8 ); + }break ;case R_X86_64_32 : -if (*(u32 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,4 ); -if (val != *(u32 *)loc) - goto overflow; +if (!clear) { +if (*(u32 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,4 ); +if (val != *(u32 *)loc) + goto overflow; + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_32S : -if (*(s32 *)loc !=0 ) - goto invalid_relocation; -write (loc, &val,4 ); -if ((s64 )val != *(s32 *)loc) - goto overflow; +if (!clear) { +if (*(s32 *)loc !=0 ) + goto invalid_relocation; +write (loc, &val,4 ); +if ((s64 )val != *(s32 *)loc) + goto overflow; + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_PC32 :case R_X86_64_PLT32 : -if (*(u32 *)loc !=0 ) - goto invalid_relocation; - val -= (u64 )loc; -write (loc, &val,4 ); +if (!clear) { +if (*(u32 *)loc !=0 ) + goto invalid_relocation; + val -= (u64 )loc; +write (loc, &val,4 );#if 0 -if ((s64 )val != *(s32 *)loc) - goto overflow; +if ((s64 )val != *(s32 *)loc) + goto overflow;#endif + }else { +write (loc, &zero,4 ); + }break ;case R_X86_64_PC64 : -if (*(u64 *)loc !=0 ) - goto invalid_relocation; - val -= (u64 )loc; -write (loc, &val,8 ); +if (!clear) { +if (*(u64 *)loc !=0 ) + goto invalid_relocation; + val -= (u64 )loc; +write (loc, &val,8 ); + }else { +write (loc, &zero,8 ); + }break ;default : pr_err("%s: Unknown rela relocation: %llu\n",@@ -234,8 +256,8 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,mutex_lock (&text_mutex); } - ret =__apply_relocate_add (sechdrs, strtab, symindex, relsec, me, - write); + ret =__apply_clear_relocate_add (sechdrs, strtab, symindex, relsec, me, + write, false/* clear */ );if (!early) {text_poke_sync ();@@ -245,6 +267,32 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return ret; } +#ifdef CONFIG_LIVEPATCH + +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +bool early = me->state ==MODULE_STATE_UNFORMED ; +void *(*write)(void *,const void *,size_t ) = memcpy; + +if (!early) { + write = text_poke; +mutex_lock (&text_mutex); + } + +__apply_clear_relocate_add (sechdrs, strtab, symindex, relsec, me, + write, true/* clear */ ); + +if (!early) { +text_poke_sync (); +mutex_unlock (&text_mutex); + } +} +#endif +#endif int module_finalize(const Elf_Ehdr *hdr,include/linux/moduleloader.h +7
-0@@ -72,6 +72,13 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int symindex, unsigned int relsec, struct module *mod); +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf64_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me); +#endif #else static inline int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,kernel/livepatch/core.c +40
-1@@ -316,6 +316,45 @@ int klp_apply_section_relocs(struct module *pmod, Elf_Shdr *sechdrs,return apply_relocate_add (sechdrs, strtab, symndx, secndx, pmod); } +static void klp_clear_object_relocations (struct module *pmod, +struct klp_object *obj) +{ +int i, cnt; +const char *objname, *secname; +char sec_objname[MODULE_NAME_LEN ]; +Elf_Shdr *sec; + + objname =klp_is_module (obj) ? obj->name :"vmlinux" ; + +/* For each klp relocation section */ +for (i =1 ; i < pmod->klp_info->hdr.e_shnum; i++) { + sec = pmod->klp_info->sechdrs + i; + secname = pmod->klp_info->secstrings + sec->sh_name; +if (!(sec->sh_flags &SHF_RELA_LIVEPATCH )) +continue ; + +/* +* Format: .klp.rela.sec_objname.section_name +* See comment in klp_resolve_symbols() for an explanation +* of the selected field width value. +*/ + secname = pmod->klp_info->secstrings + sec->sh_name; + cnt =sscanf (secname,".klp.rela.%55[^.]" , sec_objname); +if (cnt !=1 ) { +pr_err ("section %s has an incorrectly formatted name\n" , + secname); +continue ; + } + +if (strcmp (objname, sec_objname)) +continue ; + +clear_relocate_add (pmod->klp_info->sechdrs, + pmod->core_kallsyms.strtab, + pmod->klp_info->symndx, i, pmod); + } +} +/* * Sysfs Interface * @@ -1154,7 +1193,7 @@ static void klp_cleanup_module_patches_limited(struct module *mod,klp_unpatch_object (obj);klp_post_unpatch_callback (obj); - +klp_clear_object_relocations (patch->mod, obj);klp_free_object_loaded (obj);break ; }Signature · 1 line
2.30.2
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (tools/testing/selftests/livepatch/test-song.sh) such that:
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch module
The results:
x86_64 : pass
s390x : pass
ppc64le : crash
I suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobe test_klp_convert1test_klp_convert1: tainting kernel with TAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobe test_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1: saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1: driver_name, 0: test_klp_convert_modtest_klp_convert1: test_klp_get_driver_name(), 0: test_klp_convert_modtest_klp_convert1: homonym_string, 1: homonym string Atest_klp_convert1: get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 = lib/livepatch/test_klp_convert_mod_a.c static stringtest_klp_convert1: klp_string.67890 = lib/livepatch/test_klp_convert_mod_b.c static string
% rmmod test_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0
GPR00: c000000000056568 c000000007223ae0 c000000002a68a00 0000000000000001
GPR04: c0080000021706f0 000000000000002d 0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000 c00000000ffca080 0000000000000000 0000000000000000
GPR16: 0000010005bf1810 000000010c0f7370 c0000000011b7e50 c0000000011b7e68
GPR20: c0080000021501c8 c008000002150228 0000000000000030 0000000060000000
GPR24: c008000002160380 c000000056b43000 000000000000ff20 c000000056b43c00
GPR28: aaaaaaaaaaaaaaab c000000056b43b40 0000000000000000 c00800000214014c NIP [c00000000005659c] clear_relocate_add+0x11c/0x1c0
LR [c000000000056590] clear_relocate_add+0x110/0x1c0
Call Trace:
[c000000007223ae0] [ffffffffffffffff] 0xffffffffffffffff (unreliable) [c000000007223ba0] [c00000000021e3a8] klp_cleanup_module_patches_limited+0x448/0x480 [c000000007223cb0] [c000000000220278] klp_module_going+0x68/0x94 [c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004] system_call_exception+0x164/0x340 [c000000007223e10] [c00000000000be68] system_call_vectored_common+0xe8/0x278 --- interrupt: 3000 at 0x7fffa178fb6c
NIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000
NIP [00007fffa178fb6c] 0x7fffa178fb6c
LR [0000000000000000] 0x0
--- interrupt: 3000
Instruction dump:
40820044 813b002c 7ff5f82a 79293664 7d394a14 e9290010 7c69f82e 7fe9fa14 48052235 60000000 2c030000 41820008 <92ff0004> eadb0020 60000000 60000000
---[ end trace 0000000000000000 ]---
$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785
743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
...
785 *instruction = PPC_RAW_NOP();
786 }
$ readelf --wide --sections test_klp_convert1.ko | grep -e '\[ 6\]' -e '\[48\]' [ 6] .text.unlikely PROGBITS 0000000000000000 0001b8 0001cc 00 AX 0 0 4 [48] .klp.rela.test_klp_convert_mod..text.unlikely RELA 0000000000000000 041358 000030 18 Ao 45 6 8
$ readelf --wide --relocs test_klp_convert1.ko
...
Relocation section '.klp.rela.test_klp_convert_mod..text.unlikely' at offset 0x41358 contains 2 entries:
Offset Info Type Symbol's Value Symbol's Name + Addend
00000000000000f0 000000490000000a R_PPC64_REL24 0000000000000000 .klp.sym.test_klp_convert_mod.test_klp_get_driver_name,0 + 0
0000000000000158 000000450000000a R_PPC64_REL24 0000000000000000 .klp.sym.test_klp_convert_mod.get_homonym_string,1 + 0PS - I will be OOTO for a few weeks in Sept
[1] https://github.com/joe-lawrence/klp-convert-tree/tree/klp-convert-v7-devel+song
-- Joe
Michael Ellerman
reviewerQuoted 22 lines · Joe Lawrence
Quoted 21 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'
...
Quoted 8 lines
Quoted 1 line
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (
tools/testing/selftests/livepatch/test-song.sh) such that:
Quoted 7 lines
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch module
If you push that test code somewhere I could test it on ppc64le.
Quoted 28 lines
The results:
x86_64: pass
s390x : pass
ppc64le : crashI suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobetest_klp_convert1test_klp_convert1: tainting kernel withTAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobetest_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1:saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1:driver_name, 0:test_klp_convert_modtest_klp_convert1:test_klp_get_driver_name(), 0:test_klp_convert_modtest_klp_convert1:homonym_string, 1: homonym string Atest_klp_convert1:get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 =lib/livepatch/test_klp_convert_mod_a.cstatic stringtest_klp_convert1: klp_string.67890 =lib/livepatch/test_klp_convert_mod_b.cstatic string
% rmmodtest_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6
Quoted 10 lines
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0
This is saying you don't have permissions to write at that address.
Quoted 46 lines
GPR00:
c000000000056568c000000007223ae0c000000002a68a000000000000000001
GPR04:c0080000021706f0000000000000002d0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000c00000000ffca0800000000000000000 0000000000000000
GPR16:0000010005bf1810000000010c0f7370c0000000011b7e50c0000000011b7e68
GPR20:c0080000021501c8c0080000021502280000000000000030 0000000060000000
GPR24:c008000002160380c000000056b43000000000000000ff20c000000056b43c00
GPR28: aaaaaaaaaaaaaaabc000000056b43b400000000000000000c00800000214014cNIP [c00000000005659c]clear_relocate_add+0x11c/0x1c0
LR [c000000000056590]clear_relocate_add+0x110/0x1c0Call Trace:[
c000000007223ae0] [ffffffffffffffff]0xffffffffffffffff(unreliable) [c000000007223ba0] [c00000000021e3a8]klp_cleanup_module_patches_limited+0x448/0x480[c000000007223cb0] [c000000000220278]klp_module_going+0x68/0x94[c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004]system_call_exception+0x164/0x340[c000000007223e10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffa178fb6cNIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000NIP [
00007fffa178fb6c]0x7fffa178fb6c
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:
40820044813b002c7ff5f82a792936647d394a14e92900107c69f82e7fe9fa1448052235 600000002c03000041820008 <92ff0004>eadb002060000000 60000000---[ end trace 0000000000000000 ]---$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)
Quoted 6 lines
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size /sizeof(*rela); i++) {
...
785 *instruction =PPC_RAW_NOP();
786 }
Has the module text been marked RW prior to this? I suspect not?
In which case you need to use patch_instruction() here.
cheers
Song Liu
maintainerQuoted 137 lines · Michael Ellerman
Quoted 22 lines · Joe Lawrence
Quoted 21 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'...
Quoted 8 lines
Quoted 1 line
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (
tools/testing/selftests/livepatch/test-song.sh) such that:Quoted 7 lines
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch moduleIf you push that test code somewhere I could test it on ppc64le.
Quoted 28 lines
The results:
x86_64: pass
s390x : pass
ppc64le : crashI suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobetest_klp_convert1test_klp_convert1: tainting kernel withTAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobetest_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1:saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1:driver_name, 0:test_klp_convert_modtest_klp_convert1:test_klp_get_driver_name(), 0:test_klp_convert_modtest_klp_convert1:homonym_string, 1: homonym string Atest_klp_convert1:get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 =lib/livepatch/test_klp_convert_mod_a.cstatic stringtest_klp_convert1: klp_string.67890 =lib/livepatch/test_klp_convert_mod_b.cstatic string
% rmmodtest_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6Quoted 10 lines
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0This is saying you don't have permissions to write at that address.
Quoted 46 lines
GPR00:
c000000000056568c000000007223ae0c000000002a68a000000000000000001
GPR04:c0080000021706f0000000000000002d0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000c00000000ffca0800000000000000000 0000000000000000
GPR16:0000010005bf1810000000010c0f7370c0000000011b7e50c0000000011b7e68
GPR20:c0080000021501c8c0080000021502280000000000000030 0000000060000000
GPR24:c008000002160380c000000056b43000000000000000ff20c000000056b43c00
GPR28: aaaaaaaaaaaaaaabc000000056b43b400000000000000000c00800000214014cNIP [c00000000005659c]clear_relocate_add+0x11c/0x1c0
LR [c000000000056590]clear_relocate_add+0x110/0x1c0Call Trace:[
c000000007223ae0] [ffffffffffffffff]0xffffffffffffffff(unreliable) [c000000007223ba0] [c00000000021e3a8]klp_cleanup_module_patches_limited+0x448/0x480[c000000007223cb0] [c000000000220278]klp_module_going+0x68/0x94[c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004]system_call_exception+0x164/0x340[c000000007223e10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffa178fb6cNIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000NIP [
00007fffa178fb6c]0x7fffa178fb6c
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:
40820044813b002c7ff5f82a792936647d394a14e92900107c69f82e7fe9fa1448052235 600000002c03000041820008 <92ff0004>eadb002060000000 60000000---[ end trace 0000000000000000 ]---$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)Quoted 6 lines
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size /sizeof(*rela); i++) {
...
785 *instruction =PPC_RAW_NOP();
786 }
Quoted 5 lines
Has the module text been marked RW prior to this? I suspect not?
In which case you need to use
patch_instruction()here.cheers
Thanks folks!
I guess something like this would fix compile for ppc32 and fix crash for ppc64.
I also pushed it to
https://git.kernel.org/pub/scm/linux/kernel/git/song/linux.git/log/?h=klp-module-reload
This includes Joe's klp-convert patches and this patch.
Thanks!
Song
arch/powerpc/kernel/module_32.c +10 -0
@@ -285,6 +285,16 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf32_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE notrace int module_trampoline_target(struct module *mod, unsigned long addr,unsigned long *target)
arch/powerpc/kernel/module_64.c +1 -1
@@ -782,7 +782,7 @@ void clear_relocate_add(Elf64_Shdr *sechdrs,continue ; instruction +=1 ; - *instruction =PPC_RAW_NOP (); +patch_instruction (instruction,PPC_RAW_NOP ()); } }
include/linux/moduleloader.h +1 -1
@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,
Quoted 185 lines · Song Liu
Quoted 137 lines · Michael Ellerman
Quoted 22 lines · Joe Lawrence
Quoted 21 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'...
Quoted 8 lines
Quoted 1 line
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (
tools/testing/selftests/livepatch/test-song.sh) such that:Quoted 7 lines
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch moduleIf you push that test code somewhere I could test it on ppc64le.
Quoted 28 lines
The results:
x86_64: pass
s390x : pass
ppc64le : crashI suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobetest_klp_convert1test_klp_convert1: tainting kernel withTAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobetest_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1:saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1:driver_name, 0:test_klp_convert_modtest_klp_convert1:test_klp_get_driver_name(), 0:test_klp_convert_modtest_klp_convert1:homonym_string, 1: homonym string Atest_klp_convert1:get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 =lib/livepatch/test_klp_convert_mod_a.cstatic stringtest_klp_convert1: klp_string.67890 =lib/livepatch/test_klp_convert_mod_b.cstatic string
% rmmodtest_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6Quoted 10 lines
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0This is saying you don't have permissions to write at that address.
Quoted 46 lines
GPR00:
c000000000056568c000000007223ae0c000000002a68a000000000000000001
GPR04:c0080000021706f0000000000000002d0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000c00000000ffca0800000000000000000 0000000000000000
GPR16:0000010005bf1810000000010c0f7370c0000000011b7e50c0000000011b7e68
GPR20:c0080000021501c8c0080000021502280000000000000030 0000000060000000
GPR24:c008000002160380c000000056b43000000000000000ff20c000000056b43c00
GPR28: aaaaaaaaaaaaaaabc000000056b43b400000000000000000c00800000214014cNIP [c00000000005659c]clear_relocate_add+0x11c/0x1c0
LR [c000000000056590]clear_relocate_add+0x110/0x1c0Call Trace:[
c000000007223ae0] [ffffffffffffffff]0xffffffffffffffff(unreliable) [c000000007223ba0] [c00000000021e3a8]klp_cleanup_module_patches_limited+0x448/0x480[c000000007223cb0] [c000000000220278]klp_module_going+0x68/0x94[c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004]system_call_exception+0x164/0x340[c000000007223e10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffa178fb6cNIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000NIP [
00007fffa178fb6c]0x7fffa178fb6c
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:
40820044813b002c7ff5f82a792936647d394a14e92900107c69f82e7fe9fa1448052235 600000002c03000041820008 <92ff0004>eadb002060000000 60000000---[ end trace 0000000000000000 ]---$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)Quoted 6 lines
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size /sizeof(*rela); i++) {
...
785 *instruction =PPC_RAW_NOP();
786 }Quoted 5 lines
Has the module text been marked RW prior to this? I suspect not?
In which case you need to use
patch_instruction()here.cheers
Thanks folks!
I guess something like this would fix compile for ppc32 and fix crash for ppc64.
I also pushed it to
https://git.kernel.org/pub/scm/linux/kernel/git/song/linux.git/log/?h=klp-module-reload
This includes Joe's klp-convert patches and this patch.
Thanks!
Songarch/powerpc/kernel/module_32.c +10
-0@@ -285,6 +285,16 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf32_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE notrace int module_trampoline_target(struct module *mod, unsigned long addr,unsigned long *target)arch/powerpc/kernel/module_64.c +0
-0
arch/powerpc/kernel/module_64.c void clear_relocate_add(Elf64_Shdr *sechdrs, · line 785
Quoted 5 lines
arch/powerpc/kernel/module_64.c +1
-1@@ -782,7 +782,7 @@ void clear_relocate_add(Elf64_Shdr *sechdrs,continue ; instruction +=1 ; - *instruction =PPC_RAW_NOP (); +patch_instruction (instruction,PPC_RAW_NOP ());
Close. I believe PPC_RAW_NOP() needs to be passed to ppc_inst() like:
arch/powerpc/kernel/module_64.c +1 -1
@@ -782,7 +782,7 @@ void clear_relocate_add(Elf64_Shdr *sechdrs,continue ; instruction +=1 ; -patch_instruction (instruction,PPC_RAW_NOP ()); +patch_instruction (instruction,ppc_inst (PPC_RAW_NOP ())); } }
And with that tweak, new result:
ppc64le : pass
Thanks guys,
-- Joe
Quoted 17 lines
arch/powerpc/kernel/module_64.c +0
-0} }include/linux/moduleloader.h +1
-1@@ -73,7 +73,7 @@ int apply_relocate_add(Elf_Shdr *sechdrs,unsigned int relsec, struct module *mod);#ifdef CONFIG_LIVEPATCH -void clear_relocate_add(Elf64_Shdr *sechdrs, +void clear_relocate_add(Elf_Shdr *sechdrs, const char *strtab, unsigned int symindex, unsigned int relsec,
Song Liu
maintainerQuoted 209 lines · Joe Lawrence
Quoted 185 lines · Song Liu
Quoted 137 lines · Michael Ellerman
Quoted 22 lines · Joe Lawrence
Quoted 21 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'...
Quoted 8 lines
Quoted 1 line
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (
tools/testing/selftests/livepatch/test-song.sh) such that:Quoted 7 lines
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch moduleIf you push that test code somewhere I could test it on ppc64le.
Quoted 28 lines
The results:
x86_64: pass
s390x : pass
ppc64le : crashI suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobetest_klp_convert1test_klp_convert1: tainting kernel withTAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobetest_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1:saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1:driver_name, 0:test_klp_convert_modtest_klp_convert1:test_klp_get_driver_name(), 0:test_klp_convert_modtest_klp_convert1:homonym_string, 1: homonym string Atest_klp_convert1:get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 =lib/livepatch/test_klp_convert_mod_a.cstatic stringtest_klp_convert1: klp_string.67890 =lib/livepatch/test_klp_convert_mod_b.cstatic string
% rmmodtest_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6Quoted 10 lines
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0This is saying you don't have permissions to write at that address.
Quoted 46 lines
GPR00:
c000000000056568c000000007223ae0c000000002a68a000000000000000001
GPR04:c0080000021706f0000000000000002d0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000c00000000ffca0800000000000000000 0000000000000000
GPR16:0000010005bf1810000000010c0f7370c0000000011b7e50c0000000011b7e68
GPR20:c0080000021501c8c0080000021502280000000000000030 0000000060000000
GPR24:c008000002160380c000000056b43000000000000000ff20c000000056b43c00
GPR28: aaaaaaaaaaaaaaabc000000056b43b400000000000000000c00800000214014cNIP [c00000000005659c]clear_relocate_add+0x11c/0x1c0
LR [c000000000056590]clear_relocate_add+0x110/0x1c0Call Trace:[
c000000007223ae0] [ffffffffffffffff]0xffffffffffffffff(unreliable) [c000000007223ba0] [c00000000021e3a8]klp_cleanup_module_patches_limited+0x448/0x480[c000000007223cb0] [c000000000220278]klp_module_going+0x68/0x94[c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004]system_call_exception+0x164/0x340[c000000007223e10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffa178fb6cNIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000NIP [
00007fffa178fb6c]0x7fffa178fb6c
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:
40820044813b002c7ff5f82a792936647d394a14e92900107c69f82e7fe9fa1448052235 600000002c03000041820008 <92ff0004>eadb002060000000 60000000---[ end trace 0000000000000000 ]---$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)Quoted 6 lines
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size /sizeof(*rela); i++) {
...
785 *instruction =PPC_RAW_NOP();
786 }Quoted 5 lines
Has the module text been marked RW prior to this? I suspect not?
In which case you need to use
patch_instruction()here.cheers
Thanks folks!
I guess something like this would fix compile for ppc32 and fix crash for ppc64.
I also pushed it to
https://git.kernel.org/pub/scm/linux/kernel/git/song/linux.git/log/?h=klp-module-reload
This includes Joe's klp-convert patches and this patch.
Thanks!
Songarch/powerpc/kernel/module_32.c +10
-0@@ -285,6 +285,16 @@ int apply_relocate_add(Elf32_Shdr *sechdrs,return 0 ; } +#ifdef CONFIG_LIVEPATCH +void clear_relocate_add (Elf32_Shdr *sechdrs, +const char *strtab, +unsigned int symindex, +unsigned int relsec, +struct module *me) +{ +} +#endif +#ifdef CONFIG_DYNAMIC_FTRACE notrace int module_trampoline_target(struct module *mod, unsigned long addr,unsigned long *target)arch/powerpc/kernel/module_64.c +0
-0arch/powerpc/kernel/module_64.c void clear_relocate_add(Elf64_Shdr *sechdrs, · line 785
Quoted 5 lines
arch/powerpc/kernel/module_64.c +1
-1@@ -782,7 +782,7 @@ void clear_relocate_add(Elf64_Shdr *sechdrs,continue ; instruction +=1 ; - *instruction =PPC_RAW_NOP (); +patch_instruction (instruction,PPC_RAW_NOP ());Close. I believe
PPC_RAW_NOP()needs to be passed toppc_inst()like:arch/powerpc/kernel/module_64.c +1
-1@@ -782,7 +782,7 @@ void clear_relocate_add(Elf64_Shdr *sechdrs,continue ; instruction +=1 ; -patch_instruction (instruction,PPC_RAW_NOP ()); +patch_instruction (instruction,ppc_inst (PPC_RAW_NOP ())); } }
Quoted 5 lines
And with that tweak, new result:
ppc64le : pass
Thanks!
I will fold this in and send v6.
Song
Joe Lawrence
maintainerQuoted 136 lines · Michael Ellerman
Quoted 22 lines · Joe Lawrence
Quoted 21 lines · Song Liu
From: Miroslav Benes <[email protected]>
Josh reported a bug:
When the object to be patched is a module, and that module is rmmod'ed and reloaded, it fails to load with:
module: x86/modules: Skipping invalid relocation target, existing value is nonzero for type 2, loc
00000000ba0302e9, valffffffffa03e293c
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'The livepatch module has a relocation which references a symbol in the _previous_ loading of nfsd. When
apply_relocate_add()tries to replace the old relocation with a new one, it sees that the previous one is nonzero and it errors out.On ppc64le, we have a similar issue:
module_64:livepatch_nfsd: Expected nop after call, gote8410018ate_show+0x60/0x548[livepatch_nfsd]
livepatch: failed to initialize patch 'livepatch_nfsd' for module 'nfsd' (-8)
livepatch: patch 'livepatch_nfsd' failed for module 'nfsd', refusing to load module 'nfsd'...
Quoted 8 lines
Quoted 1 line
Hi Song,
Applying your patch on top of my latest klp-convert-tree branch [1], I modified a few of its late module patching tests (
tools/testing/selftests/livepatch/test-song.sh) such that:Quoted 7 lines
1 - A livepatch module is loaded
- this module contains klp-relocations to objects in (2)
2 - A target test module is loaded
3 - Unload the test target module
- Clear klp-relocations in (1)
4 - Repeat target module load (2) / unload (3) a few times 5 - Unload livepatch moduleIf you push that test code somewhere I could test it on ppc64le.
Quoted 28 lines
The results:
x86_64: pass
s390x : pass
ppc64le : crashI suspect Power 32-bit would suffer the same fate, but I don't have hardware to verify. See the kernel log from the crash below...
===== TEST: klp-convert symbols (late module patching) =====
% modprobetest_klp_convert1test_klp_convert1: tainting kernel withTAINT_LIVEPATCH
livepatch: enabling patch 'test_klp_convert1'
livepatch: 'test_klp_convert1': starting patching transition
livepatch: 'test_klp_convert1': patching complete
% modprobetest_klp_convert_mod
livepatch: applying patch 'test_klp_convert1' to loading module 'test_klp_convert_mod'test_klp_convert1:saved_command_line, 0: BOOT_IMAGE=(ieee1275//vdevice/v-scsi@30000003/disk@8100000000000000,msdos2)/vmlinuz-5.19.0+ root=/dev/mapper/rhel_ibm--p9z--18--lp7-root ro crashkernel=2G-4G:384M,4G-16G:512M,16G-64G:1G,64G-128G:2G,128G-:4G rd.lvm.lv=rhel_ibm-p9z-18-lp7/root rd.lvm.lv=rhel_ibm-p9z-18-lp7/swaptest_klp_convert1:driver_name, 0:test_klp_convert_modtest_klp_convert1:test_klp_get_driver_name(), 0:test_klp_convert_modtest_klp_convert1:homonym_string, 1: homonym string Atest_klp_convert1:get_homonym_string(), 1: homonym string Atest_klp_convert1: klp_string.12345 =lib/livepatch/test_klp_convert_mod_a.cstatic stringtest_klp_convert1: klp_string.67890 =lib/livepatch/test_klp_convert_mod_b.cstatic string
% rmmodtest_klp_convert_mod
livepatch: reverting patch 'test_klp_convert1' on unloading module 'test_klp_convert_mod'module_64: Clearing ADD relocate section 48 to 6Quoted 10 lines
BUG: Unable to handle kernel data access on write at 0xc008000002140150 Faulting instruction address: 0xc00000000005659c Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(-) test_klp_convert1(K) bonding tls rfkill pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod CPU: 6 PID: 4766 Comm: rmmod Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c00000000005659c LR: c000000000056590 CTR: 0000000000000024 REGS: c000000007223840 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000000009033 <SF,EE,ME,IR,DR,RI,LE> CR: 48008282 XER: 0000000a CFAR: c0000000000a87e0 DAR: c008000002140150 DSISR: 0a000000 IRQMASK: 0This is saying you don't have permissions to write at that address.
Quoted 46 lines
GPR00:
c000000000056568c000000007223ae0c000000002a68a000000000000000001
GPR04:c0080000021706f0000000000000002d0000000000000000 0000000000000000
GPR08: 0000000000000066 0000001200000010 0000000000000000 0000000000008000
GPR12: 0000000000000000c00000000ffca0800000000000000000 0000000000000000
GPR16:0000010005bf1810000000010c0f7370c0000000011b7e50c0000000011b7e68
GPR20:c0080000021501c8c0080000021502280000000000000030 0000000060000000
GPR24:c008000002160380c000000056b43000000000000000ff20c000000056b43c00
GPR28: aaaaaaaaaaaaaaabc000000056b43b400000000000000000c00800000214014cNIP [c00000000005659c]clear_relocate_add+0x11c/0x1c0
LR [c000000000056590]clear_relocate_add+0x110/0x1c0Call Trace:[
c000000007223ae0] [ffffffffffffffff]0xffffffffffffffff(unreliable) [c000000007223ba0] [c00000000021e3a8]klp_cleanup_module_patches_limited+0x448/0x480[c000000007223cb0] [c000000000220278]klp_module_going+0x68/0x94[c000000007223ce0] [c00000000022f480] __do_sys_delete_module.constprop.0+0x1d0/0x390 [c000000007223db0] [c00000000002f004]system_call_exception+0x164/0x340[c000000007223e10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffa178fb6cNIP: 00007fffa178fb6c LR: 0000000000000000 CTR: 0000000000000000 REGS: c000000007223e80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000280f033 <SF,VEC,VSX,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48002482 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000081 00007ffff2d1b720 00007fffa1887200 0000010005bf1878 GPR04: 0000000000000800 000000000000000a 0000000000000000 00000000000000da GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffa201c540 0000000000000000 0000000000000000 GPR16: 0000010005bf1810 000000010c0f7370 000000010c0f8090 000000010c0f8078 GPR20: 000000010c0f8050 000000010c0f80a8 000000010c0f7518 000000010c0f80d0 GPR24: 00007ffff2d1b830 00007ffff2d1efbb 0000000000000000 0000010005bf02a0 GPR28: 00007ffff2d1be50 0000000000000000 0000010005bf1810 0000000000100000NIP [
00007fffa178fb6c]0x7fffa178fb6c
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:
40820044813b002c7ff5f82a792936647d394a14e92900107c69f82e7fe9fa1448052235 600000002c03000041820008 <92ff0004>eadb002060000000 60000000---[ end trace 0000000000000000 ]---$ addr2line 0xc00000000005659c -e vmlinux /root/klp-convert-tree/arch/powerpc/kernel/module_64.c:785743 void clear_relocate_add(Elf64_Shdr *sechdrs,
744 const char *strtab, 745 unsigned int symindex, 746 unsigned int relsec, 747 struct module *me)Quoted 6 lines
748 {
...
759 for (i = 0; i < sechdrs[relsec].sh_size /sizeof(*rela); i++) {
...
785 *instruction =PPC_RAW_NOP();
786 }
Quoted 4 lines
Has the module text been marked RW prior to this? I suspect not?
In which case you need to use
patch_instruction()here.
Hi Michael,
While we're on the topic of klp-relocations and Power, I saw a similar access problem when writing (late) relocations into .data..ro_after_init. I'm not entirely convinced this should be allowed (ie, is it really read-only after .init or ???), but it seems that other arches currently allow it ...
$ ./tools/testing/selftests/livepatch/test-shadow-vars.sh
===== TEST: klp-convert data relocations (late module patching) =====
% modprobe test_klp_convert_data
livepatch: enabling patch 'test_klp_convert_data'
livepatch: 'test_klp_convert_data': starting patching transition
livepatch: 'test_klp_convert_data': patching complete
% modprobe test_klp_convert_mod
...module_64: Applying ADD relocate section 54 to 20module_64: RELOC at 000000008482d02a: 38-type as .klp.sym.test_klp_convert_mod.static_ro_after_init,0 (0xc0080000016d0084) + 0
BUG: Unable to handle kernel data access on write at 0xc0080000021d0000 Faulting instruction address: 0xc000000000055f14 Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(+) test_klp_convert_data(K) bonding rfkill tls pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod [last unloaded: test_klp_convert_mod] CPU: 0 PID: 17089 Comm: modprobe Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c000000000055f14 LR: c00000000021ef28 CTR: c000000000055f14 REGS: c0000000387af5a0 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000002009033 <SF,VEC,EE,ME,IR,DR,RI,LE> CR: 88228444 XER: 00000000 CFAR: c000000000055e04 DAR: c0080000021d0000 DSISR: 42000000 IRQMASK: 0
GPR00: c00000000021ef28 c0000000387af840 c000000002a68a00 c0000000088b3000
GPR04: c008000002230084 0000000000000026 0000000000000036 c0080000021e0480
GPR08: 000000007c426214 c000000000055f14 c000000000055e08 0000000000000d80
GPR12: c00000000021d9b0 c000000002d90000 c0000000088b3000 c0080000021f0810
GPR16: c0080000021c0638 c0000000088b3d80 00000000ffffffff c000000001181e38
GPR20: c0000000029dc088 c0080000021e0480 c0080000021f0870 aaaaaaaaaaaaaaab
GPR24: c0000000088b3c40 c0080000021d0000 c0080000021f0000 0000000000000000
GPR28: c0080000021d0000 0000000000000000 c0080000021c0638 0000000000000810 NIP [c000000000055f14] apply_relocate_add+0x474/0x9e0
LR [c00000000021ef28] klp_apply_section_relocs+0x208/0x2d0
Call Trace:
[c0000000387af840] [c0000000387af920] 0xc0000000387af920 (unreliable) [c0000000387af940] [c00000000021ef28] klp_apply_section_relocs+0x208/0x2d0 [c0000000387afa30] [c00000000021f080] klp_init_object_loaded+0x90/0x1e0 [c0000000387afac0] [c0000000002200ac] klp_module_coming+0x3dc/0x5c0 [c0000000387afb70] [c000000000231414] load_module+0xf64/0x13a0 [c0000000387afc90] [c000000000231b8c] __do_sys_finit_module+0xdc/0x180 [c0000000387afdb0] [c00000000002f004] system_call_exception+0x164/0x340 [c0000000387afe10] [c00000000000be68] system_call_vectored_common+0xe8/0x278 --- interrupt: 3000 at 0x7fffb6af4710
NIP: 00007fffb6af4710 LR: 0000000000000000 CTR: 0000000000000000 REGS: c0000000387afe80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000000f033 <SF,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48224244 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000161 00007fffe06f5550 00007fffb6bf7200 0000000000000005 GPR04: 0000000105f36ca0 0000000000000000 0000000000000005 0000000000000000 GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffb738c540 0000000000000020 0000000000000000 GPR16: 0000010024d31de0 0000000000000000 0000000105f37d50 0000010024d302f8 GPR20: 0000000000000001 0000000000000908 0000010024d32020 0000010024d319b0 GPR24: 0000000000000000 0000000000000000 0000010024d32040 0000010024d303f0 GPR28: 0000010024d31e00 0000000105f36ca0 0000000000040000 0000010024d319b0
NIP [00007fffb6af4710] 0x7fffb6af4710
LR [0000000000000000] 0x0
--- interrupt: 3000
Instruction dump:0000061c 0000061c 0000061c 0000061c 0000061c 0000061c 0000061c 0000061c 00000288 00000248 60000000 7c992050 <7c9ce92a> 60000000 60000000 e9310020
---[ end trace 0000000000000000 ]---
$ readelf --wide --sections lib/livepatch/test_klp_convert_data.ko | grep -e '\[20\]' -e '\[54\]' [20] .data..ro_after_init PROGBITS 0000000000000000 001a58 000008 00 WA 0 0 8 [54] .klp.rela.test_klp_convert_mod..data..ro_after_init RELA 0000000000000000 0426e8 000018 18 Ao 49 20 8
I can push a branch up to github if you'd like to try it yourself.
Regards,
-- Joe
Michael Ellerman
reviewerQuoted 2 lines · Joe Lawrence
Quoted 1 line · Michael Ellerman
Joe Lawrence <[email protected]> writes:
...
Quoted 8 lines
Hi Michael,
While we're on the topic of klp-relocations and Power, I saw a similar access problem when writing (late) relocations into .data..ro_after_init. I'm not entirely convinced this should be allowed (ie, is it really read-only after .init or ???), but it seems that other arches currently allow it ...
I guess that's because we didn't properly fix apply_relocate_add() in
https://github.com/linuxppc/issues/issues/375 ?
If other arches allow it then we don't want to be the odd one out :)
So I guess we need to implement it.
Quoted 59 lines
$ ./tools/testing/selftests/livepatch/test-shadow-vars.sh===== TEST: klp-convert data relocations (late module patching) =====
% modprobetest_klp_convert_data
livepatch: enabling patch 'test_klp_convert_data'
livepatch: 'test_klp_convert_data': starting patching transition
livepatch: 'test_klp_convert_data': patching complete
% modprobetest_klp_convert_mod
...module_64: Applying ADD relocate section 54 to 20module_64: RELOC at000000008482d02a: 38-type as .klp.sym.test_klp_convert_mod.static_ro_after_init,0 (0xc0080000016d0084) + 0BUG: Unable to handle kernel data access on write at 0xc0080000021d0000 Faulting instruction address: 0xc000000000055f14 Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(+) test_klp_convert_data(K) bonding rfkill tls pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod [last unloaded: test_klp_convert_mod] CPU: 0 PID: 17089 Comm: modprobe Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c000000000055f14 LR: c00000000021ef28 CTR: c000000000055f14 REGS: c0000000387af5a0 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000002009033 <SF,VEC,EE,ME,IR,DR,RI,LE> CR: 88228444 XER: 00000000 CFAR: c000000000055e04 DAR: c0080000021d0000 DSISR: 42000000 IRQMASK: 0GPR00:
c00000000021ef28c0000000387af840c000000002a68a00c0000000088b3000
GPR04:c0080000022300840000000000000026 0000000000000036c0080000021e0480
GPR08:000000007c426214c000000000055f14c000000000055e080000000000000d80
GPR12:c00000000021d9b0c000000002d90000c0000000088b3000c0080000021f0810
GPR16:c0080000021c0638c0000000088b3d8000000000ffffffffc000000001181e38
GPR20:c0000000029dc088c0080000021e0480c0080000021f0870aaaaaaaaaaaaaaab
GPR24:c0000000088b3c40c0080000021d0000c0080000021f00000000000000000000
GPR28:c0080000021d00000000000000000000c0080000021c06380000000000000810 NIP [c000000000055f14]apply_relocate_add+0x474/0x9e0
LR [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0Call Trace:[
c0000000387af840] [c0000000387af920]0xc0000000387af920(unreliable) [c0000000387af940] [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0[c0000000387afa30] [c00000000021f080]klp_init_object_loaded+0x90/0x1e0[c0000000387afac0] [c0000000002200ac]klp_module_coming+0x3dc/0x5c0[c0000000387afb70] [c000000000231414]load_module+0xf64/0x13a0[c0000000387afc90] [c000000000231b8c]__do_sys_finit_module+0xdc/0x180[c0000000387afdb0] [c00000000002f004]system_call_exception+0x164/0x340[c0000000387afe10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffb6af4710NIP: 00007fffb6af4710 LR: 0000000000000000 CTR: 0000000000000000 REGS: c0000000387afe80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000000f033 <SF,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48224244 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000161 00007fffe06f5550 00007fffb6bf7200 0000000000000005 GPR04: 0000000105f36ca0 0000000000000000 0000000000000005 0000000000000000 GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffb738c540 0000000000000020 0000000000000000 GPR16: 0000010024d31de0 0000000000000000 0000000105f37d50 0000010024d302f8 GPR20: 0000000000000001 0000000000000908 0000010024d32020 0000010024d319b0 GPR24: 0000000000000000 0000000000000000 0000010024d32040 0000010024d303f0 GPR28: 0000010024d31e00 0000000105f36ca0 0000000000040000 0000010024d319b0NIP [
00007fffb6af4710]0x7fffb6af4710
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:0000061c0000061c0000061c0000061c0000061c0000061c0000061c0000061c00000288 00000248 600000007c992050<7c9ce92a> 60000000 60000000e9310020
Quoted 7 lines
---[ end trace 0000000000000000 ]---$ readelf --wide --sections lib/livepatch/test_klp_convert_data.ko | grep -e '\[20\]' -e '\[54\]' [20] .data..ro_after_init PROGBITS 0000000000000000 001a58 000008 00 WA 0 0 8 [54] .klp.rela.test_klp_convert_mod..data..ro_after_init RELA 0000000000000000 0426e8 000018 18 Ao 49 20 8I can push a branch up to github if you'd like to try it yourself.
That would help thanks.
cheers
Joe Lawrence
maintainerQuoted 13 lines · Michael Ellerman
Quoted 2 lines · Joe Lawrence
Quoted 1 line · Michael Ellerman
Joe Lawrence <[email protected]> writes:
...
Quoted 8 lines
Hi Michael,
While we're on the topic of klp-relocations and Power, I saw a similar access problem when writing (late) relocations into .data..ro_after_init. I'm not entirely convinced this should be allowed (ie, is it really read-only after .init or ???), but it seems that other arches currently allow it ...
Quoted 7 lines
I guess that's because we didn't properly fix
apply_relocate_add()in
https://github.com/linuxppc/issues/issues/375 ?If other arches allow it then we don't want to be the odd one out :)
So I guess we need to implement it.
FWIW, I think it this particular relocation is pretty rare, we haven't seen it in real patches nor do we have a kpatch test that generates it. I only hit a crash as I was trying to write a more exhaustive test for the klp-convert implementation.
Quoted 59 lines
Quoted 59 lines
===== TEST: klp-convert data relocations (late module patching) =====
% modprobetest_klp_convert_data
livepatch: enabling patch 'test_klp_convert_data'
livepatch: 'test_klp_convert_data': starting patching transition
livepatch: 'test_klp_convert_data': patching complete
% modprobetest_klp_convert_mod
...module_64: Applying ADD relocate section 54 to 20module_64: RELOC at000000008482d02a: 38-type as .klp.sym.test_klp_convert_mod.static_ro_after_init,0 (0xc0080000016d0084) + 0BUG: Unable to handle kernel data access on write at 0xc0080000021d0000 Faulting instruction address: 0xc000000000055f14 Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(+) test_klp_convert_data(K) bonding rfkill tls pseries_rng drm fuse drm_panel_orientation_quirks xfs libcrc32c sd_mod t10_pi sg ibmvscsi ibmveth scsi_transport_srp vmx_crypto dm_mirror dm_region_hash dm_log dm_mod [last unloaded: test_klp_convert_mod] CPU: 0 PID: 17089 Comm: modprobe Kdump: loaded Tainted: G K 5.19.0+ #1 NIP: c000000000055f14 LR: c00000000021ef28 CTR: c000000000055f14 REGS: c0000000387af5a0 TRAP: 0300 Tainted: G K (5.19.0+) MSR: 8000000002009033 <SF,VEC,EE,ME,IR,DR,RI,LE> CR: 88228444 XER: 00000000 CFAR: c000000000055e04 DAR: c0080000021d0000 DSISR: 42000000 IRQMASK: 0GPR00:
c00000000021ef28c0000000387af840c000000002a68a00c0000000088b3000
GPR04:c0080000022300840000000000000026 0000000000000036c0080000021e0480
GPR08:000000007c426214c000000000055f14c000000000055e080000000000000d80
GPR12:c00000000021d9b0c000000002d90000c0000000088b3000c0080000021f0810
GPR16:c0080000021c0638c0000000088b3d8000000000ffffffffc000000001181e38
GPR20:c0000000029dc088c0080000021e0480c0080000021f0870aaaaaaaaaaaaaaab
GPR24:c0000000088b3c40c0080000021d0000c0080000021f00000000000000000000
GPR28:c0080000021d00000000000000000000c0080000021c06380000000000000810 NIP [c000000000055f14]apply_relocate_add+0x474/0x9e0
LR [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0Call Trace:[
c0000000387af840] [c0000000387af920]0xc0000000387af920(unreliable) [c0000000387af940] [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0[c0000000387afa30] [c00000000021f080]klp_init_object_loaded+0x90/0x1e0[c0000000387afac0] [c0000000002200ac]klp_module_coming+0x3dc/0x5c0[c0000000387afb70] [c000000000231414]load_module+0xf64/0x13a0[c0000000387afc90] [c000000000231b8c]__do_sys_finit_module+0xdc/0x180[c0000000387afdb0] [c00000000002f004]system_call_exception+0x164/0x340[c0000000387afe10] [c00000000000be68]system_call_vectored_common+0xe8/0x278--- interrupt: 3000 at0x7fffb6af4710NIP: 00007fffb6af4710 LR: 0000000000000000 CTR: 0000000000000000 REGS: c0000000387afe80 TRAP: 3000 Tainted: G K (5.19.0+) MSR: 800000000000f033 <SF,EE,PR,FP,ME,IR,DR,RI,LE> CR: 48224244 XER: 00000000 IRQMASK: 0 GPR00: 0000000000000161 00007fffe06f5550 00007fffb6bf7200 0000000000000005 GPR04: 0000000105f36ca0 0000000000000000 0000000000000005 0000000000000000 GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000 GPR12: 0000000000000000 00007fffb738c540 0000000000000020 0000000000000000 GPR16: 0000010024d31de0 0000000000000000 0000000105f37d50 0000010024d302f8 GPR20: 0000000000000001 0000000000000908 0000010024d32020 0000010024d319b0 GPR24: 0000000000000000 0000000000000000 0000010024d32040 0000010024d303f0 GPR28: 0000010024d31e00 0000000105f36ca0 0000000000040000 0000010024d319b0NIP [
00007fffb6af4710]0x7fffb6af4710
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:0000061c0000061c0000061c0000061c0000061c0000061c0000061c0000061c00000288 00000248 600000007c992050<7c9ce92a> 60000000 60000000e9310020---[ end trace 0000000000000000 ]---
Quoted 8 lines
Quoted 5 lines
$ readelf --wide --sections lib/livepatch/test_klp_convert_data.ko | grep -e '\[20\]' -e '\[54\]' [20] .data..ro_after_init PROGBITS 0000000000000000 001a58 000008 00 WA 0 0 8 [54] .klp.rela.test_klp_convert_mod..data..ro_after_init RELA 0000000000000000 0426e8 000018 18 Ao 49 20 8I can push a branch up to github if you'd like to try it yourself.
That would help thanks.
This branch should do it:
https://github.com/joe-lawrence/klp-convert-tree/tree/klp-convert-v7-devel
Boot that and then run tools/testing/selftests/livepatch/test-livepatch.sh
-- Joe
Quoted 27 lines · Joe Lawrence
Quoted 16 lines · Michael Ellerman
Quoted 2 lines · Joe Lawrence
Quoted 1 line · Michael Ellerman
Joe Lawrence <[email protected]> writes:
...
Quoted 11 lines
Hi Michael,
While we're on the topic of klp-relocations and Power, I saw a similar
access problem when writing (late) relocations into .data..ro_after_init. I'm not entirely convinced this should be allowed
(ie, is it really read-only after .init or ???), but it seems that other
arches currently allow it ...Quoted 9 lines
I guess that's because we didn't properly fix
apply_relocate_add()in
https://github.com/linuxppc/issues/issues/375 ?If other arches allow it then we don't want to be the odd one out :)
So I guess we need to implement it.
Quoted 7 lines
FWIW, I think it this particular relocation is pretty rare, we haven't
seen it in real patches nor do we have a kpatch test that generates it.
I only hit a crash as I was trying to write a more exhaustive test for
the klp-convert implementation.
I'll revive my proper fix. I stopped working on it since my previous version was hitting endian bugs with some relocations & it didn't seem necessary at the time. Shouldn't take too much to get it going again.
Quoted 114 lines
Quoted 108 lines
Quoted 108 lines
===== TEST: klp-convert data relocations (late module patching)
=====
% modprobetest_klp_convert_data
livepatch: enabling patch 'test_klp_convert_data'
livepatch: 'test_klp_convert_data': starting patching transition
livepatch: 'test_klp_convert_data': patching complete % modprobetest_klp_convert_mod
...module_64: Applying ADD relocate section 54 to 20module_64: RELOC at000000008482d02a: 38-type as .klp.sym.test_klp_convert_mod.static_ro_after_init,0 (0xc0080000016d0084) + 0BUG: Unable to handle kernel data access on write at 0xc0080000021d0000 Faulting instruction address: 0xc000000000055f14 Oops: Kernel access of bad area, sig: 11 [#1] LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries Modules linked in: test_klp_convert_mod(+)
test_klp_convert_data(K)bonding rfkill tlspseries_rngdrm fusedrm_panel_orientation_quirksxfs libcrc32csd_modt10_pisg ibmvscsi ibmvethscsi_transport_srpvmx_cryptodm_mirrordm_region_hashdm_logdm_mod[last unloaded:test_klp_convert_mod]CPU: 0 PID: 17089 Comm: modprobe Kdump: loaded Tainted:G K 5.19.0+ #1
NIP:c000000000055f14LR:c00000000021ef28CTR:c000000000055f14
REGS:c0000000387af5a0TRAP: 0300 Tainted: G K (5.19.0+)
MSR: 8000000002009033 <SF,VEC,EE,ME,IR,DR,RI,LE> CR: 88228444
XER: 00000000
CFAR:c000000000055e04DAR:c0080000021d0000DSISR: 42000000
IRQMASK: 0
GPR00:c00000000021ef28c0000000387af840c000000002a68a00c0000000088b3000
GPR04:c0080000022300840000000000000026 0000000000000036c0080000021e0480
GPR08:000000007c426214c000000000055f14c000000000055e080000000000000d80
GPR12:c00000000021d9b0c000000002d90000c0000000088b3000c0080000021f0810
GPR16:c0080000021c0638c0000000088b3d8000000000ffffffffc000000001181e38
GPR20:c0000000029dc088c0080000021e0480c0080000021f0870aaaaaaaaaaaaaaab
GPR24:c0000000088b3c40c0080000021d0000c0080000021f00000000000000000000
GPR28:c0080000021d00000000000000000000c0080000021c06380000000000000810
NIP [c000000000055f14]apply_relocate_add+0x474/0x9e0LR [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0Call Trace:[
c0000000387af840] [c0000000387af920]0xc0000000387af920(unreliable)
[c0000000387af940] [c00000000021ef28]klp_apply_section_relocs+0x208/0x2d0[c0000000387afa30] [c00000000021f080]klp_init_object_loaded+0x90/0x1e0[c0000000387afac0] [c0000000002200ac]klp_module_coming+0x3dc/0x5c0 [c0000000387afb70] [c000000000231414] load_module+0xf64/0x13a0[
c0000000387afc90] [c000000000231b8c]__do_sys_finit_module+0xdc/0x180
[c0000000387afdb0] [c00000000002f004]system_call_exception+0x164/0x340
[c0000000387afe10] [c00000000000be68]system_call_vectored_common+0xe8/0x278
--- interrupt: 3000 at0x7fffb6af4710
NIP:00007fffb6af4710LR: 0000000000000000 CTR: 0000000000000000
REGS:c0000000387afe80TRAP: 3000 Tainted: G K (5.19.0+)
MSR:800000000000f033<SF,EE,PR,FP,ME,IR,DR,RI,LE> CR:
48224244 XER: 00000000
IRQMASK: 0
GPR00: 000000000000016100007fffe06f555000007fffb6bf72000000000000000005
GPR04:0000000105f36ca00000000000000000 0000000000000005 0000000000000000
GPR08: 0000000000000000 0000000000000000 0000000000000000 0000000000000000
GPR12: 000000000000000000007fffb738c5400000000000000020 0000000000000000
GPR16:0000010024d31de000000000000000000000000105f37d500000010024d302f8
GPR20: 0000000000000001 00000000000009080000010024d320200000010024d319b0
GPR24: 0000000000000000 00000000000000000000010024d320400000010024d303f0
GPR28:0000010024d31e000000000105f36ca000000000000400000000010024d319b0
NIP [00007fffb6af4710]0x7fffb6af4710
LR [0000000000000000]0x0
--- interrupt: 3000
Instruction dump:0000061c0000061c0000061c0000061c0000061c0000061c0000061c0000061c
00000288 00000248 600000007c992050<7c9ce92a> 60000000 60000000e9310020---[ end trace 0000000000000000 ]---$ readelf --wide --sections lib/livepatch/test_klp_convert_data.ko | grep -e '\[20\]' -e '\[54\]' [20] .data..ro_after_init PROGBITS 0000000000000000 001a58 000008 00 WA 0 0 8 [54] .klp.rela.test_klp_convert_mod..data..ro_after_init RELA 0000000000000000 0426e8 000018 18 Ao 49 20 8Quoted 5 lines
Quoted 2 lines
I can push a branch up to github if you'd like to try it yourself.
That would help thanks.
Quoted 6 lines
This branch should do it:
https://github.com/joe-lawrence/klp-convert-tree/tree/klp-convert-v7-devel
Boot that and then run
tools/testing/selftests/livepatch/test-livepatch.sh
Was able to reproduce, and confirm that using text patching for R_PPC64_ADDR64 works fixes it.
- Russell
Quoted 3 lines
-- Joe