From: Naveed Khan <naveed@digiscrypt.com>
To: bpf@vger.kernel.org
Subject: [PATCH v3] libbpf: initialize btf_ext out-parameter early in btf_parse_elf()
Date: Thu, 23 Jul 2026 12:14:46 +0530 [thread overview]
Message-ID: <178478908698.2.4018643732078767322@digiscrypt.com> (raw)
In-Reply-To: <alTie0_4pu-T5Bik@krava>
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.
Note that the similar initialization in btf_parse() is intentionally
kept: btf_parse() returns without ever calling btf_parse_elf() when
btf_parse_raw() succeeds (raw BTF file, no .BTF.ext) or fails with an
error other than -EPROTO, so its own initialization is what defines
*btf_ext for btf__parse() callers on those paths.
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;
+
if (elf_version(EV_CURRENT) == EV_NONE) {
pr_warn("failed to init libelf for %s\n", path);
return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
next prev parent reply other threads:[~2026-07-23 6:44 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 ` Naveed Khan [this message]
2026-07-23 7:01 ` [PATCH v3] " sashiko-bot
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=178478908698.2.4018643732078767322@digiscrypt.com \
--to=naveed@digiscrypt.com \
--cc=bpf@vger.kernel.org \
/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