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

tetragon: un/pin fixes #3079

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions pkg/sensors/tracing/enforcer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,56 @@ func testEnforcerPersistentNoKeep(t *testing.T, builder func() *EnforcerSpecBuil
run(2, "exit status 22")
}

// We test following scenario:
// - load enforcement policy
// - 1st run of test binary, make sure enforcement policy is triggered
// - remove enforcement policy via sensor manager
// - 2nd run of test binary, no enforcement
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(), "")
Copy link
Member

Choose a reason for hiding this comment

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

In the commit you specify "test that simulates enforcement policy unload (not exit)" but you use Delete instead of DisableTracingPolicy, is that intended? You don't exactly test the scenario here #3033, maybe you want to modify one or add another one?

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 @@ -839,6 +889,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 @@ -858,4 +911,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)
})
}
Loading