blob: 918c34a8347e6eb2b33624026f28d0a36bf77476 [file] [log] [blame]
David Howells3e301482005-06-23 22:00:56 -07001/* key-ui.h: key userspace interface stuff
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2004 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#ifndef _LINUX_KEY_UI_H
13#define _LINUX_KEY_UI_H
14
15#include <linux/key.h>
16
17/* the key tree */
18extern struct rb_root key_serial_tree;
19extern spinlock_t key_serial_lock;
20
21/* required permissions */
22#define KEY_VIEW 0x01 /* require permission to view attributes */
23#define KEY_READ 0x02 /* require permission to read content */
24#define KEY_WRITE 0x04 /* require permission to update / modify */
25#define KEY_SEARCH 0x08 /* require permission to search (keyring) or find (key) */
26#define KEY_LINK 0x10 /* require permission to link */
27#define KEY_ALL 0x1f /* all the above permissions */
28
29/*
30 * the keyring payload contains a list of the keys to which the keyring is
31 * subscribed
32 */
33struct keyring_list {
David Howells76d8aea2005-06-23 22:00:49 -070034 struct rcu_head rcu; /* RCU deletion hook */
35 unsigned short maxkeys; /* max keys this list can hold */
36 unsigned short nkeys; /* number of keys currently held */
37 unsigned short delkey; /* key to be unlinked by RCU */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct key *keys[0];
39};
40
41
42/*
43 * check to see whether permission is granted to use a key in the desired way
44 */
David Howells664cceb2005-09-28 17:03:15 +010045static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
David Howells664cceb2005-09-28 17:03:15 +010047 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 key_perm_t kperm;
49
David Howells664cceb2005-09-28 17:03:15 +010050 if (is_key_possessed(key_ref))
51 kperm = key->perm >> 24;
52 else if (key->uid == current->fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 kperm = key->perm >> 16;
54 else if (key->gid != -1 &&
55 key->perm & KEY_GRP_ALL &&
56 in_group_p(key->gid)
57 )
58 kperm = key->perm >> 8;
59 else
60 kperm = key->perm;
61
62 kperm = kperm & perm & KEY_ALL;
63
64 return kperm == perm;
65}
66
67/*
68 * check to see whether permission is granted to use a key in at least one of
69 * the desired ways
70 */
David Howells664cceb2005-09-28 17:03:15 +010071static inline int key_any_permission(const key_ref_t key_ref, key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
David Howells664cceb2005-09-28 17:03:15 +010073 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 key_perm_t kperm;
75
David Howells664cceb2005-09-28 17:03:15 +010076 if (is_key_possessed(key_ref))
77 kperm = key->perm >> 24;
78 else if (key->uid == current->fsuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 kperm = key->perm >> 16;
80 else if (key->gid != -1 &&
81 key->perm & KEY_GRP_ALL &&
82 in_group_p(key->gid)
83 )
84 kperm = key->perm >> 8;
85 else
86 kperm = key->perm;
87
88 kperm = kperm & perm & KEY_ALL;
89
90 return kperm != 0;
91}
92
David Howells3e301482005-06-23 22:00:56 -070093static inline int key_task_groups_search(struct task_struct *tsk, gid_t gid)
94{
95 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David Howells3e301482005-06-23 22:00:56 -070097 task_lock(tsk);
98 ret = groups_search(tsk->group_info, gid);
99 task_unlock(tsk);
100 return ret;
101}
102
David Howells664cceb2005-09-28 17:03:15 +0100103static inline int key_task_permission(const key_ref_t key_ref,
David Howells3e301482005-06-23 22:00:56 -0700104 struct task_struct *context,
105 key_perm_t perm)
106{
David Howells664cceb2005-09-28 17:03:15 +0100107 struct key *key = key_ref_to_ptr(key_ref);
David Howells3e301482005-06-23 22:00:56 -0700108 key_perm_t kperm;
109
David Howells664cceb2005-09-28 17:03:15 +0100110 if (is_key_possessed(key_ref)) {
111 kperm = key->perm >> 24;
112 }
113 else if (key->uid == context->fsuid) {
David Howells3e301482005-06-23 22:00:56 -0700114 kperm = key->perm >> 16;
115 }
116 else if (key->gid != -1 &&
117 key->perm & KEY_GRP_ALL && (
118 key->gid == context->fsgid ||
119 key_task_groups_search(context, key->gid)
120 )
121 ) {
122 kperm = key->perm >> 8;
123 }
124 else {
125 kperm = key->perm;
126 }
127
128 kperm = kperm & perm & KEY_ALL;
129
130 return kperm == perm;
131
132}
133
David Howells664cceb2005-09-28 17:03:15 +0100134extern key_ref_t lookup_user_key(struct task_struct *context,
135 key_serial_t id, int create, int partial,
136 key_perm_t perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138extern long join_session_keyring(const char *name);
139
140extern struct key_type *key_type_lookup(const char *type);
141extern void key_type_put(struct key_type *ktype);
142
143#define key_negative_timeout 60 /* default timeout on a negative key's existence */
144
145
146#endif /* _LINUX_KEY_UI_H */