Re: [PATCH v6 05/14] arm64: kexec_file: Fix elf_headers memory leak in retry loop

From: Catalin Marinas

Date: Sat Oct 10 2026 - 16:01:25 EST


On Mon, Sep 21, 2026 at 05:04:41PM +0800, Jinjie Ruan wrote:
> If load_other_segments() fails after image->elf_headers is assigned,
> the memory lifecycle is safely managed by the global kimage object
> and will be freed in arch_kimage_file_post_load_cleanup().
>
> However, during a retry loop in image_load(), a subsequent iteration
> will allocate a new buffer and overwrite image->elf_headers. This
> permanently leaks the stale memory from the previous iteration before
> the global cleanup can track it.
>
> Fix this by explicitly freeing the stale `image->elf_headers` buffer
> before assigning the newly allocated headers.
>
> Cc: Catalin Marinas <catalin.marinas@xxxxxxx>
> Cc: Will Deacon <will@xxxxxxxxxx>
> Cc: Thomas Huth <thuth@xxxxxxxxxx>
> Cc: Breno Leitao <leitao@xxxxxxxxxx>
> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> Cc: Yeoreum Yun <yeoreum.yun@xxxxxxx>
> Cc: Baoquan He <bhe@xxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Fixes: 108aa503657e ("arm64: kexec_file: try more regions if loading segments fails")
> Signed-off-by: Jinjie Ruan <ruanjinjie@xxxxxxxxxx>
> ---
> arch/arm64/kernel/machine_kexec_file.c | 4 ++++
> 1 file changed, 4 insertions(+)

Acked-by: Catalin Marinas <catalin.marinas@xxxxxxx>