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

Fix memory leaks in k8s audit and json events #1041

Merged
merged 2 commits into from
Feb 6, 2020

Conversation

fntlnz
Copy link
Contributor

@fntlnz fntlnz commented Feb 6, 2020

What type of PR is this?

/kind bug

What this PR does / why we need it:

Fixes the memory leak bug reported by many users on the Falco slack.

WRT I started doing some memory analysis on my dev environment and noticed a pattern I didn’t like:

All the snapshots I was able to take were showing a linear increase in memory usage

image

Here’s an evidence of the leak, in how audit events are processed.

image

The problem I observed is that whenever the add_extracted_value function (https://github.com/falcosecurity/falco/blob/master/userspace/engine/json_evt.cpp#L629-L639) is called it adds an element to a list of strings. After that, old elements are never flushed. That leads to the memory leak we can see in the massif output above.

After some debugging, I noticed that add_extracted_value was only called when json_event_filter_check has m_uses_paths set to true. However, that value is never explicitly set to true in that codebase.

Then I noticed that whenever a new filtercheck is created (code : https://github.com/falcosecurity/falco/blob/master/userspace/engine/json_evt.cpp#L1379) the function allocate_new is called.

However, the created filter check does not contain false as a value for m_uses_paths but a numerical value. The reason for this is that while doing type conversion.
And since in C++ only zero is false and everything else is true that’s why we call add_extracted and have the memory leak.

This is the situation after my fix, as you can see the memory is pretty much always allocated and immediately released after processing.

image

Which issue(s) this PR fixes:

Fixes #1040

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

fix: memory leak introduced in 0.18.0 happening while using json events and the kubernetes audit endpoint

… default

Signed-off-by: Lorenzo Fontana <lo@linux.com>
Signed-off-by: Lorenzo Fontana <lo@linux.com>
@fntlnz
Copy link
Contributor Author

fntlnz commented Feb 6, 2020

Here is the massif file of falco before the fix

massif.out-before.zip

And this is after:
massif.out-after.zip

@fntlnz
Copy link
Contributor Author

fntlnz commented Feb 6, 2020

Artifacts to test this can be found here: https://circleci.com/gh/falcosecurity/falco/341#artifacts/containers/0

@leodido
Copy link
Member

leodido commented Feb 6, 2020

/cc @leodido

@poiana poiana requested a review from leodido February 6, 2020 14:02
Copy link
Contributor

@krisnova krisnova left a comment

Choose a reason for hiding this comment

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

Brava @fntlnz

Perfetto!

@poiana
Copy link
Contributor

poiana commented Feb 6, 2020

LGTM label has been added.

Git tree hash: af23c3997319bfdcd33f6abdba6917bde3ea5bc9

@poiana poiana added the approved label Feb 6, 2020
Copy link
Contributor

@mstemm mstemm left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this!

@poiana
Copy link
Contributor

poiana commented Feb 6, 2020

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: kris-nova, leodido, mstemm

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [kris-nova,leodido,mstemm]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit af3d89b into master Feb 6, 2020
@poiana poiana deleted the fix/memory-leak-use-paths branch February 6, 2020 18:16
@rajibmitra
Copy link

rajibmitra commented Feb 6, 2020

great findings ! Yes we should be releasing falco now. @fntlnz

@fntlnz fntlnz mentioned this pull request Feb 17, 2020
@fntlnz fntlnz added this to the 0.19.1 milestone Feb 17, 2020
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.

memory leak while processing the audit endpoint
6 participants