bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: bpf@vger.kernel.org
Subject: [PATCH bpf] libbpf: Fix segfault in light skeleton for objects without BTF
Date: Thu, 30 Sep 2021 11:46:34 +0530	[thread overview]
Message-ID: <20210930061634.1840768-1-memxor@gmail.com> (raw)

When fed an empty BPF object, bpftool gen skeleton -L crashes at
btf__set_fd since it assumes presence of obj->btf, however for the
sequence below clang adds no .BTF section (hence no BTF).

$ touch a.bpf.c
$ clang -O2 -g -target bpf -c a.bpf.c
$ bpftool gen skeleton -L a.bpf.o
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */

struct a_bpf {
	struct bpf_loader_ctx ctx;
Segmentation fault (core dumped)

The same occurs for files compiled without BTF info, i.e. without
clang's -g flag.

Fixes: 67234743736a (libbpf: Generate loader program out of BPF ELF file.)
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
 tools/lib/bpf/libbpf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0f85d441fca3..b6b130171b86 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -7031,7 +7031,8 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)

 	if (obj->gen_loader) {
 		/* reset FDs */
-		btf__set_fd(obj->btf, -1);
+		if (obj->btf)
+			btf__set_fd(obj->btf, -1);
 		for (i = 0; i < obj->nr_maps; i++)
 			obj->maps[i].fd = -1;
 		if (!err)

             reply	other threads:[~2021-09-30  6:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-30  6:16 Kumar Kartikeya Dwivedi [this message]
2021-09-30 22:54 ` [PATCH bpf] libbpf: Fix segfault in light skeleton for objects without BTF Daniel Borkmann

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:

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

  git send-email \
    --in-reply-to=20210930061634.1840768-1-memxor@gmail.com \
    --to=memxor@gmail.com \
    --cc=bpf@vger.kernel.org \


* 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;
as well as URLs for NNTP newsgroup(s).