Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to load LSM and Tracing programs #953

Merged
merged 2 commits into from
May 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions pkg/sensors/program/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,46 @@ func NoAttach(load *Program) AttachFunc {
}
}

func TracingAttach(load *Program) AttachFunc {
return func(prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
linkFn := func() (link.Link, error) {
return link.AttachTracing(link.TracingOptions{
Copy link
Contributor

Choose a reason for hiding this comment

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

is this for fentry?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this can be used for fentry, fexit, and fmod_ret programs.

Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
}

func LSMAttach(load *Program) AttachFunc {
return func(prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
linkFn := func() (link.Link, error) {
return link.AttachLSM(link.LSMOptions{
Program: prog,
})
}
lnk, err := linkFn()
if err != nil {
return nil, fmt.Errorf("attaching '%s' failed: %w", spec.Name, err)
}
return &unloader.RelinkUnloader{
UnloadProg: unloader.PinUnloader{Prog: prog}.Unload,
IsLinked: true,
Link: lnk,
RelinkFn: linkFn,
}, nil
}
}

func MultiKprobeAttach(load *Program) AttachFunc {
return func(prog *ebpf.Program, spec *ebpf.ProgramSpec) (unloader.Unloader, error) {
data, ok := load.AttachData.(*MultiKprobeAttachData)
Expand Down Expand Up @@ -248,6 +288,14 @@ func LoadMultiKprobeProgram(bpfDir, mapDir string, load *Program, verbose int) e
return loadProgram(bpfDir, []string{mapDir}, load, MultiKprobeAttach(load), ci, verbose)
}

func LoadTracingProgram(bpfDir, mapDir string, load *Program, verbose int) error {
return loadProgram(bpfDir, []string{mapDir}, load, TracingAttach(load), nil, verbose)
}

func LoadLSMProgram(bpfDir, mapDir string, load *Program, verbose int) error {
return loadProgram(bpfDir, []string{mapDir}, load, LSMAttach(load), nil, verbose)
}

func slimVerifierError(errStr string) string {
// The error is potentially up to 'verifierLogBufferSize' bytes long,
// and most of it is not interesting. For a user-friendly output, we'll
Expand Down