From df1d9d0cd6b491c2011b5354dcded8231f674c98 Mon Sep 17 00:00:00 2001 From: Ewerton Scaboro da Silva Date: Mon, 31 Jul 2023 12:12:21 -0700 Subject: [PATCH 1/3] Fix for Segfault in initiate_socket_connection (socketio_berkeley) when DNS resolution fails (gh#636) --- adapters/socketio_berkeley.c | 44 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/adapters/socketio_berkeley.c b/adapters/socketio_berkeley.c index 26787c023..3b0d2e829 100755 --- a/adapters/socketio_berkeley.c +++ b/adapters/socketio_berkeley.c @@ -536,31 +536,31 @@ static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance) } } - if (socket_io_instance->address_type == ADDRESS_TYPE_IP) - { - socket_io_instance->socket = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - } - else + if(result == 0) { - socket_io_instance->socket = socket(AF_UNIX, SOCK_STREAM, 0); - } + if (socket_io_instance->address_type == ADDRESS_TYPE_IP) + { + socket_io_instance->socket = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + } + else + { + socket_io_instance->socket = socket(AF_UNIX, SOCK_STREAM, 0); + } - if (socket_io_instance->socket < SOCKET_SUCCESS) - { - LogError("Failure: socket create failure %d.", socket_io_instance->socket); - result = MU_FAILURE; - } -#ifndef __APPLE__ - else if (socket_io_instance->target_mac_address != NULL && - set_target_network_interface(socket_io_instance->socket, socket_io_instance->target_mac_address) != 0) - { - LogError("Failure: failed selecting target network interface (MACADDR=%s).", socket_io_instance->target_mac_address); - result = MU_FAILURE; - } -#endif //__APPLE__ + if (socket_io_instance->socket < SOCKET_SUCCESS) + { + LogError("Failure: socket create failure %d.", socket_io_instance->socket); + result = MU_FAILURE; + } + #ifndef __APPLE__ + else if (socket_io_instance->target_mac_address != NULL && + set_target_network_interface(socket_io_instance->socket, socket_io_instance->target_mac_address) != 0) + { + LogError("Failure: failed selecting target network interface (MACADDR=%s).", socket_io_instance->target_mac_address); + result = MU_FAILURE; + } + #endif //__APPLE__ - if(result == 0) - { if ((-1 == (flags = fcntl(socket_io_instance->socket, F_GETFL, 0))) || (fcntl(socket_io_instance->socket, F_SETFL, flags | O_NONBLOCK) == -1)) { From 03f6e6559fa6995e180a4703143ae4c7ec164466 Mon Sep 17 00:00:00 2001 From: Ewerton Scaboro da Silva Date: Mon, 31 Jul 2023 15:54:28 -0700 Subject: [PATCH 2/3] Add missing else statement --- adapters/socketio_berkeley.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adapters/socketio_berkeley.c b/adapters/socketio_berkeley.c index 3b0d2e829..6b271d733 100755 --- a/adapters/socketio_berkeley.c +++ b/adapters/socketio_berkeley.c @@ -560,8 +560,7 @@ static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance) result = MU_FAILURE; } #endif //__APPLE__ - - if ((-1 == (flags = fcntl(socket_io_instance->socket, F_GETFL, 0))) || + else if ((-1 == (flags = fcntl(socket_io_instance->socket, F_GETFL, 0))) || (fcntl(socket_io_instance->socket, F_SETFL, flags | O_NONBLOCK) == -1)) { LogError("Failure: fcntl failure."); From d460bb721d370eb3918d2445f2ce8ed63d1eba4d Mon Sep 17 00:00:00 2001 From: RLeclair Date: Wed, 2 Aug 2023 17:08:33 +0000 Subject: [PATCH 3/3] Adding check to assert hostname is not NULL --- adapters/socketio_berkeley.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/adapters/socketio_berkeley.c b/adapters/socketio_berkeley.c index 6b271d733..00a32b147 100755 --- a/adapters/socketio_berkeley.c +++ b/adapters/socketio_berkeley.c @@ -520,19 +520,27 @@ static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance) } else { - size_t hostname_len = strlen(socket_io_instance->hostname); - if (hostname_len + 1 > sizeof(addrInfoUn.sun_path)) + if (socket_io_instance->hostname != NULL) { - LogError("Hostname %s is too long for a unix socket (max len = %lu)", socket_io_instance->hostname, (unsigned long)sizeof(addrInfoUn.sun_path)); - result = MU_FAILURE; + size_t hostname_len = strlen(socket_io_instance->hostname); + if (hostname_len + 1 > sizeof(addrInfoUn.sun_path)) + { + LogError("Hostname %s is too long for a unix socket (max len = %lu)", socket_io_instance->hostname, (unsigned long)sizeof(addrInfoUn.sun_path)); + result = MU_FAILURE; + } + else + { + memset(&addrInfoUn, 0, sizeof(addrInfoUn)); + addrInfoUn.sun_family = AF_UNIX; + // No need to add NULL terminator due to the above memset + (void)memcpy(addrInfoUn.sun_path, socket_io_instance->hostname, hostname_len); + result = 0; + } } else { - memset(&addrInfoUn, 0, sizeof(addrInfoUn)); - addrInfoUn.sun_family = AF_UNIX; - // No need to add NULL terminator due to the above memset - (void)memcpy(addrInfoUn.sun_path, socket_io_instance->hostname, hostname_len); - result = 0; + LogError("Hostname is NULL"); + result = MU_FAILURE; } } @@ -582,7 +590,7 @@ static int initiate_socket_connection(SOCKET_IO_INSTANCE* socket_io_instance) LogError("Failure: connect failure %d.", errno); result = MU_FAILURE; } - else + else // result == 0 || errno == EINPROGRESS { // Async connect will return -1. result = 0;