CRED: Separate task security context from task_struct
[linux-2.6-block.git] / security / keys / permission.c
CommitLineData
468ed2b0
DH
1/* permission.c: key permission determination
2 *
3 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
29db9190 13#include <linux/security.h>
468ed2b0
DH
14#include "internal.h"
15
16/*****************************************************************************/
17/*
18 * check to see whether permission is granted to use a key in the desired way,
19 * but permit the security modules to override
20 */
21int key_task_permission(const key_ref_t key_ref,
22 struct task_struct *context,
23 key_perm_t perm)
24{
b6dff3ec 25 struct cred *cred = context->cred;
468ed2b0
DH
26 struct key *key;
27 key_perm_t kperm;
28 int ret;
29
30 key = key_ref_to_ptr(key_ref);
31
468ed2b0 32 /* use the second 8-bits of permissions for keys the caller owns */
b6dff3ec 33 if (key->uid == cred->fsuid) {
468ed2b0
DH
34 kperm = key->perm >> 16;
35 goto use_these_perms;
36 }
37
38 /* use the third 8-bits of permissions for keys the caller has a group
39 * membership in common with */
40 if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
b6dff3ec 41 if (key->gid == cred->fsgid) {
468ed2b0
DH
42 kperm = key->perm >> 8;
43 goto use_these_perms;
44 }
45
b6dff3ec
DH
46 spin_lock(&cred->lock);
47 ret = groups_search(cred->group_info, key->gid);
48 spin_unlock(&cred->lock);
468ed2b0
DH
49
50 if (ret) {
51 kperm = key->perm >> 8;
52 goto use_these_perms;
53 }
54 }
55
56 /* otherwise use the least-significant 8-bits */
57 kperm = key->perm;
58
59use_these_perms:
7ab501db
DH
60 /* use the top 8-bits of permissions for keys the caller possesses
61 * - possessor permissions are additive with other permissions
62 */
63 if (is_key_possessed(key_ref))
64 kperm |= key->perm >> 24;
65
468ed2b0
DH
66 kperm = kperm & perm & KEY_ALL;
67
29db9190
DH
68 if (kperm != perm)
69 return -EACCES;
70
71 /* let LSM be the final arbiter */
72 return security_key_permission(key_ref, context, perm);
468ed2b0
DH
73
74} /* end key_task_permission() */
75
76EXPORT_SYMBOL(key_task_permission);
b5f545c8
DH
77
78/*****************************************************************************/
79/*
80 * validate a key
81 */
82int key_validate(struct key *key)
83{
84 struct timespec now;
85 int ret = 0;
86
87 if (key) {
88 /* check it's still accessible */
89 ret = -EKEYREVOKED;
90 if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
91 test_bit(KEY_FLAG_DEAD, &key->flags))
92 goto error;
93
94 /* check it hasn't expired */
95 ret = 0;
96 if (key->expiry) {
97 now = current_kernel_time();
98 if (now.tv_sec >= key->expiry)
99 ret = -EKEYEXPIRED;
100 }
101 }
102
103 error:
104 return ret;
105
106} /* end key_validate() */
107
108EXPORT_SYMBOL(key_validate);