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