Skip to content

Fix multithreaded read/write with TLS for OpenSSL. #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion libvncclient/tls_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typedef SSIZE_T ssize_t;

static rfbBool rfbTLSInitialized = FALSE;
static MUTEX_TYPE *mutex_buf = NULL;
static MUTEX_TYPE mutex_rw;

struct CRYPTO_dynlock_value {
MUTEX_TYPE mutex;
Expand Down Expand Up @@ -157,6 +158,8 @@ InitializeTLS(void)
for (i = 0; i < CRYPTO_num_locks(); i++)
MUTEX_INIT(mutex_buf[i]);

MUTEX_INIT(mutex_rw);

CRYPTO_set_locking_callback(locking_function);
CRYPTO_set_id_callback(id_function);
CRYPTO_set_dynlock_create_callback(dyn_create_function);
Expand Down Expand Up @@ -630,7 +633,9 @@ ReadFromTLS(rfbClient* client, char *out, unsigned int n)
{
ssize_t ret;

MUTEX_LOCK(mutex_rw);
ret = SSL_read (client->tlsSession, out, n);
MUTEX_UNLOCK(mutex_rw);

if (ret >= 0)
return ret;
Expand All @@ -653,8 +658,9 @@ WriteToTLS(rfbClient* client, const char *buf, unsigned int n)

while (offset < n)
{

MUTEX_LOCK(mutex_rw);
ret = SSL_write (client->tlsSession, buf + offset, (size_t)(n-offset));
MUTEX_UNLOCK(mutex_rw);

if (ret < 0)
errno = ssl_errno (client->tlsSession, ret);
Expand Down Expand Up @@ -689,6 +695,8 @@ void FreeTLS(rfbClient* client)
mutex_buf = NULL;
}

MUTEX_FREE(mutex_rw);

SSL_free(client->tlsSession);
}

Expand Down