[PATCH 02/37] KEYS: Check starting keyring as part of search

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: David Howells
Date: Wednesday, February 20, 2008 - 9:06 am

Check the starting keyring as part of the search to (a) see if that is what
we're searching for, and (b) to check it is still valid for searching.

The scenario:  User in process A does things that cause things to be
created in its process session keyring.  The user then does an su to
another user and starts a new process, B.  The two processes now
share the same process session keyring.

Process B does an NFS access which results in an upcall to gssd.
When gssd attempts to instantiate the context key (to be linked
into the process session keyring), it is denied access even though it
has an authorization key.

The order of calls is:

   keyctl_instantiate_key()
      lookup_user_key()				    (the default: case)
         search_process_keyrings(current)
	    search_process_keyrings(rka->context)   (recursive call)
	       keyring_search_aux()

keyring_search_aux() verifies the keys and keyrings underneath the
top-level keyring it is given, but that top-level keyring is neither
fully validated nor checked to see if it is the thing being searched for.

This patch changes keyring_search_aux() to:
1) do more validation on the top keyring it is given and
2) check whether that top-level keyring is the thing being searched for


Signed-off-by: Kevin Coffman <kwc@citi.umich.edu>
Signed-off-by: David Howells <dhowells@redhat.com>
---

 security/keys/keyring.c |   35 +++++++++++++++++++++++++++++++----
 1 files changed, 31 insertions(+), 4 deletions(-)


diff --git a/security/keys/keyring.c b/security/keys/keyring.c
index 88292e3..76b89b2 100644
--- a/security/keys/keyring.c
+++ b/security/keys/keyring.c
@@ -292,7 +292,7 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
 
 	struct keyring_list *keylist;
 	struct timespec now;
-	unsigned long possessed;
+	unsigned long possessed, kflags;
 	struct key *keyring, *key;
 	key_ref_t key_ref;
 	long err;
@@ -318,6 +318,32 @@ key_ref_t keyring_search_aux(key_ref_t keyring_ref,
 	now = current_kernel_time();
 	err = -EAGAIN;
 	sp = 0;
+	
+	/* firstly we should check to see if this top-level keyring is what we
+	 * are looking for */
+	key_ref = ERR_PTR(-EAGAIN);
+	kflags = keyring->flags;
+	if (keyring->type == type && match(keyring, description)) {
+		key = keyring;
+
+		/* check it isn't negative and hasn't expired or been
+		 * revoked */
+		if (kflags & (1 << KEY_FLAG_REVOKED))
+			goto error_2;
+		if (key->expiry && now.tv_sec >= key->expiry)
+			goto error_2;
+		key_ref = ERR_PTR(-ENOKEY);
+		if (kflags & (1 << KEY_FLAG_NEGATIVE))
+			goto error_2;
+		goto found;
+	}
+
+	/* otherwise, the top keyring must not be revoked, expired, or
+	 * negatively instantiated if we are to search it */
+	key_ref = ERR_PTR(-EAGAIN);
+	if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) ||
+	    (keyring->expiry && now.tv_sec >= keyring->expiry))
+		goto error_2;
 
 	/* start processing a new keyring */
 descend:
@@ -331,13 +357,14 @@ descend:
 	/* iterate through the keys in this keyring first */
 	for (kix = 0; kix < keylist->nkeys; kix++) {
 		key = keylist->keys[kix];
+		kflags = key->flags;
 
 		/* ignore keys not of this type */
 		if (key->type != type)
 			continue;
 
 		/* skip revoked keys and expired keys */
-		if (test_bit(KEY_FLAG_REVOKED, &key->flags))
+		if (kflags & (1 << KEY_FLAG_REVOKED))
 			continue;
 
 		if (key->expiry && now.tv_sec >= key->expiry)
@@ -352,8 +379,8 @@ descend:
 					context, KEY_SEARCH) < 0)
 			continue;
 
-		/* we set a different error code if we find a negative key */
-		if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
+		/* we set a different error code if we pass a negative key */
+		if (kflags & (1 << KEY_FLAG_NEGATIVE)) {
 			err = -ENOKEY;
 			continue;
 		}

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/37] Permit filesystem local caching, David Howells, (Wed Feb 20, 9:05 am)
[PATCH 02/37] KEYS: Check starting keyring as part of search, David Howells, (Wed Feb 20, 9:06 am)
[PATCH 19/37] CacheFiles: Export things for CacheFiles, David Howells, (Wed Feb 20, 9:07 am)
[PATCH 22/37] NFS: Add FS-Cache option bit and debug bit, David Howells, (Wed Feb 20, 9:07 am)
[PATCH 25/37] NFS: Define and create server-level objects, David Howells, (Wed Feb 20, 9:08 am)
[PATCH 28/37] NFS: Use local disk inode cache, David Howells, (Wed Feb 20, 9:08 am)
[PATCH 31/37] NFS: FS-Cache page management, David Howells, (Wed Feb 20, 9:08 am)
[PATCH 36/37] NFS: Display local caching state, David Howells, (Wed Feb 20, 9:09 am)
Re: [PATCH 00/37] Permit filesystem local caching, Serge E. Hallyn, (Wed Feb 20, 12:58 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Wed Feb 20, 1:11 pm)
Re: [PATCH 00/37] Permit filesystem local caching, Daniel Phillips, (Wed Feb 20, 8:07 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Thu Feb 21, 5:31 am)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Thu Feb 21, 7:55 am)
Re: [PATCH 00/37] Permit filesystem local caching, Kevin Coffman, (Thu Feb 21, 8:17 am)
Re: [PATCH 00/37] Permit filesystem local caching, Daniel Phillips, (Thu Feb 21, 3:44 pm)
RE: [PATCH 00/37] Permit filesystem local caching, Muntz, Daniel, (Thu Feb 21, 3:52 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Thu Feb 21, 4:33 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Thu Feb 21, 5:07 pm)
Re: [PATCH 00/37] Permit filesystem local caching, Daniel Phillips, (Thu Feb 21, 5:57 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Fri Feb 22, 5:48 am)
Re: [PATCH 00/37] Permit filesystem local caching, Chris Mason, (Fri Feb 22, 6:52 am)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Fri Feb 22, 9:12 am)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Fri Feb 22, 9:14 am)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Fri Feb 22, 9:47 am)
Re: [PATCH 00/37] Permit filesystem local caching, Daniel Phillips, (Fri Feb 22, 3:25 pm)
Re: [PATCH 00/37] Permit filesystem local caching, David Howells, (Fri Feb 22, 6:22 pm)