-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Prevent concurrent access to data in InMemoryPerProcess' resolveXXX methods #3216
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
Merged
airween
merged 4 commits into
owasp-modsecurity:v3/master
from
eduar-hte:inmemory-collection-shared-mutex
Aug 13, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2b3c95
Prevent concurrent access to data structure in resolve methods
eduar-hte 4e15f9e
Turn off LMDB by default in Windows build to align with defaults for …
eduar-hte 293cd21
Removed usage of pthreads and replaced with std C++ features
eduar-hte 4bf9616
Adding multithreaded example from issue #3054 (by airween)
eduar-hte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
[requires] | ||
yajl/2.1.0 | ||
pcre2/10.42 | ||
pthreads4w/3.0.0 | ||
libxml2/2.12.6 | ||
lua/5.4.6 | ||
libcurl/8.6.0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
|
||
noinst_PROGRAMS = multithread | ||
|
||
multithread_SOURCES = \ | ||
multithread.cc | ||
|
||
multithread_LDADD = \ | ||
$(CURL_LDADD) \ | ||
$(GEOIP_LDADD) \ | ||
$(GLOBAL_LDADD) \ | ||
$(LIBXML2_LDADD) \ | ||
$(LMDB_LDADD) \ | ||
$(MAXMIND_LDADD) \ | ||
$(LUA_LDADD) \ | ||
$(PCRE_LDADD) \ | ||
$(SSDEEP_LDADD) \ | ||
$(YAJL_LDADD) | ||
|
||
multithread_LDFLAGS = \ | ||
-L$(top_builddir)/src/.libs/ \ | ||
$(GEOIP_LDFLAGS) \ | ||
-lmodsecurity \ | ||
-lm \ | ||
-lstdc++ \ | ||
$(LMDB_LDFLAGS) \ | ||
$(LUA_LDFLAGS) \ | ||
$(MAXMIND_LDFLAGS) \ | ||
$(SSDEEP_LDFLAGS) \ | ||
$(YAJL_LDFLAGS) | ||
|
||
multithread_CPPFLAGS = \ | ||
$(GLOBAL_CFLAGS) \ | ||
-I$(top_builddir)/headers \ | ||
-I$(top_builddir) \ | ||
-g \ | ||
-I../others \ | ||
-fPIC \ | ||
-O3 \ | ||
$(CURL_CFLAGS) \ | ||
$(GEOIP_CFLAGS) \ | ||
$(GLOBAL_CPPFLAGS) \ | ||
$(MODSEC_NO_LOGS) \ | ||
$(YAJL_CFLAGS) \ | ||
$(LMDB_CFLAGS) \ | ||
$(LUA_CFLAGS) \ | ||
$(PCRE_CFLAGS) \ | ||
$(LIBXML2_CFLAGS) | ||
|
||
|
||
MAINTAINERCLEANFILES = \ | ||
Makefile.in | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
SecDebugLog debug.log | ||
SecDebugLogLevel 9 | ||
|
||
|
||
SecRule REQUEST_HEADERS:User-Agent ".*" "id:1,phase:1,t:sha1,t:hexEncode,setvar:tx.ua_hash=%{MATCHED_VAR}" | ||
|
||
SecAction "id:2,phase:2,initcol:ip=%{REMOTE_ADDR}_%{tx.ua_hash}" | ||
|
||
SecRule REQUEST_HEADERS:User-Agent "@rx .*" "id:3,phase:2,setvar:ip.auth_attempt=+1" | ||
|
||
SecRule ARGS:foo "@rx herewego" "id:4,phase:2,setvar:ip.foo=bar,expirevar:ip.foo=2" | ||
#SecRule ARGS:foo "@rx herewego" "id:4,phase:2,setvar:ip.foo=bar" | ||
SecRule IP "@rx bar" "id:5,phase:2,pass" | ||
SecRule IP:auth_attempt "@rx bar" "id:6,phase:2,pass" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <iostream> | ||
#include <thread> | ||
#include <array> | ||
|
||
#include <modsecurity/modsecurity.h> | ||
#include <modsecurity/transaction.h> | ||
#include <modsecurity/rules_set.h> | ||
|
||
static void process_request(modsecurity::ModSecurity *modsec, modsecurity::RulesSet *rules, int tid) { | ||
std::cout << "Hello World! It's me, thread #" << tid << std::endl; | ||
|
||
for(int i = 0; i != 1'000; i++) { | ||
auto modsecTransaction = std::make_unique<modsecurity::Transaction>(modsec, rules, nullptr); | ||
|
||
modsecTransaction->processConnection("127.0.0.1", 12345, "127.0.0.1", 80); | ||
modsecTransaction->processURI( | ||
"https://www.modsecurity.org/test?foo=herewego", | ||
"GET", "1.1"); | ||
|
||
modsecTransaction->addRequestHeader("User-Agent", | ||
"Basic ModSecurity example"); | ||
modsecTransaction->processRequestHeaders(); | ||
modsecTransaction->processRequestBody(); | ||
|
||
modsecTransaction->addResponseHeader("HTTP/1.1", | ||
"200 OK"); | ||
modsecTransaction->processResponseHeaders(200, "HTTP 1.2"); | ||
modsecTransaction->processResponseBody(); | ||
|
||
modsecTransaction->processLogging(); | ||
|
||
std::this_thread::sleep_for(std::chrono::microseconds(100)); | ||
} | ||
|
||
std::cout << "Thread #" << tid << " exits" << std::endl; | ||
} | ||
|
||
int main (int argc, char *argv[]) { | ||
auto modsec = std::make_unique<modsecurity::ModSecurity>(); | ||
modsec->setConnectorInformation("ModSecurity-test v0.0.1-alpha (Simple " \ | ||
"example on how to use ModSecurity API"); | ||
|
||
char main_rule_uri[] = "basic_rules.conf"; | ||
auto rules = std::make_unique<modsecurity::RulesSet>(); | ||
if (rules->loadFromUri(main_rule_uri) < 0) { | ||
std::cerr << "Problems loading the rules..." << std::endl; | ||
std::cerr << rules->m_parserError.str() << std::endl; | ||
return -1; | ||
} | ||
|
||
constexpr auto NUM_THREADS = 100; | ||
std::array<std::thread, NUM_THREADS> threads; | ||
|
||
for (auto i = 0; i != threads.size(); ++i) { | ||
threads[i] = std::thread( | ||
[&modsec, &rules, i]() { | ||
process_request(modsec.get(), rules.get(), i); | ||
}); | ||
} | ||
|
||
std::this_thread::sleep_for(std::chrono::microseconds(10000)); | ||
|
||
for (auto i = 0; i != threads.size(); ++i) { | ||
threads[i].join(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.