BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/7] Improvements to access module BTF with bpf_token
@ 2026-11-26 14:51 Toke Høiland-Jørgensen
  2026-11-26 14:51 ` [PATCH bpf-next v3 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; 13+ messages in thread
From: Toke Høiland-Jørgensen @ 2026-11-26 14:51 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 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 (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 ++-
 include/uapi/linux/capability.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                          | 25 ++++++++++--
 tools/lib/bpf/libbpf.map                        |  4 ++
 tools/testing/selftests/bpf/prog_tests/token.c  | 53 ++++++++++++++++++++-----
 tools/testing/selftests/bpf/progs/priv_module.c | 15 +++++++
 10 files changed, 176 insertions(+), 30 deletions(-)
---
base-commit: 486b5ce7184f7449ef2fe1e951455b0b8a114060
change-id: 20261006-bpf-relax-get-next-id-6d025833eee7


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

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

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 14:51 [PATCH bpf-next v3 0/7] Improvements to access module BTF with bpf_token Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 1/7] bpf: Relax BPF_*_GET_NEXT_ID permission checks to CAP_BPF and bpf_token Toke Høiland-Jørgensen
2026-11-26 15:19   ` bot+bpf-ci
2026-11-26 14:51 ` [PATCH bpf-next v3 2/7] libbpf: Add bpf_*_get_next_id_opts() functions Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 3/7] libbpf: Automatically set BPF_F_TOKEN_FD when getting BTF fd Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 4/7] libbpf: Move bpf_object_prepare_token() to object creation Toke Høiland-Jørgensen
2026-11-26 15:11   ` sashiko-bot
2026-11-26 19:44     ` Toke Høiland-Jørgensen
2026-11-26 15:47   ` bot+bpf-ci
2026-11-26 19:46     ` Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 5/7] libbpf: Pass in token_fd when iterating kernel BTF objects Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 6/7] selftests/bpf: Explicitly disable token in token/obj_priv_freplace_prog_fail test Toke Høiland-Jørgensen
2026-11-26 14:51 ` [PATCH bpf-next v3 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