Skip to content

Commit 0c50fa4

Browse files
[3.10] gh-145986: Avoid unbound C recursion in conv_content_model in pyexpat.c (CVE 2026-4224) (GH-145987)
Fix C stack overflow (CVE-2026-4224) when an Expat parser with a registered `ElementDeclHandler` parses inline DTD containing deeply nested content model. --------- (cherry picked from commit eb0e8be) (cherry picked from commit e5caf45) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent ba6eba5 commit 0c50fa4

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/test/test_pyexpat.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from xml.parsers import expat
1717
from xml.parsers.expat import errors
1818

19-
from test.support import import_helper
19+
from test.support import import_helper, infinite_recursion
2020
from test.support import sortdict
2121

2222

@@ -648,6 +648,24 @@ def test_change_size_2(self):
648648
parser.Parse(xml2, True)
649649
self.assertEqual(self.n, 4)
650650

651+
class ElementDeclHandlerTest(unittest.TestCase):
652+
def test_deeply_nested_content_model(self):
653+
# This should raise a RecursionError and not crash.
654+
# See https://github.com/python/cpython/issues/145986.
655+
N = 500_000
656+
data = (
657+
b'<!DOCTYPE root [\n<!ELEMENT root '
658+
+ b'(a, ' * N + b'a' + b')' * N
659+
+ b'>\n]>\n<root/>\n'
660+
)
661+
662+
parser = expat.ParserCreate()
663+
parser.ElementDeclHandler = lambda _1, _2: None
664+
with infinite_recursion():
665+
with self.assertRaises(RecursionError):
666+
parser.Parse(data)
667+
668+
651669
class MalformedInputTest(unittest.TestCase):
652670
def test1(self):
653671
xml = b"\0\r\n"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`xml.parsers.expat`: Fixed a crash caused by unbounded C recursion when
2+
converting deeply nested XML content models with
3+
:meth:`~xml.parsers.expat.xmlparser.ElementDeclHandler`.
4+
This addresses :cve:`2026-4224`.

Modules/pyexpat.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ static PyObject *
574574
conv_content_model(XML_Content * const model,
575575
PyObject *(*conv_string)(const XML_Char *))
576576
{
577+
if (Py_EnterRecursiveCall(" in conv_content_model")) {
578+
return NULL;
579+
}
580+
577581
PyObject *result = NULL;
578582
PyObject *children = PyTuple_New(model->numchildren);
579583
int i;
@@ -585,14 +589,16 @@ conv_content_model(XML_Content * const model,
585589
conv_string);
586590
if (child == NULL) {
587591
Py_XDECREF(children);
588-
return NULL;
592+
goto done;
589593
}
590594
PyTuple_SET_ITEM(children, i, child);
591595
}
592596
result = Py_BuildValue("(iiO&N)",
593597
model->type, model->quant,
594598
conv_string,model->name, children);
595599
}
600+
done:
601+
Py_LeaveRecursiveCall();
596602
return result;
597603
}
598604

0 commit comments

Comments
 (0)