All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pu Lehui" <pulehui@huaweicloud.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v3 6/9] bpf, arm64: Fix memory leak in bpf_jit_free
Date: Sat, 25 Jul 2026 16:01:03 +0000	[thread overview]
Message-ID: <20260725160104.309A31F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260725154915.2488611-7-pulehui@huaweicloud.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] `bpf_jit_free()` ignores the return value of `bpf_jit_binary_pack_finalize()`, leading to a Use-After-Free and Double Free if finalization fails.
- [High] Pre-existing percpu memory leak of `priv_stack_ptr` on BPF JIT compilation failure.
- [High] The identical memory leak of `ctx.offset` fixed by this patch in ARM64 remains unfixed in the LoongArch BPF JIT compiler.
--

--- Patch [6]: [PATCH bpf-next v3 6/9] bpf, arm64: Fix memory leak in bpf_jit_free ---
commit c1be108f68505531829d01d1fddc5f23ead947da
Author: Pu Lehui <pulehui@huawei.com>

bpf, arm64: Fix memory leak in bpf_jit_free

When JIT compilation fails for a subprogram, the BPF core cleans up
previously compiled subprograms by calling bpf_jit_free(). This commit adds
a missing kvfree(jit_data->ctx.offset) to bpf_jit_free() in ARM64,
preventing a memory leak of the JIT context offsets array.

> diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c
> index 4cdc7dfb05ba9..f4e4d4578e38a 100644
> --- a/arch/arm64/net/bpf_jit_comp.c
> +++ b/arch/arm64/net/bpf_jit_comp.c
> @@ -3237,6 +3237,7 @@ void bpf_jit_free(struct bpf_prog *prog)

[Severity: High]
This isn't a bug introduced by this patch, but is there a pre-existing
percpu memory leak of priv_stack_ptr when JIT compilation fails?

Looking at bpf_int_jit_compile() when build_body() fails, out_free_hdr frees
ro_header but does not set it to NULL. In the out_off block:

arch/arm64/net/bpf_jit_comp.c:bpf_int_jit_compile() {
    ...
    if (!ro_header && priv_stack_ptr) {
        free_percpu(priv_stack_ptr);
        prog->aux->priv_stack_ptr = NULL;
    }
    ...
}

Because ro_header is a dangling pointer, this check evaluates to false,
skipping the free. Later, when the BPF core aborts and calls
bpf_jit_free(prog), prog->jited is 0:

arch/arm64/net/bpf_jit_comp.c:bpf_jit_free() {
    if (prog->jited) {
        ...
    }
    ...
}

This skips the cleanup block entirely, leaving priv_stack_ptr permanently
leaked. Would it be appropriate to address this as well?

>  		 */
>  		if (jit_data) {
>  			bpf_jit_binary_pack_finalize(jit_data->ro_header, jit_data->header);

[Severity: Critical]
This is a pre-existing issue, but could ignoring the return value of
bpf_jit_binary_pack_finalize() here lead to a use-after-free and double free?

If bpf_arch_text_copy() fails during finalization,
bpf_jit_binary_pack_finalize() frees the ro_header internally and returns an
error:

kernel/bpf/core.c:bpf_jit_binary_pack_finalize() {
    ...
    if (IS_ERR(ptr)) {
        bpf_prog_pack_free(ro_header, ro_header->size);
        return PTR_ERR(ptr);
    }
    ...
}

Since bpf_jit_free() ignores this error, it proceeds to retrieve the freed
header and unconditionally frees it again:

arch/arm64/net/bpf_jit_comp.c:bpf_jit_free() {
    ...
    prog->bpf_func = (void *)prog->bpf_func - cfi_get_offset();
    hdr = bpf_jit_binary_pack_hdr(prog);
    bpf_jit_binary_pack_free(hdr, NULL);
}

Inside bpf_jit_binary_pack_free(), it reads ro_header->size (use-after-free)
and calls bpf_prog_pack_free() on it again (double free). Can this cause
memory corruption?

> +			kvfree(jit_data->ctx.offset);

[Severity: High]
This isn't a bug introduced by this patch, but does the LoongArch BPF JIT
compiler suffer from this exact same memory leak?

Looking at the LoongArch implementation, it uses the same multi-pass logic
for subprograms and preserves ctx.offset in jit_data. However, its
bpf_jit_free() does not free ctx.offset:

arch/loongarch/net/bpf_jit.c:bpf_jit_free() {
    ...
    if (jit_data) {
        bpf_jit_binary_pack_finalize(jit_data->ro_header, jit_data->header);
        kfree(jit_data);
    }
    ...
}

Should a similar kvfree() be added to arch/loongarch/net/bpf_jit.c?

>  			kfree(jit_data);
>  		}
>  		prog->bpf_func = (void *)prog->bpf_func - cfi_get_offset();

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260725154915.2488611-1-pulehui@huaweicloud.com?part=6

  reply	other threads:[~2026-07-25 16:01 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-25 15:49 [PATCH bpf-next v3 0/9] Fixes for bpf_jit_free Pu Lehui
2026-07-25 15:49 ` [PATCH bpf-next v3 1/9] bpf: Extract the bpf_jit_binary_hdr helper Pu Lehui
2026-07-25 15:49 ` [PATCH bpf-next v3 2/9] s390/bpf: Fix memory leak in bpf_jit_free Pu Lehui
2026-07-25 15:49 ` [PATCH bpf-next v3 3/9] parisc: " Pu Lehui
2026-07-25 15:54   ` sashiko-bot
2026-07-25 15:49 ` [PATCH bpf-next v3 4/9] bpf, sparc64: " Pu Lehui
2026-07-25 15:49 ` [PATCH bpf-next v3 5/9] ARC, bpf: " Pu Lehui
2026-07-25 15:49 ` [PATCH bpf-next v3 6/9] bpf, arm64: " Pu Lehui
2026-07-25 16:01   ` sashiko-bot [this message]
2026-07-25 15:49 ` [PATCH bpf-next v3 7/9] bpf, arm64: Fix private stack leak when JIT failed Pu Lehui
2026-07-25 15:55   ` sashiko-bot
2026-07-25 15:49 ` [PATCH bpf-next v3 8/9] riscv, bpf: Adjust bpf_func to account for CFI offset in bpf_jit_free Pu Lehui
2026-07-25 15:57   ` sashiko-bot
2026-07-25 15:49 ` [PATCH bpf-next v3 9/9] bpf: Fix double-free RO header " Pu Lehui

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260725160104.309A31F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=pulehui@huaweicloud.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.