]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - security/keys/internal.h
keys: Replace uid/gid/perm permissions checking with an ACL
[mirror_ubuntu-jammy-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>
5dd43ce2 16#include <linux/wait_bit.h>
5b825c3a 17#include <linux/cred.h>
76181c13 18#include <linux/key-type.h>
413cd3d9 19#include <linux/task_work.h>
ddbb4114 20#include <linux/keyctl.h>
ddb99e11 21#include <linux/refcount.h>
f1c316a3 22#include <linux/compat.h>
1da177e4 23
a27bb332
KO
24struct iovec;
25
76181c13
DH
26#ifdef __KDEBUG
27#define kenter(FMT, ...) \
dd6f953a 28 printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
76181c13 29#define kleave(FMT, ...) \
dd6f953a 30 printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
76181c13 31#define kdebug(FMT, ...) \
d84f4f99 32 printk(KERN_DEBUG " "FMT"\n", ##__VA_ARGS__)
3e30148c 33#else
76181c13 34#define kenter(FMT, ...) \
dd6f953a 35 no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
76181c13 36#define kleave(FMT, ...) \
dd6f953a 37 no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
76181c13
DH
38#define kdebug(FMT, ...) \
39 no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
3e30148c
DH
40#endif
41
0c061b57 42extern struct key_type key_type_dead;
1da177e4 43extern struct key_type key_type_user;
9f6ed2ca 44extern struct key_type key_type_logon;
1da177e4
LT
45
46/*****************************************************************************/
47/*
973c9f4f
DH
48 * Keep track of keys for a user.
49 *
50 * This needs to be separate to user_struct to avoid a refcount-loop
51 * (user_struct pins some keyrings which pin this struct).
52 *
53 * We also keep track of keys under request from userspace for this UID here.
1da177e4
LT
54 */
55struct key_user {
56 struct rb_node node;
76181c13 57 struct mutex cons_lock; /* construction initiation lock */
1da177e4 58 spinlock_t lock;
ddb99e11 59 refcount_t usage; /* for accessing qnkeys & qnbytes */
1da177e4
LT
60 atomic_t nkeys; /* number of keys */
61 atomic_t nikeys; /* number of instantiated keys */
9a56c2db 62 kuid_t uid;
1da177e4
LT
63 int qnkeys; /* number of keys allocated to this user */
64 int qnbytes; /* number of bytes allocated to this user */
65};
66
1da177e4
LT
67extern struct rb_root key_user_tree;
68extern spinlock_t key_user_lock;
69extern struct key_user root_key_user;
70
9a56c2db 71extern struct key_user *key_user_lookup(kuid_t uid);
1da177e4
LT
72extern void key_user_put(struct key_user *user);
73
0b77f5bf 74/*
973c9f4f 75 * Key quota limits.
0b77f5bf
DH
76 * - root has its own separate limits to everyone else
77 */
78extern unsigned key_quota_root_maxkeys;
79extern unsigned key_quota_root_maxbytes;
80extern unsigned key_quota_maxkeys;
81extern unsigned key_quota_maxbytes;
82
83#define KEYQUOTA_LINK_BYTES 4 /* a link in a keyring is worth 4 bytes */
1da177e4
LT
84
85
8bc16dea 86extern struct kmem_cache *key_jar;
1da177e4
LT
87extern struct rb_root key_serial_tree;
88extern spinlock_t key_serial_lock;
76181c13 89extern struct mutex key_construction_mutex;
1da177e4 90extern wait_queue_head_t request_key_conswq;
2e12256b
DH
91extern struct key_acl default_key_acl;
92extern struct key_acl joinable_keyring_acl;
1da177e4 93
355ef8e1 94extern void key_set_index_key(struct keyring_index_key *index_key);
2e12256b 95
e9e349b0
DH
96extern struct key_type *key_type_lookup(const char *type);
97extern void key_type_put(struct key_type *ktype);
98
df593ee2
DH
99extern int __key_link_lock(struct key *keyring,
100 const struct keyring_index_key *index_key);
ed0ac5c7
DH
101extern int __key_move_lock(struct key *l_keyring, struct key *u_keyring,
102 const struct keyring_index_key *index_key);
f70e2e06 103extern int __key_link_begin(struct key *keyring,
16feef43 104 const struct keyring_index_key *index_key,
b2a4df20 105 struct assoc_array_edit **_edit);
f70e2e06 106extern int __key_link_check_live_key(struct key *keyring, struct key *key);
b2a4df20 107extern void __key_link(struct key *key, struct assoc_array_edit **_edit);
f70e2e06 108extern void __key_link_end(struct key *keyring,
16feef43 109 const struct keyring_index_key *index_key,
b2a4df20 110 struct assoc_array_edit *edit);
1da177e4 111
b2a4df20
DH
112extern key_ref_t find_key_to_update(key_ref_t keyring_ref,
113 const struct keyring_index_key *index_key);
1da177e4 114
3e30148c
DH
115extern struct key *keyring_search_instkey(struct key *keyring,
116 key_serial_t target_id);
117
b2a4df20
DH
118extern int iterate_over_keyring(const struct key *keyring,
119 int (*func)(const struct key *key, void *data),
120 void *data);
121
4bdf0bc3
DH
122struct keyring_search_context {
123 struct keyring_index_key index_key;
124 const struct cred *cred;
46291959 125 struct key_match_data match_data;
4bdf0bc3 126 unsigned flags;
614d8c39
DH
127#define KEYRING_SEARCH_NO_STATE_CHECK 0x0001 /* Skip state checks */
128#define KEYRING_SEARCH_DO_STATE_CHECK 0x0002 /* Override NO_STATE_CHECK */
129#define KEYRING_SEARCH_NO_UPDATE_TIME 0x0004 /* Don't update times */
130#define KEYRING_SEARCH_NO_CHECK_PERM 0x0008 /* Don't check permissions */
131#define KEYRING_SEARCH_DETECT_TOO_DEEP 0x0010 /* Give an error on excessive depth */
0b0a8415 132#define KEYRING_SEARCH_SKIP_EXPIRED 0x0020 /* Ignore expired keys (intention to replace) */
dcf49dbc 133#define KEYRING_SEARCH_RECURSE 0x0040 /* Search child keyrings also */
4bdf0bc3 134
b2a4df20
DH
135 int (*iterator)(const void *object, void *iterator_data);
136
4bdf0bc3
DH
137 /* Internal stuff */
138 int skipped_ret;
139 bool possessed;
140 key_ref_t result;
074d5898 141 time64_t now;
4bdf0bc3
DH
142};
143
0c903ab6
DH
144extern bool key_default_cmp(const struct key *key,
145 const struct key_match_data *match_data);
e59428f7 146extern key_ref_t keyring_search_rcu(key_ref_t keyring_ref,
4bdf0bc3
DH
147 struct keyring_search_context *ctx);
148
e59428f7
DH
149extern key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx);
150extern key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx);
1da177e4 151
237bbd29 152extern struct key *find_keyring_by_name(const char *name, bool uid_keyring);
1da177e4 153
0f44e4d9
DH
154extern int look_up_user_keyrings(struct key **, struct key **);
155extern struct key *get_user_session_keyring_rcu(const struct cred *);
d84f4f99
DH
156extern int install_thread_keyring_to_cred(struct cred *);
157extern int install_process_keyring_to_cred(struct cred *);
685bfd2c 158extern int install_session_keyring_to_cred(struct cred *, struct key *);
3e30148c
DH
159
160extern struct key *request_key_and_link(struct key_type *type,
161 const char *description,
a58946c1 162 struct key_tag *domain_tag,
4a38e122
DH
163 const void *callout_info,
164 size_t callout_len,
4e54f085 165 void *aux,
2e12256b 166 struct key_acl *acl,
7e047ef5
DH
167 struct key *dest_keyring,
168 unsigned long flags);
3e30148c 169
0c903ab6
DH
170extern bool lookup_user_key_possessed(const struct key *key,
171 const struct key_match_data *match_data);
5593122e
DH
172#define KEY_LOOKUP_CREATE 0x01
173#define KEY_LOOKUP_PARTIAL 0x02
174#define KEY_LOOKUP_FOR_UNLINK 0x04
e9e349b0
DH
175
176extern long join_session_keyring(const char *name);
67d12145 177extern void key_change_session_keyring(struct callback_head *twork);
e9e349b0 178
0c061b57 179extern struct work_struct key_gc_work;
5d135440 180extern unsigned key_gc_delay;
074d5898 181extern void keyring_gc(struct key *keyring, time64_t limit);
2b6aa412
MM
182extern void keyring_restriction_gc(struct key *keyring,
183 struct key_type *dead_type);
074d5898 184extern void key_schedule_gc(time64_t gc_at);
fd75815f 185extern void key_schedule_gc_links(void);
0c061b57 186extern void key_gc_keytype(struct key_type *ktype);
5d135440 187
e9e349b0 188extern int key_task_permission(const key_ref_t key_ref,
d84f4f99 189 const struct cred *cred,
2e12256b
DH
190 u32 desired_perm);
191extern unsigned int key_acl_to_perm(const struct key_acl *acl);
192extern long key_set_acl(struct key *key, struct key_acl *acl);
193extern void key_put_acl(struct key_acl *acl);
e9e349b0 194
973c9f4f
DH
195/*
196 * Check to see whether permission is granted to use a key in the desired way.
197 */
f5895943 198static inline int key_permission(const key_ref_t key_ref, unsigned perm)
e9e349b0 199{
d84f4f99 200 return key_task_permission(key_ref, current_cred(), perm);
e9e349b0
DH
201}
202
3e30148c
DH
203extern struct key_type key_type_request_key_auth;
204extern struct key *request_key_auth_new(struct key *target,
822ad64d 205 const char *op,
4a38e122 206 const void *callout_info,
8bbf4976
DH
207 size_t callout_len,
208 struct key *dest_keyring);
3e30148c
DH
209
210extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1da177e4 211
fd75815f
DH
212/*
213 * Determine whether a key is dead.
214 */
074d5898 215static inline bool key_is_dead(const struct key *key, time64_t limit)
fd75815f
DH
216{
217 return
218 key->flags & ((1 << KEY_FLAG_DEAD) |
219 (1 << KEY_FLAG_INVALIDATED)) ||
218e6424
DH
220 (key->expiry > 0 && key->expiry <= limit) ||
221 key->domain_tag->removed;
fd75815f
DH
222}
223
1da177e4 224/*
973c9f4f 225 * keyctl() functions
1da177e4
LT
226 */
227extern long keyctl_get_keyring_ID(key_serial_t, int);
228extern long keyctl_join_session_keyring(const char __user *);
229extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
230extern long keyctl_revoke_key(key_serial_t);
231extern long keyctl_keyring_clear(key_serial_t);
232extern long keyctl_keyring_link(key_serial_t, key_serial_t);
ed0ac5c7 233extern long keyctl_keyring_move(key_serial_t, key_serial_t, key_serial_t, unsigned int);
1da177e4
LT
234extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
235extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
236extern long keyctl_keyring_search(key_serial_t, const char __user *,
237 const char __user *, key_serial_t);
238extern long keyctl_read_key(key_serial_t, char __user *, size_t);
239extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
2e12256b 240extern long keyctl_setperm_key(key_serial_t, unsigned int);
1da177e4
LT
241extern long keyctl_instantiate_key(key_serial_t, const void __user *,
242 size_t, key_serial_t);
243extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
3e30148c 244extern long keyctl_set_reqkey_keyring(int);
017679c4 245extern long keyctl_set_timeout(key_serial_t, unsigned);
b5f545c8 246extern long keyctl_assume_authority(key_serial_t);
70a5bb72
DH
247extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
248 size_t buflen);
ee18d64c 249extern long keyctl_session_to_parent(void);
fdd1b945 250extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t);
ee009e4a
DH
251extern long keyctl_instantiate_key_iov(key_serial_t,
252 const struct iovec __user *,
253 unsigned, key_serial_t);
fd75815f 254extern long keyctl_invalidate_key(key_serial_t);
ee009e4a 255
b353a1f7 256struct iov_iter;
ee009e4a 257extern long keyctl_instantiate_key_common(key_serial_t,
b353a1f7
AV
258 struct iov_iter *,
259 key_serial_t);
6563c91f
MM
260extern long keyctl_restrict_keyring(key_serial_t id,
261 const char __user *_type,
262 const char __user *_restriction);
f36f8c75
DH
263#ifdef CONFIG_PERSISTENT_KEYRINGS
264extern long keyctl_get_persistent(uid_t, key_serial_t);
265extern unsigned persistent_keyring_expiry;
266#else
267static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)
268{
269 return -EOPNOTSUPP;
270}
271#endif
1da177e4 272
ddbb4114
MM
273#ifdef CONFIG_KEY_DH_OPERATIONS
274extern long keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
f1c316a3
SM
275 size_t, struct keyctl_kdf_params __user *);
276extern long __keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
277 size_t, struct keyctl_kdf_params *);
278#ifdef CONFIG_KEYS_COMPAT
279extern long compat_keyctl_dh_compute(struct keyctl_dh_params __user *params,
280 char __user *buffer, size_t buflen,
281 struct compat_keyctl_kdf_params __user *kdf);
282#endif
283#define KEYCTL_KDF_MAX_OUTPUT_LEN 1024 /* max length of KDF output */
284#define KEYCTL_KDF_MAX_OI_LEN 64 /* max length of otherinfo */
ddbb4114
MM
285#else
286static inline long keyctl_dh_compute(struct keyctl_dh_params __user *params,
4693fc73 287 char __user *buffer, size_t buflen,
f1c316a3
SM
288 struct keyctl_kdf_params __user *kdf)
289{
290 return -EOPNOTSUPP;
291}
292
293#ifdef CONFIG_KEYS_COMPAT
294static inline long compat_keyctl_dh_compute(
295 struct keyctl_dh_params __user *params,
296 char __user *buffer, size_t buflen,
297 struct keyctl_kdf_params __user *kdf)
ddbb4114
MM
298{
299 return -EOPNOTSUPP;
300}
301#endif
f1c316a3 302#endif
ddbb4114 303
00d60fd3
DH
304#ifdef CONFIG_ASYMMETRIC_KEY_TYPE
305extern long keyctl_pkey_query(key_serial_t,
306 const char __user *,
307 struct keyctl_pkey_query __user *);
308
309extern long keyctl_pkey_verify(const struct keyctl_pkey_params __user *,
310 const char __user *,
311 const void __user *, const void __user *);
312
313extern long keyctl_pkey_e_d_s(int,
314 const struct keyctl_pkey_params __user *,
315 const char __user *,
316 const void __user *, void __user *);
317#else
318static inline long keyctl_pkey_query(key_serial_t id,
319 const char __user *_info,
320 struct keyctl_pkey_query __user *_res)
321{
322 return -EOPNOTSUPP;
323}
324
325static inline long keyctl_pkey_verify(const struct keyctl_pkey_params __user *params,
326 const char __user *_info,
327 const void __user *_in,
328 const void __user *_in2)
329{
330 return -EOPNOTSUPP;
331}
332
333static inline long keyctl_pkey_e_d_s(int op,
334 const struct keyctl_pkey_params __user *params,
335 const char __user *_info,
336 const void __user *_in,
337 void __user *_out)
338{
339 return -EOPNOTSUPP;
340}
341#endif
342
45e0f30c
DH
343extern long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen);
344
1da177e4 345/*
973c9f4f 346 * Debugging key validation
1da177e4
LT
347 */
348#ifdef KEY_DEBUGGING
349extern void __key_check(const struct key *);
350
351static inline void key_check(const struct key *key)
352{
353 if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
354 __key_check(key);
355}
356
357#else
358
359#define key_check(key) do {} while(0)
360
361#endif
362
363#endif /* _INTERNAL_H */