[resend PATCH] riscv: fix memory leakage in process_accumulated_relocations

laokz posted 1 patch 1 year, 2 months ago
arch/riscv/kernel/module.c | 1 +
1 file changed, 1 insertion(+)
[resend PATCH] riscv: fix memory leakage in process_accumulated_relocations
Posted by laokz 1 year, 2 months ago
Sorry for last weird email.
---

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

Signed-off-by: Kai Zhang <zhangkai@iscas.ac.cn>
---
 arch/riscv/kernel/module.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 1cd461f3d87..f8c3c4b47dc 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -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);


Re: [resend PATCH] riscv: fix memory leakage in process_accumulated_relocations
Posted by Alexandre Ghiti 11 months, 1 week ago
Hi Kai,

On 16/11/2024 13:07, laokz wrote:
> Sorry for last weird email.
> ---
>
> 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
>
> Signed-off-by: Kai Zhang <zhangkai@iscas.ac.cn>
> ---
>   arch/riscv/kernel/module.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> index 1cd461f3d87..f8c3c4b47dc 100644
> --- a/arch/riscv/kernel/module.c
> +++ b/arch/riscv/kernel/module.c
> @@ -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);
>
>
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv


This patch is not relevant anymore since commit 03f0b548537f ("riscv: 
module: remove relocation_head rel_entry member allocation") which 
turned the dynamic allocation of rel_entry into a static allocation.

Thanks anyway Kai, that was a good catch.

Alex

Re: [resend PATCH] riscv: fix memory leakage in process_accumulated_relocations
Posted by Palmer Dabbelt 11 months, 1 week ago
On Sat, 16 Nov 2024 04:07:18 PST (-0800), zhangkai@iscas.ac.cn wrote:
> Sorry for last weird email.
> ---
>
> 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
>
> Signed-off-by: Kai Zhang <zhangkai@iscas.ac.cn>
> ---
>  arch/riscv/kernel/module.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> index 1cd461f3d87..f8c3c4b47dc 100644
> --- a/arch/riscv/kernel/module.c
> +++ b/arch/riscv/kernel/module.c
> @@ -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);

I just found this one.  The patch is also a bit mangled, so it doesn't 
apply (and probably got missed by some tools).

I had this buffer up to say something else, but I don't remember what it 
was.