Skip to contentSkip to conversation

ndctl, keys: fix a memory leak in dimm_create_key()

PATCH In review Vishal Verma 2 participants 8 messages Feb 2, 2019 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH 1/4 ndctl, keys: fix a memory leak in dimm_create_key()

Static analysis reports that we are leaking 'fp' in the above function. Add an fclose() to close the leak.

---
 ndctl/util/keys.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
ndctl/util/keys.c +9 -8
index 85818c0..acc2ff3 100644
@@ -260,14 +260,15 @@ static key_serial_t dimm_create_key(struct ndctl_dimm *dimm,
 
 	 wrote = fwrite(buffer, 1, size, fp);
 	 if (wrote != size) {
-		 if (wrote == -1)
-			 rc = -errno;
-		 else
-			 rc = -EIO;
-		 fprintf(stderr, "Failed to write to %s: %s\n",
-				 path, strerror(-rc));
-		 free(buffer);
-		 return rc;
+		if (wrote == -1)
+			rc = -errno;
+		else
+			rc = -EIO;
+		fprintf(stderr, "Failed to write to %s: %s\n",
+				path, strerror(-rc));
+		fclose(fp);
+		free(buffer);
+		return rc;
 	 }
 
 	 fclose(fp);
Signature · 6 lines
2.20.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
author maintainer
#1 lore raw

PATCH 2/4 ndctl, keys: fix a resource leak in verify_kek()

Static analysis reports that we were leaking key_handle in the above function. Fix the error handling to properly free it for every error case.

---
 ndctl/util/keys.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
ndctl/util/keys.c +11 -5
index acc2ff3..c1f2e84 100644
@@ -393,6 +393,7 @@ static int verify_kek(struct ndctl_dimm *dimm, const char *kek)
 {
 	char *type, *desc, *key_handle;
 	key_serial_t key;
+	int rc = 0;
 
 	key_handle = strdup(kek);
 	if (!key_handle)
@@ -401,29 +402,34 @@ static int verify_kek(struct ndctl_dimm *dimm, const char *kek)
 	type = strtok(key_handle, ":");
 	if (!type) {
 		fprintf(stderr, "No key type found for kek handle\n");
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	if (strcmp(type, "trusted") != 0 &&
 			strcmp(type, "user") != 0) {
 		fprintf(stderr, "No such key type: %s", type);
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	desc = strtok(NULL, ":");
 	if (!desc) {
 		fprintf(stderr, "No description found for kek handle\n");
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	key = keyctl_search(KEY_SPEC_USER_KEYRING, type, desc, 0);
 	if (key < 0) {
 		fprintf(stderr, "No key encryption key found\n");
-		return key;
+		rc = key;
+		goto out;
 	}
 
+out:
 	free(key_handle);
-	return 0;
+	return rc;
 }
 
 int ndctl_dimm_setup_key(struct ndctl_dimm *dimm, const char *kek,
Signature · 6 lines
2.20.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 2/4
#2 lore raw

Re: [ndctl PATCH 2/4] ndctl, keys: fix a resource leak in verify_kek()

Quoted 6 lines · Vishal Verma

Static analysis reports that we were leaking key_handle in the above function. Fix the error handling to properly free it for every error case.

Looks good,

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author maintainer
#3 lore raw

PATCH 3/4 libndctl: fix a couple of theoretical buffer overruns

In reply to Vishal Verma

Static analysis reports that the ndctl_dimm_get_security() function can overflow 'buf'. Just above it, ndctl_dimm_get_available_labels() seems to make the same mistake. Fix both of these by allocating a buffer of the correct size.

---
 ndctl/lib/dimm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ndctl/lib/dimm.c +2 -2
index 0a4ca79..22cf4e1 100644
@@ -582,7 +582,7 @@ NDCTL_EXPORT unsigned long ndctl_dimm_get_available_labels(
 	struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
 	char *path = dimm->dimm_buf;
 	int rc, len = dimm->buf_len;
-	char buf[20];
+	char buf[SYSFS_ATTR_SIZE];
 
 	if (snprintf(path, len, "%s/available_slots", dimm->dimm_path) >= len) {
 		err(ctx, "%s: buffer too small!\n",
@@ -605,8 +605,8 @@ NDCTL_EXPORT enum ndctl_security_state ndctl_dimm_get_security(
 {
 	struct ndctl_ctx *ctx = ndctl_dimm_get_ctx(dimm);
 	char *path = dimm->dimm_buf;
+	char buf[SYSFS_ATTR_SIZE];
 	int len = dimm->buf_len;
-	char buf[64];
 	int rc;
 
 	if (snprintf(path, len, "%s/security", dimm->dimm_path) >= len) {
Signature · 6 lines
2.20.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 3/4
#4 lore raw

Re: [ndctl PATCH 3/4] libndctl: fix a couple of theoretical buffer overruns

Quoted 7 lines · Vishal Verma

Static analysis reports that the ndctl_dimm_get_security() function can overflow 'buf'. Just above it, ndctl_dimm_get_available_labels() seems to make the same mistake. Fix both of these by allocating a buffer of the correct size.

Looks good,

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

author maintainer
#5 lore raw

PATCH 4/4 ndctl, load-keys: fix a resource leak in load_dimm_keys()

In reply to Vishal Verma

Static analysis reports that we potentially leak 'fname' in the above function. Fix by freeing it as well as 'blob' in the exit path.

---
 ndctl/load-keys.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ndctl/load-keys.c +3 -1
index 184fa2d..95c4d4d 100644
@@ -86,7 +86,7 @@ static int load_dimm_keys(struct loadkeys *lk_ctx)
 {
 	int rc;
 	struct dirent *dent;
-	char *fname, *id, *blob;
+	char *fname = NULL, *id, *blob = NULL;
 	char desc[ND_KEY_DESC_SIZE];
 	int size, count = 0;
 	key_serial_t key;
@@ -142,6 +142,8 @@ static int load_dimm_keys(struct loadkeys *lk_ctx)
 
 	printf("%d nvdimm keys loaded\n", count);
 
+	free(fname);
+	free(blob);
 	return 0;
 }
 
Signature · 6 lines
2.20.1

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm
Reviewed-by on v1 4/4
#6 lore raw

Re: [ndctl PATCH 4/4] ndctl, load-keys: fix a resource leak in load_dimm_keys()

Quoted 5 lines · Vishal Verma

Static analysis reports that we potentially leak 'fname' in the above function. Fix by freeing it as well as 'blob' in the exit path.

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

Reviewed-by on v1 1/4
#7 lore raw
In reply to Vishal Verma
Quoted 27 lines · Vishal Verma

Static analysis reports that we are leaking 'fp' in the above function. Add an fclose() to close the leak.

---
 ndctl/util/keys.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
ndctl/util/keys.c +2 -8
index 85818c0..acc2ff3 100644
@@ -260,14 +260,15 @@ static key_serial_t dimm_create_key(struct ndctl_dimm *dimm,
 
         wrote = fwrite(buffer, 1, size, fp);
         if (wrote != size) {
-                if (wrote == -1)
-                        rc = -errno;
-                else
-                        rc = -EIO;
-                fprintf(stderr, "Failed to write to %s: %s\n",
-                                path, strerror(-rc));
-                free(buffer);
-                return rc;
+               if (wrote == -1)
+                       rc = -errno;

ndctl/util/keys.c static key_serial_t dimm_create_key(struct ndctl_dimm *dimm, · line 274

Quoted 10 lines
ndctl/util/keys.c +7 -0
+               else
+                       rc = -EIO;
+               fprintf(stderr, "Failed to write to %s: %s\n",
+                               path, strerror(-rc));
+               fclose(fp);
+               free(buffer);
+               return rc;
         }
 
         fclose(fp);

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm