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

tracingpolicy: add BPF operations support #2943

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions pkg/sensors/tracing/kprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6970,3 +6970,67 @@ tetragon_missed_prog_probes_total{attach="wake_up_new_task",policy="__base__"} 0
prometheus.BuildFQName(consts.MetricsNamespace, "", "missed_prog_probes_total")))

}

func TestKprobeBpfCmd(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So I guess the idea of the test is to detect the tetragon bpf program / map loading?

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 kept it small, but it can be used to detect all bpf commands.

var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

ctx, cancel := context.WithTimeout(context.Background(), tus.Conf().CmdWaitTime)
defer cancel()

hook := `apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "bpf-cmd"
spec:
kprobes:
- call: "security_bpf"
syscall: false
args:
- index: 0
type: "bpf_cmd"
`
createCrdFile(t, hook)

obs, err := observertesthelper.GetDefaultObserverWithFile(t, ctx, testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}
observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

time.Sleep(1 * time.Second)
err = loadTestCrd()
if err != nil {
t.Fatalf("Loading test CRD failed: %s", err)
}

mapCreate := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("security_bpf")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithValues(
ec.NewKprobeArgumentChecker().WithBpfCmdArg(tetragon.BpfCmd_BPF_MAP_CREATE),
),
)

progLoad := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("security_bpf")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithValues(
ec.NewKprobeArgumentChecker().WithBpfCmdArg(tetragon.BpfCmd_BPF_PROG_LOAD),
),
)

btfLoad := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("security_bpf")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithValues(
ec.NewKprobeArgumentChecker().WithBpfCmdArg(tetragon.BpfCmd_BPF_BTF_LOAD),
),
)

checker := ec.NewUnorderedEventChecker(mapCreate, progLoad, btfLoad)

err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}
Loading