BPF List
 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 4/4] bpf: Fix double-free RO header in bpf_jit_free
Date: Wed, 22 Jul 2026 11:33:53 +0000	[thread overview]
Message-ID: <20260722113354.503391F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260722112754.1615201-5-pulehui@huaweicloud.com>

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

Pre-existing issues:
- [Critical] When `bpf_jit_binary_pack_finalize` fails, `bpf_jit_binary_pack_free` reads an uninitialized/garbage `size` from `ro_header->size`, leading to a kernel panic in `vfree()` and module memory uncharge counter corruption.
--

--- Patch [4]: [PATCH bpf-next 4/4] bpf: Fix double-free RO header in bpf_jit_free ---
commit 4586888c771a3a31208f8ce31608697cc4147140
Author: Pu Lehui <pulehui@huawei.com>

bpf: Fix double-free RO header in bpf_jit_free

This commit modifies bpf_jit_binary_pack_finalize() to drop the implicit free
of ro_header on failure. Architectures are updated to explicitly call
bpf_jit_binary_pack_free() in their error paths instead.

> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index de7515ea1beae..d017bb0397b88 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
> @@ -3988,7 +3988,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
>  			 * Both cases are serious bugs and justify WARN_ON.
>  			 */
>  			if (WARN_ON(bpf_jit_binary_pack_finalize(header, rw_header))) {
> -				/* header has been freed */
> +				bpf_jit_binary_pack_free(header, NULL);
>  				header = NULL;
>  				goto out_image;
>  			}

[ ... ]

> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 47fe047ad30b8..77195911a3b37 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -1237,10 +1237,9 @@ int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header,
>  
>  	kvfree(rw_header);
>  
> -	if (IS_ERR(ptr)) {
> -		bpf_prog_pack_free(ro_header, ro_header->size);
> +	if (IS_ERR(ptr))
>  		return PTR_ERR(ptr);
> -	}
> +
>  	return 0;
>  }

[Severity: Critical]
This is a pre-existing issue, but does reading ro_header->size cause a panic
when bpf_arch_text_copy() fails?

If bpf_arch_text_copy() returns an error, the RO memory was not written, so
ro_header->size contains uninitialized garbage or illegal instructions. Because
kvfree(rw_header) is called unconditionally just before this check, the only
header holding the correct allocation size is destroyed before the caller can
properly free the resource.

When the architecture fallback explicitly calls bpf_jit_binary_pack_free()
(or previously when this function called bpf_prog_pack_free directly), it
reads the garbage size from ro_header->size:

kernel/bpf/core.c:bpf_jit_binary_pack_free() {
    u32 size = ro_header->size; 
    bpf_prog_pack_free(ro_header, size);
    ...
    bpf_jit_uncharge_modmem(size); 
}

Since this garbage size typically exceeds BPF_PROG_PACK_SIZE, the allocator
falls back to vfree(ro_header). As ro_header is a pointer to the interior of
a vmalloc'd pack, vfree() will trigger a kernel panic. Additionally,
bpf_jit_uncharge_modmem() drastically underflows the module memory tracking
counter.

Could the size be preserved or passed differently to avoid reading from the
uninitialized RO header during the error path?

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

  reply	other threads:[~2026-07-22 11:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 11:27 [PATCH bpf-next 0/4] Fixes for bpf_jit_free Pu Lehui
2026-07-22 11:27 ` [PATCH bpf-next 1/4] bpf, arm64: Fix memory leak in bpf_jit_free Pu Lehui
2026-07-22 11:38   ` sashiko-bot
2026-07-24  1:39     ` Pu Lehui
2026-07-23  1:22   ` Xu Kuohai
2026-07-22 11:27 ` [PATCH bpf-next 2/4] LoongArch: BPF: " Pu Lehui
2026-07-22 11:35   ` sashiko-bot
2026-07-24  3:16     ` Pu Lehui
2026-07-24  8:04   ` Tiezhu Yang
2026-07-24  8:15     ` Huacai Chen
2026-07-22 11:27 ` [PATCH bpf-next 3/4] riscv, bpf: Adjust bpf_func to account for CFI offset " Pu Lehui
2026-07-22 11:38   ` sashiko-bot
2026-07-24  1:40     ` Pu Lehui
2026-07-22 11:27 ` [PATCH bpf-next 4/4] bpf: Fix double-free RO header " Pu Lehui
2026-07-22 11:33   ` sashiko-bot [this message]
2026-07-24  3:14     ` 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=20260722113354.503391F00A3A@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox