CRED: Inaugurate COW credentials
[linux-block.git] / security / keys / permission.c
index 03db073ba45c526ef82979babcc4abff47ea8438..5d9fc7b93f2e988f0b96c8fb2e89caacac4147cb 100644 (file)
  */
 
 #include <linux/module.h>
+#include <linux/security.h>
 #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 key *key;
@@ -28,7 +36,7 @@ int key_task_permission(const key_ref_t key_ref,
        key = key_ref_to_ptr(key_ref);
 
        /* use the second 8-bits of permissions for keys the caller owns */
-       if (key->uid == context->fsuid) {
+       if (key->uid == cred->fsuid) {
                kperm = key->perm >> 16;
                goto use_these_perms;
        }
@@ -36,15 +44,12 @@ int key_task_permission(const key_ref_t key_ref,
        /* use the third 8-bits of permissions for keys the caller has a group
         * membership in common with */
        if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
-               if (key->gid == context->fsgid) {
+               if (key->gid == cred->fsgid) {
                        kperm = key->perm >> 8;
                        goto use_these_perms;
                }
 
-               task_lock(context);
-               ret = groups_search(context->group_info, key->gid);
-               task_unlock(context);
-
+               ret = groups_search(cred->group_info, key->gid);
                if (ret) {
                        kperm = key->perm >> 8;
                        goto use_these_perms;
@@ -55,6 +60,7 @@ int key_task_permission(const key_ref_t key_ref,
        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
         */
@@ -63,8 +69,44 @@ use_these_perms:
 
        kperm = kperm & perm & KEY_ALL;
 
-       return kperm == perm;
+       if (kperm != perm)
+               return -EACCES;
+
+       /* let LSM be the final arbiter */
+       return security_key_permission(key_ref, cred, perm);
 
 } /* end key_task_permission() */
 
 EXPORT_SYMBOL(key_task_permission);
+
+/*****************************************************************************/
+/*
+ * validate a key
+ */
+int key_validate(struct key *key)
+{
+       struct timespec now;
+       int ret = 0;
+
+       if (key) {
+               /* check it's still accessible */
+               ret = -EKEYREVOKED;
+               if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
+                   test_bit(KEY_FLAG_DEAD, &key->flags))
+                       goto error;
+
+               /* check it hasn't expired */
+               ret = 0;
+               if (key->expiry) {
+                       now = current_kernel_time();
+                       if (now.tv_sec >= key->expiry)
+                               ret = -EKEYEXPIRED;
+               }
+       }
+
+ error:
+       return ret;
+
+} /* end key_validate() */
+
+EXPORT_SYMBOL(key_validate);