* [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring
@ 2026-11-26 14:12 Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 01/14] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
` (14 more replies)
0 siblings, 15 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke, Martin Belanger
Hey all,
the current NVMe authentication code is using a hand-crafted key
structure; idea was to have the initial implementation with a minimal
set of dependencies.
(And me not having a good grasp on how to use the kernel keyring :-)
That had the drawback that keys always had to be specified on the
nvme-cli commandline, which is far from ideal from a security standpoint.
So this patchset switches the authentication code over to use
'struct key' to hold DH-HMAC-CHAP secrets. User-facing interfaces
(namely argument to 'nvme connect') remain the same, but the key data
is converted into keys which are stored as a new key type 'dhchap' with
a random UUID as description in the .nvme keyring.
With this I have updated the dhchap arguments to 'nvme connect' and
the configfs interface to either be the keydata (ie the original
interface), a key description, _or_ a key serial referring to a
pre-populated dhchap key in the kernel keyring. This allows for external
provisioning of keys and avoids the security risk from having to specify
the key data on the kernel commandline.
The entire patchset can be found at
git://git.kernel.org/pub/scm/linux/kernel/git/hare/nvme.git
branch dhchap-keyring.v6
As usual, comments and reviews are welcome.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
Changes in v7:
- Fixup next round of issues from sashiko
- Modify to use key_serial_t instead of 'struct key' to pass key references
- Include suggestions from Martin Belanger
- Link to v6: https://lore.kernel.org/r/20261123-dhchap-keyring-v4-v6-0-5082a49715ab@kernel.org
Changes in v6:
- Fixup next round of issues from sashiko
- Fixup kbuild robot issues
- Link to v5: https://lore.kernel.org/r/20261123-dhchap-keyring-v4-v5-0-81eefb4c0724@kernel.org
Changes in v5:
- Fixup issues found by sashiko
- Link to v4: https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org
---
Hannes Reinecke (14):
nvme-auth: modify nvme_auth_transform_key() to return status
nvme-keyring: add 'dhchap' key type
nvme-keyring: drop nvme_tls_psk_revoked()
Documentation/security/keys: fixup function name
nvme-keyring: link addtional keyring into .nvme keyring
nvme-auth: switch to use 'struct key'
nvme: parse dhchap keys during option parsing
nvmet-auth: parse dhchap key from configfs attribute
nvme: allow to pass in key description as dhchap secret
nvme-auth: allow to specify the key by key serial
nvme-auth: wait for authentication to finish when changing keys
nvme-fabrics: allow to pass in keyring by name
nvme-auth: link pre-provisioned keys into .nvme keyring
nvme-auth: allow to update pre-provisioned keys
Documentation/ABI/stable/sysfs-nvme | 10 +
Documentation/security/keys/core.rst | 2 +-
drivers/nvme/common/Kconfig | 2 +
drivers/nvme/common/auth.c | 232 ++++++++++------------
drivers/nvme/common/keyring.c | 370 ++++++++++++++++++++++++++++++++---
drivers/nvme/host/Kconfig | 1 -
drivers/nvme/host/auth.c | 176 ++++++++++++-----
drivers/nvme/host/fabrics.c | 169 ++++++++++++----
drivers/nvme/host/fabrics.h | 12 +-
drivers/nvme/host/nvme.h | 6 +-
drivers/nvme/host/sysfs.c | 300 ++++++++++++++++++++++------
drivers/nvme/host/tcp.c | 5 +-
drivers/nvme/target/Kconfig | 1 -
drivers/nvme/target/auth.c | 250 +++++++++++++++--------
drivers/nvme/target/configfs.c | 84 ++++++--
drivers/nvme/target/nvmet.h | 15 +-
include/linux/nvme-auth.h | 17 +-
include/linux/nvme-keyring.h | 47 ++++-
18 files changed, 1257 insertions(+), 442 deletions(-)
---
base-commit: 68099cf27253a8c89d703a282d3d811446d9453b
change-id: 20261120-dhchap-keyring-v4-09f0bac1d8c5
Best regards,
--
Hannes Reinecke <hare@kernel.org>
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 01/14] nvme-auth: modify nvme_auth_transform_key() to return status
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
@ 2026-11-26 14:12 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type Hannes Reinecke
` (13 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
In preparation for converting the DH-HMAC-CHAP code to use the
kernel keyring modify nvme_auth_transform_key() to return a status
and provide the transformed data as argument on the command line as
raw data.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/auth.c | 37 ++++++++++++++++++++-----------------
drivers/nvme/host/auth.c | 44 +++++++++++++++++++++++---------------------
drivers/nvme/target/auth.c | 37 +++++++++++++++++++++----------------
include/linux/nvme-auth.h | 4 ++--
4 files changed, 66 insertions(+), 56 deletions(-)
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index 1fdc4e4ed0b94d8a36aade033f19e17463f15632..308af3d7c7383acbfb327921c8a650898f0e15ea 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -317,37 +317,40 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
return -EINVAL;
}
-struct nvme_dhchap_key *nvme_auth_transform_key(
- const struct nvme_dhchap_key *key, const char *nqn)
+int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
+ u8 **transformed_secret)
{
struct nvme_auth_hmac_ctx hmac;
- struct nvme_dhchap_key *transformed_key;
- int ret, key_len;
+ u8 *key_data;
+ size_t transformed_len;
+ int ret;
if (!key) {
pr_warn("No key specified\n");
- return ERR_PTR(-ENOKEY);
+ return -ENOKEY;
}
if (key->hash == 0) {
- key_len = nvme_auth_key_struct_size(key->len);
- transformed_key = kmemdup(key, key_len, GFP_KERNEL);
- if (!transformed_key)
- return ERR_PTR(-ENOMEM);
- return transformed_key;
+ key_data = kzalloc(key->len, GFP_KERNEL);
+ if (!key_data)
+ return -ENOMEM;
+ memcpy(key_data, key->key, key->len);
+ *transformed_secret = key_data;
+ return key->len;
}
ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
if (ret)
- return ERR_PTR(ret);
- key_len = nvme_auth_hmac_hash_len(key->hash);
- transformed_key = nvme_auth_alloc_key(key_len, key->hash);
- if (!transformed_key) {
+ return ret;
+ transformed_len = nvme_auth_hmac_hash_len(key->hash);
+ key_data = kzalloc(transformed_len, GFP_KERNEL);
+ if (!key_data) {
memzero_explicit(&hmac, sizeof(hmac));
- return ERR_PTR(-ENOMEM);
+ return -ENOMEM;
}
nvme_auth_hmac_update(&hmac, nqn, strlen(nqn));
nvme_auth_hmac_update(&hmac, "NVMe-over-Fabrics", 17);
- nvme_auth_hmac_final(&hmac, transformed_key->key);
- return transformed_key;
+ nvme_auth_hmac_final(&hmac, key_data);
+ *transformed_secret = key_data;
+ return transformed_len;
}
EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 9a3da74dec5aa818538dfbc36fd27f7f756cc059..295e0a9533c8995c32781324650d73b6bfedbf13 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -22,7 +22,8 @@ struct nvme_dhchap_queue_context {
struct work_struct auth_work;
struct nvme_ctrl *ctrl;
struct crypto_kpp *dh_tfm;
- struct nvme_dhchap_key *transformed_key;
+ u8 *transformed_secret;
+ size_t transformed_len;
void *buf;
int qid;
int error;
@@ -436,22 +437,21 @@ static int nvme_auth_dhchap_setup_host_response(struct nvme_ctrl *ctrl,
dev_dbg(ctrl->device, "%s: qid %d host response seq %u transaction %d\n",
__func__, chap->qid, chap->s1, chap->transaction);
- if (!chap->transformed_key) {
- chap->transformed_key = nvme_auth_transform_key(ctrl->host_key,
- ctrl->opts->host->nqn);
- if (IS_ERR(chap->transformed_key)) {
- ret = PTR_ERR(chap->transformed_key);
- chap->transformed_key = NULL;
+ if (!chap->transformed_secret) {
+ ret = nvme_auth_transform_key(ctrl->host_key,
+ ctrl->opts->host->nqn,
+ &chap->transformed_secret);
+ if (ret < 0)
return ret;
- }
+ chap->transformed_len = ret;
} else {
dev_dbg(ctrl->device, "%s: qid %d re-using host response\n",
__func__, chap->qid);
}
ret = nvme_auth_hmac_init(&hmac, chap->hash_id,
- chap->transformed_key->key,
- chap->transformed_key->len);
+ chap->transformed_secret,
+ chap->transformed_len);
if (ret)
goto out;
@@ -500,19 +500,20 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
struct nvme_dhchap_queue_context *chap)
{
struct nvme_auth_hmac_ctx hmac;
- struct nvme_dhchap_key *transformed_key;
+ u8 *transformed_secret;
+ size_t transformed_len;
u8 buf[4], *challenge = chap->c2;
int ret;
- transformed_key = nvme_auth_transform_key(ctrl->ctrl_key,
- ctrl->opts->subsysnqn);
- if (IS_ERR(transformed_key)) {
- ret = PTR_ERR(transformed_key);
+ ret = nvme_auth_transform_key(ctrl->ctrl_key,
+ ctrl->opts->subsysnqn,
+ &transformed_secret);
+ if (ret < 0)
return ret;
- }
+ transformed_len = ret;
- ret = nvme_auth_hmac_init(&hmac, chap->hash_id, transformed_key->key,
- transformed_key->len);
+ ret = nvme_auth_hmac_init(&hmac, chap->hash_id, transformed_secret,
+ transformed_len);
if (ret) {
dev_warn(ctrl->device, "qid %d: failed to init hmac, error %d\n",
chap->qid, ret);
@@ -565,7 +566,7 @@ static int nvme_auth_dhchap_setup_ctrl_response(struct nvme_ctrl *ctrl,
if (challenge != chap->c2)
kfree(challenge);
memzero_explicit(&hmac, sizeof(hmac));
- nvme_auth_free_key(transformed_key);
+ kfree_sensitive(transformed_secret);
return ret;
}
@@ -628,8 +629,9 @@ static int nvme_auth_dhchap_exponential(struct nvme_ctrl *ctrl,
static void nvme_auth_reset_dhchap(struct nvme_dhchap_queue_context *chap)
{
- nvme_auth_free_key(chap->transformed_key);
- chap->transformed_key = NULL;
+ kfree_sensitive(chap->transformed_secret);
+ chap->transformed_secret = NULL;
+ chap->transformed_len = 0;
kfree_sensitive(chap->host_key);
chap->host_key = NULL;
chap->host_key_len = 0;
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index a4a2a5cfd823f921a9e9503f41de51b846c43296..150818d7009d8c2fca96e12fd54fdc1cb6b68882 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -295,17 +295,19 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
struct nvme_auth_hmac_ctx hmac;
struct nvmet_ctrl *ctrl = req->sq->ctrl;
u8 *challenge = req->sq->dhchap_c1;
- struct nvme_dhchap_key *transformed_key;
+ u8 *transformed_secret;
+ size_t transformed_len;
u8 buf[4];
int ret;
- transformed_key = nvme_auth_transform_key(ctrl->host_key,
- ctrl->hostnqn);
- if (IS_ERR(transformed_key))
- return PTR_ERR(transformed_key);
+ ret = nvme_auth_transform_key(ctrl->host_key, ctrl->hostnqn,
+ &transformed_secret);
+ if (ret < 0)
+ return ret;
+ transformed_len = ret;
- ret = nvme_auth_hmac_init(&hmac, ctrl->shash_id, transformed_key->key,
- transformed_key->len);
+ ret = nvme_auth_hmac_init(&hmac, ctrl->shash_id, transformed_secret,
+ transformed_len);
if (ret)
goto out_free_response;
@@ -357,7 +359,7 @@ int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
kfree(challenge);
out_free_response:
memzero_explicit(&hmac, sizeof(hmac));
- nvme_auth_free_key(transformed_key);
+ kfree_sensitive(transformed_secret);
return ret;
}
@@ -367,17 +369,20 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
struct nvme_auth_hmac_ctx hmac;
struct nvmet_ctrl *ctrl = req->sq->ctrl;
u8 *challenge = req->sq->dhchap_c2;
- struct nvme_dhchap_key *transformed_key;
+ u8 *transformed_secret;
+ size_t transformed_len;
u8 buf[4];
int ret;
- transformed_key = nvme_auth_transform_key(ctrl->ctrl_key,
- ctrl->subsys->subsysnqn);
- if (IS_ERR(transformed_key))
- return PTR_ERR(transformed_key);
+ ret = nvme_auth_transform_key(ctrl->ctrl_key,
+ ctrl->subsys->subsysnqn,
+ &transformed_secret);
+ if (ret < 0)
+ return ret;
+ transformed_len = ret;
- ret = nvme_auth_hmac_init(&hmac, ctrl->shash_id, transformed_key->key,
- transformed_key->len);
+ ret = nvme_auth_hmac_init(&hmac, ctrl->shash_id, transformed_secret,
+ transformed_len);
if (ret)
goto out_free_response;
@@ -426,7 +431,7 @@ int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
kfree(challenge);
out_free_response:
memzero_explicit(&hmac, sizeof(hmac));
- nvme_auth_free_key(transformed_key);
+ kfree_sensitive(transformed_secret);
return ret;
}
diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
index d674d8ab26e6b3b4b297ebaefa3f33b8ec97bc0a..1c5ce635b169dd0747c1344e9adcc14839776cd1 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -41,8 +41,8 @@ u32 nvme_auth_key_struct_size(u32 key_len);
struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash);
void nvme_auth_free_key(struct nvme_dhchap_key *key);
struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash);
-struct nvme_dhchap_key *nvme_auth_transform_key(
- const struct nvme_dhchap_key *key, const char *nqn);
+int nvme_auth_transform_key(const struct nvme_dhchap_key *key,
+ const char *nqn, u8 **transformed_secret);
int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key);
int nvme_auth_augmented_challenge(u8 hmac_id, const u8 *skey, size_t skey_len,
const u8 *challenge, u8 *aug, size_t hlen);
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked() Hannes Reinecke
` (12 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
Add a 'dhchap' keytype to store DH-HMAC-CHAP secret keys.
Keys are stored with a 'user-type' compatible payload, such
that one can use 'user_read()' to access the raw contents
and the 'read()' callback to get the base64-encoded key
data in the DH-HMAC-CHAP secret representation.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/Kconfig | 1 +
drivers/nvme/common/keyring.c | 209 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 210 insertions(+)
diff --git a/drivers/nvme/common/Kconfig b/drivers/nvme/common/Kconfig
index f1639db65fd38abde01832a7404cbc48cf49a93e..24c893c71d86527c1cdf3adace2151f2894afbb6 100644
--- a/drivers/nvme/common/Kconfig
+++ b/drivers/nvme/common/Kconfig
@@ -3,6 +3,7 @@
config NVME_KEYRING
tristate
select KEYS
+ select CRC32
config NVME_AUTH
tristate
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 87cb51e1f5a7f330af4705f4058f661d15f5984a..de8dab10fa1059d8bfc2351edd7bb68ee52207a5 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -4,6 +4,9 @@
*/
#include <linux/module.h>
+#include <linux/crc32.h>
+#include <linux/base64.h>
+#include <linux/unaligned.h>
#include <linux/seq_file.h>
#include <linux/key-type.h>
#include <keys/user-type.h>
@@ -253,6 +256,205 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
}
EXPORT_SYMBOL_GPL(nvme_tls_psk_default);
+static void nvme_dhchap_psk_describe(const struct key *key, struct seq_file *m)
+{
+ seq_puts(m, key->description);
+ seq_printf(m, ": %u", key->datalen);
+}
+
+/**
+ * nvme_dhchap_psk_preparse - prepare DH-HMAC-CHAP key data
+ * @prep: preparsed payload of the key data
+ *
+ * Decode the DH-HMAC-CHAP key data passed in @prep and
+ * store the resulting binary data. The binary data includes
+ * space for the CRC, the version, and the hmac identifier,
+ * but the data length is just the key data without the CRC.
+ * This allows the user to read the key data via the
+ * 'user_read()' function. The additional 'version' ahd 'hmac'
+ * data is used in the ->read() callback to generate the
+ * base64 encoded key.
+ */
+static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
+{
+ struct user_key_payload *upayload;
+ size_t datalen = prep->datalen;
+ int ret = -EINVAL, offset = 0;
+ char *p;
+ u32 crc;
+ u8 version, hmac;
+
+ if (!prep->data || prep->datalen < 10) {
+ pr_debug("%s: Invalid key data", __func__);
+ goto out_err;
+ }
+ if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
+ &version, &hmac, &offset) != 2 || offset != 10) {
+ pr_debug("%s: invalid key header\n", __func__);
+ goto out_err;
+ }
+
+ if (version != 1 || hmac > 3) {
+ pr_debug("%s: invalid key version\n", __func__);
+ goto out_err;
+ }
+
+ /* skip header and final ':' character */
+ if (offset > prep->datalen) {
+ pr_debug("%s: invalid key data\n", __func__);
+ goto out_err;
+ }
+ datalen = prep->datalen - offset;
+ p = memchr(prep->data + offset, ':', datalen);
+ if (p) {
+ datalen = (p - (char *)prep->data);
+ datalen -= offset;
+ }
+
+ /*
+ * payload is < key | version | hmac >
+ * base64 decode will always return less data
+ * than the encoded data, so allocating the size
+ * of the encoded data will be large enough.
+ */
+ upayload = kzalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
+ if (!upayload) {
+ ret = -ENOMEM;
+ goto out_err;
+ }
+
+ /* decode the data */
+ prep->payload.data[0] = upayload;
+ ret = base64_decode(prep->data + offset, datalen, upayload->data,
+ true, BASE64_STD);
+ if (ret < 0) {
+ pr_debug("%s: Failed to decode key\n", __func__);
+ ret = -EINVAL;
+ goto out_free;
+ }
+ if (ret != 36 && ret != 52 && ret != 68) {
+ pr_debug("%s: invalid decoded key size '%d'\n",
+ __func__, ret);
+ ret = -EINVAL;
+ goto out_free;
+ }
+ ret -= 4;
+ crc = ~crc32(~0, upayload->data, ret);
+ if (get_unaligned_le32(upayload->data + ret) != crc) {
+ /* CRC mismatch */
+ pr_debug("%s: CRC mismatch for key\n", __func__);
+ ret = -EKEYREJECTED;
+ goto out_free;
+ }
+ /* append version and hmac to the payload */
+ upayload->data[ret + 4] = version;
+ upayload->data[ret + 5] = hmac;
+ upayload->datalen = ret;
+ prep->quotalen = ret + 6;
+ return 0;
+out_free:
+ kfree_sensitive(upayload);
+out_err:
+ prep->payload.data[0] = NULL;
+ prep->quotalen = 0;
+ return ret;
+}
+
+/**
+ * nvme_dhchap_encoded_key_size - Size of the base64-encoded key
+ * @size: size of the encoded key
+ *
+ * Returns the expected size of the key after base64 decoding
+ * include 4 bytes of CRC.
+ */
+static inline int nvme_dhchap_encoded_key_size(int size)
+{
+ int keylen = -EINVAL;
+
+ switch (size) {
+ case 32:
+ keylen = 48;
+ break;
+ case 48:
+ keylen = 72;
+ break;
+ case 64:
+ keylen = 92;
+ break;
+ default:
+ break;
+ }
+ return keylen;
+}
+
+/**
+ * nvme_dhchap_psk_read - read callback for dhchap key types
+ * @key: key to read from
+ * @buffer: buffer for the key contents
+ * @buflen: length of @buffer
+ *
+ * Formats the DH-HMAC-CHAP key in base64-encoded form as
+ * defined in the NVM Express TCP Transport Specification Rev 1.1,
+ * Section 3.6.1.5 'PSK Interchange Format'. The payload of @key
+ * is assumed to be of 'struct user_key_payload', with the data
+ * consisting of the key data of length 'upayload->datalen', followed
+ * by four bytes of CRC, one byte version, and one byte hmac fields.
+ */
+static long nvme_dhchap_psk_read(const struct key *key,
+ char *buffer, size_t buflen)
+{
+ const struct user_key_payload *upayload;
+ size_t datalen, keylen;
+ u8 version, hmac;
+ long ret;
+
+ upayload = user_key_payload_locked(key);
+ /*
+ * Expected output string length:
+ * strlen("DHHC-") +
+ * version (= 1) +
+ * ':' (= 1) +
+ * hmac (= 2) +
+ * ':' (= 1) +
+ * encoded base64 key length +
+ * ':' (= 1)
+ */
+ ret = nvme_dhchap_encoded_key_size(upayload->datalen);
+ if (ret < 0)
+ return ret;
+ keylen = ret + 11;
+ if (!buffer || !buflen)
+ return keylen;
+ else if (buflen < keylen)
+ return -EINVAL;
+
+ memset(buffer, 0, buflen);
+ version = upayload->data[upayload->datalen + 4];
+ hmac = upayload->data[upayload->datalen + 5];
+ ret = sprintf(buffer, "DHHC-%01hhu:%02hhu:", version, hmac);
+ if (ret < 0)
+ return -ENOKEY;
+ /* Include the trailing CRC */
+ datalen = upayload->datalen + 4;
+ ret += base64_encode(upayload->data, datalen, buffer + ret,
+ true, BASE64_STD);
+ buffer[ret] = ':';
+ ret++;
+ return ret;
+}
+
+static struct key_type nvme_dhchap_psk_key_type = {
+ .name = "dhchap",
+ .flags = KEY_TYPE_NET_DOMAIN,
+ .preparse = nvme_dhchap_psk_preparse,
+ .free_preparse = user_free_preparse,
+ .instantiate = generic_key_instantiate,
+ .revoke = user_revoke,
+ .destroy = user_destroy,
+ .describe = nvme_dhchap_psk_describe,
+ .read = nvme_dhchap_psk_read,
+};
+
static int __init nvme_keyring_init(void)
{
int err;
@@ -271,11 +473,18 @@ static int __init nvme_keyring_init(void)
key_put(nvme_keyring);
return err;
}
+ err = register_key_type(&nvme_dhchap_psk_key_type);
+ if (err) {
+ unregister_key_type(&nvme_tls_psk_key_type);
+ key_put(nvme_keyring);
+ return err;
+ }
return 0;
}
static void __exit nvme_keyring_exit(void)
{
+ unregister_key_type(&nvme_dhchap_psk_key_type);
unregister_key_type(&nvme_tls_psk_key_type);
key_revoke(nvme_keyring);
key_put(nvme_keyring);
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked()
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 04/14] Documentation/security/keys: fixup function name Hannes Reinecke
` (11 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
Replace with build-in function key_validate().
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/keyring.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index de8dab10fa1059d8bfc2351edd7bb68ee52207a5..373f51e1032a4d29ef945a62fd9fb3720f8a9ea8 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -22,24 +22,20 @@ key_serial_t nvme_keyring_id(void)
}
EXPORT_SYMBOL_GPL(nvme_keyring_id);
-static bool nvme_tls_psk_revoked(struct key *psk)
-{
- return test_bit(KEY_FLAG_REVOKED, &psk->flags) ||
- test_bit(KEY_FLAG_INVALIDATED, &psk->flags);
-}
-
struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
struct key *key = key_lookup(key_id);
+ int ret;
if (IS_ERR(key)) {
pr_err("key id %08x not found\n", key_id);
return key;
}
- if (nvme_tls_psk_revoked(key)) {
- pr_err("key id %08x revoked\n", key_id);
+ ret = key_validate(key);
+ if (ret < 0) {
+ pr_err("key id %08x invalid\n", key_id);
key_put(key);
- return ERR_PTR(-EKEYREVOKED);
+ return ERR_PTR(ret);
}
return key;
}
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 04/14] Documentation/security/keys: fixup function name
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (2 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked() Hannes Reinecke
@ 2026-11-26 14:12 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring Hannes Reinecke
` (10 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
The function is called 'key_validate()', not 'validate_key()'.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
Documentation/security/keys/core.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Documentation/security/keys/core.rst b/Documentation/security/keys/core.rst
index 37ed42382f1672a5c822e7a042b077ffb84c7abd..c0f69e93e0c5f0efe0ae2e7a45a3a40391346423 100644
--- a/Documentation/security/keys/core.rst
+++ b/Documentation/security/keys/core.rst
@@ -1306,7 +1306,7 @@ payload contents" for more information.
* To check the validity of a key, this function can be called::
- int validate_key(struct key *key);
+ int key_validate(struct key *key);
This checks that the key in question hasn't expired or and hasn't been
revoked. Should the key be invalid, error EKEYEXPIRED or EKEYREVOKED will
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (3 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 04/14] Documentation/security/keys: fixup function name Hannes Reinecke
@ 2026-11-26 14:12 ` 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
` (9 subsequent siblings)
14 siblings, 2 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
Rather than switching to a different keyring (and trying
to deal with possibly restricted access to that keyring)
we should link the additional keyring into the default .nvme
keyring to make the keys available for lookup for
the lifetime of the corresponding nvme structures.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/keyring.c | 63 ++++++++++++++++++++++++++-----------------
drivers/nvme/host/auth.c | 3 +--
drivers/nvme/host/fabrics.c | 13 +++++++--
drivers/nvme/host/sysfs.c | 3 +++
drivers/nvme/host/tcp.c | 3 +--
drivers/nvme/target/auth.c | 2 +-
include/linux/nvme-keyring.h | 30 +++++++++++++++------
7 files changed, 77 insertions(+), 40 deletions(-)
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 373f51e1032a4d29ef945a62fd9fb3720f8a9ea8..0fc6b4825c2dd9a23daaf4aae3d9f63f4f07c164 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -22,6 +22,24 @@ key_serial_t nvme_keyring_id(void)
}
EXPORT_SYMBOL_GPL(nvme_keyring_id);
+const char *nvme_keyring_name(void)
+{
+ return nvme_keyring->description;
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_name);
+
+int nvme_keyring_link(struct key *keyring)
+{
+ return key_link(nvme_keyring, keyring);
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_link);
+
+int nvme_keyring_unlink(struct key *keyring)
+{
+ return key_unlink(nvme_keyring, keyring);
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_unlink);
+
struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
struct key *key = key_lookup(key_id);
@@ -88,14 +106,12 @@ static struct key_type nvme_tls_psk_key_type = {
.read = user_read,
};
-static struct key *nvme_tls_psk_lookup(struct key *keyring,
- const char *hostnqn, const char *subnqn,
+static struct key *nvme_tls_psk_lookup(const char *hostnqn, const char *subnqn,
u8 hmac, u8 psk_ver, bool generated)
{
char *identity;
size_t identity_len = (NVMF_NQN_SIZE) * 2 + 11;
key_ref_t keyref;
- key_serial_t keyring_id;
identity = kzalloc(identity_len, GFP_KERNEL);
if (!identity)
@@ -104,12 +120,8 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
snprintf(identity, identity_len, "NVMe%u%c%02u %s %s",
psk_ver, generated ? 'G' : 'R', hmac, hostnqn, subnqn);
- if (!keyring)
- keyring = nvme_keyring;
- keyring_id = key_serial(keyring);
- pr_debug("keyring %x lookup tls psk '%s'\n",
- keyring_id, identity);
- keyref = keyring_search(make_key_ref(keyring, true),
+ pr_debug("lookup tls psk '%s'\n", identity);
+ keyref = keyring_search(make_key_ref(nvme_keyring, true),
&nvme_tls_psk_key_type,
identity, false);
if (IS_ERR(keyref)) {
@@ -125,7 +137,6 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
/**
* nvme_tls_psk_refresh - Refresh TLS PSK
- * @keyring: Keyring holding the TLS PSK
* @hostnqn: Host NQN to use
* @subnqn: Subsystem NQN to use
* @hmac_id: Hash function identifier
@@ -139,9 +150,8 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
*
* Returns the updated key success or an error pointer otherwise.
*/
-struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, const char *subnqn, u8 hmac_id,
- u8 *data, size_t data_len, const char *digest)
+struct key *nvme_tls_psk_refresh(const char *hostnqn, const char *subnqn,
+ u8 hmac_id, u8 *data, size_t data_len, const char *digest)
{
key_perm_t keyperm =
KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
@@ -149,7 +159,6 @@ struct key *nvme_tls_psk_refresh(struct key *keyring,
KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
char *identity;
key_ref_t keyref;
- key_serial_t keyring_id;
struct key *key;
if (!hostnqn || !subnqn || !data || !data_len)
@@ -160,12 +169,8 @@ struct key *nvme_tls_psk_refresh(struct key *keyring,
if (!identity)
return ERR_PTR(-ENOMEM);
- if (!keyring)
- keyring = nvme_keyring;
- keyring_id = key_serial(keyring);
- pr_debug("keyring %x refresh tls psk '%s'\n",
- keyring_id, identity);
- keyref = key_create_or_update(make_key_ref(keyring, true),
+ pr_debug("refresh tls psk '%s'\n", identity);
+ keyref = key_create_or_update(make_key_ref(nvme_keyring, true),
"psk", identity, data, data_len,
keyperm, KEY_ALLOC_NOT_IN_QUOTA |
KEY_ALLOC_BUILT_IN |
@@ -226,10 +231,18 @@ static struct nvme_tls_psk_priority_list {
};
/*
- * nvme_tls_psk_default - Return the preferred PSK to use for TLS ClientHello
+ * nvme_tls_psk_default - Select the preferred PSK to use for TLS ClientHello
+ * @hostnqn: Host NQN of the NVME TLS connection
+ * @subnqn: Subsystem NQN of the NVME TLS connection
+ *
+ * Looks up the preferred PSK to be used for the TLC ClientHello.
+ * The preferred PSK is the first matching PSK from the nvme_tls_psk_prio
+ * array.
+ *
+ * Returns: the key serial number of the preferred PSK or 0 if no matchine
+ * PSK was found.
*/
-key_serial_t nvme_tls_psk_default(struct key *keyring,
- const char *hostnqn, const char *subnqn)
+key_serial_t nvme_tls_psk_default(const char *hostnqn, const char *subnqn)
{
struct key *tls_key;
key_serial_t tls_key_id;
@@ -240,8 +253,8 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
u8 ver = nvme_tls_psk_prio[prio].psk_ver;
enum nvme_tcp_tls_cipher cipher = nvme_tls_psk_prio[prio].cipher;
- tls_key = nvme_tls_psk_lookup(keyring, hostnqn, subnqn,
- cipher, ver, generated);
+ tls_key = nvme_tls_psk_lookup(hostnqn, subnqn, cipher,
+ ver, generated);
if (!IS_ERR(tls_key)) {
tls_key_id = tls_key->serial;
key_put(tls_key);
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 295e0a9533c8995c32781324650d73b6bfedbf13..06399c075405353325b05c099811a54134fbeb53 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -726,8 +726,7 @@ static int nvme_auth_secure_concat(struct nvme_ctrl *ctrl,
goto out_free_digest;
}
- tls_key = nvme_tls_psk_refresh(ctrl->opts->keyring,
- ctrl->opts->host->nqn,
+ tls_key = nvme_tls_psk_refresh(ctrl->opts->host->nqn,
ctrl->opts->subsysnqn, chap->hash_id,
tls_psk, psk_len, digest);
if (IS_ERR(tls_key)) {
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 5ed91557f07065a284e664950a9c71d771614ec2..6a918d72d14c5dd92d00191c184e72b0e5614b7a 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -995,7 +995,11 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- key_put(opts->keyring);
+ ret = nvme_keyring_link(opts->keyring);
+ if (ret) {
+ key_put(key);
+ goto out;
+ }
opts->keyring = key;
break;
case NVMF_OPT_TLS_KEY:
@@ -1296,7 +1300,12 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
void nvmf_free_options(struct nvmf_ctrl_options *opts)
{
nvmf_host_put(opts->host);
- key_put(opts->keyring);
+ if (opts->keyring) {
+ int ret = nvme_keyring_unlink(opts->keyring);
+
+ WARN_ON(ret);
+ key_put(opts->keyring);
+ }
key_put(opts->tls_key);
kfree(opts->transport);
kfree(opts->traddr);
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 35f871e3e0c0ae49b390f0fb64c538796207a98f..c1375f23a97f6b5a47bb8aad6b12e7ff3ad12638 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -6,6 +6,7 @@
*/
#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
#include <linux/blkdev.h>
#include "nvme.h"
@@ -1069,6 +1070,8 @@ static ssize_t tls_keyring_show(struct device *dev,
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct key *keyring = ctrl->opts->keyring;
+ if (!keyring)
+ return sysfs_emit(buf, "%s\n", nvme_keyring_name());
return sysfs_emit(buf, "%s\n", keyring->description);
}
static DEVICE_ATTR_RO(tls_keyring);
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa51a6f47b10ef2400839f9dd18e..585aba1d544518d6e712e23c7987741b130aee4d 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -2416,8 +2416,7 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
if (ctrl->opts->tls_key)
pskid = key_serial(ctrl->opts->tls_key);
else if (ctrl->opts->tls) {
- pskid = nvme_tls_psk_default(ctrl->opts->keyring,
- ctrl->opts->host->nqn,
+ pskid = nvme_tls_psk_default(ctrl->opts->host->nqn,
ctrl->opts->subsysnqn);
if (!pskid) {
dev_err(ctrl->device, "no valid PSK found\n");
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 150818d7009d8c2fca96e12fd54fdc1cb6b68882..7ebd867c89468e94e31fd2739d3ee77018775f18 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -523,7 +523,7 @@ void nvmet_auth_insert_psk(struct nvmet_sq *sq)
goto out_free_digest;
}
#ifdef CONFIG_NVME_TARGET_TCP_TLS
- tls_key = nvme_tls_psk_refresh(NULL, sq->ctrl->hostnqn,
+ tls_key = nvme_tls_psk_refresh(sq->ctrl->hostnqn,
sq->ctrl->subsys->subsysnqn,
sq->ctrl->shash_id, tls_psk, psk_len,
digest);
diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
index ab8971afa9734a276d9197076b780321460dddc2..680eb93088d485ca3368f615715d71fa570b1077 100644
--- a/include/linux/nvme-keyring.h
+++ b/include/linux/nvme-keyring.h
@@ -10,20 +10,22 @@
#if IS_ENABLED(CONFIG_NVME_KEYRING)
-struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, const char *subnqn, u8 hmac_id,
- u8 *data, size_t data_len, const char *digest);
-key_serial_t nvme_tls_psk_default(struct key *keyring,
- const char *hostnqn, const char *subnqn);
+struct key *nvme_tls_psk_refresh(const char *hostnqn, const char *subnqn,
+ u8 hmac_id, u8 *data, size_t data_len, const char *digest);
+key_serial_t nvme_tls_psk_default(const char *hostnqn, const char *subnqn);
key_serial_t nvme_keyring_id(void);
+const char *nvme_keyring_name(void);
+int nvme_keyring_link(struct key *keyring);
+int nvme_keyring_unlink(struct key *keyring);
struct key *nvme_tls_key_lookup(key_serial_t key_id);
+
#else
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, char *subnqn, u8 hmac_id,
+ const char *hostnqn, const char *subnqn, u8 hmac_id,
u8 *data, size_t data_len, const char *digest)
{
- return ERR_PTR(-ENOTSUPP);
+ return ERR_PTR(-EOPNOTSUPP);
}
static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
const char *hostnqn, const char *subnqn)
@@ -34,9 +36,21 @@ static inline key_serial_t nvme_keyring_id(void)
{
return 0;
}
+static const char *nvme_keyring_name(void)
+{
+ return NULL;
+}
+static int nvme_keyring_link(struct key *keyring)
+{
+ return 0;
+}
+int nvme_keyring_unlink(struct key *keyring)
+{
+ return 0;
+}
static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
- return ERR_PTR(-ENOTSUPP);
+ return ERR_PTR(-EOPNOTSUPP);
}
#endif /* !CONFIG_NVME_KEYRING */
#endif /* _NVME_KEYRING_H */
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 06/14] nvme-auth: switch to use 'struct key'
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (4 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring Hannes Reinecke
@ 2026-11-26 14:12 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 07/14] nvme: parse dhchap keys during option parsing Hannes Reinecke
` (8 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
The current interface of having to specify the keys in plaintext is
quite awkward, and doesn't allow for the use of pre-provisioned keys.
This patch introduces a new key type 'dhchap' to store the DH-HMAC-CHAP
keys and modify handling functions to use 'struct key'.
The now unused 'struct nvme_dhchap_key' definitions are dropped.
The 'dhchap' keys are stored with a random UUID as description, and
a ->read callback is implemented to generated the encoded data string
as defined in the NVME base specification.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/Kconfig | 1 +
drivers/nvme/common/auth.c | 197 ++++++++++++++++-------------------------
drivers/nvme/common/keyring.c | 85 ++++++++++++++++++
drivers/nvme/host/Kconfig | 1 -
drivers/nvme/host/auth.c | 26 ++++--
drivers/nvme/host/nvme.h | 4 +-
drivers/nvme/host/sysfs.c | 24 +++--
drivers/nvme/target/Kconfig | 1 -
drivers/nvme/target/auth.c | 52 +++++------
drivers/nvme/target/configfs.c | 4 +-
drivers/nvme/target/nvmet.h | 8 +-
include/linux/nvme-auth.h | 16 +---
include/linux/nvme-keyring.h | 17 ++++
13 files changed, 244 insertions(+), 192 deletions(-)
diff --git a/drivers/nvme/common/Kconfig b/drivers/nvme/common/Kconfig
index 24c893c71d86527c1cdf3adace2151f2894afbb6..c6ed92c61c1bdf5fda6d4bb7807d4118b601dd6f 100644
--- a/drivers/nvme/common/Kconfig
+++ b/drivers/nvme/common/Kconfig
@@ -12,6 +12,7 @@ config NVME_AUTH
select CRYPTO_DH_RFC7919_GROUPS
select CRYPTO_LIB_SHA256
select CRYPTO_LIB_SHA512
+ select NVME_KEYRING
config NVME_AUTH_KUNIT_TEST
tristate "KUnit tests for NVMe authentication" if !KUNIT_ALL_TESTS
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index 308af3d7c7383acbfb327921c8a650898f0e15ea..ce16e7bf0c0edb892386dda97aacca0e758d3324 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -13,6 +13,8 @@
#include <crypto/sha2.h>
#include <linux/nvme.h>
#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+#include <keys/user-type.h>
static u32 nvme_dhchap_seqnum;
static DEFINE_MUTEX(nvme_dhchap_mutex);
@@ -138,89 +140,29 @@ size_t nvme_auth_hmac_hash_len(u8 hmac_id)
}
EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
-u32 nvme_auth_key_struct_size(u32 key_len)
-{
- struct nvme_dhchap_key key;
-
- return struct_size(&key, key, key_len);
-}
-EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size);
-
-struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash)
+/**
+ * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
+ *
+ * @secret: key data
+ * @secret_len: length of @secret
+ *
+ * Extracts a dhchap key from @secret.
+ *
+ * Returns the dhchap key or an error pointer on failure.
+ */
+struct key *nvme_auth_extract_key(const char *secret, size_t secret_len)
{
- struct nvme_dhchap_key *key;
- const char *p;
- u32 crc;
- int ret, key_len;
- size_t allocated_len = strlen(secret);
-
- /* Secret might be affixed with a ':' */
- p = strrchr(secret, ':');
- if (p)
- allocated_len = p - secret;
- key = nvme_auth_alloc_key(allocated_len, 0);
- if (!key)
- return ERR_PTR(-ENOMEM);
-
- key_len = base64_decode(secret, allocated_len, key->key, true, BASE64_STD);
- if (key_len < 0) {
- pr_debug("base64 key decoding error %d\n",
- key_len);
- ret = key_len;
- goto out_free_key;
- }
-
- if (key_len != 36 && key_len != 52 &&
- key_len != 68) {
- pr_err("Invalid key len %d\n", key_len);
- ret = -EINVAL;
- goto out_free_key;
- }
-
- /* The last four bytes is the CRC in little-endian format */
- key_len -= 4;
- /*
- * The linux implementation doesn't do pre- and post-increments,
- * so we have to do it manually.
- */
- crc = ~crc32(~0, key->key, key_len);
+ struct key *key;
- if (get_unaligned_le32(key->key + key_len) != crc) {
- pr_err("key crc mismatch (key %08x, crc %08x)\n",
- get_unaligned_le32(key->key + key_len), crc);
- ret = -EKEYREJECTED;
- goto out_free_key;
+ key = nvme_dhchap_psk_create(secret, secret_len);
+ if (!IS_ERR(key)) {
+ pr_debug("generated dhchap key %s\n",
+ key->description);
}
- key->len = key_len;
- key->hash = key_hash;
return key;
-out_free_key:
- nvme_auth_free_key(key);
- return ERR_PTR(ret);
}
EXPORT_SYMBOL_GPL(nvme_auth_extract_key);
-struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash)
-{
- u32 num_bytes = nvme_auth_key_struct_size(len);
- struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL);
-
- if (key) {
- key->len = len;
- key->hash = hash;
- }
- return key;
-}
-EXPORT_SYMBOL_GPL(nvme_auth_alloc_key);
-
-void nvme_auth_free_key(struct nvme_dhchap_key *key)
-{
- if (!key)
- return;
- kfree_sensitive(key);
-}
-EXPORT_SYMBOL_GPL(nvme_auth_free_key);
-
/*
* Start computing an HMAC value, given the algorithm ID and raw key.
*
@@ -317,11 +259,13 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
return -EINVAL;
}
-int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
+int nvme_auth_transform_key(struct key *key, const char *nqn,
u8 **transformed_secret)
{
struct nvme_auth_hmac_ctx hmac;
- u8 *key_data;
+ long key_len = 0;
+ u8 *transformed_data;
+ u8 *key_data, key_hash;
size_t transformed_len;
int ret;
@@ -329,28 +273,66 @@ int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
pr_warn("No key specified\n");
return -ENOKEY;
}
- if (key->hash == 0) {
- key_data = kzalloc(key->len, GFP_KERNEL);
- if (!key_data)
- return -ENOMEM;
- memcpy(key_data, key->key, key->len);
- *transformed_secret = key_data;
- return key->len;
- }
- ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
- if (ret)
+ down_read(&key->sem);
+ ret = key_validate(key);
+ if (ret) {
+ pr_warn("%s: key %08x invalidated\n",
+ __func__, key_serial(key));
+ up_read(&key->sem);
return ret;
- transformed_len = nvme_auth_hmac_hash_len(key->hash);
- key_data = kzalloc(transformed_len, GFP_KERNEL);
+ }
+ key_len = user_read(key, NULL, 0);
+ if (key_len <= 0) {
+ pr_warn("failed to get length from key %08x: error %ld\n",
+ key_serial(key), key_len);
+ up_read(&key->sem);
+ return key_len < 0 ? key_len : -ENOKEY;
+ }
+
+ key_data = kzalloc(key_len, GFP_KERNEL);
if (!key_data) {
- memzero_explicit(&hmac, sizeof(hmac));
+ up_read(&key->sem);
return -ENOMEM;
}
+ ret = user_read(key, (char *)key_data, key_len);
+ key_hash = nvme_dhchap_psk_hash(key);
+ up_read(&key->sem);
+ if (ret != key_len) {
+ if (ret < 0) {
+ pr_warn("failed to read from key %08x: error %d\n",
+ key_serial(key), ret);
+ } else {
+ pr_warn("only read %d of %ld bytes from key %08x\n",
+ ret, key_len, key_serial(key));
+ ret = -ENOKEY;
+ }
+ goto out_free_data;
+ }
+ if (key_hash == 0) {
+ *transformed_secret = key_data;
+ return key_len;
+ }
+
+ transformed_len = nvme_auth_hmac_hash_len(key_hash);
+ transformed_data = kzalloc(transformed_len, GFP_KERNEL);
+ if (!transformed_data) {
+ ret = -ENOMEM;
+ goto out_free_data;
+ }
+
+ ret = nvme_auth_hmac_init(&hmac, key_hash, key_data, key_len);
+ if (ret) {
+ kfree_sensitive(transformed_data);
+ goto out_free_data;
+ }
nvme_auth_hmac_update(&hmac, nqn, strlen(nqn));
nvme_auth_hmac_update(&hmac, "NVMe-over-Fabrics", 17);
- nvme_auth_hmac_final(&hmac, key_data);
- *transformed_secret = key_data;
- return transformed_len;
+ nvme_auth_hmac_final(&hmac, transformed_data);
+ *transformed_secret = transformed_data;
+ ret = transformed_len;
+out_free_data:
+ kfree_sensitive(key_data);
+ return ret;
}
EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
@@ -488,33 +470,6 @@ int nvme_auth_gen_session_key(struct crypto_kpp *dh_tfm,
}
EXPORT_SYMBOL_GPL(nvme_auth_gen_session_key);
-int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key)
-{
- struct nvme_dhchap_key *key;
- u8 key_hash;
- int offset = 0;
-
- if (!secret) {
- *ret_key = NULL;
- return 0;
- }
-
- if (sscanf(secret, "DHHC-1:%hhd:%n", &key_hash, &offset) != 1 ||
- !offset)
- return -EINVAL;
-
- /* Pass in the secret without the 'DHHC-1:XX:' prefix */
- key = nvme_auth_extract_key(secret + offset, key_hash);
- if (IS_ERR(key)) {
- *ret_key = NULL;
- return PTR_ERR(key);
- }
-
- *ret_key = key;
- return 0;
-}
-EXPORT_SYMBOL_GPL(nvme_auth_parse_key);
-
/**
* nvme_auth_generate_psk - Generate a PSK for TLS
* @hmac_id: Hash function identifier
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 0fc6b4825c2dd9a23daaf4aae3d9f63f4f07c164..dc0a4fb9296f1d937992028f0520f7a184eec6ae 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -464,6 +464,91 @@ static struct key_type nvme_dhchap_psk_key_type = {
.read = nvme_dhchap_psk_read,
};
+/**
+ * nvme_dhchap_psk_create - Create DH-HMAC-CHAP key
+ * @data: Key data
+ * @data_len: Length of @data
+ *
+ * Creates a new DH-HMAC-CHAP key 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)
+{
+ key_perm_t keyperm =
+ KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
+ KEY_POS_WRITE | KEY_POS_LINK | KEY_POS_SETATTR |
+ KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
+ char *identity;
+ key_ref_t keyref;
+ struct key *key;
+ uuid_t key_uuid;
+
+ if (data == NULL || !data_len)
+ return ERR_PTR(-EINVAL);
+
+ generate_random_uuid(key_uuid.b);
+ identity = kasprintf(GFP_KERNEL, "%pU", &key_uuid);
+ if (!identity)
+ return ERR_PTR(-ENOMEM);
+
+ pr_debug("generate dhchap psk '%s'\n", identity);
+ keyref = key_create(make_key_ref(nvme_keyring, true),
+ "dhchap", identity, data, data_len,
+ keyperm, KEY_ALLOC_NOT_IN_QUOTA |
+ KEY_ALLOC_BUILT_IN |
+ KEY_ALLOC_BYPASS_RESTRICTION);
+ if (IS_ERR(keyref)) {
+ pr_warn("refresh dhchap psk '%s' failed, error %ld\n",
+ identity, PTR_ERR(keyref));
+ kfree(identity);
+ return ERR_PTR(-ENOKEY);
+ }
+ key = key_ref_to_ptr(keyref);
+ pr_debug("generated dhchap psk %08x\n", key_serial(key));
+ kfree(identity);
+ return key;
+}
+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.
+ * Returns the key or an error pointer if not found.
+ */
+struct key *nvme_dhchap_psk_lookup(const char *identity)
+{
+ key_ref_t keyref;
+
+ pr_debug("lookup dhchap psk '%s'\n", identity);
+
+ keyref = keyring_search(make_key_ref(nvme_keyring, true),
+ &nvme_dhchap_psk_key_type,
+ identity, false);
+ if (IS_ERR(keyref)) {
+ pr_debug("lookup dhchap psk '%s' failed, error %ld\n",
+ identity, PTR_ERR(keyref));
+ return ERR_PTR(-ENOKEY);
+ }
+
+ return key_ref_to_ptr(keyref);
+}
+EXPORT_SYMBOL_GPL(nvme_dhchap_psk_lookup);
+
+u8 nvme_dhchap_psk_hash(struct key *key)
+{
+ const struct user_key_payload *upayload;
+ u8 hmac;
+
+ if (!key)
+ return 0;
+ upayload = user_key_payload_locked(key);
+ hmac = upayload->data[upayload->datalen + 5];
+ return hmac;
+}
+EXPORT_SYMBOL_GPL(nvme_dhchap_psk_hash);
+
static int __init nvme_keyring_init(void)
{
int err;
diff --git a/drivers/nvme/host/Kconfig b/drivers/nvme/host/Kconfig
index 31974c7dd20c9174961eac91e3823e939d325daa..6d350ef617bfb47770ebce48c3abb25a4b6a4c8c 100644
--- a/drivers/nvme/host/Kconfig
+++ b/drivers/nvme/host/Kconfig
@@ -115,7 +115,6 @@ config NVME_HOST_AUTH
bool "NVMe over Fabrics In-Band Authentication in host side"
depends on NVME_CORE
select NVME_AUTH
- select NVME_KEYRING
help
This provides support for NVMe over Fabrics In-Band Authentication in
host side.
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 06399c075405353325b05c099811a54134fbeb53..d6abdb4f1e21c77761f4eb00723fc9d853428295 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -1037,13 +1037,21 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
if (!ctrl->opts)
return 0;
- ret = nvme_auth_parse_key(ctrl->opts->dhchap_secret, &ctrl->host_key);
- if (ret)
+ 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;
- ret = nvme_auth_parse_key(ctrl->opts->dhchap_ctrl_secret,
- &ctrl->ctrl_key);
- if (ret)
+ }
+
+ 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;
+ }
if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
return 0;
@@ -1064,10 +1072,10 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
return 0;
err_free_dhchap_ctrl_secret:
- nvme_auth_free_key(ctrl->ctrl_key);
+ key_put(ctrl->ctrl_key);
ctrl->ctrl_key = NULL;
err_free_dhchap_secret:
- nvme_auth_free_key(ctrl->host_key);
+ key_put(ctrl->host_key);
ctrl->host_key = NULL;
return ret;
}
@@ -1089,11 +1097,11 @@ void nvme_auth_free(struct nvme_ctrl *ctrl)
kvfree(ctrl->dhchap_ctxs);
}
if (ctrl->host_key) {
- nvme_auth_free_key(ctrl->host_key);
+ key_put(ctrl->host_key);
ctrl->host_key = NULL;
}
if (ctrl->ctrl_key) {
- nvme_auth_free_key(ctrl->ctrl_key);
+ key_put(ctrl->ctrl_key);
ctrl->ctrl_key = NULL;
}
}
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 2543e52d6358a635623fab925167bea7ac4a24a2..434e3612c996a969075aa5844c51906c605e6f56 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -461,8 +461,8 @@ struct nvme_ctrl {
struct work_struct dhchap_auth_work;
struct mutex dhchap_auth_mutex;
struct nvme_dhchap_queue_context *dhchap_ctxs;
- struct nvme_dhchap_key *host_key;
- struct nvme_dhchap_key *ctrl_key;
+ struct key *host_key;
+ struct key *ctrl_key;
u16 transaction;
#endif
key_serial_t tls_pskid;
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index c1375f23a97f6b5a47bb8aad6b12e7ff3ad12638..e7e4fe2ad506ac30ba88a964c52d0dd3a995d75b 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -837,8 +837,6 @@ static ssize_t dhchap_secret_store(struct device *dev,
return -EINVAL;
if (count < 7)
return -EINVAL;
- if (memcmp(buf, "DHHC-1:", 7))
- return -EINVAL;
dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
if (!dhchap_secret)
@@ -846,13 +844,12 @@ static ssize_t dhchap_secret_store(struct device *dev,
memcpy(dhchap_secret, buf, count);
nvme_auth_stop(ctrl);
if (strcmp(dhchap_secret, opts->dhchap_secret)) {
- struct nvme_dhchap_key *key, *host_key;
- int ret;
+ struct key *key, *host_key;
- ret = nvme_auth_parse_key(dhchap_secret, &key);
- if (ret) {
+ key = nvme_auth_extract_key(dhchap_secret, count);
+ if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
- return ret;
+ return PTR_ERR(key);
}
kfree_sensitive(opts->dhchap_secret);
opts->dhchap_secret = dhchap_secret;
@@ -860,7 +857,7 @@ static ssize_t dhchap_secret_store(struct device *dev,
mutex_lock(&ctrl->dhchap_auth_mutex);
ctrl->host_key = key;
mutex_unlock(&ctrl->dhchap_auth_mutex);
- nvme_auth_free_key(host_key);
+ key_put(host_key);
} else
kfree_sensitive(dhchap_secret);
/* Start re-authentication */
@@ -903,13 +900,12 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
memcpy(dhchap_secret, buf, count);
nvme_auth_stop(ctrl);
if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
- struct nvme_dhchap_key *key, *ctrl_key;
- int ret;
+ struct key *key, *ctrl_key;
- ret = nvme_auth_parse_key(dhchap_secret, &key);
- if (ret) {
+ key = nvme_auth_extract_key(dhchap_secret, count);
+ if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
- return ret;
+ return PTR_ERR(key);
}
kfree_sensitive(opts->dhchap_ctrl_secret);
opts->dhchap_ctrl_secret = dhchap_secret;
@@ -917,7 +913,7 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
mutex_lock(&ctrl->dhchap_auth_mutex);
ctrl->ctrl_key = key;
mutex_unlock(&ctrl->dhchap_auth_mutex);
- nvme_auth_free_key(ctrl_key);
+ key_put(ctrl_key);
} else
kfree_sensitive(dhchap_secret);
/* Start re-authentication */
diff --git a/drivers/nvme/target/Kconfig b/drivers/nvme/target/Kconfig
index 69bde270115e29bc8c89f4f58791190932dbe852..700e87d9e610e2532a871ad8af90573a1931c30b 100644
--- a/drivers/nvme/target/Kconfig
+++ b/drivers/nvme/target/Kconfig
@@ -5,7 +5,6 @@ config NVME_TARGET
depends on BLOCK
select CONFIGFS_FS
select NVME_KEYRING if NVME_TARGET_TCP_TLS
- select KEYS if NVME_TARGET_TCP_TLS
select SGL_ALLOC
help
This enabled target side support for the NVMe protocol, that is
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 7ebd867c89468e94e31fd2739d3ee77018775f18..e1d92f5455219fd9e1845c2dca569fbb4d6450c5 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -150,7 +150,10 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
int ret = 0;
struct nvmet_host_link *p;
struct nvmet_host *host = NULL;
- struct nvme_dhchap_key *host_key, *ctrl_key = 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))
@@ -188,42 +191,39 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
goto out_unlock;
}
- /*
- * Skip the 'DHHC-1:XX:' prefix. Keep the old keys until the new ones
- * are there: a controller without a host key needs no authentication.
- */
- host_key = nvme_auth_extract_key(host->dhchap_secret +
- host->dhchap_key_offset,
- host->dhchap_key_hash);
+ host_key = nvme_auth_extract_key(host->dhchap_secret,
+ strlen(host->dhchap_secret));
if (IS_ERR(host_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 +
- host->dhchap_ctrl_key_offset,
- host->dhchap_ctrl_key_hash);
+ ctrl_key = nvme_auth_extract_key(host->dhchap_ctrl_secret,
+ strlen(host->dhchap_ctrl_secret));
if (IS_ERR(ctrl_key)) {
- nvme_auth_free_key(host_key);
+ key_put(host_key);
ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
goto out_unlock;
}
}
- nvme_auth_free_key(ctrl->host_key);
+ key_put(ctrl->host_key);
ctrl->host_key = host_key;
- nvme_auth_free_key(ctrl->ctrl_key);
+ key_put(ctrl->ctrl_key);
ctrl->ctrl_key = ctrl_key;
ctrl->shash_id = host->dhchap_hash_id;
#ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
- pr_debug("%s: using hash %s key %*ph\n", __func__,
- ctrl->host_key->hash > 0 ?
- nvme_auth_hmac_name(ctrl->host_key->hash) : "none",
- (int)ctrl->host_key->len, ctrl->host_key->key);
- if (ctrl->ctrl_key)
- pr_debug("%s: using ctrl hash %s key %*ph\n", __func__,
- ctrl->ctrl_key->hash > 0 ?
- nvme_auth_hmac_name(ctrl->ctrl_key->hash) : "none",
- (int)ctrl->ctrl_key->len, ctrl->ctrl_key->key);
+ 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",
+ 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",
+ key_serial(ctrl->ctrl_key));
+ }
#endif
out_unlock:
up_read(&nvmet_config_sem);
@@ -263,11 +263,13 @@ void nvmet_destroy_auth(struct nvmet_ctrl *ctrl)
ctrl->dh_key = NULL;
if (ctrl->host_key) {
- nvme_auth_free_key(ctrl->host_key);
+ key_revoke(ctrl->host_key);
+ key_put(ctrl->host_key);
ctrl->host_key = NULL;
}
if (ctrl->ctrl_key) {
- nvme_auth_free_key(ctrl->ctrl_key);
+ key_revoke(ctrl->ctrl_key);
+ key_put(ctrl->ctrl_key);
ctrl->ctrl_key = NULL;
}
#ifdef CONFIG_NVME_TARGET_TCP_TLS
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 27c50fa533e70e443a2eb3fed5e31e730fb0ce3c..ff3a8d74de59966952a3991542077feebae10770 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -2234,7 +2234,7 @@ static struct config_group nvmet_ports_group;
static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
char *page)
{
- u8 *dhchap_secret;
+ char *dhchap_secret;
ssize_t ret;
down_read(&nvmet_config_sem);
@@ -2267,7 +2267,7 @@ CONFIGFS_ATTR(nvmet_host_, dhchap_key);
static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
char *page)
{
- u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
+ char *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
ssize_t ret;
down_read(&nvmet_config_sem);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index e33884a81efc9f96cdc5d0120e0701f06ce7d873..1dfdac159dab044566aac47862665784feddec0a 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -309,8 +309,8 @@ struct nvmet_ctrl {
bool pi_support;
bool concat;
#ifdef CONFIG_NVME_TARGET_AUTH
- struct nvme_dhchap_key *host_key;
- struct nvme_dhchap_key *ctrl_key;
+ struct key *host_key;
+ struct key *ctrl_key;
u8 shash_id;
struct crypto_kpp *dh_tfm;
u8 dh_gid;
@@ -399,8 +399,8 @@ static inline struct nvmet_subsys *namespaces_to_subsys(
struct nvmet_host {
struct config_group group;
- u8 *dhchap_secret;
- u8 *dhchap_ctrl_secret;
+ char *dhchap_secret;
+ char *dhchap_ctrl_secret;
int dhchap_key_offset;
int dhchap_ctrl_key_offset;
u8 dhchap_key_hash;
diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
index 1c5ce635b169dd0747c1344e9adcc14839776cd1..ac618b82585ec8d81f1aa92823821488eba32a32 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -9,12 +9,6 @@
#include <crypto/kpp.h>
#include <crypto/sha2.h>
-struct nvme_dhchap_key {
- size_t len;
- u8 hash;
- u8 key[] __counted_by(len);
-};
-
u32 nvme_auth_get_seqnum(void);
const char *nvme_auth_dhgroup_name(u8 dhgroup_id);
const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id);
@@ -37,13 +31,9 @@ void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data,
size_t data_len);
void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out);
-u32 nvme_auth_key_struct_size(u32 key_len);
-struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash);
-void nvme_auth_free_key(struct nvme_dhchap_key *key);
-struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash);
-int nvme_auth_transform_key(const struct nvme_dhchap_key *key,
- const char *nqn, u8 **transformed_secret);
-int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key);
+struct key *nvme_auth_extract_key(const char *secret, size_t secret_len);
+int nvme_auth_transform_key(struct key *key, const char *nqn,
+ u8 **transformed_secret);
int nvme_auth_augmented_challenge(u8 hmac_id, const u8 *skey, size_t skey_len,
const u8 *challenge, u8 *aug, size_t hlen);
int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid);
diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
index 680eb93088d485ca3368f615715d71fa570b1077..d50dc2c514bcecfab24e24fc2e7aa1cbbbc6c1d2 100644
--- a/include/linux/nvme-keyring.h
+++ b/include/linux/nvme-keyring.h
@@ -20,6 +20,10 @@ int nvme_keyring_link(struct key *keyring);
int nvme_keyring_unlink(struct key *keyring);
struct key *nvme_tls_key_lookup(key_serial_t key_id);
+struct key *nvme_dhchap_psk_create(const char *data, size_t data_len);
+struct key *nvme_dhchap_psk_lookup(const char *identity);
+u8 nvme_dhchap_psk_hash(struct key *key);
+
#else
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
const char *hostnqn, const char *subnqn, u8 hmac_id,
@@ -52,5 +56,18 @@ static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
return ERR_PTR(-EOPNOTSUPP);
}
+static inline struct key *nvme_dhchap_psk_create(const char *data,
+ size_t data_len)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_lookup(const char *identity)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+static inline u8 nvme_dhchap_psk_hash(struct key *key)
+{
+ return 0;
+}
#endif /* !CONFIG_NVME_KEYRING */
#endif /* _NVME_KEYRING_H */
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 07/14] nvme: parse dhchap keys during option parsing
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (5 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 06/14] nvme-auth: switch to use 'struct key' Hannes Reinecke
@ 2026-11-26 14:12 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
` (7 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
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 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@kernel.org>
---
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@suse.de>
@@ -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
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (6 preceding siblings ...)
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
2026-11-26 14:12 ` [PATCH v7 09/14] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
` (6 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
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
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 09/14] nvme: allow to pass in key description as dhchap secret
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (7 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
@ 2026-11-26 14:12 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 10/14] nvme-auth: allow to specify the key by key serial Hannes Reinecke
` (5 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
In order to use pre-populated keys update 'nvme_auth_extract_key()'
to accept a key description argument in addition to the currently
implemented raw key data. To mark this change rename the 'secret'
parameter of that function to 'input', and add a function description
to document the usage. Also add a 'generated' flag to distinguish
whether the keys have been auto-generated (which should be revoked
after use) or pre-populated (which should not be revoked).
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/auth.c | 23 ++++++++++++++++-----
drivers/nvme/host/auth.c | 15 ++++++++++++++
drivers/nvme/host/fabrics.c | 30 +++++++++++++++++++++++++--
drivers/nvme/host/fabrics.h | 4 ++++
drivers/nvme/host/nvme.h | 2 ++
drivers/nvme/host/sysfs.c | 47 ++++++++++++++++++++++++++++++------------
drivers/nvme/target/auth.c | 32 ++++++++++++++++++----------
drivers/nvme/target/configfs.c | 12 +++++++----
drivers/nvme/target/nvmet.h | 2 ++
include/linux/nvme-auth.h | 3 ++-
10 files changed, 134 insertions(+), 36 deletions(-)
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index ce16e7bf0c0edb892386dda97aacca0e758d3324..15e72fe8868af5698809cd6634a1f1f1da11ca2d 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -143,19 +143,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
/**
* nvme_auth_extract_key - extract the DH-HMAC-CHAP key
*
- * @secret: key data
- * @secret_len: length of @secret
+ * @input: key description or key data
+ * @input_len: length of @input
+ * @generated: indicate whether a key has been generated
*
- * Extracts a dhchap key from @secret.
+ * Extracts or generates a key from @input.
+ * @input can either be a key description or raw key data; in
+ * the latter case a key is generated from @input and
+ * @generated is set to 'true'.
*
* Returns the dhchap key or an error pointer on failure.
*/
-struct key *nvme_auth_extract_key(const char *secret, size_t secret_len)
+struct key *nvme_auth_extract_key(const char *input, size_t input_len,
+ bool *generated)
{
struct key *key;
- key = nvme_dhchap_psk_create(secret, secret_len);
+ /* Check if @input is a key description */
+ key = nvme_dhchap_psk_lookup(input);
if (!IS_ERR(key)) {
+ *generated = false;
+ return key;
+ }
+ /* Generate a key from @input data */
+ key = nvme_dhchap_psk_create(input, input_len);
+ if (!IS_ERR(key)) {
+ *generated = true;
pr_debug("generated dhchap key %s\n",
key->description);
}
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 285aa1c71f1026d17408f64500c7b41007c9d493..37307e64a4925c085f5393afbb1359780404ad71 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -1031,11 +1031,21 @@ static void nvme_auth_clear_key(struct nvme_ctrl *ctrl, bool is_ctrl)
if (is_ctrl) {
key = ctrl->ctrl_key;
ctrl->ctrl_key = NULL;
+ generated = ctrl->ctrl_key_generated;
+ ctrl->ctrl_key_generated = false;
} else {
key = ctrl->host_key;
ctrl->host_key = NULL;
+ generated = ctrl->host_key_generated;
+ ctrl->host_key_generated = false;
}
if (key) {
+ if (generated) {
+ dev_dbg(ctrl->device, "%s: revoke dhchap%s key %08x\n",
+ __func__, is_ctrl ? " ctrl" : " host",
+ key_serial(key));
+ key_revoke(key);
+ }
dev_dbg(ctrl->device, "%s: drop dhchap%s key %08x\n",
__func__, is_ctrl ? " ctrl" : " host",
key_serial(key));
@@ -1050,6 +1060,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
{
struct nvme_dhchap_queue_context *chap;
struct key *host_key, *ctrl_key = NULL;
+ bool host_key_generated, ctrl_key_generated;
int i, ret;
mutex_init(&ctrl->dhchap_auth_mutex);
@@ -1077,6 +1088,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
key_put(host_key);
return -ENOKEY;
}
+ host_key_generated = ctrl->opts->dhchap_key_generated;
if (ctrl->opts->dhchap_ctrl_key) {
ctrl_key = key_lookup(ctrl->opts->dhchap_ctrl_key);
@@ -1096,6 +1108,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
key_put(host_key);
return -EKEYREVOKED;
}
+ ctrl_key_generated = ctrl->opts->dhchap_ctrl_key_generated;
}
pr_debug("%s: using dhchap host key %08x\n",
@@ -1103,6 +1116,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
if (ctrl->host_key)
nvme_auth_clear_host_key(ctrl);
ctrl->host_key = host_key;
+ ctrl->host_key_generated = host_key_generated;
if (ctrl->ctrl_key)
nvme_auth_clear_ctrl_key(ctrl);
@@ -1110,6 +1124,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
pr_debug("%s: using dhchap ctrl key %08x\n",
__func__, key_serial(ctrl_key));
ctrl->ctrl_key = ctrl_key;
+ ctrl->ctrl_key_generated = ctrl_key_generated;
}
ctrl->dhchap_ctxs = kvzalloc_objs(*chap, ctrl_max_dhchaps(ctrl));
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 6c453b4f5cab1546fb036236bbfa8682a8611adb..bba6a4848f2271beee5a4b051419fcb1e703211f 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -742,7 +742,9 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
opts->keyring = NULL;
opts->concat = false;
opts->dhchap_key = 0;
+ opts->dhchap_key_generated = false;
opts->dhchap_ctrl_key = 0;
+ opts->dhchap_ctrl_key_generated = false;
options = o = kstrdup(buf, GFP_KERNEL);
if (!options)
@@ -1094,7 +1096,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
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));
+ key = nvme_auth_extract_key(host_secret, strlen(host_secret),
+ &opts->dhchap_key_generated);
if (IS_ERR(key)) {
ret = PTR_ERR(key);
goto out;
@@ -1111,7 +1114,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
goto out;
}
pr_debug("lookup dhchap ctrl secret '%s'\n", ctrl_secret);
- key = nvme_auth_extract_key(ctrl_secret, strlen(ctrl_secret));
+ key = nvme_auth_extract_key(ctrl_secret, strlen(ctrl_secret),
+ &opts->dhchap_ctrl_key_generated);
if (IS_ERR(key)) {
ret = PTR_ERR(key);
goto out;
@@ -1326,7 +1330,29 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
void nvmf_free_options(struct nvmf_ctrl_options *opts)
{
+ struct key *key;
+
nvmf_host_put(opts->host);
+ if (opts->dhchap_key &&
+ opts->dhchap_key_generated) {
+ key = key_lookup(opts->dhchap_key);
+ if (!IS_ERR(key)) {
+ pr_debug("revoke dhchap host key %08x\n",
+ opts->dhchap_key);
+ key_revoke(key);
+ key_put(key);
+ }
+ }
+ if (opts->dhchap_ctrl_key &&
+ opts->dhchap_ctrl_key_generated) {
+ key = key_lookup(opts->dhchap_ctrl_key);
+ if (!IS_ERR(key)) {
+ pr_debug("revoke dhchap ctrl key %08x\n",
+ opts->dhchap_ctrl_key);
+ key_revoke(key);
+ key_put(key);
+ }
+ }
if (opts->keyring) {
int ret = nvme_keyring_unlink(opts->keyring);
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index 49a6f6b6833a852a55e3bd3a3fbf3f1ca3e8c5d0..f2cce812362e2e9ec2f35837f16fbdf11ee87c35 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -99,6 +99,8 @@ enum {
* @dhchap_key: DH-HMAC-CHAP pre-shared key
* @dhchap_ctrl_key: DH-HMAC-CHAP controller pre-shared key for bi-directional
* authentication
+ * @dhchap_key_generated: True if the @dhchap_key has been auto-generated
+ * @dhchap_ctrl_key_generated: True if @dhchap_ctrl_key has been auto-generated
* @keyring: Keyring to use for key lookups
* @tls_key: TLS key for encrypted connections (TCP)
* @tls: Start TLS encrypted connections (TCP)
@@ -129,6 +131,8 @@ struct nvmf_ctrl_options {
struct nvmf_host *host;
key_serial_t dhchap_key;
key_serial_t dhchap_ctrl_key;
+ bool dhchap_key_generated;
+ bool dhchap_ctrl_key_generated;
struct key *keyring;
struct key *tls_key;
bool tls;
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index 434e3612c996a969075aa5844c51906c605e6f56..1161f94cca19b1e223ebc4e2894c6e3ae7d6779a 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -464,6 +464,8 @@ struct nvme_ctrl {
struct key *host_key;
struct key *ctrl_key;
u16 transaction;
+ bool host_key_generated;
+ bool ctrl_key_generated;
#endif
key_serial_t tls_pskid;
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 27f06394af83601ae4ad4a06f3f235d48c5a73d9..ab912a4b281c717ca569062c19850d305f20500d 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -833,13 +833,14 @@ static ssize_t dhchap_secret_show(struct device *dev,
down_read(&key->sem);
if (key_validate(key))
count = sysfs_emit(buf, "<invalidated>\n");
- else {
+ else if (ctrl->host_key_generated) {
count = key->type->read(key, buf, PAGE_SIZE);
if (count > 0) {
buf[count] = '\n';
count++;
}
- }
+ } else
+ count = sysfs_emit(buf, "%s\n", key->description);
up_read(&key->sem);
mutex_unlock(&ctrl->dhchap_auth_mutex);
return count;
@@ -851,6 +852,7 @@ static ssize_t dhchap_secret_store(struct device *dev,
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct key *key, *old_key;
char *dhchap_secret;
+ bool generated = false;
size_t len;
int ret;
@@ -864,7 +866,8 @@ static ssize_t dhchap_secret_store(struct device *dev,
memcpy(dhchap_secret, buf, len);
nvme_auth_stop(ctrl);
- key = nvme_auth_extract_key(dhchap_secret, len);
+ key = nvme_auth_extract_key(dhchap_secret, len,
+ &generated);
if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
return PTR_ERR(key);
@@ -875,7 +878,11 @@ static ssize_t dhchap_secret_store(struct device *dev,
if (ret) {
dev_warn(ctrl->device, "key %08x invalidated\n",
key_serial(key));
- key_revoke(key);
+ if (generated) {
+ dev_dbg(ctrl->device, "revoke host key %08x\n",
+ key_serial(key));
+ key_revoke(key);
+ }
key_put(key);
kfree_sensitive(dhchap_secret);
return ret;
@@ -885,10 +892,15 @@ static ssize_t dhchap_secret_store(struct device *dev,
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 {
+ if (ctrl->host_key_generated) {
+ dev_dbg(ctrl->device, "revoke host key %08x\n",
+ key_serial(old_key));
+ key_revoke(old_key);
+ }
ctrl->host_key = key;
+ ctrl->host_key_generated = generated;
}
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key);
@@ -915,16 +927,18 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
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 {
+ else if (ctrl->ctrl_key_generated) {
count = key->type->read(key, buf, PAGE_SIZE);
if (count > 0) {
buf[count] = '\n';
count++;
}
- }
+ } else
+ count = sysfs_emit(buf, "%s\n", key->description);
up_read(&key->sem);
mutex_unlock(&ctrl->dhchap_auth_mutex);
return count;
@@ -936,6 +950,7 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct key *key, *old_key;
char *dhchap_secret;
+ bool generated = false;
size_t len;
int ret;
@@ -948,7 +963,8 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
return -ENOMEM;
memcpy(dhchap_secret, buf, len);
nvme_auth_stop(ctrl);
- key = nvme_auth_extract_key(dhchap_secret, len);
+ key = nvme_auth_extract_key(dhchap_secret, len,
+ &generated);
if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
return PTR_ERR(key);
@@ -959,9 +975,11 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
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);
+ if (generated) {
+ dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
+ key_serial(key));
+ key_revoke(key);
+ }
key_put(key);
kfree_sensitive(dhchap_secret);
return ret;
@@ -973,10 +991,13 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
key_serial(old_key));
old_key = key;
} else {
- dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
+ if (ctrl->ctrl_key_generated) {
+ dev_dbg(ctrl->device, "revoke ctrl key %08x\n",
key_serial(old_key));
- key_revoke(old_key);
+ key_revoke(old_key);
+ }
ctrl->ctrl_key = key;
+ ctrl->ctrl_key_generated = generated;
}
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key);
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index ff0e8569d70675e80c1a8f18733a83317184026a..54e79c8a7cae67a509a9f6b993b7886e706af8e0 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -20,31 +20,38 @@
#include "nvmet.h"
static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
- struct key *new_key)
+ struct key *new_key, bool new_generated)
{
key_serial_t old_key_id = 0, new_key_id = key_serial(new_key);
struct key *key = NULL;
+ bool generated = false;
if (set_ctrl) {
if (host->dhchap_ctrl_key) {
if (host->dhchap_ctrl_key == new_key_id) {
- if (new_key)
+ if (new_key && new_generated)
key_revoke(new_key);
return;
}
old_key_id = host->dhchap_ctrl_key;
+ generated = host->dhchap_ctrl_key_generated;
+ host->dhchap_ctrl_key_generated = false;
}
host->dhchap_ctrl_key = new_key_id;
+ host->dhchap_key_generated = new_generated;
} else {
if (host->dhchap_key) {
if (host->dhchap_key == new_key_id) {
- if (new_key)
+ if (new_key && new_generated)
key_revoke(new_key);
return;
}
old_key_id = host->dhchap_key;
+ generated = host->dhchap_key_generated;
+ host->dhchap_key_generated = false;
}
host->dhchap_key = new_key_id;
+ host->dhchap_key_generated = new_generated;
}
if (!old_key_id)
return;
@@ -52,32 +59,35 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
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);
+ if (generated) {
+ 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);
+ nvmet_auth_update_key(host, false, NULL, false);
+ nvmet_auth_update_key(host, true, NULL, false);
}
int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool set_ctrl)
{
+ bool generated = false;
struct key *key;
size_t len;
if (!secret || !strlen(secret)) {
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, NULL);
+ nvmet_auth_update_key(host, set_ctrl, NULL, false);
up_write(&nvmet_config_sem);
return 0;
}
len = strcspn(secret, "\n");
- key = nvme_auth_extract_key(secret, len);
+ key = nvme_auth_extract_key(secret, len, &generated);
if (IS_ERR(key)) {
pr_debug("%s: invalid key specification\n", __func__);
return PTR_ERR(key);
@@ -92,7 +102,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
}
up_read(&key->sem);
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, key);
+ nvmet_auth_update_key(host, set_ctrl, key, generated);
up_write(&nvmet_config_sem);
key_put(key);
return 0;
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index f6bfda757f076293e6e3a2db4d4245ea3a754b40..3defa3f9484972265a5351ed08055735c859be74 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -2248,13 +2248,15 @@ static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
down_read(&key->sem);
if (key_validate(key))
ret = sprintf(page, "<invalidated>\n");
- else {
+ else if (host->dhchap_key_generated) {
ret = key->type->read(key, page, PAGE_SIZE);
if (ret > 0) {
page[ret] = '\n';
ret++;
}
- }
+ } else
+ ret = scnprintf(page, PAGE_SIZE,
+ "%s\n", key->description);
up_read(&key->sem);
key_put(key);
}
@@ -2305,13 +2307,15 @@ static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
down_read(&key->sem);
if (key_validate(key))
ret = sprintf(page, "<invalidated>\n");
- else {
+ else if (host->dhchap_ctrl_key_generated) {
ret = key->type->read(key, page, PAGE_SIZE);
if (ret > 0) {
page[ret] = '\n';
ret++;
}
- }
+ } else
+ ret = scnprintf(page, PAGE_SIZE,
+ "%s\n", key->description);
up_read(&key->sem);
key_put(key);
}
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index df84776b90f3e198f19f158e82ddddc473ee7296..f6848a2f4e4c7857dcd710522db80a24a13f3e3e 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -400,7 +400,9 @@ static inline struct nvmet_subsys *namespaces_to_subsys(
struct nvmet_host {
struct config_group group;
key_serial_t dhchap_key;
+ bool dhchap_key_generated;
key_serial_t dhchap_ctrl_key;
+ bool dhchap_ctrl_key_generated;
u8 dhchap_hash_id;
u8 dhchap_dhgroup_id;
};
diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
index ac618b82585ec8d81f1aa92823821488eba32a32..1f912a40aca3fab85498f85ba43059a427bb956f 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -31,7 +31,8 @@ void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data,
size_t data_len);
void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out);
-struct key *nvme_auth_extract_key(const char *secret, size_t secret_len);
+struct key *nvme_auth_extract_key(const char *input, size_t input_len,
+ bool *generated);
int nvme_auth_transform_key(struct key *key, const char *nqn,
u8 **transformed_secret);
int nvme_auth_augmented_challenge(u8 hmac_id, const u8 *skey, size_t skey_len,
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 10/14] nvme-auth: allow to specify the key by key serial
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (8 preceding siblings ...)
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 11/14] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
` (4 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
Update nvme_auth_extract_key() to accept a key serial
number in addition to the existing input formats.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/auth.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index 15e72fe8868af5698809cd6634a1f1f1da11ca2d..e336c698437dcea4d7ce4c435efda0b9f26eb63d 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -148,7 +148,7 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
* @generated: indicate whether a key has been generated
*
* Extracts or generates a key from @input.
- * @input can either be a key description or raw key data; in
+ * @input can be a key serial, a key description or raw key data; in
* the latter case a key is generated from @input and
* @generated is set to 'true'.
*
@@ -158,7 +158,18 @@ struct key *nvme_auth_extract_key(const char *input, size_t input_len,
bool *generated)
{
struct key *key;
-
+ unsigned long key_serial;
+ int err;
+
+ /* Check if @input is a key serial */
+ err = kstrtoul(input, 0, &key_serial);
+ if (!err) {
+ key = key_lookup(key_serial);
+ if (!IS_ERR(key)) {
+ *generated = false;
+ return key;
+ }
+ }
/* Check if @input is a key description */
key = nvme_dhchap_psk_lookup(input);
if (!IS_ERR(key)) {
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 11/14] nvme-auth: wait for authentication to finish when changing keys
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (9 preceding siblings ...)
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 12/14] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
` (3 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
When changing DH-HMAC-CHAP keys in sysfs we need to wait for authentication
to complete on all queues. Otherwise the user might change the keys while
authentication is in progress, causing the authentication to fail and
I/O to be interrupted.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
---
drivers/nvme/host/sysfs.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index ab912a4b281c717ca569062c19850d305f20500d..4ebc03a8ddcaede1344988dfe816bbaf3746bb55 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -908,7 +908,7 @@ static ssize_t dhchap_secret_store(struct device *dev,
/* Start re-authentication */
dev_info(ctrl->device, "re-authenticating controller\n");
queue_work(nvme_wq, &ctrl->dhchap_auth_work);
-
+ flush_work(&ctrl->dhchap_auth_work);
return count;
}
@@ -1005,7 +1005,7 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
/* Start re-authentication */
dev_info(ctrl->device, "re-authenticating controller\n");
queue_work(nvme_wq, &ctrl->dhchap_auth_work);
-
+ flush_work(&ctrl->dhchap_auth_work);
return count;
}
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 12/14] nvme-fabrics: allow to pass in keyring by name
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (10 preceding siblings ...)
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring Hannes Reinecke
` (2 subsequent siblings)
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
The current interface of passing in keyrings by serial number is very
impractical for configuration scripts, as the serial number is not
persistent across reboots, and so any configuration files will need
to be adjusted.
This patchs allows to specify the keyring by name in addition to
the serial number to simplify configuration file handling.
And the keyring fabrics option is made available to all transports
as it is no longer TCP-specific.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/host/fabrics.c | 31 ++++++++++++++++++++++---------
drivers/nvme/host/sysfs.c | 33 ++++++++++++++++++++++++++++++++-
drivers/nvme/host/tcp.c | 2 +-
3 files changed, 55 insertions(+), 11 deletions(-)
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index bba6a4848f2271beee5a4b051419fcb1e703211f..12a30ed16f9ef32c74a51a0ae6ff8ce0ea628301 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -663,8 +663,8 @@ static struct key *nvmf_parse_key(int key_id)
{
struct key *key;
- if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
- pr_err("TLS is not supported\n");
+ if (!IS_ENABLED(CONFIG_NVME_KEYRING)) {
+ pr_err("nvme keyring is not supported\n");
return ERR_PTR(-EINVAL);
}
@@ -697,8 +697,10 @@ static const match_table_t opt_tokens = {
{ NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" },
{ NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" },
{ NVMF_OPT_TOS, "tos=%d" },
+#if IS_ENABLED(CONFIG_NVME_KEYRING)
+ { NVMF_OPT_KEYRING, "keyring=%s" },
+#endif
#ifdef CONFIG_NVME_TCP_TLS
- { NVMF_OPT_KEYRING, "keyring=%d" },
{ NVMF_OPT_TLS_KEY, "tls_key=%d" },
#endif
{ NVMF_OPT_FAIL_FAST_TMO, "fast_io_fail_tmo=%d" },
@@ -993,14 +995,24 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
opts->tos = token;
break;
case NVMF_OPT_KEYRING:
- if (match_int(args, &key_id) || key_id <= 0) {
- ret = -EINVAL;
+#if IS_ENABLED(CONFIG_NVME_KEYRING)
+ p = match_strdup(args);
+ if (!p) {
+ ret = -ENOMEM;
goto out;
}
- key = nvmf_parse_key(key_id);
+ key = request_key(&key_type_keyring, p, NULL);
+ kfree(p);
if (IS_ERR(key)) {
- ret = PTR_ERR(key);
- goto out;
+ if (match_int(args, &key_id) || key_id <= 0) {
+ ret = -EINVAL;
+ goto out;
+ }
+ key = nvmf_parse_key(key_id);
+ if (IS_ERR(key)) {
+ ret = PTR_ERR(key);
+ goto out;
+ }
}
ret = nvme_keyring_link(opts->keyring);
if (ret) {
@@ -1008,6 +1020,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
goto out;
}
opts->keyring = key;
+#endif
break;
case NVMF_OPT_TLS_KEY:
if (match_int(args, &key_id) || key_id <= 0) {
@@ -1376,7 +1389,7 @@ EXPORT_SYMBOL_GPL(nvmf_free_options);
NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
NVMF_OPT_DISABLE_SQFLOW | NVMF_OPT_DISCOVERY |\
NVMF_OPT_FAIL_FAST_TMO | NVMF_OPT_DHCHAP_SECRET |\
- NVMF_OPT_DHCHAP_CTRL_SECRET)
+ NVMF_OPT_DHCHAP_CTRL_SECRET | NVMF_OPT_KEYRING)
static struct nvme_ctrl *
nvmf_create_ctrl(struct device *dev, const char *buf)
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 4ebc03a8ddcaede1344988dfe816bbaf3746bb55..0ffea12fcfb6b5e79100fca4f125a655042695ca 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -1212,7 +1212,38 @@ static ssize_t tls_keyring_show(struct device *dev,
return sysfs_emit(buf, "%s\n", nvme_keyring_name());
return sysfs_emit(buf, "%s\n", keyring->description);
}
-static DEVICE_ATTR_RO(tls_keyring);
+
+static ssize_t tls_keyring_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+ struct key *keyring = ctrl->opts->keyring;
+ int ret;
+
+ keyring = request_key(&key_type_keyring, buf, NULL);
+ if (IS_ERR(keyring)) {
+ unsigned long keyring_id;
+
+ ret = kstrtoul(buf, 0, &keyring_id);
+ if (ret < 0)
+ return -EINVAL;
+ keyring = key_lookup(keyring_id);
+ if (IS_ERR(keyring))
+ return -EINVAL;
+ }
+ if (ctrl->opts->keyring) {
+ ret = nvme_keyring_unlink(ctrl->opts->keyring);
+ if (ret < 0) {
+ pr_warn("failed to unlink '%08X' from default keyring\n",
+ key_serial(ctrl->opts->keyring));
+ key_put(keyring);
+ return ret;
+ }
+ }
+ ctrl->opts->keyring = keyring;
+ return count;
+}
+static DEVICE_ATTR_ADMIN_RW(tls_keyring);
static ssize_t tls_mode_show(struct device *dev,
struct device_attribute *attr, char *buf)
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 585aba1d544518d6e712e23c7987741b130aee4d..c3149a9151f557534a7d9cb3cbe0b3c61a037833 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -3432,7 +3432,7 @@ static struct nvmf_transport_ops nvme_tcp_transport = {
NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS |
- NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY | NVMF_OPT_CONCAT,
+ NVMF_OPT_TLS_KEY | NVMF_OPT_CONCAT,
.create_ctrl = nvme_tcp_create_ctrl,
};
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (11 preceding siblings ...)
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 ` Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys Hannes Reinecke
2026-11-26 16:00 ` [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Jarkko Sakkinen
14 siblings, 0 replies; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke
Pre-provisioned keys might live in a different keyring which might
have a different lifetime than the structures referencing the key.
So to ensure the key remains accessible link the pre-provisioned key
into the .nvme keyring, and unlink it once it's no longer needed.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/host/fabrics.c | 48 +++++++++++++++++++++++++++++++++------------
drivers/nvme/target/auth.c | 39 +++++++++++++++++++++++++++---------
2 files changed, 66 insertions(+), 21 deletions(-)
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 12a30ed16f9ef32c74a51a0ae6ff8ce0ea628301..e7f51474ab5e1f67fca21d84c4bddcbe9e557571 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -1115,7 +1115,17 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- pr_debug("using dhchap host key %08x\n", key_serial(key));
+ if (!opts->dhchap_key_generated) {
+ pr_debug("linking dhchap host key %08x\n", key_serial(key));
+ ret = nvme_keyring_link(key);
+ if (ret) {
+ pr_debug("failed to link dhchap host key %08x\n",
+ key_serial(key));
+ key_put(key);
+ goto out;
+ }
+ } else
+ pr_debug("using dhchap host key %08x\n", key_serial(key));
opts->dhchap_key = key_serial(key);
key_put(key);
#endif
@@ -1133,7 +1143,17 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+ if (!opts->dhchap_ctrl_key_generated) {
+ pr_debug("linking dhchap ctrl key %08x\n", key_serial(key));
+ ret = nvme_keyring_link(key);
+ if (ret) {
+ pr_warn("failed to link dhchap ctrl key %08x\n",
+ key_serial(key));
+ key_put(key);
+ goto out;
+ }
+ } else
+ pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
opts->dhchap_ctrl_key = key_serial(key);
key_put(key);
#endif
@@ -1346,23 +1366,27 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
struct key *key;
nvmf_host_put(opts->host);
- if (opts->dhchap_key &&
- opts->dhchap_key_generated) {
+ if (opts->dhchap_key) {
key = key_lookup(opts->dhchap_key);
if (!IS_ERR(key)) {
- pr_debug("revoke dhchap host key %08x\n",
- opts->dhchap_key);
- key_revoke(key);
+ if (opts->dhchap_key_generated) {
+ pr_debug("revoke dhchap host key %08x\n",
+ opts->dhchap_key);
+ key_revoke(key);
+ } else
+ nvme_keyring_unlink(key);
key_put(key);
}
}
- if (opts->dhchap_ctrl_key &&
- opts->dhchap_ctrl_key_generated) {
+ if (opts->dhchap_ctrl_key) {
key = key_lookup(opts->dhchap_ctrl_key);
if (!IS_ERR(key)) {
- pr_debug("revoke dhchap ctrl key %08x\n",
- opts->dhchap_ctrl_key);
- key_revoke(key);
+ if (opts->dhchap_ctrl_key_generated) {
+ pr_debug("revoke dhchap ctrl key %08x\n",
+ opts->dhchap_ctrl_key);
+ key_revoke(key);
+ } else
+ nvme_keyring_unlink(key);
key_put(key);
}
}
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 54e79c8a7cae67a509a9f6b993b7886e706af8e0..04a9177ffa1f48445b54f0b6ec3f63e7c111fe06 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -19,19 +19,20 @@
#include "nvmet.h"
-static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
+static int nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
struct key *new_key, bool new_generated)
{
key_serial_t old_key_id = 0, new_key_id = key_serial(new_key);
struct key *key = NULL;
bool generated = false;
+ int ret;
if (set_ctrl) {
if (host->dhchap_ctrl_key) {
if (host->dhchap_ctrl_key == new_key_id) {
if (new_key && new_generated)
key_revoke(new_key);
- return;
+ return 0;
}
old_key_id = host->dhchap_ctrl_key;
generated = host->dhchap_ctrl_key_generated;
@@ -44,7 +45,7 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
if (host->dhchap_key == new_key_id) {
if (new_key && new_generated)
key_revoke(new_key);
- return;
+ return 0;
}
old_key_id = host->dhchap_key;
generated = host->dhchap_key_generated;
@@ -53,18 +54,37 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
host->dhchap_key = new_key_id;
host->dhchap_key_generated = new_generated;
}
+ if (new_key && !new_generated) {
+ ret = nvme_keyring_link(new_key);
+ if (ret < 0) {
+ pr_warn("%s: failed to link %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(new_key));
+ return ret;
+ }
+ }
+
if (!old_key_id)
- return;
+ return 0;
key = key_lookup(old_key_id);
if (IS_ERR(key))
- return;
+ return PTR_ERR(key);
if (generated) {
pr_debug("%s: revoke %s key %08x\n", __func__,
set_ctrl ? "ctrl" : "host", key_serial(key));
key_revoke(key);
+ ret = 0;
+ } else {
+ ret = nvme_keyring_unlink(key);
+ if (ret < 0) {
+ pr_warn("%s: failed to unlink %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(new_key));
+ }
}
key_put(key);
+ return ret;
}
void nvmet_auth_clear_key(struct nvmet_host *host)
@@ -79,12 +99,13 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool generated = false;
struct key *key;
size_t len;
+ int ret;
if (!secret || !strlen(secret)) {
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, NULL, false);
+ ret = nvmet_auth_update_key(host, set_ctrl, NULL, false);
up_write(&nvmet_config_sem);
- return 0;
+ return ret;
}
len = strcspn(secret, "\n");
key = nvme_auth_extract_key(secret, len, &generated);
@@ -102,10 +123,10 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
}
up_read(&key->sem);
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, key, generated);
+ ret = nvmet_auth_update_key(host, set_ctrl, key, generated);
up_write(&nvmet_config_sem);
key_put(key);
- return 0;
+ return ret;
}
int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id)
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (12 preceding siblings ...)
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 ` 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
14 siblings, 1 reply; 20+ messages in thread
From: Hannes Reinecke @ 2026-11-26 14:12 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jarkko Sakkinen, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Kees Cook, Gustavo A. R. Silva
Cc: linux-nvme, linux-kernel, keyrings, linux-doc, linux-hardening,
Hannes Reinecke, Martin Belanger
As we're passing key serial ids between fabrics options and the
actual nvme controller structure we can implement key rotation by
allowing the key contents to be updated (ie calling 'keyctl update').
This results in the key serial to remain stable, and we just need
to do a re-authentication with the new key contents.
In order for this to work we need to disallow changing of
pre-provisioned keys from sysfs, and implement an .update callback
to the dhchap key type.
Suggested-by: Martin Belanger <martin.nvme.dev@gmail.com>
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/keyring.c | 1 +
drivers/nvme/host/sysfs.c | 20 ++++++++++++++++++--
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index bee64c592752a7179e1e6575f96749a7ae26a6d6..ffabd37cd090d8fe3e461a02cdee0dc4cd4135c5 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -100,6 +100,7 @@ static struct key_type nvme_tls_psk_key_type = {
.free_preparse = user_free_preparse,
.match_preparse = nvme_tls_psk_match_preparse,
.instantiate = generic_key_instantiate,
+ .update = user_update,
.revoke = user_revoke,
.destroy = user_destroy,
.describe = nvme_tls_psk_describe,
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 0ffea12fcfb6b5e79100fca4f125a655042695ca..0eb68b3973a1e7d6fbd6b84fba2a6880a1da4dcd 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -887,9 +887,17 @@ static ssize_t dhchap_secret_store(struct device *dev,
kfree_sensitive(dhchap_secret);
return ret;
}
+ if (!generated &&
+ key_serial(old_key) != key_serial(key)) {
+ dev_warn(ctrl->device, "cannot change pre-populated key %08x\n",
+ key_serial(old_key));
+ key_put(key);
+ kfree_sensitive(dhchap_secret);
+ return -EPERM;
+ }
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->host_key;
- if (old_key && key_serial(old_key) == key_serial(key)) {
+ if (old_key && old_key == key) {
dev_warn(ctrl->device, "using existing key %08x\n",
key_serial(old_key));
old_key = key;
@@ -984,9 +992,17 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
kfree_sensitive(dhchap_secret);
return ret;
}
+ if (!generated &&
+ key_serial(old_key) != key_serial(key)) {
+ dev_warn(ctrl->device, "cannot change pre-populated key %08x\n",
+ key_serial(old_key));
+ key_put(key);
+ kfree_sensitive(dhchap_secret);
+ return -EPERM;
+ }
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->ctrl_key;
- if (old_key && key_serial(old_key) == key_serial(key)) {
+ if (old_key && old_key == key) {
dev_warn(ctrl->device, "using existing ctrl key %08x\n",
key_serial(old_key));
old_key = key;
--
2.51.0
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (13 preceding siblings ...)
2026-11-26 14:12 ` [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys Hannes Reinecke
@ 2026-11-26 16:00 ` Jarkko Sakkinen
2026-11-26 16:03 ` Jarkko Sakkinen
14 siblings, 1 reply; 20+ messages in thread
From: Jarkko Sakkinen @ 2026-11-26 16:00 UTC (permalink / raw)
To: Hannes Reinecke
Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Kees Cook,
Gustavo A. R. Silva, linux-nvme, linux-kernel, keyrings,
linux-doc, linux-hardening, Martin Belanger
On Thu, Nov 26, 2026 at 03:12:18PM +0100, Hannes Reinecke wrote:
> Hey all,
>
> the current NVMe authentication code is using a hand-crafted key
> structure; idea was to have the initial implementation with a minimal
> set of dependencies.
> (And me not having a good grasp on how to use the kernel keyring :-)
> That had the drawback that keys always had to be specified on the
> nvme-cli commandline, which is far from ideal from a security standpoint.
>
> So this patchset switches the authentication code over to use
> 'struct key' to hold DH-HMAC-CHAP secrets. User-facing interfaces
> (namely argument to 'nvme connect') remain the same, but the key data
> is converted into keys which are stored as a new key type 'dhchap' with
> a random UUID as description in the .nvme keyring.
>
> With this I have updated the dhchap arguments to 'nvme connect' and
> the configfs interface to either be the keydata (ie the original
> interface), a key description, _or_ a key serial referring to a
> pre-populated dhchap key in the kernel keyring. This allows for external
> provisioning of keys and avoids the security risk from having to specify
> the key data on the kernel commandline.
>
> The entire patchset can be found at
> git://git.kernel.org/pub/scm/linux/kernel/git/hare/nvme.git
> branch dhchap-keyring.v6
>
> As usual, comments and reviews are welcome.
>
> Signed-off-by: Hannes Reinecke <hare@kernel.org>
> ---
> Changes in v7:
> - Fixup next round of issues from sashiko
> - Modify to use key_serial_t instead of 'struct key' to pass key references
> - Include suggestions from Martin Belanger
> - Link to v6: https://lore.kernel.org/r/20261123-dhchap-keyring-v4-v6-0-5082a49715ab@kernel.org
>
> Changes in v6:
> - Fixup next round of issues from sashiko
> - Fixup kbuild robot issues
> - Link to v5: https://lore.kernel.org/r/20261123-dhchap-keyring-v4-v5-0-81eefb4c0724@kernel.org
>
> Changes in v5:
> - Fixup issues found by sashiko
> - Link to v4: https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org
>
> ---
> Hannes Reinecke (14):
> nvme-auth: modify nvme_auth_transform_key() to return status
> nvme-keyring: add 'dhchap' key type
> nvme-keyring: drop nvme_tls_psk_revoked()
> Documentation/security/keys: fixup function name
> nvme-keyring: link addtional keyring into .nvme keyring
> nvme-auth: switch to use 'struct key'
> nvme: parse dhchap keys during option parsing
> nvmet-auth: parse dhchap key from configfs attribute
> nvme: allow to pass in key description as dhchap secret
> nvme-auth: allow to specify the key by key serial
> nvme-auth: wait for authentication to finish when changing keys
> nvme-fabrics: allow to pass in keyring by name
> nvme-auth: link pre-provisioned keys into .nvme keyring
> nvme-auth: allow to update pre-provisioned keys
>
> Documentation/ABI/stable/sysfs-nvme | 10 +
> Documentation/security/keys/core.rst | 2 +-
> drivers/nvme/common/Kconfig | 2 +
> drivers/nvme/common/auth.c | 232 ++++++++++------------
> drivers/nvme/common/keyring.c | 370 ++++++++++++++++++++++++++++++++---
> drivers/nvme/host/Kconfig | 1 -
> drivers/nvme/host/auth.c | 176 ++++++++++++-----
> drivers/nvme/host/fabrics.c | 169 ++++++++++++----
> drivers/nvme/host/fabrics.h | 12 +-
> drivers/nvme/host/nvme.h | 6 +-
> drivers/nvme/host/sysfs.c | 300 ++++++++++++++++++++++------
> drivers/nvme/host/tcp.c | 5 +-
> drivers/nvme/target/Kconfig | 1 -
> drivers/nvme/target/auth.c | 250 +++++++++++++++--------
> drivers/nvme/target/configfs.c | 84 ++++++--
> drivers/nvme/target/nvmet.h | 15 +-
> include/linux/nvme-auth.h | 17 +-
> include/linux/nvme-keyring.h | 47 ++++-
> 18 files changed, 1257 insertions(+), 442 deletions(-)
> ---
> base-commit: 68099cf27253a8c89d703a282d3d811446d9453b
> change-id: 20261120-dhchap-keyring-v4-09f0bac1d8c5
>
> Best regards,
> --
> Hannes Reinecke <hare@kernel.org>
>
Without looking into detail to the changes, it'd be best if key type
semantics would align with [1]. See the preparation patches for other
subsystems.
I'll send updated patch set after v7.5 merge window. The only reason
I'm not moving forward sooner is to gather as many bug fixes as
possible for the "previous world", which LLM bots kindly provide.
Otherwise, this needs to be refactored very soon after the patch set is
merged.
[1] https://lore.kernel.org/keyrings/20261112040242.247168-1-jarkko@kernel.org/
Br, Jarkko
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring
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
0 siblings, 0 replies; 20+ messages in thread
From: Jarkko Sakkinen @ 2026-11-26 16:03 UTC (permalink / raw)
To: Hannes Reinecke
Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers, David Howells,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Kees Cook,
Gustavo A. R. Silva, linux-nvme, linux-kernel, keyrings,
linux-doc, linux-hardening, Martin Belanger
On Thu, Nov 26, 2026 at 06:00:08PM +0200, Jarkko Sakkinen wrote:
> Without looking into detail to the changes, it'd be best if key type
> semantics would align with [1]. See the preparation patches for other
> subsystems.
>
> I'll send updated patch set after v7.5 merge window. The only reason
> I'm not moving forward sooner is to gather as many bug fixes as
> possible for the "previous world", which LLM bots kindly provide.
>
> Otherwise, this needs to be refactored very soon after the patch set is
> merged.
>
> [1] https://lore.kernel.org/keyrings/20261112040242.247168-1-jarkko@kernel.org/
I'm not saying that this could not be merged first or try to put that
type of causality in place. The point is that it would be best if this
was prepared as size-fit as possible with best effort.
See e.g., S390 changes in the patch set above to see most complicated
upgrade.
Br, Jarkko
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
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
1 sibling, 0 replies; 20+ messages in thread
From: kernel test robot @ 2026-11-26 19:49 UTC (permalink / raw)
To: Hannes Reinecke, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers,
David Howells, Jarkko Sakkinen, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Kees Cook, Gustavo A. R. Silva
Cc: llvm, oe-kbuild-all, linux-nvme, linux-kernel, keyrings,
linux-doc, linux-hardening
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/20261126-151219
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261126-dhchap-keyring-v4-v7-5-ee438ffea527%40kernel.org
patch subject: [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20261127/20261127034638.jk3QPDaD-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project fb29ca8163c3309160711e5a14eb46bc6d4fdad2)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261127/20261127034638.jk3QPDaD-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/oe-kbuild-all/20261127034638.jk3QPDaD-lkp@intel.com/
All error/warnings (new ones prefixed by >>):
In file included from drivers/nvme/host/sysfs.c:9:
>> include/linux/nvme-keyring.h:47:5: warning: no previous prototype for function 'nvme_keyring_unlink' [-Wmissing-prototypes]
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
include/linux/nvme-keyring.h:47:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
| static
include/linux/nvme-keyring.h:39:20: warning: unused function 'nvme_keyring_name' [-Wunused-function]
39 | static const char *nvme_keyring_name(void)
| ^~~~~~~~~~~~~~~~~
include/linux/nvme-keyring.h:43:12: warning: unused function 'nvme_keyring_link' [-Wunused-function]
43 | static int nvme_keyring_link(struct key *keyring)
| ^~~~~~~~~~~~~~~~~
3 warnings generated.
--
In file included from drivers/nvme/host/fabrics.c:15:
>> include/linux/nvme-keyring.h:47:5: warning: no previous prototype for function 'nvme_keyring_unlink' [-Wmissing-prototypes]
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
include/linux/nvme-keyring.h:47:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
| static
include/linux/nvme-keyring.h:39:20: warning: unused function 'nvme_keyring_name' [-Wunused-function]
39 | static const char *nvme_keyring_name(void)
| ^~~~~~~~~~~~~~~~~
2 warnings generated.
--
In file included from drivers/nvme/host/tcp.c:15:
>> include/linux/nvme-keyring.h:47:5: warning: no previous prototype for function 'nvme_keyring_unlink' [-Wmissing-prototypes]
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
include/linux/nvme-keyring.h:47:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
| static
>> drivers/nvme/host/tcp.c:2420:34: error: too few arguments to function call, expected 3, have 2
2419 | pskid = nvme_tls_psk_default(ctrl->opts->host->nqn,
| ~~~~~~~~~~~~~~~~~~~~
2420 | ctrl->opts->subsysnqn);
| ^
include/linux/nvme-keyring.h:30:28: note: 'nvme_tls_psk_default' declared here
30 | static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
| ^ ~~~~~~~~~~~~~~~~~~~~
31 | const char *hostnqn, const char *subnqn)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
--
In file included from tcp.c:15:
>> include/linux/nvme-keyring.h:47:5: warning: no previous prototype for function 'nvme_keyring_unlink' [-Wmissing-prototypes]
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
include/linux/nvme-keyring.h:47:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
47 | int nvme_keyring_unlink(struct key *keyring)
| ^
| static
tcp.c:2420:34: error: too few arguments to function call, expected 3, have 2
2419 | pskid = nvme_tls_psk_default(ctrl->opts->host->nqn,
| ~~~~~~~~~~~~~~~~~~~~
2420 | ctrl->opts->subsysnqn);
| ^
include/linux/nvme-keyring.h:30:28: note: 'nvme_tls_psk_default' declared here
30 | static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
| ^ ~~~~~~~~~~~~~~~~~~~~
31 | const char *hostnqn, const char *subnqn)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
vim +2420 drivers/nvme/host/tcp.c
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2409
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2410 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2411 {
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2412 int ret;
be8e82caa68599 Hannes Reinecke 2023-08-24 2413 key_serial_t pskid = 0;
be8e82caa68599 Hannes Reinecke 2023-08-24 2414
363895767fbfa0 Hannes Reinecke 2024-07-22 2415 if (nvme_tcp_tls_configured(ctrl)) {
adf22c520b5ec4 Hannes Reinecke 2023-08-24 2416 if (ctrl->opts->tls_key)
adf22c520b5ec4 Hannes Reinecke 2023-08-24 2417 pskid = key_serial(ctrl->opts->tls_key);
e88a7595b57f2a Hannes Reinecke 2025-02-24 2418 else if (ctrl->opts->tls) {
83137991c12380 Hannes Reinecke 2026-11-26 2419 pskid = nvme_tls_psk_default(ctrl->opts->host->nqn,
be8e82caa68599 Hannes Reinecke 2023-08-24 @2420 ctrl->opts->subsysnqn);
be8e82caa68599 Hannes Reinecke 2023-08-24 2421 if (!pskid) {
be8e82caa68599 Hannes Reinecke 2023-08-24 2422 dev_err(ctrl->device, "no valid PSK found\n");
ef184b8844bf98 Guixin Liu 2023-12-31 2423 return -ENOKEY;
be8e82caa68599 Hannes Reinecke 2023-08-24 2424 }
be8e82caa68599 Hannes Reinecke 2023-08-24 2425 }
363895767fbfa0 Hannes Reinecke 2024-07-22 2426 }
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2427
be8e82caa68599 Hannes Reinecke 2023-08-24 2428 ret = nvme_tcp_alloc_queue(ctrl, 0, pskid);
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2429 if (ret)
ef184b8844bf98 Guixin Liu 2023-12-31 2430 return ret;
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2431
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2432 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2433 if (ret)
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2434 goto out_free_queue;
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2435
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2436 return 0;
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2437
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2438 out_free_queue:
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2439 nvme_tcp_free_queue(ctrl, 0);
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2440 return ret;
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2441 }
3f2304f8c6d6ed Sagi Grimberg 2018-12-03 2442
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
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
1 sibling, 0 replies; 20+ messages in thread
From: kernel test robot @ 2026-11-26 20:22 UTC (permalink / raw)
To: Hannes Reinecke, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers,
David Howells, Jarkko Sakkinen, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Kees Cook, Gustavo A. R. Silva
Cc: oe-kbuild-all, linux-nvme, linux-kernel, keyrings, linux-doc,
linux-hardening
Hi Hannes,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 68099cf27253a8c89d703a282d3d811446d9453b]
url: https://github.com/intel-lab-lkp/linux/commits/Hannes-Reinecke/nvme-auth-modify-nvme_auth_transform_key-to-return-status/20261126-151219
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261126-dhchap-keyring-v4-v7-5-ee438ffea527%40kernel.org
patch subject: [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
config: i386-randconfig-1003-20261126 (https://download.01.org/0day-ci/archive/20261127/20261127041445.UXBLiTUG-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/20261127/20261127041445.UXBLiTUG-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/oe-kbuild-all/20261127041445.UXBLiTUG-lkp@intel.com/
All warnings (new ones prefixed by >>):
In file included from drivers/nvme/host/sysfs.c:9:
>> include/linux/nvme-keyring.h:47:5: warning: no previous prototype for 'nvme_keyring_unlink' [-Wmissing-prototypes]
47 | int nvme_keyring_unlink(struct key *keyring)
| ^~~~~~~~~~~~~~~~~~~
>> include/linux/nvme-keyring.h:43:12: warning: 'nvme_keyring_link' defined but not used [-Wunused-function]
43 | static int nvme_keyring_link(struct key *keyring)
| ^~~~~~~~~~~~~~~~~
>> include/linux/nvme-keyring.h:39:20: warning: 'nvme_keyring_name' defined but not used [-Wunused-function]
39 | static const char *nvme_keyring_name(void)
| ^~~~~~~~~~~~~~~~~
vim +/nvme_keyring_unlink +47 include/linux/nvme-keyring.h
22
23 #else
24 static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
25 const char *hostnqn, const char *subnqn, u8 hmac_id,
26 u8 *data, size_t data_len, const char *digest)
27 {
28 return ERR_PTR(-EOPNOTSUPP);
29 }
30 static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
31 const char *hostnqn, const char *subnqn)
32 {
33 return 0;
34 }
35 static inline key_serial_t nvme_keyring_id(void)
36 {
37 return 0;
38 }
> 39 static const char *nvme_keyring_name(void)
40 {
41 return NULL;
42 }
> 43 static int nvme_keyring_link(struct key *keyring)
44 {
45 return 0;
46 }
> 47 int nvme_keyring_unlink(struct key *keyring)
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys
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
0 siblings, 0 replies; 20+ messages in thread
From: kernel test robot @ 2026-11-26 21:15 UTC (permalink / raw)
To: Hannes Reinecke, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Hannes Reinecke, Chaitanya Kulkarni, Eric Biggers,
David Howells, Jarkko Sakkinen, Jonathan Corbet, Shuah Khan,
Randy Dunlap, Kees Cook, Gustavo A. R. Silva
Cc: llvm, oe-kbuild-all, linux-nvme, linux-kernel, keyrings,
linux-doc, linux-hardening, Martin Belanger
Hi Hannes,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 68099cf27253a8c89d703a282d3d811446d9453b]
url: https://github.com/intel-lab-lkp/linux/commits/Hannes-Reinecke/nvme-auth-modify-nvme_auth_transform_key-to-return-status/20261126-151219
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261126-dhchap-keyring-v4-v7-14-ee438ffea527%40kernel.org
patch subject: [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys
config: hexagon-randconfig-1001-20261127 (https://download.01.org/0day-ci/archive/20261127/20261127044024.YlqHcQQx-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project fb29ca8163c3309160711e5a14eb46bc6d4fdad2)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261127/20261127044024.YlqHcQQx-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/oe-kbuild-all/20261127044024.YlqHcQQx-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/nvme/host/sysfs.c:891:17: warning: variable 'old_key' is uninitialized when used here [-Wuninitialized]
891 | key_serial(old_key) != key_serial(key)) {
| ^~~~~~~
drivers/nvme/host/sysfs.c:853:27: note: initialize the variable 'old_key' to silence this warning
853 | struct key *key, *old_key;
| ^
| = NULL
drivers/nvme/host/sysfs.c:996:17: warning: variable 'old_key' is uninitialized when used here [-Wuninitialized]
996 | key_serial(old_key) != key_serial(key)) {
| ^~~~~~~
drivers/nvme/host/sysfs.c:959:27: note: initialize the variable 'old_key' to silence this warning
959 | struct key *key, *old_key;
| ^
| = NULL
2 warnings generated.
vim +/old_key +891 drivers/nvme/host/sysfs.c
848
849 static ssize_t dhchap_secret_store(struct device *dev,
850 struct device_attribute *attr, const char *buf, size_t count)
851 {
852 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
853 struct key *key, *old_key;
854 char *dhchap_secret;
855 bool generated = false;
856 size_t len;
857 int ret;
858
859 if (!ctrl->host_key || !count)
860 return -EINVAL;
861
862 len = strcspn(buf, "\n");
863 dhchap_secret = kzalloc(len + 1, GFP_KERNEL);
864 if (!dhchap_secret)
865 return -ENOMEM;
866 memcpy(dhchap_secret, buf, len);
867 nvme_auth_stop(ctrl);
868
869 key = nvme_auth_extract_key(dhchap_secret, len,
870 &generated);
871 if (IS_ERR(key)) {
872 kfree_sensitive(dhchap_secret);
873 return PTR_ERR(key);
874 }
875 down_read(&key->sem);
876 ret = key_validate(key);
877 up_read(&key->sem);
878 if (ret) {
879 dev_warn(ctrl->device, "key %08x invalidated\n",
880 key_serial(key));
881 if (generated) {
882 dev_dbg(ctrl->device, "revoke host key %08x\n",
883 key_serial(key));
884 key_revoke(key);
885 }
886 key_put(key);
887 kfree_sensitive(dhchap_secret);
888 return ret;
889 }
890 if (!generated &&
> 891 key_serial(old_key) != key_serial(key)) {
892 dev_warn(ctrl->device, "cannot change pre-populated key %08x\n",
893 key_serial(old_key));
894 key_put(key);
895 kfree_sensitive(dhchap_secret);
896 return -EPERM;
897 }
898 mutex_lock(&ctrl->dhchap_auth_mutex);
899 old_key = ctrl->host_key;
900 if (old_key && old_key == key) {
901 dev_warn(ctrl->device, "using existing key %08x\n",
902 key_serial(old_key));
903 old_key = key;
904 } else {
905 if (ctrl->host_key_generated) {
906 dev_dbg(ctrl->device, "revoke host key %08x\n",
907 key_serial(old_key));
908 key_revoke(old_key);
909 }
910 ctrl->host_key = key;
911 ctrl->host_key_generated = generated;
912 }
913 mutex_unlock(&ctrl->dhchap_auth_mutex);
914 key_put(old_key);
915 kfree_sensitive(dhchap_secret);
916 /* Start re-authentication */
917 dev_info(ctrl->device, "re-authenticating controller\n");
918 queue_work(nvme_wq, &ctrl->dhchap_auth_work);
919 flush_work(&ctrl->dhchap_auth_work);
920 return count;
921 }
922
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2026-11-26 21:16 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
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
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®