Skip to content

Commit ca3881f

Browse files
petrpavludkruces
authored andcommitted
module: Fix memory deallocation on error path in move_module()
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]>
1 parent d7b8f8e commit ca3881f

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
@@ -2697,7 +2697,7 @@ static int find_module_sections(struct module *mod, struct load_info *info)
26972697
static int move_module(struct module *mod, struct load_info *info)
26982698
{
26992699
int i;
2700-
enum mod_mem_type t = 0;
2700+
enum mod_mem_type t = MOD_MEM_NUM_TYPES;
27012701
int ret = -ENOMEM;
27022702
bool codetag_section_found = false;
27032703

@@ -2776,7 +2776,7 @@ static int move_module(struct module *mod, struct load_info *info)
27762776
return 0;
27772777
out_err:
27782778
module_memory_restore_rox(mod);
2779-
for (t--; t >= 0; t--)
2779+
while (t--)
27802780
module_memory_free(mod, t);
27812781
if (codetag_section_found)
27822782
codetag_free_module_sections(mod);

0 commit comments

Comments
 (0)