-
Notifications
You must be signed in to change notification settings - Fork 103
fix(container): Propagate engine initialization errors to the caller #1020
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
Open
rabbitstack
wants to merge
1
commit into
falcosecurity:main
Choose a base branch
from
rabbitstack:container-plugin-engine-initialization-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snippet introduces a memory leak, because
C.Cstring("")allocates a C string on the heap, and the Go runtime will not garbage-collect it. We should add a call todefer C.free(...). but in order to be sure that this is called just afterStartWorker(...)invocation, and regardless the fact that this function could panic, I would add a new wrapping function like the following:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The string buffer is deallocated on the C/C++ side of the plugin: https://github.com/falcosecurity/plugins/pull/1020/files#diff-66d6df581476d08b6c98945b62b26d631c6a61a3393e39f23334a2476eac0312R59. Pretty much in line with the preexisting code to free the
enabled_enginesstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some concerns here. First of all, we have two callers to
StartWorker():main()function inmain_exe.goin Go codemy_plugin::start_async_events()method in C++ codeIn
main_exe.go:main(), we are indeed allocating a C string witherrmsg := C.CString(""). This is our first allocation. We then pass its address toStartWorker()that, in some cases, replaces the pointed value:The memory backing the first allocation is leaked after this instruction. Moreover, after
StartWorker()returns, nobody deallocates the second string (the one containingerrs.String()we just saw): this is a second leak.Now let's analyze the second code path: the one starting from
my_plugin::start_async_events(). Aserris initialized withnullptr, a call toStartWorker()will work fine as long as it doesn't panic: if it panics, who is gonna release its allocated string? Notice that panics are also problematic for themain_exe.go:main()path.In order to simplify handling, and fixing all these issues, I would suggest to design a wrapper around
StartWorker()that allocates the C string and (this is important), ensures this string is deallocated, wheter or not the call toStartWorker()panicked. This is also easier to maintain, as we are putting handling ownership in a single place.Finally, I agree with you that
enabled_enginesshould be better handled. Specifically, who ensures it is initialized whenfree((void *)enabled_engines);is executed?WDYT? @rabbitstack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ekoops I agree with your analysis and the sentiment about generally improving the error handling. I didn't want to be too disruptive and adopted the same approach as with the
enabled_enginesstring :). That said,main_exe.gosource can be ignored as it is solely used to verify the go-worker inner-workings during development without the need to spin up a full-fledged plugin running inside Falco.Also, IMO, the leaks you identified are not critical, since
StartWorkeris invoked once during plugin lifetime. However, it is definitely worth the improvements you highlighted above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ekoops I currently have too many things on my plate, so I may ask @deepskyblue86 to take over if he's not overcommitted ;)