Skip to content

Commit eb0e8be

Browse files
gh-145986: Avoid unbound C recursion in conv_content_model in pyexpat.c (CVE 2026-4224) (#145987)
Fix C stack overflow (CVE-2026-4224) when an Expat parser with a registered `ElementDeclHandler` parses inline DTD containing deeply nested content model. --------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 33044b0 commit eb0e8be

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Lib/test/test_pyexpat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,25 @@ def test_trigger_leak(self):
701701
parser.ElementDeclHandler = lambda _1, _2: None
702702
self.assertRaises(TypeError, parser.Parse, data, True)
703703

704+
@support.skip_if_unlimited_stack_size
705+
@support.skip_emscripten_stack_overflow()
706+
@support.skip_wasi_stack_overflow()
707+
def test_deeply_nested_content_model(self):
708+
# This should raise a RecursionError and not crash.
709+
# See https://github.com/python/cpython/issues/145986.
710+
N = 500_000
711+
data = (
712+
b'<!DOCTYPE root [\n<!ELEMENT root '
713+
+ b'(a, ' * N + b'a' + b')' * N
714+
+ b'>\n]>\n<root/>\n'
715+
)
716+
717+
parser = expat.ParserCreate()
718+
parser.ElementDeclHandler = lambda _1, _2: None
719+
with support.infinite_recursion():
720+
with self.assertRaises(RecursionError):
721+
parser.Parse(data)
722+
704723
class MalformedInputTest(unittest.TestCase):
705724
def test1(self):
706725
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44

55
#include "Python.h"
6+
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
67
#include "pycore_import.h" // _PyImport_SetModule()
78
#include "pycore_pyhash.h" // _Py_HashSecret
89
#include "pycore_traceback.h" // _PyTraceback_Add()
@@ -607,6 +608,10 @@ static PyObject *
607608
conv_content_model(XML_Content * const model,
608609
PyObject *(*conv_string)(void *))
609610
{
611+
if (_Py_EnterRecursiveCall(" in conv_content_model")) {
612+
return NULL;
613+
}
614+
610615
PyObject *result = NULL;
611616
PyObject *children = PyTuple_New(model->numchildren);
612617
int i;
@@ -618,14 +623,16 @@ conv_content_model(XML_Content * const model,
618623
conv_string);
619624
if (child == NULL) {
620625
Py_XDECREF(children);
621-
return NULL;
626+
goto done;
622627
}
623628
PyTuple_SET_ITEM(children, i, child);
624629
}
625630
result = Py_BuildValue("(iiO&N)",
626631
model->type, model->quant,
627632
conv_string, model->name, children);
628633
}
634+
done:
635+
_Py_LeaveRecursiveCall();
629636
return result;
630637
}
631638

0 commit comments

Comments
 (0)