Skip to contentSkip to conversation

nvme: validate CNTLID

PATCH Superseded Hannes Reinecke 2 participants 4 messages May 3, 2019 NVMe

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover nvme: validate CNTLID

Hi all,

here are two patches to validate correct CNTLID information. A controller might violate the constrain the each CNTLID has to be unique within a subsystem, which then would cause the host to crash.
So these patches prevent this situation by validate the CNTLID and not use the cntlid as part of the device name.

As usual, comments and reviews are welcome.

Changes to v1:
- split cntlid validation into a separate helper and moved to nvme_init_subsystem()

Hannes Reinecke (2):
nvme-multipath: avoid crash on invalid subsystem cntlid enumeration
nvme: validate cntlid during controller initialisation

 drivers/nvme/host/core.c      | 27 +++++++++++++++++++++++++++
 drivers/nvme/host/multipath.c |  2 +-
 2 files changed, 28 insertions(+), 1 deletion(-)
Signature · 1 line
2.16.4
#1 lore raw

PATCH v2 1/2 nvme-multipath: avoid crash on invalid subsystem cntlid enumeration

A process holding an open reference to a removed disk prevents it from completing deletion, so its name continues to exist. A subsequent gendisk creation may have the same cntlid which risks collision when using that for the name. Use the unique ctrl->instance instead.

Signed-off-by Hannes Reinecke <hare at suse.com>
---
 drivers/nvme/host/multipath.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/multipath.c +1 -1
index 5c9429d41120..499acf07d61a 100644
@@ -31,7 +31,7 @@ void nvme_set_disk_name(char *disk_name, struct nvme_ns *ns,
 		sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
 	} else if (ns->head->disk) {
 		sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
-				ctrl->cntlid, ns->head->instance);
+				ctrl->instance, ns->head->instance);
 		*flags = GENHD_FL_HIDDEN;
 	} else {
 		sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
Signature · 1 line
2.16.4
#2 lore raw

PATCH v2 2/2 nvme: validate cntlid during controller initialisation

In reply to Hannes Reinecke

From: Hannes Reinecke <[email protected]>

The CNTLID value is required to be unique, and we do rely on this for correct operation. So reject any controller for which a non-unique CNTLID has been detected.

Signed-off-by Hannes Reinecke <hare at suse.com>
---
 drivers/nvme/host/core.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
drivers/nvme/host/core.c +27 -0
index cd16d98d1f1a..b0396135f097 100644
@@ -2358,6 +2358,25 @@ static int nvme_active_ctrls(struct nvme_subsystem *subsys)
 	return count;
 }
 
+static bool nvme_duplicate_cntlid(struct nvme_subsystem *subsys,
+				  struct nvme_ctrl *ctrl)
+{
+	struct nvme_ctrl *tmp;
+	bool ret = false;
+
+	mutex_lock(&subsys->lock);
+	list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) {
+		if (tmp == ctrl)
+			continue;
+		if (tmp->cntlid == ctrl->cntlid) {
+			ret = true;
+			break;
+		}
+	}
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 {
 	struct nvme_subsystem *subsys, *found;
@@ -2408,6 +2427,14 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 			ret = -EINVAL;
 			goto out_unlock;
 		}
+		if (nvme_duplicate_cntlid(found, ctrl)) {
+			dev_err(ctrl->device,
+				"Duplicate cntlid %u, rejecting\n",
+				ctrl->cntlid);
+			nvme_put_subsystem(found);
+			ret = -EINVAL;
+			goto out_unlock;
+		}
 
 		__nvme_release_subsystem(subsys);
 		subsys = found;
Signature · 1 line
2.16.4
#3 lore raw

PATCH v2 2/2 nvme: validate cntlid during controller initialisation

Quoted 8 lines · Hannes Reinecke
drivers/nvme/host/core.c +8 -0
+static bool nvme_duplicate_cntlid(struct nvme_subsystem *subsys,
+				  struct nvme_ctrl *ctrl)
+{
+	struct nvme_ctrl *tmp;
+	bool ret = false;
+
+	mutex_lock(&subsys->lock);
+	list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) {

drivers/nvme/host/core.c static int nvme_active_ctrls(struct nvme_subsystem *subsys) · line 2378

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+		if (tmp == ctrl)
+			continue;
+		if (tmp->cntlid == ctrl->cntlid) {
+			ret = true;
+			break;
+		}
+	}
+	mutex_unlock(&subsys->lock);
+	return ret;
+}

We'll need to do the duplicate check under the same subsys->lock critical section as adding the controller to the list, otherwise it will be racy.