keys: skip keys from another user namespace
[safe/jmp/linux-2.6] / security / keys / permission.c
index baf3d5f..0ed802c 100644 (file)
 #include "internal.h"
 
 /*****************************************************************************/
-/*
- * check to see whether permission is granted to use a key in the desired way,
- * but permit the security modules to override
+/**
+ * key_task_permission - Check a key can be used
+ * @key_ref: The key to check
+ * @cred: The credentials to use
+ * @perm: The permissions to check for
+ *
+ * Check to see whether permission is granted to use a key in the desired way,
+ * but permit the security modules to override.
+ *
+ * The caller must hold either a ref on cred or must hold the RCU readlock or a
+ * spinlock.
  */
-int key_task_permission(const key_ref_t key_ref,
-                       struct task_struct *context,
+int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
                        key_perm_t perm)
 {
-       struct cred *cred = context->cred;
        struct key *key;
        key_perm_t kperm;
        int ret;
 
        key = key_ref_to_ptr(key_ref);
 
+       if (key->user->user_ns != cred->user->user_ns)
+               goto use_other_perms;
+
        /* use the second 8-bits of permissions for keys the caller owns */
        if (key->uid == cred->fsuid) {
                kperm = key->perm >> 16;
@@ -43,20 +52,20 @@ int key_task_permission(const key_ref_t key_ref,
                        goto use_these_perms;
                }
 
-               spin_lock(&cred->lock);
                ret = groups_search(cred->group_info, key->gid);
-               spin_unlock(&cred->lock);
-
                if (ret) {
                        kperm = key->perm >> 8;
                        goto use_these_perms;
                }
        }
 
+use_other_perms:
+
        /* otherwise use the least-significant 8-bits */
        kperm = key->perm;
 
 use_these_perms:
+
        /* use the top 8-bits of permissions for keys the caller possesses
         * - possessor permissions are additive with other permissions
         */
@@ -69,7 +78,7 @@ use_these_perms:
                return -EACCES;
 
        /* let LSM be the final arbiter */
-       return security_key_permission(key_ref, context, perm);
+       return security_key_permission(key_ref, cred, perm);
 
 } /* end key_task_permission() */