Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.14] gh-142664: fix UAF inmemoryview.__hash__ via re-entrant data's__hash__ (GH-143217)#143221

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
picnixz merged 1 commit intopython:3.14frommiss-islington:backport-00e24b8-3.14
Dec 27, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletionsLib/test/test_memoryview.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -387,6 +387,20 @@ def test_hash_writable(self):
m = self._view(b)
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):
# Check memoryviews are weakrefable
for tp in self._types:
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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.
13 changes: 10 additions & 3 deletionsObjects/memoryobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3222,9 +3222,16 @@
"memoryview: hashing is restricted to formats 'B', 'b' or 'c'");
return -1;
}
if (view->obj != NULL && PyObject_Hash(view->obj) == -1) {
/* Keep the original error message */
return -1;
if (view->obj != NULL) {
// Prevent 'self' from being freed when computing the item's hash.
// See https://github.com/python/cpython/issues/142664.
self->exports++;
int rc = PyObject_Hash(view->obj);

Check warning on line 3229 in Objects/memoryobject.c

View workflow job for this annotation

GitHub Actions/ Windows / Build and test (x64)

'initializing': conversion from 'Py_hash_t' to 'int', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3229 in Objects/memoryobject.c

View workflow job for this annotation

GitHub Actions/ Windows (free-threading) / Build and test (x64)

'initializing': conversion from 'Py_hash_t' to 'int', possible loss of data [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3229 in Objects/memoryobject.c

View workflow job for this annotation

GitHub Actions/ Windows / Build and test (arm64)

'initializing': conversion from 'Py_hash_t' to 'int', possible loss of data [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3229 in Objects/memoryobject.c

View workflow job for this annotation

GitHub Actions/ Windows (free-threading) / Build and test (arm64)

'initializing': conversion from 'Py_hash_t' to 'int', possible loss of data [C:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
self->exports--;
if (rc == -1) {
/* Keep the original error message */
return -1;
}
}

if (!MV_C_CONTIGUOUS(self->flags)) {
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp