Skip to content

Commit 5b7ec4e

Browse files
tonghuarootserhiy-storchaka
authored andcommitted
gh-153539: Fix use-after-free in TextIOWrapper.tell() with a reentrant decoder (GH-153540)
TextIOWrapper.tell() used a borrowed next_input from the snapshot across the decoder's getstate/decode/setstate calls, so a decoder that reenters seek() from getstate could free it and leave tell() reading freed memory. Own the reference across those calls, matching the sibling textiowrapper_read_chunk. (cherry picked from commit fa0ec86) Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
1 parent f71fbc5 commit 5b7ec4e

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

Lib/test/test_io/test_textio.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,49 @@ def _to_memoryview(buf):
14481448
class CTextIOWrapperTest(TextIOWrapperTest, CTestCase):
14491449
shutdown_error = "LookupError: unknown encoding: ascii"
14501450

1451+
def test_reentrant_seek_during_tell(self):
1452+
# gh-153539: reading short of _CHUNK_SIZE leaves residual bytes in the
1453+
# snapshot, so tell() re-decodes and calls the decoder's getstate(); a
1454+
# reentrant seek() there must not free the snapshot tell() still uses.
1455+
# C-only: _pyio binds next_input as a strong local and cannot crash.
1456+
wrapper = None
1457+
armed = False
1458+
1459+
class ReentrantDecoder(codecs.IncrementalDecoder):
1460+
def decode(self, input, final=False):
1461+
return bytes(input).decode("latin-1")
1462+
def getstate(self):
1463+
nonlocal armed
1464+
if wrapper is not None and armed:
1465+
armed = False
1466+
wrapper.seek(0)
1467+
return (b"", 0)
1468+
def setstate(self, state):
1469+
pass
1470+
1471+
def search(name):
1472+
if name != "reentrant_tell_test":
1473+
return None
1474+
return codecs.CodecInfo(
1475+
name=name,
1476+
encode=lambda s, e='strict': (s.encode("latin-1"), len(s)),
1477+
decode=lambda b, e='strict': (bytes(b).decode("latin-1"), len(b)),
1478+
incrementaldecoder=ReentrantDecoder)
1479+
1480+
codecs.register(search)
1481+
self.addCleanup(codecs.unregister, search)
1482+
raw = self.BytesIO(b"abcdefghijklmnop" * 8)
1483+
wrapper = self.TextIOWrapper(self.BufferedReader(raw),
1484+
encoding="reentrant_tell_test", newline="")
1485+
wrapper._CHUNK_SIZE = 8
1486+
wrapper.read(5)
1487+
armed = True
1488+
self.assertIsInstance(wrapper.tell(), int)
1489+
# tell() at the snapshot boundary takes the early return that owns and
1490+
# must release next_input; exercise it too (leak-checked under -R).
1491+
wrapper.seek(0)
1492+
self.assertIsInstance(wrapper.tell(), int)
1493+
14511494
def test_initialization(self):
14521495
r = self.BytesIO(b"\xc3\xa9\n\n")
14531496
b = self.BufferedReader(r, 1000)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash in the C implementation of :meth:`io.TextIOWrapper.tell` when the
2+
decoder's ``getstate`` method triggers a reentrant seek, or when another thread
3+
seeks the same stream concurrently. Patch by tonghuaroot.

Modules/_io/textio.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2745,7 +2745,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
27452745
PyObject *res;
27462746
PyObject *posobj = NULL;
27472747
cookie_type cookie = {0,0,0,0,0};
2748-
PyObject *next_input;
2748+
PyObject *next_input = NULL;
27492749
Py_ssize_t chars_to_skip, chars_decoded;
27502750
Py_ssize_t skip_bytes, skip_back;
27512751
PyObject *saved_state = NULL;
@@ -2797,11 +2797,15 @@ _io_TextIOWrapper_tell_impl(textio *self)
27972797

27982798
assert (PyBytes_Check(next_input));
27992799

2800+
/* Own next_input: a reentrant or concurrent seek can drop the snapshot. */
2801+
Py_INCREF(next_input);
2802+
28002803
cookie.start_pos -= PyBytes_GET_SIZE(next_input);
28012804

28022805
/* How many decoded characters have been used up since the snapshot? */
28032806
if (self->decoded_chars_used == 0) {
28042807
/* We haven't moved from the snapshot point. */
2808+
Py_DECREF(next_input);
28052809
return textiowrapper_build_cookie(&cookie);
28062810
}
28072811

@@ -2942,6 +2946,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
29422946
}
29432947

29442948
finally:
2949+
Py_XDECREF(next_input);
29452950
res = PyObject_CallMethodOneArg(
29462951
self->decoder, &_Py_ID(setstate), saved_state);
29472952
Py_DECREF(saved_state);
@@ -2954,6 +2959,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
29542959
return textiowrapper_build_cookie(&cookie);
29552960

29562961
fail:
2962+
Py_XDECREF(next_input);
29572963
if (saved_state) {
29582964
PyObject *exc = PyErr_GetRaisedException();
29592965
res = PyObject_CallMethodOneArg(

0 commit comments

Comments
 (0)