From 4c1298e1d139137a344d98162342d882daf681ed Mon Sep 17 00:00:00 2001 From: jaroslav blaska Date: Fri, 21 Feb 2020 20:39:11 +0100 Subject: [PATCH 1/5] Update AsyncTCP.cpp There is a memory leak in the AsyncClient. The SSL part is not freed by the destructor. My opinion is that it is caused by _lwip_fin where we have _pcb = NULL; but when we want to delete ssl part by int tcp_ssl_free(struct tcp_pcb *tcp) it takes +pcb as an argument but it is NULL... I think this as the simplest solution. --- src/AsyncTCP.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index abd5afd9..db5f39a6 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -630,7 +630,9 @@ AsyncClient::AsyncClient(tcp_pcb* pcb) AsyncClient::~AsyncClient(){ if(_pcb) { _close(); - } + } else { + tcp_ssl_free_by_arg(this); + } _free_closed_slot(); } From 00c460929b5afc67567e64600705f5cf1f0987fa Mon Sep 17 00:00:00 2001 From: jaroslav blaska Date: Fri, 21 Feb 2020 20:42:52 +0100 Subject: [PATCH 2/5] Update tcp_mbedtls.c --- src/tcp_mbedtls.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/tcp_mbedtls.c b/src/tcp_mbedtls.c index c482c610..d3e0e95e 100644 --- a/src/tcp_mbedtls.c +++ b/src/tcp_mbedtls.c @@ -525,6 +525,52 @@ int tcp_ssl_free(struct tcp_pcb *tcp) { return 0; } +int tcp_ssl_free_by_arg(void *arg) { + TCP_SSL_DEBUG("tcp_ssl_free_by_arg(%x)\n", arg); + if(arg == NULL) { + return -1; + } + tcp_ssl_t * item = tcp_ssl_array; + if (item) { + if(item->arg == arg){ + TCP_SSL_DEBUG("tcp_ssl_free_by_arg arg found at first place\n"); + tcp_ssl_array = tcp_ssl_array->next; + if(item->tcp_pbuf != NULL) { + pbuf_free(item->tcp_pbuf); + } + mbedtls_ssl_free(&item->ssl_ctx); + mbedtls_ssl_config_free(&item->ssl_conf); + mbedtls_ctr_drbg_free(&item->drbg_ctx); + mbedtls_entropy_free(&item->entropy_ctx); + free(item); + return 0; + } + + while(item->next && item->next->arg != arg) + item = item->next; + + if(item->next == NULL){ + TCP_SSL_DEBUG("tcp_ssl_free_by_arg arg not found\n"); + return ERR_TCP_SSL_INVALID_CLIENTFD_DATA;//item not found + } + tcp_ssl_t * i = item->next; + item->next = i->next; + if(i->tcp_pbuf != NULL){ + pbuf_free(i->tcp_pbuf); + } + mbedtls_ssl_free(&i->ssl_ctx); + mbedtls_ssl_config_free(&i->ssl_conf); + mbedtls_ctr_drbg_free(&i->drbg_ctx); + mbedtls_entropy_free(&i->entropy_ctx); + free(i); + + return 0; + } else { + TCP_SSL_DEBUG("tcp_ssl_free_by_arg array empty\n"); + return 0; + } +} + bool tcp_ssl_has(struct tcp_pcb *tcp) { return tcp_ssl_get(tcp) != NULL; } From c20b82540d5841154d318db17093c1afdebf024c Mon Sep 17 00:00:00 2001 From: jaroslav blaska Date: Fri, 21 Feb 2020 20:43:55 +0100 Subject: [PATCH 3/5] Update tcp_mbedtls.h --- src/tcp_mbedtls.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tcp_mbedtls.h b/src/tcp_mbedtls.h index 8b932c7c..334bb274 100644 --- a/src/tcp_mbedtls.h +++ b/src/tcp_mbedtls.h @@ -36,6 +36,7 @@ int tcp_ssl_write(struct tcp_pcb *tcp, uint8_t *data, size_t len); int tcp_ssl_read(struct tcp_pcb *tcp, struct pbuf *p); int tcp_ssl_handshake_step(struct tcp_pcb *tcp); int tcp_ssl_free(struct tcp_pcb *tcp); +int tcp_ssl_free_by_arg(void * arg); bool tcp_ssl_has(struct tcp_pcb *tcp); void tcp_ssl_arg(struct tcp_pcb *tcp, void * arg); void tcp_ssl_data(struct tcp_pcb *tcp, tcp_ssl_data_cb_t arg); From 72eb565ddc133f3f6828c99905a5aff80376d4a2 Mon Sep 17 00:00:00 2001 From: jaroslav blaska Date: Fri, 21 Feb 2020 20:47:15 +0100 Subject: [PATCH 4/5] Update AsyncTCP.cpp --- src/AsyncTCP.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index db5f39a6..11825dc1 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -630,7 +630,9 @@ AsyncClient::AsyncClient(tcp_pcb* pcb) AsyncClient::~AsyncClient(){ if(_pcb) { _close(); - } else { + } + else + { tcp_ssl_free_by_arg(this); } _free_closed_slot(); From ed8ba6255a9a6432e9e9394bc08ac84838044873 Mon Sep 17 00:00:00 2001 From: jaroslav blaska Date: Fri, 21 Feb 2020 20:58:26 +0100 Subject: [PATCH 5/5] Update AsyncTCP.cpp --- src/AsyncTCP.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AsyncTCP.cpp b/src/AsyncTCP.cpp index 11825dc1..a01b1ccd 100644 --- a/src/AsyncTCP.cpp +++ b/src/AsyncTCP.cpp @@ -631,10 +631,12 @@ AsyncClient::~AsyncClient(){ if(_pcb) { _close(); } +#if ASYNC_TCP_SSL_ENABLED else { tcp_ssl_free_by_arg(this); - } + } +#endif _free_closed_slot(); }