Skip to content

Commit

Permalink
[CWS] remove interface{} and map of maps in invalid discarder check (
Browse files Browse the repository at this point in the history
…#17855)

* remove `interface{}` from `isInvalidDiscarder`

* remove `interface{}` from `InvalidDiscarders`

* remove map of map in invalid discarder check
  • Loading branch information
paulcacheux authored Jun 27, 2023
1 parent 704a642 commit 7615734
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions pkg/security/probe/discarders.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ var (
)

var (
dentryInvalidDiscarder = []interface{}{""}
dentryInvalidDiscarder = []string{""}
eventZeroDiscarder = &model.Event{
FieldHandlers: &model.DefaultFieldHandlers{},
ContainerContext: &model.ContainerContext{},
}
)

// InvalidDiscarders exposes list of values that are not discarders
var InvalidDiscarders = map[eval.Field][]interface{}{
var InvalidDiscarders = map[eval.Field][]string{
"open.file.path": dentryInvalidDiscarder,
"unlink.file.path": dentryInvalidDiscarder,
"chmod.file.path": dentryInvalidDiscarder,
Expand Down Expand Up @@ -518,27 +518,23 @@ func filenameDiscarderWrapper(eventType model.EventType, getter inodeEventGetter
}

// isInvalidDiscarder returns whether the given value is a valid discarder for the given field
func isInvalidDiscarder(field eval.Field, value interface{}) bool {
values, exists := invalidDiscarders[field]
if !exists {
return false
}

return values[value]
func isInvalidDiscarder(field eval.Field, value string) bool {
return invalidDiscarders[invalidDiscarderEntry{
field: field,
value: value,
}]
}

// rearrange invalid discarders for fast lookup
func createInvalidDiscardersCache() map[eval.Field]map[interface{}]bool {
invalidDiscarders := make(map[eval.Field]map[interface{}]bool)
func createInvalidDiscardersCache() map[invalidDiscarderEntry]bool {
invalidDiscarders := make(map[invalidDiscarderEntry]bool)

for field, values := range InvalidDiscarders {
ivalues := invalidDiscarders[field]
if ivalues == nil {
ivalues = make(map[interface{}]bool)
invalidDiscarders[field] = ivalues
}
for _, value := range values {
ivalues[value] = true
invalidDiscarders[invalidDiscarderEntry{
field: field,
value: value,
}] = true
}
}

Expand Down Expand Up @@ -700,7 +696,12 @@ func dumpDiscarders(resolver *dentry.Resolver, pidMap, inodeMap, statsFB, statsB
return dump, nil
}

var invalidDiscarders map[eval.Field]map[interface{}]bool
type invalidDiscarderEntry struct {
field eval.Field
value string
}

var invalidDiscarders map[invalidDiscarderEntry]bool

func init() {
invalidDiscarders = createInvalidDiscardersCache()
Expand Down

0 comments on commit 7615734

Please sign in to comment.