All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
  2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
  2026-11-20 11:32   ` sashiko-bot
  0 siblings, 1 reply; 4+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Hannes Reinecke, Chaitanya Kulkarni
  Cc: linux-nvme, linux-kernel, Hannes Reinecke

We really should parse the dhchap keys during option parsing to avoid
having to pass around the plain dhchap secret. During options parsing
we will create a 'dhchap' key with a random UUID as description, and
store the key serial in the 'opts' structure.
This simplifies key handling as on every access the key needs to be
looked up and checked for validity before accessing the key data.

Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
 drivers/nvme/host/auth.c    | 118 +++++++++++++++++++---------
 drivers/nvme/host/fabrics.c |  84 +++++++++++++-------
 drivers/nvme/host/fabrics.h |   8 +-
 drivers/nvme/host/sysfs.c   | 186 +++++++++++++++++++++++++++++++-------------
 4 files changed, 276 insertions(+), 120 deletions(-)

diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 0ddf3834f4ea2bfedaf08b901db282b79df32879..c189096f825eab4e3053021d7437423ecb049c79 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -940,11 +940,6 @@ int nvme_auth_negotiate(struct nvme_ctrl *ctrl, int qid)
 		return -ENOKEY;
 	}
 
-	if (ctrl->opts->dhchap_ctrl_secret && !ctrl->ctrl_key) {
-		dev_warn(ctrl->device, "qid %d: invalid ctrl key\n", qid);
-		return -ENOKEY;
-	}
-
 	chap = &ctrl->dhchap_ctxs[qid];
 	cancel_work_sync(&chap->auth_work);
 	queue_work(nvme_auth_wq, &chap->auth_work);
@@ -1029,6 +1024,29 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
 	}
 }
 
+static void nvme_auth_clear_key(struct nvme_ctrl *ctrl, bool is_ctrl)
+{
+	struct key *key;
+
+	if (is_ctrl) {
+		key = ctrl->ctrl_key;
+		ctrl->ctrl_key = NULL;
+	} else {
+		key = ctrl->host_key;
+		ctrl->host_key = NULL;
+	}
+	if (key) {
+		dev_dbg(ctrl->device, "%s: revoke dhchap%s key %08x\n",
+			__func__, is_ctrl ? " ctrl" : " host",
+			key_serial(key));
+		key_revoke(key);
+		key_put(key);
+	}
+}
+
+#define nvme_auth_clear_host_key(c) nvme_auth_clear_key(c, false)
+#define nvme_auth_clear_ctrl_key(c) nvme_auth_clear_key(c, true)
+
 int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 {
 	struct nvme_dhchap_queue_context *chap;
@@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 	INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
 	if (!ctrl->opts)
 		return 0;
-	ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
-				ctrl->opts->dhchap_secret,
-				strlen(ctrl->opts->dhchap_secret));
-	if (IS_ERR(ctrl->host_key)) {
-		ret = PTR_ERR(ctrl->host_key);
-		ctrl->host_key = NULL;
-		return ret;
+	if (!ctrl->opts->dhchap_key) {
+		nvme_auth_clear_host_key(ctrl);
+		nvme_auth_clear_ctrl_key(ctrl);
+		return 0;
 	}
+	if (!ctrl->host_key)
+		nvme_auth_clear_host_key(ctrl);
 
-	ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
-				ctrl->opts->dhchap_ctrl_secret,
-				strlen(ctrl->opts->dhchap_ctrl_secret));
-	if (IS_ERR(ctrl->ctrl_key)) {
-		ret = PTR_ERR(ctrl->ctrl_key);
-		ctrl->ctrl_key = NULL;
-		goto err_free_dhchap_secret;
+	ctrl->host_key = key_get(ctrl->opts->dhchap_key);
+	if (!ctrl->host_key) {
+		dev_warn(ctrl->device,
+			 "dhchap host key %08x not present\n",
+			 key_serial(ctrl->opts->dhchap_key));
+		return -ENOKEY;
 	}
+	down_read(&ctrl->host_key->sem);
+	ret = key_validate(ctrl->host_key);
+	up_read(&ctrl->host_key->sem);
+	if (ret) {
+		dev_warn(ctrl->device,
+			 "dhchap host key %08x invalidated\n",
+			 key_serial(ctrl->host_key));
+		key_put(ctrl->host_key);
+		ctrl->host_key = NULL;
+		return -ENOKEY;
+	}
+	dev_dbg(ctrl->device,
+		"%s: using dhchap host key %08x\n",
+		__func__, key_serial(ctrl->host_key));
 
-	if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
-		return 0;
+	if (ctrl->ctrl_key)
+		nvme_auth_clear_ctrl_key(ctrl);
+
+	if (ctrl->opts->dhchap_ctrl_key) {
+		ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
+		if (!ctrl->ctrl_key) {
+			dev_warn(ctrl->device,
+				 "dhchap ctrl key %08x not present\n",
+				 key_serial(ctrl->opts->dhchap_ctrl_key));
+			return -ENOKEY;
+		}
+		down_read(&ctrl->ctrl_key->sem);
+		ret = key_validate(ctrl->ctrl_key);
+		up_read(&ctrl->ctrl_key->sem);
+		if (ret) {
+			dev_warn(ctrl->device,
+				 "dhchap ctrl key %08x invalidated\n",
+				 key_serial(ctrl->ctrl_key));
+			key_put(ctrl->ctrl_key);
+			ctrl->ctrl_key = NULL;
+			return -EKEYREVOKED;
+		}
+		dev_dbg(ctrl->device,
+			"%s: using dhchap ctrl key %08x\n",
+			__func__, key_serial(ctrl->ctrl_key));
+	}
 
 	ctrl->dhchap_ctxs = kvzalloc_objs(*chap, ctrl_max_dhchaps(ctrl));
 	if (!ctrl->dhchap_ctxs) {
-		ret = -ENOMEM;
-		goto err_free_dhchap_ctrl_secret;
+		nvme_auth_clear_ctrl_key(ctrl);
+		nvme_auth_clear_host_key(ctrl);
+		return -ENOMEM;
 	}
 
 	for (i = 0; i < ctrl_max_dhchaps(ctrl); i++) {
@@ -1074,13 +1129,6 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 	}
 
 	return 0;
-err_free_dhchap_ctrl_secret:
-	key_put(ctrl->ctrl_key);
-	ctrl->ctrl_key = NULL;
-err_free_dhchap_secret:
-	key_put(ctrl->host_key);
-	ctrl->host_key = NULL;
-	return ret;
 }
 EXPORT_SYMBOL_GPL(nvme_auth_init_ctrl);
 
@@ -1099,14 +1147,8 @@ void nvme_auth_free(struct nvme_ctrl *ctrl)
 			nvme_auth_free_dhchap(&ctrl->dhchap_ctxs[i]);
 		kvfree(ctrl->dhchap_ctxs);
 	}
-	if (ctrl->host_key) {
-		key_put(ctrl->host_key);
-		ctrl->host_key = NULL;
-	}
-	if (ctrl->ctrl_key) {
-		key_put(ctrl->ctrl_key);
-		ctrl->ctrl_key = NULL;
-	}
+	nvme_auth_clear_host_key(ctrl);
+	nvme_auth_clear_ctrl_key(ctrl);
 }
 EXPORT_SYMBOL_GPL(nvme_auth_free);
 
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 5ed91557f07065a284e664950a9c71d771614ec2..7ed8618257ca9fadc65202261a86b7ead209df7c 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -12,6 +12,7 @@
 #include <linux/seq_file.h>
 #include "nvme.h"
 #include "fabrics.h"
+#include <linux/nvme-auth.h>
 #include <linux/nvme-keyring.h>
 
 static DECLARE_RWSEM(nvmf_transports_rwsem);
@@ -717,6 +718,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 {
 	substring_t args[MAX_OPT_ARGS];
 	char *options, *o, *p;
+	char *host_secret = NULL, *ctrl_secret = NULL;
 	int token, ret = 0;
 	size_t nqnlen  = 0;
 	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
@@ -738,6 +740,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 	opts->tls_key = NULL;
 	opts->keyring = NULL;
 	opts->concat = false;
+	opts->dhchap_key = NULL;
+	opts->dhchap_ctrl_key = NULL;
 
 	options = o = kstrdup(buf, GFP_KERNEL);
 	if (!options)
@@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 				ret = -ENOMEM;
 				goto out;
 			}
-			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
-				pr_err("Invalid DH-CHAP secret %s\n", p);
-				kfree_sensitive(p);
-				ret = -EINVAL;
-				goto out;
-			}
-			kfree_sensitive(opts->dhchap_secret);
-			opts->dhchap_secret = p;
+			kfree_sensitive(host_secret);
+			host_secret = p;
 			break;
 		case NVMF_OPT_DHCHAP_CTRL_SECRET:
 			p = match_strdup(args);
@@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 				ret = -ENOMEM;
 				goto out;
 			}
-			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
-				pr_err("Invalid DH-CHAP secret %s\n", p);
-				kfree_sensitive(p);
-				ret = -EINVAL;
-				goto out;
-			}
-			kfree_sensitive(opts->dhchap_ctrl_secret);
-			opts->dhchap_ctrl_secret = p;
+			kfree_sensitive(ctrl_secret);
+			ctrl_secret = p;
 			break;
 		case NVMF_OPT_TLS:
 			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
@@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
 				opts->fast_io_fail_tmo, ctrl_loss_tmo);
 	}
+
+	opts->host = nvmf_host_add(hostnqn, &hostid);
+	if (IS_ERR(opts->host)) {
+		ret = PTR_ERR(opts->host);
+		opts->host = NULL;
+		goto out;
+	}
+
+	if (host_secret) {
+		pr_debug("lookup host identity '%s'\n", host_secret);
+		key = nvme_auth_extract_key(opts->keyring, host_secret,
+					    strlen(host_secret));
+		if (IS_ERR(key)) {
+			ret = PTR_ERR(key);
+			goto out;
+		}
+		pr_debug("using dhchap host key %08x\n", key_serial(key));
+		opts->dhchap_key = key;
+	}
+	if (ctrl_secret) {
+		if (!opts->dhchap_key) {
+			ret = -EINVAL;
+			goto out;
+		}
+		pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
+		key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
+					    strlen(ctrl_secret));
+		if (IS_ERR(key)) {
+			ret = PTR_ERR(key);
+			goto out;
+		}
+		pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+		opts->dhchap_ctrl_key = key;
+	}
+
 	if (opts->concat) {
 		if (opts->tls) {
 			pr_err("Secure concatenation over TLS is not supported\n");
@@ -1097,21 +1124,16 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			ret = -EINVAL;
 			goto out;
 		}
-		if (!opts->dhchap_secret) {
+		if (!opts->dhchap_key) {
 			pr_err("Need to enable DH-CHAP for secure concatenation\n");
 			ret = -EINVAL;
 			goto out;
 		}
 	}
 
-	opts->host = nvmf_host_add(hostnqn, &hostid);
-	if (IS_ERR(opts->host)) {
-		ret = PTR_ERR(opts->host);
-		opts->host = NULL;
-		goto out;
-	}
-
 out:
+	kfree_sensitive(ctrl_secret);
+	kfree_sensitive(host_secret);
 	kfree(options);
 	return ret;
 }
@@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
 	kfree(opts->subsysnqn);
 	kfree(opts->host_traddr);
 	kfree(opts->host_iface);
-	kfree_sensitive(opts->dhchap_secret);
-	kfree_sensitive(opts->dhchap_ctrl_secret);
+	if (opts->dhchap_key) {
+		pr_debug("revoke dhchap host key %08x\n",
+			 key_serial(opts->dhchap_key));
+		key_revoke(opts->dhchap_key);
+		key_put(opts->dhchap_key);
+	}
+	if (opts->dhchap_ctrl_key) {
+		pr_debug("revoke dhchap ctrl key %08x\n",
+			 key_serial(opts->dhchap_ctrl_key));
+		key_revoke(opts->dhchap_key);
+		key_put(opts->dhchap_ctrl_key);
+	}
 	kfree(opts);
 }
 EXPORT_SYMBOL_GPL(nvmf_free_options);
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index caf5503d0833296aa03c028c0199cf92cc409edc..786f1b4819f076ac1f133070523be1780ec4d8b7 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -96,8 +96,8 @@ enum {
  * @discovery_nqn: indicates if the subsysnqn is the well-known discovery NQN.
  * @kato:	Keep-alive timeout.
  * @host:	Virtual NVMe host, contains the NQN and Host ID.
- * @dhchap_secret: DH-HMAC-CHAP secret
- * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
+ * @dhchap_key: DH-HMAC-CHAP pre-shared key
+ * @dhchap_ctrl_key: DH-HMAC-CHAP controller pre-shared key for bi-directional
  *              authentication
  * @keyring:    Keyring to use for key lookups
  * @tls_key:    TLS key for encrypted connections (TCP)
@@ -127,8 +127,8 @@ struct nvmf_ctrl_options {
 	bool			duplicate_connect;
 	unsigned int		kato;
 	struct nvmf_host	*host;
-	char			*dhchap_secret;
-	char			*dhchap_ctrl_secret;
+	struct key		*dhchap_key;
+	struct key		*dhchap_ctrl_key;
 	struct key		*keyring;
 	struct key		*tls_key;
 	bool			tls;
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index a5c1511e49ac275e75a70ff89958ed8f0cb2c633..1311f5e29b004436e39a723242748a150e36dc68 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -6,6 +6,8 @@
  */
 
 #include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>
 #include <linux/blkdev.h>
 
 #include "nvme.h"
@@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
-	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key = ctrl->host_key;
+	ssize_t count;
 
-	if (!opts->dhchap_secret)
+	if (!key)
 		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
+	down_read(&key->sem);
+	if (key_validate(key))
+		count = sysfs_emit(buf, "<invalidated>\n");
+	else {
+		count = key->type->read(key, buf, PAGE_SIZE);
+		if (count > 0) {
+			buf[count] = '\n';
+			count++;
+		}
+	}
+	up_read(&key->sem);
+	return count;
 }
 
 static ssize_t dhchap_secret_store(struct device *dev,
@@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key, *old_key;
 	char *dhchap_secret;
+	size_t len;
+	int ret;
 
-	if (!ctrl->opts->dhchap_secret)
-		return -EINVAL;
-	if (count < 7)
+	if (!ctrl->host_key || !count)
 		return -EINVAL;
 
-	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
+	len = strcspn(buf, "\n");
+	dhchap_secret = kzalloc(len + 1, GFP_KERNEL);
 	if (!dhchap_secret)
 		return -ENOMEM;
-	memcpy(dhchap_secret, buf, count);
+	memcpy(dhchap_secret, buf, len);
 	nvme_auth_stop(ctrl);
-	if (strcmp(dhchap_secret, opts->dhchap_secret)) {
-		struct key *key, *host_key;
-
-		key = nvme_auth_extract_key(opts->keyring, dhchap_secret,
-					    count);
-		if (IS_ERR(key)) {
-			kfree_sensitive(dhchap_secret);
-			return PTR_ERR(key);
-		}
-		kfree_sensitive(opts->dhchap_secret);
-		opts->dhchap_secret = dhchap_secret;
-		host_key = ctrl->host_key;
-		mutex_lock(&ctrl->dhchap_auth_mutex);
-		ctrl->host_key = key;
-		mutex_unlock(&ctrl->dhchap_auth_mutex);
-		key_put(host_key);
-	} else
+
+	key = nvme_auth_extract_key(opts->keyring, dhchap_secret, count);
+	if (IS_ERR(key)) {
 		kfree_sensitive(dhchap_secret);
+		return PTR_ERR(key);
+	}
+	down_read(&key->sem);
+	ret = key_validate(key);
+	up_read(&key->sem);
+	if (ret) {
+		dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
+		dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
+		key_revoke(key);
+		key_put(key);
+		kfree_sensitive(dhchap_secret);
+		return ret;
+	}
+	mutex_lock(&ctrl->dhchap_auth_mutex);
+	old_key = ctrl->host_key;
+	dev_dbg(ctrl->dev, "revoke host key %08x\n",
+		key_serial(old_key));
+	key_revoke(old_key);
+
+	ctrl->host_key = key;
+	mutex_unlock(&ctrl->dhchap_auth_mutex);
+	key_put(old_key);
+	kfree_sensitive(dhchap_secret);
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
-	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key = ctrl->ctrl_key;
+	size_t count;
 
-	if (!opts->dhchap_ctrl_secret)
+	if (!key)
 		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+	down_read(&key->sem);
+	if (key_validate(key))
+		count = sysfs_emit(buf, "<invalidated>");
+	else {
+		count = key->type->read(key, buf, PAGE_SIZE);
+		if (count > 0) {
+			buf[count] = '\n';
+			count++;
+		}
+	}
+	up_read(&key->sem);
+	return count;
 }
 
 static ssize_t dhchap_ctrl_secret_store(struct device *dev,
@@ -885,38 +922,46 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key, *old_key;
 	char *dhchap_secret;
+	size_t len;
+	int ret;
 
-	if (!ctrl->opts->dhchap_ctrl_secret)
-		return -EINVAL;
-	if (count < 7)
-		return -EINVAL;
-	if (memcmp(buf, "DHHC-1:", 7))
+	if (!ctrl->ctrl_key || !count)
 		return -EINVAL;
 
-	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
+	len = strcspn(buf, "\n");
+	dhchap_secret = kzalloc(len + 1, GFP_KERNEL);
 	if (!dhchap_secret)
 		return -ENOMEM;
-	memcpy(dhchap_secret, buf, count);
+	memcpy(dhchap_secret, buf, len);
 	nvme_auth_stop(ctrl);
-	if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
-		struct key *key, *ctrl_key;
-
-		key = nvme_auth_extract_key(opts->keyring, dhchap_secret,
-					    count);
-		if (IS_ERR(key)) {
-			kfree_sensitive(dhchap_secret);
-			return PTR_ERR(key);
-		}
-		kfree_sensitive(opts->dhchap_ctrl_secret);
-		opts->dhchap_ctrl_secret = dhchap_secret;
-		ctrl_key = ctrl->ctrl_key;
-		mutex_lock(&ctrl->dhchap_auth_mutex);
-		ctrl->ctrl_key = key;
-		mutex_unlock(&ctrl->dhchap_auth_mutex);
-		key_put(ctrl_key);
-	} else
+	key = nvme_auth_extract_key(opts->keyring, dhchap_secret, count);
+	if (IS_ERR(key)) {
 		kfree_sensitive(dhchap_secret);
+		return PTR_ERR(key);
+	}
+	down_read(&key->sem);
+	ret = key_validate(key);
+	up_read(&key->sem);
+	if (ret) {
+		dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
+		dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
+		key_revoke(key);
+		key_put(key);
+		kfree_sensitive(dhchap_secret);
+		return ret;
+	}
+	mutex_lock(&ctrl->dhchap_auth_mutex);
+	old_key = ctrl->ctrl_key;
+	dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
+		key_serial(old_key));
+	key_revoke(old_key);
+
+	ctrl->ctrl_key = key;
+	mutex_unlock(&ctrl->dhchap_auth_mutex);
+	key_put(old_key);
+	kfree_sensitive(dhchap_secret);
 	/* Start re-authentication */
 	dev_info(ctrl->device, "re-authenticating controller\n");
 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
 }
 
 static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
+
+static ssize_t dhchap_key_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	struct key *key = ctrl->host_key;
+	size_t count;
+
+	if (!key)
+		return 0;
+	down_read(&key->sem);
+	if (key_validate(key))
+		count = sysfs_emit(buf, "<invalidated>\n");
+	else {
+		count = key->type->read(key, buf, PAGE_SIZE);
+		if (count > 0) {
+			buf[count] = '\n';
+			count++;
+		}
+	}
+	up_read(&key->sem);
+	return count;
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_key);
+
+static ssize_t dhchap_ctrl_key_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	if (!ctrl->ctrl_key)
+		return 0;
+	return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
 #endif
 
 static struct attribute *nvme_dev_attrs[] = {
@@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
 #ifdef CONFIG_NVME_HOST_AUTH
 	&dev_attr_dhchap_secret.attr,
 	&dev_attr_dhchap_ctrl_secret.attr,
+	&dev_attr_dhchap_key.attr,
+	&dev_attr_dhchap_ctrl_key.attr,
 #endif
 	&dev_attr_adm_passthru_err_log_enabled.attr,
 	NULL

-- 
2.51.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
  2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
@ 2026-11-20 11:32   ` sashiko-bot
  2026-11-20 13:55     ` Hannes Reinecke
  0 siblings, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-11-20 11:32 UTC (permalink / raw)
  To: Hannes Reinecke; +Cc: linux-nvme

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
  dhchap_ctrl_key
- [High] nvme: build failure in nvmf_parse_options() and sysfs.c without
  auth/keyring
- [High] nvme: removing secret length checks exposes underflows in key
  preparsing
- [Medium] nvme: out-of-bounds write on read error in
  dhchap_ctrl_secret_show()
- [Medium] nvme-auth: host_key ref leak and inverted check in
  nvme_auth_init_ctrl()
- [Medium] nvme: unlocked host_key and ctrl_key access in dhchap sysfs
  show callbacks
- [Low] nvme: check !ctrl->opts for dhchap_key attrs in
  nvme_dev_attrs_are_visible()
--

commit ddf0d4926edd9ec44ea25839b02c0ba20380f64e
Author: Hannes Reinecke <hare@kernel.org>

nvme: parse dhchap keys during option parsing

This commit parses DH-HMAC-CHAP secrets into struct key pointers during
fabric option parsing instead of storing raw secret strings in
nvmf_ctrl_options, and adds sysfs attributes to expose the host and
controller key serials.

> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
> index 0ddf3834f4ea2..c189096f825ea 100644
> --- a/drivers/nvme/host/auth.c
> +++ b/drivers/nvme/host/auth.c
[ ... ]
> @@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>  	INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
>  	if (!ctrl->opts)
>  		return 0;
> -	ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
> -				ctrl->opts->dhchap_secret,
> -				strlen(ctrl->opts->dhchap_secret));
> -	if (IS_ERR(ctrl->host_key)) {
> -		ret = PTR_ERR(ctrl->host_key);
> -		ctrl->host_key = NULL;
> -		return ret;
> +	if (!ctrl->opts->dhchap_key) {
> +		nvme_auth_clear_host_key(ctrl);
> +		nvme_auth_clear_ctrl_key(ctrl);
> +		return 0;
>  	}
> +	if (!ctrl->host_key)
> +		nvme_auth_clear_host_key(ctrl);
>
> -	ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
> -				ctrl->opts->dhchap_ctrl_secret,
> -				strlen(ctrl->opts->dhchap_ctrl_secret));
> -	if (IS_ERR(ctrl->ctrl_key)) {
> -		ret = PTR_ERR(ctrl->ctrl_key);
> -		ctrl->ctrl_key = NULL;
> -		goto err_free_dhchap_secret;
> +	ctrl->host_key = key_get(ctrl->opts->dhchap_key);
> +	if (!ctrl->host_key) {
> +		dev_warn(ctrl->device,
> +			 "dhchap host key %08x not present\n",
> +			 key_serial(ctrl->opts->dhchap_key));
> +		return -ENOKEY;
>  	}
> +	down_read(&ctrl->host_key->sem);
> +	ret = key_validate(ctrl->host_key);
> +	up_read(&ctrl->host_key->sem);
> +	if (ret) {
> +		dev_warn(ctrl->device,
> +			 "dhchap host key %08x invalidated\n",
> +			 key_serial(ctrl->host_key));
> +		key_put(ctrl->host_key);
> +		ctrl->host_key = NULL;
> +		return -ENOKEY;
> +	}
> +	dev_dbg(ctrl->device,
> +		"%s: using dhchap host key %08x\n",
> +		__func__, key_serial(ctrl->host_key));
>
> -	if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
> -		return 0;
> +	if (ctrl->ctrl_key)
> +		nvme_auth_clear_ctrl_key(ctrl);
> +
> +	if (ctrl->opts->dhchap_ctrl_key) {
> +		ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
> +		if (!ctrl->ctrl_key) {
> +			dev_warn(ctrl->device,
> +				 "dhchap ctrl key %08x not present\n",
> +				 key_serial(ctrl->opts->dhchap_ctrl_key));
> +			return -ENOKEY;
> +		}
> +		down_read(&ctrl->ctrl_key->sem);
> +		ret = key_validate(ctrl->ctrl_key);
> +		up_read(&ctrl->ctrl_key->sem);
> +		if (ret) {
> +			dev_warn(ctrl->device,
> +				 "dhchap ctrl key %08x invalidated\n",
> +				 key_serial(ctrl->ctrl_key));
> +			key_put(ctrl->ctrl_key);
> +			ctrl->ctrl_key = NULL;
> +			return -EKEYREVOKED;
> +		}

[Severity: Medium]
Do the error returns in the ctrl->opts->dhchap_ctrl_key block leak the
ctrl->host_key reference taken earlier in nvme_auth_init_ctrl()?

When nvme_auth_init_ctrl() fails, nvme_init_ctrl() jumps to
out_release_instance before initializing ctrl->ctrl_device, so
nvme_auth_free() is never called on that error path.

Also, the check before nvme_auth_clear_host_key(ctrl) is inverted
(!ctrl->host_key instead of ctrl->host_key), and nvme_init_ctrl() calls
nvme_auth_init_ctrl() before assigning ctrl->device = &ctrl->ctrl_device,
so the dev_warn() and dev_dbg() calls here pass a NULL ctrl->device.

> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index 5ed91557f0706..7ed8618257ca9 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c
[ ... ]
> @@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>  				ret = -ENOMEM;
>  				goto out;
>  			}
> -			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
> -				pr_err("Invalid DH-CHAP secret %s\n", p);
> -				kfree_sensitive(p);
> -				ret = -EINVAL;
> -				goto out;
> -			}
> -			kfree_sensitive(opts->dhchap_secret);
> -			opts->dhchap_secret = p;
> +			kfree_sensitive(host_secret);
> +			host_secret = p;
>  			break;
>  		case NVMF_OPT_DHCHAP_CTRL_SECRET:
>  			p = match_strdup(args);
> @@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>  				ret = -ENOMEM;
>  				goto out;
>  			}
> -			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
> -				pr_err("Invalid DH-CHAP secret %s\n", p);
> -				kfree_sensitive(p);
> -				ret = -EINVAL;
> -				goto out;
> -			}
> -			kfree_sensitive(opts->dhchap_ctrl_secret);
> -			opts->dhchap_ctrl_secret = p;
> +			kfree_sensitive(ctrl_secret);
> +			ctrl_secret = p;
>  			break;

[Severity: High]
Does removing the minimum length checks here and in dhchap_secret_store() /
dhchap_ctrl_secret_store() allow short secret strings to underflow datalen
and ret in nvme_dhchap_psk_preparse()?

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
    ...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
		   &version, &hmac) != 2) {
    ...
	}

	/* skip header and final ':' character */
	datalen -= 11;
}

Because %*s is assignment-suppressed and %02hhu matches one or two digits,
sscanf() returns 2 for 8- to 10-byte strings such as "DHHC-1:0:a", causing
unsigned datalen -= 11 to wrap around and wrap the kzalloc() size
calculation sizeof(*upayload) + datalen.  For 11- to 15-byte inputs such as
"DHHC-1:00:a" where base64_decode() returns 0..3, ret -= 4 in
nvme_dhchap_psk_preparse() becomes negative and is passed as an unsigned
length to crc32(~0, upayload->data, ret), reading out-of-bounds across
kernel memory.

[ ... ]

> @@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>  			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
>  				opts->fast_io_fail_tmo, ctrl_loss_tmo);
>  	}
> +
> +	opts->host = nvmf_host_add(hostnqn, &hostid);
> +	if (IS_ERR(opts->host)) {
> +		ret = PTR_ERR(opts->host);
> +		opts->host = NULL;
> +		goto out;
> +	}
> +
> +	if (host_secret) {
> +		pr_debug("lookup host identity '%s'\n", host_secret);
> +		key = nvme_auth_extract_key(opts->keyring, host_secret,
> +					    strlen(host_secret));
> +		if (IS_ERR(key)) {
> +			ret = PTR_ERR(key);
> +			goto out;
> +		}
> +		pr_debug("using dhchap host key %08x\n", key_serial(key));
> +		opts->dhchap_key = key;
> +	}
> +	if (ctrl_secret) {
> +		if (!opts->dhchap_key) {
> +			ret = -EINVAL;
> +			goto out;
> +		}
> +		pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
> +		key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
> +					    strlen(ctrl_secret));

[Severity: High]
Does calling nvme_auth_extract_key() unconditionally here break the build
when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH (or when
CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m)?

include/linux/nvme-auth.h declares nvme_auth_extract_key() without a
!CONFIG_NVME_AUTH stub, and match_token() is an external call so the
compiler cannot dead-code-eliminate the host_secret and ctrl_secret
branches when CONFIG_NVME_HOST_AUTH is disabled.

In addition, drivers/nvme/host/sysfs.c now includes <linux/nvme-keyring.h>
unconditionally, which fails to compile when CONFIG_NVME_KEYRING is
disabled due to a stray semicolon and a non-inline function definition in
the fallback block:

include/linux/nvme-keyring.h:
static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
		const char *hostnqn, const char *subnqn, u8 hmac);
{
	return ERR_PTR(-EOPNOTSUPP);
}
u8 nvme_dhchap_psk_hash(struct key *key)
{
	return 0;
}

[ ... ]

> @@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>  	kfree(opts->subsysnqn);
>  	kfree(opts->host_traddr);
>  	kfree(opts->host_iface);
> -	kfree_sensitive(opts->dhchap_secret);
> -	kfree_sensitive(opts->dhchap_ctrl_secret);
> +	if (opts->dhchap_key) {
> +		pr_debug("revoke dhchap host key %08x\n",
> +			 key_serial(opts->dhchap_key));
> +		key_revoke(opts->dhchap_key);
> +		key_put(opts->dhchap_key);
> +	}
> +	if (opts->dhchap_ctrl_key) {
> +		pr_debug("revoke dhchap ctrl key %08x\n",
> +			 key_serial(opts->dhchap_ctrl_key));
> +		key_revoke(opts->dhchap_key);
> +		key_put(opts->dhchap_ctrl_key);
> +	}

[Severity: High]
In the opts->dhchap_ctrl_key block, should key_revoke() be passed
opts->dhchap_ctrl_key instead of opts->dhchap_key?  Passing
opts->dhchap_key dereferences the host key after key_put(opts->dhchap_key)
has already dropped its reference, and leaves opts->dhchap_ctrl_key
unrevoked.

> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index a5c1511e49ac2..1311f5e29b004 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
[ ... ]
> @@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
>  		struct device_attribute *attr, char *buf)
>  {
>  	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> -	struct nvmf_ctrl_options *opts = ctrl->opts;
> +	struct key *key = ctrl->host_key;
> +	ssize_t count;
>
> -	if (!opts->dhchap_secret)
> +	if (!key)
>  		return sysfs_emit(buf, "none\n");
> -	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
> +	down_read(&key->sem);
> +	if (key_validate(key))
> +		count = sysfs_emit(buf, "<invalidated>\n");
> +	else {
> +		count = key->type->read(key, buf, PAGE_SIZE);
> +		if (count > 0) {
> +			buf[count] = '\n';
> +			count++;
> +		}
> +	}
> +	up_read(&key->sem);
> +	return count;
>  }
[ ... ]
> @@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
[ ... ]
> +	mutex_lock(&ctrl->dhchap_auth_mutex);
> +	old_key = ctrl->host_key;
> +	dev_dbg(ctrl->dev, "revoke host key %08x\n",
> +		key_serial(old_key));
> +	key_revoke(old_key);
> +
> +	ctrl->host_key = key;
> +	mutex_unlock(&ctrl->dhchap_auth_mutex);
> +	key_put(old_key);

[Severity: Medium]
Can a concurrent read of dhchap_secret (or dhchap_ctrl_secret, dhchap_key,
or dhchap_ctrl_key) race with dhchap_secret_store() or
dhchap_ctrl_secret_store() and trigger a use-after-free on the old key?

CPU0                              CPU1
dhchap_secret_show()
  key = ctrl->host_key;
                                  dhchap_secret_store()
                                    mutex_lock(&ctrl->dhchap_auth_mutex);
                                    old_key = ctrl->host_key;
                                    key_revoke(old_key);
                                    ctrl->host_key = key;
                                    mutex_unlock(&ctrl->dhchap_auth_mutex);
                                    key_put(old_key);  <-- frees old_key
  down_read(&key->sem);  <-- use-after-free

None of dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(),
or dhchap_ctrl_key_show() hold ctrl->dhchap_auth_mutex or take a key_get()
reference before dereferencing ctrl->host_key or ctrl->ctrl_key.  Once a
key has been replaced via sysfs store (so opts->dhchap_key no longer holds
a reference), ctrl->host_key holds the only reference, and key_put(old_key)
frees it while a concurrent show callback is using it.

> @@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
>  		struct device_attribute *attr, char *buf)
>  {
>  	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> -	struct nvmf_ctrl_options *opts = ctrl->opts;
> +	struct key *key = ctrl->ctrl_key;
> +	size_t count;
>
> -	if (!opts->dhchap_ctrl_secret)
> +	if (!key)
>  		return sysfs_emit(buf, "none\n");
> -	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
> +	down_read(&key->sem);
> +	if (key_validate(key))
> +		count = sysfs_emit(buf, "<invalidated>");
> +	else {
> +		count = key->type->read(key, buf, PAGE_SIZE);
> +		if (count > 0) {
> +			buf[count] = '\n';
> +			count++;
> +		}
> +	}
> +	up_read(&key->sem);
> +	return count;
>  }

[Severity: Medium]
Can a negative error return from key->type->read() cause an out-of-bounds
write before buf in dhchap_ctrl_secret_show() and dhchap_key_show()?

Unlike dhchap_secret_show(), which declares ssize_t count, both
dhchap_ctrl_secret_show() and dhchap_key_show() declare count as unsigned
size_t.  If key->type->read() returns a negative error code (such as
-EINVAL), count wraps to a large positive value, passes count > 0, and
writes '\n' to buf[count] out-of-bounds.  Commit "nvme: allow to pass in
key description as dhchap secret" later in this series replaces
key->type->read() in dhchap_key_show(), but dhchap_ctrl_secret_show()
retains the unsigned size_t count and also omits the trailing newline on
"<invalidated>".

[ ... ]

> @@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
>  }
>
>  static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
> +
> +static ssize_t dhchap_key_show(struct device *dev,
> +		struct device_attribute *attr, char *buf)
> +{
> +	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> +	struct key *key = ctrl->host_key;
> +	size_t count;
> +
> +	if (!key)
> +		return 0;
> +	down_read(&key->sem);
> +	if (key_validate(key))
> +		count = sysfs_emit(buf, "<invalidated>\n");
> +	else {
> +		count = key->type->read(key, buf, PAGE_SIZE);
> +		if (count > 0) {
> +			buf[count] = '\n';
> +			count++;
> +		}
> +	}
> +	up_read(&key->sem);
> +	return count;
> +}
> +static DEVICE_ATTR_ADMIN_RO(dhchap_key);
> +
> +static ssize_t dhchap_ctrl_key_show(struct device *dev,
> +		struct device_attribute *attr, char *buf)
> +{
> +	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> +
> +	if (!ctrl->ctrl_key)
> +		return 0;
> +	return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
> +}
> +static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
>  #endif
>
>  static struct attribute *nvme_dev_attrs[] = {
> @@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
>  #ifdef CONFIG_NVME_HOST_AUTH
>  	&dev_attr_dhchap_secret.attr,
>  	&dev_attr_dhchap_ctrl_secret.attr,
> +	&dev_attr_dhchap_key.attr,
> +	&dev_attr_dhchap_ctrl_key.attr,
>  #endif

[Severity: Low]
Should nvme_dev_attrs_are_visible() hide dev_attr_dhchap_key.attr and
dev_attr_dhchap_ctrl_key.attr when !ctrl->opts, the same way it hides
dev_attr_dhchap_secret.attr and dev_attr_dhchap_ctrl_secret.attr?  Without
that check, both attributes appear on non-Fabrics controllers (such as PCIe
controllers) when CONFIG_NVME_HOST_AUTH is enabled.

Also, in this patch dhchap_key_show() outputs the key payload via
key->type->read() instead of key_serial(key) as dhchap_ctrl_key_show() does
(though commit "nvme: allow to pass in key description as dhchap secret"
later in the series updates dhchap_key_show() to emit key_serial()).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=4


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
  2026-11-20 11:32   ` sashiko-bot
@ 2026-11-20 13:55     ` Hannes Reinecke
  0 siblings, 0 replies; 4+ messages in thread
From: Hannes Reinecke @ 2026-11-20 13:55 UTC (permalink / raw)
  To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme

On 11/20/26 12:32 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 7 potential
> issues to consider:
> - [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
>    dhchap_ctrl_key
> - [High] nvme: build failure in nvmf_parse_options() and sysfs.c without
>    auth/keyring
> - [High] nvme: removing secret length checks exposes underflows in key
>    preparsing
> - [Medium] nvme: out-of-bounds write on read error in
>    dhchap_ctrl_secret_show()
> - [Medium] nvme-auth: host_key ref leak and inverted check in
>    nvme_auth_init_ctrl()
> - [Medium] nvme: unlocked host_key and ctrl_key access in dhchap sysfs
>    show callbacks
> - [Low] nvme: check !ctrl->opts for dhchap_key attrs in
>    nvme_dev_attrs_are_visible()
> --
> 
> commit ddf0d4926edd9ec44ea25839b02c0ba20380f64e
> Author: Hannes Reinecke <hare@kernel.org>
> 
> nvme: parse dhchap keys during option parsing
> 
> This commit parses DH-HMAC-CHAP secrets into struct key pointers during
> fabric option parsing instead of storing raw secret strings in
> nvmf_ctrl_options, and adds sysfs attributes to expose the host and
> controller key serials.
> 
>> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
>> index 0ddf3834f4ea2..c189096f825ea 100644
>> --- a/drivers/nvme/host/auth.c
>> +++ b/drivers/nvme/host/auth.c
> [ ... ]
>> @@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>>   	INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
>>   	if (!ctrl->opts)
>>   		return 0;
>> -	ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
>> -				ctrl->opts->dhchap_secret,
>> -				strlen(ctrl->opts->dhchap_secret));
>> -	if (IS_ERR(ctrl->host_key)) {
>> -		ret = PTR_ERR(ctrl->host_key);
>> -		ctrl->host_key = NULL;
>> -		return ret;
>> +	if (!ctrl->opts->dhchap_key) {
>> +		nvme_auth_clear_host_key(ctrl);
>> +		nvme_auth_clear_ctrl_key(ctrl);
>> +		return 0;
>>   	}
>> +	if (!ctrl->host_key)
>> +		nvme_auth_clear_host_key(ctrl);
>>
>> -	ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
>> -				ctrl->opts->dhchap_ctrl_secret,
>> -				strlen(ctrl->opts->dhchap_ctrl_secret));
>> -	if (IS_ERR(ctrl->ctrl_key)) {
>> -		ret = PTR_ERR(ctrl->ctrl_key);
>> -		ctrl->ctrl_key = NULL;
>> -		goto err_free_dhchap_secret;
>> +	ctrl->host_key = key_get(ctrl->opts->dhchap_key);
>> +	if (!ctrl->host_key) {
>> +		dev_warn(ctrl->device,
>> +			 "dhchap host key %08x not present\n",
>> +			 key_serial(ctrl->opts->dhchap_key));
>> +		return -ENOKEY;
>>   	}
>> +	down_read(&ctrl->host_key->sem);
>> +	ret = key_validate(ctrl->host_key);
>> +	up_read(&ctrl->host_key->sem);
>> +	if (ret) {
>> +		dev_warn(ctrl->device,
>> +			 "dhchap host key %08x invalidated\n",
>> +			 key_serial(ctrl->host_key));
>> +		key_put(ctrl->host_key);
>> +		ctrl->host_key = NULL;
>> +		return -ENOKEY;
>> +	}
>> +	dev_dbg(ctrl->device,
>> +		"%s: using dhchap host key %08x\n",
>> +		__func__, key_serial(ctrl->host_key));
>>
>> -	if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
>> -		return 0;
>> +	if (ctrl->ctrl_key)
>> +		nvme_auth_clear_ctrl_key(ctrl);
>> +
>> +	if (ctrl->opts->dhchap_ctrl_key) {
>> +		ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
>> +		if (!ctrl->ctrl_key) {
>> +			dev_warn(ctrl->device,
>> +				 "dhchap ctrl key %08x not present\n",
>> +				 key_serial(ctrl->opts->dhchap_ctrl_key));
>> +			return -ENOKEY;
>> +		}
>> +		down_read(&ctrl->ctrl_key->sem);
>> +		ret = key_validate(ctrl->ctrl_key);
>> +		up_read(&ctrl->ctrl_key->sem);
>> +		if (ret) {
>> +			dev_warn(ctrl->device,
>> +				 "dhchap ctrl key %08x invalidated\n",
>> +				 key_serial(ctrl->ctrl_key));
>> +			key_put(ctrl->ctrl_key);
>> +			ctrl->ctrl_key = NULL;
>> +			return -EKEYREVOKED;
>> +		}
> 
> [Severity: Medium]
> Do the error returns in the ctrl->opts->dhchap_ctrl_key block leak the
> ctrl->host_key reference taken earlier in nvme_auth_init_ctrl()?
> 
> When nvme_auth_init_ctrl() fails, nvme_init_ctrl() jumps to
> out_release_instance before initializing ctrl->ctrl_device, so
> nvme_auth_free() is never called on that error path.
> 
> Also, the check before nvme_auth_clear_host_key(ctrl) is inverted
> (!ctrl->host_key instead of ctrl->host_key), and nvme_init_ctrl() calls
> nvme_auth_init_ctrl() before assigning ctrl->device = &ctrl->ctrl_device,
> so the dev_warn() and dev_dbg() calls here pass a NULL ctrl->device.
> 
Restructured the function to only update the ->host_key and ->ctrl_key
values once the keys are validated.

>> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
>> index 5ed91557f0706..7ed8618257ca9 100644
>> --- a/drivers/nvme/host/fabrics.c
>> +++ b/drivers/nvme/host/fabrics.c
> [ ... ]
>> @@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>>   				ret = -ENOMEM;
>>   				goto out;
>>   			}
>> -			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
>> -				pr_err("Invalid DH-CHAP secret %s\n", p);
>> -				kfree_sensitive(p);
>> -				ret = -EINVAL;
>> -				goto out;
>> -			}
>> -			kfree_sensitive(opts->dhchap_secret);
>> -			opts->dhchap_secret = p;
>> +			kfree_sensitive(host_secret);
>> +			host_secret = p;
>>   			break;
>>   		case NVMF_OPT_DHCHAP_CTRL_SECRET:
>>   			p = match_strdup(args);
>> @@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>>   				ret = -ENOMEM;
>>   				goto out;
>>   			}
>> -			if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
>> -				pr_err("Invalid DH-CHAP secret %s\n", p);
>> -				kfree_sensitive(p);
>> -				ret = -EINVAL;
>> -				goto out;
>> -			}
>> -			kfree_sensitive(opts->dhchap_ctrl_secret);
>> -			opts->dhchap_ctrl_secret = p;
>> +			kfree_sensitive(ctrl_secret);
>> +			ctrl_secret = p;
>>   			break;
> 
> [Severity: High]
> Does removing the minimum length checks here and in dhchap_secret_store() /
> dhchap_ctrl_secret_store() allow short secret strings to underflow datalen
> and ret in nvme_dhchap_psk_preparse()?
> 
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
>      ...
> 	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> 		   &version, &hmac) != 2) {
>      ...
> 	}
> 
> 	/* skip header and final ':' character */
> 	datalen -= 11;
> }
> 
> Because %*s is assignment-suppressed and %02hhu matches one or two digits,
> sscanf() returns 2 for 8- to 10-byte strings such as "DHHC-1:0:a", causing
> unsigned datalen -= 11 to wrap around and wrap the kzalloc() size
> calculation sizeof(*upayload) + datalen.  For 11- to 15-byte inputs such as
> "DHHC-1:00:a" where base64_decode() returns 0..3, ret -= 4 in
> nvme_dhchap_psk_preparse() becomes negative and is passed as an unsigned
> length to crc32(~0, upayload->data, ret), reading out-of-bounds across
> kernel memory.
> 
The parsing routines have been fixed with reworks of the previous patches.

> [ ... ]
> 
>> @@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>>   			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
>>   				opts->fast_io_fail_tmo, ctrl_loss_tmo);
>>   	}
>> +
>> +	opts->host = nvmf_host_add(hostnqn, &hostid);
>> +	if (IS_ERR(opts->host)) {
>> +		ret = PTR_ERR(opts->host);
>> +		opts->host = NULL;
>> +		goto out;
>> +	}
>> +
>> +	if (host_secret) {
>> +		pr_debug("lookup host identity '%s'\n", host_secret);
>> +		key = nvme_auth_extract_key(opts->keyring, host_secret,
>> +					    strlen(host_secret));
>> +		if (IS_ERR(key)) {
>> +			ret = PTR_ERR(key);
>> +			goto out;
>> +		}
>> +		pr_debug("using dhchap host key %08x\n", key_serial(key));
>> +		opts->dhchap_key = key;
>> +	}
>> +	if (ctrl_secret) {
>> +		if (!opts->dhchap_key) {
>> +			ret = -EINVAL;
>> +			goto out;
>> +		}
>> +		pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
>> +		key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
>> +					    strlen(ctrl_secret));
> 
> [Severity: High]
> Does calling nvme_auth_extract_key() unconditionally here break the build
> when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH (or when
> CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m)?
> 
> include/linux/nvme-auth.h declares nvme_auth_extract_key() without a
> !CONFIG_NVME_AUTH stub, and match_token() is an external call so the
> compiler cannot dead-code-eliminate the host_secret and ctrl_secret
> branches when CONFIG_NVME_HOST_AUTH is disabled.
> 
> In addition, drivers/nvme/host/sysfs.c now includes <linux/nvme-keyring.h>
> unconditionally, which fails to compile when CONFIG_NVME_KEYRING is
> disabled due to a stray semicolon and a non-inline function definition in
> the fallback block:
> 
> include/linux/nvme-keyring.h:
> static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
> 		const char *hostnqn, const char *subnqn, u8 hmac);
> {
> 	return ERR_PTR(-EOPNOTSUPP);
> }
> u8 nvme_dhchap_psk_hash(struct key *key)
> {
> 	return 0;
> }
> 
Fixed up with the next version.

> [ ... ]
> 
>> @@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>>   	kfree(opts->subsysnqn);
>>   	kfree(opts->host_traddr);
>>   	kfree(opts->host_iface);
>> -	kfree_sensitive(opts->dhchap_secret);
>> -	kfree_sensitive(opts->dhchap_ctrl_secret);
>> +	if (opts->dhchap_key) {
>> +		pr_debug("revoke dhchap host key %08x\n",
>> +			 key_serial(opts->dhchap_key));
>> +		key_revoke(opts->dhchap_key);
>> +		key_put(opts->dhchap_key);
>> +	}
>> +	if (opts->dhchap_ctrl_key) {
>> +		pr_debug("revoke dhchap ctrl key %08x\n",
>> +			 key_serial(opts->dhchap_ctrl_key));
>> +		key_revoke(opts->dhchap_key);
>> +		key_put(opts->dhchap_ctrl_key);
>> +	}
> 
> [Severity: High]
> In the opts->dhchap_ctrl_key block, should key_revoke() be passed
> opts->dhchap_ctrl_key instead of opts->dhchap_key?  Passing
> opts->dhchap_key dereferences the host key after key_put(opts->dhchap_key)
> has already dropped its reference, and leaves opts->dhchap_ctrl_key
> unrevoked.
> 
Of course. Fixed up with the next version.

>> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
>> index a5c1511e49ac2..1311f5e29b004 100644
>> --- a/drivers/nvme/host/sysfs.c
>> +++ b/drivers/nvme/host/sysfs.c
> [ ... ]
>> @@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
>>   		struct device_attribute *attr, char *buf)
>>   {
>>   	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> -	struct nvmf_ctrl_options *opts = ctrl->opts;
>> +	struct key *key = ctrl->host_key;
>> +	ssize_t count;
>>
>> -	if (!opts->dhchap_secret)
>> +	if (!key)
>>   		return sysfs_emit(buf, "none\n");
>> -	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
>> +	down_read(&key->sem);
>> +	if (key_validate(key))
>> +		count = sysfs_emit(buf, "<invalidated>\n");
>> +	else {
>> +		count = key->type->read(key, buf, PAGE_SIZE);
>> +		if (count > 0) {
>> +			buf[count] = '\n';
>> +			count++;
>> +		}
>> +	}
>> +	up_read(&key->sem);
>> +	return count;
>>   }
> [ ... ]
>> @@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
> [ ... ]
>> +	mutex_lock(&ctrl->dhchap_auth_mutex);
>> +	old_key = ctrl->host_key;
>> +	dev_dbg(ctrl->dev, "revoke host key %08x\n",
>> +		key_serial(old_key));
>> +	key_revoke(old_key);
>> +
>> +	ctrl->host_key = key;
>> +	mutex_unlock(&ctrl->dhchap_auth_mutex);
>> +	key_put(old_key);
> 
> [Severity: Medium]
> Can a concurrent read of dhchap_secret (or dhchap_ctrl_secret, dhchap_key,
> or dhchap_ctrl_key) race with dhchap_secret_store() or
> dhchap_ctrl_secret_store() and trigger a use-after-free on the old key?
> 
> CPU0                              CPU1
> dhchap_secret_show()
>    key = ctrl->host_key;
>                                    dhchap_secret_store()
>                                      mutex_lock(&ctrl->dhchap_auth_mutex);
>                                      old_key = ctrl->host_key;
>                                      key_revoke(old_key);
>                                      ctrl->host_key = key;
>                                      mutex_unlock(&ctrl->dhchap_auth_mutex);
>                                      key_put(old_key);  <-- frees old_key
>    down_read(&key->sem);  <-- use-after-free
> 
> None of dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(),
> or dhchap_ctrl_key_show() hold ctrl->dhchap_auth_mutex or take a key_get()
> reference before dereferencing ctrl->host_key or ctrl->ctrl_key.  Once a
> key has been replaced via sysfs store (so opts->dhchap_key no longer holds
> a reference), ctrl->host_key holds the only reference, and key_put(old_key)
> frees it while a concurrent show callback is using it.
> 
Updated the _show functions to take the dhchap_auth_mutex.

>> @@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
>>   		struct device_attribute *attr, char *buf)
>>   {
>>   	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> -	struct nvmf_ctrl_options *opts = ctrl->opts;
>> +	struct key *key = ctrl->ctrl_key;
>> +	size_t count;
>>
>> -	if (!opts->dhchap_ctrl_secret)
>> +	if (!key)
>>   		return sysfs_emit(buf, "none\n");
>> -	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
>> +	down_read(&key->sem);
>> +	if (key_validate(key))
>> +		count = sysfs_emit(buf, "<invalidated>");
>> +	else {
>> +		count = key->type->read(key, buf, PAGE_SIZE);
>> +		if (count > 0) {
>> +			buf[count] = '\n';
>> +			count++;
>> +		}
>> +	}
>> +	up_read(&key->sem);
>> +	return count;
>>   }
> 
> [Severity: Medium]
> Can a negative error return from key->type->read() cause an out-of-bounds
> write before buf in dhchap_ctrl_secret_show() and dhchap_key_show()?
> 
> Unlike dhchap_secret_show(), which declares ssize_t count, both
> dhchap_ctrl_secret_show() and dhchap_key_show() declare count as unsigned
> size_t.  If key->type->read() returns a negative error code (such as
> -EINVAL), count wraps to a large positive value, passes count > 0, and
> writes '\n' to buf[count] out-of-bounds.  Commit "nvme: allow to pass in
> key description as dhchap secret" later in this series replaces
> key->type->read() in dhchap_key_show(), but dhchap_ctrl_secret_show()
> retains the unsigned size_t count and also omits the trailing newline on
> "<invalidated>".
> 
Used 'ssize_t' here, too.

> [ ... ]
> 
>> @@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
>>   }
>>
>>   static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
>> +
>> +static ssize_t dhchap_key_show(struct device *dev,
>> +		struct device_attribute *attr, char *buf)
>> +{
>> +	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> +	struct key *key = ctrl->host_key;
>> +	size_t count;
>> +
>> +	if (!key)
>> +		return 0;
>> +	down_read(&key->sem);
>> +	if (key_validate(key))
>> +		count = sysfs_emit(buf, "<invalidated>\n");
>> +	else {
>> +		count = key->type->read(key, buf, PAGE_SIZE);
>> +		if (count > 0) {
>> +			buf[count] = '\n';
>> +			count++;
>> +		}
>> +	}
>> +	up_read(&key->sem);
>> +	return count;
>> +}
>> +static DEVICE_ATTR_ADMIN_RO(dhchap_key);
>> +
>> +static ssize_t dhchap_ctrl_key_show(struct device *dev,
>> +		struct device_attribute *attr, char *buf)
>> +{
>> +	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> +
>> +	if (!ctrl->ctrl_key)
>> +		return 0;
>> +	return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
>> +}
>> +static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
>>   #endif
>>
>>   static struct attribute *nvme_dev_attrs[] = {
>> @@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
>>   #ifdef CONFIG_NVME_HOST_AUTH
>>   	&dev_attr_dhchap_secret.attr,
>>   	&dev_attr_dhchap_ctrl_secret.attr,
>> +	&dev_attr_dhchap_key.attr,
>> +	&dev_attr_dhchap_ctrl_key.attr,
>>   #endif
> 
> [Severity: Low]
> Should nvme_dev_attrs_are_visible() hide dev_attr_dhchap_key.attr and
> dev_attr_dhchap_ctrl_key.attr when !ctrl->opts, the same way it hides
> dev_attr_dhchap_secret.attr and dev_attr_dhchap_ctrl_secret.attr?  Without
> that check, both attributes appear on non-Fabrics controllers (such as PCIe
> controllers) when CONFIG_NVME_HOST_AUTH is enabled.
> 
Yes, it should. Updated for the next version.

> Also, in this patch dhchap_key_show() outputs the key payload via
> key->type->read() instead of key_serial(key) as dhchap_ctrl_key_show() does
> (though commit "nvme: allow to pass in key description as dhchap secret"
> later in the series updates dhchap_key_show() to emit key_serial()).
> 
Fixed up with the next version.

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare@suse.de                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
@ 2026-11-20 23:36 kernel test robot
  0 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2026-11-20 23:36 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp

:::::: 
:::::: Manual check reason: "same reports for this branch"
:::::: 

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20261120-dhchap-keyring-v4-v4-4-5f8c7d9926ee@kernel.org>
References: <20261120-dhchap-keyring-v4-v4-4-5f8c7d9926ee@kernel.org>
TO: Hannes Reinecke <hare@kernel.org>
TO: Keith Busch <kbusch@kernel.org>
TO: Jens Axboe <axboe@kernel.dk>
TO: Christoph Hellwig <hch@lst.de>
TO: Sagi Grimberg <sagi@grimberg.me>
TO: Hannes Reinecke <hare@suse.de>
TO: Chaitanya Kulkarni <kch@nvidia.com>
CC: linux-nvme@lists.infradead.org
CC: linux-kernel@vger.kernel.org

Hi Hannes,

kernel test robot noticed the following build errors:

[auto build test ERROR on 68099cf27253a8c89d703a282d3d811446d9453b]

url:    https://github.com/intel-lab-lkp/linux/commits/Hannes-Reinecke/nvme-auth-modify-nvme_auth_transform_key-to-return-status/20261120-121400
base:   68099cf27253a8c89d703a282d3d811446d9453b
patch link:    https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-4-5f8c7d9926ee%40kernel.org
patch subject: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
:::::: branch date: 10 hours ago
:::::: commit date: 10 hours ago
config: i386-buildonly-randconfig-1001-20261121 (https://download.01.org/0day-ci/archive/20261121/202611210754.yAo7gNGw-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210754.yAo7gNGw-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/r/202611210754.yAo7gNGw-lkp@intel.com/

All errors (new ones prefixed by >>):

   ld: drivers/nvme/host/fabrics.o: in function `nvmf_parse_options':
>> drivers/nvme/host/fabrics.c:1091:(.text+0x1389): undefined reference to `nvme_auth_extract_key'
>> ld: drivers/nvme/host/fabrics.c:1106:(.text+0x13cd): undefined reference to `nvme_auth_extract_key'


vim +1091 drivers/nvme/host/fabrics.c

07bfcd09a28855 Christoph Hellwig    2016-06-13   715  
07bfcd09a28855 Christoph Hellwig    2016-06-13   716  static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
07bfcd09a28855 Christoph Hellwig    2016-06-13   717  		const char *buf)
07bfcd09a28855 Christoph Hellwig    2016-06-13   718  {
07bfcd09a28855 Christoph Hellwig    2016-06-13   719  	substring_t args[MAX_OPT_ARGS];
07bfcd09a28855 Christoph Hellwig    2016-06-13   720  	char *options, *o, *p;
7347ce3e720e2e Hannes Reinecke      2026-11-20   721  	char *host_secret = NULL, *ctrl_secret = NULL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   722  	int token, ret = 0;
07bfcd09a28855 Christoph Hellwig    2016-06-13   723  	size_t nqnlen  = 0;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   724  	int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   725  	uuid_t hostid;
ae8bd606e09bbd Max Gurtovoy         2023-05-12   726  	char hostnqn[NVMF_NQN_SIZE];
adf22c520b5ec4 Hannes Reinecke      2023-08-24   727  	struct key *key;
07bfcd09a28855 Christoph Hellwig    2016-06-13   728  
07bfcd09a28855 Christoph Hellwig    2016-06-13   729  	/* Set defaults */
07bfcd09a28855 Christoph Hellwig    2016-06-13   730  	opts->queue_size = NVMF_DEF_QUEUE_SIZE;
07bfcd09a28855 Christoph Hellwig    2016-06-13   731  	opts->nr_io_queues = num_online_cpus();
07bfcd09a28855 Christoph Hellwig    2016-06-13   732  	opts->reconnect_delay = NVMF_DEF_RECONNECT_DELAY;
32feb6de47242e Martin George        2021-02-11   733  	opts->kato = 0;
3b3387620780fc James Smart          2017-10-20   734  	opts->duplicate_connect = false;
8c4dfea97f15b8 Victor Gladkov       2020-11-24   735  	opts->fast_io_fail_tmo = NVMF_DEF_FAIL_FAST_TMO;
3b49fa807284ec Sagi Grimberg        2018-12-03   736  	opts->hdr_digest = false;
20d44e86321299 Sagi Grimberg        2018-12-03   737  	opts->data_digest = false;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   738  	opts->tos = -1; /* < 0 == use transport default */
be8e82caa68599 Hannes Reinecke      2023-08-24   739  	opts->tls = false;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   740  	opts->tls_key = NULL;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   741  	opts->keyring = NULL;
e88a7595b57f2a Hannes Reinecke      2025-02-24   742  	opts->concat = false;
7347ce3e720e2e Hannes Reinecke      2026-11-20   743  	opts->dhchap_key = NULL;
7347ce3e720e2e Hannes Reinecke      2026-11-20   744  	opts->dhchap_ctrl_key = NULL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   745  
07bfcd09a28855 Christoph Hellwig    2016-06-13   746  	options = o = kstrdup(buf, GFP_KERNEL);
07bfcd09a28855 Christoph Hellwig    2016-06-13   747  	if (!options)
07bfcd09a28855 Christoph Hellwig    2016-06-13   748  		return -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   749  
ae8bd606e09bbd Max Gurtovoy         2023-05-12   750  	/* use default host if not given by user space */
ae8bd606e09bbd Max Gurtovoy         2023-05-12   751  	uuid_copy(&hostid, &nvmf_default_host->id);
ae8bd606e09bbd Max Gurtovoy         2023-05-12   752  	strscpy(hostnqn, nvmf_default_host->nqn, NVMF_NQN_SIZE);
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   753  
07bfcd09a28855 Christoph Hellwig    2016-06-13   754  	while ((p = strsep(&o, ",\n")) != NULL) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   755  		if (!*p)
07bfcd09a28855 Christoph Hellwig    2016-06-13   756  			continue;
07bfcd09a28855 Christoph Hellwig    2016-06-13   757  
07bfcd09a28855 Christoph Hellwig    2016-06-13   758  		token = match_token(p, opt_tokens, args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   759  		opts->mask |= token;
07bfcd09a28855 Christoph Hellwig    2016-06-13   760  		switch (token) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   761  		case NVMF_OPT_TRANSPORT:
07bfcd09a28855 Christoph Hellwig    2016-06-13   762  			p = match_strdup(args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   763  			if (!p) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   764  				ret = -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   765  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   766  			}
59a2f3f00fd744 Chengguang Xu        2018-04-14   767  			kfree(opts->transport);
07bfcd09a28855 Christoph Hellwig    2016-06-13   768  			opts->transport = p;
07bfcd09a28855 Christoph Hellwig    2016-06-13   769  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   770  		case NVMF_OPT_NQN:
07bfcd09a28855 Christoph Hellwig    2016-06-13   771  			p = match_strdup(args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   772  			if (!p) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   773  				ret = -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   774  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   775  			}
59a2f3f00fd744 Chengguang Xu        2018-04-14   776  			kfree(opts->subsysnqn);
07bfcd09a28855 Christoph Hellwig    2016-06-13   777  			opts->subsysnqn = p;
07bfcd09a28855 Christoph Hellwig    2016-06-13   778  			nqnlen = strlen(opts->subsysnqn);
07bfcd09a28855 Christoph Hellwig    2016-06-13   779  			if (nqnlen >= NVMF_NQN_SIZE) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   780  				pr_err("%s needs to be < %d bytes\n",
07bfcd09a28855 Christoph Hellwig    2016-06-13   781  					opts->subsysnqn, NVMF_NQN_SIZE);
07bfcd09a28855 Christoph Hellwig    2016-06-13   782  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   783  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   784  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   785  			opts->discovery_nqn =
07bfcd09a28855 Christoph Hellwig    2016-06-13   786  				!(strcmp(opts->subsysnqn,
07bfcd09a28855 Christoph Hellwig    2016-06-13   787  					 NVME_DISC_SUBSYS_NAME));
07bfcd09a28855 Christoph Hellwig    2016-06-13   788  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   789  		case NVMF_OPT_TRADDR:
07bfcd09a28855 Christoph Hellwig    2016-06-13   790  			p = match_strdup(args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   791  			if (!p) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   792  				ret = -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   793  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   794  			}
59a2f3f00fd744 Chengguang Xu        2018-04-14   795  			kfree(opts->traddr);
07bfcd09a28855 Christoph Hellwig    2016-06-13   796  			opts->traddr = p;
07bfcd09a28855 Christoph Hellwig    2016-06-13   797  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   798  		case NVMF_OPT_TRSVCID:
07bfcd09a28855 Christoph Hellwig    2016-06-13   799  			p = match_strdup(args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   800  			if (!p) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   801  				ret = -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   802  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   803  			}
59a2f3f00fd744 Chengguang Xu        2018-04-14   804  			kfree(opts->trsvcid);
07bfcd09a28855 Christoph Hellwig    2016-06-13   805  			opts->trsvcid = p;
07bfcd09a28855 Christoph Hellwig    2016-06-13   806  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   807  		case NVMF_OPT_QUEUE_SIZE:
07bfcd09a28855 Christoph Hellwig    2016-06-13   808  			if (match_int(args, &token)) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   809  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   810  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   811  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   812  			if (token < NVMF_MIN_QUEUE_SIZE ||
07bfcd09a28855 Christoph Hellwig    2016-06-13   813  			    token > NVMF_MAX_QUEUE_SIZE) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   814  				pr_err("Invalid queue_size %d\n", token);
07bfcd09a28855 Christoph Hellwig    2016-06-13   815  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   816  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   817  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   818  			opts->queue_size = token;
07bfcd09a28855 Christoph Hellwig    2016-06-13   819  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   820  		case NVMF_OPT_NR_IO_QUEUES:
07bfcd09a28855 Christoph Hellwig    2016-06-13   821  			if (match_int(args, &token)) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   822  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   823  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   824  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   825  			if (token <= 0) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   826  				pr_err("Invalid number of IOQs %d\n", token);
07bfcd09a28855 Christoph Hellwig    2016-06-13   827  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   828  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   829  			}
0475821e229cfd Roland Dreier        2018-03-05   830  			if (opts->discovery_nqn) {
0475821e229cfd Roland Dreier        2018-03-05   831  				pr_debug("Ignoring nr_io_queues value for discovery controller\n");
0475821e229cfd Roland Dreier        2018-03-05   832  				break;
0475821e229cfd Roland Dreier        2018-03-05   833  			}
0475821e229cfd Roland Dreier        2018-03-05   834  
07bfcd09a28855 Christoph Hellwig    2016-06-13   835  			opts->nr_io_queues = min_t(unsigned int,
07bfcd09a28855 Christoph Hellwig    2016-06-13   836  					num_online_cpus(), token);
07bfcd09a28855 Christoph Hellwig    2016-06-13   837  			break;
038bd4cb6766c6 Sagi Grimberg        2016-06-13   838  		case NVMF_OPT_KATO:
038bd4cb6766c6 Sagi Grimberg        2016-06-13   839  			if (match_int(args, &token)) {
038bd4cb6766c6 Sagi Grimberg        2016-06-13   840  				ret = -EINVAL;
038bd4cb6766c6 Sagi Grimberg        2016-06-13   841  				goto out;
038bd4cb6766c6 Sagi Grimberg        2016-06-13   842  			}
038bd4cb6766c6 Sagi Grimberg        2016-06-13   843  
038bd4cb6766c6 Sagi Grimberg        2016-06-13   844  			if (token < 0) {
038bd4cb6766c6 Sagi Grimberg        2016-06-13   845  				pr_err("Invalid keep_alive_tmo %d\n", token);
038bd4cb6766c6 Sagi Grimberg        2016-06-13   846  				ret = -EINVAL;
038bd4cb6766c6 Sagi Grimberg        2016-06-13   847  				goto out;
8edd11c9ad3a62 Guilherme G. Piccoli 2017-09-14   848  			} else if (token == 0 && !opts->discovery_nqn) {
038bd4cb6766c6 Sagi Grimberg        2016-06-13   849  				/* Allowed for debug */
038bd4cb6766c6 Sagi Grimberg        2016-06-13   850  				pr_warn("keep_alive_tmo 0 won't execute keep alives!!!\n");
038bd4cb6766c6 Sagi Grimberg        2016-06-13   851  			}
038bd4cb6766c6 Sagi Grimberg        2016-06-13   852  			opts->kato = token;
038bd4cb6766c6 Sagi Grimberg        2016-06-13   853  			break;
42a45274c22582 Sagi Grimberg        2017-03-18   854  		case NVMF_OPT_CTRL_LOSS_TMO:
42a45274c22582 Sagi Grimberg        2017-03-18   855  			if (match_int(args, &token)) {
42a45274c22582 Sagi Grimberg        2017-03-18   856  				ret = -EINVAL;
42a45274c22582 Sagi Grimberg        2017-03-18   857  				goto out;
42a45274c22582 Sagi Grimberg        2017-03-18   858  			}
42a45274c22582 Sagi Grimberg        2017-03-18   859  
42a45274c22582 Sagi Grimberg        2017-03-18   860  			if (token < 0)
42a45274c22582 Sagi Grimberg        2017-03-18   861  				pr_warn("ctrl_loss_tmo < 0 will reconnect forever\n");
42a45274c22582 Sagi Grimberg        2017-03-18   862  			ctrl_loss_tmo = token;
42a45274c22582 Sagi Grimberg        2017-03-18   863  			break;
8c4dfea97f15b8 Victor Gladkov       2020-11-24   864  		case NVMF_OPT_FAIL_FAST_TMO:
8c4dfea97f15b8 Victor Gladkov       2020-11-24   865  			if (match_int(args, &token)) {
8c4dfea97f15b8 Victor Gladkov       2020-11-24   866  				ret = -EINVAL;
8c4dfea97f15b8 Victor Gladkov       2020-11-24   867  				goto out;
8c4dfea97f15b8 Victor Gladkov       2020-11-24   868  			}
8c4dfea97f15b8 Victor Gladkov       2020-11-24   869  
8c4dfea97f15b8 Victor Gladkov       2020-11-24   870  			if (token >= 0)
8c4dfea97f15b8 Victor Gladkov       2020-11-24   871  				pr_warn("I/O fail on reconnect controller after %d sec\n",
8c4dfea97f15b8 Victor Gladkov       2020-11-24   872  					token);
8e8aaf512a91ae Maurizio Lombardi    2021-11-12   873  			else
8e8aaf512a91ae Maurizio Lombardi    2021-11-12   874  				token = -1;
8e8aaf512a91ae Maurizio Lombardi    2021-11-12   875  
8c4dfea97f15b8 Victor Gladkov       2020-11-24   876  			opts->fast_io_fail_tmo = token;
8c4dfea97f15b8 Victor Gladkov       2020-11-24   877  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   878  		case NVMF_OPT_HOSTNQN:
07bfcd09a28855 Christoph Hellwig    2016-06-13   879  			p = match_strdup(args);
07bfcd09a28855 Christoph Hellwig    2016-06-13   880  			if (!p) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   881  				ret = -ENOMEM;
07bfcd09a28855 Christoph Hellwig    2016-06-13   882  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   883  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   884  			nqnlen = strlen(p);
07bfcd09a28855 Christoph Hellwig    2016-06-13   885  			if (nqnlen >= NVMF_NQN_SIZE) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   886  				pr_err("%s needs to be < %d bytes\n",
07bfcd09a28855 Christoph Hellwig    2016-06-13   887  					p, NVMF_NQN_SIZE);
8eadfcb1b63d7d Bart Van Assche      2016-10-18   888  				kfree(p);
07bfcd09a28855 Christoph Hellwig    2016-06-13   889  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   890  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   891  			}
ae8bd606e09bbd Max Gurtovoy         2023-05-12   892  			strscpy(hostnqn, p, NVMF_NQN_SIZE);
8eadfcb1b63d7d Bart Van Assche      2016-10-18   893  			kfree(p);
07bfcd09a28855 Christoph Hellwig    2016-06-13   894  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13   895  		case NVMF_OPT_RECONNECT_DELAY:
07bfcd09a28855 Christoph Hellwig    2016-06-13   896  			if (match_int(args, &token)) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   897  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   898  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   899  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   900  			if (token <= 0) {
07bfcd09a28855 Christoph Hellwig    2016-06-13   901  				pr_err("Invalid reconnect_delay %d\n", token);
07bfcd09a28855 Christoph Hellwig    2016-06-13   902  				ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13   903  				goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13   904  			}
07bfcd09a28855 Christoph Hellwig    2016-06-13   905  			opts->reconnect_delay = token;
07bfcd09a28855 Christoph Hellwig    2016-06-13   906  			break;
478bcb9388f2c3 James Smart          2016-08-02   907  		case NVMF_OPT_HOST_TRADDR:
478bcb9388f2c3 James Smart          2016-08-02   908  			p = match_strdup(args);
478bcb9388f2c3 James Smart          2016-08-02   909  			if (!p) {
478bcb9388f2c3 James Smart          2016-08-02   910  				ret = -ENOMEM;
478bcb9388f2c3 James Smart          2016-08-02   911  				goto out;
478bcb9388f2c3 James Smart          2016-08-02   912  			}
59a2f3f00fd744 Chengguang Xu        2018-04-14   913  			kfree(opts->host_traddr);
478bcb9388f2c3 James Smart          2016-08-02   914  			opts->host_traddr = p;
478bcb9388f2c3 James Smart          2016-08-02   915  			break;
3ede8f72a9a282 Martin Belanger      2021-05-20   916  		case NVMF_OPT_HOST_IFACE:
3ede8f72a9a282 Martin Belanger      2021-05-20   917  			p = match_strdup(args);
3ede8f72a9a282 Martin Belanger      2021-05-20   918  			if (!p) {
3ede8f72a9a282 Martin Belanger      2021-05-20   919  				ret = -ENOMEM;
3ede8f72a9a282 Martin Belanger      2021-05-20   920  				goto out;
3ede8f72a9a282 Martin Belanger      2021-05-20   921  			}
3ede8f72a9a282 Martin Belanger      2021-05-20   922  			kfree(opts->host_iface);
3ede8f72a9a282 Martin Belanger      2021-05-20   923  			opts->host_iface = p;
3ede8f72a9a282 Martin Belanger      2021-05-20   924  			break;
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   925  		case NVMF_OPT_HOST_ID:
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   926  			p = match_strdup(args);
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   927  			if (!p) {
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   928  				ret = -ENOMEM;
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   929  				goto out;
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   930  			}
df351ef7378934 Roland Dreier        2018-01-11   931  			ret = uuid_parse(p, &hostid);
df351ef7378934 Roland Dreier        2018-01-11   932  			if (ret) {
9b483da15dd0c9 Guan Junxiong        2017-08-03   933  				pr_err("Invalid hostid %s\n", p);
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   934  				ret = -EINVAL;
6e49412016f5f2 Johannes Thumshirn   2018-01-25   935  				kfree(p);
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   936  				goto out;
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   937  			}
6e49412016f5f2 Johannes Thumshirn   2018-01-25   938  			kfree(p);
6bfe04255d5ed5 Johannes Thumshirn   2017-06-20   939  			break;
3b3387620780fc James Smart          2017-10-20   940  		case NVMF_OPT_DUP_CONNECT:
3b3387620780fc James Smart          2017-10-20   941  			opts->duplicate_connect = true;
3b3387620780fc James Smart          2017-10-20   942  			break;
8154ed730bc64f Sagi Grimberg        2018-11-19   943  		case NVMF_OPT_DISABLE_SQFLOW:
8154ed730bc64f Sagi Grimberg        2018-11-19   944  			opts->disable_sqflow = true;
8154ed730bc64f Sagi Grimberg        2018-11-19   945  			break;
3b49fa807284ec Sagi Grimberg        2018-12-03   946  		case NVMF_OPT_HDR_DIGEST:
3b49fa807284ec Sagi Grimberg        2018-12-03   947  			opts->hdr_digest = true;
3b49fa807284ec Sagi Grimberg        2018-12-03   948  			break;
20d44e86321299 Sagi Grimberg        2018-12-03   949  		case NVMF_OPT_DATA_DIGEST:
20d44e86321299 Sagi Grimberg        2018-12-03   950  			opts->data_digest = true;
20d44e86321299 Sagi Grimberg        2018-12-03   951  			break;
330f6b8a70771f Sagi Grimberg        2018-12-11   952  		case NVMF_OPT_NR_WRITE_QUEUES:
330f6b8a70771f Sagi Grimberg        2018-12-11   953  			if (match_int(args, &token)) {
330f6b8a70771f Sagi Grimberg        2018-12-11   954  				ret = -EINVAL;
330f6b8a70771f Sagi Grimberg        2018-12-11   955  				goto out;
330f6b8a70771f Sagi Grimberg        2018-12-11   956  			}
330f6b8a70771f Sagi Grimberg        2018-12-11   957  			if (token <= 0) {
330f6b8a70771f Sagi Grimberg        2018-12-11   958  				pr_err("Invalid nr_write_queues %d\n", token);
330f6b8a70771f Sagi Grimberg        2018-12-11   959  				ret = -EINVAL;
330f6b8a70771f Sagi Grimberg        2018-12-11   960  				goto out;
330f6b8a70771f Sagi Grimberg        2018-12-11   961  			}
330f6b8a70771f Sagi Grimberg        2018-12-11   962  			opts->nr_write_queues = token;
330f6b8a70771f Sagi Grimberg        2018-12-11   963  			break;
89d43802b0e729 Sagi Grimberg        2018-12-14   964  		case NVMF_OPT_NR_POLL_QUEUES:
89d43802b0e729 Sagi Grimberg        2018-12-14   965  			if (match_int(args, &token)) {
89d43802b0e729 Sagi Grimberg        2018-12-14   966  				ret = -EINVAL;
89d43802b0e729 Sagi Grimberg        2018-12-14   967  				goto out;
89d43802b0e729 Sagi Grimberg        2018-12-14   968  			}
89d43802b0e729 Sagi Grimberg        2018-12-14   969  			if (token <= 0) {
89d43802b0e729 Sagi Grimberg        2018-12-14   970  				pr_err("Invalid nr_poll_queues %d\n", token);
89d43802b0e729 Sagi Grimberg        2018-12-14   971  				ret = -EINVAL;
89d43802b0e729 Sagi Grimberg        2018-12-14   972  				goto out;
89d43802b0e729 Sagi Grimberg        2018-12-14   973  			}
89d43802b0e729 Sagi Grimberg        2018-12-14   974  			opts->nr_poll_queues = token;
89d43802b0e729 Sagi Grimberg        2018-12-14   975  			break;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   976  		case NVMF_OPT_TOS:
52b4451a9e5ae1 Israel Rukshin       2019-08-18   977  			if (match_int(args, &token)) {
52b4451a9e5ae1 Israel Rukshin       2019-08-18   978  				ret = -EINVAL;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   979  				goto out;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   980  			}
52b4451a9e5ae1 Israel Rukshin       2019-08-18   981  			if (token < 0) {
52b4451a9e5ae1 Israel Rukshin       2019-08-18   982  				pr_err("Invalid type of service %d\n", token);
52b4451a9e5ae1 Israel Rukshin       2019-08-18   983  				ret = -EINVAL;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   984  				goto out;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   985  			}
52b4451a9e5ae1 Israel Rukshin       2019-08-18   986  			if (token > 255) {
52b4451a9e5ae1 Israel Rukshin       2019-08-18   987  				pr_warn("Clamping type of service to 255\n");
52b4451a9e5ae1 Israel Rukshin       2019-08-18   988  				token = 255;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   989  			}
52b4451a9e5ae1 Israel Rukshin       2019-08-18   990  			opts->tos = token;
52b4451a9e5ae1 Israel Rukshin       2019-08-18   991  			break;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   992  		case NVMF_OPT_KEYRING:
adf22c520b5ec4 Hannes Reinecke      2023-08-24   993  			if (match_int(args, &key_id) || key_id <= 0) {
adf22c520b5ec4 Hannes Reinecke      2023-08-24   994  				ret = -EINVAL;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   995  				goto out;
adf22c520b5ec4 Hannes Reinecke      2023-08-24   996  			}
adf22c520b5ec4 Hannes Reinecke      2023-08-24   997  			key = nvmf_parse_key(key_id);
adf22c520b5ec4 Hannes Reinecke      2023-08-24   998  			if (IS_ERR(key)) {
adf22c520b5ec4 Hannes Reinecke      2023-08-24   999  				ret = PTR_ERR(key);
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1000  				goto out;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1001  			}
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1002  			key_put(opts->keyring);
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1003  			opts->keyring = key;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1004  			break;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1005  		case NVMF_OPT_TLS_KEY:
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1006  			if (match_int(args, &key_id) || key_id <= 0) {
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1007  				ret = -EINVAL;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1008  				goto out;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1009  			}
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1010  			key = nvmf_parse_key(key_id);
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1011  			if (IS_ERR(key)) {
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1012  				ret = PTR_ERR(key);
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1013  				goto out;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1014  			}
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1015  			key_put(opts->tls_key);
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1016  			opts->tls_key = key;
adf22c520b5ec4 Hannes Reinecke      2023-08-24  1017  			break;
20e8b689c90880 Hannes Reinecke      2021-09-22  1018  		case NVMF_OPT_DISCOVERY:
20e8b689c90880 Hannes Reinecke      2021-09-22  1019  			opts->discovery_nqn = true;
20e8b689c90880 Hannes Reinecke      2021-09-22  1020  			break;
f50fff73d620cd Hannes Reinecke      2022-06-27  1021  		case NVMF_OPT_DHCHAP_SECRET:
f50fff73d620cd Hannes Reinecke      2022-06-27  1022  			p = match_strdup(args);
f50fff73d620cd Hannes Reinecke      2022-06-27  1023  			if (!p) {
f50fff73d620cd Hannes Reinecke      2022-06-27  1024  				ret = -ENOMEM;
f50fff73d620cd Hannes Reinecke      2022-06-27  1025  				goto out;
f50fff73d620cd Hannes Reinecke      2022-06-27  1026  			}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1027  			kfree_sensitive(host_secret);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1028  			host_secret = p;
f50fff73d620cd Hannes Reinecke      2022-06-27  1029  			break;
f50fff73d620cd Hannes Reinecke      2022-06-27  1030  		case NVMF_OPT_DHCHAP_CTRL_SECRET:
f50fff73d620cd Hannes Reinecke      2022-06-27  1031  			p = match_strdup(args);
f50fff73d620cd Hannes Reinecke      2022-06-27  1032  			if (!p) {
f50fff73d620cd Hannes Reinecke      2022-06-27  1033  				ret = -ENOMEM;
f50fff73d620cd Hannes Reinecke      2022-06-27  1034  				goto out;
f50fff73d620cd Hannes Reinecke      2022-06-27  1035  			}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1036  			kfree_sensitive(ctrl_secret);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1037  			ctrl_secret = p;
f50fff73d620cd Hannes Reinecke      2022-06-27  1038  			break;
be8e82caa68599 Hannes Reinecke      2023-08-24  1039  		case NVMF_OPT_TLS:
be8e82caa68599 Hannes Reinecke      2023-08-24  1040  			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
be8e82caa68599 Hannes Reinecke      2023-08-24  1041  				pr_err("TLS is not supported\n");
be8e82caa68599 Hannes Reinecke      2023-08-24  1042  				ret = -EINVAL;
be8e82caa68599 Hannes Reinecke      2023-08-24  1043  				goto out;
be8e82caa68599 Hannes Reinecke      2023-08-24  1044  			}
be8e82caa68599 Hannes Reinecke      2023-08-24  1045  			opts->tls = true;
be8e82caa68599 Hannes Reinecke      2023-08-24  1046  			break;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1047  		case NVMF_OPT_CONCAT:
e88a7595b57f2a Hannes Reinecke      2025-02-24  1048  			if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
e88a7595b57f2a Hannes Reinecke      2025-02-24  1049  				pr_err("TLS is not supported\n");
e88a7595b57f2a Hannes Reinecke      2025-02-24  1050  				ret = -EINVAL;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1051  				goto out;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1052  			}
e88a7595b57f2a Hannes Reinecke      2025-02-24  1053  			opts->concat = true;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1054  			break;
07bfcd09a28855 Christoph Hellwig    2016-06-13  1055  		default:
07bfcd09a28855 Christoph Hellwig    2016-06-13  1056  			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
07bfcd09a28855 Christoph Hellwig    2016-06-13  1057  				p);
07bfcd09a28855 Christoph Hellwig    2016-06-13  1058  			ret = -EINVAL;
07bfcd09a28855 Christoph Hellwig    2016-06-13  1059  			goto out;
07bfcd09a28855 Christoph Hellwig    2016-06-13  1060  		}
07bfcd09a28855 Christoph Hellwig    2016-06-13  1061  	}
07bfcd09a28855 Christoph Hellwig    2016-06-13  1062  
461fbc8f0ed1ac Hannes Reinecke      2018-05-24  1063  	if (opts->discovery_nqn) {
461fbc8f0ed1ac Hannes Reinecke      2018-05-24  1064  		opts->nr_io_queues = 0;
9846ac0143fe98 Sagi Grimberg        2019-01-07  1065  		opts->nr_write_queues = 0;
9846ac0143fe98 Sagi Grimberg        2019-01-07  1066  		opts->nr_poll_queues = 0;
181303d03525ea Hannes Reinecke      2018-05-24  1067  		opts->duplicate_connect = true;
32feb6de47242e Martin George        2021-02-11  1068  	} else {
32feb6de47242e Martin George        2021-02-11  1069  		if (!opts->kato)
32feb6de47242e Martin George        2021-02-11  1070  			opts->kato = NVME_DEFAULT_KATO;
461fbc8f0ed1ac Hannes Reinecke      2018-05-24  1071  	}
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1072  	if (ctrl_loss_tmo < 0) {
42a45274c22582 Sagi Grimberg        2017-03-18  1073  		opts->max_reconnects = -1;
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1074  	} else {
42a45274c22582 Sagi Grimberg        2017-03-18  1075  		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
42a45274c22582 Sagi Grimberg        2017-03-18  1076  						opts->reconnect_delay);
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1077  		if (ctrl_loss_tmo < opts->fast_io_fail_tmo)
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1078  			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1079  				opts->fast_io_fail_tmo, ctrl_loss_tmo);
8c4dfea97f15b8 Victor Gladkov       2020-11-24  1080  	}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1081  
7347ce3e720e2e Hannes Reinecke      2026-11-20  1082  	opts->host = nvmf_host_add(hostnqn, &hostid);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1083  	if (IS_ERR(opts->host)) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1084  		ret = PTR_ERR(opts->host);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1085  		opts->host = NULL;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1086  		goto out;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1087  	}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1088  
7347ce3e720e2e Hannes Reinecke      2026-11-20  1089  	if (host_secret) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1090  		pr_debug("lookup host identity '%s'\n", host_secret);
7347ce3e720e2e Hannes Reinecke      2026-11-20 @1091  		key = nvme_auth_extract_key(opts->keyring, host_secret,
7347ce3e720e2e Hannes Reinecke      2026-11-20  1092  					    strlen(host_secret));
7347ce3e720e2e Hannes Reinecke      2026-11-20  1093  		if (IS_ERR(key)) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1094  			ret = PTR_ERR(key);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1095  			goto out;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1096  		}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1097  		pr_debug("using dhchap host key %08x\n", key_serial(key));
7347ce3e720e2e Hannes Reinecke      2026-11-20  1098  		opts->dhchap_key = key;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1099  	}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1100  	if (ctrl_secret) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1101  		if (!opts->dhchap_key) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1102  			ret = -EINVAL;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1103  			goto out;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1104  		}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1105  		pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
7347ce3e720e2e Hannes Reinecke      2026-11-20 @1106  		key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
7347ce3e720e2e Hannes Reinecke      2026-11-20  1107  					    strlen(ctrl_secret));
7347ce3e720e2e Hannes Reinecke      2026-11-20  1108  		if (IS_ERR(key)) {
7347ce3e720e2e Hannes Reinecke      2026-11-20  1109  			ret = PTR_ERR(key);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1110  			goto out;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1111  		}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1112  		pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
7347ce3e720e2e Hannes Reinecke      2026-11-20  1113  		opts->dhchap_ctrl_key = key;
7347ce3e720e2e Hannes Reinecke      2026-11-20  1114  	}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1115  
e88a7595b57f2a Hannes Reinecke      2025-02-24  1116  	if (opts->concat) {
e88a7595b57f2a Hannes Reinecke      2025-02-24  1117  		if (opts->tls) {
e88a7595b57f2a Hannes Reinecke      2025-02-24  1118  			pr_err("Secure concatenation over TLS is not supported\n");
e88a7595b57f2a Hannes Reinecke      2025-02-24  1119  			ret = -EINVAL;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1120  			goto out;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1121  		}
e88a7595b57f2a Hannes Reinecke      2025-02-24  1122  		if (opts->tls_key) {
e88a7595b57f2a Hannes Reinecke      2025-02-24  1123  			pr_err("Cannot specify a TLS key for secure concatenation\n");
e88a7595b57f2a Hannes Reinecke      2025-02-24  1124  			ret = -EINVAL;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1125  			goto out;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1126  		}
7347ce3e720e2e Hannes Reinecke      2026-11-20  1127  		if (!opts->dhchap_key) {
e88a7595b57f2a Hannes Reinecke      2025-02-24  1128  			pr_err("Need to enable DH-CHAP for secure concatenation\n");
e88a7595b57f2a Hannes Reinecke      2025-02-24  1129  			ret = -EINVAL;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1130  			goto out;
e88a7595b57f2a Hannes Reinecke      2025-02-24  1131  		}
e88a7595b57f2a Hannes Reinecke      2025-02-24  1132  	}
42a45274c22582 Sagi Grimberg        2017-03-18  1133  
07bfcd09a28855 Christoph Hellwig    2016-06-13  1134  out:
7347ce3e720e2e Hannes Reinecke      2026-11-20  1135  	kfree_sensitive(ctrl_secret);
7347ce3e720e2e Hannes Reinecke      2026-11-20  1136  	kfree_sensitive(host_secret);
07bfcd09a28855 Christoph Hellwig    2016-06-13  1137  	kfree(options);
07bfcd09a28855 Christoph Hellwig    2016-06-13  1138  	return ret;
07bfcd09a28855 Christoph Hellwig    2016-06-13  1139  }
07bfcd09a28855 Christoph Hellwig    2016-06-13  1140  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 23:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 23:36 [PATCH v4 4/9] nvme: parse dhchap keys during option parsing kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-20 11:32   ` sashiko-bot
2026-11-20 13:55     ` Hannes Reinecke

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.