All of lore.kernel.org
 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 2/2] selftests/bpf: Verify gen_loader skips BPF token preparation
Date: Mon, 23 Nov 2026 15:57:30 +1100	[thread overview]
Message-ID: <20261123045730.1960346-3-tweek@google.com> (raw)
In-Reply-To: <20261123045730.1960346-1-tweek@google.com>

Add a token/obj_gen_loader_no_token subtest that mounts a delegated
bpffs over /sys/fs/bpf in an isolated mount namespace and verifies that
bpf_object__gen_loader() does not acquire a BPF token when:
 - /sys/fs/bpf has implicit token delegation enabled,
 - opts.bpf_token_path points to a delegated bpffs, or
 - LIBBPF_BPF_TOKEN_PATH points to a nonexistent path.

Signed-off-by: Thiébaud Weksteen <tweek@google.com>
---
 .../testing/selftests/bpf/prog_tests/token.c  | 82 +++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
index f2f5d36ae00a..2aeb814ea376 100644
--- a/tools/testing/selftests/bpf/prog_tests/token.c
+++ b/tools/testing/selftests/bpf/prog_tests/token.c
@@ -1051,6 +1051,79 @@ static int userns_obj_priv_implicit_token_envvar(int mnt_fd, struct token_lsm *l
 	return -EINVAL;
 }
 
+static int verify_gen_loader_no_token(const struct bpf_object_open_opts *opts)
+{
+	LIBBPF_OPTS(gen_loader_opts, gen_opts);
+	struct priv_prog *skel;
+	int err;
+
+	skel = priv_prog__open_opts(opts);
+	if (!ASSERT_OK_PTR(skel, "gen_loader_open"))
+		return -EINVAL;
+
+	err = bpf_object__gen_loader(skel->obj, &gen_opts);
+	if (!ASSERT_OK(err, "gen_loader_init"))
+		goto out;
+
+	err = bpf_object__load(skel->obj);
+	if (!ASSERT_OK(err, "gen_loader_load"))
+		goto out;
+
+	if (!ASSERT_EQ(bpf_object__token_fd(skel->obj), -1, "gen_loader_token_fd") ||
+	    !ASSERT_OK_PTR(gen_opts.insns, "gen_loader_insns") ||
+	    !ASSERT_GT(gen_opts.insns_sz, 0, "gen_loader_insns_sz") ||
+	    !ASSERT_OK_PTR(gen_opts.data, "gen_loader_data") ||
+	    !ASSERT_GT(gen_opts.data_sz, 0, "gen_loader_data_sz"))
+		err = -EINVAL;
+
+out:
+	priv_prog__destroy(skel);
+	return err;
+}
+
+static int userns_obj_gen_loader_no_token(int mnt_fd, struct token_lsm *lsm_skel)
+{
+	LIBBPF_OPTS(bpf_object_open_opts, opts);
+	struct priv_prog *skel;
+	char buf[256];
+	int err;
+
+	/* Mount delegated BPF FS over /sys/fs/bpf and confirm normal load uses it. */
+	err = sys_move_mount(mnt_fd, "", AT_FDCWD, "/sys/fs/bpf", MOVE_MOUNT_F_EMPTY_PATH);
+	if (!ASSERT_OK(err, "move_mount_bpffs"))
+		return -EINVAL;
+
+	skel = priv_prog__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "normal_implicit_token_load"))
+		return -EINVAL;
+	if (!ASSERT_OK_FD(bpf_object__token_fd(skel->obj), "normal_load_token_fd")) {
+		priv_prog__destroy(skel);
+		return -EINVAL;
+	}
+	priv_prog__destroy(skel);
+
+	/* Implicit /sys/fs/bpf token must not be created during gen_loader. */
+	if (verify_gen_loader_no_token(NULL))
+		return -EINVAL;
+
+	/* Explicit bpf_token_path must also be ignored during gen_loader. */
+	snprintf(buf, sizeof(buf), "/proc/self/fd/%d", mnt_fd);
+	opts.bpf_token_path = buf;
+	if (verify_gen_loader_no_token(&opts))
+		return -EINVAL;
+
+	/* Invalid LIBBPF_BPF_TOKEN_PATH must not fail offline gen_loader. */
+	err = setenv(TOKEN_ENVVAR, "/nonexistent/bpffs", 1 /*overwrite*/);
+	if (!ASSERT_OK(err, "setenv_invalid_token_path"))
+		return -EINVAL;
+	err = verify_gen_loader_no_token(NULL);
+	unsetenv(TOKEN_ENVVAR);
+	if (err)
+		return -EINVAL;
+
+	return 0;
+}
+
 static bool kallsyms_has_bpf_func(struct ksyms *ksyms, const char *func_name)
 {
 	char name[256];
@@ -1243,6 +1316,15 @@ void serial_test_token(void)
 
 		subtest_userns(&opts, userns_obj_priv_implicit_token_envvar);
 	}
+	if (test__start_subtest("obj_gen_loader_no_token")) {
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD),
+			.progs = bit(BPF_PROG_TYPE_XDP),
+			.attachs = ~0ULL,
+		};
+
+		subtest_userns(&opts, userns_obj_gen_loader_no_token);
+	}
 	if (test__start_subtest("bpf_token_info")) {
 		struct bpffs_opts opts = {
 			.cmds = bit(BPF_MAP_CREATE),
-- 
2.56.1.750.gf70dc682cf-goog


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

Thread overview: 4+ 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 ` [PATCH bpf 1/2] libbpf: Skip BPF token preparation when generating loader Thiébaud Weksteen
2026-11-23  4:57 ` Thiébaud Weksteen [this message]
2026-11-25 21:00 ` [PATCH bpf 0/2] libbpf: Skip BPF token preparation for gen_loader patchwork-bot+netdevbpf

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-3-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 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.