* [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring
@ 2026-11-20 11:13 Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
` (8 more replies)
0 siblings, 9 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:13 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
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 the
kernel keyring. User-facing interface (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 kernel 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) _or_ a key description referring to a pre-populated dhchap
key in the kernel keyring. This allows for easier 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.v4
As usual, comments and reviews are welcome.
Changes to v3:
- Update to 7.4
- Add patch to set keyring for nvmet
Changes to v2:
- Update to v7.1
- Include reviews fromn Sagi
- Clarify decoded PSK length
- Add more function descriptions
Changes to the original submission:
- Dropped patches merged with upstream
- Modified the interface to refer to keys via the description
and not the serial number
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
Hannes Reinecke (9):
nvme-auth: modify nvme_auth_transform_key() to return status
nvme-keyring: add 'dhchap' key type
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: wait for authentication to finish when changing keys
nvme-fabrics: allow to pass in keyring by name
nvmet: add configfs attribute 'dhchap_keyring'
drivers/nvme/common/Kconfig | 1 +
drivers/nvme/common/auth.c | 213 ++++++++++++----------------
drivers/nvme/common/keyring.c | 314 +++++++++++++++++++++++++++++++++++++++++
drivers/nvme/host/Kconfig | 1 -
drivers/nvme/host/auth.c | 171 +++++++++++++++-------
drivers/nvme/host/fabrics.c | 121 +++++++++++-----
drivers/nvme/host/fabrics.h | 12 +-
drivers/nvme/host/nvme.h | 6 +-
drivers/nvme/host/sysfs.c | 212 +++++++++++++++++++++-------
drivers/nvme/target/Kconfig | 1 -
drivers/nvme/target/auth.c | 229 +++++++++++++++++++-----------
drivers/nvme/target/configfs.c | 144 ++++++++++++++++---
drivers/nvme/target/nvmet.h | 16 +--
include/linux/nvme-auth.h | 11 +-
include/linux/nvme-keyring.h | 26 +++-
15 files changed, 1092 insertions(+), 386 deletions(-)
---
base-commit: 68099cf27253a8c89d703a282d3d811446d9453b
change-id: 20261120-dhchap-keyring-v4-09f0bac1d8c5
Best regards,
--
Hannes Reinecke <hare@kernel.org>
^ permalink raw reply [flat|nested] 30+ messages in thread
* [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:19 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
` (7 subsequent siblings)
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
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 | 38 +++++++++++++++++++-------------------
drivers/nvme/host/auth.c | 44 +++++++++++++++++++++++---------------------
drivers/nvme/target/auth.c | 37 +++++++++++++++++++++----------------
include/linux/nvme-auth.h | 4 ++--
4 files changed, 65 insertions(+), 58 deletions(-)
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index 1fdc4e4ed0b94d8a36aade033f19e17463f15632..569f21ad4fc7ed3e85083727ad25078420410f07 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -317,37 +317,37 @@ 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 *transformed_data;
+ 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);
+ 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) {
- memzero_explicit(&hmac, sizeof(hmac));
- return ERR_PTR(-ENOMEM);
- }
+ return ret;
+ transformed_len = nvme_auth_hmac_hash_len(key->hash);
+ key_data = kzalloc(transformed_len, GFP_KERNEL);
+ if (!key_data)
+ 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 related [flat|nested] 30+ messages in thread
* [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:28 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
` (6 subsequent siblings)
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
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/keyring.c | 216 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 216 insertions(+)
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 87cb51e1f5a7f330af4705f4058f661d15f5984a..96561f2dd81b8152f3d320fb63cdd605c132260e 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,212 @@ 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);
+}
+
+static bool nvme_dhchap_psk_match(const struct key *key,
+ const struct key_match_data *match_data)
+{
+ const char *match_id;
+ size_t match_len;
+
+ if (!key->description) {
+ pr_debug("%s: no key description\n", __func__);
+ return false;
+ }
+ if (!match_data->raw_data) {
+ pr_debug("%s: no match data\n", __func__);
+ return false;
+ }
+ match_id = match_data->raw_data;
+ match_len = strlen(match_id);
+ pr_debug("%s: match '%s' '%s' len %zd\n",
+ __func__, match_id, key->description, match_len);
+
+ return !memcmp(key->description, match_id, match_len);
+}
+
+static int nvme_dhchap_psk_match_preparse(struct key_match_data *match_data)
+{
+ match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
+ match_data->cmp = nvme_dhchap_psk_match;
+ return 0;
+}
+
+/**
+ * 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, keylen;
+ int ret;
+ u32 crc;
+ u8 version, hmac;
+
+ if (!prep->data) {
+ pr_debug("%s: Empty data", __func__);
+ prep->payload.data[0] = NULL;
+ prep->quotalen = 0;
+ return -EINVAL;
+ }
+
+ if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
+ &version, &hmac) != 2) {
+ pr_debug("%s: invalid key data '%s'\n", __func__,
+ (char *)prep->data);
+ prep->payload.data[0] = NULL;
+ prep->quotalen = 0;
+ return -EINVAL;
+ }
+
+ /* skip header and final ':' character */
+ datalen -= 11;
+
+ /*
+ * 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) {
+ prep->payload.data[0] = NULL;
+ prep->quotalen = 0;
+ return -ENOMEM;
+ }
+
+ /* decode the data */
+ prep->quotalen = keylen;
+ prep->payload.data[0] = upayload;
+ ret = base64_decode(prep->data + 10, datalen, upayload->data,
+ true, BASE64_STD);
+ if (ret < 0) {
+ pr_debug("%s: Failed to decode key %s\n",
+ __func__, (char *)prep->data + 10);
+ return ret;
+ }
+ ret -= 4;
+ crc = ~crc32(~0, upayload->data, ret);
+ if (get_unaligned_le32(upayload->data + ret) != crc) {
+ pr_debug("%s: CRC mismatch for key\n", __func__);
+ /* CRC mismatch */
+ return -EKEYREJECTED;
+ }
+ /* append version and hmac to the payload */
+ upayload->data[ret + 4] = version;
+ upayload->data[ret + 5] = hmac;
+ upayload->datalen = ret;
+ return 0;
+}
+
+/**
+ * nvme_dhchap_decoded_key_size - Size of the base64-decoded key
+ * @size: size of the encoded key
+ *
+ * Returns the expected size of the key after base64 decoding.
+ */
+static inline int nvme_dhchap_decoded_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
+ *
+ * Formets 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 +
+ * CRC length (= 4) +
+ * ':' (= 1)
+ */
+ keylen = nvme_dhchap_decoded_key_size(upayload->datalen) + 15;
+ if (keylen < 0 || !buffer || buflen == 0)
+ return keylen;
+
+ 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,
+ .match_preparse = nvme_dhchap_psk_match_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 +480,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 related [flat|nested] 30+ messages in thread
* [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
` (2 more replies)
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
` (5 subsequent siblings)
8 siblings, 3 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
Use the new key type 'dhchap' to store the DH-HMAC-CHAP keys and modify
handling function to use 'struct key'. With that we can drop the now
unused 'struct nvme_dhchap_key' definitions.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/Kconfig | 1 +
drivers/nvme/common/auth.c | 193 ++++++++++++++++--------------------------
drivers/nvme/common/keyring.c | 98 +++++++++++++++++++++
drivers/nvme/host/Kconfig | 1 -
drivers/nvme/host/auth.c | 28 ++++--
drivers/nvme/host/nvme.h | 4 +-
drivers/nvme/host/sysfs.c | 26 +++---
drivers/nvme/target/Kconfig | 1 -
drivers/nvme/target/auth.c | 52 ++++++------
drivers/nvme/target/nvmet.h | 4 +-
include/linux/nvme-auth.h | 11 +--
include/linux/nvme-keyring.h | 26 +++++-
12 files changed, 263 insertions(+), 182 deletions(-)
diff --git a/drivers/nvme/common/Kconfig b/drivers/nvme/common/Kconfig
index f1639db65fd38abde01832a7404cbc48cf49a93e..9f72c2668f2f6b71f20d737f9849f1bb9cd88dc6 100644
--- a/drivers/nvme/common/Kconfig
+++ b/drivers/nvme/common/Kconfig
@@ -11,6 +11,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 569f21ad4fc7ed3e85083727ad25078420410f07..b7092deed9b06e6ee9bcd819dc6e58ed15069c50 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,30 @@ 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(struct key *keyring, 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;
- }
+ struct key *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;
+ key = nvme_dhchap_psk_create(keyring, secret, secret_len);
+ if (!IS_ERR(key)) {
+ pr_debug("generated dhchap key %s\n",
+ key->description);
}
-
- /* 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);
-
- 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->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,12 +260,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;
+ long key_len = 0;
u8 *transformed_data;
- u8 *key_data;
+ u8 *key_data, key_hash;
size_t transformed_len;
int ret;
@@ -330,24 +274,64 @@ 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);
- memcpy(key_data, key->key, key->len);
+ 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;
+ }
+ 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) {
+ up_read(&key->sem);
+ return -ENOMEM;
+ }
+ ret = user_read(key, 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;
+ return key_len;
}
- ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
+
+ ret = nvme_auth_hmac_init(&hmac, key_hash, key_data, key_len);
if (ret)
- return ret;
- transformed_len = nvme_auth_hmac_hash_len(key->hash);
- key_data = kzalloc(transformed_len, GFP_KERNEL);
- if (!key_data)
- return -ENOMEM;
+ goto out_free_data;
+ 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;
+ }
+
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);
@@ -485,33 +469,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 96561f2dd81b8152f3d320fb63cdd605c132260e..044bf37a19630353217cfc0befce9081144fbba4 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -462,6 +462,104 @@ static struct key_type nvme_dhchap_psk_key_type = {
.read = nvme_dhchap_psk_read,
};
+/**
+ * nvme_dhchap_psk_create - Create DH-HMAC-CHAP key
+ * @keyring: keyring for the new key or NULL for the default keyring
+ * @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(struct key *keyring,
+ const u8 *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;
+ key_serial_t keyring_id;
+ 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);
+ if (!keyring)
+ keyring = nvme_keyring;
+ keyring_id = key_serial(keyring);
+
+ pr_debug("keyring %x generate dhchap psk '%s'\n",
+ keyring_id, identity);
+ keyref = key_create(make_key_ref(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
+ * @keyring: keyring to use or NULL for default keyring
+ * @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(struct key *keyring, const char *identity)
+{
+ key_ref_t keyref;
+ key_serial_t keyring_id;
+
+ if (!keyring)
+ keyring = nvme_keyring;
+ keyring_id = key_serial(keyring);
+ pr_debug("keyring %x lookup dhchap psk '%s'\n",
+ keyring_id, identity);
+
+ keyref = keyring_search(make_key_ref(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 295e0a9533c8995c32781324650d73b6bfedbf13..0ddf3834f4ea2bfedaf08b901db282b79df32879 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -1038,13 +1038,23 @@ 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->keyring,
+ ctrl->opts->dhchap_secret,
+ strlen(ctrl->opts->dhchap_secret));
+ if (IS_ERR(ctrl->host_key)) {
+ ret = PTR_ERR(ctrl->host_key);
+ ctrl->host_key = NULL;
return ret;
- ret = nvme_auth_parse_key(ctrl->opts->dhchap_ctrl_secret,
- &ctrl->ctrl_key);
- if (ret)
+ }
+
+ ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
+ ctrl->opts->dhchap_ctrl_secret,
+ strlen(ctrl->opts->dhchap_ctrl_secret));
+ if (IS_ERR(ctrl->ctrl_key)) {
+ ret = PTR_ERR(ctrl->ctrl_key);
+ ctrl->ctrl_key = NULL;
goto err_free_dhchap_secret;
+ }
if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
return 0;
@@ -1065,10 +1075,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;
}
@@ -1090,11 +1100,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 35f871e3e0c0ae49b390f0fb64c538796207a98f..a5c1511e49ac275e75a70ff89958ed8f0cb2c633 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -836,8 +836,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)
@@ -845,13 +843,13 @@ 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(opts->keyring, 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;
@@ -859,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 */
@@ -902,13 +900,13 @@ 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(opts->keyring, 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;
@@ -916,7 +914,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 150818d7009d8c2fca96e12fd54fdc1cb6b68882..26457ce4acc2a8664ec9c2a9ed6d66fed93b61fa 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(NULL, 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(NULL, 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/nvmet.h b/drivers/nvme/target/nvmet.h
index e33884a81efc9f96cdc5d0120e0701f06ce7d873..5e1f0e78a388252e6e0ec8405006136e278749cb 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;
diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
index 1c5ce635b169dd0747c1344e9adcc14839776cd1..b8ea0945131d193e9343a5e9fd703f0a4ba3dfec 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -37,13 +37,10 @@ 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(struct key *keyring, 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 ab8971afa9734a276d9197076b780321460dddc2..9fef4b6add787bda2cc5e7e13287f5601009522d 100644
--- a/include/linux/nvme-keyring.h
+++ b/include/linux/nvme-keyring.h
@@ -18,12 +18,17 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
key_serial_t nvme_keyring_id(void);
struct key *nvme_tls_key_lookup(key_serial_t key_id);
+
+struct key *nvme_dhchap_psk_create(struct key *keyring,
+ const u8 *data, size_t data_len);
+struct key *nvme_dhchap_psk_lookup(struct key *keyring, 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, 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)
@@ -36,7 +41,22 @@ static inline key_serial_t nvme_keyring_id(void)
}
static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
- return ERR_PTR(-ENOTSUPP);
+ return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
+ const char *hostnqn, const char *subnqn,
+ u8 *data, size_t data_len)
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
+ const char *hostnqn, const char *subnqn, u8 hmac);
+{
+ return ERR_PTR(-EOPNOTSUPP);
+}
+u8 nvme_dhchap_psk_hash(struct key *key)
+{
+ return 0;
}
#endif /* !CONFIG_NVME_KEYRING */
#endif /* _NVME_KEYRING_H */
--
2.51.0
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (2 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:32 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
` (4 subsequent siblings)
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
We really should parse the dhchap keys during option parsing to avoid
having to pass around the plain dhchap secret. During options parsing
we will create a 'dhchap' key with a random UUID as description, and
store the key serial in the 'opts' structure.
This simplifies key handling as on every access the key needs to be
looked up and checked for validity before accessing the key data.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/host/auth.c | 118 +++++++++++++++++++---------
drivers/nvme/host/fabrics.c | 84 +++++++++++++-------
drivers/nvme/host/fabrics.h | 8 +-
drivers/nvme/host/sysfs.c | 186 +++++++++++++++++++++++++++++++-------------
4 files changed, 276 insertions(+), 120 deletions(-)
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 0ddf3834f4ea2bfedaf08b901db282b79df32879..c189096f825eab4e3053021d7437423ecb049c79 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -940,11 +940,6 @@ int nvme_auth_negotiate(struct nvme_ctrl *ctrl, int qid)
return -ENOKEY;
}
- if (ctrl->opts->dhchap_ctrl_secret && !ctrl->ctrl_key) {
- dev_warn(ctrl->device, "qid %d: invalid ctrl key\n", qid);
- return -ENOKEY;
- }
-
chap = &ctrl->dhchap_ctxs[qid];
cancel_work_sync(&chap->auth_work);
queue_work(nvme_auth_wq, &chap->auth_work);
@@ -1029,6 +1024,29 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
}
}
+static void nvme_auth_clear_key(struct nvme_ctrl *ctrl, bool is_ctrl)
+{
+ struct key *key;
+
+ if (is_ctrl) {
+ key = ctrl->ctrl_key;
+ ctrl->ctrl_key = NULL;
+ } else {
+ key = ctrl->host_key;
+ ctrl->host_key = NULL;
+ }
+ if (key) {
+ dev_dbg(ctrl->device, "%s: revoke dhchap%s key %08x\n",
+ __func__, is_ctrl ? " ctrl" : " host",
+ key_serial(key));
+ key_revoke(key);
+ key_put(key);
+ }
+}
+
+#define nvme_auth_clear_host_key(c) nvme_auth_clear_key(c, false)
+#define nvme_auth_clear_ctrl_key(c) nvme_auth_clear_key(c, true)
+
int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
{
struct nvme_dhchap_queue_context *chap;
@@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
if (!ctrl->opts)
return 0;
- ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
- ctrl->opts->dhchap_secret,
- strlen(ctrl->opts->dhchap_secret));
- if (IS_ERR(ctrl->host_key)) {
- ret = PTR_ERR(ctrl->host_key);
- ctrl->host_key = NULL;
- return ret;
+ if (!ctrl->opts->dhchap_key) {
+ nvme_auth_clear_host_key(ctrl);
+ nvme_auth_clear_ctrl_key(ctrl);
+ return 0;
}
+ if (!ctrl->host_key)
+ nvme_auth_clear_host_key(ctrl);
- ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
- ctrl->opts->dhchap_ctrl_secret,
- strlen(ctrl->opts->dhchap_ctrl_secret));
- if (IS_ERR(ctrl->ctrl_key)) {
- ret = PTR_ERR(ctrl->ctrl_key);
- ctrl->ctrl_key = NULL;
- goto err_free_dhchap_secret;
+ ctrl->host_key = key_get(ctrl->opts->dhchap_key);
+ if (!ctrl->host_key) {
+ dev_warn(ctrl->device,
+ "dhchap host key %08x not present\n",
+ key_serial(ctrl->opts->dhchap_key));
+ return -ENOKEY;
}
+ down_read(&ctrl->host_key->sem);
+ ret = key_validate(ctrl->host_key);
+ up_read(&ctrl->host_key->sem);
+ if (ret) {
+ dev_warn(ctrl->device,
+ "dhchap host key %08x invalidated\n",
+ key_serial(ctrl->host_key));
+ key_put(ctrl->host_key);
+ ctrl->host_key = NULL;
+ return -ENOKEY;
+ }
+ dev_dbg(ctrl->device,
+ "%s: using dhchap host key %08x\n",
+ __func__, key_serial(ctrl->host_key));
- if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
- return 0;
+ if (ctrl->ctrl_key)
+ nvme_auth_clear_ctrl_key(ctrl);
+
+ if (ctrl->opts->dhchap_ctrl_key) {
+ ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
+ if (!ctrl->ctrl_key) {
+ dev_warn(ctrl->device,
+ "dhchap ctrl key %08x not present\n",
+ key_serial(ctrl->opts->dhchap_ctrl_key));
+ return -ENOKEY;
+ }
+ down_read(&ctrl->ctrl_key->sem);
+ ret = key_validate(ctrl->ctrl_key);
+ up_read(&ctrl->ctrl_key->sem);
+ if (ret) {
+ dev_warn(ctrl->device,
+ "dhchap ctrl key %08x invalidated\n",
+ key_serial(ctrl->ctrl_key));
+ key_put(ctrl->ctrl_key);
+ ctrl->ctrl_key = NULL;
+ return -EKEYREVOKED;
+ }
+ dev_dbg(ctrl->device,
+ "%s: using dhchap ctrl key %08x\n",
+ __func__, key_serial(ctrl->ctrl_key));
+ }
ctrl->dhchap_ctxs = kvzalloc_objs(*chap, ctrl_max_dhchaps(ctrl));
if (!ctrl->dhchap_ctxs) {
- ret = -ENOMEM;
- goto err_free_dhchap_ctrl_secret;
+ nvme_auth_clear_ctrl_key(ctrl);
+ nvme_auth_clear_host_key(ctrl);
+ return -ENOMEM;
}
for (i = 0; i < ctrl_max_dhchaps(ctrl); i++) {
@@ -1074,13 +1129,6 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
}
return 0;
-err_free_dhchap_ctrl_secret:
- key_put(ctrl->ctrl_key);
- ctrl->ctrl_key = NULL;
-err_free_dhchap_secret:
- key_put(ctrl->host_key);
- ctrl->host_key = NULL;
- return ret;
}
EXPORT_SYMBOL_GPL(nvme_auth_init_ctrl);
@@ -1099,14 +1147,8 @@ void nvme_auth_free(struct nvme_ctrl *ctrl)
nvme_auth_free_dhchap(&ctrl->dhchap_ctxs[i]);
kvfree(ctrl->dhchap_ctxs);
}
- if (ctrl->host_key) {
- key_put(ctrl->host_key);
- ctrl->host_key = NULL;
- }
- if (ctrl->ctrl_key) {
- key_put(ctrl->ctrl_key);
- ctrl->ctrl_key = NULL;
- }
+ nvme_auth_clear_host_key(ctrl);
+ nvme_auth_clear_ctrl_key(ctrl);
}
EXPORT_SYMBOL_GPL(nvme_auth_free);
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 5ed91557f07065a284e664950a9c71d771614ec2..7ed8618257ca9fadc65202261a86b7ead209df7c 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -12,6 +12,7 @@
#include <linux/seq_file.h>
#include "nvme.h"
#include "fabrics.h"
+#include <linux/nvme-auth.h>
#include <linux/nvme-keyring.h>
static DECLARE_RWSEM(nvmf_transports_rwsem);
@@ -717,6 +718,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
{
substring_t args[MAX_OPT_ARGS];
char *options, *o, *p;
+ char *host_secret = NULL, *ctrl_secret = NULL;
int token, ret = 0;
size_t nqnlen = 0;
int ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO, key_id;
@@ -738,6 +740,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
opts->tls_key = NULL;
opts->keyring = NULL;
opts->concat = false;
+ opts->dhchap_key = NULL;
+ opts->dhchap_ctrl_key = NULL;
options = o = kstrdup(buf, GFP_KERNEL);
if (!options)
@@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = -ENOMEM;
goto out;
}
- if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
- pr_err("Invalid DH-CHAP secret %s\n", p);
- kfree_sensitive(p);
- ret = -EINVAL;
- goto out;
- }
- kfree_sensitive(opts->dhchap_secret);
- opts->dhchap_secret = p;
+ kfree_sensitive(host_secret);
+ host_secret = p;
break;
case NVMF_OPT_DHCHAP_CTRL_SECRET:
p = match_strdup(args);
@@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = -ENOMEM;
goto out;
}
- if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
- pr_err("Invalid DH-CHAP secret %s\n", p);
- kfree_sensitive(p);
- ret = -EINVAL;
- goto out;
- }
- kfree_sensitive(opts->dhchap_ctrl_secret);
- opts->dhchap_ctrl_secret = p;
+ kfree_sensitive(ctrl_secret);
+ ctrl_secret = p;
break;
case NVMF_OPT_TLS:
if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) {
@@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
opts->fast_io_fail_tmo, ctrl_loss_tmo);
}
+
+ opts->host = nvmf_host_add(hostnqn, &hostid);
+ if (IS_ERR(opts->host)) {
+ ret = PTR_ERR(opts->host);
+ opts->host = NULL;
+ goto out;
+ }
+
+ if (host_secret) {
+ pr_debug("lookup host identity '%s'\n", host_secret);
+ key = nvme_auth_extract_key(opts->keyring, host_secret,
+ strlen(host_secret));
+ if (IS_ERR(key)) {
+ ret = PTR_ERR(key);
+ goto out;
+ }
+ pr_debug("using dhchap host key %08x\n", key_serial(key));
+ opts->dhchap_key = key;
+ }
+ if (ctrl_secret) {
+ if (!opts->dhchap_key) {
+ ret = -EINVAL;
+ goto out;
+ }
+ pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
+ key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
+ strlen(ctrl_secret));
+ if (IS_ERR(key)) {
+ ret = PTR_ERR(key);
+ goto out;
+ }
+ pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+ opts->dhchap_ctrl_key = key;
+ }
+
if (opts->concat) {
if (opts->tls) {
pr_err("Secure concatenation over TLS is not supported\n");
@@ -1097,21 +1124,16 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = -EINVAL;
goto out;
}
- if (!opts->dhchap_secret) {
+ if (!opts->dhchap_key) {
pr_err("Need to enable DH-CHAP for secure concatenation\n");
ret = -EINVAL;
goto out;
}
}
- opts->host = nvmf_host_add(hostnqn, &hostid);
- if (IS_ERR(opts->host)) {
- ret = PTR_ERR(opts->host);
- opts->host = NULL;
- goto out;
- }
-
out:
+ kfree_sensitive(ctrl_secret);
+ kfree_sensitive(host_secret);
kfree(options);
return ret;
}
@@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
kfree(opts->subsysnqn);
kfree(opts->host_traddr);
kfree(opts->host_iface);
- kfree_sensitive(opts->dhchap_secret);
- kfree_sensitive(opts->dhchap_ctrl_secret);
+ if (opts->dhchap_key) {
+ pr_debug("revoke dhchap host key %08x\n",
+ key_serial(opts->dhchap_key));
+ key_revoke(opts->dhchap_key);
+ key_put(opts->dhchap_key);
+ }
+ if (opts->dhchap_ctrl_key) {
+ pr_debug("revoke dhchap ctrl key %08x\n",
+ key_serial(opts->dhchap_ctrl_key));
+ key_revoke(opts->dhchap_key);
+ key_put(opts->dhchap_ctrl_key);
+ }
kfree(opts);
}
EXPORT_SYMBOL_GPL(nvmf_free_options);
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index caf5503d0833296aa03c028c0199cf92cc409edc..786f1b4819f076ac1f133070523be1780ec4d8b7 100644
--- a/drivers/nvme/host/fabrics.h
+++ b/drivers/nvme/host/fabrics.h
@@ -96,8 +96,8 @@ enum {
* @discovery_nqn: indicates if the subsysnqn is the well-known discovery NQN.
* @kato: Keep-alive timeout.
* @host: Virtual NVMe host, contains the NQN and Host ID.
- * @dhchap_secret: DH-HMAC-CHAP secret
- * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional
+ * @dhchap_key: DH-HMAC-CHAP pre-shared key
+ * @dhchap_ctrl_key: DH-HMAC-CHAP controller pre-shared key for bi-directional
* authentication
* @keyring: Keyring to use for key lookups
* @tls_key: TLS key for encrypted connections (TCP)
@@ -127,8 +127,8 @@ struct nvmf_ctrl_options {
bool duplicate_connect;
unsigned int kato;
struct nvmf_host *host;
- char *dhchap_secret;
- char *dhchap_ctrl_secret;
+ struct key *dhchap_key;
+ struct key *dhchap_ctrl_key;
struct key *keyring;
struct key *tls_key;
bool tls;
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index a5c1511e49ac275e75a70ff89958ed8f0cb2c633..1311f5e29b004436e39a723242748a150e36dc68 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -6,6 +6,8 @@
*/
#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>
#include <linux/blkdev.h>
#include "nvme.h"
@@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
- struct nvmf_ctrl_options *opts = ctrl->opts;
+ struct key *key = ctrl->host_key;
+ ssize_t count;
- if (!opts->dhchap_secret)
+ if (!key)
return sysfs_emit(buf, "none\n");
- return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
+ down_read(&key->sem);
+ if (key_validate(key))
+ count = sysfs_emit(buf, "<invalidated>\n");
+ else {
+ count = key->type->read(key, buf, PAGE_SIZE);
+ if (count > 0) {
+ buf[count] = '\n';
+ count++;
+ }
+ }
+ up_read(&key->sem);
+ return count;
}
static ssize_t dhchap_secret_store(struct device *dev,
@@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
{
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct nvmf_ctrl_options *opts = ctrl->opts;
+ struct key *key, *old_key;
char *dhchap_secret;
+ size_t len;
+ int ret;
- if (!ctrl->opts->dhchap_secret)
- return -EINVAL;
- if (count < 7)
+ if (!ctrl->host_key || !count)
return -EINVAL;
- dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
+ len = strcspn(buf, "\n");
+ dhchap_secret = kzalloc(len + 1, GFP_KERNEL);
if (!dhchap_secret)
return -ENOMEM;
- memcpy(dhchap_secret, buf, count);
+ memcpy(dhchap_secret, buf, len);
nvme_auth_stop(ctrl);
- if (strcmp(dhchap_secret, opts->dhchap_secret)) {
- struct key *key, *host_key;
-
- key = nvme_auth_extract_key(opts->keyring, dhchap_secret,
- count);
- if (IS_ERR(key)) {
- kfree_sensitive(dhchap_secret);
- return PTR_ERR(key);
- }
- kfree_sensitive(opts->dhchap_secret);
- opts->dhchap_secret = dhchap_secret;
- host_key = ctrl->host_key;
- mutex_lock(&ctrl->dhchap_auth_mutex);
- ctrl->host_key = key;
- mutex_unlock(&ctrl->dhchap_auth_mutex);
- key_put(host_key);
- } else
+
+ key = nvme_auth_extract_key(opts->keyring, dhchap_secret, count);
+ if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
+ return PTR_ERR(key);
+ }
+ down_read(&key->sem);
+ ret = key_validate(key);
+ up_read(&key->sem);
+ if (ret) {
+ dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
+ dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
+ key_revoke(key);
+ key_put(key);
+ kfree_sensitive(dhchap_secret);
+ return ret;
+ }
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ old_key = ctrl->host_key;
+ dev_dbg(ctrl->dev, "revoke host key %08x\n",
+ key_serial(old_key));
+ key_revoke(old_key);
+
+ ctrl->host_key = key;
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ key_put(old_key);
+ kfree_sensitive(dhchap_secret);
/* Start re-authentication */
dev_info(ctrl->device, "re-authenticating controller\n");
queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
- struct nvmf_ctrl_options *opts = ctrl->opts;
+ struct key *key = ctrl->ctrl_key;
+ size_t count;
- if (!opts->dhchap_ctrl_secret)
+ if (!key)
return sysfs_emit(buf, "none\n");
- return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+ down_read(&key->sem);
+ if (key_validate(key))
+ count = sysfs_emit(buf, "<invalidated>");
+ else {
+ count = key->type->read(key, buf, PAGE_SIZE);
+ if (count > 0) {
+ buf[count] = '\n';
+ count++;
+ }
+ }
+ up_read(&key->sem);
+ return count;
}
static ssize_t dhchap_ctrl_secret_store(struct device *dev,
@@ -885,38 +922,46 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
{
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct nvmf_ctrl_options *opts = ctrl->opts;
+ struct key *key, *old_key;
char *dhchap_secret;
+ size_t len;
+ int ret;
- if (!ctrl->opts->dhchap_ctrl_secret)
- return -EINVAL;
- if (count < 7)
- return -EINVAL;
- if (memcmp(buf, "DHHC-1:", 7))
+ if (!ctrl->ctrl_key || !count)
return -EINVAL;
- dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
+ len = strcspn(buf, "\n");
+ dhchap_secret = kzalloc(len + 1, GFP_KERNEL);
if (!dhchap_secret)
return -ENOMEM;
- memcpy(dhchap_secret, buf, count);
+ memcpy(dhchap_secret, buf, len);
nvme_auth_stop(ctrl);
- if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
- struct key *key, *ctrl_key;
-
- key = nvme_auth_extract_key(opts->keyring, dhchap_secret,
- count);
- if (IS_ERR(key)) {
- kfree_sensitive(dhchap_secret);
- return PTR_ERR(key);
- }
- kfree_sensitive(opts->dhchap_ctrl_secret);
- opts->dhchap_ctrl_secret = dhchap_secret;
- ctrl_key = ctrl->ctrl_key;
- mutex_lock(&ctrl->dhchap_auth_mutex);
- ctrl->ctrl_key = key;
- mutex_unlock(&ctrl->dhchap_auth_mutex);
- key_put(ctrl_key);
- } else
+ key = nvme_auth_extract_key(opts->keyring, dhchap_secret, count);
+ if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
+ return PTR_ERR(key);
+ }
+ down_read(&key->sem);
+ ret = key_validate(key);
+ up_read(&key->sem);
+ if (ret) {
+ dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
+ dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
+ key_revoke(key);
+ key_put(key);
+ kfree_sensitive(dhchap_secret);
+ return ret;
+ }
+ mutex_lock(&ctrl->dhchap_auth_mutex);
+ old_key = ctrl->ctrl_key;
+ dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
+ key_serial(old_key));
+ key_revoke(old_key);
+
+ ctrl->ctrl_key = key;
+ mutex_unlock(&ctrl->dhchap_auth_mutex);
+ key_put(old_key);
+ kfree_sensitive(dhchap_secret);
/* Start re-authentication */
dev_info(ctrl->device, "re-authenticating controller\n");
queue_work(nvme_wq, &ctrl->dhchap_auth_work);
@@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
}
static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
+
+static ssize_t dhchap_key_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+ struct key *key = ctrl->host_key;
+ size_t count;
+
+ if (!key)
+ return 0;
+ down_read(&key->sem);
+ if (key_validate(key))
+ count = sysfs_emit(buf, "<invalidated>\n");
+ else {
+ count = key->type->read(key, buf, PAGE_SIZE);
+ if (count > 0) {
+ buf[count] = '\n';
+ count++;
+ }
+ }
+ up_read(&key->sem);
+ return count;
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_key);
+
+static ssize_t dhchap_ctrl_key_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+ if (!ctrl->ctrl_key)
+ return 0;
+ return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
#endif
static struct attribute *nvme_dev_attrs[] = {
@@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
#ifdef CONFIG_NVME_HOST_AUTH
&dev_attr_dhchap_secret.attr,
&dev_attr_dhchap_ctrl_secret.attr,
+ &dev_attr_dhchap_key.attr,
+ &dev_attr_dhchap_ctrl_key.attr,
#endif
&dev_attr_adm_passthru_err_log_enabled.attr,
NULL
--
2.51.0
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (3 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:28 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
` (3 subsequent siblings)
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
In preparation for moving the nvmet DH-HMAC-CHAP code over
to using the kernel keyring convert the configfs interface
to use 'struct key' instead of storing the key data directly.
This avoids having to pass around a pointer with the actual
key material.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/target/auth.c | 181 +++++++++++++++++++++++++----------------
drivers/nvme/target/configfs.c | 80 ++++++++++++++----
drivers/nvme/target/nvmet.h | 9 +-
3 files changed, 178 insertions(+), 92 deletions(-)
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 26457ce4acc2a8664ec9c2a9ed6d66fed93b61fa..33b45b4b202d4b36956d2e1cf0a4036692779f22 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -19,52 +19,63 @@
#include "nvmet.h"
+void nvmet_auth_revoke_key(struct nvmet_host *host, bool set_ctrl)
+{
+ struct key *key = NULL;
+
+ if (set_ctrl) {
+ if (host->dhchap_ctrl_key) {
+ key = host->dhchap_ctrl_key;
+ host->dhchap_ctrl_key = NULL;
+ }
+ } else {
+ if (host->dhchap_key) {
+ key = host->dhchap_key;
+ host->dhchap_key = NULL;
+ }
+ }
+ if (key) {
+ pr_debug("%s: revoke %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(key));
+ key_revoke(key);
+ key_put(key);
+ }
+}
+
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)) {
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_revoke_key(host, set_ctrl);
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(NULL, 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_revoke_key(host, set_ctrl);
+ if (set_ctrl)
+ host->dhchap_ctrl_key = key;
+ else
+ host->dhchap_key = key;
up_write(&nvmet_config_sem);
return 0;
}
@@ -150,10 +161,8 @@ 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 key *host_key, *ctrl_key = NULL;
-#ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
- u8 host_hash, ctrl_hash;
-#endif
+ struct key *key;
+ key_serial_t key_id;
down_read(&nvmet_config_sem);
if (nvmet_is_disc_subsys(ctrl->subsys))
@@ -186,44 +195,74 @@ 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(NULL, host->dhchap_secret,
- strlen(host->dhchap_secret));
- if (IS_ERR(host_key)) {
- ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
+ key = key_get(host->dhchap_key);
+ if (!key) {
+ pr_warn("%s: host key %08x not found\n",
+ __func__, key_serial(host->dhchap_key));
goto out_unlock;
}
- if (host->dhchap_ctrl_secret) {
- ctrl_key = nvme_auth_extract_key(NULL, host->dhchap_ctrl_secret,
- strlen(host->dhchap_ctrl_secret));
- if (IS_ERR(ctrl_key)) {
- key_put(host_key);
- ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
- goto out_unlock;
+ down_read(&key->sem);
+ ret = key_validate(key);
+ if (!ret) {
+ if (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 = key;
+ }
+ key_id = key_serial(key);
+ up_read(&key->sem);
+ if (ret) {
+ pr_warn("host key %08x invalidated\n", key_id);
+ key_put(key);
+ key = ERR_PTR(-EKEYREVOKED);
}
- key_put(ctrl->host_key);
- ctrl->host_key = host_key;
- key_put(ctrl->ctrl_key);
- 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",
- 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));
+ pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
+ nvme_auth_hmac_name(ctrl->shash_id), key_id);
+#endif
+ if (!host->dhchap_ctrl_key) {
+ if (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;
+ }
+ goto out_unlock;
}
+
+ key = key_get(host->dhchap_ctrl_key);
+ if (!key) {
+ pr_warn("%s: ctrl key %08x not found\n",
+ __func__, key_serial(host->dhchap_ctrl_key));
+ goto out_unlock;
+ }
+ down_read(&key->sem);
+ ret = key_validate(key);
+ if (!ret) {
+ if (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 = key;
+ }
+ key_id = key_serial(key);
+ up_read(&key->sem);
+ if (ret) {
+ pr_warn("ctrl key %08x invalidated\n", key_id);
+ key_put(key);
+ goto out_unlock;
+ }
+#ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
+ pr_debug("%s: using dhchap ctrl hash %s key %08x\n", __func__,
+ nvme_auth_hmac_name(ctrl->shash_id), key_id);
#endif
out_unlock:
up_read(&nvmet_config_sem);
@@ -263,12 +302,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 27c50fa533e70e443a2eb3fed5e31e730fb0ce3c..880cf5151588a382045e83952b1f64b95a432ca8 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)
{
- u8 *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_get(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;
+ size_t len;
int ret;
- ret = nvmet_auth_set_key(host, page, false);
+ len = strcspn(page, "\n");
+ if (!len)
+ return -EINVAL;
+ 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)
{
- u8 *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_get(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,18 @@ 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;
+ size_t len;
int ret;
- ret = nvmet_auth_set_key(host, page, true);
+ len = strcspn(page, "\n");
+ if (!len)
+ return -EINVAL;
+ keydata = kstrndup(page, len, GFP_KERNEL);
+ if (!keydata)
+ return -ENOMEM;
+
+ ret = nvmet_auth_set_key(host, keydata, true);
/*
* Re-authentication is a soft state, so keep the
* current authentication valid until the host
@@ -2365,8 +2413,8 @@ 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_revoke_key(host, false);
+ nvmet_auth_revoke_key(host, true);
#endif
kfree(host);
}
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index 5e1f0e78a388252e6e0ec8405006136e278749cb..e708c80e8426f426adf4e98defaa9e77e30d9a58 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;
- u8 *dhchap_secret;
- u8 *dhchap_ctrl_secret;
- int dhchap_key_offset;
- int dhchap_ctrl_key_offset;
- u8 dhchap_key_hash;
- u8 dhchap_ctrl_key_hash;
+ struct key *dhchap_key;
+ struct key *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_revoke_key(struct nvmet_host *host, bool set_ctrl);
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 related [flat|nested] 30+ messages in thread
* [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (4 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
` (2 subsequent siblings)
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
In order to use pre-populated keys update 'nvme_auth_extract_key()'
to accept a key serial number as 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.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/auth.c | 24 +++++++++----
drivers/nvme/host/auth.c | 17 +++++++--
drivers/nvme/host/fabrics.c | 24 +++++++++----
drivers/nvme/host/fabrics.h | 4 +++
drivers/nvme/host/nvme.h | 2 ++
drivers/nvme/host/sysfs.c | 80 ++++++++++++++++++++++++++++--------------
drivers/nvme/target/auth.c | 25 ++++++++++---
drivers/nvme/target/configfs.c | 10 +++---
drivers/nvme/target/nvmet.h | 2 ++
include/linux/nvme-auth.h | 2 +-
10 files changed, 139 insertions(+), 51 deletions(-)
diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
index b7092deed9b06e6ee9bcd819dc6e58ed15069c50..c65bfafe9e3fce4b9f74a382fbf96883ccccb062 100644
--- a/drivers/nvme/common/auth.c
+++ b/drivers/nvme/common/auth.c
@@ -143,20 +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 serial 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 serial 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(struct key *keyring, const char *secret,
- size_t secret_len)
+struct key *nvme_auth_extract_key(struct key *keyring, const char *input,
+ size_t input_len, bool *generated)
{
struct key *key;
- key = nvme_dhchap_psk_create(keyring, secret, secret_len);
+ /* Check if @input is a key serial number */
+ key = nvme_dhchap_psk_lookup(keyring, input);
if (!IS_ERR(key)) {
+ *generated = false;
+ return key;
+ }
+ /* Generate a key from @input data */
+ key = nvme_dhchap_psk_create(keyring, 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 c189096f825eab4e3053021d7437423ecb049c79..470a6e7e436449dadec1a728581dabb3e2c3b6d3 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -1027,19 +1027,29 @@ static void nvme_ctrl_auth_work(struct work_struct *work)
static void nvme_auth_clear_key(struct nvme_ctrl *ctrl, bool is_ctrl)
{
struct key *key;
+ bool generated;
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) {
- dev_dbg(ctrl->device, "%s: revoke dhchap%s key %08x\n",
+ 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));
- key_revoke(key);
key_put(key);
}
}
@@ -1071,6 +1081,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
key_serial(ctrl->opts->dhchap_key));
return -ENOKEY;
}
+ ctrl->host_key_generated = ctrl->opts->dhchap_key_generated;
down_read(&ctrl->host_key->sem);
ret = key_validate(ctrl->host_key);
up_read(&ctrl->host_key->sem);
@@ -1097,6 +1108,8 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
key_serial(ctrl->opts->dhchap_ctrl_key));
return -ENOKEY;
}
+ ctrl->ctrl_key_generated =
+ ctrl->opts->dhchap_ctrl_key_generated;
down_read(&ctrl->ctrl_key->sem);
ret = key_validate(ctrl->ctrl_key);
up_read(&ctrl->ctrl_key->sem);
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 7ed8618257ca9fadc65202261a86b7ead209df7c..cd728768a68fc41135967cae44b1c3c5b0676dd3 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -741,7 +741,9 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
opts->keyring = NULL;
opts->concat = false;
opts->dhchap_key = NULL;
+ opts->dhchap_key_generated = false;
opts->dhchap_ctrl_key = NULL;
+ opts->dhchap_ctrl_key_generated = false;
options = o = kstrdup(buf, GFP_KERNEL);
if (!options)
@@ -1089,7 +1091,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
if (host_secret) {
pr_debug("lookup host identity '%s'\n", host_secret);
key = nvme_auth_extract_key(opts->keyring, host_secret,
- strlen(host_secret));
+ strlen(host_secret),
+ &opts->dhchap_key_generated);
if (IS_ERR(key)) {
ret = PTR_ERR(key);
goto out;
@@ -1104,7 +1107,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
}
pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
- strlen(ctrl_secret));
+ strlen(ctrl_secret),
+ &opts->dhchap_ctrl_key_generated);
if (IS_ERR(key)) {
ret = PTR_ERR(key);
goto out;
@@ -1327,15 +1331,23 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
kfree(opts->host_traddr);
kfree(opts->host_iface);
if (opts->dhchap_key) {
- pr_debug("revoke dhchap host key %08x\n",
+ if (opts->dhchap_key_generated) {
+ pr_debug("revoke dhchap host key %08x\n",
+ key_serial(opts->dhchap_key));
+ key_revoke(opts->dhchap_key);
+ }
+ pr_debug("drop dhchap host key %08x\n",
key_serial(opts->dhchap_key));
- key_revoke(opts->dhchap_key);
key_put(opts->dhchap_key);
}
if (opts->dhchap_ctrl_key) {
- pr_debug("revoke dhchap ctrl key %08x\n",
+ if (opts->dhchap_ctrl_key_generated) {
+ pr_debug("revoke dhchap ctrl key %08x\n",
+ key_serial(opts->dhchap_ctrl_key));
+ key_revoke(opts->dhchap_key);
+ }
+ pr_debug("drop dhchap ctrl key %08x\n",
key_serial(opts->dhchap_ctrl_key));
- key_revoke(opts->dhchap_key);
key_put(opts->dhchap_ctrl_key);
}
kfree(opts);
diff --git a/drivers/nvme/host/fabrics.h b/drivers/nvme/host/fabrics.h
index 786f1b4819f076ac1f133070523be1780ec4d8b7..e55276356bdab4d8c738db329fee0c3a61afd510 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;
struct key *dhchap_key;
struct key *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 1311f5e29b004436e39a723242748a150e36dc68..47caed2ce39786848a5acdb1dc50e4b156694086 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -828,13 +828,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);
return count;
}
@@ -846,6 +847,7 @@ static ssize_t dhchap_secret_store(struct device *dev,
struct nvmf_ctrl_options *opts = ctrl->opts;
struct key *key, *old_key;
char *dhchap_secret;
+ bool generated = false;
size_t len;
int ret;
@@ -859,7 +861,8 @@ static ssize_t dhchap_secret_store(struct device *dev,
memcpy(dhchap_secret, buf, len);
nvme_auth_stop(ctrl);
- key = nvme_auth_extract_key(opts->keyring, dhchap_secret, count);
+ key = nvme_auth_extract_key(opts->keyring, dhchap_secret,
+ len, &generated);
if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
return PTR_ERR(key);
@@ -869,19 +872,25 @@ static ssize_t dhchap_secret_store(struct device *dev,
up_read(&key->sem);
if (ret) {
dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
- dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
- key_revoke(key);
+ if (generated) {
+ dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
+ key_revoke(key);
+ synchronize_rcu();
+ }
key_put(key);
kfree_sensitive(dhchap_secret);
return ret;
}
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->host_key;
- dev_dbg(ctrl->dev, "revoke host key %08x\n",
- key_serial(old_key));
- key_revoke(old_key);
-
+ if (ctrl->host_key_generated) {
+ dev_dbg(ctrl->dev, "revoke host key %08x\n",
+ key_serial(old_key));
+ key_revoke(old_key);
+ synchronize_rcu();
+ }
ctrl->host_key = key;
+ ctrl->host_key_generated = generated;
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key);
kfree_sensitive(dhchap_secret);
@@ -906,13 +915,14 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
down_read(&key->sem);
if (key_validate(key))
count = sysfs_emit(buf, "<invalidated>");
- 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);
return count;
}
@@ -924,6 +934,7 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
struct nvmf_ctrl_options *opts = ctrl->opts;
struct key *key, *old_key;
char *dhchap_secret;
+ bool generated = false;
size_t len;
int ret;
@@ -936,7 +947,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(opts->keyring, dhchap_secret, count);
+ key = nvme_auth_extract_key(opts->keyring, dhchap_secret, len,
+ &generated);
if (IS_ERR(key)) {
kfree_sensitive(dhchap_secret);
return PTR_ERR(key);
@@ -946,19 +958,25 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
up_read(&key->sem);
if (ret) {
dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
- dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
- key_revoke(key);
+ if (generated) {
+ dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
+ key_revoke(key);
+ synchronize_rcu();
+ }
key_put(key);
kfree_sensitive(dhchap_secret);
return ret;
}
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->ctrl_key;
- dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
- key_serial(old_key));
- key_revoke(old_key);
-
+ if (ctrl->ctrl_key_generated) {
+ dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
+ key_serial(old_key));
+ key_revoke(old_key);
+ synchronize_rcu();
+ }
ctrl->ctrl_key = key;
+ ctrl->ctrl_key_generated = generated;
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key);
kfree_sensitive(dhchap_secret);
@@ -983,13 +1001,8 @@ static ssize_t dhchap_key_show(struct device *dev,
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++;
- }
- }
+ else
+ count = sysfs_emit(buf, "%08x\n", key_serial(key));
up_read(&key->sem);
return count;
}
@@ -999,10 +1012,19 @@ 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 = ctrl->ctrl_key;
+ size_t count;
- if (!ctrl->ctrl_key)
+ if (!key)
return 0;
- return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
+ 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);
+ return count;
+
}
static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
#endif
@@ -1066,8 +1088,12 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
#ifdef CONFIG_NVME_HOST_AUTH
if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
return 0;
+ if (a == &dev_attr_dhchap_key.attr && ctrl->host_key_generated)
+ return 0;
if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
return 0;
+ if (a == &dev_attr_dhchap_ctrl_key.attr && ctrl->ctrl_key_generated)
+ return 0;
#endif
return a->mode;
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 33b45b4b202d4b36956d2e1cf0a4036692779f22..4f2489c5d894b24f25eea8e55e26d2afa7ea40b1 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -22,23 +22,34 @@
void nvmet_auth_revoke_key(struct nvmet_host *host, bool set_ctrl)
{
struct key *key = NULL;
+ bool generated = false;
if (set_ctrl) {
if (host->dhchap_ctrl_key) {
key = host->dhchap_ctrl_key;
+ generated = host->dhchap_ctrl_key_generated;
host->dhchap_ctrl_key = NULL;
+ host->dhchap_ctrl_key_generated = false;
}
} else {
if (host->dhchap_key) {
key = host->dhchap_key;
+ generated = host->dhchap_key_generated;
host->dhchap_key = NULL;
+ host->dhchap_key_generated = false;
}
}
if (key) {
- pr_debug("%s: revoke %s key %08x\n",
+ if (generated) {
+ pr_debug("%s: revoke %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(key));
+ key_revoke(key);
+ synchronize_rcu();
+ }
+ pr_debug("%s: drop %s key %08x\n",
__func__, set_ctrl ? "ctrl" : "host",
key_serial(key));
- key_revoke(key);
key_put(key);
}
}
@@ -46,6 +57,7 @@ void nvmet_auth_revoke_key(struct nvmet_host *host, bool set_ctrl)
int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool set_ctrl)
{
+ bool generated = false;
struct key *key;
size_t len;
@@ -56,7 +68,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
return 0;
}
len = strcspn(secret, "\n");
- key = nvme_auth_extract_key(NULL, secret, len);
+ key = nvme_auth_extract_key(NULL, secret, len, &generated);
if (IS_ERR(key)) {
pr_debug("%s: invalid key specification\n", __func__);
return PTR_ERR(key);
@@ -72,10 +84,13 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
up_read(&key->sem);
down_write(&nvmet_config_sem);
nvmet_auth_revoke_key(host, set_ctrl);
- if (set_ctrl)
+ if (set_ctrl) {
host->dhchap_ctrl_key = key;
- else
+ host->dhchap_ctrl_key_generated = generated;
+ } else {
host->dhchap_key = key;
+ host->dhchap_key_generated = generated;
+ }
up_write(&nvmet_config_sem);
return 0;
}
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 880cf5151588a382045e83952b1f64b95a432ca8..319947aa0a40f918e15e9e2130ebf3a79e2d6efe 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -2248,13 +2248,14 @@ 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 = sprintf(page, "%s\n", key->description);
up_read(&key->sem);
key_put(key);
}
@@ -2305,13 +2306,14 @@ 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 = sprintf(page, "%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 e708c80e8426f426adf4e98defaa9e77e30d9a58..c8757cedcd841742ff79c830335cfab6e9fc2401 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;
struct key *dhchap_key;
+ bool dhchap_key_generated;
struct key *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 b8ea0945131d193e9343a5e9fd703f0a4ba3dfec..506d903017352782bb58e4b4e2a8b835fd56a60d 100644
--- a/include/linux/nvme-auth.h
+++ b/include/linux/nvme-auth.h
@@ -38,7 +38,7 @@ void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data,
void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out);
struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
- size_t secret_len);
+ size_t secret_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 related [flat|nested] 30+ messages in thread
* [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (5 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:26 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
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 | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 47caed2ce39786848a5acdb1dc50e4b156694086..ce327a018b0d0a24e26a7f9bfce761157aafe871 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -875,7 +875,6 @@ static ssize_t dhchap_secret_store(struct device *dev,
if (generated) {
dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
key_revoke(key);
- synchronize_rcu();
}
key_put(key);
kfree_sensitive(dhchap_secret);
@@ -897,7 +896,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;
}
@@ -973,7 +972,6 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
key_serial(old_key));
key_revoke(old_key);
- synchronize_rcu();
}
ctrl->ctrl_key = key;
ctrl->ctrl_key_generated = generated;
@@ -983,7 +981,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 related [flat|nested] 30+ messages in thread
* [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (6 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:37 ` sashiko-bot
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
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 TLS keyring by name in addition to
the serial number to simplify configuration file handling.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/host/fabrics.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index cd728768a68fc41135967cae44b1c3c5b0676dd3..71da67b543aa49b6e1a3721313d984f2e50e0ab4 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -14,6 +14,7 @@
#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);
@@ -993,13 +994,23 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
break;
case NVMF_OPT_KEYRING:
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;
+ p = match_strdup(args);
+ if (!p) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ key = request_key(&key_type_keyring, p, NULL);
+ kfree(p);
+ if (IS_ERR(key)) {
+ ret = PTR_ERR(key);
+ goto out;
+ }
+ } else {
+ key = nvmf_parse_key(key_id);
+ if (IS_ERR(key)) {
+ ret = PTR_ERR(key);
+ goto out;
+ }
}
key_put(opts->keyring);
opts->keyring = key;
--
2.51.0
^ permalink raw reply related [flat|nested] 30+ messages in thread
* [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring'
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
` (7 preceding siblings ...)
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
@ 2026-11-20 11:14 ` Hannes Reinecke
2026-11-20 11:39 ` sashiko-bot
8 siblings, 1 reply; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:14 UTC (permalink / raw)
To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
Hannes Reinecke, Chaitanya Kulkarni
Cc: linux-nvme, linux-kernel, Hannes Reinecke
The authentication code now fetches the key from the kernel keystore, so
we should be able to specify which keyring to use for looking up keys.
So add a configfs attribute 'dhchap_keyring' for the 'host' directory
to specify the keyring to use.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
---
drivers/nvme/target/auth.c | 2 +-
drivers/nvme/target/configfs.c | 62 ++++++++++++++++++++++++++++++++++++++++++
drivers/nvme/target/nvmet.h | 1 +
3 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 4f2489c5d894b24f25eea8e55e26d2afa7ea40b1..d583813ef4f274853ec2adf961f2d0c3c3544be5 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -68,7 +68,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
return 0;
}
len = strcspn(secret, "\n");
- key = nvme_auth_extract_key(NULL, secret, len, &generated);
+ key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
if (IS_ERR(key)) {
pr_debug("%s: invalid key specification\n", __func__);
return PTR_ERR(key);
diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
index 319947aa0a40f918e15e9e2130ebf3a79e2d6efe..ffe0f852e59d9d45e7e90ebf8926c8be6fbf688e 100644
--- a/drivers/nvme/target/configfs.c
+++ b/drivers/nvme/target/configfs.c
@@ -2347,6 +2347,62 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
+static ssize_t nvmet_host_dhchap_keyring_show(struct config_item *item,
+ char *page)
+{
+ struct nvmet_host *host = to_host(item);
+ struct key *keyring;
+ ssize_t ret;
+
+ down_read(&nvmet_config_sem);
+ keyring = key_get(host->dhchap_keyring);
+ if (!keyring) {
+ page[0] = '\0';
+ ret = 0;
+ } else {
+ down_read(&keyring->sem);
+ if (key_validate(keyring))
+ ret = sprintf(page, "<invalidated>\n");
+ else
+ ret = sprintf(page, "%s\n", keyring->description);
+ up_read(&keyring->sem);
+ key_put(keyring);
+ }
+ up_read(&nvmet_config_sem);
+ return ret;
+}
+
+static ssize_t nvmet_host_dhchap_keyring_store(struct config_item *item,
+ const char *page, size_t count)
+{
+ struct nvmet_host *host = to_host(item);
+ struct key *keyring;
+ char *desc;
+ size_t len;
+ int ret = 0;
+
+ len = strcspn(page, "\n");
+ if (!len)
+ return -EINVAL;
+ desc = kstrndup(page, len, GFP_KERNEL);
+ if (!desc)
+ return -ENOMEM;
+ keyring = request_key(&key_type_keyring, desc, NULL);
+ if (IS_ERR(keyring)) {
+ ret = PTR_ERR(keyring);
+ } else {
+ down_write(&nvmet_config_sem);
+ key_put(host->dhchap_keyring);
+ host->dhchap_keyring = keyring;
+ up_write(&nvmet_config_sem);
+ }
+ kfree(desc);
+
+ return ret ? -ret : count;
+}
+
+CONFIGFS_ATTR(nvmet_host_, dhchap_keyring);
+
static ssize_t nvmet_host_dhchap_hash_show(struct config_item *item,
char *page)
{
@@ -2404,6 +2460,7 @@ CONFIGFS_ATTR(nvmet_host_, dhchap_dhgroup);
static struct configfs_attribute *nvmet_host_attrs[] = {
&nvmet_host_attr_dhchap_key,
&nvmet_host_attr_dhchap_ctrl_key,
+ &nvmet_host_attr_dhchap_keyring,
&nvmet_host_attr_dhchap_hash,
&nvmet_host_attr_dhchap_dhgroup,
NULL,
@@ -2445,6 +2502,11 @@ static struct config_group *nvmet_hosts_make_group(struct config_group *group,
#ifdef CONFIG_NVME_TARGET_AUTH
/* Default to SHA256 */
host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
+ host->dhchap_keyring = key_lookup(nvme_keyring_id());
+ if (IS_ERR(host->dhchap_keyring)) {
+ kfree(host);
+ return ERR_PTR(-ENOKEY);
+ }
#endif
config_group_init_type_name(&host->group, name, &nvmet_host_type);
diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index c8757cedcd841742ff79c830335cfab6e9fc2401..0f76633c6aa65cec26516bd7f0c55dea815a57f5 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -403,6 +403,7 @@ struct nvmet_host {
bool dhchap_key_generated;
struct key *dhchap_ctrl_key;
bool dhchap_ctrl_key_generated;
+ struct key *dhchap_keyring;
u8 dhchap_hash_id;
u8 dhchap_dhgroup_id;
};
--
2.51.0
^ permalink raw reply related [flat|nested] 30+ messages in thread
* Re: [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
@ 2026-11-20 11:19 ` sashiko-bot
2026-11-20 11:42 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:19 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] nvme-auth: NULL pointer dereference in
nvme_auth_transform_key()
- [Low] nvme-auth: unused variable transformed_data in
nvme_auth_transform_key()
- [Low] nvme-auth: missing memzero_explicit() on error in
nvme_auth_transform_key()
--
commit b03dd5c2705fd9466c887dbc6671b901be8cc15b
Author: Hannes Reinecke <hare@kernel.org>
nvme-auth: modify nvme_auth_transform_key() to return status
This commit changes nvme_auth_transform_key() to return the transformed key
length or a negative error code and pass the allocated raw secret buffer via
an output pointer, updating all callers in the NVMe host and target auth code.
> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index 1fdc4e4ed0b94..569f21ad4fc7e 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
> @@ -317,37 +317,37 @@ 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 *transformed_data;
> + u8 *key_data;
> + size_t transformed_len;
> + int ret;
[Severity: Low]
Is transformed_data unused in this patch? Declaring it here triggers a
-Wunused-variable warning until later commit "nvme-auth: switch to use
'struct key'" uses it.
>
> 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);
> + memcpy(key_data, key->key, key->len);
> + *transformed_secret = key_data;
> + return key->len;
> }
[Severity: High]
Can this cause a NULL pointer dereference in memcpy() if kzalloc() fails
when key->hash == 0? A later patch in the series, commit "nvme-auth: switch
to use 'struct key'", adds a NULL check on key_data, but intermediate
commits can crash here on allocation failure.
> 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) {
> - memzero_explicit(&hmac, sizeof(hmac));
> - return ERR_PTR(-ENOMEM);
> - }
> + return ret;
> + transformed_len = nvme_auth_hmac_hash_len(key->hash);
> + key_data = kzalloc(transformed_len, GFP_KERNEL);
> + if (!key_data)
> + return -ENOMEM;
[Severity: Low]
Should this error path call memzero_explicit(&hmac, sizeof(hmac)) before
returning -ENOMEM, as the code did prior to this patch so key-derived HMAC
state is not left on the stack?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=1
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
@ 2026-11-20 11:26 ` sashiko-bot
2026-11-20 14:52 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:26 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvme-auth: asymmetric and undocumented removal of
synchronize_rcu() in sysfs
--
commit 5f508c67e7480252b016c544aeba6f80edc88b85
Author: Hannes Reinecke <hare@kernel.org>
nvme-auth: wait for authentication to finish when changing keys
This commit flushes ctrl->dhchap_auth_work after queuing re-authentication in
dhchap_secret_store() and dhchap_ctrl_secret_store(), and removes two
synchronize_rcu() calls after key_revoke().
> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index 47caed2ce3978..ce327a018b0d0 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
> @@ -875,7 +875,6 @@ static ssize_t dhchap_secret_store(struct device *dev,
> if (generated) {
> dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
> key_revoke(key);
> - synchronize_rcu();
> }
> key_put(key);
> kfree_sensitive(dhchap_secret);
[ ... ]
> @@ -973,7 +972,6 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
> dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
> key_serial(old_key));
> key_revoke(old_key);
> - synchronize_rcu();
> }
> ctrl->ctrl_key = key;
> ctrl->ctrl_key_generated = generated;
[Severity: Low]
Why are synchronize_rcu() calls removed from mismatched paths in
dhchap_secret_store() and dhchap_ctrl_secret_store(), and without mention in
the commit message?
In dhchap_secret_store(), synchronize_rcu() is removed from the
key_validate() error path, but kept when revoking old_key under
ctrl->dhchap_auth_mutex:
drivers/nvme/host/sysfs.c:dhchap_secret_store() {
...
if (ctrl->host_key_generated) {
dev_dbg(ctrl->dev, "revoke host key %08x\n",
key_serial(old_key));
key_revoke(old_key);
synchronize_rcu();
}
...
}
In dhchap_ctrl_secret_store(), it is removed when revoking old_key, but kept
in the key_validate() error path:
drivers/nvme/host/sysfs.c:dhchap_ctrl_secret_store() {
...
if (generated) {
dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
key_revoke(key);
synchronize_rcu();
}
...
}
Both functions handle keys of type nvme_dhchap_psk_key_type (whose
user_revoke() callback frees the payload via call_rcu()), so the two store
callbacks should handle key_revoke() symmetrically.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=7
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
@ 2026-11-20 11:28 ` sashiko-bot
2026-11-20 12:28 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:28 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Critical] nvme-keyring: out-of-bounds reads and underflows in
nvme_dhchap_psk_preparse()
- [High] nvme-keyring: uninitialized keylen used in
nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: unsigned error check and size mismatch in
nvme_dhchap_psk_read()
- [Medium] nvme-keyring: out-of-bounds memcmp() in
nvme_dhchap_psk_match()
--
commit 329ebc74c917187a04d7c54885592dcb5f74d0dd
Author: Hannes Reinecke <hare@kernel.org>
nvme-keyring: add 'dhchap' key type
This commit adds a 'dhchap' key type to store DH-HMAC-CHAP secret keys with
a user-type compatible payload and a read callback that formats keys in their
base64-encoded representation.
> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
> index 87cb51e1f5a7f..96561f2dd81b8 100644
> --- a/drivers/nvme/common/keyring.c
> +++ b/drivers/nvme/common/keyring.c
[ ... ]
> @@ -253,6 +256,212 @@ 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);
> +}
> +
> +static bool nvme_dhchap_psk_match(const struct key *key,
> + const struct key_match_data *match_data)
> +{
> + const char *match_id;
> + size_t match_len;
> +
> + if (!key->description) {
> + pr_debug("%s: no key description\n", __func__);
> + return false;
> + }
> + if (!match_data->raw_data) {
> + pr_debug("%s: no match data\n", __func__);
> + return false;
> + }
> + match_id = match_data->raw_data;
> + match_len = strlen(match_id);
> + pr_debug("%s: match '%s' '%s' len %zd\n",
> + __func__, match_id, key->description, match_len);
> +
> + return !memcmp(key->description, match_id, match_len);
> +}
[Severity: Medium]
Can memcmp() read past the end of key->description here? key_alloc()
allocates strlen(key->description) + 1 bytes, so searching with a match_id
longer than key->description reads out of bounds, while a shorter match_id
matches any key whose description shares that prefix.
[ ... ]
> +static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
> +{
> + struct user_key_payload *upayload;
> + size_t datalen = prep->datalen, keylen;
> + int ret;
> + u32 crc;
> + u8 version, hmac;
> +
> + if (!prep->data) {
> + pr_debug("%s: Empty data", __func__);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
> +
> + if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> + &version, &hmac) != 2) {
> + pr_debug("%s: invalid key data '%s'\n", __func__,
> + (char *)prep->data);
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -EINVAL;
> + }
> +
> + /* skip header and final ':' character */
> + datalen -= 11;
> +
> + /*
> + * 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) {
> + prep->payload.data[0] = NULL;
> + prep->quotalen = 0;
> + return -ENOMEM;
> + }
> +
> + /* decode the data */
> + prep->quotalen = keylen;
[Severity: High]
Isn't keylen uninitialized when assigned to prep->quotalen?
generic_key_instantiate() passes prep->quotalen to key_payload_reserve(),
corrupting per-user key quota accounting and exposing the lower 16 bits of
the stack value via key->datalen in /proc/keys.
> + prep->payload.data[0] = upayload;
> + ret = base64_decode(prep->data + 10, datalen, upayload->data,
> + true, BASE64_STD);
> + if (ret < 0) {
> + pr_debug("%s: Failed to decode key %s\n",
> + __func__, (char *)prep->data + 10);
> + return ret;
> + }
> + ret -= 4;
> + crc = ~crc32(~0, upayload->data, ret);
[Severity: Critical]
Can malformed or short payloads from add_key() trigger out-of-bounds reads
and integer underflows in nvme_dhchap_psk_preparse()?
add_key() copies plen bytes from userspace into prep->data without appending
a NUL terminator, so sscanf() and the pr_debug() %s calls can read past
prep->data + prep->datalen. Because %*s is assignment-suppressed and
%02hhu matches one or two digits, sscanf() can also return 2 when
prep->datalen < 11 (for example, "DHHC-1:01"), causing datalen -= 11 to
underflow and wrap sizeof(*upayload) + datalen in kzalloc().
Even when prep->datalen >= 11, if base64_decode() returns 0..3 (such as on
an 11-byte payload "DHHC-1:01::"), ret -= 4 becomes negative and wraps to
SIZE_MAX - 3 .. SIZE_MAX when passed as size_t len to crc32(). In addition,
base64_decode() returns -1 on error, which leaks out as -EPERM instead of
-EINVAL, and the decoded key length is never checked against the valid
DH-HMAC-CHAP sizes (32, 48, or 64 bytes).
[ ... ]
> +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 +
> + * CRC length (= 4) +
> + * ':' (= 1)
> + */
> + keylen = nvme_dhchap_decoded_key_size(upayload->datalen) + 15;
> + if (keylen < 0 || !buffer || buflen == 0)
> + return keylen;
> +
> + if (buflen < keylen)
> + return -EINVAL;
[Severity: Medium]
Since keylen is an unsigned size_t, is keylen < 0 always false here?
Adding 15 before checking for an error also turns -EINVAL from
nvme_dhchap_decoded_key_size() into -7 (-E2BIG), and double-counts the
4-byte CRC because nvme_dhchap_decoded_key_size() already returns the
encoded length of the key plus CRC (48, 72, or 92 bytes), so the total
formatted length is 4 bytes larger than what nvme_dhchap_psk_read()
actually writes (+ 11).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=2
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
@ 2026-11-20 11:28 ` sashiko-bot
2026-11-20 14:15 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:28 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] nvmet-auth: missing error abort and invalid status in
nvmet_setup_auth()
- [High] nvmet-auth: missing initialization of ctrl->shash_id in
nvmet_setup_auth()
- [High] nvmet-auth: missing length checks in nvme_dhchap_psk_preparse()
- [Medium] nvmet-auth: memory leak of keydata in
nvmet_host_dhchap_ctrl_key_store()
- [Medium] nvmet-auth: empty writes rejected in configfs key store
callbacks
--
commit 4bc851b3a915dfe8fe3d7c3db8fe8c3fa58c01ca
Author: Hannes Reinecke <hare@kernel.org>
nvmet-auth: parse dhchap key from configfs attribute
This commit converts the nvmet configfs DH-HMAC-CHAP key handling to store
host and controller keys as struct key pointers rather than raw secret
strings.
> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 26457ce4acc2a..33b45b4b202d4 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -19,52 +19,63 @@
[ ... ]
> 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)) {
> 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_revoke_key(host, set_ctrl);
> 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(NULL, secret, len);
> + if (IS_ERR(key)) {
> + pr_debug("%s: invalid key specification\n", __func__);
> + return PTR_ERR(key);
> + }
[Severity: High]
Can passing unvalidated configfs strings into nvme_auth_extract_key()
underflow datalen and ret in nvme_dhchap_psk_preparse()?
nvme_auth_extract_key() calls nvme_dhchap_psk_create() -> key_create() ->
nvme_dhchap_psk_preparse():
drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
...
if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
&version, &hmac) != 2) {
...
return -EINVAL;
}
/* skip header and final ':' character */
datalen -= 11;
...
ret = base64_decode(prep->data + 10, datalen, upayload->data,
true, BASE64_STD);
if (ret < 0) {
...
return ret;
}
ret -= 4;
crc = ~crc32(~0, upayload->data, ret);
...
}
Because %02hhu matches 1 or 2 digits and %*s is not counted in sscanf()'s
return value, a short string such as "DHHC-1:1:" (9 bytes) returns 2 with
datalen < 11, so datalen -= 11 underflows size_t datalen and passes a huge
length to base64_decode(). Similarly, a short payload that decodes to 0..3
bytes (such as "DHHC-1:01:AA==:") makes ret -= 4 negative, which is then
converted to size_t in crc32() and reads past upayload->data.
[ ... ]
> @@ -186,44 +195,74 @@ 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(NULL, host->dhchap_secret,
> - strlen(host->dhchap_secret));
> - if (IS_ERR(host_key)) {
> - ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> + key = key_get(host->dhchap_key);
> + if (!key) {
> + pr_warn("%s: host key %08x not found\n",
> + __func__, key_serial(host->dhchap_key));
> goto out_unlock;
> }
> - if (host->dhchap_ctrl_secret) {
> - ctrl_key = nvme_auth_extract_key(NULL, host->dhchap_ctrl_secret,
> - strlen(host->dhchap_ctrl_secret));
> - if (IS_ERR(ctrl_key)) {
> - key_put(host_key);
> - ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> - goto out_unlock;
> + down_read(&key->sem);
> + ret = key_validate(key);
> + if (!ret) {
> + if (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 = key;
> + }
> + key_id = key_serial(key);
> + up_read(&key->sem);
> + if (ret) {
> + pr_warn("host key %08x invalidated\n", key_id);
> + key_put(key);
> + key = ERR_PTR(-EKEYREVOKED);
> }
[Severity: High]
When key_validate(key) fails on host->dhchap_key, doesn't execution fall
through instead of setting ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE and
jumping to out_unlock?
If host->dhchap_ctrl_key is present and valid, the second key_validate()
overwrites ret with 0, so nvmet_setup_auth() returns 0 while leaving
ctrl->host_key NULL on initial connect, bypassing authentication because
nvmet_has_auth() and nvmet_check_auth_status() treat a NULL ctrl->host_key
as no authentication required. If host->dhchap_ctrl_key is absent or also
invalid, nvmet_setup_auth() returns the negative errno from key_validate()
truncated to u8 instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, and if
key_get() returns NULL it jumps to out_unlock with ret == 0.
> - key_put(ctrl->host_key);
> - ctrl->host_key = host_key;
> - key_put(ctrl->ctrl_key);
> - 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",
> - 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));
> + pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
> + nvme_auth_hmac_name(ctrl->shash_id), key_id);
> +#endif
[Severity: High]
Does dropping ctrl->shash_id = host->dhchap_hash_id leave ctrl->shash_id at
0?
nvmet_auth_negotiate() matches the host's proposed HMAC IDs against
ctrl->shash_id, so leaving it at 0 ignores the host's configured dhchap_hash
attribute and always falls back to fallback_hash_id (and passes 0 to
nvme_auth_hmac_name() in the debug prints here).
> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index 27c50fa533e70..880cf5151588a 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c
[ ... ]
> @@ -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;
> + size_t len;
> int ret;
>
> - ret = nvmet_auth_set_key(host, page, false);
> + len = strcspn(page, "\n");
> + if (!len)
> + return -EINVAL;
> + keydata = kstrndup(page, len, GFP_KERNEL);
> + if (!keydata)
> + return -ENOMEM;
> +
> + ret = nvmet_auth_set_key(host, keydata, false);
> + kfree_sensitive(keydata);
[Severity: Medium]
Does returning -EINVAL when !len in nvmet_host_dhchap_key_store() and
nvmet_host_dhchap_ctrl_key_store() prevent clearing a configured key via
configfs? An empty or newline-only write is rejected before reaching the
!strlen(secret) revocation branch in nvmet_auth_set_key().
[ ... ]
> @@ -2284,9 +2323,18 @@ 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;
> + size_t len;
> int ret;
>
> - ret = nvmet_auth_set_key(host, page, true);
> + len = strcspn(page, "\n");
> + if (!len)
> + return -EINVAL;
> + keydata = kstrndup(page, len, GFP_KERNEL);
> + if (!keydata)
> + return -ENOMEM;
> +
> + ret = nvmet_auth_set_key(host, keydata, true);
> /*
> * Re-authentication is a soft state, so keep the
> * current authentication valid until the host
[Severity: Medium]
Does nvmet_host_dhchap_ctrl_key_store() leak keydata? Unlike
nvmet_host_dhchap_key_store(), it does not call kfree_sensitive(keydata)
after nvmet_auth_set_key().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=5
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
@ 2026-11-20 11:31 ` sashiko-bot
2026-11-20 13:08 ` Hannes Reinecke
2026-11-20 19:24 ` kernel test robot
2026-11-20 19:37 ` kernel test robot
2 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:31 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] nvme-keyring: build failure and mismatched stubs when
!CONFIG_NVME_KEYRING
- [High] nvme-auth: missing key length validation in
nvme_auth_extract_key()
- [Low] nvme-auth: missing kernel-doc param and leftover struct
nvme_dhchap_key
- [Low] nvme-auth: pointer signedness mismatches in key extraction and
reading
--
commit ee0a57ee4ed46fe92599175ec65168d3ebd0acca
Author: Hannes Reinecke <hare@kernel.org>
nvme-auth: switch to use 'struct key'
This commit replaces struct nvme_dhchap_key with struct key using the
dhchap key type across the NVMe common, host, and target authentication
code.
> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index 569f21ad4fc7e..b7092deed9b06 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
[ ... ]
> -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(struct key *keyring, 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;
> - }
> + struct key *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;
> + key = nvme_dhchap_psk_create(keyring, secret, secret_len);
> + if (!IS_ERR(key)) {
> + pr_debug("generated dhchap key %s\n",
> + key->description);
> }
> -
> - /* 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);
> -
> - 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->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);
[Severity: High]
Does replacing the parser here with nvme_dhchap_psk_create() ->
key_create() -> nvme_dhchap_psk_preparse() drop the decoded key length and
prefix length checks, allowing an out-of-bounds read in crc32()?
drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
...
if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
&version, &hmac) != 2) {
...
}
/* skip header and final ':' character */
datalen -= 11;
...
/* decode the data */
prep->quotalen = keylen;
prep->payload.data[0] = upayload;
ret = base64_decode(prep->data + 10, datalen, upayload->data,
true, BASE64_STD);
if (ret < 0) {
...
return ret;
}
ret -= 4;
crc = ~crc32(~0, upayload->data, ret);
if (get_unaligned_le32(upayload->data + ret) != crc) {
...
}
nvme_dhchap_psk_preparse() does not verify that ret is 36, 52, or 68
before subtracting 4, so a secret whose base64 body decodes to 0..3 bytes
(such as "DHHC-1:00:AAAA:") makes ret negative and passes a huge size_t to
crc32(), reading past upayload->data across kernel memory, while other
invalid decoded sizes are accepted.
A 1-digit hash ID also satisfies sscanf(), so a 10-byte secret such as
"DHHC-1:0:a" underflows datalen -= 11 to SIZE_MAX (or has its first base64
character skipped by prep->data + 10), and prep->quotalen is assigned from
the uninitialized local variable keylen. In addition,
nvme_dhchap_psk_create() replaces PTR_ERR(keyref) with ERR_PTR(-ENOKEY) on
key_create() failure instead of propagating -EINVAL, -EKEYREJECTED, or
-ENOMEM.
Finally, dhchap_secret_store() and dhchap_ctrl_secret_store() pass raw
count (including any trailing newline) to nvme_auth_extract_key(), leaving
the trailing ':' in the base64 input; later commits "nvme: parse dhchap
keys during option parsing" and "nvme: allow to pass in key description as
dhchap secret" in the series address the sysfs newline handling, while the
nvme_dhchap_psk_preparse() validation defects and error code clobbering
remain.
[ ... ]
> @@ -317,12 +260,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;
> + long key_len = 0;
> u8 *transformed_data;
> - u8 *key_data;
> + u8 *key_data, key_hash;
[ ... ]
> + key_data = kzalloc(key_len, GFP_KERNEL);
> + if (!key_data) {
> + up_read(&key->sem);
> + return -ENOMEM;
> + }
> + ret = user_read(key, key_data, key_len);
[Severity: Low]
Does passing u8 *key_data to user_read() (which expects char *buffer)
trigger a -Wpointer-sign compiler warning?
Similar pointer-signedness mismatches occur in nvme_auth_extract_key() when
passing const char *secret to const u8 *data of nvme_dhchap_psk_create(),
and in nvmet_setup_auth() when passing u8 * host->dhchap_secret and
host->dhchap_ctrl_secret to strlen() and nvme_auth_extract_key().
> diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
> index 1c5ce635b169d..b8ea0945131d1 100644
> --- a/include/linux/nvme-auth.h
> +++ b/include/linux/nvme-auth.h
> @@ -37,13 +37,10 @@ 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(struct key *keyring, const char *secret,
> + size_t secret_len);
[Severity: Low]
The commit message notes that the unused struct nvme_dhchap_key definition
can be dropped, but struct nvme_dhchap_key is still present at the top of
include/linux/nvme-auth.h. Also, the new kernel-doc comment on
nvme_auth_extract_key() in drivers/nvme/common/auth.c omits @keyring, which
causes a W=1 kernel-doc warning.
> diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
> index ab8971afa9734..9fef4b6add787 100644
> --- a/include/linux/nvme-keyring.h
> +++ b/include/linux/nvme-keyring.h
> @@ -18,12 +18,17 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
>
> key_serial_t nvme_keyring_id(void);
> struct key *nvme_tls_key_lookup(key_serial_t key_id);
> +
> +struct key *nvme_dhchap_psk_create(struct key *keyring,
> + const u8 *data, size_t data_len);
> +struct key *nvme_dhchap_psk_lookup(struct key *keyring, 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, 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)
> @@ -36,7 +41,22 @@ static inline key_serial_t nvme_keyring_id(void)
> }
> static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
> {
> - return ERR_PTR(-ENOTSUPP);
> + return ERR_PTR(-EOPNOTSUPP);
> +}
> +static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
> + const char *hostnqn, const char *subnqn,
> + u8 *data, size_t data_len)
> +{
> + return ERR_PTR(-EOPNOTSUPP);
> +}
> +static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
> + const char *hostnqn, const char *subnqn, u8 hmac);
> +{
> + return ERR_PTR(-EOPNOTSUPP);
> +}
> +u8 nvme_dhchap_psk_hash(struct key *key)
> +{
> + return 0;
> }
> #endif /* !CONFIG_NVME_KEYRING */
> #endif /* _NVME_KEYRING_H */
[Severity: High]
Does the !CONFIG_NVME_KEYRING branch fail to build when
include/linux/nvme-keyring.h is included with CONFIG_NVME_KEYRING disabled?
The nvme_dhchap_psk_lookup() stub has a stray semicolon after its parameter
list and takes four arguments instead of matching the two-argument
declaration in the CONFIG_NVME_KEYRING branch. nvme_dhchap_psk_hash() is
defined in the header without static inline, causing multiple-definition
linker errors across translation units. This branch also defines
nvme_dhchap_psk_refresh() instead of a stub for nvme_dhchap_psk_create(),
and removes hostnqn, subnqn, and hmac_id from the nvme_tls_psk_refresh()
stub signature.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=3
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
@ 2026-11-20 11:31 ` sashiko-bot
2026-11-20 14:48 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:31 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
dhchap_ctrl_key
- [High] nvme-auth: empty/prefix match and self-revocation in
nvme_auth_extract_key()
- [High] nvme-fabrics: link failure calling nvme_auth_extract_key()
without NVME_AUTH
- [Medium] nvme: unlocked access to dhchap keys and flags in sysfs show
callbacks
- [Medium] nvme: broken sysfs visibility for dhchap_key in
nvme_dev_attrs_are_visible()
- [Medium] nvme-auth: host_key leak and stale flags on
nvme_auth_init_ctrl() error
- [Medium] nvmet: memory leak of keydata buffer in
nvmet_host_dhchap_ctrl_key_store()
- [Low] nvme: unsigned count and missing newline in
dhchap_ctrl_secret_show()
- [Low] nvme-auth: missing keyring param in nvme_auth_extract_key()
kernel-doc
--
commit 2b7a36fa0b1a7939d6a4d7f1cdc10818701d7d40
Author: Hannes Reinecke <hare@kernel.org>
nvme: allow to pass in key description as dhchap secret
This commit updates nvme_auth_extract_key() to look up pre-populated
DH-HMAC-CHAP keys in the keyring before falling back to generating an
ephemeral key from raw secret material, and tracks whether keys were
auto-generated across the host and target drivers so only generated keys
are revoked on release.
> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index b7092deed9b06..c65bfafe9e3fc 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
> @@ -143,20 +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 serial 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 serial 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(struct key *keyring, const char *secret,
> - size_t secret_len)
> +struct key *nvme_auth_extract_key(struct key *keyring, const char *input,
> + size_t input_len, bool *generated)
[Severity: Low]
The kernel-doc comment for nvme_auth_extract_key() omits the @keyring
parameter, which triggers a W=1 kernel-doc warning. Also, the comment and
commit message describe @input as a key serial number even though
nvme_dhchap_psk_lookup() searches by key description, and the prototype in
include/linux/nvme-auth.h still names the parameters secret and secret_len.
> {
> struct key *key;
>
> - key = nvme_dhchap_psk_create(keyring, secret, secret_len);
> + /* Check if @input is a key serial number */
> + key = nvme_dhchap_psk_lookup(keyring, input);
> if (!IS_ERR(key)) {
> + *generated = false;
> + return key;
> + }
> + /* Generate a key from @input data */
> + key = nvme_dhchap_psk_create(keyring, input, input_len);
> + if (!IS_ERR(key)) {
> + *generated = true;
> pr_debug("generated dhchap key %s\n",
> key->description);
> }
[Severity: High]
Can calling nvme_dhchap_psk_lookup() here match the wrong key, read out of
bounds, or cause the caller to revoke the key it just looked up?
nvme_dhchap_psk_lookup() searches with nvme_dhchap_psk_match():
drivers/nvme/common/keyring.c:nvme_dhchap_psk_match() {
...
match_id = match_data->raw_data;
match_len = strlen(match_id);
pr_debug("%s: match '%s' '%s' len %zd\n",
__func__, match_id, key->description, match_len);
return !memcmp(key->description, match_id, match_len);
}
Because nvme_dhchap_psk_match() compares match_len bytes with memcmp()
instead of strcmp(), and nvme_auth_extract_key() does not reject
input_len == 0:
1. Writing a single newline to sysfs dhchap_secret_store() or
dhchap_ctrl_secret_store() passes an empty string (match_len == 0), so
!memcmp(key->description, "", 0) matches the first dhchap key in the
keyring with *generated = false.
2. For non-empty inputs, memcmp() prefix-matches any key whose description
starts with input when match_len < strlen(key->description), and reads
past the end of the key->description allocation when match_len >
strlen(key->description) (for example, when passing a raw DHHC-1:...
secret while a key with a shorter description is in the keyring).
3. If nvme_dhchap_psk_lookup() returns the controller's or target host's
currently active auto-generated key (either via an empty write or by
writing the current key's UUID description to dhchap_secret_store(),
dhchap_ctrl_secret_store(), or nvmet_auth_set_key()), the store path
sees that old_key was auto-generated and calls key_revoke(old_key)
without checking key == old_key, revoking the key it then keeps active.
> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
> index c189096f825ea..470a6e7e43644 100644
> --- a/drivers/nvme/host/auth.c
> +++ b/drivers/nvme/host/auth.c
[ ... ]
> @@ -1071,6 +1081,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
> key_serial(ctrl->opts->dhchap_key));
> return -ENOKEY;
> }
> + ctrl->host_key_generated = ctrl->opts->dhchap_key_generated;
> down_read(&ctrl->host_key->sem);
> ret = key_validate(ctrl->host_key);
> up_read(&ctrl->host_key->sem);
> @@ -1097,6 +1108,8 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
> key_serial(ctrl->opts->dhchap_ctrl_key));
> return -ENOKEY;
> }
> + ctrl->ctrl_key_generated =
> + ctrl->opts->dhchap_ctrl_key_generated;
> down_read(&ctrl->ctrl_key->sem);
> ret = key_validate(ctrl->ctrl_key);
> up_read(&ctrl->ctrl_key->sem);
[Severity: Medium]
If acquiring or validating ctrl->ctrl_key fails in nvme_auth_init_ctrl(),
does this leak ctrl->host_key and leave stale *_key_generated flags set?
On failure of key_get(ctrl->opts->dhchap_ctrl_key) or
key_validate(ctrl->ctrl_key), nvme_auth_init_ctrl() returns -ENOKEY or
-EKEYREVOKED without calling nvme_auth_clear_host_key(ctrl). In
nvme_init_ctrl(), a failure from nvme_auth_init_ctrl() jumps to
out_release_instance before device_initialize(&ctrl->ctrl_device), so
nvme_auth_free() never runs and the ctrl->host_key reference is leaked.
The error paths after setting ctrl->host_key_generated and
ctrl->ctrl_key_generated also clear the key pointer without resetting the
corresponding generated boolean to false.
> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index 7ed8618257ca9..cd728768a68fc 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c
[ ... ]
> @@ -1089,7 +1091,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> if (host_secret) {
> pr_debug("lookup host identity '%s'\n", host_secret);
> key = nvme_auth_extract_key(opts->keyring, host_secret,
> - strlen(host_secret));
> + strlen(host_secret),
> + &opts->dhchap_key_generated);
> if (IS_ERR(key)) {
> ret = PTR_ERR(key);
> goto out;
> @@ -1104,7 +1107,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> }
> pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
> key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
> - strlen(ctrl_secret));
> + strlen(ctrl_secret),
> + &opts->dhchap_ctrl_key_generated);
> if (IS_ERR(key)) {
> ret = PTR_ERR(key);
> goto out;
[Severity: High]
Does calling nvme_auth_extract_key() unconditionally in nvmf_parse_options()
break linking when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH?
CONFIG_NVME_FABRICS does not select CONFIG_NVME_AUTH, and
include/linux/nvme-auth.h does not provide a !CONFIG_NVME_AUTH stub for
nvme_auth_extract_key(), so builds with CONFIG_NVME_HOST_AUTH disabled (or
CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m) fail to link.
> @@ -1327,15 +1331,23 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
> kfree(opts->host_traddr);
> kfree(opts->host_iface);
> if (opts->dhchap_key) {
> - pr_debug("revoke dhchap host key %08x\n",
> + if (opts->dhchap_key_generated) {
> + pr_debug("revoke dhchap host key %08x\n",
> + key_serial(opts->dhchap_key));
> + key_revoke(opts->dhchap_key);
> + }
> + pr_debug("drop dhchap host key %08x\n",
> key_serial(opts->dhchap_key));
> - key_revoke(opts->dhchap_key);
> key_put(opts->dhchap_key);
> }
> if (opts->dhchap_ctrl_key) {
> - pr_debug("revoke dhchap ctrl key %08x\n",
> + if (opts->dhchap_ctrl_key_generated) {
> + pr_debug("revoke dhchap ctrl key %08x\n",
> + key_serial(opts->dhchap_ctrl_key));
> + key_revoke(opts->dhchap_key);
> + }
> + pr_debug("drop dhchap ctrl key %08x\n",
> key_serial(opts->dhchap_ctrl_key));
> - key_revoke(opts->dhchap_key);
> key_put(opts->dhchap_ctrl_key);
> }
> kfree(opts);
[Severity: High]
In the opts->dhchap_ctrl_key block, should key_revoke(opts->dhchap_key) be
key_revoke(opts->dhchap_ctrl_key)?
Passing opts->dhchap_key here accesses it after key_put(opts->dhchap_key)
has already dropped the reference (risking a use-after-free), revokes a
pre-populated host key when only the controller key was auto-generated, and
leaves the auto-generated opts->dhchap_ctrl_key unrevoked.
> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index 1311f5e29b004..47caed2ce3978 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
> @@ -828,13 +828,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);
> return count;
> }
[ ... ]
> @@ -869,19 +872,25 @@ static ssize_t dhchap_secret_store(struct device *dev,
> up_read(&key->sem);
> if (ret) {
> dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
> - dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
> - key_revoke(key);
> + if (generated) {
> + dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
> + key_revoke(key);
> + synchronize_rcu();
> + }
> key_put(key);
> kfree_sensitive(dhchap_secret);
> return ret;
> }
> mutex_lock(&ctrl->dhchap_auth_mutex);
> old_key = ctrl->host_key;
> - dev_dbg(ctrl->dev, "revoke host key %08x\n",
> - key_serial(old_key));
> - key_revoke(old_key);
> -
> + if (ctrl->host_key_generated) {
> + dev_dbg(ctrl->dev, "revoke host key %08x\n",
> + key_serial(old_key));
> + key_revoke(old_key);
> + synchronize_rcu();
> + }
> ctrl->host_key = key;
> + ctrl->host_key_generated = generated;
> mutex_unlock(&ctrl->dhchap_auth_mutex);
> key_put(old_key);
> kfree_sensitive(dhchap_secret);
[Severity: Medium]
Can dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(), and
dhchap_ctrl_key_show() race with concurrent stores and trigger a
use-after-free or read a mismatched *_key_generated flag?
Unlike nvmet_host_dhchap_key_show() in drivers/nvme/target/configfs.c, the
host sysfs show callbacks read ctrl->host_key / ctrl->ctrl_key and
ctrl->host_key_generated / ctrl->ctrl_key_generated without holding
ctrl->dhchap_auth_mutex or pinning the key with key_get():
CPU0 CPU1
dhchap_secret_show() dhchap_secret_store()
key = ctrl->host_key;
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->host_key;
ctrl->host_key = new_key;
ctrl->host_key_generated = true;
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key);
down_read(&key->sem); <-- use-after-free
Even if old_key still has another reference (such as when old_key was a
pre-populated user key in the keyring), CPU0 can pair old_key with the newly
written ctrl->host_key_generated == true and dump the pre-populated key's
raw secret via key->type->read() instead of its description.
> @@ -906,13 +915,14 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
> down_read(&key->sem);
> if (key_validate(key))
> count = sysfs_emit(buf, "<invalidated>");
> - 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);
> return count;
> }
[Severity: Low]
In dhchap_ctrl_secret_show(), count is declared as unsigned size_t (unlike
ssize_t in dhchap_secret_show()), so a negative error return from
key->type->read() would wrap to a large positive value, pass count > 0, and
write out of bounds at buf[count] = '\n'. Also,
sysfs_emit(buf, "<invalidated>") is missing the trailing '\n' present in the
other show callbacks.
[ ... ]
> @@ -1066,8 +1088,12 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
> #ifdef CONFIG_NVME_HOST_AUTH
> if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
> return 0;
> + if (a == &dev_attr_dhchap_key.attr && ctrl->host_key_generated)
> + return 0;
> if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
> return 0;
> + if (a == &dev_attr_dhchap_ctrl_key.attr && ctrl->ctrl_key_generated)
> + return 0;
> #endif
>
> return a->mode;
[Severity: Medium]
Should the visibility checks for dev_attr_dhchap_key.attr and
dev_attr_dhchap_ctrl_key.attr also check !ctrl->opts, and how is attribute
visibility updated when keys change at runtime?
Without a !ctrl->opts check, dhchap_key and dhchap_ctrl_key are exposed in
sysfs on non-Fabrics controllers (such as PCIe controllers, where
ctrl->opts is NULL and the *_key_generated fields are false) even though
dhchap_secret and dhchap_ctrl_secret are hidden. In addition, because
nvme_dev_attrs_are_visible() is only evaluated when the sysfs group is
created and dhchap_secret_store() / dhchap_ctrl_secret_store() do not call
sysfs_update_group(), switching a controller at runtime between an
auto-generated secret and a pre-populated key description leaves dhchap_key
and dhchap_ctrl_key stuck in their initial visibility state.
> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index 880cf5151588a..319947aa0a40f 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c
[ ... ]
> @@ -2305,13 +2306,14 @@ 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 = sprintf(page, "%s\n", key->description);
> up_read(&key->sem);
> key_put(key);
> }
[Severity: Medium]
Does nvmet_host_dhchap_ctrl_key_store() just below this function leak
keydata on every write?
drivers/nvme/target/configfs.c:nvmet_host_dhchap_ctrl_key_store() {
...
keydata = kstrndup(page, len, GFP_KERNEL);
if (!keydata)
return -ENOMEM;
ret = nvmet_auth_set_key(host, keydata, true);
...
return ret < 0 ? ret : count;
}
Unlike nvmet_host_dhchap_key_store(), which calls kfree_sensitive(keydata)
after nvmet_auth_set_key(), nvmet_host_dhchap_ctrl_key_store() returns
without freeing keydata, leaking the buffer and leaving key material in slab
memory.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=6
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
@ 2026-11-20 11:32 ` sashiko-bot
2026-11-20 13:55 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:32 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
dhchap_ctrl_key
- [High] nvme: build failure in nvmf_parse_options() and sysfs.c without
auth/keyring
- [High] nvme: removing secret length checks exposes underflows in key
preparsing
- [Medium] nvme: out-of-bounds write on read error in
dhchap_ctrl_secret_show()
- [Medium] nvme-auth: host_key ref leak and inverted check in
nvme_auth_init_ctrl()
- [Medium] nvme: unlocked host_key and ctrl_key access in dhchap sysfs
show callbacks
- [Low] nvme: check !ctrl->opts for dhchap_key attrs in
nvme_dev_attrs_are_visible()
--
commit ddf0d4926edd9ec44ea25839b02c0ba20380f64e
Author: Hannes Reinecke <hare@kernel.org>
nvme: parse dhchap keys during option parsing
This commit parses DH-HMAC-CHAP secrets into struct key pointers during
fabric option parsing instead of storing raw secret strings in
nvmf_ctrl_options, and adds sysfs attributes to expose the host and
controller key serials.
> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
> index 0ddf3834f4ea2..c189096f825ea 100644
> --- a/drivers/nvme/host/auth.c
> +++ b/drivers/nvme/host/auth.c
[ ... ]
> @@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
> INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
> if (!ctrl->opts)
> return 0;
> - ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
> - ctrl->opts->dhchap_secret,
> - strlen(ctrl->opts->dhchap_secret));
> - if (IS_ERR(ctrl->host_key)) {
> - ret = PTR_ERR(ctrl->host_key);
> - ctrl->host_key = NULL;
> - return ret;
> + if (!ctrl->opts->dhchap_key) {
> + nvme_auth_clear_host_key(ctrl);
> + nvme_auth_clear_ctrl_key(ctrl);
> + return 0;
> }
> + if (!ctrl->host_key)
> + nvme_auth_clear_host_key(ctrl);
>
> - ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
> - ctrl->opts->dhchap_ctrl_secret,
> - strlen(ctrl->opts->dhchap_ctrl_secret));
> - if (IS_ERR(ctrl->ctrl_key)) {
> - ret = PTR_ERR(ctrl->ctrl_key);
> - ctrl->ctrl_key = NULL;
> - goto err_free_dhchap_secret;
> + ctrl->host_key = key_get(ctrl->opts->dhchap_key);
> + if (!ctrl->host_key) {
> + dev_warn(ctrl->device,
> + "dhchap host key %08x not present\n",
> + key_serial(ctrl->opts->dhchap_key));
> + return -ENOKEY;
> }
> + down_read(&ctrl->host_key->sem);
> + ret = key_validate(ctrl->host_key);
> + up_read(&ctrl->host_key->sem);
> + if (ret) {
> + dev_warn(ctrl->device,
> + "dhchap host key %08x invalidated\n",
> + key_serial(ctrl->host_key));
> + key_put(ctrl->host_key);
> + ctrl->host_key = NULL;
> + return -ENOKEY;
> + }
> + dev_dbg(ctrl->device,
> + "%s: using dhchap host key %08x\n",
> + __func__, key_serial(ctrl->host_key));
>
> - if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
> - return 0;
> + if (ctrl->ctrl_key)
> + nvme_auth_clear_ctrl_key(ctrl);
> +
> + if (ctrl->opts->dhchap_ctrl_key) {
> + ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
> + if (!ctrl->ctrl_key) {
> + dev_warn(ctrl->device,
> + "dhchap ctrl key %08x not present\n",
> + key_serial(ctrl->opts->dhchap_ctrl_key));
> + return -ENOKEY;
> + }
> + down_read(&ctrl->ctrl_key->sem);
> + ret = key_validate(ctrl->ctrl_key);
> + up_read(&ctrl->ctrl_key->sem);
> + if (ret) {
> + dev_warn(ctrl->device,
> + "dhchap ctrl key %08x invalidated\n",
> + key_serial(ctrl->ctrl_key));
> + key_put(ctrl->ctrl_key);
> + ctrl->ctrl_key = NULL;
> + return -EKEYREVOKED;
> + }
[Severity: Medium]
Do the error returns in the ctrl->opts->dhchap_ctrl_key block leak the
ctrl->host_key reference taken earlier in nvme_auth_init_ctrl()?
When nvme_auth_init_ctrl() fails, nvme_init_ctrl() jumps to
out_release_instance before initializing ctrl->ctrl_device, so
nvme_auth_free() is never called on that error path.
Also, the check before nvme_auth_clear_host_key(ctrl) is inverted
(!ctrl->host_key instead of ctrl->host_key), and nvme_init_ctrl() calls
nvme_auth_init_ctrl() before assigning ctrl->device = &ctrl->ctrl_device,
so the dev_warn() and dev_dbg() calls here pass a NULL ctrl->device.
> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index 5ed91557f0706..7ed8618257ca9 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c
[ ... ]
> @@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> ret = -ENOMEM;
> goto out;
> }
> - if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
> - pr_err("Invalid DH-CHAP secret %s\n", p);
> - kfree_sensitive(p);
> - ret = -EINVAL;
> - goto out;
> - }
> - kfree_sensitive(opts->dhchap_secret);
> - opts->dhchap_secret = p;
> + kfree_sensitive(host_secret);
> + host_secret = p;
> break;
> case NVMF_OPT_DHCHAP_CTRL_SECRET:
> p = match_strdup(args);
> @@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> ret = -ENOMEM;
> goto out;
> }
> - if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
> - pr_err("Invalid DH-CHAP secret %s\n", p);
> - kfree_sensitive(p);
> - ret = -EINVAL;
> - goto out;
> - }
> - kfree_sensitive(opts->dhchap_ctrl_secret);
> - opts->dhchap_ctrl_secret = p;
> + kfree_sensitive(ctrl_secret);
> + ctrl_secret = p;
> break;
[Severity: High]
Does removing the minimum length checks here and in dhchap_secret_store() /
dhchap_ctrl_secret_store() allow short secret strings to underflow datalen
and ret in nvme_dhchap_psk_preparse()?
drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
...
if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
&version, &hmac) != 2) {
...
}
/* skip header and final ':' character */
datalen -= 11;
}
Because %*s is assignment-suppressed and %02hhu matches one or two digits,
sscanf() returns 2 for 8- to 10-byte strings such as "DHHC-1:0:a", causing
unsigned datalen -= 11 to wrap around and wrap the kzalloc() size
calculation sizeof(*upayload) + datalen. For 11- to 15-byte inputs such as
"DHHC-1:00:a" where base64_decode() returns 0..3, ret -= 4 in
nvme_dhchap_psk_preparse() becomes negative and is passed as an unsigned
length to crc32(~0, upayload->data, ret), reading out-of-bounds across
kernel memory.
[ ... ]
> @@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
> opts->fast_io_fail_tmo, ctrl_loss_tmo);
> }
> +
> + opts->host = nvmf_host_add(hostnqn, &hostid);
> + if (IS_ERR(opts->host)) {
> + ret = PTR_ERR(opts->host);
> + opts->host = NULL;
> + goto out;
> + }
> +
> + if (host_secret) {
> + pr_debug("lookup host identity '%s'\n", host_secret);
> + key = nvme_auth_extract_key(opts->keyring, host_secret,
> + strlen(host_secret));
> + if (IS_ERR(key)) {
> + ret = PTR_ERR(key);
> + goto out;
> + }
> + pr_debug("using dhchap host key %08x\n", key_serial(key));
> + opts->dhchap_key = key;
> + }
> + if (ctrl_secret) {
> + if (!opts->dhchap_key) {
> + ret = -EINVAL;
> + goto out;
> + }
> + pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
> + key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
> + strlen(ctrl_secret));
[Severity: High]
Does calling nvme_auth_extract_key() unconditionally here break the build
when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH (or when
CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m)?
include/linux/nvme-auth.h declares nvme_auth_extract_key() without a
!CONFIG_NVME_AUTH stub, and match_token() is an external call so the
compiler cannot dead-code-eliminate the host_secret and ctrl_secret
branches when CONFIG_NVME_HOST_AUTH is disabled.
In addition, drivers/nvme/host/sysfs.c now includes <linux/nvme-keyring.h>
unconditionally, which fails to compile when CONFIG_NVME_KEYRING is
disabled due to a stray semicolon and a non-inline function definition in
the fallback block:
include/linux/nvme-keyring.h:
static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
const char *hostnqn, const char *subnqn, u8 hmac);
{
return ERR_PTR(-EOPNOTSUPP);
}
u8 nvme_dhchap_psk_hash(struct key *key)
{
return 0;
}
[ ... ]
> @@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
> kfree(opts->subsysnqn);
> kfree(opts->host_traddr);
> kfree(opts->host_iface);
> - kfree_sensitive(opts->dhchap_secret);
> - kfree_sensitive(opts->dhchap_ctrl_secret);
> + if (opts->dhchap_key) {
> + pr_debug("revoke dhchap host key %08x\n",
> + key_serial(opts->dhchap_key));
> + key_revoke(opts->dhchap_key);
> + key_put(opts->dhchap_key);
> + }
> + if (opts->dhchap_ctrl_key) {
> + pr_debug("revoke dhchap ctrl key %08x\n",
> + key_serial(opts->dhchap_ctrl_key));
> + key_revoke(opts->dhchap_key);
> + key_put(opts->dhchap_ctrl_key);
> + }
[Severity: High]
In the opts->dhchap_ctrl_key block, should key_revoke() be passed
opts->dhchap_ctrl_key instead of opts->dhchap_key? Passing
opts->dhchap_key dereferences the host key after key_put(opts->dhchap_key)
has already dropped its reference, and leaves opts->dhchap_ctrl_key
unrevoked.
> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index a5c1511e49ac2..1311f5e29b004 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
[ ... ]
> @@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
> struct device_attribute *attr, char *buf)
> {
> struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> - struct nvmf_ctrl_options *opts = ctrl->opts;
> + struct key *key = ctrl->host_key;
> + ssize_t count;
>
> - if (!opts->dhchap_secret)
> + if (!key)
> return sysfs_emit(buf, "none\n");
> - return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
> + down_read(&key->sem);
> + if (key_validate(key))
> + count = sysfs_emit(buf, "<invalidated>\n");
> + else {
> + count = key->type->read(key, buf, PAGE_SIZE);
> + if (count > 0) {
> + buf[count] = '\n';
> + count++;
> + }
> + }
> + up_read(&key->sem);
> + return count;
> }
[ ... ]
> @@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
[ ... ]
> + mutex_lock(&ctrl->dhchap_auth_mutex);
> + old_key = ctrl->host_key;
> + dev_dbg(ctrl->dev, "revoke host key %08x\n",
> + key_serial(old_key));
> + key_revoke(old_key);
> +
> + ctrl->host_key = key;
> + mutex_unlock(&ctrl->dhchap_auth_mutex);
> + key_put(old_key);
[Severity: Medium]
Can a concurrent read of dhchap_secret (or dhchap_ctrl_secret, dhchap_key,
or dhchap_ctrl_key) race with dhchap_secret_store() or
dhchap_ctrl_secret_store() and trigger a use-after-free on the old key?
CPU0 CPU1
dhchap_secret_show()
key = ctrl->host_key;
dhchap_secret_store()
mutex_lock(&ctrl->dhchap_auth_mutex);
old_key = ctrl->host_key;
key_revoke(old_key);
ctrl->host_key = key;
mutex_unlock(&ctrl->dhchap_auth_mutex);
key_put(old_key); <-- frees old_key
down_read(&key->sem); <-- use-after-free
None of dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(),
or dhchap_ctrl_key_show() hold ctrl->dhchap_auth_mutex or take a key_get()
reference before dereferencing ctrl->host_key or ctrl->ctrl_key. Once a
key has been replaced via sysfs store (so opts->dhchap_key no longer holds
a reference), ctrl->host_key holds the only reference, and key_put(old_key)
frees it while a concurrent show callback is using it.
> @@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
> struct device_attribute *attr, char *buf)
> {
> struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> - struct nvmf_ctrl_options *opts = ctrl->opts;
> + struct key *key = ctrl->ctrl_key;
> + size_t count;
>
> - if (!opts->dhchap_ctrl_secret)
> + if (!key)
> return sysfs_emit(buf, "none\n");
> - return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
> + down_read(&key->sem);
> + if (key_validate(key))
> + count = sysfs_emit(buf, "<invalidated>");
> + else {
> + count = key->type->read(key, buf, PAGE_SIZE);
> + if (count > 0) {
> + buf[count] = '\n';
> + count++;
> + }
> + }
> + up_read(&key->sem);
> + return count;
> }
[Severity: Medium]
Can a negative error return from key->type->read() cause an out-of-bounds
write before buf in dhchap_ctrl_secret_show() and dhchap_key_show()?
Unlike dhchap_secret_show(), which declares ssize_t count, both
dhchap_ctrl_secret_show() and dhchap_key_show() declare count as unsigned
size_t. If key->type->read() returns a negative error code (such as
-EINVAL), count wraps to a large positive value, passes count > 0, and
writes '\n' to buf[count] out-of-bounds. Commit "nvme: allow to pass in
key description as dhchap secret" later in this series replaces
key->type->read() in dhchap_key_show(), but dhchap_ctrl_secret_show()
retains the unsigned size_t count and also omits the trailing newline on
"<invalidated>".
[ ... ]
> @@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
> }
>
> static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
> +
> +static ssize_t dhchap_key_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> + struct key *key = ctrl->host_key;
> + size_t count;
> +
> + if (!key)
> + return 0;
> + down_read(&key->sem);
> + if (key_validate(key))
> + count = sysfs_emit(buf, "<invalidated>\n");
> + else {
> + count = key->type->read(key, buf, PAGE_SIZE);
> + if (count > 0) {
> + buf[count] = '\n';
> + count++;
> + }
> + }
> + up_read(&key->sem);
> + return count;
> +}
> +static DEVICE_ATTR_ADMIN_RO(dhchap_key);
> +
> +static ssize_t dhchap_ctrl_key_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
> +
> + if (!ctrl->ctrl_key)
> + return 0;
> + return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
> +}
> +static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
> #endif
>
> static struct attribute *nvme_dev_attrs[] = {
> @@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
> #ifdef CONFIG_NVME_HOST_AUTH
> &dev_attr_dhchap_secret.attr,
> &dev_attr_dhchap_ctrl_secret.attr,
> + &dev_attr_dhchap_key.attr,
> + &dev_attr_dhchap_ctrl_key.attr,
> #endif
[Severity: Low]
Should nvme_dev_attrs_are_visible() hide dev_attr_dhchap_key.attr and
dev_attr_dhchap_ctrl_key.attr when !ctrl->opts, the same way it hides
dev_attr_dhchap_secret.attr and dev_attr_dhchap_ctrl_secret.attr? Without
that check, both attributes appear on non-Fabrics controllers (such as PCIe
controllers) when CONFIG_NVME_HOST_AUTH is enabled.
Also, in this patch dhchap_key_show() outputs the key payload via
key->type->read() instead of key_serial(key) as dhchap_ctrl_key_show() does
(though commit "nvme: allow to pass in key description as dhchap secret"
later in the series updates dhchap_key_show() to emit key_serial()).
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=4
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
@ 2026-11-20 11:37 ` sashiko-bot
2026-11-20 15:02 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:37 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] nvme-fabrics: opt_tokens keyring=%d pattern rejects keyring
names
- [Medium] nvme-fabrics: build failure in nvmf_parse_options() when
!CONFIG_KEYS
--
commit 0ba36c8d6abaed9ca222b0bbab73723c1ce8243d
Author: Hannes Reinecke <hare@kernel.org>
nvme-fabrics: allow to pass in keyring by name
This commit updates nvmf_parse_options() so the NVMF_OPT_KEYRING option
falls back to looking up a keyring by description via request_key() when the
argument is not a positive integer serial number.
> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
> index cd728768a68fc..71da67b543aa4 100644
> --- a/drivers/nvme/host/fabrics.c
> +++ b/drivers/nvme/host/fabrics.c
[ ... ]
> @@ -993,13 +994,23 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
> break;
> case NVMF_OPT_KEYRING:
> 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;
> + p = match_strdup(args);
[Severity: High]
Can a keyring name ever reach this branch while opt_tokens still defines
NVMF_OPT_KEYRING as "keyring=%d"?
drivers/nvme/host/fabrics.c:opt_tokens {
...
{ NVMF_OPT_KEYRING, "keyring=%d" },
...
}
Because match_token() requires "%d" arguments to be valid integers, any
non-numeric name such as "keyring=.nvme" fails to match and makes
nvmf_parse_options() reject the option with -EINVAL in the default switch
case. In addition, reading /dev/nvme-fabrics still advertises "keyring=%d"
to userspace, while passing a non-positive integer serial number such as
"keyring=0" or "keyring=-1" enters this fallback and looks up the number as
a keyring description instead of returning -EINVAL.
> + if (!p) {
> + ret = -ENOMEM;
> + goto out;
> + }
> + key = request_key(&key_type_keyring, p, NULL);
[Severity: Medium]
Does this break the build when CONFIG_NVME_FABRICS is enabled and
CONFIG_KEYS is disabled?
CONFIG_NVME_FABRICS only selects CONFIG_NVME_KEYRING (which selects
CONFIG_KEYS) when CONFIG_NVME_TCP_TLS or CONFIG_NVME_HOST_AUTH is enabled.
Both key_type_keyring in include/linux/key-type.h and request_key() in
include/linux/key.h are declared under #ifdef CONFIG_KEYS with no stub for
!CONFIG_KEYS builds.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=8
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring'
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
@ 2026-11-20 11:39 ` sashiko-bot
2026-11-20 15:27 ` Hannes Reinecke
0 siblings, 1 reply; 30+ messages in thread
From: sashiko-bot @ 2026-11-20 11:39 UTC (permalink / raw)
To: Hannes Reinecke; +Cc: linux-nvme
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] nvmet: keyring reference leak in nvmet_host_release()
- [Medium] nvmet: inverted error return sign in
nvmet_host_dhchap_keyring_store()
- [Medium] nvmet: unlocked host->dhchap_keyring access in
nvmet_auth_set_key()
- [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show()
via sprintf()
--
commit 7f1bb5c7e432b0938e23615bf24751f61d823e03
Author: Hannes Reinecke <hare@kernel.org>
nvmet: add configfs attribute 'dhchap_keyring'
This commit adds a dhchap_keyring configfs attribute to the nvmet host
directory so userspace can specify the keyring used for DH-HMAC-CHAP key
lookups.
> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index 4f2489c5d894b..d583813ef4f27 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -68,7 +68,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
> return 0;
> }
> len = strcspn(secret, "\n");
> - key = nvme_auth_extract_key(NULL, secret, len, &generated);
> + key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
> if (IS_ERR(key)) {
> pr_debug("%s: invalid key specification\n", __func__);
> return PTR_ERR(key);
[Severity: Medium]
Can a concurrent write to dhchap_keyring free host->dhchap_keyring while
nvmet_auth_set_key() is using it?
CPU0 CPU1
nvmet_host_dhchap_key_store()
nvmet_auth_set_key()
keyring = host->dhchap_keyring
nvmet_host_dhchap_keyring_store()
down_write(&nvmet_config_sem)
key_put(host->dhchap_keyring)
host->dhchap_keyring = new_ring
up_write(&nvmet_config_sem)
nvme_auth_extract_key(keyring, ...)
... <-- use-after-free
nvmet_host_dhchap_keyring_store() replaces host->dhchap_keyring and drops
the old reference under nvmet_config_sem, whereas nvmet_auth_set_key()
reads and uses host->dhchap_keyring without holding nvmet_config_sem or
pinning it with key_get().
> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index 319947aa0a40f..ffe0f852e59d9 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c
> @@ -2347,6 +2347,62 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
>
> CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
>
> +static ssize_t nvmet_host_dhchap_keyring_show(struct config_item *item,
> + char *page)
> +{
> + struct nvmet_host *host = to_host(item);
> + struct key *keyring;
> + ssize_t ret;
> +
> + down_read(&nvmet_config_sem);
> + keyring = key_get(host->dhchap_keyring);
> + if (!keyring) {
> + page[0] = '\0';
> + ret = 0;
> + } else {
> + down_read(&keyring->sem);
> + if (key_validate(keyring))
> + ret = sprintf(page, "<invalidated>\n");
> + else
> + ret = sprintf(page, "%s\n", keyring->description);
> + up_read(&keyring->sem);
> + key_put(keyring);
> + }
> + up_read(&nvmet_config_sem);
> + return ret;
> +}
[Severity: Medium]
Can sprintf() write one byte past the end of page here?
add_key() accepts descriptions up to 4095 bytes (KEY_MAX_DESC_SIZE - 1),
and configfs fill_write_buffer() accepts a 4095-byte write without a
trailing newline. Formatting a 4095-byte keyring->description with "%s\n"
writes 4097 bytes (including the NUL terminator) into the 4096-byte
PAGE_SIZE configfs read buffer. Using sysfs_emit() or snprintf(page,
PAGE_SIZE, ...) would avoid the overflow.
> +
> +static ssize_t nvmet_host_dhchap_keyring_store(struct config_item *item,
> + const char *page, size_t count)
> +{
> + struct nvmet_host *host = to_host(item);
> + struct key *keyring;
> + char *desc;
> + size_t len;
> + int ret = 0;
> +
> + len = strcspn(page, "\n");
> + if (!len)
> + return -EINVAL;
> + desc = kstrndup(page, len, GFP_KERNEL);
> + if (!desc)
> + return -ENOMEM;
> + keyring = request_key(&key_type_keyring, desc, NULL);
> + if (IS_ERR(keyring)) {
> + ret = PTR_ERR(keyring);
> + } else {
> + down_write(&nvmet_config_sem);
> + key_put(host->dhchap_keyring);
> + host->dhchap_keyring = keyring;
> + up_write(&nvmet_config_sem);
> + }
> + kfree(desc);
> +
> + return ret ? -ret : count;
> +}
[Severity: Medium]
Does negating ret return a positive value when request_key() fails?
PTR_ERR(keyring) is already negative, so returning -ret causes
configfs_write_iter() to treat the positive errno as a successful write
byte count and silently ignore the failure.
[ ... ]
> @@ -2445,6 +2502,11 @@ static struct config_group *nvmet_hosts_make_group(struct config_group *group,
> #ifdef CONFIG_NVME_TARGET_AUTH
> /* Default to SHA256 */
> host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
> + host->dhchap_keyring = key_lookup(nvme_keyring_id());
> + if (IS_ERR(host->dhchap_keyring)) {
> + kfree(host);
> + return ERR_PTR(-ENOKEY);
> + }
> #endif
>
> config_group_init_type_name(&host->group, name, &nvmet_host_type);
[Severity: High]
Does removing a host group leak host->dhchap_keyring?
Both key_lookup() here and request_key() in
nvmet_host_dhchap_keyring_store() acquire a reference on
host->dhchap_keyring, but nvmet_host_release() never calls
key_put(host->dhchap_keyring):
drivers/nvme/target/configfs.c:nvmet_host_release() {
struct nvmet_host *host = to_host(item);
#ifdef CONFIG_NVME_TARGET_AUTH
nvmet_auth_revoke_key(host, false);
nvmet_auth_revoke_key(host, true);
#endif
kfree(host);
}
Every removed host configfs directory leaks a reference on its configured
keyring.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=9
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status
2026-11-20 11:19 ` sashiko-bot
@ 2026-11-20 11:42 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 11:42 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:19 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issues to consider:
> - [High] nvme-auth: NULL pointer dereference in
> nvme_auth_transform_key()
> - [Low] nvme-auth: unused variable transformed_data in
> nvme_auth_transform_key()
> - [Low] nvme-auth: missing memzero_explicit() on error in
> nvme_auth_transform_key()
> --
>
> commit b03dd5c2705fd9466c887dbc6671b901be8cc15b
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme-auth: modify nvme_auth_transform_key() to return status
>
> This commit changes nvme_auth_transform_key() to return the transformed key
> length or a negative error code and pass the allocated raw secret buffer via
> an output pointer, updating all callers in the NVMe host and target auth code.
>
>> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
>> index 1fdc4e4ed0b94..569f21ad4fc7e 100644
>> --- a/drivers/nvme/common/auth.c
>> +++ b/drivers/nvme/common/auth.c
>> @@ -317,37 +317,37 @@ 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 *transformed_data;
>> + u8 *key_data;
>> + size_t transformed_len;
>> + int ret;
>
> [Severity: Low]
> Is transformed_data unused in this patch? Declaring it here triggers a
> -Wunused-variable warning until later commit "nvme-auth: switch to use
> 'struct key'" uses it.
>
Okay, will be removing it.
>>
>> 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);
>> + memcpy(key_data, key->key, key->len);
>> + *transformed_secret = key_data;
>> + return key->len;
>> }
>
> [Severity: High]
> Can this cause a NULL pointer dereference in memcpy() if kzalloc() fails
> when key->hash == 0? A later patch in the series, commit "nvme-auth: switch
> to use 'struct key'", adds a NULL check on key_data, but intermediate
> commits can crash here on allocation failure.
>
Fixed up for the next version.
>> 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) {
>> - memzero_explicit(&hmac, sizeof(hmac));
>> - return ERR_PTR(-ENOMEM);
>> - }
>> + return ret;
>> + transformed_len = nvme_auth_hmac_hash_len(key->hash);
>> + key_data = kzalloc(transformed_len, GFP_KERNEL);
>> + if (!key_data)
>> + return -ENOMEM;
>
> [Severity: Low]
> Should this error path call memzero_explicit(&hmac, sizeof(hmac)) before
> returning -ENOMEM, as the code did prior to this patch so key-derived HMAC
> state is not left on the stack?
>
Fixed up for the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type
2026-11-20 11:28 ` sashiko-bot
@ 2026-11-20 12:28 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 12:28 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:28 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [Critical] nvme-keyring: out-of-bounds reads and underflows in
> nvme_dhchap_psk_preparse()
> - [High] nvme-keyring: uninitialized keylen used in
> nvme_dhchap_psk_preparse()
> - [Medium] nvme-keyring: unsigned error check and size mismatch in
> nvme_dhchap_psk_read()
> - [Medium] nvme-keyring: out-of-bounds memcmp() in
> nvme_dhchap_psk_match()
> --
>
> commit 329ebc74c917187a04d7c54885592dcb5f74d0dd
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme-keyring: add 'dhchap' key type
>
> This commit adds a 'dhchap' key type to store DH-HMAC-CHAP secret keys with
> a user-type compatible payload and a read callback that formats keys in their
> base64-encoded representation.
>
>> diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
>> index 87cb51e1f5a7f..96561f2dd81b8 100644
>> --- a/drivers/nvme/common/keyring.c
>> +++ b/drivers/nvme/common/keyring.c
> [ ... ]
>> @@ -253,6 +256,212 @@ 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);
>> +}
>> +
>> +static bool nvme_dhchap_psk_match(const struct key *key,
>> + const struct key_match_data *match_data)
>> +{
>> + const char *match_id;
>> + size_t match_len;
>> +
>> + if (!key->description) {
>> + pr_debug("%s: no key description\n", __func__);
>> + return false;
>> + }
>> + if (!match_data->raw_data) {
>> + pr_debug("%s: no match data\n", __func__);
>> + return false;
>> + }
>> + match_id = match_data->raw_data;
>> + match_len = strlen(match_id);
>> + pr_debug("%s: match '%s' '%s' len %zd\n",
>> + __func__, match_id, key->description, match_len);
>> +
>> + return !memcmp(key->description, match_id, match_len);
>> +}
>
> [Severity: Medium]
> Can memcmp() read past the end of key->description here? key_alloc()
> allocates strlen(key->description) + 1 bytes, so searching with a match_id
> longer than key->description reads out of bounds, while a shorter match_id
> matches any key whose description shares that prefix.
>
Correct, the size for both strings should match. I'll fix it up for the
next round.
> [ ... ]
>
>> +static int nvme_dhchap_psk_preparse(struct key_preparsed_payload *prep)
>> +{
>> + struct user_key_payload *upayload;
>> + size_t datalen = prep->datalen, keylen;
>> + int ret;
>> + u32 crc;
>> + u8 version, hmac;
>> +
>> + if (!prep->data) {
>> + pr_debug("%s: Empty data", __func__);
>> + prep->payload.data[0] = NULL;
>> + prep->quotalen = 0;
>> + return -EINVAL;
>> + }
>> +
>> + if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
>> + &version, &hmac) != 2) {
>> + pr_debug("%s: invalid key data '%s'\n", __func__,
>> + (char *)prep->data);
>> + prep->payload.data[0] = NULL;
>> + prep->quotalen = 0;
>> + return -EINVAL;
>> + }
>> +
>> + /* skip header and final ':' character */
>> + datalen -= 11;
>> +
>> + /*
>> + * 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) {
>> + prep->payload.data[0] = NULL;
>> + prep->quotalen = 0;
>> + return -ENOMEM;
>> + }
>> +
>> + /* decode the data */
>> + prep->quotalen = keylen;
>
> [Severity: High]
> Isn't keylen uninitialized when assigned to prep->quotalen?
> generic_key_instantiate() passes prep->quotalen to key_payload_reserve(),
> corrupting per-user key quota accounting and exposing the lower 16 bits of
> the stack value via key->datalen in /proc/keys.
>
Indeed. Fixed up for the next version.
>> + prep->payload.data[0] = upayload;
>> + ret = base64_decode(prep->data + 10, datalen, upayload->data,
>> + true, BASE64_STD);
>> + if (ret < 0) {
>> + pr_debug("%s: Failed to decode key %s\n",
>> + __func__, (char *)prep->data + 10);
>> + return ret;
>> + }
>> + ret -= 4;
>> + crc = ~crc32(~0, upayload->data, ret);
>
> [Severity: Critical]
> Can malformed or short payloads from add_key() trigger out-of-bounds reads
> and integer underflows in nvme_dhchap_psk_preparse()?
>
> add_key() copies plen bytes from userspace into prep->data without appending
> a NUL terminator, so sscanf() and the pr_debug() %s calls can read past
> prep->data + prep->datalen. Because %*s is assignment-suppressed and
> %02hhu matches one or two digits, sscanf() can also return 2 when
> prep->datalen < 11 (for example, "DHHC-1:01"), causing datalen -= 11 to
> underflow and wrap sizeof(*upayload) + datalen in kzalloc().
>
Correct. I've updated the parsing code to the improved version in
nvme_auth_extract_key().
> Even when prep->datalen >= 11, if base64_decode() returns 0..3 (such as on
> an 11-byte payload "DHHC-1:01::"), ret -= 4 becomes negative and wraps to
> SIZE_MAX - 3 .. SIZE_MAX when passed as size_t len to crc32(). In addition,
> base64_decode() returns -1 on error, which leaks out as -EPERM instead of
> -EINVAL, and the decoded key length is never checked against the valid
> DH-HMAC-CHAP sizes (32, 48, or 64 bytes).
>
Ok, fixed up for the next version.
> [ ... ]
>
>> +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 +
>> + * CRC length (= 4) +
>> + * ':' (= 1)
>> + */
>> + keylen = nvme_dhchap_decoded_key_size(upayload->datalen) + 15;
>> + if (keylen < 0 || !buffer || buflen == 0)
>> + return keylen;
>> +
>> + if (buflen < keylen)
>> + return -EINVAL;
>
> [Severity: Medium]
> Since keylen is an unsigned size_t, is keylen < 0 always false here?
> Adding 15 before checking for an error also turns -EINVAL from
> nvme_dhchap_decoded_key_size() into -7 (-E2BIG), and double-counts the
> 4-byte CRC because nvme_dhchap_decoded_key_size() already returns the
> encoded length of the key plus CRC (48, 72, or 92 bytes), so the total
> formatted length is 4 bytes larger than what nvme_dhchap_psk_read()
> actually writes (+ 11).
>
Ok, fixed up for the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
2026-11-20 11:31 ` sashiko-bot
@ 2026-11-20 13:08 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 13:08 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:31 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] nvme-keyring: build failure and mismatched stubs when
> !CONFIG_NVME_KEYRING
> - [High] nvme-auth: missing key length validation in
> nvme_auth_extract_key()
> - [Low] nvme-auth: missing kernel-doc param and leftover struct
> nvme_dhchap_key
> - [Low] nvme-auth: pointer signedness mismatches in key extraction and
> reading
> --
>
> commit ee0a57ee4ed46fe92599175ec65168d3ebd0acca
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme-auth: switch to use 'struct key'
>
> This commit replaces struct nvme_dhchap_key with struct key using the
> dhchap key type across the NVMe common, host, and target authentication
> code.
>
>> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
>> index 569f21ad4fc7e..b7092deed9b06 100644
>> --- a/drivers/nvme/common/auth.c
>> +++ b/drivers/nvme/common/auth.c
> [ ... ]
>> -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(struct key *keyring, 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;
>> - }
>> + struct key *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;
>> + key = nvme_dhchap_psk_create(keyring, secret, secret_len);
>> + if (!IS_ERR(key)) {
>> + pr_debug("generated dhchap key %s\n",
>> + key->description);
>> }
>> -
>> - /* 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);
>> -
>> - 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->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);
>
> [Severity: High]
> Does replacing the parser here with nvme_dhchap_psk_create() ->
> key_create() -> nvme_dhchap_psk_preparse() drop the decoded key length and
> prefix length checks, allowing an out-of-bounds read in crc32()?
>
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
> ...
> if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> &version, &hmac) != 2) {
> ...
> }
>
> /* skip header and final ':' character */
> datalen -= 11;
> ...
> /* decode the data */
> prep->quotalen = keylen;
> prep->payload.data[0] = upayload;
> ret = base64_decode(prep->data + 10, datalen, upayload->data,
> true, BASE64_STD);
> if (ret < 0) {
> ...
> return ret;
> }
> ret -= 4;
> crc = ~crc32(~0, upayload->data, ret);
> if (get_unaligned_le32(upayload->data + ret) != crc) {
> ...
> }
>
> nvme_dhchap_psk_preparse() does not verify that ret is 36, 52, or 68
> before subtracting 4, so a secret whose base64 body decodes to 0..3 bytes
> (such as "DHHC-1:00:AAAA:") makes ret negative and passes a huge size_t to
> crc32(), reading past upayload->data across kernel memory, while other
> invalid decoded sizes are accepted.
>
> A 1-digit hash ID also satisfies sscanf(), so a 10-byte secret such as
> "DHHC-1:0:a" underflows datalen -= 11 to SIZE_MAX (or has its first base64
> character skipped by prep->data + 10), and prep->quotalen is assigned from
> the uninitialized local variable keylen. In addition,
> nvme_dhchap_psk_create() replaces PTR_ERR(keyref) with ERR_PTR(-ENOKEY) on
> key_create() failure instead of propagating -EINVAL, -EKEYREJECTED, or
> -ENOMEM.
>
> Finally, dhchap_secret_store() and dhchap_ctrl_secret_store() pass raw
> count (including any trailing newline) to nvme_auth_extract_key(), leaving
> the trailing ':' in the base64 input; later commits "nvme: parse dhchap
> keys during option parsing" and "nvme: allow to pass in key description as
> dhchap secret" in the series address the sysfs newline handling, while the
> nvme_dhchap_psk_preparse() validation defects and error code clobbering
> remain.
>
That should now be fixed with the reworked version of the previous patch.
> [ ... ]
>
>> @@ -317,12 +260,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;
>> + long key_len = 0;
>> u8 *transformed_data;
>> - u8 *key_data;
>> + u8 *key_data, key_hash;
> [ ... ]
>> + key_data = kzalloc(key_len, GFP_KERNEL);
>> + if (!key_data) {
>> + up_read(&key->sem);
>> + return -ENOMEM;
>> + }
>> + ret = user_read(key, key_data, key_len);
>
> [Severity: Low]
> Does passing u8 *key_data to user_read() (which expects char *buffer)
> trigger a -Wpointer-sign compiler warning?
>
Possibly. Fixed it up for the next version.
> Similar pointer-signedness mismatches occur in nvme_auth_extract_key() when
> passing const char *secret to const u8 *data of nvme_dhchap_psk_create(),
> and in nvmet_setup_auth() when passing u8 * host->dhchap_secret and
> host->dhchap_ctrl_secret to strlen() and nvme_auth_extract_key().
>
Yes, that is confusing. I've now modified host->dhchap_secret and
host->dhchap_ctrl_secret to be of type 'const char *'.
>> diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
>> index 1c5ce635b169d..b8ea0945131d1 100644
>> --- a/include/linux/nvme-auth.h
>> +++ b/include/linux/nvme-auth.h
>> @@ -37,13 +37,10 @@ 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(struct key *keyring, const char *secret,
>> + size_t secret_len);
>
> [Severity: Low]
> The commit message notes that the unused struct nvme_dhchap_key definition
> can be dropped, but struct nvme_dhchap_key is still present at the top of
> include/linux/nvme-auth.h. Also, the new kernel-doc comment on
> nvme_auth_extract_key() in drivers/nvme/common/auth.c omits @keyring, which
> causes a W=1 kernel-doc warning.
>
Fixed with the next version.
>> diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
>> index ab8971afa9734..9fef4b6add787 100644
>> --- a/include/linux/nvme-keyring.h
>> +++ b/include/linux/nvme-keyring.h
>> @@ -18,12 +18,17 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
>>
>> key_serial_t nvme_keyring_id(void);
>> struct key *nvme_tls_key_lookup(key_serial_t key_id);
>> +
>> +struct key *nvme_dhchap_psk_create(struct key *keyring,
>> + const u8 *data, size_t data_len);
>> +struct key *nvme_dhchap_psk_lookup(struct key *keyring, 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, 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)
>> @@ -36,7 +41,22 @@ static inline key_serial_t nvme_keyring_id(void)
>> }
>> static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
>> {
>> - return ERR_PTR(-ENOTSUPP);
>> + return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
>> + const char *hostnqn, const char *subnqn,
>> + u8 *data, size_t data_len)
>> +{
>> + return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
>> + const char *hostnqn, const char *subnqn, u8 hmac);
>> +{
>> + return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +u8 nvme_dhchap_psk_hash(struct key *key)
>> +{
>> + return 0;
>> }
>> #endif /* !CONFIG_NVME_KEYRING */
>> #endif /* _NVME_KEYRING_H */
>
> [Severity: High]
> Does the !CONFIG_NVME_KEYRING branch fail to build when
> include/linux/nvme-keyring.h is included with CONFIG_NVME_KEYRING disabled?
>
> The nvme_dhchap_psk_lookup() stub has a stray semicolon after its parameter
> list and takes four arguments instead of matching the two-argument
> declaration in the CONFIG_NVME_KEYRING branch. nvme_dhchap_psk_hash() is
> defined in the header without static inline, causing multiple-definition
> linker errors across translation units. This branch also defines
> nvme_dhchap_psk_refresh() instead of a stub for nvme_dhchap_psk_create(),
> and removes hostnqn, subnqn, and hmac_id from the nvme_tls_psk_refresh()
> stub signature.
>
Fixed up the declarations with the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 4/9] nvme: parse dhchap keys during option parsing
2026-11-20 11:32 ` sashiko-bot
@ 2026-11-20 13:55 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 13:55 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:32 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 7 potential
> issues to consider:
> - [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
> dhchap_ctrl_key
> - [High] nvme: build failure in nvmf_parse_options() and sysfs.c without
> auth/keyring
> - [High] nvme: removing secret length checks exposes underflows in key
> preparsing
> - [Medium] nvme: out-of-bounds write on read error in
> dhchap_ctrl_secret_show()
> - [Medium] nvme-auth: host_key ref leak and inverted check in
> nvme_auth_init_ctrl()
> - [Medium] nvme: unlocked host_key and ctrl_key access in dhchap sysfs
> show callbacks
> - [Low] nvme: check !ctrl->opts for dhchap_key attrs in
> nvme_dev_attrs_are_visible()
> --
>
> commit ddf0d4926edd9ec44ea25839b02c0ba20380f64e
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme: parse dhchap keys during option parsing
>
> This commit parses DH-HMAC-CHAP secrets into struct key pointers during
> fabric option parsing instead of storing raw secret strings in
> nvmf_ctrl_options, and adds sysfs attributes to expose the host and
> controller key serials.
>
>> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
>> index 0ddf3834f4ea2..c189096f825ea 100644
>> --- a/drivers/nvme/host/auth.c
>> +++ b/drivers/nvme/host/auth.c
> [ ... ]
>> @@ -1038,31 +1056,68 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>> INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
>> if (!ctrl->opts)
>> return 0;
>> - ctrl->host_key = nvme_auth_extract_key(ctrl->opts->keyring,
>> - ctrl->opts->dhchap_secret,
>> - strlen(ctrl->opts->dhchap_secret));
>> - if (IS_ERR(ctrl->host_key)) {
>> - ret = PTR_ERR(ctrl->host_key);
>> - ctrl->host_key = NULL;
>> - return ret;
>> + if (!ctrl->opts->dhchap_key) {
>> + nvme_auth_clear_host_key(ctrl);
>> + nvme_auth_clear_ctrl_key(ctrl);
>> + return 0;
>> }
>> + if (!ctrl->host_key)
>> + nvme_auth_clear_host_key(ctrl);
>>
>> - ctrl->ctrl_key = nvme_auth_extract_key(ctrl->opts->keyring,
>> - ctrl->opts->dhchap_ctrl_secret,
>> - strlen(ctrl->opts->dhchap_ctrl_secret));
>> - if (IS_ERR(ctrl->ctrl_key)) {
>> - ret = PTR_ERR(ctrl->ctrl_key);
>> - ctrl->ctrl_key = NULL;
>> - goto err_free_dhchap_secret;
>> + ctrl->host_key = key_get(ctrl->opts->dhchap_key);
>> + if (!ctrl->host_key) {
>> + dev_warn(ctrl->device,
>> + "dhchap host key %08x not present\n",
>> + key_serial(ctrl->opts->dhchap_key));
>> + return -ENOKEY;
>> }
>> + down_read(&ctrl->host_key->sem);
>> + ret = key_validate(ctrl->host_key);
>> + up_read(&ctrl->host_key->sem);
>> + if (ret) {
>> + dev_warn(ctrl->device,
>> + "dhchap host key %08x invalidated\n",
>> + key_serial(ctrl->host_key));
>> + key_put(ctrl->host_key);
>> + ctrl->host_key = NULL;
>> + return -ENOKEY;
>> + }
>> + dev_dbg(ctrl->device,
>> + "%s: using dhchap host key %08x\n",
>> + __func__, key_serial(ctrl->host_key));
>>
>> - if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
>> - return 0;
>> + if (ctrl->ctrl_key)
>> + nvme_auth_clear_ctrl_key(ctrl);
>> +
>> + if (ctrl->opts->dhchap_ctrl_key) {
>> + ctrl->ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
>> + if (!ctrl->ctrl_key) {
>> + dev_warn(ctrl->device,
>> + "dhchap ctrl key %08x not present\n",
>> + key_serial(ctrl->opts->dhchap_ctrl_key));
>> + return -ENOKEY;
>> + }
>> + down_read(&ctrl->ctrl_key->sem);
>> + ret = key_validate(ctrl->ctrl_key);
>> + up_read(&ctrl->ctrl_key->sem);
>> + if (ret) {
>> + dev_warn(ctrl->device,
>> + "dhchap ctrl key %08x invalidated\n",
>> + key_serial(ctrl->ctrl_key));
>> + key_put(ctrl->ctrl_key);
>> + ctrl->ctrl_key = NULL;
>> + return -EKEYREVOKED;
>> + }
>
> [Severity: Medium]
> Do the error returns in the ctrl->opts->dhchap_ctrl_key block leak the
> ctrl->host_key reference taken earlier in nvme_auth_init_ctrl()?
>
> When nvme_auth_init_ctrl() fails, nvme_init_ctrl() jumps to
> out_release_instance before initializing ctrl->ctrl_device, so
> nvme_auth_free() is never called on that error path.
>
> Also, the check before nvme_auth_clear_host_key(ctrl) is inverted
> (!ctrl->host_key instead of ctrl->host_key), and nvme_init_ctrl() calls
> nvme_auth_init_ctrl() before assigning ctrl->device = &ctrl->ctrl_device,
> so the dev_warn() and dev_dbg() calls here pass a NULL ctrl->device.
>
Restructured the function to only update the ->host_key and ->ctrl_key
values once the keys are validated.
>> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
>> index 5ed91557f0706..7ed8618257ca9 100644
>> --- a/drivers/nvme/host/fabrics.c
>> +++ b/drivers/nvme/host/fabrics.c
> [ ... ]
>> @@ -1020,14 +1024,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> ret = -ENOMEM;
>> goto out;
>> }
>> - if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
>> - pr_err("Invalid DH-CHAP secret %s\n", p);
>> - kfree_sensitive(p);
>> - ret = -EINVAL;
>> - goto out;
>> - }
>> - kfree_sensitive(opts->dhchap_secret);
>> - opts->dhchap_secret = p;
>> + kfree_sensitive(host_secret);
>> + host_secret = p;
>> break;
>> case NVMF_OPT_DHCHAP_CTRL_SECRET:
>> p = match_strdup(args);
>> @@ -1035,14 +1033,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> ret = -ENOMEM;
>> goto out;
>> }
>> - if (strlen(p) < 11 || strncmp(p, "DHHC-1:", 7)) {
>> - pr_err("Invalid DH-CHAP secret %s\n", p);
>> - kfree_sensitive(p);
>> - ret = -EINVAL;
>> - goto out;
>> - }
>> - kfree_sensitive(opts->dhchap_ctrl_secret);
>> - opts->dhchap_ctrl_secret = p;
>> + kfree_sensitive(ctrl_secret);
>> + ctrl_secret = p;
>> break;
>
> [Severity: High]
> Does removing the minimum length checks here and in dhchap_secret_store() /
> dhchap_ctrl_secret_store() allow short secret strings to underflow datalen
> and ret in nvme_dhchap_psk_preparse()?
>
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
> ...
> if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> &version, &hmac) != 2) {
> ...
> }
>
> /* skip header and final ':' character */
> datalen -= 11;
> }
>
> Because %*s is assignment-suppressed and %02hhu matches one or two digits,
> sscanf() returns 2 for 8- to 10-byte strings such as "DHHC-1:0:a", causing
> unsigned datalen -= 11 to wrap around and wrap the kzalloc() size
> calculation sizeof(*upayload) + datalen. For 11- to 15-byte inputs such as
> "DHHC-1:00:a" where base64_decode() returns 0..3, ret -= 4 in
> nvme_dhchap_psk_preparse() becomes negative and is passed as an unsigned
> length to crc32(~0, upayload->data, ret), reading out-of-bounds across
> kernel memory.
>
The parsing routines have been fixed with reworks of the previous patches.
> [ ... ]
>
>> @@ -1086,6 +1078,41 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
>> opts->fast_io_fail_tmo, ctrl_loss_tmo);
>> }
>> +
>> + opts->host = nvmf_host_add(hostnqn, &hostid);
>> + if (IS_ERR(opts->host)) {
>> + ret = PTR_ERR(opts->host);
>> + opts->host = NULL;
>> + goto out;
>> + }
>> +
>> + if (host_secret) {
>> + pr_debug("lookup host identity '%s'\n", host_secret);
>> + key = nvme_auth_extract_key(opts->keyring, host_secret,
>> + strlen(host_secret));
>> + if (IS_ERR(key)) {
>> + ret = PTR_ERR(key);
>> + goto out;
>> + }
>> + pr_debug("using dhchap host key %08x\n", key_serial(key));
>> + opts->dhchap_key = key;
>> + }
>> + if (ctrl_secret) {
>> + if (!opts->dhchap_key) {
>> + ret = -EINVAL;
>> + goto out;
>> + }
>> + pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
>> + key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
>> + strlen(ctrl_secret));
>
> [Severity: High]
> Does calling nvme_auth_extract_key() unconditionally here break the build
> when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH (or when
> CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m)?
>
> include/linux/nvme-auth.h declares nvme_auth_extract_key() without a
> !CONFIG_NVME_AUTH stub, and match_token() is an external call so the
> compiler cannot dead-code-eliminate the host_secret and ctrl_secret
> branches when CONFIG_NVME_HOST_AUTH is disabled.
>
> In addition, drivers/nvme/host/sysfs.c now includes <linux/nvme-keyring.h>
> unconditionally, which fails to compile when CONFIG_NVME_KEYRING is
> disabled due to a stray semicolon and a non-inline function definition in
> the fallback block:
>
> include/linux/nvme-keyring.h:
> static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
> const char *hostnqn, const char *subnqn, u8 hmac);
> {
> return ERR_PTR(-EOPNOTSUPP);
> }
> u8 nvme_dhchap_psk_hash(struct key *key)
> {
> return 0;
> }
>
Fixed up with the next version.
> [ ... ]
>
>> @@ -1304,8 +1326,18 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>> kfree(opts->subsysnqn);
>> kfree(opts->host_traddr);
>> kfree(opts->host_iface);
>> - kfree_sensitive(opts->dhchap_secret);
>> - kfree_sensitive(opts->dhchap_ctrl_secret);
>> + if (opts->dhchap_key) {
>> + pr_debug("revoke dhchap host key %08x\n",
>> + key_serial(opts->dhchap_key));
>> + key_revoke(opts->dhchap_key);
>> + key_put(opts->dhchap_key);
>> + }
>> + if (opts->dhchap_ctrl_key) {
>> + pr_debug("revoke dhchap ctrl key %08x\n",
>> + key_serial(opts->dhchap_ctrl_key));
>> + key_revoke(opts->dhchap_key);
>> + key_put(opts->dhchap_ctrl_key);
>> + }
>
> [Severity: High]
> In the opts->dhchap_ctrl_key block, should key_revoke() be passed
> opts->dhchap_ctrl_key instead of opts->dhchap_key? Passing
> opts->dhchap_key dereferences the host key after key_put(opts->dhchap_key)
> has already dropped its reference, and leaves opts->dhchap_ctrl_key
> unrevoked.
>
Of course. Fixed up with the next version.
>> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
>> index a5c1511e49ac2..1311f5e29b004 100644
>> --- a/drivers/nvme/host/sysfs.c
>> +++ b/drivers/nvme/host/sysfs.c
> [ ... ]
>> @@ -818,11 +820,23 @@ static ssize_t dhchap_secret_show(struct device *dev,
>> struct device_attribute *attr, char *buf)
>> {
>> struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> - struct nvmf_ctrl_options *opts = ctrl->opts;
>> + struct key *key = ctrl->host_key;
>> + ssize_t count;
>>
>> - if (!opts->dhchap_secret)
>> + if (!key)
>> return sysfs_emit(buf, "none\n");
>> - return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
>> + down_read(&key->sem);
>> + if (key_validate(key))
>> + count = sysfs_emit(buf, "<invalidated>\n");
>> + else {
>> + count = key->type->read(key, buf, PAGE_SIZE);
>> + if (count > 0) {
>> + buf[count] = '\n';
>> + count++;
>> + }
>> + }
>> + up_read(&key->sem);
>> + return count;
>> }
> [ ... ]
>> @@ -830,36 +844,47 @@ static ssize_t dhchap_secret_store(struct device *dev,
> [ ... ]
>> + mutex_lock(&ctrl->dhchap_auth_mutex);
>> + old_key = ctrl->host_key;
>> + dev_dbg(ctrl->dev, "revoke host key %08x\n",
>> + key_serial(old_key));
>> + key_revoke(old_key);
>> +
>> + ctrl->host_key = key;
>> + mutex_unlock(&ctrl->dhchap_auth_mutex);
>> + key_put(old_key);
>
> [Severity: Medium]
> Can a concurrent read of dhchap_secret (or dhchap_ctrl_secret, dhchap_key,
> or dhchap_ctrl_key) race with dhchap_secret_store() or
> dhchap_ctrl_secret_store() and trigger a use-after-free on the old key?
>
> CPU0 CPU1
> dhchap_secret_show()
> key = ctrl->host_key;
> dhchap_secret_store()
> mutex_lock(&ctrl->dhchap_auth_mutex);
> old_key = ctrl->host_key;
> key_revoke(old_key);
> ctrl->host_key = key;
> mutex_unlock(&ctrl->dhchap_auth_mutex);
> key_put(old_key); <-- frees old_key
> down_read(&key->sem); <-- use-after-free
>
> None of dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(),
> or dhchap_ctrl_key_show() hold ctrl->dhchap_auth_mutex or take a key_get()
> reference before dereferencing ctrl->host_key or ctrl->ctrl_key. Once a
> key has been replaced via sysfs store (so opts->dhchap_key no longer holds
> a reference), ctrl->host_key holds the only reference, and key_put(old_key)
> frees it while a concurrent show callback is using it.
>
Updated the _show functions to take the dhchap_auth_mutex.
>> @@ -873,11 +898,23 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
>> struct device_attribute *attr, char *buf)
>> {
>> struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> - struct nvmf_ctrl_options *opts = ctrl->opts;
>> + struct key *key = ctrl->ctrl_key;
>> + size_t count;
>>
>> - if (!opts->dhchap_ctrl_secret)
>> + if (!key)
>> return sysfs_emit(buf, "none\n");
>> - return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
>> + down_read(&key->sem);
>> + if (key_validate(key))
>> + count = sysfs_emit(buf, "<invalidated>");
>> + else {
>> + count = key->type->read(key, buf, PAGE_SIZE);
>> + if (count > 0) {
>> + buf[count] = '\n';
>> + count++;
>> + }
>> + }
>> + up_read(&key->sem);
>> + return count;
>> }
>
> [Severity: Medium]
> Can a negative error return from key->type->read() cause an out-of-bounds
> write before buf in dhchap_ctrl_secret_show() and dhchap_key_show()?
>
> Unlike dhchap_secret_show(), which declares ssize_t count, both
> dhchap_ctrl_secret_show() and dhchap_key_show() declare count as unsigned
> size_t. If key->type->read() returns a negative error code (such as
> -EINVAL), count wraps to a large positive value, passes count > 0, and
> writes '\n' to buf[count] out-of-bounds. Commit "nvme: allow to pass in
> key description as dhchap secret" later in this series replaces
> key->type->read() in dhchap_key_show(), but dhchap_ctrl_secret_show()
> retains the unsigned size_t count and also omits the trailing newline on
> "<invalidated>".
>
Used 'ssize_t' here, too.
> [ ... ]
>
>> @@ -925,6 +970,41 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
>> }
>>
>> static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
>> +
>> +static ssize_t dhchap_key_show(struct device *dev,
>> + struct device_attribute *attr, char *buf)
>> +{
>> + struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> + struct key *key = ctrl->host_key;
>> + size_t count;
>> +
>> + if (!key)
>> + return 0;
>> + down_read(&key->sem);
>> + if (key_validate(key))
>> + count = sysfs_emit(buf, "<invalidated>\n");
>> + else {
>> + count = key->type->read(key, buf, PAGE_SIZE);
>> + if (count > 0) {
>> + buf[count] = '\n';
>> + count++;
>> + }
>> + }
>> + up_read(&key->sem);
>> + return count;
>> +}
>> +static DEVICE_ATTR_ADMIN_RO(dhchap_key);
>> +
>> +static ssize_t dhchap_ctrl_key_show(struct device *dev,
>> + struct device_attribute *attr, char *buf)
>> +{
>> + struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
>> +
>> + if (!ctrl->ctrl_key)
>> + return 0;
>> + return sysfs_emit(buf, "%08x\n", key_serial(ctrl->ctrl_key));
>> +}
>> +static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);
>> #endif
>>
>> static struct attribute *nvme_dev_attrs[] = {
>> @@ -956,6 +1036,8 @@ static struct attribute *nvme_dev_attrs[] = {
>> #ifdef CONFIG_NVME_HOST_AUTH
>> &dev_attr_dhchap_secret.attr,
>> &dev_attr_dhchap_ctrl_secret.attr,
>> + &dev_attr_dhchap_key.attr,
>> + &dev_attr_dhchap_ctrl_key.attr,
>> #endif
>
> [Severity: Low]
> Should nvme_dev_attrs_are_visible() hide dev_attr_dhchap_key.attr and
> dev_attr_dhchap_ctrl_key.attr when !ctrl->opts, the same way it hides
> dev_attr_dhchap_secret.attr and dev_attr_dhchap_ctrl_secret.attr? Without
> that check, both attributes appear on non-Fabrics controllers (such as PCIe
> controllers) when CONFIG_NVME_HOST_AUTH is enabled.
>
Yes, it should. Updated for the next version.
> Also, in this patch dhchap_key_show() outputs the key payload via
> key->type->read() instead of key_serial(key) as dhchap_ctrl_key_show() does
> (though commit "nvme: allow to pass in key description as dhchap secret"
> later in the series updates dhchap_key_show() to emit key_serial()).
>
Fixed up with the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute
2026-11-20 11:28 ` sashiko-bot
@ 2026-11-20 14:15 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 14:15 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:28 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 5 potential
> issues to consider:
> - [High] nvmet-auth: missing error abort and invalid status in
> nvmet_setup_auth()
> - [High] nvmet-auth: missing initialization of ctrl->shash_id in
> nvmet_setup_auth()
> - [High] nvmet-auth: missing length checks in nvme_dhchap_psk_preparse()
> - [Medium] nvmet-auth: memory leak of keydata in
> nvmet_host_dhchap_ctrl_key_store()
> - [Medium] nvmet-auth: empty writes rejected in configfs key store
> callbacks
> --
>
> commit 4bc851b3a915dfe8fe3d7c3db8fe8c3fa58c01ca
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvmet-auth: parse dhchap key from configfs attribute
>
> This commit converts the nvmet configfs DH-HMAC-CHAP key handling to store
> host and controller keys as struct key pointers rather than raw secret
> strings.
>
>> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
>> index 26457ce4acc2a..33b45b4b202d4 100644
>> --- a/drivers/nvme/target/auth.c
>> +++ b/drivers/nvme/target/auth.c
>> @@ -19,52 +19,63 @@
>
> [ ... ]
>
>> 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)) {
>> 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_revoke_key(host, set_ctrl);
>> 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(NULL, secret, len);
>> + if (IS_ERR(key)) {
>> + pr_debug("%s: invalid key specification\n", __func__);
>> + return PTR_ERR(key);
>> + }
>
> [Severity: High]
> Can passing unvalidated configfs strings into nvme_auth_extract_key()
> underflow datalen and ret in nvme_dhchap_psk_preparse()?
>
> nvme_auth_extract_key() calls nvme_dhchap_psk_create() -> key_create() ->
> nvme_dhchap_psk_preparse():
>
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
> ...
> if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> &version, &hmac) != 2) {
> ...
> return -EINVAL;
> }
>
> /* skip header and final ':' character */
> datalen -= 11;
> ...
> ret = base64_decode(prep->data + 10, datalen, upayload->data,
> true, BASE64_STD);
> if (ret < 0) {
> ...
> return ret;
> }
> ret -= 4;
> crc = ~crc32(~0, upayload->data, ret);
> ...
> }
>
> Because %02hhu matches 1 or 2 digits and %*s is not counted in sscanf()'s
> return value, a short string such as "DHHC-1:1:" (9 bytes) returns 2 with
> datalen < 11, so datalen -= 11 underflows size_t datalen and passes a huge
> length to base64_decode(). Similarly, a short payload that decodes to 0..3
> bytes (such as "DHHC-1:01:AA==:") makes ret -= 4 negative, which is then
> converted to size_t in crc32() and reads past upayload->data.
>
The parsing routines have been fixed up with updates to earlier patches.
> [ ... ]
>
>> @@ -186,44 +195,74 @@ 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(NULL, host->dhchap_secret,
>> - strlen(host->dhchap_secret));
>> - if (IS_ERR(host_key)) {
>> - ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
>> + key = key_get(host->dhchap_key);
>> + if (!key) {
>> + pr_warn("%s: host key %08x not found\n",
>> + __func__, key_serial(host->dhchap_key));
>> goto out_unlock;
>> }
>> - if (host->dhchap_ctrl_secret) {
>> - ctrl_key = nvme_auth_extract_key(NULL, host->dhchap_ctrl_secret,
>> - strlen(host->dhchap_ctrl_secret));
>> - if (IS_ERR(ctrl_key)) {
>> - key_put(host_key);
>> - ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
>> - goto out_unlock;
>> + down_read(&key->sem);
>> + ret = key_validate(key);
>> + if (!ret) {
>> + if (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 = key;
>> + }
>> + key_id = key_serial(key);
>> + up_read(&key->sem);
>> + if (ret) {
>> + pr_warn("host key %08x invalidated\n", key_id);
>> + key_put(key);
>> + key = ERR_PTR(-EKEYREVOKED);
>> }
>
> [Severity: High]
> When key_validate(key) fails on host->dhchap_key, doesn't execution fall
> through instead of setting ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE and
> jumping to out_unlock?
>
> If host->dhchap_ctrl_key is present and valid, the second key_validate()
> overwrites ret with 0, so nvmet_setup_auth() returns 0 while leaving
> ctrl->host_key NULL on initial connect, bypassing authentication because
> nvmet_has_auth() and nvmet_check_auth_status() treat a NULL ctrl->host_key
> as no authentication required. If host->dhchap_ctrl_key is absent or also
> invalid, nvmet_setup_auth() returns the negative errno from key_validate()
> truncated to u8 instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, and if
> key_get() returns NULL it jumps to out_unlock with ret == 0.
>
Correct. Fixed up with the next version.
>> - key_put(ctrl->host_key);
>> - ctrl->host_key = host_key;
>> - key_put(ctrl->ctrl_key);
>> - 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",
>> - 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));
>> + pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
>> + nvme_auth_hmac_name(ctrl->shash_id), key_id);
>> +#endif
>
> [Severity: High]
> Does dropping ctrl->shash_id = host->dhchap_hash_id leave ctrl->shash_id at
> 0?
>
> nvmet_auth_negotiate() matches the host's proposed HMAC IDs against
> ctrl->shash_id, so leaving it at 0 ignores the host's configured dhchap_hash
> attribute and always falls back to fallback_hash_id (and passes 0 to
> nvme_auth_hmac_name() in the debug prints here).
>
Of course. Reverted with the next version.
>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 27c50fa533e70..880cf5151588a 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
>
> [ ... ]
>
>> @@ -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;
>> + size_t len;
>> int ret;
>>
>> - ret = nvmet_auth_set_key(host, page, false);
>> + len = strcspn(page, "\n");
>> + if (!len)
>> + return -EINVAL;
>> + keydata = kstrndup(page, len, GFP_KERNEL);
>> + if (!keydata)
>> + return -ENOMEM;
>> +
>> + ret = nvmet_auth_set_key(host, keydata, false);
>> + kfree_sensitive(keydata);
>
> [Severity: Medium]
> Does returning -EINVAL when !len in nvmet_host_dhchap_key_store() and
> nvmet_host_dhchap_ctrl_key_store() prevent clearing a configured key via
> configfs? An empty or newline-only write is rejected before reaching the
> !strlen(secret) revocation branch in nvmet_auth_set_key().
>
Fixed up with the next version.
> [ ... ]
>
>> @@ -2284,9 +2323,18 @@ 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;
>> + size_t len;
>> int ret;
>>
>> - ret = nvmet_auth_set_key(host, page, true);
>> + len = strcspn(page, "\n");
>> + if (!len)
>> + return -EINVAL;
>> + keydata = kstrndup(page, len, GFP_KERNEL);
>> + if (!keydata)
>> + return -ENOMEM;
>> +
>> + ret = nvmet_auth_set_key(host, keydata, true);
>> /*
>> * Re-authentication is a soft state, so keep the
>> * current authentication valid until the host
>
> [Severity: Medium]
> Does nvmet_host_dhchap_ctrl_key_store() leak keydata? Unlike
> nvmet_host_dhchap_key_store(), it does not call kfree_sensitive(keydata)
> after nvmet_auth_set_key().
>
Fixed up with the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret
2026-11-20 11:31 ` sashiko-bot
@ 2026-11-20 14:48 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 14:48 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:31 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 9 potential
> issues to consider:
> - [High] nvme-fabrics: wrong key revoked in nvmf_free_options() for
> dhchap_ctrl_key
> - [High] nvme-auth: empty/prefix match and self-revocation in
> nvme_auth_extract_key()
> - [High] nvme-fabrics: link failure calling nvme_auth_extract_key()
> without NVME_AUTH
> - [Medium] nvme: unlocked access to dhchap keys and flags in sysfs show
> callbacks
> - [Medium] nvme: broken sysfs visibility for dhchap_key in
> nvme_dev_attrs_are_visible()
> - [Medium] nvme-auth: host_key leak and stale flags on
> nvme_auth_init_ctrl() error
> - [Medium] nvmet: memory leak of keydata buffer in
> nvmet_host_dhchap_ctrl_key_store()
> - [Low] nvme: unsigned count and missing newline in
> dhchap_ctrl_secret_show()
> - [Low] nvme-auth: missing keyring param in nvme_auth_extract_key()
> kernel-doc
> --
>
> commit 2b7a36fa0b1a7939d6a4d7f1cdc10818701d7d40
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme: allow to pass in key description as dhchap secret
>
> This commit updates nvme_auth_extract_key() to look up pre-populated
> DH-HMAC-CHAP keys in the keyring before falling back to generating an
> ephemeral key from raw secret material, and tracks whether keys were
> auto-generated across the host and target drivers so only generated keys
> are revoked on release.
>
>> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
>> index b7092deed9b06..c65bfafe9e3fc 100644
>> --- a/drivers/nvme/common/auth.c
>> +++ b/drivers/nvme/common/auth.c
>> @@ -143,20 +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 serial 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 serial 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(struct key *keyring, const char *secret,
>> - size_t secret_len)
>> +struct key *nvme_auth_extract_key(struct key *keyring, const char *input,
>> + size_t input_len, bool *generated)
>
> [Severity: Low]
> The kernel-doc comment for nvme_auth_extract_key() omits the @keyring
> parameter, which triggers a W=1 kernel-doc warning. Also, the comment and
> commit message describe @input as a key serial number even though
> nvme_dhchap_psk_lookup() searches by key description, and the prototype in
> include/linux/nvme-auth.h still names the parameters secret and secret_len.
>
>> {
>> struct key *key;
>>
>> - key = nvme_dhchap_psk_create(keyring, secret, secret_len);
>> + /* Check if @input is a key serial number */
>> + key = nvme_dhchap_psk_lookup(keyring, input);
>> if (!IS_ERR(key)) {
>> + *generated = false;
>> + return key;
>> + }
>> + /* Generate a key from @input data */
>> + key = nvme_dhchap_psk_create(keyring, input, input_len);
>> + if (!IS_ERR(key)) {
>> + *generated = true;
>> pr_debug("generated dhchap key %s\n",
>> key->description);
>> }
>
> [Severity: High]
> Can calling nvme_dhchap_psk_lookup() here match the wrong key, read out of
> bounds, or cause the caller to revoke the key it just looked up?
>
> nvme_dhchap_psk_lookup() searches with nvme_dhchap_psk_match():
>
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_match() {
> ...
> match_id = match_data->raw_data;
> match_len = strlen(match_id);
> pr_debug("%s: match '%s' '%s' len %zd\n",
> __func__, match_id, key->description, match_len);
>
> return !memcmp(key->description, match_id, match_len);
> }
>
> Because nvme_dhchap_psk_match() compares match_len bytes with memcmp()
> instead of strcmp(), and nvme_auth_extract_key() does not reject
> input_len == 0:
>
> 1. Writing a single newline to sysfs dhchap_secret_store() or
> dhchap_ctrl_secret_store() passes an empty string (match_len == 0), so
> !memcmp(key->description, "", 0) matches the first dhchap key in the
> keyring with *generated = false.
> 2. For non-empty inputs, memcmp() prefix-matches any key whose description
> starts with input when match_len < strlen(key->description), and reads
> past the end of the key->description allocation when match_len >
> strlen(key->description) (for example, when passing a raw DHHC-1:...
> secret while a key with a shorter description is in the keyring).
> 3. If nvme_dhchap_psk_lookup() returns the controller's or target host's
> currently active auto-generated key (either via an empty write or by
> writing the current key's UUID description to dhchap_secret_store(),
> dhchap_ctrl_secret_store(), or nvmet_auth_set_key()), the store path
> sees that old_key was auto-generated and calls key_revoke(old_key)
> without checking key == old_key, revoking the key it then keeps active.
>
key parsing has been fixed up with the updated version of the previous
patch.
>> diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
>> index c189096f825ea..470a6e7e43644 100644
>> --- a/drivers/nvme/host/auth.c
>> +++ b/drivers/nvme/host/auth.c
> [ ... ]
>> @@ -1071,6 +1081,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>> key_serial(ctrl->opts->dhchap_key));
>> return -ENOKEY;
>> }
>> + ctrl->host_key_generated = ctrl->opts->dhchap_key_generated;
>> down_read(&ctrl->host_key->sem);
>> ret = key_validate(ctrl->host_key);
>> up_read(&ctrl->host_key->sem);
>> @@ -1097,6 +1108,8 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
>> key_serial(ctrl->opts->dhchap_ctrl_key));
>> return -ENOKEY;
>> }
>> + ctrl->ctrl_key_generated =
>> + ctrl->opts->dhchap_ctrl_key_generated;
>> down_read(&ctrl->ctrl_key->sem);
>> ret = key_validate(ctrl->ctrl_key);
>> up_read(&ctrl->ctrl_key->sem);
>
> [Severity: Medium]
> If acquiring or validating ctrl->ctrl_key fails in nvme_auth_init_ctrl(),
> does this leak ctrl->host_key and leave stale *_key_generated flags set?
>
> On failure of key_get(ctrl->opts->dhchap_ctrl_key) or
> key_validate(ctrl->ctrl_key), nvme_auth_init_ctrl() returns -ENOKEY or
> -EKEYREVOKED without calling nvme_auth_clear_host_key(ctrl). In
> nvme_init_ctrl(), a failure from nvme_auth_init_ctrl() jumps to
> out_release_instance before device_initialize(&ctrl->ctrl_device), so
> nvme_auth_free() never runs and the ctrl->host_key reference is leaked.
> The error paths after setting ctrl->host_key_generated and
> ctrl->ctrl_key_generated also clear the key pointer without resetting the
> corresponding generated boolean to false.
>
Fixed with the next version.
>> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
>> index 7ed8618257ca9..cd728768a68fc 100644
>> --- a/drivers/nvme/host/fabrics.c
>> +++ b/drivers/nvme/host/fabrics.c
> [ ... ]
>> @@ -1089,7 +1091,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> if (host_secret) {
>> pr_debug("lookup host identity '%s'\n", host_secret);
>> key = nvme_auth_extract_key(opts->keyring, host_secret,
>> - strlen(host_secret));
>> + strlen(host_secret),
>> + &opts->dhchap_key_generated);
>> if (IS_ERR(key)) {
>> ret = PTR_ERR(key);
>> goto out;
>> @@ -1104,7 +1107,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> }
>> pr_debug("lookup ctrl identity '%s'\n", ctrl_secret);
>> key = nvme_auth_extract_key(opts->keyring, ctrl_secret,
>> - strlen(ctrl_secret));
>> + strlen(ctrl_secret),
>> + &opts->dhchap_ctrl_key_generated);
>> if (IS_ERR(key)) {
>> ret = PTR_ERR(key);
>> goto out;
>
> [Severity: High]
> Does calling nvme_auth_extract_key() unconditionally in nvmf_parse_options()
> break linking when CONFIG_NVME_FABRICS is enabled without CONFIG_NVME_AUTH?
>
> CONFIG_NVME_FABRICS does not select CONFIG_NVME_AUTH, and
> include/linux/nvme-auth.h does not provide a !CONFIG_NVME_AUTH stub for
> nvme_auth_extract_key(), so builds with CONFIG_NVME_HOST_AUTH disabled (or
> CONFIG_NVME_FABRICS=y and CONFIG_NVME_AUTH=m) fail to link.
>
Fixed with the next version.
>> @@ -1327,15 +1331,23 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
>> kfree(opts->host_traddr);
>> kfree(opts->host_iface);
>> if (opts->dhchap_key) {
>> - pr_debug("revoke dhchap host key %08x\n",
>> + if (opts->dhchap_key_generated) {
>> + pr_debug("revoke dhchap host key %08x\n",
>> + key_serial(opts->dhchap_key));
>> + key_revoke(opts->dhchap_key);
>> + }
>> + pr_debug("drop dhchap host key %08x\n",
>> key_serial(opts->dhchap_key));
>> - key_revoke(opts->dhchap_key);
>> key_put(opts->dhchap_key);
>> }
>> if (opts->dhchap_ctrl_key) {
>> - pr_debug("revoke dhchap ctrl key %08x\n",
>> + if (opts->dhchap_ctrl_key_generated) {
>> + pr_debug("revoke dhchap ctrl key %08x\n",
>> + key_serial(opts->dhchap_ctrl_key));
>> + key_revoke(opts->dhchap_key);
>> + }
>> + pr_debug("drop dhchap ctrl key %08x\n",
>> key_serial(opts->dhchap_ctrl_key));
>> - key_revoke(opts->dhchap_key);
>> key_put(opts->dhchap_ctrl_key);
>> }
>> kfree(opts);
>
> [Severity: High]
> In the opts->dhchap_ctrl_key block, should key_revoke(opts->dhchap_key) be
> key_revoke(opts->dhchap_ctrl_key)?
>
> Passing opts->dhchap_key here accesses it after key_put(opts->dhchap_key)
> has already dropped the reference (risking a use-after-free), revokes a
> pre-populated host key when only the controller key was auto-generated, and
> leaves the auto-generated opts->dhchap_ctrl_key unrevoked.
>
Fixed with the next version.
>> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
>> index 1311f5e29b004..47caed2ce3978 100644
>> --- a/drivers/nvme/host/sysfs.c
>> +++ b/drivers/nvme/host/sysfs.c
>> @@ -828,13 +828,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);
>> return count;
>> }
> [ ... ]
>> @@ -869,19 +872,25 @@ static ssize_t dhchap_secret_store(struct device *dev,
>> up_read(&key->sem);
>> if (ret) {
>> dev_warn(ctrl->dev, "key %08x invalidated\n", key_serial(key));
>> - dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
>> - key_revoke(key);
>> + if (generated) {
>> + dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
>> + key_revoke(key);
>> + synchronize_rcu();
>> + }
>> key_put(key);
>> kfree_sensitive(dhchap_secret);
>> return ret;
>> }
>> mutex_lock(&ctrl->dhchap_auth_mutex);
>> old_key = ctrl->host_key;
>> - dev_dbg(ctrl->dev, "revoke host key %08x\n",
>> - key_serial(old_key));
>> - key_revoke(old_key);
>> -
>> + if (ctrl->host_key_generated) {
>> + dev_dbg(ctrl->dev, "revoke host key %08x\n",
>> + key_serial(old_key));
>> + key_revoke(old_key);
>> + synchronize_rcu();
>> + }
>> ctrl->host_key = key;
>> + ctrl->host_key_generated = generated;
>> mutex_unlock(&ctrl->dhchap_auth_mutex);
>> key_put(old_key);
>> kfree_sensitive(dhchap_secret);
>
> [Severity: Medium]
> Can dhchap_secret_show(), dhchap_ctrl_secret_show(), dhchap_key_show(), and
> dhchap_ctrl_key_show() race with concurrent stores and trigger a
> use-after-free or read a mismatched *_key_generated flag?
>
> Unlike nvmet_host_dhchap_key_show() in drivers/nvme/target/configfs.c, the
> host sysfs show callbacks read ctrl->host_key / ctrl->ctrl_key and
> ctrl->host_key_generated / ctrl->ctrl_key_generated without holding
> ctrl->dhchap_auth_mutex or pinning the key with key_get():
>
> CPU0 CPU1
> dhchap_secret_show() dhchap_secret_store()
> key = ctrl->host_key;
> mutex_lock(&ctrl->dhchap_auth_mutex);
> old_key = ctrl->host_key;
> ctrl->host_key = new_key;
> ctrl->host_key_generated = true;
> mutex_unlock(&ctrl->dhchap_auth_mutex);
> key_put(old_key);
> down_read(&key->sem); <-- use-after-free
>
> Even if old_key still has another reference (such as when old_key was a
> pre-populated user key in the keyring), CPU0 can pair old_key with the newly
> written ctrl->host_key_generated == true and dump the pre-populated key's
> raw secret via key->type->read() instead of its description.
>
Fixed with the next version.
>> @@ -906,13 +915,14 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
>> down_read(&key->sem);
>> if (key_validate(key))
>> count = sysfs_emit(buf, "<invalidated>");
>> - 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);
>> return count;
>> }
>
> [Severity: Low]
> In dhchap_ctrl_secret_show(), count is declared as unsigned size_t (unlike
> ssize_t in dhchap_secret_show()), so a negative error return from
> key->type->read() would wrap to a large positive value, pass count > 0, and
> write out of bounds at buf[count] = '\n'. Also,
> sysfs_emit(buf, "<invalidated>") is missing the trailing '\n' present in the
> other show callbacks.
>
Fixed with the next version.
> [ ... ]
>
>> @@ -1066,8 +1088,12 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
>> #ifdef CONFIG_NVME_HOST_AUTH
>> if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
>> return 0;
>> + if (a == &dev_attr_dhchap_key.attr && ctrl->host_key_generated)
>> + return 0;
>> if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
>> return 0;
>> + if (a == &dev_attr_dhchap_ctrl_key.attr && ctrl->ctrl_key_generated)
>> + return 0;
>> #endif
>>
>> return a->mode;
>
> [Severity: Medium]
> Should the visibility checks for dev_attr_dhchap_key.attr and
> dev_attr_dhchap_ctrl_key.attr also check !ctrl->opts, and how is attribute
> visibility updated when keys change at runtime?
>
> Without a !ctrl->opts check, dhchap_key and dhchap_ctrl_key are exposed in
> sysfs on non-Fabrics controllers (such as PCIe controllers, where
> ctrl->opts is NULL and the *_key_generated fields are false) even though
> dhchap_secret and dhchap_ctrl_secret are hidden. In addition, because
> nvme_dev_attrs_are_visible() is only evaluated when the sysfs group is
> created and dhchap_secret_store() / dhchap_ctrl_secret_store() do not call
> sysfs_update_group(), switching a controller at runtime between an
> auto-generated secret and a pre-populated key description leaves dhchap_key
> and dhchap_ctrl_key stuck in their initial visibility state.
>
Fixed with the new version.
>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 880cf5151588a..319947aa0a40f 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
> [ ... ]
>> @@ -2305,13 +2306,14 @@ 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 = sprintf(page, "%s\n", key->description);
>> up_read(&key->sem);
>> key_put(key);
>> }
>
> [Severity: Medium]
> Does nvmet_host_dhchap_ctrl_key_store() just below this function leak
> keydata on every write?
>
> drivers/nvme/target/configfs.c:nvmet_host_dhchap_ctrl_key_store() {
> ...
> keydata = kstrndup(page, len, GFP_KERNEL);
> if (!keydata)
> return -ENOMEM;
>
> ret = nvmet_auth_set_key(host, keydata, true);
> ...
> return ret < 0 ? ret : count;
> }
>
> Unlike nvmet_host_dhchap_key_store(), which calls kfree_sensitive(keydata)
> after nvmet_auth_set_key(), nvmet_host_dhchap_ctrl_key_store() returns
> without freeing keydata, leaking the buffer and leaving key material in slab
> memory.
>
Already fixed with the update to a previous patch.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys
2026-11-20 11:26 ` sashiko-bot
@ 2026-11-20 14:52 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 14:52 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:26 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Low] nvme-auth: asymmetric and undocumented removal of
> synchronize_rcu() in sysfs
> --
>
> commit 5f508c67e7480252b016c544aeba6f80edc88b85
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme-auth: wait for authentication to finish when changing keys
>
> This commit flushes ctrl->dhchap_auth_work after queuing re-authentication in
> dhchap_secret_store() and dhchap_ctrl_secret_store(), and removes two
> synchronize_rcu() calls after key_revoke().
>
>> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
>> index 47caed2ce3978..ce327a018b0d0 100644
>> --- a/drivers/nvme/host/sysfs.c
>> +++ b/drivers/nvme/host/sysfs.c
>> @@ -875,7 +875,6 @@ static ssize_t dhchap_secret_store(struct device *dev,
>> if (generated) {
>> dev_dbg(ctrl->dev, "revoke host key %08x\n", key_serial(key));
>> key_revoke(key);
>> - synchronize_rcu();
>> }
>> key_put(key);
>> kfree_sensitive(dhchap_secret);
>
> [ ... ]
>
>> @@ -973,7 +972,6 @@ static ssize_t dhchap_ctrl_secret_store(struct device *dev,
>> dev_dbg(ctrl->dev, "revoke ctrl key %08x\n",
>> key_serial(old_key));
>> key_revoke(old_key);
>> - synchronize_rcu();
>> }
>> ctrl->ctrl_key = key;
>> ctrl->ctrl_key_generated = generated;
>
> [Severity: Low]
> Why are synchronize_rcu() calls removed from mismatched paths in
> dhchap_secret_store() and dhchap_ctrl_secret_store(), and without mention in
> the commit message?
>
> In dhchap_secret_store(), synchronize_rcu() is removed from the
> key_validate() error path, but kept when revoking old_key under
> ctrl->dhchap_auth_mutex:
>
Oversight. Will be fixed up with the next version.
> drivers/nvme/host/sysfs.c:dhchap_secret_store() {
> ...
> if (ctrl->host_key_generated) {
> dev_dbg(ctrl->dev, "revoke host key %08x\n",
> key_serial(old_key));
> key_revoke(old_key);
> synchronize_rcu();
> }
> ...
> }
>
> In dhchap_ctrl_secret_store(), it is removed when revoking old_key, but kept
> in the key_validate() error path:
>
> drivers/nvme/host/sysfs.c:dhchap_ctrl_secret_store() {
> ...
> if (generated) {
> dev_dbg(ctrl->dev, "revoke ctrl key %08x\n", key_serial(key));
> key_revoke(key);
> synchronize_rcu();
> }
> ...
> }
>
> Both functions handle keys of type nvme_dhchap_psk_key_type (whose
> user_revoke() callback frees the payload via call_rcu()), so the two store
> callbacks should handle key_revoke() symmetrically.
>
Similarly. Will be fixed with the next round.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name
2026-11-20 11:37 ` sashiko-bot
@ 2026-11-20 15:02 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 15:02 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:37 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [High] nvme-fabrics: opt_tokens keyring=%d pattern rejects keyring
> names
> - [Medium] nvme-fabrics: build failure in nvmf_parse_options() when
> !CONFIG_KEYS
> --
>
> commit 0ba36c8d6abaed9ca222b0bbab73723c1ce8243d
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvme-fabrics: allow to pass in keyring by name
>
> This commit updates nvmf_parse_options() so the NVMF_OPT_KEYRING option
> falls back to looking up a keyring by description via request_key() when the
> argument is not a positive integer serial number.
>
>> diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
>> index cd728768a68fc..71da67b543aa4 100644
>> --- a/drivers/nvme/host/fabrics.c
>> +++ b/drivers/nvme/host/fabrics.c
>
> [ ... ]
>
>> @@ -993,13 +994,23 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
>> break;
>> case NVMF_OPT_KEYRING:
>> 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;
>> + p = match_strdup(args);
>
> [Severity: High]
> Can a keyring name ever reach this branch while opt_tokens still defines
> NVMF_OPT_KEYRING as "keyring=%d"?
>
> drivers/nvme/host/fabrics.c:opt_tokens {
> ...
> { NVMF_OPT_KEYRING, "keyring=%d" },
> ...
> }
>
> Because match_token() requires "%d" arguments to be valid integers, any
> non-numeric name such as "keyring=.nvme" fails to match and makes
> nvmf_parse_options() reject the option with -EINVAL in the default switch
> case. In addition, reading /dev/nvme-fabrics still advertises "keyring=%d"
> to userspace, while passing a non-positive integer serial number such as
> "keyring=0" or "keyring=-1" enters this fallback and looks up the number as
> a keyring description instead of returning -EINVAL.
>
Fixed with the next round.
>> + if (!p) {
>> + ret = -ENOMEM;
>> + goto out;
>> + }
>> + key = request_key(&key_type_keyring, p, NULL);
>
> [Severity: Medium]
> Does this break the build when CONFIG_NVME_FABRICS is enabled and
> CONFIG_KEYS is disabled?
>
> CONFIG_NVME_FABRICS only selects CONFIG_NVME_KEYRING (which selects
> CONFIG_KEYS) when CONFIG_NVME_TCP_TLS or CONFIG_NVME_HOST_AUTH is enabled.
> Both key_type_keyring in include/linux/key-type.h and request_key() in
> include/linux/key.h are declared under #ifdef CONFIG_KEYS with no stub for
> !CONFIG_KEYS builds.
>
Fixed with the next round.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring'
2026-11-20 11:39 ` sashiko-bot
@ 2026-11-20 15:27 ` Hannes Reinecke
0 siblings, 0 replies; 30+ messages in thread
From: Hannes Reinecke @ 2026-11-20 15:27 UTC (permalink / raw)
To: sashiko-reviews, Hannes Reinecke; +Cc: linux-nvme
On 11/20/26 12:39 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] nvmet: keyring reference leak in nvmet_host_release()
> - [Medium] nvmet: inverted error return sign in
> nvmet_host_dhchap_keyring_store()
> - [Medium] nvmet: unlocked host->dhchap_keyring access in
> nvmet_auth_set_key()
> - [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show()
> via sprintf()
> --
>
> commit 7f1bb5c7e432b0938e23615bf24751f61d823e03
> Author: Hannes Reinecke <hare@kernel.org>
>
> nvmet: add configfs attribute 'dhchap_keyring'
>
> This commit adds a dhchap_keyring configfs attribute to the nvmet host
> directory so userspace can specify the keyring used for DH-HMAC-CHAP key
> lookups.
>
>> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
>> index 4f2489c5d894b..d583813ef4f27 100644
>> --- a/drivers/nvme/target/auth.c
>> +++ b/drivers/nvme/target/auth.c
>> @@ -68,7 +68,7 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
>> return 0;
>> }
>> len = strcspn(secret, "\n");
>> - key = nvme_auth_extract_key(NULL, secret, len, &generated);
>> + key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
>> if (IS_ERR(key)) {
>> pr_debug("%s: invalid key specification\n", __func__);
>> return PTR_ERR(key);
>
> [Severity: Medium]
> Can a concurrent write to dhchap_keyring free host->dhchap_keyring while
> nvmet_auth_set_key() is using it?
>
> CPU0 CPU1
> nvmet_host_dhchap_key_store()
> nvmet_auth_set_key()
> keyring = host->dhchap_keyring
> nvmet_host_dhchap_keyring_store()
> down_write(&nvmet_config_sem)
> key_put(host->dhchap_keyring)
> host->dhchap_keyring = new_ring
> up_write(&nvmet_config_sem)
> nvme_auth_extract_key(keyring, ...)
> ... <-- use-after-free
>
> nvmet_host_dhchap_keyring_store() replaces host->dhchap_keyring and drops
> the old reference under nvmet_config_sem, whereas nvmet_auth_set_key()
> reads and uses host->dhchap_keyring without holding nvmet_config_sem or
> pinning it with key_get().
>
Fixed with the next version.
>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 319947aa0a40f..ffe0f852e59d9 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
>> @@ -2347,6 +2347,62 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
>>
>> CONFIGFS_ATTR(nvmet_host_, dhchap_ctrl_key);
>>
>> +static ssize_t nvmet_host_dhchap_keyring_show(struct config_item *item,
>> + char *page)
>> +{
>> + struct nvmet_host *host = to_host(item);
>> + struct key *keyring;
>> + ssize_t ret;
>> +
>> + down_read(&nvmet_config_sem);
>> + keyring = key_get(host->dhchap_keyring);
>> + if (!keyring) {
>> + page[0] = '\0';
>> + ret = 0;
>> + } else {
>> + down_read(&keyring->sem);
>> + if (key_validate(keyring))
>> + ret = sprintf(page, "<invalidated>\n");
>> + else
>> + ret = sprintf(page, "%s\n", keyring->description);
>> + up_read(&keyring->sem);
>> + key_put(keyring);
>> + }
>> + up_read(&nvmet_config_sem);
>> + return ret;
>> +}
>
> [Severity: Medium]
> Can sprintf() write one byte past the end of page here?
>
> add_key() accepts descriptions up to 4095 bytes (KEY_MAX_DESC_SIZE - 1),
> and configfs fill_write_buffer() accepts a 4095-byte write without a
> trailing newline. Formatting a 4095-byte keyring->description with "%s\n"
> writes 4097 bytes (including the NUL terminator) into the 4096-byte
> PAGE_SIZE configfs read buffer. Using sysfs_emit() or snprintf(page,
> PAGE_SIZE, ...) would avoid the overflow.
>
>> +
>> +static ssize_t nvmet_host_dhchap_keyring_store(struct config_item *item,
>> + const char *page, size_t count)
>> +{
>> + struct nvmet_host *host = to_host(item);
>> + struct key *keyring;
>> + char *desc;
>> + size_t len;
>> + int ret = 0;
>> +
>> + len = strcspn(page, "\n");
>> + if (!len)
>> + return -EINVAL;
>> + desc = kstrndup(page, len, GFP_KERNEL);
>> + if (!desc)
>> + return -ENOMEM;
>> + keyring = request_key(&key_type_keyring, desc, NULL);
>> + if (IS_ERR(keyring)) {
>> + ret = PTR_ERR(keyring);
>> + } else {
>> + down_write(&nvmet_config_sem);
>> + key_put(host->dhchap_keyring);
>> + host->dhchap_keyring = keyring;
>> + up_write(&nvmet_config_sem);
>> + }
>> + kfree(desc);
>> +
>> + return ret ? -ret : count;
>> +}
>
> [Severity: Medium]
> Does negating ret return a positive value when request_key() fails?
> PTR_ERR(keyring) is already negative, so returning -ret causes
> configfs_write_iter() to treat the positive errno as a successful write
> byte count and silently ignore the failure.
>
Fixed with the new version.
> [ ... ]
>
>> @@ -2445,6 +2502,11 @@ static struct config_group *nvmet_hosts_make_group(struct config_group *group,
>> #ifdef CONFIG_NVME_TARGET_AUTH
>> /* Default to SHA256 */
>> host->dhchap_hash_id = NVME_AUTH_HASH_SHA256;
>> + host->dhchap_keyring = key_lookup(nvme_keyring_id());
>> + if (IS_ERR(host->dhchap_keyring)) {
>> + kfree(host);
>> + return ERR_PTR(-ENOKEY);
>> + }
>> #endif
>>
>> config_group_init_type_name(&host->group, name, &nvmet_host_type);
>
> [Severity: High]
> Does removing a host group leak host->dhchap_keyring?
>
> Both key_lookup() here and request_key() in
> nvmet_host_dhchap_keyring_store() acquire a reference on
> host->dhchap_keyring, but nvmet_host_release() never calls
> key_put(host->dhchap_keyring):
>
> drivers/nvme/target/configfs.c:nvmet_host_release() {
> struct nvmet_host *host = to_host(item);
>
> #ifdef CONFIG_NVME_TARGET_AUTH
> nvmet_auth_revoke_key(host, false);
> nvmet_auth_revoke_key(host, true);
> #endif
> kfree(host);
> }
>
> Every removed host configfs directory leaks a reference on its configured
> keyring.
>
Fixed with the next version.
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
hare@suse.de +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
@ 2026-11-20 19:24 ` kernel test robot
2026-11-20 19:37 ` kernel test robot
2 siblings, 0 replies; 30+ messages in thread
From: kernel test robot @ 2026-11-20 19:24 UTC (permalink / raw)
To: Hannes Reinecke, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Hannes Reinecke, Chaitanya Kulkarni
Cc: llvm, oe-kbuild-all, linux-nvme, linux-kernel
Hi Hannes,
kernel test robot noticed the following build errors:
[auto build test ERROR on 68099cf27253a8c89d703a282d3d811446d9453b]
url: https://github.com/intel-lab-lkp/linux/commits/Hannes-Reinecke/nvme-auth-modify-nvme_auth_transform_key-to-return-status/20261120-121400
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee%40kernel.org
patch subject: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20261121/202611210318.EedT1DJk-lkp@intel.com/config)
compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 3b14d7c370558b46e5f0c6494a05c492e59bf32c)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210318.EedT1DJk-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/202611210318.EedT1DJk-lkp@intel.com/
All error/warnings (new ones prefixed by >>):
In file included from drivers/nvme/host/fabrics.c:15:
>> include/linux/nvme-keyring.h:54:1: error: expected identifier or '('
54 | {
| ^
>> include/linux/nvme-keyring.h:57:4: warning: no previous prototype for function 'nvme_dhchap_psk_hash' [-Wmissing-prototypes]
57 | u8 nvme_dhchap_psk_hash(struct key *key)
| ^
include/linux/nvme-keyring.h:57:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
57 | u8 nvme_dhchap_psk_hash(struct key *key)
| ^
| static
1 warning and 1 error generated.
vim +54 include/linux/nvme-keyring.h
26
27 #else
28 static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
29 u8 *data, size_t data_len, const char *digest)
30 {
31 return ERR_PTR(-EOPNOTSUPP);
32 }
33 static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
34 const char *hostnqn, const char *subnqn)
35 {
36 return 0;
37 }
38 static inline key_serial_t nvme_keyring_id(void)
39 {
40 return 0;
41 }
42 static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
43 {
44 return ERR_PTR(-EOPNOTSUPP);
45 }
46 static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
47 const char *hostnqn, const char *subnqn,
48 u8 *data, size_t data_len)
49 {
50 return ERR_PTR(-EOPNOTSUPP);
51 }
52 static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
53 const char *hostnqn, const char *subnqn, u8 hmac);
> 54 {
55 return ERR_PTR(-EOPNOTSUPP);
56 }
> 57 u8 nvme_dhchap_psk_hash(struct key *key)
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 30+ messages in thread
* Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
2026-11-20 19:24 ` kernel test robot
@ 2026-11-20 19:37 ` kernel test robot
2 siblings, 0 replies; 30+ messages in thread
From: kernel test robot @ 2026-11-20 19:37 UTC (permalink / raw)
To: Hannes Reinecke, Keith Busch, Jens Axboe, Christoph Hellwig,
Sagi Grimberg, Hannes Reinecke, Chaitanya Kulkarni
Cc: oe-kbuild-all, linux-nvme, linux-kernel
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/20261120-121400
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee%40kernel.org
patch subject: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20261121/202611210315.8aA2D5rd-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210315.8aA2D5rd-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/202611210315.8aA2D5rd-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> Warning: drivers/nvme/common/auth.c:154 function parameter 'keyring' not described in 'nvme_auth_extract_key'
>> Warning: drivers/nvme/common/auth.c:154 function parameter 'keyring' not described in 'nvme_auth_extract_key'
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 30+ messages in thread
end of thread, other threads:[~2026-11-20 19:37 UTC | newest]
Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-20 11:19 ` sashiko-bot
2026-11-20 11:42 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-20 11:28 ` sashiko-bot
2026-11-20 12:28 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
2026-11-20 13:08 ` Hannes Reinecke
2026-11-20 19:24 ` kernel test robot
2026-11-20 19:37 ` kernel test robot
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-20 11:32 ` sashiko-bot
2026-11-20 13:55 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-20 11:28 ` sashiko-bot
2026-11-20 14:15 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
2026-11-20 14:48 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-20 11:26 ` sashiko-bot
2026-11-20 14:52 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-20 11:37 ` sashiko-bot
2026-11-20 15:02 ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-20 11:39 ` sashiko-bot
2026-11-20 15:27 ` Hannes Reinecke
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.