@@ -11,6 +11,7 @@ import (
11
11
"time"
12
12
13
13
"github.com/cilium/tetragon/pkg/api/ops"
14
+ "github.com/cilium/tetragon/pkg/api/processapi"
14
15
"github.com/cilium/tetragon/pkg/cgroups"
15
16
grpcexec "github.com/cilium/tetragon/pkg/grpc/exec"
16
17
"github.com/cilium/tetragon/pkg/sensors"
@@ -20,6 +21,7 @@ import (
20
21
"github.com/cilium/tetragon/pkg/observer"
21
22
"github.com/cilium/tetragon/pkg/option"
22
23
"github.com/cilium/tetragon/pkg/sensors/base"
24
+ "github.com/cilium/tetragon/pkg/sensors/cgroup/cgrouptrackmap"
23
25
testsensor "github.com/cilium/tetragon/pkg/sensors/test"
24
26
"github.com/cilium/tetragon/pkg/testutils"
25
27
"github.com/cilium/tetragon/pkg/testutils/perfring"
@@ -200,3 +202,114 @@ func TestCgroupNoEvents(t *testing.T) {
200
202
}
201
203
}
202
204
}
205
+
206
+ // Ensure that we get cgroup_{mkdir|rmdir} events
207
+ func TestCgroupEventMkdirRmdir (t * testing.T ) {
208
+ testutils .CaptureLog (t , logger .GetLogger ().(* logrus.Logger ))
209
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTimeout )
210
+ defer cancel ()
211
+
212
+ option .Config .HubbleLib = tus .Conf ().TetragonLib
213
+ option .Config .Verbosity = 5
214
+
215
+ _ , err := observer .GetDefaultObserver (t , ctx , tus .Conf ().TetragonLib )
216
+ if err != nil {
217
+ t .Fatalf ("GetDefaultObserver error: %s" , err )
218
+ }
219
+
220
+ testManager := tus .StartTestSensorManager (ctx , t )
221
+ observer .SensorManager = testManager .Manager
222
+
223
+ testManager .AddAndEnableSensors (ctx , t , loadedSensors )
224
+
225
+ // Set Tracking level to 3 so we receive notifcations about
226
+ // /sys/fs/cgroup/$1/$2/$3 all cgroups that are at level <=3
227
+ trackingCgrpLevel := uint32 (3 )
228
+ setupTgRuntimeConf (t , trackingCgrpLevel , uint32 (logrus .TraceLevel ))
229
+
230
+ cgroupFSPath := cgroups .GetCgroupFSPath ()
231
+ assert .NotEmpty (t , cgroupFSPath )
232
+
233
+ dir , hierarchy := getTestCgroupDirAndHierarchy (t )
234
+ cgroupRmdir (t , cgroupFSPath , hierarchy , tetragonCgrpRoot )
235
+
236
+ matchedPath := dir
237
+ finalPath := filepath .Join (cgroupFSPath , hierarchy , dir )
238
+ _ , err = os .Stat (finalPath )
239
+ if err == nil {
240
+ t .Fatalf ("Test %s failed cgroup test hierarchy should not exist '%s'" , t .Name (), finalPath )
241
+ }
242
+
243
+ t .Cleanup (func () {
244
+ cgroupRmdir (t , cgroupFSPath , hierarchy , dir )
245
+ })
246
+
247
+ trigger := func () {
248
+ err = cgroupMkdir (t , cgroupFSPath , hierarchy , dir )
249
+ assert .NoError (t , err )
250
+
251
+ err = cgroupRmdir (t , cgroupFSPath , hierarchy , dir )
252
+ assert .NoError (t , err )
253
+ }
254
+
255
+ mkdir := false
256
+ rmdir := false
257
+ cgrpMap := testsensor .GetCgroupsTrackingMap ()
258
+ cgrpMapPath := filepath .Join (bpf .MapPrefixPath (), cgrpMap .Name )
259
+ cgrpTrackingId := uint64 (0 )
260
+ events := perfring .RunTestEvents (t , ctx , trigger )
261
+ for _ , ev := range events {
262
+ if msg , ok := ev .(* grpcexec.MsgCgroupEventUnix ); ok {
263
+ if msg .Common .Op == ops .MSG_OP_CGROUP {
264
+ cgrpPath := cgroups .CgroupNameFromCStr (msg .Path [:processapi .CGROUP_PATH_LENGTH ])
265
+ op := ops .CgroupOpCode (msg .CgrpOp )
266
+ st := ops .CgroupState (msg .CgrpData .State ).String ()
267
+ logger .GetLogger ().WithFields (logrus.Fields {
268
+ "cgroup.event" : op .String (),
269
+ "PID" : msg .PID ,
270
+ "NSPID" : msg .NSPID ,
271
+ "cgroup.IDTracker" : msg .CgrpidTracker ,
272
+ "cgroup.ID" : msg .Cgrpid ,
273
+ "cgroup.state" : st ,
274
+ "cgroup.level" : msg .CgrpData .Level ,
275
+ "cgroup.path" : cgrpPath ,
276
+ }).Info ("Received Cgroup event" )
277
+
278
+ assert .NotZero (t , msg .PID )
279
+ assert .NotZero (t , msg .Cgrpid )
280
+ assert .NotZero (t , msg .CgrpidTracker )
281
+ assert .NotEqualValues (t , msg .CgrpData .State , ops .CGROUP_UNTRACKED )
282
+ assert .NotZero (t , msg .CgrpData .Level )
283
+
284
+ switch op {
285
+ case ops .MSG_OP_CGROUP_MKDIR :
286
+ assert .EqualValues (t , ops .CGROUP_NEW , msg .CgrpData .State )
287
+ // Match only our test
288
+ if cgrpPath == matchedPath {
289
+ cgrpName := cgroups .CgroupNameFromCStr (msg .CgrpData .Name [:processapi .CGROUP_NAME_LENGTH ])
290
+ assert .EqualValues (t , t .Name (), cgrpName )
291
+
292
+ mkdir = true
293
+ cgrpTrackingId = msg .CgrpidTracker
294
+ }
295
+ case ops .MSG_OP_CGROUP_RMDIR :
296
+ // Match only our test
297
+ if cgrpPath == matchedPath {
298
+ cgrpName := cgroups .CgroupNameFromCStr (msg .CgrpData .Name [:processapi .CGROUP_NAME_LENGTH ])
299
+ assert .EqualValues (t , t .Name (), cgrpName )
300
+ rmdir = true
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ // Ensure that we received proper events
308
+ assert .Equal (t , true , mkdir )
309
+ assert .Equal (t , true , rmdir )
310
+ assert .NotZero (t , true , cgrpTrackingId )
311
+
312
+ // Should be removed from the tracking map
313
+ _ , err = cgrouptrackmap .LookupTrackingCgroup (cgrpMapPath , cgrpTrackingId )
314
+ assert .Error (t , err )
315
+ }
0 commit comments