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

feat: add event enable/disable #3466

Merged
merged 1 commit into from
Sep 19, 2023

Conversation

josedonizetti
Copy link
Contributor

@josedonizetti josedonizetti commented Sep 12, 2023

The event manager manages the state (disabled/enabled) of events globably on tracee.

1. Explain what the PR does

This PR is part of the work to support a v1 for GRPC, it adds an enable/disable method to the policyManager, with a state struct to hold the information about the event that is enable/disbale globally as well as per policy, this struct in the future can hold the event emit/submit mask and also a policyMask to enable/disable policies globally.

Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need an event manager - can you please explain what other responsibilities will it have in the future other than enabling/disabling an event?
If we choose to keep the event manager, I think it should include all the state about the events:

  • The events that were selected by the user
  • The event configuration (submit/emit)
  • The policies that have this event enabled (so this means moving the policy bitmap of an event from policy manager to the event manager).

One thing that I didn't see here is calling the existing API to unload/load a signature event - do you plan to add it as well?

@josedonizetti
Copy link
Contributor Author

josedonizetti commented Sep 13, 2023

@yanivagman I think in the future the event manager should be the one responsible for eventsState, which hold the events the user selected and also the the emit, submit mask.

But I don't think it should have the policyBitmap, because disabling an event globably is different than disabling a rule in a policy, if I have a mask for the event.SecurityBPF of 0b0011 on the policyManager which means we have this event as a rule into policy 0 and 1, if I disable the event, I should stop emitting/submitting it, but later when I enable it back event should still have the mask of the policies 0b0011, right? We shouldn't change the policyBitmask for an event disable/enable. That is why I didn't want to do a enable/disable event into the policymanager.

Also, not refactoring the eventsState now is a way to simplify this change to deliver the API, because eventsState is used everywhere, so encapsulating it first, and refactoring later should be the approach here.

Unloading/loading a signature should be done later, I want to first finish the API to have it ready in the case we want to release an RC, the unloading/loading can't be done without changing any public API.

WDTY?

@yanivagman
Copy link
Collaborator

But I don't think it should have the policyBitmap, because disabling an event globably is different than disabling a rule in a policy, if I have a mask for the event.SecurityBPF of 0b0011 on the policyManager which means we have this event as a rule into policy 0 and 1, if I disable the event, I should stop emitting/submitting it, but later when I enable it back event should still have the mask of the policies 0b0011, right? We shouldn't change the policyBitmask for an event disable/enable. That is why I didn't want to do a enable/disable event into the policymanager.

The fact that we can keep the policy bitmap in the event state doesn't say we should modify it if the event was disable/enabled. Per each event, its state can be composed of:

  1. Which policies selected it - emit, submit, etc
  2. Is the event enabled/disabled globally

Also, not refactoring the eventsState now is a way to simplify this change to deliver the API, because eventsState is used everywhere, so encapsulating it first, and refactoring later should be the approach here.

Ok

Unloading/loading a signature should be done later, I want to first finish the API to have it ready in the case we want to release an RC, the unloading/loading can't be done without changing any public API.

I guess you meant CAN be done, right? If so then yes, I agree

@@ -561,6 +561,13 @@ func (t *Tracee) sinkEvents(ctx context.Context, in <-chan *trace.Event) <-chan
continue // might happen during initialization (ctrl+c seg faults)
}

// Is the event disabled?
if !t.eventManager.IsEventEnabled(events.ID(event.EventID)) {
logger.Debugw("event dropped because it is disabled", "event", event.EventName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea for the future: instead of logging the drop (which is noisy, even in a debugging, in a huge submission of a disabled event), we could count them like we do with lost events.

Copy link
Contributor Author

@josedonizetti josedonizetti Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Will add a TODO/issue about it, should be part of the metrics.

@josedonizetti
Copy link
Contributor Author

But I don't think it should have the policyBitmap, because disabling an event globably is different than disabling a rule in a policy, if I have a mask for the event.SecurityBPF of 0b0011 on the policyManager which means we have this event as a rule into policy 0 and 1, if I disable the event, I should stop emitting/submitting it, but later when I enable it back event should still have the mask of the policies 0b0011, right? We shouldn't change the policyBitmask for an event disable/enable. That is why I didn't want to do a enable/disable event into the policymanager.

The fact that we can keep the policy bitmap in the event state doesn't say we should modify it if the event was disable/enabled. Per each event, its state can be composed of:

  1. Which policies selected it - emit, submit, etc
  2. Is the event enabled/disabled globally

Ah! Ok, so let's merge both policyManager and eventManager? I think I prefer the name policyManager, and it has both states inside a structure like eventState{policy.Bitmap, bool (for enabled/disabled which later becomes emit/subimt}, and we have one check only check if an event is enabled instead of checking first if the event is enabled, and then if the rule is enabled (later if the policy is enabled). WDYT? or you think two maps, one for each is best?

Unloading/loading a signature should be done later, I want to first finish the API to have it ready in the case we want to release an RC, the unloading/loading can't be done without changing any public API.

I guess you meant CAN be done, right? If so then yes, I agree

Yes, exactly, we do it after having the API working as an internal change.

@geyslan
Copy link
Member

geyslan commented Sep 14, 2023

Ah! Ok, so let's merge both policyManager and eventManager? I think I prefer the name policyManager, and it has both states inside a structure like eventState{policy.Bitmap, bool (for enabled/disabled which later becomes emit/subimt}, ...

I also do prefer naming it policyManager. 👍🏼

After that type merge I'll start the relocation of the scattered Policies and EventState logic into the policyManager, as pointed in this EPIC issue: #3239

@josedonizetti josedonizetti force-pushed the add-event-manager branch 3 times, most recently from a74f957 to 4da8a82 Compare September 19, 2023 00:59
@josedonizetti josedonizetti changed the title feat: add event manager feat: add event enable/disable Sep 19, 2023
Copy link
Member

@geyslan geyslan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. There's a question ahead.

pkg/ebpf/policy_manager.go Show resolved Hide resolved
}

return pm.isRuleEnabled(matchedPolicies, ruleId)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be ruleId or eventId then?
Should it be isRuleEnabled or isEventEnabled?

Copy link
Contributor Author

@josedonizetti josedonizetti Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using ruleId where in the future it is a rule, although now it is type events.ID and I'm using eventId where it is an event, rule depends on passing policy information, event doesn't

And we have both concepts, Rules can be enabled/disabled, and Events can be enabled/disable, so we are exposing synchronized methods to deal with it (specially good to test both concepts), but in the pipeline we want to do a single test, get the mutex only one time, so we use IsEnabled which in the future should also cover the case of Policies enabled/disabled


// not synchronized, use IsEventEnabled instead
func (pm *policyManager) isEventEnabled(evenId events.ID) bool {
state, ok := pm.rules[evenId]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use pm.rules once with eventId (typo: eventId) and above with ruleId - which one is correct?
Reminder that in the future rule id will be composed of event id and some index

Copy link
Contributor Author

@josedonizetti josedonizetti Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using ruleId where in the future it is a rule, although now it is type events.ID and I'm using eventId where it is an event, rule depends on passing policy information, event doesn't

pkg/ebpf/policy_manager.go Outdated Show resolved Hide resolved
pkg/ebpf/policy_manager.go Outdated Show resolved Hide resolved
The event manager manages the state (disabled/enabled) of events
globably on tracee.
pm.mutex.Lock()
defer pm.mutex.Unlock()

state, ok := pm.rules[eventId]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how will this code look like in the future when we will have rule ids different than event id?
We will not be able to use pm.rules map anymore, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but it is an internal structure we should be able to refactor it without affecting anything that is consuming it. Right? It is encapsulated.

Copy link
Collaborator

@yanivagman yanivagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@josedonizetti josedonizetti merged commit d6962cd into aquasecurity:main Sep 19, 2023
26 checks passed
@josedonizetti josedonizetti deleted the add-event-manager branch September 19, 2023 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants