Skip to content

ctypes: use-after-free write in scalar field setters when resize() runs during value conversion #162539

Backlog

Description

@agenticensor

Crash report

Setting a scalar field of a ctypes object computes the destination pointer (b_ptr + offset) before converting the value. Conversion can execute arbitrary Python code (bool, index, float). Calling ctypes.resize() on the same object inside that code reallocs the buffer and frees the old block; the setter then writes through the stale pointer (use-after-free write), and the assignment is silently lost.

PYTHONMALLOC=malloc valgrind --tool=memcheck python3 poc.py

import ctypes

class S(ctypes.Structure):
    _fields_ = [('pad', ctypes.c_char * 4080), ('val', ctypes.c_longlong)]

obj = S()

class Evil:
    def __index__(self):
        ctypes.resize(obj, 1 << 20)
        return 0x4142434445464748

obj.val = Evil()
print("survived", hex(obj.val))
==12195== Invalid write of size 8
==12195==    at 0x13D03C8: i64_set (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x13C5DC5: _PyCData_set (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x13C5C3C: PyCData_set (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x13D18CE: PyCField_set_lock_held (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x183A28D: PyObject_GenericSetAttr (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x18395AF: _TAIL_CALL_STORE_ATTR (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x1888ABE: _PyEval_Vector (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x18887A5: PyEval_EvalCode (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x192B334: run_mod (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x1A7354C: _PyRun_File (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x1A73366: _PyRun_SimpleFile (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==    by 0x1A72EC5: _PyRun_AnyFile (in /home/agenticensor/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15)
==12195==  Address 0x542b6e0 is 4,080 bytes inside a block of size 4,088 free'd
==12195==    at 0x484BDD0: realloc (vg_replace_malloc.c:1801)

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.15.2 (main, Nov 11 2026, 00:19:20) [Clang 23.1.3 ]

Activity

  1. added
    type-crash
    on Nov 24, 2026
  2. added
    extension-modules
    on Nov 24, 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-ctypestype-crash

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions