Skip to content

ssl: raise IOError in #ssl_read and #ssl_write if the IO is closed - #1165

Closed
sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:ssl-syswrite-closed-io
Closed

sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:ssl-syswrite-closed-io

Conversation

@sourya2704

Copy link
Copy Markdown

Fixes #1156.

Before #1105, SSLSocket#sysread and #syswrite called GetOpenFile() on the underlying IO, which raises IOError if it has been closed. That check went away when the blocking methods moved to Ruby, so after IO#close on the underlying socket, SSL_write() kept using the old file descriptor number. With the reproduction from #1156, the record is written to a new socket that happens to get the same number:

before: syswrite returned 5; the new socket received "\x17\x03\x03\x00\x16..."
after:  syswrite raised IOError: closed stream

This adds the same check at the start of #ssl_read and #ssl_write, so both the blocking and non-blocking methods raise IOError: closed stream, as they did before #1105. sysread already raised in this case through the wait path, but #ssl_read now checks directly as well.

#1106 tracks the closed state of the SSLSocket itself, and mentions that the underlying socket may be closed directly with IO#close. This covers that case, so the two don't overlap.

The new test closes the underlying TCPSocket and checks that syswrite and sysread raise IOError. It fails without the change.

The C implementation of SSLSocket#sysread and #syswrite used to check
the underlying IO with GetOpenFile(), which raises IOError if it has
been closed. That check went away when the blocking methods were moved
to Ruby, so after IO#close, SSL_write() kept using the old file
descriptor number. If a new socket got the same number, the TLS record
was written to it.

Check that the underlying IO is still open before calling SSL_read() or
SSL_write().
@rhenium

rhenium commented Nov 30, 2026

Copy link
Copy Markdown
Member

Thanks for the patch. However, this affects more than these two methods. I've submitted #1171 to hopefully fix all affected code paths.

@rhenium rhenium closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSLSocket#syswrite no longer raises IOError after IO#close

2 participants