Skip to contentSkip to conversation

KEYS: Fix credential handling in lookup_user_key()

PATCH New Jarkko Sakkinen 2 participants 4 messages Nov 29

Subsystems (4)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

cover KEYS: Fix credential handling in lookup_user_key()

The commit messages looked a bit too stub so I refined them just a bit and piggybacked these changes for a sanity check round:

1. Return the user session keyring directly on non-creating KEY_SPEC_SESSION_KEYRING lookups instead of mutating task credentials.
2. Reject KEY_LOOKUP_CREATE with -EPERM when subjective credentials are overridden, avoiding the BUG_ON() in commit_creds().

Hope this helps with the clarity. They are pushed to my -next mirror branch but I can still either drop or add more tags. If nothing comes up, I'll just move forward with the PR.

Cc David Howells <[email protected]>
Cc Guicai Ma <[email protected]>
Cc James Morris <[email protected]>
Cc Jann Horn <[email protected]>
Cc Jonathan Corbet <[email protected]>
Cc Paul Moore <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Shuah Khan <[email protected]>

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/keyctl.c               |  5 ++-
 security/keys/process_keys.c         | 50 +++++++++-------------------
 3 files changed, 22 insertions(+), 40 deletions(-)

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
prerequisite-patch-id: 6b9f0c26cb5c2290d5a2a78c18ccf225b09a6bbb
prerequisite-patch-id: 0f71199bbbbaf9fe1fa82afb1926b452c493532e
prerequisite-patch-id: 97be79eb77addef7282d5cb9966c4208cf0e965e --
2.47.3

author maintainer
#1 lore raw

PATCH 1/2 KEYS: Return user session keyring on lookup

In lookup_user_key(), looking up KEY_SPEC_SESSION_KEYRING without KEY_LOOKUP_CREATE installs the user session keyring into the task's credentials when no session keyring is present.

Mutating credentials on a read-only lookup leaves credentials cached by other subsystems, such as file->f_cred, out of sync with the task.

Return the user session keyring directly without installing it, and check the resolved keyring in keyctl_session_to_parent().

---
 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
index 37ed42382f16..e3a215e55f81 100644
@@ -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
index d6b10fd67627..b11716f88ded 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/2 KEYS: Reject keyring creation with overridden credentials

In reply to Jarkko Sakkinen

Creating a thread, process, or session keyring in lookup_user_key() installs new task credentials via commit_creds(), which triggers a BUG_ON() when the task runs with overridden credentials.

Reject KEY_LOOKUP_CREATE with -EPERM when credentials are overridden.

---
 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
index e3a215e55f81..a60b2f725a0b 100644
@@ -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
index cdf03b8124d0..c878a5f8d47c 100644
@@ -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) && current_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