Skip to content

Commit ecd7b97

Browse files
authored
Merge commit from fork
fix: prevent segmentation fault if the XML node is empty
2 parents c01e5db + 8879413 commit ecd7b97

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

apache2/msc_xml.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static void msc_xml_on_start_elementns(
3636
xml_parser_state->pathlen += (taglen + 1);
3737
char *newpath = apr_pstrcat(msr->mp, xml_parser_state->currpath, ".", (char *)localname, NULL);
3838
xml_parser_state->currpath = newpath;
39+
xml_parser_state->currpathbufflen += taglen + 1; // +1 for the '.' character here too
3940

4041
int *new_stack_item = (int *)apr_array_push(xml_parser_state->has_child_stack);
4142
*new_stack_item = 0;
@@ -44,6 +45,7 @@ static void msc_xml_on_start_elementns(
4445
// this is necessary because if there is any text between the tags (new line, etc)
4546
// it will be added to the current value
4647
xml_parser_state->currval = NULL;
48+
xml_parser_state->currvalbufflen = 0;
4749

4850
// if there is an item before the current one we set that has a child
4951
if (xml_parser_state->depth > 1) {
@@ -72,8 +74,12 @@ static void msc_xml_on_end_elementns(
7274
if (apr_table_elts(msr->arguments)->nelts >= msr->txcfg->arguments_limit) {
7375
if (msr->txcfg->debuglog_level >= 4) {
7476
msr_log(msr, 4, "Skipping request argument, over limit (XML): name \"%s\", value \"%s\"",
75-
log_escape_ex(msr->mp, xml_parser_state->currpath, strlen(xml_parser_state->currpath)),
76-
log_escape_ex(msr->mp, xml_parser_state->currval, strlen(xml_parser_state->currval)));
77+
log_escape_ex(msr->mp, xml_parser_state->currpath, xml_parser_state->currpathbufflen),
78+
log_escape_ex(msr->mp,
79+
(xml_parser_state->currval == NULL ? apr_pstrndup(msr->mp, "", 1) : xml_parser_state->currval),
80+
(xml_parser_state->currvalbufflen == 0 ? 1 : xml_parser_state->currvalbufflen)
81+
)
82+
);
7783
}
7884
msr->msc_reqbody_error = 1;
7985
msr->xml->xml_error = apr_psprintf(msr->mp, "More than %ld ARGS (GET + XML)", msr->txcfg->arguments_limit);
@@ -84,15 +90,15 @@ static void msc_xml_on_end_elementns(
8490
msc_arg * arg = (msc_arg *) apr_pcalloc(msr->mp, sizeof(msc_arg));
8591

8692
arg->name = xml_parser_state->currpath;
87-
arg->name_len = strlen(arg->name);
88-
arg->value = xml_parser_state->currval;
89-
arg->value_len = strlen(xml_parser_state->currval);
93+
arg->name_len = xml_parser_state->currpathbufflen;
94+
arg->value = (xml_parser_state->currval == NULL) ? apr_pstrndup(msr->mp, "", 1) : xml_parser_state->currval;
95+
arg->value_len = (xml_parser_state->currvalbufflen == 0) ? 1 : xml_parser_state->currvalbufflen;
9096
arg->value_origin_len = arg->value_len;
9197
arg->origin = "XML";
9298

9399
if (msr->txcfg->debuglog_level >= 9) {
94100
msr_log(msr, 9, "Adding XML argument '%s' with value '%s'",
95-
xml_parser_state->currpath, xml_parser_state->currval);
101+
arg->name, arg->value);
96102
}
97103

98104
apr_table_addn(msr->arguments,
@@ -106,9 +112,11 @@ static void msc_xml_on_end_elementns(
106112
// -1 is needed because we don't need the last '.'
107113
char * newpath = apr_pstrndup(msr->mp, xml_parser_state->currpath, xml_parser_state->pathlen - 1);
108114
xml_parser_state->currpath = newpath;
115+
xml_parser_state->currpathbufflen = xml_parser_state->pathlen - 1;
109116

110117
xml_parser_state->depth--;
111118
xml_parser_state->currval = NULL;
119+
xml_parser_state->currvalbufflen = 0;
112120
}
113121

114122
static void msc_xml_on_characters(void *ctx, const xmlChar *ch, int len) {
@@ -123,6 +131,7 @@ static void msc_xml_on_characters(void *ctx, const xmlChar *ch, int len) {
123131
((xml_parser_state->currval != NULL) ? xml_parser_state->currval : ""),
124132
apr_pstrndup(msr->mp, (const char *)ch, len),
125133
NULL);
134+
xml_parser_state->currvalbufflen += len;
126135
// check if the memory allocation was successful
127136
if (xml_parser_state->currval == NULL) {
128137
msr->xml->xml_error = apr_psprintf(msr->mp, "Failed to allocate memory for XML value.");
@@ -174,8 +183,9 @@ int xml_init(modsec_rec *msr, char **error_msg) {
174183
msr->xml->xml_parser_state->depth = 0;
175184
msr->xml->xml_parser_state->pathlen = 4; // "xml\0"
176185
msr->xml->xml_parser_state->currpath = apr_pstrdup(msr->mp, "xml");
186+
msr->xml->xml_parser_state->currpathbufflen = 3; // "xml"
177187
msr->xml->xml_parser_state->currval = NULL;
178-
msr->xml->xml_parser_state->currpathbufflen = 4;
188+
msr->xml->xml_parser_state->currvalbufflen = 0;
179189
// initialize the stack with item of 10
180190
// this will store the information about nodes
181191
// 10 is just an initial value, it can be automatically incremented

apache2/msc_xml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct msc_xml_parser_state {
3131
char * currpath;
3232
char * currval;
3333
size_t currpathbufflen;
34+
size_t currvalbufflen;
3435
apr_pool_t * mp;
3536
};
3637

0 commit comments

Comments
 (0)