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