Skip to content

Commit dba1a42

Browse files
committed
chore: tidy up policy manager and related code
- Remove Snapshot() use. In the future it could be managed by the policy manager itself. - After PolicyManager initialization (first stages), Policies are not accessed directly anymore, but through the PolicyManager. - t.config.Policies is now only transient and used for PolicyManager initialization. - Policy version is deprecated and to be removed soon.
1 parent 77d6758 commit dba1a42

File tree

12 files changed

+134
-86
lines changed

12 files changed

+134
-86
lines changed

pkg/cmd/cobra/cobra.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
231231
}
232232

233233
cfg.Policies = policies
234-
policy.Snapshots().Store(cfg.Policies)
235234

236235
// Output command line flags
237236

pkg/cmd/urfave/urfave.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/aquasecurity/tracee/pkg/config"
1212
"github.com/aquasecurity/tracee/pkg/errfmt"
1313
"github.com/aquasecurity/tracee/pkg/logger"
14-
"github.com/aquasecurity/tracee/pkg/policy"
1514
"github.com/aquasecurity/tracee/pkg/utils/environment"
1615
)
1716

@@ -126,7 +125,6 @@ func GetTraceeRunner(c *cli.Context, version string) (cmd.Runner, error) {
126125
return runner, err
127126
}
128127
cfg.Policies = policies
129-
policy.Snapshots().Store(cfg.Policies)
130128

131129
broadcast, err := printer.NewBroadcast(
132130
output.PrinterConfigs,

pkg/ebpf/events_pipeline.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/aquasecurity/tracee/pkg/errfmt"
1313
"github.com/aquasecurity/tracee/pkg/events"
1414
"github.com/aquasecurity/tracee/pkg/logger"
15-
"github.com/aquasecurity/tracee/pkg/policy"
1615
"github.com/aquasecurity/tracee/pkg/utils"
1716
"github.com/aquasecurity/tracee/types/trace"
1817
)
@@ -296,20 +295,14 @@ func (t *Tracee) matchPolicies(event *trace.Event) uint64 {
296295
eventID := events.ID(event.EventID)
297296
bitmap := event.MatchedPoliciesKernel
298297

299-
policies, err := policy.Snapshots().Get(event.PoliciesVersion)
300-
if err != nil {
301-
t.handleError(err)
302-
return 0
303-
}
304-
305298
// Short circuit if there are no policies in userland that need filtering.
306-
if bitmap&policies.FilterableInUserland() == 0 {
299+
if !t.policyManager.FilterableInUserland(bitmap) {
307300
event.MatchedPoliciesUser = bitmap // store untouched bitmap to be used in sink stage
308301
return bitmap
309302
}
310303

311304
// range through each userland filterable policy
312-
for it := policies.CreateUserlandIterator(); it.HasNext(); {
305+
for it := t.policyManager.CreateUserlandIterator(); it.HasNext(); {
313306
p := it.Next()
314307
// Policy ID is the bit offset in the bitmap.
315308
bitOffset := uint(p.ID)
@@ -465,14 +458,8 @@ func (t *Tracee) processEvents(ctx context.Context, in <-chan *trace.Event) (
465458
continue
466459
}
467460

468-
policies, err := policy.Snapshots().Get(event.PoliciesVersion)
469-
if err != nil {
470-
t.handleError(err)
471-
continue
472-
}
473-
474461
// Get a bitmap with all policies containing container filters
475-
policiesWithContainerFilter := policies.WithContainerFilterEnabled()
462+
policiesWithContainerFilter := t.policyManager.WithContainerFilterEnabled()
476463

477464
// Filter out events that don't have a container ID from all the policies that
478465
// have container filters. This will guarantee that any of those policies
@@ -616,13 +603,8 @@ func (t *Tracee) sinkEvents(ctx context.Context, in <-chan *trace.Event) <-chan
616603
continue
617604
}
618605

619-
policies, err := policy.Snapshots().Get(event.PoliciesVersion)
620-
if err != nil {
621-
t.handleError(err)
622-
continue
623-
}
624606
// Populate the event with the names of the matched policies.
625-
event.MatchedPolicies = policies.MatchedNames(event.MatchedPoliciesUser)
607+
event.MatchedPolicies = t.policyManager.MatchedNames(event.MatchedPoliciesUser)
626608

627609
// Parse args here if the rule engine is not enabled (parsed there if it is).
628610
if !t.config.EngineConfig.Enabled {

pkg/ebpf/tracee.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ func New(cfg config.Config) (*Tracee, error) {
223223
return nil, errfmt.Errorf("validation error: %v", err)
224224
}
225225

226-
policyManager := policy.NewPolicyManager()
227-
228226
// Create Tracee
229227

230228
t := &Tracee{
@@ -236,10 +234,15 @@ func New(cfg config.Config) (*Tracee, error) {
236234
eventsState: make(map[events.ID]events.EventState),
237235
eventSignatures: make(map[events.ID]bool),
238236
streamsManager: streams.NewStreamsManager(),
239-
policyManager: policyManager,
237+
policyManager: policy.NewPolicyManager(cfg.Policies),
240238
requiredKsyms: []string{},
241239
}
242240

241+
// In the future Tracee Config will be changed in runtime, and will demand a proper
242+
// object to manage it. config.Config is currently a transient object that should be
243+
// used only to create the Tracee instance.
244+
t.config.Policies = nil // policies must be managed by the policy manager
245+
243246
eventsDependencies := dependencies.NewDependenciesManager(
244247
func(id events.ID) events.Dependencies {
245248
return events.Core.GetDefinitionByID(id).GetDependencies()
@@ -328,7 +331,7 @@ func New(cfg config.Config) (*Tracee, error) {
328331

329332
// TODO: extract this to a function to be called from here and from
330333
// policies changes.
331-
for it := t.config.Policies.CreateAllIterator(); it.HasNext(); {
334+
for it := t.policyManager.CreateAllIterator(); it.HasNext(); {
332335
p := it.Next()
333336
for e := range p.EventsToTrace {
334337
var submit, emit uint64
@@ -340,7 +343,7 @@ func New(cfg config.Config) (*Tracee, error) {
340343
utils.SetBit(&emit, uint(p.ID))
341344
t.selectEvent(e, events.EventState{Submit: submit, Emit: emit})
342345

343-
policyManager.EnableRule(p.ID, e)
346+
t.policyManager.EnableRule(p.ID, e)
344347
}
345348
}
346349

@@ -675,7 +678,7 @@ func (t *Tracee) initDerivationTable() error {
675678
shouldSubmit := func(id events.ID) func() bool {
676679
return func() bool { return t.eventsState[id].Submit > 0 }
677680
}
678-
symbolsCollisions := derive.SymbolsCollision(t.contSymbolsLoader, t.config.Policies)
681+
symbolsCollisions := derive.SymbolsCollision(t.contSymbolsLoader, t.policyManager)
679682

680683
executeFailedGen, err := derive.InitProcessExecuteFailedGenerator()
681684
if err != nil {
@@ -719,7 +722,7 @@ func (t *Tracee) initDerivationTable() error {
719722
Enabled: shouldSubmit(events.SymbolsLoaded),
720723
DeriveFunction: derive.SymbolsLoaded(
721724
t.contSymbolsLoader,
722-
t.config.Policies,
725+
t.policyManager,
723726
),
724727
},
725728
events.SymbolsCollision: {
@@ -895,12 +898,12 @@ func (t *Tracee) getOptionsConfig() uint32 {
895898

896899
// newConfig returns a new Config instance based on the current Tracee state and
897900
// the given policies config and version.
898-
func (t *Tracee) newConfig(cfg *policy.PoliciesConfig, version uint16) *Config {
901+
func (t *Tracee) newConfig(cfg *policy.PoliciesConfig) *Config {
899902
return &Config{
900903
TraceePid: uint32(os.Getpid()),
901904
Options: t.getOptionsConfig(),
902905
CgroupV1Hid: uint32(t.cgroups.GetDefaultCgroupHierarchyID()),
903-
PoliciesVersion: version,
906+
PoliciesVersion: 1, // version will be removed soon
904907
PoliciesConfig: *cfg,
905908
}
906909
}
@@ -963,7 +966,7 @@ func (t *Tracee) initKsymTableRequiredSyms() error {
963966
}
964967
}
965968
if _, ok := t.eventsState[events.PrintMemDump]; ok {
966-
for it := t.config.Policies.CreateAllIterator(); it.HasNext(); {
969+
for it := t.policyManager.CreateAllIterator(); it.HasNext(); {
967970
p := it.Next()
968971
// This might break in the future if PrintMemDump will become a dependency of another event.
969972
_, isChosen := p.EventsToTrace[events.PrintMemDump]
@@ -1145,7 +1148,7 @@ func (t *Tracee) populateBPFMaps() error {
11451148
}
11461149

11471150
// Initialize config and filter maps
1148-
err = t.populateFilterMaps(t.config.Policies, false)
1151+
err = t.populateFilterMaps(false)
11491152
if err != nil {
11501153
return errfmt.WrapError(err)
11511154
}
@@ -1217,8 +1220,8 @@ func (t *Tracee) populateBPFMaps() error {
12171220
}
12181221

12191222
// populateFilterMaps populates the eBPF maps with the given policies
1220-
func (t *Tracee) populateFilterMaps(newPolicies *policy.Policies, updateProcTree bool) error {
1221-
polCfg, err := newPolicies.UpdateBPF(
1223+
func (t *Tracee) populateFilterMaps(updateProcTree bool) error {
1224+
polCfg, err := t.policyManager.UpdateBPF(
12221225
t.bpfModule,
12231226
t.containers,
12241227
t.eventsState,
@@ -1232,7 +1235,7 @@ func (t *Tracee) populateFilterMaps(newPolicies *policy.Policies, updateProcTree
12321235

12331236
// Create new config with updated policies and update eBPF map
12341237

1235-
cfg := t.newConfig(polCfg, newPolicies.Version())
1238+
cfg := t.newConfig(polCfg)
12361239
if err := cfg.UpdateBPF(t.bpfModule); err != nil {
12371240
return errfmt.WrapError(err)
12381241
}
@@ -1382,7 +1385,7 @@ func (t *Tracee) initBPF() error {
13821385
}
13831386

13841387
// returned PoliciesConfig is not used here, therefore it's discarded
1385-
_, err = t.config.Policies.UpdateBPF(t.bpfModule, t.containers, t.eventsState, t.eventsParamTypes, false, true)
1388+
_, err = t.policyManager.UpdateBPF(t.bpfModule, t.containers, t.eventsState, t.eventsParamTypes, false, true)
13861389
if err != nil {
13871390
return errfmt.WrapError(err)
13881391
}
@@ -1715,11 +1718,11 @@ func (t *Tracee) getSelfLoadedPrograms(kprobesOnly bool) map[string]int {
17151718
func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17161719
var matchedPolicies uint64
17171720

1718-
setMatchedPolicies := func(event *trace.Event, matchedPolicies uint64, pols *policy.Policies) {
1719-
event.PoliciesVersion = pols.Version()
1721+
setMatchedPolicies := func(event *trace.Event, matchedPolicies uint64, pManager *policy.PolicyManager) {
1722+
event.PoliciesVersion = 1 // version will be removed soon
17201723
event.MatchedPoliciesKernel = matchedPolicies
17211724
event.MatchedPoliciesUser = matchedPolicies
1722-
event.MatchedPolicies = pols.MatchedNames(matchedPolicies)
1725+
event.MatchedPolicies = pManager.MatchedNames(matchedPolicies)
17231726
}
17241727

17251728
policiesMatch := func(state events.EventState) uint64 {
@@ -1731,7 +1734,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17311734
matchedPolicies = policiesMatch(t.eventsState[events.InitNamespaces])
17321735
if matchedPolicies > 0 {
17331736
systemInfoEvent := events.InitNamespacesEvent()
1734-
setMatchedPolicies(&systemInfoEvent, matchedPolicies, t.config.Policies)
1737+
setMatchedPolicies(&systemInfoEvent, matchedPolicies, t.policyManager)
17351738
out <- &systemInfoEvent
17361739
_ = t.stats.EventCount.Increment()
17371740
}
@@ -1743,7 +1746,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17431746
existingContainerEvents := events.ExistingContainersEvents(t.containers, t.config.NoContainersEnrich)
17441747
for i := range existingContainerEvents {
17451748
event := &(existingContainerEvents[i])
1746-
setMatchedPolicies(event, matchedPolicies, t.config.Policies)
1749+
setMatchedPolicies(event, matchedPolicies, t.policyManager)
17471750
out <- event
17481751
_ = t.stats.EventCount.Increment()
17491752
}
@@ -1754,7 +1757,7 @@ func (t *Tracee) invokeInitEvents(out chan *trace.Event) {
17541757
matchedPolicies = policiesMatch(t.eventsState[events.FtraceHook])
17551758
if matchedPolicies > 0 {
17561759
ftraceBaseEvent := events.GetFtraceBaseEvent()
1757-
setMatchedPolicies(ftraceBaseEvent, matchedPolicies, t.config.Policies)
1760+
setMatchedPolicies(ftraceBaseEvent, matchedPolicies, t.policyManager)
17581761
logger.Debugw("started ftraceHook goroutine")
17591762

17601763
// TODO: Ideally, this should be inside the goroutine and be computed before each run,
@@ -1823,18 +1826,7 @@ func (t *Tracee) triggerMemDump(event trace.Event) []error {
18231826

18241827
var errs []error
18251828

1826-
// We want to use the policies of relevant to the triggering event
1827-
policies, err := policy.Snapshots().Get(event.PoliciesVersion)
1828-
if err != nil {
1829-
logger.Debugw("Error getting policies for print_mem_dump event", "error", err)
1830-
// For fallback, try to use latest policies
1831-
policies, err = policy.Snapshots().GetLast()
1832-
if err != nil {
1833-
return []error{err}
1834-
}
1835-
}
1836-
1837-
for it := policies.CreateAllIterator(); it.HasNext(); {
1829+
for it := t.policyManager.CreateAllIterator(); it.HasNext(); {
18381830
p := it.Next()
18391831
// This might break in the future if PrintMemDump will become a dependency of another event.
18401832
_, isChosen := p.EventsToTrace[events.PrintMemDump]
@@ -1972,7 +1964,7 @@ func (t *Tracee) Subscribe(policyNames []string) (*streams.Stream, error) {
19721964
var policyMask uint64
19731965

19741966
for _, policyName := range policyNames {
1975-
p, err := t.config.Policies.LookupByName(policyName)
1967+
p, err := t.policyManager.LookupByName(policyName)
19761968
if err != nil {
19771969
return nil, err
19781970
}
@@ -2023,7 +2015,7 @@ func (t *Tracee) EnableRule(policyNames []string, ruleId string) error {
20232015
}
20242016

20252017
for _, policyName := range policyNames {
2026-
p, err := t.config.Policies.LookupByName(policyName)
2018+
p, err := t.policyManager.LookupByName(policyName)
20272019
if err != nil {
20282020
return err
20292021
}
@@ -2042,7 +2034,7 @@ func (t *Tracee) DisableRule(policyNames []string, ruleId string) error {
20422034
}
20432035

20442036
for _, policyName := range policyNames {
2045-
p, err := t.config.Policies.LookupByName(policyName)
2037+
p, err := t.policyManager.LookupByName(policyName)
20462038
if err != nil {
20472039
return err
20482040
}

pkg/events/derive/symbols_collision.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import (
2626
// `sched_process_exec` event for handling.
2727
//
2828

29-
func SymbolsCollision(soLoader sharedobjs.DynamicSymbolsLoader, policies *policy.Policies,
29+
func SymbolsCollision(
30+
soLoader sharedobjs.DynamicSymbolsLoader,
31+
pManager *policy.PolicyManager,
3032
) DeriveFunction {
3133
symbolsCollisionFilters := map[string]filters.Filter[*filters.StringFilter]{}
3234

3335
// pick white and black lists from the filters (TODO: change this)
34-
for it := policies.CreateAllIterator(); it.HasNext(); {
36+
for it := pManager.CreateAllIterator(); it.HasNext(); {
3537
p := it.Next()
3638
f := p.DataFilter.GetEventFilters(events.SymbolsCollision)
3739
maps.Copy(symbolsCollisionFilters, f)

pkg/events/derive/symbols_collision_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,12 @@ func TestSymbolsCollision(t *testing.T) {
493493
}
494494

495495
ps := policy.NewPolicies()
496-
policy.Snapshots().Store(ps)
497496
err := ps.Set(p)
498497
require.NoError(t, err)
498+
pManager := policy.NewPolicyManager(ps)
499499

500500
// Pick derive function from mocked tests
501-
deriveFunc := SymbolsCollision(mockLoader, ps)
501+
deriveFunc := SymbolsCollision(mockLoader, pManager)
502502

503503
mockLoader.addSOSymbols(
504504
testSOInstance{

pkg/events/derive/symbols_loaded.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import (
2020

2121
func SymbolsLoaded(
2222
soLoader sharedobjs.DynamicSymbolsLoader,
23-
policies *policy.Policies,
23+
pManager *policy.PolicyManager,
2424
) DeriveFunction {
2525
symbolsLoadedFilters := map[string]filters.Filter[*filters.StringFilter]{}
2626

27-
for it := policies.CreateAllIterator(); it.HasNext(); {
27+
for it := pManager.CreateAllIterator(); it.HasNext(); {
2828
p := it.Next()
2929
f := p.DataFilter.GetEventFilters(events.SymbolsLoaded)
3030
maps.Copy(symbolsLoadedFilters, f)

pkg/policy/policies.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ func (ps *Policies) Count() int {
8080
return ps.count()
8181
}
8282

83+
// Deprecated: Version returns the version of the Policies.
84+
// Will be removed soon.
8385
func (ps *Policies) Version() uint16 {
84-
return ps.version
86+
return 1 // version will be removed soon
8587
}
8688

8789
// WithContainerFilterEnabled returns a bitmap of policies that have at least one container filter type enabled.

0 commit comments

Comments
 (0)