lib: prevent module unloading if memory is not freed
Skip freeing module's data section if there are non-zero allocation tags because otherwise, once these allocations are freed, the access to their code tag would cause UAF. Link: https://lkml.kernel.org/r/20240321163705.3067592-13-surenb@google.com Signed-off-by: Suren Baghdasaryan <surenb@google.com> Tested-by: Kees Cook <keescook@chromium.org> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alex Gaynor <alex.gaynor@gmail.com> Cc: Alice Ryhl <aliceryhl@google.com> Cc: Andreas Hindborg <a.hindborg@samsung.com> Cc: Benno Lossin <benno.lossin@proton.me> Cc: "Björn Roy Baron" <bjorn3_gh@protonmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Christoph Lameter <cl@linux.com> Cc: Dennis Zhou <dennis@kernel.org> Cc: Gary Guo <gary@garyguo.net> Cc: Kent Overstreet <kent.overstreet@linux.dev> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Tejun Heo <tj@kernel.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Wedson Almeida Filho <wedsonaf@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
committed by
Andrew Morton
parent
a473573964
commit
47a92dfbe0
+19
-8
@@ -1211,15 +1211,19 @@ static void *module_memory_alloc(unsigned int size, enum mod_mem_type type)
|
||||
return module_alloc(size);
|
||||
}
|
||||
|
||||
static void module_memory_free(void *ptr, enum mod_mem_type type)
|
||||
static void module_memory_free(void *ptr, enum mod_mem_type type,
|
||||
bool unload_codetags)
|
||||
{
|
||||
if (!unload_codetags && mod_mem_type_is_core_data(type))
|
||||
return;
|
||||
|
||||
if (mod_mem_use_vmalloc(type))
|
||||
vfree(ptr);
|
||||
else
|
||||
module_memfree(ptr);
|
||||
}
|
||||
|
||||
static void free_mod_mem(struct module *mod)
|
||||
static void free_mod_mem(struct module *mod, bool unload_codetags)
|
||||
{
|
||||
for_each_mod_mem_type(type) {
|
||||
struct module_memory *mod_mem = &mod->mem[type];
|
||||
@@ -1230,20 +1234,27 @@ static void free_mod_mem(struct module *mod)
|
||||
/* Free lock-classes; relies on the preceding sync_rcu(). */
|
||||
lockdep_free_key_range(mod_mem->base, mod_mem->size);
|
||||
if (mod_mem->size)
|
||||
module_memory_free(mod_mem->base, type);
|
||||
module_memory_free(mod_mem->base, type,
|
||||
unload_codetags);
|
||||
}
|
||||
|
||||
/* MOD_DATA hosts mod, so free it at last */
|
||||
lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size);
|
||||
module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA);
|
||||
module_memory_free(mod->mem[MOD_DATA].base, MOD_DATA, unload_codetags);
|
||||
}
|
||||
|
||||
/* Free a module, remove from lists, etc. */
|
||||
static void free_module(struct module *mod)
|
||||
{
|
||||
bool unload_codetags;
|
||||
|
||||
trace_module_free(mod);
|
||||
|
||||
codetag_unload_module(mod);
|
||||
unload_codetags = codetag_unload_module(mod);
|
||||
if (!unload_codetags)
|
||||
pr_warn("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n",
|
||||
mod->name);
|
||||
|
||||
mod_sysfs_teardown(mod);
|
||||
|
||||
/*
|
||||
@@ -1285,7 +1296,7 @@ static void free_module(struct module *mod)
|
||||
kfree(mod->args);
|
||||
percpu_modfree(mod);
|
||||
|
||||
free_mod_mem(mod);
|
||||
free_mod_mem(mod, unload_codetags);
|
||||
}
|
||||
|
||||
void *__symbol_get(const char *symbol)
|
||||
@@ -2298,7 +2309,7 @@ static int move_module(struct module *mod, struct load_info *info)
|
||||
return 0;
|
||||
out_enomem:
|
||||
for (t--; t >= 0; t--)
|
||||
module_memory_free(mod->mem[t].base, t);
|
||||
module_memory_free(mod->mem[t].base, t, true);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -2428,7 +2439,7 @@ static void module_deallocate(struct module *mod, struct load_info *info)
|
||||
percpu_modfree(mod);
|
||||
module_arch_freeing_init(mod);
|
||||
|
||||
free_mod_mem(mod);
|
||||
free_mod_mem(mod, true);
|
||||
}
|
||||
|
||||
int __weak module_finalize(const Elf_Ehdr *hdr,
|
||||
|
||||
Reference in New Issue
Block a user