Skip to content

Commit b9836bc

Browse files
authored
Merge pull request #3005 from martinhsv/v3/master
Fix memory leaks in ValidateSchema
2 parents fd67c6e + b180de5 commit b9836bc

File tree

3 files changed

+45
-72
lines changed

3 files changed

+45
-72
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- Fix memory leaks in ValidateSchema
5+
[Issue #3005 - @martinhsv, @zimmerle]
46
- Add support for expirevar action
57
[Issue #1803, #3001 - @martinhsv]
68
- Fix: lmdb regex match on non-null terminated string

src/operators/validate_schema.cc

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ModSecurity, http://www.modsecurity.org/
3-
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
3+
* Copyright (c) 2015 - 2023 Trustwave Holdings, Inc. (http://www.trustwave.com/)
44
*
55
* You may not use this file except in compliance with
66
* the License. You may obtain a copy of the License at
@@ -39,97 +39,88 @@ bool ValidateSchema::init(const std::string &file, std::string *error) {
3939
}
4040

4141

42-
bool ValidateSchema::evaluate(Transaction *t,
42+
bool ValidateSchema::evaluate(Transaction *transaction,
4343
const std::string &str) {
44-
int rc;
4544

46-
m_parserCtx = xmlSchemaNewParserCtxt(m_resource.c_str());
47-
if (m_parserCtx == NULL) {
45+
if (transaction->m_xml->m_data.doc == NULL) {
46+
ms_dbg_a(transaction, 4, "XML document tree could not be found for " \
47+
"schema validation.");
48+
return true;
49+
}
50+
51+
if (transaction->m_xml->m_data.well_formed != 1) {
52+
ms_dbg_a(transaction, 4, "XML: Schema validation failed because " \
53+
"content is not well formed.");
54+
return true;
55+
}
56+
57+
xmlSchemaParserCtxtPtr parserCtx = xmlSchemaNewParserCtxt(m_resource.c_str());
58+
if (parserCtx == NULL) {
4859
std::stringstream err;
4960
err << "XML: Failed to load Schema from file: ";
5061
err << m_resource;
5162
err << ". ";
5263
if (m_err.empty() == false) {
5364
err << m_err;
5465
}
55-
ms_dbg_a(t, 4, err.str());
66+
ms_dbg_a(transaction, 4, err.str());
5667
return true;
5768
}
5869

59-
xmlSchemaSetParserErrors(m_parserCtx,
70+
xmlSchemaSetParserErrors(parserCtx,
6071
(xmlSchemaValidityErrorFunc)error_load,
6172
(xmlSchemaValidityWarningFunc)warn_load, &m_err);
6273

63-
xmlThrDefSetGenericErrorFunc(m_parserCtx,
74+
xmlThrDefSetGenericErrorFunc(parserCtx,
6475
null_error);
6576

66-
xmlSetGenericErrorFunc(m_parserCtx,
77+
xmlSetGenericErrorFunc(parserCtx,
6778
null_error);
6879

69-
m_schema = xmlSchemaParse(m_parserCtx);
70-
if (m_schema == NULL) {
80+
xmlSchemaPtr schema = xmlSchemaParse(parserCtx);
81+
if (schema == NULL) {
7182
std::stringstream err;
7283
err << "XML: Failed to load Schema: ";
7384
err << m_resource;
7485
err << ".";
7586
if (m_err.empty() == false) {
7687
err << " " << m_err;
7788
}
78-
ms_dbg_a(t, 4, err.str());
79-
xmlSchemaFreeParserCtxt(m_parserCtx);
89+
ms_dbg_a(transaction, 4, err.str());
90+
xmlSchemaFreeParserCtxt(parserCtx);
8091
return true;
8192
}
8293

83-
m_validCtx = xmlSchemaNewValidCtxt(m_schema);
84-
if (m_validCtx == NULL) {
94+
xmlSchemaValidCtxtPtr validCtx = xmlSchemaNewValidCtxt(schema);
95+
if (validCtx == NULL) {
8596
std::stringstream err("XML: Failed to create validation context.");
8697
if (m_err.empty() == false) {
8798
err << " " << m_err;
8899
}
89-
ms_dbg_a(t, 4, err.str());
100+
ms_dbg_a(transaction, 4, err.str());
101+
xmlSchemaFree(schema);
102+
xmlSchemaFreeParserCtxt(parserCtx);
90103
return true;
91104
}
92105

93106
/* Send validator errors/warnings to msr_log */
94-
xmlSchemaSetValidErrors(m_validCtx,
107+
xmlSchemaSetValidErrors(validCtx,
95108
(xmlSchemaValidityErrorFunc)error_runtime,
96-
(xmlSchemaValidityWarningFunc)warn_runtime, t);
109+
(xmlSchemaValidityWarningFunc)warn_runtime, transaction);
97110

98-
if (t->m_xml->m_data.doc == NULL) {
99-
ms_dbg_a(t, 4, "XML document tree could not be found for " \
100-
"schema validation.");
101-
return true;
102-
}
111+
int rc = xmlSchemaValidateDoc(validCtx, transaction->m_xml->m_data.doc);
103112

104-
if (t->m_xml->m_data.well_formed != 1) {
105-
ms_dbg_a(t, 4, "XML: Schema validation failed because " \
106-
"content is not well formed.");
107-
return true;
108-
}
109-
110-
/* Make sure there were no other generic processing errors */
111-
/*
112-
if (msr->msc_reqbody_error) {
113-
ms_dbg_a(t, 4, "XML: Schema validation could not proceed due to previous"
114-
" processing errors.");
115-
return true;
116-
}
117-
*/
118-
119-
rc = xmlSchemaValidateDoc(m_validCtx, t->m_xml->m_data.doc);
113+
xmlSchemaFreeValidCtxt(validCtx);
114+
xmlSchemaFree(schema);
115+
xmlSchemaFreeParserCtxt(parserCtx);
120116
if (rc != 0) {
121-
ms_dbg_a(t, 4, "XML: Schema validation failed.");
122-
xmlSchemaFree(m_schema);
123-
xmlSchemaFreeParserCtxt(m_parserCtx);
117+
ms_dbg_a(transaction, 4, "XML: Schema validation failed.");
124118
return true; /* No match. */
119+
} else {
120+
ms_dbg_a(transaction, 4, "XML: Successfully validated payload against " \
121+
"Schema: " + m_resource);
122+
return false;
125123
}
126-
127-
ms_dbg_a(t, 4, "XML: Successfully validated payload against " \
128-
"Schema: " + m_resource);
129-
xmlSchemaFree(m_schema);
130-
xmlSchemaFreeParserCtxt(m_parserCtx);
131-
132-
return false;
133124
}
134125

135126
#endif

src/operators/validate_schema.h

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ModSecurity, http://www.modsecurity.org/
3-
* Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
3+
* Copyright (c) 2015 - 2023 Trustwave Holdings, Inc. (http://www.trustwave.com/)
44
*
55
* You may not use this file except in compliance with
66
* the License. You may obtain a copy of the License at
@@ -36,27 +36,10 @@ namespace operators {
3636
class ValidateSchema : public Operator {
3737
public:
3838
/** @ingroup ModSecurity_Operator */
39-
#ifndef WITH_LIBXML2
4039
explicit ValidateSchema(std::unique_ptr<RunTimeString> param)
4140
: Operator("ValidateSchema", std::move(param)) { }
42-
#else
43-
explicit ValidateSchema(std::unique_ptr<RunTimeString> param)
44-
: Operator("ValidateSchema", std::move(param)),
45-
m_parserCtx(NULL),
46-
m_validCtx(NULL),
47-
m_schema(NULL) { }
48-
~ValidateSchema() {
49-
/*
50-
if (m_schema != NULL) {
51-
xmlSchemaFree(m_schema);
52-
m_schema = NULL;
53-
}
54-
*/
55-
if (m_validCtx != NULL) {
56-
xmlSchemaFreeValidCtxt(m_validCtx);
57-
m_validCtx = NULL;
58-
}
59-
}
41+
~ValidateSchema() { }
42+
#ifdef WITH_LIBXML2
6043

6144
bool evaluate(Transaction *transaction, const std::string &str) override;
6245
bool init(const std::string &file, std::string *error) override;
@@ -129,9 +112,6 @@ class ValidateSchema : public Operator {
129112
}
130113

131114
private:
132-
xmlSchemaParserCtxtPtr m_parserCtx;
133-
xmlSchemaValidCtxtPtr m_validCtx;
134-
xmlSchemaPtr m_schema;
135115
std::string m_resource;
136116
std::string m_err;
137117
#endif

0 commit comments

Comments
 (0)