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

Protected finding data #3779

Merged

Conversation

NDStrahilevitz
Copy link
Collaborator

@NDStrahilevitz NDStrahilevitz commented Dec 26, 2023

1. Explain what the PR does

f72fd50 fix(finding): use thread safe finding data access
ff65fc6 chore(all): change all detect.Finding to pointers
156569e chore(go.mod): bump types

Fix #3748

2. Explain how to test it

Make sure no regression happens, this change should not fundamentally change current behavior, but rather allow correct signature authoring.

3. Design choice

I initially wanted to break the Finding struct by adding the following struct:

type FindingData struct {
     data map[string]any
      l *sync.RWMutex
}

And add the thread-safe methods to that struct. This worked perfectly fine, with the api break being functionally not hard to fix with search and replaces. Only one problem happened, the output templates in tracee-rules were broken. I've made multiple attempts at fixing it, including adding custom json marshalling, but nothing worked. The only thing which worked with no breaking was keeping the existing Data field in place and untouched.

This introduced the problem of adding the mutex into the existing struct: if we add the mutex as a pointer then using the thread-safe methods as is would cause a nil pointer reference panic. This means that either all users now need to initialize the mutex themselves, or use a constructor which is not enforced for use (except for the otherwise occuring crash). That meant that the mutex must be a non pointer value.
That lead to the problem of mutex copying. mutex copying is an issue, since the locks are now unshared, which would mean that our problem is left unresolved. the way to use non pointer mutexes is if the object itself (the finding) is always passed around as a reference. Which meant changing the whole codebase to do just that.

Why was I still ok with choosing this solution?

  1. Because as explained, I couldn't find any other viable one which didn't break templates
  2. Because templates and tracee-rules and all that fun stuff will likely be removed once we finish the "everything-is-an-event" project. Alongside that, the finding struct can also be removed, thereby removing this sad design choice.

go.mod Outdated
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
kernel.org/pub/linux/libs/security/libcap/psx v1.2.68 // indirect
)

replace github.com/kubernetes/cri-api => k8s.io/cri-api v0.23.5-rc.0

replace github.com/aquasecurity/tracee/types => ./types
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO: types PR and bump

Copy link
Collaborator

@AlonZivony AlonZivony left a comment

Choose a reason for hiding this comment

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

LGTM

Due to the change in the previous types commit, a mutex was added to the
detect.Finding struct.
In order not to break existing usage, the new mutex was added not as a
pointer. This would cause a mutex copy almost everywhere, making the
change ineffective.
As such, the struct must be passed as a reference everywhere instead.
@NDStrahilevitz NDStrahilevitz merged commit 1c5ca95 into aquasecurity:main Dec 31, 2023
30 checks passed
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.

Concurrent map iteration and write in finding
2 participants