Skip to content

prog.go: cache BTF for CAP_SYS_ADMIN-less freplace#2011

Merged
dylandreimerink merged 1 commit into
cilium:mainfrom
mejedi:unpriveleged-freplace
May 27, 2026
Merged

prog.go: cache BTF for CAP_SYS_ADMIN-less freplace#2011
dylandreimerink merged 1 commit into
cilium:mainfrom
mejedi:unpriveleged-freplace

Conversation

@mejedi
Copy link
Copy Markdown
Contributor

@mejedi mejedi commented May 15, 2026

Before the change, performing freplace with cilium/ebpf required CAP_SYS_ADMIN.

The subprogram to replace is specified via AttachTarget (*Program) and AttachTo (string) in the replacement ProgramSpec. Since the kernel expects a BTF type ID of the subprogram to replace, the library has to resolve the AttachTo string. It needs BTF of the AttachTarget. To obtain BTF from the kernel CAP_SYS_ADMIN is needed.

Extend Program with btf (*btf.Handle). It is populated for Program-s loaded by the library. Program's .Handle() leverages the cached BTF transparaently, enabling CAP_SYS_ADMIN-less freplace.

Add SetHandle method to Program to cover program references obtained via NewProgramFromFD(), and LoadPinnedProgram(). The intended usage is as follows:

  • a CAP_SYS_ADMIN-less user obtains a program reference and the corresponding BTF (e.g. both from a pin);

  • calls .SetHandle() on the Program;

  • freplace succeeds with the Program as AttachTarget.

@mejedi mejedi requested review from a team, dylandreimerink and mmat11 as code owners May 15, 2026 10:29
Copy link
Copy Markdown
Member

@dylandreimerink dylandreimerink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this PR, all makes sense to me. Just got two minor comments.

Comment thread prog.go
Comment thread prog.go Outdated
Before the change, performing freplace with cilium/ebpf required
CAP_SYS_ADMIN.

The subprogram to replace is specified via AttachTarget (*Program) and
AttachTo (string) in the replacement ProgramSpec. Since the kernel
expects a BTF type ID of the subprogram to replace, the library has to
resolve the AttachTo string. It needs BTF of the AttachTarget. To obtain
BTF from the kernel CAP_SYS_ADMIN is needed.

Extend Program with btf (*btf.Handle). It is populated for Program-s
loaded by the library. Program's .Handle() leverages the cached BTF
transparaently, enabling CAP_SYS_ADMIN-less freplace.

Add SetHandle method to Program to cover program references obtained via
NewProgramFromFD(), and LoadPinnedProgram(). The intended usage is as
follows:

 * a CAP_SYS_ADMIN-less user obtains a program reference and the
   corresponding BTF (e.g. both from a pin);

 * calls .SetHandle() on the Program;

 * freplace succeeds with the Program as AttachTarget.

Signed-off-by: Nick Zavaritsky <mejedi@gmail.com>
@mejedi mejedi force-pushed the unpriveleged-freplace branch from e5fbf6d to 429ca76 Compare May 21, 2026 20:03
@mejedi mejedi requested a review from dylandreimerink May 21, 2026 20:23
Copy link
Copy Markdown
Member

@dylandreimerink dylandreimerink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes, this seems good to me!

@dylandreimerink dylandreimerink merged commit 3b6c645 into cilium:main May 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants