Skip to content

functools.partial: Use-after-free in partial_call() when called callable mutates partial via __setstate__ (GIL & free-threaded) #163184

Backlog

Description

@nevercodecorrect

Bug report

Bug Description

In Modules/_functoolsmodule.c, partial_vectorcall() takes strong references (Py_NewRef) to pto->fn, pto->args, and pto->kw before invoking the wrapped callable, whereas partial_call() (used whenever pto->fn does not implement vectorcall, such as functools.lru_cache wrappers) passes borrowed pto->fn directly to PyObject_Call(pto->fn, tot_args, tot_kw).

Because PyObject_Call() does not increment callable's reference count, if pto->fn holds the sole reference to the lru_cache wrapper and the wrapped Python function replaces pto->fn via p.__setstate__(...), the active lru_cache object is deallocated while bounded_lru_cache_wrapper() is still executing on the C stack, causing an immediate single-threaded heap use-after-free on both standard GIL and free-threaded (--disable-gil) builds.

(Note on related issues: gh-157841 / PR gh-160480 track free-threaded concurrency races on partial.__setstate__; this issue affects single-threaded standard GIL builds as well.)

Reproducer

import functools

p = None

def make_partial():
    @functools.lru_cache(maxsize=4)
    def inner(x):
        p.__setstate__((lambda y: y, (), {}, None))
        return x + 1

    return functools.partial(inner)

p = make_partial()
p(41)
./configure --with-address-sanitizer --with-pydebug
make -j"$(nproc)"
PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python reproduce.py

also affects --disable-gil builds; on --disable-gil --with-address-sanitizer

==571==ERROR: AddressSanitizer: heap-use-after-free on address 0x50f000054ff8 at pc 0x55709b122114 bp 0x7ffff7290290 sp 0x7ffff7290288
READ of size 8 at 0x50f000054ff8 thread T0
    #0 0x55709b122113 in bounded_lru_cache_update_lock_held /third_party/cpython-stock/Modules/_functoolsmodule.c:1383:71
    #1 0x55709b122113 in bounded_lru_cache_wrapper /third_party/cpython-stock/Modules/_functoolsmodule.c:1541:14
    #2 0x55709aa4bf5c in _PyObject_Call /third_party/cpython-stock/Objects/call.c:361:18
    #3 0x55709b11a4f8 in partial_call /third_party/cpython-stock/Modules/_functoolsmodule.c:599:21
    #4 0x55709aa4a149 in _PyObject_MakeTpCall /third_party/cpython-stock/Objects/call.c:242:18
    #5 0x55709ada6d57 in _Py_VectorCallInstrumentation_StackRefSteal /third_party/cpython-stock/Python/ceval.c:755:11
    ...

0x50f000054ff8 is located 88 bytes inside of 168-byte region [0x50f000054fa0,0x50f000055048)
freed by thread T0 here:
    #0 0x55709a826706 in free (/third_party/cpython/build-host-gil-asan/python+0x3d9706) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x55709b11eea2 in lru_cache_dealloc /third_party/cpython-stock/Modules/_functoolsmodule.c:1668:5
    #2 0x55709ab91b63 in py_dealloc /third_party/cpython-stock/Objects/object.c:3348:5
    #3 0x55709b11cd24 in Py_DECREF /third_party/cpython-stock/Include/refcount.h:408:9
    #4 0x55709b11cd24 in partial_setstate /third_party/cpython-stock/Modules/_functoolsmodule.c:784:5
    #5 0x55709aa496b2 in _PyObject_VectorcallTstate /third_party/cpython-stock/Include/internal/pycore_call.h:143:11
    ...

previously allocated by thread T0 here:
    #0 0x55709a82699f in malloc (third_party/cpython/build-host-gil-asan/python+0x3d999f) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
    #1 0x55709ac12523 in _PyObject_MallocWithType /third_party/cpython-stock/Include/internal/pycore_object_alloc.h:46:17
    #2 0x55709ac12523 in _PyType_AllocNoTrack /third_party/cpython-stock/Objects/typeobject.c:2572:19
    #3 0x55709ac12071 in PyType_GenericAlloc /third_party/cpython-stock/Objects/typeobject.c:2603:21
    #4 0x55709b11fe65 in lru_cache_new /third_party/cpython-stock/Modules/_functoolsmodule.c:1600:31
    #5 0x55709ac1ed66 in type_call /third_party/cpython-stock/Objects/typeobject.c:2498:11
    ...

SUMMARY: AddressSanitizer: heap-use-after-free /third_party/cpython-stock/Modules/_functoolsmodule.c:1383:71 in bounded_lru_cache_update_lock_held

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-modulestype-bugtype-crash

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions