Skip to content

Commit

Permalink
ssl: create a temporary frozen string buffer when writing
Browse files Browse the repository at this point in the history
Since a blocking SSLSocket#syswrite call allows context switches while
waiting for the underlying socket to be ready, we must freeze the string
buffer to prevent other threads from modifying it.

Reference: #452
  • Loading branch information
rhenium committed Sep 26, 2021
1 parent 657a473 commit aea874b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions ext/openssl/ossl_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1954,21 +1954,21 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
int nwrite = 0;
rb_io_t *fptr;
int nonblock = opts != Qfalse;
VALUE io;
VALUE tmp, io;

StringValue(str);
tmp = rb_str_new_frozen(StringValue(str));
GetSSL(self, ssl);
io = rb_attr_get(self, id_i_io);
GetOpenFile(io, fptr);
if (ssl_started(ssl)) {
for (;;){
int num = RSTRING_LENINT(str);
for (;;) {
int num = RSTRING_LENINT(tmp);

/* SSL_write(3ssl) manpage states num == 0 is undefined */
if (num == 0)
goto end;

nwrite = SSL_write(ssl, RSTRING_PTR(str), num);
nwrite = SSL_write(ssl, RSTRING_PTR(tmp), num);
switch(ssl_get_error(ssl, nwrite)){
case SSL_ERROR_NONE:
goto end;
Expand Down

0 comments on commit aea874b

Please sign in to comment.