From ea0bccb592ababe4f6a102886184a64e8b8003c1 Mon Sep 17 00:00:00 2001 From: Caleb Sander Date: Tue, 28 Nov 2023 19:13:42 -0700 Subject: [PATCH] linux: use cleanup functions Use cleanup attributes from cleanup.h to avoid boilerplate cleanup code. Signed-off-by: Caleb Sander --- src/nvme/linux.c | 431 ++++++++++++++++++++++------------------------- 1 file changed, 201 insertions(+), 230 deletions(-) diff --git a/src/nvme/linux.c b/src/nvme/linux.c index 7bbd4a8f..163086ea 100644 --- a/src/nvme/linux.c +++ b/src/nvme/linux.c @@ -35,6 +35,7 @@ #include +#include "cleanup.h" #include "linux.h" #include "tree.h" #include "log.h" @@ -43,8 +44,8 @@ static int __nvme_open(const char *name) { - char *path; - int fd, ret; + _cleanup_free_ char *path = NULL; + int ret; ret = asprintf(&path, "%s/%s", "/dev", name); if (ret < 0) { @@ -52,9 +53,7 @@ static int __nvme_open(const char *name) return -1; } - fd = open(path, O_RDONLY); - free(path); - return fd; + return open(path, O_RDONLY); } int nvme_open(const char *name) @@ -125,7 +124,7 @@ int nvme_fw_download_seq(int fd, __u32 size, __u32 xfer, __u32 offset, int nvme_get_telemetry_max(int fd, enum nvme_telemetry_da *da, size_t *data_tx) { - struct nvme_id_ctrl *id_ctrl; + _cleanup_free_ struct nvme_id_ctrl *id_ctrl; int err; id_ctrl = __nvme_alloc(sizeof(*id_ctrl)); @@ -134,10 +133,8 @@ int nvme_get_telemetry_max(int fd, enum nvme_telemetry_da *da, size_t *data_tx) return -1; } err = nvme_identify_ctrl(fd, id_ctrl); - if (err) { - free(id_ctrl); + if (err) return err; - } if (data_tx) { *data_tx = id_ctrl->mdts; @@ -155,7 +152,6 @@ int nvme_get_telemetry_max(int fd, enum nvme_telemetry_da *da, size_t *data_tx) *da = NVME_TELEMETRY_DA_4; } - free(id_ctrl); return err; } @@ -167,7 +163,8 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_ struct nvme_telemetry_log *telem; enum nvme_cmd_get_log_lid lid; - void *log, *tmp; + _cleanup_free_ void *log; + void *tmp; int err; size_t dalb; struct nvme_get_log_args args = { @@ -204,11 +201,12 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_ } if (err) - goto free; + return err; telem = log; if (ctrl && !telem->ctrlavail) { *buf = log; + log = NULL; *size = xfer; return 0; } @@ -229,22 +227,19 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_ break; default: errno = EINVAL; - err = -1; - goto free; + return -1; } if (dalb == 0) { errno = ENOENT; - err = -1; - goto free; + return -1; } *size = (dalb + 1) * xfer; tmp = realloc(log, *size); if (!tmp) { errno = ENOMEM; - err = -1; - goto free; + return -1; } log = tmp; @@ -252,13 +247,12 @@ int nvme_get_telemetry_log(int fd, bool create, bool ctrl, bool rae, size_t max_ args.log = log; args.len = *size; err = nvme_get_log_page(fd, max_data_tx, &args); - if (!err) { - *buf = log; - return 0; - } -free: - free(log); - return err; + if (err) + return err; + + *buf = log; + log = NULL; + return 0; } @@ -299,8 +293,9 @@ int nvme_get_new_host_telemetry(int fd, struct nvme_telemetry_log **log, int nvme_get_lba_status_log(int fd, bool rae, struct nvme_lba_status_log **log) { - __u32 size = sizeof(struct nvme_lba_status_log); - void *buf, *tmp; + __u32 size; + _cleanup_free_ struct nvme_lba_status_log *buf; + void *tmp; int err; struct nvme_get_log_args args = { .args_size = sizeof(args), @@ -316,38 +311,42 @@ int nvme_get_lba_status_log(int fd, bool rae, struct nvme_lba_status_log **log) .ot = false, }; - buf = malloc(size); + buf = malloc(sizeof(*buf)); if (!buf) return -1; - *log = buf; - err = nvme_get_log_lba_status(fd, true, 0, size, buf); - if (err) - goto free; + err = nvme_get_log_lba_status(fd, true, 0, sizeof(*buf), buf); + if (err) { + *log = NULL; + return err; + } - size = le32_to_cpu((*log)->lslplen); - if (!size) + size = le32_to_cpu(buf->lslplen); + if (!size) { + *log = buf; + buf = NULL; return 0; + } tmp = realloc(buf, size); if (!tmp) { - err = -1; - goto free; + *log = NULL; + return -1; } buf = tmp; - *log = buf; args.lid = NVME_LOG_LID_LBA_STATUS; args.log = buf; args.len = size; err = nvme_get_log_page(fd, 4096, &args); - if (!err) - return 0; + if (err) { + *log = NULL; + return err; + } -free: - *log = NULL; - free(buf); - return err; + *log = buf; + buf = NULL; + return 0; } static int nvme_ns_attachment(int fd, __u32 nsid, __u16 num_ctrls, @@ -386,7 +385,7 @@ int nvme_namespace_detach_ctrls(int fd, __u32 nsid, __u16 num_ctrls, int nvme_get_ana_log_len(int fd, size_t *analen) { - struct nvme_id_ctrl *ctrl; + _cleanup_free_ struct nvme_id_ctrl *ctrl; int ret; ctrl = __nvme_alloc(sizeof(*ctrl)); @@ -395,21 +394,18 @@ int nvme_get_ana_log_len(int fd, size_t *analen) return -1; } ret = nvme_identify_ctrl(fd, ctrl); - if (ret) { - free(ctrl); + if (ret) return ret; - } *analen = sizeof(struct nvme_ana_log) + le32_to_cpu(ctrl->nanagrpid) * sizeof(struct nvme_ana_group_desc) + le32_to_cpu(ctrl->mnan) * sizeof(__le32); - free(ctrl); return 0; } int nvme_get_logical_block_size(int fd, __u32 nsid, int *blksize) { - struct nvme_id_ns *ns; + _cleanup_free_ struct nvme_id_ns *ns; __u8 flbas; int ret; @@ -419,21 +415,18 @@ int nvme_get_logical_block_size(int fd, __u32 nsid, int *blksize) return -1; } ret = nvme_identify_ns(fd, nsid, ns); - if (ret) { - free(ns); + if (ret) return ret; - } nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas); *blksize = 1 << ns->lbaf[flbas].ds; - free(ns); return 0; } static int __nvme_set_attr(const char *path, const char *value) { - int ret, fd; + _cleanup_fd_ int fd; fd = open(path, O_WRONLY); if (fd < 0) { @@ -443,23 +436,19 @@ static int __nvme_set_attr(const char *path, const char *value) #endif return -1; } - ret = write(fd, value, strlen(value)); - close(fd); - return ret; + return write(fd, value, strlen(value)); } int nvme_set_attr(const char *dir, const char *attr, const char *value) { - char *path; + _cleanup_free_ char *path = NULL; int ret; ret = asprintf(&path, "%s/%s", dir, attr); if (ret < 0) return -1; - ret = __nvme_set_attr(path, value); - free(path); - return ret; + return __nvme_set_attr(path, value); } static char *__nvme_get_attr(const char *path) @@ -493,7 +482,7 @@ static char *__nvme_get_attr(const char *path) char *nvme_get_attr(const char *dir, const char *attr) { - char *path, *value; + _cleanup_free_ char *path = NULL; int ret; ret = asprintf(&path, "%s/%s", dir, attr); @@ -502,9 +491,7 @@ char *nvme_get_attr(const char *dir, const char *attr) return NULL; } - value = __nvme_get_attr(path); - free(path); - return value; + return __nvme_get_attr(path); } char *nvme_get_subsys_attr(nvme_subsystem_t s, const char *attr) @@ -598,16 +585,19 @@ static const EVP_MD *select_hmac(int hmac, size_t *key_len) return md; } +static DEFINE_CLEANUP_FUNC( + cleanup_evp_pkey_ctx, EVP_PKEY_CTX *, EVP_PKEY_CTX_free) +#define _cleanup_evp_pkey_ctx_ __cleanup__(cleanup_evp_pkey_ctx) + static int derive_retained_key(int hmac, const char *hostnqn, unsigned char *generated, unsigned char *retained, size_t key_len) { const EVP_MD *md; - EVP_PKEY_CTX *ctx; + _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL; uint16_t length = key_len & 0xFFFF; size_t hmac_len; - int ret; md = select_hmac(hmac, &hmac_len); if (!md || hmac_len > key_len) { @@ -622,37 +612,44 @@ static int derive_retained_key(int hmac, const char *hostnqn, } if (EVP_PKEY_derive_init(ctx) <= 0) { - ret = -ENOMEM; - goto out_free_ctx; - } - ret = -ENOKEY; - if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) - goto out_free_ctx; - if (EVP_PKEY_CTX_set1_hkdf_key(ctx, generated, key_len) <= 0) - goto out_free_ctx; + errno = ENOMEM; + return -1; + } + if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) { + errno = ENOKEY; + return -1; + } + if (EVP_PKEY_CTX_set1_hkdf_key(ctx, generated, key_len) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)&length, 2) <= 0) - goto out_free_ctx; + (const unsigned char *)&length, 2) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)"tls13 ", 6) <= 0) - goto out_free_ctx; + (const unsigned char *)"tls13 ", 6) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)"HostNQN", 7) <= 0) - goto out_free_ctx; + (const unsigned char *)"HostNQN", 7) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)hostnqn, strlen(hostnqn)) <= 0) - goto out_free_ctx; - - if (EVP_PKEY_derive(ctx, retained, &key_len) > 0) - ret = key_len; + (const unsigned char *)hostnqn, strlen(hostnqn)) <= 0) { + errno = ENOKEY; + return -1; + } -out_free_ctx: - if (ret < 0) { - errno = -ret; - ret = -1; + if (EVP_PKEY_derive(ctx, retained, &key_len) <= 0) { + errno = ENOKEY; + return -1; } - EVP_PKEY_CTX_free(ctx); - return ret; + + return key_len; } static int derive_tls_key(int hmac, const char *identity, @@ -660,10 +657,9 @@ static int derive_tls_key(int hmac, const char *identity, unsigned char *psk, size_t key_len) { const EVP_MD *md; - EVP_PKEY_CTX *ctx; + _cleanup_evp_pkey_ctx_ EVP_PKEY_CTX *ctx = NULL; size_t hmac_len; uint16_t length = key_len & 0xFFFF; - int ret; md = select_hmac(hmac, &hmac_len); if (!md || hmac_len > key_len) { @@ -678,51 +674,59 @@ static int derive_tls_key(int hmac, const char *identity, } if (EVP_PKEY_derive_init(ctx) <= 0) { - ret = -ENOMEM; - goto out_free_ctx; - } - ret = -ENOKEY; - if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) - goto out_free_ctx; - if (EVP_PKEY_CTX_set1_hkdf_key(ctx, retained, key_len) <= 0) - goto out_free_ctx; + errno = ENOMEM; + return -1; + } + if (EVP_PKEY_CTX_set_hkdf_md(ctx, md) <= 0) { + errno = ENOKEY; + return -1; + } + if (EVP_PKEY_CTX_set1_hkdf_key(ctx, retained, key_len) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)&length, 2) <= 0) - goto out_free_ctx; + (const unsigned char *)&length, 2) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)"tls13 ", 6) <= 0) - goto out_free_ctx; + (const unsigned char *)"tls13 ", 6) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, - (const unsigned char *)"nvme-tls-psk", 12) <= 0) - goto out_free_ctx; + (const unsigned char *)"nvme-tls-psk", 12) <= 0) { + errno = ENOKEY; + return -1; + } if (EVP_PKEY_CTX_add1_hkdf_info(ctx, (const unsigned char *)identity, - strlen(identity)) <= 0) - goto out_free_ctx; - - if (EVP_PKEY_derive(ctx, psk, &key_len) > 0) - ret = key_len; + strlen(identity)) <= 0) { + errno = ENOKEY; + return -1; + } -out_free_ctx: - EVP_PKEY_CTX_free(ctx); - if (ret < 0) { - errno = -ret; - ret = -1; + if (EVP_PKEY_derive(ctx, psk, &key_len) <= 0) { + errno = ENOKEY; + return -1; } - return ret; + return key_len; } #endif /* CONFIG_OPENSSL */ #ifdef CONFIG_OPENSSL_1 +static DEFINE_CLEANUP_FUNC(cleanup_hmac_ctx, HMAC_CTX *, HMAC_CTX_free) +#define _cleanup_hmac_ctx_ __cleanup__(cleanup_hmac_ctx) + int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, unsigned int key_len, unsigned char *secret, unsigned char *key) { const char hmac_seed[] = "NVMe-over-Fabrics"; - HMAC_CTX *hmac_ctx; + _cleanup_hmac_ctx_ HMAC_CTX *hmac_ctx; const EVP_MD *md; - int err = -1; ENGINE_load_builtin_engines(); ENGINE_register_all_complete(); @@ -730,14 +734,13 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, hmac_ctx = HMAC_CTX_new(); if (!hmac_ctx) { errno = ENOMEM; - return err; + return -1; } switch (hmac) { case NVME_HMAC_ALG_NONE: memcpy(key, secret, key_len); - err = 0; - goto out; + return 0; case NVME_HMAC_ALG_SHA2_256: md = EVP_sha256(); break; @@ -749,41 +752,37 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, break; default: errno = EINVAL; - goto out; + return -1; } if (!md) { errno = EINVAL; - goto out; + return -1; } if (!HMAC_Init_ex(hmac_ctx, secret, key_len, md, NULL)) { errno = ENOMEM; - goto out; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)hostnqn, strlen(hostnqn))) { errno = ENOKEY; - goto out; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)hmac_seed, strlen(hmac_seed))) { errno = ENOKEY; - goto out; + return -1; } if (!HMAC_Final(hmac_ctx, key, &key_len)) { errno = ENOKEY; - goto out; + return -1; } - err = 0; - -out: - HMAC_CTX_free(hmac_ctx); - return err; + return 0; } static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, @@ -793,10 +792,10 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, static const char hmac_seed[] = "NVMe-over-Fabrics"; size_t hmac_len; const EVP_MD *md = select_hmac(hmac, &hmac_len); - HMAC_CTX *hmac_ctx; - unsigned char *psk_ctx; - char *enc_ctx; - size_t len = -1; + _cleanup_hmac_ctx_ HMAC_CTX *hmac_ctx = NULL; + _cleanup_free_ unsigned char *psk_ctx = NULL; + _cleanup_free_ char *enc_ctx = NULL; + size_t len; if (version == 0) { sprintf(identity, "NVMe%01dR%02d %s %s", @@ -815,103 +814,102 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, } if (!md) { errno = EINVAL; - goto out_free_hmac; + return -1; } psk_ctx = malloc(key_len); if (!psk_ctx) { errno = ENOMEM; - goto out_free_hmac; + return -1; } if (!HMAC_Init_ex(hmac_ctx, retained, key_len, md, NULL)) { errno = ENOMEM; - goto out_free_psk; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)hostnqn, strlen(hostnqn))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)" ", 1)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)subsysnqn, strlen(subsysnqn))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)" ", 1)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!HMAC_Update(hmac_ctx, (unsigned char *)hmac_seed, strlen(hmac_seed))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!HMAC_Final(hmac_ctx, psk_ctx, (unsigned int *)&key_len)) { errno = ENOKEY; - goto out_free_psk; + return -1; } enc_ctx = malloc(key_len * 2); memset(enc_ctx, 0, key_len * 2); len = base64_encode(psk_ctx, key_len, enc_ctx); if (len < 0) { errno = ENOKEY; - goto out_free_enc; + return len; } sprintf(identity, "NVMe%01dR%02d %s %s %s", version, hmac, hostnqn, subsysnqn, enc_ctx); - len = strlen(identity); -out_free_enc: - free(enc_ctx); -out_free_psk: - free(psk_ctx); -out_free_hmac: - HMAC_CTX_free(hmac_ctx); - return len; + return strlen(identity); } #endif /* !CONFIG_OPENSSL_1 */ #ifdef CONFIG_OPENSSL_3 +static DEFINE_CLEANUP_FUNC( + cleanup_ossl_lib_ctx, OSSL_LIB_CTX *, OSSL_LIB_CTX_free) +#define _cleanup_ossl_lib_ctx_ __cleanup__(cleanup_ossl_lib_ctx) +static DEFINE_CLEANUP_FUNC(cleanup_evp_mac_ctx, EVP_MAC_CTX *, EVP_MAC_CTX_free) +#define _cleanup_evp_mac_ctx_ __cleanup__(cleanup_evp_mac_ctx) +static DEFINE_CLEANUP_FUNC(cleanup_evp_mac, EVP_MAC *, EVP_MAC_free) +#define _cleanup_evp_mac_ __cleanup__(cleanup_evp_mac) + int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, unsigned int key_len, unsigned char *secret, unsigned char *key) { const char hmac_seed[] = "NVMe-over-Fabrics"; OSSL_PARAM params[2], *p = params; - OSSL_LIB_CTX *lib_ctx; - EVP_MAC_CTX *mac_ctx = NULL; - EVP_MAC *mac = NULL; + _cleanup_ossl_lib_ctx_ OSSL_LIB_CTX *lib_ctx; + _cleanup_evp_mac_ctx_ EVP_MAC_CTX *mac_ctx = NULL; + _cleanup_evp_mac_ EVP_MAC *mac = NULL; char *progq = NULL; char *digest; size_t len; - int err = -1; lib_ctx = OSSL_LIB_CTX_new(); if (!lib_ctx) { errno = ENOMEM; - return err; + return -1; } mac = EVP_MAC_fetch(lib_ctx, OSSL_MAC_NAME_HMAC, progq); if (!mac) { errno = ENOMEM; - goto out; + return -1; } mac_ctx = EVP_MAC_CTX_new(mac); if (!mac_ctx) { errno = ENOMEM; - goto out; + return -1; } switch (hmac) { case NVME_HMAC_ALG_NONE: memcpy(key, secret, key_len); - err = 0; - goto out; + return 0; case NVME_HMAC_ALG_SHA2_256: digest = OSSL_DIGEST_NAME_SHA2_256; break; @@ -923,7 +921,7 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, break; default: errno = EINVAL; - goto out; + return -1; } *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest, @@ -932,39 +930,32 @@ int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, if (!EVP_MAC_init(mac_ctx, secret, key_len, params)) { errno = ENOKEY; - goto out; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)hostnqn, strlen(hostnqn))) { errno = ENOKEY; - goto out; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)hmac_seed, strlen(hmac_seed))) { errno = ENOKEY; - goto out; + return -1; } if (!EVP_MAC_final(mac_ctx, key, &len, key_len)) { errno = ENOKEY; - goto out; + return -1; } if (len != key_len) { errno = EMSGSIZE; - goto out; + return -1; } - err = 0; - -out: - EVP_MAC_CTX_free(mac_ctx); - EVP_MAC_free(mac); - OSSL_LIB_CTX_free(lib_ctx); - - return err; + return 0; } static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, @@ -974,14 +965,14 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, static const char hmac_seed[] = "NVMe-over-Fabrics"; size_t hmac_len; OSSL_PARAM params[2], *p = params; - OSSL_LIB_CTX *lib_ctx; - EVP_MAC_CTX *mac_ctx = NULL; - EVP_MAC *mac = NULL; + _cleanup_ossl_lib_ctx_ OSSL_LIB_CTX *lib_ctx = NULL; + _cleanup_evp_mac_ctx_ EVP_MAC_CTX *mac_ctx = NULL; + _cleanup_evp_mac_ EVP_MAC *mac = NULL; char *progq = NULL; char *digest = NULL; - unsigned char *psk_ctx; - char *enc_ctx; - size_t len = -1; + _cleanup_free_ unsigned char *psk_ctx = NULL; + _cleanup_free_ char *enc_ctx = NULL; + size_t len; if (version == 0) { sprintf(identity, "NVMe%01dR%02d %s %s", @@ -1001,13 +992,13 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, mac = EVP_MAC_fetch(lib_ctx, OSSL_MAC_NAME_HMAC, progq); if (!mac) { errno = ENOMEM; - goto out_free_ossl; + return -1; } mac_ctx = EVP_MAC_CTX_new(mac); if (!mac_ctx) { errno = ENOMEM; - goto out_free_mac; + return -1; } switch (hmac) { case NVME_HMAC_ALG_SHA2_256: @@ -1021,7 +1012,7 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, break; } if (!digest) - goto out_free_ctx; + return -1; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest, 0); *p = OSSL_PARAM_construct_end(); @@ -1029,66 +1020,54 @@ static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, psk_ctx = malloc(key_len); if (!psk_ctx) { errno = ENOMEM; - goto out_free_ctx; + return -1; } if (!EVP_MAC_init(mac_ctx, retained, key_len, params)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)hostnqn, strlen(hostnqn))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)" ", 1)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)subsysnqn, strlen(subsysnqn))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)" ", 1)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_update(mac_ctx, (unsigned char *)hmac_seed, strlen(hmac_seed))) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (!EVP_MAC_final(mac_ctx, psk_ctx, &hmac_len, key_len)) { errno = ENOKEY; - goto out_free_psk; + return -1; } if (hmac_len > key_len) { errno = EMSGSIZE; - goto out_free_psk; + return -1; } enc_ctx = malloc(hmac_len * 2); memset(enc_ctx, 0, hmac_len * 2); len = base64_encode(psk_ctx, hmac_len, enc_ctx); if (len < 0) { errno = ENOKEY; - goto out_free_enc; + return len; } sprintf(identity, "NVMe%01dR%02d %s %s %s", version, hmac, hostnqn, subsysnqn, enc_ctx); - len = strlen(identity); -out_free_enc: - free(enc_ctx); -out_free_psk: - free(psk_ctx); -out_free_ctx: - EVP_MAC_CTX_free(mac_ctx); -out_free_mac: - EVP_MAC_free(mac); -out_free_ossl: - OSSL_LIB_CTX_free(lib_ctx); - - return len; + return strlen(identity); } #endif /* !CONFIG_OPENSSL_3 */ @@ -1097,7 +1076,7 @@ static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn, int hmac, unsigned char *configured, unsigned char *psk, int key_len) { - unsigned char *retained; + _cleanup_free_ unsigned char *retained = NULL; int ret = -1; if (!hostnqn || !subsysnqn || !identity || !psk) { @@ -1112,15 +1091,12 @@ static int derive_nvme_keys(const char *hostnqn, const char *subsysnqn, } ret = derive_retained_key(hmac, hostnqn, configured, retained, key_len); if (ret < 0) - goto out; + return ret; ret = gen_tls_identity(hostnqn, subsysnqn, version, hmac, identity, retained, key_len); if (ret < 0) - goto out; - ret = derive_tls_key(hmac, identity, retained, psk, key_len); -out: - free(retained); - return ret; + return ret; + return derive_tls_key(hmac, identity, retained, psk, key_len); } static size_t nvme_identity_len(int hmac, int version, const char *hostnqn, @@ -1146,7 +1122,7 @@ char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn, { char *identity; size_t identity_len; - unsigned char *psk; + _cleanup_free_ unsigned char *psk = NULL; int ret = -1; identity_len = nvme_identity_len(hmac, version, hostnqn, subsysnqn); @@ -1164,7 +1140,6 @@ char *nvme_generate_tls_key_identity(const char *hostnqn, const char *subsysnqn, memset(psk, 0, key_len); ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac, configured_key, psk, key_len); - free(psk); out_free_identity: if (ret < 0) { free(identity); @@ -1218,10 +1193,10 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type, int version, int hmac, unsigned char *configured_key, int key_len) { - key_serial_t keyring_id, key = 0; - char *identity; + key_serial_t keyring_id, key; + _cleanup_free_ char *identity = NULL; size_t identity_len; - unsigned char *psk; + _cleanup_free_ unsigned char *psk = NULL; int ret = -1; keyring_id = nvme_lookup_keyring(keyring); @@ -1241,13 +1216,13 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type, psk = malloc(key_len); if (!psk) { errno = ENOMEM; - goto out_free_identity; + return 0; } memset(psk, 0, key_len); ret = derive_nvme_keys(hostnqn, subsysnqn, identity, version, hmac, configured_key, psk, key_len); if (ret != key_len) - goto out_free_psk; + return 0; key = keyctl_search(keyring_id, key_type, identity, 0); if (key > 0) { @@ -1259,10 +1234,6 @@ long nvme_insert_tls_key_versioned(const char *keyring, const char *key_type, if (key < 0) key = 0; } -out_free_psk: - free(psk); -out_free_identity: - free(identity); return key; } -- 2.50.1