Skip to content

Commit 196edfb

Browse files
miss-islingtonStanFromIrelandpicnixz
authored
[3.13] gh-145986: Avoid unbound C recursion in conv_content_model in pyexpat.c (CVE 2026-4224) (GH-145987) (#145996)
* 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> * Remvoe `skip_if_unlimited_stack_size` decorator * Remove more decorators not on this branch --------- 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 1d6e037 commit 196edfb

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Lib/test/test_pyexpat.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,22 @@ def test_trigger_leak(self):
688688
parser.ElementDeclHandler = lambda _1, _2: None
689689
self.assertRaises(TypeError, parser.Parse, data, True)
690690

691+
def test_deeply_nested_content_model(self):
692+
# This should raise a RecursionError and not crash.
693+
# See https://github.com/python/cpython/issues/145986.
694+
N = 500_000
695+
data = (
696+
b'<!DOCTYPE root [\n<!ELEMENT root '
697+
+ b'(a, ' * N + b'a' + b')' * N
698+
+ b'>\n]>\n<root/>\n'
699+
)
700+
701+
parser = expat.ParserCreate()
702+
parser.ElementDeclHandler = lambda _1, _2: None
703+
with support.infinite_recursion():
704+
with self.assertRaises(RecursionError):
705+
parser.Parse(data)
706+
691707
class MalformedInputTest(unittest.TestCase):
692708
def test1(self):
693709
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)