* [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
0 siblings, 1 reply; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-24 17:44 UTC | newest]
Thread overview: 2+ 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox