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 committed by Felipe Zimmerle
parent 8fa4fc67af
commit 820396f784
24 changed files with 255 additions and 172 deletions

View File

@ -78,19 +78,15 @@ class AnchoredSetVariable : public std::unordered_multimap<std::string,
void unset();
void set(const std::string &key, const std::string &value,
size_t offset);
void set(const std::string &key, const bpstd::string_view &value,
size_t offset);
void set(const std::string &key, const char *value,
size_t offset);
void set(const std::string &key, const std::string &value, size_t offset) {
set(key, value, offset, value.size());
}
void set(const std::string &key, const std::string &value,
size_t offset, size_t len);
void setCopy(std::string key, std::string value, size_t offset);
void set(const std::string &key, const bpstd::string_view &value,
size_t offset);
void resolve(VariableValues *l);
void resolve(VariableValues *l,

View File

@ -626,18 +626,7 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
int m_secRuleEngine;
std::string m_variableDuration;
std::map<std::string, std::string> m_variableEnvs;
std::string m_variableHighestSeverityAction;
std::string m_variableRemoteUser;
std::string m_variableTime;
std::string m_variableTimeDay;
std::string m_variableTimeEpoch;
std::string m_variableTimeHour;
std::string m_variableTimeMin;
std::string m_variableTimeSec;
std::string m_variableTimeWDay;
std::string m_variableTimeYear;
std::vector<std::shared_ptr<RequestBodyProcessor::MultipartPartTmpFile>> m_multipartPartTmpFiles;

View File

@ -20,9 +20,13 @@
#include <memory>
#include <list>
#include <utility>
#include <vector>
#endif
#include "modsecurity/variable_origin.h"
#ifdef __cplusplus
#include "modsecurity/string_view.hpp"
#endif
#ifndef HEADERS_MODSECURITY_VARIABLE_VALUE_H_
#define HEADERS_MODSECURITY_VARIABLE_VALUE_H_
@ -38,70 +42,208 @@ class VariableValue;
using VariableValues = std::vector<std::shared_ptr<const VariableValue>>;
using Origins = std::vector<VariableOrigin>;
class Collection;
class VariableValue {
public:
/**
*
*
* Use cases for VariableValue creation:
*
* AnchoredSet - Use case A (eg. ARGS). - Collection + Key
* Anchored - Use case B (eg. REQUEST_URI). - Key
* Custom - Use case C (eg. WEBAPP_ID). - Key
* CustomSet
* Fixed - Use case D (eg. TX). - Collection + Key
* Dynamic - Use case E (eg. ENV). - Collection + Key
*
*
* | Key | Collec. | Key + Collec. | Value | Full Name
* A | & | & | * | & | *
* B | x | & | * | & | &
* C | x | & | * | & | &
* D | & | & | * | & | *
* E | & | & | * | * | *
*
*
*
* Currently big picture of getName and getKey:
*
* getName()
* - Lua Engine - RuleWithOperator - Transaction
* - UpdateMatchedVar - logging (audit)
* - ExecuteOperatorAt
* - ResolveMatchMsg
* - RulesExceptions (key and value)
*
*
* getKey()
* - Transaction - Variable
* - LogGen - Contains
* - Regexp
*
*
* Performance strategy: Delay the name resolution till is really necessary.
*
*
*/
explicit VariableValue(const std::string *key,
/**
*
* Use case C + VariableModificatorCount
*
*
**/
explicit VariableValue(const std::string *collection,
const std::string *value = nullptr)
: m_collection(""),
m_key(*key),
m_keyWithCollection(*key),
m_value(value != nullptr?*value:"")
{ }
: m_origin(),
m_value(),
m_valueHolder(new std::string(value != nullptr?*value:"")), // FIXME: do we really need a copy here?
m_key(nullptr),
m_keyHolder(nullptr),
m_collection(collection)
{
m_value = m_valueHolder.get();
};
/* Use case D.1. - ARGS */
VariableValue(const std::string *collection,
std::unique_ptr<std::string> key,
std::unique_ptr<std::string> value)
: m_origin(),
m_value(nullptr),
m_valueHolder(std::move(value)),
m_key(nullptr),
m_keyHolder(std::move(key)),
m_collection(collection)
{
m_value = m_valueHolder.get();
m_key = m_keyHolder.get();
};
/* Use case D.2. - RULE */
VariableValue(const std::string *collection,
const std::string *key,
std::unique_ptr<std::string> value)
: m_origin(),
m_value(nullptr),
m_valueHolder(std::move(value)),
m_key(key),
m_keyHolder(nullptr),
m_collection(collection)
{
m_value = m_valueHolder.get();
};
/* Use case D.3. - TX */
VariableValue(const std::string *collection,
const std::string *key,
const std::string *value)
: m_collection(*collection),
m_key(*key),
m_keyWithCollection(*collection + ":" + *key),
m_value(*value)
{ }
: m_origin(),
m_value(value),
m_valueHolder(nullptr),
m_key(key),
m_keyHolder(nullptr),
m_collection(collection)
{ };
// FIXME: It maybe the case for VariableValue to use string_view for everything.
/* Use case D.4. - MATCHED_VARS */
VariableValue(const std::string *collection,
const std::string *key,
const bpstd::string_view *value)
: m_origin(),
m_value(),
m_valueHolder(std::unique_ptr<std::string>(new std::string(value->c_str()))),
m_key(key),
m_keyHolder(nullptr),
m_collection(collection)
{
m_value = m_valueHolder.get();
};
/* Use case E.1. - Env */
VariableValue(std::unique_ptr<std::string> value,
std::unique_ptr<std::string> key,
std::shared_ptr<std::string> collection)
: m_origin(),
m_value(nullptr),
m_valueHolder(std::move(value)),
m_key(nullptr),
m_keyHolder(std::move(key)),
m_collection(collection.get())
{
m_value = m_valueHolder.get();
m_key = m_keyHolder.get();
};
/* Use case E.2. - DURATION */
VariableValue(std::unique_ptr<std::string> value,
const std::string *collection)
: m_origin(),
m_value(nullptr),
m_valueHolder(std::move(value)),
m_key(nullptr),
m_keyHolder(nullptr),
m_collection(collection)
{
m_value = m_valueHolder.get();
};
VariableValue(const VariableValue &o) = delete;
const std::string& getName() const noexcept {
return m_keyWithCollection;
}
VariableValue operator=(const VariableValue &o) = delete;
const std::string& getValue() const noexcept {
return m_value;
return *m_value;
}
const std::string& getKey() const {
return m_key;
const std::string& getKey() const noexcept {
return *m_key;
}
const std::string& getCollection() const {
return m_collection;
inline const std::string getName() const noexcept {
if (m_key == nullptr || m_key->empty()) {
return *m_collection;
}
return *m_collection + ":" + *m_key;
}
void setValue(const std::string &value) {
m_value = value;
m_value = &value;
}
void addOrigin(VariableOrigin origin) {
m_orign.push_back(std::move(origin));
m_origin.push_back(std::move(origin));
}
const Origins& getOrigin() const {
return m_orign;
return m_origin;
}
private:
Origins m_orign;
std::string m_collection;
std::string m_key;
std::string m_keyWithCollection;
std::string m_value;
Origins m_origin;
const std::string *m_value;
std::unique_ptr<std::string> m_valueHolder;
const std::string *m_key;
std::unique_ptr<std::string> m_keyHolder;
const std::string *m_collection;
};
} // namespace modsecurity

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

@ -34,7 +34,8 @@ AnchoredVariable::AnchoredVariable(Transaction *t,
m_offset(0),
m_name(name),
m_value(""),
m_var(std::make_shared<VariableValue>(&name)) {
m_var() {
m_var = std::make_shared<VariableValue>(&m_name);
}
void AnchoredVariable::unset() {

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,7 @@ 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);
auto var = std::make_shared<VariableValue>(&m_retName, &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;
}