nvme: validate CNTLID
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
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.
--- drivers/nvme/host/multipath.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/multipath.c +1 -1
@@ -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
PATCH v2 2/2 nvme: validate cntlid during controller initialisation
In reply to Hannes ReineckeFrom: 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.
--- drivers/nvme/host/core.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
drivers/nvme/host/core.c +27 -0
@@ -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
Christoph Hellwig
maintainerPATCH 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.