Skip to content

Commit aa018d4

Browse files
martinhsvFelipe Zimmerle
authored andcommitted
Fix memory leaks in ValidateSchema
1 parent 571a59b commit aa018d4

File tree

3 files changed

+36
-64
lines changed

3 files changed

+36
-64
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+
[#2469 - @martinhsv, @zimmerle]
46
- Fix memory leak of ValidateDTD's m_dtd
57
[#2469 - @martinhsv, @zimmerle]
68
- Replaces put with setenv in SetEnv action

src/operators/validate_schema.cc

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ bool ValidateSchema::evaluate(Transaction *transaction,
4343
const RuleWithActions *rule,
4444
const bpstd::string_view &str,
4545
RuleMessage *ruleMessage) {
46-
int rc;
4746

48-
m_parserCtx = xmlSchemaNewParserCtxt(m_resource.c_str());
49-
if (m_parserCtx == NULL) {
47+
if (transaction->m_xml->m_data.doc == NULL) {
48+
ms_dbg_a(transaction, 4, "XML document tree could not be found for " \
49+
"schema validation.");
50+
return true;
51+
}
52+
if (transaction->m_xml->m_data.well_formed != 1) {
53+
ms_dbg_a(transaction, 4, "XML: Schema validation failed because " \
54+
"content is not well formed.");
55+
return true;
56+
}
57+
58+
xmlSchemaParserCtxtPtr parserCtx = xmlSchemaNewParserCtxt(m_resource.c_str());
59+
if (parserCtx == NULL) {
5060
std::stringstream err;
5161
err << "XML: Failed to load Schema from file: ";
5262
err << m_resource;
@@ -58,18 +68,18 @@ bool ValidateSchema::evaluate(Transaction *transaction,
5868
return true;
5969
}
6070

61-
xmlSchemaSetParserErrors(m_parserCtx,
71+
xmlSchemaSetParserErrors(parserCtx,
6272
(xmlSchemaValidityErrorFunc)error_load,
6373
(xmlSchemaValidityWarningFunc)warn_load, &m_err);
6474

65-
xmlThrDefSetGenericErrorFunc(m_parserCtx,
75+
xmlThrDefSetGenericErrorFunc(parserCtx,
6676
null_error);
6777

68-
xmlSetGenericErrorFunc(m_parserCtx,
78+
xmlSetGenericErrorFunc(parserCtx,
6979
null_error);
7080

71-
m_schema = xmlSchemaParse(m_parserCtx);
72-
if (m_schema == NULL) {
81+
xmlSchemaPtr schema = xmlSchemaParse(parserCtx);
82+
if (schema == NULL) {
7383
std::stringstream err;
7484
err << "XML: Failed to load Schema: ";
7585
err << m_resource;
@@ -78,60 +88,40 @@ bool ValidateSchema::evaluate(Transaction *transaction,
7888
err << " " << m_err;
7989
}
8090
ms_dbg_a(transaction, 4, err.str());
81-
xmlSchemaFreeParserCtxt(m_parserCtx);
91+
xmlSchemaFreeParserCtxt(parserCtx);
8292
return true;
8393
}
8494

85-
m_validCtx = xmlSchemaNewValidCtxt(m_schema);
86-
if (m_validCtx == NULL) {
95+
xmlSchemaValidCtxtPtr validCtx = xmlSchemaNewValidCtxt(schema);
96+
if (validCtx == NULL) {
8797
std::stringstream err("XML: Failed to create validation context.");
8898
if (m_err.empty() == false) {
8999
err << " " << m_err;
90100
}
91101
ms_dbg_a(transaction, 4, err.str());
102+
xmlSchemaFree(schema);
103+
xmlSchemaFreeParserCtxt(parserCtx);
92104
return true;
93105
}
94106

95107
/* Send validator errors/warnings to msr_log */
96-
xmlSchemaSetValidErrors(m_validCtx,
108+
xmlSchemaSetValidErrors(validCtx,
97109
(xmlSchemaValidityErrorFunc)error_runtime,
98110
(xmlSchemaValidityWarningFunc)warn_runtime, transaction);
99111

100-
if (transaction->m_xml->m_data.doc == NULL) {
101-
ms_dbg_a(transaction, 4, "XML document tree could not be found for " \
102-
"schema validation.");
103-
return true;
104-
}
105-
106-
if (transaction->m_xml->m_data.well_formed != 1) {
107-
ms_dbg_a(transaction, 4, "XML: Schema validation failed because " \
108-
"content is not well formed.");
109-
return true;
110-
}
111-
112-
/* Make sure there were no other generic processing errors */
113-
/*
114-
if (msr->msc_reqbody_error) {
115-
ms_dbg_a(t, 4, "XML: Schema validation could not proceed due to previous"
116-
" processing errors.");
117-
return true;
118-
}
119-
*/
112+
int rc = xmlSchemaValidateDoc(validCtx, transaction->m_xml->m_data.doc);
120113

121-
rc = xmlSchemaValidateDoc(m_validCtx, transaction->m_xml->m_data.doc);
114+
xmlSchemaFreeValidCtxt(validCtx);
115+
xmlSchemaFree(schema);
116+
xmlSchemaFreeParserCtxt(parserCtx);
122117
if (rc != 0) {
123118
ms_dbg_a(transaction, 4, "XML: Schema validation failed.");
124-
xmlSchemaFree(m_schema);
125-
xmlSchemaFreeParserCtxt(m_parserCtx);
126119
return true; /* No match. */
120+
} else {
121+
ms_dbg_a(transaction, 4, "XML: Successfully validated payload against " \
122+
"Schema: " + m_resource);
123+
return false;
127124
}
128-
129-
ms_dbg_a(transaction, 4, "XML: Successfully validated payload against " \
130-
"Schema: " + m_resource);
131-
xmlSchemaFree(m_schema);
132-
xmlSchemaFreeParserCtxt(m_parserCtx);
133-
134-
return false;
135125
}
136126

137127
#endif

src/operators/validate_schema.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
6245
const RuleWithActions *rule,
@@ -133,9 +116,6 @@ class ValidateSchema : public Operator {
133116
}
134117

135118
private:
136-
xmlSchemaParserCtxtPtr m_parserCtx;
137-
xmlSchemaValidCtxtPtr m_validCtx;
138-
xmlSchemaPtr m_schema;
139119
std::string m_resource;
140120
std::string m_err;
141121
#endif

0 commit comments

Comments
 (0)