All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hannes Reinecke" <hare@kernel.org>
Cc: linux-nvme@lists.infradead.org
Subject: Re: [PATCH v4 3/9] nvme-auth: switch to use 'struct key'
Date: Fri, 20 Nov 2026 11:31:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-225045@kernel.org> (raw)
In-Reply-To: <20261120-dhchap-keyring-v4-v4-3-5f8c7d9926ee@kernel.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] nvme-keyring: build failure and mismatched stubs when
  !CONFIG_NVME_KEYRING
- [High] nvme-auth: missing key length validation in
  nvme_auth_extract_key()
- [Low] nvme-auth: missing kernel-doc param and leftover struct
  nvme_dhchap_key
- [Low] nvme-auth: pointer signedness mismatches in key extraction and
  reading
--

commit ee0a57ee4ed46fe92599175ec65168d3ebd0acca
Author: Hannes Reinecke <hare@kernel.org>

nvme-auth: switch to use 'struct key'

This commit replaces struct nvme_dhchap_key with struct key using the
dhchap key type across the NVMe common, host, and target authentication
code.

> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
> index 569f21ad4fc7e..b7092deed9b06 100644
> --- a/drivers/nvme/common/auth.c
> +++ b/drivers/nvme/common/auth.c
[ ... ]
> -struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash)
> +/**
> + * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
> + *
> + * @secret: key data
> + * @secret_len: length of @secret
> + *
> + * Extracts a dhchap key from @secret.
> + *
> + * Returns the dhchap key or an error pointer on failure.
> + */
> +struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
> +				  size_t secret_len)
>  {
> -	struct nvme_dhchap_key *key;
> -	const char *p;
> -	u32 crc;
> -	int ret, key_len;
> -	size_t allocated_len = strlen(secret);
> -
> -	/* Secret might be affixed with a ':' */
> -	p = strrchr(secret, ':');
> -	if (p)
> -		allocated_len = p - secret;
> -	key = nvme_auth_alloc_key(allocated_len, 0);
> -	if (!key)
> -		return ERR_PTR(-ENOMEM);
> -
> -	key_len = base64_decode(secret, allocated_len, key->key, true, BASE64_STD);
> -	if (key_len < 0) {
> -		pr_debug("base64 key decoding error %d\n",
> -			 key_len);
> -		ret = key_len;
> -		goto out_free_key;
> -	}
> +	struct key *key;
>  
> -	if (key_len != 36 && key_len != 52 &&
> -	    key_len != 68) {
> -		pr_err("Invalid key len %d\n", key_len);
> -		ret = -EINVAL;
> -		goto out_free_key;
> +	key = nvme_dhchap_psk_create(keyring, secret, secret_len);
> +	if (!IS_ERR(key)) {
> +		pr_debug("generated dhchap key %s\n",
> +			 key->description);
>  	}
> -
> -	/* The last four bytes is the CRC in little-endian format */
> -	key_len -= 4;
> -	/*
> -	 * The linux implementation doesn't do pre- and post-increments,
> -	 * so we have to do it manually.
> -	 */
> -	crc = ~crc32(~0, key->key, key_len);
> -
> -	if (get_unaligned_le32(key->key + key_len) != crc) {
> -		pr_err("key crc mismatch (key %08x, crc %08x)\n",
> -		       get_unaligned_le32(key->key + key_len), crc);
> -		ret = -EKEYREJECTED;
> -		goto out_free_key;
> -	}
> -	key->len = key_len;
> -	key->hash = key_hash;
>  	return key;
> -out_free_key:
> -	nvme_auth_free_key(key);
> -	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(nvme_auth_extract_key);

[Severity: High]
Does replacing the parser here with nvme_dhchap_psk_create() ->
key_create() -> nvme_dhchap_psk_preparse() drop the decoded key length and
prefix length checks, allowing an out-of-bounds read in crc32()?

drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
	...
	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
		   &version, &hmac) != 2) {
	...
	}

	/* skip header and final ':' character */
	datalen -= 11;
	...
	/* decode the data */
	prep->quotalen = keylen;
	prep->payload.data[0] = upayload;
	ret = base64_decode(prep->data + 10, datalen, upayload->data,
			    true, BASE64_STD);
	if (ret < 0) {
		...
		return ret;
	}
	ret -= 4;
	crc = ~crc32(~0, upayload->data, ret);
	if (get_unaligned_le32(upayload->data + ret) != crc) {
	...
}

nvme_dhchap_psk_preparse() does not verify that ret is 36, 52, or 68
before subtracting 4, so a secret whose base64 body decodes to 0..3 bytes
(such as "DHHC-1:00:AAAA:") makes ret negative and passes a huge size_t to
crc32(), reading past upayload->data across kernel memory, while other
invalid decoded sizes are accepted.

A 1-digit hash ID also satisfies sscanf(), so a 10-byte secret such as
"DHHC-1:0:a" underflows datalen -= 11 to SIZE_MAX (or has its first base64
character skipped by prep->data + 10), and prep->quotalen is assigned from
the uninitialized local variable keylen.  In addition,
nvme_dhchap_psk_create() replaces PTR_ERR(keyref) with ERR_PTR(-ENOKEY) on
key_create() failure instead of propagating -EINVAL, -EKEYREJECTED, or
-ENOMEM.

Finally, dhchap_secret_store() and dhchap_ctrl_secret_store() pass raw
count (including any trailing newline) to nvme_auth_extract_key(), leaving
the trailing ':' in the base64 input; later commits "nvme: parse dhchap
keys during option parsing" and "nvme: allow to pass in key description as
dhchap secret" in the series address the sysfs newline handling, while the
nvme_dhchap_psk_preparse() validation defects and error code clobbering
remain.

[ ... ]

> @@ -317,12 +260,13 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
>  	return -EINVAL;
>  }
>  
> -int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
> +int nvme_auth_transform_key(struct key *key, const char *nqn,
>  			    u8 **transformed_secret)
>  {
>  	struct nvme_auth_hmac_ctx hmac;
> +	long key_len = 0;
>  	u8 *transformed_data;
> -	u8 *key_data;
> +	u8 *key_data, key_hash;
[ ... ]
> +	key_data = kzalloc(key_len, GFP_KERNEL);
> +	if (!key_data) {
> +		up_read(&key->sem);
> +		return -ENOMEM;
> +	}
> +	ret = user_read(key, key_data, key_len);

[Severity: Low]
Does passing u8 *key_data to user_read() (which expects char *buffer)
trigger a -Wpointer-sign compiler warning?

Similar pointer-signedness mismatches occur in nvme_auth_extract_key() when
passing const char *secret to const u8 *data of nvme_dhchap_psk_create(),
and in nvmet_setup_auth() when passing u8 * host->dhchap_secret and
host->dhchap_ctrl_secret to strlen() and nvme_auth_extract_key().

> diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
> index 1c5ce635b169d..b8ea0945131d1 100644
> --- a/include/linux/nvme-auth.h
> +++ b/include/linux/nvme-auth.h
> @@ -37,13 +37,10 @@ void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data,
>  			   size_t data_len);
>  void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out);
>  
> -u32 nvme_auth_key_struct_size(u32 key_len);
> -struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash);
> -void nvme_auth_free_key(struct nvme_dhchap_key *key);
> -struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash);
> -int nvme_auth_transform_key(const struct nvme_dhchap_key *key,
> -			    const char *nqn, u8 **transformed_secret);
> -int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key);
> +struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
> +				  size_t secret_len);

[Severity: Low]
The commit message notes that the unused struct nvme_dhchap_key definition
can be dropped, but struct nvme_dhchap_key is still present at the top of
include/linux/nvme-auth.h.  Also, the new kernel-doc comment on
nvme_auth_extract_key() in drivers/nvme/common/auth.c omits @keyring, which
causes a W=1 kernel-doc warning.

> diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
> index ab8971afa9734..9fef4b6add787 100644
> --- a/include/linux/nvme-keyring.h
> +++ b/include/linux/nvme-keyring.h
> @@ -18,12 +18,17 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
>  
>  key_serial_t nvme_keyring_id(void);
>  struct key *nvme_tls_key_lookup(key_serial_t key_id);
> +
> +struct key *nvme_dhchap_psk_create(struct key *keyring,
> +		const u8 *data, size_t data_len);
> +struct key *nvme_dhchap_psk_lookup(struct key *keyring, const char *identity);
> +u8 nvme_dhchap_psk_hash(struct key *key);
> +
>  #else
>  static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
> -		const char *hostnqn, char *subnqn, u8 hmac_id,
>  		u8 *data, size_t data_len, const char *digest)
>  {
> -	return ERR_PTR(-ENOTSUPP);
> +	return ERR_PTR(-EOPNOTSUPP);
>  }
>  static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
>  		const char *hostnqn, const char *subnqn)
> @@ -36,7 +41,22 @@ static inline key_serial_t nvme_keyring_id(void)
>  }
>  static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
>  {
> -	return ERR_PTR(-ENOTSUPP);
> +	return ERR_PTR(-EOPNOTSUPP);
> +}
> +static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
> +		const char *hostnqn, const char *subnqn,
> +		u8 *data, size_t data_len)
> +{
> +	return ERR_PTR(-EOPNOTSUPP);
> +}
> +static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
> +		const char *hostnqn, const char *subnqn, u8 hmac);
> +{
> +	return ERR_PTR(-EOPNOTSUPP);
> +}
> +u8 nvme_dhchap_psk_hash(struct key *key)
> +{
> +	return 0;
>  }
>  #endif /* !CONFIG_NVME_KEYRING */
>  #endif /* _NVME_KEYRING_H */

[Severity: High]
Does the !CONFIG_NVME_KEYRING branch fail to build when
include/linux/nvme-keyring.h is included with CONFIG_NVME_KEYRING disabled?

The nvme_dhchap_psk_lookup() stub has a stray semicolon after its parameter
list and takes four arguments instead of matching the two-argument
declaration in the CONFIG_NVME_KEYRING branch.  nvme_dhchap_psk_hash() is
defined in the header without static inline, causing multiple-definition
linker errors across translation units.  This branch also defines
nvme_dhchap_psk_refresh() instead of a stub for nvme_dhchap_psk_create(),
and removes hostnqn, subnqn, and hmac_id from the nvme_tls_psk_refresh()
stub signature.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-dhchap-keyring-v4-v4-0-5f8c7d9926ee@kernel.org?part=3


  reply	other threads:[~2026-11-20 11:31 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 11:13 [PATCH v4 0/9] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 1/9] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-20 11:19   ` sashiko-bot
2026-11-20 11:42     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 2/9] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-20 11:28   ` sashiko-bot
2026-11-20 12:28     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 3/9] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-20 11:31   ` sashiko-bot [this message]
2026-11-20 13:08     ` Hannes Reinecke
2026-11-20 19:24   ` kernel test robot
2026-11-20 19:37   ` kernel test robot
2026-11-20 11:14 ` [PATCH v4 4/9] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-20 11:32   ` sashiko-bot
2026-11-20 13:55     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 5/9] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-20 11:28   ` sashiko-bot
2026-11-20 14:15     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 6/9] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-20 11:31   ` sashiko-bot
2026-11-20 14:48     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 7/9] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-20 11:26   ` sashiko-bot
2026-11-20 14:52     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 8/9] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-20 11:37   ` sashiko-bot
2026-11-20 15:02     ` Hannes Reinecke
2026-11-20 11:14 ` [PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring' Hannes Reinecke
2026-11-20 11:39   ` sashiko-bot
2026-11-20 15:27     ` Hannes Reinecke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-225045@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=hare@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.