Article View: linux.kernel.netdev
Article #12877[PATCH v5 10/12] KEYS: Pin the backing kernel module for a key type
From: Jarkko Sakkinen
Date: Sun, 08 Nov 2026 22:27
Date: Sun, 08 Nov 2026 22:27
310 lines
17773 bytes
17773 bytes
Add the owner field (populated with THIS_MODULE) to struct key_type.
Pin the module in key_alloc() and keyring_restrict(), and release it in
key_gc_unused_keys() and keyring_destroy(), which are wires to the keys
and restrictions.
Reject module owners still in MODULE_STATE_COMING so that the failed
module_init() paths cannot leave behind referenced keys.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
include/linux/key-type.h | 6 ++++-
security/keys/gc.c | 14 ++++++++++--
security/keys/key.c | 48 ++++++++++++++++++++++++++++++----------
security/keys/keyring.c | 36 +++++++++++++++++++++++++-----
4 files changed, 83 insertions(+), 21 deletions(-)
diff --git a/include/linux/key-type.h b/include/linux/key-type.h
index c4f329b6ae3f..9ecec31ad571 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;
@@ -161,6 +163,7 @@ struct key_type {
const void *in, const void *in2);
/* internal fields */
+ struct module *owner;
atomic64_t index_id; /* Opaque keyring index ID */
struct list_head link; /* link in types list */
struct lock_class_key lock_class; /* key->sem lock class */
@@ -168,7 +171,8 @@ struct key_type {
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 9b625436d3b5..7b2f32998f58 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -6,6 +6,7 @@
*/
#include <linux/slab.h>
+#include <linux/module.h>
#include <linux/security.h>
#include <keys/keyring-type.h>
#include "internal.h"
@@ -150,6 +151,7 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
while (!list_empty(keys)) {
struct key *key =
list_entry(keys->next, struct key, graveyard_link);
+ struct module *owner = key->type->owner;
short state = key->state;
list_del(&key->graveyard_link);
@@ -163,8 +165,15 @@ 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 && key->type->destroy)
- key->type->destroy(key);
+ if (state == KEY_IS_POSITIVE) {
+ if (key->type->destroy)
+ key->type->destroy(key);
+ } else if (key->restrict_link) {
+ if (key->restrict_link->keytype)
+ module_put(key->restrict_link->keytype->owner);
+ key_put(key->restrict_link->key);
+ kfree(key->restrict_link);
+ }
security_key_free(key);
@@ -178,6 +187,7 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
memzero_explicit(key, sizeof(*key));
kmem_cache_free(key_jar, key);
+ module_put(owner);
}
}
diff --git a/security/keys/key.c b/security/keys/key.c
index 3e604d25d35e..be68d19f91d3 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,19 +235,34 @@ struct key *key_alloc(struct key_type *type, const char *desc,
key = ERR_PTR(-EINVAL);
if (!desc || !*desc)
- goto error;
+ goto out;
+
+ /* Failed module initialization ignores outstanding module references. */
+ if ((type->owner && module_is_coming(type->owner)) ||
+ !try_module_get(type->owner)) {
+ key = ERR_PTR(-ENOENT);
+ goto out;
+ }
+
+ if (restrict_link && restrict_link->keytype &&
+ ((restrict_link->keytype->owner &&
+ module_is_coming(restrict_link->keytype->owner)) ||
+ !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 */
@@ -323,8 +339,6 @@ struct key *key_alloc(struct key_type *type, const char *desc,
refcount_inc(&key->domain_tag->usage);
atomic_inc(&user->nkeys);
key_alloc_serial(key);
-
-error:
return key;
security_error:
@@ -338,7 +352,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);
@@ -352,13 +366,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);
@@ -1231,19 +1254,20 @@ 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));
+ ktype->owner = owner;
ret = -EEXIST;
down_write(&key_types_sem);
@@ -1264,7 +1288,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 5281da9d5bf7..f90ab46ecfab 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>
@@ -436,6 +437,9 @@ static void keyring_destroy(struct key *keyring)
if (keyring->restrict_link) {
struct key_restriction *keyres = keyring->restrict_link;
+ if (keyres->keytype)
+ module_put(keyres->keytype->owner);
+
key_put(keyres->key);
kfree(keyres);
}
@@ -540,6 +544,9 @@ 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)
+ module_put(restrict_link->keytype->owner);
+ keyring->restrict_link = NULL;
key_put(keyring);
keyring = ERR_PTR(ret);
}
@@ -1062,7 +1069,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);
@@ -1070,7 +1077,15 @@ 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 &&
+ ((restrict_link->keytype->owner &&
+ module_is_coming(restrict_link->keytype->owner)) ||
+ !try_module_get(restrict_link->keytype->owner))) {
+ ret = -ENOENT;
+ goto out_free_restriction;
}
down_write(&keyring->sem);
@@ -1088,12 +1103,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);
--
2.47.3
Technical Details
Message-ID:
<20261108222719.304712-11-jarkko@kernel.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358899-linux-kernel-netdev=dewy.ruReferences:
<20261108222719.304712-1-jarkko@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358899-linux-kernel-netdev=dewy.ru From: jarkko@kernel.org (Jarkko Sakkinen) Newsgroups: linux.kernel.netdev Subject: [PATCH v5 10/12] KEYS: Pin the backing kernel module for a key type Date: Mon, 9 Nov 2026 00:27:16 +0200 Organization: Usenet Network Message-ID: <20261108222719.304712-11-jarkko@kernel.org> References: <20261108222719.304712-1-jarkko@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="220144"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:EyseiJtb4SZHIvzJqTvT70QnPWU= sha256:dXINy5jKJaQklhE0ChINl52/6HVI7NPSgse8KQii51Y= sha1:b4sQ1BaZd+Il15o3JJpJehJYJHY= sha256:NGbY9P3rslca0KMzN4ZmPm6xk58RqqFgexAzfeCESMs= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 17FBC11402E; Mon, 9 Nov 2026 01:30:51 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.301 X-Spam-Status: No, score=-2.301 tagged_above=-9999 required=6.31 tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HTYwxM3bYGCb; Mon, 9 Nov 2026 01:30:44 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-358899-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id E852511402D; Mon, 9 Nov 2026 01:30:35 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 68A53819A1; Sun, 8 Nov 2026 22:28:29 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794176887; b=NLbPUNm7SqQ8VqH9Fviae5ffkw2D2nUVohf46l9JUagjQqisrCOgQf9tHfanCW2XnSHR j5EAWK62ad9AgbnFKspl+dVmGjLbB/WDMhLK/YlNERQRtfVPq4vmtSqu2kPcAfsN4jz0P 5SR336c8qjvs2dWkQMvY693jKVBBsIX/9/aoOdehHlM/K6cr3Rawd3PBVXXJ6VlftpA1V 3eP3sgp5RK1LRH5qr8yo7mX70MhvdL7Igxu+YP3YJ6Q3XlvA6c66Vjg/UKpixzgcwloWn 9JjTztFMSuP43qXkEVCUfNVsG2oCWIc+tS9DBpsJHbMGvGVuoBOQOvxLqlHCwQe/x4Q== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794176887; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Transfer-Encoding; bh=pPJtaNXXVN6W3aSE9iN/Uh6en78jkh+qXfwU+6KoP0E=; b=MkSwCJg3mnx6UY+RCX92KsufmxDFG7X07/IkBKHwwJLbS/fxYb+c9kx1uvRQ/OAJLcL5 YoQopdorlnny7Xfdt4kzyqUM9Tkmff1ZwKZKTGnFtRmFiKOSxvuesMAEyUeHKef/cL1Qc 1EWvv6HCS1+jIVNqMCKvnZx7oErJiOWt0JlBTpCltORyuieLdtVlkpaE5X3fmE0WTnhZv 8DrRTibVkfFjj8lRqwFAbL3wZGXGQzqy4pOZzW1lK/KUJZP3GMGEGr4vZYKyzeNsw2tKw +9Zr9q3vsyFzvY9ycVtw3LjaWJvw/a1hsRq4b9E/H5JtYb/UQOuhR33niEbLt7xOHTg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e2hYSWFB; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7A445606A336; Sun, 8 Nov 2026 22:28:07 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e2hYSWFB X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794176886; b=xKg1bk1GCPHhtBNCSKzxLwAZMS+Ei7kg3og0r4p3BXQCkdKB+OlcSnCYZ9m0Vzi6bkcb YtYNxN584/1Y1Vr8478KFEkqZNEsLbK1C2IKVyMh4r94filfureTAh95xFElvCTsQ28Bu Fm6DOhR/l5kwZLBrv5VJhWCwt15KuYpzx26UqoZXrtUd8XvBN4cPzwO9cuDfvmdd64Ju5 /EkEeZkfy6GIXRhKDOrpF6Gts6uAcPCOqsz1H1S/cxj+PuXHFtT+Zp2gEeh/g51kXbN4r bZatBBjv/Pv4W2meHWar5zKn5R1aB8AnGxbg/bHhvdhYiQ8I14kiIMCd3DOrBm56PMA== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794176886; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=pPJtaNXXVN6W3aSE9iN/Uh6en78jkh+qXfwU+6KoP0E=; b=Tut27j8bphzXdJwz7V/Tsqwk1zc80EDJ1cP9UxKvR8UwHYo76oIzPl5eH1D8U5UVDdjC UnPQ9lvhgmlRYStvj27NZQNZMsEQL2Xgt6/2Li6ZndbQMOxo0gNU2jzAe78q+Pz19NSjU tZgmgxs0awp1kuh5N0mGwhYjizSvywXVYCyyhLzfZofVlG4zQj+2h967Ix+EbqoTy7MB9 bZq2im2eW4ovzLzn7mP0kdgC98I3mk6JVcsFAqhTa9EGC3ygWVnxNq19PolJ9QUBkZMEE CQH/1WLF4bb3KkIVJwe1cdtEFCAISsS4Xgh6bMhkXiAQHoTokNQcDZl/7HGRf7Kz6MQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=e2hYSWFB; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 53082606D819; Sun, 8 Nov 2026 22:28:06 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 2CEF21F008A3; Sun, 8 Nov 2026 22:28:05 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794176885; bh=pPJtaNXXVN6W3aSE9iN/Uh6en78jkh+qXfwU+6KoP0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=e2hYSWFB4Q86hiXFZvJKZYFc9sVaGrYGHg+uVGAQknBcn1oAKpOJd3Bj3RFztVztj ALLKsIB/GH3symVZgEpzP7WTpnZtsOxNWeloaoic86R/HLgK1cxZknX1loIHthECW6 dcWKguSJ8LCBVepfcQX8mzlX3FlAOrmyXmt/fEbJ1DkJIm9izlPYRgxj4OqYjFnISD kaWZLma5jW1/1zr9RtgNEeECrqJMXZBDapDSkna8sFEg2kpAoqU32F9BlnkeZKRpBf b+n50R6ESr8BnP9eZz1U5GYGXOEDL4tKEsAWlUaOWyc4QuhRAV97nsl+J6h+G4sHda N5V0DLoXoRexQ== X-Cc: Jarkko Sakkinen <jarkko@kernel.org>, keyrings@vger.kernel.org, linux-afs@lists.infradead.org, linux-cifs@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, Alexander Gordeev <agordeev@linux.ibm.com>, Anna Schumaker <anna@kernel.org>, Bharath SM <bharathsm@microsoft.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, "David S. Miller" <davem@davemloft.net>, Eric Biggers <ebiggers@kernel.org>, 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>, Marc Dionne <marc.dionne@auristor.com>, 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>, Ronnie Sahlberg <ronniesahlberg@gmail.com>, "Serge E. Hallyn" <serge@hallyn.com>, 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> X-Mailer: git-send-email 2.47.3 X-In-Reply-To: <20261108222719.304712-1-jarkko@kernel.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list