BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v4 0/6] Improvements to access module BTF with bpf_token
@ 2026-11-30 11:26 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
                   ` (5 more replies)
  0 siblings, 6 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

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

The change in patch 4 temporarily breaks the token selftests because it
relies on the old behaviour. Patch 5 updates the selftests to fix this
up. Finally, patch 6 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 v4 (all suggested by Alexei):
- Reuse the check from BPF_BTF_GET_FD_BY_ID, so that the *_GET_NEXT_ID
  checks become token_capable(CAP_SYS_ADMIN)
- Only add one new getter function to libbpf (bpf_btf_get_next_id_opts)
- Don't retry in load_module_btfs() if the kernel rejects the token,
  just treat it like an EPERM (returning 0).
- Drop the patch that automatically sets BPF_F_TOKEN_FD in bpf_btf_get_fd_by_id_opts()
- Link to v3: https://patch.msgid.link/20261126-bpf-relax-get-next-id-v3-0-338e1d3077e7@toke.dk

Changes in v3:
- Update the comment on CAP_BPF in capability.h to match the changes.
- Unset obj->token_fd in bpf_object__gen_loader().
- Remove redundant bpf_object__prepare() in succesful test and update
  comment.
- Link to v2: https://patch.msgid.link/20261126-bpf-relax-get-next-id-v2-0-039e92757f9c@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 (6):
      bpf: Allow BPF_*_GET_NEXT_ID permission checks to pass with bpf_token
      libbpf: Add bpf_btf_get_next_id_opts() function
      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

 kernel/bpf/syscall.c                            | 72 ++++++++++++++-----------
 tools/lib/bpf/bpf.c                             | 24 ++++++---
 tools/lib/bpf/bpf.h                             |  3 ++
 tools/lib/bpf/libbpf.c                          | 20 +++++--
 tools/lib/bpf/libbpf.map                        |  1 +
 tools/testing/selftests/bpf/prog_tests/token.c  | 53 ++++++++++++++----
 tools/testing/selftests/bpf/progs/priv_module.c | 15 ++++++
 7 files changed, 136 insertions(+), 52 deletions(-)
---
base-commit: b7a509bd447085118d03dd962d2cca308e283b57
change-id: 20261006-bpf-relax-get-next-id-6d025833eee7


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

end of thread, other threads:[~2026-11-30 11:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [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
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
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

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