Skip to contentSkip to conversation

riscv: fix memory leakage in process_accumulated_relocations

PATCH Superseded [email protected] 2 participants 3 messages Nov 16, 2024 – Nov 20, 2024 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: fix memory leakage in process_accumulated_relocations

When module relocation is done, process_accumulated_relocations() frees all dynamic allocated memory. rel_head_iter->rel_entry is missed to free that kmemleak might report:

unreferenced object 0xffffffd880c5fc40 (size 16):
comm "insmod", pid 1101, jiffies 4295045138
hex dump (first 16 bytes):

    e0 c0 f5 87 d8 ff ff ff 60 c5 f5 87 d8 ff ff ff  ........`.......
  backtrace (crc d2ecb20c):
    [<00000000b01655f6>] kmalloc_trace_noprof+0x268/0x2f6
    [<000000006dc0067a>] add_relocation_to_accumulate.constprop.0+0xf2/0x1aa
    [<00000000e1b29a36>] apply_relocate_add+0x13c/0x36e
    [<000000007543f1fb>] load_module+0x5c6/0x83e
    [<00000000abce12e8>] init_module_from_file+0x74/0xaa
    [<0000000049413e3d>] idempotent_init_module+0x116/0x22e
    [<00000000f9b98b85>] __riscv_sys_finit_module+0x62/0xae
---
 arch/riscv/kernel/module.c | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/kernel/module.c +1 -0
index 1cd461f3d87..f8c3c4b47dc 100644
@@ -643,6 +643,7 @@ process_accumulated_relocations(struct module *me,
                        }
                        reloc_handlers[curr_type].accumulate_handler(
                                me, location, buffer);
+                       kfree(rel_head_iter->rel_entry);
                        kfree(rel_head_iter);
                }
                kfree(bucket_iter);
Signature · 7 lines
2.39.5

</[email protected]>
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw
Quoted 27 lines · On 16/11/2024 11:42, [email protected] wrote:

When module relocation is done, process_accumulated_relocations() frees all dynamic allocated memory. rel_head_iter-&gt;rel_entry is missed to free that kmemleak might report:

unreferenced object 0xffffffd880c5fc40 (size 16):
comm "insmod", pid 1101, jiffies 4295045138
hex dump (first 16 bytes):

    e0 c0 f5 87 d8 ff ff ff 60 c5 f5 87 d8 ff ff ff  ........`.......
  backtrace (crc d2ecb20c):
    [&lt;00000000b01655f6&gt;] kmalloc_trace_noprof+0x268/0x2f6
    [&lt;000000006dc0067a&gt;] add_relocation_to_accumulate.constprop.0+0xf2/0x1aa
    [&lt;00000000e1b29a36&gt;] apply_relocate_add+0x13c/0x36e
    [&lt;000000007543f1fb&gt;] load_module+0x5c6/0x83e
    [&lt;00000000abce12e8&gt;] init_module_from_file+0x74/0xaa
    [&lt;0000000049413e3d&gt;] idempotent_init_module+0x116/0x22e
    [&lt;00000000f9b98b85&gt;] __riscv_sys_finit_module+0x62/0xae
---
 arch/riscv/kernel/module.c | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/kernel/module.c +0 -0
index 1cd461f3d87..f8c3c4b47dc 100644

arch/riscv/kernel/module.c process_accumulated_relocations(struct module *me, · line 646

Quoted 4 lines
arch/riscv/kernel/module.c +1 -0
@@ -643,6 +643,7 @@ process_accumulated_relocations(struct module *me,
                        }
                        reloc_handlers[curr_type].accumulate_handler(
                                me, location, buffer);
+                       kfree(rel_head_iter-&gt;rel_entry);

Hey Kai,

Your patch output is messed up probably due to some encoding issue.

But looknig at it and the module code, it seems like rel_entry does not need to be a pointer. It can be a plain "struct list_head rel_entry" and then simply pass the list_head pointer rather than allocating/freeing it. That remove an allocation as well as some memory leak.

BTW, for fixes, you should add a Fixes: tag with the original commit sha1 that introduced the bug.

Thanks,

Clément

Quoted 3 lines
arch/riscv/kernel/module.c +0 -0
                        kfree(rel_head_iter);
                }
                kfree(bucket_iter);
author
#2 lore raw
Quoted 42 lines · Clément Léger
Quoted 32 lines · On 16/11/2024 11:42, [email protected] wrote:

When module relocation is done, process_accumulated_relocations() frees all dynamic allocated memory. rel_head_iter-&gt;rel_entry is missed to free that kmemleak might report:

unreferenced object 0xffffffd880c5fc40 (size 16):
  comm "insmod", pid 1101, jiffies 4295045138
  hex dump (first 16 bytes):
    e0 c0 f5 87 d8 ff ff ff 60 c5 f5 87 d8 ff ff ff ........`.......
  backtrace (crc d2ecb20c):
    [&lt;00000000b01655f6&gt;] kmalloc_trace_noprof+0x268/0x2f6 [&lt;000000006dc0067a&gt;]

add_relocation_to_accumulate.constprop.0+0xf2/0x1aa
    [&lt;00000000e1b29a36&gt;] apply_relocate_add+0x13c/0x36e
    [&lt;000000007543f1fb&gt;] load_module+0x5c6/0x83e
    [&lt;00000000abce12e8&gt;] init_module_from_file+0x74/0xaa
    [&lt;0000000049413e3d&gt;] idempotent_init_module+0x116/0x22e
    [&lt;00000000f9b98b85&gt;] __riscv_sys_finit_module+0x62/0xae

---
 arch/riscv/kernel/module.c | 1 +

 1 file changed, 1 insertion(+)
arch/riscv/kernel/module.c +0 -0
index 1cd461f3d87..f8c3c4b47dc 100644
@@ -643,6 +643,7 @@ process_accumulated_relocations(struct module
 *me,
                         }
Quoted 4 lines


reloc_handlers[curr_type].accumulate_handler(
                                me, location, buffer); +                       kfree(rel_head_iter-&gt;rel_entry);

Hey Kai,

Quoted 1 line

Your patch output is messed up probably due to some encoding issue.

Sorry, I've already resent one.

Quoted 4 lines

But looknig at it and the module code, it seems like rel_entry does not
need to be a pointer. It can be a plain "struct list_head rel_entry" and

I didn't find the plain(none pointer) usage. Where please?

Quoted 5 lines

then simply pass the list_head pointer rather than allocating/freeing it. That remove an allocation as well as some memory leak.

BTW, for fixes, you should add a Fixes: tag with the original commit sha1 that introduced the bug.

Thank you. I'll try.

laokz

Quoted 7 lines

Thanks,

Clément

Quoted 3 lines

                        kfree(rel_head_iter);
                }
                kfree(bucket_iter);