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

Hannes Reinecke hare at suse.de
Fri Nov 20 06:15:09 PST 2026


On 11/20/26 12:28 PM, sashiko-bot at kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 5 potential
> issues to consider:
> - [High] nvmet-auth: missing error abort and invalid status in
>    nvmet_setup_auth()
> - [High] nvmet-auth: missing initialization of ctrl->shash_id in
>    nvmet_setup_auth()
> - [High] nvmet-auth: missing length checks in nvme_dhchap_psk_preparse()
> - [Medium] nvmet-auth: memory leak of keydata in
>    nvmet_host_dhchap_ctrl_key_store()
> - [Medium] nvmet-auth: empty writes rejected in configfs key store
>    callbacks
> --
> 
> commit 4bc851b3a915dfe8fe3d7c3db8fe8c3fa58c01ca
> Author: Hannes Reinecke <hare at kernel.org>
> 
> nvmet-auth: parse dhchap key from configfs attribute
> 
> This commit converts the nvmet configfs DH-HMAC-CHAP key handling to store
> host and controller keys as struct key pointers rather than raw secret
> strings.
> 
>> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
>> index 26457ce4acc2a..33b45b4b202d4 100644
>> --- a/drivers/nvme/target/auth.c
>> +++ b/drivers/nvme/target/auth.c
>> @@ -19,52 +19,63 @@
> 
> [ ... ]
> 
>>   int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
>>   		       bool set_ctrl)
>>   {
>> -	unsigned char key_hash;
>> -	char *dhchap_secret;
>> -	int offset = 0;
>> +	struct key *key;
>> +	size_t len;
>>   
>>   	if (!strlen(secret)) {
>>   		down_write(&nvmet_config_sem);
>> -		if (set_ctrl) {
>> -			kfree(host->dhchap_ctrl_secret);
>> -			host->dhchap_ctrl_secret = NULL;
>> -			host->dhchap_ctrl_key_hash = 0;
>> -			host->dhchap_ctrl_key_offset = 0;
>> -		} else {
>> -			kfree(host->dhchap_secret);
>> -			host->dhchap_secret = NULL;
>> -			host->dhchap_key_hash = 0;
>> -			host->dhchap_key_offset = 0;
>> -		}
>> +		nvmet_auth_revoke_key(host, set_ctrl);
>>   		up_write(&nvmet_config_sem);
>>   		return 0;
>>   	}
>> -	if (sscanf(secret, "DHHC-1:%hhd:%n", &key_hash, &offset) != 1 ||
>> -	    !offset)
>> -		return -EINVAL;
>> -	if (key_hash > 3) {
>> -		pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
>> -			 key_hash);
>> -		return -EINVAL;
>> -	}
>> -	dhchap_secret = kstrdup(secret, GFP_KERNEL);
>> -	if (!dhchap_secret)
>> -		return -ENOMEM;
>> +	len = strcspn(secret, "\n");
>> +	key = nvme_auth_extract_key(NULL, secret, len);
>> +	if (IS_ERR(key)) {
>> +		pr_debug("%s: invalid key specification\n", __func__);
>> +		return PTR_ERR(key);
>> +	}
> 
> [Severity: High]
> Can passing unvalidated configfs strings into nvme_auth_extract_key()
> underflow datalen and ret in nvme_dhchap_psk_preparse()?
> 
> nvme_auth_extract_key() calls nvme_dhchap_psk_create() -> key_create() ->
> nvme_dhchap_psk_preparse():
> 
> drivers/nvme/common/keyring.c:nvme_dhchap_psk_preparse() {
> 	...
> 	if (sscanf(prep->data, "DHHC-%01hhu:%02hhu:%*s",
> 		   &version, &hmac) != 2) {
> 		...
> 		return -EINVAL;
> 	}
> 
> 	/* skip header and final ':' character */
> 	datalen -= 11;
> 	...
> 	ret = base64_decode(prep->data + 10, datalen, upayload->data,
> 			    true, BASE64_STD);
> 	if (ret < 0) {
> 		...
> 		return ret;
> 	}
> 	ret -= 4;
> 	crc = ~crc32(~0, upayload->data, ret);
> 	...
> }
> 
> Because %02hhu matches 1 or 2 digits and %*s is not counted in sscanf()'s
> return value, a short string such as "DHHC-1:1:" (9 bytes) returns 2 with
> datalen < 11, so datalen -= 11 underflows size_t datalen and passes a huge
> length to base64_decode().  Similarly, a short payload that decodes to 0..3
> bytes (such as "DHHC-1:01:AA==:") makes ret -= 4 negative, which is then
> converted to size_t in crc32() and reads past upayload->data.
> 
The parsing routines have been fixed up with updates to earlier patches.

> [ ... ]
> 
>> @@ -186,44 +195,74 @@ u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, bool reset)
>>   		goto out_unlock;
>>   	}
>>   
>> -	if (!host->dhchap_secret) {
>> +	if (!host->dhchap_key) {
>>   		pr_debug("No authentication provided\n");
>>   		goto out_unlock;
>>   	}
>>   
>> -	host_key = nvme_auth_extract_key(NULL, host->dhchap_secret,
>> -					 strlen(host->dhchap_secret));
>> -	if (IS_ERR(host_key)) {
>> -		ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
>> +	key = key_get(host->dhchap_key);
>> +	if (!key) {
>> +		pr_warn("%s: host key %08x not found\n",
>> +			__func__, key_serial(host->dhchap_key));
>>   		goto out_unlock;
>>   	}
>> -	if (host->dhchap_ctrl_secret) {
>> -		ctrl_key = nvme_auth_extract_key(NULL, host->dhchap_ctrl_secret,
>> -						 strlen(host->dhchap_ctrl_secret));
>> -		if (IS_ERR(ctrl_key)) {
>> -			key_put(host_key);
>> -			ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
>> -			goto out_unlock;
>> +	down_read(&key->sem);
>> +	ret = key_validate(key);
>> +	if (!ret) {
>> +		if (ctrl->host_key) {
>> +			pr_debug("%s: drop host key %08x\n",
>> +				 __func__, key_serial(ctrl->host_key));
>> +			key_put(ctrl->host_key);
>>   		}
>> +		ctrl->host_key = key;
>> +	}
>> +	key_id = key_serial(key);
>> +	up_read(&key->sem);
>> +	if (ret) {
>> +		pr_warn("host key %08x invalidated\n", key_id);
>> +		key_put(key);
>> +		key = ERR_PTR(-EKEYREVOKED);
>>   	}
> 
> [Severity: High]
> When key_validate(key) fails on host->dhchap_key, doesn't execution fall
> through instead of setting ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE and
> jumping to out_unlock?
> 
> If host->dhchap_ctrl_key is present and valid, the second key_validate()
> overwrites ret with 0, so nvmet_setup_auth() returns 0 while leaving
> ctrl->host_key NULL on initial connect, bypassing authentication because
> nvmet_has_auth() and nvmet_check_auth_status() treat a NULL ctrl->host_key
> as no authentication required.  If host->dhchap_ctrl_key is absent or also
> invalid, nvmet_setup_auth() returns the negative errno from key_validate()
> truncated to u8 instead of NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE, and if
> key_get() returns NULL it jumps to out_unlock with ret == 0.
> 
Correct. Fixed up with the next version.

>> -	key_put(ctrl->host_key);
>> -	ctrl->host_key = host_key;
>> -	key_put(ctrl->ctrl_key);
>> -	ctrl->ctrl_key = ctrl_key;
>> -	ctrl->shash_id = host->dhchap_hash_id;
>>   #ifdef CONFIG_NVME_TARGET_AUTH_DEBUG
>> -	host_hash = nvme_dhchap_psk_hash(ctrl->host_key);
>> -	pr_debug("%s: using hash %s key %u\n", __func__,
>> -		 ctrl_hash > 0 ?
>> -		 nvme_auth_hmac_name(ctrl_hash) : "none",
>> -		 key_serial(ctrl->host_key));
>> -	if (ctrl->ctrl_key) {
>> -		ctrl_hash = nvme_dhchap_psk_hash(ctrl->ctrl_key);
>> -		pr_debug("%s: using ctrl hash %s key %u\n", __func__,
>> -			 ctrl_hash > 0 ?
>> -			 nvme_auth_hmac_name(ctrl_hash) : "none",
>> -			 key_serial(ctrl->ctrl_key));
>> +	pr_debug("%s: using dhchap hash %s key %08x\n", __func__,
>> +		 nvme_auth_hmac_name(ctrl->shash_id), key_id);
>> +#endif
> 
> [Severity: High]
> Does dropping ctrl->shash_id = host->dhchap_hash_id leave ctrl->shash_id at
> 0?
> 
> nvmet_auth_negotiate() matches the host's proposed HMAC IDs against
> ctrl->shash_id, so leaving it at 0 ignores the host's configured dhchap_hash
> attribute and always falls back to fallback_hash_id (and passes 0 to
> nvme_auth_hmac_name() in the debug prints here).
> 
Of course. Reverted with the next version.

>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 27c50fa533e70..880cf5151588a 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
> 
> [ ... ]
> 
>> @@ -2251,9 +2266,19 @@ static ssize_t nvmet_host_dhchap_key_store(struct config_item *item,
>>   		const char *page, size_t count)
>>   {
>>   	struct nvmet_host *host = to_host(item);
>> +	u8 *keydata;
>> +	size_t len;
>>   	int ret;
>>   
>> -	ret = nvmet_auth_set_key(host, page, false);
>> +	len = strcspn(page, "\n");
>> +	if (!len)
>> +		return -EINVAL;
>> +	keydata = kstrndup(page, len, GFP_KERNEL);
>> +	if (!keydata)
>> +		return -ENOMEM;
>> +
>> +	ret = nvmet_auth_set_key(host, keydata, false);
>> +	kfree_sensitive(keydata);
> 
> [Severity: Medium]
> Does returning -EINVAL when !len in nvmet_host_dhchap_key_store() and
> nvmet_host_dhchap_ctrl_key_store() prevent clearing a configured key via
> configfs?  An empty or newline-only write is rejected before reaching the
> !strlen(secret) revocation branch in nvmet_auth_set_key().
> 
Fixed up with the next version.

> [ ... ]
> 
>> @@ -2284,9 +2323,18 @@ static ssize_t nvmet_host_dhchap_ctrl_key_store(struct config_item *item,
>>   		const char *page, size_t count)
>>   {
>>   	struct nvmet_host *host = to_host(item);
>> +	u8 *keydata;
>> +	size_t len;
>>   	int ret;
>>   
>> -	ret = nvmet_auth_set_key(host, page, true);
>> +	len = strcspn(page, "\n");
>> +	if (!len)
>> +		return -EINVAL;
>> +	keydata = kstrndup(page, len, GFP_KERNEL);
>> +	if (!keydata)
>> +		return -ENOMEM;
>> +
>> +	ret = nvmet_auth_set_key(host, keydata, true);
>>   	/*
>>   	 * Re-authentication is a soft state, so keep the
>>   	 * current authentication valid until the host
> 
> [Severity: Medium]
> Does nvmet_host_dhchap_ctrl_key_store() leak keydata?  Unlike
> nvmet_host_dhchap_key_store(), it does not call kfree_sensitive(keydata)
> after nvmet_auth_set_key().
> 

Fixed up with the next version.

Cheers,

Hannes
-- 
Dr. Hannes Reinecke                  Kernel Storage Architect
hare 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