BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Naveed Khan" <naveed@digiscrypt.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v3] libbpf: initialize btf_ext out-parameter early in btf_parse_elf()
Date: Thu, 23 Jul 2026 07:01:22 +0000	[thread overview]
Message-ID: <20260723070122.99F921F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <178478908698.2.4018643732078767322@digiscrypt.com>

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

Pre-existing issues:
- [High] btf_parse_elf() incorrectly uses IS_ERR() to check for btf_ext__new() failure, causing .BTF.ext parsing errors to be silently ignored.
- [High] A latent double-free of dist_base_btf exists on the error path of btf_parse_elf().
--

--- Patch [1]: [PATCH v3] libbpf: initialize btf_ext out-parameter early in btf_parse_elf() ---
commit 5cc9e33bf25334c39330a03f9b8133245f9ed0c1
Author: Naveed Khan <naveed@digiscrypt.com>

libbpf: initialize btf_ext out-parameter early in btf_parse_elf()

This commit initializes the btf_ext out-parameter to NULL early in
btf_parse_elf(). This prevents callers that pass an uninitialized pointer
from encountering stack garbage during cleanup if the function fails before
reaching the .BTF.ext parsing step.

> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 823bce895178d..cabe8286f06c9 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;
> +
>  	if (elf_version(EV_CURRENT) == EV_NONE) {
>  		pr_warn("failed to init libelf for %s\n", path);
>  		return ERR_PTR(-LIBBPF_ERRNO__LIBELF);

[Severity: High]
This is a pre-existing issue, but does btf_parse_elf() incorrectly use
IS_ERR() to check for btf_ext__new() failure further down in this function?

tools/lib/bpf/btf.c:btf_parse_elf() {
        ...
        if (btf_ext && secs.btf_ext_data) {
                *btf_ext = btf_ext__new(secs.btf_ext_data->d_buf, 
                                        secs.btf_ext_data->d_size);
                if (IS_ERR(*btf_ext)) {
                        err = PTR_ERR(*btf_ext);
                        goto done;
                }
        ...
}

If btf_ext__new() returns NULL on error rather than ERR_PTR, IS_ERR() will
evaluate to false. This would cause the function to silently ignore
malformed .BTF.ext data or allocation failures, leaving *btf_ext as NULL
and misleading the caller into assuming there is no extended BTF data.

[Severity: High]
This is also a pre-existing issue, but is there a latent double-free of
dist_base_btf on the error path of btf_parse_elf()?

Earlier in the function, ownership of dist_base_btf is transferred to btf:

tools/lib/bpf/btf.c:btf_parse_elf() {
        ...
        if (dist_base_btf)
                btf->owns_base = true;
        ...
}

Then on the error path, both are explicitly freed:

done:
        ...
        btf__free(dist_base_btf);
        btf__free(btf);

If btf->owns_base is true, btf__free(btf) recursively frees its base BTF,
which points to the same dist_base_btf object. If an error occurs after
the ownership transfer (such as if the btf_ext__new() error check is fixed),
would this lead to a double-free?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/178478908698.2.4018643732078767322@digiscrypt.com?part=1

      reply	other threads:[~2026-07-23  7:01 UTC|newest]

Thread overview: 7+ 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
2026-07-23  6:44         ` [PATCH v3] " Naveed Khan
2026-07-23  7:01           ` sashiko-bot [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=20260723070122.99F921F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=naveed@digiscrypt.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