Skip to content

Commit

Permalink
tetragon: Update tests for KprobeMatchBinaries
Browse files Browse the repository at this point in the history
Adding tests for Postix and NotPostfix operators in
TestKprobeMatchBinaries and TestKprobeMatchBinariesPerfring.

Signed-off-by: Andrei Fedotov <[email protected]>
  • Loading branch information
anfedotoff committed Aug 12, 2024
1 parent 1948184 commit 87e64e8
Showing 1 changed file with 58 additions and 30 deletions.
88 changes: 58 additions & 30 deletions pkg/sensors/tracing/kprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3867,7 +3867,7 @@ func matchBinariesTest(t *testing.T, operator string, values []string, kpChecker
assert.NoError(t, err)
}

const skipMatchBinariesPrefix = "kernels without large progs do not support matchBinaries Prefix/NotPrefix"
const skipMatchBinaries = "kernels without large progs do not support matchBinaries Prefix/NotPrefix/Postfix/NotPostfix"

func TestKprobeMatchBinaries(t *testing.T) {
t.Run("In", func(t *testing.T) {
Expand All @@ -3878,48 +3878,39 @@ func TestKprobeMatchBinaries(t *testing.T) {
})
t.Run("Prefix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinariesPrefix)
t.Skip(skipMatchBinaries)
}
matchBinariesTest(t, "Prefix", []string{"/usr/bin/t"}, createBinariesChecker("/usr/bin/tail", "/etc/passwd"))
})
t.Run("NotPrefix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinariesPrefix)
t.Skip(skipMatchBinaries)
}
matchBinariesTest(t, "NotPrefix", []string{"/usr/bin/t"}, createBinariesChecker("/usr/bin/head", "/etc/passwd"))
})
t.Run("Postfix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinaries)
}
matchBinariesTest(t, "Postfix", []string{"bin/tail"}, createBinariesChecker("/usr/bin/tail", "/etc/passwd"))
})
t.Run("NotPostfix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinaries)
}
matchBinariesTest(t, "NotPostfix", []string{"bin/tail"}, createBinariesChecker("/usr/bin/head", "/etc/passwd"))
})
}

func TestKprobeMatchBinariesPrefixLargePath(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip()
}

// create a large temporary directory path
tmpDir := t.TempDir()
targetBinLargePath := tmpDir
// add (255 + 1) * 15 = 3840 chars to the path
// max is 4096 and we want to leave some space for the tmpdir + others
for range 15 {
targetBinLargePath += "/" + strings.Repeat("a", unix.NAME_MAX)
}
err := os.MkdirAll(targetBinLargePath, 0755)
require.NoError(t, err)

// copy the binary into it
targetBinLargePath += "/true"
fileExec, err := exec.LookPath("true")
require.NoError(t, err)
err = exec.Command("cp", fileExec, targetBinLargePath).Run()
require.NoError(t, err)
func matchBinariesLargePathTest(t *testing.T, operator string, values []string, binary string) {

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

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

createCrdFile(t, getMatchBinariesCrd("Prefix", []string{tmpDir}))
createCrdFile(t, getMatchBinariesCrd(operator, values))

obs, err := observertesthelper.GetDefaultObserverWithFile(t, ctx, testConfigFile, tus.Conf().TetragonLib, observertesthelper.WithMyPid())
if err != nil {
Expand All @@ -3928,15 +3919,46 @@ func TestKprobeMatchBinariesPrefixLargePath(t *testing.T) {
observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

if err := exec.Command(targetBinLargePath).Run(); err != nil {
if err := exec.Command(binary).Run(); err != nil {
t.Fatalf("failed to run true: %s", err)
}

checker := ec.NewUnorderedEventChecker(ec.NewProcessKprobeChecker("").
WithProcess(ec.NewProcessChecker().WithBinary(sm.Full(targetBinLargePath))).
WithProcess(ec.NewProcessChecker().WithBinary(sm.Full(binary))).
WithFunctionName(sm.Full("fd_install")))
err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)

}
func TestKprobeMatchBinariesLargePath(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip()
}

// create a large temporary directory path
tmpDir := t.TempDir()
targetBinLargePath := tmpDir
// add (255 + 1) * 15 = 3840 chars to the path
// max is 4096 and we want to leave some space for the tmpdir + others
for range 15 {
targetBinLargePath += "/" + strings.Repeat("a", unix.NAME_MAX)
}
err := os.MkdirAll(targetBinLargePath, 0755)
require.NoError(t, err)

// copy the binary into it
targetBinLargePath += "/true"
fileExec, err := exec.LookPath("true")
require.NoError(t, err)
err = exec.Command("cp", fileExec, targetBinLargePath).Run()
require.NoError(t, err)

t.Run("Prefix", func(t *testing.T) {
matchBinariesLargePathTest(t, "Prefix", []string{tmpDir}, targetBinLargePath)
})
t.Run("Postfix", func(t *testing.T) {
matchBinariesLargePathTest(t, "Postfix", []string{"/true"}, targetBinLargePath)
})
}

// matchBinariesPerfringTest checks that the matchBinaries do correctly
Expand Down Expand Up @@ -4024,10 +4046,16 @@ func TestKprobeMatchBinariesPerfring(t *testing.T) {
})
t.Run("Prefix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinariesPrefix)
t.Skip(skipMatchBinaries)
}
matchBinariesPerfringTest(t, "Prefix", []string{"/usr/bin/t"})
})
t.Run("Postfix", func(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinaries)
}
matchBinariesPerfringTest(t, "Postfix", []string{"tail"})
})
}

// TestKprobeMatchBinariesEarlyExec checks that the matchBinaries can filter
Expand Down Expand Up @@ -4106,7 +4134,7 @@ func TestKprobeMatchBinariesEarlyExec(t *testing.T) {
// of its machinery.
func TestKprobeMatchBinariesPrefixMatchArgs(t *testing.T) {
if !kernels.EnableLargeProgs() {
t.Skip(skipMatchBinariesPrefix)
t.Skip(skipMatchBinaries)
}

testutils.CaptureLog(t, logger.GetLogger().(*logrus.Logger))
Expand Down

0 comments on commit 87e64e8

Please sign in to comment.