@@ -69,13 +69,6 @@ type RemediationProcessContext struct {
6969 PID uint32 `json:"pid,omitempty"`
7070}
7171
72- // RemediationContainerContext represents the container context for remediation events
73- // easyjson:json
74- type RemediationContainerContext struct {
75- CreatedAt uint64 `json:"created_at,omitempty"`
76- ID string `json:"id,omitempty"`
77- }
78-
7972// RemediationAgentContext represents the agent context for remediation events
8073// easyjson:json
8174type RemediationAgentContext struct {
@@ -109,15 +102,6 @@ func (k RemediationEvent) ToJSON() ([]byte, error) {
109102 return utils .MarshalEasyJSON (k )
110103}
111104
112- func getAgentEventID (rule * rules.Rule ) string {
113- for _ , tag := range rule .Tags {
114- if strings .HasPrefix (tag , "agent_event_id:" ) {
115- return tag [len ("agent_event_id:" ):]
116- }
117- }
118- return ""
119- }
120-
121105func getRemediationTagBool (rule * rules.Rule ) bool {
122106 for _ , tag := range rule .Tags {
123107 if strings .HasPrefix (tag , "remediation_rule:" ) {
@@ -140,27 +124,30 @@ func generateNetworkIsolationActionKey(ruleID string, filter string) string {
140124 return NetworkIsolationKeyPrefix + ruleID + hex .EncodeToString (hash [:])
141125
142126}
143- func generateRemediationActionKey (rule * rules. Rule ) string {
127+ func generateRemediationActionKey (key string ) string {
144128 // prefix + agent_event_id
145- return RemediationKeyPrefix + getAgentEventID ( rule )
129+ return RemediationKeyPrefix + key
146130}
147131
148132func getRemediationKeyFromAction (rule * rules.Rule , action * rules.Action ) string {
149- if getRemediationTagBool (rule ) {
150- // Should not have multiple remediation actions for the same rule that were generated by customers (remediation)
151- return generateRemediationActionKey (rule )
152- }
133+ key := ""
134+
153135 // Having multiple actions in the same rulemeans that they are from a rule that was not dynamically generated for the remediation feature
154136 // We assume this combination unique
155137 if action .Def .Kill != nil {
156138 // ruleID + scope + signal
157- return generateKillActionKey (rule .ID , action .Def .Kill .Scope , action .Def .Kill .Signal )
158- }
159- if action .Def .NetworkFilter != nil {
139+ key = generateKillActionKey (rule .ID , action .Def .Kill .Scope , action .Def .Kill .Signal )
140+ } else if action .Def .NetworkFilter != nil {
160141 // ruleID + bpffilter
161- return generateNetworkIsolationActionKey (rule .ID , action .Def .NetworkFilter .BPFFilter )
142+ key = generateNetworkIsolationActionKey (rule .ID , action .Def .NetworkFilter .BPFFilter )
143+ }
144+
145+ if getRemediationTagBool (rule ) {
146+ // Should not have multiple remediation actions for the same rule that were generated by customers (remediation)
147+ return generateRemediationActionKey (key )
162148 }
163- return ""
149+
150+ return key
164151}
165152
166153// NewRemediationEvent creates a new Remediation event from the latest action report
@@ -211,6 +198,17 @@ func getTagsFromRule(rule *rules.Rule) RuleTags {
211198 return ruleTags
212199}
213200
201+ // tagsToRuleTags converts a slice of "key:value" tags into RuleTags map.
202+ func tagsToRuleTags (tags []string ) RuleTags {
203+ ruleTags := make (RuleTags )
204+ for _ , tag := range tags {
205+ if before , after , ok := strings .Cut (tag , ":" ); ok {
206+ ruleTags [before ] = after
207+ }
208+ }
209+ return ruleTags
210+ }
211+
214212// HandleRemediationStatus is called when a new ruleset is loaded
215213// It cleans up the activeRemediations map from the kill actions and network isolation actions that are not persistent
216214func (p * EBPFProbe ) HandleRemediationStatus (rs * rules.RuleSet ) {
@@ -278,7 +276,33 @@ func (p *EBPFProbe) HandleRemediationStatus(rs *rules.RuleSet) {
278276 }
279277}
280278
281- func (p * EBPFProbe ) HandleKillRemediation (rule * rules.Rule , ev * model.Event , report * KillActionReport , action * rules.Action ) {
279+ // SendCustomEventKillAction sends a custom remediation event for a resolved kill action report
280+ func (p * EBPFProbe ) SendCustomEventKillAction (report model.ActionReport , tags []string ) {
281+ killReport , ok := report .(* KillActionReport )
282+ if ! ok {
283+ return
284+ }
285+ killReport .RLock ()
286+ status := string (killReport .Status )
287+ scope := killReport .Scope
288+ pid := killReport .Pid
289+ killReport .RUnlock ()
290+
291+ containerContext := killReport .GetRemediationContainerContext ()
292+
293+ remediation := & Remediation {
294+ actionType : RemediationTypeKill ,
295+ triggered : true ,
296+ scope : scope ,
297+ containerContext : containerContext ,
298+ processContext : RemediationProcessContext {PID : pid },
299+ ruleTags : tagsToRuleTags (tags ),
300+ }
301+ re := NewRemediationEvent (p , remediation , status , RemediationTypeKillStr )
302+ p .SendRemediationEvent (re )
303+ }
304+
305+ func (p * EBPFProbe ) HandleKillRemediation (rule * rules.Rule , ev * model.Event , action * rules.Action ) {
282306 remediationKey := getRemediationKeyFromAction (rule , action )
283307 p .activeRemediationsLock .Lock ()
284308 defer p .activeRemediationsLock .Unlock ()
@@ -292,30 +316,9 @@ func (p *EBPFProbe) HandleKillRemediation(rule *rules.Rule, ev *model.Event, rep
292316 remediation .policy = ""
293317 remediation .ruleTags = getTagsFromRule (rule )
294318
295- } else {
296- // Don't create a new entry for kill actions that are not from the remediation feature
297- // It will only be used to send an event
298- remediation = & Remediation {
299- actionType : RemediationTypeKill ,
300- triggered : true ,
301- processContext : RemediationProcessContext {
302- PID : ev .ProcessContext .Process .Pid ,
303- },
304- containerContext : RemediationContainerContext {
305- ID : string (ev .ProcessContext .Process .ContainerContext .ContainerID ),
306- CreatedAt : ev .ProcessContext .Process .ContainerContext .CreatedAt ,
307- },
308- scope : action .Def .Kill .Scope ,
309- }
310319 }
311-
312- // Get kill status
313- report .RLock ()
314- status := string (report .Status )
315- report .RUnlock ()
316- // Send custom event
317- killActionEvent := NewRemediationEvent (p , remediation , status , RemediationTypeKillStr )
318- p .SendRemediationEvent (killActionEvent )
320+ // Don't send an event for kill action here
321+ // The event will be sent when the report is resolved to handle the cases where disarmers are used
319322}
320323
321324func (p * EBPFProbe ) HandleNetworkRemediation (rule * rules.Rule , ev * model.Event , report * RawPacketActionReport , action * rules.Action ) {
0 commit comments