-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proxy_contains (weakref.proxy) can access an object with 0 refcount #82576
Comments
The implementation of weakref.proxy's methods call back into the Python API using a "borrowed reference" of the weakly referenced object (acquired via PyWeakref_GET_OBJECT). This API call may delete the last reference to the object (either directly or via GC), leaving a dangling pointer, which can be subsequently dereferenced. Tested with Python 3.8.0b4 (debug build) The following code crashes with a debug build of Python 3.8.0b4 on Linux. import weakref
obj = None
class MyObj:
def __iter__(self):
global obj
del obj
return NotImplemented
obj = MyObj()
p = weakref.proxy(obj)
print(5 in p) This particular test case does not crash with a release build (on Linux). The implementation of return PySequence_Contains(PyWeakref_GET_OBJECT(proxy), value); https://github.com/python/cpython/blob/v3.8.0b4/Objects/weakrefobject.c#L556 This eventually calls _PySequence_IterSearch. The call to PyObject_GetIter can call arbitrary code, which can lead to seq (the proxy's referent) being deleted. The subsequent call to type_error dereferences a dead object. it = PyObject_GetIter(seq);
if (it == NULL) {
type_error("argument of type '%.200s' is not iterable", seq);
return -1;
} https://github.com/python/cpython/blob/v3.8.0b4/Objects/abstract.c#L2003-L2007 I believe some functions, like proxy_length, may be immune to this problem because they do not access the borrowed referent after calling into user code. However, this is hard to verify from reading the code and may be fragile -- small changes to PyObject_Length/Size, for example, might . See also https://bugs.python.org/issue16602 |
I'm marking this as release blocker for the 3.8 incoming release so we don't forget about fixing this. |
Note I'm going to ignore this for the purposes of 2.7.17 because it doesn't look like a new regression. |
All fixed now, right? |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: