From 422bee76626925dad86c5aa3ccfef9a4207c414a Mon Sep 17 00:00:00 2001 From: Gowrima Date: Tue, 29 Oct 2024 14:51:19 -0700 Subject: [PATCH 1/6] Fixing memory leak by freeing memory allocated by strdup in cert_t --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 84a2245b2d6..919e91006b9 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1324,6 +1324,8 @@ static zhandle_t *zookeeper_init_internal(const char *host, watcher_fn watcher, if (cert) { zh->fd->cert = calloc(1, sizeof(zcert_t)); memcpy(zh->fd->cert, cert, sizeof(zcert_t)); + free(cert.certstr); + free(cert.ca); } #ifdef _WIN32 From 519512c8815885b73056f2ede33415a5608748bd Mon Sep 17 00:00:00 2001 From: Gowrima Date: Tue, 29 Oct 2024 16:43:26 -0700 Subject: [PATCH 2/6] Fixing memory leak by freeing memory allocated by strdup in cert_t --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 919e91006b9..802aa07a0b4 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1324,8 +1324,8 @@ static zhandle_t *zookeeper_init_internal(const char *host, watcher_fn watcher, if (cert) { zh->fd->cert = calloc(1, sizeof(zcert_t)); memcpy(zh->fd->cert, cert, sizeof(zcert_t)); - free(cert.certstr); - free(cert.ca); + free(cert->certstr); + free(cert->ca); } #ifdef _WIN32 From b388e989fcf09a960cd88c1ccaf279116496661f Mon Sep 17 00:00:00 2001 From: Gowrima Date: Tue, 29 Oct 2024 21:51:43 -0700 Subject: [PATCH 3/6] Fixing memory leak by freeing memory allocated by strdup in cert_t --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 802aa07a0b4..a8434ab9095 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1324,8 +1324,6 @@ static zhandle_t *zookeeper_init_internal(const char *host, watcher_fn watcher, if (cert) { zh->fd->cert = calloc(1, sizeof(zcert_t)); memcpy(zh->fd->cert, cert, sizeof(zcert_t)); - free(cert->certstr); - free(cert->ca); } #ifdef _WIN32 @@ -3875,6 +3873,7 @@ int zookeeper_close(zhandle_t *zh) LOG_INFO(LOGCALLBACK(zh), "Freeing zookeeper resources for sessionId=%#llx\n", zh->client_id.client_id); destroy(zh); adaptor_destroy(zh); + free(zh->fd->cert); free(zh->fd); free(zh); #ifdef _WIN32 From 6d71307355ce363d9886d4707f63be70c9b49582 Mon Sep 17 00:00:00 2001 From: Gowrima Date: Tue, 29 Oct 2024 22:03:15 -0700 Subject: [PATCH 4/6] Fixing memory leak by freeing memory allocated by strdup in cert_t --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index a8434ab9095..be7bb950030 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -3873,7 +3873,8 @@ int zookeeper_close(zhandle_t *zh) LOG_INFO(LOGCALLBACK(zh), "Freeing zookeeper resources for sessionId=%#llx\n", zh->client_id.client_id); destroy(zh); adaptor_destroy(zh); - free(zh->fd->cert); + free(zh->fd->cert->certstr); + free(zh->fd->cert->ca); free(zh->fd); free(zh); #ifdef _WIN32 From 264cec25fa6543fd70a52f509b2f147896ded177 Mon Sep 17 00:00:00 2001 From: Gowrima Date: Wed, 30 Oct 2024 10:24:37 -0700 Subject: [PATCH 5/6] Fix memory leak from ssl cert in c client --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index be7bb950030..385ca1059b1 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -692,6 +692,7 @@ static void destroy(zhandle_t *zh) #ifdef HAVE_OPENSSL_H if (zh->fd->cert) { free(zh->fd->cert->certstr); + free(zh->fd->cert->ca); free(zh->fd->cert); zh->fd->cert = NULL; } @@ -3873,8 +3874,6 @@ int zookeeper_close(zhandle_t *zh) LOG_INFO(LOGCALLBACK(zh), "Freeing zookeeper resources for sessionId=%#llx\n", zh->client_id.client_id); destroy(zh); adaptor_destroy(zh); - free(zh->fd->cert->certstr); - free(zh->fd->cert->ca); free(zh->fd); free(zh); #ifdef _WIN32 From 441ec15e0b4e890c62d66700650760d298e00873 Mon Sep 17 00:00:00 2001 From: Gowrima Date: Thu, 28 Nov 2024 09:48:28 -0800 Subject: [PATCH 6/6] Removing heap memory allocation for cert.ca using strdup() since strtok() doesn't always return a pointer to the heap allocated memory. --- zookeeper-client/zookeeper-client-c/src/zookeeper.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 385ca1059b1..972215bf009 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -692,7 +692,6 @@ static void destroy(zhandle_t *zh) #ifdef HAVE_OPENSSL_H if (zh->fd->cert) { free(zh->fd->cert->certstr); - free(zh->fd->cert->ca); free(zh->fd->cert); zh->fd->cert = NULL; } @@ -1455,7 +1454,7 @@ zhandle_t *zookeeper_init_ssl(const char *host, const char *cert, watcher_fn wat { zcert_t zcert; zcert.certstr = strdup(cert); - zcert.ca = strtok(strdup(cert), ","); + zcert.ca = strtok(zcert.certstr, ","); zcert.cert = strtok(NULL, ","); zcert.key = strtok(NULL, ","); zcert.passwd = strtok(NULL, ",");