BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token
@ 2026-11-26 13:20 Toke Høiland-Jørgensen
  2026-11-26 13:20 ` [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:20 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

This series helps seamlessly run an application inside a user namespace
with a bpf_token delegation. The need for these changes was discussed at
my talk at this yeas's LPC:

https://lpc.events/event/20/contributions/2441/

The problem the series aims to fix is one we discovered while developing
the bpf-rbacd application discussed in the talk: when delegating
permissions with a bpf_token, a libbpf-based tracing application works
seamlessly when it is attaching an fentry program to a function that is
in vmlinux, but fails if it tries to attach the same program to a
function that's defined in a module. There are two reasons for this:

(1) When trying to find the BTF ID to attach to in the module, libbpf
    uses the BPF_BTF_GET_NEXT_ID command for the bpf() syscall, which
    requires global CAP_SYS_ADMIN.

(2) Even with the issue above fixed, libbpf does not have the token
    available at the time it does the BTF iteration.

The first patch relaxes the kernel's permission check to make it
possible to iterate BTF objects with bpf_token permissions, fixing
issue (1) above.

Patches 2-5 changes libbpf's behaviour around accessing tokens so that
token loading is done on object creation instead of on load, and passes
in the token when iterating BTF objects in the kernel, fixing issue (2)
above.

Patch 6 updates the selftests to correspond to the new behaviour, and
patch 7 adds a new test that checks that we can attach fentry programs
to module functions inside a user namespace, which was the original
issue resolved by this series.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
Changes in v2:
- Pass the right struct name to OPTS_VALID in bpf_get_next_id_opts().
- Add the new symbols to the right libbpf version section.
- Reorder permission checks in load_module_btfs() and check for EPERM
  after bpf_btf_get_fd_by_id_opts().
- Assign empty string to the bpf_token_path instead of '\0' to disable
  the token.
- Rebase on newest bpf-next.
- Link to v1: https://patch.msgid.link/20261125-bpf-relax-get-next-id-v1-0-49175227c8cf@toke.dk

---
Toke Høiland-Jørgensen (7):
      bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token
      libbpf: Add bpf_*_get_next_id_opts() functions
      libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd
      libbpf: Move bpf_object_prepare_token() to object creation
      libbpf: Pass in token_fd when iterating kernel BTF objects
      selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test
      selftests/bpf: Add test for attaching fentry to a module with bpf_token

 include/uapi/linux/bpf.h                        |  5 ++-
 kernel/bpf/syscall.c                            | 33 ++++++++++++++----
 tools/include/uapi/linux/bpf.h                  |  5 ++-
 tools/lib/bpf/bpf.c                             | 45 +++++++++++++++++++++----
 tools/lib/bpf/bpf.h                             | 16 +++++++++
 tools/lib/bpf/libbpf.c                          | 20 +++++++++--
 tools/lib/bpf/libbpf.map                        |  4 +++
 tools/testing/selftests/bpf/prog_tests/token.c  | 45 +++++++++++++++++++++++--
 tools/testing/selftests/bpf/progs/priv_module.c | 15 +++++++++
 9 files changed, 167 insertions(+), 21 deletions(-)
---
base-commit: 486b5ce7184f7449ef2fe1e951455b0b8a114060
change-id: 20261006-bpf-relax-get-next-id-6d025833eee7


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
@ 2026-11-26 13:20 ` Toke Høiland-Jørgensen
  2026-11-26 13:53   ` bot+bpf-ci
  2026-11-26 13:21 ` [PATCH bpf-next v2 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:20 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

The BPF_*_GET_NEXT_ID commands are currently restricted to system-wide
CAP_SYS_ADMIN, but iterating the objects themselves is not actually an
administrator-only operation, so this seems overly restrictive.

In particular, being able to walk the BTF IDs is necessary for libbpf to
attach to functions in modules. So the CAP_SYS_ADMIN restriction
prevents some BPF applications from working with permissions delegated
by a bpf_token.

Relax the permission check in bpf_obj_get_next_id() to
token_capable(CAP_BPF). There is no change to the operations that turn
the IDs into file descriptors (BPF_*_GET_FD_BY_ID commands).

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 include/uapi/linux/bpf.h       |  5 ++++-
 kernel/bpf/syscall.c           | 33 ++++++++++++++++++++++++++-------
 tools/include/uapi/linux/bpf.h |  5 ++++-
 3 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 62de0919db72..cdc313f9ab14 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -1786,7 +1786,10 @@ union bpf_attr {
 		};
 		__u32		next_id;
 		__u32		open_flags;
-		__s32		fd_by_id_token_fd;
+		union {
+			__s32		fd_by_id_token_fd;
+			__s32		get_next_id_token_fd;
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index ec8e78792911..1440c10ea0cc 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4872,21 +4872,40 @@ static int bpf_prog_test_run(const union bpf_attr *attr,
 	return ret;
 }
 
-#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
+#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD get_next_id_token_fd
 
-static int bpf_obj_get_next_id(const union bpf_attr *attr,
+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)
 {
+	struct bpf_token *token = NULL;
 	u32 next_id = attr->start_id;
 	int err = 0;
 
 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	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_BPF)) {
+		bpf_token_put(token);
 		return -EPERM;
+	}
+
+	bpf_token_put(token);
 
 	next_id++;
 	spin_lock_bh(lock);
@@ -6523,15 +6542,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 +6605,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/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 62de0919db72..cdc313f9ab14 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1786,7 +1786,10 @@ union bpf_attr {
 		};
 		__u32		next_id;
 		__u32		open_flags;
-		__s32		fd_by_id_token_fd;
+		union {
+			__s32		fd_by_id_token_fd;
+			__s32		get_next_id_token_fd;
+		};
 	};
 
 	struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 2/7] libbpf: Add bpf_*_get_next_id_opts() functions
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
  2026-11-26 13:20 ` [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  2026-11-26 13:21 ` [PATCH bpf-next v2 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

Add opts-based variants of the bpf_*_get_next_id() functions which allow
passing in a token fd to add to the bpf() syscall.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/lib/bpf/bpf.c      | 43 +++++++++++++++++++++++++++++++++++++------
 tools/lib/bpf/bpf.h      | 16 ++++++++++++++++
 tools/lib/bpf/libbpf.map |  4 ++++
 3 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 38dcb31e8c0e..773efb585bca 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1149,14 +1149,21 @@ 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_next_id_opts *opts)
 {
-	const size_t attr_sz = offsetofend(union bpf_attr, open_flags);
+	const size_t attr_sz = offsetofend(union bpf_attr, get_next_id_token_fd);
 	union bpf_attr attr;
 	int err;
 
+	if (!OPTS_VALID(opts, bpf_get_next_id_opts))
+		return libbpf_err(-EINVAL);
+
 	memset(&attr, 0, attr_sz);
 	attr.start_id = start_id;
+	attr.get_next_id_token_fd = OPTS_GET(opts, token_fd, 0);
+	if (attr.get_next_id_token_fd)
+		attr.open_flags = BPF_F_TOKEN_FD;
 
 	err = sys_bpf(cmd, &attr, attr_sz);
 	if (!err)
@@ -1167,22 +1174,46 @@ 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_prog_get_next_id_opts(__u32 start_id, __u32 *next_id,
+			      const struct bpf_get_next_id_opts *opts)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_PROG_GET_NEXT_ID, opts);
 }
 
 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_map_get_next_id_opts(__u32 start_id, __u32 *next_id,
+			     const struct bpf_get_next_id_opts *opts)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_MAP_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_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID, NULL);
+}
+
+int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id,
+			      const struct bpf_get_next_id_opts *opts)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_BTF_GET_NEXT_ID, opts);
 }
 
 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_link_get_next_id_opts(__u32 start_id, __u32 *next_id,
+			      const struct bpf_get_next_id_opts *opts)
+{
+	return bpf_obj_get_next_id(start_id, next_id, BPF_LINK_GET_NEXT_ID, opts);
 }
 
 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 fe3b7fd8512b..cb5104734710 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -524,6 +524,22 @@ LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
 
+struct bpf_get_next_id_opts {
+	size_t sz; /* size of this struct for forward/backward compatibility */
+	__u32 token_fd;
+	size_t :0;
+};
+#define bpf_get_next_id_opts__last_field token_fd
+
+LIBBPF_API int bpf_prog_get_next_id_opts(__u32 start_id, __u32 *next_id,
+					 const struct bpf_get_next_id_opts *opts);
+LIBBPF_API int bpf_map_get_next_id_opts(__u32 start_id, __u32 *next_id,
+					 const struct bpf_get_next_id_opts *opts);
+LIBBPF_API int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id,
+					 const struct bpf_get_next_id_opts *opts);
+LIBBPF_API int bpf_link_get_next_id_opts(__u32 start_id, __u32 *next_id,
+					 const struct bpf_get_next_id_opts *opts);
+
 struct bpf_get_fd_by_id_opts {
 	size_t sz; /* size of this struct for forward/backward compatibility */
 	__u32 open_flags; /* permissions requested for the operation on fd */
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index a45666f546fa..4a87b3500741 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -476,6 +476,10 @@ LIBBPF_1.8.0 {
 
 LIBBPF_1.9.0 {
 	global:
+		bpf_btf_get_next_id_opts;
+		bpf_link_get_next_id_opts;
+		bpf_map_get_next_id_opts;
+		bpf_prog_get_next_id_opts;
 		bpf_program__load;
 		bpf_program__load_strategy;
 		bpf_program__set_load_strategy;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
  2026-11-26 13:20 ` [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
  2026-11-26 13:21 ` [PATCH bpf-next v2 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  2026-11-26 13:21 ` [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

The bpf_btf_get_fd_by_id_opts() function allows setting the token fd in
the opts, but leaves it to the caller to set the BPF_F_TOKEN_FD flag.
This is error-prone, and there's no reason we can't just set it on the
caller's behalf when an fd is supplied, so let's just do that.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/lib/bpf/bpf.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 773efb585bca..3d2dbf6d5fcf 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1276,6 +1276,8 @@ int bpf_btf_get_fd_by_id_opts(__u32 id,
 	attr.btf_id = id;
 	attr.open_flags = OPTS_GET(opts, open_flags, 0);
 	attr.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0);
+	if (attr.fd_by_id_token_fd)
+		attr.open_flags |= BPF_F_TOKEN_FD;
 
 	fd = sys_bpf_fd(BPF_BTF_GET_FD_BY_ID, &attr, attr_sz);
 	return libbpf_err_errno(fd);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
                   ` (2 preceding siblings ...)
  2026-11-26 13:21 ` [PATCH bpf-next v2 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  2026-11-26 13:38   ` sashiko-bot
  2026-11-26 13:21 ` [PATCH bpf-next v2 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

Move the automatic acquisition of a bpf_token from object prepare time
to object creation time. This ensures that the token is available for
any probing done before loading the object, in particular for BTF access
in bpf_program__set_attach_target().

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 540daf7752b4..6d0e71aff3e1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9281,6 +9281,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
 	err = err ? : bpf_object__init_maps(obj, opts);
 	err = err ? : bpf_object_init_progs(obj, opts);
 	err = err ? : bpf_object__collect_relos(obj);
+	err = err ? : bpf_object_prepare_token(obj);
 	if (err)
 		goto out;
 
@@ -9873,7 +9874,6 @@ static int bpf_object_prepare(struct bpf_object *obj, const char *target_btf_pat
 	}
 
 	err = bpf_object_check_gen_loader(obj);
-	err = err ? : bpf_object_prepare_token(obj);
 	err = err ? : bpf_object__probe_loading(obj);
 	err = err ? : bpf_object__load_vmlinux_btf(obj, false);
 	err = err ? : bpf_object__resolve_externs(obj, obj->kconfig);

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
                   ` (3 preceding siblings ...)
  2026-11-26 13:21 ` [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  2026-11-26 13:21 ` [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
  2026-11-26 13:21 ` [PATCH bpf-next v2 7/7] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen
  6 siblings, 0 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

When loading module BTF, libbpf will iterate through the available BTF
IDs in the kernel and try to load them. This fails inside a user
namespace because of missing permissions. The kernel now supports
passing in a bpf_token to BPF_BTF_GET_NEXT_ID, so amend the iteration to
pass in a token if it exists.

Passing in the token doesn't work on older kernels, so if we get an
EINVAL back when trying to iterate, we reset any tokens and try again.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/lib/bpf/libbpf.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 6d0e71aff3e1..6bfa5a9eb8ba 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6017,6 +6017,10 @@ static bool is_btf_mod_allowed(const struct bpf_object *obj, const char *name)
 
 static int load_module_btfs(struct bpf_object *obj)
 {
+	DECLARE_LIBBPF_OPTS(bpf_get_next_id_opts, next_opts,
+			    .token_fd = obj->token_fd);
+	DECLARE_LIBBPF_OPTS(bpf_get_fd_by_id_opts, fd_opts,
+			    .token_fd = obj->token_fd);
 	struct bpf_btf_info info;
 	struct module_btf *mod_btf;
 	struct btf *btf;
@@ -6041,7 +6045,13 @@ 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, &next_opts);
+		if (err && errno == EINVAL && next_opts.token_fd) {
+			pr_debug("retrying module BTF load without token fd\n");
+			next_opts.token_fd = 0;
+			fd_opts.token_fd = 0;
+			err = bpf_btf_get_next_id_opts(id, &id, &next_opts);
+		}
 		if (err && errno == ENOENT)
 			return 0;
 		if (err && errno == EPERM) {
@@ -6054,10 +6064,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, &fd_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;

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
                   ` (4 preceding siblings ...)
  2026-11-26 13:21 ` [PATCH bpf-next v2 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  2026-11-26 13:53   ` bot+bpf-ci
  2026-11-26 13:21 ` [PATCH bpf-next v2 7/7] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen
  6 siblings, 1 reply; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

Now that we changed the behaviour of libbpf to automatically load the
token fd on object creation, we can no longer rely on the absence of
bpf_object__prepare() preventing the token from being present. Instead,
explicitly disable the token by setting token_path to '\0' in the
token/obj_priv_freplace_prog_fail test.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/testing/selftests/bpf/prog_tests/token.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
index f2f5d36ae00a..17cd7f176e09 100644
--- a/tools/testing/selftests/bpf/prog_tests/token.c
+++ b/tools/testing/selftests/bpf/prog_tests/token.c
@@ -797,7 +797,7 @@ static int userns_obj_priv_prog(int mnt_fd, struct token_lsm *lsm_skel)
 }
 
 static int userns_obj_priv_freplace_setup(int mnt_fd, struct priv_freplace_prog **fr_skel,
-					  struct priv_prog **skel, int *tgt_fd)
+					  struct priv_prog **skel, int *tgt_fd, bool fr_token)
 {
 	LIBBPF_OPTS(bpf_object_open_opts, opts);
 	int err;
@@ -813,6 +813,8 @@ static int userns_obj_priv_freplace_setup(int mnt_fd, struct priv_freplace_prog
 	if (!ASSERT_OK(err, "priv_prog__load"))
 		return -EINVAL;
 
+	if (!fr_token)
+		opts.bpf_token_path = "";
 	*fr_skel = priv_freplace_prog__open_opts(&opts);
 	if (!ASSERT_OK_PTR(*skel, "priv_freplace_prog__open_opts"))
 		return -EINVAL;
@@ -830,7 +832,7 @@ static int userns_obj_priv_freplace_prog(int mnt_fd, struct token_lsm *lsm_skel)
 	struct priv_prog *skel = NULL;
 	int err, tgt_fd;
 
-	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd);
+	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd, true);
 	if (!ASSERT_OK(err, "setup"))
 		goto out;
 
@@ -858,7 +860,7 @@ static int userns_obj_priv_freplace_prog_fail(int mnt_fd, struct token_lsm *lsm_
 	struct priv_prog *skel = NULL;
 	int err, tgt_fd;
 
-	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd);
+	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd, false);
 	if (!ASSERT_OK(err, "setup"))
 		goto out;
 

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH bpf-next v2 7/7] selftests/bpf: Add test for attaching fentry to a module with bpf_token
  2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
                   ` (5 preceding siblings ...)
  2026-11-26 13:21 ` [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
@ 2026-11-26 13:21 ` Toke Høiland-Jørgensen
  6 siblings, 0 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:21 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, Martin KaFai Lau,
	Song Liu, Yonghong Song, Jiri Olsa, Emil Tsalapatis,
	Ihor Solodrai, John Fastabend
  Cc: bpf, Toke Høiland-Jørgensen

Add a new subtest to the token prog_test which attaches an fentry
program to a module function. This requires libbpf to read the module
BTF information using a bpf_token, which is only possible with the
fixes in the previous commits.

Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
 tools/testing/selftests/bpf/prog_tests/token.c  | 37 +++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/priv_module.c | 15 ++++++++++
 2 files changed, 52 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
index 17cd7f176e09..c46574839563 100644
--- a/tools/testing/selftests/bpf/prog_tests/token.c
+++ b/tools/testing/selftests/bpf/prog_tests/token.c
@@ -26,6 +26,7 @@
 #include "token_kallsyms.skel.h"
 #include "token_lsm.skel.h"
 #include "priv_freplace_prog.skel.h"
+#include "priv_module.skel.h"
 
 static inline int sys_mount(const char *dev_name, const char *dir_name,
 			    const char *type, unsigned long flags,
@@ -1105,6 +1106,34 @@ static int userns_obj_priv_prog_kallsyms(int mnt_fd, struct token_lsm *lsm_skel)
 	return err;
 }
 
+static int userns_obj_priv_fentry_module(int mnt_fd, struct token_lsm *lsm_skel)
+{
+	LIBBPF_OPTS(bpf_object_open_opts, opts);
+	struct priv_module *skel = NULL;
+	char buf[256];
+	int err;
+
+	/* use bpf_token_path to provide BPF FS path */
+	snprintf(buf, sizeof(buf), "/proc/self/fd/%d", mnt_fd);
+	opts.bpf_token_path = buf;
+	skel = priv_module__open_opts(&opts);
+	if (!ASSERT_OK_PTR(skel, "priv_module__open_opts"))
+		return -EINVAL;
+
+	err = priv_module__load(skel);
+	if (!ASSERT_OK(err, "priv_module__load"))
+		goto out;
+
+	err = priv_module__attach(skel);
+	if (!ASSERT_OK(err, "priv_module__attach"))
+		goto out;
+
+out:
+	priv_module__destroy(skel);
+	return err;
+}
+
+
 #define bit(n) (1ULL << (n))
 
 static int userns_bpf_token_info(int mnt_fd, struct token_lsm *lsm_skel)
@@ -1201,6 +1230,14 @@ void serial_test_token(void)
 		};
 		subtest_userns(&opts, userns_obj_priv_freplace_prog_fail);
 	}
+	if (test__start_subtest("obj_priv_fentry_module")) {
+		struct bpffs_opts opts = {
+			.cmds = bit(BPF_BTF_LOAD) | bit(BPF_PROG_LOAD) | bit(BPF_BTF_GET_FD_BY_ID) | bit(BPF_BTF_GET_NEXT_ID),
+			.progs = bit(BPF_PROG_TYPE_TRACING),
+			.attachs = ~0ULL,
+		};
+		subtest_userns(&opts, userns_obj_priv_fentry_module);
+	}
 	if (test__start_subtest("obj_priv_btf_fail")) {
 		struct bpffs_opts opts = {
 			/* disallow BTF loading */
diff --git a/tools/testing/selftests/bpf/progs/priv_module.c b/tools/testing/selftests/bpf/progs/priv_module.c
new file mode 100644
index 000000000000..3743ceb43c42
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/priv_module.c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+#include "../test_kmods/bpf_testmod.h"
+
+SEC("fentry/bpf_testmod_test_read")
+int BPF_PROG(handle_fentry)
+{
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";

-- 
2.56.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation
  2026-11-26 13:21 ` [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
@ 2026-11-26 13:38   ` sashiko-bot
  2026-11-26 13:52     ` Toke Høiland-Jørgensen
  0 siblings, 1 reply; 12+ messages in thread
From: sashiko-bot @ 2026-11-26 13:38 UTC (permalink / raw)
  To: Toke Høiland-Jørgensen; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] libbpf: bpf_object_open() runs bpf_object_prepare_token()
  before gen_loader
--

commit c11c45202857082e826eb4bc3551463d49a2d5fa
Author: Toke Høiland-Jørgensen <toke@toke.dk>

libbpf: Move bpf_object_prepare_token() to object creation

This commit moves bpf_object_prepare_token() from bpf_object_prepare() to
bpf_object_open() so that the BPF token is available before loading the
object, such as during BTF access in bpf_program__set_attach_target().

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 540daf7752b45..6d0e71aff3e14 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9281,6 +9281,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
>  	err = err ? : bpf_object__init_maps(obj, opts);
>  	err = err ? : bpf_object_init_progs(obj, opts);
>  	err = err ? : bpf_object__collect_relos(obj);
> +	err = err ? : bpf_object_prepare_token(obj);
>  	if (err)
>  		goto out;
>  

[Severity: Medium]
Does calling bpf_object_prepare_token() during bpf_object_open() bypass the
obj->gen_loader check added in d92defe04ad5 ("libbpf: Skip BPF token
preparation when generating loader")?

obj->gen_loader is only set when the caller invokes bpf_object__gen_loader()
on the already-opened object, so it is always NULL inside bpf_object_open():

tools/lib/bpf/libbpf.c:bpf_object_prepare_token() {
	...
	if (obj->gen_loader)
		return 0;
	...
}

If the host has a token-delegated bpffs mounted at /sys/fs/bpf (or
LIBBPF_BPF_TOKEN_PATH set), bpf_object_open() populates obj->token_fd and
bpf_object__gen_loader() does not clear it.  Subsequent calls to
bpf_object__create_map() and bpf_object_load_prog() then set BPF_F_TOKEN_FD
in the generated loader program's map_flags and prog_flags without a
runtime token_fd, causing the generated loader to fail at runtime.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-bpf-relax-get-next-id-v2-0-039e92757f9c@toke.dk?part=4

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation
  2026-11-26 13:38   ` sashiko-bot
@ 2026-11-26 13:52     ` Toke Høiland-Jørgensen
  0 siblings, 0 replies; 12+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 13:52 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: bpf

sashiko-bot@kernel.org writes:

> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] libbpf: bpf_object_open() runs bpf_object_prepare_token()
>   before gen_loader
> --
>
> commit c11c45202857082e826eb4bc3551463d49a2d5fa
> Author: Toke Høiland-Jørgensen <toke@toke.dk>
>
> libbpf: Move bpf_object_prepare_token() to object creation
>
> This commit moves bpf_object_prepare_token() from bpf_object_prepare() to
> bpf_object_open() so that the BPF token is available before loading the
> object, such as during BTF access in bpf_program__set_attach_target().
>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 540daf7752b45..6d0e71aff3e14 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -9281,6 +9281,7 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
>>  	err = err ? : bpf_object__init_maps(obj, opts);
>>  	err = err ? : bpf_object_init_progs(obj, opts);
>>  	err = err ? : bpf_object__collect_relos(obj);
>> +	err = err ? : bpf_object_prepare_token(obj);
>>  	if (err)
>>  		goto out;
>>  
>
> [Severity: Medium]
> Does calling bpf_object_prepare_token() during bpf_object_open() bypass the
> obj->gen_loader check added in d92defe04ad5 ("libbpf: Skip BPF token
> preparation when generating loader")?
>
> obj->gen_loader is only set when the caller invokes bpf_object__gen_loader()
> on the already-opened object, so it is always NULL inside bpf_object_open():
>
> tools/lib/bpf/libbpf.c:bpf_object_prepare_token() {
> 	...
> 	if (obj->gen_loader)
> 		return 0;
> 	...
> }
>
> If the host has a token-delegated bpffs mounted at /sys/fs/bpf (or
> LIBBPF_BPF_TOKEN_PATH set), bpf_object_open() populates obj->token_fd and
> bpf_object__gen_loader() does not clear it.  Subsequent calls to
> bpf_object__create_map() and bpf_object_load_prog() then set BPF_F_TOKEN_FD
> in the generated loader program's map_flags and prog_flags without a
> runtime token_fd, causing the generated loader to fail at runtime.

Ah, I had not noticed that gen_loader was set in a separate call. I'll
add clearing of the token_fd to bpf_object__gen_loader() and respin.

-Toke

pw-bot: cr

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test
  2026-11-26 13:21 ` [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
@ 2026-11-26 13:53   ` bot+bpf-ci
  0 siblings, 0 replies; 12+ messages in thread
From: bot+bpf-ci @ 2026-11-26 13:53 UTC (permalink / raw)
  To: toke, ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, john.fastabend
  Cc: bpf, toke, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 2045 bytes --]

> selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test

TL;DR: Test issue in token.c: a stale comment, and the positive freplace test
may no longer cover getting the token at open time, since the failing test now
runs with the token disabled.

> diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
> --- a/tools/testing/selftests/bpf/prog_tests/token.c
> +++ b/tools/testing/selftests/bpf/prog_tests/token.c

[ ... ]

> @@ -830,7 +832,7 @@ static int userns_obj_priv_freplace_prog(int mnt_fd, struct token_lsm *lsm_skel)
>  	struct priv_prog *skel = NULL;
>  	int err, tgt_fd;
>  
> -	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd);
> +	err = userns_obj_priv_freplace_setup(mnt_fd, &fr_skel, &skel, &tgt_fd, true);
>  	if (!ASSERT_OK(err, "setup"))
>  		goto out;

This isn't a bug, but the comment above userns_obj_priv_freplace_prog() still
says:

  /* Verify that freplace works from user namespace, because bpf token is loaded
   * in bpf_object__prepare
   */

After the libbpf change in "libbpf: Move bpf_object_prepare_token() to object
creation", bpf_object_prepare_token() is only called from bpf_object_open(),
so the token is already present when priv_freplace_prog__open_opts() returns.
Is that comment still accurate?

The positive test also still calls bpf_object__prepare(fr_skel->obj) before
bpf_program__set_attach_target().  Previously
userns_obj_priv_freplace_prog_fail covered calling set_attach_target() with a
token on an object that is only opened, and that now runs with the token
disabled.

Could the comment be updated, and could the bpf_object__prepare() call be
dropped (or a variant without it be added), so that a test still covers
getting the token at open time?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45831277005

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token
  2026-11-26 13:20 ` [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
@ 2026-11-26 13:53   ` bot+bpf-ci
  0 siblings, 0 replies; 12+ messages in thread
From: bot+bpf-ci @ 2026-11-26 13:53 UTC (permalink / raw)
  To: toke, ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
	yonghong.song, jolsa, emil, ihor.solodrai, john.fastabend
  Cc: bpf, toke, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 1380 bytes --]

> bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token

TL;DR: Documentation nit only: the CAP_BPF comment in capability.h may now be
stale, since ID iteration needs only CAP_BPF while ID-to-fd conversion still
needs CAP_SYS_ADMIN.

> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c

[ ... ]

> +	if (!bpf_token_capable(token, CAP_BPF)) {
> +		bpf_token_put(token);
>  		return -EPERM;
> +	}
> +
> +	bpf_token_put(token);

Should include/uapi/linux/capability.h be updated along with this change?
The CAP_BPF description there still says:

 * CAP_SYS_ADMIN is required to iterate system wide loaded
 * programs, maps, links, BTFs and convert their IDs to file descriptors.

After this patch, iterating the IDs needs only CAP_BPF (or a token that
delegates the command), while bpf_prog_get_fd_by_id(),
bpf_map_get_fd_by_id() and bpf_link_get_fd_by_id() still call
capable(CAP_SYS_ADMIN).  Could that sentence be split so it says
iteration needs CAP_BPF and the ID to fd conversion needs CAP_SYS_ADMIN?

No later patch in the series touches capability.h.


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45831277005

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-26 13:53 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 13:20 [PATCH bpf-next v2 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-26 13:20 ` [PATCH bpf-next v2 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
2026-11-26 13:53   ` bot+bpf-ci
2026-11-26 13:21 ` [PATCH bpf-next v2 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
2026-11-26 13:21 ` [PATCH bpf-next v2 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
2026-11-26 13:21 ` [PATCH bpf-next v2 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
2026-11-26 13:38   ` sashiko-bot
2026-11-26 13:52     ` Toke Høiland-Jørgensen
2026-11-26 13:21 ` [PATCH bpf-next v2 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
2026-11-26 13:21 ` [PATCH bpf-next v2 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
2026-11-26 13:53   ` bot+bpf-ci
2026-11-26 13:21 ` [PATCH bpf-next v2 7/7] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox