From 5b92ad659e5c31401f90d2ef16d3763c1c0bb903 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 09:55:05 -0800 Subject: [PATCH 1/2] Limit Darwin TSan instrumentation around native thread-local errno access --- ixwebsocket/IXSocket.cpp | 8 ++++++++ test/IXSocketTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index d60f1657..8a484c82 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -306,6 +306,11 @@ namespace ix #endif } + // Darwin TSan can confuse reused per-thread errno storage with shared memory. + // These accessors touch only OS thread-local error state; keep callers instrumented. +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif int Socket::getErrno() { int err; @@ -319,6 +324,9 @@ namespace ix return err; } +#if defined(__APPLE__) && defined(__clang__) + __attribute__((no_sanitize("thread"))) +#endif void Socket::setErrno(int err) { #ifdef _WIN32 diff --git a/test/IXSocketTest.cpp b/test/IXSocketTest.cpp index 740090c1..fdc22d98 100644 --- a/test/IXSocketTest.cpp +++ b/test/IXSocketTest.cpp @@ -13,6 +13,7 @@ #include #include #include +#include using namespace ix; @@ -107,6 +108,29 @@ TEST_CASE("Empty and cancelled socket reads do not receive data", "[socket][read CHECK(socket.reads == 0); } +TEST_CASE("Socket error state remains local when threads are reused", "[socket][errno]") +{ + std::atomic isolated {true}; + auto churn = [&](int error) + { + for (int i = 0; i < 16; ++i) + { + std::thread worker( + [&, error] + { + Socket::setErrno(error); + if (Socket::getErrno() != error) isolated = false; + }); + worker.join(); + } + }; + std::thread a(churn, EWOULDBLOCK); + std::thread b(churn, EINVAL); + a.join(); + b.join(); + CHECK(isolated.load()); +} + namespace ix { void testSocket(const std::string& host, From 78c4041ba9d9904f089553d7a58f8f5b80eeae88 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Tue, 17 Nov 2026 10:54:33 -0800 Subject: [PATCH 2/2] Wait for socket readiness during TLS client handshakes --- ixwebsocket/IXSocketAppleSSL.cpp | 22 +++++++-- ixwebsocket/IXSocketAppleSSL.h | 2 + ixwebsocket/IXSocketMbedTLS.cpp | 82 +++++++++++-------------------- ixwebsocket/IXSocketMbedTLS.h | 1 + ixwebsocket/IXSocketOpenSSL.cpp | 6 ++- test/IXSocketAppleSSLReadTest.cpp | 36 +++++++++++++- test/IXSocketMbedTLSWriteTest.cpp | 27 ++++++++++ test/IXSocketOpenSSLWriteTest.cpp | 27 ++++++++++ 8 files changed, 142 insertions(+), 61 deletions(-) diff --git a/ixwebsocket/IXSocketAppleSSL.cpp b/ixwebsocket/IXSocketAppleSSL.cpp index 70893d1e..7d96ca06 100644 --- a/ixwebsocket/IXSocketAppleSSL.cpp +++ b/ixwebsocket/IXSocketAppleSSL.cpp @@ -68,7 +68,9 @@ namespace ix OSStatus SocketAppleSSL::readFromSocket(SSLConnectionRef connection, void* data, size_t* len) { - int fd = (int) (long) connection; + auto* socket = static_cast(const_cast(connection)); + int fd = socket->_sockfd; + socket->_handshakeWantsRead = true; if (fd < 0) return errSSLInternal; assert(data != nullptr); @@ -116,7 +118,9 @@ namespace ix const void* data, size_t* len) { - int fd = (int) (long) connection; + auto* socket = static_cast(const_cast(connection)); + int fd = socket->_sockfd; + socket->_handshakeWantsRead = false; if (fd < 0) return errSSLInternal; assert(data != nullptr); @@ -176,17 +180,25 @@ namespace ix status = SSLHandshake(_sslContext); // Interrupt the handshake - if (isCancellationRequested()) + if (isCancellationRequested && isCancellationRequested()) { errMsg = "Cancellation requested"; return errSSLInternal; } + if (status == errSSLWouldBlock) + { + const auto ready = _handshakeWantsRead ? isReadyToRead(10) : isReadyToWrite(10); + if (ready == PollResultType::Error) + { + errMsg = "Socket error during TLS handshake"; + return errSSLInternal; + } + } } while (status == errSSLWouldBlock || status == errSSLServerAuthCompleted); return status; } - // No wait support bool SocketAppleSSL::connect(const std::string& host, int port, std::string& errMsg, @@ -203,7 +215,7 @@ namespace ix SSLSetIOFuncs( _sslContext, SocketAppleSSL::readFromSocket, SocketAppleSSL::writeToSocket); - SSLSetConnection(_sslContext, (SSLConnectionRef)(long) _sockfd); + SSLSetConnection(_sslContext, this); SSLSetProtocolVersionMin(_sslContext, kTLSProtocol12); if (!_tlsOptions.disable_hostname_validation) diff --git a/ixwebsocket/IXSocketAppleSSL.h b/ixwebsocket/IXSocketAppleSSL.h index 91c5da14..87598029 100644 --- a/ixwebsocket/IXSocketAppleSSL.h +++ b/ixwebsocket/IXSocketAppleSSL.h @@ -44,6 +44,8 @@ namespace ix const CancellationRequest& isCancellationRequested); SSLContextRef _sslContext; + // SecureTransport uses one would-block status for both I/O directions. + bool _handshakeWantsRead = true; mutable std::mutex _mutex; // AppleSSL routines are not thread-safe SocketTLSOptions _tlsOptions; diff --git a/ixwebsocket/IXSocketMbedTLS.cpp b/ixwebsocket/IXSocketMbedTLS.cpp index 8893babf..c7b0377e 100644 --- a/ixwebsocket/IXSocketMbedTLS.cpp +++ b/ixwebsocket/IXSocketMbedTLS.cpp @@ -248,6 +248,36 @@ namespace ix mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL); + return handshake(errMsg, isCancellationRequested); + } + + bool SocketMbedTLS::connect(const std::string& host, + int port, + std::string& errMsg, + const CancellationRequest& isCancellationRequested) + { + { + std::lock_guard lock(_mutex); + _sockfd = connectToHost(host, port, errMsg, isCancellationRequested); + if (_sockfd == -1) return false; + } + + bool isClient = true; + bool initialized = init(host, isClient, errMsg); + if (!initialized) + { + close(); + return false; + } + + mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL); + + return handshake(errMsg, isCancellationRequested); + } + + bool SocketMbedTLS::handshake(std::string& errMsg, + const CancellationRequest& isCancellationRequested) + { int res; do { @@ -299,58 +329,6 @@ namespace ix return true; } - bool SocketMbedTLS::connect(const std::string& host, - int port, - std::string& errMsg, - const CancellationRequest& isCancellationRequested) - { - { - std::lock_guard lock(_mutex); - _sockfd = connectToHost(host, port, errMsg, isCancellationRequested); - if (_sockfd == -1) return false; - } - - bool isClient = true; - bool initialized = init(host, isClient, errMsg); - if (!initialized) - { - close(); - return false; - } - - mbedtls_ssl_set_bio(&_ssl, &_sockfd, mbedtls_net_send, mbedtls_net_recv, NULL); - - int res; - do - { - { - std::lock_guard lock(_mutex); - res = mbedtls_ssl_handshake(&_ssl); - } - - if (isCancellationRequested()) - { - errMsg = "Cancellation requested"; - close(); - return false; - } - } while (res == MBEDTLS_ERR_SSL_WANT_READ || res == MBEDTLS_ERR_SSL_WANT_WRITE); - - if (res != 0) - { - char buf[256]; - mbedtls_strerror(res, buf, sizeof(buf)); - - errMsg = "error in handshake : "; - errMsg += buf; - - close(); - return false; - } - - return true; - } - void SocketMbedTLS::close() { std::lock_guard lock(_mutex); diff --git a/ixwebsocket/IXSocketMbedTLS.h b/ixwebsocket/IXSocketMbedTLS.h index 63ec3cda..0841ca82 100644 --- a/ixwebsocket/IXSocketMbedTLS.h +++ b/ixwebsocket/IXSocketMbedTLS.h @@ -61,6 +61,7 @@ namespace ix bool init(const std::string& host, bool isClient, std::string& errMsg); void initMBedTLS(); + bool handshake(std::string& errMsg, const CancellationRequest& isCancellationRequested); bool loadSystemCertificates(std::string& errMsg); }; diff --git a/ixwebsocket/IXSocketOpenSSL.cpp b/ixwebsocket/IXSocketOpenSSL.cpp index d1307abe..5b275ddb 100644 --- a/ixwebsocket/IXSocketOpenSSL.cpp +++ b/ixwebsocket/IXSocketOpenSSL.cpp @@ -397,7 +397,7 @@ namespace ix return false; } - if (isCancellationRequested()) + if (isCancellationRequested && isCancellationRequested()) { errMsg = "Cancellation requested"; return false; @@ -419,7 +419,9 @@ namespace ix bool rc = false; if (reason == SSL_ERROR_WANT_READ || reason == SSL_ERROR_WANT_WRITE) { - rc = true; + auto ready = reason == SSL_ERROR_WANT_READ ? isReadyToRead(10) : isReadyToWrite(10); + rc = ready != PollResultType::Error; + if (!rc) errMsg = "Socket error during TLS handshake"; } else { diff --git a/test/IXSocketAppleSSLReadTest.cpp b/test/IXSocketAppleSSLReadTest.cpp index b8829886..60d5c8f9 100644 --- a/test/IXSocketAppleSSLReadTest.cpp +++ b/test/IXSocketAppleSSLReadTest.cpp @@ -19,12 +19,31 @@ namespace ix *size = 0; return errSecIO; } - static void install(SocketAppleSSL& socket) + static OSStatus readBlocked(SSLConnectionRef, void*, size_t* size) + { + *size = 0; + return errSSLWouldBlock; + } + static OSStatus writeBlocked(SSLConnectionRef, const void*, size_t* size) + { + *size = 0; + return errSSLWouldBlock; + } + static OSStatus handshake(SocketAppleSSL& socket, + std::string& error, + const CancellationRequest& cancelled) + { + return socket.tlsHandShake(error, cancelled); + } + static void install(SocketAppleSSL& socket, bool blocked = false, bool reading = true) { socket._sslContext = SSLCreateContext(kCFAllocatorDefault, kSSLClientSide, kSSLStreamType); REQUIRE(socket._sslContext != nullptr); - REQUIRE(SSLSetIOFuncs(socket._sslContext, read, write) == noErr); + socket._handshakeWantsRead = reading; + REQUIRE(SSLSetIOFuncs(socket._sslContext, + blocked ? readBlocked : read, + blocked ? writeBlocked : write) == noErr); } }; } // namespace ix @@ -40,3 +59,16 @@ TEST_CASE("SecureTransport terminal errors clear a previous wait status", "[sock CHECK_FALSE(ix::Socket::isWaitNeeded()); CHECK(ix::Socket::getErrno() != 0); } + +TEST_CASE("SecureTransport handshakes stop on readiness errors", "[socket_apple][handshake_wait]") +{ + const bool reading = GENERATE(false, true); + ix::SocketAppleSSL socket(ix::SocketTLSOptions {}); + ix::SocketAppleSSLReadTestAccess::install(socket, true, reading); + unsigned checks = 0; + std::string error; + CHECK(ix::SocketAppleSSLReadTestAccess::handshake( + socket, error, [&] { return ++checks > 3; }) == errSSLInternal); + CHECK(checks == 1); + CHECK(error == "Socket error during TLS handshake"); +} diff --git a/test/IXSocketMbedTLSWriteTest.cpp b/test/IXSocketMbedTLSWriteTest.cpp index 22bb5479..e2b26f47 100644 --- a/test/IXSocketMbedTLSWriteTest.cpp +++ b/test/IXSocketMbedTLSWriteTest.cpp @@ -34,6 +34,16 @@ namespace ix { struct SocketMbedTLSWriteTestAccess { + static bool handshake(SocketMbedTLS& socket, + std::string& error, + const CancellationRequest& cancelled) + { + return socket.handshake(error, cancelled); + } + static int reset(SocketMbedTLS& socket) + { + return mbedtls_ssl_session_reset(&socket._ssl); + } static void install(SocketMbedTLS& socket, mbedtls_ssl_context& session) { socket.initMBedTLS(); @@ -307,6 +317,23 @@ TEST_CASE("MbedTLS readBytes completes from TLS buffers without polling", "[sock CHECK(result.second == "abc"); } +TEST_CASE("MbedTLS client handshakes stop on readiness errors", "[socket_mbedtls][handshake_wait]") +{ + const bool blockedWrite = GENERATE(false, true); + ix::SocketMbedTLS socket(ix::SocketTLSOptions {}); + MemoryConnection connection(socket); + REQUIRE(ix::SocketMbedTLSWriteTestAccess::reset(socket) == 0); + connection.client.incoming.clear(); + connection.server.incoming.clear(); + if (blockedWrite) connection.client.writesBeforeBlocking = 0; + unsigned checks = 0; + std::string error; + CHECK_FALSE( + ix::SocketMbedTLSWriteTestAccess::handshake(socket, error, [&] { return ++checks > 3; })); + CHECK(checks == 1); + CHECK(error == "Socket error during TLS handshake"); +} + #else TEST_CASE("MbedTLS memory write tests require TLS 1.2 PSK and PSA crypto", "[socket_mbedtls]") { diff --git a/test/IXSocketOpenSSLWriteTest.cpp b/test/IXSocketOpenSSLWriteTest.cpp index 3e453b82..f6b9ac62 100644 --- a/test/IXSocketOpenSSLWriteTest.cpp +++ b/test/IXSocketOpenSSLWriteTest.cpp @@ -13,6 +13,16 @@ namespace ix { struct SocketOpenSSLWriteTestAccess { + static bool handshake(SocketOpenSSL& socket, + std::string& error, + const CancellationRequest& cancelled) + { + return socket.openSSLClientHandshake("memory.invalid", error, cancelled); + } + static int reset(SocketOpenSSL& socket) + { + return SSL_clear(socket._ssl_connection); + } static void install(SocketOpenSSL& socket, SSL*& session, SSL_CTX*& context) { std::swap(socket._ssl_connection, session); @@ -283,6 +293,23 @@ TEST_CASE("OpenSSL readBytes completes from TLS buffers without polling", "[sock CHECK(result.second == "abc"); } +TEST_CASE("OpenSSL client handshakes stop on readiness errors", "[socket_openssl][handshake_wait]") +{ + const bool blockedWrite = GENERATE(false, true); + ix::SocketOpenSSL socket(ix::SocketTLSOptions {}); + MemoryConnection connection(socket); + REQUIRE(ix::SocketOpenSSLWriteTestAccess::reset(socket) == 1); + connection.client.incoming.clear(); + connection.server.incoming.clear(); + if (blockedWrite) connection.client.writesBeforeBlocking = 0; + unsigned checks = 0; + std::string error; + CHECK_FALSE( + ix::SocketOpenSSLWriteTestAccess::handshake(socket, error, [&] { return ++checks > 3; })); + CHECK(checks == 1); + CHECK(error == "Socket error during TLS handshake"); +} + #else TEST_CASE("OpenSSL memory write tests require TLS 1.2 PSK and OpenSSL 1.1 APIs", "[socket_openssl]") {