Skip to content
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

GUACAMOLE-1910: Add locking around TLS socket #482

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/libguac/guacamole/socket-ssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "socket-types.h"

#include <openssl/ssl.h>
#include <pthread.h>

/**
* SSL socket-specific data.
Expand All @@ -54,6 +55,12 @@ typedef struct guac_socket_ssl_data {
*/
SSL* ssl;

/**
* Lock that is acquired when an instruction is being written, and released
* when the instruction is finished being written.
*/
pthread_mutex_t socket_lock;

} guac_socket_ssl_data;

/**
Expand Down
40 changes: 40 additions & 0 deletions src/libguac/socket-ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "guacamole/socket.h"
#include "wait-fd.h"

#include <pthread.h>
#include <stdlib.h>

#include <openssl/ssl.h>
Expand Down Expand Up @@ -97,10 +98,42 @@ static int __guac_socket_ssl_free_handler(guac_socket* socket) {
/* Close file descriptor */
close(data->fd);

pthread_mutex_destroy(&(data->socket_lock));

guac_mem_free(data);
return 0;
}

/**
* Acquires exclusive access to the given socket.
*
* @param socket
* The guac_socket to which exclusive access is requested.
*/
static void __guac_socket_ssl_lock_handler(guac_socket* socket) {

guac_socket_ssl_data* data = (guac_socket_ssl_data*) socket->data;

/* Acquire exclusive access to the socket */
pthread_mutex_lock(&(data->socket_lock));

}

/**
* Releases exclusive access to the given socket.
*
* @param socket
* The guac_socket to which exclusive access is released.
*/
static void __guac_socket_ssl_unlock_handler(guac_socket* socket) {

guac_socket_ssl_data* data = (guac_socket_ssl_data*) socket->data;

/* Relinquish exclusive access to the socket */
pthread_mutex_unlock(&(data->socket_lock));

}

guac_socket* guac_socket_open_secure(SSL_CTX* context, int fd) {

/* Create new SSL structure */
Expand Down Expand Up @@ -129,6 +162,11 @@ guac_socket* guac_socket_open_secure(SSL_CTX* context, int fd) {
return NULL;
}

pthread_mutexattr_t lock_attributes;
pthread_mutexattr_init(&lock_attributes);
pthread_mutexattr_setpshared(&lock_attributes, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&(data->socket_lock), &lock_attributes);

/* Store file descriptor as socket data */
data->fd = fd;
socket->data = data;
Expand All @@ -138,6 +176,8 @@ guac_socket* guac_socket_open_secure(SSL_CTX* context, int fd) {
socket->write_handler = __guac_socket_ssl_write_handler;
socket->select_handler = __guac_socket_ssl_select_handler;
socket->free_handler = __guac_socket_ssl_free_handler;
socket->lock_handler = __guac_socket_ssl_lock_handler;
socket->unlock_handler = __guac_socket_ssl_unlock_handler;

return socket;

Expand Down
Loading