Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: hare@suse.de (Hannes Reinecke)
Subject: [PATCHv3 2/2] nvme: validate cntlid during controller initialisation
Date: Fri,  3 May 2019 15:37:36 +0200	[thread overview]
Message-ID: <20190503133736.111201-3-hare@suse.de> (raw)
In-Reply-To: <20190503133736.111201-1-hare@suse.de>

From: Hannes Reinecke <hare@suse.com>

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 | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index cd16d98d1f1a..dc74f7ba6f4a 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2358,6 +2358,23 @@ 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;
+
+	list_for_each_entry(tmp, &subsys->ctrls, subsys_entry) {
+		if (tmp == ctrl)
+			continue;
+		if (tmp->cntlid == ctrl->cntlid) {
+			ret = true;
+			break;
+		}
+	}
+	return ret;
+}
+
 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 {
 	struct nvme_subsystem *subsys, *found;
@@ -2411,6 +2428,7 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 
 		__nvme_release_subsystem(subsys);
 		subsys = found;
+		ret = 0;
 	} else {
 		ret = device_add(&subsys->dev);
 		if (ret) {
@@ -2434,10 +2452,20 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 	}
 
 	mutex_lock(&subsys->lock);
-	list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
+	if (!nvme_duplicate_cntlid(subsys, ctrl))
+		list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
+	else {
+		dev_err(ctrl->device,
+			"Duplicate cntlid %u, rejecting\n",
+			ctrl->cntlid);
+		ctrl->subsys = NULL;
+		sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
+		nvme_put_subsystem(subsys);
+		ret = -EINVAL;
+	}
 	mutex_unlock(&subsys->lock);
 
-	return 0;
+	return ret;
 
 out_unlock:
 	mutex_unlock(&nvme_subsystems_lock);
-- 
2.16.4

  parent reply	other threads:[~2019-05-03 13:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-03 13:37 [PATCHv3 0/2] nvme: validate CNTLID Hannes Reinecke
2019-05-03 13:37 ` [PATCHv3 1/2] nvme-multipath: avoid crash on invalid subsystem cntlid enumeration Hannes Reinecke
2019-05-03 16:01   ` Keith Busch
2019-05-08  7:54   ` Christoph Hellwig
2019-05-03 13:37 ` Hannes Reinecke [this message]
2019-05-03 16:16   ` [PATCHv3 2/2] nvme: validate cntlid during controller initialisation Keith Busch
2019-05-03 19:12   ` Max Gurtovoy
2019-05-04 14:20   ` Minwoo Im
2019-05-08  7:54   ` Christoph Hellwig
2019-05-10 14:51   ` John Donnelly
2019-05-10 18:12     ` Chaitanya Kulkarni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190503133736.111201-3-hare@suse.de \
    --to=hare@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox