KEYS: Return user session keyring on lookup
Subsystems (4)
Jarkko Sakkinen
maintainerPATCH v4 1/2 KEYS: Return user session keyring on lookup
A process without a session keyring looking up KEY_SPEC_SESSION_KEYRING without KEY_LOOKUP_CREATE mutates the credentials. This causes struct creds instances shared with other subsystems to become stale.
Address this by returning the resolved user session keyring directly without installing it into credentials.
---
v4:
- No changes, resend.
v3:
- In keyctl_session_to_parent(), check the resolved cred->session_keyring instead of mycred->session_keyring.
v2:
- Added mandatory tags.
- No code changes.
- Link: https://lore.kernel.org/keyrings/[email protected]/
Documentation/security/keys/core.rst | 6 ++-- security/keys/keyctl.c | 5 ++-- security/keys/process_keys.c | 44 +++++++--------------------- 3 files changed, 16 insertions(+), 39 deletions(-) --- Documentation/security/keys/core.rst | 6 ++-- security/keys/keyctl.c | 5 ++-- security/keys/process_keys.c | 44 +++++++--------------------- 3 files changed, 16 insertions(+), 39 deletions(-)
Documentation/security/keys/core.rst +4 -2
@@ -165,8 +165,10 @@ The key service provides a number of features besides keys: When a process changes its real UID, if it used to have no session key, it will be subscribed to the default session key for the new UID. - If a process attempts to access its session key when it doesn't have one, - it will be subscribed to the default for its current UID. + If a process attempts to access its session keyring when it doesn't have + one, the default user session keyring for its current UID is returned + without being installed into its credentials. If creation is requested, + an anonymous session keyring is installed. * Each user has two quotas against which the keys they own are tracked. One limits the total number of keys and keyrings, the other limits the total
security/keys/keyctl.c +2 -3
@@ -1682,8 +1682,7 @@ long keyctl_session_to_parent(void)* there's no point */ mycred =current_cred (); pcred =__task_cred (parent); -if (mycred == pcred || - mycred->session_keyring == pcred->session_keyring) { +if (cred->session_keyring == pcred->session_keyring) { ret =0 ; goto unlock; }
@@ -1701,7 +1700,7 @@ long keyctl_session_to_parent(void)/* the keyrings must have the same UID */ if ((pcred->session_keyring && !uid_eq (pcred->session_keyring->uid, mycred->euid)) || - !uid_eq (mycred->session_keyring->uid, mycred->euid)) + !uid_eq (cred->session_keyring->uid, mycred->euid)) goto unlock;/* cancel an already pending keyring replacement */
security/keys/process_keys.c +10 -34
61 lines not on this page · raw message
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v4 2/2 KEYS: Reject keyring creation with overridden credentials
If the current task is running with overridden credentials calling commit_creds() is forbidden and triggers a BUG_ON().
Reject keyring creation with -EPERM when credentials are overridden.
---
v4:
- No changes, resend.
v3:
- Collapse and open-code cred_overridden() to the entry point of lookup_user_key() (David).
v2:
- Added mandatory tags.
- No code changes.
- Link: https://lore.kernel.org/keyrings/[email protected]/
Documentation/security/keys/core.rst | 3 ++- security/keys/process_keys.c | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) --- Documentation/security/keys/core.rst | 3 ++- security/keys/process_keys.c | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-)
Documentation/security/keys/core.rst +2 -1
@@ -168,7 +168,8 @@ The key service provides a number of features besides keys: If a process attempts to access its session keyring when it doesn't have one, the default user session keyring for its current UID is returned without being installed into its credentials. If creation is requested, - an anonymous session keyring is installed. + an anonymous session keyring is installed, failing with -EPERM if + credentials are overridden. * Each user has two quotas against which the keys they own are tracked. One limits the total number of keys and keyrings, the other limits the total
security/keys/process_keys.c +5 -1
@@ -633,7 +633,8 @@ void key_register_fs_verity_keyring(struct key *keyring)* to a key or the best found key was a negative key; -EKEYREVOKED or * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the * found key doesn't grant the requested permit or the LSM denied access to it; -* or -ENOMEM if a special keyring couldn't be created. +* -ENOMEM if a special keyring couldn't be created; or -EPERM if KEY_LOOKUP_CREATE +* is requested while credentials are overridden. * * In the case of a successful return, the possession attribute is set on the * returned key reference.
@@ -652,6 +653,9 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,key_ref_t key_ref, skey_ref;int ret; +if ((lflags &KEY_LOOKUP_CREATE ) &¤t_cred () !=current_real_cred ()) +return ERR_PTR (-EPERM ); + try_again: ctx.cred =get_current_cred (); key_ref =ERR_PTR (-ENOKEY );
Signature · 1 line
2.47.3
Re: [PATCH v4 2/2] KEYS: Reject keyring creation with overridden credentials
Quoted 4 lines · Jarkko Sakkinen
If the current task is running with overridden credentials calling
commit_creds()is forbidden and triggers aBUG_ON().Reject keyring creation with
-EPERMwhen credentials are overridden.
Tested on the same arm64/KVM KASAN/lockdep setup on top of patch 1/2. Under override_creds(), lookup_user_key() with KEY_LOOKUP_CREATE now returns -EPERM for KEY_SPEC_{SESSION,PROCESS,THREAD,USER}_KEYRING without hitting commit_creds().
Jarkko Sakkinen
maintainerRe: [PATCH v4 2/2] KEYS: Reject keyring creation with overridden credentials
Quoted 6 lines · Guicai Ma
Quoted 4 lines · Jarkko Sakkinen
If the current task is running with overridden credentials calling
commit_creds()is forbidden and triggers aBUG_ON().Reject keyring creation with
-EPERMwhen credentials are overridden.
Quoted 7 lines
Tested on the same arm64/KVM KASAN/lockdep setup on top of patch 1/2. Under
override_creds(),lookup_user_key()withKEY_LOOKUP_CREATEnow returns-EPERMfor KEY_SPEC_{SESSION,PROCESS,THREAD,USER}_KEYRING without hittingcommit_creds().Reviewed-by Guicai Ma <[email protected]>Tested-by Guicai Ma <[email protected]>
Thanks a bunch!
Br, Jarkko
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Quoted 6 lines · Jarkko Sakkinen
A process without a session keyring looking up
KEY_SPEC_SESSION_KEYRINGwithoutKEY_LOOKUP_CREATEmutates the credentials. This causes struct creds instances shared with other subsystems to become stale.Address this by returning the resolved user session keyring directly without installing it into credentials.
Tested on jarkko/for-next-keys (aa9e16503df9) in an arm64/KVM guest with KASAN and lockdep. With a small module calling lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_SEARCH) under override_creds(), the unpatched kernel hits the BUG at kernel/cred.c:376 and the patched one returns the user session keyring without setting cred->session_keyring.
Userspace checks for KEYCTL_GET_KEYRING_ID (create=0 and 1) and KEYCTL_SESSION_TO_PARENT (shared NULL and non-NULL session keyrings) also pass cleanly.