Skip to content

Refactor callbacks in OpenSSL::SSL - #1080

Merged
rhenium merged 4 commits into
ruby:masterfrom
rhenium:ky/ssl-callback-errors
Nov 23, 2026
Merged

rhenium merged 4 commits into
ruby:masterfrom
rhenium:ky/ssl-callback-errors

Conversation

@rhenium

@rhenium rhenium commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

Extracted from #736


ssl: use ossl_cb_state_*() for handling exceptions in callbacks

Simplify handling of the temporary rb_protect() jump states. This also
avoids potential corruption of OpenSSL's internal state if the
SSLSocket object is frozen in the middle of SSL_*() calls.

Also, skip calling Ruby callbacks while we are waiting for a chance to
resume a jump with rb_jump_tag(). This cannot currently happen as far
as I know, but it is cheap to check this.


ssl: harden info_callback

Use rb_protect() to avoid exiting from SSL_accept() or SSL_read()
prematurely.

Also set the callback to SSL_CTX instead of SSL, similarly to what we
do for other callbacks configurable on SSLContext.


ssl: harden client_cert_cb

Currently, if the callback raises an exception or returns an invalid
value, the handshake is resumed as if the callback has not been called.
This is contrary to the documentation.

Propagate exceptions raised by the callback to the originating method.

Raise an exception if the callback returns an invalid value. It is
immediately caught by rb_protect() and will similarly be re-raised in
the originating method.


ssl: harden session_{get,new,remove}_cb

Prepare the Array objects for the argument inside rb_protect(). This
object allocation may potentially raise an exception and corrupt
OpenSSL's internal state.

Also look up the callback Proc objects directly from SSLContext's
instance variables to simplify.

@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch 2 times, most recently from a05a89f to 5ae433b Compare August 31, 2026 12:32
@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch from 5ae433b to 11bf938 Compare October 13, 2026 09:58
@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch 2 times, most recently from deb1dfb to 79b6a35 Compare November 12, 2026 13:34
@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch 3 times, most recently from be2c8c7 to 5955591 Compare November 23, 2026 15:02
@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch from 5955591 to 465660b Compare November 23, 2026 15:08
Simplify handling of the temporary rb_protect() jump states. This also
avoids potential corruption of OpenSSL's internal state if the
SSLSocket object is frozen in the middle of SSL_*() calls.

Also, skip calling Ruby callbacks while we are waiting for a chance to
resume a jump with rb_jump_tag(). This cannot currently happen as far
as I know, but it is cheap to check this.
Use rb_protect() to avoid exiting from SSL_accept() or SSL_read()
prematurely.

Also set the callback to SSL_CTX instead of SSL, similarly to what we
do for other callbacks configurable on SSLContext.
Currently, if the callback raises an exception or returns an invalid
value, the handshake is resumed as if the callback has not been called.
This is contrary to the documentation.

Propagate exceptions raised by the callback to the originating method.

Raise an exception if the callback returns an invalid value. It is
immediately caught by rb_protect() and will similarly be re-raised in
the originating method.
Prepare the Array objects for the argument inside rb_protect(). This
object allocation may potentially raise an exception and corrupt
OpenSSL's internal state.

Also look up the callback Proc objects directly from SSLContext's
instance variables to simplify.
@rhenium
rhenium force-pushed the ky/ssl-callback-errors branch from 465660b to f1a1b37 Compare November 23, 2026 15:54
@rhenium
rhenium merged commit 4324773 into ruby:master Nov 23, 2026
48 checks passed
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.

1 participant