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

[3.14] gh-157364: Fix use-after-free in io.TextIOWrapper during reentrant detach by vstinner · Pull Request #159600 · 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) .h  (1) .py  (2) .rst  (4) All 4 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
20 changes: 20 additions & 0 deletions Lib/test/test_free_threading/test_io.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 @@ -191,3 +191,23 @@ def reset_worker():
decoder.reset()

run_concurrently([decode_worker] * 2 + [reset_worker] * 2)


class TextIOWrapperTest(TestCase):
def test_buffer_detach_race(self):
make = lambda: io.TextIOWrapper(io.BytesIO())
slot = [make()]

def reader():
for _ in range(1000):
try:
slot[0].buffer
except ValueError:
pass

def detacher():
for _ in range(1000):
slot[0] = make()
slot[0].detach()

run_concurrently([reader, detacher])
80 changes: 80 additions & 0 deletions Lib/test/test_io.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 @@ -4276,6 +4276,86 @@ def closed(self):
wrapper = self.TextIOWrapper(raw)
wrapper.close() # should not crash

def test_reentrant_detach_during_flush(self):
# gh-143008: Reentrant detach() during flush should not crash.

class DetachOnce(self.BufferedRandom):
wrapper = None

def detach_once(self):
original = self.wrapper
self.wrapper = None
if original is not None:
original.detach()
original.flush()

class DetachOnFlush(DetachOnce):
def flush(self):
self.detach_once()

class DetachOnWrite(DetachOnce):
def write(self, b):
self.detach_once()
return len(b)

# Separate reference for after detach_once.
wrapper = None

def make_text(buffer):
nonlocal wrapper
buffer.wrapper = self.TextIOWrapper(buffer, encoding='utf-8')
wrapper = buffer.wrapper

# Many calls could result in the same null self->buffer crash.
tests = [
('truncate', lambda: wrapper.truncate(0)),
('close', lambda: wrapper.close()),
('detach', lambda: wrapper.detach()),
('seek', lambda: wrapper.seek(0)),
('tell', lambda: wrapper.tell()),
('reconfigure', lambda: wrapper.reconfigure(line_buffering=True)),
]
for name, method in tests:
with self.subTest(name):
make_text(DetachOnFlush(self.MockRawIO()))
self.assertRaisesRegex(ValueError, "detached", method)

# Should not crash.
with self.subTest('read via writeflush'):
make_text(DetachOnWrite(self.MockRawIO()))
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")
with self.assertRaisesRegex(ValueError,
"underlying buffer has been detached"):
getattr(wrapper, 'buffer')


class PyTextIOWrapperTest(TextIOWrapperTest):
io = pyio
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 crash in :class:`io.TextIOWrapper` when reentrant
:meth:`io.TextIOBase.detach` is called reentrantly from the underlying buffer.
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 @@
Fix race conditions when re-initializing a :class:`io.TextIOWrapper` object.
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 @@
Fixed data-race when calling :meth:`io.TextIOBase.detach` in
:term:`free-threaded build`.
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.
27 changes: 26 additions & 1 deletion Modules/_io/clinic/textio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading

Back | FazBrowse Home | New Git URL