Skip to content

gh-143375: Fix a crash in BufferedWriter.seek when passing an object with a specially crafted __index__#143577

Open
tomasr8 wants to merge 14 commits intopython:mainfrom
tomasr8:buffer-seek
Open

gh-143375: Fix a crash in BufferedWriter.seek when passing an object with a specially crafted __index__#143577
tomasr8 wants to merge 14 commits intopython:mainfrom
tomasr8:buffer-seek

Conversation

@tomasr8
Copy link
Copy Markdown
Member

@tomasr8 tomasr8 commented Jan 8, 2026

Adds a check after the call to PyNumber_AsOff_t which could have closed the file as a side-effect.

@@ -0,0 +1,2 @@
Fix a crash in the ``seek`` method of :class:`~io.BufferedWriter` when
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.

Is it only BufferedWriter or other buffered streams?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's also BufferedReader and BufferedRandom since they share the same implementation. I updated the news entry and added tests!

Comment on lines +1396 to +1398
// PyNumber_AsOff_t calls user code via __index__, which
// could have closed the file.
CHECK_CLOSED(self, "seek of closed file")
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.

I do not think this comment is needed. It is trivial if we do all right, and we do not what to add such comments for each second line.

Remove CHECK_CLOSED above, it is redundant. Also, I think it is better to move PyNumber_AsOff_t immediately after the whence check.

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.

Oh, and CHECK_INITIALIZED also should be after PyNumber_AsOff_t. Add a test for concurrent detach().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I do not think this comment is needed.

Removed

Remove CHECK_CLOSED above, it is redundant.

Also removed

Also, I think it is better to move PyNumber_AsOff_t immediately after the whence check.

Done

Add a test for concurrent detach().

Added!

@@ -0,0 +1,2 @@
Fix a crash in the ``seek`` method of :class:`~io.BufferedWriter` when
passing an object with a specially crafted :meth:`~object.__index__`.
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.

No, a specially crafted __index__() is not a cause, it is only a mean of reproducing the issue. The root issue is that the buffered stream can be closed (or detached?) concurrently with seek(). This can happen in other thread (or in the garbage collector), when the GIL is released in not so special custom __index__().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

True, I updated the news entry with the root cause. Let me know if it's ok!

@serhiy-storchaka
Copy link
Copy Markdown
Member

And I think TextIOWrapper has the same issue. See #143007.

@tomasr8 tomasr8 requested a review from serhiy-storchaka April 5, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants