nvmet: change sn size and check validity
authorNoam Gottlieb <ngottlieb@nvidia.com>
Mon, 7 Jun 2021 09:23:21 +0000 (12:23 +0300)
committerChristoph Hellwig <hch@lst.de>
Thu, 17 Jun 2021 13:51:19 +0000 (15:51 +0200)
According to the NVM specification, the serial_number should be 20 bytes
(bytes 23:04 of the Identify Controller data structure), and should
contain only ASCII characters.

In accordance, the serial_number size is changed to 20 bytes and before
any attempt to store a new value in serial_number we check that the
input is valid - i.e. contains only ASCII characters, is not empty and
does not exceed 20 bytes.

Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Signed-off-by: Noam Gottlieb <ngottlieb@nvidia.com>
Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/target/admin-cmd.c
drivers/nvme/target/configfs.c
drivers/nvme/target/core.c
drivers/nvme/target/discovery.c
drivers/nvme/target/nvmet.h

index dcd49a72f2f3c13dfa70589ad09058afcbe4c712..9c73dbfb822830102032f6eb7c77648084623fc6 100644 (file)
@@ -357,9 +357,7 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
        id->vid = 0;
        id->ssvid = 0;
 
-       memset(id->sn, ' ', sizeof(id->sn));
-       bin2hex(id->sn, &ctrl->subsys->serial,
-               min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
+       memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
        memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
                       strlen(subsys->model_number), ' ');
        memcpy_and_pad(id->fr, sizeof(id->fr),
index 65a0cf99f557daef0db50dd9e33adf18b30c9b66..027b28aaf7cd51bca89851e2d3cc1eddda4df13b 100644 (file)
@@ -1030,24 +1030,43 @@ static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
 }
 CONFIGFS_ATTR(nvmet_subsys_, attr_version);
 
+/* See Section 1.5 of NVMe 1.4 */
+static bool nvmet_is_ascii(const char c)
+{
+       return c >= 0x20 && c <= 0x7e;
+}
+
 static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
                                             char *page)
 {
        struct nvmet_subsys *subsys = to_subsys(item);
 
-       return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
+       return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
 }
 
 static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
                                              const char *page, size_t count)
 {
-       u64 serial;
+       struct nvmet_subsys *subsys = to_subsys(item);
+       int pos, len = strcspn(page, "\n");
 
-       if (sscanf(page, "%llx\n", &serial) != 1)
+       if (!len || len > NVMET_SN_MAX_SIZE) {
+               pr_err("Serial Number can not be empty or exceed %d Bytes\n",
+                      NVMET_SN_MAX_SIZE);
                return -EINVAL;
+       }
+
+       for (pos = 0; pos < len; pos++) {
+               if (!nvmet_is_ascii(page[pos])) {
+                       pr_err("Serial Number must contain only ASCII strings\n");
+                       return -EINVAL;
+               }
+       }
 
        down_write(&nvmet_config_sem);
-       to_subsys(item)->serial = serial;
+       mutex_lock(&subsys->lock);
+       memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
+       mutex_unlock(&subsys->lock);
        up_write(&nvmet_config_sem);
 
        return count;
@@ -1128,12 +1147,6 @@ static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
        return ret;
 }
 
-/* See Section 1.5 of NVMe 1.4 */
-static bool nvmet_is_ascii(const char c)
-{
-       return c >= 0x20 && c <= 0x7e;
-}
-
 static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
                const char *page, size_t count)
 {
index 4ae4bea6625de6f9c85cfac1e54b37864b52ad06..213a0c2af4f7f31251dbee95932bd89f14dcb694 100644 (file)
@@ -1493,6 +1493,7 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
                enum nvme_subsys_type type)
 {
        struct nvmet_subsys *subsys;
+       char serial[NVMET_SN_MAX_SIZE / 2];
 
        subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
        if (!subsys)
@@ -1500,7 +1501,8 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
 
        subsys->ver = NVMET_DEFAULT_VS;
        /* generate a random serial number as our controllers are ephemeral: */
-       get_random_bytes(&subsys->serial, sizeof(subsys->serial));
+       get_random_bytes(&serial, sizeof(serial));
+       bin2hex(subsys->serial, &serial, sizeof(serial));
 
        switch (type) {
        case NVME_NQN_NVME:
index fc3645fc2c24988c808d3ebbd26437afe4091e7c..b7fdad13094acfc9e3e48ba1669460416184e26f 100644 (file)
@@ -262,9 +262,7 @@ static void nvmet_execute_disc_identify(struct nvmet_req *req)
                goto out;
        }
 
-       memset(id->sn, ' ', sizeof(id->sn));
-       bin2hex(id->sn, &ctrl->subsys->serial,
-               min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
+       memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
        memset(id->fr, ' ', sizeof(id->fr));
        memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
        memcpy_and_pad(id->fr, sizeof(id->fr),
index d69a409515d6509ed41910defa206b6ad1547720..0ae809ca428cbe4e153fba577e4c51724ecb7135 100644 (file)
@@ -28,6 +28,7 @@
 #define NVMET_NO_ERROR_LOC             ((u16)-1)
 #define NVMET_DEFAULT_CTRL_MODEL       "Linux"
 #define NVMET_MN_MAX_SIZE              40
+#define NVMET_SN_MAX_SIZE              20
 
 /*
  * Supported optional AENs:
@@ -229,7 +230,7 @@ struct nvmet_subsys {
        u16                     max_qid;
 
        u64                     ver;
-       u64                     serial;
+       char                    serial[NVMET_SN_MAX_SIZE];
        char                    *subsysnqn;
        bool                    pi_support;