Repository navigation
Description
Bug report
Bug Description
In Modules/_functoolsmodule.c, keyobject_richcompare() loads compare = lhs->cmp, x = lhs->object, and y = rhs->object as borrowed references and passes {x, y} directly to PyObject_Vectorcall(compare, args, 2, NULL) without taking strong references (Py_NewRef).
Because PEP 590 vectorcall requires the caller to own/keep args[] alive across the call, passing a METH_FASTCALL comparator such as operator.lt forwards borrowed x and y into PyObject_RichCompare(x, y, Py_LT). If x.__lt__(other) mutates k2.obj = None, deletes its local other reference, and returns NotImplemented, y is immediately deallocated while do_richcompare() (Objects/object.c) still holds w = y and dispatches the reflected y.__gt__(x) check on freed memory. On --disable-gil builds, concurrent k2.obj = ... mutations also race directly on keyobject->object.
Reproducer
import functools
import operator
Key = functools.cmp_to_key(operator.lt)
k2 = None
class Left:
def __lt__(self, other):
k2.obj = None
del other
return NotImplemented
class Right:
def __gt__(self, other):
return True
k1 = Key(Left())
k2 = Key(Right())
k1 < k2./configure --with-address-sanitizer --with-pydebug
make -j"$(nproc)"
PYTHONMALLOC=malloc ASAN_OPTIONS="detect_leaks=0:symbolize=1" ./python reproduce.pyalso affects --disable-gil builds; on --disable-gil --with-address-sanitizer
==587==ERROR: AddressSanitizer: heap-use-after-free on address 0x5130000279e8 at pc 0x563645356880 bp 0x7ffc3c941940 sp 0x7ffc3c941938
READ of size 8 at 0x5130000279e8 thread T0
#0 0x56364535687f in _Py_TYPE_impl /third_party/cpython-stock/Include/object.h:234:16
#1 0x56364535687f in do_richcompare /third_party/cpython-stock/Objects/object.c:1076:37
#2 0x56364535687f in PyObject_RichCompare /third_party/cpython-stock/Objects/object.c:1120:21
#3 0x5636452186b2 in _PyObject_VectorcallTstate /third_party/cpython-stock/Include/internal/pycore_call.h:143:11
#4 0x5636458f36dc in keyobject_richcompare /third_party/cpython-stock/Modules/_functoolsmodule.c:958:21
#5 0x5636453563aa in do_richcompare /third_party/cpython-stock/Objects/object.c:1071:15
...
0x5130000279e8 is located 40 bytes inside of 328-byte region [0x5130000279c0,0x513000027b08)
freed by thread T0 here:
#0 0x563644ff5706 in free (/third_party/cpython/build-host-gil-asan/python+0x3d9706) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
#1 0x56364540511d in subtype_dealloc /third_party/cpython-stock/Objects/typeobject.c:2924:5
#2 0x563645360b63 in py_dealloc /third_party/cpython-stock/Objects/object.c:3348:5
#3 0x563644efba89 in _PyEval_EvalFrameDefault /third_party/cpython-stock/Python/generated_cases.c.h:5900:13
#4 0x563645574ce8 in _PyEval_EvalFrame /third_party/cpython-stock/Include/internal/pycore_ceval.h:122:16
#5 0x563645574ce8 in _PyEval_Vector /third_party/cpython-stock/Python/ceval.c:2228:12
...
previously allocated by thread T0 here:
#0 0x563644ff599f in malloc (/third_party/cpython/build-host-gil-asan/python+0x3d999f) (BuildId: 1b9373ee70b51e37802bb03b5997c608c39484aa)
#1 0x5636453e1523 in _PyObject_MallocWithType /third_party/cpython-stock/Include/internal/pycore_object_alloc.h:46:17
#2 0x5636453e1523 in _PyType_AllocNoTrack /third_party/cpython-stock/Objects/typeobject.c:2572:19
#3 0x5636453e1071 in PyType_GenericAlloc /third_party/cpython-stock/Objects/typeobject.c:2603:21
#4 0x5636453edd66 in type_call /third_party/cpython-stock/Objects/typeobject.c:2498:11
#5 0x563645219149 in _PyObject_MakeTpCall /third_party/cpython-stock/Objects/call.c:242:18
...
SUMMARY: AddressSanitizer: heap-use-after-free /third_party/cpython-stock/Include/object.h:234:16 in _Py_TYPE_impl
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Activity