Skip to content

Commit c8e2c7b

Browse files
committed
selected by enum
1 parent 6e508bb commit c8e2c7b

2 files changed

Lines changed: 56 additions & 40 deletions

File tree

pkg/ebpf/tracee.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func (t *Tracee) initTailCall(tailCall events.TailCall) error {
578578
// derived and the corresponding function to derive into that Event.
579579
func (t *Tracee) initDerivationTable() error {
580580
shouldEmit := func(id events.ID) func() bool {
581-
return func() bool { return t.policyManager.IsEventEmitted(id) }
581+
return func() bool { return t.policyManager.ShouldEmitEvent(id) }
582582
}
583583
symbolsCollisions := derive.SymbolsCollision(t.contSymbolsLoader, t.policyManager)
584584

pkg/policy/policy_manager.go

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,22 @@ type EventRules struct {
5353
containerFilteredRules uint64 // Bitmap to track container-filtered rules
5454
}
5555

56+
type RuleSelectionType int
57+
58+
const (
59+
NotSelected RuleSelectionType = iota
60+
SelectedByUser
61+
SelectedByDependency
62+
SelectedByBootstrap
63+
)
64+
5665
// EventRule represents a single rule within an event's rule set.
5766
type EventRule struct {
58-
ID uint8 // Unique ID of the rule within the event (0-63) - used for bitmap position
59-
Data *RuleData // Data associated with the rule
60-
Policy *Policy // Reference to the policy where the rule was defined
61-
Emit bool // Flag to indicate whether the event should be emitted or not
62-
IsDependencyRule bool // Flag to indicate that this rule is a dependency rule
63-
DerivedRuleID uint8 // ID of the rule in derived event that caused this dependency rule
67+
ID uint8 // Unique ID of the rule within the event (0-63) - used for bitmap position
68+
Data *RuleData // Data associated with the rule
69+
Policy *Policy // Reference to the policy where the rule was defined
70+
SelectionType RuleSelectionType // How the rule was selected: by user, by dependency, or by bootstrap policy
71+
DerivedRuleID uint8 // For dependency rules, ID of the rule that caused the dependency
6472
}
6573

6674
func NewManager(
@@ -312,6 +320,10 @@ func (pm *PolicyManager) AddPolicy(policy *Policy, opts ...AddPolicyOption) erro
312320

313321
// RemovePolicy removes a policy from the PolicyManager.
314322
func (pm *PolicyManager) RemovePolicy(policyName string) error {
323+
if pm.bootstrapPolicy != nil && policyName == pm.bootstrapPolicy.Name {
324+
return errfmt.Errorf("cannot remove bootstrap policy")
325+
}
326+
315327
pm.mu.Lock()
316328
defer pm.mu.Unlock()
317329

@@ -387,22 +399,22 @@ func deepCopyEventRules(original EventRules) EventRules {
387399
// Deep copy Rules
388400
for i, rule := range original.Rules {
389401
copied.Rules[i] = &EventRule{
390-
ID: rule.ID,
391-
Data: rule.Data, // Data pointers can be shared
392-
Policy: rule.Policy, // Policy pointers can be shared
393-
Emit: rule.Emit,
394-
IsDependencyRule: rule.IsDependencyRule,
402+
ID: rule.ID,
403+
Data: rule.Data, // Data pointers can be shared
404+
Policy: rule.Policy, // Policy pointers can be shared
405+
SelectionType: rule.SelectionType,
406+
DerivedRuleID: rule.DerivedRuleID,
395407
}
396408
}
397409

398410
// Deep copy UserlandRules
399411
for i, rule := range original.UserlandRules {
400412
copied.UserlandRules[i] = &EventRule{
401-
ID: rule.ID,
402-
Data: rule.Data, // Data pointers can be shared
403-
Policy: rule.Policy, // Policy pointers can be shared
404-
Emit: rule.Emit,
405-
IsDependencyRule: rule.IsDependencyRule,
413+
ID: rule.ID,
414+
Data: rule.Data, // Data pointers can be shared
415+
Policy: rule.Policy, // Policy pointers can be shared
416+
SelectionType: rule.SelectionType,
417+
DerivedRuleID: rule.DerivedRuleID,
406418
}
407419
}
408420

@@ -443,7 +455,7 @@ func (pm *PolicyManager) updateRulesForEvent(eventID events.ID, tempRules map[ev
443455

444456
// Save existing dependency rules (created by rules with event that depend on this event)
445457
for _, rule := range existingEventRules.Rules {
446-
if rule.IsDependencyRule {
458+
if rule.SelectionType == SelectedByDependency {
447459
existingDepRules = append(existingDepRules, rule)
448460
}
449461
}
@@ -468,10 +480,14 @@ func (pm *PolicyManager) updateRulesForEvent(eventID events.ID, tempRules map[ev
468480
}
469481

470482
rule := &EventRule{
471-
ID: ruleIDCounter,
472-
Data: &ruleData,
473-
Policy: policy,
474-
Emit: policy != pm.bootstrapPolicy,
483+
ID: ruleIDCounter,
484+
Data: &ruleData,
485+
Policy: policy,
486+
SelectionType: SelectedByUser,
487+
}
488+
489+
if policy == pm.bootstrapPolicy {
490+
rule.SelectionType = SelectedByBootstrap
475491
}
476492

477493
rules = append(rules, rule)
@@ -549,26 +565,25 @@ func (pm *PolicyManager) addTransitiveDependencyRules(
549565
}
550566

551567
// Check if dependency rule already exists
552-
isDuplicate := false
568+
dependencyRuleExists := false
553569
for _, existingRule := range eventRules.Rules {
554-
if existingRule.IsDependencyRule &&
570+
if existingRule.SelectionType == SelectedByDependency &&
555571
existingRule.Policy == parentRule.Policy &&
556572
existingRule.Data == parentRule.Data {
557-
isDuplicate = true
573+
dependencyRuleExists = true
558574
break
559575
}
560576
}
561577

562-
if !isDuplicate {
578+
if !dependencyRuleExists {
563579
// Create dependency rule using parent's data and policy context
564580
// This allows tracking which rule/policy caused this dependency
565581
rule := &EventRule{
566-
ID: eventRules.rulesCount,
567-
Data: parentRule.Data,
568-
Policy: parentRule.Policy,
569-
Emit: false,
570-
IsDependencyRule: true,
571-
DerivedRuleID: parentRule.ID,
582+
ID: eventRules.rulesCount,
583+
Data: parentRule.Data,
584+
Policy: parentRule.Policy,
585+
SelectionType: SelectedByDependency,
586+
DerivedRuleID: parentRule.ID,
572587
}
573588

574589
eventRules.Rules = append(eventRules.Rules, rule)
@@ -741,7 +756,7 @@ func (pm *PolicyManager) GetMatchedRulesInfo(eventID events.ID, matchedRuleIDsBi
741756
continue
742757
}
743758

744-
if rule.Emit {
759+
if rule.SelectionType == SelectedByUser {
745760
matchedPolicyNames = append(matchedPolicyNames, rule.Policy.Name)
746761
utils.SetBit(&matchedRulesRes, uint(rule.ID))
747762
}
@@ -771,7 +786,7 @@ func (pm *PolicyManager) GetDerivedEventMatchedRules(
771786
}
772787

773788
baseRule, ok := baseEventRules.ruleIDToEventRule[ruleID]
774-
if !ok || !baseRule.IsDependencyRule {
789+
if !ok || baseRule.SelectionType != SelectedByDependency {
775790
continue
776791
}
777792

@@ -842,9 +857,10 @@ func (pm *PolicyManager) IsEventSelected(eventID events.ID) bool {
842857
return ok
843858
}
844859

845-
// IsEventEmitted checks if an event has at least one rule with the Emit flag set to true,
846-
// indicating that the event was explicitly selected by a policy and should be emitted.
847-
func (pm *PolicyManager) IsEventEmitted(eventID events.ID) bool {
860+
// ShouldEmitEvent checks if an event has at least one rule that was explicitly
861+
// selected by a user (not a dependency or bootstrap rule), indicating that the event
862+
// should be emitted.
863+
func (pm *PolicyManager) ShouldEmitEvent(eventID events.ID) bool {
848864
pm.mu.RLock()
849865
defer pm.mu.RUnlock()
850866

@@ -854,12 +870,12 @@ func (pm *PolicyManager) IsEventEmitted(eventID events.ID) bool {
854870
}
855871

856872
for _, rule := range eventRules.Rules {
857-
if rule.Emit {
858-
return true // Found at least one rule with Emit set to true
873+
if rule.SelectionType == SelectedByUser {
874+
return true // Found at least one rule explicitly selected by the user
859875
}
860876
}
861877

862-
return false // No rules have Emit set to true
878+
return false // No rules were explicitly selected by the user
863879
}
864880

865881
// GetAllMatchedRulesBitmap returns a bitmap where all bits corresponding to

0 commit comments

Comments
 (0)