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

tests: add test for go caller function and package #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
run: (cd testpkgs/basiccgo && make all)

- name: Run tests
run: (cd track_syscalls && sudo go test)
run: (cd track_syscalls && sudo go test ./...)
4 changes: 2 additions & 2 deletions track_syscalls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
func runBuildMode(args Args) {
syscalls := make(map[string]map[int]bool)
setupAndRun(args.BinaryPath, args.ModManifest, func(event ebpfEvent, stackTrace []uint64, objs *ebpfObjects) {
callerPackage, _, err := stackanalyzer.GetCallerPackageAndFunction(stackTrace)
callerPackage, _, err := stackanalyzer.ResolveCallerAndPackageNameFromStackTrace(stackTrace)
if err != nil {
log.Printf("Error getting caller package: %v", err)
return
Expand Down Expand Up @@ -97,7 +97,7 @@ func runEnforceMode(args Args) {
defer f.Close()

setupAndRun(args.BinaryPath, args.ModManifest, func(event ebpfEvent, stackTrace []uint64, objs *ebpfObjects) {
callerPackage, _, err := stackanalyzer.GetCallerPackageAndFunction(stackTrace)
callerPackage, _, err := stackanalyzer.ResolveCallerAndPackageNameFromStackTrace(stackTrace)
if err != nil {
log.Printf("Error getting caller package: %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion track_syscalls/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBuild(t *testing.T) {
StopTracer(tracer)

var expectedCallerFunction = "Go caller function: example.com/filereader.ExecuteMaliciousCGO"
var expectedCallerPackage = "Go caller package: example.com"
var expectedCallerPackage = "Go caller package: example.com/filereader"
var actualOutput = outputBuffer.String()

if !strings.Contains(actualOutput, expectedCallerFunction) {
Expand Down
24 changes: 18 additions & 6 deletions track_syscalls/stackanalyzer/stackanalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ImportedPackages struct {
packages []string
}

// ToDo: do not use global variables
var ImportedPackagesCache *ImportedPackages

func LoadModuleCache(modManifest string) error {
Expand Down Expand Up @@ -69,7 +70,7 @@ func ResolveSymbols(stackTrace []uint64) string {
return strings.Join(resolved, "\n")
}

func GetCallerPackageAndFunction(stackTrace []uint64) (string, string, error) {
func ResolveCallerAndPackageNameFromStackTrace(stackTrace []uint64) (string, string, error) {

if ImportedPackagesCache == nil {
return "", "", fmt.Errorf("ImportedPackagesCache is not initialized")
Expand All @@ -83,11 +84,22 @@ func GetCallerPackageAndFunction(stackTrace []uint64) (string, string, error) {
symbol := binanalyzer.Resolve(addr)

// Check if the symbol contains an imported Go package
for _, pkgName := range ImportedPackagesCache.packages {
if strings.Contains(symbol, pkgName) {
funcName := strings.TrimPrefix(symbol, pkgName+".")
return pkgName, funcName, nil
}
pkgName, funcName, err := GetCallerPackageAndFunction(symbol)
if err != nil {
return "", "", err
}
if pkgName != "" && funcName != "" {
return pkgName, funcName, nil
}
}
return "", "", nil
}

func GetCallerPackageAndFunction(resolvedSymbol string) (string, string, error) {
for _, pkgName := range ImportedPackagesCache.packages {
if strings.Contains(resolvedSymbol, pkgName) {
funcName := strings.TrimPrefix(resolvedSymbol, pkgName+".")
return pkgName, funcName, nil
}
}
return "", "", nil
Expand Down
50 changes: 50 additions & 0 deletions track_syscalls/stackanalyzer/stackanalyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package stackanalyzer

import (
"testing"
)

func TestLoadModuleCache_CanParseRequireInGoMod(t *testing.T) {
// arrange
var modManifest string = "../../testdata/go.mod"

// act
err := LoadModuleCache(modManifest)
if err != nil {
t.Errorf("Error initalising module cache: %v", err)
}

// assert
if len(ImportedPackagesCache.packages) != 1 {
t.Errorf("Expected 1 module, got %d", len(ImportedPackagesCache.packages))
}
if ImportedPackagesCache.packages[0] != "example.com" {
t.Errorf("Expected module to be example.com, got %s", ImportedPackagesCache.packages[0])
}
}

func TestGetCallerPackageAndFunction_GetCallerAndPackageInformationFromBasiccgo(t *testing.T) {
// arrange
var stackFrame = "example.com/filereader.ExecuteMaliciousCGO"
var modManifest string = "../../testdata/go.mod"

// act
// this is to load the module cache
err := LoadModuleCache(modManifest)
if err != nil {
t.Errorf("Error initalising module cache: %v", err)
}
callerPackage, callerFunction, err := GetCallerPackageAndFunction(stackFrame)

// assert
if err != nil {
t.Errorf("Error getting caller package and function: %v", err)
}

if callerPackage != "example.com/filereader" {
t.Errorf("Expected caller package to be example.com/filereader, got %s", callerPackage)
}
if callerFunction != "example.com/filereader.ExecuteMaliciousCGO" {
t.Errorf("Expected caller function to be ExecuteMaliciousCGO, got %s", callerFunction)
}
}
5 changes: 2 additions & 3 deletions track_syscalls/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"golang.org/x/sys/unix"
"log"
"os"
"os/signal"
Expand All @@ -15,14 +16,12 @@ import (
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sys/unix"
)

func logEvent(event ebpfEvent, stackTrace []uint64) {
resolvedStackTrace := stackanalyzer.ResolveSymbols(stackTrace)
callerPackage, callerFunction, err := stackanalyzer.GetCallerPackageAndFunction(stackTrace)
callerPackage, callerFunction, err := stackanalyzer.ResolveCallerAndPackageNameFromStackTrace(stackTrace)
if err != nil {
log.Printf("Error getting caller package: %v", err)
return
}

Expand Down
Loading