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 ksymbols mem consumption #4095

Merged

Conversation

yanivagman
Copy link
Collaborator

@yanivagman yanivagman commented Jun 2, 2024

1. Explain what the PR does

fix: add missing dependency for hooked_syscalls event
The hooked_syscalls event requires CAP_SYSLOG in order to
refresh its symbol table when reading /proc/kallsyms.
Add this missing dependency.
feat(ksymbols): restore lazy ksyms implementation
Use modified ksymbols implementation. The new implementation may take
a list of required symbols and addresses to track. If the list is given,
symbol scanning will only save those symbols or addresses which were
given in the list. If a new symbol is queried, then a rescan is needed.

Refactor tracee initialization to find all necessary symbols to track
ahead of runtime.

2. Explain how to test it

3. Other comments

Copy link
Collaborator

@NDStrahilevitz NDStrahilevitz left a comment

Choose a reason for hiding this comment

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

Note the last comment in particular, that is an oversight I made in the original implementation.

pkg/ebpf/probes/probes.go Outdated Show resolved Hide resolved
pkg/ebpf/probes/trace.go Outdated Show resolved Hide resolved
pkg/ebpf/probes/trace.go Outdated Show resolved Hide resolved
@@ -862,6 +890,104 @@ func (t *Tracee) newConfig(cfg *policy.PoliciesConfig, version uint16) *Config {
}
}

func (t *Tracee) initKsymTableRequiredSyms() error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to make sure we always initialize the text segment in relevant events. Perhaps we may want it as a symbol always present. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shouldn't this be handled by event dependencies?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It should, that's why the first part was about double-checking that.

pkg/utils/environment/kernel_symbols.go Show resolved Hide resolved
@geyslan
Copy link
Member

geyslan commented Jun 3, 2024

Tested and the args from hooked_syscall are being populated correctly.

Related to the memory footprint this clearly brings a smaller initial mem consumption (RSS): 249496 (PR) < 456036 (main)

This PR

ps -p `pgrep tracee` -o pid,vsz,rss,cmd
    PID    VSZ   RSS CMD
 412217 4201868 249496 ./dist/tracee -e hooked_syscall

main branch

ps -p `pgrep tracee` -o pid,vsz,rss,cmd
    PID    VSZ   RSS CMD
 423066 4918396 456036 ./dist/tracee -e hooked_syscall

@geyslan geyslan force-pushed the fix_ksymbols_mem_consumption_2 branch from b210980 to c111214 Compare June 3, 2024 12:48
@geyslan
Copy link
Member

geyslan commented Jun 3, 2024

@NDStrahilevitz I addressed some of your comments. The remainder is left for further consideration.

@NDStrahilevitz
Copy link
Collaborator

NDStrahilevitz commented Jun 3, 2024

@NDStrahilevitz I addressed some of your comments. The remainder is left for further consideration.

My last comment still seems rather critical IMO, @yanivagman WDYT? I fear that if we don't address it we will introduce a performance regression while a hooked_symbol event is triggered.

@geyslan geyslan force-pushed the fix_ksymbols_mem_consumption_2 branch 2 times, most recently from 0636aa9 to ef12be7 Compare June 3, 2024 16:20
@NDStrahilevitz NDStrahilevitz self-requested a review June 3, 2024 19:04
@NDStrahilevitz NDStrahilevitz dismissed their stale review June 3, 2024 19:04

Dismissing for +1

Copy link
Collaborator

@NDStrahilevitz NDStrahilevitz left a comment

Choose a reason for hiding this comment

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

LGTM once e2e fixes are resolved.

The hooked_syscalls and do_init_module events require CAP_SYSLOG in
order to refresh its symbol table when reading /proc/kallsyms.
Add these missing dependencies.
@yanivagman yanivagman force-pushed the fix_ksymbols_mem_consumption_2 branch from ef12be7 to 6913d6b Compare June 3, 2024 19:35
@geyslan
Copy link
Member

geyslan commented Jun 3, 2024

@yanivagman @NDStrahilevitz ignore the wip (3cacd03). It can be overridden.

kernel_new_mod_t new_mod = {.insert_time = insert_time};
u64 mod_addr = (u64) mod;
// new_module_map - must be after the module is added to modules list,
// otherwise there's a risk for race condition
bpf_map_update_elem(&new_module_map, &mod_addr, &new_mod, BPF_ANY);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@OriGlassman Proposed this internally afaik, should we keep this?

@NDStrahilevitz NDStrahilevitz force-pushed the fix_ksymbols_mem_consumption_2 branch from 646578f to 425af51 Compare June 4, 2024 18:54
NDStrahilevitz and others added 2 commits June 4, 2024 22:40
Use modified ksymbols implementation. The new implementation may take
a list of required symbols and addresses to track. If the list is given,
symbol scanning will only save those symbols or addresses which were
given in the list. If a new symbol is queried, then a rescan is needed.

Refactor tracee initialization to find all necessary symbols to track
ahead of runtime.

Co-authored-by: Geyslan Gregório <geyslan@gmail.com>
Co-authored-by: Yaniv Agman <yanivagman@gmail.com>
This commit refactors the refresh logic of KernelSymbolTable to be sequential,
removing the use of goroutines and channels.

The decision to simplify the code was made for the following reasons:
1. Simplicity: The sequential approach simplifies the codebase,
   making it easier to understand, maintain, and debug.
2. Less Relevant Concurrency: With the introduction of "required symbols",
   the need for concurrency has been reduced. We now skip memory allocation
   for symbols that are not required, which constitutes the majority of the
   symbols in /proc/kallsyms.
@yanivagman yanivagman force-pushed the fix_ksymbols_mem_consumption_2 branch 2 times, most recently from f436739 to 3879a21 Compare June 5, 2024 10:22
@yanivagman yanivagman merged commit 625f393 into aquasecurity:main Jun 5, 2024
32 checks passed
@yanivagman yanivagman deleted the fix_ksymbols_mem_consumption_2 branch June 5, 2024 11:41
@yanivagman yanivagman linked an issue Jun 25, 2024 that may be closed by this pull request
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.

Increased memory usage compared to v0.19.0
3 participants