Skip to content

Commit e0a8a6d

Browse files
miss-islingtonStanFromIrelandpicnixz
authored
[3.14] gh-145986: Avoid unbound C recursion in conv_content_model in pyexpat.c (CVE 2026-4224) (GH-145987) (#145995)
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 64e2acb commit e0a8a6d

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

692+
@support.skip_if_unlimited_stack_size
693+
@support.skip_emscripten_stack_overflow()
694+
@support.skip_wasi_stack_overflow()
695+
def test_deeply_nested_content_model(self):
696+
# This should raise a RecursionError and not crash.
697+
# See https://github.com/python/cpython/issues/145986.
698+
N = 500_000
699+
data = (
700+
b'<!DOCTYPE root [\n<!ELEMENT root '
701+
+ b'(a, ' * N + b'a' + b')' * N
702+
+ b'>\n]>\n<root/>\n'
703+
)
704+
705+
parser = expat.ParserCreate()
706+
parser.ElementDeclHandler = lambda _1, _2: None
707+
with support.infinite_recursion():
708+
with self.assertRaises(RecursionError):
709+
parser.Parse(data)
710+
692711
class MalformedInputTest(unittest.TestCase):
693712
def test1(self):
694713
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()
@@ -603,6 +604,10 @@ static PyObject *
603604
conv_content_model(XML_Content * const model,
604605
PyObject *(*conv_string)(void *))
605606
{
607+
if (_Py_EnterRecursiveCall(" in conv_content_model")) {
608+
return NULL;
609+
}
610+
606611
PyObject *result = NULL;
607612
PyObject *children = PyTuple_New(model->numchildren);
608613
int i;
@@ -614,14 +619,16 @@ conv_content_model(XML_Content * const model,
614619
conv_string);
615620
if (child == NULL) {
616621
Py_XDECREF(children);
617-
return NULL;
622+
goto done;
618623
}
619624
PyTuple_SET_ITEM(children, i, child);
620625
}
621626
result = Py_BuildValue("(iiO&N)",
622627
model->type, model->quant,
623628
conv_string, model->name, children);
624629
}
630+
done:
631+
_Py_LeaveRecursiveCall();
625632
return result;
626633
}
627634

0 commit comments

Comments
 (0)