Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 7.59.x] [CWS] Reset rule disarmers only after a new ruleset is loaded #30045

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/security/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type PlatformProbe interface {
DumpDiscarders() (string, error)
FlushDiscarders() error
ApplyRuleSet(_ *rules.RuleSet) (*kfilters.ApplyRuleSetReport, error)
OnNewRuleSetLoaded(_ *rules.RuleSet)
OnNewDiscarder(_ *rules.RuleSet, _ *model.Event, _ eval.Field, _ eval.EventType)
HandleActions(_ *eval.Context, _ *rules.Rule)
NewEvent() *model.Event
Expand Down Expand Up @@ -229,10 +230,15 @@ func (p *Probe) FlushDiscarders() error {

// ApplyRuleSet setup the probes for the provided set of rules and returns the policy report.
func (p *Probe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetReport, error) {
return p.PlatformProbe.ApplyRuleSet(rs)
}

// OnNewRuleSetLoaded resets statistics and states once a new rule set is loaded
func (p *Probe) OnNewRuleSetLoaded(rs *rules.RuleSet) {
p.ruleActionStatsLock.Lock()
clear(p.ruleActionStats)
p.ruleActionStatsLock.Unlock()
return p.PlatformProbe.ApplyRuleSet(rs)
p.PlatformProbe.OnNewRuleSetLoaded(rs)
}

// Snapshot runs the different snapshot functions of the resolvers that
Expand Down
7 changes: 5 additions & 2 deletions pkg/security/probe/probe_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,6 @@ func (p *EBPFProbe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetRepor
// activity dump & security profiles
needRawSyscalls := p.isNeededForActivityDump(model.SyscallsEventType.String())

p.processKiller.Apply(rs)

// kill action
if p.config.RuntimeSecurity.EnforcementEnabled && isKillActionPresent(rs) {
if !p.config.RuntimeSecurity.EnforcementRawSyscallEnabled {
Expand Down Expand Up @@ -1640,6 +1638,11 @@ func (p *EBPFProbe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetRepor
return ars, nil
}

// OnNewRuleSetLoaded resets statistics and states once a new rule set is loaded
func (p *EBPFProbe) OnNewRuleSetLoaded(rs *rules.RuleSet) {
p.processKiller.Reset(rs)
}

// NewEvent returns a new event
func (p *EBPFProbe) NewEvent() *model.Event {
return NewEBPFEvent(p.fieldHandlers)
Expand Down
8 changes: 6 additions & 2 deletions pkg/security/probe/probe_ebpfless.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,15 @@ func (p *EBPFLessProbe) FlushDiscarders() error {
}

// ApplyRuleSet applies the new ruleset
func (p *EBPFLessProbe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetReport, error) {
p.processKiller.Apply(rs)
func (p *EBPFLessProbe) ApplyRuleSet(_ *rules.RuleSet) (*kfilters.ApplyRuleSetReport, error) {
return &kfilters.ApplyRuleSetReport{}, nil
}

// OnNewRuleSetLoaded resets statistics and states once a new rule set is loaded
func (p *EBPFLessProbe) OnNewRuleSetLoaded(rs *rules.RuleSet) {
p.processKiller.Reset(rs)
}

// HandleActions handles the rule actions
func (p *EBPFLessProbe) HandleActions(ctx *eval.Context, rule *rules.Rule) {
ev := ctx.Event.(*model.Event)
Expand Down
4 changes: 4 additions & 0 deletions pkg/security/probe/probe_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (p *Probe) ApplyRuleSet(_ *rules.RuleSet) (*kfilters.ApplyRuleSetReport, er
return nil, nil
}

// OnNewRuleSetLoaded resets statistics and states once a new rule set is loaded
func (p *Probe) OnNewRuleSetLoaded(_ *rules.RuleSet) {
}

// OnNewDiscarder is called when a new discarder is found. We currently don't generate discarders on Windows.
func (p *Probe) OnNewDiscarder(_ *rules.RuleSet, _ *model.Event, _ eval.Field, _ eval.EventType) {
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/security/probe/probe_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,8 +1263,6 @@ func (p *WindowsProbe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetRe
}
}

p.processKiller.Apply(rs)

ars, err := kfilters.NewApplyRuleSetReport(p.config.Probe, rs)
if err != nil {
return nil, err
Expand All @@ -1289,6 +1287,11 @@ func (p *WindowsProbe) ApplyRuleSet(rs *rules.RuleSet) (*kfilters.ApplyRuleSetRe
return ars, nil
}

// OnNewRuleSetLoaded resets statistics and states once a new rule set is loaded
func (p *WindowsProbe) OnNewRuleSetLoaded(rs *rules.RuleSet) {
p.processKiller.Reset(rs)
}

// FlushDiscarders invalidates all the discarders
func (p *WindowsProbe) FlushDiscarders() error {
p.discardedPaths.Purge()
Expand Down
4 changes: 2 additions & 2 deletions pkg/security/probe/process_killer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ func (p *ProcessKiller) KillAndReport(kill *rules.KillDefinition, rule *rules.Ru
return true
}

// Apply applies to ruleset to the process killer
func (p *ProcessKiller) Apply(rs *rules.RuleSet) {
// Reset the state and statistics of the process killer
func (p *ProcessKiller) Reset(rs *rules.RuleSet) {
if p.cfg.RuntimeSecurity.EnforcementEnabled {
var ruleSetHasKillAction bool
var rulesetHasKillDisarmer bool
Expand Down
3 changes: 3 additions & 0 deletions pkg/security/rules/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ func (e *RuleEngine) LoadPolicies(providers []rules.PolicyProvider, sendLoadedRe
return fmt.Errorf("failed to flush discarders: %w", err)
}

// reset the probe process killer state once the new ruleset is loaded
e.probe.OnNewRuleSetLoaded(rs)

content, _ := json.Marshal(report)
seclog.Debugf("Policy report: %s", content)

Expand Down
Loading