Skip to content

Commit

Permalink
tetragon: Add persistent enforcement test for policy unload
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Nov 7, 2024
1 parent 55875a9 commit ff5dab6
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions pkg/sensors/tracing/enforcer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,51 @@ func testEnforcerPersistentNoKeep(t *testing.T, builder func() *EnforcerSpecBuil
run(2, "exit status 22")
}

func testEnforcerPersistentUnload(t *testing.T, builder func() *EnforcerSpecBuilder, expected, test string) {
testEnforcerCheckSkip(t)

if !bpf.HasLinkPin() {
t.Skip("skipping persistent enforcer test, link pin is not available")
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

tus.LoadSensor(t, base.GetInitialSensor())
path := bpf.MapPrefixPath()
mgr, err := sensors.StartSensorManager(path, true /* KeepSensorsOnExit */)
assert.NoError(t, err)

run := func(idx int, exp string) {
cmd := exec.Command(test, "0xfffe")
err := cmd.Run()

t.Logf("Run %s: %v\n", cmd, err)
if err == nil || err.Error() != exp {
t.Fatalf("run %d: Wrong error '%v' expected '%s'", idx, err, exp)
}
}

tp, err := builder().WithoutMultiKprobe().Build()
assert.NoError(t, err)

err = mgr.AddTracingPolicy(ctx, tp)
assert.NoError(t, err)

// first run - sensors are loaded, we should get kill/override
run(1, expected)

// remove the policy and we should get rid of the enforcement
err = mgr.DeleteTracingPolicy(ctx, tp.TpName(), "")
assert.NoError(t, err)

// bpf pinned links removal is asynchronous, we need to wait to be sure it's gone
time.Sleep(2 * time.Second)

// third run - sensors are unloaded, map dir is removed, we should get no enforcement
run(2, "exit status 22")
}

func TestEnforcerPersistentOverride(t *testing.T) {
test := testutils.RepoRootPath("contrib/tester-progs/enforcer-tester")

Expand All @@ -824,6 +869,9 @@ func TestEnforcerPersistentOverride(t *testing.T) {
t.Run("persistent-override-no-keep", func(t *testing.T) {
testEnforcerPersistentNoKeep(t, builder, "exit status 17", test)
})
t.Run("persistent-override-extra", func(t *testing.T) {
testEnforcerPersistentUnload(t, builder, "exit status 17", test)
})
}

func TestEnforcerPersistentKill(t *testing.T) {
Expand All @@ -843,4 +891,7 @@ func TestEnforcerPersistentKill(t *testing.T) {
t.Run("persistent-kill-no-keep", func(t *testing.T) {
testEnforcerPersistentNoKeep(t, builder, "signal: killed", test)
})
t.Run("persistent-kill-extra", func(t *testing.T) {
testEnforcerPersistentUnload(t, builder, "signal: killed", test)
})
}

0 comments on commit ff5dab6

Please sign in to comment.