Skip to contentSkip to conversation

keys: Restrict KEYCTL_SESSION_TO_PARENT according to ptrace_may_access()

PATCH In review Mickaël Salaün 4 participants 14 messages Jul 29, 2024 – Aug 2, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keys: Restrict KEYCTL_SESSION_TO_PARENT according to ptrace_may_access()

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Cc David Howells <[email protected]>
Cc Günther Noack <[email protected]>
Cc James Morris <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Kees Cook <[email protected]>
Cc Paul Moore <[email protected]>
Fixes ee18d64c1f63 ("KEYS: Add a keyctl to install a process's session keyring on its parent [try #6]")
---
 security/keys/keyctl.c | 5 +++++
 1 file changed, 5 insertions(+)
security/keys/keyctl.c +5 -0
index ab927a142f51..511bf79fa14c 100644
@@ -21,6 +21,7 @@
 #include <linux/security.h>
 #include <linux/uio.h>
 #include <linux/uaccess.h>
+#include <linux/ptrace.h>
 #include <keys/request_key_auth-type.h>
 #include "internal.h"
 
@@ -1687,6 +1688,10 @@ long keyctl_session_to_parent(void)
 	    !gid_eq(pcred->sgid, mycred->egid))
 		goto unlock;
 
+	/* The child must be allowed to impersonate its parent process. */
+	if (!ptrace_may_access(parent, PTRACE_MODE_ATTACH_REALCREDS))
+		goto unlock;
+
 	/* the keyrings must have the same UID */
 	if ((pcred->session_keyring &&
 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||

base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b

Signature · 1 line
2.45.2
#1 lore raw
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

I think KEYCTL_SESSION_TO_PARENT is not a great design, but I'm not sure if we can improve it much without risking some breakage.

#2 lore raw
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check. So this change should be OK with Yama and most use cases.

Quoted 4 lines

I think KEYCTL_SESSION_TO_PARENT is not a great design, but I'm not sure if we can improve it much without risking some breakage.

I think this is a security issue that a process can change another process's credentials. If the main use cases is for shell commands, it should be OK.

The alternative would be to restore the key_session_to_parent LSM hook [1], and update most LSMs to block this kind of credential tampering, which will lead to the same result but with only partial users being protected.

[1] commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

#3 lore raw
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

#4 lore raw
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

#5 lore raw
Quoted 34 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Quoted 3 lines

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

#6 lore raw
Quoted 35 lines · Jann Horn
Quoted 34 lines · Mickaël Salaün
Quoted 32 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Quoted 7 lines
Quoted 3 lines

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

maintainer
#7 lore raw
Quoted 40 lines · Mickaël Salaün
Quoted 39 lines · Jann Horn
Quoted 37 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Quoted 6 lines
Quoted 3 lines

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

While I was looking at this in another off-list thread I think I came around to the same conclusion: I think we want the security_key_session_to_parent() hook back, and while I'm wearing my SELinux hat, I think we want a SELinux implementation.

Mickaël, is this something you want to work on?

Signature · 1 line
paul-moore.com
#8 lore raw
Quoted 45 lines · Paul Moore
Quoted 44 lines · Mickaël Salaün
Quoted 42 lines · Jann Horn
Quoted 34 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Quoted 3 lines

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

Quoted 7 lines
Quoted 2 lines

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

While I was looking at this in another off-list thread I think I came around to the same conclusion: I think we want the security_key_session_to_parent() hook back, and while I'm wearing my SELinux hat, I think we want a SELinux implementation.

FYI: Those checks, including the hook that formerly existed there, are (somewhat necessarily) racy wrt concurrent security context changes of the parent because they come before asynchronous work is posted to the parent to do the keyring update.

In theory we could make them synchronous if we have the child wait for the parent to enter task work... actually, with that we could probably get rid of the whole cred_transfer hack and have the parent do prepare_creds() and commit_creds() normally, and propagate any errors back to the child, as long as we don't create any deadlocks with this...

Quoted 1 line

Mickaël, is this something you want to work on?

maintainer
#9 lore raw
Quoted 54 lines · Jann Horn
Quoted 45 lines · Paul Moore
Quoted 44 lines · Mickaël Salaün
Quoted 42 lines · Jann Horn
Quoted 34 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Quoted 3 lines

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

Quoted 7 lines
Quoted 2 lines

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

While I was looking at this in another off-list thread I think I came around to the same conclusion: I think we want the security_key_session_to_parent() hook back, and while I'm wearing my SELinux hat, I think we want a SELinux implementation.

Quoted 4 lines

FYI: Those checks, including the hook that formerly existed there, are (somewhat necessarily) racy wrt concurrent security context changes of the parent because they come before asynchronous work is posted to the parent to do the keyring update.

I was wondering about something similar while looking at keyctl_session_to_parent(), aren't all of the parent's cred checks here racy?

Quoted 6 lines

In theory we could make them synchronous if we have the child wait for the parent to enter task work... actually, with that we could probably get rid of the whole cred_transfer hack and have the parent do prepare_creds() and commit_creds() normally, and propagate any errors back to the child, as long as we don't create any deadlocks with this...

Assuming that no problems are caused by waiting on the parent, this might be the best approach. Should we also move, or duplicate, the cred checks into the parent's context to avoid any races?

Quoted 1 line
Quoted 1 line

Mickaël, is this something you want to work on?

Signature · 1 line
paul-moore.com
#10 lore raw
Quoted 55 lines · Paul Moore
Quoted 54 lines · Jann Horn
Quoted 52 lines · Paul Moore
Quoted 40 lines · Mickaël Salaün
Quoted 39 lines · Jann Horn
Quoted 37 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Quoted 6 lines
Quoted 3 lines

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

While I was looking at this in another off-list thread I think I came around to the same conclusion: I think we want the security_key_session_to_parent() hook back, and while I'm wearing my SELinux hat, I think we want a SELinux implementation.

Quoted 8 lines
Quoted 4 lines

FYI: Those checks, including the hook that formerly existed there, are (somewhat necessarily) racy wrt concurrent security context changes of the parent because they come before asynchronous work is posted to the parent to do the keyring update.

I was wondering about something similar while looking at keyctl_session_to_parent(), aren't all of the parent's cred checks here racy?

Yeah...

Quoted 10 lines
Quoted 6 lines

In theory we could make them synchronous if we have the child wait for the parent to enter task work... actually, with that we could probably get rid of the whole cred_transfer hack and have the parent do prepare_creds() and commit_creds() normally, and propagate any errors back to the child, as long as we don't create any deadlocks with this...

Assuming that no problems are caused by waiting on the parent, this might be the best approach. Should we also move, or duplicate, the cred checks into the parent's context to avoid any races?

Yeah, I think that'd probably be a reasonable way to do it. Post task work to the parent, wait for the task work to finish (with an interruptible sleep that cancels the work item on EINTR), and then do the checks and stuff in the parent. I guess whether we should also do racy checks in the child before that depends on whether we're worried about a child without the necessary permissions being able to cause spurious syscall restarts in the parent...

Quoted 4 lines
Quoted 1 line
Quoted 1 line

Mickaël, is this something you want to work on?

--
paul-moore.com

#11 lore raw
Quoted 78 lines · Jann Horn
Quoted 55 lines · Paul Moore
Quoted 54 lines · Jann Horn
Quoted 52 lines · Paul Moore
Quoted 40 lines · Mickaël Salaün
Quoted 39 lines · Jann Horn
Quoted 37 lines · Mickaël Salaün
Quoted 24 lines · Jann Horn
Quoted 19 lines · Mickaël Salaün
Quoted 10 lines · Jann Horn
Quoted 8 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Quoted 7 lines

FWIW, my understanding is that the intended usecase of KEYCTL_SESSION_TO_PARENT is that command-line tools (like "keyctl new_session" and "e4crypt new_session") want to be able to change the keyring of the parent process that spawned them (which I think is usually a shell?); and Yama LSM, which I think is fairly widely used at this point, by default prevents a child process from using PTRACE_MODE_ATTACH on its parent.

Quoted 3 lines

About Yama, the patched keyctl_session_to_parent() function already check if the current's and the parent's credentials are the same before this new ptrace_may_access() check.

Quoted 8 lines

prepare_exec_creds() in execve() always creates new credentials which are stored in bprm->cred and then later committed in begin_new_exec(). Also, fork() always copies the credentials in copy_creds(). So the "mycred == pcred" condition in keyctl_session_to_parent() basically never applies, I think.
Also: When that condition is true, the whole operation is a no-op, since if the credentials are the same, then the session keyring that the credentials point to must also be the same.

Correct, it's not a content comparison. We could compare the credential's data for this specific KEYCTL_SESSION_TO_PARENT call, I guess this should not be performance sensitive.

Quoted 6 lines
Quoted 3 lines

Yeah, though I guess keyctl_session_to_parent() is already kind of doing that for the UID information; and for LSMs that would mean adding an extra LSM hook?

I'm wondering why security_key_session_to_parent() was never used: see commit 3011a344cdcd ("security: remove dead hook key_session_to_parent")

While I was looking at this in another off-list thread I think I came around to the same conclusion: I think we want the security_key_session_to_parent() hook back, and while I'm wearing my SELinux hat, I think we want a SELinux implementation.

Quoted 8 lines
Quoted 4 lines

FYI: Those checks, including the hook that formerly existed there, are (somewhat necessarily) racy wrt concurrent security context changes of the parent because they come before asynchronous work is posted to the parent to do the keyring update.

I was wondering about something similar while looking at keyctl_session_to_parent(), aren't all of the parent's cred checks here racy?

Yeah...

Quoted 10 lines
Quoted 6 lines

In theory we could make them synchronous if we have the child wait for the parent to enter task work... actually, with that we could probably get rid of the whole cred_transfer hack and have the parent do prepare_creds() and commit_creds() normally, and propagate any errors back to the child, as long as we don't create any deadlocks with this...

Assuming that no problems are caused by waiting on the parent, this might be the best approach. Should we also move, or duplicate, the cred checks into the parent's context to avoid any races?

Quoted 7 lines

Yeah, I think that'd probably be a reasonable way to do it. Post task work to the parent, wait for the task work to finish (with an interruptible sleep that cancels the work item on EINTR), and then do the checks and stuff in the parent. I guess whether we should also do racy checks in the child before that depends on whether we're worried about a child without the necessary permissions being able to cause spurious syscall restarts in the parent...

Why doing the check only in the parent and reporting back the result to the child could be a security issue? I guess duplicating the check would just avoid executing useless code in the parent side if the child doesn't have enough privileges right?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Mickaël, is this something you want to work on?

I'll let you handle the new design of the hook, but I'll review it. :)

I guess we're not OK to tie the KEYCTL_SESSION_TO_PARENT call to a ptrace_may_access() mainly because of the Yama case? I'm wondering if we should add an exception for Yama here, or if each LSM should implement its own new hook with the related new bit of security policy. I guess some systems with a fine-tuned SELinux policy could be an issue too.

Anyway, I wondering what was the motivation to only/mainly check EUID/EGID for keyring change.

#12 lore raw
In reply to Jann Horn
Quoted 24 lines · Jann Horn
Quoted 9 lines · Paul Moore
Quoted 4 lines · Jann Horn

FYI: Those checks, including the hook that formerly existed there, are (somewhat necessarily) racy wrt concurrent security context changes of the parent because they come before asynchronous work is posted to the parent to do the keyring update.

I was wondering about something similar while looking at keyctl_session_to_parent(), aren't all of the parent's cred checks here racy?

Yeah...

Quoted 10 lines
Quoted 6 lines

In theory we could make them synchronous if we have the child wait for the parent to enter task work... actually, with that we could probably get rid of the whole cred_transfer hack and have the parent do prepare_creds() and commit_creds() normally, and propagate any errors back to the child, as long as we don't create any deadlocks with this...

Assuming that no problems are caused by waiting on the parent, this might be the best approach. Should we also move, or duplicate, the cred checks into the parent's context to avoid any races?

Quoted 7 lines

Yeah, I think that'd probably be a reasonable way to do it. Post task work to the parent, wait for the task work to finish (with an interruptible sleep that cancels the work item on EINTR), and then do the checks and stuff in the parent. I guess whether we should also do racy checks in the child before that depends on whether we're worried about a child without the necessary permissions being able to cause spurious syscall restarts in the parent...

I hacked up an RFC patch for this approach:
https://lore.kernel.org/r/[email protected]

#13 lore raw
In reply to Mickaël Salaün
Quoted 3 lines · Mickaël Salaün

A process can modify its parent's credentials with KEYCTL_SESSION_TO_PARENT when their EUID and EGID are the same. This doesn't take into account all possible access controls.

Add a smoke test transcript here, which demonstrates the above for A/B testing sake so that there is no need to invent one by the reviewer.

Otherwise, it is too involved to give tested-by tag to this patch.

Quoted 44 lines

Enforce the same access checks as for impersonating a process.

The current credentials checks are untouch because they check against EUID and EGID, whereas ptrace_may_access() checks against UID and GID.

Cc David Howells <[email protected]>
Cc Günther Noack <[email protected]>
Cc James Morris <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Kees Cook <[email protected]>
Cc Paul Moore <[email protected]>
Fixes ee18d64c1f63 ("KEYS: Add a keyctl to install a process's session keyring on its parent [try #6]")
---
 security/keys/keyctl.c | 5 +++++
 1 file changed, 5 insertions(+)
security/keys/keyctl.c +5 -0
index ab927a142f51..511bf79fa14c 100644
@@ -21,6 +21,7 @@
 #include <linux/security.h>
 #include <linux/uio.h>
 #include <linux/uaccess.h>
+#include <linux/ptrace.h>
 #include <keys/request_key_auth-type.h>
 #include "internal.h"
 
@@ -1687,6 +1688,10 @@ long keyctl_session_to_parent(void)
 	    !gid_eq(pcred->sgid, mycred->egid))
 		goto unlock;
 
+	/* The child must be allowed to impersonate its parent process. */
+	if (!ptrace_may_access(parent, PTRACE_MODE_ATTACH_REALCREDS))
+		goto unlock;
+
 	/* the keyrings must have the same UID */
 	if ((pcred->session_keyring &&
 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
Quoted 1 line

base-commit: 8400291e289ee6b2bf9779ff1c83a291501f017b

BR, Jarkko