]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
test: pass a large enough buffer to nvme_identify_ns_descs()
authorCaleb Sander <csander@purestorage.com>
Sat, 12 Aug 2023 19:55:12 +0000 (13:55 -0600)
committerDaniel Wagner <wagi@monom.org>
Thu, 17 Aug 2023 12:56:12 +0000 (14:56 +0200)
nvme_identify_ns_descs() takes a struct nvme_ns_id_desc * parameter,
but passes it as the data to nvme_identify(), which sets data_len = 4K.
But struct nvme_ns_id_desc only represents the start of a single
Namespace Identification Descriptor, so it is less than 4 KB.
So it needs to be explicitly allocated with at least 4 KB.
Allocate a 4 KB buffer in test.c to avoid a stack buffer overflow.

Signed-off-by: Caleb Sander <csander@purestorage.com>
test/test.c

index e41fc9d55491c25a96140ec3e61b6a5771c9c8d9..abaf74e3c0e248ddd73feda8ae77329fa0810c75 100644 (file)
@@ -16,6 +16,7 @@
  * somewhere.
  */
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 #include <inttypes.h>
@@ -274,7 +275,7 @@ static int test_namespace(nvme_ns_t n)
 {
        int ret, nsid = nvme_ns_get_nsid(n), fd = nvme_ns_get_fd(n);
        struct nvme_id_ns ns = { 0 }, allocated = { 0 };
-       struct nvme_ns_id_desc descs = { 0 };
+       struct nvme_ns_id_desc *descs;
        __u32 result = 0;
        __u8 flbas;
 
@@ -292,11 +293,16 @@ static int test_namespace(nvme_ns_t n)
                printf("  Identify allocated ns\n");
        else
                printf("  ERROR: Identify allocated ns:%x\n", ret);
-       ret = nvme_identify_ns_descs(fd, nsid,  &descs);
+       descs = malloc(NVME_IDENTIFY_DATA_SIZE);
+       if (!descs)
+               return -1;
+
+       ret = nvme_identify_ns_descs(fd, nsid, descs);
        if (!ret)
                printf("  Identify NS Descriptors\n");
        else
                printf("  ERROR: Identify NS Descriptors:%x\n", ret);
+       free(descs);
        ret = nvme_get_features_write_protect(fd, nsid,
                NVME_GET_FEATURES_SEL_CURRENT, &result);
        if (!ret)