Skip to content

Fixed LMDB collection errors #1787

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions src/collection/backend/lmdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@

#undef LMDB_STDOUT_COUT


namespace modsecurity {
namespace collection {
namespace backend {


#ifdef WITH_LMDB


LMDB::LMDB() : Collection(""), m_env(NULL) {
LMDB::LMDB(std::string name) :
Collection(name), m_env(NULL) {
mdb_env_create(&m_env);
mdb_env_open(m_env, "./modsec-shared-collections",
MDB_WRITEMAP | MDB_NOSUBDIR, 0664);
Expand Down Expand Up @@ -121,7 +120,7 @@ void LMDB::lmdb_debug(int rc, std::string op, std::string scope) {
}
std::cout << std::endl;
} else if (op == "del") {
td::cout << scope << ", delete procedure failed: ";
std::cout << scope << ", delete procedure failed: ";
switch (rc) {
case EACCES:
std::cout << "an attempt was made to write in a ";
Expand Down Expand Up @@ -494,22 +493,40 @@ void LMDB::resolveMultiMatches(const std::string& var,
}

while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
if (key.mv_size <= keySize + 1) {
continue;
}
//
// I don't see what's the reason of this clause
//
// eg:
// looking for the variable: 'test', keySize will 4
// found key: 'test', key.mv_size will 4
// key.mv_size IS LESS than keySize+1, so we will continue?
//
//if (key.mv_size <= keySize + 1) {
// continue;
//}
char *a = reinterpret_cast<char *>(key.mv_data);
if (a[keySize] != ':') {
continue;
}
//
// also don't understand this part
//
// key.mv_data will 'test', but there isn't ':' at the end,
// so we will skip it?
//
//if (a[keySize] != ':') {
// continue;
//}

// this will never evaluate with the two statements above,
// but I think this is the only required check
if (strncmp(var.c_str(), a, keySize) != 0) {
continue;
}
VariableValue *v = new VariableValue(
new std::string(reinterpret_cast<char *>(key.mv_data),
l->insert(l->begin(), new VariableValue(
&m_name,
new std::string(reinterpret_cast<char *>(key.mv_data),
key.mv_size),
new std::string(reinterpret_cast<char *>(data.mv_data),
data.mv_size));
l->insert(l->begin(), v);
new std::string(reinterpret_cast<char *>(data.mv_data),
data.mv_size))
);
}

mdb_cursor_close(cursor);
Expand Down
2 changes: 1 addition & 1 deletion src/collection/backend/lmdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace backend {
class LMDB :
public Collection {
public:
LMDB();
LMDB(std::string name);
~LMDB();
void store(std::string key, std::string value) override;

Expand Down
10 changes: 5 additions & 5 deletions src/modsecurity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ ModSecurity::ModSecurity()
: m_connector(""),
m_whoami(""),
#ifdef WITH_LMDB
m_global_collection(new collection::backend::LMDB()),
m_resource_collection(new collection::backend::LMDB()),
m_ip_collection(new collection::backend::LMDB()),
m_session_collection(new collection::backend::LMDB()),
m_user_collection(new collection::backend::LMDB()),
m_global_collection(new collection::backend::LMDB("GLOBAL")),
m_resource_collection(new collection::backend::LMDB("RESOURCE")),
m_ip_collection(new collection::backend::LMDB("IP")),
m_session_collection(new collection::backend::LMDB("SESSION")),
m_user_collection(new collection::backend::LMDB("USER")),
#else
m_global_collection(new collection::backend::InMemoryPerProcess("GLOBAL")),
m_ip_collection(new collection::backend::InMemoryPerProcess("IP")),
Expand Down