Linux Integrity Measurement 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 14/14] KEYS: Remove key_type_dead
Date: Sun, 29 Nov 2026 19:05:43 +0200	[thread overview]
Message-ID: <20261129170543.2915157-15-jarkko@kernel.org> (raw)
In-Reply-To: <20261129170543.2915157-1-jarkko@kernel.org>

unregister_key_type() no longer needs to retype live keys as keys and
restrictions pin the module while they reference it.

Remove key_type_dead, KEY_FLAG_DEAD, and associated dead-keytype
reaping code and documentation from the keyring GC, and warn if
unregister_key_type() is called with extant keys.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 Documentation/security/keys/core.rst |  29 ++---
 include/linux/key.h                  |   1 -
 security/keys/gc.c                   | 174 ++-------------------------
 security/keys/internal.h             |  10 +-
 security/keys/key.c                  | 170 ++++++++------------------
 security/keys/keyring.c              |  53 --------
 security/keys/permission.c           |   7 +-
 security/keys/proc.c                 |   2 +-
 8 files changed, 77 insertions(+), 369 deletions(-)

diff --git a/Documentation/security/keys/core.rst b/Documentation/security/keys/core.rst
index a60b2f725a0b..d21c178a51ae 100644
--- a/Documentation/security/keys/core.rst
+++ b/Documentation/security/keys/core.rst
@@ -48,10 +48,8 @@ Each key has a number of attributes:
      can be added or used. Userspace programs cannot define new types directly.
 
      Key types are represented in the kernel by struct key_type. This defines a
-     number of operations that can be performed on a key of that type.
-
-     Should a type be removed from the system, all the keys of that type will
-     be invalidated.
+     number of operations that can be performed on a key of that type. Live keys
+     and keyring restrictions pin the module implementing their key type.
 
   *  Each key has a description. This should be a printable string. The key
      type provides an operation to perform a match between the description on a
@@ -100,9 +98,7 @@ Each key has a number of attributes:
       *  Revoked. A key is put in this state by userspace action. It can't be
 	 found or operated upon (apart from by unlinking it).
 
-      *  Dead. The key's type was unregistered, and so the key is now useless.
-
-Keys in the last three states are subject to garbage collection.  See the
+Keys in the last two states are subject to garbage collection.  See the
 section on "Garbage collection".
 
 
@@ -1289,9 +1285,9 @@ payload contents" for more information.
     the function that will be called each time an attempt is made to link a
     key into the new keyring.  The structure may also contain a key pointer
     and an associated key type.  The function is called to check whether a key
-    may be added into the keyring or not.  The key type is used by the garbage
-    collector to clean up function or data pointers in this structure if the
-    given key type is unregistered.  Callers of key_create_or_update() within
+    may be added into the keyring or not.  If an associated key type is set, its
+    owning module is pinned for the lifetime of the restriction.  Callers of
+    key_create_or_update() within
     the kernel can pass KEY_ALLOC_BYPASS_RESTRICTION to suppress the check.
     An example of using this is to manage rings of cryptographic keys that are
     set up when the kernel boots where userspace is also permitted to add keys
@@ -1606,8 +1602,7 @@ The structure has a number of fields, some of which are mandatory:
      when it is being destroyed.
 
      This method does not need to lock the key to access the payload; it can
-     consider the key as being inaccessible at this time. Note that the key's
-     type may have been changed before this function is called.
+     consider the key as being inaccessible at this time.
 
      It is not safe to sleep in this method; the caller may hold spinlocks.
 
@@ -1855,11 +1850,9 @@ the rings are provided for reference.
 Garbage Collection
 ==================
 
-Dead keys (for which the type has been removed) will be automatically unlinked
-from those keyrings that point to them and deleted as soon as possible by a
-background garbage collector.
-
-Similarly, revoked and expired keys will be garbage collected, but only after a
-certain amount of time has passed.  This time is set as a number of seconds in::
+Unreferenced keys are destroyed asynchronously by a background garbage
+collector.  Revoked and expired keys are unlinked from keyrings and garbage
+collected after a certain amount of time has passed.  This time is set as a
+number of seconds in::
 
 	/proc/sys/kernel/keys/gc_delay
diff --git a/include/linux/key.h b/include/linux/key.h
index edd852608763..7f7ceb5d8625 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -233,7 +233,6 @@ struct key {
 #endif
 
 	unsigned long		flags;		/* status flags (change with bitops) */
-#define KEY_FLAG_DEAD		0	/* set if key type has been deleted */
 #define KEY_FLAG_REVOKED	1	/* set if key had been revoked */
 #define KEY_FLAG_IN_QUOTA	2	/* set if key consumes quota */
 #define KEY_FLAG_USER_CONSTRUCT	3	/* set if key is being constructed in userspace */
diff --git a/security/keys/gc.c b/security/keys/gc.c
index eaddd1bab37c..ab51edf235b8 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -47,24 +47,9 @@ static void key_gc_timer_func(struct timer_list *);
 static DEFINE_TIMER(key_gc_timer, key_gc_timer_func);
 
 static time64_t key_gc_next_run = TIME64_MAX;
-static struct key_type *key_gc_dead_keytype;
-
 static unsigned long key_gc_flags;
 #define KEY_GC_KEY_EXPIRED	0	/* A key expired and needs unlinking */
-#define KEY_GC_REAP_KEYTYPE	1	/* A keytype is being unregistered */
-#define KEY_GC_REAPING_KEYTYPE	2	/* Cleared when keytype reaped */
-#define KEY_GC_REAP_REQUESTED	3	/* Another complete pass was requested */
-
-DEFINE_SRCU(key_type_srcu);
-
-
-/*
- * Any key whose type gets unregistered will be re-typed to this if it can't be
- * immediately unlinked.
- */
-struct key_type key_type_dead = {
-	.name = ".dead",
-};
+#define KEY_GC_REAP_REQUESTED	1	/* Another complete pass was requested */
 
 /*
  * Schedule a new GC pass or re-schedule an ongoing GC pass.
@@ -92,7 +77,7 @@ void key_schedule_gc(time64_t gc_at)
 
 	kenter("%lld", gc_at - now);
 
-	if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
+	if (gc_at <= now) {
 		kdebug("IMMEDIATE");
 		/* Catch expirations that became due during the scan. */
 		key_schedule_gc_work(true);
@@ -128,37 +113,6 @@ static void key_gc_timer_func(struct timer_list *unused)
 	key_schedule_gc_work(true);
 }
 
-/*
- * Reap keys of dead type.
- *
- * We use three flags to make sure we see three complete cycles of the garbage
- * collector: the first to mark keys of that type as being dead, the second to
- * collect dead links and the third to clean up the dead keys.  We have to be
- * careful as there may already be a cycle in progress.
- *
- * The caller must be holding key_types_sem.
- */
-void key_gc_keytype(struct key_type *ktype)
-{
-	kenter("%s", ktype->name);
-
-	key_gc_dead_keytype = ktype;
-	set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
-	smp_mb();
-	set_mask_bits(&key_gc_flags, 0,
-		      BIT(KEY_GC_REAP_KEYTYPE) | BIT(KEY_GC_REAP_REQUESTED));
-
-	kdebug("schedule");
-	queue_work(key_gc_wq, &key_gc_work.work);
-
-	kdebug("sleep");
-	wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
-		    TASK_UNINTERRUPTIBLE);
-
-	key_gc_dead_keytype = NULL;
-	kleave("");
-}
-
 /*
  * Garbage collect a list of unreferenced, detached keys
  */
@@ -329,10 +283,6 @@ static void key_garbage_collector(struct work_struct *work)
 #define KEY_GC_REAPING_LINKS	0x02	/* - We need to reap links */
 #define KEY_GC_DESTROYING	0x04	/* - We need to destroy keys */
 #define KEY_GC_SCANNING		0x08	/* - Scan in progress */
-#define KEY_GC_REAPING_DEAD_1	0x10	/* - We need to mark dead keys */
-#define KEY_GC_REAPING_DEAD_2	0x20	/* - We need to reap dead key links */
-#define KEY_GC_REAPING_DEAD_3	0x40	/* - We need to reap dead keys */
-#define KEY_GC_FOUND_DEAD_KEY	0x80	/* - We found at least one dead key */
 
 	unsigned int chunk = KEY_GC_CHUNK_SIZE;
 	unsigned long flags;
@@ -350,19 +300,11 @@ static void key_garbage_collector(struct work_struct *work)
 		goto continue_scanning;
 	}
 
-	flags = set_mask_bits(&key_gc_flags,
-			      BIT(KEY_GC_KEY_EXPIRED) |
-			      BIT(KEY_GC_REAP_KEYTYPE) |
-			      BIT(KEY_GC_REAP_REQUESTED), 0);
+	flags = xchg(&key_gc_flags, 0);
 	gc->start = ktime_get_real_seconds();
-
-	/* Work out what we're going to be doing in this pass */
-	gc->state &= KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2;
-	gc->state <<= 1;
+	gc->state = 0;
 	if (flags & BIT(KEY_GC_KEY_EXPIRED))
 		gc->state |= KEY_GC_REAPING_LINKS;
-	if (flags & BIT(KEY_GC_REAP_KEYTYPE))
-		gc->state |= KEY_GC_REAPING_DEAD_1;
 	kdebug("new pass %x", gc->state);
 
 	gc->end = TIME64_MAX;
@@ -388,20 +330,6 @@ static void key_garbage_collector(struct work_struct *work)
 			goto found_unreferenced_key;
 		}
 
-		if (unlikely(gc->state & KEY_GC_REAPING_DEAD_1)) {
-			if (key->type == key_gc_dead_keytype) {
-				gc->state |= KEY_GC_FOUND_DEAD_KEY;
-				set_bit(KEY_FLAG_DEAD, &key->flags);
-				key->perm = 0;
-				goto skip_dead_key;
-			} else if (key->type == &key_type_keyring &&
-				   key->restrict_link) {
-				if (refcount_inc_not_zero(&key->usage))
-					goto found_restricted_keyring;
-				goto wait_unreferenced_key;
-			}
-		}
-
 		expiry = key->expiry;
 		if (expiry != TIME64_MAX) {
 			if (!(key->type->flags & KEY_TYPE_INSTANT_REAP))
@@ -413,25 +341,10 @@ static void key_garbage_collector(struct work_struct *work)
 			}
 		}
 
-		if (unlikely(gc->state & KEY_GC_REAPING_DEAD_2))
-			if (key->type == key_gc_dead_keytype)
-				gc->state |= KEY_GC_FOUND_DEAD_KEY;
-
-		if ((gc->state & KEY_GC_REAPING_LINKS) ||
-		    unlikely(gc->state & KEY_GC_REAPING_DEAD_2)) {
-			if (key->type == &key_type_keyring)
-				goto found_keyring;
-		}
-
-		if (unlikely(gc->state & KEY_GC_REAPING_DEAD_3)) {
-			if (key->type == key_gc_dead_keytype) {
-				if (refcount_inc_not_zero(&key->usage))
-					goto destroy_dead_key;
-				goto wait_unreferenced_key;
-			}
-		}
+		if ((gc->state & KEY_GC_REAPING_LINKS) &&
+		    key->type == &key_type_keyring)
+			goto found_keyring;
 
-	skip_dead_key:
 		if (spin_is_contended(&key_serial_lock) || need_resched())
 			goto contended;
 	}
@@ -461,19 +374,7 @@ static void key_garbage_collector(struct work_struct *work)
 	if (gc->end != TIME64_MAX)
 		key_schedule_gc(gc->end);
 
-	if (unlikely((gc->state & KEY_GC_REAPING_DEAD_1) &&
-		     (gc->state & KEY_GC_FOUND_DEAD_KEY))) {
-		/* Wait for any in-flight key_instantiate_and_link() or
-		 * key_reject_and_link() calls that did not observe
-		 * KEY_FLAG_DEAD before pass 2 unlinks dead keys from keyrings
-		 * and pass 3 retypes them to key_type_dead.
-		 */
-		kdebug("gc dead type sync");
-		synchronize_srcu(&key_type_srcu);
-	}
-
-	if (unlikely(gc->state & KEY_GC_REAPING_DEAD_2) ||
-	    !list_empty(&key_gc_graveyard)) {
+	if (!list_empty(&key_gc_graveyard)) {
 		/* Make sure that all pending keyring payload destructions are
 		 * fulfilled and that people aren't now looking at dead or
 		 * dying keys that they don't have a reference upon or a link
@@ -496,25 +397,6 @@ static void key_garbage_collector(struct work_struct *work)
 	}
 	gc->state &= ~KEY_GC_DESTROYING;
 
-	if (unlikely(gc->state & (KEY_GC_REAPING_DEAD_1 |
-				  KEY_GC_REAPING_DEAD_2))) {
-		if (!(gc->state & KEY_GC_FOUND_DEAD_KEY)) {
-			/* No remaining dead keys: short circuit the remaining
-			 * keytype reap cycles.
-			 */
-			kdebug("dead short");
-			gc->state &= ~(KEY_GC_REAPING_DEAD_1 | KEY_GC_REAPING_DEAD_2);
-			gc->state |= KEY_GC_REAPING_DEAD_3;
-		} else {
-			gc->state |= KEY_GC_REAP_AGAIN;
-		}
-	}
-
-	if (unlikely(gc->state & KEY_GC_REAPING_DEAD_3)) {
-		kdebug("dead wake");
-		clear_and_wake_up_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
-	}
-
 	if ((gc->state & KEY_GC_REAP_AGAIN) ||
 	    test_bit(KEY_GC_REAP_REQUESTED, &key_gc_flags))
 		queue_work(key_gc_wq, &key_gc_work.work);
@@ -523,13 +405,6 @@ static void key_garbage_collector(struct work_struct *work)
 	kleave(" [end %x]", gc->state);
 	return;
 
-	/* Wait for an in-flight final key_put() to finish clearing USER_ALIVE. */
-wait_unreferenced_key:
-	spin_unlock(&key_serial_lock);
-	while (test_bit_acquire(KEY_FLAG_USER_ALIVE, &key->flags))
-		cpu_relax();
-	spin_lock(&key_serial_lock);
-
 	/* We found an unreferenced key - once we've removed it from the tree,
 	 * we can safely drop the lock.
 	 */
@@ -542,40 +417,13 @@ static void key_garbage_collector(struct work_struct *work)
 	gc->state |= KEY_GC_REAP_AGAIN;
 	goto maybe_resched;
 
-	/* We found a restricted keyring and need to update the restriction if
-	 * it is associated with the dead key type.
-	 */
-found_restricted_keyring:
-	spin_unlock(&key_serial_lock);
-	keyring_restriction_gc(key, key_gc_dead_keytype);
-	key_put(key);
-	goto maybe_resched;
-
 	/* We found a keyring and we need to check the payload for links to
-	 * dead or expired keys.  We don't flag another reap immediately as we
-	 * have to wait for the old payload to be destroyed by RCU before we
-	 * can reap the keys to which it refers.
+	 * expired or invalidated keys.  We don't flag another reap immediately
+	 * as we have to wait for the old payload to be destroyed by RCU before
+	 * we can reap the keys to which it refers.
 	 */
 found_keyring:
 	spin_unlock(&key_serial_lock);
 	keyring_gc(key, gc->start);
 	goto maybe_resched;
-
-	/* We found a dead key that is still referenced.  Reset its type and
-	 * destroy its payload with its semaphore held.
-	 */
-destroy_dead_key:
-	spin_unlock(&key_serial_lock);
-	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)
-		key_gc_dead_keytype->destroy(key);
-	memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
-	up_write(&key->sem);
-	key_put(key);
-	goto maybe_resched;
 }
diff --git a/security/keys/internal.h b/security/keys/internal.h
index 59e743f24e61..6c972bceddc6 100644
--- a/security/keys/internal.h
+++ b/security/keys/internal.h
@@ -19,7 +19,6 @@
 #include <linux/compat.h>
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
-#include <linux/srcu.h>
 
 struct iovec;
 
@@ -39,7 +38,6 @@ struct iovec;
 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
 #endif
 
-extern struct key_type key_type_dead;
 extern struct key_type key_type_user;
 extern struct key_type key_type_logon;
 
@@ -87,7 +85,6 @@ extern struct kmem_cache *key_jar;
 extern struct rb_root key_serial_tree;
 extern spinlock_t key_serial_lock;
 extern struct mutex key_construction_mutex;
-extern struct srcu_struct key_type_srcu;
 extern wait_queue_head_t request_key_conswq;
 
 extern void key_set_index_key(struct keyring_index_key *index_key);
@@ -167,12 +164,9 @@ extern void key_change_session_keyring(struct callback_head *twork);
 extern unsigned key_gc_delay;
 extern struct workqueue_struct *key_gc_wq;
 extern void keyring_gc(struct key *keyring, time64_t limit);
-extern void keyring_restriction_gc(struct key *keyring,
-				   struct key_type *dead_type);
 void key_set_expiry(struct key *key, time64_t expiry);
 extern void key_schedule_gc(time64_t gc_at);
 void key_schedule_gc_work(bool expired);
-extern void key_gc_keytype(struct key_type *ktype);
 
 extern int key_task_permission(const key_ref_t key_ref,
 			       const struct cred *cred,
@@ -229,9 +223,7 @@ static inline bool key_is_dead(const struct key *key, time64_t limit)
 			return true;
 	}
 
-	return
-		key->flags & ((1 << KEY_FLAG_DEAD) |
-			      (1 << KEY_FLAG_INVALIDATED)) ||
+	return (key->flags & (1 << KEY_FLAG_INVALIDATED)) ||
 		key->domain_tag->removed;
 }
 
diff --git a/security/keys/key.c b/security/keys/key.c
index dff8f02a262c..ef687fea05cc 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -446,10 +446,7 @@ static void mark_key_instantiated(struct key *key, int reject_error)
 
 /*
  * Instantiate a key and link it into the target keyring atomically.  Must be
- * called with the target keyring's semaphore writelocked.  The caller must
- * hold key_type_srcu (after checking !KEY_FLAG_DEAD) or key_types_sem for
- * reading via key_type_lookup(), so the type cannot be retyped to
- * key_type_dead or its module unloaded while its methods run.
+ * called with the target keyring's semaphore writelocked.
  */
 static int __key_instantiate_and_link(struct key *key,
 				      struct key_preparsed_payload *prep,
@@ -523,9 +520,8 @@ static int __key_instantiate_and_link(struct key *key,
  *
  * If successful, 0 is returned, the authorisation token is revoked and anyone
  * waiting for the key is woken up.  If the key was already instantiated,
- * -EBUSY will be returned.  If the key's type has been removed, -EKEYREVOKED
- * will be returned.  If the key type does not provide an instantiate method,
- * -EOPNOTSUPP will be returned.
+ * -EBUSY will be returned.  If the key type does not provide an instantiate
+ * method, -EOPNOTSUPP will be returned.
  */
 int key_instantiate_and_link(struct key *key,
 			     const void *data,
@@ -535,25 +531,10 @@ int key_instantiate_and_link(struct key *key,
 {
 	struct key_preparsed_payload prep;
 	struct assoc_array_edit *edit = NULL;
-	int ret, srcu_idx;
-
-	/*
-	 * Hold key_type_srcu across key->type accesses and linking into
-	 * @keyring.  Pass 1 of key_garbage_collector() sets KEY_FLAG_DEAD
-	 * and runs synchronize_srcu(&key_type_srcu) before pass 2 unlinks
-	 * dead keys and pass 3 retypes them to key_type_dead.
-	 */
-	srcu_idx = srcu_read_lock(&key_type_srcu);
+	int ret;
 
-	if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
-		ret = -EKEYREVOKED;
-		goto out;
-	}
-
-	if (!key->type->instantiate) {
-		ret = -EOPNOTSUPP;
-		goto out;
-	}
+	if (!key->type->instantiate)
+		return -EOPNOTSUPP;
 
 	memset(&prep, 0, sizeof(prep));
 	prep.orig_description = key->description;
@@ -564,17 +545,17 @@ int key_instantiate_and_link(struct key *key,
 	if (key->type->preparse) {
 		ret = key->type->preparse(&prep);
 		if (ret < 0)
-			goto out_free_preparse;
+			goto error;
 	}
 
 	if (keyring) {
 		ret = __key_link_lock(keyring, &key->index_key);
 		if (ret < 0)
-			goto out_free_preparse;
+			goto error;
 
 		ret = __key_link_begin(keyring, &key->index_key, &edit);
 		if (ret < 0)
-			goto out_link_end;
+			goto error_link_end;
 
 		if (keyring->restrict_link && keyring->restrict_link->check) {
 			struct key_restriction *keyres = keyring->restrict_link;
@@ -582,22 +563,19 @@ int key_instantiate_and_link(struct key *key,
 			ret = keyres->check(keyring, key->type, &prep.payload,
 					    keyres->key);
 			if (ret < 0)
-				goto out_link_end;
+				goto error_link_end;
 		}
 	}
 
 	ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
 
-out_link_end:
+error_link_end:
 	if (keyring)
 		__key_link_end(keyring, &key->index_key, edit);
 
-out_free_preparse:
+error:
 	if (key->type->preparse)
 		key->type->free_preparse(&prep);
-
-out:
-	srcu_read_unlock(&key_type_srcu, srcu_idx);
 	return ret;
 }
 
@@ -622,10 +600,7 @@ EXPORT_SYMBOL(key_instantiate_and_link);
  *
  * If successful, 0 is returned, the authorisation token is revoked and anyone
  * waiting for the key is woken up.  If the key was already instantiated,
- * -EBUSY will be returned.  If the key's type has been removed, the key is
- * left uninstantiated, the authorisation token is revoked, anyone waiting
- * for the key is woken up, and -EKEYREVOKED is returned if @keyring was
- * given.
+ * -EBUSY will be returned.
  */
 int key_reject_and_link(struct key *key,
 			unsigned timeout,
@@ -634,8 +609,7 @@ int key_reject_and_link(struct key *key,
 			struct key *authkey)
 {
 	struct assoc_array_edit *edit = NULL;
-	int ret, awaken, link_ret = 0, srcu_idx;
-	bool dead;
+	int ret, awaken, link_ret = 0;
 
 	key_check(key);
 	key_check(keyring);
@@ -643,88 +617,51 @@ int key_reject_and_link(struct key *key,
 	awaken = 0;
 	ret = -EBUSY;
 
-	/*
-	 * Hold key_type_srcu across linking into @keyring and
-	 * key_set_expiry() (which reads key->type->flags).  If the key is
-	 * already marked dead, skip linking, key->sem, and negative
-	 * instantiation so wait_for_key_construction() wakes up and returns
-	 * -EKEYREVOKED via key_validate().
-	 */
-	srcu_idx = srcu_read_lock(&key_type_srcu);
-	dead = test_bit(KEY_FLAG_DEAD, &key->flags);
-
 	if (keyring) {
-		if (dead) {
-			link_ret = -EKEYREVOKED;
-		} else {
-			link_ret = __key_link_lock(keyring, &key->index_key);
-			if (link_ret == 0) {
-				/*
-				 * Check the restriction under the keyring
-				 * semaphore. keyring_restrict() installs it
-				 * while holding the semaphore, so testing it
-				 * beforehand races with a concurrent
-				 * restriction install.
-				 */
-				if (keyring->restrict_link) {
-					__key_link_end(keyring, &key->index_key,
-						       edit);
-					ret = -EPERM;
-					goto out;
-				}
-				link_ret = __key_link_begin(keyring,
-							    &key->index_key,
-							    &edit);
-				if (link_ret < 0)
-					__key_link_end(keyring, &key->index_key,
-						       edit);
+		link_ret = __key_link_lock(keyring, &key->index_key);
+		if (link_ret == 0) {
+			/*
+			 * Check the restriction under the keyring semaphore.
+			 * keyring_restrict() installs it while holding the
+			 * semaphore, so testing it beforehand races with a
+			 * concurrent restriction install.
+			 */
+			if (keyring->restrict_link) {
+				__key_link_end(keyring, &key->index_key, edit);
+				return -EPERM;
 			}
+			link_ret = __key_link_begin(keyring, &key->index_key, &edit);
+			if (link_ret < 0)
+				__key_link_end(keyring, &key->index_key, edit);
 		}
 	}
 
 	mutex_lock(&key_construction_mutex);
+	down_write_nested(&key->sem, 2);
 
-	if (dead) {
-		if (key->state == KEY_IS_UNINSTANTIATED) {
-			if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT,
-					       &key->flags))
-				awaken = 1;
-
-			ret = 0;
-
-			/* disable the authorisation key */
-			if (authkey)
-				key_invalidate(authkey);
-		}
-	} else {
-		down_write_nested(&key->sem, 2);
-
-		/* can't instantiate twice */
-		if (key->state == KEY_IS_UNINSTANTIATED) {
-			/* mark the key as being negatively instantiated */
-			atomic_inc(&key->user->nikeys);
-			mark_key_instantiated(key, -error);
-			notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
-			key_set_expiry(key, ktime_get_real_seconds() + timeout);
-
-			if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT,
-					       &key->flags))
-				awaken = 1;
+	/* can't instantiate twice */
+	if (key->state == KEY_IS_UNINSTANTIATED) {
+		/* mark the key as being negatively instantiated */
+		atomic_inc(&key->user->nikeys);
+		mark_key_instantiated(key, -error);
+		notify_key(key, NOTIFY_KEY_INSTANTIATED, -error);
+		key_set_expiry(key, ktime_get_real_seconds() + timeout);
 
-			ret = 0;
+		if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
+			awaken = 1;
 
-			/* and link it into the destination keyring */
-			if (keyring && link_ret == 0)
-				__key_link(keyring, key, &edit);
+		ret = 0;
 
-			/* disable the authorisation key */
-			if (authkey)
-				key_invalidate(authkey);
-		}
+		/* and link it into the destination keyring */
+		if (keyring && link_ret == 0)
+			__key_link(keyring, key, &edit);
 
-		up_write(&key->sem);
+		/* disable the authorisation key */
+		if (authkey)
+			key_invalidate(authkey);
 	}
 
+	up_write(&key->sem);
 	mutex_unlock(&key_construction_mutex);
 
 	if (keyring && link_ret == 0)
@@ -734,10 +671,7 @@ int key_reject_and_link(struct key *key,
 	if (awaken)
 		wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
 
-	ret = ret == 0 ? link_ret : ret;
-out:
-	srcu_read_unlock(&key_type_srcu, srcu_idx);
-	return ret;
+	return ret == 0 ? link_ret : ret;
 }
 EXPORT_SYMBOL(key_reject_and_link);
 
@@ -1371,18 +1305,15 @@ EXPORT_SYMBOL(__register_key_type);
  * unregister_key_type - Unregister a type of key.
  * @ktype: The key type.
  *
- * Unregister a key type and mark all the extant keys of this type as dead.
- * Those keys of this type are then destroyed to get rid of their payloads and
- * they and their links will be garbage collected as soon as possible.
+ * Unregister a key type.
  */
 void unregister_key_type(struct key_type *ktype)
 {
 	down_write(&key_types_sem);
+	WARN_ON_ONCE(atomic_read(&ktype->nr_keys));
 	list_del_init(&ktype->link);
-	downgrade_write(&key_types_sem);
-	key_gc_keytype(ktype);
 	pr_notice("Key type %s unregistered\n", ktype->name);
-	up_read(&key_types_sem);
+	up_write(&key_types_sem);
 }
 EXPORT_SYMBOL(unregister_key_type);
 
@@ -1401,7 +1332,6 @@ void __init key_init(void)
 
 	/* add the special key types */
 	list_add_tail(&key_type_keyring.link, &key_types_list);
-	list_add_tail(&key_type_dead.link, &key_types_list);
 	list_add_tail(&key_type_user.link, &key_types_list);
 	list_add_tail(&key_type_logon.link, &key_types_list);
 
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index b9a639723fbe..0a931ecb5162 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -1792,56 +1792,3 @@ void keyring_gc(struct key *keyring, time64_t limit)
 	up_write(&keyring->sem);
 	kleave(" [gc]");
 }
-
-/*
- * Garbage collect restriction pointers from a keyring.
- *
- * Keyring restrictions are associated with a key type, and must be cleaned
- * up if the key type is unregistered. The restriction is altered to always
- * reject additional keys so a keyring cannot be opened up by unregistering
- * a key type.
- *
- * Not called with any keyring locks held. The keyring's key struct will not
- * be deallocated under us as only our caller may deallocate it.
- *
- * The caller is required to hold key_types_sem and dead_type->sem. This is
- * fulfilled by key_gc_keytype() holding the locks on behalf of
- * key_garbage_collector(), which it invokes on a workqueue.
- */
-void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
-{
-	struct key_restriction *keyres;
-
-	kenter("%x{%s}", keyring->serial, keyring->description ?: "");
-
-	/*
-	 * keyring->restrict_link is only assigned at key allocation time
-	 * or with the key type locked, so the only values that could be
-	 * concurrently assigned to keyring->restrict_link are for key
-	 * types other than dead_type. Given this, it's ok to check
-	 * the key type before acquiring keyring->sem.
-	 */
-	if (!dead_type || !keyring->restrict_link ||
-	    keyring->restrict_link->keytype != dead_type) {
-		kleave(" [no restriction gc]");
-		return;
-	}
-
-	/* Lock the keyring to ensure that a link is not in progress */
-	down_write(&keyring->sem);
-
-	keyres = keyring->restrict_link;
-	if (keyres->keytype == dead_type) {
-		keyres->check = restrict_link_reject;
-
-		key_put(keyres->key);
-		keyres->key = NULL;
-		module_put(dead_type->owner);
-		atomic_dec_return_release(&dead_type->nr_keys);
-		keyres->keytype = NULL;
-	}
-
-	up_write(&keyring->sem);
-
-	kleave(" [restriction gc]");
-}
diff --git a/security/keys/permission.c b/security/keys/permission.c
index 4a61f804e80f..ba29f9b60477 100644
--- a/security/keys/permission.c
+++ b/security/keys/permission.c
@@ -96,8 +96,8 @@ EXPORT_SYMBOL(key_task_permission);
  * @key: The key to be validated.
  *
  * Check that a key is valid, returning 0 if the key is okay, -ENOKEY if the
- * key is invalidated, -EKEYREVOKED if the key's type has been removed or if
- * the key has been revoked or -EKEYEXPIRED if the key has expired.
+ * key is invalidated, -EKEYREVOKED if the key has been revoked or
+ * -EKEYEXPIRED if the key has expired.
  */
 int key_validate(const struct key *key)
 {
@@ -108,8 +108,7 @@ int key_validate(const struct key *key)
 		return -ENOKEY;
 
 	/* check it's still accessible */
-	if (flags & ((1 << KEY_FLAG_REVOKED) |
-		     (1 << KEY_FLAG_DEAD)))
+	if (flags & (1 << KEY_FLAG_REVOKED))
 		return -EKEYREVOKED;
 
 	/* check it hasn't expired */
diff --git a/security/keys/proc.c b/security/keys/proc.c
index aea06a74d0fe..ce5325daaa4e 100644
--- a/security/keys/proc.c
+++ b/security/keys/proc.c
@@ -227,7 +227,7 @@ static int proc_keys_show(struct seq_file *m, void *v)
 		   key->serial,
 		   state != KEY_IS_UNINSTANTIATED ? 'I' : '-',
 		   showflag(flags, 'R', KEY_FLAG_REVOKED),
-		   showflag(flags, 'D', KEY_FLAG_DEAD),
+		   '-',
 		   showflag(flags, 'Q', KEY_FLAG_IN_QUOTA),
 		   showflag(flags, 'U', KEY_FLAG_USER_CONSTRUCT),
 		   state < 0 ? 'N' : '-',
-- 
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 ` [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 ` [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
2026-11-29 17:05 ` Jarkko Sakkinen [this message]
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-15-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