BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Toke Høiland-Jørgensen" <toke@toke.dk>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects
Date: Wed, 25 Nov 2026 15:56:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-233687@kernel.org> (raw)
In-Reply-To: <20261125-bpf-relax-get-next-id-v1-5-49175227c8cf@toke.dk>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] libbpf: EINVAL retry in load_module_btfs() skips EPERM and
  ENOENT checks
- [Medium] libbpf: unhandled EPERM in load_module_btfs() after token
  retry and fd fetch
--

commit 5f5f5856a82ba2af642d2a227760228210dc3824
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() in load_module_btfs(), retrying without a
token if the kernel returns EINVAL.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 2ede3081a20eb..65c2af92db2f3 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -6014,6 +6014,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;
> @@ -6038,20 +6042,26 @@ 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 == ENOENT)
>  			return 0;
>  		if (err && errno == EPERM) {
>  			pr_debug("skipping module BTFs loading, missing privileges\n");
>  			return 0;
>  		}
> +		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) {
>  			err = -errno;
>  			pr_warn("failed to iterate BTF objects: %s\n", errstr(err));
>  			return err;
>  		}

[Severity: Medium]
Should the EINVAL retry in load_module_btfs() run before the ENOENT and
EPERM checks?

On an older kernel that rejects next_opts.token_fd with EINVAL, the
retried bpf_btf_get_next_id_opts() call without a token can return EPERM
(for example, inside an unprivileged user namespace) or ENOENT.  Because
those checks precede the retry block, load_module_btfs() falls through to
if (err) and fails with -EPERM or -ENOENT instead of skipping module BTF
loading and returning 0.

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

[Severity: Medium]
What happens if bpf_btf_get_fd_by_id_opts() fails with EPERM after
bpf_btf_get_next_id_opts() succeeds?

In kernel/bpf/syscall.c, bpf_obj_get_next_id() only requires
bpf_token_allow_cmd(token, BPF_BTF_GET_NEXT_ID) and CAP_BPF, whereas
bpf_btf_get_fd_by_id() requires bpf_token_allow_cmd(token,
BPF_BTF_GET_FD_BY_ID) and CAP_SYS_ADMIN.  If the caller or token has
CAP_BPF without CAP_SYS_ADMIN (or allows BPF_BTF_GET_NEXT_ID without
BPF_BTF_GET_FD_BY_ID), bpf_btf_get_next_id_opts() succeeds and
bpf_btf_get_fd_by_id_opts() fails with EPERM, where the error path below
only ignores ENOENT and aborts load_module_btfs() with -EPERM instead of
skipping module BTF loading and returning 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-bpf-relax-get-next-id-v1-0-49175227c8cf@toke.dk?part=5

  reply	other threads:[~2026-11-25 15:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:31 [PATCH bpf-next 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-25 15:31 ` [PATCH bpf-next 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
2026-11-25 15:31 ` [PATCH bpf-next 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
2026-11-25 15:46   ` sashiko-bot
2026-11-26  9:48     ` Toke Høiland-Jørgensen
2026-11-25 15:31 ` [PATCH bpf-next 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
2026-11-25 15:31 ` [PATCH bpf-next 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
2026-11-25 15:31 ` [PATCH bpf-next 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
2026-11-25 15:56   ` sashiko-bot [this message]
2026-11-25 15:31 ` [PATCH bpf-next 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
2026-11-25 15:40   ` sashiko-bot
2026-11-25 15:31 ` [PATCH bpf-next 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=sashiko-outbox-233687@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=toke@toke.dk \
    /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