mirror of
https://github.com/python/cpython.git
synced 2026-05-06 12:49:07 -04:00
[3.10] gh-145986: Avoid unbound C recursion in conv_content_model in pyexpat.c (CVE 2026-4224) (GH-145987) (#146002)
* [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 commiteb0e8be3a7) (cherry picked from commite5caf45faa) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> * Update Misc/NEWS.d/next/Security/2026-03-14-17-31-39.gh-issue-145986.ifSSr8.rst --------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
@@ -16,7 +16,7 @@ import traceback
|
||||
from xml.parsers import expat
|
||||
from xml.parsers.expat import errors
|
||||
|
||||
from test.support import import_helper
|
||||
from test.support import import_helper, infinite_recursion
|
||||
from test.support import sortdict
|
||||
|
||||
|
||||
@@ -648,6 +648,24 @@ class ChardataBufferTest(unittest.TestCase):
|
||||
parser.Parse(xml2, True)
|
||||
self.assertEqual(self.n, 4)
|
||||
|
||||
class ElementDeclHandlerTest(unittest.TestCase):
|
||||
def test_deeply_nested_content_model(self):
|
||||
# This should raise a RecursionError and not crash.
|
||||
# See https://github.com/python/cpython/issues/145986.
|
||||
N = 500_000
|
||||
data = (
|
||||
b'<!DOCTYPE root [\n<!ELEMENT root '
|
||||
+ b'(a, ' * N + b'a' + b')' * N
|
||||
+ b'>\n]>\n<root/>\n'
|
||||
)
|
||||
|
||||
parser = expat.ParserCreate()
|
||||
parser.ElementDeclHandler = lambda _1, _2: None
|
||||
with infinite_recursion():
|
||||
with self.assertRaises(RecursionError):
|
||||
parser.Parse(data)
|
||||
|
||||
|
||||
class MalformedInputTest(unittest.TestCase):
|
||||
def test1(self):
|
||||
xml = b"\0\r\n"
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
:mod:`xml.parsers.expat`: Fixed a crash caused by unbounded C recursion when
|
||||
converting deeply nested XML content models with
|
||||
:meth:`~xml.parsers.expat.xmlparser.ElementDeclHandler`.
|
||||
This addresses `CVE-2026-4224 <https://www.cve.org/CVERecord?id=CVE-2026-4224>`_.
|
||||
+7
-1
@@ -574,6 +574,10 @@ static PyObject *
|
||||
conv_content_model(XML_Content * const model,
|
||||
PyObject *(*conv_string)(const XML_Char *))
|
||||
{
|
||||
if (Py_EnterRecursiveCall(" in conv_content_model")) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *result = NULL;
|
||||
PyObject *children = PyTuple_New(model->numchildren);
|
||||
int i;
|
||||
@@ -585,7 +589,7 @@ conv_content_model(XML_Content * const model,
|
||||
conv_string);
|
||||
if (child == NULL) {
|
||||
Py_XDECREF(children);
|
||||
return NULL;
|
||||
goto done;
|
||||
}
|
||||
PyTuple_SET_ITEM(children, i, child);
|
||||
}
|
||||
@@ -593,6 +597,8 @@ conv_content_model(XML_Content * const model,
|
||||
model->type, model->quant,
|
||||
conv_string,model->name, children);
|
||||
}
|
||||
done:
|
||||
Py_LeaveRecursiveCall();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user