-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fix WiFiSecureClient memory leaks when SSL/TLS connection fails #5945
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ static int _handle_error(int err, const char * function, int line) | |
|
||
void ssl_init(sslclient_context *ssl_client) | ||
{ | ||
// reset embedded pointers to zero | ||
memset(ssl_client, 0, sizeof(sslclient_context)); | ||
mbedtls_ssl_init(&ssl_client->ssl_ctx); | ||
mbedtls_ssl_config_init(&ssl_client->ssl_conf); | ||
mbedtls_ctr_drbg_init(&ssl_client->drbg_ctx); | ||
|
@@ -232,6 +234,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p | |
ret = mbedtls_pk_parse_key(&ssl_client->client_key, (const unsigned char *)cli_key, strlen(cli_key) + 1, NULL, 0); | ||
|
||
if (ret != 0) { | ||
mbedtls_x509_crt_free(&ssl_client->client_cert); // cert+key are free'd in pair | ||
return handle_error(ret); | ||
} | ||
|
||
|
@@ -243,7 +246,7 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p | |
// Hostname set here should match CN in server certificate | ||
if((ret = mbedtls_ssl_set_hostname(&ssl_client->ssl_ctx, host)) != 0){ | ||
return handle_error(ret); | ||
} | ||
} | ||
|
||
mbedtls_ssl_conf_rng(&ssl_client->ssl_conf, mbedtls_ctr_drbg_random, &ssl_client->drbg_ctx); | ||
|
||
|
@@ -260,8 +263,8 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p | |
return handle_error(ret); | ||
} | ||
if((millis()-handshake_start_time)>ssl_client->handshake_timeout) | ||
return -1; | ||
vTaskDelay(2);//2 ticks | ||
return -1; | ||
vTaskDelay(2);//2 ticks | ||
} | ||
|
||
|
||
|
@@ -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. | ||
return handle_error(ret); | ||
} else { | ||
log_v("Certificate verified."); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
mbedtls_x509_crt_free(&ssl_client->ca_cert); | ||
} | ||
if (ssl_client->ssl_conf.key_cert != NULL) { | ||
mbedtls_x509_crt_free(&ssl_client->client_cert); | ||
mbedtls_pk_free(&ssl_client->client_key); | ||
} | ||
mbedtls_ssl_free(&ssl_client->ssl_ctx); | ||
mbedtls_ssl_config_free(&ssl_client->ssl_conf); | ||
mbedtls_ctr_drbg_free(&ssl_client->drbg_ctx); | ||
mbedtls_entropy_free(&ssl_client->entropy_ctx); | ||
// reset embedded pointers to zero | ||
memset(ssl_client, 0, sizeof(sslclient_context)); | ||
} | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?