]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - security/keys/permission.c
6284b1496c298be958621ccf0556792176a1cbab
[karo-tx-linux.git] / security / keys / permission.c
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>
13 #include <linux/security.h>
14 #include "internal.h"
15
16 /**
17  * key_task_permission - Check a key can be used
18  * @key_ref: The key to check
19  * @cred: The credentials to use
20  * @perm: The permissions to check for
21  *
22  * Check to see whether permission is granted to use a key in the desired way,
23  * but permit the security modules to override.
24  *
25  * The caller must hold either a ref on cred or must hold the RCU readlock or a
26  * spinlock.
27  */
28 int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
29                         key_perm_t perm)
30 {
31         struct key *key;
32         key_perm_t kperm;
33         int ret;
34
35         key = key_ref_to_ptr(key_ref);
36
37         if (key->user->user_ns != cred->user->user_ns)
38                 goto use_other_perms;
39
40         /* use the second 8-bits of permissions for keys the caller owns */
41         if (key->uid == cred->fsuid) {
42                 kperm = key->perm >> 16;
43                 goto use_these_perms;
44         }
45
46         /* use the third 8-bits of permissions for keys the caller has a group
47          * membership in common with */
48         if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
49                 if (key->gid == cred->fsgid) {
50                         kperm = key->perm >> 8;
51                         goto use_these_perms;
52                 }
53
54                 ret = groups_search(cred->group_info, key->gid);
55                 if (ret) {
56                         kperm = key->perm >> 8;
57                         goto use_these_perms;
58                 }
59         }
60
61 use_other_perms:
62
63         /* otherwise use the least-significant 8-bits */
64         kperm = key->perm;
65
66 use_these_perms:
67
68         /* use the top 8-bits of permissions for keys the caller possesses
69          * - possessor permissions are additive with other permissions
70          */
71         if (is_key_possessed(key_ref))
72                 kperm |= key->perm >> 24;
73
74         kperm = kperm & perm & KEY_ALL;
75
76         if (kperm != perm)
77                 return -EACCES;
78
79         /* let LSM be the final arbiter */
80         return security_key_permission(key_ref, cred, perm);
81 }
82
83 EXPORT_SYMBOL(key_task_permission);
84
85 /*
86  * validate a key
87  */
88 int key_validate(struct key *key)
89 {
90         struct timespec now;
91         int ret = 0;
92
93         if (key) {
94                 /* check it's still accessible */
95                 ret = -EKEYREVOKED;
96                 if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
97                     test_bit(KEY_FLAG_DEAD, &key->flags))
98                         goto error;
99
100                 /* check it hasn't expired */
101                 ret = 0;
102                 if (key->expiry) {
103                         now = current_kernel_time();
104                         if (now.tv_sec >= key->expiry)
105                                 ret = -EKEYEXPIRED;
106                 }
107         }
108
109 error:
110         return ret;
111 }
112
113 EXPORT_SYMBOL(key_validate);