Skip to contentSkip to conversation

nvme-auth: switch to use the kernel keyring

PATCH Superseded Hannes Reinecke 4 participants 28 messages Nov 23

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover nvme-auth: switch to use the kernel keyring

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

---
Changes in v5:
- Fixup issues found by sashiko
- Link to v4: https://lore.kernel.org/r/[email protected]

---
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     | 214 ++++++++++++--------------
 drivers/nvme/common/keyring.c  | 333 +++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/host/Kconfig      |   1 -
 drivers/nvme/host/auth.c       | 179 +++++++++++++++-------
 drivers/nvme/host/fabrics.c    | 143 +++++++++++++-----
 drivers/nvme/host/fabrics.h    |  12 +-
 drivers/nvme/host/nvme.h       |   6 +-
 drivers/nvme/host/sysfs.c      | 236 ++++++++++++++++++++++-------
 drivers/nvme/target/Kconfig    |   1 -
 drivers/nvme/target/auth.c     | 231 ++++++++++++++++++----------
 drivers/nvme/target/configfs.c | 146 ++++++++++++++++--
 drivers/nvme/target/nvmet.h    |  16 +-
 include/linux/nvme-auth.h      |  17 +--
 include/linux/nvme-keyring.h   |  26 +++-
 15 files changed, 1170 insertions(+), 392 deletions(-)

---
base-commit: 68099cf27253a8c89d703a282d3d811446d9453b
change-id: 20261120-dhchap-keyring-v4-09f0bac1d8c5

Best regards,

Signature · 1 line
Hannes Reinecke <[email protected]>
#1 lore raw

PATCH v5 1/9 nvme-auth: modify nvme_auth_transform_key() to return status

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.

---
 drivers/nvme/common/auth.c | 37 ++++++++++++++++++++-----------------
 drivers/nvme/host/auth.c   | 44 +++++++++++++++++++++++---------------------
 drivers/nvme/target/auth.c | 37 +++++++++++++++++++++----------------
 include/linux/nvme-auth.h  |  4 ++--
 4 files changed, 66 insertions(+), 56 deletions(-)
drivers/nvme/common/auth.c +20 -17
index 1fdc4e4ed0b94d8a36aade033f19e17463f15632..308af3d7c7383acbfb327921c8a650898f0e15ea 100644
@@ -317,37 +317,40 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
 	return -EINVAL;
 }
 
-struct nvme_dhchap_key *nvme_auth_transform_key(
-		const struct nvme_dhchap_key *key, const char *nqn)
+int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
+			    u8 **transformed_secret)
 {
 	struct nvme_auth_hmac_ctx hmac;
-	struct nvme_dhchap_key *transformed_key;
-	int ret, key_len;
+	u8 *key_data;
+	size_t transformed_len;
+	int ret;
 
 	if (!key) {
 		pr_warn("No key specified\n");
-		return ERR_PTR(-ENOKEY);
+		return -ENOKEY;
 	}
 	if (key->hash == 0) {
-		key_len = nvme_auth_key_struct_size(key->len);
-		transformed_key = kmemdup(key, key_len, GFP_KERNEL);
-		if (!transformed_key)
-			return ERR_PTR(-ENOMEM);
-		return transformed_key;
+		key_data = kzalloc(key->len, GFP_KERNEL);
+		if (!key_data)
+			return -ENOMEM;
+		memcpy(key_data, key->key, key->len);
+		*transformed_secret = key_data;
+		return key->len;
 	}
 	ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
 	if (ret)
-		return ERR_PTR(ret);
-	key_len = nvme_auth_hmac_hash_len(key->hash);
-	transformed_key = nvme_auth_alloc_key(key_len, key->hash);
-	if (!transformed_key) {
+		return ret;
+	transformed_len = nvme_auth_hmac_hash_len(key->hash);
+	key_data = kzalloc(transformed_len, GFP_KERNEL);
+	if (!key_data) {
 		memzero_explicit(&hmac, sizeof(hmac));
-		return ERR_PTR(-ENOMEM);
+		return -ENOMEM;
 	}
 	nvme_auth_hmac_update(&hmac, nqn, strlen(nqn));
 	nvme_auth_hmac_update(&hmac, "NVMe-over-Fabrics", 17);
-	nvme_auth_hmac_final(&hmac, transformed_key->key);
-	return transformed_key;
+	nvme_auth_hmac_final(&hmac, key_data);
+	*transformed_secret = key_data;
+	return transformed_len;
 }
 EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
 
drivers/nvme/host/auth.c +23 -21

86 lines not on this page · raw message

drivers/nvme/target/auth.c +21 -16
index a4a2a5cfd823f921a9e9503f41de51b846c43296..150818d7009d8c2fca96e12fd54fdc1cb6b68882 100644
@@ -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;
 }
 
include/linux/nvme-auth.h +2 -2
index d674d8ab26e6b3b4b297ebaefa3f33b8ec97bc0a..1c5ce635b169dd0747c1344e9adcc14839776cd1 100644
@@ -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);
Signature · 1 line
2.51.0
#2 lore raw

PATCH v5 2/9 nvme-keyring: add 'dhchap' key type

In reply to 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.

---
 drivers/nvme/common/keyring.c | 235 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 235 insertions(+)
drivers/nvme/common/keyring.c +235 -0
index 87cb51e1f5a7f330af4705f4058f661d15f5984a..d0c36d153afdb39ae698cf86cefe53f82af34907 100644
@@ -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,231 @@ 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);
+	if (match_len != strlen(key->description)) {
+		pr_debug("%s: match '%s' '%s' different size\n",
+			 __func__, match_id, key->description);
+		return false;
+	}
+	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;
+	int ret, offset;
+	char *p;
+	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:%n",
+		   &version, &hmac, &offset) != 2 || !offset) {
+		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 = strlen(prep->data + offset);
+	p = strrchr(prep->data + offset, ':');
+	if (p)
+		datalen--;
+
+	/*
+	 * 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->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);
+		prep->payload.data[0] = NULL;
+		return ret;
+	}
+	if (ret != 36 && ret != 52 && ret != 68) {
+		pr_debug("%s: invalid decoded key size '%d'\n",
+			 __func__, ret);
+		prep->payload.data[0] = NULL;
+		return -EINVAL;
+	}
+	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 */
+		prep->payload.data[0] = NULL;
+		return -EKEYREJECTED;
+	}
+	/* append version and hmac to the payload */
+	upayload->data[ret + 4] = version;
+	upayload->data[ret + 5] = hmac;
+	upayload->datalen = ret;
+	prep->quotalen = ret + 6;
+	return 0;
+}
+
+/**
+ * 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;
+
+	if (!buffer || buflen == 0)
+		return -EINVAL;
+
+	upayload = user_key_payload_locked(key);
+	/*
+	 * Expected output string length:
+	 * strlen("DHHC-") +
+	 * version (= 1) +
+	 * ':' (= 1) +
+	 * hmac (= 2) +
+	 * ':' (= 1) +
+	 * encoded base64 key length +
+	 * ':' (= 1)
+	 */
+	ret = nvme_dhchap_decoded_key_size(upayload->datalen);
+	if (ret < 0)
+		return ret;
+	keylen = ret + 11;
+	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 +499,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);
Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH v5 2/9] nvme-keyring: add 'dhchap' key type

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider: - [Critical] nvme-keyring: out-of-bounds read on prep->data in nvme_dhchap_psk_preparse() - [High]…

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [Critical] nvme-keyring: out-of-bounds read on prep->data in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: uninitialized offset used in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: header offset mismatch in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: memory leak of upayload on error in nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: broken key length queries in nvme_dhchap_psk_read()
- [Low] nvme-keyring: inverted naming and comment for nvme_dhchap_decoded_key_size()
- [Low] nvme-keyring: missing select CRC32 in Kconfig for nvme_dhchap_psk_preparse()
- [Low] nvme-keyring: redundant nvme_dhchap_psk_match_preparse() forces linear scan
--

commit d9b56f363bf407639fb6f4f459820389f42a36f1
Author: Hannes Reinecke <[email protected]>

nvme-keyring: add 'dhchap' key type

This commit registers a 'dhchap' key type in the NVMe keyring module to parse, store, and read back base64-encoded DH-HMAC-CHAP secret keys.

Quoted 5 lines
drivers/nvme/common/keyring.c +0 -0
index 87cb51e1f5a7f..d0c36d153afdb 100644

drivers/nvme/common/keyring.c line 12

Quoted 9 lines
drivers/nvme/common/keyring.c +3 -0
@@ -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>

[Severity: Low]
nvme_dhchap_psk_preparse() calls crc32(), which expands to crc32_le(), but config NVME_KEYRING in drivers/nvme/common/Kconfig only selects KEYS and does not select CRC32. Should NVME_KEYRING select CRC32 so builds without CONFIG_CRC32 enabled by another option do not fail at link time?

Quoted 1 line
drivers/nvme/common/keyring.c +0 -0
@@ -253,6 +256,231 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,

[ ... ]

Quoted 23 lines
drivers/nvme/common/keyring.c +23 -0
+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);
+	if (match_len != strlen(key->description)) {
+		pr_debug("%s: match '%s' '%s' different size\n",
+			 __func__, match_id, key->description);
+		return false;
+	}
+	pr_debug("%s: match '%s' '%s' len %zd\n",
+		 __func__, match_id, key->description, match_len);

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 297

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+
+	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;
+}

[Severity: Low]
Is nvme_dhchap_psk_match_preparse() needed here? nvme_dhchap_psk_match() checks for an exact description match identical to key_default_cmp(), so overriding lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE forces every dhchap key lookup to do a linear scan of the keyring instead of a direct associative array lookup.

Quoted 28 lines
drivers/nvme/common/keyring.c +28 -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;
+	int ret, offset;
+	char *p;
+	u32 crc;
+	u8 version, hmac;
+
+	if (!prep->data) {
+		pr_debug("%s: Empty data", __func__);
+		prep->payload.data[0] = NULL;
+		prep->quotalen = 0;
+		return -EINVAL;

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 335

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	}
+
+	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
+		   &version, &hmac, &offset) != 2 || !offset) {
+		pr_debug("%s: invalid key data '%s'\n", __func__,
+			 (char *)prep->data);
+		prep->payload.data[0] = NULL;
+		prep->quotalen = 0;
+		return -EINVAL;
+	}

[Severity: High]
Can offset be used uninitialized when the second ':' in the header is missing?

In vsscanf(), %n does not increment the conversion count. An input such as "DHHC-1:00" converts version and hmac, fails to match the trailing ':', and returns 2 before reaching %n. Because offset is not initialized to 0, !offset reads whatever value is on the stack and later passes prep->data + offset to strlen() and strrchr().

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 341

Quoted 6 lines
drivers/nvme/common/keyring.c +6 -0
+
+	/* skip header and final ':' character */
+	datalen = strlen(prep->data + offset);
+	p = strrchr(prep->data + offset, ':');
+	if (p)
+		datalen--;

[Severity: Critical]
Can sscanf(), strlen(), strrchr(), and pr_debug() read past the end of prep->data?

add_key() and keyctl_update_key() allocate an exact plen-byte buffer with kvmalloc() and copy the user payload without adding a NUL terminator:

security/keys/keyctl.c:SYSCALL_DEFINE5(add_key) {
    ...
	if (plen) {
		ret = -ENOMEM;
		payload = kvmalloc(plen, GFP_KERNEL);
		if (!payload)
			goto error2;

		ret = -EFAULT;
		if (copy_from_user(payload, _payload, plen) != 0)
			goto error3;
	}
    ...
}

nvme_dhchap_psk_preparse() never bounds prep->data by prep->datalen before treating it as a NUL-terminated string, so a non-NUL-terminated payload from userspace causes out-of-bounds kernel memory reads.

Quoted 14 lines
drivers/nvme/common/keyring.c +14 -0
+
+	/*
+	 * 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;
+	}
+

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 365

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	/* decode the data */
+	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);
+		prep->payload.data[0] = NULL;
+		return ret;
+	}

[Severity: High]
Can base64_decode() read 1 byte past the key buffer and decode the wrong slice when offset is not 10?

Because 2 in %02hhu is a maximum field width, a header with a single-digit HMAC such as "DHHC-1:1:<base64>" sets offset to 9. datalen is computed from prep->data + offset, but base64_decode() starts at prep->data + 10, skipping the first base64 character and reading 1 byte past the end of the string.

Also, if a trailing ':' is followed by any other character (such as a newline), decrementing datalen by 1 instead of using p - (prep->data + offset) leaves trailing characters in datalen. And when base64_decode() fails it returns -1, so returning ret directly returns -EPERM to userspace instead of -EINVAL.

Quoted 4 lines
drivers/nvme/common/keyring.c +4 -0
+	if (ret != 36 && ret != 52 && ret != 68) {
+		pr_debug("%s: invalid decoded key size '%d'\n",
+			 __func__, ret);
+		prep->payload.data[0] = NULL;

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 379

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+		return -EINVAL;
+	}
+	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 */
+		prep->payload.data[0] = NULL;
+		return -EKEYREJECTED;
+	}

[Severity: High]
Does clearing prep->payload.data[0] = NULL on the error paths above leak upayload?

When ->preparse() returns an error, the keyring core calls ->free_preparse(), which is user_free_preparse() and frees prep->payload.data[0] via kfree_sensitive(). Setting prep->payload.data[0] = NULL on base64_decode(), key size, and CRC failures without freeing upayload causes user_free_preparse() to see NULL and leak the allocation.

[ ... ]

Quoted 15 lines
drivers/nvme/common/keyring.c +15 -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:

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 412

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+		keylen = 72;
+		break;
+	case 64:
+		keylen = 92;
+		break;
+	default:
+		break;
+	}
+	return keylen;
+}

[Severity: Low]
This isn't a bug, but the function name and kernel-doc comment are inverted:
this helper takes the decoded binary key size (32, 48, or 64) and returns the base64-encoded size including the 4-byte CRC (48, 72, or 92).

Quoted 32 lines
drivers/nvme/common/keyring.c +32 -0
+
+/**
+ * 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;
+
+	if (!buffer || buflen == 0)
+		return -EINVAL;
+
+	upayload = user_key_payload_locked(key);
+	/*
+	 * Expected output string length:
+	 * strlen("DHHC-") +
+	 * version (= 1) +
+	 * ':' (= 1) +
+	 * hmac (= 2) +

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 454

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	 * ':' (= 1) +
+	 * encoded base64 key length +
+	 * ':' (= 1)
+	 */
+	ret = nvme_dhchap_decoded_key_size(upayload->datalen);
+	if (ret < 0)
+		return ret;
+	keylen = ret + 11;
+	if (buflen < keylen)
+		return -EINVAL;

[Severity: Medium]
Should nvme_dhchap_psk_read() return keylen instead of -EINVAL when buffer is NULL, buflen is 0, or buflen < keylen?

key_type->read() is required to return the needed buffer size even when no buffer or a too-small buffer is provided. keyctl_read_key() calls __keyctl_read_key(key, NULL, 0) both when userspace probes the key length (such as keyctl read or keyctl print) and on the first pass whenever buflen > PAGE_SIZE, so returning -EINVAL breaks those reads.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#4 lore raw

Re: [PATCH v5 2/9] nvme-keyring: add 'dhchap' key type

Quoted 35 lines · On 11/23/26 1:36 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 8 potential issues to consider:
- [Critical] nvme-keyring: out-of-bounds read on prep->data in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: uninitialized offset used in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: header offset mismatch in nvme_dhchap_psk_preparse()
- [High] nvme-keyring: memory leak of upayload on error in nvme_dhchap_psk_preparse()
- [Medium] nvme-keyring: broken key length queries in nvme_dhchap_psk_read()
- [Low] nvme-keyring: inverted naming and comment for nvme_dhchap_decoded_key_size()
- [Low] nvme-keyring: missing select CRC32 in Kconfig for nvme_dhchap_psk_preparse()
- [Low] nvme-keyring: redundant nvme_dhchap_psk_match_preparse() forces linear scan
--

commit d9b56f363bf407639fb6f4f459820389f42a36f1
Author: Hannes Reinecke <[email protected]>

nvme-keyring: add 'dhchap' key type

This commit registers a 'dhchap' key type in the NVMe keyring module to parse, store, and read back base64-encoded DH-HMAC-CHAP secret keys.

Quoted 7 lines
drivers/nvme/common/keyring.c +0 -0
index 87cb51e1f5a7f..d0c36d153afdb 100644
@@ -4,6 +4,9 @@
   */
 
Quoted 14 lines

drivers/nvme/common/keyring.c line 12

Quoted 7 lines
drivers/nvme/common/keyring.c +3 -0
  #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>

[Severity: Low]
nvme_dhchap_psk_preparse() calls crc32(), which expands to crc32_le(), but config NVME_KEYRING in drivers/nvme/common/Kconfig only selects KEYS and does not select CRC32. Should NVME_KEYRING select CRC32 so builds without CONFIG_CRC32 enabled by another option do not fail at link time?

You are right, will be fixed in the next version.

Quoted 36 lines
Quoted 1 line
drivers/nvme/common/keyring.c +0 -0
@@ -253,6 +256,231 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,

[ ... ]

Quoted 32 lines
drivers/nvme/common/keyring.c +32 -0
+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);
+	if (match_len != strlen(key->description)) {
+		pr_debug("%s: match '%s' '%s' different size\n",
+			 __func__, match_id, key->description);
+		return false;
+	}
+	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;
Quoted 9 lines
Quoted 1 line

+}

[Severity: Low]
Is nvme_dhchap_psk_match_preparse() needed here? nvme_dhchap_psk_match() checks for an exact description match identical to key_default_cmp(), so overriding lookup_type to KEYRING_SEARCH_LOOKUP_ITERATE forces every dhchap key lookup to do a linear scan of the keyring instead of a direct associative array lookup.

Possibly not. I'll drop it for the next version.

Quoted 43 lines
Quoted 28 lines
drivers/nvme/common/keyring.c +28 -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;
+	int ret, offset;
+	char *p;
+	u32 crc;
+	u8 version, hmac;
+
+	if (!prep->data) {
+		pr_debug("%s: Empty data", __func__);
+		prep->payload.data[0] = NULL;
+		prep->quotalen = 0;
+		return -EINVAL;

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 335

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	}
+
+	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
+		   &version, &hmac, &offset) != 2 || !offset) {
+		pr_debug("%s: invalid key data '%s'\n", __func__,
+			 (char *)prep->data);
+		prep->payload.data[0] = NULL;
+		prep->quotalen = 0;
+		return -EINVAL;
+	}

[Severity: High]
Can offset be used uninitialized when the second ':' in the header is missing?

Quoted 6 lines

In vsscanf(), %n does not increment the conversion count. An input such as "DHHC-1:00" converts version and hmac, fails to match the trailing ':', and returns 2 before reaching %n. Because offset is not initialized to 0, !offset reads whatever value is on the stack and later passes prep->data + offset to strlen() and strrchr().

Fixed by initializing 'offset'.

Quoted 29 lines

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 341

Quoted 6 lines
drivers/nvme/common/keyring.c +6 -0
+
+	/* skip header and final ':' character */
+	datalen = strlen(prep->data + offset);
+	p = strrchr(prep->data + offset, ':');
+	if (p)
+		datalen--;

[Severity: Critical]
Can sscanf(), strlen(), strrchr(), and pr_debug() read past the end of prep->data?

add_key() and keyctl_update_key() allocate an exact plen-byte buffer with kvmalloc() and copy the user payload without adding a NUL terminator:

security/keys/keyctl.c:SYSCALL_DEFINE5(add_key) {
     ...
	if (plen) {
		ret = -ENOMEM;
		payload = kvmalloc(plen, GFP_KERNEL);
		if (!payload)
			goto error2;

		ret = -EFAULT;
		if (copy_from_user(payload, _payload, plen) != 0)
			goto error3;
	}
     ...
}
Quoted 4 lines

nvme_dhchap_psk_preparse() never bounds prep->data by prep->datalen before treating it as a NUL-terminated string, so a non-NUL-terminated payload from userspace causes out-of-bounds kernel memory reads.

Fixed by checking the data length and using memchr().

Quoted 29 lines
Quoted 14 lines
drivers/nvme/common/keyring.c +14 -0
+
+	/*
+	 * 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;
+	}
+

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 365

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	/* decode the data */
+	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);
+		prep->payload.data[0] = NULL;
+		return ret;
+	}

[Severity: High]
Can base64_decode() read 1 byte past the key buffer and decode the wrong slice when offset is not 10?

Quoted 5 lines

Because 2 in %02hhu is a maximum field width, a header with a single-digit HMAC such as "DHHC-1:1:<base64>" sets offset to 9. datalen is computed from prep->data + offset, but base64_decode() starts at prep->data + 10, skipping the first base64 character and reading 1 byte past the end of the string.

Fixed by checking for 'offset == 10' earlier on.

Quoted 6 lines

Also, if a trailing ':' is followed by any other character (such as a newline), decrementing datalen by 1 instead of using p - (prep->data + offset) leaves trailing characters in datalen. And when base64_decode() fails it returns -1, so returning ret directly returns -EPERM to userspace instead of -EINVAL.

Fixed by returning -EINVAL here.

Quoted 19 lines
Quoted 4 lines
drivers/nvme/common/keyring.c +4 -0
+	if (ret != 36 && ret != 52 && ret != 68) {
+		pr_debug("%s: invalid decoded key size '%d'\n",
+			 __func__, ret);
+		prep->payload.data[0] = NULL;

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 379

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+		return -EINVAL;
+	}
+	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 */
+		prep->payload.data[0] = NULL;
+		return -EKEYREJECTED;
+	}

[Severity: High]
Does clearing prep->payload.data[0] = NULL on the error paths above leak upayload?

Quoted 6 lines

When ->preparse() returns an error, the keyring core calls ->free_preparse(), which is user_free_preparse() and frees prep->payload.data[0] via kfree_sensitive(). Setting prep->payload.data[0] = NULL on base64_decode(), key size, and CRC failures without freeing upayload causes user_free_preparse() to see NULL and leak the allocation.

Fixed with the next version.

Quoted 24 lines

[ ... ]

Quoted 22 lines
drivers/nvme/common/keyring.c +22 -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;
Quoted 9 lines

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 412

Quoted 3 lines
drivers/nvme/common/keyring.c +3 -0
+	}
+	return keylen;
+}

[Severity: Low]
This isn't a bug, but the function name and kernel-doc comment are inverted:
this helper takes the decoded binary key size (32, 48, or 64) and returns the base64-encoded size including the 4-byte CRC (48, 72, or 92).

Updated the kernel-doc for the next version.

Quoted 47 lines
Quoted 32 lines
drivers/nvme/common/keyring.c +32 -0
+
+/**
+ * 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;
+
+	if (!buffer || buflen == 0)
+		return -EINVAL;
+
+	upayload = user_key_payload_locked(key);
+	/*
+	 * Expected output string length:
+	 * strlen("DHHC-") +
+	 * version (= 1) +
+	 * ':' (= 1) +
+	 * hmac (= 2) +

drivers/nvme/common/keyring.c key_serial_t nvme_tls_psk_default(struct key *keyring, · line 454

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+	 * ':' (= 1) +
+	 * encoded base64 key length +
+	 * ':' (= 1)
+	 */
+	ret = nvme_dhchap_decoded_key_size(upayload->datalen);
+	if (ret < 0)
+		return ret;
+	keylen = ret + 11;
+	if (buflen < keylen)
+		return -EINVAL;

[Severity: Medium]
Should nvme_dhchap_psk_read() return keylen instead of -EINVAL when buffer is NULL, buflen is 0, or buflen < keylen?

Quoted 6 lines

key_type->read() is required to return the needed buffer size even when no buffer or a too-small buffer is provided. keyctl_read_key() calls __keyctl_read_key(key, NULL, 0) both when userspace probes the key length (such as keyctl read or keyctl print) and on the first pass whenever buflen > PAGE_SIZE, so returning -EINVAL breaks those reads.

Fixed in the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#5 lore raw

PATCH v5 3/9 nvme-auth: switch to use 'struct key'

In reply to 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.

---
 drivers/nvme/common/Kconfig    |   1 +
 drivers/nvme/common/auth.c     | 197 ++++++++++++++++-------------------------
 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/configfs.c |   4 +-
 drivers/nvme/target/nvmet.h    |   8 +-
 include/linux/nvme-auth.h      |  17 +---
 include/linux/nvme-keyring.h   |  26 +++++-
 13 files changed, 268 insertions(+), 195 deletions(-)
drivers/nvme/common/Kconfig +1 -0
index f1639db65fd38abde01832a7404cbc48cf49a93e..9f72c2668f2f6b71f20d737f9849f1bb9cd88dc6 100644
@@ -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
drivers/nvme/common/auth.c +76 -121

243 lines not on this page · raw message

drivers/nvme/common/keyring.c +98 -0

104 lines not on this page · raw message

drivers/nvme/host/Kconfig +0 -1
index 31974c7dd20c9174961eac91e3823e939d325daa..6d350ef617bfb47770ebce48c3abb25a4b6a4c8c 100644
@@ -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.
drivers/nvme/host/auth.c +19 -9
index 295e0a9533c8995c32781324650d73b6bfedbf13..0ddf3834f4ea2bfedaf08b901db282b79df32879 100644
@@ -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;
 	}
 }
drivers/nvme/host/nvme.h +2 -2
index 2543e52d6358a635623fab925167bea7ac4a24a2..434e3612c996a969075aa5844c51906c605e6f56 100644
@@ -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;
drivers/nvme/host/sysfs.c +12 -14
index 35f871e3e0c0ae49b390f0fb64c538796207a98f..a5c1511e49ac275e75a70ff89958ed8f0cb2c633 100644
@@ -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 */
drivers/nvme/target/Kconfig +0 -1
index 69bde270115e29bc8c89f4f58791190932dbe852..700e87d9e610e2532a871ad8af90573a1931c30b 100644
@@ -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
drivers/nvme/target/auth.c +27 -25

87 lines not on this page · raw message

drivers/nvme/target/configfs.c +2 -2
index 27c50fa533e70e443a2eb3fed5e31e730fb0ce3c..ff3a8d74de59966952a3991542077feebae10770 100644
@@ -2234,7 +2234,7 @@ static struct config_group nvmet_ports_group;
 static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
 		char *page)
 {
-	u8 *dhchap_secret;
+	char *dhchap_secret;
 	ssize_t ret;
 
 	down_read(&nvmet_config_sem);
@@ -2267,7 +2267,7 @@ CONFIGFS_ATTR(nvmet_host_, dhchap_key);
 static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
 		char *page)
 {
-	u8 *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
+	char *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
 	ssize_t ret;
 
 	down_read(&nvmet_config_sem);
drivers/nvme/target/nvmet.h +4 -4
index e33884a81efc9f96cdc5d0120e0701f06ce7d873..1dfdac159dab044566aac47862665784feddec0a 100644
@@ -309,8 +309,8 @@ struct nvmet_ctrl {
 	bool			pi_support;
 	bool			concat;
 #ifdef CONFIG_NVME_TARGET_AUTH
-	struct nvme_dhchap_key	*host_key;
-	struct nvme_dhchap_key	*ctrl_key;
+	struct key		*host_key;
+	struct key		*ctrl_key;
 	u8			shash_id;
 	struct crypto_kpp	*dh_tfm;
 	u8			dh_gid;
@@ -399,8 +399,8 @@ static inline struct nvmet_subsys *namespaces_to_subsys(
 
 struct nvmet_host {
 	struct config_group	group;
-	u8			*dhchap_secret;
-	u8			*dhchap_ctrl_secret;
+	char			*dhchap_secret;
+	char			*dhchap_ctrl_secret;
 	int			dhchap_key_offset;
 	int			dhchap_ctrl_key_offset;
 	u8			dhchap_key_hash;
include/linux/nvme-auth.h +4 -13
index 1c5ce635b169dd0747c1344e9adcc14839776cd1..75e1e75e298e1c3b408ea234705edd7abf788d1d 100644
@@ -9,12 +9,6 @@
 #include <crypto/kpp.h>
 #include <crypto/sha2.h>
 
-struct nvme_dhchap_key {
-	size_t len;
-	u8 hash;
-	u8 key[] __counted_by(len);
-};
-
 u32 nvme_auth_get_seqnum(void);
 const char *nvme_auth_dhgroup_name(u8 dhgroup_id);
 const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id);
@@ -37,13 +31,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);
include/linux/nvme-keyring.h +23 -3
index ab8971afa9734a276d9197076b780321460dddc2..a2ba7588e2d736c405b7c15666a85e500783a91c 100644
@@ -18,12 +18,18 @@ 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 char *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,
+		const char *hostnqn, const char *subnqn, *data, 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 +42,21 @@ 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_create(struct key *keyring,
+		const char *data, size_t data_len)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
+		const char *identity)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+u8 nvme_dhchap_psk_hash(struct key *key)
+{
+	return 0;
 }
 #endif /* !CONFIG_NVME_KEYRING */
 #endif /* _NVME_KEYRING_H */
Signature · 1 line
2.51.0
#6 lore raw

Re: [PATCH v5 3/9] nvme-auth: switch to use 'struct key'

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] nvme-keyring: build failure in !CONFIG_NVME_KEYRING header stubs - [High] nvme-auth: broken offset…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] nvme-keyring: build failure in !CONFIG_NVME_KEYRING header stubs
- [High] nvme-auth: broken offset, payload leak, and -ENOKEY in key extraction
- [Low] nvme-auth: missing memzero_explicit() on error in nvme_auth_transform_key()
--

commit 784e3a15ff15feda9e4de8c374a9d16d4c147b86
Author: Hannes Reinecke <[email protected]>

nvme-auth: switch to use 'struct key'

This commit switches NVMe in-band authentication to store DH-HMAC-CHAP keys in struct key using the dhchap key type and removes struct nvme_dhchap_key.

Quoted 4 lines
drivers/nvme/common/auth.c +0 -0
index 308af3d7c7383..ee26fb416ee62 100644

[ ... ]

Quoted 72 lines
drivers/nvme/common/auth.c +39 -15
@@ -317,11 +261,13 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
 	return -EINVAL;
 }
 
-int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
+int nvme_auth_transform_key(struct key *key, const char *nqn,
 			    u8 **transformed_secret)
 {
 	struct nvme_auth_hmac_ctx hmac;
-	u8 *key_data;
+	long key_len = 0;
+	u8 *transformed_data;
+	u8 *key_data, key_hash;
 	size_t transformed_len;
 	int ret;
 
@@ -329,28 +275,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);
-		if (!key_data)
-			return -ENOMEM;
-		memcpy(key_data, key->key, key->len);
-		*transformed_secret = key_data;
-		return key->len;
-	}
-	ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
-	if (ret)
+	down_read(&key->sem);
+	ret = key_validate(key);
+	if (ret) {
+		pr_warn("%s: key %08x invalidated\n",
+			__func__, key_serial(key));
+		up_read(&key->sem);
 		return ret;
-	transformed_len = nvme_auth_hmac_hash_len(key->hash);
-	key_data = kzalloc(transformed_len, GFP_KERNEL);
+	}
+	key_len = user_read(key, NULL, 0);
+	if (key_len <= 0) {
+		pr_warn("failed to get length from key %08x: error %ld\n",
+			key_serial(key), key_len);
+		up_read(&key->sem);
+		return key_len < 0 ? key_len : -ENOKEY;
+	}
+
+	key_data = kzalloc(key_len, GFP_KERNEL);
 	if (!key_data) {
-		memzero_explicit(&hmac, sizeof(hmac));
+		up_read(&key->sem);
 		return -ENOMEM;
 	}
+	ret = user_read(key, (char *)key_data, key_len);
+	key_hash = nvme_dhchap_psk_hash(key);
+	up_read(&key->sem);
+	if (ret != key_len) {
+		if (ret < 0) {
+			pr_warn("failed to read from key %08x: error %d\n",
+				key_serial(key), ret);
+		} else {
+			pr_warn("only read %d of %ld bytes from key %08x\n",
+				ret, key_len, key_serial(key));
+			ret = -ENOKEY;
+		}
+		goto out_free_data;
+	}
+	if (key_hash == 0) {
+		*transformed_secret = key_data;
+		return key_len;
+	}

drivers/nvme/common/auth.c int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn, · line 326

Quoted 10 lines
drivers/nvme/common/auth.c +10 -0
+
+	ret = nvme_auth_hmac_init(&hmac, key_hash, key_data, key_len);
+	if (ret)
+		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;
+	}

[Severity: Low]
Does the !transformed_data error path leave key-derived state in hmac on the stack? Before this patch, failing to allocate the output buffer after nvme_auth_hmac_init() called memzero_explicit(&hmac, sizeof(hmac)) because nvme_auth_hmac_final() is not reached to zeroize the context.

Quoted 44 lines
drivers/nvme/common/keyring.c +36 -0
index d0c36d153afdb..08e2a8e39faac 100644
@@ -481,6 +481,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 char *data, size_t data_len)
+{
+	key_perm_t keyperm =
+		KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
+		KEY_POS_WRITE | KEY_POS_LINK | KEY_POS_SETATTR |
+		KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
+	char *identity;
+	key_ref_t keyref;
+	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),

drivers/nvme/common/keyring.c static struct key_type nvme_dhchap_psk_key_type = { · line 529

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+			    "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);
+	}

[Severity: High]
Routing nvme_auth_extract_key() through nvme_dhchap_psk_create() and nvme_dhchap_psk_preparse() introduces several regressions when parsing or rejecting DH-HMAC-CHAP secrets:

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
		   &version, &hmac, &offset) != 2 || !offset) {
	...
	/* skip header and final ':' character */
	datalen = strlen(prep->data + offset);
	p = strrchr(prep->data + offset, ':');
	if (p)
		datalen--;
	...
	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);
		prep->payload.data[0] = NULL;
		return ret;
	}
	if (ret != 36 && ret != 52 && ret != 68) {
		pr_debug("%s: invalid decoded key size '%d'\n",
			 __func__, ret);
		prep->payload.data[0] = NULL;
		return -EINVAL;
	}
	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 */
		prep->payload.data[0] = NULL;
		return -EKEYREJECTED;
	}
	...
}

Does base64_decode() fail on secrets with a single-digit hash ID (such as "DHHC-1:0:...") and leak upayload on error?

With a 1-digit hash ID, offset is 9, so hardcoding prep->data + 10 skips the first base64 character and reads one byte past the end of the payload. When base64_decode(), the decoded length check, or the CRC check fails, setting prep->payload.data[0] = NULL before returning prevents user_free_preparse() from freeing upayload, leaking it on every rejected key.

In addition, prep->data is passed to sscanf(), strlen(), and strrchr() without bounding to prep->datalen (and with offset uninitialized if the colon after the hash ID is omitted), "if (p) datalen--" only subtracts 1 instead of truncating at p when a trailing newline follows ':', and nvme_dhchap_psk_create() overwrites the error from key_create() with -ENOKEY.

Quoted 38 lines
include/linux/nvme-keyring.h +16 -3
index ab8971afa9734..a2ba7588e2d73 100644
@@ -18,12 +18,18 @@ 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 char *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,
+		const char *hostnqn, const char *subnqn, *data, 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 +42,21 @@ 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_create(struct key *keyring,
+		const char *data, size_t data_len)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,

include/linux/nvme-keyring.h static inline key_serial_t nvme_keyring_id(void) · line 62

Quoted 10 lines
include/linux/nvme-keyring.h +7 -0
+		const char *identity)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+u8 nvme_dhchap_psk_hash(struct key *key)
+{
+	return 0;
 }
 #endif /* !CONFIG_NVME_KEYRING */
 #endif /* _NVME_KEYRING_H */

[Severity: High]
As reported by kernel test robot on v4 [1], does this break the build when CONFIG_NVME_KEYRING is disabled? The !CONFIG_NVME_KEYRING stub for nvme_tls_psk_refresh() has a stray untyped *data parameter before hmac_id, and nvme_dhchap_psk_hash() is defined in the header without static inline, causing multiple-definition linker errors across translation units that include linux/nvme-keyring.h.

[1] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer
#7 lore raw

Re: [PATCH v5 3/9] nvme-auth: switch to use 'struct key'

Quoted 103 lines · On 11/23/26 1:35 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] nvme-keyring: build failure in !CONFIG_NVME_KEYRING header stubs
- [High] nvme-auth: broken offset, payload leak, and -ENOKEY in key extraction
- [Low] nvme-auth: missing memzero_explicit() on error in nvme_auth_transform_key()
--

commit 784e3a15ff15feda9e4de8c374a9d16d4c147b86
Author: Hannes Reinecke <[email protected]>

nvme-auth: switch to use 'struct key'

This commit switches NVMe in-band authentication to store DH-HMAC-CHAP keys in struct key using the dhchap key type and removes struct nvme_dhchap_key.

Quoted 4 lines
drivers/nvme/common/auth.c +0 -0
index 308af3d7c7383..ee26fb416ee62 100644

[ ... ]

Quoted 80 lines
drivers/nvme/common/auth.c +47 -15
@@ -317,11 +261,13 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
  	return -EINVAL;
  }
 
-int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
+int nvme_auth_transform_key(struct key *key, const char *nqn,
  			    u8 **transformed_secret)
  {
  	struct nvme_auth_hmac_ctx hmac;
-	u8 *key_data;
+	long key_len = 0;
+	u8 *transformed_data;
+	u8 *key_data, key_hash;
  	size_t transformed_len;
  	int ret;
 
@@ -329,28 +275,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);
-		if (!key_data)
-			return -ENOMEM;
-		memcpy(key_data, key->key, key->len);
-		*transformed_secret = key_data;
-		return key->len;
-	}
-	ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len);
-	if (ret)
+	down_read(&key->sem);
+	ret = key_validate(key);
+	if (ret) {
+		pr_warn("%s: key %08x invalidated\n",
+			__func__, key_serial(key));
+		up_read(&key->sem);
  		return ret;
-	transformed_len = nvme_auth_hmac_hash_len(key->hash);
-	key_data = kzalloc(transformed_len, GFP_KERNEL);
+	}
+	key_len = user_read(key, NULL, 0);
+	if (key_len <= 0) {
+		pr_warn("failed to get length from key %08x: error %ld\n",
+			key_serial(key), key_len);
+		up_read(&key->sem);
+		return key_len < 0 ? key_len : -ENOKEY;
+	}
+
+	key_data = kzalloc(key_len, GFP_KERNEL);
  	if (!key_data) {
-		memzero_explicit(&hmac, sizeof(hmac));
+		up_read(&key->sem);
  		return -ENOMEM;
  	}
+	ret = user_read(key, (char *)key_data, key_len);
+	key_hash = nvme_dhchap_psk_hash(key);
+	up_read(&key->sem);
+	if (ret != key_len) {
+		if (ret < 0) {
+			pr_warn("failed to read from key %08x: error %d\n",
+				key_serial(key), ret);
+		} else {
+			pr_warn("only read %d of %ld bytes from key %08x\n",
+				ret, key_len, key_serial(key));
+			ret = -ENOKEY;
+		}
+		goto out_free_data;
+	}
+	if (key_hash == 0) {
+		*transformed_secret = key_data;
+		return key_len;
+	}
+
+	ret = nvme_auth_hmac_init(&hmac, key_hash, key_data, key_len);
+	if (ret)
+		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;
Quoted 9 lines

drivers/nvme/common/auth.c int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn, · line 326

Quoted 2 lines
drivers/nvme/common/auth.c +2 -0
+		goto out_free_data;
+	}

[Severity: Low]
Does the !transformed_data error path leave key-derived state in hmac on the stack? Before this patch, failing to allocate the output buffer after nvme_auth_hmac_init() called memzero_explicit(&hmac, sizeof(hmac)) because nvme_auth_hmac_final() is not reached to zeroize the context.

Moved kzalloc() before calling nvme_auth_hmac_init().

Quoted 97 lines
Quoted 44 lines
drivers/nvme/common/keyring.c +36 -0
index d0c36d153afdb..08e2a8e39faac 100644
@@ -481,6 +481,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 char *data, size_t data_len)
+{
+	key_perm_t keyperm =
+		KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
+		KEY_POS_WRITE | KEY_POS_LINK | KEY_POS_SETATTR |
+		KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
+	char *identity;
+	key_ref_t keyref;
+	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),

drivers/nvme/common/keyring.c static struct key_type nvme_dhchap_psk_key_type = { · line 529

Quoted 10 lines
drivers/nvme/common/keyring.c +10 -0
+			    "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);
+	}

[Severity: High]
Routing nvme_auth_extract_key() through nvme_dhchap_psk_create() and nvme_dhchap_psk_preparse() introduces several regressions when parsing or rejecting DH-HMAC-CHAP secrets:

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
		   &version, &hmac, &offset) != 2 || !offset) {
	...
	/* skip header and final ':' character */
	datalen = strlen(prep->data + offset);
	p = strrchr(prep->data + offset, ':');
	if (p)
		datalen--;
	...
	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);
		prep->payload.data[0] = NULL;
		return ret;
	}
	if (ret != 36 && ret != 52 && ret != 68) {
		pr_debug("%s: invalid decoded key size '%d'\n",
			 __func__, ret);
		prep->payload.data[0] = NULL;
		return -EINVAL;
	}
	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 */
		prep->payload.data[0] = NULL;
		return -EKEYREJECTED;
	}
	...
}
Quoted 3 lines

Does base64_decode() fail on secrets with a single-digit hash ID (such as "DHHC-1:0:...") and leak upayload on error?

key parsing has been fixed with an updated version to the previous patch.

Quoted 71 lines

With a 1-digit hash ID, offset is 9, so hardcoding prep->data + 10 skips the first base64 character and reads one byte past the end of the payload. When base64_decode(), the decoded length check, or the CRC check fails, setting prep->payload.data[0] = NULL before returning prevents user_free_preparse() from freeing upayload, leaking it on every rejected key.

In addition, prep->data is passed to sscanf(), strlen(), and strrchr() without bounding to prep->datalen (and with offset uninitialized if the colon after the hash ID is omitted), "if (p) datalen--" only subtracts 1 instead of truncating at p when a trailing newline follows ':', and nvme_dhchap_psk_create() overwrites the error from key_create() with -ENOKEY.

Quoted 38 lines
include/linux/nvme-keyring.h +16 -3
index ab8971afa9734..a2ba7588e2d73 100644
@@ -18,12 +18,18 @@ 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 char *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,
+		const char *hostnqn, const char *subnqn, *data, 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 +42,21 @@ 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_create(struct key *keyring,
+		const char *data, size_t data_len)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,

include/linux/nvme-keyring.h static inline key_serial_t nvme_keyring_id(void) · line 62

Quoted 10 lines
include/linux/nvme-keyring.h +7 -0
+		const char *identity)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+u8 nvme_dhchap_psk_hash(struct key *key)
+{
+	return 0;
  }
  #endif /* !CONFIG_NVME_KEYRING */
  #endif /* _NVME_KEYRING_H */

[Severity: High]
As reported by kernel test robot on v4 [1], does this break the build when CONFIG_NVME_KEYRING is disabled? The !CONFIG_NVME_KEYRING stub for nvme_tls_psk_refresh() has a stray untyped *data parameter before hmac_id, and nvme_dhchap_psk_hash() is defined in the header without static inline, causing multiple-definition linker errors across translation units that include linux/nvme-keyring.h.

Quoted 2 lines

[1] https://lore.kernel.org/r/[email protected]

Fixed with the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#8 lore raw

PATCH v5 4/9 nvme: parse dhchap keys during option parsing

In reply to 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.

---
 drivers/nvme/host/auth.c    | 124 ++++++++++++++++++--------
 drivers/nvme/host/fabrics.c |  96 ++++++++++++++------
 drivers/nvme/host/fabrics.h |   8 +-
 drivers/nvme/host/sysfs.c   | 212 +++++++++++++++++++++++++++++++++-----------
 4 files changed, 320 insertions(+), 120 deletions(-)
drivers/nvme/host/auth.c +86 -38
index 0ddf3834f4ea2bfedaf08b901db282b79df32879..535cc433dd35d305a40604f99627e4a248262f17 100644
@@ -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,40 +1024,106 @@ 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;
+	struct key *host_key, *ctrl_key = NULL;
 	int i, ret;
 
 	mutex_init(&ctrl->dhchap_auth_mutex);
 	INIT_WORK(&ctrl->dhchap_auth_work, nvme_ctrl_auth_work);
 	if (!ctrl->opts)
 		return 0;
-	ctrl->host_key = nvme_auth_extract_key(ctrl->opts->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;
 	}
 
-	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;
+	host_key = key_get(ctrl->opts->dhchap_key);
+	if (!host_key) {
+		dev_warn(ctrl->device,
+			 "dhchap host key %08x not present\n",
+			 key_serial(ctrl->opts->dhchap_key));
+		return -ENOKEY;
+	}
+	down_read(&host_key->sem);
+	ret = key_validate(host_key);
+	up_read(&host_key->sem);
+	if (ret) {
+		dev_warn(ctrl->device,
+			 "dhchap host key %08x invalidated\n",
+			 key_serial(host_key));
+		key_put(host_key);
+		return -ENOKEY;
 	}
 
-	if (!ctrl->opts->dhchap_secret && !ctrl->opts->dhchap_ctrl_secret)
-		return 0;
+	if (ctrl->opts->dhchap_ctrl_key) {
+		ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
+		if (!ctrl_key) {
+			dev_warn(ctrl->device,
+				 "dhchap ctrl key %08x not present\n",
+				 key_serial(ctrl->opts->dhchap_ctrl_key));
+			key_put(host_key);
+			return -ENOKEY;
+		}
+		down_read(&ctrl_key->sem);
+		ret = key_validate(ctrl_key);
+		up_read(&ctrl_key->sem);
+		if (ret) {
+			dev_warn(ctrl->device,
+				 "dhchap ctrl key %08x invalidated\n",
+				 key_serial(ctrl_key));
+			key_put(ctrl_key);
+			key_put(host_key);
+			return -EKEYREVOKED;
+		}
+	}
+
+	dev_dbg(ctrl->device,
+		"%s: using dhchap host key %08x\n",
+		__func__, key_serial(ctrl->host_key));
+	if (ctrl->host_key)
+		nvme_auth_clear_host_key(ctrl);
+	ctrl->host_key = host_key;
+
+	if (ctrl->ctrl_key)
+		nvme_auth_clear_ctrl_key(ctrl);
+	if (ctrl_key) {
+		dev_dbg(ctrl->device,
+			"%s: using dhchap ctrl key %08x\n",
+			__func__, key_serial(ctrl->ctrl_key));
+		ctrl->ctrl_key = ctrl_key;
+	}
 
 	ctrl->dhchap_ctxs = kvzalloc_objs(*chap, ctrl_max_dhchaps(ctrl));
 	if (!ctrl->dhchap_ctxs) {
-		ret = -ENOMEM;
-		goto err_free_dhchap_ctrl_secret;
+		nvme_auth_clear_ctrl_key(ctrl);
+		nvme_auth_clear_host_key(ctrl);
+		return -ENOMEM;
 	}
 
 	for (i = 0; i < ctrl_max_dhchaps(ctrl); i++) {
@@ -1074,13 +1135,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 +1153,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);
 
drivers/nvme/host/fabrics.c +70 -26

151 lines not on this page · raw message

drivers/nvme/host/fabrics.h +4 -4
index caf5503d0833296aa03c028c0199cf92cc409edc..786f1b4819f076ac1f133070523be1780ec4d8b7 100644
@@ -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;
drivers/nvme/host/sysfs.c +160 -52

280 lines not on this page · raw message

Signature · 1 line
2.51.0
#9 lore raw

Re: [PATCH v5 4/9] nvme: parse dhchap keys during option parsing

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] nvme: build failure in sysfs.c when CONFIG_NVME_KEYRING is disabled - [Low] nvme-auth: log of…

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] nvme: build failure in sysfs.c when CONFIG_NVME_KEYRING is disabled
- [Low] nvme-auth: log of unassigned keys and NULL device in nvme_auth_init_ctrl()
- [Low] nvme-fabrics: dead !CONFIG_NVME_HOST_AUTH branches in nvmf_parse_options()
- [Low] nvme: missing trailing newline in dhchap_ctrl_secret_show() on invalid key
- [Low] nvme: wrong device pointer logged in dhchap_secret_store() and ctrl store
- [Low] nvme: undocumented dhchap_key sysfs attrs and inaccurate changelog
--

commit 8a46b5c132a1f37ed808c2b1576c3ace8af7cf7f
Author: Hannes Reinecke <[email protected]>

nvme: parse dhchap keys during option parsing

This commit extracts DH-HMAC-CHAP secrets into key objects during fabrics option parsing and exposes the active keys and their serial numbers via controller sysfs attributes.

Quoted 4 lines
drivers/nvme/host/auth.c +0 -0
index 0ddf3834f4ea2..535cc433dd35d 100644

[ ... ]

Quoted 1 line
drivers/nvme/host/auth.c +0 -0
@@ -1029,40 +1024,106 @@ static void nvme_ctrl_auth_work(struct work_struct *work)

[ ... ]

Quoted 5 lines
drivers/nvme/host/auth.c +5 -0
+	dev_dbg(ctrl->device,
+		"%s: using dhchap host key %08x\n",
+		__func__, key_serial(ctrl->host_key));
+	if (ctrl->host_key)
+		nvme_auth_clear_host_key(ctrl);

drivers/nvme/host/auth.c static void nvme_ctrl_auth_work(struct work_struct *work) · line 1120

Quoted 10 lines
drivers/nvme/host/auth.c +10 -0
+	ctrl->host_key = host_key;
+
+	if (ctrl->ctrl_key)
+		nvme_auth_clear_ctrl_key(ctrl);
+	if (ctrl_key) {
+		dev_dbg(ctrl->device,
+			"%s: using dhchap ctrl key %08x\n",
+			__func__, key_serial(ctrl->ctrl_key));
+		ctrl->ctrl_key = ctrl_key;
+	}

[Severity: Low]
Do these two dev_dbg() calls pass ctrl->host_key and ctrl->ctrl_key to key_serial() before assigning host_key and ctrl_key, always logging key serial 00000000?

Also, nvme_init_ctrl() calls nvme_auth_init_ctrl() before setting ctrl->device = &ctrl->ctrl_device, so ctrl->device is NULL for all dev_warn() and dev_dbg() calls in nvme_auth_init_ctrl().

Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 5ed91557f0706..0c09888be31f7 100644

[ ... ]

Quoted 40 lines
drivers/nvme/host/fabrics.c +36 -0
@@ -1086,6 +1078,53 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
 				opts->fast_io_fail_tmo, ctrl_loss_tmo);
 	}
+
+	opts->host = nvmf_host_add(hostnqn, &hostid);
+	if (IS_ERR(opts->host)) {
+		ret = PTR_ERR(opts->host);
+		opts->host = NULL;
+		goto out;
+	}
+
+	if (host_secret) {
+#ifdef CONFIG_NVME_HOST_AUTH
+		pr_debug("lookup 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;
+#else
+		pr_warn("nvme authentication is disabled\n");
+		ret = -EINVAL;
+		goto out;
+#endif
+	}
+	if (ctrl_secret) {
+#ifdef CONFIG_NVME_HOST_AUTH
+		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);

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 1126

Quoted 10 lines
drivers/nvme/host/fabrics.c +10 -0
+			goto out;
+		}
+		pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+		opts->dhchap_ctrl_key = key;
+#else
+		pr_warn("nvme authentication is disabled\n");
+		ret = -EINVAL;
+		goto out;
+#endif
+	}

[Severity: Low]
Can the !CONFIG_NVME_HOST_AUTH branches under if (host_secret) and if (ctrl_secret) ever be reached?

NVMF_OPT_DHCHAP_SECRET and NVMF_OPT_DHCHAP_CTRL_SECRET in opt_tokens are guarded by #ifdef CONFIG_NVME_HOST_AUTH, so when CONFIG_NVME_HOST_AUTH is disabled, match_token() rejects dhchap_secret and dhchap_ctrl_secret as unknown parameters and jumps to out while host_secret and ctrl_secret are still NULL.

drivers/nvme/host/sysfs.c line 11

Quoted 11 lines
drivers/nvme/host/sysfs.c +2 -0
index a5c1511e49ac2..e73c3ad89adc0 100644
@@ -6,6 +6,8 @@
  */
 
 #include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>
 #include <linux/blkdev.h>

[Severity: High]
Does including linux/nvme-keyring.h unconditionally in sysfs.c break the build when CONFIG_NVME_CORE is enabled and CONFIG_NVME_KEYRING is disabled?

In the !IS_ENABLED(CONFIG_NVME_KEYRING) block of include/linux/nvme-keyring.h (updated earlier in commit "nvme-auth: switch to use 'struct key'"), nvme_tls_psk_refresh() has a malformed parameter list and nvme_dhchap_psk_hash() is defined without static inline:

include/linux/nvme-keyring.h:
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
const char *hostnqn, const char *subnqn, *data, u8 hmac_id, u8 *data, size_t data_len, const char *digest)
...
u8 nvme_dhchap_psk_hash(struct key *key)
{
return 0;
}

Since sysfs.c is built for any CONFIG_NVME_CORE configuration and does not use any symbols from linux/nvme-keyring.h, compiling with CONFIG_NVME_KEYRING=n fails with syntax and multiple-definition errors.

Quoted 1 line
drivers/nvme/host/sysfs.c +0 -0
@@ -830,36 +850,47 @@ static ssize_t dhchap_secret_store(struct device *dev,

[ ... ]

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+	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));

drivers/nvme/host/sysfs.c static ssize_t dhchap_secret_store(struct device *dev, · line 888

Quoted 10 lines
drivers/nvme/host/sysfs.c +10 -0
+		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);

[Severity: Low]
Should these dev_warn() and dev_dbg() calls in dhchap_secret_store() and dhchap_ctrl_secret_store() use ctrl->device instead of ctrl->dev?

ctrl->dev is the parent transport device (such as the shared /sys/class/nvme-fabrics/ctl device), whereas ctrl->device is the nvmeX controller device used by the rest of the function.

Quoted 9 lines
drivers/nvme/host/sysfs.c +2 -2
@@ -873,11 +904,28 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
 		struct device_attribute *attr, char *buf)
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
-	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key;
+	ssize_t count;
 
-	if (!opts->dhchap_ctrl_secret)

drivers/nvme/host/sysfs.c static ssize_t dhchap_ctrl_secret_show(struct device *dev, · line 918

Quoted 10 lines
drivers/nvme/host/sysfs.c +8 -1
+	mutex_lock(&ctrl->dhchap_auth_mutex);
+	key = ctrl->ctrl_key;
+	if (!key) {
+		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+	}
+	down_read(&key->sem);
+	if (key_validate(key))
+		count = sysfs_emit(buf, "<invalidated>");

[Severity: Low]
Is a trailing newline missing from "<invalidated>" in dhchap_ctrl_secret_show()? dhchap_secret_show(), dhchap_key_show(), and dhchap_ctrl_key_show() all emit "<invalidated>\n".

[ ... ]

Quoted 8 lines
drivers/nvme/host/sysfs.c +4 -0
@@ -925,6 +981,52 @@ 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)
+{

[ ... ]

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_key);
+
+static ssize_t dhchap_ctrl_key_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{

[ ... ]

drivers/nvme/host/sysfs.c

Quoted 2 lines
drivers/nvme/host/sysfs.c +2 -0
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);

[Severity: Low]
This isn't a bug, but the new dhchap_key and dhchap_ctrl_key sysfs attributes are not mentioned in the commit message or documented in Documentation/ABI/stable/sysfs-nvme. Also, the commit message says the key serial is stored in struct nvmf_ctrl_options, whereas opts stores struct key pointers (dhchap_key and dhchap_ctrl_key).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#10 lore raw

Re: [PATCH v5 4/9] nvme: parse dhchap keys during option parsing

Quoted 53 lines · On 11/23/26 1:34 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] nvme: build failure in sysfs.c when CONFIG_NVME_KEYRING is disabled
- [Low] nvme-auth: log of unassigned keys and NULL device in nvme_auth_init_ctrl()
- [Low] nvme-fabrics: dead !CONFIG_NVME_HOST_AUTH branches in nvmf_parse_options()
- [Low] nvme: missing trailing newline in dhchap_ctrl_secret_show() on invalid key
- [Low] nvme: wrong device pointer logged in dhchap_secret_store() and ctrl store
- [Low] nvme: undocumented dhchap_key sysfs attrs and inaccurate changelog
--

commit 8a46b5c132a1f37ed808c2b1576c3ace8af7cf7f
Author: Hannes Reinecke <[email protected]>

nvme: parse dhchap keys during option parsing

This commit extracts DH-HMAC-CHAP secrets into key objects during fabrics option parsing and exposes the active keys and their serial numbers via controller sysfs attributes.

Quoted 4 lines
drivers/nvme/host/auth.c +0 -0
index 0ddf3834f4ea2..535cc433dd35d 100644

[ ... ]

Quoted 1 line
drivers/nvme/host/auth.c +0 -0
@@ -1029,40 +1024,106 @@ static void nvme_ctrl_auth_work(struct work_struct *work)

[ ... ]

Quoted 5 lines
drivers/nvme/host/auth.c +5 -0
+	dev_dbg(ctrl->device,
+		"%s: using dhchap host key %08x\n",
+		__func__, key_serial(ctrl->host_key));
+	if (ctrl->host_key)
+		nvme_auth_clear_host_key(ctrl);

drivers/nvme/host/auth.c static void nvme_ctrl_auth_work(struct work_struct *work) · line 1120

Quoted 10 lines
drivers/nvme/host/auth.c +10 -0
+	ctrl->host_key = host_key;
+
+	if (ctrl->ctrl_key)
+		nvme_auth_clear_ctrl_key(ctrl);
+	if (ctrl_key) {
+		dev_dbg(ctrl->device,
+			"%s: using dhchap ctrl key %08x\n",
+			__func__, key_serial(ctrl->ctrl_key));
+		ctrl->ctrl_key = ctrl_key;
+	}

[Severity: Low]
Do these two dev_dbg() calls pass ctrl->host_key and ctrl->ctrl_key to key_serial() before assigning host_key and ctrl_key, always logging key serial 00000000?

Quoted 4 lines

Also, nvme_init_ctrl() calls nvme_auth_init_ctrl() before setting ctrl->device = &ctrl->ctrl_device, so ctrl->device is NULL for all dev_warn() and dev_dbg() calls in nvme_auth_init_ctrl().

Fixed with the next version.

Quoted 60 lines
Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 5ed91557f0706..0c09888be31f7 100644

[ ... ]

Quoted 40 lines
drivers/nvme/host/fabrics.c +36 -0
@@ -1086,6 +1078,53 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  			pr_warn("failfast tmo (%d) larger than controller loss tmo (%d)\n",
  				opts->fast_io_fail_tmo, ctrl_loss_tmo);
  	}
+
+	opts->host = nvmf_host_add(hostnqn, &hostid);
+	if (IS_ERR(opts->host)) {
+		ret = PTR_ERR(opts->host);
+		opts->host = NULL;
+		goto out;
+	}
+
+	if (host_secret) {
+#ifdef CONFIG_NVME_HOST_AUTH
+		pr_debug("lookup 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;
+#else
+		pr_warn("nvme authentication is disabled\n");
+		ret = -EINVAL;
+		goto out;
+#endif
+	}
+	if (ctrl_secret) {
+#ifdef CONFIG_NVME_HOST_AUTH
+		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);

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 1126

Quoted 10 lines
drivers/nvme/host/fabrics.c +10 -0
+			goto out;
+		}
+		pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+		opts->dhchap_ctrl_key = key;
+#else
+		pr_warn("nvme authentication is disabled\n");
+		ret = -EINVAL;
+		goto out;
+#endif
+	}

[Severity: Low]
Can the !CONFIG_NVME_HOST_AUTH branches under if (host_secret) and if (ctrl_secret) ever be reached?

Quoted 6 lines

NVMF_OPT_DHCHAP_SECRET and NVMF_OPT_DHCHAP_CTRL_SECRET in opt_tokens are guarded by #ifdef CONFIG_NVME_HOST_AUTH, so when CONFIG_NVME_HOST_AUTH is disabled, match_token() rejects dhchap_secret and dhchap_ctrl_secret as unknown parameters and jumps to out while host_secret and ctrl_secret are still NULL.

Removed the '#else' branches for the next version.

Quoted 32 lines

drivers/nvme/host/sysfs.c line 11

Quoted 11 lines
drivers/nvme/host/sysfs.c +2 -0
index a5c1511e49ac2..e73c3ad89adc0 100644
@@ -6,6 +6,8 @@
   */
 
  #include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
+#include <linux/key-type.h>
  #include <linux/blkdev.h>

[Severity: High]
Does including linux/nvme-keyring.h unconditionally in sysfs.c break the build when CONFIG_NVME_CORE is enabled and CONFIG_NVME_KEYRING is disabled?

In the !IS_ENABLED(CONFIG_NVME_KEYRING) block of include/linux/nvme-keyring.h (updated earlier in commit "nvme-auth: switch to use 'struct key'"), nvme_tls_psk_refresh() has a malformed parameter list and nvme_dhchap_psk_hash() is defined without static inline:

include/linux/nvme-keyring.h:
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
const char *hostnqn, const char *subnqn, *data, u8 hmac_id, u8 *data, size_t data_len, const char *digest)
...
u8 nvme_dhchap_psk_hash(struct key *key)
{
return 0;
}

Quoted 4 lines

Since sysfs.c is built for any CONFIG_NVME_CORE configuration and does not use any symbols from linux/nvme-keyring.h, compiling with CONFIG_NVME_KEYRING=n fails with syntax and multiple-definition errors.

Fixed wit the next version.

Quoted 23 lines
Quoted 1 line
drivers/nvme/host/sysfs.c +0 -0
@@ -830,36 +850,47 @@ static ssize_t dhchap_secret_store(struct device *dev,

[ ... ]

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+	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));

drivers/nvme/host/sysfs.c static ssize_t dhchap_secret_store(struct device *dev, · line 888

Quoted 10 lines
drivers/nvme/host/sysfs.c +10 -0
+		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);

[Severity: Low]
Should these dev_warn() and dev_dbg() calls in dhchap_secret_store() and dhchap_ctrl_secret_store() use ctrl->device instead of ctrl->dev?

Quoted 4 lines

ctrl->dev is the parent transport device (such as the shared /sys/class/nvme-fabrics/ctl device), whereas ctrl->device is the nvmeX controller device used by the rest of the function.

Fixed with the next version.

Quoted 8 lines
Quoted 8 lines
drivers/nvme/host/sysfs.c +2 -1
@@ -873,11 +904,28 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
  		struct device_attribute *attr, char *buf)
  {
  	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
-	struct nvmf_ctrl_options *opts = ctrl->opts;
+	struct key *key;
+	ssize_t count;
 
Quoted 17 lines

drivers/nvme/host/sysfs.c static ssize_t dhchap_ctrl_secret_show(struct device *dev, · line 918

Quoted 11 lines
drivers/nvme/host/sysfs.c +8 -2
-	if (!opts->dhchap_ctrl_secret)
+	mutex_lock(&ctrl->dhchap_auth_mutex);
+	key = ctrl->ctrl_key;
+	if (!key) {
+		mutex_unlock(&ctrl->dhchap_auth_mutex);
  		return sysfs_emit(buf, "none\n");
-	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
+	}
+	down_read(&key->sem);
+	if (key_validate(key))
+		count = sysfs_emit(buf, "<invalidated>");

[Severity: Low]
Is a trailing newline missing from "<invalidated>" in dhchap_ctrl_secret_show()? dhchap_secret_show(), dhchap_key_show(), and dhchap_ctrl_key_show() all emit "<invalidated>\n".

Fixed with the next version.

Quoted 18 lines

[ ... ]

Quoted 8 lines
drivers/nvme/host/sysfs.c +4 -0
@@ -925,6 +981,52 @@ 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)
+{

[ ... ]

Quoted 6 lines
drivers/nvme/host/sysfs.c +6 -0
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_key);
+
+static ssize_t dhchap_ctrl_key_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{

[ ... ]

Quoted 10 lines

drivers/nvme/host/sysfs.c

Quoted 2 lines
drivers/nvme/host/sysfs.c +2 -0
+}
+static DEVICE_ATTR_ADMIN_RO(dhchap_ctrl_key);

[Severity: Low]
This isn't a bug, but the new dhchap_key and dhchap_ctrl_key sysfs attributes are not mentioned in the commit message or documented in Documentation/ABI/stable/sysfs-nvme. Also, the commit message says the key serial is stored in struct nvmf_ctrl_options, whereas opts stores struct key pointers (dhchap_key and dhchap_ctrl_key).

Updated for the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#11 lore raw

PATCH v5 5/9 nvmet-auth: parse dhchap key from configfs attribute

In reply to 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.

---
 drivers/nvme/target/auth.c     | 181 +++++++++++++++++++++++++----------------
 drivers/nvme/target/configfs.c |  81 ++++++++++++++----
 drivers/nvme/target/nvmet.h    |   9 +-
 3 files changed, 180 insertions(+), 91 deletions(-)
drivers/nvme/target/auth.c +112 -69
index 26457ce4acc2a8664ec9c2a9ed6d66fed93b61fa..8b77164512c9cb2d50ee5cdd45152b15f425ca92 100644
@@ -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,76 @@ 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);
+		goto out_unlock;
 	}
-	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 +304,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;
 	}
drivers/nvme/target/configfs.c +65 -16

127 lines not on this page · raw message

drivers/nvme/target/nvmet.h +3 -6
index 1dfdac159dab044566aac47862665784feddec0a..e708c80e8426f426adf4e98defaa9e77e30d9a58 100644
@@ -399,12 +399,8 @@ static inline struct nvmet_subsys *namespaces_to_subsys(
 
 struct nvmet_host {
 	struct config_group	group;
-	char			*dhchap_secret;
-	char			*dhchap_ctrl_secret;
-	int			dhchap_key_offset;
-	int			dhchap_ctrl_key_offset;
-	u8			dhchap_key_hash;
-	u8			dhchap_ctrl_key_hash;
+	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);
Signature · 1 line
2.51.0
#12 lore raw

Re: [PATCH v5 5/9] nvmet-auth: parse dhchap key from configfs attribute

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key - [High] nvmet-auth…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [High] nvmet-auth: invalid DH-CHAP status and partial key update in nvmet_setup_auth()
- [High] nvmet-auth: shared key revocation in nvmet_auth_revoke_key() breaks active ctrls
- [High] nvmet-auth: uninitialized offset and OOB read in nvme_dhchap_psk_preparse()
- [Medium] nvmet-auth: missing DH-CHAP version and hash ID check in nvmet_auth_set_key()
--

commit 46da02dd0755f9ec506cfdb7b2885930c4ee4ffd
Author: Hannes Reinecke <[email protected]>

nvmet-auth: parse dhchap key from configfs attribute

This commit converts the nvmet configfs DH-HMAC-CHAP key interface to store keys as struct key pointers on struct nvmet_host rather than raw secret strings.

Quoted 21 lines
drivers/nvme/target/auth.c +13 -0
index 26457ce4acc2a..8b77164512c9c 100644
@@ -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;

drivers/nvme/target/auth.c line 44

Quoted 10 lines
drivers/nvme/target/auth.c +10 -0
+		}
+	}
+	if (key) {
+		pr_debug("%s: revoke %s key %08x\n",
+			 __func__, set_ctrl ? "ctrl" : "host",
+			 key_serial(key));
+		key_revoke(key);
+		key_put(key);
+	}
+}

[Severity: High]
Does revoking the key here break active controllers that still hold a reference to it?

nvmet_setup_auth() shares host->dhchap_key and host->dhchap_ctrl_key with connected controllers via key_get(). When a key is updated or cleared in configfs, calling key_revoke() here marks that shared key revoked and clears its payload, even though nvmet_host_dhchap_key_store() notes that existing authentication must stay valid until the host requests re-authentication. Because nvmet_execute_auth_send() only re-runs nvmet_setup_auth() on the admin queue, in-flight authentication or later I/O queue authentication on an existing controller continues using ctrl->host_key and fails in key_validate() with -EKEYREVOKED.

In addition, when host->dhchap_key is cleared to NULL, nvmet_setup_auth() returns early without dropping ctrl->host_key or ctrl->ctrl_key, leaving the controller pointing to the revoked key on re-authentication.

Quoted 20 lines
drivers/nvme/target/auth.c +3 -11
+
 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;

drivers/nvme/target/auth.c line 47

Quoted 10 lines
drivers/nvme/target/auth.c +1 -6
-			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;

[Severity: High]
Does removing the offset check here expose an uninitialized read and an out-of-bounds read in nvme_dhchap_psk_preparse()?

Commit 62dae895f986 ("nvmet-auth: store the DH-CHAP key offset instead of hardcoding it") initialized offset = 0 and used the parsed %n offset. Delegating parsing to nvme_auth_extract_key() -> nvme_dhchap_psk_preparse() loses both fixes:

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	int ret, offset;
...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%n",
		   &version, &hmac, &offset) != 2 || !offset) {
...
	datalen = strlen(prep->data + offset);
...
	ret = base64_decode(prep->data + 10, datalen, upayload->data,
			    true, BASE64_STD);
}

Because %n is not counted in the sscanf() return value, an input missing the trailing colon (such as "DHHC-1:1") returns 2 without writing offset, so strlen(prep->data + offset) uses an uninitialized stack variable. And when a single-digit HMAC ID is used (such as "DHHC-1:1:...", where offset is 9), base64_decode() hardcodes prep->data + 10 with datalen computed from offset 9, reading one byte past the NUL terminator.

Quoted 4 lines
drivers/nvme/target/auth.c +0 -4
-	if (key_hash > 3) {
-		pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
-			 key_hash);
-		return -EINVAL;

drivers/nvme/target/auth.c line 63

Quoted 10 lines
drivers/nvme/target/auth.c +6 -4
-	}
-	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: Medium]
Does switching to nvme_auth_extract_key() drop the DH-CHAP version and hash ID validation?

Previously, nvmet_auth_set_key() required the "DHHC-1:" prefix and rejected key_hash > 3 with -EINVAL. nvme_dhchap_psk_preparse() parses "DHHC-%01hhu:%02hhu:%n" into version and hmac without checking that version == 1 or hmac <= 3, allowing keys with unsupported versions or hash IDs to be stored via configfs and deferring failure until authentication time.

[ ... ]

Quoted 88 lines
drivers/nvme/target/auth.c +46 -27
@@ -186,44 +195,76 @@ 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_put(ctrl->host_key);
-	ctrl->host_key = host_key;
-	key_put(ctrl->ctrl_key);
-	ctrl->ctrl_key = ctrl_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);
+		goto out_unlock;
+	}
 	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);

drivers/nvme/target/auth.c u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset) · line 264

Quoted 10 lines
drivers/nvme/target/auth.c +10 -0
+		}
+		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;
+	}

[Severity: High]
Does nvmet_setup_auth() return a negative errno instead of a DH-HMAC-CHAP status code when key_validate() fails, and overwrite ctrl->host_key before host->dhchap_ctrl_key is validated?

nvmet_execute_auth_send() stores the u8 return value of nvmet_setup_auth() directly in req->sq->dhchap_status to send in the DH-HMAC-CHAP_Failure1 message. When key_validate() fails for either key, ret holds a negative errno (such as -EKEYREVOKED) instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, so (u8)ret is sent on the wire as the failure reason code. Conversely, if key_get() returns NULL, ret is still 0 and nvmet_setup_auth() reports success without installing the key.

Also, ctrl->host_key and ctrl->shash_id are replaced before host->dhchap_ctrl_key is validated, undoing commit 41ad4af45ac9 ("nvmet-auth: keep the old keys if the new ones fail") and leaving the controller with a mismatched host/controller key pair if controller key validation fails during re-authentication.

[ ... ]

Quoted 4 lines
drivers/nvme/target/configfs.c +0 -0
index ff3a8d74de599..12c093f4c8363 100644

[ ... ]

Quoted 8 lines
drivers/nvme/target/configfs.c +2 -0
@@ -2251,9 +2266,19 @@ static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
 		const char *page, size_t count)
 {
 	struct nvmet_host *host = to_host(item);
+	u8 *keydata = NULL;
+	size_t len;
 	int ret;
 

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_key_store(struct config_item *item, · line 2281

Quoted 10 lines
drivers/nvme/target/configfs.c +9 -1
-	ret = nvmet_auth_set_key(host, page, false);
+	len = strcspn(page, "\n");
+	if (len) {
+		keydata = kstrndup(page, len, GFP_KERNEL);
+		if (!keydata)
+			return -ENOMEM;
+	}
+
+	ret = nvmet_auth_set_key(host, keydata, false);
+	kfree_sensitive(keydata);

[Severity: High]
Does clearing dhchap_key or dhchap_ctrl_key via configfs trigger a NULL pointer dereference in nvmet_auth_set_key()?

When an empty string or a newline is written to nvmet_host_dhchap_key_store() or nvmet_host_dhchap_ctrl_key_store() to clear a key, len is 0 and keydata remains NULL. nvmet_auth_set_key() then evaluates if (!strlen(secret)) without checking if secret is NULL first.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer
#13 lore raw

Re: [PATCH v5 5/9] nvmet-auth: parse dhchap key from configfs attribute

Quoted 69 lines · On 11/23/26 1:29 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [High] nvmet-auth: invalid DH-CHAP status and partial key update in nvmet_setup_auth()
- [High] nvmet-auth: shared key revocation in nvmet_auth_revoke_key() breaks active ctrls
- [High] nvmet-auth: uninitialized offset and OOB read in nvme_dhchap_psk_preparse()
- [Medium] nvmet-auth: missing DH-CHAP version and hash ID check in nvmet_auth_set_key()
--

commit 46da02dd0755f9ec506cfdb7b2885930c4ee4ffd
Author: Hannes Reinecke <[email protected]>

nvmet-auth: parse dhchap key from configfs attribute

This commit converts the nvmet configfs DH-HMAC-CHAP key interface to store keys as struct key pointers on struct nvmet_host rather than raw secret strings.

Quoted 21 lines
drivers/nvme/target/auth.c +13 -0
index 26457ce4acc2a..8b77164512c9c 100644
@@ -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;

drivers/nvme/target/auth.c line 44

Quoted 10 lines
drivers/nvme/target/auth.c +10 -0
+		}
+	}
+	if (key) {
+		pr_debug("%s: revoke %s key %08x\n",
+			 __func__, set_ctrl ? "ctrl" : "host",
+			 key_serial(key));
+		key_revoke(key);
+		key_put(key);
+	}
+}

[Severity: High]
Does revoking the key here break active controllers that still hold a reference to it?

nvmet_setup_auth() shares host->dhchap_key and host->dhchap_ctrl_key with connected controllers via key_get(). When a key is updated or cleared in configfs, calling key_revoke() here marks that shared key revoked and clears its payload, even though nvmet_host_dhchap_key_store() notes that existing authentication must stay valid until the host requests re-authentication. Because nvmet_execute_auth_send() only re-runs nvmet_setup_auth() on the admin queue, in-flight authentication or later I/O queue authentication on an existing controller continues using ctrl->host_key and fails in key_validate() with -EKEYREVOKED.

Quoted 4 lines

In addition, when host->dhchap_key is cleared to NULL, nvmet_setup_auth() returns early without dropping ctrl->host_key or ctrl->ctrl_key, leaving the controller pointing to the revoked key on re-authentication.

This is okay. Once a queue is authenticated we cannot 'unset' the key
(and move the queue to unauthenticated) as this would break security
policies set during queue establishment.
The only way to clear a key for an authenticated queue is to disconnect
the queue (and controller) and do a full nvme connect without authentication.
Similarly, a revoked key will need to be used for re-authentication until the admin provides a valid key; we cannot disable or remove that key as this would disable authentication.

Quoted 35 lines
Quoted 20 lines
drivers/nvme/target/auth.c +3 -11
+
  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;

drivers/nvme/target/auth.c line 47

Quoted 10 lines
drivers/nvme/target/auth.c +1 -6
-			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;

[Severity: High]
Does removing the offset check here expose an uninitialized read and an out-of-bounds read in nvme_dhchap_psk_preparse()?

Quoted 5 lines

Commit 62dae895f986 ("nvmet-auth: store the DH-CHAP key offset instead of hardcoding it") initialized offset = 0 and used the parsed %n offset. Delegating parsing to nvme_auth_extract_key() -> nvme_dhchap_psk_preparse() loses both fixes:

Fixed with the next version.

Quoted 11 lines

[Severity: Medium]
Does switching to nvme_auth_extract_key() drop the DH-CHAP version and hash ID validation?

Previously, nvmet_auth_set_key() required the "DHHC-1:" prefix and rejected key_hash > 3 with -EINVAL. nvme_dhchap_psk_preparse() parses "DHHC-%01hhu:%02hhu:%n" into version and hmac without checking that version == 1 or hmac <= 3, allowing keys with unsupported versions or hash IDs to be stored via configfs and deferring failure until authentication time.

Fixed with the next version.

Quoted 114 lines

[ ... ]

Quoted 88 lines
drivers/nvme/target/auth.c +46 -27
@@ -186,44 +195,76 @@ 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_put(ctrl->host_key);
-	ctrl->host_key = host_key;
-	key_put(ctrl->ctrl_key);
-	ctrl->ctrl_key = ctrl_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);
+		goto out_unlock;
+	}
  	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);

drivers/nvme/target/auth.c u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset) · line 264

Quoted 10 lines
drivers/nvme/target/auth.c +10 -0
+		}
+		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;
+	}

[Severity: High]
Does nvmet_setup_auth() return a negative errno instead of a DH-HMAC-CHAP status code when key_validate() fails, and overwrite ctrl->host_key before host->dhchap_ctrl_key is validated?

nvmet_execute_auth_send() stores the u8 return value of nvmet_setup_auth() directly in req->sq->dhchap_status to send in the DH-HMAC-CHAP_Failure1 message. When key_validate() fails for either key, ret holds a negative errno (such as -EKEYREVOKED) instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, so (u8)ret is sent on the wire as the failure reason code. Conversely, if key_get() returns NULL, ret is still 0 and nvmet_setup_auth() reports success without installing the key.

Quoted 6 lines

Also, ctrl->host_key and ctrl->shash_id are replaced before host->dhchap_ctrl_key is validated, undoing commit 41ad4af45ac9 ("nvmet-auth: keep the old keys if the new ones fail") and leaving the controller with a mismatched host/controller key pair if controller key validation fails during re-authentication.

Fixed for the next version.

Quoted 32 lines

[ ... ]

Quoted 4 lines
drivers/nvme/target/configfs.c +0 -0
index ff3a8d74de599..12c093f4c8363 100644

[ ... ]

Quoted 8 lines
drivers/nvme/target/configfs.c +2 -0
@@ -2251,9 +2266,19 @@ static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
  		const char *page, size_t count)
  {
  	struct nvmet_host *host = to_host(item);
+	u8 *keydata = NULL;
+	size_t len;
  	int ret;
 

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_key_store(struct config_item *item, · line 2281

Quoted 10 lines
drivers/nvme/target/configfs.c +9 -1
-	ret = nvmet_auth_set_key(host, page, false);
+	len = strcspn(page, "\n");
+	if (len) {
+		keydata = kstrndup(page, len, GFP_KERNEL);
+		if (!keydata)
+			return -ENOMEM;
+	}
+
+	ret = nvmet_auth_set_key(host, keydata, false);
+	kfree_sensitive(keydata);

[Severity: High]
Does clearing dhchap_key or dhchap_ctrl_key via configfs trigger a NULL pointer dereference in nvmet_auth_set_key()?

Quoted 5 lines

When an empty string or a newline is written to nvmet_host_dhchap_key_store() or nvmet_host_dhchap_ctrl_key_store() to clear a key, len is 0 and keydata remains NULL. nvmet_auth_set_key() then evaluates if (!strlen(secret)) without checking if secret is NULL first.

Fixed with the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#14 lore raw

PATCH v5 6/9 nvme: allow to pass in key description as dhchap secret

In reply to 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.

---
 drivers/nvme/common/auth.c     | 24 ++++++++++++----
 drivers/nvme/host/auth.c       | 19 +++++++++++--
 drivers/nvme/host/fabrics.c    | 26 ++++++++++++++----
 drivers/nvme/host/fabrics.h    |  4 +++
 drivers/nvme/host/nvme.h       |  2 ++
 drivers/nvme/host/sysfs.c      | 62 ++++++++++++++++++++++++++++--------------
 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, 132 insertions(+), 44 deletions(-)
drivers/nvme/common/auth.c +18 -6
index ee26fb416ee6281ca9b101a5b00847736fd75b98..abdad41daa8fb62d8b5c656927a02f72c5403739 100644
@@ -144,20 +144,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
  * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
  *
  * @keyring: keyring to be used for the resulting 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);
 	}
drivers/nvme/host/auth.c +17 -2
index 535cc433dd35d305a40604f99627e4a248262f17..98913eaeb6d6884c61bcd8ccfb838dc246e9b9f2 100644
@@ -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);
 	}
 }
@@ -1051,6 +1061,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 {
 	struct nvme_dhchap_queue_context *chap;
 	struct key *host_key, *ctrl_key = NULL;
+	bool host_key_generated, ctrl_key_generated;
 	int i, ret;
 
 	mutex_init(&ctrl->dhchap_auth_mutex);
@@ -1080,6 +1091,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 		key_put(host_key);
 		return -ENOKEY;
 	}
+	host_key_generated = ctrl->opts->dhchap_key_generated;
 
 	if (ctrl->opts->dhchap_ctrl_key) {
 		ctrl_key = key_get(ctrl->opts->dhchap_ctrl_key);
@@ -1101,6 +1113,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 			key_put(host_key);
 			return -EKEYREVOKED;
 		}
+		ctrl_key_generated = ctrl->opts->dhchap_ctrl_key_generated;
 	}
 
 	dev_dbg(ctrl->device,
@@ -1109,6 +1122,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 	if (ctrl->host_key)
 		nvme_auth_clear_host_key(ctrl);
 	ctrl->host_key = host_key;
+	ctrl->host_key_generated = host_key_generated;
 
 	if (ctrl->ctrl_key)
 		nvme_auth_clear_ctrl_key(ctrl);
@@ -1117,6 +1131,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 			"%s: using dhchap ctrl key %08x\n",
 			__func__, key_serial(ctrl->ctrl_key));
 		ctrl->ctrl_key = ctrl_key;
+		ctrl->ctrl_key_generated = ctrl_key_generated;
 	}
 
 	ctrl->dhchap_ctxs = kvzalloc_objs(*chap, ctrl_max_dhchaps(ctrl));
drivers/nvme/host/fabrics.c +20 -6
index 0c09888be31f773491d4e8d2e474f8c27951340b..1277749c6e2e3012b5161a7d03589a6ced300a05 100644
@@ -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)
@@ -1090,7 +1092,8 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 #ifdef CONFIG_NVME_HOST_AUTH
 		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;
@@ -1111,7 +1114,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;
@@ -1339,15 +1343,25 @@ 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);
+			synchronize_rcu();
+		}
+		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_ctrl_key);
+			synchronize_rcu();
+		}
+		pr_debug("drop dhchap ctrl key %08x\n",
 			 key_serial(opts->dhchap_ctrl_key));
-		key_revoke(opts->dhchap_ctrl_key);
 		key_put(opts->dhchap_ctrl_key);
 	}
 	kfree(opts);
drivers/nvme/host/fabrics.h +4 -0
index 786f1b4819f076ac1f133070523be1780ec4d8b7..e55276356bdab4d8c738db329fee0c3a61afd510 100644
@@ -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;
drivers/nvme/host/nvme.h +2 -0
index 434e3612c996a969075aa5844c51906c605e6f56..1161f94cca19b1e223ebc4e2894c6e3ae7d6779a 100644
@@ -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;
 
drivers/nvme/host/sysfs.c +42 -20

150 lines not on this page · raw message

drivers/nvme/target/auth.c +20 -5
index 8b77164512c9cb2d50ee5cdd45152b15f425ca92..7204ace149565205241b7a85ab62389dafa980b6 100644
@@ -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;
 }
drivers/nvme/target/configfs.c +6 -4
index 12c093f4c8363927b5d41d0cc4445db50d8202b0..bc46d760aaa4a3316e6c62390cebf32092e5062f 100644
@@ -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);
 	}
drivers/nvme/target/nvmet.h +2 -0
index e708c80e8426f426adf4e98defaa9e77e30d9a58..c8757cedcd841742ff79c830335cfab6e9fc2401 100644
@@ -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;
 };
include/linux/nvme-auth.h +1 -1
index 75e1e75e298e1c3b408ea234705edd7abf788d1d..d5fbae8fc510b08579454b992cd4286bfb8c4f48 100644
@@ -32,7 +32,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,
Signature · 1 line
2.51.0
#15 lore raw

Re: [PATCH v5 6/9] nvme: allow to pass in key description as dhchap secret

Thank you for your contribution! Sashiko AI review found 10 potential issues to consider: - [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key - [High]…

Thank you for your contribution! Sashiko AI review found 10 potential issues to consider:
- [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [High] nvme-keyring: broken !CONFIG_NVME_KEYRING stubs in include/linux/nvme-keyring.h
- [Medium] nvme: broken key visibility check in nvme_dev_attrs_are_visible()
- [Medium] nvme-auth: self-revocation when updating key with active key description
- [Medium] nvmet: off-by-one PAGE_SIZE overflow via sprintf() in dhchap key configfs show
- [Medium] nvme-auth: unnecessary synchronize_rcu() stalls after key_revoke() under locks
- [Low] nvme-auth: mismatched key serial docs and prototype in nvme_auth_extract_key()
- [Low] nvme-auth: key_serial() logged before key assignment in nvme_auth_init_ctrl()
- [Low] nvme-sysfs: missing trailing newline on invalidated in dhchap_ctrl_secret_show()
- [Low] nvme-auth: nvme_auth_init_ctrl() logs key_serial() before assigning ctrl keys
--

commit 4ce0c96fc574d457ee6098926055522bf6b8f799
Author: Hannes Reinecke <[email protected]>

nvme: allow to pass in key description as dhchap secret

This commit updates nvme_auth_extract_key() to look up a pre-populated DH-HMAC-CHAP key in the keyring before falling back to generating a key from raw secret data, and tracks whether the key was auto-generated so only generated keys are revoked on teardown.

Quoted 22 lines
drivers/nvme/common/auth.c +7 -3
index ee26fb416ee62..abdad41daa8fb 100644
@@ -144,20 +144,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
  * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
  *
  * @keyring: keyring to be used for the resulting 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.
  */

drivers/nvme/common/auth.c EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len); · line 164

Quoted 10 lines
drivers/nvme/common/auth.c +4 -3
-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);

[Severity: Low]
This isn't a bug, but the commit message body, kernel-doc and inline comment refer to @input as a key serial number, whereas nvme_dhchap_psk_lookup() searches the keyring by key description (key->description).

Quoted 4 lines
drivers/nvme/host/auth.c +0 -0
index 535cc433dd35d..98913eaeb6d68 100644

[ ... ]

drivers/nvme/host/auth.c int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl) · line 1125

Quoted 5 lines
drivers/nvme/host/auth.c +1 -0
@@ -1109,6 +1122,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 	if (ctrl->host_key)
 		nvme_auth_clear_host_key(ctrl);
 	ctrl->host_key = host_key;
+	ctrl->host_key_generated = host_key_generated;

[Severity: Low]
Just above this in nvme_auth_init_ctrl(), dev_dbg() logs key_serial(ctrl->host_key) before ctrl->host_key = host_key is assigned:

drivers/nvme/host/auth.c:nvme_auth_init_ctrl() {
	...
	dev_dbg(ctrl->device,
		"%s: using dhchap host key %08x\n",
		__func__, key_serial(ctrl->host_key));
	if (ctrl->host_key)
		nvme_auth_clear_host_key(ctrl);
	ctrl->host_key = host_key;
	...
}

Should that log key_serial(host_key) instead so it does not print 00000000 (or the previous key's serial on reset)?

drivers/nvme/host/auth.c int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl) · line 1135

Quoted 8 lines
drivers/nvme/host/auth.c +1 -0
 	if (ctrl->ctrl_key)
 		nvme_auth_clear_ctrl_key(ctrl);
@@ -1117,6 +1131,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
 			"%s: using dhchap ctrl key %08x\n",
 			__func__, key_serial(ctrl->ctrl_key));
 		ctrl->ctrl_key = ctrl_key;
+		ctrl->ctrl_key_generated = ctrl_key_generated;
 	}

[Severity: Low]
Similarly, nvme_auth_clear_ctrl_key(ctrl) clears ctrl->ctrl_key to NULL right before dev_dbg() logs key_serial(ctrl->ctrl_key), so this always prints 00000000 instead of key_serial(ctrl_key).

Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 0c09888be31f7..1277749c6e2e3 100644

[ ... ]

Quoted 20 lines
drivers/nvme/host/fabrics.c +9 -3
@@ -1339,15 +1343,25 @@ 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);
+			synchronize_rcu();
+		}
+		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",

drivers/nvme/host/fabrics.c void nvmf_free_options(struct nvmf_ctrl_options *opts) · line 1367

Quoted 10 lines
drivers/nvme/host/fabrics.c +5 -1
+				 key_serial(opts->dhchap_ctrl_key));
+			key_revoke(opts->dhchap_ctrl_key);
+			synchronize_rcu();
+		}
+		pr_debug("drop dhchap ctrl key %08x\n",
 			 key_serial(opts->dhchap_ctrl_key));
-		key_revoke(opts->dhchap_ctrl_key);
 		key_put(opts->dhchap_ctrl_key);
 	}
 	kfree(opts);

[Severity: Medium]
Why call synchronize_rcu() after key_revoke() here, in dhchap_secret_store(), dhchap_ctrl_secret_store() and nvmet_auth_revoke_key()?

key_revoke() sets KEY_FLAG_REVOKED under key->sem and schedules payload destruction via call_rcu(), while all DH-HMAC-CHAP key readers serialize on key->sem and key_validate(). Waiting synchronously for an RCU grace period stalls callers unnecessarily, including while holding ctrl->dhchap_auth_mutex in dhchap_[ctrl_]secret_store() and while holding down_write(&nvmet_config_sem) in nvmet_auth_set_key(), which blocks all concurrent target connections and configfs operations.

Quoted 4 lines
drivers/nvme/host/sysfs.c +0 -0
index e73c3ad89adc0..e6d42da84e168 100644

[ ... ]

Quoted 20 lines
drivers/nvme/host/sysfs.c +5 -5
@@ -875,19 +878,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);

drivers/nvme/host/sysfs.c static ssize_t dhchap_secret_store(struct device *dev, · line 900

Quoted 10 lines
drivers/nvme/host/sysfs.c +7 -1
-
+	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);

[Severity: Medium]
Can this revoke the newly selected key when the active key was auto-generated?

An auto-generated key is linked into the keyring with a UUID description. If a user writes that active key's description to dhchap_secret_store() (or dhchap_ctrl_secret_store(), or nvmet_auth_set_key() on the target), nvme_auth_extract_key() finds it in the keyring before the old key is revoked and returns key == old_key with generated = false. Because ctrl->host_key_generated is still true, key_revoke(old_key) then revokes key itself, causing subsequent authentication to fail with -EKEYREVOKED. Should these paths check old_key != key before revoking old_key?

drivers/nvme/host/sysfs.c static ssize_t dhchap_ctrl_secret_show(struct device *dev, · line 928

Quoted 8 lines
drivers/nvme/host/sysfs.c +1 -0
@@ -913,16 +922,18 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
 		mutex_unlock(&ctrl->dhchap_auth_mutex);
 		return sysfs_emit(buf, "none\n");
 	}
+
 	down_read(&key->sem);
 	if (key_validate(key))
 		count = sysfs_emit(buf, "<invalidated>");

[Severity: Low]
Should "<invalidated>" have a trailing newline here to match dhchap_secret_show() and the other branches of dhchap_ctrl_secret_show()?

[ ... ]

drivers/nvme/host/sysfs.c static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, · line 1119

Quoted 13 lines
drivers/nvme/host/sysfs.c +4 -2
@@ -1090,9 +1110,11 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
 		return 0;
 	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
 		return 0;
-	if (a == &dev_attr_dhchap_key.attr && !ctrl->opts)
+	if (a == &dev_attr_dhchap_key.attr && !ctrl->opts &&
+	    !ctrl->host_key_generated)
 		return 0;
-	if (a == &dev_attr_dhchap_ctrl_key.attr && !ctrl->opts)
+	if (a == &dev_attr_dhchap_ctrl_key.attr && !ctrl->opts &&
+	    !ctrl->ctrl_key_generated)
 		return 0;
 #endif

[Severity: Medium]
Should the visibility conditions for dev_attr_dhchap_key.attr and dev_attr_dhchap_ctrl_key.attr use || instead of &&?

On Fabrics controllers ctrl->opts is non-NULL, so !ctrl->opts is false and the !ctrl->host_key_generated / !ctrl->ctrl_key_generated checks are never reached. In addition, because sysfs group visibility is only evaluated at device creation unless sysfs_update_group() is called, changing ctrl->host_key_generated or ctrl->ctrl_key_generated later in dhchap_secret_store() or dhchap_ctrl_secret_store() will not update the visibility of these attributes.

Quoted 4 lines
drivers/nvme/target/auth.c +0 -0
index 8b77164512c9c..7204ace149565 100644

[ ... ]

Quoted 9 lines
drivers/nvme/target/auth.c +1 -0
@@ -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;
 

drivers/nvme/target/auth.c int nvmet_auth_set_key(struct nvmet_host *host, const char *secret, · line 71

Quoted 5 lines
drivers/nvme/target/auth.c +1 -1
@@ -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);

[Severity: High]
Does clearing a host or controller key via configfs trigger a NULL pointer dereference in nvmet_auth_set_key()?

In nvmet_host_dhchap_key_store() and nvmet_host_dhchap_ctrl_key_store(), writing an empty string or newline leaves keydata as NULL:

drivers/nvme/target/configfs.c:nvmet_host_dhchap_key_store() {
	...
	len = strcspn(page, "\n");
	if (len) {
		keydata = kstrndup(page, len, GFP_KERNEL);
		if (!keydata)
			return -ENOMEM;
	}

	ret = nvmet_auth_set_key(host, keydata, false);
	...
}

nvmet_auth_set_key() then calls strlen(secret) at the start of the function without checking if (!secret), dereferencing NULL.

Quoted 28 lines
drivers/nvme/target/configfs.c +4 -3
index 12c093f4c8363..bc46d760aaa4a 100644
@@ -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);

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item, · line 2319

Quoted 10 lines
drivers/nvme/target/configfs.c +2 -1
 			if (ret > 0) {
 				page[ret] = '\n';
 				ret++;
 			}
-		}
+		} else
+			ret = sprintf(page, "%s\n", key->description);
 		up_read(&key->sem);
 		key_put(key);
 	}

[Severity: Medium]
Can sprintf(page, "%s\n", key->description) in nvmet_host_dhchap_key_show() and nvmet_host_dhchap_ctrl_key_show() overflow the PAGE_SIZE configfs buffer by 1 byte?

add_key() permits key->description up to 4095 characters (KEY_MAX_DESC_SIZE is 4096 including the NUL terminator), so appending "\n" and the terminating NUL writes 4097 bytes into the 4096-byte page buffer. Using scnprintf(page, PAGE_SIZE, "%s\n", key->description) would avoid writing past the end of page.

include/linux/nvme-auth.h void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data, · line 35

Quoted 10 lines
include/linux/nvme-auth.h +1 -1
index 75e1e75e298e1..d5fbae8fc510b 100644
@@ -32,7 +32,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);

[Severity: Low]
The commit message notes that the secret parameter of nvme_auth_extract_key() was renamed to input, but this declaration in include/linux/nvme-auth.h still uses secret and secret_len.

[Severity: High]
Also in the keyring headers included by drivers/nvme/host/sysfs.c, drivers/nvme/host/fabrics.c and drivers/nvme/target/configfs.c, do the !CONFIG_NVME_KEYRING stubs in include/linux/nvme-keyring.h break the build when CONFIG_NVME_KEYRING is disabled?

include/linux/nvme-keyring.h {
	...
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
		const char *hostnqn, const char *subnqn, *data, u8 hmac_id,
		u8 *data, size_t data_len, const char *digest)
{
	return ERR_PTR(-ENOTSUPP);
}
	...
u8 nvme_dhchap_psk_hash(struct key *key)
{
	return 0;
}

}

nvme_tls_psk_refresh() has a stray untyped *data parameter before u8 hmac_id, and nvme_dhchap_psk_hash() is missing static inline, causing compile and multiple-definition link errors whenever CONFIG_NVME_KEYRING=n.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
maintainer
#16 lore raw

Re: [PATCH v5 6/9] nvme: allow to pass in key description as dhchap secret

Quoted 63 lines · On 11/23/26 1:36 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 10 potential issues to consider:
- [High] nvmet-auth: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [High] nvme-keyring: broken !CONFIG_NVME_KEYRING stubs in include/linux/nvme-keyring.h
- [Medium] nvme: broken key visibility check in nvme_dev_attrs_are_visible()
- [Medium] nvme-auth: self-revocation when updating key with active key description
- [Medium] nvmet: off-by-one PAGE_SIZE overflow via sprintf() in dhchap key configfs show
- [Medium] nvme-auth: unnecessary synchronize_rcu() stalls after key_revoke() under locks
- [Low] nvme-auth: mismatched key serial docs and prototype in nvme_auth_extract_key()
- [Low] nvme-auth: key_serial() logged before key assignment in nvme_auth_init_ctrl()
- [Low] nvme-sysfs: missing trailing newline on invalidated in dhchap_ctrl_secret_show()
- [Low] nvme-auth: nvme_auth_init_ctrl() logs key_serial() before assigning ctrl keys
--

commit 4ce0c96fc574d457ee6098926055522bf6b8f799
Author: Hannes Reinecke <[email protected]>

nvme: allow to pass in key description as dhchap secret

This commit updates nvme_auth_extract_key() to look up a pre-populated DH-HMAC-CHAP key in the keyring before falling back to generating a key from raw secret data, and tracks whether the key was auto-generated so only generated keys are revoked on teardown.

Quoted 29 lines
drivers/nvme/common/auth.c +9 -5
index ee26fb416ee62..abdad41daa8fb 100644
@@ -144,20 +144,32 @@ EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
   * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
   *
   * @keyring: keyring to be used for the resulting 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;
 
Quoted 10 lines

drivers/nvme/common/auth.c EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len); · line 164

Quoted 3 lines
drivers/nvme/common/auth.c +2 -1
-	key = nvme_dhchap_psk_create(keyring, secret, secret_len);
+	/* Check if @input is a key serial number */
+	key = nvme_dhchap_psk_lookup(keyring, input);

[Severity: Low]
This isn't a bug, but the commit message body, kernel-doc and inline comment refer to @input as a key serial number, whereas nvme_dhchap_psk_lookup() searches the keyring by key description (key->description).

Yes, that is why I put the comment in to indicate that we first try to check if 'input' is a key serial number.

Quoted 29 lines
Quoted 4 lines
drivers/nvme/host/auth.c +0 -0
index 535cc433dd35d..98913eaeb6d68 100644

[ ... ]

drivers/nvme/host/auth.c int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl) · line 1125

Quoted 5 lines
drivers/nvme/host/auth.c +1 -0
@@ -1109,6 +1122,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
  	if (ctrl->host_key)
  		nvme_auth_clear_host_key(ctrl);
  	ctrl->host_key = host_key;
+	ctrl->host_key_generated = host_key_generated;

[Severity: Low]
Just above this in nvme_auth_init_ctrl(), dev_dbg() logs key_serial(ctrl->host_key) before ctrl->host_key = host_key is assigned:

drivers/nvme/host/auth.c:nvme_auth_init_ctrl() {
	...
	dev_dbg(ctrl->device,
		"%s: using dhchap host key %08x\n",
		__func__, key_serial(ctrl->host_key));
	if (ctrl->host_key)
		nvme_auth_clear_host_key(ctrl);
	ctrl->host_key = host_key;
	...
}

Should that log key_serial(host_key) instead so it does not print 00000000 (or the previous key's serial on reset)?

Quoted 14 lines

drivers/nvme/host/auth.c int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl) · line 1135

Quoted 8 lines
drivers/nvme/host/auth.c +1 -0
  	if (ctrl->ctrl_key)
  		nvme_auth_clear_ctrl_key(ctrl);
@@ -1117,6 +1131,7 @@ int nvme_auth_init_ctrl(struct nvme_ctrl *ctrl)
  			"%s: using dhchap ctrl key %08x\n",
  			__func__, key_serial(ctrl->ctrl_key));
  		ctrl->ctrl_key = ctrl_key;
+		ctrl->ctrl_key_generated = ctrl_key_generated;
  	}

[Severity: Low]
Similarly, nvme_auth_clear_ctrl_key(ctrl) clears ctrl->ctrl_key to NULL right before dev_dbg() logs key_serial(ctrl->ctrl_key), so this always prints 00000000 instead of key_serial(ctrl_key).

Fixed with the next version.

Quoted 41 lines
Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 0c09888be31f7..1277749c6e2e3 100644

[ ... ]

Quoted 20 lines
drivers/nvme/host/fabrics.c +9 -3
@@ -1339,15 +1343,25 @@ 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);
+			synchronize_rcu();
+		}
+		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",

drivers/nvme/host/fabrics.c void nvmf_free_options(struct nvmf_ctrl_options *opts) · line 1367

Quoted 10 lines
drivers/nvme/host/fabrics.c +5 -1
+				 key_serial(opts->dhchap_ctrl_key));
+			key_revoke(opts->dhchap_ctrl_key);
+			synchronize_rcu();
+		}
+		pr_debug("drop dhchap ctrl key %08x\n",
  			 key_serial(opts->dhchap_ctrl_key));
-		key_revoke(opts->dhchap_ctrl_key);
  		key_put(opts->dhchap_ctrl_key);
  	}
  	kfree(opts);

[Severity: Medium]
Why call synchronize_rcu() after key_revoke() here, in dhchap_secret_store(), dhchap_ctrl_secret_store() and nvmet_auth_revoke_key()?

Quoted 8 lines

key_revoke() sets KEY_FLAG_REVOKED under key->sem and schedules payload destruction via call_rcu(), while all DH-HMAC-CHAP key readers serialize on key->sem and key_validate(). Waiting synchronously for an RCU grace period stalls callers unnecessarily, including while holding ctrl->dhchap_auth_mutex in dhchap_[ctrl_]secret_store() and while holding down_write(&nvmet_config_sem) in nvmet_auth_set_key(), which blocks all concurrent target connections and configfs operations.

Okay, dropped the call to synchronize_rcu() for the next version.

Quoted 40 lines
Quoted 4 lines
drivers/nvme/host/sysfs.c +0 -0
index e73c3ad89adc0..e6d42da84e168 100644

[ ... ]

Quoted 20 lines
drivers/nvme/host/sysfs.c +5 -5
@@ -875,19 +878,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);

drivers/nvme/host/sysfs.c static ssize_t dhchap_secret_store(struct device *dev, · line 900

Quoted 10 lines
drivers/nvme/host/sysfs.c +7 -1
-
+	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);

[Severity: Medium]
Can this revoke the newly selected key when the active key was auto-generated?

Quoted 9 lines

An auto-generated key is linked into the keyring with a UUID description. If a user writes that active key's description to dhchap_secret_store() (or dhchap_ctrl_secret_store(), or nvmet_auth_set_key() on the target), nvme_auth_extract_key() finds it in the keyring before the old key is revoked and returns key == old_key with generated = false. Because ctrl->host_key_generated is still true, key_revoke(old_key) then revokes key itself, causing subsequent authentication to fail with -EKEYREVOKED. Should these paths check old_key != key before revoking old_key?

Fixed with the next version.

Quoted 4 lines
Quoted 4 lines
drivers/nvme/host/sysfs.c +0 -0
@@ -913,16 +922,18 @@ static ssize_t dhchap_ctrl_secret_show(struct device *dev,
  		mutex_unlock(&ctrl->dhchap_auth_mutex);
  		return sysfs_emit(buf, "none\n");
  	}
Quoted 9 lines

drivers/nvme/host/sysfs.c static ssize_t dhchap_ctrl_secret_show(struct device *dev, · line 928

Quoted 4 lines
drivers/nvme/host/sysfs.c +1 -0
+
  	down_read(&key->sem);
  	if (key_validate(key))
  		count = sysfs_emit(buf, "<invalidated>");

[Severity: Low]
Should "<invalidated>" have a trailing newline here to match dhchap_secret_show() and the other branches of dhchap_ctrl_secret_show()?

Fixed with the next version.

Quoted 20 lines

[ ... ]

drivers/nvme/host/sysfs.c static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, · line 1119

Quoted 13 lines
drivers/nvme/host/sysfs.c +4 -2
@@ -1090,9 +1110,11 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
  		return 0;
  	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
  		return 0;
-	if (a == &dev_attr_dhchap_key.attr && !ctrl->opts)
+	if (a == &dev_attr_dhchap_key.attr && !ctrl->opts &&
+	    !ctrl->host_key_generated)
  		return 0;
-	if (a == &dev_attr_dhchap_ctrl_key.attr && !ctrl->opts)
+	if (a == &dev_attr_dhchap_ctrl_key.attr && !ctrl->opts &&
+	    !ctrl->ctrl_key_generated)
  		return 0;
  #endif

[Severity: Medium]
Should the visibility conditions for dev_attr_dhchap_key.attr and dev_attr_dhchap_ctrl_key.attr use || instead of &&?

Quoted 8 lines

On Fabrics controllers ctrl->opts is non-NULL, so !ctrl->opts is false and the !ctrl->host_key_generated / !ctrl->ctrl_key_generated checks are never reached. In addition, because sysfs group visibility is only evaluated at device creation unless sysfs_update_group() is called, changing ctrl->host_key_generated or ctrl->ctrl_key_generated later in dhchap_secret_store() or dhchap_ctrl_secret_store() will not update the visibility of these attributes.

Dropped from the next version.

Quoted 40 lines
Quoted 4 lines
drivers/nvme/target/auth.c +0 -0
index 8b77164512c9c..7204ace149565 100644

[ ... ]

Quoted 9 lines
drivers/nvme/target/auth.c +1 -0
@@ -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;
 

drivers/nvme/target/auth.c int nvmet_auth_set_key(struct nvmet_host *host, const char *secret, · line 71

Quoted 5 lines
drivers/nvme/target/auth.c +1 -1
@@ -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);

[Severity: High]
Does clearing a host or controller key via configfs trigger a NULL pointer dereference in nvmet_auth_set_key()?

In nvmet_host_dhchap_key_store() and nvmet_host_dhchap_ctrl_key_store(), writing an empty string or newline leaves keydata as NULL:

drivers/nvme/target/configfs.c:nvmet_host_dhchap_key_store() {
	...
	len = strcspn(page, "\n");
	if (len) {
		keydata = kstrndup(page, len, GFP_KERNEL);
		if (!keydata)
			return -ENOMEM;
	}

	ret = nvmet_auth_set_key(host, keydata, false);
	...
}
Quoted 3 lines

nvmet_auth_set_key() then calls strlen(secret) at the start of the function without checking if (!secret), dereferencing NULL.

Fixed with the next version.

Quoted 44 lines
Quoted 28 lines
drivers/nvme/target/configfs.c +4 -3
index 12c093f4c8363..bc46d760aaa4a 100644
@@ -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);

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item, · line 2319

Quoted 10 lines
drivers/nvme/target/configfs.c +2 -1
  			if (ret > 0) {
  				page[ret] = '\n';
  				ret++;
  			}
-		}
+		} else
+			ret = sprintf(page, "%s\n", key->description);
  		up_read(&key->sem);
  		key_put(key);
  	}

[Severity: Medium]
Can sprintf(page, "%s\n", key->description) in nvmet_host_dhchap_key_show() and nvmet_host_dhchap_ctrl_key_show() overflow the PAGE_SIZE configfs buffer by 1 byte?

Quoted 6 lines

add_key() permits key->description up to 4095 characters (KEY_MAX_DESC_SIZE is 4096 including the NUL terminator), so appending "\n" and the terminating NUL writes 4097 bytes into the 4096-byte page buffer. Using scnprintf(page, PAGE_SIZE, "%s\n", key->description) would avoid writing past the end of page.

Fixed with the next version.

Quoted 7 lines
Quoted 7 lines
include/linux/nvme-auth.h +0 -0
index 75e1e75e298e1..d5fbae8fc510b 100644
@@ -32,7 +32,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);
 
Quoted 9 lines

include/linux/nvme-auth.h void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data, · line 35

Quoted 3 lines
include/linux/nvme-auth.h +1 -1
  struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
-				  size_t secret_len);
+				  size_t secret_len, bool *generated);

[Severity: Low]
The commit message notes that the secret parameter of nvme_auth_extract_key() was renamed to input, but this declaration in include/linux/nvme-auth.h still uses secret and secret_len.

Fixed with the next version.

Quoted 19 lines

[Severity: High]
Also in the keyring headers included by drivers/nvme/host/sysfs.c, drivers/nvme/host/fabrics.c and drivers/nvme/target/configfs.c, do the !CONFIG_NVME_KEYRING stubs in include/linux/nvme-keyring.h break the build when CONFIG_NVME_KEYRING is disabled?

include/linux/nvme-keyring.h {
	...
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
		const char *hostnqn, const char *subnqn, *data, u8 hmac_id,
		u8 *data, size_t data_len, const char *digest)
{
	return ERR_PTR(-ENOTSUPP);
}
	...
u8 nvme_dhchap_psk_hash(struct key *key)
{
	return 0;
}
Quoted 6 lines

}

nvme_tls_psk_refresh() has a stray untyped *data parameter before u8 hmac_id, and nvme_dhchap_psk_hash() is missing static inline, causing compile and multiple-definition link errors whenever CONFIG_NVME_KEYRING=n.

Fixed with the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#17 lore raw

PATCH v5 7/9 nvme-auth: wait for authentication to finish when changing keys

In reply to 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.

---
 drivers/nvme/host/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/sysfs.c +2 -2
index e6d42da84e1688110eb81e59161dc7c497bd0627..33f78914e8b5e6331e3a82f6bafa3c0bdc0b8ea6 100644
@@ -903,7 +903,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;
 }
 
@@ -995,7 +995,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;
 }
 
Signature · 1 line
2.51.0
#18 lore raw

PATCH v5 8/9 nvme-fabrics: allow to pass in keyring by name

In reply to 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.

---
 drivers/nvme/host/fabrics.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
drivers/nvme/host/fabrics.c +25 -8
index 1277749c6e2e3012b5161a7d03589a6ced300a05..5dc1e1d5fb4629b82b2bce4d37fe9ce5d17acab6 100644
@@ -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);
@@ -697,7 +698,7 @@ static const match_table_t opt_tokens = {
 	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
 	{ NVMF_OPT_TOS,			"tos=%d"		},
 #ifdef CONFIG_NVME_TCP_TLS
-	{ NVMF_OPT_KEYRING,		"keyring=%d"		},
+	{ NVMF_OPT_KEYRING,		"keyring=%s"		},
 	{ NVMF_OPT_TLS_KEY,		"tls_key=%d"		},
 #endif
 	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},
@@ -992,17 +993,33 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			opts->tos = token;
 			break;
 		case NVMF_OPT_KEYRING:
+#ifdef CONFIG_NVME_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;
+#else
+			pr_warn("nvme keyring is disabled\n");
+			ret = -EINVAL;
+			goto out;
+#endif
 			break;
 		case NVMF_OPT_TLS_KEY:
 			if (match_int(args, &key_id) || key_id <= 0) {
Signature · 1 line
2.51.0
#19 lore raw

Re: [PATCH v5 8/9] nvme-fabrics: allow to pass in keyring by name

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] nvme-fabrics: keyring= fails in nvmf_parse_options() for CONFIG_NVME_KEYRING=m - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] nvme-fabrics: keyring= fails in nvmf_parse_options() for CONFIG_NVME_KEYRING=m
- [Medium] nvme-fabrics: match_int() in nvmf_parse_options() misparses keyring names
--

commit 357df670b0cbbd8ea5cc07d96b4413e6b84ca631
Author: Hannes Reinecke <[email protected]>

nvme-fabrics: allow to pass in keyring by name

This commit updates the NVMe-oF option parser to accept a keyring name in addition to a numeric key serial ID for the keyring= option.

Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 1277749c6e2e3..5dc1e1d5fb462 100644

[ ... ]

Quoted 10 lines
drivers/nvme/host/fabrics.c +1 -1
@@ -697,7 +698,7 @@ static const match_table_t opt_tokens = {
 	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
 	{ NVMF_OPT_TOS,			"tos=%d"		},
 #ifdef CONFIG_NVME_TCP_TLS
-	{ NVMF_OPT_KEYRING,		"keyring=%d"		},
+	{ NVMF_OPT_KEYRING,		"keyring=%s"		},
 	{ NVMF_OPT_TLS_KEY,		"tls_key=%d"		},
 #endif
 	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 997

Quoted 5 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -992,17 +993,33 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			opts->tos = token;
 			break;
 		case NVMF_OPT_KEYRING:
+#ifdef CONFIG_NVME_KEYRING
 			if (match_int(args, &key_id) || key_id <= 0) {

[Severity: Medium]
Can match_int() misparse a keyring name that starts with a positive number (such as "1nvme") as a numeric key serial ID?

Because opt_tokens now matches keyring=%s instead of %d, args[0] spans the entire string without requiring all characters to be digits. match_int() -> match_number() calls simple_strtol(buf, &endp, 0) and only checks endp == buf, ignoring trailing non-numeric characters on strings under 24 bytes. A keyring name with a leading positive integer will therefore take the else branch and pass that prefix to nvmf_parse_key(key_id) instead of looking up the keyring by name via request_key().

Quoted 22 lines
drivers/nvme/host/fabrics.c +15 -7
-				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);

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 1022

Quoted 10 lines
drivers/nvme/host/fabrics.c +7 -0
+					goto out;
+				}
 			}
 			key_put(opts->keyring);
 			opts->keyring = key;
+#else
+			pr_warn("nvme keyring is disabled\n");
+			ret = -EINVAL;
+			goto out;
+#endif

[Severity: High]
Does #ifdef CONFIG_NVME_KEYRING reject all keyring= options with -EINVAL when CONFIG_NVME_KEYRING=m?

NVME_KEYRING is a tristate Kconfig symbol, so modular builds define CONFIG_NVME_KEYRING_MODULE rather than CONFIG_NVME_KEYRING, causing nvmf_parse_options() to compile the #else branch and fail controller creation. Should this use #if IS_ENABLED(CONFIG_NVME_KEYRING) (like include/linux/nvme-keyring.h)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
maintainer
#20 lore raw

Re: [PATCH v5 8/9] nvme-fabrics: allow to pass in keyring by name

Quoted 43 lines · On 11/23/26 1:29 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] nvme-fabrics: keyring= fails in nvmf_parse_options() for CONFIG_NVME_KEYRING=m
- [Medium] nvme-fabrics: match_int() in nvmf_parse_options() misparses keyring names
--

commit 357df670b0cbbd8ea5cc07d96b4413e6b84ca631
Author: Hannes Reinecke <[email protected]>

nvme-fabrics: allow to pass in keyring by name

This commit updates the NVMe-oF option parser to accept a keyring name in addition to a numeric key serial ID for the keyring= option.

Quoted 4 lines
drivers/nvme/host/fabrics.c +0 -0
index 1277749c6e2e3..5dc1e1d5fb462 100644

[ ... ]

Quoted 10 lines
drivers/nvme/host/fabrics.c +1 -1
@@ -697,7 +698,7 @@ static const match_table_t opt_tokens = {
  	{ NVMF_OPT_NR_POLL_QUEUES,	"nr_poll_queues=%d"	},
  	{ NVMF_OPT_TOS,			"tos=%d"		},
  #ifdef CONFIG_NVME_TCP_TLS
-	{ NVMF_OPT_KEYRING,		"keyring=%d"		},
+	{ NVMF_OPT_KEYRING,		"keyring=%s"		},
  	{ NVMF_OPT_TLS_KEY,		"tls_key=%d"		},
  #endif
  	{ NVMF_OPT_FAIL_FAST_TMO,	"fast_io_fail_tmo=%d"	},

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 997

Quoted 5 lines
drivers/nvme/host/fabrics.c +1 -0
@@ -992,17 +993,33 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
  			opts->tos = token;
  			break;
  		case NVMF_OPT_KEYRING:
+#ifdef CONFIG_NVME_KEYRING
  			if (match_int(args, &key_id) || key_id <= 0) {

[Severity: Medium]
Can match_int() misparse a keyring name that starts with a positive number (such as "1nvme") as a numeric key serial ID?

Quoted 9 lines

Because opt_tokens now matches keyring=%s instead of %d, args[0] spans the entire string without requiring all characters to be digits. match_int() -> match_number() calls simple_strtol(buf, &endp, 0) and only checks endp == buf, ignoring trailing non-numeric characters on strings under 24 bytes. A keyring name with a leading positive integer will therefore take the else branch and pass that prefix to nvmf_parse_key(key_id) instead of looking up the keyring by name via request_key().

Yes, it can. but this will be a user error.

Quoted 37 lines
Quoted 22 lines
drivers/nvme/host/fabrics.c +15 -7
-				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);

drivers/nvme/host/fabrics.c static int nvmf_parse_options(struct nvmf_ctrl_options *opts, · line 1022

Quoted 10 lines
drivers/nvme/host/fabrics.c +7 -0
+					goto out;
+				}
  			}
  			key_put(opts->keyring);
  			opts->keyring = key;
+#else
+			pr_warn("nvme keyring is disabled\n");
+			ret = -EINVAL;
+			goto out;
+#endif

[Severity: High]
Does #ifdef CONFIG_NVME_KEYRING reject all keyring= options with -EINVAL when CONFIG_NVME_KEYRING=m?

Quoted 6 lines

NVME_KEYRING is a tristate Kconfig symbol, so modular builds define CONFIG_NVME_KEYRING_MODULE rather than CONFIG_NVME_KEYRING, causing nvmf_parse_options() to compile the #else branch and fail controller creation. Should this use #if IS_ENABLED(CONFIG_NVME_KEYRING) (like include/linux/nvme-keyring.h)?

Fixed with the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#21 lore raw

PATCH v5 9/9 nvmet: add configfs attribute 'dhchap_keyring'

In reply to 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.

---
 drivers/nvme/target/auth.c     |  4 ++-
 drivers/nvme/target/configfs.c | 63 ++++++++++++++++++++++++++++++++++++++++++
 drivers/nvme/target/nvmet.h    |  1 +
 3 files changed, 67 insertions(+), 1 deletion(-)
drivers/nvme/target/auth.c +3 -1
index 7204ace149565205241b7a85ab62389dafa980b6..84091803af300f840aa00c593eb41c1874450183 100644
@@ -68,7 +68,9 @@ 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);
+	down_read(&nvmet_config_sem);
+	key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
+	up_read(&nvmet_config_sem);
 	if (IS_ERR(key)) {
 		pr_debug("%s: invalid key specification\n", __func__);
 		return PTR_ERR(key);
drivers/nvme/target/configfs.c +63 -0

87 lines not on this page · raw message

drivers/nvme/target/nvmet.h +1 -0
index c8757cedcd841742ff79c830335cfab6e9fc2401..0f76633c6aa65cec26516bd7f0c55dea815a57f5 100644
@@ -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;
 };
Signature · 1 line
2.51.0
#22 lore raw

Re: [PATCH v5 9/9] nvmet: add configfs attribute 'dhchap_keyring'

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] nvmet: NULL pointer dereference in nvmet_auth_set_key() when clearing key - [Medium] nvmet: cannot…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] nvmet: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [Medium] nvmet: cannot write or reset .nvme in nvmet_host_dhchap_keyring_store()
- [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show() via sprintf()
--

commit 1e868858e36190db74c6e7c75fe1ba260a535d71
Author: Hannes Reinecke <[email protected]>

nvmet: add configfs attribute 'dhchap_keyring'

This commit adds a dhchap_keyring configfs attribute to the nvmet host group so userspace can select which keyring is used to look up DH-HMAC-CHAP keys.

Quoted 5 lines
drivers/nvme/target/auth.c +0 -0
index 7204ace149565..84091803af300 100644

drivers/nvme/target/auth.c int nvmet_auth_set_key(struct nvmet_host *host, const char *secret, · line 76

Quoted 10 lines
drivers/nvme/target/auth.c +3 -1
@@ -68,7 +68,9 @@ 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);
+	down_read(&nvmet_config_sem);
+	key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
+	up_read(&nvmet_config_sem);
 	if (IS_ERR(key)) {
 		pr_debug("%s: invalid key specification\n", __func__);
 		return PTR_ERR(key);

[Severity: High]
Does nvmet_auth_set_key() dereference a NULL secret when clearing a key via configfs?

Earlier in this series, commit "nvmet-auth: parse dhchap key from configfs attribute" updated nvmet_host_dhchap_key_store() and nvmet_host_dhchap_ctrl_key_store() to leave keydata as NULL when an empty line is written:

drivers/nvme/target/configfs.c:nvmet_host_dhchap_key_store() {
	...
	len = strcspn(page, "\n");
	if (len) {
		keydata = kstrndup(page, len, GFP_KERNEL);
		if (!keydata)
			return -ENOMEM;
	}

	ret = nvmet_auth_set_key(host, keydata, false);
	...
}

nvmet_auth_set_key() then checks if (!strlen(secret)) without testing !secret first, triggering a NULL pointer dereference whenever an empty string or newline is written to dhchap_key or dhchap_ctrl_key to revoke a configured key.

Quoted 22 lines
drivers/nvme/target/configfs.c +14 -0
index bc46d760aaa4a..eb20599cf372d 100644
@@ -2348,6 +2348,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);

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item, · line 2374

Quoted 10 lines
drivers/nvme/target/configfs.c +10 -0
+		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(page, "%s\n", keyring->description) write one byte past the end of the PAGE_SIZE configfs read buffer?

add_key() allows keyring descriptions of up to KEY_MAX_DESC_SIZE - 1 (4095) bytes, and writing a 4095-byte description without a trailing newline to dhchap_keyring succeeds in nvmet_host_dhchap_keyring_store(). Reading dhchap_keyring afterwards writes 4095 description bytes, a newline, and a NUL terminator at page[4096], overflowing the 4096-byte buffer by one byte. Using snprintf(page, PAGE_SIZE, ...) avoids the out-of-bounds write.

Quoted 29 lines
drivers/nvme/target/configfs.c +29 -0
+
+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 < 0 ? ret : count;
+}

[ ... ]

drivers/nvme/target/configfs.c static struct config_group *nvmet_hosts_make_group(struct config_group *group, · line 2512

Quoted 10 lines
drivers/nvme/target/configfs.c +5 -0
@@ -2446,6 +2504,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

[Severity: Medium]
Can userspace restore or reset dhchap_keyring back to the default .nvme keyring through nvmet_host_dhchap_keyring_store()?

nvmet_hosts_make_group() initializes host->dhchap_keyring to the .nvme keyring via key_lookup(nvme_keyring_id()), so reading dhchap_keyring shows ".nvme" (and the !keyring branch in nvmet_host_dhchap_keyring_show() is unreachable). However, nvme_keyring_init() allocates .nvme without linking it into any process keyring, while nvmet_host_dhchap_keyring_store() looks up desc via request_key(&key_type_keyring, desc, NULL), which only searches the calling process's keyrings and rejects empty writes with -EINVAL.

As a result, tools such as nvmetcli that save and restore configfs attributes fail with -ENOKEY when writing ".nvme" back unless .nvme was manually linked into the caller's session keyring, and writing an empty value to reset dhchap_keyring to the default fails with -EINVAL.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
maintainer
#23 lore raw

Re: [PATCH v5 9/9] nvmet: add configfs attribute 'dhchap_keyring'

Quoted 57 lines · On 11/23/26 1:35 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] nvmet: NULL pointer dereference in nvmet_auth_set_key() when clearing key
- [Medium] nvmet: cannot write or reset .nvme in nvmet_host_dhchap_keyring_store()
- [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show() via sprintf()
--

commit 1e868858e36190db74c6e7c75fe1ba260a535d71
Author: Hannes Reinecke <[email protected]>

nvmet: add configfs attribute 'dhchap_keyring'

This commit adds a dhchap_keyring configfs attribute to the nvmet host group so userspace can select which keyring is used to look up DH-HMAC-CHAP keys.

Quoted 5 lines
drivers/nvme/target/auth.c +0 -0
index 7204ace149565..84091803af300 100644

drivers/nvme/target/auth.c int nvmet_auth_set_key(struct nvmet_host *host, const char *secret, · line 76

Quoted 10 lines
drivers/nvme/target/auth.c +3 -1
@@ -68,7 +68,9 @@ 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);
+	down_read(&nvmet_config_sem);
+	key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
+	up_read(&nvmet_config_sem);
  	if (IS_ERR(key)) {
  		pr_debug("%s: invalid key specification\n", __func__);
  		return PTR_ERR(key);

[Severity: High]
Does nvmet_auth_set_key() dereference a NULL secret when clearing a key via configfs?

Earlier in this series, commit "nvmet-auth: parse dhchap key from configfs attribute" updated nvmet_host_dhchap_key_store() and nvmet_host_dhchap_ctrl_key_store() to leave keydata as NULL when an empty line is written:

drivers/nvme/target/configfs.c:nvmet_host_dhchap_key_store() {
	...
	len = strcspn(page, "\n");
	if (len) {
		keydata = kstrndup(page, len, GFP_KERNEL);
		if (!keydata)
			return -ENOMEM;
	}

	ret = nvmet_auth_set_key(host, keydata, false);
	...
}
Quoted 5 lines

nvmet_auth_set_key() then checks if (!strlen(secret)) without testing !secret first, triggering a NULL pointer dereference whenever an empty string or newline is written to dhchap_key or dhchap_ctrl_key to revoke a configured key.

Fixed with the next version.

Quoted 37 lines
Quoted 22 lines
drivers/nvme/target/configfs.c +14 -0
index bc46d760aaa4a..eb20599cf372d 100644
@@ -2348,6 +2348,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);

drivers/nvme/target/configfs.c static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item, · line 2374

Quoted 10 lines
drivers/nvme/target/configfs.c +10 -0
+		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(page, "%s\n", keyring->description) write one byte past the end of the PAGE_SIZE configfs read buffer?

Quoted 7 lines

add_key() allows keyring descriptions of up to KEY_MAX_DESC_SIZE - 1 (4095) bytes, and writing a 4095-byte description without a trailing newline to dhchap_keyring succeeds in nvmet_host_dhchap_keyring_store(). Reading dhchap_keyring afterwards writes 4095 description bytes, a newline, and a NUL terminator at page[4096], overflowing the 4096-byte buffer by one byte. Using snprintf(page, PAGE_SIZE, ...) avoids the out-of-bounds write.

Fixed with the next version.

Quoted 55 lines
Quoted 29 lines
drivers/nvme/target/configfs.c +29 -0
+
+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 < 0 ? ret : count;
+}

[ ... ]

drivers/nvme/target/configfs.c static struct config_group *nvmet_hosts_make_group(struct config_group *group, · line 2512

Quoted 10 lines
drivers/nvme/target/configfs.c +5 -0
@@ -2446,6 +2504,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

[Severity: Medium]
Can userspace restore or reset dhchap_keyring back to the default .nvme keyring through nvmet_host_dhchap_keyring_store()?

nvmet_hosts_make_group() initializes host->dhchap_keyring to the .nvme keyring via key_lookup(nvme_keyring_id()), so reading dhchap_keyring shows ".nvme" (and the !keyring branch in nvmet_host_dhchap_keyring_show() is unreachable). However, nvme_keyring_init() allocates .nvme without linking it into any process keyring, while nvmet_host_dhchap_keyring_store() looks up desc via request_key(&key_type_keyring, desc, NULL), which only searches the calling process's keyrings and rejects empty writes with -EINVAL.

Quoted 5 lines

As a result, tools such as nvmetcli that save and restore configfs attributes fail with -ENOKEY when writing ".nvme" back unless .nvme was manually linked into the caller's session keyring, and writing an empty value to reset dhchap_keyring to the default fails with -EINVAL.

Fixed with the next version.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
#24 lore raw
In reply to Hannes Reinecke
Quoted 4 lines · Hannes Reinecke

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.

Hi Hannes,

How should nvme-cli determine whether the kernel accepts a key description for DH-HMAC-CHAP?

I will add DH-HMAC-CHAP support to nvme-keysd. It will import the secrets into the .nvme keyring, the same way it imports TLS PSKs today, and nvme-cli will pass the key description to 'nvme connect'. On an older kernel, nvme-keysd must not import the secrets, and nvme-cli must pass the key data instead. So both need to know what the kernel supports.

I see these options:

1. Check the kernel version. This does not cover backports.

2. Use a separate option for the key description, for example dhchap_key=%s and dhchap_ctrl_key=%s. nvme-cli would then detect support through /dev/nvme-fabrics, as it does for the other options. It also avoids having one string that can mean either a description or key data.

3. Something else. add_key() fails with ENODEV when the 'dhchap' key type does not exist, but this does not show that the connect options accept a description.

What do you prefer?

Martin

#25 lore raw
Quoted 2 lines · Martin Belanger

How should nvme-cli determine whether the kernel accepts a key description for DH-HMAC-CHAP?

[...]

Quoted 5 lines

2. Use a separate option for the key description, for example dhchap_key=%s and dhchap_ctrl_key=%s. nvme-cli would then detect support through /dev/nvme-fabrics, as it does for the other options. It also avoids having one string that can mean either a description or key data.

Hi Hannes,

A follow-up with test results. I added DH-HMAC-CHAP support to nvme-keysd and tested it with v5 in a VM, with nvmet over TCP and keys in both directions. Import, connect by key description, a wrong key, and a key replacement all work.

For detection, nvme-keysd now uses add_key(): it fails with ENODEV when the 'dhchap' key type does not exist. On an older kernel, the connect fails with "Invalid DH-CHAP secret <name>", which is clear enough. So nvme-cli does not need a new option for this. A separate option would still avoid the ambiguity between a description and key data, but that is your decision.

One finding: add_key() with the exact secret length, without a trailing NUL, fails at random with EPERM. nvme_dhchap_psk_preparse() treats the payload as a C string and reads past it (the bug Sashiko reported), and base64_decode() returns -1. User space passes the length without a NUL, keyctl(1) too. Could v6 parse exactly prep->datalen bytes? For now nvme-keysd passes the NUL.

Martin

maintainer
#26 lore raw
Quoted 12 lines · Martin Belanger
Quoted 2 lines · Martin Belanger

How should nvme-cli determine whether the kernel accepts a key description for DH-HMAC-CHAP?

[...]

Quoted 5 lines

2. Use a separate option for the key description, for example dhchap_key=%s and dhchap_ctrl_key=%s. nvme-cli would then detect support through /dev/nvme-fabrics, as it does for the other options. It also avoids having one string that can mean either a description or key data.

Quoted 7 lines

Hi Hannes,

A follow-up with test results. I added DH-HMAC-CHAP support to nvme-keysd and tested it with v5 in a VM, with nvmet over TCP and keys in both directions. Import, connect by key description, a wrong key, and a key replacement all work.

Cool.

Quoted 7 lines

For detection, nvme-keysd now uses add_key(): it fails with ENODEV when the 'dhchap' key type does not exist. On an older kernel, the connect fails with "Invalid DH-CHAP secret <name>", which is clear enough. So nvme-cli does not need a new option for this. A separate option would still avoid the ambiguity between a description and key data, but that is your decision.

Quoted 7 lines

One finding: add_key() with the exact secret length, without a trailing NUL, fails at random with EPERM. nvme_dhchap_psk_preparse() treats the payload as a C string and reads past it (the bug Sashiko reported), and base64_decode() returns -1. User space passes the length without a NUL, keyctl(1) too. Could v6 parse exactly prep->datalen bytes? For now nvme-keysd passes the NUL.

Yeah, that should be addressed with v6.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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
maintainer
#27 lore raw
In reply to Martin Belanger
Quoted 30 lines · Martin Belanger
Quoted 4 lines · Hannes Reinecke

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.

Hi Hannes,

How should nvme-cli determine whether the kernel accepts a key description for DH-HMAC-CHAP?

I will add DH-HMAC-CHAP support to nvme-keysd. It will import the secrets into the .nvme keyring, the same way it imports TLS PSKs today, and nvme-cli will pass the key description to 'nvme connect'. On an older kernel, nvme-keysd must not import the secrets, and nvme-cli must pass the key data instead. So both need to know what the kernel supports.

I see these options:

1. Check the kernel version. This does not cover backports.

2. Use a separate option for the key description, for example dhchap_key=%s and dhchap_ctrl_key=%s. nvme-cli would then detect support through /dev/nvme-fabrics, as it does for the other options. It also avoids having one string that can mean either a description or key data.

Quoted 4 lines

3. Something else. add_key() fails with ENODEV when the 'dhchap' key type does not exist, but this does not show that the connect options accept a description.

I think option 2) is the easiest. We already have support for that selection mechanism in nvme-cli, and anyone backporting only parts of this series deserves no better.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                                +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