File CVE-2026-4224-expat-unbound-C-recursion.patch of Package python315
From d3dd83794717ad183a9de1cd9bff7940e6b024f4 Mon Sep 17 00:00:00 2001
From: Stan Ulbrych <stan@ulbrych.org>
Date: Sat, 14 Mar 2026 17:35:27 +0000
Subject: [PATCH 1/7] Fix crash in `conv_content_model` function in `pyexpat`
---
Lib/test/test_pyexpat.py | 19 ++++++++++
Misc/NEWS.d/next/Security/2026-03-14-17-31-39.gh-issue-145986.ifSSr8.rst | 4 ++
Modules/pyexpat.c | 9 ++++
3 files changed, 31 insertions(+), 1 deletion(-)
create mode 100644 Misc/NEWS.d/next/Library/2026-03-14-17-31-39.gh-issue-111111.ifSSr8.rst
Index: Python-3.15.0a7/Lib/test/test_pyexpat.py
===================================================================
--- Python-3.15.0a7.orig/Lib/test/test_pyexpat.py 2026-03-24 11:42:35.793988139 +0100
+++ Python-3.15.0a7/Lib/test/test_pyexpat.py 2026-03-24 11:42:35.928988239 +0100
@@ -701,6 +701,25 @@
parser.ElementDeclHandler = lambda _1, _2: None
self.assertRaises(TypeError, parser.Parse, data, True)
+ @support.skip_if_unlimited_stack_size
+ @support.skip_emscripten_stack_overflow()
+ @support.skip_wasi_stack_overflow()
+ 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 support.infinite_recursion():
+ with self.assertRaises(RecursionError):
+ parser.Parse(data)
+
class MalformedInputTest(unittest.TestCase):
def test1(self):
xml = b"\0\r\n"
Index: Python-3.15.0a7/Misc/NEWS.d/next/Security/2026-03-14-17-31-39.gh-issue-145986.ifSSr8.rst
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ Python-3.15.0a7/Misc/NEWS.d/next/Security/2026-03-14-17-31-39.gh-issue-145986.ifSSr8.rst 2026-03-24 11:42:35.930068121 +0100
@@ -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`.
Index: Python-3.15.0a7/Modules/pyexpat.c
===================================================================
--- Python-3.15.0a7.orig/Modules/pyexpat.c 2026-03-10 13:31:15.000000000 +0100
+++ Python-3.15.0a7/Modules/pyexpat.c 2026-03-24 11:42:35.930497962 +0100
@@ -3,6 +3,7 @@
#endif
#include "Python.h"
+#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_import.h" // _PyImport_SetModule()
#include "pycore_pyhash.h" // _Py_HashSecret
#include "pycore_traceback.h" // _PyTraceback_Add()
@@ -607,6 +608,10 @@
conv_content_model(XML_Content * const model,
PyObject *(*conv_string)(void *))
{
+ if (_Py_EnterRecursiveCall(" in conv_content_model")) {
+ return NULL;
+ }
+
PyObject *result = NULL;
PyObject *children = PyTuple_New(model->numchildren);
int i;
@@ -618,7 +623,7 @@
conv_string);
if (child == NULL) {
Py_XDECREF(children);
- return NULL;
+ goto done;
}
PyTuple_SET_ITEM(children, i, child);
}
@@ -626,6 +631,8 @@
model->type, model->quant,
conv_string, model->name, children);
}
+done:
+ _Py_LeaveRecursiveCall();
return result;
}