* [PATCH v7 01/14] KEYS: Move key garbage collection to a dedicated workqueue
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
@ 2026-11-29 17:05 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 02/14] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
` (13 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
key_gc_work is currently scheduled on the default system workqueue,
sharing execution and flush domains with unrelated system work items.
Allocate a dedicated key_gc workqueue in key_init() with WQ_PERCPU to
preserve the per-CPU bound execution and cache locality of
schedule_work(), and queue key_gc_work onto key_gc_wq.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
security/keys/gc.c | 10 ++++++----
security/keys/internal.h | 1 +
security/keys/key.c | 6 +++++-
3 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/security/keys/gc.c b/security/keys/gc.c
index 61cb2b9a7aaf..a095e5d48529 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -19,6 +19,8 @@ unsigned key_gc_delay = 5 * 60;
* Reaper for unused keys.
*/
static void key_garbage_collector(struct work_struct *work);
+
+struct workqueue_struct *key_gc_wq __ro_after_init;
DECLARE_WORK(key_gc_work, key_garbage_collector);
/*
@@ -59,7 +61,7 @@ void key_schedule_gc(time64_t gc_at)
if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
kdebug("IMMEDIATE");
- schedule_work(&key_gc_work);
+ queue_work(key_gc_wq, &key_gc_work);
} else if (gc_at < key_gc_next_run) {
kdebug("DEFERRED");
key_gc_next_run = gc_at;
@@ -87,7 +89,7 @@ void key_set_expiry(struct key *key, time64_t expiry)
void key_schedule_gc_links(void)
{
set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
- schedule_work(&key_gc_work);
+ queue_work(key_gc_wq, &key_gc_work);
}
/*
@@ -121,7 +123,7 @@ void key_gc_keytype(struct key_type *ktype)
set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
kdebug("schedule");
- schedule_work(&key_gc_work);
+ queue_work(key_gc_wq, &key_gc_work);
kdebug("sleep");
wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
@@ -335,7 +337,7 @@ static void key_garbage_collector(struct work_struct *work)
}
if (gc_state & KEY_GC_REAP_AGAIN)
- schedule_work(&key_gc_work);
+ queue_work(key_gc_wq, &key_gc_work);
kleave(" [end %x]", gc_state);
return;
diff --git a/security/keys/internal.h b/security/keys/internal.h
index 73d71de274c2..e7a999d226d3 100644
--- a/security/keys/internal.h
+++ b/security/keys/internal.h
@@ -166,6 +166,7 @@ extern void key_change_session_keyring(struct callback_head *twork);
extern struct work_struct key_gc_work;
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);
diff --git a/security/keys/key.c b/security/keys/key.c
index 3c3fe43d874c..4f38b58b1a45 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -742,7 +742,7 @@ void key_put(struct key *key)
}
/* Mark key as safe for GC after key->user done. */
clear_bit_unlock(KEY_FLAG_USER_ALIVE, &key->flags);
- schedule_work(&key_gc_work);
+ queue_work(key_gc_wq, &key_gc_work);
}
}
}
@@ -1366,6 +1366,10 @@ void __init key_init(void)
key_jar = kmem_cache_create("key_jar", sizeof(struct key),
0, SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_NO_MERGE, NULL);
+ key_gc_wq = alloc_workqueue("key_gc", WQ_PERCPU, 0);
+ if (!key_gc_wq)
+ panic("Cannot create key_gc workqueue\n");
+
/* 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);
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 02/14] KEYS: key_garbage_collector(): process in chunks
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 03/14] KEYS: Add key_gc_flush() for draining RCU callbacks Jarkko Sakkinen
` (12 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
key_garbage_collector() has a design that is not resistant enough for
workqueue lockups. The duration scales up relative to the number of items
in the serial tree, which causes a recurring theme of being guilty of
causing workqueue lockups.
Resolve the problem by processing in chunks:
1. Declare a static variable key_gc_work, which owns the garbage collection
in-flight state, and key_schedule_gc_work() for re-scheduling the work.
2. Define KEY_GC_CHUNK_SIZE, set to 64 keys, as the bound for the number
of items scanned and destroyed.
3. Define KEY_GC_DESTROYING to mark resume to destruction without repeating
the RCU grace period.
4. Define KEY_GC_REAP_REQUESTED for preserving full-pass requests across
the continuations.
keygcbench results without the patch:
unused: min=413.359 avg=1817.240 max=3221.120 var=1970880.024
expired: min=0.002 avg=1360.124 max=2481.990 var=1055093.994
combined: min=0.002 avg=1542.970 max=3221.120 var=1471557.422
keygcbench results with the patch:
unused: min=0.005 avg=0.126 max=54.173 var=0.946
expired: min=0.002 avg=0.210 max=216.470 var=7.894
combined: min=0.002 avg=0.158 max=216.470 var=3.627
These results translate to:
- >99.9% drop in the combined variance.
- 3.0% increase in the total task time.
The value 64 for KEY_GC_CHUNK_SIZE was cherry-picked from the set {1, 2,
..., 1024} by running the benchmark for each chunk size three times.
At the far left variance is 97% lower but at the same time total time
grows by 133%, when compared to the selected chunk size. The size was
picked based on getting a reasonable variance with a minimal throughput
decline.
Fixes: 0c061b5707ab ("KEYS: Correctly destroy key payloads when their keytype is removed")
Link: https://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd-test.git/tree/br2-external/package/keygcbench
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
security/keys/gc.c | 181 ++++++++++++++++++++++++---------------
security/keys/internal.h | 3 +-
security/keys/key.c | 4 +-
security/keys/keyring.c | 2 +-
4 files changed, 118 insertions(+), 72 deletions(-)
diff --git a/security/keys/gc.c b/security/keys/gc.c
index a095e5d48529..e38ee4839885 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -10,10 +10,7 @@
#include <keys/keyring-type.h>
#include "internal.h"
-/*
- * Delay between key revocation/expiry in seconds
- */
-unsigned key_gc_delay = 5 * 60;
+#define KEY_GC_CHUNK_SIZE 64
/*
* Reaper for unused keys.
@@ -21,7 +18,21 @@ unsigned key_gc_delay = 5 * 60;
static void key_garbage_collector(struct work_struct *work);
struct workqueue_struct *key_gc_wq __ro_after_init;
-DECLARE_WORK(key_gc_work, key_garbage_collector);
+
+static struct {
+ time64_t start; /* expiry range start */
+ time64_t end; /* expiry range end */
+ struct rb_node *cursor; /* serial tree position */
+ u8 state; /* state flags */
+ struct work_struct work; /* work item */
+} key_gc_work = {
+ .work = __WORK_INITIALIZER(key_gc_work.work, key_garbage_collector),
+};
+
+/*
+ * Delay between key revocation/expiry in seconds
+ */
+unsigned int key_gc_delay = 5 * 60;
/*
* Reaper for links from keyrings to dead keys.
@@ -36,6 +47,7 @@ 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);
@@ -48,6 +60,21 @@ struct key_type key_type_dead = {
.name = ".dead",
};
+/*
+ * Schedule a new GC pass or re-schedule an ongoing GC pass.
+ */
+void key_schedule_gc_work(bool expired)
+{
+ unsigned long flags = BIT(KEY_GC_REAP_REQUESTED);
+
+ if (expired)
+ flags |= BIT(KEY_GC_KEY_EXPIRED);
+
+ /* Publish all request flags together, after the caller's updates. */
+ set_mask_bits(&key_gc_flags, 0, flags);
+ queue_work(key_gc_wq, &key_gc_work.work);
+}
+
/*
* Schedule a garbage collection run.
* - time precision isn't particularly important
@@ -61,7 +88,8 @@ void key_schedule_gc(time64_t gc_at)
if (gc_at <= now || test_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags)) {
kdebug("IMMEDIATE");
- queue_work(key_gc_wq, &key_gc_work);
+ /* Catch expirations that became due during the scan. */
+ key_schedule_gc_work(true);
} else if (gc_at < key_gc_next_run) {
kdebug("DEFERRED");
key_gc_next_run = gc_at;
@@ -83,15 +111,6 @@ void key_set_expiry(struct key *key, time64_t expiry)
}
}
-/*
- * Schedule a dead links collection run.
- */
-void key_schedule_gc_links(void)
-{
- set_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags);
- queue_work(key_gc_wq, &key_gc_work);
-}
-
/*
* Some key's cleanup time was met after it expired, so we need to get the
* reaper to go through a cycle finding expired keys.
@@ -100,7 +119,7 @@ static void key_gc_timer_func(struct timer_list *unused)
{
kenter("");
key_gc_next_run = TIME64_MAX;
- key_schedule_gc_links();
+ key_schedule_gc_work(true);
}
/*
@@ -120,10 +139,11 @@ void key_gc_keytype(struct key_type *ktype)
key_gc_dead_keytype = ktype;
set_bit(KEY_GC_REAPING_KEYTYPE, &key_gc_flags);
smp_mb();
- set_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags);
+ 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);
+ queue_work(key_gc_wq, &key_gc_work.work);
kdebug("sleep");
wait_on_bit(&key_gc_flags, KEY_GC_REAPING_KEYTYPE,
@@ -138,7 +158,9 @@ void key_gc_keytype(struct key_type *ktype)
*/
static noinline void key_gc_unused_keys(struct list_head *keys)
{
- while (!list_empty(keys)) {
+ unsigned int chunk = KEY_GC_CHUNK_SIZE;
+
+ while (!list_empty(keys) && chunk--) {
struct key *key =
list_entry(keys->next, struct key, graveyard_link);
short state = key->state;
@@ -182,54 +204,67 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
static void key_garbage_collector(struct work_struct *work)
{
static LIST_HEAD(graveyard);
- static u8 gc_state; /* Internal persistent state */
+ typeof(key_gc_work) *gc = &key_gc_work;
#define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
#define KEY_GC_REAPING_LINKS 0x02 /* - We need to reap links */
+#define KEY_GC_DESTROYING 0x04 /* - We need to destroy keys */
#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 */
- struct rb_node *cursor;
+ unsigned int chunk = KEY_GC_CHUNK_SIZE;
+ unsigned long flags;
struct key *key;
- time64_t new_timer, limit, expiry;
+ time64_t expiry;
- kenter("[%lx,%x]", key_gc_flags, gc_state);
+ kenter("[%lx,%x]", key_gc_flags, gc->state);
- limit = ktime_get_real_seconds();
+ if (gc->state & KEY_GC_DESTROYING)
+ goto destroy_keys;
+ if (gc->cursor) {
+ spin_lock(&key_serial_lock);
+ goto continue_scanning;
+ }
- /* 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;
- if (test_and_clear_bit(KEY_GC_KEY_EXPIRED, &key_gc_flags))
- gc_state |= KEY_GC_REAPING_LINKS;
+ flags = set_mask_bits(&key_gc_flags,
+ BIT(KEY_GC_KEY_EXPIRED) |
+ BIT(KEY_GC_REAP_KEYTYPE) |
+ BIT(KEY_GC_REAP_REQUESTED), 0);
+ gc->start = ktime_get_real_seconds();
- if (test_and_clear_bit(KEY_GC_REAP_KEYTYPE, &key_gc_flags))
- gc_state |= KEY_GC_REAPING_DEAD_1;
- kdebug("new pass %x", gc_state);
+ /* 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;
+ 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);
- new_timer = TIME64_MAX;
+ gc->end = TIME64_MAX;
/* As only this function is permitted to remove things from the key
* serial tree, if cursor is non-NULL then it will always point to a
* valid node in the tree - even if lock got dropped.
*/
spin_lock(&key_serial_lock);
- cursor = rb_first(&key_serial_tree);
+ gc->cursor = rb_first(&key_serial_tree);
continue_scanning:
- while (cursor) {
- key = rb_entry(cursor, struct key, serial_node);
- cursor = rb_next(cursor);
+ while (gc->cursor && chunk) {
+ key = rb_entry(gc->cursor, struct key, serial_node);
+ gc->cursor = rb_next(gc->cursor);
+ chunk--;
if (!test_bit_acquire(KEY_FLAG_USER_ALIVE, &key->flags)) {
/* Clobber key->user after final put seen. */
goto found_unreferenced_key;
}
- if (unlikely(gc_state & KEY_GC_REAPING_DEAD_1)) {
+ if (unlikely(gc->state & KEY_GC_REAPING_DEAD_1)) {
if (key->type == key_gc_dead_keytype) {
- gc_state |= KEY_GC_FOUND_DEAD_KEY;
+ gc->state |= KEY_GC_FOUND_DEAD_KEY;
set_bit(KEY_FLAG_DEAD, &key->flags);
key->perm = 0;
goto skip_dead_key;
@@ -243,24 +278,24 @@ static void key_garbage_collector(struct work_struct *work)
if (expiry != TIME64_MAX) {
if (!(key->type->flags & KEY_TYPE_INSTANT_REAP))
expiry += key_gc_delay;
- if (expiry > limit && expiry < new_timer) {
+ if (expiry > gc->start && expiry < gc->end) {
kdebug("will expire %x in %lld",
- key_serial(key), key->expiry - limit);
- new_timer = key->expiry;
+ key_serial(key), expiry - gc->start);
+ gc->end = expiry;
}
}
- if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2))
+ if (unlikely(gc->state & KEY_GC_REAPING_DEAD_2))
if (key->type == key_gc_dead_keytype)
- gc_state |= KEY_GC_FOUND_DEAD_KEY;
+ gc->state |= KEY_GC_FOUND_DEAD_KEY;
- if ((gc_state & KEY_GC_REAPING_LINKS) ||
- unlikely(gc_state & KEY_GC_REAPING_DEAD_2)) {
+ 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 (unlikely(gc->state & KEY_GC_REAPING_DEAD_3))
if (key->type == key_gc_dead_keytype)
goto destroy_dead_key;
@@ -273,7 +308,11 @@ static void key_garbage_collector(struct work_struct *work)
spin_unlock(&key_serial_lock);
maybe_resched:
- if (cursor) {
+ if (gc->cursor) {
+ if (!chunk) {
+ queue_work(key_gc_wq, &key_gc_work.work);
+ goto out;
+ }
cond_resched();
spin_lock(&key_serial_lock);
goto continue_scanning;
@@ -285,13 +324,11 @@ static void key_garbage_collector(struct work_struct *work)
*/
kdebug("pass complete");
- if (new_timer != TIME64_MAX) {
- new_timer += key_gc_delay;
- key_schedule_gc(new_timer);
- }
+ 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))) {
+ 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
@@ -301,7 +338,7 @@ static void key_garbage_collector(struct work_struct *work)
synchronize_srcu(&key_type_srcu);
}
- if (unlikely(gc_state & KEY_GC_REAPING_DEAD_2) ||
+ if (unlikely(gc->state & KEY_GC_REAPING_DEAD_2) ||
!list_empty(&graveyard)) {
/* Make sure that all pending keyring payload destructions are
* fulfilled and that people aren't now looking at dead or
@@ -312,33 +349,43 @@ static void key_garbage_collector(struct work_struct *work)
synchronize_rcu();
}
+ gc->state |= KEY_GC_DESTROYING;
+
+destroy_keys:
if (!list_empty(&graveyard)) {
kdebug("gc keys");
key_gc_unused_keys(&graveyard);
+ if (!list_empty(&graveyard)) {
+ queue_work(key_gc_wq, &key_gc_work.work);
+ goto out;
+ }
}
+ 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)) {
+ 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;
+ 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;
+ gc->state |= KEY_GC_REAP_AGAIN;
}
}
- if (unlikely(gc_state & KEY_GC_REAPING_DEAD_3)) {
+ 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)
- queue_work(key_gc_wq, &key_gc_work);
- kleave(" [end %x]", gc_state);
+ 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);
+out:
+ kleave(" [end %x]", gc->state);
return;
/* We found an unreferenced key - once we've removed it from the tree,
@@ -350,7 +397,7 @@ static void key_garbage_collector(struct work_struct *work)
spin_unlock(&key_serial_lock);
list_add_tail(&key->graveyard_link, &graveyard);
- gc_state |= KEY_GC_REAP_AGAIN;
+ gc->state |= KEY_GC_REAP_AGAIN;
goto maybe_resched;
/* We found a restricted keyring and need to update the restriction if
@@ -368,7 +415,7 @@ static void key_garbage_collector(struct work_struct *work)
*/
found_keyring:
spin_unlock(&key_serial_lock);
- keyring_gc(key, limit);
+ keyring_gc(key, gc->start);
goto maybe_resched;
/* We found a dead key that is still referenced. Reset its type and
diff --git a/security/keys/internal.h b/security/keys/internal.h
index e7a999d226d3..59e743f24e61 100644
--- a/security/keys/internal.h
+++ b/security/keys/internal.h
@@ -164,7 +164,6 @@ extern bool lookup_user_key_possessed(const struct key *key,
extern long join_session_keyring(const char *name);
extern void key_change_session_keyring(struct callback_head *twork);
-extern struct work_struct key_gc_work;
extern unsigned key_gc_delay;
extern struct workqueue_struct *key_gc_wq;
extern void keyring_gc(struct key *keyring, time64_t limit);
@@ -172,7 +171,7 @@ 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);
-extern void key_schedule_gc_links(void);
+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,
diff --git a/security/keys/key.c b/security/keys/key.c
index 4f38b58b1a45..ec789a264412 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -742,7 +742,7 @@ void key_put(struct key *key)
}
/* Mark key as safe for GC after key->user done. */
clear_bit_unlock(KEY_FLAG_USER_ALIVE, &key->flags);
- queue_work(key_gc_wq, &key_gc_work);
+ key_schedule_gc_work(false);
}
}
}
@@ -1263,7 +1263,7 @@ void key_invalidate(struct key *key)
down_write_nested(&key->sem, 1);
if (!test_and_set_bit(KEY_FLAG_INVALIDATED, &key->flags)) {
notify_key(key, NOTIFY_KEY_INVALIDATED, 0);
- key_schedule_gc_links();
+ key_schedule_gc_work(true);
}
up_write(&key->sem);
}
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 357f56360810..5aa8d706c238 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -265,7 +265,7 @@ void key_remove_domain(struct key_tag *domain_tag)
{
domain_tag->removed = true;
if (!key_put_tag(domain_tag))
- key_schedule_gc_links();
+ key_schedule_gc_work(true);
}
/*
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 03/14] KEYS: Add key_gc_flush() for draining RCU callbacks
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 04/14] dns_resolver: Move the key type into a separate module Jarkko Sakkinen
` (11 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
keyring_clear() and key_unlink() release keyring links via call_rcu(),
and key_put() defers final key destruction to key_gc_work.
Once keys and keyring restrictions pin their key type, callers that
clear an internal cache before module unload need a synchronous barrier
so module references can be dropped before delete_module() checks the
refcount.
Track active and pending-GC reference counts on struct key_type and add
key_gc_flush(type) to drain pending RCU callbacks and synchronously reap
unreferenced keys referencing that type in a single bounded pass without
waiting on unrelated or newly created keys.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
include/linux/key-type.h | 2 +
include/linux/key.h | 6 ++
security/keys/gc.c | 165 +++++++++++++++++++++++++++++++++++----
security/keys/key.c | 8 ++
security/keys/keyring.c | 17 ++--
5 files changed, 176 insertions(+), 22 deletions(-)
diff --git a/include/linux/key-type.h b/include/linux/key-type.h
index c4f329b6ae3f..668793bf613a 100644
--- a/include/linux/key-type.h
+++ b/include/linux/key-type.h
@@ -162,6 +162,8 @@ struct key_type {
/* internal fields */
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 list_head link; /* link in types list */
struct lock_class_key lock_class; /* key->sem lock class */
} __randomize_layout;
diff --git a/include/linux/key.h b/include/linux/key.h
index 420412aab926..edd852608763 100644
--- a/include/linux/key.h
+++ b/include/linux/key.h
@@ -31,6 +31,7 @@ typedef int32_t key_serial_t;
typedef uint32_t key_perm_t;
struct key;
+struct key_type;
struct net;
#ifdef CONFIG_KEYS
@@ -439,6 +440,7 @@ extern int restrict_link_reject(struct key *keyring,
struct key *restriction_key);
extern int keyring_clear(struct key *keyring);
+void key_gc_flush(struct key_type *type);
extern key_ref_t keyring_search(key_ref_t keyring,
struct key_type *type,
@@ -527,6 +529,10 @@ extern void key_init(void);
#define key_remove_domain(d) do { } while(0)
#define key_lookup(k) NULL
+static inline void key_gc_flush(struct key_type *type)
+{
+}
+
#endif /* CONFIG_KEYS */
#endif /* __KERNEL__ */
#endif /* _LINUX_KEY_H */
diff --git a/security/keys/gc.c b/security/keys/gc.c
index e38ee4839885..ca5c1a6bdd45 100644
--- a/security/keys/gc.c
+++ b/security/keys/gc.c
@@ -5,6 +5,7 @@
* Written by David Howells (dhowells@redhat.com)
*/
+#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/security.h>
#include <keys/keyring-type.h>
@@ -29,6 +30,10 @@ static struct {
.work = __WORK_INITIALIZER(key_gc_work.work, key_garbage_collector),
};
+static LIST_HEAD(key_gc_graveyard);
+static DEFINE_MUTEX(key_gc_lock);
+static DEFINE_MUTEX(key_gc_flush_lock);
+
/*
* Delay between key revocation/expiry in seconds
*/
@@ -163,6 +168,9 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
while (!list_empty(keys) && chunk--) {
struct key *key =
list_entry(keys->next, struct key, graveyard_link);
+ struct key_type *type = key->type;
+ struct key_type *restrict_type =
+ key->restrict_link ? key->restrict_link->keytype : NULL;
short state = key->state;
list_del(&key->graveyard_link);
@@ -176,8 +184,8 @@ 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 && type->destroy)
+ type->destroy(key);
security_key_free(key);
@@ -191,9 +199,107 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
memzero_explicit(key, sizeof(*key));
kmem_cache_free(key_jar, key);
+ if (restrict_type) {
+ atomic_dec_return_release(&restrict_type->nr_keys);
+ atomic_dec_return_release(&restrict_type->gc_pending);
+ }
+ atomic_dec_return_release(&type->nr_keys);
+ atomic_dec_return_release(&type->gc_pending);
}
}
+static unsigned int key_type_refs(const struct key *key,
+ const struct key_type *type)
+{
+ return (key->type == type) +
+ (key->restrict_link && key->restrict_link->keytype == type);
+}
+
+/**
+ * key_gc_flush - Drain unreferenced keys of a given key type
+ * @type: Key type whose unreferenced keys and keyring-unlink RCU callbacks to
+ * drain.
+ *
+ * Context: Process context. May sleep.
+ */
+void key_gc_flush(struct key_type *type)
+{
+ struct key *key, *tmp;
+ struct rb_node *cursor;
+ int detached = 0;
+ LIST_HEAD(graveyard);
+
+ if (!atomic_read_acquire(&type->nr_keys))
+ return;
+
+ rcu_barrier();
+ if (!atomic_read_acquire(&type->gc_pending))
+ return;
+
+ mutex_lock(&key_gc_flush_lock);
+ if (!atomic_read_acquire(&type->gc_pending))
+ goto out_unlock;
+
+ mutex_lock(&key_gc_lock);
+
+ list_for_each_entry_safe(key, tmp, &key_gc_graveyard, graveyard_link) {
+ unsigned int refs = key_type_refs(key, type);
+
+ if (refs) {
+ list_move_tail(&key->graveyard_link, &graveyard);
+ detached += refs;
+ }
+ }
+
+ if (detached < atomic_read_acquire(&type->gc_pending)) {
+ spin_lock(&key_serial_lock);
+ cursor = rb_first(&key_serial_tree);
+ while (cursor) {
+ unsigned int refs;
+
+ key = rb_entry(cursor, struct key, serial_node);
+ cursor = rb_next(cursor);
+ refs = key_type_refs(key, type);
+
+ if (refs &&
+ !test_bit_acquire(KEY_FLAG_USER_ALIVE,
+ &key->flags)) {
+ if (key_gc_work.cursor == &key->serial_node)
+ key_gc_work.cursor = cursor;
+ rb_erase(&key->serial_node, &key_serial_tree);
+ list_add_tail(&key->graveyard_link, &graveyard);
+ detached += refs;
+ if (detached >=
+ atomic_read_acquire(&type->gc_pending))
+ break;
+ }
+
+ if (cursor &&
+ (spin_is_contended(&key_serial_lock) ||
+ need_resched())) {
+ spin_unlock(&key_serial_lock);
+ cond_resched();
+ spin_lock(&key_serial_lock);
+ }
+ }
+ spin_unlock(&key_serial_lock);
+ }
+
+ mutex_unlock(&key_gc_lock);
+
+ if (!list_empty(&graveyard)) {
+ synchronize_rcu();
+ while (!list_empty(&graveyard)) {
+ key_gc_unused_keys(&graveyard);
+ cond_resched();
+ }
+ }
+
+out_unlock:
+ mutex_unlock(&key_gc_flush_lock);
+}
+EXPORT_SYMBOL_GPL(key_gc_flush);
+
/*
* Garbage collector for unused keys.
*
@@ -203,11 +309,11 @@ static noinline void key_gc_unused_keys(struct list_head *keys)
*/
static void key_garbage_collector(struct work_struct *work)
{
- static LIST_HEAD(graveyard);
typeof(key_gc_work) *gc = &key_gc_work;
#define KEY_GC_REAP_AGAIN 0x01 /* - Need another cycle */
#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 */
@@ -220,9 +326,11 @@ static void key_garbage_collector(struct work_struct *work)
kenter("[%lx,%x]", key_gc_flags, gc->state);
+ mutex_lock(&key_gc_lock);
+
if (gc->state & KEY_GC_DESTROYING)
goto destroy_keys;
- if (gc->cursor) {
+ if (gc->state & KEY_GC_SCANNING) {
spin_lock(&key_serial_lock);
goto continue_scanning;
}
@@ -244,17 +352,20 @@ static void key_garbage_collector(struct work_struct *work)
gc->end = TIME64_MAX;
- /* As only this function is permitted to remove things from the key
- * serial tree, if cursor is non-NULL then it will always point to a
- * valid node in the tree - even if lock got dropped.
+ /* Only holders of key_gc_lock remove entries from key_serial_tree,
+ * so a non-NULL cursor remains valid when key_serial_lock is dropped.
*/
spin_lock(&key_serial_lock);
gc->cursor = rb_first(&key_serial_tree);
+ if (gc->cursor)
+ gc->state |= KEY_GC_SCANNING;
continue_scanning:
while (gc->cursor && chunk) {
key = rb_entry(gc->cursor, struct key, serial_node);
gc->cursor = rb_next(gc->cursor);
+ if (!gc->cursor)
+ gc->state &= ~KEY_GC_SCANNING;
chunk--;
if (!test_bit_acquire(KEY_FLAG_USER_ALIVE, &key->flags)) {
@@ -270,7 +381,9 @@ static void key_garbage_collector(struct work_struct *work)
goto skip_dead_key;
} else if (key->type == &key_type_keyring &&
key->restrict_link) {
- goto found_restricted_keyring;
+ if (refcount_inc_not_zero(&key->usage))
+ goto found_restricted_keyring;
+ goto wait_unreferenced_key;
}
}
@@ -295,9 +408,13 @@ static void key_garbage_collector(struct work_struct *work)
goto found_keyring;
}
- if (unlikely(gc->state & KEY_GC_REAPING_DEAD_3))
- if (key->type == key_gc_dead_keytype)
- goto destroy_dead_key;
+ 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;
+ }
+ }
skip_dead_key:
if (spin_is_contended(&key_serial_lock) || need_resched())
@@ -305,10 +422,12 @@ static void key_garbage_collector(struct work_struct *work)
}
contended:
+ if (!gc->cursor)
+ gc->state &= ~KEY_GC_SCANNING;
spin_unlock(&key_serial_lock);
maybe_resched:
- if (gc->cursor) {
+ if (gc->state & KEY_GC_SCANNING) {
if (!chunk) {
queue_work(key_gc_wq, &key_gc_work.work);
goto out;
@@ -339,7 +458,7 @@ static void key_garbage_collector(struct work_struct *work)
}
if (unlikely(gc->state & KEY_GC_REAPING_DEAD_2) ||
- !list_empty(&graveyard)) {
+ !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
@@ -352,10 +471,10 @@ static void key_garbage_collector(struct work_struct *work)
gc->state |= KEY_GC_DESTROYING;
destroy_keys:
- if (!list_empty(&graveyard)) {
+ if (!list_empty(&key_gc_graveyard)) {
kdebug("gc keys");
- key_gc_unused_keys(&graveyard);
- if (!list_empty(&graveyard)) {
+ key_gc_unused_keys(&key_gc_graveyard);
+ if (!list_empty(&key_gc_graveyard)) {
queue_work(key_gc_wq, &key_gc_work.work);
goto out;
}
@@ -385,9 +504,17 @@ static void key_garbage_collector(struct work_struct *work)
test_bit(KEY_GC_REAP_REQUESTED, &key_gc_flags))
queue_work(key_gc_wq, &key_gc_work.work);
out:
+ mutex_unlock(&key_gc_lock);
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.
*/
@@ -396,7 +523,7 @@ static void key_garbage_collector(struct work_struct *work)
rb_erase(&key->serial_node, &key_serial_tree);
spin_unlock(&key_serial_lock);
- list_add_tail(&key->graveyard_link, &graveyard);
+ list_add_tail(&key->graveyard_link, &key_gc_graveyard);
gc->state |= KEY_GC_REAP_AGAIN;
goto maybe_resched;
@@ -406,6 +533,7 @@ static void key_garbage_collector(struct work_struct *work)
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
@@ -425,10 +553,13 @@ static void key_garbage_collector(struct work_struct *work)
spin_unlock(&key_serial_lock);
kdebug("destroy key %d", key->serial);
down_write(&key->sem);
+ atomic_inc(&key_type_dead.nr_keys);
+ 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/key.c b/security/keys/key.c
index ec789a264412..fecf8c3a991c 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -235,6 +235,8 @@ struct key *key_alloc(struct key_type *type, const char *desc,
key = ERR_PTR(-EINVAL);
if (!desc || !*desc)
goto error;
+ if (restrict_link && type != &key_type_keyring)
+ goto error;
if (type->vet_description) {
ret = type->vet_description(desc);
@@ -322,6 +324,9 @@ struct key *key_alloc(struct key_type *type, const char *desc,
/* publish the key by giving it a serial number */
refcount_inc(&key->domain_tag->usage);
atomic_inc(&user->nkeys);
+ atomic_inc(&type->nr_keys);
+ if (restrict_link && restrict_link->keytype)
+ atomic_inc(&restrict_link->keytype->nr_keys);
key_alloc_serial(key);
error:
@@ -741,6 +746,9 @@ void key_put(struct key *key)
spin_unlock_irqrestore(&key->user->lock, flags);
}
/* Mark key as safe for GC after key->user done. */
+ if (key->restrict_link && key->restrict_link->keytype)
+ atomic_inc(&key->restrict_link->keytype->gc_pending);
+ atomic_inc(&key->type->gc_pending);
clear_bit_unlock(KEY_FLAG_USER_ALIVE, &key->flags);
key_schedule_gc_work(false);
}
diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 5aa8d706c238..4f906073d312 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -540,6 +540,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)
+ atomic_dec_return_release(&restrict_link->keytype->nr_keys);
+ keyring->restrict_link = NULL;
key_put(keyring);
keyring = ERR_PTR(ret);
}
@@ -1081,6 +1084,8 @@ int keyring_restrict(key_ref_t keyring_ref, const char *type,
} else if (keyring_detect_restriction_cycle(keyring, restrict_link)) {
ret = -EDEADLK;
} else {
+ if (restrict_link->keytype)
+ atomic_inc(&restrict_link->keytype->nr_keys);
keyring->restrict_link = restrict_link;
notify_key(keyring, NOTIFY_KEY_SETATTR, 0);
}
@@ -1808,12 +1813,14 @@ void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type)
down_write(&keyring->sem);
keyres = keyring->restrict_link;
+ if (keyres->keytype == dead_type) {
+ keyres->check = restrict_link_reject;
- keyres->check = restrict_link_reject;
-
- key_put(keyres->key);
- keyres->key = NULL;
- keyres->keytype = NULL;
+ key_put(keyres->key);
+ keyres->key = NULL;
+ atomic_dec_return_release(&dead_type->nr_keys);
+ keyres->keytype = NULL;
+ }
up_write(&keyring->sem);
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 04/14] dns_resolver: Move the key type into a separate module
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (2 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads Jarkko Sakkinen
` (10 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
Once keys pin to the kernel module of their key type, having the DNS key
type in the same modules as the resolver will introduce a circular
dependency preventing module_exit() from ever running.
Introduce dns_resolver_key.ko containing key_type_dns_resolver. On module
exit, clear the resolver keyring, drop the credentials, and call
key_gc_flush() to drain unreferenced DNS resolver keys.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
Documentation/networking/dns_resolver.rst | 8 ++-
net/Makefile | 2 +-
net/dns_resolver/Kconfig | 15 +++++
net/dns_resolver/Makefile | 4 +-
net/dns_resolver/dns_key.c | 65 ++++-----------------
net/dns_resolver/dns_query.c | 70 +++++++++++++++++++++++
net/dns_resolver/internal.h | 7 ---
7 files changed, 104 insertions(+), 67 deletions(-)
diff --git a/Documentation/networking/dns_resolver.rst b/Documentation/networking/dns_resolver.rst
index 52f298834db6..898f4d7e10da 100644
--- a/Documentation/networking/dns_resolver.rst
+++ b/Documentation/networking/dns_resolver.rst
@@ -39,6 +39,7 @@ Compilation
The module should be enabled by turning on the kernel configuration options::
+ CONFIG_DNS_KEY - tristate "DNS Resolver key type"
CONFIG_DNS_RESOLVER - tristate "DNS Resolver support"
@@ -116,8 +117,8 @@ Keys of dns_resolver type can be read from userspace using keyctl_read() or
Mechanism
=========
-The dns_resolver module registers a key type called "dns_resolver". Keys of
-this type are used to transport and cache DNS lookup results from userspace.
+The dns_resolver_key module registers a key type called "dns_resolver". Keys
+of this type are used to transport and cache DNS lookup results from userspace.
When dns_query() is invoked, it calls request_key() to search the local
keyrings for a cached DNS result. If that fails to find one, it upcalls to
@@ -148,6 +149,7 @@ Debugging
=========
Debugging messages can be turned on dynamically by writing a 1 into the
-following file::
+following files::
/sys/module/dns_resolver/parameters/debug
+ /sys/module/dns_resolver_key/parameters/debug
diff --git a/net/Makefile b/net/Makefile
index 5b2dd7f07a85..69c16a7851b4 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -55,7 +55,7 @@ obj-$(CONFIG_IEEE802154) += ieee802154/
obj-$(CONFIG_MAC802154) += mac802154/
obj-$(CONFIG_SYSCTL) += sysctl_net.o
-obj-$(CONFIG_DNS_RESOLVER) += dns_resolver/
+obj-$(CONFIG_DNS_KEY) += dns_resolver/
obj-$(CONFIG_CEPH_LIB) += ceph/
obj-$(CONFIG_BATMAN_ADV) += batman-adv/
obj-$(CONFIG_NFC) += nfc/
diff --git a/net/dns_resolver/Kconfig b/net/dns_resolver/Kconfig
index 7c2dba273e35..2618bad2e771 100644
--- a/net/dns_resolver/Kconfig
+++ b/net/dns_resolver/Kconfig
@@ -1,10 +1,25 @@
# SPDX-License-Identifier: GPL-2.0-only
+
+#
+# Configuration for DNS Resolver key type
+#
+config DNS_KEY
+ tristate "DNS Resolver key type"
+ depends on KEYS
+ help
+ Support the key type used to cache DNS lookups independently of
+ the in-kernel DNS query service.
+
+ To compile this as a module, choose M here: the module is called
+ dns_resolver_key.
+
#
# Configuration for DNS Resolver
#
config DNS_RESOLVER
tristate "DNS Resolver support"
depends on KEYS
+ select DNS_KEY
help
Saying Y here will include support for the DNS Resolver key type
which can be used to make upcalls to perform DNS lookups in
diff --git a/net/dns_resolver/Makefile b/net/dns_resolver/Makefile
index 877532d662d0..586e4c6514de 100644
--- a/net/dns_resolver/Makefile
+++ b/net/dns_resolver/Makefile
@@ -3,6 +3,8 @@
# Makefile for the Linux DNS Resolver.
#
+obj-$(CONFIG_DNS_KEY) += dns_resolver_key.o
obj-$(CONFIG_DNS_RESOLVER) += dns_resolver.o
-dns_resolver-y := dns_key.o dns_query.o
+dns_resolver-y := dns_query.o
+dns_resolver_key-y := dns_key.o
diff --git a/net/dns_resolver/dns_key.c b/net/dns_resolver/dns_key.c
index 451247864a63..7eecbefa1816 100644
--- a/net/dns_resolver/dns_key.c
+++ b/net/dns_resolver/dns_key.c
@@ -9,6 +9,7 @@
* Wang Lei (wang840925@gmail.com)
* David Howells (dhowells@redhat.com)
*/
+
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
@@ -22,15 +23,13 @@
#include <keys/user-type.h>
#include "internal.h"
-MODULE_DESCRIPTION("DNS Resolver");
+MODULE_DESCRIPTION("DNS Resolver key type");
MODULE_AUTHOR("Wang Lei");
MODULE_LICENSE("GPL");
-unsigned int dns_resolver_debug;
+static unsigned int dns_resolver_debug;
module_param_named(debug, dns_resolver_debug, uint, 0644);
-MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
-
-const struct cred *dns_resolver_cache;
+MODULE_PARM_DESC(debug, "DNS Resolver key debugging mask");
#define DNS_ERRORNO_OPTION "dnserror"
@@ -318,61 +317,17 @@ struct key_type key_type_dns_resolver = {
.describe = dns_resolver_describe,
.read = dns_resolver_read,
};
+EXPORT_SYMBOL_GPL(key_type_dns_resolver);
-static int __init init_dns_resolver(void)
+static int __init init_dns_key(void)
{
- struct cred *cred;
- struct key *keyring;
- int ret;
-
- /* create an override credential set with a special thread keyring in
- * which DNS requests are cached
- *
- * this is used to prevent malicious redirections from being installed
- * with add_key().
- */
- cred = prepare_kernel_cred(&init_task);
- if (!cred)
- return -ENOMEM;
-
- keyring = keyring_alloc(".dns_resolver",
- GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
- (KEY_POS_ALL & ~KEY_POS_SETATTR) |
- KEY_USR_VIEW | KEY_USR_READ,
- KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
- if (IS_ERR(keyring)) {
- ret = PTR_ERR(keyring);
- goto failed_put_cred;
- }
-
- ret = register_key_type(&key_type_dns_resolver);
- if (ret < 0)
- goto failed_put_key;
-
- /* instruct request_key() to use this special keyring as a cache for
- * the results it looks up */
- set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
- cred->thread_keyring = keyring;
- cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
- dns_resolver_cache = cred;
-
- kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
- return 0;
-
-failed_put_key:
- key_put(keyring);
-failed_put_cred:
- put_cred(cred);
- return ret;
+ return register_key_type(&key_type_dns_resolver);
}
-static void __exit exit_dns_resolver(void)
+static void __exit exit_dns_key(void)
{
- key_revoke(dns_resolver_cache->thread_keyring);
unregister_key_type(&key_type_dns_resolver);
- put_cred(dns_resolver_cache);
}
-module_init(init_dns_resolver)
-module_exit(exit_dns_resolver)
-MODULE_LICENSE("GPL");
+module_init(init_dns_key);
+module_exit(exit_dns_key);
diff --git a/net/dns_resolver/dns_query.c b/net/dns_resolver/dns_query.c
index b8fb9a1245bf..9fd3ac16b19d 100644
--- a/net/dns_resolver/dns_query.c
+++ b/net/dns_resolver/dns_query.c
@@ -24,8 +24,10 @@
*/
#include <linux/module.h>
+#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/cred.h>
+#include <linux/keyctl.h>
#include <linux/dns_resolver.h>
#include <linux/err.h>
#include <net/net_namespace.h>
@@ -35,6 +37,9 @@
#include "internal.h"
+static unsigned int dns_resolver_debug;
+static const struct cred *dns_resolver_cache;
+
/**
* dns_query - Query the DNS
* @net: The network namespace to operate in.
@@ -137,3 +142,68 @@ int dns_query(struct net *net,
return ret;
}
EXPORT_SYMBOL(dns_query);
+
+static int __init init_dns_resolver(void)
+{
+ struct cred *cred;
+ struct key *keyring;
+ int ret;
+
+ /*
+ * Create an override credential set with a special thread keyring in
+ * which DNS requests are cached. This prevents malicious redirections
+ * from being installed with add_key().
+ */
+ cred = prepare_kernel_cred(&init_task);
+ if (!cred)
+ return -ENOMEM;
+
+ keyring = keyring_alloc(".dns_resolver",
+ GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
+ (KEY_POS_ALL & ~KEY_POS_SETATTR) |
+ KEY_USR_VIEW | KEY_USR_READ,
+ KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
+ if (IS_ERR(keyring)) {
+ ret = PTR_ERR(keyring);
+ goto failed_put_cred;
+ }
+
+ /*
+ * Instruct request_key() to use this special keyring as a cache for
+ * the results it looks up.
+ */
+ set_bit(KEY_FLAG_ROOT_CAN_CLEAR, &keyring->flags);
+ cred->thread_keyring = keyring;
+ cred->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
+ dns_resolver_cache = cred;
+
+ kdebug("DNS resolver keyring: %d\n", key_serial(keyring));
+ return 0;
+
+failed_put_cred:
+ put_cred(cred);
+ return ret;
+}
+
+static void __exit exit_dns_resolver(void)
+{
+ int ret;
+
+ ret = keyring_clear(dns_resolver_cache->thread_keyring);
+ key_revoke(dns_resolver_cache->thread_keyring);
+ put_cred(dns_resolver_cache);
+ /* Drain deferred credential and keyring destruction. */
+ if (ret)
+ key_gc_flush(&key_type_keyring);
+ key_gc_flush(&key_type_dns_resolver);
+}
+
+module_init(init_dns_resolver);
+module_exit(exit_dns_resolver);
+
+MODULE_DESCRIPTION("DNS Resolver");
+MODULE_AUTHOR("Wang Lei");
+MODULE_LICENSE("GPL");
+
+module_param_named(debug, dns_resolver_debug, uint, 0644);
+MODULE_PARM_DESC(debug, "DNS Resolver debugging mask");
diff --git a/net/dns_resolver/internal.h b/net/dns_resolver/internal.h
index d0d8edcea092..855e0bbf2dd5 100644
--- a/net/dns_resolver/internal.h
+++ b/net/dns_resolver/internal.h
@@ -18,16 +18,9 @@ enum {
dns_key_error,
};
-/*
- * dns_key.c
- */
-extern const struct cred *dns_resolver_cache;
-
/*
* debug tracing
*/
-extern unsigned int dns_resolver_debug;
-
#define kdebug(FMT, ...) \
do { \
if (unlikely(dns_resolver_debug)) \
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 05/14] NFSv4: Clear idmap keyring cache when the last client unloads
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (3 preceding siblings ...)
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 ` 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
` (9 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
id_resolver_cache->thread_keyring caches id_resolver and
id_resolver_legacy keys up until nfs_idmap_quit() tears down the keyring.
Once keys pin the kernel module of their key type, the cached keys
prevent module_exit() from ever happening.
Track active idmap instances with an atomic counter and clear the idmap
cache keyring via a work item once the last client is destroyed,
flushing synchronously when called from normal process context while
keeping rpciod and nfsiod release paths non-blocking and reclaim-safe.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
fs/nfs/nfs4idmap.c | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/fs/nfs/nfs4idmap.c b/fs/nfs/nfs4idmap.c
index bc397110d977..b97f68ed209d 100644
--- a/fs/nfs/nfs4idmap.c
+++ b/fs/nfs/nfs4idmap.c
@@ -47,6 +47,10 @@
#include <keys/request_key_auth-type.h>
#include <linux/module.h>
#include <linux/user_namespace.h>
+#include <linux/mutex.h>
+#include <linux/ratelimit.h>
+#include <linux/sched.h>
+#include <linux/workqueue.h>
#include "internal.h"
#include "netns.h"
@@ -58,6 +62,12 @@
static const struct cred *id_resolver_cache;
static struct key_type key_type_id_resolver_legacy;
+/* Serializes new idmap admission with deferred idmap cache clearing. */
+static DEFINE_MUTEX(nfs_idmap_cache_mutex);
+static atomic_t nfs_idmap_users = ATOMIC_INIT(0);
+static void nfs_idmap_clear_workfn(struct work_struct *work);
+static DECLARE_WORK(nfs_idmap_clear_work, nfs_idmap_clear_workfn);
+
struct idmap_legacy_upcalldata {
struct rpc_pipe_msg pipe_msg;
struct idmap_msg idmap_msg;
@@ -242,6 +252,7 @@ int nfs_idmap_init(void)
void nfs_idmap_quit(void)
{
+ cancel_work_sync(&nfs_idmap_clear_work);
key_revoke(id_resolver_cache->thread_keyring);
unregister_key_type(&key_type_id_resolver);
unregister_key_type(&key_type_id_resolver_legacy);
@@ -469,7 +480,10 @@ nfs_idmap_new(struct nfs_client *clp)
if (error)
goto err_destroy_pipe;
+ mutex_lock(&nfs_idmap_cache_mutex);
+ atomic_inc(&nfs_idmap_users);
clp->cl_idmap = idmap;
+ mutex_unlock(&nfs_idmap_cache_mutex);
return 0;
err_destroy_pipe:
rpc_destroy_pipe_data(idmap->idmap_pipe);
@@ -479,6 +493,24 @@ nfs_idmap_new(struct nfs_client *clp)
return error;
}
+static void nfs_idmap_clear_workfn(struct work_struct *work)
+{
+ int ret;
+
+ mutex_lock(&nfs_idmap_cache_mutex);
+ if (!atomic_read(&nfs_idmap_users)) {
+ ret = keyring_clear(id_resolver_cache->thread_keyring);
+ if (ret) {
+ pr_warn_ratelimited("NFS: Failed to clear idmap cache: %d; cache pins remain\n",
+ ret);
+ } else {
+ key_gc_flush(&key_type_id_resolver);
+ key_gc_flush(&key_type_id_resolver_legacy);
+ }
+ }
+ mutex_unlock(&nfs_idmap_cache_mutex);
+}
+
void
nfs_idmap_delete(struct nfs_client *clp)
{
@@ -493,6 +525,14 @@ nfs_idmap_delete(struct nfs_client *clp)
rpc_destroy_pipe_data(idmap->idmap_pipe);
put_user_ns(idmap->user_ns);
kfree(idmap);
+
+ if (atomic_dec_and_test(&nfs_idmap_users)) {
+ schedule_work(&nfs_idmap_clear_work);
+ if (!(current->flags &
+ (PF_WQ_WORKER | PF_MEMALLOC | PF_MEMALLOC_NOFS |
+ PF_MEMALLOC_NOIO)))
+ flush_work(&nfs_idmap_clear_work);
+ }
}
static int nfs_idmap_prepare_message(char *desc, struct idmap *idmap,
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 06/14] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (4 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys Jarkko Sakkinen
` (8 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
CIFS caches cifs.idmap and cifs.spnego keys in module-owned keyrings
that are only destroyed in module_exit().
Once keys pin the kernel module of the key type, the cached entries will
retain CIFS pinned after the last SMB session has been torn down.
Clear the idmap and SPNEGO cache keyrings via a work item when the last
session is freed, flushing synchronously from normal process context
while keeping workqueue and noreclaim socket-demux session releases
non-blocking and reclaim-safe.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
fs/smb/client/cifs_spnego.c | 14 +++++++++++++
fs/smb/client/cifsacl.c | 14 +++++++++++++
fs/smb/client/cifsfs.c | 4 ++++
fs/smb/client/cifsproto.h | 9 +++++++++
fs/smb/client/misc.c | 39 +++++++++++++++++++++++++++++++++++--
5 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/fs/smb/client/cifs_spnego.c b/fs/smb/client/cifs_spnego.c
index 44c407275680..8072f7b9811d 100644
--- a/fs/smb/client/cifs_spnego.c
+++ b/fs/smb/client/cifs_spnego.c
@@ -15,6 +15,7 @@
#include <linux/key-type.h>
#include <linux/keyctl.h>
#include <linux/inet.h>
+#include <linux/ratelimit.h>
#include "cifsglob.h"
#include "cifs_spnego.h"
#include "cifs_debug.h"
@@ -190,6 +191,19 @@ cifs_get_spnego_key(struct cifs_ses *sesInfo,
return spnego_key;
}
+void
+cifs_spnego_clear_cache(void)
+{
+ int ret;
+
+ ret = keyring_clear(spnego_cred->thread_keyring);
+ if (ret)
+ pr_warn_ratelimited("Failed to clear SPNEGO cache: %d; cache pins remain\n",
+ ret);
+ else
+ key_gc_flush(&cifs_spnego_key_type);
+}
+
int
init_cifs_spnego(void)
{
diff --git a/fs/smb/client/cifsacl.c b/fs/smb/client/cifsacl.c
index d8f30c35cbe5..814fe4dfbdad 100644
--- a/fs/smb/client/cifsacl.c
+++ b/fs/smb/client/cifsacl.c
@@ -13,6 +13,7 @@
#include <linux/string.h>
#include <linux/keyctl.h>
#include <linux/key-type.h>
+#include <linux/ratelimit.h>
#include <uapi/linux/posix_acl.h>
#include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h>
@@ -539,6 +540,19 @@ sid_to_id(struct cifs_sb_info *cifs_sb, struct smb_sid *psid,
return 0;
}
+void
+cifs_idmap_clear_cache(void)
+{
+ int ret;
+
+ ret = keyring_clear(root_cred->thread_keyring);
+ if (ret)
+ pr_warn_ratelimited("Failed to clear idmap cache: %d; cache pins remain\n",
+ ret);
+ else
+ key_gc_flush(&cifs_idmap_key_type);
+}
+
int
init_cifs_idmap(void)
{
diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 9faba158f8b6..d2eaefe9db69 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -2292,6 +2292,7 @@ init_cifs(void)
out_clean_proc:
cifs_proc_clean();
+ cifs_cancel_session_cache_clear();
exit_cifs_idmap();
out_cifs_swn_init:
#ifdef CONFIG_CIFS_SWN_UPCALL
@@ -2340,6 +2341,9 @@ exit_cifs(void)
unregister_filesystem(&smb3_fs_type);
cifs_proc_clean();
cifs_release_automount_timer();
+ flush_workqueue(serverclose_wq);
+ flush_workqueue(cifsiod_wq);
+ cifs_cancel_session_cache_clear();
exit_cifs_idmap();
#ifdef CONFIG_CIFS_SWN_UPCALL
cifs_genl_exit();
diff --git a/fs/smb/client/cifsproto.h b/fs/smb/client/cifsproto.h
index 15123c7b9373..9304a85a8ab9 100644
--- a/fs/smb/client/cifsproto.h
+++ b/fs/smb/client/cifsproto.h
@@ -58,8 +58,17 @@ do { \
} while (0)
int init_cifs_idmap(void);
void exit_cifs_idmap(void);
+void cifs_idmap_clear_cache(void);
+void cifs_cancel_session_cache_clear(void);
int init_cifs_spnego(void);
void exit_cifs_spnego(void);
+#ifdef CONFIG_CIFS_UPCALL
+void cifs_spnego_clear_cache(void);
+#else
+static inline void cifs_spnego_clear_cache(void)
+{
+}
+#endif
const char *build_path_from_dentry(struct dentry *direntry, void *page);
char *__build_path_from_dentry_optional_prefix(struct dentry *direntry,
void *page, const char *tree,
diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c
index a8f4c8644562..89fb4a685d41 100644
--- a/fs/smb/client/misc.c
+++ b/fs/smb/client/misc.c
@@ -9,7 +9,10 @@
#include <linux/slab.h>
#include <linux/ctype.h>
#include <linux/mempool.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
#include <linux/vmalloc.h>
+#include <linux/workqueue.h>
#include "cifsglob.h"
#include "cifsproto.h"
#include "cifs_debug.h"
@@ -33,6 +36,11 @@ struct tcon_list {
struct cifs_tcon *tcon;
};
+/* Serialize session admission with deferred idmap/SPNEGO cache clearing. */
+static DEFINE_MUTEX(cifs_session_mutex);
+static void cifs_cache_clear_workfn(struct work_struct *work);
+static DECLARE_WORK(cifs_cache_clear_work, cifs_cache_clear_workfn);
+
/* The xid serves as a useful identifier for each incoming vfs request,
in a similar way to the mid which is useful to track each sent smb,
and CurrentXid can also provide a running counter (although it
@@ -74,7 +82,6 @@ sesInfoAlloc(void)
ret_buf = kzalloc_obj(struct cifs_ses);
if (ret_buf) {
- atomic_inc(&sesInfoAllocCount);
spin_lock_init(&ret_buf->ses_lock);
ret_buf->ses_status = SES_NEW;
++ret_buf->ses_count;
@@ -84,10 +91,31 @@ sesInfoAlloc(void)
spin_lock_init(&ret_buf->iface_lock);
INIT_LIST_HEAD(&ret_buf->iface_list);
spin_lock_init(&ret_buf->chan_lock);
+
+ mutex_lock(&cifs_session_mutex);
+ atomic_inc(&sesInfoAllocCount);
+ mutex_unlock(&cifs_session_mutex);
}
return ret_buf;
}
+static void
+cifs_cache_clear_workfn(struct work_struct *work)
+{
+ mutex_lock(&cifs_session_mutex);
+ if (!atomic_read(&sesInfoAllocCount)) {
+ cifs_idmap_clear_cache();
+ cifs_spnego_clear_cache();
+ }
+ mutex_unlock(&cifs_session_mutex);
+}
+
+void
+cifs_cancel_session_cache_clear(void)
+{
+ cancel_work_sync(&cifs_cache_clear_work);
+}
+
void
sesInfoFree(struct cifs_ses *buf_to_free)
{
@@ -99,7 +127,6 @@ sesInfoFree(struct cifs_ses *buf_to_free)
}
unload_nls(buf_to_free->local_nls);
- atomic_dec(&sesInfoAllocCount);
kfree(buf_to_free->serverOS);
kfree(buf_to_free->serverDomain);
kfree(buf_to_free->serverNOS);
@@ -115,6 +142,14 @@ sesInfoFree(struct cifs_ses *buf_to_free)
kref_put(&iface->refcount, release_iface);
spin_unlock(&buf_to_free->iface_lock);
kfree_sensitive(buf_to_free);
+
+ if (atomic_dec_and_test(&sesInfoAllocCount)) {
+ schedule_work(&cifs_cache_clear_work);
+ if (!(current->flags &
+ (PF_WQ_WORKER | PF_MEMALLOC | PF_MEMALLOC_NOFS |
+ PF_MEMALLOC_NOIO)))
+ flush_work(&cifs_cache_clear_work);
+ }
}
struct cifs_tcon *
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (5 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 08/14] rxrpc: Release conn->key when init_connection_security() fails Jarkko Sakkinen
` (7 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
cert_store falls back to unregistering and re-registering
key_type_cert_store_key to purge keys when keyring invalidation fails.
Once unregister_key_type() stops reaping live keys via key_type_dead,
that fallback can no longer clear extant certificates.
Register key_type_cert_store_key once during init before creating sysfs
attributes, and track subsystem-created keys explicitly so that the
refresh operation unlinks and invalidates previous entries directly.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
arch/s390/kernel/cert_store.c | 175 ++++++++++++++--------------------
1 file changed, 69 insertions(+), 106 deletions(-)
diff --git a/arch/s390/kernel/cert_store.c b/arch/s390/kernel/cert_store.c
index 6e518ff38322..45220bcc9cdc 100644
--- a/arch/s390/kernel/cert_store.c
+++ b/arch/s390/kernel/cert_store.c
@@ -17,6 +17,7 @@
#include <linux/key.h>
#include <linux/keyctl.h>
#include <linux/kobject.h>
+#include <linux/list.h>
#include <linux/module.h>
#include <linux/rwsem.h>
#include <linux/seq_file.h>
@@ -317,6 +318,18 @@ static int check_certificate_valid(const struct vce *vce)
return check_certificate_hash(vce);
}
+struct cert_store_key {
+ struct list_head list;
+ struct key *key;
+};
+
+static DEFINE_MUTEX(cs_refresh_lock);
+/* Owned references, protected by cs_refresh_lock. */
+static LIST_HEAD(cs_keys);
+static struct key *active_cs_keyring;
+static struct key *cs_keyring_parent;
+
+/* Return an owned reference to the user session keyring. */
static struct key *get_user_session_keyring(void)
{
key_ref_t us_keyring_ref;
@@ -326,139 +339,76 @@ static struct key *get_user_session_keyring(void)
if (IS_ERR(us_keyring_ref)) {
pr_dbf_msg("Couldn't get user session keyring: %ld",
PTR_ERR(us_keyring_ref));
- return ERR_PTR(-ENOKEY);
+ return ERR_CAST(us_keyring_ref);
}
return key_ref_to_ptr(us_keyring_ref);
}
-/* Invalidate all keys from cert_store keyring. */
-static int invalidate_keyring_keys(struct key *keyring)
-{
- unsigned long num_keys, key_index;
- long keyring_payload_len, rc;
- key_serial_t *key_array;
- struct key *current_key;
-
- scoped_guard(rwsem_read, &keyring->sem) {
- keyring_payload_len = key_type_keyring.read(keyring, NULL, 0);
- if (keyring_payload_len <= 0)
- return keyring_payload_len;
-
- num_keys = keyring_payload_len / sizeof(key_serial_t);
- key_array = kzalloc_objs(key_serial_t, num_keys);
- if (!key_array)
- return -ENOMEM;
-
- rc = key_type_keyring.read(keyring, (char *)key_array, keyring_payload_len);
- }
-
- if (rc != keyring_payload_len) {
- pr_dbf_msg("Couldn't read keyring payload");
- rc = rc < 0 ? rc : -EINVAL;
- goto out;
- }
-
- rc = 0;
- for (key_index = 0; key_index < num_keys; key_index++) {
- current_key = key_lookup(key_array[key_index]);
- if (IS_ERR(current_key))
- continue;
- pr_dbf_msg("Invalidating key %08x", current_key->serial);
-
- rc = key_unlink(keyring, current_key);
- if (rc)
- pr_dbf_msg("Couldn't unlink key %08x: %ld", current_key->serial, rc);
- key_invalidate(current_key);
- key_put(current_key);
- if (rc)
- break;
- }
-out:
- kfree(key_array);
- return rc;
-}
-
-static struct key *find_cs_keyring(void)
-{
- struct key *us_keyring;
- key_ref_t cs_keyring_ref;
-
- us_keyring = get_user_session_keyring();
- if (!IS_ERR(us_keyring)) {
- cs_keyring_ref = keyring_search(make_key_ref(us_keyring, true),
- &key_type_keyring,
- CERT_STORE_KEYRING_NAME,
- false);
- key_put(us_keyring);
- if (!IS_ERR(cs_keyring_ref))
- return key_ref_to_ptr(cs_keyring_ref);
- }
- return NULL;
-}
-
static void cleanup_cs_keys(void)
{
- struct key *cs_keyring, *us_keyring;
+ struct cert_store_key *entry, *tmp;
+ int rc;
- cs_keyring = find_cs_keyring();
- if (!cs_keyring)
+ if (!active_cs_keyring)
return;
- pr_dbf_msg("Found cert_store keyring. Purging...");
- /*
- * Remove cert_store_key_type in case invalidation
- * of old cert_store keys failed (= severe error).
- */
- if (invalidate_keyring_keys(cs_keyring))
- unregister_key_type(&key_type_cert_store_key);
-
- keyring_clear(cs_keyring);
- key_invalidate(cs_keyring);
- us_keyring = get_user_session_keyring();
- if (!IS_ERR(us_keyring)) {
- key_unlink(us_keyring, cs_keyring);
- key_put(us_keyring);
+ /* Invalidate even certificates unlinked or linked into other keyrings. */
+ list_for_each_entry_safe(entry, tmp, &cs_keys, list) {
+ key_invalidate(entry->key);
+ key_put(entry->key);
+ list_del(&entry->list);
+ kfree(entry);
}
- key_put(cs_keyring);
+
+ rc = keyring_clear(active_cs_keyring);
+ if (rc)
+ pr_dbf_msg("Couldn't clear cert_store keyring: %d", rc);
+ key_invalidate(active_cs_keyring);
+ rc = key_unlink(cs_keyring_parent, active_cs_keyring);
+ if (rc)
+ pr_dbf_msg("Couldn't unlink cert_store keyring: %d", rc);
+ key_put(active_cs_keyring);
+ key_put(cs_keyring_parent);
+ active_cs_keyring = NULL;
+ cs_keyring_parent = NULL;
}
+/* Return a borrowed reference; cleanup_cs_keys() owns the keyring. */
static struct key *create_cs_keyring(void)
{
struct key_restriction *restriction;
- struct key *cs_keyring, *us_keyring;
+ struct key *cs_keyring, *parent;
/* Cleanup previous cs_keyring and all associated keys if any. */
cleanup_cs_keys();
- us_keyring = get_user_session_keyring();
- if (IS_ERR(us_keyring))
- return NULL;
+ parent = get_user_session_keyring();
+ if (IS_ERR(parent))
+ return parent;
+
restriction = kzalloc_obj(*restriction);
if (!restriction) {
- key_put(us_keyring);
- return NULL;
+ key_put(parent);
+ return ERR_PTR(-ENOMEM);
}
/* Reject all links, the kernel bypasses the restriction. */
restriction->check = restrict_link_reject;
+
cs_keyring = keyring_alloc(CERT_STORE_KEYRING_NAME, GLOBAL_ROOT_UID,
GLOBAL_ROOT_GID, current_cred(),
(KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW | KEY_USR_READ,
KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_SET_KEEP,
- restriction, us_keyring);
- key_put(us_keyring);
+ restriction, parent);
if (IS_ERR(cs_keyring)) {
kfree(restriction);
pr_dbf_msg("Can't allocate cert_store keyring");
- return NULL;
+ key_put(parent);
+ return cs_keyring;
}
+ active_cs_keyring = cs_keyring;
+ cs_keyring_parent = parent;
pr_dbf_msg("Successfully allocated cert_store keyring: %08x", cs_keyring->serial);
- /*
- * In case a previous clean-up ran into an
- * error and unregistered key type.
- */
- register_key_type(&key_type_cert_store_key);
-
return cs_keyring;
}
@@ -496,6 +446,7 @@ static char *get_key_description(struct vcssb *vcssb, const struct vce *vce)
static int create_key_from_vce(struct vcssb *vcssb, struct vce *vce,
struct key *keyring)
{
+ struct cert_store_key *entry;
key_ref_t newkey;
char *desc;
int rc;
@@ -504,6 +455,13 @@ static int create_key_from_vce(struct vcssb *vcssb, struct vce *vce,
if (!desc)
return -ENOMEM;
+ /* Reserve cleanup tracking before the key can be linked elsewhere. */
+ entry = kmalloc_obj(*entry);
+ if (!entry) {
+ rc = -ENOMEM;
+ goto out;
+ }
+
newkey = key_create_or_update(
make_key_ref(keyring, true), CERT_STORE_KEY_TYPE_NAME,
desc, (u8 *)vce + vce->vce_hdr.vc_offset,
@@ -515,10 +473,12 @@ static int create_key_from_vce(struct vcssb *vcssb, struct vce *vce,
if (rc) {
pr_dbf_msg("Couldn't create a key from Certificate Entry (%d)", rc);
rc = -ENOKEY;
+ kfree(entry);
goto out;
}
- key_ref_put(newkey);
+ entry->key = key_ref_to_ptr(newkey);
+ list_add_tail(&entry->list, &cs_keys);
out:
kfree(desc);
return rc;
@@ -738,13 +698,13 @@ static int fill_cs_keyring(void)
if (rc)
goto cleanup_keys;
- rc = -ENOMEM;
cs_keyring = create_cs_keyring();
- if (!cs_keyring)
+ if (IS_ERR(cs_keyring)) {
+ rc = PTR_ERR(cs_keyring);
goto cleanup_keys;
+ }
rc = add_certificates_to_keyring(vcssb, cs_keyring);
- key_put(cs_keyring);
if (rc)
goto cleanup_keys;
@@ -757,7 +717,6 @@ static int fill_cs_keyring(void)
return rc;
}
-static DEFINE_MUTEX(cs_refresh_lock);
static int cs_status_val = -1;
static ssize_t cs_status_show(struct kobject *kobj,
@@ -829,14 +788,18 @@ static int __init cert_store_init(void)
if (!cert_store_kobj)
goto cleanup_dbf;
- rc = sysfs_create_files(cert_store_kobj, cert_store_attrs);
+ rc = register_key_type(&key_type_cert_store_key);
if (rc)
goto cleanup_kobj;
- register_key_type(&key_type_cert_store_key);
+ rc = sysfs_create_files(cert_store_kobj, cert_store_attrs);
+ if (rc)
+ goto cleanup_key_type;
return rc;
+cleanup_key_type:
+ unregister_key_type(&key_type_cert_store_key);
cleanup_kobj:
kobject_put(cert_store_kobj);
cleanup_dbf:
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 08/14] rxrpc: Release conn->key when init_connection_security() fails
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (6 preceding siblings ...)
2026-11-29 17:05 ` [PATCH v7 07/14] s390/cert_store: Start tracking subsystem keys Jarkko Sakkinen
@ 2026-11-29 17:05 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 09/14] rxrpc: Move key types to their own module Jarkko Sakkinen
` (6 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
Clear conn->key before calling key_put() when init_connection_security()
fails, publish conn->key with smp_store_release(), and read its serial
under rcu_read_lock() in rxrpc_connection_seq_show().
Fixes: 17926a79320a ("[AF_RXRPC]: Provide secure RxRPC sockets for use by userspace and kernel both")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
net/rxrpc/conn_event.c | 7 ++++++-
net/rxrpc/key.c | 3 ++-
net/rxrpc/proc.c | 6 +++++-
net/rxrpc/rxgk.c | 3 ++-
4 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/net/rxrpc/conn_event.c b/net/rxrpc/conn_event.c
index 611c790bc6d0..096807f13354 100644
--- a/net/rxrpc/conn_event.c
+++ b/net/rxrpc/conn_event.c
@@ -298,8 +298,13 @@ static int rxrpc_process_event(struct rxrpc_connection *conn,
ret = conn->security->init_connection_security(
conn, conn->key->payload.data[0]);
- if (ret < 0)
+ if (ret < 0) {
+ struct key *key = conn->key;
+
+ WRITE_ONCE(conn->key, NULL);
+ key_put(key);
return ret;
+ }
spin_lock_irq(&conn->state_lock);
if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
diff --git a/net/rxrpc/key.c b/net/rxrpc/key.c
index cbd26da44951..8fb30848011d 100644
--- a/net/rxrpc/key.c
+++ b/net/rxrpc/key.c
@@ -686,7 +686,8 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
if (ret < 0)
goto error;
- conn->key = key;
+ /* Pair with READ_ONCE() in rxrpc_connection_seq_show(). */
+ smp_store_release(&conn->key, key);
_leave(" = 0 [%d]", key_serial(key));
return 0;
diff --git a/net/rxrpc/proc.c b/net/rxrpc/proc.c
index e9a27fa7b25d..8c17514c0c57 100644
--- a/net/rxrpc/proc.c
+++ b/net/rxrpc/proc.c
@@ -146,6 +146,7 @@ static int rxrpc_connection_seq_show(struct seq_file *seq, void *v)
struct rxrpc_connection *conn;
struct rxrpc_net *rxnet = rxrpc_net(seq_file_net(seq));
const char *state;
+ key_serial_t kserial;
char lbuff[RXRPC_PROC_ADDRBUF_SIZE], rbuff[RXRPC_PROC_ADDRBUF_SIZE];
if (v == &rxnet->conn_proc_list) {
@@ -168,6 +169,9 @@ static int rxrpc_connection_seq_show(struct seq_file *seq, void *v)
scnprintf(lbuff, sizeof(lbuff), "%pISpc", &conn->local->srx.transport);
scnprintf(rbuff, sizeof(rbuff), "%pISpc", &conn->peer->srx.transport);
print:
+ rcu_read_lock();
+ kserial = key_serial(READ_ONCE(conn->key));
+ rcu_read_unlock();
state = rxrpc_is_conn_aborted(conn) ?
rxrpc_call_completions[conn->completion] :
rxrpc_conn_states[conn->state];
@@ -182,7 +186,7 @@ static int rxrpc_connection_seq_show(struct seq_file *seq, void *v)
refcount_read(&conn->ref),
atomic_read(&conn->active),
state,
- key_serial(conn->key),
+ kserial,
conn->tx_serial,
conn->hi_serial,
conn->channels[0].call_id,
diff --git a/net/rxrpc/rxgk.c b/net/rxrpc/rxgk.c
index 77a67ace1d24..ad0c883ab5d8 100644
--- a/net/rxrpc/rxgk.c
+++ b/net/rxrpc/rxgk.c
@@ -1255,7 +1255,8 @@ static int rxgk_verify_response(struct rxrpc_connection *conn,
if (ret < 0)
goto out_gk;
- conn->key = key;
+ /* Pair with READ_ONCE() in rxrpc_connection_seq_show(). */
+ smp_store_release(&conn->key, key);
key = NULL;
ret = 0;
out_gk:
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 09/14] rxrpc: Move key types to their own module
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (7 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module Jarkko Sakkinen
` (5 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
Once keys pin the kernel module implementing their key type, hosting rxrpc
and rxrpc_s keys inside the same module prevents unloading the transport
module while keys remain in keyrings or cached connections.
Split the key types and their preparse logic into a separate rxrpc_keys.ko
module that rxrpc.ko depends on, and flush key GC on rxrpc exit.
By doing this, rxrpc.ko can be removed while the persistent keys pin only
rxrpc_keys.ko.
Co-developed-by: David Howells <dhowells@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
Documentation/networking/rxrpc.rst | 2 +-
net/Makefile | 2 +-
net/rxrpc/Kconfig | 20 +++
net/rxrpc/Makefile | 7 +-
net/rxrpc/af_rxrpc.c | 21 +--
net/rxrpc/ar-internal.h | 53 +++---
net/rxrpc/key.c | 129 +++++---------
net/rxrpc/rxgk.c | 66 --------
net/rxrpc/rxkad.c | 47 ------
net/rxrpc/security.c | 185 ++++++++++++++++++++
net/rxrpc/server_key.c | 260 +++++++++++++++++------------
11 files changed, 437 insertions(+), 355 deletions(-)
diff --git a/Documentation/networking/rxrpc.rst b/Documentation/networking/rxrpc.rst
index eca055a536aa..394190c40d6e 100644
--- a/Documentation/networking/rxrpc.rst
+++ b/Documentation/networking/rxrpc.rst
@@ -1165,5 +1165,5 @@ API Function Reference
.. kernel-doc:: net/rxrpc/recvmsg.c
.. kernel-doc:: net/rxrpc/rxgk.c
.. kernel-doc:: net/rxrpc/rxkad.c
+.. kernel-doc:: net/rxrpc/security.c
.. kernel-doc:: net/rxrpc/sendmsg.c
-.. kernel-doc:: net/rxrpc/server_key.c
diff --git a/net/Makefile b/net/Makefile
index 69c16a7851b4..97b2bd2ace7b 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -30,7 +30,7 @@ obj-$(CONFIG_LAPB) += lapb/
obj-$(CONFIG_CAN) += can/
obj-$(CONFIG_BT) += bluetooth/
obj-$(CONFIG_SUNRPC) += sunrpc/
-obj-$(CONFIG_AF_RXRPC) += rxrpc/
+obj-$(CONFIG_RXRPC_KEYS) += rxrpc/
obj-$(CONFIG_AF_KCM) += kcm/
obj-$(CONFIG_STREAM_PARSER) += strparser/
obj-$(CONFIG_ATM) += atm/
diff --git a/net/rxrpc/Kconfig b/net/rxrpc/Kconfig
index 2cebb666dc07..30135ae62bd6 100644
--- a/net/rxrpc/Kconfig
+++ b/net/rxrpc/Kconfig
@@ -3,6 +3,18 @@
# RxRPC session sockets
#
+config RXRPC_KEYS
+ tristate "RxRPC key types"
+ select CRYPTO
+ select CRYPTO_LIB_DES if RXKAD
+ select KEYS
+ help
+ Say Y or M here to include support for rxrpc and rxrpc_s key types
+ independently of RxRPC session sockets.
+
+ To compile this as a module, choose M here: the module will be called
+ rxrpc_keys.
+
config AF_RXRPC
tristate "RxRPC session sockets"
depends on INET
@@ -10,6 +22,7 @@ config AF_RXRPC
select CRYPTO_LIB_DES if RXKAD
select KEYS
select NET_UDP_TUNNEL
+ select RXRPC_KEYS
help
Say Y or M here to include support for RxRPC session sockets (just
the transport part, not the presentation part: (un)marshalling is
@@ -54,6 +67,9 @@ config AF_RXRPC_DEBUG
See Documentation/networking/rxrpc.rst.
+endif
+
+if RXRPC_KEYS
config RXKAD
bool "RxRPC Kerberos security"
@@ -86,6 +102,10 @@ config RXGK
See Documentation/networking/rxrpc.rst.
+endif
+
+if AF_RXRPC
+
config RXPERF
tristate "RxRPC test service"
help
diff --git a/net/rxrpc/Makefile b/net/rxrpc/Makefile
index f994f9f30a29..196e0ce085ba 100644
--- a/net/rxrpc/Makefile
+++ b/net/rxrpc/Makefile
@@ -3,6 +3,7 @@
# Makefile for Linux kernel RxRPC
#
+obj-$(CONFIG_RXRPC_KEYS) += rxrpc_keys.o
obj-$(CONFIG_AF_RXRPC) += rxrpc.o
rxrpc-y := \
@@ -19,7 +20,6 @@ rxrpc-y := \
input_rack.o \
insecure.o \
io_thread.o \
- key.o \
local_event.o \
local_object.o \
misc.o \
@@ -32,7 +32,6 @@ rxrpc-y := \
rtt.o \
security.o \
sendmsg.o \
- server_key.o \
skbuff.o \
txbuf.o \
utils.o
@@ -45,5 +44,9 @@ rxrpc-$(CONFIG_RXGK) += \
rxgk_app.o \
rxgk_kdf.o
+rxrpc_keys-y := \
+ key.o \
+ server_key.o
+
obj-$(CONFIG_RXPERF) += rxperf.o
obj-$(CONFIG_KUNIT) += tests/
diff --git a/net/rxrpc/af_rxrpc.c b/net/rxrpc/af_rxrpc.c
index 9ab0f22c881e..af9aec7444c4 100644
--- a/net/rxrpc/af_rxrpc.c
+++ b/net/rxrpc/af_rxrpc.c
@@ -1077,18 +1077,6 @@ static int __init af_rxrpc_init(void)
goto error_sock;
}
- ret = register_key_type(&key_type_rxrpc);
- if (ret < 0) {
- pr_crit("Cannot register client key type\n");
- goto error_key_type;
- }
-
- ret = register_key_type(&key_type_rxrpc_s);
- if (ret < 0) {
- pr_crit("Cannot register server key type\n");
- goto error_key_type_s;
- }
-
ret = rxrpc_sysctl_init();
if (ret < 0) {
pr_crit("Cannot register sysctls\n");
@@ -1098,10 +1086,6 @@ static int __init af_rxrpc_init(void)
return 0;
error_sysctls:
- unregister_key_type(&key_type_rxrpc_s);
-error_key_type_s:
- unregister_key_type(&key_type_rxrpc);
-error_key_type:
sock_unregister(PF_RXRPC);
error_sock:
proto_unregister(&rxrpc_proto);
@@ -1124,8 +1108,6 @@ static void __exit af_rxrpc_exit(void)
{
_enter("");
rxrpc_sysctl_exit();
- unregister_key_type(&key_type_rxrpc_s);
- unregister_key_type(&key_type_rxrpc);
sock_unregister(PF_RXRPC);
proto_unregister(&rxrpc_proto);
unregister_pernet_device(&rxrpc_net_ops);
@@ -1139,6 +1121,9 @@ static void __exit af_rxrpc_exit(void)
destroy_workqueue(rxrpc_workqueue);
rxrpc_exit_security();
kmem_cache_destroy(rxrpc_call_jar);
+ key_gc_flush(&key_type_keyring);
+ key_gc_flush(&key_type_rxrpc);
+ key_gc_flush(&key_type_rxrpc_s);
_leave("");
}
diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h
index 865f05fe37ab..1bc5ab77b413 100644
--- a/net/rxrpc/ar-internal.h
+++ b/net/rxrpc/ar-internal.h
@@ -273,18 +273,6 @@ struct rxrpc_security {
/* Clean up a security service */
void (*exit)(void);
- /* Parse the information from a server key */
- int (*preparse_server_key)(struct key_preparsed_payload *);
-
- /* Clean up the preparse buffer after parsing a server key */
- void (*free_preparse_server_key)(struct key_preparsed_payload *);
-
- /* Destroy the payload of a server key */
- void (*destroy_server_key)(struct key *);
-
- /* Describe a server key */
- void (*describe_server_key)(const struct key *, struct seq_file *);
-
/* initialise a connection's security */
int (*init_connection_security)(struct rxrpc_connection *,
struct rxrpc_key_token *);
@@ -1307,10 +1295,6 @@ extern const struct rxrpc_security rxrpc_no_security;
*/
extern struct key_type key_type_rxrpc;
-int rxrpc_request_key(struct rxrpc_sock *, sockptr_t , int);
-int rxrpc_get_server_data_key(struct rxrpc_connection *, const void *, time64_t,
- u32);
-
/*
* local_event.c
*/
@@ -1492,6 +1476,11 @@ const struct rxrpc_security *rxrpc_get_incoming_security(struct rxrpc_sock *,
struct sk_buff *);
struct key *rxrpc_look_up_server_security(struct rxrpc_connection *,
struct sk_buff *, u32, u32);
+int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen);
+int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
+ const void *session_key, time64_t expiry,
+ u32 kvno);
+int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen);
/*
* sendmsg.c
@@ -1505,8 +1494,6 @@ int rxrpc_do_sendmsg(struct rxrpc_sock *, struct msghdr *, size_t);
*/
extern struct key_type key_type_rxrpc_s;
-int rxrpc_server_keyring(struct rxrpc_sock *, sockptr_t, int);
-
/*
* skbuff.c
*/
@@ -1618,7 +1605,13 @@ static inline unsigned int rxrpc_tx_in_flight(const struct rxrpc_call *call)
/*
* debug tracing
*/
+#ifdef RXRPC_KEY_PROVIDER
+extern unsigned int rxrpc_keys_debug;
+#define RXRPC_DEBUG_MASK rxrpc_keys_debug
+#else
extern unsigned int rxrpc_debug;
+#define RXRPC_DEBUG_MASK rxrpc_debug
+#endif
#define dbgprintk(FMT,...) \
printk("[%-6.6s] "FMT"\n", current->comm ,##__VA_ARGS__)
@@ -1638,22 +1631,22 @@ extern unsigned int rxrpc_debug;
#define RXRPC_DEBUG_KLEAVE 0x02
#define RXRPC_DEBUG_KDEBUG 0x04
-#define _enter(FMT,...) \
-do { \
- if (unlikely(rxrpc_debug & RXRPC_DEBUG_KENTER)) \
- kenter(FMT,##__VA_ARGS__); \
+#define _enter(FMT, ...) \
+do { \
+ if (unlikely(RXRPC_DEBUG_MASK & RXRPC_DEBUG_KENTER)) \
+ kenter(FMT, ##__VA_ARGS__); \
} while (0)
-#define _leave(FMT,...) \
-do { \
- if (unlikely(rxrpc_debug & RXRPC_DEBUG_KLEAVE)) \
- kleave(FMT,##__VA_ARGS__); \
+#define _leave(FMT, ...) \
+do { \
+ if (unlikely(RXRPC_DEBUG_MASK & RXRPC_DEBUG_KLEAVE)) \
+ kleave(FMT, ##__VA_ARGS__); \
} while (0)
-#define _debug(FMT,...) \
-do { \
- if (unlikely(rxrpc_debug & RXRPC_DEBUG_KDEBUG)) \
- kdebug(FMT,##__VA_ARGS__); \
+#define _debug(FMT, ...) \
+do { \
+ if (unlikely(RXRPC_DEBUG_MASK & RXRPC_DEBUG_KDEBUG)) \
+ kdebug(FMT, ##__VA_ARGS__); \
} while (0)
#else
diff --git a/net/rxrpc/key.c b/net/rxrpc/key.c
index 8fb30848011d..49a1ce28da95 100644
--- a/net/rxrpc/key.c
+++ b/net/rxrpc/key.c
@@ -9,6 +9,7 @@
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#define RXRPC_KEY_PROVIDER
#include <linux/module.h>
#include <linux/net.h>
@@ -615,90 +616,6 @@ static void rxrpc_describe(const struct key *key, struct seq_file *m)
}
}
-/*
- * grab the security key for a socket
- */
-int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
-{
- struct key *key;
- char *description;
-
- _enter("");
-
- if (optlen <= 0 || optlen > PAGE_SIZE - 1 || rx->key)
- return -EINVAL;
-
- description = memdup_sockptr_nul(optval, optlen);
- if (IS_ERR(description))
- return PTR_ERR(description);
-
- key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
- if (IS_ERR(key)) {
- kfree(description);
- _leave(" = %ld", PTR_ERR(key));
- return PTR_ERR(key);
- }
-
- rx->key = key;
- kfree(description);
- _leave(" = 0 [key %x]", key->serial);
- return 0;
-}
-
-/*
- * generate a server data key
- */
-int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
- const void *session_key,
- time64_t expiry,
- u32 kvno)
-{
- const struct cred *cred = current_cred();
- struct key *key;
- int ret;
-
- struct {
- u32 kver;
- struct rxrpc_key_data_v1 v1;
- } data;
-
- _enter("");
-
- key = key_alloc(&key_type_rxrpc, "x",
- GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
- KEY_ALLOC_NOT_IN_QUOTA, NULL);
- if (IS_ERR(key)) {
- _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
- return -ENOMEM;
- }
-
- _debug("key %d", key_serial(key));
-
- data.kver = 1;
- data.v1.security_index = RXRPC_SECURITY_RXKAD;
- data.v1.ticket_length = 0;
- data.v1.expiry = rxrpc_time64_to_u32(expiry);
- data.v1.kvno = 0;
-
- memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
-
- ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
- if (ret < 0)
- goto error;
-
- /* Pair with READ_ONCE() in rxrpc_connection_seq_show(). */
- smp_store_release(&conn->key, key);
- _leave(" = 0 [%d]", key_serial(key));
- return 0;
-
-error:
- key_revoke(key);
- key_put(key);
- _leave(" = -ENOMEM [ins %d]", ret);
- return -ENOMEM;
-}
-EXPORT_SYMBOL(rxrpc_get_server_data_key);
-
/**
* rxrpc_get_null_key - Generate a null RxRPC key
* @keyname: The name to give the key.
@@ -894,3 +811,47 @@ static long rxrpc_read(const struct key *key,
_leave(" = %zu", size);
return size;
}
+
+static int __init rxrpc_keys_init(void)
+{
+ int ret;
+
+ ret = register_key_type(&key_type_rxrpc);
+ if (ret < 0) {
+ pr_crit("Cannot register client key type\n");
+ goto error_key_type;
+ }
+
+ ret = register_key_type(&key_type_rxrpc_s);
+ if (ret < 0) {
+ pr_crit("Cannot register server key type\n");
+ goto error_key_type_s;
+ }
+
+ return 0;
+
+error_key_type_s:
+ unregister_key_type(&key_type_rxrpc);
+
+error_key_type:
+ return ret;
+}
+
+static void __exit rxrpc_keys_exit(void)
+{
+ unregister_key_type(&key_type_rxrpc_s);
+ unregister_key_type(&key_type_rxrpc);
+}
+
+module_init(rxrpc_keys_init);
+module_exit(rxrpc_keys_exit);
+
+#ifdef CONFIG_AF_RXRPC_DEBUG
+unsigned int rxrpc_keys_debug;
+module_param_named(debug, rxrpc_keys_debug, uint, 0644);
+MODULE_PARM_DESC(debug, "RxRPC keys debugging mask");
+#endif
+
+MODULE_DESCRIPTION("RxRPC key types");
+MODULE_AUTHOR("David Howells <dhowells@redhat.com>");
+MODULE_LICENSE("GPL");
diff --git a/net/rxrpc/rxgk.c b/net/rxrpc/rxgk.c
index ad0c883ab5d8..d5bbb50b0cbd 100644
--- a/net/rxrpc/rxgk.c
+++ b/net/rxrpc/rxgk.c
@@ -14,68 +14,6 @@
#include "ar-internal.h"
#include "rxgk_common.h"
-/*
- * Parse the information from a server key
- */
-static int rxgk_preparse_server_key(struct key_preparsed_payload *prep)
-{
- const struct krb5_enctype *krb5;
- struct krb5_buffer *server_key = (void *)&prep->payload.data[2];
- unsigned int service, sec_class, kvno, enctype;
- int n = 0;
-
- _enter("%zu", prep->datalen);
-
- if (sscanf(prep->orig_description, "%u:%u:%u:%u%n",
- &service, &sec_class, &kvno, &enctype, &n) != 4)
- return -EINVAL;
-
- if (prep->orig_description[n])
- return -EINVAL;
-
- krb5 = crypto_krb5_find_enctype(enctype);
- if (!krb5)
- return -ENOPKG;
-
- prep->payload.data[0] = (struct krb5_enctype *)krb5;
-
- if (prep->datalen != krb5->key_len)
- return -EKEYREJECTED;
-
- server_key->len = prep->datalen;
- server_key->data = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
- if (!server_key->data)
- return -ENOMEM;
-
- _leave(" = 0");
- return 0;
-}
-
-static void rxgk_free_server_key(union key_payload *payload)
-{
- struct krb5_buffer *server_key = (void *)&payload->data[2];
-
- kfree_sensitive(server_key->data);
-}
-
-static void rxgk_free_preparse_server_key(struct key_preparsed_payload *prep)
-{
- rxgk_free_server_key(&prep->payload);
-}
-
-static void rxgk_destroy_server_key(struct key *key)
-{
- rxgk_free_server_key(&key->payload);
-}
-
-static void rxgk_describe_server_key(const struct key *key, struct seq_file *m)
-{
- const struct krb5_enctype *krb5 = key->payload.data[0];
-
- if (krb5)
- seq_printf(m, ": %s", krb5->name);
-}
-
/*
* Handle rekeying the connection when we see our limits overrun or when the
* far side decided to rekey.
@@ -1336,10 +1274,6 @@ const struct rxrpc_security rxgk_yfs = {
.no_key_abort = RXGK_NOTAUTH,
.init = rxgk_init,
.exit = rxgk_exit,
- .preparse_server_key = rxgk_preparse_server_key,
- .free_preparse_server_key = rxgk_free_preparse_server_key,
- .destroy_server_key = rxgk_destroy_server_key,
- .describe_server_key = rxgk_describe_server_key,
.init_connection_security = rxgk_init_connection_security,
.alloc_txbuf = rxgk_alloc_txbuf,
.secure_packet = rxgk_secure_packet,
diff --git a/net/rxrpc/rxkad.c b/net/rxrpc/rxkad.c
index ca9f0e82cb9a..2936c6effae3 100644
--- a/net/rxrpc/rxkad.c
+++ b/net/rxrpc/rxkad.c
@@ -47,50 +47,6 @@ struct rxkad_level2_hdr {
static void rxkad_prime_packet_security(struct rxrpc_connection *conn,
const struct fcrypt_key *cipher);
-/*
- * Parse the information from a server key
- *
- * The data should be the 8-byte secret key.
- */
-static int rxkad_preparse_server_key(struct key_preparsed_payload *prep)
-{
- struct des_ctx *des_key;
- int err;
-
- if (prep->datalen != 8)
- return -EINVAL;
-
- memcpy(&prep->payload.data[2], prep->data, 8);
-
- des_key = kmalloc_obj(*des_key);
- if (!des_key) {
- _leave(" = -ENOMEM");
- return -ENOMEM;
- }
-
- err = des_expand_key(des_key, prep->data, 8);
- if (err) {
- kfree_sensitive(des_key);
- _leave(" = %d", err);
- return err;
- }
-
- prep->payload.data[0] = des_key;
- _leave(" = 0");
- return 0;
-}
-
-static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep)
-{
- kfree_sensitive(prep->payload.data[0]);
-}
-
-static void rxkad_destroy_server_key(struct key *key)
-{
- kfree_sensitive(key->payload.data[0]);
- key->payload.data[0] = NULL;
-}
-
/*
* initialise connection security
*/
@@ -1124,9 +1080,6 @@ const struct rxrpc_security rxkad = {
.no_key_abort = RXKADUNKNOWNKEY,
.init = rxkad_init,
.exit = rxkad_exit,
- .preparse_server_key = rxkad_preparse_server_key,
- .free_preparse_server_key = rxkad_free_preparse_server_key,
- .destroy_server_key = rxkad_destroy_server_key,
.init_connection_security = rxkad_init_connection_security,
.alloc_txbuf = rxkad_alloc_txbuf,
.secure_packet = rxkad_secure_packet,
diff --git a/net/rxrpc/security.c b/net/rxrpc/security.c
index 2bfbf2b2bb37..288a954d20c4 100644
--- a/net/rxrpc/security.c
+++ b/net/rxrpc/security.c
@@ -10,6 +10,8 @@
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/crypto.h>
+#include <linux/key-type.h>
+#include <linux/slab.h>
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <keys/rxrpc-type.h>
@@ -208,3 +210,186 @@ struct key *rxrpc_look_up_server_security(struct rxrpc_connection *conn,
read_unlock(&conn->local->services_lock);
return key;
}
+
+/*
+ * grab the security key for a socket
+ */
+int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
+{
+ struct key *key;
+ char *description;
+
+ _enter("");
+
+ if (optlen <= 0 || optlen > PAGE_SIZE - 1 || rx->key)
+ return -EINVAL;
+
+ description = memdup_sockptr_nul(optval, optlen);
+ if (IS_ERR(description))
+ return PTR_ERR(description);
+
+ key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
+ if (IS_ERR(key)) {
+ kfree(description);
+ _leave(" = %ld", PTR_ERR(key));
+ return PTR_ERR(key);
+ }
+
+ rx->key = key;
+ kfree(description);
+ _leave(" = 0 [key %x]", key->serial);
+ return 0;
+}
+
+/*
+ * generate a server data key
+ */
+int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
+ const void *session_key,
+ time64_t expiry,
+ u32 kvno)
+{
+ const struct cred *cred = current_cred();
+ struct {
+ u32 kver;
+ struct rxrpc_key_data_v1 v1;
+ } data;
+ struct key *key;
+ int ret;
+
+ _enter("");
+
+ key = key_alloc(&key_type_rxrpc, "x",
+ GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
+ KEY_ALLOC_NOT_IN_QUOTA, NULL);
+ if (IS_ERR(key)) {
+ _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
+ return -ENOMEM;
+ }
+
+ _debug("key %d", key_serial(key));
+
+ data.kver = 1;
+ data.v1.security_index = RXRPC_SECURITY_RXKAD;
+ data.v1.ticket_length = 0;
+ data.v1.expiry = rxrpc_time64_to_u32(expiry);
+ data.v1.kvno = 0;
+
+ memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
+
+ ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
+ if (ret < 0)
+ goto error;
+
+ /* Pair with READ_ONCE() in rxrpc_connection_seq_show(). */
+ smp_store_release(&conn->key, key);
+ _leave(" = 0 [%d]", key_serial(key));
+ return 0;
+
+error:
+ key_revoke(key);
+ key_put(key);
+ _leave(" = -ENOMEM [ins %d]", ret);
+ return -ENOMEM;
+}
+EXPORT_SYMBOL(rxrpc_get_server_data_key);
+
+/*
+ * grab the security keyring for a server socket
+ */
+int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
+{
+ struct key *key;
+ char *description;
+
+ _enter("");
+
+ if (rx->securities)
+ return -EINVAL;
+
+ if (optlen <= 0 || optlen > PAGE_SIZE - 1)
+ return -EINVAL;
+
+ description = memdup_sockptr_nul(optval, optlen);
+ if (IS_ERR(description))
+ return PTR_ERR(description);
+
+ key = request_key(&key_type_keyring, description, NULL);
+ if (IS_ERR(key)) {
+ kfree(description);
+ _leave(" = %ld", PTR_ERR(key));
+ return PTR_ERR(key);
+ }
+
+ rx->securities = key;
+ kfree(description);
+ _leave(" = 0 [key %x]", key->serial);
+ return 0;
+}
+
+/**
+ * rxrpc_sock_set_security_keyring - Set the security keyring for a kernel service
+ * @sk: The socket to set the keyring on
+ * @keyring: The keyring to set
+ *
+ * Set the server security keyring on an rxrpc socket. This is used to provide
+ * the encryption keys for a kernel service.
+ *
+ * Return: %0 if successful and a negative error code otherwise.
+ */
+int rxrpc_sock_set_security_keyring(struct sock *sk, struct key *keyring)
+{
+ struct rxrpc_sock *rx = rxrpc_sk(sk);
+ int ret = 0;
+
+ lock_sock(sk);
+ if (rx->securities)
+ ret = -EINVAL;
+ else if (rx->sk.sk_state != RXRPC_UNBOUND)
+ ret = -EISCONN;
+ else
+ rx->securities = key_get(keyring);
+ release_sock(sk);
+ return ret;
+}
+EXPORT_SYMBOL(rxrpc_sock_set_security_keyring);
+
+/**
+ * rxrpc_sock_set_manage_response - Set the manage-response flag for a kernel service
+ * @sk: The socket to set the keyring on
+ * @set: True to set, false to clear the flag
+ *
+ * Set the flag on an rxrpc socket to say that the caller wants to manage the
+ * RESPONSE packet and the user-defined data it may contain. Setting this
+ * means that recvmsg() will return messages with RXRPC_CHALLENGED in the
+ * control message buffer containing information about the challenge.
+ *
+ * The user should respond to the challenge by passing RXRPC_RESPOND or
+ * RXRPC_RESPOND_ABORT control messages with sendmsg() to the same call.
+ * Supplementary control messages, such as RXRPC_RESP_RXGK_APPDATA, may be
+ * included to indicate the parts the user wants to supply.
+ *
+ * The server will be passed the response data with a RXRPC_RESPONDED control
+ * message when it gets the first data from each call.
+ *
+ * Note that this is only honoured by security classes that need auxiliary data
+ * (e.g. RxGK). Those that don't offer the facility (e.g. RxKAD) respond
+ * without consulting userspace.
+ *
+ * Return: The previous setting.
+ */
+int rxrpc_sock_set_manage_response(struct sock *sk, bool set)
+{
+ struct rxrpc_sock *rx = rxrpc_sk(sk);
+ int ret;
+
+ lock_sock(sk);
+ ret = !!test_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
+ if (set)
+ set_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
+ else
+ clear_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
+ release_sock(sk);
+ return ret;
+}
+EXPORT_SYMBOL(rxrpc_sock_set_manage_response);
diff --git a/net/rxrpc/server_key.c b/net/rxrpc/server_key.c
index 3efe104b1930..0be259dd0df0 100644
--- a/net/rxrpc/server_key.c
+++ b/net/rxrpc/server_key.c
@@ -9,6 +9,11 @@
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#define RXRPC_KEY_PROVIDER
+
+#include <crypto/des.h>
+#include <crypto/krb5.h>
+#include <linux/fips.h>
#include <linux/module.h>
#include <linux/net.h>
@@ -42,6 +47,7 @@ struct key_type key_type_rxrpc_s = {
.destroy = rxrpc_destroy_s,
.describe = rxrpc_describe_s,
};
+EXPORT_SYMBOL(key_type_rxrpc_s);
/*
* Vet the description for an RxRPC server key.
@@ -60,156 +66,198 @@ static int rxrpc_vet_description_s(const char *desc)
return 0;
}
+#ifdef CONFIG_RXKAD
/*
- * Preparse a server secret key.
+ * Parse the information from a server key
+ *
+ * The data should be the 8-byte secret key.
*/
-static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
+static int rxkad_preparse_server_key(struct key_preparsed_payload *prep)
{
- const struct rxrpc_security *sec;
- unsigned int service, sec_class;
- int n;
+ struct des_ctx *des_key;
+ int err;
+
+ if (fips_enabled)
+ return -ENOPKG;
+
+ if (prep->datalen != 8)
+ return -EINVAL;
+
+ memcpy(&prep->payload.data[2], prep->data, 8);
+
+ des_key = kmalloc_obj(*des_key);
+ if (!des_key) {
+ _leave(" = -ENOMEM");
+ return -ENOMEM;
+ }
+
+ err = des_expand_key(des_key, prep->data, 8);
+ if (err) {
+ kfree_sensitive(des_key);
+ _leave(" = %d", err);
+ return err;
+ }
+
+ prep->payload.data[0] = des_key;
+ _leave(" = 0");
+ return 0;
+}
+
+static void rxkad_free_preparse_server_key(struct key_preparsed_payload *prep)
+{
+ kfree_sensitive(prep->payload.data[0]);
+}
+
+static void rxkad_destroy_server_key(struct key *key)
+{
+ kfree_sensitive(key->payload.data[0]);
+ key->payload.data[0] = NULL;
+}
+#endif
+
+#ifdef CONFIG_RXGK
+/*
+ * Parse the information from a server key
+ */
+static int rxgk_preparse_server_key(struct key_preparsed_payload *prep)
+{
+ const struct krb5_enctype *krb5;
+ struct krb5_buffer *server_key = (void *)&prep->payload.data[2];
+ unsigned int service, sec_class, kvno, enctype;
+ int n = 0;
_enter("%zu", prep->datalen);
- if (!prep->orig_description)
+ if (sscanf(prep->orig_description, "%u:%u:%u:%u%n",
+ &service, &sec_class, &kvno, &enctype, &n) != 4)
return -EINVAL;
- if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
+ if (prep->orig_description[n])
return -EINVAL;
- sec = rxrpc_security_lookup(sec_class);
- if (!sec)
+ krb5 = crypto_krb5_find_enctype(enctype);
+ if (!krb5)
return -ENOPKG;
- prep->payload.data[1] = (struct rxrpc_security *)sec;
+ prep->payload.data[0] = (struct krb5_enctype *)krb5;
- if (!sec->preparse_server_key)
- return -EINVAL;
+ if (prep->datalen != krb5->key_len)
+ return -EKEYREJECTED;
+
+ server_key->len = prep->datalen;
+ server_key->data = kmemdup(prep->data, prep->datalen, GFP_KERNEL);
+ if (!server_key->data)
+ return -ENOMEM;
- return sec->preparse_server_key(prep);
+ _leave(" = 0");
+ return 0;
}
-static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
+static void rxgk_free_server_key(union key_payload *payload)
{
- const struct rxrpc_security *sec = prep->payload.data[1];
+ struct krb5_buffer *server_key = (void *)&payload->data[2];
- if (sec && sec->free_preparse_server_key)
- sec->free_preparse_server_key(prep);
+ kfree_sensitive(server_key->data);
}
-static void rxrpc_destroy_s(struct key *key)
+static void rxgk_free_preparse_server_key(struct key_preparsed_payload *prep)
{
- const struct rxrpc_security *sec = key->payload.data[1];
+ rxgk_free_server_key(&prep->payload);
+}
- if (sec && sec->destroy_server_key)
- sec->destroy_server_key(key);
+static void rxgk_destroy_server_key(struct key *key)
+{
+ rxgk_free_server_key(&key->payload);
}
-static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
+static void rxgk_describe_server_key(const struct key *key, struct seq_file *m)
{
- const struct rxrpc_security *sec = key->payload.data[1];
+ const struct krb5_enctype *krb5 = key->payload.data[0];
- seq_puts(m, key->description);
- if (sec && sec->describe_server_key)
- sec->describe_server_key(key, m);
+ if (krb5)
+ seq_printf(m, ": %s", krb5->name);
}
+#endif
/*
- * grab the security keyring for a server socket
+ * Preparse a server secret key.
*/
-int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
+static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
{
- struct key *key;
- char *description;
+ unsigned int service, sec_class;
+ int n;
- _enter("");
+ _enter("%zu", prep->datalen);
- if (rx->securities)
+ if (!prep->orig_description)
return -EINVAL;
- if (optlen <= 0 || optlen > PAGE_SIZE - 1)
+ if (sscanf(prep->orig_description, "%u:%u%n", &service, &sec_class, &n) != 2)
return -EINVAL;
- description = memdup_sockptr_nul(optval, optlen);
- if (IS_ERR(description))
- return PTR_ERR(description);
+ prep->payload.data[1] = (void *)(uintptr_t)sec_class;
- key = request_key(&key_type_keyring, description, NULL);
- if (IS_ERR(key)) {
- kfree(description);
- _leave(" = %ld", PTR_ERR(key));
- return PTR_ERR(key);
+ switch (sec_class) {
+#ifdef CONFIG_RXKAD
+ case RXRPC_SECURITY_RXKAD:
+ return rxkad_preparse_server_key(prep);
+#endif
+#ifdef CONFIG_RXGK
+ case RXRPC_SECURITY_YFS_RXGK:
+ return rxgk_preparse_server_key(prep);
+#endif
+ default:
+ return -ENOPKG;
}
+}
- rx->securities = key;
- kfree(description);
- _leave(" = 0 [key %x]", key->serial);
- return 0;
+static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
+{
+ switch ((uintptr_t)prep->payload.data[1]) {
+#ifdef CONFIG_RXKAD
+ case RXRPC_SECURITY_RXKAD:
+ rxkad_free_preparse_server_key(prep);
+ break;
+#endif
+#ifdef CONFIG_RXGK
+ case RXRPC_SECURITY_YFS_RXGK:
+ rxgk_free_preparse_server_key(prep);
+ break;
+#endif
+ default:
+ break;
+ }
}
-/**
- * rxrpc_sock_set_security_keyring - Set the security keyring for a kernel service
- * @sk: The socket to set the keyring on
- * @keyring: The keyring to set
- *
- * Set the server security keyring on an rxrpc socket. This is used to provide
- * the encryption keys for a kernel service.
- *
- * Return: %0 if successful and a negative error code otherwise.
- */
-int rxrpc_sock_set_security_keyring(struct sock *sk, struct key *keyring)
+static void rxrpc_destroy_s(struct key *key)
{
- struct rxrpc_sock *rx = rxrpc_sk(sk);
- int ret = 0;
-
- lock_sock(sk);
- if (rx->securities)
- ret = -EINVAL;
- else if (rx->sk.sk_state != RXRPC_UNBOUND)
- ret = -EISCONN;
- else
- rx->securities = key_get(keyring);
- release_sock(sk);
- return ret;
+ switch ((uintptr_t)key->payload.data[1]) {
+#ifdef CONFIG_RXKAD
+ case RXRPC_SECURITY_RXKAD:
+ rxkad_destroy_server_key(key);
+ break;
+#endif
+#ifdef CONFIG_RXGK
+ case RXRPC_SECURITY_YFS_RXGK:
+ rxgk_destroy_server_key(key);
+ break;
+#endif
+ default:
+ break;
+ }
}
-EXPORT_SYMBOL(rxrpc_sock_set_security_keyring);
-/**
- * rxrpc_sock_set_manage_response - Set the manage-response flag for a kernel service
- * @sk: The socket to set the keyring on
- * @set: True to set, false to clear the flag
- *
- * Set the flag on an rxrpc socket to say that the caller wants to manage the
- * RESPONSE packet and the user-defined data it may contain. Setting this
- * means that recvmsg() will return messages with RXRPC_CHALLENGED in the
- * control message buffer containing information about the challenge.
- *
- * The user should respond to the challenge by passing RXRPC_RESPOND or
- * RXRPC_RESPOND_ABORT control messages with sendmsg() to the same call.
- * Supplementary control messages, such as RXRPC_RESP_RXGK_APPDATA, may be
- * included to indicate the parts the user wants to supply.
- *
- * The server will be passed the response data with a RXRPC_RESPONDED control
- * message when it gets the first data from each call.
- *
- * Note that this is only honoured by security classes that need auxiliary data
- * (e.g. RxGK). Those that don't offer the facility (e.g. RxKAD) respond
- * without consulting userspace.
- *
- * Return: The previous setting.
- */
-int rxrpc_sock_set_manage_response(struct sock *sk, bool set)
+static void rxrpc_describe_s(const struct key *key, struct seq_file *m)
{
- struct rxrpc_sock *rx = rxrpc_sk(sk);
- int ret;
-
- lock_sock(sk);
- ret = !!test_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
- if (set)
- set_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
- else
- clear_bit(RXRPC_SOCK_MANAGE_RESPONSE, &rx->flags);
- release_sock(sk);
- return ret;
+ seq_puts(m, key->description);
+
+ switch ((uintptr_t)key->payload.data[1]) {
+#ifdef CONFIG_RXGK
+ case RXRPC_SECURITY_YFS_RXGK:
+ rxgk_describe_server_key(key, m);
+ break;
+#endif
+ default:
+ break;
+ }
}
-EXPORT_SYMBOL(rxrpc_sock_set_manage_response);
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 10/14] nvme-keyring: Move the PSK key type into a separate module
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (8 preceding siblings ...)
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
2026-11-29 17:05 ` [PATCH v7 11/14] platform/cznic: turris-signing-key: Flush key GC on key release Jarkko Sakkinen
` (4 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 11/14] platform/cznic: turris-signing-key: Flush key GC on key release
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (9 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 12/14] KEYS: trusted: Unregister key type on module exit in TEE backend Jarkko Sakkinen
` (3 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
turris_signing_key_release() unlinks and puts the device's signing key
when the parent device unbinds.
Once keys pin the kernel module of their key type, flush key garbage
collection in turris_signing_key_release() so the module reference is
released synchronously before module unload. Also allocate the keyring
before registering the key type during module initialization.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
drivers/platform/cznic/turris-signing-key.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/drivers/platform/cznic/turris-signing-key.c b/drivers/platform/cznic/turris-signing-key.c
index 3827178565e2..fc06a44f96f3 100644
--- a/drivers/platform/cznic/turris-signing-key.c
+++ b/drivers/platform/cznic/turris-signing-key.c
@@ -129,6 +129,7 @@ static void turris_signing_key_release(void *key)
{
key_unlink(turris_signing_keyring, key);
key_put(key);
+ key_gc_flush(&turris_signing_key_type);
}
int
@@ -158,10 +159,6 @@ static int turris_signing_key_init(void)
{
int err;
- err = register_key_type(&turris_signing_key_type);
- if (err)
- return err;
-
turris_signing_keyring = keyring_alloc(".turris-signing-keys",
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
(KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW |
@@ -171,18 +168,22 @@ static int turris_signing_key_init(void)
NULL, NULL);
if (IS_ERR(turris_signing_keyring)) {
pr_err("Cannot allocate Turris keyring\n");
-
- unregister_key_type(&turris_signing_key_type);
-
return PTR_ERR(turris_signing_keyring);
}
+ err = register_key_type(&turris_signing_key_type);
+ if (err) {
+ key_put(turris_signing_keyring);
+ return err;
+ }
+
return 0;
}
module_init(turris_signing_key_init);
static void turris_signing_key_exit(void)
{
+ key_revoke(turris_signing_keyring);
key_put(turris_signing_keyring);
unregister_key_type(&turris_signing_key_type);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 12/14] KEYS: trusted: Unregister key type on module exit in TEE backend
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (10 preceding siblings ...)
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 ` Jarkko Sakkinen
2026-11-29 17:05 ` [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
` (2 subsequent siblings)
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
trusted_key_remove() is a TEE client bus .remove() callback that runs
when the underlying TEE device unbinds, whereas key_type_trusted is
owned by trusted.ko.
Once keys pin trusted.ko and unregister_key_type() no longer reaps
extant keys via key_type_dead, unregistering key_type_trusted from
trusted_key_remove() can run while trusted keys still exist.
Keep key_type_trusted registered until trusted_tee_exit(), matching the
other trusted-key backends, while trusted_key_remove() tears down the
TEE session so subsequent TEE operations return -ENODEV.
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
security/keys/trusted-keys/trusted_tee.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c
index 87ea549f7f0b..4b67f504a54b 100644
--- a/security/keys/trusted-keys/trusted_tee.c
+++ b/security/keys/trusted-keys/trusted_tee.c
@@ -57,6 +57,7 @@ struct trusted_key_tee_private {
};
static struct trusted_key_tee_private pvt_data;
+static bool trusted_key_registered;
/* Do not hold this mutex across key type registration or unregistration. */
static DEFINE_MUTEX(trusted_key_tee_mutex);
@@ -266,10 +267,15 @@ static int trusted_key_probe(struct tee_client_device *rng_device)
pvt_data.dev = dev;
mutex_unlock(&trusted_key_tee_mutex);
- ret = register_key_type(&key_type_trusted);
- if (!ret)
- return 0;
+ if (!trusted_key_registered) {
+ ret = register_key_type(&key_type_trusted);
+ if (ret)
+ goto err_close_session;
+ trusted_key_registered = true;
+ }
+ return 0;
+err_close_session:
mutex_lock(&trusted_key_tee_mutex);
tee_client_close_session(ctx, pvt_data.session_id);
out_ctx:
@@ -284,8 +290,6 @@ static int trusted_key_probe(struct tee_client_device *rng_device)
static void trusted_key_remove(struct tee_client_device *dev)
{
- unregister_key_type(&key_type_trusted);
-
mutex_lock(&trusted_key_tee_mutex);
tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
tee_client_close_context(pvt_data.ctx);
@@ -320,6 +324,8 @@ static int trusted_tee_init(void)
static void trusted_tee_exit(void)
{
tee_client_driver_unregister(&trusted_key_driver);
+ if (trusted_key_registered)
+ unregister_key_type(&key_type_trusted);
}
struct trusted_key_ops trusted_key_tee_ops = {
--
2.47.3
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 13/14] KEYS: Pin the backing kernel module for a key type
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (11 preceding siblings ...)
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
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
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* [PATCH v7 14/14] KEYS: Remove key_type_dead
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (12 preceding siblings ...)
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
2026-11-29 17:11 ` [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:05 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, linux-afs, linux-cifs, linux-doc,
linux-integrity, linux-kernel, linux-nfs, linux-s390,
linux-security-module, netdev, samba-technical, Alexander Gordeev,
Anna Schumaker, Bharath SM, Christian Borntraeger, David Howells,
David S. Miller, Eric Dumazet, Heiko Carstens, Jakub Kicinski,
James Bottomley, James Morris, Jonathan Corbet, Marc Dionne,
Marek Behún, Mimi Zohar, Namjae Jeon, Paolo Abeni,
Paul Moore, Paulo Alcantara, Randy Dunlap, Ronnie Sahlberg,
Serge E. Hallyn, Shuah Khan, Shyam Prasad N, Simon Horman,
Sumit Garg, Sven Schnelle, Tom Talpey, Trond Myklebust,
Vasily Gorbik, Willem de Bruijn
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
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH v7 00/14] KEYS: Drop key_type_dead
2026-11-29 17:05 [PATCH v7 00/14] KEYS: Drop key_type_dead Jarkko Sakkinen
` (13 preceding siblings ...)
2026-11-29 17:05 ` [PATCH v7 14/14] KEYS: Remove key_type_dead Jarkko Sakkinen
@ 2026-11-29 17:11 ` Jarkko Sakkinen
14 siblings, 0 replies; 16+ messages in thread
From: Jarkko Sakkinen @ 2026-11-29 17:11 UTC (permalink / raw)
To: keyrings
Cc: linux-afs, linux-cifs, linux-doc, linux-integrity, linux-kernel,
linux-nfs, linux-s390, linux-security-module, netdev,
samba-technical, Alexander Gordeev, Anna Schumaker, Bharath SM,
Christian Borntraeger, David Howells, David S. Miller,
Eric Dumazet, Heiko Carstens, Jakub Kicinski, James Bottomley,
James Morris, Jonathan Corbet, Marc Dionne, Marek Behún,
Mimi Zohar, Namjae Jeon, Paolo Abeni, Paul Moore, Paulo Alcantara,
Randy Dunlap, Ronnie Sahlberg, Serge E. Hallyn, Shuah Khan,
Shyam Prasad N, Simon Horman, Sumit Garg, Sven Schnelle,
Tom Talpey, Trond Myklebust, Vasily Gorbik, Willem de Bruijn
On Sun, Nov 29, 2026 at 07:05:29PM +0200, Jarkko Sakkinen wrote:
> This installation of the series is mainly published due the improved
> draining code that filters according to the key type. Teardown is further
> softened by a dedicated fail-tolerant WQ and chunked reading. There is no
> rush to merge this but getting the influenced subsystems to review their
> part would be important and valuable so that we can start planning
> scheduling with likely earliest ETA of v7.6.
v7.5 lands rhashtable migration (from assoc_array). For neither there
is a perfect timing to land them as they do a "from old to new
world" change but by staging these "vector fixes" the whole process
is manageable. Nothing that unusual or unforeseen.
>
> Replace key_type_dead retyping in unregister_key_type() with module
> reference pinning on struct key_type so live keys and keyring restrictions
> keep their backing module loaded until key_gc_unused_keys() completes, and
> drop key_type_dead along with its multi-pass garbage collection machinery.
> This has a user-space visible effect: rmmod returns -EBUSY while keys or
> keyring restrictions still reference the module.
>
> CHANGES
> =======
>
> v7:
> - Renamed the patch set to "keys: Drop key_type_dead".
> - Dropped the three KEYS: trusted TEE fixes merged in v7.4-rc5.
> - Reordered the dedicated key_gc workqueue (now WQ_PERCPU without
> WQ_MEM_RECLAIM) and chunked key_garbage_collector() patches to the
> front of the series.
> - Filtered key_gc_flush() by struct key_type (including keyring
> restrictions) and serialized flushes in a single bounded pass.
> - Deferred NFSv4 and SMB client last-user cache clearing to work items so
> client and noreclaim demux paths do not block on synchronize_rcu().
> - Split nvme_tls_psk_key_type into nvme-psk-key.ko, flushed key GC on
> turris-signing-key release, and moved TEE key-type unregistration to
> trusted_tee_exit().
> - Hardened rxrpc, s390/cert_store, and KEYS core module reference release
> and dead-keytype cleanup based on pre-review findings.
>
> v6:
> - Split "KEYS: trusted: TEE: Synchronize session lifetime" into two
> patches:
> 1. "KEYS: trusted: Update static calls before init"
> 2. "KEYS: trusted: TEE: Synchronize session lifetime"
> No actual code changes.
>
> v5:
> - Another round of fixes resolving Sashiko remarks.
> - Reordered TEE session lifetime synchronization patch before suppressing
> sysfs bind attributes.
> - Release conn->key in rxrpc_process_event() on init_connection_security()
> failure
> - Update Documentation/networking/rxrpc.rst to reference
> net/rxrpc/security.c instead of net/rxrpc/server_key.c.
> - Store the delay-adjusted reap timestamp in gc->end in
> key_garbage_collector() instead of recording unadjusted key->expiry and
> adding key_gc_delay a second time.
>
> v4:
> - Replaced WARN_ON() around fallible keyring_clear() in CIFS idmap and
> SPNEGO cache-clearing helpers with pr_warn_ratelimited().
> - Moved key garbage collection from the shared events workqueue onto a
> dedicated WQ_MEM_RECLAIM | WQ_PERCPU workqueue (key_gc), allocated in
> key_init(), so WQ_MEM_RECLAIM workers (nfsiod/cifsiod) and reclaim paths
> can flush key GC safely.
>
> v3:
> - Split dns_resolver and rxrpc key types into dedicated provider modules
> (dns_resolver_key.ko and rxrpc_keys.ko) instead of client refcounting,
> so cached or retained keys do not pin dns_resolver.ko or rxrpc.ko.
> - Dropped the standalone nvme-keyring fix as it was picked up by the
> subsystem maintainers.
> - Hardened key_gc_flush() to serialize against chunked key_gc_work
> continuations, and fixed a restriction module reference leak on failed
> keyring instantiation.
>
> v2:
> - Used v1 as a test probe for Sashiko to detect the corner cases instead
> of trying to resolve even the known ones beforehand.
> - Converted the client subsystems to be compatible with the module
> pinning semantics.
>
> VERSIONS
> ========
>
> - v6:
> https://lore.kernel.org/keyrings/20261112040242.247168-1-jarkko@kernel.org/
> - v5:
> https://lore.kernel.org/keyrings/20261108222719.304712-1-jarkko@kernel.org/#t
> - v4:
> https://lore.kernel.org/keyrings/20261107131512.3603168-1-jarkko@kernel.org/
> - v3:
> https://lore.kernel.org/keyrings/20261105005311.804098-1-jarkko@kernel.org/
> - v2:
> https://lore.kernel.org/linux-integrity/20261103030226.2685251-1-jarkko@kernel.org/
>
> Cc: keyrings@vger.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: netdev@vger.kernel.org
> Cc: samba-technical@lists.samba.org
> Cc: Alexander Gordeev <agordeev@linux.ibm.com>
> Cc: Anna Schumaker <anna@kernel.org>
> Cc: Bharath SM <bharathsm@microsoft.com>
> Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
> Cc: David Howells <dhowells@redhat.com>
> Cc: "David S. Miller" <davem@davemloft.net>
> Cc: Eric Dumazet <edumazet@kernel.org>
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Jakub Kicinski <kuba@kernel.org>
> Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
> Cc: James Morris <jmorris@namei.org>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: Jonathan Corbet <corbet@lwn.net>
> Cc: Marc Dionne <marc.dionne@auristor.com>
> Cc: "Marek Behún" <kabel@kernel.org>
> Cc: Mimi Zohar <zohar@linux.ibm.com>
> Cc: Namjae Jeon <linkinjeon@kernel.org>
> Cc: Paolo Abeni <pabeni@redhat.com>
> Cc: Paul Moore <paul@paul-moore.com>
> Cc: Paulo Alcantara <pc@manguebit.org>
> Cc: Randy Dunlap <rdunlap@infradead.org>
> Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
> Cc: "Serge E. Hallyn" <serge@hallyn.com>
> Cc: Shuah Khan <skhan@linuxfoundation.org>
> Cc: Shyam Prasad N <sprasad@microsoft.com>
> Cc: Simon Horman <horms@kernel.org>
> Cc: Sumit Garg <sumit.garg@kernel.org>
> Cc: Sven Schnelle <svens@linux.ibm.com>
> Cc: Tom Talpey <tom@talpey.com>
> Cc: Trond Myklebust <trondmy@kernel.org>
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: Willem de Bruijn <willemb@google.com>
>
> Jarkko Sakkinen (14):
> KEYS: Move key garbage collection to a dedicated workqueue
> KEYS: key_garbage_collector(): process in chunks
> KEYS: Add key_gc_flush() for draining RCU callbacks
> dns_resolver: Move the key type into a separate module
> NFSv4: Clear idmap keyring cache when the last client unloads
> smb: client: Clear idmap/SPNEGO caches when the last session is
> unloaded
> s390/cert_store: Start tracking subsystem keys
> rxrpc: Release conn->key when init_connection_security() fails
> rxrpc: Move key types to their own module
> nvme-keyring: Move the PSK key type into a separate module
> platform/cznic: turris-signing-key: Flush key GC on key release
> KEYS: trusted: Unregister key type on module exit in TEE backend
> KEYS: Pin the backing kernel module for a key type
> KEYS: Remove key_type_dead
>
> Documentation/networking/dns_resolver.rst | 8 +-
> Documentation/networking/rxrpc.rst | 2 +-
> Documentation/security/keys/core.rst | 29 +-
> arch/s390/kernel/cert_store.c | 175 ++++----
> drivers/nvme/common/Kconfig | 9 +-
> drivers/nvme/common/Makefile | 2 +
> drivers/nvme/common/keyring.c | 68 +---
> drivers/nvme/common/psk_key.c | 74 ++++
> drivers/platform/cznic/turris-signing-key.c | 15 +-
> fs/nfs/nfs4idmap.c | 40 ++
> fs/smb/client/cifs_spnego.c | 14 +
> fs/smb/client/cifsacl.c | 14 +
> fs/smb/client/cifsfs.c | 4 +
> fs/smb/client/cifsproto.h | 9 +
> fs/smb/client/misc.c | 39 +-
> include/linux/key-type.h | 8 +-
> include/linux/key.h | 7 +-
> include/linux/nvme-keyring.h | 4 +
> net/Makefile | 4 +-
> net/dns_resolver/Kconfig | 15 +
> net/dns_resolver/Makefile | 4 +-
> net/dns_resolver/dns_key.c | 65 +--
> net/dns_resolver/dns_query.c | 70 ++++
> net/dns_resolver/internal.h | 7 -
> net/rxrpc/Kconfig | 20 +
> net/rxrpc/Makefile | 7 +-
> net/rxrpc/af_rxrpc.c | 21 +-
> net/rxrpc/ar-internal.h | 53 ++-
> net/rxrpc/conn_event.c | 7 +-
> net/rxrpc/key.c | 128 +++---
> net/rxrpc/proc.c | 6 +-
> net/rxrpc/rxgk.c | 69 +---
> net/rxrpc/rxkad.c | 47 ---
> net/rxrpc/security.c | 185 +++++++++
> net/rxrpc/server_key.c | 260 +++++++-----
> security/keys/gc.c | 418 +++++++++++---------
> security/keys/internal.h | 14 +-
> security/keys/key.c | 233 +++++------
> security/keys/keyring.c | 87 ++--
> security/keys/permission.c | 7 +-
> security/keys/proc.c | 2 +-
> security/keys/trusted-keys/trusted_tee.c | 16 +-
> 42 files changed, 1245 insertions(+), 1021 deletions(-)
> create mode 100644 drivers/nvme/common/psk_key.c
>
>
> base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
> prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
> prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
> prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e
> prerequisite-patch-id: 4517a290b914c1a79717aece49041479eca4b399
> prerequisite-patch-id: 0b49963321abe30437c8e82ffd64fa235c9746bd
> --
> 2.47.3
>
Br, Jarkko
^ permalink raw reply [flat|nested] 16+ messages in thread