4242#include <poll.h>
4343
4444
45+ /* Compile-time check: TLS recv functions pass PTTLS_PACKET_MAX_SIZE to
46+ * wolfSSL_read, and callers provide WH_COMM_MTU-sized buffers. These must
47+ * be equal to prevent buffer overflow. */
48+ typedef char pttls_size_check [(PTTLS_PACKET_MAX_SIZE == WH_COMM_MTU ) ? 1 : -1 ];
49+
4550#ifndef WOLFHSM_CFG_NO_CRYPTO
4651
4752/* returns 1 (true) if the error passed in is a notice for non blocking
@@ -247,6 +252,7 @@ int posixTransportTls_SendRequest(void* context, uint16_t size,
247252
248253 /* Close the failed socket fd and set state for retry */
249254 if (ctx -> tcpCtx .connect_fd_p1 != 0 ) {
255+ close (ctx -> tcpCtx .connect_fd_p1 - 1 );
250256 ctx -> tcpCtx .connect_fd_p1 = 0 ;
251257 }
252258 ctx -> connect_fd_p1 = 0 ;
@@ -397,6 +403,7 @@ int posixTransportTls_InitListen(void* context, const void* config,
397403 ctx -> ssl_ctx = wolfSSL_CTX_new (wolfSSLv23_server_method ());
398404#endif
399405 if (!ctx -> ssl_ctx ) {
406+ posixTransportTcp_CleanupListen (& ctx -> tcpCtx );
400407 return WH_ERROR_ABORTED ;
401408 }
402409
@@ -408,6 +415,7 @@ int posixTransportTls_InitListen(void* context, const void* config,
408415 if (rc != WH_ERROR_OK ) {
409416 wolfSSL_CTX_free (ctx -> ssl_ctx );
410417 ctx -> ssl_ctx = NULL ;
418+ posixTransportTcp_CleanupListen (& ctx -> tcpCtx );
411419 return rc ;
412420 }
413421
@@ -479,12 +487,16 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
479487
480488 /* Make accepted socket non-blocking */
481489 if (fcntl (ctx -> accept_fd_p1 - 1 , F_SETFL , O_NONBLOCK ) != 0 ) {
490+ close (ctx -> accept_fd_p1 - 1 );
491+ ctx -> accept_fd_p1 = 0 ;
482492 return WH_ERROR_ABORTED ;
483493 }
484494
485495 /* Create SSL object for this connection */
486496 ctx -> ssl = wolfSSL_new (ctx -> ssl_ctx );
487497 if (!ctx -> ssl ) {
498+ close (ctx -> accept_fd_p1 - 1 );
499+ ctx -> accept_fd_p1 = 0 ;
488500 return WH_ERROR_ABORTED ;
489501 }
490502
@@ -493,6 +505,8 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
493505 if (rc != WOLFSSL_SUCCESS ) {
494506 wolfSSL_free (ctx -> ssl );
495507 ctx -> ssl = NULL ;
508+ close (ctx -> accept_fd_p1 - 1 );
509+ ctx -> accept_fd_p1 = 0 ;
496510 return WH_ERROR_ABORTED ;
497511 }
498512
@@ -506,6 +520,8 @@ int posixTransportTls_RecvRequest(void* context, uint16_t* out_size, void* data)
506520 }
507521 wolfSSL_free (ctx -> ssl );
508522 ctx -> ssl = NULL ;
523+ close (ctx -> accept_fd_p1 - 1 );
524+ ctx -> accept_fd_p1 = 0 ;
509525 return WH_ERROR_ABORTED ;
510526 }
511527
0 commit comments