KEYS: Prevent key type removal during key_instantiate_and_link()
Subsystems (3)
Jarkko Sakkinen
maintainerPATCH 1/1 KEYS: Prevent key type removal during key_instantiate_and_link()
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())
--- security/keys/key.c | 139 ++++++++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 58 deletions(-)
security/keys/key.c +81 -58
@@ -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
Jarkko Sakkinen
maintainerQuoted 7 lines · Jarkko Sakkinen
key_instantiate_and_link()accesseskey->typeoutsidekey->sem, which can lead into null deref due that key types can be dropped at any possible time.Address this by moving
key_construction_mutexinsidekey->sem, and match that order inkey_reject_and_link())
Quoted 13 lines
Cc David Howells <[email protected]>Cc James Morris <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Paul Moore <[email protected]>Cc "Serge E. Hallyn" <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Reported-by Guicai Ma <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Can be better addressed later with this:
https://lore.kernel.org/keyrings/[email protected]/
In erases all key type drop type of issue. Thus, totally acceptable bug fix.
Br, Jarkko
security/keys/key.c int key_reject_and_link(struct key *key, · line 635
Quoted 10 lines · Jarkko Sakkinen
security/keys/key.c +1
-1@@ -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():
security/keys/key.c int key_instantiate_and_link(struct key *key, · line 520
Quoted 6 lines
security/keys/key.c +6
-0+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
Jarkko Sakkinen
maintainerQuoted 37 lines · Guicai Ma
security/keys/key.c int key_reject_and_link(struct key *key, · line 635
Quoted 10 lines · Jarkko Sakkinen
security/keys/key.c +1
-1@@ -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 beforekey->semis taken and there is noKEY_FLAG_DEADcheck, so the link path is still taken: on 7.4-rc3 with KASAN andPROVE_LOCKING,KEYCTL_REJECTwith a target keyring on a dead key returns 0 instead of-EKEYREVOKED. If pass 3 retypes the key beforekey->semis taken,__key_link()inserts it with an edit built from the old type.Taking
key->sembefore__key_link_begin()and skipping the link withlink_ret=-EKEYREVOKEDwhenKEY_FLAG_DEADis set avoids that, and still lets the negative instantiation finish sorequest_key()waiters wake up.In
key_instantiate_and_link():security/keys/key.c int key_instantiate_and_link(struct key *key, · line 520
Quoted 6 lines
security/keys/key.c +6
-0+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->semstill carries the module'slock_class_key, whichlockdep_free_key_range()dropped. In the post-rmmod instantiate testup_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.
Quoted 2 lines
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?
Quoted 3 lines
Thanks,
Guicai
Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 44 lines · Jarkko Sakkinen
Quoted 37 lines · Guicai Ma
security/keys/key.c int key_reject_and_link(struct key *key, · line 635
Quoted 10 lines · Jarkko Sakkinen
security/keys/key.c +1
-1@@ -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 beforekey->semis taken and there is noKEY_FLAG_DEADcheck, so the link path is still taken: on 7.4-rc3 with KASAN andPROVE_LOCKING,KEYCTL_REJECTwith a target keyring on a dead key returns 0 instead of-EKEYREVOKED. If pass 3 retypes the key beforekey->semis taken,__key_link()inserts it with an edit built from the old type.Taking
key->sembefore__key_link_begin()and skipping the link withlink_ret=-EKEYREVOKEDwhenKEY_FLAG_DEADis set avoids that, and still lets the negative instantiation finish sorequest_key()waiters wake up.In
key_instantiate_and_link():security/keys/key.c int key_instantiate_and_link(struct key *key, · line 520
Quoted 6 lines
security/keys/key.c +6
-0+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->semstill carries the module'slock_class_key, whichlockdep_free_key_range()dropped. In the post-rmmod instantiate testup_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.Quoted 2 lines
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.
Quoted 6 lines
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