[PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute

sashiko-bot at kernel.org sashiko-bot at kernel.org
Thu Nov 26 06:30:12 PST 2026


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] nvmet-auth: ERR_PTR dereference in key_lookup() callers
--

commit b41d5ff16da6a60366fa816c551dfd75334950eb
Author: Hannes Reinecke <hare at kernel.org>

nvmet-auth: parse dhchap key from configfs attribute

This commit converts the nvmet DH-HMAC-CHAP configfs attributes to store
key serial numbers in struct nvmet_host and look up the keys via
key_lookup() when needed.

> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index e1d92f5455219..ff0e8569d7067 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c

[ ... ]

> @@ -186,22 +212,45 @@ 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(host->dhchap_secret,
> -					 strlen(host->dhchap_secret));
> -	if (IS_ERR(host_key)) {
> +	host_key = key_lookup(host->dhchap_key);
> +	if (!host_key) {
> +		pr_warn("%s: host key %08x not found\n",
> +			__func__, host->dhchap_key);
>  		ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
>  		goto out_unlock;
>  	}
> -	if (host->dhchap_ctrl_secret) {
> -		ctrl_key = nvme_auth_extract_key(host->dhchap_ctrl_secret,
> -						 strlen(host->dhchap_ctrl_secret));
> -		if (IS_ERR(ctrl_key)) {
> +	down_read(&host_key->sem);
> +	ret = key_validate(host_key);
> +	up_read(&host_key->sem);
> +	if (ret) {
> +		pr_warn("host key %08x invalidated\n",
> +			host->dhchap_key);
> +		key_put(host_key);
> +		ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> +		goto out_unlock;
> +	}
> +	if (host->dhchap_ctrl_key) {
> +		ctrl_key = key_lookup(host->dhchap_ctrl_key);
> +		if (!ctrl_key) {
> +			pr_warn("%s: ctrl key %08x not found\n",
> +				__func__, host->dhchap_ctrl_key);
> +			key_put(host_key);
> +			ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
> +			goto out_unlock;
> +		}
> +		down_read(&ctrl_key->sem);
>  
> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
> index ff3a8d74de599..f6bfda757f076 100644
> --- a/drivers/nvme/target/configfs.c
> +++ b/drivers/nvme/target/configfs.c

[ ... ]

> @@ -2234,15 +2235,29 @@ static struct config_group nvmet_ports_group;
>  static ssize_t nvmet_host_dhchap_key_show(struct config_item *item,
>  		char *page)
>  {
> -	char *dhchap_secret;
> +	struct nvmet_host *host = to_host(item);
> +	struct key *key;
>  	ssize_t ret;
>  
>  	down_read(&nvmet_config_sem);
> -	dhchap_secret = to_host(item)->dhchap_secret;
> -	if (!dhchap_secret)
> -		ret = sprintf(page, "\n");
> -	else
> -		ret = sprintf(page, "%s\n", dhchap_secret);
> +	key = key_lookup(host->dhchap_key);
> +	if (!key) {
> +		page[0] = '\0';
> +		ret = 0;
> +	} else {
> +		down_read(&key->sem);

[ ... ]

> @@ -2267,15 +2292,29 @@ CONFIGFS_ATTR(nvmet_host_, dhchap_key);
>  static ssize_t nvmet_host_dhchap_ctrl_key_show(struct config_item *item,
>  		char *page)
>  {
> -	char *dhchap_secret = to_host(item)->dhchap_ctrl_secret;
> +	struct nvmet_host *host = to_host(item);
> +	struct key *key;
>  	ssize_t ret;
>  
>  	down_read(&nvmet_config_sem);
> -	dhchap_secret = to_host(item)->dhchap_ctrl_secret;
> -	if (!dhchap_secret)
> -		ret = sprintf(page, "\n");
> -	else
> -		ret = sprintf(page, "%s\n", dhchap_secret);
> +	key = key_lookup(host->dhchap_ctrl_key);
> +	if (!key) {
> +		page[0] = '\0';
> +		ret = 0;
> +	} else {
> +		down_read(&key->sem);

[Severity: High]
Should the key_lookup() checks in nvmet_setup_auth(),
nvmet_host_dhchap_key_show(), and nvmet_host_dhchap_ctrl_key_show() use
IS_ERR() instead of testing for NULL?

key_lookup() returns ERR_PTR(-ENOKEY) when a key serial is not found
(including when dhchap_key or dhchap_ctrl_key is 0 because no key is
configured).  Checking !key lets ERR_PTR(-ENOKEY) through and crashes at
down_read(&key->sem) when reading the configfs attributes on a host
without a key configured or when a key is not found during
authentication setup.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-dhchap-keyring-v4-v7-0-ee438ffea527@kernel.org?part=8



More information about the Linux-nvme mailing list