-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathexec.go
683 lines (594 loc) · 23 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package exec
import (
"errors"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/api/ops"
"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/cgroups"
"github.com/cilium/tetragon/pkg/eventcache"
"github.com/cilium/tetragon/pkg/ktime"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/metrics/errormetrics"
"github.com/cilium/tetragon/pkg/option"
"github.com/cilium/tetragon/pkg/process"
readerexec "github.com/cilium/tetragon/pkg/reader/exec"
"github.com/cilium/tetragon/pkg/reader/notify"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/wrapperspb"
)
const (
ProcessRefCnt = iota
ParentRefCnt
AncestorsRefCnt
)
func (msg *MsgExecveEventUnix) getCleanupEvent() *MsgProcessCleanupEventUnix {
if msg.Unix.Msg.CleanupProcess.Ktime == 0 {
return nil
}
return &MsgProcessCleanupEventUnix{
PID: msg.Unix.Msg.CleanupProcess.Pid,
Ktime: msg.Unix.Msg.CleanupProcess.Ktime,
}
}
// GetProcessExec returns Exec protobuf message for a given process, including the ancestor list.
func GetProcessExec(event *MsgExecveEventUnix, useCache bool) *tetragon.ProcessExec {
var tetragonParent *tetragon.Process
var tetragonAncestors []*tetragon.Process
var ancestors []*process.ProcessInternal
proc := process.AddExecEvent(event.Unix)
tetragonProcess := proc.UnsafeGetProcess()
parentId := tetragonProcess.ParentExecId
processId := tetragonProcess.ExecId
parent, err := process.Get(parentId)
if err == nil {
tetragonParent = parent.UnsafeGetProcess()
}
// Set the ancestors only if --enable-ancestors flag includes 'base'.
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() {
// We don't care about an error here, because later we call ec.NeededAncestors,
// that will determine if we were successful in collecting all ancestors and,
// if we were not, the event will be added to the event cache for reprocessing.
// Even if we were not able to collect all ancestors, we will still export what
// we were able to collect in the event.
ancestors, _ = process.GetAncestorProcessesInternal(tetragonProcess.ParentExecId)
for _, ancestor := range ancestors {
tetragonAncestors = append(tetragonAncestors, ancestor.UnsafeGetProcess())
}
}
// Set the cap field only if --enable-process-cred flag is set.
if err := proc.AnnotateProcess(option.Config.EnableProcessCred, option.Config.EnableProcessNs); err != nil {
logger.GetLogger().WithError(err).WithField("processId", processId).WithField("parentId", parentId).
Debugf("Failed to annotate process with capabilities and namespaces info")
}
tetragonEvent := &tetragon.ProcessExec{
Process: tetragonProcess,
Parent: tetragonParent,
Ancestors: tetragonAncestors,
}
if tetragonProcess.Pid == nil {
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}
if useCache {
if ec := eventcache.Get(); ec != nil &&
(ec.Needed(tetragonProcess) ||
(tetragonProcess.Pid.Value > 1 && ec.Needed(tetragonParent)) ||
(option.Config.EnableAncestors["base"] && ec.NeededAncestors(parent, ancestors))) {
ec.Add(proc, tetragonEvent, event.Unix.Msg.Common.Ktime, event.Unix.Process.Ktime, event)
return nil
}
}
if option.Config.EnableAncestors["base"] {
for _, ancestor := range ancestors {
ancestor.RefInc("ancestor")
}
}
if parent != nil {
parent.RefInc("parent")
}
// Finalize the process event with extra fields
if err := event.finalize(tetragonEvent, proc, eventcache.NO_EV_CACHE); err != nil {
// Propagate metric errors about finalizing the event
errormetrics.ErrorTotalInc(errormetrics.EventFinalizeProcessInfoFailed)
logger.GetLogger().WithFields(logrus.Fields{
"event.name": "Execve",
"event.process.pid": tetragonProcess.GetPid().GetValue(),
"event.process.binary": tetragonProcess.Binary,
"event.process.exec_id": processId,
"event.event_cache": "no",
}).Debugf("ExecveEvent: failed to finalize process exec event")
// For ProcessExec event we do not fail let's return what we have even if it's not complete
// The eventmetrics will count further errors
}
// do we need to cleanup anything?
if cleanupEvent := event.getCleanupEvent(); cleanupEvent != nil {
cleanupEvent.HandleMessage()
}
return tetragonEvent
}
type MsgCgroupEventUnix struct {
processapi.MsgCgroupEvent
}
func (msg *MsgCgroupEventUnix) Notify() bool {
return false
}
func (msg *MsgCgroupEventUnix) RetryInternal(_ notify.Event, _ uint64) (*process.ProcessInternal, error) {
return nil, errors.New("unreachable state: MsgCgroupEventUnix RetryInternal() was called")
}
func (msg *MsgCgroupEventUnix) Retry(_ *process.ProcessInternal, _ notify.Event) error {
return errors.New("unreachable state: MsgCgroupEventUnix Retry() was called")
}
func (msg *MsgCgroupEventUnix) HandleMessage() *tetragon.GetEventsResponse {
op := ops.CgroupOpCode(msg.CgrpOp)
st := ops.CgroupState(msg.CgrpData.State).String()
switch op {
case ops.MSG_OP_CGROUP_MKDIR, ops.MSG_OP_CGROUP_RMDIR, ops.MSG_OP_CGROUP_RELEASE:
logger.GetLogger().WithFields(logrus.Fields{
"cgroup.event": op.String(),
"PID": msg.PID,
"NSPID": msg.NSPID,
"cgroup.ID": msg.Cgrpid,
"cgroup.state": st,
"cgroup.hierarchyID": msg.CgrpData.HierarchyId,
"cgroup.level": msg.CgrpData.Level,
"cgroup.path": cgroups.CgroupNameFromCStr(msg.Path[:processapi.CGROUP_PATH_LENGTH]),
}).Debug("Received Cgroup event")
case ops.MSG_OP_CGROUP_ATTACH_TASK:
// Here we should get notification when Tetragon migrate itself
// and discovers cgroups configuration
logger.GetLogger().WithFields(logrus.Fields{
"cgroup.event": op.String(),
"PID": msg.PID,
"NSPID": msg.NSPID,
"cgroup.IDTracker": msg.CgrpidTracker,
"cgroup.ID": msg.Cgrpid,
"cgroup.state": st,
"cgroup.hierarchyID": msg.CgrpData.HierarchyId,
"cgroup.level": msg.CgrpData.Level,
"cgroup.path": cgroups.CgroupNameFromCStr(msg.Path[:processapi.CGROUP_PATH_LENGTH]),
}).Info("Received Cgroup event")
default:
logger.GetLogger().WithField("message", msg).Warn("HandleCgroupMessage: Unhandled Cgroup operation event")
}
return nil
}
func (msg *MsgCgroupEventUnix) Cast(o interface{}) notify.Message {
t := o.(processapi.MsgCgroupEvent)
return &MsgCgroupEventUnix{MsgCgroupEvent: t}
}
type MsgExecveEventUnix struct {
Unix *processapi.MsgExecveEventUnix
RefCntDone [3]bool
}
func (msg *MsgExecveEventUnix) Notify() bool {
return true
}
func (msg *MsgExecveEventUnix) RetryInternal(_ notify.Event, _ uint64) (*process.ProcessInternal, error) {
return nil, errors.New("unreachable state: MsgExecveEventUnix with missing internal")
}
func (msg *MsgExecveEventUnix) Retry(internal *process.ProcessInternal, ev notify.Event) error {
var podInfo *tetragon.Pod
tetragonProcess := ev.GetProcess()
containerId := tetragonProcess.Docker
filename := tetragonProcess.Binary
args := tetragonProcess.Arguments
nspid := msg.Unix.Process.NSPID
if option.Config.EnableK8s && containerId != "" {
podInfo = process.GetPodInfo(containerId, filename, args, nspid)
if podInfo == nil {
eventcache.CacheRetries(eventcache.PodInfo).Inc()
return eventcache.ErrFailedToGetPodInfo
}
}
// We can assume that event.internal != nil here since it's being set by AddExecEvent
// earlier in the code path. If this invariant ever changes in the future, we probably
// want to panic anyway to help us catch the bug faster. So no need to do a nil check
// here.
internal.AddPodInfo(podInfo)
// Check we have a parent with exception for pid 1, note we do this last because we want
// to ensure the podInfo and process are set before returning any errors.
if tetragonProcess.Pid.Value > 1 && !msg.RefCntDone[ParentRefCnt] {
parentId := tetragonProcess.ParentExecId
parent, err := process.Get(parentId)
if parent == nil {
eventcache.CacheRetries(eventcache.ParentInfo).Inc()
return err
}
parent.RefInc("parent")
ev.SetParent(parent.UnsafeGetProcess())
msg.RefCntDone[ParentRefCnt] = true
}
// Check if we have ancestors with exception for pid 1 and pid 2. Note that we pass
// tetragonProcess.ParentExecId to GetAncestorProcessesInternal function instead of
// tetragonProcess.ExecId, because function GetAncestorProcessesInternal returns all
// ancestors of the given process, including the immediate parent. So in order for us
// to collect ancestors beyond immediate parent, we need to pass immediate parent to
// GetAncestorProcessesInternal.
if option.Config.EnableAncestors["base"] && internal.NeededAncestors() && !msg.RefCntDone[AncestorsRefCnt] {
if ancestors, err := process.GetAncestorProcessesInternal(tetragonProcess.ParentExecId); err == nil {
var tetragonAncestors []*tetragon.Process
for _, ancestor := range ancestors {
tetragonAncestors = append(tetragonAncestors, ancestor.UnsafeGetProcess())
ancestor.RefInc("ancestor")
}
ev.SetAncestors(tetragonAncestors)
msg.RefCntDone[AncestorsRefCnt] = true
} else {
eventcache.CacheRetries(eventcache.AncestorsInfo).Inc()
return eventcache.ErrFailedToGetAncestorsInfo
}
}
// As of now pod information has been added, finalize the process event with extra fields
if err := msg.finalize(ev, internal, eventcache.FROM_EV_CACHE); err != nil {
// Propagate metric errors about finalizing the event
errormetrics.ErrorTotalInc(errormetrics.EventFinalizeProcessInfoFailed)
logger.GetLogger().WithFields(logrus.Fields{
"event.name": "Execve",
"event.process.pid": tetragonProcess.Pid.GetValue(),
"event.process.binary": filename,
"event.process.exec_id": tetragonProcess.GetExecId(),
"event.event_cache": "yes",
}).Debugf("ExecveEvent: failed to finalize process exec event")
// For ProcessExec event we do not fail let's return what we have even if it's not complete
// The eventmetrics will count further errors
}
// do we need to cleanup anything?
if cleanupEvent := msg.getCleanupEvent(); cleanupEvent != nil {
// Retry() is going to be executed in the cache loop handling function, but
// HandleMessage may enqueue something in the cache channel. To avoid a deadlock,
// execute the cleanup message handling in a separate goroutine.
go cleanupEvent.HandleMessage()
}
return nil
}
func (msg *MsgExecveEventUnix) HandleMessage() *tetragon.GetEventsResponse {
var res *tetragon.GetEventsResponse
msg.RefCntDone = [3]bool{true, false, false}
if e := GetProcessExec(msg, true); e != nil {
res = &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExec{ProcessExec: e},
Time: ktime.ToProto(msg.Unix.Msg.Common.Ktime),
}
}
return res
}
func (msg *MsgExecveEventUnix) Cast(o interface{}) notify.Message {
t := o.(processapi.MsgExecveEventUnix)
return &MsgExecveEventUnix{Unix: &t}
}
// finalize() is called to finalize Process of the event ExecveEvent
//
// The aim of this function is to finalize events by adding or updating some
// fields without propagating those into the process cache.
// These fields could be related to the specific calling event, example only
// for ProcessExec and not ProcessKprobe.
//
// There are two conditions when we could update the event:
// 1. When the event finalize handler return update set to true, for this a deep copy
// of the process was made, and the related fields added then the event.Process
// must be set with ev.SetProcess() to this returned copy.
// 2. The finalize() is called from the event cache retry in this
// case new information could have been added, so let's do another
// ev.SetProcess() call to update the process of the event.
func (msg *MsgExecveEventUnix) finalize(ev notify.Event, internal *process.ProcessInternal, cache int) error {
// This should never happen by this time
if ev.GetProcess() == nil {
return process.ErrProcessInfoMissing
}
proc, update := internal.UpdateExecOutsideCache(option.Config.EnableProcessCred)
// Update the event.Process entry:
// If update == true means we made a new copy of the process, added
// new information so let's update the event.
// If we did reach here from the event cache retries then maybe there is
// new information let's update the event again.
if update || cache == eventcache.FROM_EV_CACHE {
ev.SetProcess(proc)
}
return nil
}
type MsgCloneEventUnix struct {
processapi.MsgCloneEvent
}
func (msg *MsgCloneEventUnix) Notify() bool {
return false
}
func (msg *MsgCloneEventUnix) RetryInternal(_ notify.Event, _ uint64) (*process.ProcessInternal, error) {
return process.AddCloneEvent(&msg.MsgCloneEvent)
}
func (msg *MsgCloneEventUnix) Retry(internal *process.ProcessInternal, _ notify.Event) error {
tetragonProcess := internal.UnsafeGetProcess()
if option.Config.EnableK8s && tetragonProcess.Docker != "" && tetragonProcess.Pod == nil {
podInfo := process.GetPodInfo(tetragonProcess.Docker, tetragonProcess.Binary, tetragonProcess.Arguments, msg.NSPID)
if podInfo == nil {
eventcache.CacheRetries(eventcache.PodInfo).Inc()
return eventcache.ErrFailedToGetPodInfo
}
internal.AddPodInfo(podInfo)
}
if option.Config.EnableAncestors["base"] && internal.NeededAncestors() {
if ancestors, err := process.GetAncestorProcessesInternal(tetragonProcess.ParentExecId); err == nil {
for _, ancestor := range ancestors {
ancestor.RefInc("ancestor")
}
} else {
eventcache.CacheRetries(eventcache.AncestorsInfo).Inc()
return eventcache.ErrFailedToGetAncestorsInfo
}
}
return nil
}
func (msg *MsgCloneEventUnix) HandleMessage() *tetragon.GetEventsResponse {
var ancestors []*process.ProcessInternal
proc, _ := process.AddCloneEvent(&msg.MsgCloneEvent)
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() {
ancestors, _ = process.GetAncestorProcessesInternal(proc.UnsafeGetProcess().ParentExecId)
}
if ec := eventcache.Get(); ec != nil {
if proc == nil {
// adding to the cache due to missing parent
ec.Add(nil, nil, msg.Common.Ktime, msg.Ktime, msg)
return nil
}
parent, _ := process.Get(proc.UnsafeGetProcess().ParentExecId)
if ec.Needed(proc.UnsafeGetProcess()) ||
option.Config.EnableAncestors["base"] && ec.NeededAncestors(parent, ancestors) {
// adding to the cache due to missing pod info or ancestors
ec.Add(proc, nil, msg.Common.Ktime, msg.Ktime, msg)
return nil
}
}
if option.Config.EnableAncestors["base"] {
for _, ancestor := range ancestors {
ancestor.RefInc("ancestor")
}
}
return nil
}
func (msg *MsgCloneEventUnix) Cast(o interface{}) notify.Message {
t := o.(processapi.MsgCloneEvent)
return &MsgCloneEventUnix{MsgCloneEvent: t}
}
// GetProcessExit returns Exit protobuf message for a given process.
func GetProcessExit(event *MsgExitEventUnix) *tetragon.ProcessExit {
var tetragonProcess, tetragonParent *tetragon.Process
var tetragonAncestors []*tetragon.Process
var ancestors []*process.ProcessInternal
proc, parent := process.GetParentProcessInternal(event.ProcessKey.Pid, event.ProcessKey.Ktime)
if proc != nil {
tetragonProcess = proc.UnsafeGetProcess()
} else {
tetragonProcess = &tetragon.Process{
Pid: &wrapperspb.UInt32Value{Value: event.ProcessKey.Pid},
StartTime: ktime.ToProto(event.ProcessKey.Ktime),
}
}
if parent != nil {
tetragonParent = parent.UnsafeGetProcess()
}
// Set the ancestors only if --enable-ancestors flag includes 'base'.
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() {
// We don't care about an error here, because later we call ec.NeededAncestors,
// that will determine if we were successful in collecting all ancestors and,
// if we were not, the event will be added to the event cache for reprocessing.
// Even if we were not able to collect all ancestors, we will still export what
// we were able to collect in the event.
ancestors, _ = process.GetAncestorProcessesInternal(tetragonProcess.ParentExecId)
for _, ancestor := range ancestors {
tetragonAncestors = append(tetragonAncestors, ancestor.UnsafeGetProcess())
}
}
code := event.Info.Code >> 8
signal := readerexec.Signal(event.Info.Code & 0xFF)
if event.Info.Code&0x80 != 0 {
// Core dumped
signal = readerexec.Signal(event.Info.Code & 0x7F)
}
// Per thread tracking rules PID == TID.
//
// Exit events should have TID==PID at same time we want to correlate
// the {TID,PID} of the exit event with the {TID,PID} pair from the exec
// event. They must match because its how we link the exit event to the
// exec one.
//
// The exit event is constructed when looking up the process by its PID
// from user space cache, so we endup with the TID that was pushed
// into the process cache during clone or exec.
//
// Add extra logic to WARN on conditions where TID!=PID to aid debugging
// and catch this unexpected case. Typically this indicates a bug either
// in BPF or userspace caching logic. When this condition is encountered
// we warn about it, but for the exit event the TID of the cache process
// will be re-used.
//
// Check must be against event.Info.Tid so we cover all the cases of
// the tetragonProcess.Pid against BPF.
if tetragonProcess.Pid.GetValue() != event.Info.Tid {
logger.GetLogger().WithFields(logrus.Fields{
"event.name": "Exit",
"event.process.pid": event.ProcessKey.Pid,
"event.process.tid": event.Info.Tid,
"event.process.binary": tetragonProcess.Binary,
}).Warn("ExitEvent: process PID and TID mismatch")
errormetrics.ErrorTotalInc(errormetrics.ProcessPidTidMismatch)
}
tetragonEvent := &tetragon.ProcessExit{
Process: tetragonProcess,
Parent: tetragonParent,
Ancestors: tetragonAncestors,
Signal: signal,
Status: code,
Time: ktime.ToProto(event.Common.Ktime),
}
if tetragonProcess.Pid == nil {
eventcache.CacheErrors(eventcache.NilProcessPid, notify.EventType(tetragonEvent)).Inc()
return nil
}
if ec := eventcache.Get(); ec != nil &&
(ec.Needed(tetragonProcess) ||
(tetragonProcess.Pid.Value > 1 && ec.Needed(tetragonParent)) ||
(option.Config.EnableAncestors["base"] && ec.NeededAncestors(parent, ancestors))) {
ec.Add(nil, tetragonEvent, event.Common.Ktime, event.ProcessKey.Ktime, event)
return nil
}
if option.Config.EnableAncestors["base"] {
for _, ancestor := range ancestors {
ancestor.RefDec("ancestor")
}
}
if parent != nil {
parent.RefDec("parent")
}
if proc != nil {
proc.RefDec("process")
}
return tetragonEvent
}
type MsgExitEventUnix struct {
processapi.MsgExitEvent
RefCntDone [3]bool
}
func (msg *MsgExitEventUnix) Notify() bool {
return true
}
func (msg *MsgExitEventUnix) RetryInternal(ev notify.Event, timestamp uint64) (*process.ProcessInternal, error) {
proc, parent := process.GetParentProcessInternal(msg.ProcessKey.Pid, timestamp)
var err error
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() && !msg.RefCntDone[AncestorsRefCnt] {
if ancestors, perr := process.GetAncestorProcessesInternal(proc.UnsafeGetProcess().ParentExecId); perr == nil {
var tetragonAncestors []*tetragon.Process
for _, ancestor := range ancestors {
tetragonAncestors = append(tetragonAncestors, ancestor.UnsafeGetProcess())
ancestor.RefDec("ancestor")
}
ev.SetAncestors(tetragonAncestors)
msg.RefCntDone[AncestorsRefCnt] = true
} else {
eventcache.CacheRetries(eventcache.AncestorsInfo).Inc()
err = eventcache.ErrFailedToGetAncestorsInfo
}
}
if parent != nil {
ev.SetParent(parent.UnsafeGetProcess())
if !msg.RefCntDone[ParentRefCnt] {
parent.RefDec("parent")
msg.RefCntDone[ParentRefCnt] = true
}
} else {
eventcache.CacheRetries(eventcache.ParentInfo).Inc()
err = eventcache.ErrFailedToGetParentInfo
}
if proc != nil {
// Use cached version of the process
ev.SetProcess(proc.UnsafeGetProcess())
if !msg.RefCntDone[ProcessRefCnt] {
proc.RefDec("process")
msg.RefCntDone[ProcessRefCnt] = true
}
} else {
eventcache.CacheRetries(eventcache.ProcessInfo).Inc()
err = eventcache.ErrFailedToGetProcessInfo
}
if err == nil {
return proc, err
}
return nil, err
}
func (msg *MsgExitEventUnix) Retry(internal *process.ProcessInternal, ev notify.Event) error {
return eventcache.HandleGenericEvent(internal, ev, nil)
}
func (msg *MsgExitEventUnix) HandleMessage() *tetragon.GetEventsResponse {
var res *tetragon.GetEventsResponse
msg.RefCntDone = [3]bool{false, false, false}
e := GetProcessExit(msg)
if e != nil {
res = &tetragon.GetEventsResponse{
Event: &tetragon.GetEventsResponse_ProcessExit{ProcessExit: e},
Time: ktime.ToProto(msg.Common.Ktime),
}
}
return res
}
func (msg *MsgExitEventUnix) Cast(o interface{}) notify.Message {
t := o.(processapi.MsgExitEvent)
return &MsgExitEventUnix{MsgExitEvent: t}
}
type MsgProcessCleanupEventUnix struct {
PID uint32
Ktime uint64
RefCntDone [3]bool
}
func (msg *MsgProcessCleanupEventUnix) Notify() bool {
return false
}
func (msg *MsgProcessCleanupEventUnix) RetryInternal(_ notify.Event, timestamp uint64) (*process.ProcessInternal, error) {
proc, parent := process.GetParentProcessInternal(msg.PID, timestamp)
var err error
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() && !msg.RefCntDone[AncestorsRefCnt] {
if ancestors, perr := process.GetAncestorProcessesInternal(proc.UnsafeGetProcess().ParentExecId); perr == nil {
for _, ancestor := range ancestors {
ancestor.RefDec("ancestor")
}
msg.RefCntDone[AncestorsRefCnt] = true
} else {
eventcache.CacheRetries(eventcache.AncestorsInfo).Inc()
err = eventcache.ErrFailedToGetAncestorsInfo
}
}
if parent != nil {
if !msg.RefCntDone[ParentRefCnt] {
parent.RefDec("parent")
msg.RefCntDone[ParentRefCnt] = true
}
} else {
eventcache.CacheRetries(eventcache.ParentInfo).Inc()
err = eventcache.ErrFailedToGetParentInfo
}
if proc != nil {
if !msg.RefCntDone[ProcessRefCnt] {
proc.RefDec("process")
msg.RefCntDone[ProcessRefCnt] = true
}
} else {
eventcache.CacheRetries(eventcache.ProcessInfo).Inc()
err = eventcache.ErrFailedToGetProcessInfo
}
if err == nil {
return proc, err
}
return nil, err
}
func (msg *MsgProcessCleanupEventUnix) Retry(_ *process.ProcessInternal, _ notify.Event) error {
return nil
}
func (msg *MsgProcessCleanupEventUnix) HandleMessage() *tetragon.GetEventsResponse {
var ancestors []*process.ProcessInternal
msg.RefCntDone = [3]bool{false, false, false}
proc, parent := process.GetParentProcessInternal(msg.PID, msg.Ktime)
if option.Config.EnableAncestors["base"] && proc.NeededAncestors() {
ancestors, _ = process.GetAncestorProcessesInternal(proc.UnsafeGetProcess().ParentExecId)
}
if ec := eventcache.Get(); ec != nil &&
(proc == nil ||
parent == nil ||
option.Config.EnableAncestors["base"] && ec.NeededAncestors(parent, ancestors)) {
ec.Add(nil, nil, msg.Ktime, msg.Ktime, msg)
return nil
}
if option.Config.EnableAncestors["base"] {
for _, ancestor := range ancestors {
ancestor.RefDec("ancestor")
}
}
if parent != nil {
parent.RefDec("parent")
}
if proc != nil {
proc.RefDec("process")
}
return nil
}
func (msg *MsgProcessCleanupEventUnix) Cast(_ interface{}) notify.Message {
return &MsgProcessCleanupEventUnix{}
}