[PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring
From: Hannes Reinecke
Date: Thu Nov 26 2026 - 09:13:51 EST
Pre-provisioned keys might live in a different keyring which might
have a different lifetime than the structures referencing the key.
So to ensure the key remains accessible link the pre-provisioned key
into the .nvme keyring, and unlink it once it's no longer needed.
Signed-off-by: Hannes Reinecke <hare@xxxxxxxxxx>
---
drivers/nvme/host/fabrics.c | 48 +++++++++++++++++++++++++++++++++------------
drivers/nvme/target/auth.c | 39 +++++++++++++++++++++++++++---------
2 files changed, 66 insertions(+), 21 deletions(-)
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 12a30ed16f9ef32c74a51a0ae6ff8ce0ea628301..e7f51474ab5e1f67fca21d84c4bddcbe9e557571 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -1115,7 +1115,17 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- pr_debug("using dhchap host key %08x\n", key_serial(key));
+ if (!opts->dhchap_key_generated) {
+ pr_debug("linking dhchap host key %08x\n", key_serial(key));
+ ret = nvme_keyring_link(key);
+ if (ret) {
+ pr_debug("failed to link dhchap host key %08x\n",
+ key_serial(key));
+ key_put(key);
+ goto out;
+ }
+ } else
+ pr_debug("using dhchap host key %08x\n", key_serial(key));
opts->dhchap_key = key_serial(key);
key_put(key);
#endif
@@ -1133,7 +1143,17 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
+ if (!opts->dhchap_ctrl_key_generated) {
+ pr_debug("linking dhchap ctrl key %08x\n", key_serial(key));
+ ret = nvme_keyring_link(key);
+ if (ret) {
+ pr_warn("failed to link dhchap ctrl key %08x\n",
+ key_serial(key));
+ key_put(key);
+ goto out;
+ }
+ } else
+ pr_debug("using dhchap ctrl key %08x\n", key_serial(key));
opts->dhchap_ctrl_key = key_serial(key);
key_put(key);
#endif
@@ -1346,23 +1366,27 @@ void nvmf_free_options(struct nvmf_ctrl_options *opts)
struct key *key;
nvmf_host_put(opts->host);
- if (opts->dhchap_key &&
- opts->dhchap_key_generated) {
+ if (opts->dhchap_key) {
key = key_lookup(opts->dhchap_key);
if (!IS_ERR(key)) {
- pr_debug("revoke dhchap host key %08x\n",
- opts->dhchap_key);
- key_revoke(key);
+ if (opts->dhchap_key_generated) {
+ pr_debug("revoke dhchap host key %08x\n",
+ opts->dhchap_key);
+ key_revoke(key);
+ } else
+ nvme_keyring_unlink(key);
key_put(key);
}
}
- if (opts->dhchap_ctrl_key &&
- opts->dhchap_ctrl_key_generated) {
+ if (opts->dhchap_ctrl_key) {
key = key_lookup(opts->dhchap_ctrl_key);
if (!IS_ERR(key)) {
- pr_debug("revoke dhchap ctrl key %08x\n",
- opts->dhchap_ctrl_key);
- key_revoke(key);
+ if (opts->dhchap_ctrl_key_generated) {
+ pr_debug("revoke dhchap ctrl key %08x\n",
+ opts->dhchap_ctrl_key);
+ key_revoke(key);
+ } else
+ nvme_keyring_unlink(key);
key_put(key);
}
}
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 54e79c8a7cae67a509a9f6b993b7886e706af8e0..04a9177ffa1f48445b54f0b6ec3f63e7c111fe06 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -19,19 +19,20 @@
#include "nvmet.h"
-static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
+static int nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
struct key *new_key, bool new_generated)
{
key_serial_t old_key_id = 0, new_key_id = key_serial(new_key);
struct key *key = NULL;
bool generated = false;
+ int ret;
if (set_ctrl) {
if (host->dhchap_ctrl_key) {
if (host->dhchap_ctrl_key == new_key_id) {
if (new_key && new_generated)
key_revoke(new_key);
- return;
+ return 0;
}
old_key_id = host->dhchap_ctrl_key;
generated = host->dhchap_ctrl_key_generated;
@@ -44,7 +45,7 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
if (host->dhchap_key == new_key_id) {
if (new_key && new_generated)
key_revoke(new_key);
- return;
+ return 0;
}
old_key_id = host->dhchap_key;
generated = host->dhchap_key_generated;
@@ -53,18 +54,37 @@ static void nvmet_auth_update_key(struct nvmet_host *host, bool set_ctrl,
host->dhchap_key = new_key_id;
host->dhchap_key_generated = new_generated;
}
+ if (new_key && !new_generated) {
+ ret = nvme_keyring_link(new_key);
+ if (ret < 0) {
+ pr_warn("%s: failed to link %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(new_key));
+ return ret;
+ }
+ }
+
if (!old_key_id)
- return;
+ return 0;
key = key_lookup(old_key_id);
if (IS_ERR(key))
- return;
+ return PTR_ERR(key);
if (generated) {
pr_debug("%s: revoke %s key %08x\n", __func__,
set_ctrl ? "ctrl" : "host", key_serial(key));
key_revoke(key);
+ ret = 0;
+ } else {
+ ret = nvme_keyring_unlink(key);
+ if (ret < 0) {
+ pr_warn("%s: failed to unlink %s key %08x\n",
+ __func__, set_ctrl ? "ctrl" : "host",
+ key_serial(new_key));
+ }
}
key_put(key);
+ return ret;
}
void nvmet_auth_clear_key(struct nvmet_host *host)
@@ -79,12 +99,13 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
bool generated = false;
struct key *key;
size_t len;
+ int ret;
if (!secret || !strlen(secret)) {
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, NULL, false);
+ ret = nvmet_auth_update_key(host, set_ctrl, NULL, false);
up_write(&nvmet_config_sem);
- return 0;
+ return ret;
}
len = strcspn(secret, "\n");
key = nvme_auth_extract_key(secret, len, &generated);
@@ -102,10 +123,10 @@ int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
}
up_read(&key->sem);
down_write(&nvmet_config_sem);
- nvmet_auth_update_key(host, set_ctrl, key, generated);
+ ret = nvmet_auth_update_key(host, set_ctrl, key, generated);
up_write(&nvmet_config_sem);
key_put(key);
- return 0;
+ return ret;
}
int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id)
--
2.51.0