Skip to content

Fix memory leak of ValidateDTD's dtd object #2478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
v3.x.y - YYYY-MMM-DD (to be released)
-------------------------------------

- Fix memory leak of ValidateDTD's m_dtd
[#2469 - @martinhsv, @zimmerle]
- Replaces put with setenv in SetEnv action
[#2469 - @martinhsv, @WGH-, @zimmerle]
- Using a custom VariableMatch* implementation
Expand Down
10 changes: 4 additions & 6 deletions src/operators/validate_dtd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ bool ValidateDTD::evaluate(Transaction *transaction,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
xmlValidCtxtPtr cvp;

m_dtd = xmlParseDTD(NULL, (const xmlChar *)m_resource.c_str());
if (m_dtd == NULL) {
XmlDtdPtrManager m_dtd(xmlParseDTD(NULL, (const xmlChar *)m_resource.c_str()));
if (m_dtd.get() == NULL) {
std::string err = std::string("XML: Failed to load DTD: ") \
+ m_resource;
ms_dbg_a(transaction, 4, err);
Expand Down Expand Up @@ -79,7 +77,7 @@ bool ValidateDTD::evaluate(Transaction *transaction,
}
#endif

cvp = xmlNewValidCtxt();
xmlValidCtxtPtr cvp = xmlNewValidCtxt();
if (cvp == NULL) {
ms_dbg_a(transaction, 4,
"XML: Failed to create a validation context.");
Expand All @@ -91,7 +89,7 @@ bool ValidateDTD::evaluate(Transaction *transaction,
cvp->warning = (xmlSchemaValidityErrorFunc)warn_runtime;
cvp->userData = transaction;

if (!xmlValidateDtd(cvp, transaction->m_xml->m_data.doc, m_dtd)) {
if (!xmlValidateDtd(cvp, transaction->m_xml->m_data.doc, m_dtd.get())) {
ms_dbg_a(transaction, 4, "XML: DTD validation failed.");
xmlFreeValidCtxt(cvp);
return true;
Expand Down
23 changes: 17 additions & 6 deletions src/operators/validate_dtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,30 @@
namespace modsecurity {
namespace operators {

class ValidateDTD : public Operator {
class XmlDtdPtrManager {
public:
/** @ingroup ModSecurity_Operator */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This marking is further used by doxygen, in order to place this class under the operators sub class.

explicit ValidateDTD(std::unique_ptr<RunTimeString> param)
: Operator("ValidateDTD", std::move(param)) { }
explicit XmlDtdPtrManager(xmlDtdPtr dtd)
: m_dtd(dtd) { }
~XmlDtdPtrManager() {
#ifdef WITH_LIBXML2
~ValidateDTD() {
if (m_dtd != NULL) {
xmlFreeDtd(m_dtd);
m_dtd = NULL;
}
#endif
}
xmlDtdPtr get() const {return m_dtd;}
private:
xmlDtdPtr m_dtd; // The resource being managed
};

class ValidateDTD : public Operator {
public:
/** @ingroup ModSecurity_Operator */
explicit ValidateDTD(std::unique_ptr<RunTimeString> param)
: Operator("ValidateDTD", std::move(param)) { }
#ifdef WITH_LIBXML2
~ValidateDTD() { }

bool evaluate(Transaction *transaction,
const RuleWithActions *rule,
Expand Down Expand Up @@ -93,7 +105,6 @@ class ValidateDTD : public Operator {

private:
std::string m_resource;
xmlDtdPtr m_dtd = NULL;
#endif
};

Expand Down