-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-60198: Prevent memoryview pointing to freed heap memory #105290
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Martin Panter vadmium+py@gmail.com
PyObject *exc = PyErr_GetRaisedException(); | ||
release_res = PyObject_CallMethod(memobj, "release", NULL); | ||
_PyErr_ChainExceptions1(exc); | ||
Py_DECREF(memobj); | ||
if (release_res == NULL) { | ||
Py_XDECREF(res); | ||
return -1; | ||
} | ||
Py_DECREF(release_res); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me. The performance impact should be minimal as it's only a cleanup method call after the buffer's been entirely read.
However, since mv.release()
can potentially raise, this is a change in behavior so we have to decide whether the risk of existing code breakage is worth backporting the patch to older releases.
I see @gpshead decided this isn't a security issue so we won't be bringing it to 3.8, 3.9, 3.10. Question is if we want it in 3.11 and 3.12.
Edit: Ah, my comment below is just an example of point number 1 here: #60198 (comment) I'm not very familiar with the Python C APIs, so I might be mistaken, but I don't think this change is enough to categorically prevent use-after-free here. This PR is releasing the diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 0eac7bc06a..922104d55c 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -660,13 +660,13 @@ def __bool__(self):
def test_gh60198(self):
global view
class File(io.RawIOBase):
def readinto(self, buf):
global view
- view = buf
+ view = memoryview(buf)
def readable(self):
return True
f = io.BufferedReader(File())
# get view of buffer used by BufferedReader
|
Co-authored-by: Martin Panter vadmium+py@gmail.com