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 13/14] KEYS: Pin the backing kernel module for a key type
Date: Sun, 29 Nov 2026 19:05:42 +0200 [thread overview]
Message-ID: <20261129170543.2915157-14-jarkko@kernel.org> (raw)
In-Reply-To: <20261129170543.2915157-1-jarkko@kernel.org>
Add the owner field (populated with THIS_MODULE) to struct key_type.
Pin the module in key_alloc() and keyring_restrict(), and release both
key and restriction module references in key_gc_unused_keys() under RCU
read lock after freeing the key.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
include/linux/key-type.h | 6 ++++-
security/keys/gc.c | 20 ++++++++++++++--
security/keys/key.c | 49 ++++++++++++++++++++++++++++------------
security/keys/keyring.c | 33 +++++++++++++++++++++------
4 files changed, 84 insertions(+), 24 deletions(-)
diff --git a/include/linux/key-type.h b/include/linux/key-type.h
index 668793bf613a..0fb2276b8f61 100644
--- a/include/linux/key-type.h
+++ b/include/linux/key-type.h
@@ -10,9 +10,11 @@
#include <linux/key.h>
#include <linux/errno.h>
+#include <linux/init.h>
#ifdef CONFIG_KEYS
+struct module;
struct kernel_pkey_query;
struct kernel_pkey_params;
@@ -164,13 +166,15 @@ struct key_type {
atomic64_t index_id; /* Opaque keyring index ID */
atomic_t nr_keys; /* Number of allocated keys */
atomic_t gc_pending; /* Unreferenced keys pending GC */
+ struct module *owner;
struct list_head link; /* link in types list */
struct lock_class_key lock_class; /* key->sem lock class */
} __randomize_layout;
extern struct key_type key_type_keyring;
-extern int register_key_type(struct key_type *ktype);
+int __register_key_type(struct key_type *ktype, struct module *owner);
+#define register_key_type(ktype) __register_key_type(ktype, THIS_MODULE)
extern void unregister_key_type(struct key_type *ktype);
extern int key_payload_reserve(struct key *key, size_t datalen);
diff --git a/security/keys/gc.c b/security/keys/gc.c
index ca5c1a6bdd45..eaddd1bab37c 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -7,6 +7,7 @@
#include <linux/mutex.h>
#include <linux/slab.h>
+#include <linux/module.h>
#include <linux/security.h>
#include <keys/keyring-type.h>
#include "internal.h"
@@ -169,8 +170,11 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
struct key *key =
list_entry(keys->next, struct key, graveyard_link);
struct key_type *type = key->type;
+ struct module *owner = type->owner;
struct key_type *restrict_type =
key->restrict_link ? key->restrict_link->keytype : NULL;
+ struct module *restrict_owner =
+ restrict_type ? restrict_type->owner : NULL;
short state = key->state;
list_del(&key->graveyard_link);
@@ -184,8 +188,13 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
#endif
/* Throw away the key data if the key is instantiated */
- if (state == KEY_IS_POSITIVE && type->destroy)
- type->destroy(key);
+ if (state == KEY_IS_POSITIVE) {
+ if (type->destroy)
+ type->destroy(key);
+ } else if (key->restrict_link) {
+ key_put(key->restrict_link->key);
+ kfree(key->restrict_link);
+ }
security_key_free(key);
@@ -199,12 +208,18 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
memzero_explicit(key, sizeof(*key));
kmem_cache_free(key_jar, key);
+
+ /* Keep types mapped across free_module()'s synchronize_rcu(). */
+ rcu_read_lock();
if (restrict_type) {
+ module_put(restrict_owner);
atomic_dec_return_release(&restrict_type->nr_keys);
atomic_dec_return_release(&restrict_type->gc_pending);
}
+ module_put(owner);
atomic_dec_return_release(&type->nr_keys);
atomic_dec_return_release(&type->gc_pending);
+ rcu_read_unlock();
}
}
@@ -554,6 +569,7 @@ static void key_garbage_collector(struct work_struct *work)
kdebug("destroy key %d", key->serial);
down_write(&key->sem);
atomic_inc(&key_type_dead.nr_keys);
+ module_put(key_gc_dead_keytype->owner);
atomic_dec_return_release(&key_gc_dead_keytype->nr_keys);
key->type = &key_type_dead;
if (key_gc_dead_keytype->destroy)
diff --git a/security/keys/key.c b/security/keys/key.c
index fecf8c3a991c..dff8f02a262c 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -7,6 +7,7 @@
#include <linux/export.h>
#include <linux/init.h>
+#include <linux/module.h>
#include <linux/poison.h>
#include <linux/sched.h>
#include <linux/slab.h>
@@ -234,21 +235,32 @@ struct key *key_alloc(struct key_type *type, const char *desc,
key = ERR_PTR(-EINVAL);
if (!desc || !*desc)
- goto error;
+ goto out;
if (restrict_link && type != &key_type_keyring)
- goto error;
+ goto out;
+
+ if (!try_module_get(type->owner)) {
+ key = ERR_PTR(-ENOENT);
+ goto out;
+ }
+
+ if (restrict_link && restrict_link->keytype &&
+ !try_module_get(restrict_link->keytype->owner)) {
+ key = ERR_PTR(-ENOENT);
+ goto out_put_module;
+ }
if (type->vet_description) {
ret = type->vet_description(desc);
if (ret < 0) {
key = ERR_PTR(ret);
- goto error;
+ goto out_put_restrict_module;
}
}
desclen = strlen(desc);
if (desclen >= KEY_MAX_DESC_SIZE)
- goto error;
+ goto out_put_restrict_module;
quotalen = desclen + 1 + type->def_datalen;
/* get hold of the key tracking for this user */
@@ -328,8 +340,6 @@ struct key *key_alloc(struct key_type *type, const char *desc,
if (restrict_link && restrict_link->keytype)
atomic_inc(&restrict_link->keytype->nr_keys);
key_alloc_serial(key);
-
-error:
return key;
security_error:
@@ -343,7 +353,7 @@ struct key *key_alloc(struct key_type *type, const char *desc,
}
key_user_put(user);
key = ERR_PTR(ret);
- goto error;
+ goto out_put_restrict_module;
no_memory_3:
kmem_cache_free(key_jar, key);
@@ -357,13 +367,22 @@ struct key *key_alloc(struct key_type *type, const char *desc,
key_user_put(user);
no_memory_1:
key = ERR_PTR(-ENOMEM);
- goto error;
+ goto out_put_restrict_module;
no_quota:
spin_unlock_irqrestore(&user->lock, irqflags);
key_user_put(user);
key = ERR_PTR(-EDQUOT);
- goto error;
+
+out_put_restrict_module:
+ if (restrict_link && restrict_link->keytype)
+ module_put(restrict_link->keytype->owner);
+
+out_put_module:
+ module_put(type->owner);
+
+out:
+ return key;
}
EXPORT_SYMBOL(key_alloc);
@@ -1311,20 +1330,19 @@ int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
EXPORT_SYMBOL(generic_key_instantiate);
/**
- * register_key_type - Register a type of key.
+ * __register_key_type - Register a type of key.
* @ktype: The new key type.
+ * @owner: Module owning the key type.
*
* Register a new key type.
*
* Returns 0 on success or -EEXIST if a type of this name already exists.
*/
-int register_key_type(struct key_type *ktype)
+int __register_key_type(struct key_type *ktype, struct module *owner)
{
struct key_type *p;
int ret;
- memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
-
ret = -EEXIST;
down_write(&key_types_sem);
@@ -1334,6 +1352,9 @@ int register_key_type(struct key_type *ktype)
goto out;
}
+ memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
+ ktype->owner = owner;
+
/* store the type */
list_add(&ktype->link, &key_types_list);
@@ -1344,7 +1365,7 @@ int register_key_type(struct key_type *ktype)
up_write(&key_types_sem);
return ret;
}
-EXPORT_SYMBOL(register_key_type);
+EXPORT_SYMBOL(__register_key_type);
/**
* unregister_key_type - Unregister a type of key.
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 4f906073d312..b9a639723fbe 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -7,6 +7,7 @@
#include <linux/export.h>
#include <linux/init.h>
+#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/security.h>
@@ -540,8 +541,10 @@ struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
if (!IS_ERR(keyring)) {
ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL);
if (ret < 0) {
- if (restrict_link && restrict_link->keytype)
+ if (restrict_link && restrict_link->keytype) {
+ module_put(restrict_link->keytype->owner);
atomic_dec_return_release(&restrict_link->keytype->nr_keys);
+ }
keyring->restrict_link = NULL;
key_put(keyring);
keyring = ERR_PTR(ret);
@@ -1065,7 +1068,7 @@ int keyring_restrict(key_ref_t keyring_ref, const char *type,
if (!restrict_type->lookup_restriction) {
ret = -ENOENT;
- goto error;
+ goto out;
}
restrict_link = restrict_type->lookup_restriction(restriction);
@@ -1073,7 +1076,13 @@ int keyring_restrict(key_ref_t keyring_ref, const char *type,
if (IS_ERR(restrict_link)) {
ret = PTR_ERR(restrict_link);
- goto error;
+ goto out;
+ }
+
+ if (restrict_link && restrict_link->keytype &&
+ !try_module_get(restrict_link->keytype->owner)) {
+ ret = -ENOENT;
+ goto out_free_restriction;
}
down_write(&keyring->sem);
@@ -1093,12 +1102,21 @@ int keyring_restrict(key_ref_t keyring_ref, const char *type,
up_write(&keyring_serialise_restrict_sem);
up_write(&keyring->sem);
- if (ret < 0) {
+ if (ret < 0)
+ goto out_put_module;
+
+ goto out;
+
+out_put_module:
+ if (restrict_link && restrict_link->keytype)
+ module_put(restrict_link->keytype->owner);
+
+out_free_restriction:
+ if (restrict_link)
key_put(restrict_link->key);
- kfree(restrict_link);
- }
+ kfree(restrict_link);
-error:
+out:
if (restrict_type)
key_type_put(restrict_type);
@@ -1818,6 +1836,7 @@ void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
key_put(keyres->key);
keyres->key = NULL;
+ module_put(dead_type->owner);
atomic_dec_return_release(&dead_type->nr_keys);
keyres->keytype = NULL;
}
--
2.47.3
next prev parent reply other threads:[~2026-11-29 17:06 UTC|newest]
Thread overview: 18+ 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 ` [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module Jarkko Sakkinen
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 ` Jarkko Sakkinen [this message]
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
2026-11-29 19:28 ` Jarkko Sakkinen
2026-11-29 19:35 ` 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-14-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
all inboxes | Powered by JetHome®