Skip to content

ctypes: ctypes.cast() on Structure/Union array or sub-object fields drops _objects owner reference, causing heap use-after-free #163187

Backlog

Description

@nevercodecorrect

Bug report

Bug Description

When ctypes.cast(src, POINTER(...)) is called on a sub-object src accessed from a Structure or Union (for example sub = s.arr where Outer._fields_ = [("arr", ctypes.c_int * 64)]), sub has sub->b_base = s and sub->b_objects = NULL.

In Modules/_ctypes/_ctypes.c:cast_lock_held(), CPython calls container = PyCData_GetContainer(obj) to walk b_base to s and initialize container->b_objects (s->b_objects = Py_None), as documented in the comment right above (/* PyCData_GetContainer will initialize src.b_objects, we need this so it can be shared */). However, the next lines mistakenly inspect obj->b_objects (NULL) instead of container->b_objects, setting result->b_objects = NULL and skipping insertion of src into result->b_objects.

Once sub and s go out of scope (PyMem_Free(s->b_ptr)), the returned pointer ptr dangles into freed heap memory (ptr._objects is None), resulting in deterministic heap use-after-free reads and writes on both standard GIL and --disable-gil builds.

Reproducer

import ctypes
import gc

class Outer(ctypes.Structure):
    _fields_ = [("arr", ctypes.c_int * 64)]

def make_dangling_ptr():
    s = Outer()
    s.arr[0] = 123456
    sub = s.arr
    ptr = ctypes.cast(sub, ctypes.POINTER(ctypes.c_int))
    assert ptr._objects is None  # Bug: neither sub nor s was kept alive!
    del sub, s
    gc.collect()
    return ptr

ptr = make_dangling_ptr()
print(ptr[0])  # Heap UAF read (prints freed heap garbage or crashes under MTE/ASan)
./configure --with-address-sanitizer --with-pydebug
make -j"$(nproc)"
PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python reproduce.py
==629==ERROR: AddressSanitizer: heap-use-after-free on address 0x511000071ec0 at pc 0x7fea35b243bf bp 0x7ffe1ea0e2a0 sp 0x7ffe1ea0e298
READ of size 4 at 0x511000071ec0 thread T0
    #0 0x7fea35b243be in i32_get /third_party/cpython-stock/Modules/_ctypes/cfield.c:640:1
    #1 0x7fea35b06d6c in Pointer_item_impl /third_party/cpython-stock/Modules/_ctypes/_ctypes.c:5743:12
    #2 0x55a09ca209a3 in PyObject_GetItem /third_party/cpython-stock/Objects/abstract.c:164:26
    #3 0x55a09c78e677 in _PyEval_EvalFrameDefault /third_party/cpython-stock/Python/generated_cases.c.h:67:35
    #4 0x55a09cdd7ce8 in _PyEval_EvalFrame /third_party/cpython-stock/Include/internal/pycore_ceval.h:122:16
    #5 0x55a09cdd7ce8 in _PyEval_Vector /third_party/cpython-stock/Python/ceval.c:2228:12
    ...

0x511000071ec0 is located 0 bytes inside of 256-byte region [0x511000071ec0,0x511000071fc0)
freed by thread T0 here:
    #0 0x55a09c858706 in free (/third_party/cpython/build-host-gil-asan/python+0x3d9706) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x7fea35af3b5d in PyCData_clear /third_party/cpython-stock/Modules/_ctypes/_ctypes.c:3125:9
    #2 0x7fea35af3895 in PyCData_dealloc /third_party/cpython-stock/Modules/_ctypes/_ctypes.c:3136:11
    #3 0x55a09cc6811d in subtype_dealloc /third_party/cpython-stock/Objects/typeobject.c:2924:5
    #4 0x55a09cbc3b63 in py_dealloc /third_party/cpython-stock/Objects/object.c:3348:5
    #5 0x55a09c75ea89 in _PyEval_EvalFrameDefault /third_party/cpython-stock/Python/generated_cases.c.h:5900:13
    ...

previously allocated by thread T0 here:
    #0 0x55a09c85899f in malloc (/third_party/cpython/build-host-gil-asan/python+0x3d999f) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x7fea35af7254 in PyCData_MallocBuffer /third_party/cpython-stock/Modules/_ctypes/_ctypes.c:3373:30
    #2 0x7fea35af7254 in generic_pycdata_new /third_party/cpython-stock/Modules/_ctypes/_ctypes.c:3691:15
    #3 0x55a09cc50d66 in type_call /third_party/cpython-stock/Objects/typeobject.c:2498:11
    #4 0x55a09ca7c149 in _PyObject_MakeTpCall /third_party/cpython-stock/Objects/call.c:242:18
    #5 0x55a09cdd8d57 in _Py_VectorCallInstrumentation_StackRefSteal /third_party/cpython-stock/Python/ceval.c:755:11
    ...

SUMMARY: AddressSanitizer: heap-use-after-free /third_party/cpython-stock/Modules/_ctypes/cfield.c:640:1 in i32_get

Pls let me know if there are any other questions

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    type-bug
    on Dec 5, 2026
  2. added
    extension-modules
    type-crash
    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-ctypestype-bugtype-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions