1297 Commits

Author SHA1 Message Date
Eduardo Arias
dc0a06fc70 Improve performance of VariableOrigin instances
- The previous approach would create a std::unique_ptr and store it in
  a std::list in VariableValue (Origins)
- The new approach now stores Origins in a std::vector and constructs
  VariableOrigin elements in-place on insertion.
- Instead of having two heap-allocations for every added VariableOrigin
  instance, this performs only one.
- If multiple origins are added, std::vector's growth strategy may even
  prevent a heap-allocation. There's a cost on growing the size of the
  vector, because a copy of current elements will be necessary.
  - Introduced reserveOrigin method to notify that multiple insertions
    will be made, so that we can use std::vector's reserve and do a
    single allocation (and copy of previous elements), and then just
    initialize the new elements in-place.
2024-07-17 00:49:27 +00:00
Eduardo Arias
a3f40ef03c Replace Mbed TLS source code in repository with a submodule
- Updated to latest Mbed TLS version (v3.6.0)
2024-05-31 00:41:10 +00:00
Eduardo Arias
7732b5e8f3 Update libinjection to version v3.9.2-92-gb9fcaaf 2024-05-31 00:41:10 +00:00
Eduardo Arias
2c488386c4 Add options nounistd & never-interactive to seclang-scanner.ll
- The parser is not used interactively so we can avoid including
  unistd.h, which is not available on Windows MSVC C++ compiler.
- The #ifdef WIN32 introduced in PR #3132 would probably be overwritten
  when the parser is updated.
2024-05-19 16:38:03 +00:00
Eduardo Arias
a8e132f3a1 Replaced the use of "new" in find_resource
- Addresses SonarCloud issue cpp:S5025 (Memory should not be managed manually)
- This function was not changed for the Windows port, but a similar
change to the one suggested was done in expandEnv in the same file.
- The first stream is not destructed at the exact same point it was in
the previous code (but rather when the second stream replaces it on
assignment to the same variable). An arbitrary scope could have been
introduced to destruct the object at the same place, but it doesn't
seem to be necessary and would make the code a bit strange.
2024-05-10 02:28:13 +00:00
Eduardo Arias
b69405a372 Use default keyword to implement constructor/destructor
- Addresses SonarCloud cpp:S3490 issue (Special member function should
not be defined unless a non standard behavior is required)
2024-05-10 02:28:13 +00:00
Eduardo Arias
411bbb2d36 Updated case of winsock header files
- Address SonarCloud cpp:S3806 issues ("#include" paths should be portable)
- This is not an actual issue in this case, because WinSock2.h and
WS2tcpip.h are Windows only.
2024-05-10 02:28:13 +00:00
Eduardo Arias
d7c49ed590 Added support to lock files on Windows and major rewrite to reintroduce reference counting and remove unused code.
- In Windows build, replaced usage of fcntl with cmd F_SETLKW with
  Win32 APIs to do file locking (LockFileEx & UnlockFileEx).
- Reintroduced the reference counting initially present in the class
  which is necessary to correctly handle merging of rules. This allows
  for correctly closing the file and removing the associated entry from
  m_handlers when the file is no longer used.
  - The need for reference counting can be seen in the example
  simple_example_using_c, where rules are initially loaded locally and
  then further rules are loaded remotely. This will initially open a
  shared file for a log, then in order to merge rules, the shared file
  is opened again for the new configuration. Then, the previous
  configuration closes the shared file on destruction. That is, two
  consecutive opens are done on a shared file, which is followed by
  a close. If the shared file is not reference counted, the shared file
  will be closed while there is still a reference active. The current
  version works because closing of the file has been disabled after
  reference counting was removed.
- Replaced `std::vector` data structure with `std::unordered_map` to
  improve lookup/update times, and simplify code.
- Removed unused code
  - Shared memory to store msc_file_handler structure
    - Initially SharedFiles used shared memory to store information
    about each shared file, including its file pointer and a mutex to
    synchronize access to the file on write. See code at commit 01c13da,
    in particular, usage of lock & fp fields in the msc_file_handler_t
    structure.
    - At that time, msc_file_handler_t included reference counting too
    with the using_it field, which was incremented when a file was
    opened and decremented on close. If the reference count reached
    zero, the shared file would be closed, the lock destroyed and the
    file handler entry removed from m_handlers.
    - Reference counting was removed in commit 7f9cd76, which
    introduced the following issues in SharedFiles::close:
      - No longer closes the file pointer.
        - The file pointer appears to be reset when a.second = 0, but
	this is a local copy of the data pair obtained from m_handlers,
	so this is essentially a nop (updating a local variable that is
	not referenced later in the function).
        - NOTE: The file pointer was moved out of the shared memory in
	this commit too, and stored alongside the msc_file_handler_t
	instance in the m_handlers entry associated to the shared file.
      - The lock is no longer destroyed.
      - The shared memory is marked to be destroyed in the call to:
      shmctl(a.first->shm_id_structure, IPC_RMID, NULL);
      - The shared file entry is not removed from m_handlers, so:
        - the file pointer is still valid, which is how writing to the
	file continues to work,
        - the reference to the shared memory is also present and will
	be marked to be destroyed whenever close is called again on the
	shared file.
    - File locking using the mutex in msc_file_handler_t was replaced in
    commit 3d20304 with usage of fcntl with cmd F_SETLKW.
    - At this time, it appears that the shared memory is no longer used,
    as the file pointer and locking no longer depend on it.
  - MODSEC_USE_GENERAL_LOCK
    - This code is introduced commit 7f9cd76 and is enabled if
    MODSEC_USE_GENERAL_LOCK` is defined.
    - The define is commented out in the source code since the original
    commit and is not present in the build configuration either.
    - In commit ff9152e, in the SharedFiles constructor, the
    initialization of the local variable toBeCreated is removed. This
    means that in this version, if MODSEC_USE_GENERAL_LOCK is enabled,
    execution of the code that checks on toBeCreated is undefined.
    - Then, in commit 9b40a04, the variable toBeCreated is initialized
    again, but is now set to false, which means that if
    MODSEC_USE_GENERAL_LOCK is enabled, the shared memory and lock it
    uses will *not* be initialized and thus doesn't work (execution of
    the current version will result in trying to acquire a lock that
    will be null).
    - I conclude that the feature is not used and can be removed.
      - Additionally, if it were working, I think the lock should be
      used in SharedFiles::write as well, which is a reader of the
      underlying data structures protected by this lock when they're
      modified in SharedFiles::open & SharedFiles::close.
2024-05-10 02:28:13 +00:00
Eduardo Arias
50e78331b1 Updated Env::evaluate to support case-insensitive environment variable names in Windows
- Env::evaluate
  - Environment variable names in Windows are case-insensitive, so in
  the Windows build we use strcasecmp to ignore case when matching
  variables in transaction->m_variableEnvs.
  - If the variable is found, we use the expected variable name to
  create the VariableValue instance, as further rule processing will
  look for the variable using case-sensitive comparisons.
    - This code is not limited to Windows to avoid another #ifdef block
    because for other platforms, because the env variable names are
    case-sensitive the value from either x.first and m_name will be the
    same.
- In Windows build, avoid redefining environ, already defined by
  including stdlib.h.
2024-05-10 02:28:13 +00:00
Eduardo Arias
50c35345ed Fixed use after free in ModSecurity::processContentOffset
- Use after free issue detected with Address Sanitizer while running
  the reading_logs_with_offset example.
- Keeps reference to last element in vars vector with vars.back(). Then
  it removes the element from vars calling vars.pop_back() which
  invalidates the reference, but it's accessed later in the function.
2024-05-10 02:28:13 +00:00
Eduardo Arias
fef419f986 Minor changes related to std::shared_ptr usage in RuleWithActions
- RuleWithActions::evaluate(Transaction *transaction)
  - Removed temporary rm local variable used to immediately create
  std::shared_ptr<RuleMessage>.
- Leverage std::make_shared & auto to simplify code.
2024-05-03 23:05:34 -03:00
Eduardo Arias
10c6ee726f Added support for expandEnv, createDir & cpu_seconds on Windows
- expandEnv on Windows uses POCO C++ Libraries implementation of Glob
  - Paths of matched files are adjusted to preserve UNIX path
  separators for consistency with the rest of the code.
  - Minor change to code shared with other platforms that removes
  allocation of std::ifstream on the heap to check whether the file can
  be opened, which can be done with local stack variable that closes
  the file when out of scope.
- createDir uses _mkdir on Windows, which doesn't support configuring
  the new directory's mode.
- added public domain implementation of clock_gettime for clock_id
  CLOCK_PROCESS_CPUTIME_ID from mingw-w64's winpthreads to support
  cpu_seconds on Windows.
- Updated included headers to support compilation on Windows (using
  Visual C++)
2024-05-03 23:05:34 -03:00
Eduardo Arias
ebf1f8fd28 On Windows use the operating system's native CA store for certificate verification of https requests.
- Updated included headers to support compilation on Windows (using
  Visual C++)
2024-05-03 23:05:34 -03:00
Eduardo Arias
91a736692a Minor changes to debug_log_writer
- Removed unused m_first data member.
- Explicitly delete copy constructor and assignment operator.
- Removed unused included headers.
2024-05-03 23:05:34 -03:00
Eduardo Arias
373633ffe2 mkstemp is not available in Windows build, replaced with _mktemp_s plus _open.
- Updated included headers to support compilation on Windows (using
  Visual C++)
- Minor change to use C++ default (zero) initialization instead of
  calling memset.
2024-05-03 23:05:34 -03:00
Eduardo Arias
35949179a4 setenv is not available in Windows build, replaced with _putenv_s 2024-05-03 23:05:34 -03:00
Eduardo Arias
abbd7b2f42 Replaced usage of apr_snprintf with snprintf (already in Windows exclusive code block)
- updated included headers to support compilation on Windows (using
  Visual C++)
2024-05-03 23:05:34 -03:00
Eduardo Arias
a48856822c Updated included headers to support compilation on Windows (using Visual C++)
- most of posix related functions and constants in unistd.h can be
  found in io.h in Visual C++
- introduced src/compat/msvc.h to adjust for compiler differences (and
  avoid updating code with #ifdef blocks for Windows support)
- removed some included headers that are not needed (both on Unix and
  Windows builds)
2024-05-03 23:05:34 -03:00
Eduardo Arias
1f419bba8f Implement sonarcloud suggestions 2024-05-02 17:18:31 -03:00
Eduardo Arias
9f5dc200ba Replace final three suppressions entries with line numbers
- These were initially not included in these changes, as they were
other PRs (#3104 & #3132) that address them.
2024-04-29 22:28:42 -03:00
Eduardo Arias
7a9c0ab15f Removed unused suppresion and avoid copy of logPath 2024-04-28 14:56:37 -03:00
Eduardo Arias
4aad8e0d06 Inline cppcheck suppressions 2024-04-28 14:56:23 -03:00
Eduardo Arias
cd2dded659 Removed unnecessary break after return 2024-04-28 14:56:00 -03:00
Eduardo Arias
0cd2f459f3 Address cppcheck suppressions in lmdb 2024-04-28 14:55:49 -03:00
Eduardo Arias
fde9d279b0 Removed unnecessary cppcheck suppression and r-value reference as copy should be avoidded by RVO 2024-04-28 14:55:18 -03:00
Felipe Zipitria
30fe6f935b
fix(rbl): typo in rbl check selector
Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
2024-04-22 10:23:28 -03:00
Ervin Hegedus
16d0df0ff9
Optimized variable handling 2024-03-31 14:14:45 +02:00
Ervin Hegedus
7c4dcdfa4b
Changed 'euqal_range()' + loop by 'find()' in resolveFirst() methods 2024-03-29 16:32:34 +01:00
gberkes
5802626437 Deleted redundant code in 'ModSecurity::serverLog(...)'. 2024-03-28 20:12:30 +01:00
Ervin Hegedus
6d719bee5b
Merge pull request #3016 from M4tteoP/uri_decode_invalid
fix: makes uri decode platform independent
2024-03-05 16:11:01 +01:00
Elia Pinto
2daebc090f src/utils/acmp.cc: reduce the scope of variable in a for () loop
In general, it is always preferable to reduce
the scope of a variable in a for loop
2024-02-29 20:20:41 +01:00
Elia Pinto
7fed599fdb src/request_body_processor/multipart.cc: reduce the scope of variable in a for () loop
In general, it is always preferable to reduce
the scope of a variable in a for loop
2024-02-29 20:20:41 +01:00
Elia Pinto
b23abf440a src/operators/verify_cc.cc: reduce the scope of variable in a for () loop
In general, it is always preferable to reduce
the scope of a variable in a for loop
2024-02-29 20:20:41 +01:00
Elia Pinto
9842b92bd1 src/actions/transformations/hex_decode.cc: reduce the scope of variable in a for () loop
In general, it is always preferable to reduce
the scope of a variable in a for loop
2024-02-29 20:20:41 +01:00
Mirko Dziadzka
367a871f30 Bump the C++ version from C++11 to C++17
This will allow the usage of more modern features in the future.
2024-02-09 21:57:31 +01:00
Ervin Hegedus
5f28c2bb21
Change REQUEST_FILENAME behavior 2024-01-30 12:21:45 +01:00
Ervin Hegedus
ec8e800a6a
Set the minimum security protocol version for SecRemoteRules 2024-01-27 17:27:00 +01:00
Martin Vierula
4c7a9bd312
Add WRDE_NOCMD to wordexp call 2023-12-06 08:16:39 -08:00
Matteo Pace
fcf205d599 fix: makes uri decode platform independent 2023-11-08 17:32:41 +01:00
Martin Vierula
c11b28292d
Fix: validateDTD compile fails if when libxml2 not installed 2023-11-06 20:35:05 -08:00
Martin Vierula
cb4d7ae371
Adjust some copyright dates 2023-10-31 06:23:19 -07:00
Martin Vierula
beaa452302
Fix memory leak of validateDTD's dtd object 2023-10-30 15:40:36 -07:00
Martin Vierula
36adc58ea3
const-ify some references (satisfy cppcheck) 2023-10-27 06:20:01 -07:00
Martin Vierula
b180de53bf
Fix memory leaks in ValidateSchema 2023-10-26 16:58:52 -07:00
Martin Vierula
fd67c6eb1d
Remove unneeded heap allocation in AnchoredSetVariable::set 2023-10-25 06:07:26 -07:00
Martin Vierula
dc6cce5f0c
refactoring and remove dead code in lmdb 2023-10-24 06:36:18 -07:00
Martin Vierula
34809d8064
Add expirevar support for lmdb 2023-10-10 10:31:52 -07:00
Martin Vierula
118e1b3a44 Support expirevar for in-memory collection 2023-09-29 11:40:03 -07:00
Martin Vierula
af45ccd53f
Fix: lmdb regex match on non-null-terminated string 2023-09-18 08:27:41 -07:00
Martin Vierula
dc2e38e242
Fix memory leaks in lmdb code (new'd strings) 2023-09-17 11:36:08 -07:00