[PATCH] nvme: auth: validate DHCHAP secret before stopping authentication

Sreeraj S Kurup sreekuttan2156239 at gmail.com
Thu Sep 17 01:04:24 PDT 2026


The driver currently calls nvme_auth_stop() before checking if the new
secret key is valid. If a bad key is passed through sysfs, the key
parser fails and exits early.

Since nvme_auth_stop() was already called, the current authentication
session gets stopped. However, because the parser failed, the work
thread is never restarted. This leaves the NVMe controller stuck in a
stopped state until someone writes a valid key.

Fix this by validating the key before stopping authentication. If the
key is invalid, return an error immediately and leave the running state
machine untouched.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239 at gmail.com>
---
 drivers/nvme/host/sysfs.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 02a2490a9ed7..d6512b19752d 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -825,7 +825,9 @@ static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct nvme_dhchap_key *key = NULL;
 	char *dhchap_secret;
+	int ret;
 
 	if (!ctrl->opts->dhchap_secret)
 		return -EINVAL;
@@ -838,16 +840,17 @@ static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
 	if (!dhchap_secret)
 		return -ENOMEM;
 	memcpy(dhchap_secret, buf, count);
-	nvme_auth_stop(ctrl);
+
 	if (strcmp(dhchap_secret, opts->dhchap_secret)) {
-		struct nvme_dhchap_key *key, *host_key;
-		int ret;
+		struct nvme_dhchap_key *host_key;
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
 			kfree(dhchap_secret);
 			return ret;
 		}
+
+		nvme_auth_stop(ctrl);
 		kfree(opts->dhchap_secret);
 		opts->dhchap_secret = dhchap_secret;
 		host_key = ctrl->host_key;
@@ -855,8 +858,11 @@ static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
 		ctrl->host_key = key;
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(host_key);
-	} else
+	} else {
+		nvme_auth_stop(ctrl);
 		kfree(dhchap_secret);
+	}
+
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -883,7 +889,9 @@ static ssize_t nvme_ctrl_dhchap_ctrl_secret_store(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct nvme_dhchap_key *key = NULL;
 	char *dhchap_secret;
+	int ret;
 
 	if (!ctrl->opts->dhchap_ctrl_secret)
 		return -EINVAL;
@@ -896,16 +904,17 @@ static ssize_t nvme_ctrl_dhchap_ctrl_secret_store(struct device *dev,
 	if (!dhchap_secret)
 		return -ENOMEM;
 	memcpy(dhchap_secret, buf, count);
-	nvme_auth_stop(ctrl);
+
 	if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
-		struct nvme_dhchap_key *key, *ctrl_key;
-		int ret;
+		struct nvme_dhchap_key *ctrl_key;
 
 		ret = nvme_auth_parse_key(dhchap_secret, &key);
 		if (ret) {
 			kfree(dhchap_secret);
 			return ret;
 		}
+
+		nvme_auth_stop(ctrl);
 		kfree(opts->dhchap_ctrl_secret);
 		opts->dhchap_ctrl_secret = dhchap_secret;
 		ctrl_key = ctrl->ctrl_key;
@@ -913,8 +922,11 @@ static ssize_t nvme_ctrl_dhchap_ctrl_secret_store(struct device *dev,
 		ctrl->ctrl_key = key;
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		nvme_auth_free_key(ctrl_key);
-	} else
+	} else {
+		nvme_auth_stop(ctrl);
 		kfree(dhchap_secret);
+	}
+
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
-- 
2.54.0




More information about the Linux-nvme mailing list