From 2d784d4a70accf2d306344a02f6e38faf44264ec Mon Sep 17 00:00:00 2001 From: Martin George Date: Fri, 13 Sep 2024 14:25:13 +0530 Subject: [PATCH 1/3] tree: fix dhchap_key mem leak Valgrind revealed a mem leak when specifying the dhchap host key through the config JSON file. This was because additional memory was allocated while assigning the dhchap host key through strdup calls via both nvme_ctrl_set_dhchap_host_key() and nvme_get_ctrl_attr(), but only one of them was freed. Fix the same. Signed-off-by: Martin George --- src/nvme/tree.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 372246148..7943c4962 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -1908,6 +1908,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, c->queue_count = nvme_get_ctrl_attr(c, "queue_count"); c->serial = nvme_get_ctrl_attr(c, "serial"); c->sqsize = nvme_get_ctrl_attr(c, "sqsize"); + host_key = nvme_get_ctrl_attr(c, "dhchap_secret"); if (host_key && c->s && c->s->h && c->s->h->dhchap_key && (!strcmp(c->s->h->dhchap_key, host_key) || @@ -1915,8 +1916,11 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, free(host_key); host_key = NULL; } - if (host_key) + if (host_key) { + nvme_ctrl_set_dhchap_host_key(c, NULL); c->dhchap_key = host_key; + } + c->dhchap_ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret"); if (c->dhchap_ctrl_key && !strcmp(c->dhchap_ctrl_key, "none")) { free(c->dhchap_ctrl_key); From bdb9a839e3e1d96d2cbb764c657ec88a4a1b9ab2 Mon Sep 17 00:00:00 2001 From: Martin George Date: Fri, 13 Sep 2024 14:44:05 +0530 Subject: [PATCH 2/3] tree: fix dhchap_ctrl_key mem leak Valgrind revealed mem leaks when specifying the dhchap ctrl key through both the config JSON file and the CLI. This was because additional memory was allocated while assigning the dhchap ctrl key through strdup calls via both nvme_ctrl_set_dhchap_key() and nvme_get_ctrl_attr(), but only one of them was freed. Fix the same. Signed-off-by: Martin George --- src/nvme/tree.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 7943c4962..c1ed238ca 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -1887,7 +1887,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, const char *name) { DIR *d; - char *host_key, *tls_psk; + char *host_key, *ctrl_key, *tls_psk; d = opendir(path); if (!d) { @@ -1921,11 +1921,16 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, c->dhchap_key = host_key; } - c->dhchap_ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret"); - if (c->dhchap_ctrl_key && !strcmp(c->dhchap_ctrl_key, "none")) { - free(c->dhchap_ctrl_key); - c->dhchap_ctrl_key = NULL; + ctrl_key = nvme_get_ctrl_attr(c, "dhchap_ctrl_secret"); + if (ctrl_key && !strcmp(ctrl_key, "none")) { + free(ctrl_key); + ctrl_key = NULL; + } + if (ctrl_key) { + nvme_ctrl_set_dhchap_key(c, NULL); + c->dhchap_ctrl_key = ctrl_key; } + tls_psk = nvme_get_ctrl_attr(c, "tls_key"); if (tls_psk) { char *endptr; @@ -1936,6 +1941,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, c->cfg.tls = true; } } + c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype"); c->cntlid = nvme_get_ctrl_attr(c, "cntlid"); c->dctype = nvme_get_ctrl_attr(c, "dctype"); From 4d7eacb74f35a94af8c8c4c617eee6395ed8a378 Mon Sep 17 00:00:00 2001 From: Martin George Date: Sun, 15 Sep 2024 13:39:52 +0530 Subject: [PATCH 3/3] tree: fix tls key mem leak Valgrind revealed a mem leak when specifying the tls key through both the config JSON file and the CLI. This was because memory allocated through strdup via the nvme_get_ctrl_attr() while fetching the tls key was not getting freed. Fix it. Signed-off-by: Martin George --- src/nvme/tree.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nvme/tree.c b/src/nvme/tree.c index c1ed238ca..bdb527d2f 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -1887,7 +1887,9 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, const char *name) { DIR *d; - char *host_key, *ctrl_key, *tls_psk; + char *host_key, *ctrl_key; + + _cleanup_free_ char *tls_psk = NULL; d = opendir(path); if (!d) {