Skip to contentSkip to conversation

nvmet: nul-terminate the NQNs passed in the connect command

PATCH Superseded Christoph Hellwig 3 participants 3 messages Nov 10, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

PATCH 1/1 nvmet: nul-terminate the NQNs passed in the connect command

The host and subsystem NQNs are passed in the connect command payload and interpreted as nul-terminated strings. Ensure they actually are nul-terminated before using them.

Fixes a07b4970f464 "nvmet: add a generic NVMe target")
---
 drivers/nvme/target/fabrics-cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvme/target/fabrics-cmd.c +4 -0
index 43b5bd8bb6a52d..0920fe7ce4ac99 100644
@@ -244,6 +244,8 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 		goto out;
 	}
 
+	d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+	d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
 	status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
 				  le32_to_cpu(c->kato), &ctrl);
 	if (status)
@@ -313,6 +315,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 		goto out;
 	}
 
+	d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+	d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
 	ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
 				   le16_to_cpu(d->cntlid), req);
 	if (!ctrl) {
Signature · 1 line
2.39.2
#1 lore raw

Won't this overrun the subsysnqn and hostnqn fields? Writing to subsysnqn[NVMF_NQN_FIELD_LEN] will clobber the first char of hostnqn:

struct nvmf_connect_data {
        uuid_t hostid;
        __le16 cntlid;
        char resv4[238];
        char subsysnqn[NVMF_NQN_FIELD_LEN];
        char hostnqn[NVMF_NQN_FIELD_LEN];
        char resv5[256];
};

I think clearing the previous byte (index NVMF_NQN_FIELD_LEN - 1) would work. The spec requires NQNs to be under 223 bytes anyways, so they should never take up the whole field.

Quoted 38 lines · Christoph Hellwig

The host and subsystem NQNs are passed in the connect command payload and interpreted as nul-terminated strings. Ensure they actually are nul-terminated before using them.

Fixes a07b4970f464 "nvmet: add a generic NVMe target")
---
 drivers/nvme/target/fabrics-cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvme/target/fabrics-cmd.c +4 -1
index 43b5bd8bb6a52d..0920fe7ce4ac99 100644
@@ -244,6 +244,8 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
                goto out;
        }
 
+       d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+       d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
        status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
                                  le32_to_cpu(c->kato), &ctrl);
        if (status)
@@ -313,6 +315,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
                goto out;
        }
 
+       d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+       d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
        ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
                                   le16_to_cpu(d->cntlid), req);
        if (!ctrl) {
--

2.39.2

#2 lore raw
Quoted 12 lines · Caleb Sander

Won't this overrun the subsysnqn and hostnqn fields? Writing to subsysnqn[NVMF_NQN_FIELD_LEN] will clobber the first char of hostnqn:

struct nvmf_connect_data {
        uuid_t hostid;
        __le16 cntlid;
        char resv4[238];
        char subsysnqn[NVMF_NQN_FIELD_LEN];
        char hostnqn[NVMF_NQN_FIELD_LEN];
        char resv5[256];
};
Quoted 4 lines

I think clearing the previous byte (index NVMF_NQN_FIELD_LEN - 1) would work. The spec requires NQNs to be under 223 bytes anyways, so they should never take up the whole field.

True.
We should zero the byte in the NVMF_NQN_SIZE index, making the fields their real size.
Changing the patch from d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0'; to d->subsysnqn[NVMF_NQN_SIZE] = '\0';

Quoted 39 lines · Christoph Hellwig
Quoted 38 lines

The host and subsystem NQNs are passed in the connect command payload and interpreted as nul-terminated strings. Ensure they actually are nul-terminated before using them.

Fixes a07b4970f464 "nvmet: add a generic NVMe target")
---
 drivers/nvme/target/fabrics-cmd.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvme/target/fabrics-cmd.c +4 -1
index 43b5bd8bb6a52d..0920fe7ce4ac99 100644
@@ -244,6 +244,8 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
                goto out;
        }
 
+       d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+       d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
        status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
                                  le32_to_cpu(c->kato), &ctrl);
        if (status)
@@ -313,6 +315,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
                goto out;
        }
 
+       d->subsysnqn[NVMF_NQN_FIELD_LEN] = '\0';
+       d->hostnqn[NVMF_NQN_FIELD_LEN] = '\0';
        ctrl = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
                                   le16_to_cpu(d->cntlid), req);
        if (!ctrl) {
--

2.39.2