FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-157364: Fix use-after-free in TextIOWrapper during reentrant detach by ashm-dev · Pull Request #157370 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
30 changes: 30 additions & 0 deletions Lib/test/test_io/test_textio.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,36 @@ def make_text(buffer):
wrapper.write('x')
self.assertRaisesRegex(ValueError, "detached", wrapper.read)

def test_reentrant_detach_during_read(self):
# gh-157363, gh-157364: The buffer must stay alive until its active
# read operation returns.
wrapper = None

class DetachOnRead(self.RawIOBase):
detached = False

def readable(self):
return True

def readinto(self, b):
if self.detached:
return 0
self.detached = True
wrapper.detach()
b[:3] = b"ab\n"
return 3

for method_name in ("read", "readline"):
with self.subTest(method_name):
raw = DetachOnRead()
wrapper = self.TextIOWrapper(
self.BufferedReader(raw), encoding="utf-8")
method = getattr(wrapper, method_name)
self.assertEqual(method(), "ab\n")
Comment thread
ashm-dev marked this conversation as resolved.
with self.assertRaisesRegex(ValueError,
"underlying buffer has been detached"):
getattr(wrapper, 'buffer')

def test_reentrant_seek_during_tell(self):
# gh-153539: reading short of _CHUNK_SIZE leaves residual bytes in the
# snapshot, so tell() re-decodes and calls the decoder's getstate(); a
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a use-after-free in :class:`io.TextIOWrapper` when a call to the
underlying buffer reentrantly detaches it. Patched by Shamil Abdulaev.
28 changes: 19 additions & 9 deletions Modules/_io/textio.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,8 @@ buffer_access_safe(textio *self)
return NULL;
}

/* Returning a borrowed reference is safe since TextIOWrapper methods are
protected by critical sections. */
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
return self->buffer;
return Py_NewRef(self->buffer);
}

static PyObject *
Expand All @@ -767,7 +765,9 @@ buffer_getattr(textio *self, PyObject *attr_name)
return NULL;
}

return PyObject_GetAttr(buffer, attr_name);
PyObject *res = PyObject_GetAttr(buffer, attr_name);
Py_DECREF(buffer);
return res;
}

static PyObject *
Expand All @@ -778,7 +778,9 @@ buffer_callmethod_noargs(textio *self, PyObject *name)
return NULL;
}

return PyObject_CallMethodNoArgs(buffer, name);
PyObject *res = PyObject_CallMethodNoArgs(buffer, name);
Py_DECREF(buffer);
return res;
}

static PyObject *
Expand All @@ -789,7 +791,9 @@ buffer_callmethod_onearg(textio *self, PyObject *name, PyObject *arg)
return NULL;
}

return PyObject_CallMethodOneArg(buffer, name, arg);
PyObject *res = PyObject_CallMethodOneArg(buffer, name, arg);
Py_DECREF(buffer);
return res;
}

static void
Expand Down Expand Up @@ -1639,7 +1643,7 @@ _io_TextIOWrapper_detach_impl(textio *self)
if (buffer == NULL) {
return NULL;
}
self->buffer = NULL;
Py_CLEAR(self->buffer);
Comment thread
ashm-dev marked this conversation as resolved.
self->detached = 1;
return buffer;
}
Expand Down Expand Up @@ -1863,7 +1867,12 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)

if (needflush) {
PyObject *buffer = buffer_access_safe(self);
if (buffer == NULL || _PyFile_Flush(buffer) < 0) {
if (buffer == NULL) {
return NULL;
}
int res = _PyFile_Flush(buffer);
Py_DECREF(buffer);
if (res < 0) {
return NULL;
}
}
Expand Down Expand Up @@ -2682,6 +2691,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}
res = _PyObject_CallMethod(buf, &_Py_ID(seek), "ii", 0, 2);
Py_DECREF(buf);
Py_CLEAR(cookieObj);
if (res == NULL)
goto fail;
Expand Down Expand Up @@ -3439,7 +3449,7 @@ static PyObject *
_io_TextIOWrapper_buffer_get_impl(textio *self)
/*[clinic end generated code: output=d265a34555aa5d4b input=5951cfa148f7350a]*/
{
return Py_XNewRef(buffer_access_safe(self));
return buffer_access_safe(self);
}

static PyMethodDef incrementalnewlinedecoder_methods[] = {
Expand Down
Loading

Back | FazBrowse Home | New Git URL