@@ -35,14 +35,15 @@ import (
3535
3636 "github.com/cilium/tetragon/api/v1/tetragon"
3737 ec "github.com/cilium/tetragon/api/v1/tetragon/codegen/eventchecker"
38+ "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
39+
3840 "github.com/cilium/tetragon/pkg/api/tracingapi"
3941 "github.com/cilium/tetragon/pkg/arch"
4042 "github.com/cilium/tetragon/pkg/bpf"
4143 "github.com/cilium/tetragon/pkg/config"
4244 "github.com/cilium/tetragon/pkg/ftrace"
4345 "github.com/cilium/tetragon/pkg/grpc/tracing"
4446 "github.com/cilium/tetragon/pkg/jsonchecker"
45- "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
4647 "github.com/cilium/tetragon/pkg/kernels"
4748 "github.com/cilium/tetragon/pkg/logger"
4849 bc "github.com/cilium/tetragon/pkg/matchers/bytesmatcher"
@@ -3818,6 +3819,108 @@ spec:
38183819 require .Error (t , err )
38193820}
38203821
3822+ func getMatchParentsCrd (opStr string , vals []string ) string {
3823+ configHook := `apiVersion: cilium.io/v1alpha1
3824+ kind: TracingPolicy
3825+ metadata:
3826+ name: "testing-file-match-binaries"
3827+ spec:
3828+ kprobes:
3829+ - call: "fd_install"
3830+ syscall: false
3831+ return: false
3832+ args:
3833+ - index: 0
3834+ type: int
3835+ - index: 1
3836+ type: "file"
3837+ selectors:
3838+ - matchParents:
3839+ - operator: "` + opStr + `"
3840+ values: `
3841+ for i := range vals {
3842+ configHook += fmt .Sprintf ("\n - \" %s\" " , vals [i ])
3843+ }
3844+ return configHook
3845+ }
3846+
3847+ func createParentsChecker (parent , binary , filename string ) * ec.ProcessKprobeChecker {
3848+ kpChecker := ec .NewProcessKprobeChecker ("" ).
3849+ WithParent (ec .NewProcessChecker ().WithBinary (sm .Full (parent ))).
3850+ WithProcess (ec .NewProcessChecker ().WithBinary (sm .Full (binary ))).
3851+ WithFunctionName (sm .Full ("fd_install" )).
3852+ WithArgs (ec .NewKprobeArgumentListMatcher ().
3853+ WithOperator (lc .Subset ).
3854+ WithValues (
3855+ ec .NewKprobeArgumentChecker ().WithFileArg (ec .NewKprobeFileChecker ().WithPath (sm .Full (filename ))),
3856+ ))
3857+ return kpChecker
3858+ }
3859+
3860+ func matchParentsTest (t * testing.T , operator string , values []string , kpChecker * ec.ProcessKprobeChecker ) {
3861+ var doneWG , readyWG sync.WaitGroup
3862+ defer doneWG .Wait ()
3863+
3864+ ctx , cancel := context .WithTimeout (context .Background (), tus .Conf ().CmdWaitTime )
3865+ defer cancel ()
3866+
3867+ createCrdFile (t , getMatchParentsCrd (operator , values ))
3868+
3869+ obs , err := observertesthelper .GetDefaultObserverWithFile (t , ctx , testConfigFile , tus .Conf ().TetragonLib , observertesthelper .WithMyPid ())
3870+ if err != nil {
3871+ t .Fatalf ("GetDefaultObserverWithFile error: %s" , err )
3872+ }
3873+ observertesthelper .LoopEvents (ctx , t , & doneWG , & readyWG , obs )
3874+ readyWG .Wait ()
3875+
3876+ if err := exec .Command ("/usr/bin/bash" , "-c" , "echo '/usr/bin/tail /etc/passwd' | /usr/bin/bash" ).Run (); err != nil {
3877+ t .Fatalf ("failed to run tail /etc/passwd with /bin/bash: %s" , err )
3878+ }
3879+
3880+ if err := exec .Command ("/usr/bin/sh" , "-c" , "echo '/usr/bin/tail /etc/passwd' | /usr/bin/sh" ).Run (); err != nil {
3881+ t .Fatalf ("failed to run tail /etc/passwd with /bin/sh: %s" , err )
3882+ }
3883+
3884+ checker := ec .NewUnorderedEventChecker (kpChecker )
3885+ err = jsonchecker .JsonTestCheck (t , checker )
3886+ require .NoError (t , err )
3887+ }
3888+
3889+ const skipMatchParents = "kernels without large progs do not support matchParents Prefix/NotPrefix/Postfix/NotPostfix"
3890+
3891+ func TestKprobeMatchParents (t * testing.T ) {
3892+ t .Run ("In" , func (t * testing.T ) {
3893+ matchParentsTest (t , "In" , []string {"/usr/bin/bash" }, createParentsChecker ("/usr/bin/bash" , "/usr/bin/tail" , "/etc/passwd" ))
3894+ })
3895+ t .Run ("NotIn" , func (t * testing.T ) {
3896+ matchParentsTest (t , "NotIn" , []string {"/usr/bin/bash" }, createParentsChecker ("/usr/bin/sh" , "/usr/bin/tail" , "/etc/passwd" ))
3897+ })
3898+ t .Run ("Prefix" , func (t * testing.T ) {
3899+ if ! config .EnableLargeProgs () {
3900+ t .Skip (skipMatchParents )
3901+ }
3902+ matchParentsTest (t , "Prefix" , []string {"/usr/bin/ba" }, createParentsChecker ("/usr/bin/bash" , "/usr/bin/tail" , "/etc/passwd" ))
3903+ })
3904+ t .Run ("NotPrefix" , func (t * testing.T ) {
3905+ if ! config .EnableLargeProgs () {
3906+ t .Skip (skipMatchParents )
3907+ }
3908+ matchParentsTest (t , "NotPrefix" , []string {"/usr/bin/bas" }, createParentsChecker ("/usr/bin/sh" , "/usr/bin/tail" , "/etc/passwd" ))
3909+ })
3910+ t .Run ("Postfix" , func (t * testing.T ) {
3911+ if ! config .EnableLargeProgs () {
3912+ t .Skip (skipMatchParents )
3913+ }
3914+ matchParentsTest (t , "Postfix" , []string {"in/bash" }, createParentsChecker ("/usr/bin/bash" , "/usr/bin/tail" , "/etc/passwd" ))
3915+ })
3916+ t .Run ("NotPostfix" , func (t * testing.T ) {
3917+ if ! config .EnableLargeProgs () {
3918+ t .Skip (skipMatchParents )
3919+ }
3920+ matchParentsTest (t , "NotPostfix" , []string {"n/bash" }, createParentsChecker ("/usr/bin/sh" , "/usr/bin/tail" , "/etc/passwd" ))
3921+ })
3922+ }
3923+
38213924func getMatchBinariesCrd (opStr string , vals []string ) string {
38223925 configHook := `apiVersion: cilium.io/v1alpha1
38233926kind: TracingPolicy
0 commit comments