[PATCH v7 07/14] nvme: parse dhchap keys during option parsing

From: Hannes Reinecke

Date: Thu Nov 26 2026 - 09:13:32 EST


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 we don't need to pass around the key
itself, but rather lookup and validate the key by serial number before
accessing the key data.
This patch also creates two new sysfs attributes 'dhchap_key' and
'dhchap_ctrl_key' which will contain the key serial of the respective
keys.

Signed-off-by: Hannes Reinecke <hare@xxxxxxxxxx>
---
Documentation/ABI/stable/sysfs-nvme | 10 ++
drivers/nvme/common/keyring.c | 8 +-
drivers/nvme/host/auth.c | 116 +++++++++++++------
drivers/nvme/host/fabrics.c | 77 ++++++++-----
drivers/nvme/host/fabrics.h | 8 +-
drivers/nvme/host/sysfs.c | 223 +++++++++++++++++++++++++++---------
6 files changed, 323 insertions(+), 119 deletions(-)

diff --git a/Documentation/ABI/stable/sysfs-nvme b/Documentation/ABI/stable/sysfs-nvme
index 1826df9161d24db41287811df30c65ee6ed550ce..725499309ab4dfbdb4b0b50813b901f1a1b184dc 100644
--- a/Documentation/ABI/stable/sysfs-nvme
+++ b/Documentation/ABI/stable/sysfs-nvme
@@ -166,7 +166,9 @@ Description:
sqsize: Shows the submission queue size for this controller.

What: /sys/class/nvme/nvmeX/dhchap_secret
+What: /sys/class/nvme/nvmeX/dhchap_key
What: /sys/class/nvme/nvmeX/dhchap_ctrl_secret
+What: /sys/class/nvme/nvmeX/dhchap_ctrl_key
Date: June 2022
KernelVersion: 6.0
Contact: Hannes Reinecke <hare@xxxxxxx>
@@ -176,10 +178,18 @@ Description:
Writing must use the "DHHC-1:" key format and triggers
re-authentication.

+ dhchap_key: Shows the key serial of the key containing
+ the DH-HMAC-CHAP secret for this controller.
+ Reading returns "none" if not set.
+
dhchap_ctrl_secret: Shows or sets the controller
DH-HMAC-CHAP secret for bidirectional authentication.
Same format as dhchap_secret.

+ dhchap_ctrl_key: Shows the key serial of the key containing
+ the DH-HMAC-CHAP controller secret for bidirectional authentication.
+ Reading returns "none" if not set.
+
Only available when CONFIG_NVME_HOST_AUTH is enabled and
for fabrics controllers.

diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index dc0a4fb9296f1d937992028f0520f7a184eec6ae..bee64c592752a7179e1e6575f96749a7ae26a6d6 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -469,7 +469,8 @@ static struct key_type nvme_dhchap_psk_key_type = {
* @data: Key data
* @data_len: Length of @data
*
- * Creates a new DH-HMAC-CHAP key with a random UUID as the key identity.
+ * Creates a new DH-HMAC-CHAP key in the default .nvme keyring
+ * with a random UUID as the key identity.
* Returns the new key or an error pointer on failure.
*/
struct key *nvme_dhchap_psk_create(const char *data, size_t data_len)
@@ -514,7 +515,10 @@ EXPORT_SYMBOL_GPL(nvme_dhchap_psk_create);
* nvme_dhchap_psk_lookup - Lookup DH-HMAC-CHAP key
* @identity: key identity to search
*
- * Looks up a key with identity @identity from keyring @keyring.
+ * Looks up a key with identity @identity from the .nvme.
+ * Other keyrings might be linked into the .nvme keyring
+ * to facilitate lookup from there keyring in addition
+ * to the default .nvme keyring.
* Returns the key or an error pointer if not found.
*/
struct key *nvme_dhchap_psk_lookup(const char *identity)
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index d6abdb4f1e21c77761f4eb00723fc9d853428295..285aa1c71f1026d17408f64500c7b41007c9d493 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -939,11 +939,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);
@@ -1028,38 +1023,100 @@ 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;
+ bool generated;
+
+ 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: drop dhchap%s key %08x\n",
+ __func__, is_ctrl ? " ctrl" : " host",
+ key_serial(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;
+ struct key *host_key, *ctrl_key = NULL;
int i, ret;

mutex_init(&ctrl->dhchap_auth_mutex);
INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
if (!ctrl->opts)
return 0;
- ctrl->host_key = nvme_auth_extract_key(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;
}

- ctrl->ctrl_key = nvme_auth_extract_key(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;
+ host_key = key_lookup(ctrl->opts->dhchap_key);
+ if (!host_key) {
+ pr_warn("dhchap host key %08x not present\n",
+ ctrl->opts->dhchap_key);
+ return -ENOKEY;
+ }
+ down_read(&host_key->sem);
+ ret = key_validate(host_key);
+ up_read(&host_key->sem);
+ if (ret) {
+ pr_warn("dhchap host key %08x invalidated\n",
+ key_serial(host_key));
+ key_put(host_key);
+ return -ENOKEY;
}

- if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
- return 0;
+ if (ctrl->opts->dhchap_ctrl_key) {
+ ctrl_key = key_lookup(ctrl->opts->dhchap_ctrl_key);
+ if (!ctrl_key) {
+ pr_warn("dhchap ctrl key %08x not present\n",
+ ctrl->opts->dhchap_ctrl_key);
+ key_put(host_key);
+ return -ENOKEY;
+ }
+ down_read(&ctrl_key->sem);
+ ret = key_validate(ctrl_key);
+ up_read(&ctrl_key->sem);
+ if (ret) {
+ pr_warn("dhchap ctrl key %08x invalidated\n",
+ key_serial(ctrl_key));
+ key_put(ctrl_key);
+ key_put(host_key);
+ return -EKEYREVOKED;
+ }
+ }
+
+ pr_debug("%s: using dhchap host key %08x\n",
+ __func__, key_serial(host_key));
+ if (ctrl->host_key)
+ nvme_auth_clear_host_key(ctrl);
+ ctrl->host_key = host_key;
+
+ if (ctrl->ctrl_key)
+ nvme_auth_clear_ctrl_key(ctrl);
+ if (ctrl_key) {
+ pr_debug("%s: using dhchap ctrl key %08x\n",
+ __func__, key_serial(ctrl_key));
+ ctrl->ctrl_key = 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++) {
@@ -1071,13 +1128,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);

@@ -1096,14 +1146,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 6a918d72d14c5dd92d00191c184e72b0e5614b7a..6c453b4f5cab1546fb036236bbfa8682a8611adb 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -12,7 +12,9 @@
#include <linux/seq_file.h>
#include "nvme.h"
#include "fabrics.h"
+#include <linux/nvme-auth.h>
#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>

static DECLARE_RWSEM(nvmf_transports_rwsem);
static LIST_HEAD_GUARDED(nvmf_transports, nvmf_transports_rwsem);
@@ -717,6 +719,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 +741,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
opts->tls_key = NULL;
opts->keyring = NULL;
opts->concat = false;
+ opts->dhchap_key = 0;
+ opts->dhchap_ctrl_key = 0;

options = o = kstrdup(buf, GFP_KERNEL);
if (!options)
@@ -1024,14 +1029,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);
@@ -1039,14 +1038,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)) {
@@ -1090,6 +1083,45 @@ 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) {
+#ifdef CONFIG_NVME_HOST_AUTH
+ pr_debug("lookup dhchap host secret '%s'\n", host_secret);
+ key = nvme_auth_extract_key(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_serial(key);
+ key_put(key);
+#endif
+ }
+ if (ctrl_secret) {
+#ifdef CONFIG_NVME_HOST_AUTH
+ if (!opts->dhchap_key) {
+ ret = -EINVAL;
+ goto out;
+ }
+ pr_debug("lookup dhchap ctrl secret '%s'\n", ctrl_secret);
+ key = nvme_auth_extract_key(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_serial(key);
+ key_put(key);
+#endif
+ }
+
if (opts->concat) {
if (opts->tls) {
pr_err("Secure concatenation over TLS is not supported\n");
@@ -1101,21 +1133,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;
}
@@ -1313,8 +1340,6 @@ 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);
kfree(opts);
}
EXPORT_SYMBOL_GPL(nvmf_free_options);
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index caf5503d0833296aa03c028c0199cf92cc409edc..49a6f6b6833a852a55e3bd3a3fbf3f1ca3e8c5d0 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;
+ key_serial_t dhchap_key;
+ key_serial_t 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 e7e4fe2ad506ac30ba88a964c52d0dd3a995d75b..27f06394af83601ae4ad4a06f3f235d48c5a73d9 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -7,6 +7,7 @@

#include <linux/nvme-auth.h>
#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>
#include <linux/blkdev.h>

#include "nvme.h"
@@ -819,47 +820,79 @@ 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;
+ ssize_t count;

- if (!opts->dhchap_secret)
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ key = ctrl->host_key;
+ if (!key) {
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
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);
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ return count;
}

static ssize_t dhchap_secret_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
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(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(dhchap_secret, len);
+ 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->device, "key %08x invalidated\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;
+ if (old_key && key_serial(old_key) == key_serial(key)) {
+ dev_warn(ctrl->device, "using existing key %08x\n",
+ key_serial(old_key));
+ key_revoke(key);
+ old_key = key;
+ } else {
+ 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,49 +906,81 @@ 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;
+ ssize_t count;

- if (!opts->dhchap_ctrl_secret)
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ key = ctrl->ctrl_key;
+ if (!key) {
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
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>\n");
+ else {
+ count = key->type->read(key, buf, PAGE_SIZE);
+ if (count > 0) {
+ buf[count] = '\n';
+ count++;
+ }
+ }
+ up_read(&key->sem);
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ return count;
}

static ssize_t dhchap_ctrl_secret_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
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(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(dhchap_secret, len);
+ 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->device, "key %08x invalidated\n",
+ key_serial(key));
+ dev_dbg(ctrl->device, "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;
+ if (old_key && key_serial(old_key) == key_serial(key)) {
+ dev_warn(ctrl->device, "using existing ctrl key %08x\n",
+ key_serial(old_key));
+ old_key = key;
+ } else {
+ dev_dbg(ctrl->device, "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);
@@ -924,6 +989,56 @@ 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;
+ ssize_t count = 0;
+
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ key = ctrl->host_key;
+ if (!key) {
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ return sysfs_emit(buf, "none\n");
+ }
+ down_read(&key->sem);
+ if (key_validate(key))
+ count = sysfs_emit(buf, "<invalidated>\n");
+ else
+ count = sysfs_emit(buf, "%08x\n",
+ key_serial(key));
+ up_read(&key->sem);
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ 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);
+ struct key *key;
+ ssize_t count = 0;
+
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ key = ctrl->ctrl_key;
+ if (!key) {
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ return sysfs_emit(buf, "none\n");
+ }
+ down_read(&key->sem);
+ if (key_validate(key))
+ count = sysfs_emit(buf, "<invalidated>\n");
+ else
+ count = sysfs_emit(buf, "%08x\n",
+ key_serial(key));
+ up_read(&key->sem);
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ return count;
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
#endif

static struct attribute *nvme_dev_attrs[] = {
@@ -955,6 +1070,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
@@ -985,6 +1102,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
return 0;
if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
return 0;
+ if (a == &dev_attr_dhchap_key.attr && !ctrl->opts)
+ return 0;
+ if (a == &dev_attr_dhchap_ctrl_key.attr && !ctrl->opts)
+ return 0;
#endif

return a->mode;

--
2.51.0