-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark.go
More file actions
1239 lines (1109 loc) · 34.5 KB
/
Copy pathmark.go
File metadata and controls
1239 lines (1109 loc) · 34.5 KB
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pmark
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
"golang.org/x/sys/unix"
)
const (
// ebpf-to-userspace ring event codes
eventFork = 1
eventExit = 2
eventExec = 3
// DefaultTombCollectionEvents bounds tombstone cleanup work by doing it
// after a fixed number of processed ring events instead of on every event.
DefaultTombCollectionEvents = 1024
// DefaultTombTTL is the grace period during which exited process identities
// remain in both userspace and kernel mirrors as tombstones.
DefaultTombTTL = time.Minute
)
var pinnedMapNames = []string{"processes", "events"}
// ProcessKey is the per-boot process-lifetime identity shared with BPF.
//
// TGID is the process ID. StartTime is /proc/<pid>/stat field 22 expressed in
// USER_HZ ticks since boot. The pair is used instead of TGID alone because PIDs
// can be reused after exit.
type ProcessKey = markProcessKey // public re-export from generated code
// ProcessValue is the effective mark state stored in the pinned process map.
//
// A missing map entry, a live value with HasMark false, and a Tombstone value
// all mean "unmarked" for policy purposes. Live no-mark entries are retained
// after a process had an effective mark once, allowing newer checker rules to
// remove a mark without losing the process lifetime record. Tombstones are
// retained for a short grace period after exit so late events for an old process
// lifetime do not revive stale marks. Tombstone collection is the only operation
// that physically deletes entries.
//
// The generated Inheritance field is true for an explicit/original mark chosen
// by Check and false for a mark inherited from a parent. HasMark gates whether
// Mark and Priority are currently meaningful and whether this value can be
// inherited. Generation records the checker generation that last reconciled the
// entry. When competing updates are merged, tombstones win first, higher
// generations win second, higher priorities win third, and timestamps order
// otherwise-equivalent values. Timestamp is in CLOCK_BOOTTIME nanoseconds and is
// also used to expire tombstones.
type ProcessValue = markProcessValue // public re-export from generated code
// ProcessInfo is the userspace-enriched process view passed to callbacks.
//
// During process-tree traversal it is read from /proc and normally includes
// PPID, Comm, Cmdline, and Exe. During fork and exit events the daemon first
// tries to refresh the data from /proc; if the process is already gone or the
// identity no longer matches, only Key, PPID, and Comm from the BPF event are
// guaranteed to be present. Cmdline and Exe may be empty in that case.
type ProcessInfo struct {
Key ProcessKey
PPID uint32
Comm string
Cmdline string
Exe string
}
// CheckFunc decides whether a process should receive an explicit mark.
//
// It is called while the daemon walks the current /proc tree before attaching
// BPF programs, again immediately after attach to cover the race window, and on
// each fork event for the child process. It is not called on exit events.
//
// Returning (priority, mark, true) creates or refreshes an explicit live mark
// for the supplied process in the current checker generation. Returning (_, _,
// false) leaves the process unmarked unless it can inherit a live HasMark value
// from its parent. If the process already had an entry, the daemon keeps a live
// HasMark=false entry for the rest of that process lifetime. CheckFunc should
// be quick and must not call back into the same Daemon; after Run starts event
// processing, a blocked CheckFunc blocks ring-buffer consumption.
type CheckFunc func(ProcessInfo) (int8, uint64, bool)
// KernelMark is the BPF program's view of a process mark at event time.
type KernelMark struct {
// HasMark reports whether BPF saw a live mark for the process.
HasMark bool
// Inherited reports whether the mark seen by BPF was inherited from a
// parent rather than explicitly selected by Check.
Inherited bool
// Mark is meaningful when HasMark is true.
Mark uint64
// Priority is meaningful when HasMark is true. Higher values win when
// userspace merges otherwise comparable process updates.
Priority int8
}
// ProcessEvent describes a process transition that produced an effective mark
// state worth reporting.
//
// Type is "fork", "exec", "exit", or "unknown". Fork and exec events are
// reported only when the process has an effective live mark after userspace
// reconciliation: a BPF mark, an existing userspace mirror entry, a new Check
// match, or fork inheritance from a marked parent. Unmarked fork and exec events
// are intentionally suppressed. Exit events are reported when BPF saw a live
// mark or userspace had a live mirror entry for the exiting process; the Value
// is a tombstone. Unknown events are reported for unexpected BPF event types and
// may have nil Value.
//
// Process contains the best process metadata available at callback time.
// ParentKey is set for fork events and nil otherwise. Kernel is the raw BPF
// mark state from the event, before userspace Check and mirror reconciliation.
// Value is the effective userspace value after reconciliation; it is non-nil
// for reported fork and exit events.
type ProcessEvent struct {
Type string
Key ProcessKey
ParentKey *ProcessKey
Kernel KernelMark
Process ProcessInfo
Value *ProcessValue
}
// ProcessUpdate is emitted after the userspace mirror and kernel map have been
// updated for a process key.
//
// Updates can come from initial traversal, fork handling, exec handling, exit
// tombstoning, or event reconciliation. They are not emitted for unchanged
// values, for suppressed unmarked events, or when old tombstones are physically
// deleted during periodic cleanup.
type ProcessUpdate struct {
Key ProcessKey
Value ProcessValue
}
// Callbacks contains optional hooks used by Daemon.
//
// All callbacks are invoked synchronously by daemon code. Check and
// ProcessUpdate can run during Run before it returns because initial process
// traversal may create marks. After Run starts the event loop, Check,
// ProcessEvent, ProcessUpdate, and Logf can run on the event goroutine.
// Implementations should avoid long blocking work and should not call Stop,
// Close, or other methods that wait for the daemon from inside a callback.
type Callbacks struct {
// Check selects explicit marks for processes. Nil means no process is
// explicitly marked by userspace, though inheritance from existing marks can
// still occur.
Check CheckFunc
// ProcessEvent observes fork, exec, exit, and unknown BPF events after
// userspace reconciliation. It is for transition logging or side effects
// that need event context such as ParentKey and Kernel.
ProcessEvent func(ProcessEvent)
// ProcessUpdate observes effective map state changes after the daemon has
// attempted to write them to the kernel map. It is for consumers that care
// about the current mark state rather than the transition that caused it.
ProcessUpdate func(ProcessUpdate)
// Logf receives recoverable daemon diagnostics such as parse errors,
// traversal failures, map sync failures, and tombstone cleanup failures.
Logf func(format string, args ...any)
}
// Daemon owns loaded eBPF objects, links, ring reader, and userspace mirror.
type Daemon struct {
objs markObjects
marker *marker
pinPath string
rd *ringbuf.Reader
forkLink link.Link
exitLink link.Link
execLink link.Link
done chan error
stopOnce sync.Once
closeOnce sync.Once
tombCollectionEvents uint64
tombTTL time.Duration
}
// ProcessMapState is a point-in-time view of the pinned process map plus procfs.
type ProcessMapState struct {
Alive int
Tombstones int
Latest uint64
Entries map[ProcessKey]ProcessValue
Procs []ProcessInfo
}
// NewDaemon prepares a daemon instance. Call Run to attach programs and start
// consuming events, and Stop to detach and close resources.
func NewDaemon(
pinPath string, callbacks Callbacks, tcev uint64, tttl time.Duration,
) (*Daemon, error) {
if err := os.MkdirAll(pinPath, 0o755); err != nil {
return nil, fmt.Errorf("create pin path %q: %w", pinPath, err)
}
if err := removePinnedMaps(pinPath, pinnedMapNames...); err != nil {
return nil, fmt.Errorf("remove old pinned maps: %w", err)
}
if err := rlimit.RemoveMemlock(); err != nil {
return nil, fmt.Errorf("remove memlock: %w", err)
}
var objs markObjects
opts := ebpf.CollectionOptions{
Maps: ebpf.MapOptions{PinPath: pinPath},
}
if err := loadMarkObjects(&objs, &opts); err != nil {
return nil, fmt.Errorf("load eBPF objects: %w", err)
}
check := callbacks.Check
if check == nil {
check = func(ProcessInfo) (int8, uint64, bool) { return 0, 0, false }
}
var tombCollectionEvents uint64 = DefaultTombCollectionEvents
if tcev != 0 {
tombCollectionEvents = tcev
}
tombTTL := DefaultTombTTL
if tttl != 0 {
tombTTL = tttl
}
d := &Daemon{
objs: objs,
pinPath: pinPath,
done: make(chan error, 1),
tombCollectionEvents: tombCollectionEvents,
tombTTL: tombTTL,
}
d.marker = &marker{
processes: objs.Processes,
mirror: make(map[ProcessKey]ProcessValue),
check: check,
generation: 1,
callbacks: callbacks,
tombTTL: tombTTL,
}
return d, nil
}
func (d *Daemon) Run() error {
if d.rd != nil {
return nil
}
if err := d.marker.traverseProcessTree(); err != nil {
d.marker.logf("initial process traversal before attach: %v", err)
}
forkLink, err := link.AttachTracing(link.TracingOptions{
Program: d.objs.HandleSchedProcessFork,
AttachType: ebpf.AttachTraceRawTp,
})
if err != nil {
return fmt.Errorf("attach sched_process_fork: %w", err)
}
d.forkLink = forkLink
exitLink, err := link.AttachTracing(link.TracingOptions{
Program: d.objs.HandleSchedProcessExit,
AttachType: ebpf.AttachTraceRawTp,
})
if err != nil {
_ = d.forkLink.Close()
d.forkLink = nil
return fmt.Errorf("attach sched_process_exit: %w", err)
}
d.exitLink = exitLink
execLink, err := link.AttachTracing(link.TracingOptions{
Program: d.objs.HandleSchedProcessExec,
AttachType: ebpf.AttachTraceRawTp,
})
if err != nil {
_ = d.exitLink.Close()
_ = d.forkLink.Close()
d.exitLink = nil
d.forkLink = nil
return fmt.Errorf("attach sched_process_exec: %w", err)
}
d.execLink = execLink
if err := d.marker.traverseProcessTree(); err != nil {
d.marker.logf("initial process traversal after attach: %v", err)
}
rd, err := ringbuf.NewReader(d.objs.Events)
if err != nil {
_ = d.execLink.Close()
_ = d.exitLink.Close()
_ = d.forkLink.Close()
d.execLink = nil
d.exitLink = nil
d.forkLink = nil
return fmt.Errorf("open ring buffer: %w", err)
}
d.rd = rd
go d.runEvents()
return nil
}
func (d *Daemon) Done() <-chan error {
return d.done
}
// SetChecker installs a new checker, increments the checker generation,
// immediately traverses /proc with the new checker, and returns the resulting
// generation.
//
// Processes matched by the new checker receive live HasMark=true entries at the
// new generation. Existing live entries that no longer match and cannot inherit
// a live mark are updated to HasMark=false at the new generation and kept until
// that process exits and its tombstone is collected.
func (d *Daemon) SetChecker(check CheckFunc) (uint64, error) {
if check == nil {
check = func(ProcessInfo) (int8, uint64, bool) { return 0, 0, false }
}
return d.marker.setChecker(check)
}
// SetProcessMark explicitly sets a live mark for one process lifetime.
//
// The mark is applied with the current checker generation and the same merge,
// kernel-sync, and ProcessUpdate behavior used for marks found in BPF events or
// returned by Check. Higher-priority, newer-generation, or tombstone values that
// already exist may still win according to the normal ProcessValue merge rules.
func (d *Daemon) SetProcessMark(key ProcessKey, priority int8, mark uint64) {
d.marker.setProcessMark(key, priority, mark)
}
// ForceProcessTraversal immediately traverses /proc with the current checker.
//
// This is useful when a caller needs to refresh process state outside the
// daemon's normal startup, event, and cleanup ordering. It does not advance the
// checker generation.
func (d *Daemon) ForceProcessTraversal() error {
return d.marker.traverseProcessTree()
}
// ForceBumpGeneration increments the checker generation, immediately traverses
// /proc with the current checker, and returns the resulting generation.
//
// It has the same reconciliation effect as SetChecker with the existing checker,
// but does not replace the checker function. Use it when the checker closes over
// proving state that changed internally and all live processes need re-checking.
func (d *Daemon) ForceBumpGeneration() (uint64, error) {
return d.marker.bumpGeneration()
}
// CurrentGeneration returns the active checker generation.
func (d *Daemon) CurrentGeneration() uint64 {
return d.marker.currentGeneration()
}
// UpdateHooks replaces the non-check callbacks used by the daemon.
//
// The Check field is intentionally ignored; use SetChecker so checker changes
// always advance the generation and trigger a full traversal. Existing in-flight
// callbacks are synchronous, so this method is best called from outside daemon
// callbacks.
func (d *Daemon) UpdateHooks(callbacks Callbacks) {
d.marker.updateHooks(callbacks)
}
func (d *Daemon) Stop() error {
var err error
d.stopOnce.Do(func() {
if d.rd != nil {
err = d.rd.Close()
}
if doneErr := <-d.done; doneErr != nil && err == nil {
err = doneErr
}
d.marker.replayProcessUpdates(func(_ ProcessKey, value ProcessValue) (ProcessValue, bool) {
value.HasMark = false
return value, true
})
})
return err
}
func (d *Daemon) Close() error {
var err error
d.closeOnce.Do(func() {
if d.rd != nil {
err = errors.Join(err, d.rd.Close())
}
if d.exitLink != nil {
err = errors.Join(err, d.exitLink.Close())
}
if d.execLink != nil {
err = errors.Join(err, d.execLink.Close())
}
if d.forkLink != nil {
err = errors.Join(err, d.forkLink.Close())
}
err = errors.Join(err, d.objs.Close())
if d.pinPath != "" {
err = errors.Join(err, removePinnedMaps(d.pinPath, pinnedMapNames...))
}
})
return err
}
func (d *Daemon) runEvents() {
var err error
defer func() {
err = errors.Join(err, d.Close())
d.done <- err
close(d.done)
}()
for {
record, readErr := d.rd.Read()
if readErr != nil {
if errors.Is(readErr, ringbuf.ErrClosed) {
return
}
d.marker.logf("reading ring buffer: %v", readErr)
continue
}
var event markEvent
if readErr := binary.Read(bytes.NewReader(record.RawSample), binary.LittleEndian, &event); readErr != nil {
d.marker.logf("parsing event: %v", readErr)
continue
}
d.marker.handleEvent(event)
d.marker.eventCounter++
if d.marker.eventCounter >= d.tombCollectionEvents {
d.marker.eventCounter = 0
if collectErr := d.marker.collectTombstones(); collectErr != nil {
d.marker.logf("collecting tombstones: %v", collectErr)
}
}
}
}
// GrabProcessMapState reads the pinned processes map under pinPath.
func GrabProcessMapState(pinPath string) (ProcessMapState, error) {
processes, err := ebpf.LoadPinnedMap(filepath.Join(pinPath, "processes"), nil)
if err != nil {
return ProcessMapState{}, fmt.Errorf("open pinned processes map: %w", err)
}
defer processes.Close() //nolint:errcheck
return readProcessMapState(processes)
}
// marker owns the userspace mirror and all policy decisions around it.
type marker struct {
mu sync.Mutex
processes *ebpf.Map
mirror map[ProcessKey]ProcessValue
check CheckFunc
generation uint64
callbacks Callbacks
eventCounter uint64
tombTTL time.Duration
}
func readProcessMapState(processes *ebpf.Map) (ProcessMapState, error) {
snapshot := ProcessMapState{
Entries: make(map[ProcessKey]ProcessValue),
}
var key ProcessKey
var value ProcessValue
iter := processes.Iterate()
for iter.Next(&key, &value) {
snapshot.Entries[key] = value
if value.Tombstone {
snapshot.Tombstones++
} else {
snapshot.Alive++
}
if value.Timestamp > snapshot.Latest {
snapshot.Latest = value.Timestamp
}
}
if err := iter.Err(); err != nil {
return ProcessMapState{}, fmt.Errorf("iterate process map: %w", err)
}
procs, err := listProcesses()
if err != nil {
return ProcessMapState{}, fmt.Errorf("list processes: %w", err)
}
snapshot.Procs = procs
return snapshot, nil
}
// ListProcesses returns the current /proc process tree using the same process
// identity parsing as the daemon.
func ListProcesses() ([]ProcessInfo, error) {
return listProcesses()
}
func removePinnedMaps(pinPath string, names ...string) error {
/*
* Pinned maps survive process restarts. That is useful once the ABI is
* stable, but during development an old pinned map with the previous value
* layout makes LoadAndAssign fail before userspace can repair it. Startup
* unlinks the known pins so the freshly generated map definitions are used.
*/
for _, name := range names {
path := filepath.Join(pinPath, name)
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("%s: %w", path, err)
}
}
return nil
}
func (m *marker) handleEvent(event markEvent) {
m.mu.Lock()
defer m.mu.Unlock()
info := procInfoFromEvent(event)
switch event.Type {
case eventFork:
var value ProcessValue
hasValue := false
/*
* BPF has already attempted inheritance before emitting the event. If it
* found no mark, userspace checks its mirror because another userspace
* path may have seeded the child during the race window. Then the normal
* check callback can still make the child explicit.
*/
if event.HasMark && event.Value.HasMark {
value = event.Value
hasValue = true
m.upsertProcess(event.Key, value)
} else if existing, ok := m.mirror[event.Key]; ok && !existing.Tombstone && existing.HasMark {
value = existing
hasValue = true
m.syncKernel(event.Key, value)
}
if priority, mark, ok := m.check(info); ok {
value = m.newProcessValue(priority, mark, true)
hasValue = true
m.upsertProcess(event.Key, value)
} else if parentValue, ok := m.mirror[event.ParentKey]; ok && canInherit(parentValue) {
inherited := parentValue
inherited.Tombstone = false
inherited.HasMark = true
inherited.Inheritance = false
inherited.Generation = m.generation
inherited.Timestamp = nowBootNS()
value = inherited
hasValue = true
m.upsertProcess(event.Key, inherited)
} else if hasValue {
value = m.newNoMarkProcessValue(value)
m.upsertProcess(event.Key, value)
} else if existing, ok := m.mirror[event.Key]; ok && !existing.Tombstone {
value = m.newNoMarkProcessValue(existing)
hasValue = true
m.upsertProcess(event.Key, value)
}
if !hasValue || !value.HasMark {
return
}
m.onProcessEvent(ProcessEvent{
Type: "fork",
Key: event.Key,
ParentKey: ptrProcessKey(event.ParentKey),
Kernel: KernelMark{
HasMark: event.HasMark,
Inherited: event.HasMark && !event.Value.Inheritance,
Mark: event.Value.Mark,
Priority: event.Value.Priority,
},
Process: info,
Value: ptrProcessValue(value),
})
case eventExit:
/*
* Exit marks become tombstones rather than deletes. Tombstones are kept
* in both mirrors until periodic collection so late events for the old
* process lifetime do not accidentally revive stale marks.
*/
value := event.Value
old, ok := m.mirror[event.Key]
logExit := event.HasMark || (ok && !old.Tombstone && old.HasMark)
if ok {
value = old
}
value.Tombstone = true
value.Timestamp = maxU64(value.Timestamp, nowBootNS())
m.upsertProcessWithLog(event.Key, value, logExit)
if !logExit {
return
}
m.onProcessEvent(ProcessEvent{
Type: "exit",
Key: event.Key,
Kernel: KernelMark{
HasMark: event.HasMark,
Inherited: event.HasMark && !event.Value.Inheritance,
Mark: event.Value.Mark,
Priority: event.Value.Priority,
},
Process: info,
Value: ptrProcessValue(value),
})
case eventExec:
value := event.Value
hasValue := false
if existing, ok := m.mirror[event.Key]; ok && !existing.Tombstone {
value = existing
hasValue = true
} else if event.HasMark && event.Value.HasMark {
value = event.Value
hasValue = true
}
if priority, mark, ok := m.check(info); ok {
value = m.newProcessValue(priority, mark, true)
hasValue = true
m.upsertProcess(event.Key, value)
} else if hasValue {
value = m.newNoMarkProcessValue(value)
m.upsertProcess(event.Key, value)
}
if !hasValue || !value.HasMark {
return
}
m.onProcessEvent(ProcessEvent{
Type: "exec",
Key: event.Key,
Kernel: KernelMark{
HasMark: event.HasMark,
Inherited: event.HasMark && !event.Value.Inheritance,
Mark: event.Value.Mark,
Priority: event.Value.Priority,
},
Process: info,
Value: ptrProcessValue(value),
})
default:
m.onProcessEvent(ProcessEvent{
Type: "unknown",
Key: event.Key,
Process: info,
Kernel: KernelMark{
HasMark: event.HasMark,
Inherited: event.HasMark && !event.Value.Inheritance,
Mark: event.Value.Mark,
Priority: event.Value.Priority,
},
})
}
}
func (m *marker) traverseProcessTree() error {
m.mu.Lock()
defer m.mu.Unlock()
return m.traverseProcessTreeLocked()
}
func (m *marker) traverseProcessTreeLocked() error {
procs, err := listProcesses()
if err != nil {
return err
}
byParent := make(map[uint32][]ProcessInfo)
byPID := make(map[uint32]ProcessInfo)
for _, proc := range procs {
byPID[proc.Key.Tgid] = proc
byParent[proc.PPID] = append(byParent[proc.PPID], proc)
}
for ppid := range byParent {
sort.Slice(byParent[ppid], func(i, j int) bool {
return byParent[ppid][i].Key.Tgid < byParent[ppid][j].Key.Tgid
})
}
visited := make(map[uint32]bool, len(procs))
var walk func(ProcessInfo, *ProcessValue)
walk = func(info ProcessInfo, parentValue *ProcessValue) {
if visited[info.Key.Tgid] {
return
}
visited[info.Key.Tgid] = true
var current *ProcessValue
if priority, mark, ok := m.check(info); ok {
value := m.newProcessValue(priority, mark, true)
m.upsertProcess(info.Key, value)
current = &value
} else if parentValue != nil && m.canInheritCurrentGeneration(*parentValue) {
value := *parentValue
value.Tombstone = false
value.HasMark = true
value.Inheritance = false
value.Generation = m.generation
value.Timestamp = nowBootNS()
m.upsertProcess(info.Key, value)
current = &value
} else if existing, ok := m.mirror[info.Key]; ok && !existing.Tombstone {
value := m.newNoMarkProcessValue(existing)
m.upsertProcess(info.Key, value)
current = &value
}
for _, child := range byParent[info.Key.Tgid] {
walk(child, current)
}
}
for _, root := range byParent[0] {
walk(root, nil)
}
for _, proc := range procs {
if !visited[proc.Key.Tgid] {
var parentValue *ProcessValue
if parent, ok := byPID[proc.PPID]; ok {
if value, marked := m.mirror[parent.Key]; marked && m.canInheritCurrentGeneration(value) {
parentValue = &value
}
}
walk(proc, parentValue)
}
}
liveMarked := 0
for _, value := range m.mirror {
if !value.Tombstone && value.HasMark {
liveMarked++
}
}
m.logf("process traversal complete: procs=%d mirror_entries=%d live_marked=%d", len(procs), len(m.mirror), liveMarked)
return nil
}
func (m *marker) setChecker(check CheckFunc) (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.check = check
return m.bumpGenerationLocked()
}
func (m *marker) setProcessMark(key ProcessKey, priority int8, mark uint64) {
m.mu.Lock()
defer m.mu.Unlock()
m.upsertProcess(key, m.newProcessValue(priority, mark, true))
}
func (m *marker) bumpGeneration() (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.bumpGenerationLocked()
}
func (m *marker) bumpGenerationLocked() (uint64, error) {
m.generation++
if m.generation == 0 {
m.generation = 1
}
return m.generation, m.traverseProcessTreeLocked()
}
func (m *marker) currentGeneration() uint64 {
m.mu.Lock()
defer m.mu.Unlock()
return m.generation
}
func (m *marker) updateHooks(callbacks Callbacks) {
m.mu.Lock()
defer m.mu.Unlock()
m.callbacks.ProcessEvent = callbacks.ProcessEvent
m.callbacks.ProcessUpdate = callbacks.ProcessUpdate
m.callbacks.Logf = callbacks.Logf
m.replayProcessUpdatesLocked(func(_ ProcessKey, value ProcessValue) (ProcessValue, bool) {
return value, !value.Tombstone
})
}
func (m *marker) upsertProcess(key ProcessKey, next ProcessValue) {
m.upsertProcessWithLog(key, next, true)
}
func (m *marker) upsertProcessWithLog(key ProcessKey, next ProcessValue, logUpdate bool) {
if old, ok := m.mirror[key]; ok {
next = preferProcessValue(old, next)
if old == next {
return
}
}
m.mirror[key] = next
m.syncKernel(key, next)
if logUpdate {
m.onProcessUpdate(key, next)
}
}
func preferProcessValue(old, next ProcessValue) ProcessValue {
/*
* Merge order is intentionally independent of event source. Tombstones win
* over live values first. For the same tombstone class, newer checker
* generations win over older generations so stale checker decisions cannot
* overwrite on-the-fly checker updates. Higher priorities win next.
* Explicit marks then win over inherited marks, and newer timestamps win
* when the state class is
* otherwise the same.
*/
if old.Tombstone != next.Tombstone {
if old.Tombstone {
return old
}
return next
}
if old.Generation != next.Generation {
if old.Generation > next.Generation {
return old
}
return next
}
if old.Priority != next.Priority {
if old.Priority > next.Priority {
return old
}
return next
}
if old.Inheritance != next.Inheritance {
if old.Inheritance {
return old
}
return next
}
if old.Timestamp > next.Timestamp {
return old
}
return next
}
func (m *marker) syncKernel(key ProcessKey, value ProcessValue) {
if m.processes == nil {
return
}
if err := m.processes.Put(key, value); err != nil {
m.logf("syncing kernel process map pid=%d start_time=%d: %v", key.Tgid, key.StartTime, err)
}
}
func (m *marker) onProcessUpdate(key ProcessKey, value ProcessValue) {
if m.callbacks.ProcessUpdate != nil {
m.callbacks.ProcessUpdate(ProcessUpdate{Key: key, Value: value})
}
}
func (m *marker) replayProcessUpdates(selectValue func(ProcessKey, ProcessValue) (ProcessValue, bool)) {
m.mu.Lock()
defer m.mu.Unlock()
m.replayProcessUpdatesLocked(selectValue)
}
func (m *marker) replayProcessUpdatesLocked(selectValue func(ProcessKey, ProcessValue) (ProcessValue, bool)) {
if m.callbacks.ProcessUpdate == nil {
return
}
for key, value := range m.mirror {
if updateValue, ok := selectValue(key, value); ok {
m.callbacks.ProcessUpdate(ProcessUpdate{Key: key, Value: updateValue})
}
}
}
func (m *marker) collectTombstones() error {
m.mu.Lock()
defer m.mu.Unlock()
cutoff := nowBootNS() - uint64(m.tombTTL.Nanoseconds())
keys := make([]ProcessKey, 0)
for key, value := range m.mirror {
if value.Tombstone && value.Timestamp < cutoff {
keys = append(keys, key)
}
}
if len(keys) == 0 {
return m.regularCleanupLocked()
}
for _, key := range keys {
delete(m.mirror, key)
}
if _, err := m.processes.BatchDelete(keys, nil); err != nil {
for _, key := range keys {
if deleteErr := m.processes.Delete(key); deleteErr != nil && !errors.Is(deleteErr, ebpf.ErrKeyNotExist) {
return fmt.Errorf("batch delete failed with %v; fallback delete pid=%d start_time=%d: %w", err, key.Tgid, key.StartTime, deleteErr)
}
}
}
m.logf("collected tombstones: count=%d", len(keys))
return m.regularCleanupLocked()
}
func (m *marker) regularCleanupLocked() error {
if err := m.tombstoneDeadProcessesLocked(); err != nil {
return err
}
if m.hasExpiredLiveGenerationLocked() {
m.logf("expired checker generation found; rerunning process traversal")
return m.traverseProcessTreeLocked()
}
return nil
}
func (m *marker) tombstoneDeadProcessesLocked() error {
dead := make([]ProcessKey, 0)
for key, value := range m.mirror {
if value.Tombstone {
continue
}
if !procMatchesKey(key) {
dead = append(dead, key)
}
}
if len(dead) == 0 {
return nil
}
now := nowBootNS()
for _, key := range dead {
value := m.mirror[key]
value.Tombstone = true
value.Timestamp = maxU64(value.Timestamp, now)
m.upsertProcess(key, value)
}
m.logf("tombstoned dead processes: count=%d", len(dead))
return nil
}
func (m *marker) hasExpiredLiveGenerationLocked() bool {
for _, value := range m.mirror {
if !value.Tombstone && value.Generation < m.generation {
return true
}
}
return false
}
func (m *marker) onProcessEvent(event ProcessEvent) {
if m.callbacks.ProcessEvent != nil {
m.callbacks.ProcessEvent(event)
}
}
func (m *marker) logf(format string, args ...any) {
if m.callbacks.Logf != nil {
m.callbacks.Logf(format, args...)
}
}
func ptrProcessKey(key ProcessKey) *ProcessKey {