keys: Address lookup_user_key() mutability
Subsystems (3)
Jarkko Sakkinen
maintainercover keys: Address lookup_user_key() mutability
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentials
Documentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)
--
2.47.3
Jarkko Sakkinen
maintainerRFC PATCH 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.
--- 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
[email protected]
AI reviewRe: [RFC PATCH 1/2] keys: Return user session keyring on lookup
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
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.
Suggested-by Jann Horn <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Jarkko Sakkinen
maintainerRFC PATCH 2/2 keys: Reject keyring creation with overridden credentials
In reply to Jarkko SakkinenIf 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.
--- 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. */
@@ -578,7 +586,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.
@@ -607,6 +616,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);
@@ -625,6 +639,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);
@@ -646,6 +665,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;
@@ -658,6 +682,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
[email protected]
AI reviewRe: [RFC PATCH 2/2] keys: Reject keyring creation with overridden credentials
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 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.Signed-off-by Jarkko Sakkinen <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Jarkko Sakkinen
maintainerQuoted 13 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentials
Quoted 7 lines
Documentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)--
2.47.3
So.. should I move forward to with non-RFC v2?
Note that some tags (e.g., Fixes) are missing simply because this is RFC.
Br, Jarkko
Serge E. Hallyn
maintainerQuoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)
Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Quoted 3 lines
Note that some tags (e.g., Fixes) are missing simply because this is RFC.
Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 25 lines · Serge E. Hallyn
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Quoted 8 lines
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Thanks a lot of responding.
I fully agree with you but I'd need help for evaluating things further.
1/2 based on Jann's email about the topic and my interpretation of the suggestion. I neither used brains nor AI for this and consider the patch merely as a conversation starter (and what a great success it was on doing that) :-)
All feedback is good feedback at this point. I need supporting code to remember/recall later on so at least this serves that purpose.
Br, Jarkko
Quoted 35 lines · Jarkko Sakkinen
Quoted 25 lines · Serge E. Hallyn
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Quoted 8 lines
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Quoted 8 lines
Thanks a lot of responding.
I fully agree with you but I'd need help for evaluating things further.
1/2 based on Jann's email about the topic and my interpretation of the suggestion. I neither used brains nor AI for this and consider the patch merely as a conversation starter (and what a great success it was on doing that) :-)
:)
Quoted 4 lines
All feedback is good feedback at this point. I need supporting code to remember/recall later on so at least this serves that purpose.
Br, Jarkko
I've been staring at the before and after code for a bit now. It does even more than I was thinking :) But I was trying to detail the changed behavior (from userspace's pov), and after several attempts, it seems maybe there actually isn't any.
Q: is there any path whereby the
```
else if (test_bit(KEY_FLAG_UID_KEYRING,
&ctx.cred->session_keyring->flags) &&
lflags & KEY_LOOKUP_CREATE) {
```case can still happen? Or was this the only place where we installed the user_session keyring as session keyring, so that we can drop this branch?
Jarkko Sakkinen
maintainerQuoted 65 lines · Serge Hallyn (AMD)
Quoted 35 lines · Jarkko Sakkinen
Quoted 25 lines · Serge E. Hallyn
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Quoted 8 lines
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Quoted 8 lines
Thanks a lot of responding.
I fully agree with you but I'd need help for evaluating things further.
1/2 based on Jann's email about the topic and my interpretation of the suggestion. I neither used brains nor AI for this and consider the patch merely as a conversation starter (and what a great success it was on doing that) :-)
:)
Quoted 4 lines
All feedback is good feedback at this point. I need supporting code to remember/recall later on so at least this serves that purpose.
Br, Jarkko
I've been staring at the before and after code for a bit now. It does even more than I was thinking :) But I was trying to detail the changed behavior (from userspace's pov), and after several attempts, it seems maybe there actually isn't any.
Q: is there any path whereby the
``` else if (test_bit(KEY_FLAG_UID_KEYRING, &ctx.cred->session_keyring->flags) && lflags & KEY_LOOKUP_CREATE) { ```
Quoted 3 lines
case can still happen? Or was this the only place where we installed the
user_sessionkeyring as session keyring, so that we can drop this branch?
Right, so *I think* that it is not useful and e.g., PAM uses NULL.
At the same time it is harmless. We could remove it but it would be also uapi change (for granted, useless branch).
I could do it but not without any feedback from David.
Br, Jarkko
Serge E. Hallyn
maintainerQuoted 66 lines · Jarkko Sakkinen
Quoted 65 lines · Serge Hallyn (AMD)
Quoted 35 lines · Jarkko Sakkinen
Quoted 25 lines · Serge E. Hallyn
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Quoted 8 lines
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Quoted 8 lines
Thanks a lot of responding.
I fully agree with you but I'd need help for evaluating things further.
1/2 based on Jann's email about the topic and my interpretation of the suggestion. I neither used brains nor AI for this and consider the patch merely as a conversation starter (and what a great success it was on doing that) :-)
:)
Quoted 4 lines
All feedback is good feedback at this point. I need supporting code to remember/recall later on so at least this serves that purpose.
Br, Jarkko
I've been staring at the before and after code for a bit now. It does even more than I was thinking :) But I was trying to detail the changed behavior (from userspace's pov), and after several attempts, it seems maybe there actually isn't any.
Q: is there any path whereby the
``` else if (test_bit(KEY_FLAG_UID_KEYRING, &ctx.cred->session_keyring->flags) && lflags & KEY_LOOKUP_CREATE) { ```
Quoted 8 lines
Quoted 3 lines
case can still happen? Or was this the only place where we installed the
user_sessionkeyring as session keyring, so that we can drop this branch?Right, so *I think* that it is not useful and e.g., PAM uses NULL.
At the same time it is harmless. We could remove it but it would be also uapi change (for granted, useless branch).
There's
https://sources.debian.org/src/e2fsprogs/1.47.4-1/misc/e4crypt.c?hl=568#L568
which bemoans that:
* The fact that the keyctl system call and the add_key system
* call treats KEY_SPEC_SESSION_KEYRING differently when a
* session keyring does not exist is very unfortunate and
* confusing, but so it goes...
Near as I can tell, add_key just always uses KEY_LOOKUP_CREATE?
And https://sources.debian.org/src/krb5/1.22.1-3/src/lib/krb5/ccache/cc_keyring.c?hl=307#L307
I'm not sure whether previous kernels did that, or whether the authors mixed up testing with and without KEY_LOOKUP_CREATE.
Quoted 3 lines
I could do it but not without any feedback from David.
Br, Jarkko
Jarkko Sakkinen
maintainerQuoted 86 lines · Serge E. Hallyn
Quoted 66 lines · Jarkko Sakkinen
Quoted 65 lines · Serge Hallyn (AMD)
Quoted 35 lines · Jarkko Sakkinen
Quoted 25 lines · Serge E. Hallyn
Quoted 18 lines · Jarkko Sakkinen
Quoted 17 lines · Jarkko Sakkinen
The main objective in these patches is to remove to unneeded mutability from key look ups. It does harm and brings no value so it is pretty obvious to me that addressing this harmful behaviour is what we should do.
I based the first patch on what Jann suggested in [1]. Nothing too clever here and I'm ofc open for further suggestions.
[1] https://lore.kernel.org/keyrings/CAG48ez0XjVSR=M--UBauTm8sJuc+ZbhKzaPa3a6wrSVHyoKevw@mail.gmail.com/
Jarkko Sakkinen (2):
keys: Return user session keyring on lookup
keys: Reject keyring creation with overridden credentialsDocumentation/security/keys/core.rst | 7 ++- security/keys/process_keys.c | 65 +++++++++++++++------------- 2 files changed, 40 insertions(+), 32 deletions(-)Quoted 5 lines
Quoted 3 lines
--
2.47.3So.. should I move forward to with non-RFC v2?
Quoted 8 lines
Patch 2 absolutely makes sense, "doc, it hurts when I do this", "don't do that then."
Regarding patch 1, that seems like quite a change in behavior, right? I don't see any docs or comments that promise the current behavior, but has any userspace or subsystem come to depend on it? I guess that, if so, then it just has to create a link to the user session keyring like pam (according to what I've read) does?
Quoted 8 lines
Thanks a lot of responding.
I fully agree with you but I'd need help for evaluating things further.
1/2 based on Jann's email about the topic and my interpretation of the suggestion. I neither used brains nor AI for this and consider the patch merely as a conversation starter (and what a great success it was on doing that) :-)
:)
Quoted 4 lines
All feedback is good feedback at this point. I need supporting code to remember/recall later on so at least this serves that purpose.
Br, Jarkko
I've been staring at the before and after code for a bit now. It does even more than I was thinking :) But I was trying to detail the changed behavior (from userspace's pov), and after several attempts, it seems maybe there actually isn't any.
Q: is there any path whereby the
``` else if (test_bit(KEY_FLAG_UID_KEYRING, &ctx.cred->session_keyring->flags) && lflags & KEY_LOOKUP_CREATE) { ```Quoted 8 lines
Quoted 3 lines
case can still happen? Or was this the only place where we installed the
user_sessionkeyring as session keyring, so that we can drop this branch?Right, so *I think* that it is not useful and e.g., PAM uses NULL.
At the same time it is harmless. We could remove it but it would be also uapi change (for granted, useless branch).
There's
https://sources.debian.org/src/e2fsprogs/1.47.4-1/misc/e4crypt.c?hl=568#L568
which bemoans that:
* The fact that the keyctl system call and theadd_keysystem
* call treatsKEY_SPEC_SESSION_KEYRINGdifferently when a
* session keyring does not exist is very unfortunate and
* confusing, but so it goes...
Quoted 6 lines
Near as I can tell,
add_keyjust always usesKEY_LOOKUP_CREATE?And https://sources.debian.org/src/krb5/1.22.1-3/src/lib/krb5/ccache/cc_keyring.c?hl=307#L307
I'm not sure whether previous kernels did that, or whether the authors mixed up testing with and without
KEY_LOOKUP_CREATE.
Ugh, I feel that I always to go the code from beginning to check these up but those workarounds are still valid i.e., these patches do not address write divergence.
I would summarize my fixes along the lines of:
1. 1/2 exactly removes cred creation as a side-effect invisible to the caller..
2. 2/2 handles request_key()'s from nfsd and other in-kernel daemons. I.e. it transforms BUG_ON() into -EPERM.
I'd like to keep this patch set strictly within scope and somewhat surgical if/when it is applied.
And it looks to me a lot that this might well be the smallest possible change that addresses exactly unwanted BUG_ON() conditions pointed out by Jann surpsingly well.
Br, Jarkko