Reduce the workload on VariableValue

Last compute at the last minute, if needed.
This commit is contained in:
Felipe Zimmerle
2020-09-17 19:51:34 -03:00
parent 856a84106a
commit cc699bc794
23 changed files with 252 additions and 171 deletions

View File

@@ -41,9 +41,12 @@ void AnchoredSetVariable::unset() {
}
// FIXME: It may not be necessary to copy the content of
void AnchoredSetVariable::set(const std::string &key,
const std::string &value, size_t offset, size_t len) {
auto var = std::make_shared<VariableValue>(&m_name, &key, &value);
auto var = std::make_shared<VariableValue>(&m_name,
std::unique_ptr<std::string>(new std::string(key)),
std::unique_ptr<std::string>(new std::string(value)));
VariableOrigin origin;
origin.m_offset = offset;
@@ -55,41 +58,13 @@ void AnchoredSetVariable::set(const std::string &key,
void AnchoredSetVariable::set(const std::string &key,
const std::string &value, size_t offset) {
const bpstd::string_view &value, size_t offset) {
auto var = std::make_shared<VariableValue>(&m_name, &key, &value);
VariableOrigin origin;
origin.m_offset = offset;
origin.m_length = value.size();
var->addOrigin(std::move(origin));
emplace(key, std::move(var));
}
void AnchoredSetVariable::set(const std::string &key,
const bpstd::string_view &value, size_t offset) {
std::string v(value.c_str());
auto var = std::make_shared<VariableValue>(&m_name, &key, &v);
VariableOrigin origin;
origin.m_offset = offset;
origin.m_length = value.size();
var->addOrigin(std::move(origin));
emplace(key, var);
}
void AnchoredSetVariable::set(const std::string &key,
const char *value, size_t offset) {
std::string v(value);
auto var = std::make_shared<VariableValue>(&m_name, &key, &v);
VariableOrigin origin;
origin.m_offset = offset;
origin.m_length = strlen(value);
var->addOrigin(std::move(origin));
emplace(key, var);
}

View File

@@ -113,7 +113,8 @@ void InMemoryPerProcess::resolveMultiMatches(const std::string& var,
if (ke.toOmit(var)) {
continue;
}
l->insert(l->begin(), std::make_shared<VariableValue>(&m_name, &var, &it->second));
l->insert(l->begin(), std::make_shared<VariableValue>(&m_name, &it->first, &it->second));
}
}
}

View File

@@ -81,13 +81,15 @@ RuleWithOperator::~RuleWithOperator() {
void RuleWithOperator::updateMatchedVars(Transaction *trans,
const VariableValue *v,
const bpstd::string_view &value) {
const std::string &key = v->getName();
// FIXME: Memory leak.
const std::string *key = new std::string(v->getName());
ms_dbg_a(trans, 9, "Matched vars updated.");
trans->m_variableMatchedVar.set(value, trans->m_variableOffset);
trans->m_variableMatchedVarName.set(key, trans->m_variableOffset);
trans->m_variableMatchedVarName.set(*key, trans->m_variableOffset);
trans->m_variableMatchedVars.set(key, value, trans->m_variableOffset);
trans->m_variableMatchedVarsNames.set(key, key, trans->m_variableOffset);
trans->m_variableMatchedVars.set(*key, value, trans->m_variableOffset);
trans->m_variableMatchedVarsNames.set(*key, *key, trans->m_variableOffset);
}

View File

@@ -186,18 +186,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
m_json(NULL),
#endif
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
m_variableDuration(""),
m_variableEnvs(),
m_variableHighestSeverityAction(""),
m_variableRemoteUser(""),
m_variableTime(""),
m_variableTimeDay(""),
m_variableTimeEpoch(""),
m_variableTimeHour(""),
m_variableTimeMin(""),
m_variableTimeSec(""),
m_variableTimeWDay(""),
m_variableTimeYear(""),
m_logCbData(logCbData),
TransactionAnchoredVariables(this),
TransactionRuleMessageManagement(this) {
@@ -263,18 +252,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
m_json(NULL),
#endif
m_secRuleEngine(RulesSetProperties::PropertyNotSetRuleEngine),
m_variableDuration(""),
m_variableEnvs(),
m_variableHighestSeverityAction(""),
m_variableRemoteUser(""),
m_variableTime(""),
m_variableTimeDay(""),
m_variableTimeEpoch(""),
m_variableTimeHour(""),
m_variableTimeMin(""),
m_variableTimeSec(""),
m_variableTimeWDay(""),
m_variableTimeYear(""),
m_logCbData(logCbData),
TransactionAnchoredVariables(this),
TransactionRuleMessageManagement(this) {

View File

@@ -31,9 +31,9 @@ void Duration::evaluate(Transaction *transaction,
VariableValues *l) {
double e = utils::cpu_seconds() - transaction->m_creationTimeStamp;
transaction->m_variableDuration.assign(std::to_string(e));
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableDuration));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(std::to_string(e))),
&m_retName));
}

View File

@@ -32,28 +32,30 @@ extern char **environ;
namespace modsecurity {
namespace variables {
void Env::evaluate(Transaction *transaction,
VariableValues *l) {
void Env::evaluate(Transaction *transaction, VariableValues *l) {
bool checkForKey = getVariableKey()->length() > 0;
for (char **current = environ; *current; current++) {
std::string env = std::string(*current);
size_t pos = env.find_first_of("=");
if (pos == std::string::npos) {
continue;
}
std::string key = std::string(env, 0, pos);
std::string value = std::string(env, pos+1, env.length() - (pos + 1));
std::pair<std::string, std::string> a(key, value);
transaction->m_variableEnvs.insert(a);
}
std::unique_ptr<std::string> key(new std::string(env, 0, pos));
std::unique_ptr<std::string> value(new std::string(env, pos+1, env.length() - (pos + 1)));
for (auto& x : transaction->m_variableEnvs) {
if (x.first != *getVariableKey() && getVariableKey()->length() > 0) {
if (checkForKey && *key != *getVariableKey()) {
continue;
}
if (!m_keyExclusion.toOmit(x.first)) {
l->emplace_back(std::make_shared<VariableValue>(getVariableKeyWithCollection().get(),
&x.first, &x.second));
if (m_keyExclusion.toOmit(*key)) {
continue;
}
l->emplace_back(std::make_shared<VariableValue>(
std::move(value),
std::move(key),
getVariableKeyWithCollection()
));
}
}

View File

@@ -30,11 +30,10 @@ namespace variables {
class Env : public Variable {
public:
explicit Env(const std::string &_name)
: Variable(_name) { }
explicit Env(const std::string &name)
: Variable(name) { }
void evaluate(Transaction *transaction,
VariableValues *l) override;
void evaluate(Transaction *transaction, VariableValues *l) override;
};
} // namespace variables

View File

@@ -28,9 +28,9 @@ namespace variables {
void HighestSeverity::evaluate(Transaction *transaction,
VariableValues *l) {
transaction->m_variableHighestSeverityAction.assign(
std::to_string(transaction->m_highestSeverityAction));
l->push_back(std::make_shared<VariableValue>(getVariableKeyWithCollection().get(), &transaction->m_variableHighestSeverityAction));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(std::to_string(transaction->m_highestSeverityAction))),
getVariableKeyWithCollection().get()));
}

View File

@@ -63,7 +63,8 @@ void RemoteUser::evaluate(Transaction *transaction,
}
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
auto var = std::make_shared<VariableValue>(&l2[0]->getName(), &transaction->m_variableRemoteUser);
const std::string name = l2[0]->getName();
auto var = std::make_shared<VariableValue>(&name, &transaction->m_variableRemoteUser);
for (auto &i : l2[0]->getOrigin()) {
var->addOrigin(i);

View File

@@ -57,8 +57,7 @@ class Rule_DictElement : public RuleVariable, public VariableDictElement {
static void id(Transaction *t,
const RuleWithActions *rule,
VariableValues *l) {
std::string a = std::to_string(rule->getId());
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_id, &a);
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_id, std::unique_ptr<std::string>(new std::string(std::to_string(rule->getId()))));
VariableOrigin origin;
origin.m_offset = 0;
origin.m_length = 0;
@@ -73,8 +72,7 @@ class Rule_DictElement : public RuleVariable, public VariableDictElement {
VariableValues *l) {
if (rule->hasRevisionAction()) {
std::string a(rule->getRevision());
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_rev, &a);
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_rev, std::unique_ptr<std::string>(new std::string(rule->getRevision())));
VariableOrigin origin;
origin.m_offset = 0;
origin.m_length = 0;
@@ -90,8 +88,7 @@ class Rule_DictElement : public RuleVariable, public VariableDictElement {
VariableValues *l) {
if (rule->hasSeverityAction()) {
std::string a(std::to_string(rule->getSeverity()));
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_severity, &a);
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_severity, std::unique_ptr<std::string>(new std::string(std::to_string(rule->getSeverity()))));
VariableOrigin origin;
origin.m_offset = 0;
origin.m_length = 0;
@@ -106,8 +103,7 @@ class Rule_DictElement : public RuleVariable, public VariableDictElement {
VariableValues *l) {
if (rule->hasLogDataAction()) {
std::string a(rule->getLogData(t));
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_logdata, &a);
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_logdata, std::unique_ptr<std::string>(new std::string(rule->getLogData(t))));
VariableOrigin origin;
origin.m_offset = 0;
origin.m_length = 0;
@@ -121,8 +117,7 @@ class Rule_DictElement : public RuleVariable, public VariableDictElement {
VariableValues *l) {
if (rule->hasMessageAction()) {
std::string a(rule->getMessage(t));
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_msg, &a);
auto var = std::make_shared<VariableValue>(&m_rule, &m_rule_msg, std::unique_ptr<std::string>(new std::string(rule->getMessage(t))));
VariableOrigin origin;
origin.m_offset = 0;
origin.m_length = 0;

View File

@@ -46,8 +46,9 @@ void Time::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%H:%M:%S", &timeinfo);
transaction->m_variableTime.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTime));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeDay::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%d", &timeinfo);
transaction->m_variableTimeDay.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeDay));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -35,9 +35,10 @@ namespace variables {
void TimeEpoch::evaluate(Transaction *transaction,
VariableValues *l) {
transaction->m_variableTimeEpoch.assign(
std::to_string(std::time(nullptr)));
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeEpoch));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(std::to_string(std::time(nullptr)))),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeHour::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%H", &timeinfo);
transaction->m_variableTimeHour.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeHour));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeMin::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%M", &timeinfo);
transaction->m_variableTimeMin.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeMin));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -44,12 +44,12 @@ void TimeMon::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%m", &timeinfo);
int a = atoi(tstr);
a--;
//int a = atoi(tstr);
//a--;
transaction->m_variableTimeMin.assign(std::to_string(a));
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeMin));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeSec::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%S", &timeinfo);
transaction->m_variableTimeSec.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeSec));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeWDay::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%u", &timeinfo);
transaction->m_variableTimeWDay.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeWDay));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -45,9 +45,9 @@ void TimeYear::evaluate(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%Y", &timeinfo);
transaction->m_variableTimeYear.assign(tstr);
l->push_back(std::make_shared<VariableValue>(&m_retName, &transaction->m_variableTimeYear));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(tstr)),
&m_retName));
}

View File

@@ -772,8 +772,9 @@ class VariableModificatorCount : public Variable {
m_base->evaluate(t, &reslIn);
auto count = reslIn.size();
std::string res(std::to_string(count));
l->push_back(std::make_shared<VariableValue>(getVariableKeyWithCollection().get(), &res));
l->push_back(std::make_shared<VariableValue>(
std::unique_ptr<std::string>(new std::string(std::to_string(count))),
getVariableKeyWithCollection().get()));
return;
}