KEYS: Return user session keyring on lookup
Subsystems (4)
Jarkko Sakkinen
maintainerPATCH v2 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.
---
v2:
- Added mandatory tags.
- No code changes.
--- Documentation/security/keys/core.rst | 6 ++-- security/keys/process_keys.c | 44 +++++++--------------------- 2 files changed, 14 insertions(+), 36 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/process_keys.c +10 -34
61 lines not on this page · raw message
Signature · 1 line
2.47.3
Jarkko Sakkinen
maintainerPATCH v2 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.
---
v2:
- Added mandatory tags.
- No code changes.
--- Documentation/security/keys/core.rst | 3 ++- security/keys/process_keys.c | 31 +++++++++++++++++++++++++++- 2 files changed, 32 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 +30 -1
@@ -346,6 +346,14 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)return 0 ; } +/* +* Determine whether the current task is running with overridden credentials. +*/ +static bool cred_overridden (void ) +{ +return current_cred () !=current_real_cred (); +} +/* * Handle the fsuid changing. */
@@ -577,7 +585,8 @@ bool lookup_user_key_possessed(const struct key *key,* 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 creating one +* while credentials are overridden. * * In the case of a successful return, the possession attribute is set on the * returned key reference.
@@ -606,6 +615,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,if (!(lflags &KEY_LOOKUP_CREATE )) goto error; +if (cred_overridden ()) { + key_ref =ERR_PTR (-EPERM ); + goto error; + } + ret =install_thread_keyring ();if (ret <0 ) { key_ref =ERR_PTR (ret);
@@ -624,6 +638,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,if (!(lflags &KEY_LOOKUP_CREATE )) goto error; +if (cred_overridden ()) { + key_ref =ERR_PTR (-EPERM ); + goto error; + } + ret =install_process_keyring ();if (ret <0 ) { key_ref =ERR_PTR (ret);
@@ -645,6 +664,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,if (lflags &KEY_LOOKUP_CREATE ) {key_put (user_session); +if (cred_overridden ()) { + key_ref =ERR_PTR (-EPERM ); + goto error; + } + ret =join_session_keyring (NULL );if (ret <0 ) goto error;
@@ -657,6 +681,11 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, }else if (test_bit (KEY_FLAG_UID_KEYRING , &ctx.cred->session_keyring->flags) && lflags &KEY_LOOKUP_CREATE ) { +if (cred_overridden ()) { + key_ref =ERR_PTR (-EPERM ); + goto error; + } + ret =join_session_keyring (NULL );if (ret <0 ) goto error;
Signature · 1 line
2.47.3
David Howells
maintainerRe: [PATCH v2 2/2] KEYS: Reject keyring creation with overridden credentials
Would it make sense to just reject KEY_LOOKUP_CREATE at the top of the function if the creds are overridden?
David
Jarkko Sakkinen
maintainerRe: [PATCH v2 2/2] KEYS: Reject keyring creation with overridden credentials
Quoted 2 lines · David Howells
Would it make sense to just reject
KEY_LOOKUP_CREATEat the top of the function if the creds are overridden?
Let me check..
Yep, I suppose you're right. I'll update the patch.
Quoted 3 lines
David
Br, Jarkko
[email protected]
AI reviewRe: [PATCH v2 2/2] KEYS: Reject keyring creation with overridden credentials
In reply to Jarkko SakkinenSashiko has reviewed this patch and found no issues.
Quoted 9 lines
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.
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] keys: NULL dereference in keyctl_session_to_parent() without session keyring --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] keys: NULL dereference in keyctl_session_to_parent() without session keyring --
commit e79a6a32e7b5e470d5cab49936bcd7b8bfc551d1
Author: Jarkko Sakkinen <[email protected]>
KEYS: Return user session keyring on lookup
This commit changes lookup_user_key() so that looking up KEY_SPEC_SESSION_KEYRING without KEY_LOOKUP_CREATE returns the user session keyring directly instead of installing it into the current task's credentials.
Quoted 4 lines
security/keys/process_keys.c +0
-0
[ ... ]
Quoted 18 lines
security/keys/process_keys.c +5
-5@@ -665,20 +640,21 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,case KEY_SPEC_SESSION_KEYRING :if (!ctx.cred->session_keyring) { -/* always install a session keyring upon access if one -* doesn't exist yet */ ret =look_up_user_keyrings (NULL , &user_session);if (ret <0 ) goto error; -if (lflags &KEY_LOOKUP_CREATE ) + +if (lflags &KEY_LOOKUP_CREATE ) { +key_put (user_session); ret =join_session_keyring (NULL ); -else - ret =install_session_keyring (user_session); +if (ret <0 ) + goto error;
security/keys/process_keys.c key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, · line 657
Quoted 10 lines
security/keys/process_keys.c +5
-4+ goto reget_creds; + } -key_put (user_session); -if (ret <0 ) - goto error; - goto reget_creds; + key = user_session; + key_ref =make_key_ref (key,1 ); +break ;
[Severity: Critical]
Could returning user_session here without installing it into the current task's credentials cause a NULL pointer dereference in keyctl_session_to_parent()?
In security/keys/keyctl.c, keyctl_session_to_parent() looks up KEY_SPEC_SESSION_KEYRING without KEY_LOOKUP_CREATE and stores the returned keyring in cred->session_keyring:
security/keys/keyctl.c:keyctl_session_to_parent() {
keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
if (IS_ERR(keyring_r))
return PTR_ERR(keyring_r);
...
cred->session_keyring = key_ref_to_ptr(keyring_r);
...
mycred = current_cred();
pcred = __task_cred(parent);
if (mycred == pcred ||
mycred->session_keyring == pcred->session_keyring) {
ret = 0;
goto unlock;
}
...
/* 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))
goto unlock;
}Previously, lookup_user_key() installed user_session into the current task's credentials, guaranteeing that mycred->session_keyring was non-NULL.
With this change, if a child process has no session keyring (mycred->session_keyring is NULL) and its parent has a non-NULL pcred->session_keyring with matching ownership, the check mycred->session_keyring == pcred->session_keyring is false, and !uid_eq(mycred->session_keyring->uid, mycred->euid) dereferences NULL while holding write_lock_irq(&tasklist_lock) and rcu_read_lock().
Additionally, if both the child and the parent have session_keyring == NULL, mycred->session_keyring == pcred->session_keyring evaluates to true (NULL == NULL), causing keyctl_session_to_parent() to return 0 early without installing cred->session_keyring onto the parent.
Should keyctl_session_to_parent() check cred->session_keyring instead of mycred->session_keyring (and avoid the mycred == pcred early exit when mycred->session_keyring is NULL)?
Quoted 3 lines
security/keys/process_keys.c +0
-0}else if (test_bit (KEY_FLAG_UID_KEYRING , &ctx.cred->session_keyring->flags) && lflags &KEY_LOOKUP_CREATE ) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
syzbot ci has tested the following series
syzbot ci has tested the following series
[v2] KEYS: Return user session keyring on lookup
https://lore.kernel.org/all/[email protected]
* [PATCH v2 1/2] KEYS: Return user session keyring on lookup
* [PATCH v2 2/2] KEYS: Reject keyring creation with overridden credentials
and found the following issue:
general protection fault in keyctl_session_to_parent
Full report is available here:
https://ci.syzbot.org/series/7566814a-52ba-4f0b-b506-d7adf67fda29
***
general protection fault in keyctl_session_to_parent
tree: linux-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/next/linux-next base: 17a7501b9612a52b38bc57e33c7b64636d438bc0 arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/eee373c7-f447-49cf-a350-83830580c7b2/config
Oops: general protection fault, probably for non-canonical address 0xdffffc000000001b: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x00000000000000d8-0x00000000000000df] CPU: 0 UID: 0 PID: 5730 Comm: syz.0.6 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:keyctl_session_to_parent+0x825/0xa30 Code: 35 3b fd e9 fe fa ff ff e8 38 35 3b fd e9 f4 fa ff ff e8 2e 35 3b fd 48 8b 54 24 20 48 81 c2 d8 00 00 00 48 89 d0 48 c1 e8 03 <0f> b6 04 18 84 c0 0f 85 b2 01 00 00 8b 2a 89 ef 8b 5c 24 1c 89 de RSP: 0018:ffffc9000621fc70 EFLAGS: 00010006 RAX: 000000000000001b RBX: dffffc0000000000 RCX: ffff88810b603c00 RDX: 00000000000000d8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000003 R09: 0000000000000004 R10: dffffc0000000000 R11: fffff52000c43f7c R12: ffff88811a010000 R13: 0000000000000000 R14: ffff88810c2631a8 R15: 0000000000000000 FS: 00007f7f2068b6c0(0000) GS:ffff88818c4bf000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7f2068aff8 CR3: 000000010cae8000 CR4: 00000000000006f0 Call Trace: <TASK> __se_sys_keyctl+0x780/0x9d0 do_syscall_64+0x166/0x520 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f7f1f79f559 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f7f2068b028 EFLAGS: 00000246 ORIG_RAX: 00000000000000fa RAX: ffffffffffffffda RBX: 00007f7f1fa26270 RCX: 00007f7f1f79f559 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000012 RBP: 00007f7f1f836b20 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f7f1fa26308 R14: 00007f7f1fa26270 R15: 00007ffe789f59b8 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:keyctl_session_to_parent+0x825/0xa30 Code: 35 3b fd e9 fe fa ff ff e8 38 35 3b fd e9 f4 fa ff ff e8 2e 35 3b fd 48 8b 54 24 20 48 81 c2 d8 00 00 00 48 89 d0 48 c1 e8 03 <0f> b6 04 18 84 c0 0f 85 b2 01 00 00 8b 2a 89 ef 8b 5c 24 1c 89 de RSP: 0018:ffffc9000621fc70 EFLAGS: 00010006 RAX: 000000000000001b RBX: dffffc0000000000 RCX: ffff88810b603c00 RDX: 00000000000000d8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000003 R09: 0000000000000004 R10: dffffc0000000000 R11: fffff52000c43f7c R12: ffff88811a010000 R13: 0000000000000000 R14: ffff88810c2631a8 R15: 0000000000000000 FS: 00007f7f2068b6c0(0000) GS:ffff88818c4bf000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7f2068aff8 CR3: 000000010cae8000 CR4: 00000000000006f0
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
To test a fix for this bug, please reply with #syz test (on a separate line) and attach the patch to the email.
Notes:
- The patch will be applied on top of the tested series (as an incremental fix).
- To test a new version of the whole series, please send it directly to [email protected].
- Arguments like custom git repos and branches are not supported.
Jarkko Sakkinen
maintainerQuoted 81 lines · syzbot ci
syzbot ci has tested the following series
[v2] KEYS: Return user session keyring on lookup
https://lore.kernel.org/all/[email protected]
* [PATCH v2 1/2] KEYS: Return user session keyring on lookup
* [PATCH v2 2/2] KEYS: Reject keyring creation with overridden credentialsand found the following issue:
general protection fault in keyctl_session_to_parentFull report is available here:
https://ci.syzbot.org/series/7566814a-52ba-4f0b-b506-d7adf67fda29***
general protection fault in keyctl_session_to_parenttree: linux-next URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/next/linux-next base: 17a7501b9612a52b38bc57e33c7b64636d438bc0 arch: amd64 compiler: Debian clang version 22.1.8 (1~deb13u4), Debian LLD 22.1.8 config: https://ci.syzbot.org/builds/eee373c7-f447-49cf-a350-83830580c7b2/configOops: general protection fault, probably for non-canonical address 0xdffffc000000001b: 0000 [#1] SMP KASAN PTI KASAN: null-ptr-deref in range [0x00000000000000d8-0x00000000000000df] CPU: 0 UID: 0 PID: 5730 Comm: syz.0.6 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:keyctl_session_to_parent+0x825/0xa30 Code: 35 3b fd e9 fe fa ff ff e8 38 35 3b fd e9 f4 fa ff ff e8 2e 35 3b fd 48 8b 54 24 20 48 81 c2 d8 00 00 00 48 89 d0 48 c1 e8 03 <0f> b6 04 18 84 c0 0f 85 b2 01 00 00 8b 2a 89 ef 8b 5c 24 1c 89 de RSP: 0018:ffffc9000621fc70 EFLAGS: 00010006 RAX: 000000000000001b RBX: dffffc0000000000 RCX: ffff88810b603c00 RDX: 00000000000000d8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000003 R09: 0000000000000004 R10: dffffc0000000000 R11: fffff52000c43f7c R12: ffff88811a010000 R13: 0000000000000000 R14: ffff88810c2631a8 R15: 0000000000000000 FS: 00007f7f2068b6c0(0000) GS:ffff88818c4bf000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7f2068aff8 CR3: 000000010cae8000 CR4: 00000000000006f0 Call Trace: <TASK> __se_sys_keyctl+0x780/0x9d0 do_syscall_64+0x166/0x520 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f7f1f79f559 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f7f2068b028 EFLAGS: 00000246 ORIG_RAX: 00000000000000fa RAX: ffffffffffffffda RBX: 00007f7f1fa26270 RCX: 00007f7f1f79f559 RDX: 0000000000000000 RSI: 0000000000000000 RDI: 0000000000000012 RBP: 00007f7f1f836b20 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f7f1fa26308 R14: 00007f7f1fa26270 R15: 00007ffe789f59b8 </TASK> Modules linked in: ---[ end trace 0000000000000000 ]--- RIP: 0010:keyctl_session_to_parent+0x825/0xa30 Code: 35 3b fd e9 fe fa ff ff e8 38 35 3b fd e9 f4 fa ff ff e8 2e 35 3b fd 48 8b 54 24 20 48 81 c2 d8 00 00 00 48 89 d0 48 c1 e8 03 <0f> b6 04 18 84 c0 0f 85 b2 01 00 00 8b 2a 89 ef 8b 5c 24 1c 89 de RSP: 0018:ffffc9000621fc70 EFLAGS: 00010006 RAX: 000000000000001b RBX: dffffc0000000000 RCX: ffff88810b603c00 RDX: 00000000000000d8 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000000 R08: 0000000000000003 R09: 0000000000000004 R10: dffffc0000000000 R11: fffff52000c43f7c R12: ffff88811a010000 R13: 0000000000000000 R14: ffff88810c2631a8 R15: 0000000000000000 FS: 00007f7f2068b6c0(0000) GS:ffff88818c4bf000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f7f2068aff8 CR3: 000000010cae8000 CR4: 00000000000006f0***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
Tested-by [email protected]---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].To test a fix for this bug, please reply with
#syz test(on a separate line) and attach the patch to the email.
Quoted 6 lines
Notes:
- The patch will be applied on top of the tested series (as an incremental fix).
- To test a new version of the whole series, please send it directly to [email protected].
- Arguments like custom git repos and branches are not supported.
This helps, I save this to my TODO-folder (no time to address this week).
Br, Jarkko