Skip to content

Commit

Permalink
Detect infinite recursion in parameter entities
Browse files Browse the repository at this point in the history
When expanding a parameter entity in a DTD, infinite recursion could
lead to an infinite loop or memory exhaustion.

Thanks to Wei Lei for the first of many reports.

Fixes bug 759579.
  • Loading branch information
nwellnhof committed Jul 25, 2017
1 parent fb56f80 commit 899a5d9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
11 changes: 10 additions & 1 deletion parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,13 @@ xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
xmlGenericError(xmlGenericErrorContext,
"Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
}
if (((ctxt->inputNr > 40) && ((ctxt->options & XML_PARSE_HUGE) == 0)) ||
(ctxt->inputNr > 1024)) {
xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
while (ctxt->inputNr > 1)
xmlFreeInputStream(inputPop(ctxt));
return(-1);
}
ret = inputPush(ctxt, input);
if (ctxt->instate == XML_PARSER_EOF)
return(-1);
Expand Down Expand Up @@ -7916,8 +7923,10 @@ xmlParsePEReference(xmlParserCtxtPtr ctxt)
return;

input = xmlNewEntityInputStream(ctxt, entity);
if (xmlPushInput(ctxt, input) < 0)
if (xmlPushInput(ctxt, input) < 0) {
xmlFreeInputStream(input);
return;
}

if (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) {
/*
Expand Down
Empty file added result/errors/759579.xml
Empty file.
6 changes: 6 additions & 0 deletions result/errors/759579.xml.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Entity: line 2: parser error : Detected an entity reference loop
%z; %z; %z; %z; %z;
^
Entity: line 2:
%z; %z; %z; %z; %z;
^
7 changes: 7 additions & 0 deletions result/errors/759579.xml.str
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Entity: line 2: parser error : Detected an entity reference loop
%z; %z; %z; %z; %z;
^
Entity: line 2:
%z; %z; %z; %z; %z;
^
./test/errors/759579.xml : failed to parse
11 changes: 11 additions & 0 deletions test/errors/759579.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE doc [
<!ENTITY % z '
&#37;z; &#37;z; &#37;z; &#37;z; &#37;z;
&#37;z; &#37;z; &#37;z; &#37;z; &#37;z;
&#37;z; &#37;z; &#37;z; &#37;z; &#37;z;
&#37;z; &#37;z; &#37;z; &#37;z; &#37;z;
&#37;z; &#37;z; &#37;z; &#37;z; &#37;z;
'>
%z;
]>
<doc/>

1 comment on commit 899a5d9

@reachkurian
Copy link

@reachkurian reachkurian commented on 899a5d9 Apr 1, 2019

Choose a reason for hiding this comment

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

As per
https://security-tracker.debian.org/tracker/CVE-2017-16932
"Applying only 899a5d9 does not completely"
Please let know any other code modification related to this CVE.

Please sign in to comment.