CRED: Separate task security context from task_struct
[linux-2.6-block.git] / security / keys / permission.c
index e7f579c0eaf541e393df94815eecffc565dcde3d..baf3d5f31e7136c6a92f5cd73d947accc6308530 100644 (file)
@@ -22,6 +22,7 @@ int key_task_permission(const key_ref_t key_ref,
                        struct task_struct *context,
                        key_perm_t perm)
 {
+       struct cred *cred = context->cred;
        struct key *key;
        key_perm_t kperm;
        int ret;
@@ -29,7 +30,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;
        }
@@ -37,14 +38,14 @@ 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);
+               spin_lock(&cred->lock);
+               ret = groups_search(cred->group_info, key->gid);
+               spin_unlock(&cred->lock);
 
                if (ret) {
                        kperm = key->perm >> 8;
@@ -73,3 +74,35 @@ use_these_perms:
 } /* 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);