Skip to content

2.7.18.16: pyexpat stack overflow on deeply nested content model (CVE-2026-4224) - #99

Merged
icanhasmath merged 2 commits into
2.7.18.16from
2.7.18.16-pyexpat-content-model
Oct 28, 2026
Merged

icanhasmath merged 2 commits into
2.7.18.16from
2.7.18.16-pyexpat-content-model

Conversation

@icanhasmath

Copy link
Copy Markdown

Backport of python/cpython pythongh-145986 (CVE-2026-4224, PSF-2026-12) to 2.7.18.x.

Problem: with an ElementDeclHandler set, pyexpat converts each element declaration's content model to tuples using the recursive conv_content_model(). An inline DTD with very deep nesting overflows the C stack. On 2.7.18.15 the reproducer (500,000 levels) segfaults.

Fix: wrap the recursion in Py_EnterRecursiveCall/Py_LeaveRecursiveCall. Deep nesting now raises RuntimeError("maximum recursion depth exceeded in conv_content_model").

Behavior change: content models nested deeper than the recursion limit (1000 by default) now raise instead of being converted. Upstream made the same change.

Tests: ElementDeclHandlerTest.test_deeply_nested_content_model covers the crash, and test_nested_content_model checks a normal nested model still converts. With the fix, test_pyexpat passes 39/39, and test_xml_etree, test_xml_etree_c, test_minidom, test_sax, test_xmlrpc and test_plistlib pass.

The crash is in pyexpat, not libexpat. It reproduces, and is fixed, with the bundled expat 2.6.4. The expat version bump is a separate item.

Part of 2.7.18.16. It targets the 2.7.18.16 integration branch.

…model (CVE-2026-4224)

Backport of python/cpython pythongh-145986 (eb0e8be) to 2.7.18.x.

When a parser has an ElementDeclHandler, pyexpat converts each element
declaration's content model to nested tuples with the recursive
conv_content_model().  An inline DTD with a deeply nested content model
(e.g. 500000 levels of "(a, ") overflowed the C stack: on 2.7.18.15 the
reproducer segfaults.  conv_content_model() now goes through
Py_EnterRecursiveCall()/Py_LeaveRecursiveCall(), so deep nesting raises
RuntimeError("maximum recursion depth exceeded in conv_content_model")
instead.  As upstream, models nested deeper than the recursion limit
(1000 by default) now raise rather than being converted.

2.7.18.x: the public Py_EnterRecursiveCall() replaces the internal
_Py_EnterRecursiveCall(); C89 declarations; 2.7 raises RuntimeError, not
RecursionError.  The tests go into a new ElementDeclHandlerTest class
registered in test_main(), with a second test that a normal nested model
still converts.  The crash is in pyexpat itself; it reproduces and is
fixed with the bundled expat 2.6.4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ezequielp-activestate ezequielp-activestate left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers. The change is upstream's pythongh-145986 fix (eb0e8be), using 2.7's Py_EnterRecursiveCall.

  • Repro: an inline DTD with a 500,000-level content model and an ElementDeclHandler segfaults the base interpreter. On this branch Parse() raises RuntimeError: maximum recursion depth exceeded in conv_content_model. The handler's existing flag_error() path stops the parse.
  • Tests: test_pyexpat, test_xml_etree, test_xml_etree_c, test_minidom and test_sax pass. The new test_deeply_nested_content_model segfaults on the base.
  • Upstream since: the later Modules/pyexpat.c changes (pythongh-146169 re-entrant parsing, pythongh-159739 handler changes during callbacks, the Expat bumps) are separate issues, not follow-ups to this fix.
Resolve the test_pyexpat.py conflict with #105: both add a test class
after ForeignDTDTests and register it in test_main(); keep both
ElementDeclHandlerTest and HashSaltTest.  Modules/pyexpat.c merged
cleanly.
@icanhasmath

Copy link
Copy Markdown
Author

Merged 2.7.18.16 into this branch (7d13ea2) to resolve a conflict with #105, after #104 and #105 merged.

  • Conflict: Lib/test/test_pyexpat.py only. Both PRs add a test class after ForeignDTDTests and register it in test_main(), so the resolution keeps both ElementDeclHandlerTest and HashSaltTest.
  • pyexpat.c: Modules/pyexpat.c merged cleanly with [2.7.18.16] Give Expat parsers a 16-byte random hash salt (CVE-2026-7210) #105's hash-salt change.
  • Tests: on the merged tree (expat 2.9.0, OpenSSL 1.1.1w), test_pyexpat passes (42 tests, including both new classes and test_deeply_nested_entities), as do test_xml_etree, test_xml_etree_c, test_minidom, test_sax and test_xmlrpc. This PR's own change is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants