BPF List
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Toke Høiland-Jørgensen" <toke@toke.dk>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Eduard Zingerman" <eddyz87@gmail.com>,
	"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
	"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>,
	"John Fastabend" <john.fastabend@gmail.com>
Cc: <bpf@vger.kernel.org>
Subject: Re: [PATCH bpf-next v3 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token
Date: Sat, 28 Nov 2026 02:59:43 +0000	[thread overview]
Message-ID: <DN6IAUGCNHGW.1C7HQ1O3S82Q8@gmail.com> (raw)
In-Reply-To: <20261126-bpf-relax-get-next-id-v3-1-338e1d3077e7@toke.dk>

On Thu, Nov 26, 2026 at 06:51 AM Toke Høiland-Jørgensen <toke@toke.dk> wrote:
> Relax the permission check in bpf_obj_get_next_id() to
> token_capable(CAP_BPF), without touching the operations that turn the
> IDs into file descriptors (BPF_*_GET_FD_BY_ID commands). Update the
> capability documentation accordingly.

My bot thinks the following is a better path forward
and I have to agree. wdyt?

This will break libbpf users that have CAP_BPF and no CAP_SYS_ADMIN.
load_module_btfs() relies on EPERM from BPF_BTF_GET_NEXT_ID. See
commit 2d2c95162de8 ("libbpf: Skip modules BTF loading when
CAP_SYS_ADMIN is missing").
libbpf calls it when vmlinux BTF doesn't have a CO-RE type or a ksym.
A missing __weak kfunc is enough.

  step                        today        this patch
  --------------------------  -----------  -----------
  bpf_btf_get_next_id()       EPERM        0
  bpf_btf_get_fd_by_id()      not called   EPERM
  load_module_btfs() returns  0            -EPERM
  bpf_object__load()          ok           fails

The EPERM check in patch 5 helps the new libbpf only.
Released libbpf loads such objects today and fails with this patch.

libbpf needs the FD of the module BTF. The ID is not enough.
BPF_BTF_GET_FD_BY_ID checks bpf_token_capable(token, CAP_SYS_ADMIN).
With a token that is CAP_SYS_ADMIN in the userns of the token.
The prog, map and link variants check capable(CAP_SYS_ADMIN).
They don't take a token.
So CAP_BPF for the walk doesn't help to get the FD.

Lets do the check of BPF_BTF_GET_FD_BY_ID in both commands.
open_flags and fd_by_id_token_fd are in the struct of BPF_*_GET_*_ID
already.

  what                     this series            diff below
  -----------------------  ---------------------  -----------------------
  check without a token    CAP_BPF                CAP_SYS_ADMIN, as today
  uapi/linux/bpf.h         union, second name     no change
  uapi/linux/capability.h  changed                no change
  token code in syscall.c  one more copy          one helper, two users
  new libbpf APIs          4, one has a user      1
  new opts struct          bpf_get_next_id_opts   none
  BPF_F_TOKEN_FD           patch 3 sets it        the caller sets it
  EINVAL from old kernel   retry without token    skip, like EPERM

bpf_token_create() refuses init_user_ns. So the retry without the token
always returns EPERM.

This is instead of patches 1, 2, 3 and 5. The EPERM check of patch 5 is
in the diff. Patch 7 stays as it is.
The token selftests pass with it, and so does the case of patch 7:

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e787..4c3a413 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4872,21 +4872,49 @@ static int bpf_prog_test_run(const union bpf_attr *attr,
 	return ret;
 }

-#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
+/* The check of BPF_*_GET_NEXT_ID and BPF_BTF_GET_FD_BY_ID */
+static int bpf_obj_id_capable(const union bpf_attr *attr, enum bpf_cmd cmd)
+{
+	struct bpf_token *token = NULL;
+	int err = 0;
+
+	if (attr->open_flags & ~BPF_F_TOKEN_FD)
+		return -EINVAL;
+
+	if (attr->open_flags & BPF_F_TOKEN_FD) {
+		token = bpf_token_get_from_fd(attr->fd_by_id_token_fd);
+		if (IS_ERR(token))
+			return PTR_ERR(token);
+		if (!bpf_token_allow_cmd(token, cmd)) {
+			bpf_token_put(token);
+			token = NULL;
+		}
+	}
+
+	if (!bpf_token_capable(token, CAP_SYS_ADMIN))
+		err = -EPERM;
+
+	bpf_token_put(token);
+	return err;
+}

-static int bpf_obj_get_next_id(const union bpf_attr *attr,
+#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD fd_by_id_token_fd
+
+static int bpf_obj_get_next_id(enum bpf_cmd cmd,
+			       const union bpf_attr *attr,
 			       union bpf_attr __user *uattr,
 			       struct idr *idr,
 			       spinlock_t *lock)
 {
 	u32 next_id = attr->start_id;
-	int err = 0;
+	int err;

 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
 		return -EINVAL;

-	if (!capable(CAP_SYS_ADMIN))
-		return -EPERM;
+	err = bpf_obj_id_capable(attr, cmd);
+	if (err)
+		return err;

 	next_id++;
 	spin_lock_bh(lock);
@@ -5681,32 +5709,11 @@ static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_l

 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
 {
-	struct bpf_token *token = NULL;
-
 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
 		return -EINVAL;

-	if (attr->open_flags & ~BPF_F_TOKEN_FD)
-		return -EINVAL;
-
-	if (attr->open_flags & BPF_F_TOKEN_FD) {
-		token = bpf_token_get_from_fd(attr->fd_by_id_token_fd);
-		if (IS_ERR(token))
-			return PTR_ERR(token);
-		if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
-			bpf_token_put(token);
-			token = NULL;
-		}
-	}
-
-	if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
-		bpf_token_put(token);
-		return -EPERM;
-	}
-
-	bpf_token_put(token);
-
-	return btf_get_fd_by_id(attr->btf_id);
+	return bpf_obj_id_capable(attr, BPF_BTF_GET_FD_BY_ID) ?:
+	       btf_get_fd_by_id(attr->btf_id);
 }

 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
@@ -6523,15 +6530,15 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
 		err = bpf_prog_test_run(&attr, uattr.user);
 		break;
 	case BPF_PROG_GET_NEXT_ID:
-		err = bpf_obj_get_next_id(&attr, uattr.user,
+		err = bpf_obj_get_next_id(cmd, &attr, uattr.user,
 					  &prog_idr, &prog_idr_lock);
 		break;
 	case BPF_MAP_GET_NEXT_ID:
-		err = bpf_obj_get_next_id(&attr, uattr.user,
+		err = bpf_obj_get_next_id(cmd, &attr, uattr.user,
 					  &map_idr, &map_idr_lock);
 		break;
 	case BPF_BTF_GET_NEXT_ID:
-		err = bpf_obj_get_next_id(&attr, uattr.user,
+		err = bpf_obj_get_next_id(cmd, &attr, uattr.user,
 					  &btf_idr, &btf_idr_lock);
 		break;
 	case BPF_PROG_GET_FD_BY_ID:
@@ -6586,7 +6593,7 @@ static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size,
 		err = bpf_link_get_fd_by_id(&attr);
 		break;
 	case BPF_LINK_GET_NEXT_ID:
-		err = bpf_obj_get_next_id(&attr, uattr.user,
+		err = bpf_obj_get_next_id(cmd, &attr, uattr.user,
 					  &link_idr, &link_idr_lock);
 		break;
 	case BPF_ENABLE_STATS:
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 38dcb31..1adfd53 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1149,14 +1149,20 @@ int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
 	return libbpf_err_errno(ret);
 }

-static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
+static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd,
+			       const struct bpf_get_fd_by_id_opts *opts)
 {
-	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
+	const size_t attr_sz = offsetofend(union bpf_attr, fd_by_id_token_fd);
 	union bpf_attr attr;
 	int err;

+	if (!OPTS_VALID(opts, bpf_get_fd_by_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.start_id = start_id;
+	attr.open_flags = OPTS_GET(opts, open_flags, 0);
+	attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0);

 	err = sys_bpf(cmd, &attr, attr_sz);
 	if (!err)
@@ -1167,22 +1173,28 @@ static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)

 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
 {
-	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID);
+	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID, NULL);
 }

 int bpf_map_get_next_id(__u32 start_id, __u32 *next_id)
 {
-	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID);
+	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_GET_NEXT_ID, NULL);
+}
+
+int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id,
+			     const struct bpf_get_fd_by_id_opts *opts)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID, opts);
 }

 int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id)
 {
-	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID);
+	return bpf_btf_get_next_id_opts(start_id, next_id, NULL);
 }

 int bpf_link_get_next_id(__u32 start_id, __u32 *next_id)
 {
-	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID);
+	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID, NULL);
 }

 int bpf_prog_get_fd_by_id_opts(__u32 id,
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index fe3b7fd..b1f0882 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -532,6 +532,8 @@ struct bpf_get_fd_by_id_opts {
 };
 #define bpf_get_fd_by_id_opts__last_field token_fd

+LIBBPF_API int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id,
+				const struct bpf_get_fd_by_id_opts *opts);
 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
 LIBBPF_API int bpf_prog_get_fd_by_id_opts(__u32 id,
 				const struct bpf_get_fd_by_id_opts *opts);
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 540daf7..b52c143 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6017,6 +6017,9 @@ static bool is_btf_mod_allowed(const struct bpf_object *obj, const char *name)

 static int load_module_btfs(struct bpf_object *obj)
 {
+	LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts,
+		    .open_flags = obj->token_fd ? BPF_F_TOKEN_FD : 0,
+		    .token_fd = obj->token_fd);
 	struct bpf_btf_info info;
 	struct module_btf *mod_btf;
 	struct btf *btf;
@@ -6041,10 +6044,11 @@ static int load_module_btfs(struct bpf_object *obj)
 		return 0;

 	while (true) {
-		err = bpf_btf_get_next_id(id, &id);
+		err = bpf_btf_get_next_id_opts(id, &id, &opts);
 		if (err && errno == ENOENT)
 			return 0;
-		if (err && errno == EPERM) {
+		/* EINVAL: the kernel is too old to take a token here */
+		if (err && (errno == EPERM || (errno == EINVAL && obj->token_fd))) {
 			pr_debug("skipping module BTFs loading, missing privileges\n");
 			return 0;
 		}
@@ -6054,10 +6058,14 @@ static int load_module_btfs(struct bpf_object *obj)
 			return err;
 		}

-		fd = bpf_btf_get_fd_by_id(id);
+		fd = bpf_btf_get_fd_by_id_opts(id, &opts);
 		if (fd < 0) {
 			if (errno == ENOENT)
 				continue; /* expected race: BTF was unloaded */
+			if (errno == EPERM) {
+				pr_debug("skipping module BTFs loading, missing privileges\n");
+				return 0;
+			}
 			err = -errno;
 			pr_warn("failed to get BTF object #%u FD: %s\n", id, errstr(err));
 			return err;
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index a45666f..30f199c 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -476,6 +476,7 @@ LIBBPF_1.8.0 {

 LIBBPF_1.9.0 {
 	global:
+		bpf_btf_get_next_id_opts;
 		bpf_program__load;
 		bpf_program__load_strategy;
 		bpf_program__set_load_strategy;

pw-bot: cr

  parent reply	other threads:[~2026-11-28  2:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 14:51 [PATCH bpf-next v3 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
2026-11-26 15:19   ` bot+bpf-ci
2026-11-28  2:59   ` Alexei Starovoitov [this message]
2026-11-26 14:51 ` [PATCH bpf-next v3 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
2026-11-26 15:11   ` sashiko-bot
2026-11-26 19:44     ` Toke Høiland-Jørgensen
2026-11-26 15:47   ` bot+bpf-ci
2026-11-26 19:46     ` Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 7/7] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen

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=DN6IAUGCNHGW.1C7HQ1O3S82Q8@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@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=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=toke@toke.dk \
    --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