Skip to content

Commit

Permalink
Makes Rule a shared pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmerle committed Mar 25, 2020
1 parent f1d22f9 commit 9d15861
Show file tree
Hide file tree
Showing 16 changed files with 910 additions and 915 deletions.
18 changes: 2 additions & 16 deletions headers/modsecurity/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,6 @@ class Rule {
bool containsTag(const std::string& name, Transaction *t);
bool containsMsg(const std::string& name, Transaction *t);

int refCountDecreaseAndCheck() {
m_referenceCount--;
if (m_referenceCount == 0) {
delete this;
return 1;
}
return 0;
}


void refCountIncrease() {
m_referenceCount++;
}

void executeTransformations(
actions::Action *a,
std::shared_ptr<std::string> newValue,
Expand All @@ -140,7 +126,7 @@ class Rule {
int m_phase;
modsecurity::variables::Variables *m_variables;
operators::Operator *m_op;
Rule *m_chainedRuleChild;
std::unique_ptr<Rule> m_chainedRuleChild;
Rule *m_chainedRuleParent;
std::string m_fileName;
std::string m_marker;
Expand All @@ -150,9 +136,9 @@ class Rule {
std::vector<actions::Action *> m_actionsRuntimePre;
std::vector<actions::SetVar *> m_actionsSetVar;
std::vector<actions::Tag *> m_actionsTag;

private:
bool m_unconditional;
int m_referenceCount;
};


Expand Down
51 changes: 49 additions & 2 deletions headers/modsecurity/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,66 @@
#include <string>
#include <vector>
#include <list>
#include <memory>
#endif

#include "modsecurity/rule.h"

#ifndef HEADERS_MODSECURITY_RULES_H_
#define HEADERS_MODSECURITY_RULES_H_


#ifdef __cplusplus
namespace modsecurity {
class Rule;

class Rules : public std::vector<Rule *> {

class Rules {
public:
void dump() const {
for (int j = 0; j < m_rules.size(); j++) {
std::cout << " Rule ID: " << std::to_string(m_rules.at(j)->m_ruleId);
std::cout << "--" << m_rules.at(j) << std::endl;
}
}

int append(Rules *from, const std::vector<int64_t> &ids, std::ostringstream *err) {
size_t j = 0;
for (; j < from->size(); j++) {
Rule *rule = from->at(j).get();
if (std::binary_search(ids.begin(), ids.end(), rule->m_ruleId)) {
if (err != NULL) {
*err << "Rule id: " << std::to_string(rule->m_ruleId) \
<< " is duplicated" << std::endl;
}
return -1;
}
}
m_rules.insert(m_rules.end(), from->m_rules.begin(), from->m_rules.end());
return j;
}

bool insert(std::shared_ptr<Rule> rule) {
return insert(rule, nullptr, nullptr);
}

bool insert(std::shared_ptr<Rule> rule, const std::vector<int64_t> *ids, std::ostringstream *err) {
if (ids != nullptr && std::binary_search(ids->begin(), ids->end(), rule->m_ruleId)) {
if (err != nullptr) {
*err << "Rule id: " << std::to_string(rule->m_ruleId) \
<< " is duplicated" << std::endl;
}
return false;
}

m_rules.push_back(rule);
return true;
}

size_t size() const { return m_rules.size(); }
std::shared_ptr<Rule> operator[](int index) const { return m_rules[index]; }
std::shared_ptr<Rule> at(int index) const { return m_rules[index]; }

std::vector<std::shared_ptr<Rule> > m_rules;
};


Expand Down
3 changes: 1 addition & 2 deletions headers/modsecurity/rules_set_phases.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class Rule;
/** @ingroup ModSecurity_CPP_API */
class RulesSetPhases {
public:

~RulesSetPhases();

bool insert(Rule *rule);
bool insert(std::shared_ptr<Rule> rule);

int append(RulesSetPhases *from, std::ostringstream *err);
void dump() const;
Expand Down
78 changes: 39 additions & 39 deletions src/parser/driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Driver::Driver()
: RulesSetProperties(),
trace_scanning(false),
trace_parsing(false),
lastRule(NULL) { }
m_lastRule(nullptr) { }


Driver::~Driver() {
Expand All @@ -43,71 +43,56 @@ Driver::~Driver() {

int Driver::addSecMarker(std::string marker) {
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
Rule *rule = new Rule(marker);
std::unique_ptr<Rule> rule(new Rule(marker));
rule->m_phase = i;
m_rulesSetPhases.insert(rule);
m_rulesSetPhases.insert(std::move(rule));
}
return 0;
}


int Driver::addSecAction(Rule *rule) {
int Driver::addSecAction(std::unique_ptr<Rule> rule) {
if (rule->m_phase >= modsecurity::Phases::NUMBER_OF_PHASES) {
m_parserError << "Unknown phase: " << std::to_string(rule->m_phase);
m_parserError << std::endl;
return false;
}


m_rulesSetPhases.insert(rule);
m_rulesSetPhases.insert(std::move(rule));

return true;
}


int Driver::addSecRuleScript(RuleScript *rule) {
m_rulesSetPhases.insert(rule);
int Driver::addSecRuleScript(std::unique_ptr<RuleScript> rule) {
m_rulesSetPhases.insert(std::move(rule));
return true;
}


int Driver::addSecRule(Rule *rule) {
if (rule->m_phase >= modsecurity::Phases::NUMBER_OF_PHASES) {
m_parserError << "Unknown phase: " << std::to_string(rule->m_phase);
int Driver::addSecRule(std::unique_ptr<Rule> r) {
if (r->m_phase >= modsecurity::Phases::NUMBER_OF_PHASES) {
m_parserError << "Unknown phase: " << std::to_string(r->m_phase);
m_parserError << std::endl;
return false;
}

if (lastRule && lastRule->m_chained) {
if (lastRule->m_chainedRuleChild == NULL) {
rule->m_phase = lastRule->m_phase;
if (rule->m_theDisruptiveAction) {
m_parserError << "Disruptive actions can only be specified by";
m_parserError << " chain starter rules.";
return false;
}
lastRule->m_chainedRuleChild = rule;
rule->m_chainedRuleParent = lastRule;
return true;
} else {
Rule *a = lastRule->m_chainedRuleChild;
while (a->m_chained && a->m_chainedRuleChild != NULL) {
a = a->m_chainedRuleChild;
}
if (a->m_chained && a->m_chainedRuleChild == NULL) {
a->m_chainedRuleChild = rule;
rule->m_chainedRuleParent = a;
if (a->m_theDisruptiveAction) {
m_parserError << "Disruptive actions can only be ";
m_parserError << "specified by chain starter rules.";
return false;
}
return true;
}
/* is it a chained rule? */
if (m_lastRule != nullptr && m_lastRule->m_chained) {
r->m_phase = m_lastRule->m_phase;
if (r->m_theDisruptiveAction) {
m_parserError << "Disruptive actions can only be specified by";
m_parserError << " chain starter rules.";
return false;
}
m_lastRule->m_chainedRuleChild = std::move(r);
m_lastRule->m_chainedRuleChild->m_chainedRuleParent = m_lastRule;
m_lastRule = m_lastRule->m_chainedRuleChild.get();
return true;
}


std::shared_ptr<Rule> rule(std::move(r));
/*
* Checking if the rule has an ID and also checking if this ID is not used
* by other rule
Expand All @@ -118,6 +103,7 @@ int Driver::addSecRule(Rule *rule) {
m_parserError << std::to_string(rule->m_lineNumber) << std::endl;
return false;
}

for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
Rules *rules = m_rulesSetPhases[i];
for (int j = 0; j < rules->size(); j++) {
Expand All @@ -129,14 +115,15 @@ int Driver::addSecRule(Rule *rule) {
}
}

lastRule = rule;
m_lastRule = rule.get();
m_rulesSetPhases.insert(rule);

return true;
}


int Driver::parse(const std::string &f, const std::string &ref) {
lastRule = NULL;
m_lastRule = nullptr;
loc.push_back(new yy::location());
if (ref.empty()) {
loc.back()->begin.filename = loc.back()->end.filename = new std::string("<<reference missing or not informed>>");
Expand All @@ -155,6 +142,19 @@ int Driver::parse(const std::string &f, const std::string &ref) {
int res = parser.parse();
scan_end();

/*
* need to check for rules marked as chained but without
* a chained rule.
*
*/
/*
if (m_lastRule != nullptr && m_lastRule->m_chained) {
m_parserError << "Last rule is marked as chained but there " \
"isn't a subsequent rule." << std::endl;
return false;
}
*/

/*
if (m_auditLog->init(&error) == false) {
m_parserError << "Problems while initializing the audit logs: " \
Expand Down
8 changes: 4 additions & 4 deletions src/parser/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class Driver : public RulesSetProperties {
Driver();
virtual ~Driver();

int addSecRule(Rule *rule);
int addSecAction(Rule *rule);
int addSecRule(std::unique_ptr<Rule> rule);
int addSecAction(std::unique_ptr<Rule> rule);
int addSecMarker(std::string marker);
int addSecRuleScript(RuleScript *rule);
int addSecRuleScript(std::unique_ptr<RuleScript> rule);

bool scan_begin();
void scan_end();
Expand All @@ -89,7 +89,7 @@ class Driver : public RulesSetProperties {
std::list<yy::location *> loc;

std::string buffer;
Rule *lastRule;
Rule *m_lastRule;

RulesSetPhases m_rulesSetPhases;
};
Expand Down
2 changes: 1 addition & 1 deletion src/parser/location.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// A Bison parser, made by GNU Bison 3.5.2.
// A Bison parser, made by GNU Bison 3.5.3.

// Locations for Bison parsers in C++

Expand Down
2 changes: 1 addition & 1 deletion src/parser/position.hh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// A Bison parser, made by GNU Bison 3.5.2.
// A Bison parser, made by GNU Bison 3.5.3.

// Starting with Bison 3.2, this file is useless: the structure it
// used to define is now defined in "location.hh".
Expand Down
Loading

0 comments on commit 9d15861

Please sign in to comment.