mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-142664: fix UAF in memoryview.__hash__ via re-entrant data's __hash__ (#143217)
This commit is contained in:
parent
7726119651
commit
00e24b80e0
3 changed files with 27 additions and 3 deletions
|
|
@ -387,6 +387,20 @@ def test_hash_writable(self):
|
||||||
m = self._view(b)
|
m = self._view(b)
|
||||||
self.assertRaises(ValueError, hash, m)
|
self.assertRaises(ValueError, hash, m)
|
||||||
|
|
||||||
|
def test_hash_use_after_free(self):
|
||||||
|
# Prevent crash in memoryview(v).__hash__ with re-entrant v.__hash__.
|
||||||
|
# Regression test for https://github.com/python/cpython/issues/142664.
|
||||||
|
class E(array.array):
|
||||||
|
def __hash__(self):
|
||||||
|
mv.release()
|
||||||
|
self.clear()
|
||||||
|
return 123
|
||||||
|
|
||||||
|
v = E('B', b'A' * 4096)
|
||||||
|
mv = memoryview(v).toreadonly() # must be read-only for hash()
|
||||||
|
self.assertRaises(BufferError, hash, mv)
|
||||||
|
self.assertRaises(BufferError, mv.__hash__)
|
||||||
|
|
||||||
def test_weakref(self):
|
def test_weakref(self):
|
||||||
# Check memoryviews are weakrefable
|
# Check memoryviews are weakrefable
|
||||||
for tp in self._types:
|
for tp in self._types:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix a use-after-free crash in :meth:`memoryview.__hash__ <object.__hash__>`
|
||||||
|
when the ``__hash__`` method of the referenced object mutates that object or
|
||||||
|
the view. Patch by Bénédikt Tran.
|
||||||
|
|
@ -3231,9 +3231,16 @@ memory_hash(PyObject *_self)
|
||||||
"memoryview: hashing is restricted to formats 'B', 'b' or 'c'");
|
"memoryview: hashing is restricted to formats 'B', 'b' or 'c'");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (view->obj != NULL && PyObject_Hash(view->obj) == -1) {
|
if (view->obj != NULL) {
|
||||||
/* Keep the original error message */
|
// Prevent 'self' from being freed when computing the item's hash.
|
||||||
return -1;
|
// See https://github.com/python/cpython/issues/142664.
|
||||||
|
self->exports++;
|
||||||
|
int rc = PyObject_Hash(view->obj);
|
||||||
|
self->exports--;
|
||||||
|
if (rc == -1) {
|
||||||
|
/* Keep the original error message */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MV_C_CONTIGUOUS(self->flags)) {
|
if (!MV_C_CONTIGUOUS(self->flags)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue