Skip to content

gh-153539: Fix use-after-free in TextIOWrapper.tell() with a reentrant decoder - #153540

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
tonghuaroot:textio-tell-reentrant-uaf
Aug 17, 2026
Merged

gh-153539: Fix use-after-free in TextIOWrapper.tell() with a reentrant decoder#153540
serhiy-storchaka merged 2 commits into
python:mainfrom
tonghuaroot:textio-tell-reentrant-uaf

Conversation

@tonghuaroot

Copy link
Copy Markdown
Contributor

TextIOWrapper.tell() used a borrowed next_input from the snapshot across the decoder's getstate/decode/setstate calls, so a decoder that re-enters seek() from getstate could free it and leave tell() reading freed memory. Own the reference across those calls, matching textiowrapper_read_chunk.

…entrant decoder

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.
@cmaloney cmaloney added 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes and removed 3.15 pre-release feature fixes, bugs and security fixes 3.16 new features, bugs and security fixes labels Jul 11, 2026
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 17, 2026

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

But the main problem is concurrency -- if two threads call seek() concurrently they can reproduce this bug. Please reflect this in NEWS and comments.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Done in e011fa3: noted the concurrent seek() trigger in the NEWS and the comment.

@serhiy-storchaka
serhiy-storchaka merged commit fa0ec86 into python:main Aug 17, 2026
54 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

brittanyrey pushed a commit to brittanyrey/cpython that referenced this pull request Aug 18, 2026
…entrant decoder (pythonGH-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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants