[PATCH] nvmet-auth: lock DH-HMAC-CHAP key clearing
Hannes Reinecke
hare at suse.de
Mon Nov 23 00:13:49 PST 2026
On 11/13/26 10:42 AM, Yao Sang wrote:
> Clearing a key without nvmet_config_sem can race with configfs reads
> or authentication and cause a use-after-free.
>
> Use the same locked path to set and clear keys.
>
> Fixes: bb2df18958b4 ("nvmet-auth: allow to clear DH-HMAC-CHAP keys")
> Signed-off-by: Yao Sang <sangyao at kylinos.cn>
> ---
> drivers/nvme/target/auth.c | 43 ++++++++++++++++----------------------
> 1 file changed, 18 insertions(+), 25 deletions(-)
>
> diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
> index ccba84b1344e..3cbe90a432ef 100644
> --- a/drivers/nvme/target/auth.c
> +++ b/drivers/nvme/target/auth.c
> @@ -22,39 +22,32 @@
> int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
> bool set_ctrl)
> {
> - unsigned char key_hash;
> - char *dhchap_secret;
> -
> - if (!strlen(secret)) {
> - if (set_ctrl) {
> - kfree(host->dhchap_ctrl_secret);
> - host->dhchap_ctrl_secret = NULL;
> - host->dhchap_ctrl_key_hash = 0;
> - } else {
> - kfree(host->dhchap_secret);
> - host->dhchap_secret = NULL;
> - host->dhchap_key_hash = 0;
> + unsigned char key_hash = 0;
> + char *dhchap_secret = NULL;
> +
> + if (*secret) {
> + if (sscanf(secret, "DHHC-1:%hhd:%*s", &key_hash) != 1)
> + return -EINVAL;
> + if (key_hash > 3) {
> + pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
> + key_hash);
> + return -EINVAL;
> }
> - return 0;
> - }
> - if (sscanf(secret, "DHHC-1:%hhd:%*s", &key_hash) != 1)
> - 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;
> + dhchap_secret = strim(dhchap_secret);
> }
> - dhchap_secret = kstrdup(secret, GFP_KERNEL);
> - if (!dhchap_secret)
> - return -ENOMEM;
> +
> down_write(&nvmet_config_sem);
> if (set_ctrl) {
> kfree(host->dhchap_ctrl_secret);
> - host->dhchap_ctrl_secret = strim(dhchap_secret);
> + host->dhchap_ctrl_secret = dhchap_secret;
> host->dhchap_ctrl_key_hash = key_hash;
> } else {
> kfree(host->dhchap_secret);
> - host->dhchap_secret = strim(dhchap_secret);
> + host->dhchap_secret = dhchap_secret;
> host->dhchap_key_hash = key_hash;
> }
> up_write(&nvmet_config_sem);
This will be modified by my patchset to move DH-CHAP to the linux
keystore, but until then:
Reviewed-by: Hannes Reinecke <hare at kernel.org>
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