Skip to content

pyexpat: Heap use-after-free write in xmlparser.ParseFile() when file.read() re-enters Parse() #163186

Backlog

Description

@nevercodecorrect

Bug report

Bug Description

In Modules/pyexpat.c, pyexpat_xmlparser_ParseFile_impl() calls void *buf = XML_GetBuffer(self->itself, BUF_SIZE) to obtain a raw pointer into Expat's internal m_buffer, and then passes buf to readinst(buf, BUF_SIZE, readmethod), which invokes Python file.read(2048) while self->in_callback == 0.

Because CHECK_NOT_IN_HANDLER only checks self->in_callback (which is set during Expat XML element/data callbacks, not during file.read()), file.read() can call p.Parse(...) on the same parser. Feeding an unclosed >8KB tag causes Expat's setParserBuffer() to PyMem_Free the old m_buffer and allocate a larger buffer. When file.read() returns bytes, readinst() executes memcpy(buf, ptr, len) into the freed m_buffer — a deterministic single-threaded heap use-after-free write on both standard GIL and --disable-gil builds.

Reproducer

import pyexpat

p = pyexpat.ParserCreate()
done = [False]

class ReentrantFile:
    def read(self, n=-1):
        if not done[0]:
            done[0] = True
            # Unclosed >8KB start tag forces Expat setParserBuffer() to PyMem_Free m_buffer
            p.Parse(b"<root attr='" + b"A" * 10000, False)
            return b"B" * 1024
        return b""

try:
    p.ParseFile(ReentrantFile())
except pyexpat.ExpatError:
    pass
./configure --with-address-sanitizer --with-pydebug
make -j"$(nproc)"
PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python reproduce.py
==616==ERROR: AddressSanitizer: heap-use-after-free on address 0x51d000013680 at pc 0x5592ebb6d7be bp 0x7ffe6ebfe890 sp 0x7ffe6ebfe050
WRITE of size 1024 at 0x51d000013680 thread T0
    #0 0x5592ebb6d7bd in __asan_memcpy (/third_party/cpython/build-host-gil-asan/python+0x3d77bd) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x7fbb3f96fceb in readinst /third_party/cpython-stock/Modules/pyexpat.c:1019:5
    #2 0x7fbb3f96fceb in pyexpat_xmlparser_ParseFile_impl /third_party/cpython-stock/Modules/pyexpat.c:1070:22
    #3 0x7fbb3f96fceb in pyexpat_xmlparser_ParseFile /third_party/cpython-stock/Modules/clinic/pyexpat.c.h:133:20
    #4 0x5592ebd926b2 in _PyObject_VectorcallTstate /third_party/cpython-stock/Include/internal/pycore_call.h:143:11
    #5 0x5592ec0efd57 in _Py_VectorCallInstrumentation_StackRefSteal /third_party/cpython-stock/Python/ceval.c:755:11
    ...

0x51d000013680 is located 0 bytes inside of 2048-byte region [0x51d000013680,0x51d000013e80)
freed by thread T0 here:
    #0 0x5592ebb6f706 in free (/third_party/cpython/build-host-gil-asan/python+0x3d9706) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x7fbb3f97ed68 in setParserBuffer /third_party/cpython-stock/Modules/expat/xmlparse.c:2712:5
    #2 0x7fbb3f97ed68 in internalGetBuffer64 /third_party/cpython-stock/Modules/expat/xmlparse.c:2831:7
    #3 0x7fbb3f97dbd0 in XML_Parse64 /third_party/cpython-stock/Modules/expat/xmlparse.c:2530:26
    #4 0x7fbb3f96f786 in pyexpat_xmlparser_Parse_impl /third_party/cpython-stock/Modules/pyexpat.c:977:10
    #5 0x7fbb3f96f786 in pyexpat_xmlparser_Parse /third_party/cpython-stock/Modules/clinic/pyexpat.c.h:99:20
    ...

previously allocated by thread T0 here:
    #0 0x5592ebb6f99f in malloc (/third_party/cpython/build-host-gil-asan/python+0x3d999f) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x7fbb3f97ecb8 in internalGetBuffer64 /third_party/cpython-stock/Modules/expat/xmlparse.c:2826:28
    #2 0x7fbb3f96fac2 in pyexpat_xmlparser_ParseFile_impl /third_party/cpython-stock/Modules/pyexpat.c:1064:21
    #3 0x7fbb3f96fac2 in pyexpat_xmlparser_ParseFile /third_party/cpython-stock/Modules/clinic/pyexpat.c.h:133:20
    #4 0x5592ebd926b2 in _PyObject_VectorcallTstate /third_party/cpython-stock/Include/internal/pycore_call.h:143:11
    #5 0x5592ec0efd57 in _Py_VectorCallInstrumentation_StackRefSteal /third_party/cpython-stock/Python/ceval.c:755:11
    ...

SUMMARY: AddressSanitizer: heap-use-after-free (/third_party/cpython/build-host-gil-asan/python+0x3d77bd) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa) in __asan_memcpy

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    type-bug
    on Dec 5, 2026
  2. added
    type-crash
    extension-modules
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulestopic-XMLtype-bugtype-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions