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(libscap): use the correct memory barrier for ARM64 #2067

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

Andreagit97
Copy link
Member

@Andreagit97 Andreagit97 commented Sep 13, 2024

What type of PR is this?

/kind bug

Any specific area of the project related to this PR?

/area libscap-engine-bpf

/area libscap-engine-kmod

/area libpman

Does this PR require a change in the driver versions?

No

What this PR does / why we need it:

This PR should fix #2061.
Today smp_load_acquire and smp_store_release use the synchronization primitives of x86 also on other architectures... but thanks to the nature of our code we never noticed it until this PR #2009.

More detail in the addition of

	if(g_state.cons_pos[pos] == g_state.prod_pos[pos]) {
			return NULL;
	}

caused the g_state.prod_pos[pos] value to be used immediately after the fetch. So if it was a wrong value we might end up reading some junk from the buffer... And this is exactly what is happening here #2061. This is for the modern ebpf.

the other drivers also used the wrong primitives, but again, probably due to some luck and rare race conditions we never faced any issues...btw I tried to fix them

The fix is to use the right synchronization primitives! Unfortunately, the definitions of these primitives are in the kernel headers so to avoid the constraint of having the headers when building libscap engines, I decided to copy the needed definitions, not sure if we want to take other directions here...

Which issue(s) this PR fixes:

Fixes #2061

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Copy link

Please double check driver/API_VERSION file. See versioning.

/hold

Signed-off-by: Andrea Terzolo <andreaterzolo3@gmail.com>
Copy link

github-actions bot commented Sep 13, 2024

Perf diff from master - unit tests

     1.71%     +0.75%  [.] scap_event_decode_params
     4.28%     -0.68%  [.] gzfile_read
     9.71%     +0.64%  [.] sinsp_parser::reset
     1.12%     +0.61%  [.] sinsp_parser::event_cleanup
     1.17%     -0.53%  [.] sinsp_parser::parse_context_switch
     0.89%     -0.52%  [.] scap_file_test_helpers::capture_search_evt_by_num
     3.32%     +0.50%  [.] sinsp_thread_manager::get_thread_ref
     0.49%     +0.43%  [.] libsinsp::state::stl_container_table_adapter<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, libsinsp::state::value_table_entry_adapter<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, libsinsp::state::value_table_entry_adapter<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::dynamic_fields_t>::stl_container_table_adapter
     3.62%     +0.43%  [.] sinsp_parser::process_event
     4.45%     +0.41%  [.] sinsp_evt::load_params

Heap diff from master - unit tests

peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Heap diff from master - scap file

peak heap memory consumption: 0B
peak RSS (including heaptrack overhead): 0B
total memory leaked: 0B

Benchmarks diff from master

Comparing gbench_data.json to /root/actions-runner/_work/libs/libs/build/gbench_data.json
Benchmark                                                         Time             CPU      Time Old      Time New       CPU Old       CPU New
----------------------------------------------------------------------------------------------------------------------------------------------
BM_sinsp_split_mean                                            +0.0010         +0.0010           144           144           144           144
BM_sinsp_split_median                                          +0.0107         +0.0107           142           144           142           144
BM_sinsp_split_stddev                                          -0.8604         -0.8602             2             0             2             0
BM_sinsp_split_cv                                              -0.8605         -0.8603             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                  -0.0462         -0.0462            43            41            43            41
BM_sinsp_concatenate_paths_relative_path_median                -0.0402         -0.0402            42            41            42            41
BM_sinsp_concatenate_paths_relative_path_stddev                -0.6857         -0.6857             1             0             1             0
BM_sinsp_concatenate_paths_relative_path_cv                    -0.6705         -0.6705             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                     -0.0192         -0.0192            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_median                   -0.0159         -0.0158            17            17            17            17
BM_sinsp_concatenate_paths_empty_path_stddev                   -0.4672         -0.4672             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_cv                       -0.4567         -0.4567             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                  -0.0293         -0.0293            45            44            45            44
BM_sinsp_concatenate_paths_absolute_path_median                -0.0331         -0.0331            45            44            45            44
BM_sinsp_concatenate_paths_absolute_path_stddev                -0.3855         -0.3855             1             1             1             1
BM_sinsp_concatenate_paths_absolute_path_cv                    -0.3669         -0.3670             0             0             0             0
BM_sinsp_split_container_image_mean                            +0.0415         +0.0415           350           365           350           365
BM_sinsp_split_container_image_median                          +0.0409         +0.0409           350           365           350           365
BM_sinsp_split_container_image_stddev                          +0.8460         +0.8450             1             3             1             3
BM_sinsp_split_container_image_cv                              +0.7725         +0.7715             0             0             0             0

#endif

// This is defined by us
#if defined(__x86_64__)
Copy link
Member Author

@Andreagit97 Andreagit97 Sep 13, 2024

Choose a reason for hiding this comment

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

This is used by the scap_kmod and scap_bpf engines and it uses the same primitive of smp_store/smp_load

@poiana
Copy link
Contributor

poiana commented Sep 13, 2024

LGTM label has been added.

Git tree hash: 5a1104014ab3043c18da50fbcf6b1f96d3032a20

Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

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

This is a great fix!
Than you very much for digging into it!
/approve

@poiana
Copy link
Contributor

poiana commented Sep 13, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP

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:

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

Copy link

codecov bot commented Sep 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.58%. Comparing base (398964a) to head (7375fc8).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2067   +/-   ##
=======================================
  Coverage   73.58%   73.58%           
=======================================
  Files         253      253           
  Lines       31867    31867           
  Branches     5648     5633   -15     
=======================================
  Hits        23449    23449           
+ Misses       8398     8388   -10     
- Partials       20       30   +10     
Flag Coverage Δ
libsinsp 73.58% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@poiana poiana merged commit 5919fa9 into falcosecurity:master Sep 13, 2024
47 of 49 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.

[BUG] libsinsp-example crash on arm64 + modern_bpf
4 participants