Module load failure race fix.
Subsystems (17)
PATCH 1/3 module_arch_freeing_init(): new hook for archs before module->module_init freed.
In reply to Rusty RussellArchs have been abusing module_free() to clean up their arch-specific allocations. Since module_free() is also (ab)used by BPF and trace code, let's keep it to simple allocations, and provide a hook called before that.
This means that avr32, ia64, parisc and s390 no longer need to implement their own module_free() at all. avr32 doesn't need module_finalize() either.
--- arch/avr32/kernel/module.c | 13 +------------ arch/ia64/kernel/module.c | 6 ++---- arch/parisc/kernel/module.c | 6 +----- arch/s390/kernel/module.c | 10 +++------- arch/tile/kernel/module.c | 2 +- include/linux/moduleloader.h | 2 ++ kernel/module.c | 7 +++++++ 7 files changed, 17 insertions(+), 29 deletions(-)
arch/avr32/kernel/module.c +1 -12
@@ -19,12 +19,10 @@#include <linux/moduleloader.h> #include <linux/vmalloc.h> -void module_free (struct module *mod,void *module_region) +void module_arch_freeing_init (struct module *mod) {vfree (mod->arch.syminfo); mod->arch.syminfo =NULL ; - -vfree (module_region); } static inline int check_rela(Elf32_Rela *rela, struct module *module,
@@ -291,12 +289,3 @@ int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab,return ret; } - -int module_finalize (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs, -struct module *module) -{ -vfree (module->arch.syminfo); - module->arch.syminfo =NULL ; - -return 0 ; -}
arch/ia64/kernel/module.c +2 -4
@@ -305,14 +305,12 @@ plt_target (struct plt_entry *plt)#endif /* !USE_BRL */ void -module_free (struct module *mod,void *module_region) +module_arch_freeing_init (struct module *mod) { -if (mod && mod->arch.init_unw_table && - module_region == mod->module_init) { +if (mod->arch.init_unw_table) {unw_remove_unwind_table (mod->arch.init_unw_table); mod->arch.init_unw_table =NULL ; } -vfree (module_region); }/* Have we already seen one of these relocations? */
arch/parisc/kernel/module.c +1 -5
@@ -298,14 +298,10 @@ static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n) }#endif - -/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) +void module_arch_freeing_init (struct module *mod) {kfree (mod->arch.section); mod->arch.section =NULL ; - -vfree (module_region); }/* Additional bytes needed in front of individual sections */
arch/s390/kernel/module.c +3 -7
@@ -55,14 +55,10 @@ void *module_alloc(unsigned long size) }#endif -/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) +void module_arch_freeing_init (struct module *mod) { -if (mod) { -vfree (mod->arch.syminfo); - mod->arch.syminfo =NULL ; - } -vfree (module_region); +vfree (mod->arch.syminfo); + mod->arch.syminfo =NULL ; }static void check_rela (Elf_Rela *rela,struct module *me)
arch/tile/kernel/module.c +1 -1
@@ -83,7 +83,7 @@ void module_free(struct module *mod, void *module_region) 0, 0, 0, NULL, NULL, 0);/* -* FIXME: If module_region == mod->module_init, trim exception +* FIXME: Add module_arch_freeing_init to trim exception * table entries. */ }
include/linux/moduleloader.h +2 -0
@@ -82,4 +82,6 @@ int module_finalize(const Elf_Ehdr *hdr,/* Any cleanup needed when module leaves. */ void module_arch_cleanup (struct module *mod); +/* Any cleanup before freeing mod->module_init */ +void module_arch_freeing_init (struct module *mod);#endif
kernel/module.c +7 -0
@@ -1804,6 +1804,10 @@ void __weak module_arch_cleanup(struct module *mod) { } +void __weak module_arch_freeing_init (struct module *mod) +{ +} +/* Free a module, remove from lists, etc. */ static void free_module (struct module *mod) {
@@ -1841,6 +1845,7 @@ static void free_module(struct module *mod)/* This may be NULL, but that's OK */ unset_module_init_ro_nx (mod); +module_arch_freeing_init (mod);module_free (mod, mod->module_init);kfree (mod->args);percpu_modfree (mod);
@@ -2930,6 +2935,7 @@ static struct module *layout_and_allocate(struct load_info *info, int flags)static void module_deallocate (struct module *mod,struct load_info *info) {percpu_modfree (mod); +module_arch_freeing_init (mod);module_free (mod, mod->module_init);module_free (mod, mod->module_core); }
@@ -3055,6 +3061,7 @@ static int do_init_module(struct module *mod) mod->strtab = mod->core_strtab;#endif unset_module_init_ro_nx (mod); +module_arch_freeing_init (mod);module_free (mod, mod->module_init); mod->module_init =NULL ; mod->init_size =0 ;
Signature · 1 line
2.1.0
PATCH 2/3 module: remove mod arg from module_free, rename module_memfree().
In reply to Rusty RussellNothing needs the module pointer any more, and the next patch will call it from RCU, where the module itself might no longer exist. Removing the arg is the safest approach.
This just codifies the use of the module_alloc/module_free pattern which ftrace and bpf use.
--- arch/cris/kernel/module.c | 2 +- arch/mips/net/bpf_jit.c | 2 +- arch/nios2/kernel/module.c | 2 +- arch/powerpc/net/bpf_jit_comp.c | 2 +- arch/sparc/net/bpf_jit_comp.c | 4 ++-- arch/tile/kernel/module.c | 2 +- arch/x86/kernel/ftrace.c | 2 +- include/linux/moduleloader.h | 2 +- kernel/bpf/core.c | 2 +- kernel/kprobes.c | 2 +- kernel/module.c | 14 +++++++------- 11 files changed, 18 insertions(+), 18 deletions(-)
arch/cris/kernel/module.c +1 -1
@@ -36,7 +36,7 @@ void *module_alloc(unsigned long size) }/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) +void module_memfree (void *module_region) {kfree (module_region); }
arch/mips/net/bpf_jit.c +1 -1
@@ -1388,7 +1388,7 @@ out:void bpf_jit_free (struct bpf_prog *fp) {if (fp->jited) -module_free (NULL , fp->bpf_func); +module_memfree (fp->bpf_func);bpf_prog_unlock_free (fp); }
arch/nios2/kernel/module.c +1 -1
@@ -36,7 +36,7 @@ void *module_alloc(unsigned long size) }/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) +void module_memfree (void *module_region) {kfree (module_region); }
arch/powerpc/net/bpf_jit_comp.c +1 -1
@@ -699,7 +699,7 @@ out:void bpf_jit_free (struct bpf_prog *fp) {if (fp->jited) -module_free (NULL , fp->bpf_func); +module_memfree (fp->bpf_func);bpf_prog_unlock_free (fp); }
arch/sparc/net/bpf_jit_comp.c +2 -2
@@ -776,7 +776,7 @@ cond_branch: f_offset = addrs[i + filter[i].jf];if (unlikely (proglen + ilen > oldproglen)) {pr_err ("bpb_jit_compile fatal error\n" );kfree (addrs); -module_free (NULL , image); +module_memfree (image);return ; }memcpy (image + proglen, temp, ilen);
@@ -822,7 +822,7 @@ out:void bpf_jit_free (struct bpf_prog *fp) {if (fp->jited) -module_free (NULL , fp->bpf_func); +module_memfree (fp->bpf_func);bpf_prog_unlock_free (fp); }
arch/tile/kernel/module.c +1 -1
@@ -74,7 +74,7 @@ error:/* Free memory returned from module_alloc */ -void module_free (struct module *mod,void *module_region) +void module_memfree (void *module_region) {vfree (module_region);
arch/x86/kernel/ftrace.c +1 -1
@@ -674,7 +674,7 @@ static inline void *alloc_tramp(unsigned long size) }static inline void tramp_free (void *tramp) { -module_free (NULL , tramp); +module_memfree (tramp); }#else /* Trampolines can only be created if modules are supported */
include/linux/moduleloader.h +1 -1
@@ -26,7 +26,7 @@ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);void *module_alloc (unsigned long size);/* Free memory returned from module_alloc. */ -void module_free (struct module *mod,void *module_region); +void module_memfree (void *module_region);/* * Apply the given relocation to the (simplified) ELF. Return -error
kernel/bpf/core.c +1 -1
@@ -163,7 +163,7 @@ bpf_jit_binary_alloc(unsigned int proglen, u8 **image_ptr,void bpf_jit_binary_free (struct bpf_binary_header *hdr) { -module_free (NULL , hdr); +module_memfree (hdr); }#endif /* CONFIG_BPF_JIT */
kernel/kprobes.c +1 -1
@@ -127,7 +127,7 @@ static void *alloc_insn_page(void)static void free_insn_page (void *page) { -module_free (NULL , page); +module_memfree (page); }struct kprobe_insn_cache kprobe_insn_slots = {
kernel/module.c +7 -7
@@ -1795,7 +1795,7 @@ static void unset_module_core_ro_nx(struct module *mod) { }static void unset_module_init_ro_nx (struct module *mod) { }#endif -void __weak module_free (struct module *mod,void *module_region) +void __weak module_memfree (void *module_region) {vfree (module_region); }
@@ -1846,7 +1846,7 @@ static void free_module(struct module *mod)/* This may be NULL, but that's OK */ unset_module_init_ro_nx (mod);module_arch_freeing_init (mod); -module_free (mod, mod->module_init); +module_memfree (mod->module_init);kfree (mod->args);percpu_modfree (mod);
@@ -1855,7 +1855,7 @@ static void free_module(struct module *mod)/* Finally, free the core (containing the module structure) */ unset_module_core_ro_nx (mod); -module_free (mod, mod->module_core); +module_memfree (mod->module_core);#ifdef CONFIG_MPU update_protections (current->mm);
@@ -2790,7 +2790,7 @@ static int move_module(struct module *mod, struct load_info *info)*/ kmemleak_ignore (ptr);if (!ptr) { -module_free (mod, mod->module_core); +module_memfree (mod->module_core);return -ENOMEM ; }memset (ptr,0 , mod->init_size);
@@ -2936,8 +2936,8 @@ static void module_deallocate(struct module *mod, struct load_info *info) {percpu_modfree (mod);module_arch_freeing_init (mod); -module_free (mod, mod->module_init); -module_free (mod, mod->module_core); +module_memfree (mod->module_init); +module_memfree (mod->module_core); } int __weak module_finalize(const Elf_Ehdr *hdr,
@@ -3062,7 +3062,7 @@ static int do_init_module(struct module *mod)#endif unset_module_init_ro_nx (mod);module_arch_freeing_init (mod); -module_free (mod, mod->module_init); +module_memfree (mod->module_init); mod->module_init =NULL ; mod->init_size =0 ; mod->init_ro_size =0 ;
Signature · 1 line
2.1.0
cover Module load failure race fix.
Hi all,
This bug has been around since 2009, but only reported now.
I'm debating whether to cc: stable.
To fix it, I had to change module_free. Please ensure I haven't broken your arch!
Thanks,
Rusty.
Rusty Russell (3):
module_arch_freeing_init(): new hook for archs before
module->module_init freed.
module: remove mod arg from module_free, rename module_memfree().
module: fix race in kallsyms resolution during module load success.
arch/avr32/kernel/module.c | 13 +------- arch/cris/kernel/module.c | 2 +- arch/ia64/kernel/module.c | 6 ++-- arch/mips/net/bpf_jit.c | 2 +- arch/nios2/kernel/module.c | 2 +- arch/parisc/kernel/module.c | 6 +--- arch/powerpc/net/bpf_jit_comp.c | 2 +- arch/s390/kernel/module.c | 10 ++---- arch/sparc/net/bpf_jit_comp.c | 4 +-- arch/tile/kernel/module.c | 4 +-- arch/x86/kernel/ftrace.c | 2 +- include/linux/moduleloader.h | 4 ++- kernel/bpf/core.c | 2 +- kernel/kprobes.c | 2 +- kernel/module.c | 74 ++++++++++++++++++++++++++++++----------- 15 files changed, 76 insertions(+), 59 deletions(-)
Signature · 1 line
2.1.0
PATCH 3/3 module: fix race in kallsyms resolution during module load success.
The kallsyms routines (module_symbol_name, lookup_module_* etc) disable preemption to walk the modules rather than taking the module_mutex:
this is because they are used for symbol resolution during oopses.
This works because there are synchronize_sched() and synchronize_rcu() in the unload and failure paths. However, there's one case which doesn't have that: the normal case where module loading succeeds, and we free the init section.
We don't want a synchronize_rcu() there, because it would slow down module loading: this bug was introduced in 2009 to speed module loading in the first place.
Thus, we want to do the free in an RCU callback. We do this in the simplest possible way by allocating a new rcu_head: if we put it in the module structure we'd have to worry about that getting freed.
--- kernel/module.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-)
kernel/module.c +42 -13
@@ -2989,10 +2989,31 @@ static void do_mod_ctors(struct module *mod)#endif } +/* For freeing module_init on success, in case kallsyms traversing */ +struct mod_initfree { +struct rcu_head rcu; +void *module_init; +}; + +static void do_free_init (struct rcu_head *head) +{ + struct mod_initfree *m = container_of(head, struct mod_initfree, rcu); +module_memfree (m->module_init); +kfree (m); +} +/* This is where the real work happens */ static int do_init_module (struct module *mod) {int ret =0 ; +struct mod_initfree *freeinit; + + freeinit =kmalloc (sizeof (*freeinit),GFP_KERNEL ); +if (!freeinit) { + ret = -ENOMEM ; + goto fail; + } + freeinit->module_init = mod->module_init;/* * We want to find out whether @mod uses async during init. Clear
@@ -3005,18 +3026,7 @@ static int do_init_module(struct module *mod)if (mod->init !=NULL ) ret =do_one_initcall (mod->init);if (ret <0 ) { -/* -* Init routine failed: abort. Try to protect us from -* buggy refcounters. -*/ - mod->state =MODULE_STATE_GOING ; -synchronize_sched (); -module_put (mod); -blocking_notifier_call_chain (&module_notify_list, -MODULE_STATE_GOING , mod); -free_module (mod); -wake_up_all (&module_wq); -return ret; + goto fail_free_freeinit; }if (ret >0 ) { pr_warn("%s: '%s'->init suspiciously returned %d, it should "
@@ -3062,15 +3072,34 @@ static int do_init_module(struct module *mod)#endif unset_module_init_ro_nx (mod);module_arch_freeing_init (mod); -module_memfree (mod->module_init); mod->module_init =NULL ; mod->init_size =0 ; mod->init_ro_size =0 ; mod->init_text_size =0 ; +/* +* We want to free module_init, but be aware that kallsyms may be +* walking this with preempt disabled. In all the failure paths, +* we call synchronize_rcu/synchronize_sched, but we don't want +* to slow down the success path, so use actual RCU here. +*/ +call_rcu (&freeinit->rcu, do_free_init);mutex_unlock (&module_mutex);wake_up_all (&module_wq);return 0 ; + +fail_free_freeinit: +kfree (freeinit); +fail: +/* Try to protect us from buggy refcounters. */ + mod->state =MODULE_STATE_GOING ; +synchronize_sched (); +module_put (mod); +blocking_notifier_call_chain (&module_notify_list, +MODULE_STATE_GOING , mod); +free_module (mod); +wake_up_all (&module_wq); +return ret; }static int may_init_module (void )
Signature · 1 line
2.1.0
Re: [PATCH 1/3] module_arch_freeing_init(): new hook for archs before module->module_init freed.
In reply to Rusty RussellQuoted 8 lines · Rusty Russell
Archs have been abusing
module_free()to clean up their arch-specific allocations. Sincemodule_free()is also (ab)used by BPF and trace code, let's keep it to simple allocations, and provide a hook called before that.This means that avr32, ia64, parisc and s390 no longer need to implement their own
module_free()at all. avr32 doesn't needmodule_finalize()either.
At a glance it looks sane.
Quoted 6 lines
Signed-off-by Rusty Russell <[email protected]>Cc Chris Metcalf <[email protected]>Cc Haavard Skinnemoen <[email protected]>Cc Hans-Christian Egtvedt <[email protected]>Cc Tony Luck <[email protected]>Cc Fenghua Yu <[email protected]>
Quoted 8 lines
Cc "James E.J. Bottomley" <[email protected]>Cc Helge Deller <[email protected]>Cc Martin Schwidefsky <[email protected]>Cc Heiko Carstens <[email protected]>
Quoted 9 lines
--- arch/avr32/kernel/module.c | 13 +------------ arch/ia64/kernel/module.c | 6 ++---- arch/parisc/kernel/module.c | 6 +----- arch/s390/kernel/module.c | 10 +++------- arch/tile/kernel/module.c | 2 +- include/linux/moduleloader.h | 2 ++ kernel/module.c | 7 +++++++ 7 files changed, 17 insertions(+), 29 deletions(-)
<snipp diff>
Signature · 1 line
HcE
Re: [PATCH 1/3] module_arch_freeing_init(): new hook for archs before module->module_init freed.
In reply to Rusty RussellQuoted 23 lines · Rusty Russell
Archs have been abusing
module_free()to clean up their arch-specific allocations. Sincemodule_free()is also (ab)used by BPF and trace code, let's keep it to simple allocations, and provide a hook called before that.This means that avr32, ia64, parisc and s390 no longer need to implement their own
module_free()at all. avr32 doesn't needmodule_finalize()either.Signed-off-by Rusty Russell <[email protected]>Cc Chris Metcalf <[email protected]>Cc Haavard Skinnemoen <[email protected]>Cc Hans-Christian Egtvedt <[email protected]>Cc Tony Luck <[email protected]>Cc Fenghua Yu <[email protected]>Cc "James E.J. Bottomley" <[email protected]>Cc Helge Deller <[email protected]>Cc Martin Schwidefsky <[email protected]>Cc Heiko Carstens <[email protected]>
Quoted 9 lines
--- arch/avr32/kernel/module.c | 13 +------------ arch/ia64/kernel/module.c | 6 ++---- arch/parisc/kernel/module.c | 6 +----- arch/s390/kernel/module.c | 10 +++------- arch/tile/kernel/module.c | 2 +- include/linux/moduleloader.h | 2 ++ kernel/module.c | 7 +++++++ 7 files changed, 17 insertions(+), 29 deletions(-)
I successfully tested it on the parisc arch.
Thanks!
Helge