From: Hannes Reinecke <hare@kernel.org>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
Hannes Reinecke <hare@suse.de>,
Chaitanya Kulkarni <kch@nvidia.com>,
Eric Biggers <ebiggers@kernel.org>,
David Howells <dhowells@redhat.com>,
Jarkko Sakkinen <jarkko@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Kees Cook <kees@kernel.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org,
keyrings@vger.kernel.org, linux-doc@vger.kernel.org,
linux-hardening@vger.kernel.org,
Hannes Reinecke <hare@kernel.org>
Subject: [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring
Date: Thu, 26 Nov 2026 15:12:23 +0100 [thread overview]
Message-ID: <20261126-dhchap-keyring-v4-v7-5-ee438ffea527@kernel.org> (raw)
In-Reply-To: <20261126-dhchap-keyring-v4-v7-0-ee438ffea527@kernel.org>
Rather than switching to a different keyring (and trying
to deal with possibly restricted access to that keyring)
we should link the additional keyring into the default .nvme
keyring to make the keys available for lookup for
the lifetime of the corresponding nvme structures.
Signed-off-by: Hannes Reinecke <hare@kernel.org>
---
drivers/nvme/common/keyring.c | 63 ++++++++++++++++++++++++++-----------------
drivers/nvme/host/auth.c | 3 +--
drivers/nvme/host/fabrics.c | 13 +++++++--
drivers/nvme/host/sysfs.c | 3 +++
drivers/nvme/host/tcp.c | 3 +--
drivers/nvme/target/auth.c | 2 +-
include/linux/nvme-keyring.h | 30 +++++++++++++++------
7 files changed, 77 insertions(+), 40 deletions(-)
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 373f51e1032a4d29ef945a62fd9fb3720f8a9ea8..0fc6b4825c2dd9a23daaf4aae3d9f63f4f07c164 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -22,6 +22,24 @@ key_serial_t nvme_keyring_id(void)
}
EXPORT_SYMBOL_GPL(nvme_keyring_id);
+const char *nvme_keyring_name(void)
+{
+ return nvme_keyring->description;
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_name);
+
+int nvme_keyring_link(struct key *keyring)
+{
+ return key_link(nvme_keyring, keyring);
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_link);
+
+int nvme_keyring_unlink(struct key *keyring)
+{
+ return key_unlink(nvme_keyring, keyring);
+}
+EXPORT_SYMBOL_GPL(nvme_keyring_unlink);
+
struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
struct key *key = key_lookup(key_id);
@@ -88,14 +106,12 @@ static struct key_type nvme_tls_psk_key_type = {
.read = user_read,
};
-static struct key *nvme_tls_psk_lookup(struct key *keyring,
- const char *hostnqn, const char *subnqn,
+static struct key *nvme_tls_psk_lookup(const char *hostnqn, const char *subnqn,
u8 hmac, u8 psk_ver, bool generated)
{
char *identity;
size_t identity_len = (NVMF_NQN_SIZE) * 2 + 11;
key_ref_t keyref;
- key_serial_t keyring_id;
identity = kzalloc(identity_len, GFP_KERNEL);
if (!identity)
@@ -104,12 +120,8 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
snprintf(identity, identity_len, "NVMe%u%c%02u %s %s",
psk_ver, generated ? 'G' : 'R', hmac, hostnqn, subnqn);
- if (!keyring)
- keyring = nvme_keyring;
- keyring_id = key_serial(keyring);
- pr_debug("keyring %x lookup tls psk '%s'\n",
- keyring_id, identity);
- keyref = keyring_search(make_key_ref(keyring, true),
+ pr_debug("lookup tls psk '%s'\n", identity);
+ keyref = keyring_search(make_key_ref(nvme_keyring, true),
&nvme_tls_psk_key_type,
identity, false);
if (IS_ERR(keyref)) {
@@ -125,7 +137,6 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
/**
* nvme_tls_psk_refresh - Refresh TLS PSK
- * @keyring: Keyring holding the TLS PSK
* @hostnqn: Host NQN to use
* @subnqn: Subsystem NQN to use
* @hmac_id: Hash function identifier
@@ -139,9 +150,8 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,
*
* Returns the updated key success or an error pointer otherwise.
*/
-struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, const char *subnqn, u8 hmac_id,
- u8 *data, size_t data_len, const char *digest)
+struct key *nvme_tls_psk_refresh(const char *hostnqn, const char *subnqn,
+ u8 hmac_id, u8 *data, size_t data_len, const char *digest)
{
key_perm_t keyperm =
KEY_POS_SEARCH | KEY_POS_VIEW | KEY_POS_READ |
@@ -149,7 +159,6 @@ struct key *nvme_tls_psk_refresh(struct key *keyring,
KEY_USR_SEARCH | KEY_USR_VIEW | KEY_USR_READ;
char *identity;
key_ref_t keyref;
- key_serial_t keyring_id;
struct key *key;
if (!hostnqn || !subnqn || !data || !data_len)
@@ -160,12 +169,8 @@ struct key *nvme_tls_psk_refresh(struct key *keyring,
if (!identity)
return ERR_PTR(-ENOMEM);
- if (!keyring)
- keyring = nvme_keyring;
- keyring_id = key_serial(keyring);
- pr_debug("keyring %x refresh tls psk '%s'\n",
- keyring_id, identity);
- keyref = key_create_or_update(make_key_ref(keyring, true),
+ pr_debug("refresh tls psk '%s'\n", identity);
+ keyref = key_create_or_update(make_key_ref(nvme_keyring, true),
"psk", identity, data, data_len,
keyperm, KEY_ALLOC_NOT_IN_QUOTA |
KEY_ALLOC_BUILT_IN |
@@ -226,10 +231,18 @@ static struct nvme_tls_psk_priority_list {
};
/*
- * nvme_tls_psk_default - Return the preferred PSK to use for TLS ClientHello
+ * nvme_tls_psk_default - Select the preferred PSK to use for TLS ClientHello
+ * @hostnqn: Host NQN of the NVME TLS connection
+ * @subnqn: Subsystem NQN of the NVME TLS connection
+ *
+ * Looks up the preferred PSK to be used for the TLC ClientHello.
+ * The preferred PSK is the first matching PSK from the nvme_tls_psk_prio
+ * array.
+ *
+ * Returns: the key serial number of the preferred PSK or 0 if no matchine
+ * PSK was found.
*/
-key_serial_t nvme_tls_psk_default(struct key *keyring,
- const char *hostnqn, const char *subnqn)
+key_serial_t nvme_tls_psk_default(const char *hostnqn, const char *subnqn)
{
struct key *tls_key;
key_serial_t tls_key_id;
@@ -240,8 +253,8 @@ key_serial_t nvme_tls_psk_default(struct key *keyring,
u8 ver = nvme_tls_psk_prio[prio].psk_ver;
enum nvme_tcp_tls_cipher cipher = nvme_tls_psk_prio[prio].cipher;
- tls_key = nvme_tls_psk_lookup(keyring, hostnqn, subnqn,
- cipher, ver, generated);
+ tls_key = nvme_tls_psk_lookup(hostnqn, subnqn, cipher,
+ ver, generated);
if (!IS_ERR(tls_key)) {
tls_key_id = tls_key->serial;
key_put(tls_key);
diff --git a/drivers/nvme/host/auth.c b/drivers/nvme/host/auth.c
index 295e0a9533c8995c32781324650d73b6bfedbf13..06399c075405353325b05c099811a54134fbeb53 100644
--- a/drivers/nvme/host/auth.c
+++ b/drivers/nvme/host/auth.c
@@ -726,8 +726,7 @@ static int nvme_auth_secure_concat(struct nvme_ctrl *ctrl,
goto out_free_digest;
}
- tls_key = nvme_tls_psk_refresh(ctrl->opts->keyring,
- ctrl->opts->host->nqn,
+ tls_key = nvme_tls_psk_refresh(ctrl->opts->host->nqn,
ctrl->opts->subsysnqn, chap->hash_id,
tls_psk, psk_len, digest);
if (IS_ERR(tls_key)) {
diff --git a/drivers/nvme/host/fabrics.c b/drivers/nvme/host/fabrics.c
index 5ed91557f07065a284e664950a9c71d771614ec2..6a918d72d14c5dd92d00191c184e72b0e5614b7a 100644
--- a/drivers/nvme/host/fabrics.c
+++ b/drivers/nvme/host/fabrics.c
@@ -995,7 +995,11 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
ret = PTR_ERR(key);
goto out;
}
- key_put(opts->keyring);
+ ret = nvme_keyring_link(opts->keyring);
+ if (ret) {
+ key_put(key);
+ goto out;
+ }
opts->keyring = key;
break;
case NVMF_OPT_TLS_KEY:
@@ -1296,7 +1300,12 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,
void nvmf_free_options(struct nvmf_ctrl_options *opts)
{
nvmf_host_put(opts->host);
- key_put(opts->keyring);
+ if (opts->keyring) {
+ int ret = nvme_keyring_unlink(opts->keyring);
+
+ WARN_ON(ret);
+ key_put(opts->keyring);
+ }
key_put(opts->tls_key);
kfree(opts->transport);
kfree(opts->traddr);
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index 35f871e3e0c0ae49b390f0fb64c538796207a98f..c1375f23a97f6b5a47bb8aad6b12e7ff3ad12638 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -6,6 +6,7 @@
*/
#include <linux/nvme-auth.h>
+#include <linux/nvme-keyring.h>
#include <linux/blkdev.h>
#include "nvme.h"
@@ -1069,6 +1070,8 @@ static ssize_t tls_keyring_show(struct device *dev,
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
struct key *keyring = ctrl->opts->keyring;
+ if (!keyring)
+ return sysfs_emit(buf, "%s\n", nvme_keyring_name());
return sysfs_emit(buf, "%s\n", keyring->description);
}
static DEVICE_ATTR_RO(tls_keyring);
diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 62a594b8ab9bfa51a6f47b10ef2400839f9dd18e..585aba1d544518d6e712e23c7987741b130aee4d 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -2416,8 +2416,7 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
if (ctrl->opts->tls_key)
pskid = key_serial(ctrl->opts->tls_key);
else if (ctrl->opts->tls) {
- pskid = nvme_tls_psk_default(ctrl->opts->keyring,
- ctrl->opts->host->nqn,
+ pskid = nvme_tls_psk_default(ctrl->opts->host->nqn,
ctrl->opts->subsysnqn);
if (!pskid) {
dev_err(ctrl->device, "no valid PSK found\n");
diff --git a/drivers/nvme/target/auth.c b/drivers/nvme/target/auth.c
index 150818d7009d8c2fca96e12fd54fdc1cb6b68882..7ebd867c89468e94e31fd2739d3ee77018775f18 100644
--- a/drivers/nvme/target/auth.c
+++ b/drivers/nvme/target/auth.c
@@ -523,7 +523,7 @@ void nvmet_auth_insert_psk(struct nvmet_sq *sq)
goto out_free_digest;
}
#ifdef CONFIG_NVME_TARGET_TCP_TLS
- tls_key = nvme_tls_psk_refresh(NULL, sq->ctrl->hostnqn,
+ tls_key = nvme_tls_psk_refresh(sq->ctrl->hostnqn,
sq->ctrl->subsys->subsysnqn,
sq->ctrl->shash_id, tls_psk, psk_len,
digest);
diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
index ab8971afa9734a276d9197076b780321460dddc2..680eb93088d485ca3368f615715d71fa570b1077 100644
--- a/include/linux/nvme-keyring.h
+++ b/include/linux/nvme-keyring.h
@@ -10,20 +10,22 @@
#if IS_ENABLED(CONFIG_NVME_KEYRING)
-struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, const char *subnqn, u8 hmac_id,
- u8 *data, size_t data_len, const char *digest);
-key_serial_t nvme_tls_psk_default(struct key *keyring,
- const char *hostnqn, const char *subnqn);
+struct key *nvme_tls_psk_refresh(const char *hostnqn, const char *subnqn,
+ u8 hmac_id, u8 *data, size_t data_len, const char *digest);
+key_serial_t nvme_tls_psk_default(const char *hostnqn, const char *subnqn);
key_serial_t nvme_keyring_id(void);
+const char *nvme_keyring_name(void);
+int nvme_keyring_link(struct key *keyring);
+int nvme_keyring_unlink(struct key *keyring);
struct key *nvme_tls_key_lookup(key_serial_t key_id);
+
#else
static inline struct key *nvme_tls_psk_refresh(struct key *keyring,
- const char *hostnqn, char *subnqn, u8 hmac_id,
+ const char *hostnqn, const char *subnqn, u8 hmac_id,
u8 *data, size_t data_len, const char *digest)
{
- return ERR_PTR(-ENOTSUPP);
+ return ERR_PTR(-EOPNOTSUPP);
}
static inline key_serial_t nvme_tls_psk_default(struct key *keyring,
const char *hostnqn, const char *subnqn)
@@ -34,9 +36,21 @@ static inline key_serial_t nvme_keyring_id(void)
{
return 0;
}
+static const char *nvme_keyring_name(void)
+{
+ return NULL;
+}
+static int nvme_keyring_link(struct key *keyring)
+{
+ return 0;
+}
+int nvme_keyring_unlink(struct key *keyring)
+{
+ return 0;
+}
static inline struct key *nvme_tls_key_lookup(key_serial_t key_id)
{
- return ERR_PTR(-ENOTSUPP);
+ return ERR_PTR(-EOPNOTSUPP);
}
#endif /* !CONFIG_NVME_KEYRING */
#endif /* _NVME_KEYRING_H */
--
2.51.0
next prev parent reply other threads:[~2026-11-26 14:13 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 14:12 [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 01/14] nvme-auth: modify nvme_auth_transform_key() to return status Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 02/14] nvme-keyring: add 'dhchap' key type Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 03/14] nvme-keyring: drop nvme_tls_psk_revoked() Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 04/14] Documentation/security/keys: fixup function name Hannes Reinecke
2026-11-26 14:12 ` Hannes Reinecke [this message]
2026-11-26 19:49 ` [PATCH v7 05/14] nvme-keyring: link addtional keyring into .nvme keyring kernel test robot
2026-11-26 20:22 ` kernel test robot
2026-11-26 14:12 ` [PATCH v7 06/14] nvme-auth: switch to use 'struct key' Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 07/14] nvme: parse dhchap keys during option parsing Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 08/14] nvmet-auth: parse dhchap key from configfs attribute Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 09/14] nvme: allow to pass in key description as dhchap secret Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 10/14] nvme-auth: allow to specify the key by key serial Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 11/14] nvme-auth: wait for authentication to finish when changing keys Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 12/14] nvme-fabrics: allow to pass in keyring by name Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 13/14] nvme-auth: link pre-provisioned keys into .nvme keyring Hannes Reinecke
2026-11-26 14:12 ` [PATCH v7 14/14] nvme-auth: allow to update pre-provisioned keys Hannes Reinecke
2026-11-26 21:15 ` kernel test robot
2026-11-26 16:00 ` [PATCH v7 00/14] nvme-auth: switch to use the kernel keyring Jarkko Sakkinen
2026-11-26 16:03 ` Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-dhchap-keyring-v4-v7-5-ee438ffea527@kernel.org \
--to=hare@kernel.org \
--cc=axboe@kernel.dk \
--cc=corbet@lwn.net \
--cc=dhowells@redhat.com \
--cc=ebiggers@kernel.org \
--cc=gustavoars@kernel.org \
--cc=hare@suse.de \
--cc=hch@lst.de \
--cc=jarkko@kernel.org \
--cc=kbusch@kernel.org \
--cc=kch@nvidia.com \
--cc=kees@kernel.org \
--cc=keyrings@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=rdunlap@infradead.org \
--cc=sagi@grimberg.me \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®