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(userspace/libsinsp): stringop-overflow on libvirt_lxc #2115

Merged

Conversation

deepskyblue86
Copy link
Contributor

What type of PR is this?
/kind bug

Any specific area of the project related to this PR?

/area libsinsp

Does this PR require a change in the driver versions?

What this PR does / why we need it:

Building with Red Hat g++ 11.2.1-9 we get the following error on string assignment:

[ 34%] Building CXX object libsinsp/CMakeFiles/sinsp.dir/container_engine/libvirt_lxc.cpp.o
In file included from /opt/rh/devtoolset-11/root/usr/include/c++/11/ios:40,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/ostream:38,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/unique_ptr.h:42,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/memory:76,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/container_engine_base.h:21,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.h:24,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.cpp:19:
In static member function 'static constexpr std::char_traits<char>::char_type* std::char_traits<char>::copy(std::char_traits<char>::char_type*, const char_type*, std::size_t)',
    inlined from 'static void std::basic_string<_CharT, _Traits, _Alloc>::_M_copy(_CharT*, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:3464:21,
    inlined from 'std::basic_string<_CharT, _Traits, _Allocator>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.tcc:701:13,
    inlined from 'std::basic_string<_CharT, _Traits, _Allocator>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.tcc:689:5,
    inlined from 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4461:21,
    inlined from 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:3784:28,
    inlined from 'bool libsinsp::container_engine::libvirt_lxc::match(sinsp_threadinfo*, sinsp_container_info&)' at /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.cpp:51:17:
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/char_traits.h:437:56: error: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' writing 14 bytes into a region of size 7 overflows the destination [-Werror=stringop-overflow=]
  437 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
      |                                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

Let's just do a single assignment, and avoid eventual resize.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Building with Red Hat g++ 11.2.1-9 we get the following error on string
assignment:
```
[ 34%] Building CXX object libsinsp/CMakeFiles/sinsp.dir/container_engine/libvirt_lxc.cpp.o
In file included from /opt/rh/devtoolset-11/root/usr/include/c++/11/ios:40,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/ostream:38,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/unique_ptr.h:42,
                 from /opt/rh/devtoolset-11/root/usr/include/c++/11/memory:76,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/container_engine_base.h:21,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.h:24,
                 from /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.cpp:19:
In static member function 'static constexpr std::char_traits<char>::char_type* std::char_traits<char>::copy(std::char_traits<char>::char_type*, const char_type*, std::size_t)',
    inlined from 'static void std::basic_string<_CharT, _Traits, _Alloc>::_M_copy(_CharT*, const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:3464:21,
    inlined from 'std::basic_string<_CharT, _Traits, _Allocator>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.tcc:701:13,
    inlined from 'std::basic_string<_CharT, _Traits, _Allocator>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.tcc:689:5,
    inlined from 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::assign(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:4461:21,
    inlined from 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /opt/rh/devtoolset-11/root/usr/include/c++/11/bits/basic_string.h:3784:28,
    inlined from 'bool libsinsp::container_engine::libvirt_lxc::match(sinsp_threadinfo*, sinsp_container_info&)' at /code/agent/deps/agent-libs/userspace/libsinsp/container_engine/libvirt_lxc.cpp:51:17:
/opt/rh/devtoolset-11/root/usr/include/c++/11/bits/char_traits.h:437:56: error: 'void* __builtin_memcpy(void*, const void*, long unsigned int)' writing 14 bytes into a region of size 7 overflows the destination [-Werror=stringop-overflow=]
  437 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
      |                                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
```

Let's just do a single assignment, and avoid eventual resize.

Signed-off-by: Angelo Puglisi <angelopuglisi86@gmail.com>
@poiana
Copy link
Contributor

poiana commented Oct 16, 2024

LGTM label has been added.

Git tree hash: 842e280311388b9580f641a3a9f2eef7ff9d97bf

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 73.70%. Comparing base (c082ec3) to head (c0d8766).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...serspace/libsinsp/container_engine/libvirt_lxc.cpp 0.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2115   +/-   ##
=======================================
  Coverage   73.69%   73.70%           
=======================================
  Files         253      253           
  Lines       31916    31915    -1     
  Branches     5608     5627   +19     
=======================================
+ Hits        23521    23522    +1     
- Misses       8364     8387   +23     
+ Partials       31        6   -25     
Flag Coverage Δ
libsinsp 73.70% <0.00%> (+<0.01%) ⬆️

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.

Copy link

Perf diff from master - unit tests

     3.40%     -0.97%  [.] sinsp_thread_manager::find_thread
     3.89%     +0.90%  [.] sinsp_evt::load_params
     8.96%     +0.89%  [.] sinsp_parser::reset
     1.56%     +0.83%  [.] scap_event_decode_params
     4.94%     -0.74%  [.] sinsp_parser::process_event
     1.00%     -0.56%  [.] sinsp_fdtable::find
     1.50%     -0.55%  [.] sinsp_threadinfo::~sinsp_threadinfo
     1.56%     -0.51%  [.] sinsp_evt::get_ts
     0.22%     +0.51%  [.] std::_Hashtable<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libsinsp::state::dynamic_struct::field_info>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libsinsp::state::dynamic_struct::field_info> >, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::_M_emplace<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libsinsp::state::dynamic_struct::field_info> >
     7.95%     -0.50%  [.] sinsp::next

Heap diff from master - unit tests

peak heap memory consumption: -5.18K
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.0315         +0.0317           147           152           147           152
BM_sinsp_split_median                                          +0.0285         +0.0286           148           152           148           152
BM_sinsp_split_stddev                                          -0.0747         -0.0747             1             1             1             1
BM_sinsp_split_cv                                              -0.1030         -0.1032             0             0             0             0
BM_sinsp_concatenate_paths_relative_path_mean                  +0.1051         +0.1052            56            61            56            61
BM_sinsp_concatenate_paths_relative_path_median                +0.0946         +0.0948            56            61            56            61
BM_sinsp_concatenate_paths_relative_path_stddev                -0.8957         -0.8952             1             0             1             0
BM_sinsp_concatenate_paths_relative_path_cv                    -0.9056         -0.9052             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_mean                     +0.0146         +0.0148            25            25            25            25
BM_sinsp_concatenate_paths_empty_path_median                   +0.0117         +0.0120            24            25            24            25
BM_sinsp_concatenate_paths_empty_path_stddev                   +0.4113         +0.4088             0             0             0             0
BM_sinsp_concatenate_paths_empty_path_cv                       +0.3909         +0.3883             0             0             0             0
BM_sinsp_concatenate_paths_absolute_path_mean                  +0.0943         +0.0943            56            61            56            61
BM_sinsp_concatenate_paths_absolute_path_median                +0.0662         +0.0662            56            59            56            59
BM_sinsp_concatenate_paths_absolute_path_stddev                +5.6010         +5.6086             0             2             0             2
BM_sinsp_concatenate_paths_absolute_path_cv                    +5.0321         +5.0391             0             0             0             0
BM_sinsp_split_container_image_mean                            +0.0008         +0.0008           391           391           391           391
BM_sinsp_split_container_image_median                          -0.0022         -0.0023           391           390           391           390
BM_sinsp_split_container_image_stddev                          +1.3500         +1.3512             1             3             1             3
BM_sinsp_split_container_image_cv                              +1.3480         +1.3493             0             0             0             0

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.

Great catch, thanks for the fix!
/approve

@poiana
Copy link
Contributor

poiana commented Oct 16, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: deepskyblue86, 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

@poiana poiana merged commit d8d345a into falcosecurity:master Oct 16, 2024
48 of 49 checks passed
@FedeDP
Copy link
Contributor

FedeDP commented Oct 16, 2024

/milestone 0.19.0

@poiana poiana added this to the 0.19.0 milestone Oct 16, 2024
@deepskyblue86 deepskyblue86 deleted the fix/libvirt_lxc-stringop-overflow branch October 16, 2024 10:45
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.

5 participants