Skip to content

Commit 081e0be

Browse files
[3.13] gh-153539: Fix use-after-free in TextIOWrapper.tell() with a reentrant decoder (GH-153540) (GH-156195)
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 acdb9a7 commit 081e0be

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

Lib/test/test_io.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,6 +4065,49 @@ class CTextIOWrapperTest(TextIOWrapperTest):
40654065
io = io
40664066
shutdown_error = "LookupError: unknown encoding: ascii"
40674067

4068+
def test_reentrant_seek_during_tell(self):
4069+
# gh-153539: reading short of _CHUNK_SIZE leaves residual bytes in the
4070+
# snapshot, so tell() re-decodes and calls the decoder's getstate(); a
4071+
# reentrant seek() there must not free the snapshot tell() still uses.
4072+
# C-only: _pyio binds next_input as a strong local and cannot crash.
4073+
wrapper = None
4074+
armed = False
4075+
4076+
class ReentrantDecoder(codecs.IncrementalDecoder):
4077+
def decode(self, input, final=False):
4078+
return bytes(input).decode("latin-1")
4079+
def getstate(self):
4080+
nonlocal armed
4081+
if wrapper is not None and armed:
4082+
armed = False
4083+
wrapper.seek(0)
4084+
return (b"", 0)
4085+
def setstate(self, state):
4086+
pass
4087+
4088+
def search(name):
4089+
if name != "reentrant_tell_test":
4090+
return None
4091+
return codecs.CodecInfo(
4092+
name=name,
4093+
encode=lambda s, e='strict': (s.encode("latin-1"), len(s)),
4094+
decode=lambda b, e='strict': (bytes(b).decode("latin-1"), len(b)),
4095+
incrementaldecoder=ReentrantDecoder)
4096+
4097+
codecs.register(search)
4098+
self.addCleanup(codecs.unregister, search)
4099+
raw = self.BytesIO(b"abcdefghijklmnop" * 8)
4100+
wrapper = self.TextIOWrapper(self.BufferedReader(raw),
4101+
encoding="reentrant_tell_test", newline="")
4102+
wrapper._CHUNK_SIZE = 8
4103+
wrapper.read(5)
4104+
armed = True
4105+
self.assertIsInstance(wrapper.tell(), int)
4106+
# tell() at the snapshot boundary takes the early return that owns and
4107+
# must release next_input; exercise it too (leak-checked under -R).
4108+
wrapper.seek(0)
4109+
self.assertIsInstance(wrapper.tell(), int)
4110+
40684111
def test_initialization(self):
40694112
r = self.BytesIO(b"\xc3\xa9\n\n")
40704113
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
@@ -2726,7 +2726,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
27262726
PyObject *res;
27272727
PyObject *posobj = NULL;
27282728
cookie_type cookie = {0,0,0,0,0};
2729-
PyObject *next_input;
2729+
PyObject *next_input = NULL;
27302730
Py_ssize_t chars_to_skip, chars_decoded;
27312731
Py_ssize_t skip_bytes, skip_back;
27322732
PyObject *saved_state = NULL;
@@ -2778,11 +2778,15 @@ _io_TextIOWrapper_tell_impl(textio *self)
27782778

27792779
assert (PyBytes_Check(next_input));
27802780

2781+
/* Own next_input: a reentrant or concurrent seek can drop the snapshot. */
2782+
Py_INCREF(next_input);
2783+
27812784
cookie.start_pos -= PyBytes_GET_SIZE(next_input);
27822785

27832786
/* How many decoded characters have been used up since the snapshot? */
27842787
if (self->decoded_chars_used == 0) {
27852788
/* We haven't moved from the snapshot point. */
2789+
Py_DECREF(next_input);
27862790
return textiowrapper_build_cookie(&cookie);
27872791
}
27882792

@@ -2923,6 +2927,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
29232927
}
29242928

29252929
finally:
2930+
Py_XDECREF(next_input);
29262931
res = PyObject_CallMethodOneArg(
29272932
self->decoder, &_Py_ID(setstate), saved_state);
29282933
Py_DECREF(saved_state);
@@ -2935,6 +2940,7 @@ _io_TextIOWrapper_tell_impl(textio *self)
29352940
return textiowrapper_build_cookie(&cookie);
29362941

29372942
fail:
2943+
Py_XDECREF(next_input);
29382944
if (saved_state) {
29392945
PyObject *exc = PyErr_GetRaisedException();
29402946
res = PyObject_CallMethodOneArg(

0 commit comments

Comments
 (0)