* [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
@ 2026-11-24 17:40 Jarkko Sakkinen
2026-11-24 17:44 ` Jarkko Sakkinen
2026-11-24 23:00 ` Guicai Ma
0 siblings, 2 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24 17:40 UTC (permalink / raw)
To: keyrings
Cc: Jarkko Sakkinen, stable, linux-kernel, linux-security-module,
David Howells, James Morris, Paul Moore, Serge E. Hallyn,
Guicai Ma
key_instantiate_and_link() accesses key->type outside key->sem, which can
lead into null deref due that key types can be dropped at any possible
time.
Address this by moving key_construction_mutex inside key->sem, and match
that order in key_reject_and_link())
Cc: stable@vger.kernel.org
Cc: keyrings@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-security-module@vger.kernel.org
Cc: David Howells <dhowells@redhat.com>
Cc: James Morris <jmorris@namei.org>
Cc: Jarkko Sakkinen <jarkko@kernel.org>
Cc: Paul Moore <paul@paul-moore.com>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Reported-by: Guicai Ma <maguicai@kylinos.cn>
Closes: https://lore.kernel.org/r/20261124091243.1019217-1-maguicai@kylinos.cn
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
security/keys/key.c | 139 ++++++++++++++++++++++++++------------------
1 file changed, 81 insertions(+), 58 deletions(-)
diff --git a/security/keys/key.c b/security/keys/key.c
index 5854c2e2c4e6..ea1046bbb4db 100644
--- a/security/keys/key.c
+++ b/security/keys/key.c
@@ -422,66 +422,56 @@ 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 target key's
- * semaphore need not be locked as instantiation is serialised by
- * key_construction_mutex.
+ * called with the target keyring's semaphore writelocked (if @keyring is
+ * given) and the target key's semaphore writelocked.
*/
static int __key_instantiate_and_link(struct key *key,
struct key_preparsed_payload *prep,
struct key *keyring,
struct key *authkey,
- struct keyring_edit *edit)
+ struct keyring_edit *edit,
+ bool *awaken)
{
- int ret, awaken;
-
- key_check(key);
- key_check(keyring);
-
- awaken = 0;
- ret = -EBUSY;
+ int ret;
- mutex_lock(&key_construction_mutex);
- down_write_nested(&key->sem, 2);
+ lockdep_assert_held_write(&key->sem);
/* can't instantiate twice */
- if (key->state == KEY_IS_UNINSTANTIATED) {
- /* instantiate the key */
- ret = key->type->instantiate(key, prep);
+ if (key->state != KEY_IS_UNINSTANTIATED)
+ return -EBUSY;
- if (ret == 0) {
- /* mark the key as being instantiated */
- atomic_inc(&key->user->nikeys);
- mark_key_instantiated(key, 0);
- notify_key(key, NOTIFY_KEY_INSTANTIATED, 0);
+ /* instantiate the key */
+ ret = key->type->instantiate(key, prep);
+ if (ret)
+ return ret;
- if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
- awaken = 1;
+ mutex_lock(&key_construction_mutex);
- /* and link it into the destination keyring */
- if (keyring) {
- if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
- set_bit(KEY_FLAG_KEEP, &key->flags);
+ /* mark the key as being instantiated */
+ atomic_inc(&key->user->nikeys);
+ mark_key_instantiated(key, 0);
+ notify_key(key, NOTIFY_KEY_INSTANTIATED, 0);
- __key_link(keyring, key, edit);
- }
+ if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) && awaken)
+ *awaken = true;
- /* disable the authorisation key */
- if (authkey)
- key_invalidate(authkey);
+ /* and link it into the destination keyring */
+ if (keyring) {
+ if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
+ set_bit(KEY_FLAG_KEEP, &key->flags);
- if (prep->expiry != TIME64_MAX)
- key_set_expiry(key, prep->expiry);
- }
+ __key_link(keyring, key, edit);
}
- up_write(&key->sem);
- mutex_unlock(&key_construction_mutex);
+ /* disable the authorisation key */
+ if (authkey)
+ key_invalidate(authkey);
- /* wake up anyone waiting for a key to be constructed */
- if (awaken)
- wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
+ if (prep->expiry != TIME64_MAX)
+ key_set_expiry(key, prep->expiry);
- return ret;
+ mutex_unlock(&key_construction_mutex);
+ return 0;
}
/**
@@ -498,7 +488,9 @@ 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.
+ * -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.
*/
int key_instantiate_and_link(struct key *key,
const void *data,
@@ -508,8 +500,35 @@ int key_instantiate_and_link(struct key *key,
{
struct key_preparsed_payload prep;
struct keyring_edit edit = {};
+ bool awaken = false;
int ret;
+ key_check(key);
+ key_check(keyring);
+
+ if (keyring) {
+ ret = __key_link_lock(keyring, &key->index_key);
+ if (ret < 0)
+ return ret;
+ }
+
+ down_write_nested(&key->sem, 2);
+
+ if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
+ ret = -EKEYREVOKED;
+ goto out_unlock;
+ }
+
+ if (!key->type->instantiate) {
+ ret = -EOPNOTSUPP;
+ goto out_unlock;
+ }
+
+ if (key->state != KEY_IS_UNINSTANTIATED) {
+ ret = -EBUSY;
+ goto out_unlock;
+ }
+
memset(&prep, 0, sizeof(prep));
prep.orig_description = key->description;
prep.data = data;
@@ -519,17 +538,13 @@ int key_instantiate_and_link(struct key *key,
if (key->type->preparse) {
ret = key->type->preparse(&prep);
if (ret < 0)
- goto error;
+ goto out_free_preparse;
}
if (keyring) {
- ret = __key_link_lock(keyring, &key->index_key);
- if (ret < 0)
- goto error;
-
ret = __key_link_begin(keyring, &key->index_key, &edit);
if (ret < 0)
- goto error_link_end;
+ goto out_free_preparse;
if (keyring->restrict_link && keyring->restrict_link->check) {
struct key_restriction *keyres = keyring->restrict_link;
@@ -537,19 +552,22 @@ int key_instantiate_and_link(struct key *key,
ret = keyres->check(keyring, key->type, &prep.payload,
keyres->key);
if (ret < 0)
- goto error_link_end;
+ goto out_free_preparse;
}
}
- ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit);
-
-error_link_end:
- if (keyring)
- __key_link_end(keyring, &key->index_key, &edit);
+ ret = __key_instantiate_and_link(key, &prep, keyring, authkey, &edit,
+ &awaken);
-error:
+out_free_preparse:
if (key->type->preparse)
key->type->free_preparse(&prep);
+out_unlock:
+ up_write(&key->sem);
+ if (keyring)
+ __key_link_end(keyring, &key->index_key, &edit);
+ if (awaken)
+ wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
return ret;
}
@@ -610,11 +628,12 @@ int key_reject_and_link(struct key *key,
}
}
- mutex_lock(&key_construction_mutex);
down_write_nested(&key->sem, 2);
/* can't instantiate twice */
if (key->state == KEY_IS_UNINSTANTIATED) {
+ mutex_lock(&key_construction_mutex);
+
/* mark the key as being negatively instantiated */
atomic_inc(&key->user->nikeys);
mark_key_instantiated(key, -error);
@@ -633,10 +652,11 @@ int key_reject_and_link(struct key *key,
/* disable the authorisation key */
if (authkey)
key_invalidate(authkey);
+
+ mutex_unlock(&key_construction_mutex);
}
up_write(&key->sem);
- mutex_unlock(&key_construction_mutex);
if (keyring && link_ret == 0)
__key_link_end(keyring, &key->index_key, &edit);
@@ -957,7 +977,10 @@ static key_ref_t __key_create_or_update(key_ref_t keyring_ref,
}
/* instantiate it and link it into the target keyring */
- ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit);
+ down_write_nested(&key->sem, 2);
+ ret = __key_instantiate_and_link(key, &prep, keyring, NULL, &edit,
+ NULL);
+ up_write(&key->sem);
if (ret < 0) {
key_put(key);
key_ref = ERR_PTR(ret);
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
2026-11-24 17:40 [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link() Jarkko Sakkinen
@ 2026-11-24 17:44 ` Jarkko Sakkinen
2026-11-24 23:00 ` Guicai Ma
1 sibling, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24 17:44 UTC (permalink / raw)
To: keyrings
Cc: stable, linux-kernel, linux-security-module, David Howells,
James Morris, Paul Moore, Serge E. Hallyn, Guicai Ma
On Tue, Nov 24, 2026 at 07:40:35PM +0200, Jarkko Sakkinen wrote:
> key_instantiate_and_link() accesses key->type outside key->sem, which can
> lead into null deref due that key types can be dropped at any possible
> time.
>
> Address this by moving key_construction_mutex inside key->sem, and match
> that order in key_reject_and_link())
>
> Cc: stable@vger.kernel.org
> Cc: keyrings@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Cc: linux-security-module@vger.kernel.org
> Cc: David Howells <dhowells@redhat.com>
> Cc: James Morris <jmorris@namei.org>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: Paul Moore <paul@paul-moore.com>
> Cc: "Serge E. Hallyn" <serge@hallyn.com>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Reported-by: Guicai Ma <maguicai@kylinos.cn>
> Closes: https://lore.kernel.org/r/20261124091243.1019217-1-maguicai@kylinos.cn
> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Can be better addressed later with this:
https://lore.kernel.org/keyrings/20261112040242.247168-1-jarkko@kernel.org/
In erases all key type drop type of issue. Thus, totally acceptable
bug fix.
Br, Jarkko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
2026-11-24 17:40 [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link() Jarkko Sakkinen
2026-11-24 17:44 ` Jarkko Sakkinen
@ 2026-11-24 23:00 ` Guicai Ma
2026-11-24 23:24 ` Jarkko Sakkinen
1 sibling, 1 reply; 5+ messages in thread
From: Guicai Ma @ 2026-11-24 23:00 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: keyrings, linux-kernel, linux-security-module, David Howells,
James Morris, Paul Moore, Serge E. Hallyn
On Tue, 24 Nov 2026 19:40:35 +0200, Jarkko Sakkinen wrote:
> @@ -610,11 +628,12 @@ int key_reject_and_link(struct key *key,
> }
> }
>
> - mutex_lock(&key_construction_mutex);
> down_write_nested(&key->sem, 2);
>
> /* can't instantiate twice */
> if (key->state == KEY_IS_UNINSTANTIATED) {
> + mutex_lock(&key_construction_mutex);
Here __key_link_begin() still runs before key->sem is taken and there is
no KEY_FLAG_DEAD check, so the link path is still taken: on 7.4-rc3 with
KASAN and PROVE_LOCKING, KEYCTL_REJECT with a target keyring on a dead
key returns 0 instead of -EKEYREVOKED. If pass 3 retypes the key before
key->sem is taken, __key_link() inserts it with an edit built from the
old type.
Taking key->sem before __key_link_begin() and skipping the link with
link_ret = -EKEYREVOKED when KEY_FLAG_DEAD is set avoids that, and still
lets the negative instantiation finish so request_key() waiters wake up.
In key_instantiate_and_link():
> + down_write_nested(&key->sem, 2);
> +
> + if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
> + ret = -EKEYREVOKED;
> + goto out_unlock;
> + }
After a modular type is unloaded, key->sem still carries the module's
lock_class_key, which lockdep_free_key_range() dropped. In the post-rmmod
instantiate test up_write() here warns "trying to release lock
(&type->lock_class) ... no more locks to release". Module pinning would
remove this; it still matters for stable backports.
The 200-round instantiate/reject race and encrypted-key instantiation
pass.
Thanks,
Guicai
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
2026-11-24 23:00 ` Guicai Ma
@ 2026-11-24 23:24 ` Jarkko Sakkinen
2026-11-24 23:38 ` Jarkko Sakkinen
0 siblings, 1 reply; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24 23:24 UTC (permalink / raw)
To: Guicai Ma
Cc: keyrings, linux-kernel, linux-security-module, David Howells,
James Morris, Paul Moore, Serge E. Hallyn
On Tue, Nov 24, 2026 at 06:00:16PM -0500, Guicai Ma wrote:
> On Tue, 24 Nov 2026 19:40:35 +0200, Jarkko Sakkinen wrote:
> > @@ -610,11 +628,12 @@ int key_reject_and_link(struct key *key,
> > }
> > }
> >
> > - mutex_lock(&key_construction_mutex);
> > down_write_nested(&key->sem, 2);
> >
> > /* can't instantiate twice */
> > if (key->state == KEY_IS_UNINSTANTIATED) {
> > + mutex_lock(&key_construction_mutex);
>
> Here __key_link_begin() still runs before key->sem is taken and there is
> no KEY_FLAG_DEAD check, so the link path is still taken: on 7.4-rc3 with
> KASAN and PROVE_LOCKING, KEYCTL_REJECT with a target keyring on a dead
> key returns 0 instead of -EKEYREVOKED. If pass 3 retypes the key before
> key->sem is taken, __key_link() inserts it with an edit built from the
> old type.
>
> Taking key->sem before __key_link_begin() and skipping the link with
> link_ret = -EKEYREVOKED when KEY_FLAG_DEAD is set avoids that, and still
> lets the negative instantiation finish so request_key() waiters wake up.
>
> In key_instantiate_and_link():
> > + down_write_nested(&key->sem, 2);
> > +
> > + if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
> > + ret = -EKEYREVOKED;
> > + goto out_unlock;
> > + }
>
> After a modular type is unloaded, key->sem still carries the module's
> lock_class_key, which lockdep_free_key_range() dropped. In the post-rmmod
> instantiate test up_write() here warns "trying to release lock
> (&type->lock_class) ... no more locks to release". Module pinning would
> remove this; it still matters for stable backports.
>
> The 200-round instantiate/reject race and encrypted-key instantiation
> pass.
I'm with you on backports part of course, and apparently you are right
what you're saying. Thanks for pointing this out.
Addin SCRU just feels a bit heavy measure but the concurrency case is
also somewhat nasty. But in the end of the day it's only +10 LOC and
I understand why it works, and I also under why this does not :-)
It was not that much more complicated. Can you put the commit message
more brief and exactly put emphasis on SCRU part?
>
> Thanks,
> Guicai
Br, Jarkko
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link()
2026-11-24 23:24 ` Jarkko Sakkinen
@ 2026-11-24 23:38 ` Jarkko Sakkinen
0 siblings, 0 replies; 5+ messages in thread
From: Jarkko Sakkinen @ 2026-11-24 23:38 UTC (permalink / raw)
To: Guicai Ma
Cc: keyrings, linux-kernel, linux-security-module, David Howells,
James Morris, Paul Moore, Serge E. Hallyn
On Wed, Nov 25, 2026 at 01:24:17AM +0200, Jarkko Sakkinen wrote:
> On Tue, Nov 24, 2026 at 06:00:16PM -0500, Guicai Ma wrote:
> > On Tue, 24 Nov 2026 19:40:35 +0200, Jarkko Sakkinen wrote:
> > > @@ -610,11 +628,12 @@ int key_reject_and_link(struct key *key,
> > > }
> > > }
> > >
> > > - mutex_lock(&key_construction_mutex);
> > > down_write_nested(&key->sem, 2);
> > >
> > > /* can't instantiate twice */
> > > if (key->state == KEY_IS_UNINSTANTIATED) {
> > > + mutex_lock(&key_construction_mutex);
> >
> > Here __key_link_begin() still runs before key->sem is taken and there is
> > no KEY_FLAG_DEAD check, so the link path is still taken: on 7.4-rc3 with
> > KASAN and PROVE_LOCKING, KEYCTL_REJECT with a target keyring on a dead
> > key returns 0 instead of -EKEYREVOKED. If pass 3 retypes the key before
> > key->sem is taken, __key_link() inserts it with an edit built from the
> > old type.
> >
> > Taking key->sem before __key_link_begin() and skipping the link with
> > link_ret = -EKEYREVOKED when KEY_FLAG_DEAD is set avoids that, and still
> > lets the negative instantiation finish so request_key() waiters wake up.
> >
> > In key_instantiate_and_link():
> > > + down_write_nested(&key->sem, 2);
> > > +
> > > + if (test_bit(KEY_FLAG_DEAD, &key->flags)) {
> > > + ret = -EKEYREVOKED;
> > > + goto out_unlock;
> > > + }
> >
> > After a modular type is unloaded, key->sem still carries the module's
> > lock_class_key, which lockdep_free_key_range() dropped. In the post-rmmod
> > instantiate test up_write() here warns "trying to release lock
> > (&type->lock_class) ... no more locks to release". Module pinning would
> > remove this; it still matters for stable backports.
> >
> > The 200-round instantiate/reject race and encrypted-key instantiation
> > pass.
>
> I'm with you on backports part of course, and apparently you are right
> what you're saying. Thanks for pointing this out.
>
> Addin SCRU just feels a bit heavy measure but the concurrency case is
> also somewhat nasty. But in the end of the day it's only +10 LOC and
> I understand why it works, and I also under why this does not :-)
>
> It was not that much more complicated. Can you put the commit message
> more brief and exactly put emphasis on SCRU part?
Take your time, as I won't be sending PRs before next week.
Br, Jarkko
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 23:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 17:40 [PATCH] KEYS: Prevent key type removal during key_instantiate_and_link() Jarkko Sakkinen
2026-11-24 17:44 ` Jarkko Sakkinen
2026-11-24 23:00 ` Guicai Ma
2026-11-24 23:24 ` Jarkko Sakkinen
2026-11-24 23:38 ` Jarkko Sakkinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox