Azure-Samples / iot-middleware-freertos-samples

This repo has samples for dev kits using the Azure IoT middleware for FreeRTOS
MIT License
76 stars 46 forks source link

Uninitialized memory in TLS_Socket_Connect will cause random free of memory in mbedlts context #385

Open SavaAlexandru opened 11 months ago

SavaAlexandru commented 11 months ago

In the transport_tls_socket_using_mbedtls.c in the function TLS_Socket_Connect we allocate memory for the ssl context.

if( ( pxSSLContext = pvPortMalloc( sizeof( MbedSSLContext_t ) ) ) == NULL )

if the pxSSLContext memory is allocated we start doing the TLS operations.

If any of the TLS operation fails, we do the cleanup which involves sslContextFree( pxSSLContext );

The check for what memory to free in the sslContext attributes is done with comparison with 0. But if the memory is not initialized before, random free can appear as the struct can hold already some data

My suggestion is if the memory has been allocated, do a memset(pxSSLContext , 0, sizeof( MbedSSLContext_t ))