Skip to content

Commit a0bfd3b

Browse files
petrpavluedoardocanepa
authored andcommitted
module: Fix memory deallocation on error path in move_module()
BugLink: https://bugs.launchpad.net/bugs/2123745 [ Upstream commit ca3881f6fd8e9b6eb2d51e8718d07d3b8029d886 ] The function move_module() uses the variable t to track how many memory types it has allocated and consequently how many should be freed if an error occurs. The variable is initially set to 0 and is updated when a call to module_memory_alloc() fails. However, move_module() can fail for other reasons as well, in which case t remains set to 0 and no memory is freed. Fix the problem by initializing t to MOD_MEM_NUM_TYPES. Additionally, make the deallocation loop more robust by not relying on the mod_mem_type_t enum having a signed integer as its underlying type. Fixes: c7ee8ae ("module: add stop-grap sanity check on module memcpy()") Signed-off-by: Petr Pavlu <[email protected]> Reviewed-by: Sami Tolvanen <[email protected]> Reviewed-by: Daniel Gomez <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Daniel Gomez <[email protected]> Message-ID: <[email protected]> Signed-off-by: Sasha Levin <[email protected]> [nwager: Context change due to missing commit: c287c0723329 ("module: switch to execmem API for remapping as RW and restoring ROX")] Signed-off-by: Noah Wager <[email protected]> Signed-off-by: Edoardo Canepa <[email protected]>
1 parent 0a4f611 commit a0bfd3b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

kernel/module/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ static int find_module_sections(struct module *mod, struct load_info *info)
26352635
static int move_module(struct module *mod, struct load_info *info)
26362636
{
26372637
int i;
2638-
enum mod_mem_type t = 0;
2638+
enum mod_mem_type t = MOD_MEM_NUM_TYPES;
26392639
int ret = -ENOMEM;
26402640
bool codetag_section_found = false;
26412641

@@ -2717,7 +2717,7 @@ static int move_module(struct module *mod, struct load_info *info)
27172717

27182718
return 0;
27192719
out_err:
2720-
for (t--; t >= 0; t--)
2720+
while (t--)
27212721
module_memory_free(mod, t);
27222722
if (codetag_section_found)
27232723
codetag_free_module_sections(mod);

0 commit comments

Comments
 (0)