Skip to content

Commit 1822fc7

Browse files
StanFromIrelandpicnixz
authored andcommitted
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) 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 8469402 commit 1822fc7

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
@@ -688,6 +688,25 @@ def test_trigger_leak(self):
688688
parser.ElementDeclHandler = lambda _1, _2: None
689689
self.assertRaises(TypeError, parser.Parse, data, True)
690690

691+
@support.skip_if_unlimited_stack_size
692+
@support.skip_emscripten_stack_overflow()
693+
@support.skip_wasi_stack_overflow()
694+
def test_deeply_nested_content_model(self):
695+
# This should raise a RecursionError and not crash.
696+
# See https://github.com/python/cpython/issues/145986.
697+
N = 500_000
698+
data = (
699+
b'<!DOCTYPE root [\n<!ELEMENT root '
700+
+ b'(a, ' * N + b'a' + b')' * N
701+
+ b'>\n]>\n<root/>\n'
702+
)
703+
704+
parser = expat.ParserCreate()
705+
parser.ElementDeclHandler = lambda _1, _2: None
706+
with support.infinite_recursion():
707+
with self.assertRaises(RecursionError):
708+
parser.Parse(data)
709+
691710
class MalformedInputTest(unittest.TestCase):
692711
def test1(self):
693712
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()
@@ -572,6 +573,10 @@ static PyObject *
572573
conv_content_model(XML_Content * const model,
573574
PyObject *(*conv_string)(const XML_Char *))
574575
{
576+
if (_Py_EnterRecursiveCall(" in conv_content_model")) {
577+
return NULL;
578+
}
579+
575580
PyObject *result = NULL;
576581
PyObject *children = PyTuple_New(model->numchildren);
577582
int i;
@@ -583,14 +588,16 @@ conv_content_model(XML_Content * const model,
583588
conv_string);
584589
if (child == NULL) {
585590
Py_XDECREF(children);
586-
return NULL;
591+
goto done;
587592
}
588593
PyTuple_SET_ITEM(children, i, child);
589594
}
590595
result = Py_BuildValue("(iiO&N)",
591596
model->type, model->quant,
592597
conv_string,model->name, children);
593598
}
599+
done:
600+
_Py_LeaveRecursiveCall();
594601
return result;
595602
}
596603

0 commit comments

Comments
 (0)