All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Naveed Khan <naveed@digiscrypt.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v2] libbpf: initialize btf_ext out-parameter early in btf_parse_elf()
Date: Mon, 13 Jul 2026 15:04:59 +0200	[thread overview]
Message-ID: <alTie0_4pu-T5Bik@krava> (raw)
In-Reply-To: <178378853022.49961.12254894397759137901@digiscrypt.com>

On Sat, Jul 11, 2026 at 10:18:50PM +0530, Naveed Khan wrote:
> btf_parse_elf() only stores to *btf_ext once it reaches the .BTF.ext
> parsing step.  If the function fails before that point (open() aside,
> e.g. elf_begin() failure, missing .BTF section, or a malformed .BTF /
> .BTF.base section), the cleanup path still runs
> 
> 	if (btf_ext)
> 		btf_ext__free(*btf_ext);
> 
> and reads a value that was never written.  A caller that passes an
> uninitialized pointer, e.g.
> 
> 	struct btf_ext *ext;
> 	btf = btf__parse_elf(path, &ext);
> 
> ends up with btf_ext__free() operating on stack garbage, which can
> crash or corrupt the heap.  Current in-tree callers happen to
> NULL-initialize their pointer, but the API contract should not rely
> on that.
> 
> Initialize *btf_ext to NULL on entry so every exit path leaves the
> out-parameter in a defined state.
> 
> Reported-by: sashiko-bot@kernel.org
> Signed-off-by: Naveed Khan <naveed@digiscrypt.com>
> ---
> >From 4dc7d1c8a2a2ec4c00ec038a94a86e26ab6cf75a Mon Sep 17 00:00:00 2001
> From: Naveed Khan <naveed@digiscrypt.com>
> Date: Sat, 11 Jul 2026 16:19:36 +0530
> Subject: [PATCH] libbpf: initialize btf_ext out-parameter early in
>  btf_parse_elf()

hi,
looks like subject/changelog mishap ?

> 
> btf_parse_elf() only stores to *btf_ext once it reaches the .BTF.ext
> parsing step.  If the function fails before that point (open() aside,
> e.g. elf_begin() failure, missing .BTF section, or a malformed .BTF /
> .BTF.base section), the cleanup path still runs
> 
> 	if (btf_ext)
> 		btf_ext__free(*btf_ext);
> 
> and reads a value that was never written.  A caller that passes an
> uninitialized pointer, e.g.
> 
> 	struct btf_ext *ext;
> 	btf = btf__parse_elf(path, &ext);
> 
> ends up with btf_ext__free() operating on stack garbage, which can
> crash or corrupt the heap.  Current in-tree callers happen to
> NULL-initialize their pointer, but the API contract should not rely
> on that.
> 
> Initialize *btf_ext to NULL on entry so every exit path leaves the
> out-parameter in a defined state.
> 
> Reported-by: sashiko-bot@kernel.org
> Signed-off-by: Naveed Khan <naveed@digiscrypt.com>
> ---
>  tools/lib/bpf/btf.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 823bce8951..cabe8286f0 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -1453,6 +1453,9 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
>  	int err = 0, fd = -1;
>  	Elf *elf = NULL;
>  
> +	if (btf_ext)
> +		*btf_ext = NULL;

there's already this setup in btf_parse and it can be removed now
when we do that in btf_parse_elf

jirka

> +
>  	if (elf_version(EV_CURRENT) == EV_NONE) {
>  		pr_warn("failed to init libelf for %s\n", path);
>  		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
> -- 
> 2.52.0
> 

      reply	other threads:[~2026-07-13 13:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-07 20:18 [PATCH] libbpf: fix double-free of distilled base BTF on .BTF.ext parse error Naveed Khan
2026-07-07 20:33 ` sashiko-bot
2026-07-10 21:53   ` Andrii Nakryiko
2026-07-11 16:48     ` [PATCH v2] libbpf: initialize btf_ext out-parameter early in btf_parse_elf() Naveed Khan
2026-07-13 13:04       ` Jiri Olsa [this message]

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=alTie0_4pu-T5Bik@krava \
    --to=olsajiri@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=naveed@digiscrypt.com \
    /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.