3459 Commits

Author SHA1 Message Date
Eduardo Arias
df081af870 Adjusted pthread LDFLAG in examples required for multithreading.
- Some versions of gcc/libc require setting the pthread flag when using
  std::thread, which to implement it.
- This was found compiling the library in a Debian (bullseye) container.
2024-08-18 19:19:29 +00:00
Eduardo Arias
a5f223cb52 Individual test result should not be printed for automake output
- Test results output escape characters to highlight whether the test
  passed or failed. Additionally, the input & output for each test can
  include non-ASCII characters. These characters break parsing of
  results (.log & .trs files) with grep, as the files are interpreted
  to be binary.
2024-08-18 19:19:23 +00:00
Ervin Hegedus
752ab76238
Merge pull request #3229 from eduar-hte/pthread-makefile
Restore pthread LDFLAG in examples
2024-08-14 22:07:24 +02:00
Eduardo Arias
2cb1d032e3 Restore pthread LDFLAG. 2024-08-14 08:46:30 -07:00
Ervin Hegedus
b4f52325bd
Merge pull request #3228 from eduar-hte/asctime-multithread
Replace usage of std::ctime, which is not safe in multithread contexts
2024-08-14 14:55:53 +02:00
Ervin Hegedus
554bd30e74
Merge pull request #3227 from eduar-hte/pm-operator-multithreading
Removed unnecessary lock to call acmp_process_quick in Pm::evaluate
2024-08-14 12:36:54 +02:00
Ervin Hegedus
c9af0c747e
Merge pull request #3221 from eduar-hte/unittest-multithreaded
Add support to run unit tests in a multithreaded context
2024-08-14 12:18:07 +02:00
Ervin Hegedus
a6b287e120
Merge pull request #3225 from airween/v3/mpinvcharreqbody
feat: Check if the MP header contains invalid character
2024-08-14 09:06:14 +02:00
Eduardo Arias
ee5f95eb04 Added support to run unit tests in a multithreaded context
- This is controlled by specifying the 'mtstress' argument when running
  `unit_test`.
- The goal is to detect if the operator/transformation  fails in this
  context.
- In this mode, the test will be executed 5'000 times in 50 threads
  concurrently.
- Allocation & initialization of the operator/transformation is
  performed once in the main thread, while the evaluation is executed in
  the threads.
  - This is consistent with the library's support for multithreading,
    where initialization and loading of rules is expected to run once.
    See issue #3215.
2024-08-13 14:06:23 -07:00
Eduardo Arias
23a341eb6a Calculate sizes of strftime buffers based on format strings
- Leverage std::size to determine buffer size at compile time.
- Simplified 'TimeMon::evaluate' implementation as it was using strftime
  to get the month, convert the string to int, and then decrement it by
  one to make it zero based. This same value is already available in
  the 'struct tm' previously generated with the call to localtime_r (and
  where the month is already zero-based)
2024-08-13 13:36:03 -07:00
Eduardo Arias
5e6fcbc60b Replace usage of std::ctime, which is not safe for use in multithreaded contexts
- std::ctime returns a pointer to a string that "may be shared between
  std::asctime and std::ctime, and may be overwritten on each invocation
  of any of those functions.".
  - https://en.cppreference.com/w/cpp/chrono/c/ctime
- Replaced with call to strftime to generate the same string
  representation (using the format string: %c)
- Leveraged localtime_r (which is thread-safe) to convert time_t to
  struct tm, as required by strftime.
2024-08-13 10:54:06 -07:00
Eduardo Arias
8d6b185856 Removed unnecessary lock to call acmp_process_quick in Pm::evaluate
- This was introduced in commit 119a6fc & 7d786b3 because of a potential
  issue reported in #1573.
- The ACMP tree structure is initialized when the operator is
  initialized.
- During transaction execution the ACMP tree structure is only 'read'
  while traversing the tree (in acmp_process_quick) so this is safe for
  use in a multi-threaded environment.
2024-08-13 10:53:15 -07:00
Ervin Hegedus
718d121ee3
Merge pull request #3216 from eduar-hte/inmemory-collection-shared-mutex
Prevent concurrent access to data in InMemoryPerProcess' resolveXXX methods
2024-08-13 19:30:14 +02:00
Ervin Hegedus
32f6f78e78
Merge pull request #3222 from eduar-hte/remove-copies
Remove several string copies and unnecessary heap allocations
2024-08-13 19:25:11 +02:00
Ervin Hegedus
6388d88f38
Check if the MP header contains invalid character 2024-08-13 18:26:18 +02:00
Eduardo Arias
77adb57524 Avoid std::string copy in ssplit argument
- Other minor changes reported by sonarcloud
2024-08-12 12:59:28 -07:00
Ervin Hegedus
305f33fea1
Merge pull request #3224 from airween/v3/sethostnametestfix
fix: Fix regression test result; Add test to main test-suite list
2024-08-12 20:39:30 +02:00
Ervin Hegedus
eb26b7960c
Fix regression test result; Add test to main test-suite list 2024-08-12 18:46:19 +02:00
Eduardo Arias
cc0f893854 Removed unused overload of dash_if_empty that sonarcloud flags as potential buffer overflow 2024-08-09 14:07:39 -07:00
Eduardo Arias
8b17f3691f Inline string functions 2024-08-09 13:21:46 -07:00
Eduardo Arias
1534ee2448 Removed unnecessary copies 2024-08-09 12:52:25 -07:00
eduar-hte
f8dd09f7c9 Avoid creating a new std::string on the heap to create VariableValue
- Introduced helper method addVariableOrigin to reduce code duplication.
2024-08-09 12:52:25 -07:00
Eduardo Arias
bb07de9ad7 toupper/tolower is already receiving a copy, so it doesn't need to create a new one to transform it
- Make functions inline to improve performance
- Introduced helper method toCaseHelper to remove code duplication
2024-08-09 12:52:25 -07:00
Eduardo Arias
4bf9616f9e Adding multithreaded example from issue #3054 (by airween)
- Rewritten to use C++ libModSecurity API and std::thread (instead of
  pthreads)
2024-08-09 11:34:41 -07:00
Eduardo Arias
293cd214c7 Removed usage of pthreads and replaced with std C++ features
- Replaced pthread_mutex_t in modsecurity::operators::Pm with std::mutex
- Replaced pthread's thread usage in reading_logs_via_rule_message
  example with std::thread.
  - Simplified and modernized C++ code.
- Removed unnecessary includes of pthread.h
2024-08-09 11:34:40 -07:00
Eduardo Arias
4e15f9ef71 Turn off LMDB by default in Windows build to align with defaults for other platforms
- Replaced WITHOUT_XXX build options with WITH_XXX to make it easier to
  understand and configure.
- Updated GitHub workflow to align with these changes and include a
  build 'with lmdb' (again, analogous to non-Windows configurations)
2024-08-09 11:34:40 -07:00
Eduardo Arias
e2b3c9594f Prevent concurrent access to data structure in resolve methods
- As reported in #3054, the resolve methods in InMemoryPerProcess are
  not acquiring a lock/mutex to prevent concurrent access to the data
  structures that may be modified at the same time from other threads,
  and thus triggering undefined behaviour.
- Replace inheritance of std::unordered_multimap data structure with
  data member to prevent potential clients to use it without acquiring
  the mutex to protect concurrent access.
- Replace pthreads lock with std C++11 std::shared_mutex
  - Provides exclusive/shared lock access so that multiple readers can
    access the data at the same time, but only one writer. this is used
    to favor query performance by allowing more concurrent access to the
    data until an update needs to be performed.
  - Simplifies acquisition and disposal of lock/mutex with
    std::lock_guard, which has RAII semantics.
  - NOTE: Because std::shared_mutex is not recursive, calls to another
    function that tries to acquire the lock will fail. Introduced
    __store & __updateFirst helper methods to workaround this.
- Updates to InMemoryPerProcess::resolveFirst
  - Updated the code to store the expired var in 'expiredVars' to delete
    them after iterating over the range (and releasing the read lock, as
    'delIfExpired' needs to acquire it for exclusive access), as the
    current call to 'delIfExpired' would invalidate the range triggering
    undefined behaviour on the following iteration.
  - Noticed that in commit 118e1b3 the call to 'delIfExpired' in this
    function is done using 'it->second.getValue()'' instead of
    'it->first', which seems incorrect (based on similar code in other
    resolveXXX functions).
- Updated InMemoryPerProcess::delIfExpired to use 'std::find_if' (with a
  lambda that matches both the key and the 'isExpired' condition)
  because the data structure is a multimap. The version introduced in
  commit 118e1b3 could find an entry (not necessarily the first, because
  the map is unordered) where 'isExpired' is 'false' and exit, while
  another entry could be expired.
2024-08-09 11:34:40 -07:00
Ervin Hegedus
7bdc3c825c
Merge pull request #3220 from eduar-hte/string-null
Creating a std::string with a null pointer is undefined behaviour
2024-08-09 17:37:47 +02:00
Ervin Hegedus
3a83196a71
Merge pull request #3219 from eduar-hte/cpp17
Simplifiy configuration to build using std C++17
2024-08-09 17:34:02 +02:00
Ervin Hegedus
6f0e566f98
Merge pull request #3218 from eduar-hte/remove-dynamic-casts
Remove unnecessary dynamic casts
2024-08-09 17:24:55 +02:00
Eduardo Arias
c917d6a2dc Initialize variable in if statement to avoid doing dynamic_cast twice
- Refactored duplicate code in RuleWithOperator::getVariablesExceptions
- Leveraged auto to simplify declaration of dynamic_cast pointers.
2024-08-08 13:37:23 -07:00
Eduardo Arias
18378c10f8 Removed unnecessary dynamic_casts 2024-08-08 13:04:29 -07:00
Ervin Hegedus
a23e88f79f
Merge pull request #3114 from airween/v3/sonarmemleakfix
fix: Sonarcloud memleak fixes
2024-08-08 21:02:15 +02:00
Eduardo Arias
30a68de92d Creating a std::string with a null pointer is undefined behaviour.
- cppreference mentions this about the constructor that receives a
  const char *:
  - Constructs the string with the contents initialized with a copy of
    the null-terminated character string pointed to by s. The length of
    the string is determined by the first null character. The behavior
    is undefined if [s, s + Traits::length(s)) is not a valid range
    (for example, if s is a null pointer).
- C++23 introduces a deleted constructor to prevent this in static
  scenarios, which is how this issue was detected.
2024-08-08 11:39:37 -07:00
Eduardo Arias
59254fe3bd Simplifiy configuration to build libModSecurity with std C++17
- Leveraged autoconf again to check whether the C++ compiler supports
  the required standard version and build using it.
- Replaced the outdaded `ax_cxx_compile_stdcxx_11.m4` macro with the
  latest version of `ax_cxx_compile_stdcxx` which supports C++17.
  - https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
  - https://raw.githubusercontent.com/autoconf-archive/autoconf-archive/e4e5269db2764b9f53d759c24750ac6ca38e02ea/m4/ax_cxx_compile_stdcxx.m4
- This should also streamline updating to C++20 in the future.
2024-08-08 11:23:35 -07:00
Ervin Hegedus
a519c65902
Merge pull request #3217 from gberkes/v3/sonarcloud_Replace_this_declaration_by_a_structured_binding_declaration
V3/sonarcloud replace this declaration by a structured binding declaration
2024-08-08 17:55:09 +02:00
Ervin Hegedus
1d6e72e8e2
Merge pull request #3212 from eduar-hte/defensive-intervention
Do not assume ModSecurityIntervention argument to transaction::intervention has been initialized/cleaned
2024-08-08 17:45:34 +02:00
Eduardo Arias
cf643d6072 Avoid duplicate definition of --enable-assertions=yes configure flag on Unix builds
- This configuration flag was introduced in commit d47185d in the
  context of PR #3207.
- Moved to the configure step's 'run' command in order to be shared
  across configurations.
- For the sake of reference, matrix.platform.configure should be used
  for configuration flags that are needed for a specific
  platform/architecture (which was the reason it was introduced in
  commit d9255d8, PR #3144).
2024-08-08 08:16:14 -07:00
gberkes
c50a397a87 Suppress cppcheck false positive unassignedVariable warning. 2024-08-07 21:05:47 +02:00
gberkes
35e825d643 Refactor: replaced 3 declarations with 3 structured binding declarations.
This syntax is far more expressive and easier to understand than the old one.

Refactor: flipped the conditions in "if (contains[Tag|Msg|Id]( ... " statements
for clearer code.

Refactor: moved "Variable *b" as an init-statement inside "if()" statements for
stricter scope.

Reference: https://sonarcloud.io/project/issues?open=AY8-ff1vm_fzkWiCOtCt&id=owasp-modsecurity_ModSecurity
2024-08-07 17:55:30 +02:00
Ervin Hegedus
e8db92ebb0
Merge pull request #3214 from gberkes/v3/Use_the_init-statement_to_declare_pos_inside_the_if_statement
Refactor: used the init-statement to declare "pos" inside the if statement
2024-08-07 14:33:02 +02:00
Ervin Hegedus
a3ffc5a0d2
Merge pull request #3213 from gberkes/v3/sonar_move_these_3_includes_to_the_top_of_the_file
Refactor: moved 3 #include directives to the top of the file.
2024-08-07 14:29:34 +02:00
gberkes
ab78d4af79 Refactor: used the init-statement to declare "pos" inside the if statement.
C++17 introduced a construct to create and initialize a variable within the
condition of if and switch statements, and C++20 added this construct to
range-based for loops. Using this new feature simplifies common code patterns
and helps in giving variables the right scope.

Reference: https://sonarcloud.io/project/issues?open=AZDCieK2zGtqRpL2rnl-&id=owasp-modsecurity_ModSecurity
2024-08-07 13:05:02 +02:00
gberkes
c46f470d6b Refactor: moved 3 #include directives to the top of the file.
To aid code readability, all #include directives in a code file
should be grouped together near the top. The only items that may
precede an #include in a file are other preprocessor directives or comments.

Reference: https://sonarcloud.io/project/issues?sinceLeakPeriod=true&issueStatuses=OPEN%2CCONFIRMED&id=owasp-modsecurity_ModSecurity&open=AY8-ffgqm_fzkWiCOtCs&tab=code

Deleted some unnecessary trailing spaces as well.
2024-08-07 10:39:06 +02:00
Ervin Hegedus
0feaeacce5
Merge pull request #3211 from eduar-hte/secremoterules-regression
Fix SecRemoteRules regression test not to depend on a specific error message
2024-08-07 09:56:08 +02:00
Eduardo Arias
c802b46b7e Simplify parser error detection in testcase
- After the GitHub macOS runner images were upgraded to macOS 14.6
  (Sonoma), the test 'Include remote rules - failed download (Abort)'
  started failing because the error message reported by curl/OS is no
  longer 'HTTP response code said error'.
2024-08-06 14:40:59 -07:00
Eduardo Arias
0b5493d4e7 Minor performance improvements setting up intervention's log
- Initialize `log` temporary value on construction instead of doing
  default initialization and then calling `append`.
- Leverage `std::string_view` to replace `const std::string&` parameters
  in `utils::string::replaceAll` to avoid creating a `std::string`
  object (and associated allocation and copy) for the string literal`%d`
2024-08-06 14:40:45 -07:00
Eduardo Arias
c947f5e40d Do not assume ModSecurityIntervention argument to transaction::intervention has been initialized/cleaned
- Keep m_it->disruptive value and use it as return value to guarantee
  that the value is correct.
  - If m_it->disruptive is false and the 'it' argument has not been
    initialized/cleaned, the function may incorrectly return a non-zero
    value.
- When a disruptive intervention is being reported by the function,
  defensively initialize log & url to NULL if there's no such data to
  provide to the caller.
  - If the caller has not initialized/cleaned those fields in the 'it'
    argument, after returning from transaction::intervention, the user
    can safely read the log & url fields and in all scenarios they'll
    have valid values.
2024-08-06 14:40:45 -07:00
Ervin Hegedus
68d551c5f9
Merge pull request #3210 from eduar-hte/shared-files-deadlock
Fixed shared files deadlock in a multi-threaded Windows application
2024-08-06 17:35:41 +02:00
Ervin Hegedus
630751eee6
Merge pull request #3209 from eduar-hte/cleanup_api
Add cleanup methods to complete C based ABI
2024-08-06 15:40:48 +02:00