[PATCH v4 9/9] nvmet: add configfs attribute 'dhchap_keyring'
Hannes Reinecke
hare at suse.de
Fri Nov 20 07:27:35 PST 2026
On 11/20/26 12:39 PM, sashiko-bot at kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] nvmet: keyring reference leak in nvmet_host_release()
> - [Medium] nvmet: inverted error return sign in
> nvmet_host_dhchap_keyring_store()
> - [Medium] nvmet: unlocked host->dhchap_keyring access in
> nvmet_auth_set_key()
> - [Medium] nvmet: 1-byte OOB write in nvmet_host_dhchap_keyring_show()
> via sprintf()
> --
>
> commit 7f1bb5c7e432b0938e23615bf24751f61d823e03
> Author: Hannes Reinecke <hare at kernel.org>
>
> nvmet: add configfs attribute 'dhchap_keyring'
>
> This commit adds a dhchap_keyring configfs attribute to the nvmet host
> directory so userspace can specify the keyring used for DH-HMAC-CHAP key
> lookups.
>
>> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
>> index 4f2489c5d894b..d583813ef4f27 100644
>> --- a/drivers/nvme/target/auth.c
>> +++ b/drivers/nvme/target/auth.c
>> @@ -68,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, &generated);
>> + key = nvme_auth_extract_key(host->dhchap_keyring, secret, len, &generated);
>> if (IS_ERR(key)) {
>> pr_debug("%s: invalid key specification\n", __func__);
>> return PTR_ERR(key);
>
> [Severity: Medium]
> Can a concurrent write to dhchap_keyring free host->dhchap_keyring while
> nvmet_auth_set_key() is using it?
>
> CPU0 CPU1
> nvmet_host_dhchap_key_store()
> nvmet_auth_set_key()
> keyring = host->dhchap_keyring
> nvmet_host_dhchap_keyring_store()
> down_write(&nvmet_config_sem)
> key_put(host->dhchap_keyring)
> host->dhchap_keyring = new_ring
> up_write(&nvmet_config_sem)
> nvme_auth_extract_key(keyring, ...)
> ... <-- use-after-free
>
> nvmet_host_dhchap_keyring_store() replaces host->dhchap_keyring and drops
> the old reference under nvmet_config_sem, whereas nvmet_auth_set_key()
> reads and uses host->dhchap_keyring without holding nvmet_config_sem or
> pinning it with key_get().
>
Fixed with the next version.
>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 319947aa0a40f..ffe0f852e59d9 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
>> @@ -2347,6 +2347,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);
>> + 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() write one byte past the end of page here?
>
> add_key() accepts descriptions up to 4095 bytes (KEY_MAX_DESC_SIZE - 1),
> and configfs fill_write_buffer() accepts a 4095-byte write without a
> trailing newline. Formatting a 4095-byte keyring->description with "%s\n"
> writes 4097 bytes (including the NUL terminator) into the 4096-byte
> PAGE_SIZE configfs read buffer. Using sysfs_emit() or snprintf(page,
> PAGE_SIZE, ...) would avoid the overflow.
>
>> +
>> +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 ? -ret : count;
>> +}
>
> [Severity: Medium]
> Does negating ret return a positive value when request_key() fails?
> PTR_ERR(keyring) is already negative, so returning -ret causes
> configfs_write_iter() to treat the positive errno as a successful write
> byte count and silently ignore the failure.
>
Fixed with the new version.
> [ ... ]
>
>> @@ -2445,6 +2502,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
>>
>> config_group_init_type_name(&host->group, name, &nvmet_host_type);
>
> [Severity: High]
> Does removing a host group leak host->dhchap_keyring?
>
> Both key_lookup() here and request_key() in
> nvmet_host_dhchap_keyring_store() acquire a reference on
> host->dhchap_keyring, but nvmet_host_release() never calls
> key_put(host->dhchap_keyring):
>
> drivers/nvme/target/configfs.c:nvmet_host_release() {
> struct nvmet_host *host = to_host(item);
>
> #ifdef CONFIG_NVME_TARGET_AUTH
> nvmet_auth_revoke_key(host, false);
> nvmet_auth_revoke_key(host, true);
> #endif
> kfree(host);
> }
>
> Every removed host configfs directory leaks a reference on its configured
> keyring.
>
Fixed 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