* [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 ` Hannes Reinecke
2026-11-20 11:31 ` sashiko-bot
` (2 more replies)
0 siblings, 3 replies; 6+ 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] 6+ 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; 6+ 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] 6+ 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; 6+ 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] 6+ 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; 6+ 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] 6+ 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; 6+ 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] 6+ messages in thread
* Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
@ 2026-11-20 20:11 kernel test robot
0 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2026-11-20 20:11 UTC (permalink / raw)
To: oe-kbuild; +Cc: lkp
::::::
:::::: Manual check reason: "same reports for this branch"
::::::
BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee@kernel.org>
References: <20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee@kernel.org>
TO: Hannes Reinecke <hare@kernel.org>
TO: Keith Busch <kbusch@kernel.org>
TO: Jens Axboe <axboe@kernel.dk>
TO: Christoph Hellwig <hch@lst.de>
TO: Sagi Grimberg <sagi@grimberg.me>
TO: Hannes Reinecke <hare@suse.de>
TO: Chaitanya Kulkarni <kch@nvidia.com>
CC: linux-nvme@lists.infradead.org
CC: linux-kernel@vger.kernel.org
Hi Hannes,
kernel test robot noticed the following build errors:
[auto build test ERROR on 68099cf27253a8c89d703a282d3d811446d9453b]
url: https://github.com/intel-lab-lkp/linux/commits/Hannes-Reinecke/nvme-auth-modify-nvme_auth_transform_key-to-return-status/20261120-121400
base: 68099cf27253a8c89d703a282d3d811446d9453b
patch link: https://lore.kernel.org/r/20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee%40kernel.org
patch subject: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
:::::: branch date: 7 hours ago
:::::: commit date: 7 hours ago
config: nios2-randconfig-1000-20261120 (https://download.01.org/0day-ci/archive/20261121/202611210433.kaUKHSYE-lkp@intel.com/config)
compiler: nios2-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261121/202611210433.kaUKHSYE-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/r/202611210433.kaUKHSYE-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 '(' before '{' token
54 | {
| ^
>> include/linux/nvme-keyring.h:57:4: warning: no previous prototype for 'nvme_dhchap_psk_hash' [-Wmissing-prototypes]
57 | u8 nvme_dhchap_psk_hash(struct key *key)
| ^~~~~~~~~~~~~~~~~~~~
>> include/linux/nvme-keyring.h:52:27: warning: 'nvme_dhchap_psk_lookup' declared 'static' but never defined [-Wunused-function]
52 | static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
| ^~~~~~~~~~~~~~~~~~~~~~
vim +54 include/linux/nvme-keyring.h
50d8f97114b492 Hannes Reinecke 2026-11-20 26
9d77eb52778499 Hannes Reinecke 2023-08-24 27 #else
62eb89323cb08f Hannes Reinecke 2025-02-24 28 static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
62eb89323cb08f Hannes Reinecke 2025-02-24 29 u8 *data, size_t data_len, const char *digest)
62eb89323cb08f Hannes Reinecke 2025-02-24 30 {
50d8f97114b492 Hannes Reinecke 2026-11-20 31 return ERR_PTR(-EOPNOTSUPP);
62eb89323cb08f Hannes Reinecke 2025-02-24 32 }
501cc6f4aca8dc Hannes Reinecke 2023-08-24 33 static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
501cc6f4aca8dc Hannes Reinecke 2023-08-24 34 const char *hostnqn, const char *subnqn)
501cc6f4aca8dc Hannes Reinecke 2023-08-24 35 {
501cc6f4aca8dc Hannes Reinecke 2023-08-24 36 return 0;
501cc6f4aca8dc Hannes Reinecke 2023-08-24 37 }
9d77eb52778499 Hannes Reinecke 2023-08-24 38 static inline key_serial_t nvme_keyring_id(void)
9d77eb52778499 Hannes Reinecke 2023-08-24 39 {
9d77eb52778499 Hannes Reinecke 2023-08-24 40 return 0;
9d77eb52778499 Hannes Reinecke 2023-08-24 41 }
5bc46b49c828a6 Hannes Reinecke 2024-07-22 42 static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
5bc46b49c828a6 Hannes Reinecke 2024-07-22 43 {
50d8f97114b492 Hannes Reinecke 2026-11-20 44 return ERR_PTR(-EOPNOTSUPP);
50d8f97114b492 Hannes Reinecke 2026-11-20 45 }
50d8f97114b492 Hannes Reinecke 2026-11-20 46 static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
50d8f97114b492 Hannes Reinecke 2026-11-20 47 const char *hostnqn, const char *subnqn,
50d8f97114b492 Hannes Reinecke 2026-11-20 48 u8 *data, size_t data_len)
50d8f97114b492 Hannes Reinecke 2026-11-20 49 {
50d8f97114b492 Hannes Reinecke 2026-11-20 50 return ERR_PTR(-EOPNOTSUPP);
50d8f97114b492 Hannes Reinecke 2026-11-20 51 }
50d8f97114b492 Hannes Reinecke 2026-11-20 @52 static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
50d8f97114b492 Hannes Reinecke 2026-11-20 53 const char *hostnqn, const char *subnqn, u8 hmac);
50d8f97114b492 Hannes Reinecke 2026-11-20 @54 {
50d8f97114b492 Hannes Reinecke 2026-11-20 55 return ERR_PTR(-EOPNOTSUPP);
50d8f97114b492 Hannes Reinecke 2026-11-20 56 }
50d8f97114b492 Hannes Reinecke 2026-11-20 @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] 6+ messages in thread
end of thread, other threads:[~2026-11-20 20:11 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 20:11 [PATCH v4 3/9] nvme-auth: switch to use 'struct key' kernel test robot
-- strict thread matches above, loose matches on Subject: below --
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 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
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.