BPF List
 help / color / mirror / Atom feed
From: "Thiébaud Weksteen" <tweek@google.com>
To: Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	 Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Shuah Khan <shuah@kernel.org>
Cc: "Thiébaud Weksteen" <tweek@google.com>,
	bpf@vger.kernel.org, linux-kselftest@vger.kernel.org,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Sid Nayyar" <sidnayyar@google.com>
Subject: [PATCH bpf 1/2] libbpf: Skip BPF token preparation when generating loader
Date: Mon, 23 Nov 2026 15:57:29 +1100	[thread overview]
Message-ID: <20261123045730.1960346-2-tweek@google.com> (raw)
In-Reply-To: <20261123045730.1960346-1-tweek@google.com>

When obj->gen_loader is set, libbpf emits loader instructions instead of
issuing BPF syscalls on the host. Every other kernel-interaction step in
bpf_object_prepare() (such as bpf_object__probe_loading() and
bpf_object__load_vmlinux_btf()) already short-circuits on obj->gen_loader,
but bpf_object_prepare_token() does not.

As a result, offline loader generation (e.g. bpftool gen skeleton -L)
probes /sys/fs/bpf and honors LIBBPF_BPF_TOKEN_PATH. Worse, if the build
host has a BPF token-delegated bpffs mounted, bpf_object_prepare_token()
sets obj->token_fd, causing bpf_object__create_map() and
bpf_object_load_prog() to set BPF_F_TOKEN_FD in map_flags/prog_flags.
Because gen_loader copies those flags into the loader blob without
serializing any token_fd, the generated loader fails at runtime.

Return early in bpf_object_prepare_token() when obj->gen_loader is set.

Fixes: 6b434b61b4d9 ("libbpf: Wire up BPF token support at BPF object level")
Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
 tools/lib/bpf/libbpf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index fb8a1f07c5b6..388facd11c38 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -5212,6 +5212,9 @@ static int bpf_object_prepare_token(struct bpf_object *obj)
 	bool mandatory;
 	enum libbpf_print_level level;
 
+	if (obj->gen_loader)
+		return 0;
+
 	/* token is explicitly prevented */
 	if (obj->token_path && obj->token_path[0] == '\0') {
 		pr_debug("object '%s': token is prevented, skipping...\n", obj->name);
-- 
2.56.1.750.gf70dc682cf-goog


  reply	other threads:[~2026-11-23  4:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  4:57 [PATCH bpf 0/2] libbpf: Skip BPF token preparation for gen_loader Thiébaud Weksteen
2026-11-23  4:57 ` Thiébaud Weksteen [this message]
2026-11-23  4:57 ` [PATCH bpf 2/2] selftests/bpf: Verify gen_loader skips BPF token preparation Thiébaud Weksteen

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=20261123045730.1960346-2-tweek@google.com \
    --to=tweek@google.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=shuah@kernel.org \
    --cc=sidnayyar@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@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