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

Fix WiFiSecureClient memory leaks when SSL/TLS connection fails #5945

Merged
merged 1 commit into from
Dec 14, 2021

Conversation

ppescher
Copy link
Contributor

This should fix issue #5781 and possibly others related to the same problem: the function start_ssl_client() does not free memory allocated by mbed-tls library for data structures holding CA certificate, client certificate and private key, in case of early failures (handshake, verification, etc.), but only when the connection is successful.

The proposed fix adds some cleanup code to stop_ssl_socket() so that WiFiSecureClient does not leak memory when it finally calls stop().

@me-no-dev me-no-dev merged commit f29f448 into espressif:master Dec 14, 2021
@me-no-dev
Copy link
Member

Thanks for finding this!

@@ -280,7 +283,6 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
memset(buf, 0, sizeof(buf));
mbedtls_x509_crt_verify_info(buf, sizeof(buf), " ! ", flags);
log_e("Failed to verify peer certificate! verification info: %s", buf);
stop_ssl_socket(ssl_client, rootCABuff, cli_cert, cli_key); //It's not safe continue.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this call to stop_ssl_socket() not needed anymore? Was it unnecessary to begin with?

@@ -313,10 +315,20 @@ void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, cons
ssl_client->socket = -1;
}

// avoid memory leak if ssl connection attempt failed
if (ssl_client->ssl_conf.ca_chain != NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing for NULL is superficial and considered bad taste by some when the pointer is only handed over to free() anyway. The pointers below are not tested and thus this introduces a confusing inconstancy. IMHO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants