* [PATCH bpf-next v4 1/6] bpf: Allow BPF_*_GET_NEXT_ID permission checks to pass with bpf_token
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
@ 2026-11-30 11:26 ` Toke Høiland-Jørgensen
2026-11-30 11:26 ` [PATCH bpf-next v4 2/6] libbpf: Add bpf_btf_get_next_id_opts() function Toke Høiland-Jørgensen
` (4 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:26 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.
Change the permission check in bpf_obj_get_next_id() to
token_capable(CAP_SYS_ADMIN) instead. This is identical to what we
already do for turning a BTF ID into an FD (BPF_BTF_GET_FD_BY_ID
command), so we can reuse the check from that command, moving it to a
new helper. We also reuse the bpf_attr struct member for the token fd.
The other operations that turn the other ID types into file
descriptors (BPF_{PROG,MAP,LINK}_GET_FD_BY_ID commands) are unchanged
and still require global CAP_SYS_ADMIN.
Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
kernel/bpf/syscall.c | 72 +++++++++++++++++++++++++++++-----------------------
1 file changed, 40 insertions(+), 32 deletions(-)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 198438b95849..51c88a3eb20f 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -4903,21 +4903,50 @@ static int bpf_prog_test_run(const union bpf_attr *attr,
return ret;
}
-#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
+static int bpf_obj_id_capable(const union bpf_attr *attr, enum bpf_cmd cmd)
+{
+ struct bpf_token *token = NULL;
+ int err = 0;
-static int bpf_obj_get_next_id(const union bpf_attr *attr,
+ 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)) {
+ bpf_token_put(token);
+ return -EPERM;
+ }
+
+ bpf_token_put(token);
+ return err;
+}
+
+#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);
@@ -5713,32 +5742,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,
@@ -6558,15 +6566,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:
@@ -6621,7 +6629,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:
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH bpf-next v4 2/6] libbpf: Add bpf_btf_get_next_id_opts() function
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-30 11:26 ` [PATCH bpf-next v4 1/6] bpf: Allow BPF_*_GET_NEXT_ID permission checks to pass " Toke Høiland-Jørgensen
@ 2026-11-30 11:26 ` Toke Høiland-Jørgensen
2026-11-30 11:27 ` [PATCH bpf-next v4 3/6] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
` (3 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:26 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_btf_get_next_id() function which allow
passing in a token fd to add to the bpf() syscall. We reuse the
bpf_get_fd_by_id_opts for this to mirror the kernel API.
Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
tools/lib/bpf/bpf.c | 24 ++++++++++++++++++------
tools/lib/bpf/bpf.h | 3 +++
tools/lib/bpf/libbpf.map | 1 +
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index ad6b356cc48c..6e5329a5975c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1160,14 +1160,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.fd_by_id_token_fd = OPTS_GET(opts, token_fd, 0);
+ attr.open_flags = OPTS_GET(opts, open_flags, 0);
err = sys_bpf(cmd, &attr, attr_sz);
if (!err)
@@ -1178,22 +1184,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(__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_fd_by_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_prog_get_fd_by_id_opts(__u32 id,
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 2ac143fd1102..1f648872eb10 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -554,6 +554,9 @@ LIBBPF_API int bpf_link_get_fd_by_id_opts(__u32 id,
const struct bpf_get_fd_by_id_opts *opts);
LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
+LIBBPF_API int bpf_btf_get_next_id_opts(__u32 start_id, __u32 *next_id,
+ const struct bpf_get_fd_by_id_opts *opts);
+
/**
* @brief **bpf_prog_get_info_by_fd()** obtains information about the BPF
* program corresponding to *prog_fd*.
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index a45666f546fa..30f199c9d14e 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;
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH bpf-next v4 3/6] libbpf: Move bpf_object_prepare_token() to object creation
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-30 11:26 ` [PATCH bpf-next v4 1/6] bpf: Allow BPF_*_GET_NEXT_ID permission checks to pass " Toke Høiland-Jørgensen
2026-11-30 11:26 ` [PATCH bpf-next v4 2/6] libbpf: Add bpf_btf_get_next_id_opts() function Toke Høiland-Jørgensen
@ 2026-11-30 11:27 ` Toke Høiland-Jørgensen
2026-11-30 11:42 ` sashiko-bot
2026-11-30 11:27 ` [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:27 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().
To avoid the move re-instating the token fd into a generated loader,
clear the token fd in bpf_object__gen_loader().
Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
tools/lib/bpf/libbpf.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 61a388868373..2aa3d3e33cc2 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9284,6 +9284,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;
@@ -9876,7 +9877,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);
@@ -10546,6 +10546,11 @@ int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
gen->opts = opts;
gen->swapped_endian = !is_native_endianness(obj);
obj->gen_loader = gen;
+ if (obj->token_fd) {
+ close(obj->token_fd);
+ obj->token_fd = 0;
+ obj->feat_cache->token_fd = 0;
+ }
return 0;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v4 3/6] libbpf: Move bpf_object_prepare_token() to object creation
2026-11-30 11:27 ` [PATCH bpf-next v4 3/6] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
@ 2026-11-30 11:42 ` sashiko-bot
0 siblings, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-11-30 11:42 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:
- [Low] libbpf: dead gen_loader check in bpf_object_prepare_token()
--
commit 14d82bd3ff8081f528338c6f245edde3057b4e9d
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() and closes any acquired token fd in
bpf_object__gen_loader().
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 61a3888683737..2aa3d3e33cc2d 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -9284,6 +9284,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;
>
> @@ -9876,7 +9877,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);
> @@ -10546,6 +10546,11 @@ int bpf_object__gen_loader(struct bpf_object *obj, struct gen_loader_opts *opts)
> gen->opts = opts;
> gen->swapped_endian = !is_native_endianness(obj);
> obj->gen_loader = gen;
> + if (obj->token_fd) {
> + close(obj->token_fd);
> + obj->token_fd = 0;
> + obj->feat_cache->token_fd = 0;
> + }
> return 0;
> }
[Severity: Low]
Does moving bpf_object_prepare_token() into bpf_object_open() make the
obj->gen_loader check at the start of bpf_object_prepare_token() dead
code?
tools/lib/bpf/libbpf.c:bpf_object_prepare_token() {
...
if (obj->gen_loader)
return 0;
...
}
Commit d92defe04ad5 ("libbpf: Skip BPF token preparation when generating
loader") added that check so offline loader generation would not probe
the host bpffs. Because bpf_object_prepare_token() now runs during
bpf_object_open() before the caller can invoke bpf_object__gen_loader(),
obj->gen_loader is always NULL there, and bpf_object_open() will fail
before reaching bpf_object__gen_loader() if LIBBPF_BPF_TOKEN_PATH is set
to a path where token creation fails on the build host.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-bpf-relax-get-next-id-v4-0-807e2b53cc22@toke.dk?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
` (2 preceding siblings ...)
2026-11-30 11:27 ` [PATCH bpf-next v4 3/6] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
@ 2026-11-30 11:27 ` Toke Høiland-Jørgensen
2026-11-30 11:38 ` sashiko-bot
2026-11-30 11:27 ` [PATCH bpf-next v4 5/6] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
2026-11-30 11:27 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen
5 siblings, 1 reply; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:27 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 | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 2aa3d3e33cc2..ce748b8bde96 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6020,6 +6020,9 @@ 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_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;
@@ -6044,10 +6047,10 @@ 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) {
+ if (err && (errno == EPERM || (errno == EINVAL && opts.token_fd))) {
pr_debug("skipping module BTFs loading, missing privileges\n");
return 0;
}
@@ -6057,10 +6060,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;
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects
2026-11-30 11:27 ` [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
@ 2026-11-30 11:38 ` sashiko-bot
2026-11-30 11:49 ` Toke Høiland-Jørgensen
0 siblings, 1 reply; 10+ messages in thread
From: sashiko-bot @ 2026-11-30 11: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:
- [Low] libbpf: commit message mismatch with EINVAL handling in
load_module_btfs()
--
commit 5b2c115a6b84d1a2dda07a4ba224595597f8f78d
Author: Toke Høiland-Jørgensen <toke@toke.dk>
libbpf: Pass in token_fd when iterating kernel BTF objects
This commit passes obj->token_fd to bpf_btf_get_next_id_opts() and
bpf_btf_get_fd_by_id_opts() when iterating and loading kernel module BTFs
in load_module_btfs().
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 2aa3d3e33cc2d..ce748b8bde96e 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -6020,6 +6020,9 @@ 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_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;
> @@ -6044,10 +6047,10 @@ 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) {
> + if (err && (errno == EPERM || (errno == EINVAL && opts.token_fd))) {
> pr_debug("skipping module BTFs loading, missing privileges\n");
> return 0;
> }
> @@ -6057,10 +6060,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;
> + }
[Severity: Low]
Does the commit message match how load_module_btfs() handles EINVAL?
The changelog states that on EINVAL from older kernels, load_module_btfs()
resets any tokens and tries again, but the code instead treats
(errno == EINVAL && opts.token_fd) the same as EPERM, logs a debug message
about missing privileges, and returns 0 without retrying. The changelog
also does not mention the new EPERM check after
bpf_btf_get_fd_by_id_opts().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-bpf-relax-get-next-id-v4-0-807e2b53cc22@toke.dk?part=4
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects
2026-11-30 11:38 ` sashiko-bot
@ 2026-11-30 11:49 ` Toke Høiland-Jørgensen
0 siblings, 0 replies; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:49 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:
> - [Low] libbpf: commit message mismatch with EINVAL handling in
> load_module_btfs()
> --
>
> commit 5b2c115a6b84d1a2dda07a4ba224595597f8f78d
> Author: Toke Høiland-Jørgensen <toke@toke.dk>
>
> libbpf: Pass in token_fd when iterating kernel BTF objects
>
> This commit passes obj->token_fd to bpf_btf_get_next_id_opts() and
> bpf_btf_get_fd_by_id_opts() when iterating and loading kernel module BTFs
> in load_module_btfs().
>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 2aa3d3e33cc2d..ce748b8bde96e 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -6020,6 +6020,9 @@ 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_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;
>> @@ -6044,10 +6047,10 @@ 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) {
>> + if (err && (errno == EPERM || (errno == EINVAL && opts.token_fd))) {
>> pr_debug("skipping module BTFs loading, missing privileges\n");
>> return 0;
>> }
>> @@ -6057,10 +6060,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;
>> + }
>
> [Severity: Low]
> Does the commit message match how load_module_btfs() handles EINVAL?
>
> The changelog states that on EINVAL from older kernels, load_module_btfs()
> resets any tokens and tries again, but the code instead treats
> (errno == EINVAL && opts.token_fd) the same as EPERM, logs a debug message
> about missing privileges, and returns 0 without retrying. The changelog
> also does not mention the new EPERM check after
> bpf_btf_get_fd_by_id_opts().
Ah, oops, forgot to update the commit message when changing the code.
Will do a quick respin fixing this and the dead code complaint in the
other commit.
pw-bot: cr
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH bpf-next v4 5/6] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
` (3 preceding siblings ...)
2026-11-30 11:27 ` [PATCH bpf-next v4 4/6] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
@ 2026-11-30 11:27 ` Toke Høiland-Jørgensen
2026-11-30 11:27 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add test for attaching fentry to a module with bpf_token Toke Høiland-Jørgensen
5 siblings, 0 replies; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:27 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 and empty string
in the token/obj_priv_freplace_prog_fail test.
Drop the now redundant bpf_object__prepare() call in the succeeding
test, and update the test comment accordingly.
Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
---
tools/testing/selftests/bpf/prog_tests/token.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/token.c b/tools/testing/selftests/bpf/prog_tests/token.c
index f2f5d36ae00a..6499db1704bb 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;
@@ -821,23 +823,17 @@ static int userns_obj_priv_freplace_setup(int mnt_fd, struct priv_freplace_prog
return 0;
}
-/* Verify that freplace works from user namespace, because bpf token is loaded
- * in bpf_object__prepare
- */
+/* Verify that freplace works from user namespace */
static int userns_obj_priv_freplace_prog(int mnt_fd, struct token_lsm *lsm_skel)
{
struct priv_freplace_prog *fr_skel = NULL;
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;
- err = bpf_object__prepare(fr_skel->obj);
- if (!ASSERT_OK(err, "freplace__prepare"))
- goto out;
-
err = bpf_program__set_attach_target(fr_skel->progs.new_xdp_prog2, tgt_fd, "xdp_prog1");
if (!ASSERT_OK(err, "set_attach_target"))
goto out;
@@ -858,7 +854,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] 10+ messages in thread* [PATCH bpf-next v4 6/6] selftests/bpf: Add test for attaching fentry to a module with bpf_token
2026-11-30 11:26 [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
` (4 preceding siblings ...)
2026-11-30 11:27 ` [PATCH bpf-next v4 5/6] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
@ 2026-11-30 11:27 ` Toke Høiland-Jørgensen
5 siblings, 0 replies; 10+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-30 11:27 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 6499db1704bb..b16b9b4d3d45 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,
@@ -1099,6 +1100,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)
@@ -1195,6 +1224,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] 10+ messages in thread