]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - security/keys/internal.h
KEYS: Use structure to capture key restriction function and data
[mirror_ubuntu-hirsute-kernel.git] / security / keys / internal.h
CommitLineData
973c9f4f 1/* Authentication token and access key management internal defs
1da177e4 2 *
76181c13 3 * Copyright (C) 2003-5, 2007 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
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 _INTERNAL_H
13#define _INTERNAL_H
14
d84f4f99 15#include <linux/sched.h>
5b825c3a 16#include <linux/cred.h>
76181c13 17#include <linux/key-type.h>
413cd3d9 18#include <linux/task_work.h>
ddbb4114 19#include <linux/keyctl.h>
ddb99e11 20#include <linux/refcount.h>
1da177e4 21
a27bb332
KO
22struct iovec;
23
76181c13
DH
24#ifdef __KDEBUG
25#define kenter(FMT, ...) \
dd6f953a 26 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
76181c13 27#define kleave(FMT, ...) \
dd6f953a 28 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
76181c13 29#define kdebug(FMT, ...) \
d84f4f99 30 printk(KERN_DEBUG " "FMT"\n", ##__VA_ARGS__)
3e30148c 31#else
76181c13 32#define kenter(FMT, ...) \
dd6f953a 33 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
76181c13 34#define kleave(FMT, ...) \
dd6f953a 35 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
76181c13
DH
36#define kdebug(FMT, ...) \
37 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
3e30148c
DH
38#endif
39
0c061b57 40extern struct key_type key_type_dead;
1da177e4 41extern struct key_type key_type_user;
9f6ed2ca 42extern struct key_type key_type_logon;
1da177e4
LT
43
44/*****************************************************************************/
45/*
973c9f4f
DH
46 * Keep track of keys for a user.
47 *
48 * This needs to be separate to user_struct to avoid a refcount-loop
49 * (user_struct pins some keyrings which pin this struct).
50 *
51 * We also keep track of keys under request from userspace for this UID here.
1da177e4
LT
52 */
53struct key_user {
54 struct rb_node node;
76181c13 55 struct mutex cons_lock; /* construction initiation lock */
1da177e4 56 spinlock_t lock;
ddb99e11 57 refcount_t usage; /* for accessing qnkeys & qnbytes */
1da177e4
LT
58 atomic_t nkeys; /* number of keys */
59 atomic_t nikeys; /* number of instantiated keys */
9a56c2db 60 kuid_t uid;
1da177e4
LT
61 int qnkeys; /* number of keys allocated to this user */
62 int qnbytes; /* number of bytes allocated to this user */
63};
64
1da177e4
LT
65extern struct rb_root key_user_tree;
66extern spinlock_t key_user_lock;
67extern struct key_user root_key_user;
68
9a56c2db 69extern struct key_user *key_user_lookup(kuid_t uid);
1da177e4
LT
70extern void key_user_put(struct key_user *user);
71
0b77f5bf 72/*
973c9f4f 73 * Key quota limits.
0b77f5bf
DH
74 * - root has its own separate limits to everyone else
75 */
76extern unsigned key_quota_root_maxkeys;
77extern unsigned key_quota_root_maxbytes;
78extern unsigned key_quota_maxkeys;
79extern unsigned key_quota_maxbytes;
80
81#define KEYQUOTA_LINK_BYTES 4 /* a link in a keyring is worth 4 bytes */
1da177e4
LT
82
83
8bc16dea 84extern struct kmem_cache *key_jar;
1da177e4
LT
85extern struct rb_root key_serial_tree;
86extern spinlock_t key_serial_lock;
76181c13 87extern struct mutex key_construction_mutex;
1da177e4
LT
88extern wait_queue_head_t request_key_conswq;
89
90
e9e349b0
DH
91extern struct key_type *key_type_lookup(const char *type);
92extern void key_type_put(struct key_type *ktype);
93
f70e2e06 94extern int __key_link_begin(struct key *keyring,
16feef43 95 const struct keyring_index_key *index_key,
b2a4df20 96 struct assoc_array_edit **_edit);
f70e2e06 97extern int __key_link_check_live_key(struct key *keyring, struct key *key);
b2a4df20 98extern void __key_link(struct key *key, struct assoc_array_edit **_edit);
f70e2e06 99extern void __key_link_end(struct key *keyring,
16feef43 100 const struct keyring_index_key *index_key,
b2a4df20 101 struct assoc_array_edit *edit);
1da177e4 102
b2a4df20
DH
103extern key_ref_t find_key_to_update(key_ref_t keyring_ref,
104 const struct keyring_index_key *index_key);
1da177e4 105
3e30148c
DH
106extern struct key *keyring_search_instkey(struct key *keyring,
107 key_serial_t target_id);
108
b2a4df20
DH
109extern int iterate_over_keyring(const struct key *keyring,
110 int (*func)(const struct key *key, void *data),
111 void *data);
112
4bdf0bc3
DH
113struct keyring_search_context {
114 struct keyring_index_key index_key;
115 const struct cred *cred;
46291959 116 struct key_match_data match_data;
4bdf0bc3 117 unsigned flags;
614d8c39
DH
118#define KEYRING_SEARCH_NO_STATE_CHECK 0x0001 /* Skip state checks */
119#define KEYRING_SEARCH_DO_STATE_CHECK 0x0002 /* Override NO_STATE_CHECK */
120#define KEYRING_SEARCH_NO_UPDATE_TIME 0x0004 /* Don't update times */
121#define KEYRING_SEARCH_NO_CHECK_PERM 0x0008 /* Don't check permissions */
122#define KEYRING_SEARCH_DETECT_TOO_DEEP 0x0010 /* Give an error on excessive depth */
0b0a8415 123#define KEYRING_SEARCH_SKIP_EXPIRED 0x0020 /* Ignore expired keys (intention to replace) */
4bdf0bc3 124
b2a4df20
DH
125 int (*iterator)(const void *object, void *iterator_data);
126
4bdf0bc3
DH
127 /* Internal stuff */
128 int skipped_ret;
129 bool possessed;
130 key_ref_t result;
131 struct timespec now;
132};
133
0c903ab6
DH
134extern bool key_default_cmp(const struct key *key,
135 const struct key_match_data *match_data);
664cceb0 136extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
4bdf0bc3
DH
137 struct keyring_search_context *ctx);
138
139extern key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx);
140extern key_ref_t search_process_keyrings(struct keyring_search_context *ctx);
1da177e4 141
69664cf1 142extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1da177e4 143
8bbf4976 144extern int install_user_keyrings(void);
d84f4f99
DH
145extern int install_thread_keyring_to_cred(struct cred *);
146extern int install_process_keyring_to_cred(struct cred *);
685bfd2c 147extern int install_session_keyring_to_cred(struct cred *, struct key *);
3e30148c
DH
148
149extern struct key *request_key_and_link(struct key_type *type,
150 const char *description,
4a38e122
DH
151 const void *callout_info,
152 size_t callout_len,
4e54f085 153 void *aux,
7e047ef5
DH
154 struct key *dest_keyring,
155 unsigned long flags);
3e30148c 156
0c903ab6
DH
157extern bool lookup_user_key_possessed(const struct key *key,
158 const struct key_match_data *match_data);
5593122e 159extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
e9e349b0 160 key_perm_t perm);
5593122e
DH
161#define KEY_LOOKUP_CREATE 0x01
162#define KEY_LOOKUP_PARTIAL 0x02
163#define KEY_LOOKUP_FOR_UNLINK 0x04
e9e349b0
DH
164
165extern long join_session_keyring(const char *name);
67d12145 166extern void key_change_session_keyring(struct callback_head *twork);
e9e349b0 167
0c061b57 168extern struct work_struct key_gc_work;
5d135440
DH
169extern unsigned key_gc_delay;
170extern void keyring_gc(struct key *keyring, time_t limit);
2b6aa412
MM
171extern void keyring_restriction_gc(struct key *keyring,
172 struct key_type *dead_type);
fd75815f
DH
173extern void key_schedule_gc(time_t gc_at);
174extern void key_schedule_gc_links(void);
0c061b57 175extern void key_gc_keytype(struct key_type *ktype);
5d135440 176
e9e349b0 177extern int key_task_permission(const key_ref_t key_ref,
d84f4f99 178 const struct cred *cred,
e9e349b0
DH
179 key_perm_t perm);
180
973c9f4f
DH
181/*
182 * Check to see whether permission is granted to use a key in the desired way.
183 */
f5895943 184static inline int key_permission(const key_ref_t key_ref, unsigned perm)
e9e349b0 185{
d84f4f99 186 return key_task_permission(key_ref, current_cred(), perm);
e9e349b0
DH
187}
188
3e30148c 189/*
973c9f4f 190 * Authorisation record for request_key().
3e30148c
DH
191 */
192struct request_key_auth {
193 struct key *target_key;
8bbf4976 194 struct key *dest_keyring;
d84f4f99 195 const struct cred *cred;
4a38e122
DH
196 void *callout_info;
197 size_t callout_len;
3e30148c
DH
198 pid_t pid;
199};
200
201extern struct key_type key_type_request_key_auth;
202extern struct key *request_key_auth_new(struct key *target,
4a38e122 203 const void *callout_info,
8bbf4976
DH
204 size_t callout_len,
205 struct key *dest_keyring);
3e30148c
DH
206
207extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1da177e4 208
fd75815f
DH
209/*
210 * Determine whether a key is dead.
211 */
7e55ca6d 212static inline bool key_is_dead(const struct key *key, time_t limit)
fd75815f
DH
213{
214 return
215 key->flags & ((1 << KEY_FLAG_DEAD) |
216 (1 << KEY_FLAG_INVALIDATED)) ||
217 (key->expiry > 0 && key->expiry <= limit);
218}
219
1da177e4 220/*
973c9f4f 221 * keyctl() functions
1da177e4
LT
222 */
223extern long keyctl_get_keyring_ID(key_serial_t, int);
224extern long keyctl_join_session_keyring(const char __user *);
225extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
226extern long keyctl_revoke_key(key_serial_t);
227extern long keyctl_keyring_clear(key_serial_t);
228extern long keyctl_keyring_link(key_serial_t, key_serial_t);
229extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
230extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
231extern long keyctl_keyring_search(key_serial_t, const char __user *,
232 const char __user *, key_serial_t);
233extern long keyctl_read_key(key_serial_t, char __user *, size_t);
234extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
235extern long keyctl_setperm_key(key_serial_t, key_perm_t);
236extern long keyctl_instantiate_key(key_serial_t, const void __user *,
237 size_t, key_serial_t);
238extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
3e30148c 239extern long keyctl_set_reqkey_keyring(int);
017679c4 240extern long keyctl_set_timeout(key_serial_t, unsigned);
b5f545c8 241extern long keyctl_assume_authority(key_serial_t);
70a5bb72
DH
242extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
243 size_t buflen);
ee18d64c 244extern long keyctl_session_to_parent(void);
fdd1b945 245extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t);
ee009e4a
DH
246extern long keyctl_instantiate_key_iov(key_serial_t,
247 const struct iovec __user *,
248 unsigned, key_serial_t);
fd75815f 249extern long keyctl_invalidate_key(key_serial_t);
ee009e4a 250
b353a1f7 251struct iov_iter;
ee009e4a 252extern long keyctl_instantiate_key_common(key_serial_t,
b353a1f7
AV
253 struct iov_iter *,
254 key_serial_t);
f36f8c75
DH
255#ifdef CONFIG_PERSISTENT_KEYRINGS
256extern long keyctl_get_persistent(uid_t, key_serial_t);
257extern unsigned persistent_keyring_expiry;
258#else
259static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)
260{
261 return -EOPNOTSUPP;
262}
263#endif
1da177e4 264
ddbb4114
MM
265#ifdef CONFIG_KEY_DH_OPERATIONS
266extern long keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
4693fc73 267 size_t, void __user *);
ddbb4114
MM
268#else
269static inline long keyctl_dh_compute(struct keyctl_dh_params __user *params,
4693fc73
SM
270 char __user *buffer, size_t buflen,
271 void __user *reserved)
ddbb4114
MM
272{
273 return -EOPNOTSUPP;
274}
275#endif
276
1da177e4 277/*
973c9f4f 278 * Debugging key validation
1da177e4
LT
279 */
280#ifdef KEY_DEBUGGING
281extern void __key_check(const struct key *);
282
283static inline void key_check(const struct key *key)
284{
285 if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
286 __key_check(key);
287}
288
289#else
290
291#define key_check(key) do {} while(0)
292
293#endif
294
295#endif /* _INTERNAL_H */