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

Hannes Reinecke hare at suse.de
Fri Nov 20 05:08:08 PST 2026


On 11/20/26 12:31 PM, sashiko-bot at kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] nvme-keyring: build failure and mismatched stubs when
>    !CONFIG_NVME_KEYRING
> - [High] nvme-auth: missing key length validation in
>    nvme_auth_extract_key()
> - [Low] nvme-auth: missing kernel-doc param and leftover struct
>    nvme_dhchap_key
> - [Low] nvme-auth: pointer signedness mismatches in key extraction and
>    reading
> --
> 
> commit ee0a57ee4ed46fe92599175ec65168d3ebd0acca
> Author: Hannes Reinecke <hare at kernel.org>
> 
> nvme-auth: switch to use 'struct key'
> 
> This commit replaces struct nvme_dhchap_key with struct key using the
> dhchap key type across the NVMe common, host, and target authentication
> code.
> 
>> diff --git a/drivers/nvme/common/auth.c b/drivers/nvme/common/auth.c
>> index 569f21ad4fc7e..b7092deed9b06 100644
>> --- a/drivers/nvme/common/auth.c
>> +++ b/drivers/nvme/common/auth.c
> [ ... ]
>> -struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash)
>> +/**
>> + * nvme_auth_extract_key - extract the DH-HMAC-CHAP key
>> + *
>> + * @secret: key data
>> + * @secret_len: length of @secret
>> + *
>> + * Extracts a dhchap key from @secret.
>> + *
>> + * Returns the dhchap key or an error pointer on failure.
>> + */
>> +struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
>> +				  size_t secret_len)
>>   {
>> -	struct nvme_dhchap_key *key;
>> -	const char *p;
>> -	u32 crc;
>> -	int ret, key_len;
>> -	size_t allocated_len = strlen(secret);
>> -
>> -	/* Secret might be affixed with a ':' */
>> -	p = strrchr(secret, ':');
>> -	if (p)
>> -		allocated_len = p - secret;
>> -	key = nvme_auth_alloc_key(allocated_len, 0);
>> -	if (!key)
>> -		return ERR_PTR(-ENOMEM);
>> -
>> -	key_len = base64_decode(secret, allocated_len, key->key, true, BASE64_STD);
>> -	if (key_len < 0) {
>> -		pr_debug("base64 key decoding error %d\n",
>> -			 key_len);
>> -		ret = key_len;
>> -		goto out_free_key;
>> -	}
>> +	struct key *key;
>>   
>> -	if (key_len != 36 && key_len != 52 &&
>> -	    key_len != 68) {
>> -		pr_err("Invalid key len %d\n", key_len);
>> -		ret = -EINVAL;
>> -		goto out_free_key;
>> +	key = nvme_dhchap_psk_create(keyring, secret, secret_len);
>> +	if (!IS_ERR(key)) {
>> +		pr_debug("generated dhchap key %s\n",
>> +			 key->description);
>>   	}
>> -
>> -	/* The last four bytes is the CRC in little-endian format */
>> -	key_len -= 4;
>> -	/*
>> -	 * The linux implementation doesn't do pre- and post-increments,
>> -	 * so we have to do it manually.
>> -	 */
>> -	crc = ~crc32(~0, key->key, key_len);
>> -
>> -	if (get_unaligned_le32(key->key + key_len) != crc) {
>> -		pr_err("key crc mismatch (key %08x, crc %08x)\n",
>> -		       get_unaligned_le32(key->key + key_len), crc);
>> -		ret = -EKEYREJECTED;
>> -		goto out_free_key;
>> -	}
>> -	key->len = key_len;
>> -	key->hash = key_hash;
>>   	return key;
>> -out_free_key:
>> -	nvme_auth_free_key(key);
>> -	return ERR_PTR(ret);
>>   }
>>   EXPORT_SYMBOL_GPL(nvme_auth_extract_key);
> 
> [Severity: High]
> Does replacing the parser here with nvme_dhchap_psk_create() ->
> key_create() -> nvme_dhchap_psk_preparse() drop the decoded key length and
> prefix length checks, allowing an out-of-bounds read in crc32()?
> 
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
> 	...
> 	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> 		   &version, &hmac) != 2) {
> 	...
> 	}
> 
> 	/* skip header and final ':' character */
> 	datalen -= 11;
> 	...
> 	/* decode the data */
> 	prep->quotalen = keylen;
> 	prep->payload.data[0] = upayload;
> 	ret = base64_decode(prep->data + 10, datalen, upayload->data,
> 			    true, BASE64_STD);
> 	if (ret < 0) {
> 		...
> 		return ret;
> 	}
> 	ret -= 4;
> 	crc = ~crc32(~0, upayload->data, ret);
> 	if (get_unaligned_le32(upayload->data + ret) != crc) {
> 	...
> }
> 
> nvme_dhchap_psk_preparse() does not verify that ret is 36, 52, or 68
> before subtracting 4, so a secret whose base64 body decodes to 0..3 bytes
> (such as "DHHC-1:00:AAAA:") makes ret negative and passes a huge size_t to
> crc32(), reading past upayload->data across kernel memory, while other
> invalid decoded sizes are accepted.
> 
> A 1-digit hash ID also satisfies sscanf(), so a 10-byte secret such as
> "DHHC-1:0:a" underflows datalen -= 11 to SIZE_MAX (or has its first base64
> character skipped by prep->data + 10), and prep->quotalen is assigned from
> the uninitialized local variable keylen.  In addition,
> nvme_dhchap_psk_create() replaces PTR_ERR(keyref) with ERR_PTR(-ENOKEY) on
> key_create() failure instead of propagating -EINVAL, -EKEYREJECTED, or
> -ENOMEM.
> 
> Finally, dhchap_secret_store() and dhchap_ctrl_secret_store() pass raw
> count (including any trailing newline) to nvme_auth_extract_key(), leaving
> the trailing ':' in the base64 input; later commits "nvme: parse dhchap
> keys during option parsing" and "nvme: allow to pass in key description as
> dhchap secret" in the series address the sysfs newline handling, while the
> nvme_dhchap_psk_preparse() validation defects and error code clobbering
> remain.
> 
That should now be fixed with the reworked version of the previous patch.

> [ ... ]
> 
>> @@ -317,12 +260,13 @@ static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out)
>>   	return -EINVAL;
>>   }
>>   
>> -int nvme_auth_transform_key(const struct nvme_dhchap_key *key, const char *nqn,
>> +int nvme_auth_transform_key(struct key *key, const char *nqn,
>>   			    u8 **transformed_secret)
>>   {
>>   	struct nvme_auth_hmac_ctx hmac;
>> +	long key_len = 0;
>>   	u8 *transformed_data;
>> -	u8 *key_data;
>> +	u8 *key_data, key_hash;
> [ ... ]
>> +	key_data = kzalloc(key_len, GFP_KERNEL);
>> +	if (!key_data) {
>> +		up_read(&key->sem);
>> +		return -ENOMEM;
>> +	}
>> +	ret = user_read(key, key_data, key_len);
> 
> [Severity: Low]
> Does passing u8 *key_data to user_read() (which expects char *buffer)
> trigger a -Wpointer-sign compiler warning?
> 
Possibly. Fixed it up for the next version.

> Similar pointer-signedness mismatches occur in nvme_auth_extract_key() when
> passing const char *secret to const u8 *data of nvme_dhchap_psk_create(),
> and in nvmet_setup_auth() when passing u8 * host->dhchap_secret and
> host->dhchap_ctrl_secret to strlen() and nvme_auth_extract_key().
> 
Yes, that is confusing. I've now modified host->dhchap_secret and
host->dhchap_ctrl_secret to be of type 'const char *'.

>> diff --git a/include/linux/nvme-auth.h b/include/linux/nvme-auth.h
>> index 1c5ce635b169d..b8ea0945131d1 100644
>> --- a/include/linux/nvme-auth.h
>> +++ b/include/linux/nvme-auth.h
>> @@ -37,13 +37,10 @@ void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data,
>>   			   size_t data_len);
>>   void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out);
>>   
>> -u32 nvme_auth_key_struct_size(u32 key_len);
>> -struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash);
>> -void nvme_auth_free_key(struct nvme_dhchap_key *key);
>> -struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash);
>> -int nvme_auth_transform_key(const struct nvme_dhchap_key *key,
>> -			    const char *nqn, u8 **transformed_secret);
>> -int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key);
>> +struct key *nvme_auth_extract_key(struct key *keyring, const char *secret,
>> +				  size_t secret_len);
> 
> [Severity: Low]
> The commit message notes that the unused struct nvme_dhchap_key definition
> can be dropped, but struct nvme_dhchap_key is still present at the top of
> include/linux/nvme-auth.h.  Also, the new kernel-doc comment on
> nvme_auth_extract_key() in drivers/nvme/common/auth.c omits @keyring, which
> causes a W=1 kernel-doc warning.
> 
Fixed with the next version.

>> diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
>> index ab8971afa9734..9fef4b6add787 100644
>> --- a/include/linux/nvme-keyring.h
>> +++ b/include/linux/nvme-keyring.h
>> @@ -18,12 +18,17 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
>>   
>>   key_serial_t nvme_keyring_id(void);
>>   struct key *nvme_tls_key_lookup(key_serial_t key_id);
>> +
>> +struct key *nvme_dhchap_psk_create(struct key *keyring,
>> +		const u8 *data, size_t data_len);
>> +struct key *nvme_dhchap_psk_lookup(struct key *keyring, const char *identity);
>> +u8 nvme_dhchap_psk_hash(struct key *key);
>> +
>>   #else
>>   static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
>> -		const char *hostnqn, char *subnqn, u8 hmac_id,
>>   		u8 *data, size_t data_len, const char *digest)
>>   {
>> -	return ERR_PTR(-ENOTSUPP);
>> +	return ERR_PTR(-EOPNOTSUPP);
>>   }
>>   static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
>>   		const char *hostnqn, const char *subnqn)
>> @@ -36,7 +41,22 @@ static inline key_serial_t nvme_keyring_id(void)
>>   }
>>   static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
>>   {
>> -	return ERR_PTR(-ENOTSUPP);
>> +	return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +static inline struct key *nvme_dhchap_psk_refresh(struct key *keyring,
>> +		const char *hostnqn, const char *subnqn,
>> +		u8 *data, size_t data_len)
>> +{
>> +	return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +static inline struct key *nvme_dhchap_psk_lookup(struct key *keyring,
>> +		const char *hostnqn, const char *subnqn, u8 hmac);
>> +{
>> +	return ERR_PTR(-EOPNOTSUPP);
>> +}
>> +u8 nvme_dhchap_psk_hash(struct key *key)
>> +{
>> +	return 0;
>>   }
>>   #endif /* !CONFIG_NVME_KEYRING */
>>   #endif /* _NVME_KEYRING_H */
> 
> [Severity: High]
> Does the !CONFIG_NVME_KEYRING branch fail to build when
> include/linux/nvme-keyring.h is included with CONFIG_NVME_KEYRING disabled?
> 
> The nvme_dhchap_psk_lookup() stub has a stray semicolon after its parameter
> list and takes four arguments instead of matching the two-argument
> declaration in the CONFIG_NVME_KEYRING branch.  nvme_dhchap_psk_hash() is
> defined in the header without static inline, causing multiple-definition
> linker errors across translation units.  This branch also defines
> nvme_dhchap_psk_refresh() instead of a stub for nvme_dhchap_psk_create(),
> and removes hostnqn, subnqn, and hmac_id from the nvme_tls_psk_refresh()
> stub signature.
> 
Fixed up the declarations with the next version.

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare at suse.de                                +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich



More information about the Linux-nvme mailing list