Linux Security Modules development
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: keyrings@vger.kernel.org
Cc: "Jarkko Sakkinen" <jarkko@kernel.org>,
	linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org,
	linux-s390@vger.kernel.org,
	linux-security-module@vger.kernel.org, netdev@vger.kernel.org,
	samba-technical@lists.samba.org,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Anna Schumaker" <anna@kernel.org>,
	"Bharath SM" <bharathsm@microsoft.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>,
	"David Howells" <dhowells@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Heiko Carstens" <hca@linux.ibm.com>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"James Bottomley" <James.Bottomley@HansenPartnership.com>,
	"James Morris" <jmorris@namei.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Marc Dionne" <marc.dionne@auristor.com>,
	"Marek Behún" <kabel@kernel.org>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Namjae Jeon" <linkinjeon@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"Paulo Alcantara" <pc@manguebit.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Ronnie Sahlberg" <ronniesahlberg@gmail.com>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Shyam Prasad N" <sprasad@microsoft.com>,
	"Simon Horman" <horms@kernel.org>,
	"Sumit Garg" <sumit.garg@kernel.org>,
	"Sven Schnelle" <svens@linux.ibm.com>,
	"Tom Talpey" <tom@talpey.com>,
	"Trond Myklebust" <trondmy@kernel.org>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Willem de Bruijn" <willemb@google.com>
Subject: [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module
Date: Sun, 29 Nov 2026 19:05:39 +0200	[thread overview]
Message-ID: <20261129170543.2915157-11-jarkko@kernel.org> (raw)
In-Reply-To: <20261129170543.2915157-1-jarkko@kernel.org>

Once keys pin the kernel module of their key type, hosting the psk key
type in the same module as the .nvme keyring creates a circular
reference whenever a PSK key is linked into .nvme, preventing
nvme-keyring.ko from unloading.

Split nvme_tls_psk_key_type into nvme-psk-key.ko, selected by
CONFIG_NVME_KEYRING, and clear and flush the .nvme keyring on
nvme-keyring module exit.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/nvme/common/Kconfig   |  9 ++++-
 drivers/nvme/common/Makefile  |  2 +
 drivers/nvme/common/keyring.c | 68 ++++----------------------------
 drivers/nvme/common/psk_key.c | 74 +++++++++++++++++++++++++++++++++++
 include/linux/nvme-keyring.h  |  4 ++
 5 files changed, 95 insertions(+), 62 deletions(-)
 create mode 100644 drivers/nvme/common/psk_key.c

diff --git a/drivers/nvme/common/Kconfig b/drivers/nvme/common/Kconfig
index f1639db65fd3..8ac6050ee53a 100644
--- a/drivers/nvme/common/Kconfig
+++ b/drivers/nvme/common/Kconfig
@@ -1,8 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
+config NVME_PSK_KEY
+	tristate
+	select KEYS
+
 config NVME_KEYRING
-       tristate
-       select KEYS
+	tristate
+	select KEYS
+	select NVME_PSK_KEY
 
 config NVME_AUTH
 	tristate
diff --git a/drivers/nvme/common/Makefile b/drivers/nvme/common/Makefile
index fd9d01a60946..a9d00212a104 100644
--- a/drivers/nvme/common/Makefile
+++ b/drivers/nvme/common/Makefile
@@ -3,9 +3,11 @@
 ccflags-y			+= -I$(src)
 
 obj-$(CONFIG_NVME_AUTH)		+= nvme-auth.o
+obj-$(CONFIG_NVME_PSK_KEY)	+= nvme-psk-key.o
 obj-$(CONFIG_NVME_KEYRING)	+= nvme-keyring.o
 
 nvme-auth-y			+= auth.o
+nvme-psk-key-y			+= psk_key.o
 nvme-keyring-y			+= keyring.o
 
 obj-$(CONFIG_NVME_AUTH_KUNIT_TEST) += tests/auth_kunit.o
diff --git a/drivers/nvme/common/keyring.c b/drivers/nvme/common/keyring.c
index 87cb51e1f5a7..62f9a1be11de 100644
--- a/drivers/nvme/common/keyring.c
+++ b/drivers/nvme/common/keyring.c
@@ -4,8 +4,8 @@
  */
 
 #include <linux/module.h>
-#include <linux/seq_file.h>
 #include <linux/key-type.h>
+#include <linux/keyctl.h>
 #include <keys/user-type.h>
 #include <linux/nvme.h>
 #include <linux/nvme-tcp.h>
@@ -42,53 +42,6 @@ struct key *nvme_tls_key_lookup(key_serial_t key_id)
 }
 EXPORT_SYMBOL_GPL(nvme_tls_key_lookup);
 
-static void nvme_tls_psk_describe(const struct key *key, struct seq_file *m)
-{
-	seq_puts(m, key->description);
-	seq_printf(m, ": %u", key->datalen);
-}
-
-static bool nvme_tls_psk_match(const struct key *key,
-			       const struct key_match_data *match_data)
-{
-	const char *match_id;
-	size_t match_len;
-
-	if (!key->description) {
-		pr_debug("%s: no key description\n", __func__);
-		return false;
-	}
-	if (!match_data->raw_data) {
-		pr_debug("%s: no match data\n", __func__);
-		return false;
-	}
-	match_id = match_data->raw_data;
-	match_len = strlen(match_id);
-	pr_debug("%s: match '%s' '%s' len %zd\n",
-		 __func__, match_id, key->description, match_len);
-	return !memcmp(key->description, match_id, match_len);
-}
-
-static int nvme_tls_psk_match_preparse(struct key_match_data *match_data)
-{
-	match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
-	match_data->cmp = nvme_tls_psk_match;
-	return 0;
-}
-
-static struct key_type nvme_tls_psk_key_type = {
-	.name           = "psk",
-	.flags          = KEY_TYPE_NET_DOMAIN,
-	.preparse       = user_preparse,
-	.free_preparse  = user_free_preparse,
-	.match_preparse = nvme_tls_psk_match_preparse,
-	.instantiate    = generic_key_instantiate,
-	.revoke         = user_revoke,
-	.destroy        = user_destroy,
-	.describe       = nvme_tls_psk_describe,
-	.read           = user_read,
-};
-
 static struct key *nvme_tls_psk_lookup(struct key *keyring,
 		const char *hostnqn, const char *subnqn,
 		u8 hmac, u8 psk_ver, bool generated)
@@ -255,30 +208,25 @@ EXPORT_SYMBOL_GPL(nvme_tls_psk_default);
 
 static int __init nvme_keyring_init(void)
 {
-	int err;
-
 	nvme_keyring = keyring_alloc(".nvme",
 				     GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
 				     current_cred(),
 				     (KEY_POS_ALL & ~KEY_POS_SETATTR) |
 				     (KEY_USR_ALL & ~KEY_USR_SETATTR),
 				     KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
-	if (IS_ERR(nvme_keyring))
-		return PTR_ERR(nvme_keyring);
-
-	err = register_key_type(&nvme_tls_psk_key_type);
-	if (err) {
-		key_put(nvme_keyring);
-		return err;
-	}
-	return 0;
+	return PTR_ERR_OR_ZERO(nvme_keyring);
 }
 
 static void __exit nvme_keyring_exit(void)
 {
-	unregister_key_type(&nvme_tls_psk_key_type);
+	int ret;
+
+	ret = keyring_clear(nvme_keyring);
 	key_revoke(nvme_keyring);
 	key_put(nvme_keyring);
+	if (ret)
+		key_gc_flush(&key_type_keyring);
+	key_gc_flush(&nvme_tls_psk_key_type);
 }
 
 MODULE_LICENSE("GPL v2");
diff --git a/drivers/nvme/common/psk_key.c b/drivers/nvme/common/psk_key.c
new file mode 100644
index 000000000000..355a53629069
--- /dev/null
+++ b/drivers/nvme/common/psk_key.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2023 Hannes Reinecke, SUSE Labs
+ */
+
+#include <linux/module.h>
+#include <linux/seq_file.h>
+#include <linux/key-type.h>
+#include <keys/user-type.h>
+#include <linux/nvme-keyring.h>
+
+static void nvme_tls_psk_describe(const struct key *key, struct seq_file *m)
+{
+	seq_puts(m, key->description);
+	seq_printf(m, ": %u", key->datalen);
+}
+
+static bool nvme_tls_psk_match(const struct key *key,
+			       const struct key_match_data *match_data)
+{
+	const char *match_id;
+	size_t match_len;
+
+	if (!key->description) {
+		pr_debug("%s: no key description\n", __func__);
+		return false;
+	}
+	if (!match_data->raw_data) {
+		pr_debug("%s: no match data\n", __func__);
+		return false;
+	}
+	match_id = match_data->raw_data;
+	match_len = strlen(match_id);
+	pr_debug("%s: match '%s' '%s' len %zd\n",
+		 __func__, match_id, key->description, match_len);
+	return !memcmp(key->description, match_id, match_len);
+}
+
+static int nvme_tls_psk_match_preparse(struct key_match_data *match_data)
+{
+	match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
+	match_data->cmp = nvme_tls_psk_match;
+	return 0;
+}
+
+struct key_type nvme_tls_psk_key_type = {
+	.name           = "psk",
+	.flags          = KEY_TYPE_NET_DOMAIN,
+	.preparse       = user_preparse,
+	.free_preparse  = user_free_preparse,
+	.match_preparse = nvme_tls_psk_match_preparse,
+	.instantiate    = generic_key_instantiate,
+	.revoke         = user_revoke,
+	.destroy        = user_destroy,
+	.describe       = nvme_tls_psk_describe,
+	.read           = user_read,
+};
+EXPORT_SYMBOL_GPL(nvme_tls_psk_key_type);
+
+static int __init nvme_psk_key_init(void)
+{
+	return register_key_type(&nvme_tls_psk_key_type);
+}
+
+static void __exit nvme_psk_key_exit(void)
+{
+	unregister_key_type(&nvme_tls_psk_key_type);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
+MODULE_DESCRIPTION("NVMe TLS PSK key type");
+module_init(nvme_psk_key_init);
+module_exit(nvme_psk_key_exit);
diff --git a/include/linux/nvme-keyring.h b/include/linux/nvme-keyring.h
index ab8971afa973..803cbaaf0b6d 100644
--- a/include/linux/nvme-keyring.h
+++ b/include/linux/nvme-keyring.h
@@ -8,6 +8,10 @@
 
 #include <linux/key.h>
 
+#if IS_ENABLED(CONFIG_NVME_PSK_KEY)
+extern struct key_type nvme_tls_psk_key_type;
+#endif
+
 #if IS_ENABLED(CONFIG_NVME_KEYRING)
 
 struct key *nvme_tls_psk_refresh(struct key *keyring,
-- 
2.47.3


  parent reply	other threads:[~2026-11-29 17:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 01/14] KEYS: Move key garbage collection to a dedicated workqueue Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 02/14] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 03/14] KEYS: Add key_gc_flush() for draining RCU callbacks Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 04/14] dns_resolver: Move the key type into a separate module Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 06/14] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 08/14] rxrpc: Release conn->key when init_connection_security() fails Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 09/14] rxrpc: Move key types to their own module Jarkko Sakkinen
2026-11-29 17:05 ` Jarkko Sakkinen [this message]
2026-11-29 17:05 ` [PATCH v7 11/14] platform/cznic: turris-signing-key: Flush key GC on key release Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 12/14] KEYS: trusted: Unregister key type on module exit in TEE backend Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 14/14] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-29 17:11 ` [PATCH v7 00/14] KEYS: Drop key_type_dead 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=20261129170543.2915157-11-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=agordeev@linux.ibm.com \
    --cc=anna@kernel.org \
    --cc=bharathsm@microsoft.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=edumazet@kernel.org \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=horms@kernel.org \
    --cc=jmorris@namei.org \
    --cc=kabel@kernel.org \
    --cc=keyrings@vger.kernel.org \
    --cc=kuba@kernel.org \
    --cc=linkinjeon@kernel.org \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=marc.dionne@auristor.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=pc@manguebit.org \
    --cc=rdunlap@infradead.org \
    --cc=ronniesahlberg@gmail.com \
    --cc=samba-technical@lists.samba.org \
    --cc=serge@hallyn.com \
    --cc=skhan@linuxfoundation.org \
    --cc=sprasad@microsoft.com \
    --cc=sumit.garg@kernel.org \
    --cc=svens@linux.ibm.com \
    --cc=tom@talpey.com \
    --cc=trondmy@kernel.org \
    --cc=willemb@google.com \
    --cc=zohar@linux.ibm.com \
    /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