From: Hannes Reinecke <hare@kernel.org>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
Hannes Reinecke <hare@suse.de>,
Chaitanya Kulkarni <kch@nvidia.com>,
Eric Biggers <ebiggers@kernel.org>,
David Howells <dhowells@redhat.com>,
Jarkko Sakkinen <jarkko@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org,
keyrings@vger.kernel.org, linux-doc@vger.kernel.org,
linux-hardening@vger.kernel.org,
Hannes Reinecke <hare@kernel.org>
Subject: [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute
Date: Thu, 26 Nov 2026 15:12:26 +0100 [thread overview]
Message-ID: <20261126-dhchap-keyring-v4-v7-8-ee438ffea527@kernel.org> (raw)
In-Reply-To: <20261126-dhchap-keyring-v4-v7-0-ee438ffea527@kernel.org>
In preparation for moving the nvmet DH-HMAC-CHAP code over
to using the kernel keyring convert the configfs interface
to use a key serial instead of storing the key data directly.
The key itself will be looked up when a controller is created
via key_lookup() to avoid having to pass around a pointer with
the actual key material.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/target/auth.c | 166 ++++++++++++++++++++++++++---------------
drivers/nvme/target/configfs.c | 80 ++++++++++++++++----
drivers/nvme/target/nvmet.h | 9 +--
3 files changed, 173 insertions(+), 82 deletions(-)
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index e1d92f5455219fd9e1845c2dca569fbb4d6450c5..ff0e8569d70675e80c1a8f18733a83317184026a 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -19,53 +19,82 @@
#include "nvmet.h"
+static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
+ struct key *new_key)
+{
+ key_serial_t old_key_id = 0, new_key_id = key_serial(new_key);
+ struct key *key = NULL;
+
+ if (set_ctrl) {
+ if (host->dhchap_ctrl_key) {
+ if (host->dhchap_ctrl_key == new_key_id) {
+ if (new_key)
+ key_revoke(new_key);
+ return;
+ }
+ old_key_id = host->dhchap_ctrl_key;
+ }
+ host->dhchap_ctrl_key = new_key_id;
+ } else {
+ if (host->dhchap_key) {
+ if (host->dhchap_key == new_key_id) {
+ if (new_key)
+ key_revoke(new_key);
+ return;
+ }
+ old_key_id = host->dhchap_key;
+ }
+ host->dhchap_key = new_key_id;
+ }
+ if (!old_key_id)
+ return;
+
+ key = key_lookup(old_key_id);
+ if (IS_ERR(key))
+ return;
+ pr_debug("%s: revoke %s key %08x\n", __func__,
+ set_ctrl ? "ctrl" : "host", key_serial(key));
+ key_revoke(key);
+ key_put(key);
+}
+
+void nvmet_auth_clear_key(struct nvmet_host *host)
+{
+ nvmet_auth_update_key(host, false, NULL);
+ nvmet_auth_update_key(host, true, NULL);
+}
+
int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool set_ctrl)
{
- unsigned char key_hash;
- char *dhchap_secret;
- int offset = 0;
+ struct key *key;
+ size_t len;
- if (!strlen(secret)) {
+ if (!secret || !strlen(secret)) {
down_write(&nvmet_config_sem);
- if (set_ctrl) {
- kfree(host->dhchap_ctrl_secret);
- host->dhchap_ctrl_secret = NULL;
- host->dhchap_ctrl_key_hash = 0;
- host->dhchap_ctrl_key_offset = 0;
- } else {
- kfree(host->dhchap_secret);
- host->dhchap_secret = NULL;
- host->dhchap_key_hash = 0;
- host->dhchap_key_offset = 0;
- }
+ nvmet_auth_update_key(host, set_ctrl, NULL);
up_write(&nvmet_config_sem);
return 0;
}
- if (sscanf(secret, "DHHC-1:%hhd:%n", &key_hash, &offset) != 1 ||
- !offset)
- return -EINVAL;
- if (key_hash > 3) {
- pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
- key_hash);
- return -EINVAL;
- }
- dhchap_secret = kstrdup(secret, GFP_KERNEL);
- if (!dhchap_secret)
- return -ENOMEM;
+ len = strcspn(secret, "\n");
+ key = nvme_auth_extract_key(secret, len);
+ if (IS_ERR(key)) {
+ pr_debug("%s: invalid key specification\n", __func__);
+ return PTR_ERR(key);
+ }
+ down_read(&key->sem);
+ if (key_validate(key)) {
+ pr_warn("%s: key %08x invalidated\n",
+ __func__, key_serial(key));
+ up_read(&key->sem);
+ key_put(key);
+ return -EKEYREVOKED;
+ }
+ up_read(&key->sem);
down_write(&nvmet_config_sem);
- if (set_ctrl) {
- kfree(host->dhchap_ctrl_secret);
- host->dhchap_ctrl_secret = strim(dhchap_secret);
- host->dhchap_ctrl_key_hash = key_hash;
- host->dhchap_ctrl_key_offset = offset;
- } else {
- kfree(host->dhchap_secret);
- host->dhchap_secret = strim(dhchap_secret);
- host->dhchap_key_hash = key_hash;
- host->dhchap_key_offset = offset;
- }
+ nvmet_auth_update_key(host, set_ctrl, key);
up_write(&nvmet_config_sem);
+ key_put(key);
return 0;
}
@@ -151,9 +180,6 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
struct nvmet_host_link *p;
struct nvmet_host *host = NULL;
struct key *host_key, *ctrl_key = NULL;
-#ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
- u8 host_hash, ctrl_hash;
-#endif
down_read(&nvmet_config_sem);
if (nvmet_is_disc_subsys(ctrl->subsys))
@@ -186,22 +212,45 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
goto out_unlock;
}
- if (!host->dhchap_secret) {
+ if (!host->dhchap_key) {
pr_debug("No authentication provided\n");
goto out_unlock;
}
- host_key = nvme_auth_extract_key(host->dhchap_secret,
- strlen(host->dhchap_secret));
- if (IS_ERR(host_key)) {
+ host_key = key_lookup(host->dhchap_key);
+ if (!host_key) {
+ pr_warn("%s: host key %08x not found\n",
+ __func__, host->dhchap_key);
ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
goto out_unlock;
}
- if (host->dhchap_ctrl_secret) {
- ctrl_key = nvme_auth_extract_key(host->dhchap_ctrl_secret,
- strlen(host->dhchap_ctrl_secret));
- if (IS_ERR(ctrl_key)) {
+ down_read(&host_key->sem);
+ ret = key_validate(host_key);
+ up_read(&host_key->sem);
+ if (ret) {
+ pr_warn("host key %08x invalidated\n",
+ host->dhchap_key);
+ key_put(host_key);
+ ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
+ goto out_unlock;
+ }
+ if (host->dhchap_ctrl_key) {
+ ctrl_key = key_lookup(host->dhchap_ctrl_key);
+ if (!ctrl_key) {
+ pr_warn("%s: ctrl key %08x not found\n",
+ __func__, host->dhchap_ctrl_key);
+ key_put(host_key);
+ ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
+ goto out_unlock;
+ }
+ down_read(&ctrl_key->sem);
+ ret = key_validate(ctrl_key);
+ up_read(&ctrl_key->sem);
+ if (ret) {
+ pr_warn("ctrl key %08x invalidated\n",
+ host->dhchap_ctrl_key);
key_put(host_key);
+ key_put(ctrl_key);
ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
goto out_unlock;
}
@@ -212,18 +261,13 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
ctrl->ctrl_key = ctrl_key;
ctrl->shash_id = host->dhchap_hash_id;
#ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
- host_hash = nvme_dhchap_psk_hash(ctrl->host_key);
- pr_debug("%s: using hash %s key %u\n", __func__,
- ctrl_hash > 0 ?
- nvme_auth_hmac_name(ctrl_hash) : "none",
+ pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
+ nvme_auth_hmac_name(ctrl->shash_id),
key_serial(ctrl->host_key));
- if (ctrl->ctrl_key) {
- ctrl_hash = nvme_dhchap_psk_hash(ctrl->ctrl_key);
- pr_debug("%s: using ctrl hash %s key %u\n", __func__,
- ctrl_hash > 0 ?
- nvme_auth_hmac_name(ctrl_hash) : "none",
+ if (ctrl->ctrl_key)
+ pr_debug("%s: using dhchap ctrl hash %s key %08x\n", __func__,
+ nvme_auth_hmac_name(ctrl->shash_id),
key_serial(ctrl->ctrl_key));
- }
#endif
out_unlock:
up_read(&nvmet_config_sem);
@@ -263,12 +307,14 @@ void nvmet_destroy_auth(struct nvmet_ctrl *ctrl)
ctrl->dh_key = NULL;
if (ctrl->host_key) {
- key_revoke(ctrl->host_key);
+ pr_debug("%s: drop host key %08x\n",
+ __func__, key_serial(ctrl->host_key));
key_put(ctrl->host_key);
ctrl->host_key = NULL;
}
if (ctrl->ctrl_key) {
- key_revoke(ctrl->ctrl_key);
+ pr_debug("%s: drop ctrl key %08x\n",
+ __func__, key_serial(ctrl->ctrl_key));
key_put(ctrl->ctrl_key);
ctrl->ctrl_key = NULL;
}
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index ff3a8d74de59966952a3991542077feebae10770..f6bfda757f076293e6e3a2db4d4245ea3a754b40 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -15,6 +15,7 @@
#include <linux/pci-p2pdma.h>
#ifdef CONFIG_NVME_TARGET_AUTH
#include <linux/nvme-auth.h>
+#include <linux/key-type.h>
#endif
#include <linux/nvme-keyring.h>
#include <crypto/kpp.h>
@@ -2234,15 +2235,29 @@ static struct config_group nvmet_ports_group;
static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
char *page)
{
- char *dhchap_secret;
+ struct nvmet_host *host = to_host(item);
+ struct key *key;
ssize_t ret;
down_read(&nvmet_config_sem);
- dhchap_secret = to_host(item)->dhchap_secret;
- if (!dhchap_secret)
- ret = sprintf(page, "\n");
- else
- ret = sprintf(page, "%s\n", dhchap_secret);
+ key = key_lookup(host->dhchap_key);
+ if (!key) {
+ page[0] = '\0';
+ ret = 0;
+ } else {
+ down_read(&key->sem);
+ if (key_validate(key))
+ ret = sprintf(page, "<invalidated>\n");
+ else {
+ ret = key->type->read(key, page, PAGE_SIZE);
+ if (ret > 0) {
+ page[ret] = '\n';
+ ret++;
+ }
+ }
+ up_read(&key->sem);
+ key_put(key);
+ }
up_read(&nvmet_config_sem);
return ret;
}
@@ -2251,9 +2266,19 @@ static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
const char *page, size_t count)
{
struct nvmet_host *host = to_host(item);
+ u8 *keydata = NULL;
+ size_t len;
int ret;
- ret = nvmet_auth_set_key(host, page, false);
+ len = strcspn(page, "\n");
+ if (len) {
+ keydata = kstrndup(page, len, GFP_KERNEL);
+ if (!keydata)
+ return -ENOMEM;
+ }
+
+ ret = nvmet_auth_set_key(host, keydata, false);
+ kfree_sensitive(keydata);
/*
* Re-authentication is a soft state, so keep the
* current authentication valid until the host
@@ -2267,15 +2292,29 @@ CONFIGFS_ATTR(nvmet_host_, dhchap_key);
static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
char *page)
{
- char *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
+ struct nvmet_host *host = to_host(item);
+ struct key *key;
ssize_t ret;
down_read(&nvmet_config_sem);
- dhchap_secret = to_host(item)->dhchap_ctrl_secret;
- if (!dhchap_secret)
- ret = sprintf(page, "\n");
- else
- ret = sprintf(page, "%s\n", dhchap_secret);
+ key = key_lookup(host->dhchap_ctrl_key);
+ if (!key) {
+ page[0] = '\0';
+ ret = 0;
+ } else {
+ down_read(&key->sem);
+ if (key_validate(key))
+ ret = sprintf(page, "<invalidated>\n");
+ else {
+ ret = key->type->read(key, page, PAGE_SIZE);
+ if (ret > 0) {
+ page[ret] = '\n';
+ ret++;
+ }
+ }
+ up_read(&key->sem);
+ key_put(key);
+ }
up_read(&nvmet_config_sem);
return ret;
}
@@ -2284,9 +2323,19 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
const char *page, size_t count)
{
struct nvmet_host *host = to_host(item);
+ u8 *keydata = NULL;
+ size_t len;
int ret;
- ret = nvmet_auth_set_key(host, page, true);
+ len = strcspn(page, "\n");
+ if (len) {
+ keydata = kstrndup(page, len, GFP_KERNEL);
+ if (!keydata)
+ return -ENOMEM;
+ }
+
+ ret = nvmet_auth_set_key(host, keydata, true);
+ kfree_sensitive(keydata);
/*
* Re-authentication is a soft state, so keep the
* current authentication valid until the host
@@ -2365,8 +2414,7 @@ static void nvmet_host_release(struct config_item *item)
struct nvmet_host *host = to_host(item);
#ifdef CONFIG_NVME_TARGET_AUTH
- kfree(host->dhchap_secret);
- kfree(host->dhchap_ctrl_secret);
+ nvmet_auth_clear_key(host);
#endif
kfree(host);
}
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 1dfdac159dab044566aac47862665784feddec0a..df84776b90f3e198f19f158e82ddddc473ee7296 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -399,12 +399,8 @@ static inline struct nvmet_subsys *namespaces_to_subsys(
struct nvmet_host {
struct config_group group;
- char *dhchap_secret;
- char *dhchap_ctrl_secret;
- int dhchap_key_offset;
- int dhchap_ctrl_key_offset;
- u8 dhchap_key_hash;
- u8 dhchap_ctrl_key_hash;
+ key_serial_t dhchap_key;
+ key_serial_t dhchap_ctrl_key;
u8 dhchap_hash_id;
u8 dhchap_dhgroup_id;
};
@@ -998,6 +994,7 @@ u32 nvmet_auth_send_data_len(struct nvmet_req *req);
void nvmet_execute_auth_send(struct nvmet_req *req);
u32 nvmet_auth_receive_data_len(struct nvmet_req *req);
void nvmet_execute_auth_receive(struct nvmet_req *req);
+void nvmet_auth_clear_key(struct nvmet_host *host);
int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool set_ctrl);
int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
--
2.51.0
next prev parent reply other threads:[~2026-11-26 14:13 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 01/14] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked() Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 04/14] Documentation/security/keys: fixup function name Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring Hannes Reinecke
2026-11-26 19:49 ` kernel test robot
2026-11-26 20:22 ` kernel test robot
2026-11-26 14:12 ` [PATCH v7 06/14] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 07/14] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-26 14:12 ` Hannes Reinecke [this message]
2026-11-26 14:12 ` [PATCH v7 09/14] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 10/14] nvme-auth: allow to specify the key by key serial Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 11/14] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 12/14] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys Hannes Reinecke
2026-11-26 21:15 ` kernel test robot
2026-11-26 16:00 ` [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Jarkko Sakkinen
2026-11-26 16:03 ` Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-dhchap-keyring-v4-v7-8-ee438ffea527@kernel.org \
--to=hare@kernel.org \
--cc=axboe@kernel.dk \
--cc=corbet@lwn.net \
--cc=dhowells@redhat.com \
--cc=ebiggers@kernel.org \
--cc=gustavoars@kernel.org \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=jarkko@kernel.org \
--cc=kbusch@kernel.org \
--cc=kch@nvidia.com \
--cc=kees@kernel.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=rdunlap@infradead.org \
--cc=sagi@grimberg.me \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®