1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /* Authentication token and access key management
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
5*4882a593Smuzhiyun * Written by David Howells (dhowells@redhat.com)
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * See Documentation/security/keys/core.rst for information on keys/keyrings.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #ifndef _LINUX_KEY_H
11*4882a593Smuzhiyun #define _LINUX_KEY_H
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <linux/list.h>
15*4882a593Smuzhiyun #include <linux/rbtree.h>
16*4882a593Smuzhiyun #include <linux/rcupdate.h>
17*4882a593Smuzhiyun #include <linux/sysctl.h>
18*4882a593Smuzhiyun #include <linux/rwsem.h>
19*4882a593Smuzhiyun #include <linux/atomic.h>
20*4882a593Smuzhiyun #include <linux/assoc_array.h>
21*4882a593Smuzhiyun #include <linux/refcount.h>
22*4882a593Smuzhiyun #include <linux/time64.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #ifdef __KERNEL__
25*4882a593Smuzhiyun #include <linux/uidgid.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /* key handle serial number */
28*4882a593Smuzhiyun typedef int32_t key_serial_t;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /* key handle permissions mask */
31*4882a593Smuzhiyun typedef uint32_t key_perm_t;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun struct key;
34*4882a593Smuzhiyun struct net;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #ifdef CONFIG_KEYS
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #undef KEY_DEBUGGING
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
41*4882a593Smuzhiyun #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
42*4882a593Smuzhiyun #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
43*4882a593Smuzhiyun #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
44*4882a593Smuzhiyun #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
45*4882a593Smuzhiyun #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
46*4882a593Smuzhiyun #define KEY_POS_ALL 0x3f000000
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define KEY_USR_VIEW 0x00010000 /* user permissions... */
49*4882a593Smuzhiyun #define KEY_USR_READ 0x00020000
50*4882a593Smuzhiyun #define KEY_USR_WRITE 0x00040000
51*4882a593Smuzhiyun #define KEY_USR_SEARCH 0x00080000
52*4882a593Smuzhiyun #define KEY_USR_LINK 0x00100000
53*4882a593Smuzhiyun #define KEY_USR_SETATTR 0x00200000
54*4882a593Smuzhiyun #define KEY_USR_ALL 0x003f0000
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun #define KEY_GRP_VIEW 0x00000100 /* group permissions... */
57*4882a593Smuzhiyun #define KEY_GRP_READ 0x00000200
58*4882a593Smuzhiyun #define KEY_GRP_WRITE 0x00000400
59*4882a593Smuzhiyun #define KEY_GRP_SEARCH 0x00000800
60*4882a593Smuzhiyun #define KEY_GRP_LINK 0x00001000
61*4882a593Smuzhiyun #define KEY_GRP_SETATTR 0x00002000
62*4882a593Smuzhiyun #define KEY_GRP_ALL 0x00003f00
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
65*4882a593Smuzhiyun #define KEY_OTH_READ 0x00000002
66*4882a593Smuzhiyun #define KEY_OTH_WRITE 0x00000004
67*4882a593Smuzhiyun #define KEY_OTH_SEARCH 0x00000008
68*4882a593Smuzhiyun #define KEY_OTH_LINK 0x00000010
69*4882a593Smuzhiyun #define KEY_OTH_SETATTR 0x00000020
70*4882a593Smuzhiyun #define KEY_OTH_ALL 0x0000003f
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #define KEY_PERM_UNDEF 0xffffffff
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * The permissions required on a key that we're looking up.
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun enum key_need_perm {
78*4882a593Smuzhiyun KEY_NEED_UNSPECIFIED, /* Needed permission unspecified */
79*4882a593Smuzhiyun KEY_NEED_VIEW, /* Require permission to view attributes */
80*4882a593Smuzhiyun KEY_NEED_READ, /* Require permission to read content */
81*4882a593Smuzhiyun KEY_NEED_WRITE, /* Require permission to update / modify */
82*4882a593Smuzhiyun KEY_NEED_SEARCH, /* Require permission to search (keyring) or find (key) */
83*4882a593Smuzhiyun KEY_NEED_LINK, /* Require permission to link */
84*4882a593Smuzhiyun KEY_NEED_SETATTR, /* Require permission to change attributes */
85*4882a593Smuzhiyun KEY_NEED_UNLINK, /* Require permission to unlink key */
86*4882a593Smuzhiyun KEY_SYSADMIN_OVERRIDE, /* Special: override by CAP_SYS_ADMIN */
87*4882a593Smuzhiyun KEY_AUTHTOKEN_OVERRIDE, /* Special: override by possession of auth token */
88*4882a593Smuzhiyun KEY_DEFER_PERM_CHECK, /* Special: permission check is deferred */
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun struct seq_file;
92*4882a593Smuzhiyun struct user_struct;
93*4882a593Smuzhiyun struct signal_struct;
94*4882a593Smuzhiyun struct cred;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun struct key_type;
97*4882a593Smuzhiyun struct key_owner;
98*4882a593Smuzhiyun struct key_tag;
99*4882a593Smuzhiyun struct keyring_list;
100*4882a593Smuzhiyun struct keyring_name;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun struct key_tag {
103*4882a593Smuzhiyun struct rcu_head rcu;
104*4882a593Smuzhiyun refcount_t usage;
105*4882a593Smuzhiyun bool removed; /* T when subject removed */
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun struct keyring_index_key {
109*4882a593Smuzhiyun /* [!] If this structure is altered, the union in struct key must change too! */
110*4882a593Smuzhiyun unsigned long hash; /* Hash value */
111*4882a593Smuzhiyun union {
112*4882a593Smuzhiyun struct {
113*4882a593Smuzhiyun #ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
114*4882a593Smuzhiyun u16 desc_len;
115*4882a593Smuzhiyun char desc[sizeof(long) - 2]; /* First few chars of description */
116*4882a593Smuzhiyun #else
117*4882a593Smuzhiyun char desc[sizeof(long) - 2]; /* First few chars of description */
118*4882a593Smuzhiyun u16 desc_len;
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun unsigned long x;
122*4882a593Smuzhiyun };
123*4882a593Smuzhiyun struct key_type *type;
124*4882a593Smuzhiyun struct key_tag *domain_tag; /* Domain of operation */
125*4882a593Smuzhiyun const char *description;
126*4882a593Smuzhiyun };
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun union key_payload {
129*4882a593Smuzhiyun void __rcu *rcu_data0;
130*4882a593Smuzhiyun void *data[4];
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /*****************************************************************************/
134*4882a593Smuzhiyun /*
135*4882a593Smuzhiyun * key reference with possession attribute handling
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
138*4882a593Smuzhiyun * defined. This is because we abuse the bottom bit of the reference to carry a
139*4882a593Smuzhiyun * flag to indicate whether the calling process possesses that key in one of
140*4882a593Smuzhiyun * its keyrings.
141*4882a593Smuzhiyun *
142*4882a593Smuzhiyun * the key_ref_t has been made a separate type so that the compiler can reject
143*4882a593Smuzhiyun * attempts to dereference it without proper conversion.
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * the three functions are used to assemble and disassemble references
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun typedef struct __key_reference_with_attributes *key_ref_t;
148*4882a593Smuzhiyun
make_key_ref(const struct key * key,bool possession)149*4882a593Smuzhiyun static inline key_ref_t make_key_ref(const struct key *key,
150*4882a593Smuzhiyun bool possession)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun return (key_ref_t) ((unsigned long) key | possession);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
key_ref_to_ptr(const key_ref_t key_ref)155*4882a593Smuzhiyun static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun return (struct key *) ((unsigned long) key_ref & ~1UL);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
is_key_possessed(const key_ref_t key_ref)160*4882a593Smuzhiyun static inline bool is_key_possessed(const key_ref_t key_ref)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun return (unsigned long) key_ref & 1UL;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
166*4882a593Smuzhiyun const struct key_type *type,
167*4882a593Smuzhiyun const union key_payload *payload,
168*4882a593Smuzhiyun struct key *restriction_key);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun struct key_restriction {
171*4882a593Smuzhiyun key_restrict_link_func_t check;
172*4882a593Smuzhiyun struct key *key;
173*4882a593Smuzhiyun struct key_type *keytype;
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun enum key_state {
177*4882a593Smuzhiyun KEY_IS_UNINSTANTIATED,
178*4882a593Smuzhiyun KEY_IS_POSITIVE, /* Positively instantiated */
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /*****************************************************************************/
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * authentication token / access credential / keyring
184*4882a593Smuzhiyun * - types of key include:
185*4882a593Smuzhiyun * - keyrings
186*4882a593Smuzhiyun * - disk encryption IDs
187*4882a593Smuzhiyun * - Kerberos TGTs and tickets
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun struct key {
190*4882a593Smuzhiyun refcount_t usage; /* number of references */
191*4882a593Smuzhiyun key_serial_t serial; /* key serial number */
192*4882a593Smuzhiyun union {
193*4882a593Smuzhiyun struct list_head graveyard_link;
194*4882a593Smuzhiyun struct rb_node serial_node;
195*4882a593Smuzhiyun };
196*4882a593Smuzhiyun #ifdef CONFIG_KEY_NOTIFICATIONS
197*4882a593Smuzhiyun struct watch_list *watchers; /* Entities watching this key for changes */
198*4882a593Smuzhiyun #endif
199*4882a593Smuzhiyun struct rw_semaphore sem; /* change vs change sem */
200*4882a593Smuzhiyun struct key_user *user; /* owner of this key */
201*4882a593Smuzhiyun void *security; /* security data for this key */
202*4882a593Smuzhiyun union {
203*4882a593Smuzhiyun time64_t expiry; /* time at which key expires (or 0) */
204*4882a593Smuzhiyun time64_t revoked_at; /* time at which key was revoked */
205*4882a593Smuzhiyun };
206*4882a593Smuzhiyun time64_t last_used_at; /* last time used for LRU keyring discard */
207*4882a593Smuzhiyun kuid_t uid;
208*4882a593Smuzhiyun kgid_t gid;
209*4882a593Smuzhiyun key_perm_t perm; /* access permissions */
210*4882a593Smuzhiyun unsigned short quotalen; /* length added to quota */
211*4882a593Smuzhiyun unsigned short datalen; /* payload data length
212*4882a593Smuzhiyun * - may not match RCU dereferenced payload
213*4882a593Smuzhiyun * - payload should contain own length
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun short state; /* Key state (+) or rejection error (-) */
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun #ifdef KEY_DEBUGGING
218*4882a593Smuzhiyun unsigned magic;
219*4882a593Smuzhiyun #define KEY_DEBUG_MAGIC 0x18273645u
220*4882a593Smuzhiyun #endif
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun unsigned long flags; /* status flags (change with bitops) */
223*4882a593Smuzhiyun #define KEY_FLAG_DEAD 0 /* set if key type has been deleted */
224*4882a593Smuzhiyun #define KEY_FLAG_REVOKED 1 /* set if key had been revoked */
225*4882a593Smuzhiyun #define KEY_FLAG_IN_QUOTA 2 /* set if key consumes quota */
226*4882a593Smuzhiyun #define KEY_FLAG_USER_CONSTRUCT 3 /* set if key is being constructed in userspace */
227*4882a593Smuzhiyun #define KEY_FLAG_ROOT_CAN_CLEAR 4 /* set if key can be cleared by root without permission */
228*4882a593Smuzhiyun #define KEY_FLAG_INVALIDATED 5 /* set if key has been invalidated */
229*4882a593Smuzhiyun #define KEY_FLAG_BUILTIN 6 /* set if key is built in to the kernel */
230*4882a593Smuzhiyun #define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */
231*4882a593Smuzhiyun #define KEY_FLAG_KEEP 8 /* set if key should not be removed */
232*4882a593Smuzhiyun #define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* the key type and key description string
235*4882a593Smuzhiyun * - the desc is used to match a key against search criteria
236*4882a593Smuzhiyun * - it should be a printable string
237*4882a593Smuzhiyun * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun union {
240*4882a593Smuzhiyun struct keyring_index_key index_key;
241*4882a593Smuzhiyun struct {
242*4882a593Smuzhiyun unsigned long hash;
243*4882a593Smuzhiyun unsigned long len_desc;
244*4882a593Smuzhiyun struct key_type *type; /* type of key */
245*4882a593Smuzhiyun struct key_tag *domain_tag; /* Domain of operation */
246*4882a593Smuzhiyun char *description;
247*4882a593Smuzhiyun };
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /* key data
251*4882a593Smuzhiyun * - this is used to hold the data actually used in cryptography or
252*4882a593Smuzhiyun * whatever
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun union {
255*4882a593Smuzhiyun union key_payload payload;
256*4882a593Smuzhiyun struct {
257*4882a593Smuzhiyun /* Keyring bits */
258*4882a593Smuzhiyun struct list_head name_link;
259*4882a593Smuzhiyun struct assoc_array keys;
260*4882a593Smuzhiyun };
261*4882a593Smuzhiyun };
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* This is set on a keyring to restrict the addition of a link to a key
264*4882a593Smuzhiyun * to it. If this structure isn't provided then it is assumed that the
265*4882a593Smuzhiyun * keyring is open to any addition. It is ignored for non-keyring
266*4882a593Smuzhiyun * keys. Only set this value using keyring_restrict(), keyring_alloc(),
267*4882a593Smuzhiyun * or key_alloc().
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * This is intended for use with rings of trusted keys whereby addition
270*4882a593Smuzhiyun * to the keyring needs to be controlled. KEY_ALLOC_BYPASS_RESTRICTION
271*4882a593Smuzhiyun * overrides this, allowing the kernel to add extra keys without
272*4882a593Smuzhiyun * restriction.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun struct key_restriction *restrict_link;
275*4882a593Smuzhiyun };
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun extern struct key *key_alloc(struct key_type *type,
278*4882a593Smuzhiyun const char *desc,
279*4882a593Smuzhiyun kuid_t uid, kgid_t gid,
280*4882a593Smuzhiyun const struct cred *cred,
281*4882a593Smuzhiyun key_perm_t perm,
282*4882a593Smuzhiyun unsigned long flags,
283*4882a593Smuzhiyun struct key_restriction *restrict_link);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
287*4882a593Smuzhiyun #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
288*4882a593Smuzhiyun #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
289*4882a593Smuzhiyun #define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */
290*4882a593Smuzhiyun #define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */
291*4882a593Smuzhiyun #define KEY_ALLOC_UID_KEYRING 0x0010 /* allocating a user or user session keyring */
292*4882a593Smuzhiyun #define KEY_ALLOC_SET_KEEP 0x0020 /* Set the KEEP flag on the key/keyring */
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun extern void key_revoke(struct key *key);
295*4882a593Smuzhiyun extern void key_invalidate(struct key *key);
296*4882a593Smuzhiyun extern void key_put(struct key *key);
297*4882a593Smuzhiyun extern bool key_put_tag(struct key_tag *tag);
298*4882a593Smuzhiyun extern void key_remove_domain(struct key_tag *domain_tag);
299*4882a593Smuzhiyun
__key_get(struct key * key)300*4882a593Smuzhiyun static inline struct key *__key_get(struct key *key)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun refcount_inc(&key->usage);
303*4882a593Smuzhiyun return key;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
key_get(struct key * key)306*4882a593Smuzhiyun static inline struct key *key_get(struct key *key)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun return key ? __key_get(key) : key;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
key_ref_put(key_ref_t key_ref)311*4882a593Smuzhiyun static inline void key_ref_put(key_ref_t key_ref)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun key_put(key_ref_to_ptr(key_ref));
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun extern struct key *request_key_tag(struct key_type *type,
317*4882a593Smuzhiyun const char *description,
318*4882a593Smuzhiyun struct key_tag *domain_tag,
319*4882a593Smuzhiyun const char *callout_info);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun extern struct key *request_key_rcu(struct key_type *type,
322*4882a593Smuzhiyun const char *description,
323*4882a593Smuzhiyun struct key_tag *domain_tag);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun extern struct key *request_key_with_auxdata(struct key_type *type,
326*4882a593Smuzhiyun const char *description,
327*4882a593Smuzhiyun struct key_tag *domain_tag,
328*4882a593Smuzhiyun const void *callout_info,
329*4882a593Smuzhiyun size_t callout_len,
330*4882a593Smuzhiyun void *aux);
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun /**
333*4882a593Smuzhiyun * request_key - Request a key and wait for construction
334*4882a593Smuzhiyun * @type: Type of key.
335*4882a593Smuzhiyun * @description: The searchable description of the key.
336*4882a593Smuzhiyun * @callout_info: The data to pass to the instantiation upcall (or NULL).
337*4882a593Smuzhiyun *
338*4882a593Smuzhiyun * As for request_key_tag(), but with the default global domain tag.
339*4882a593Smuzhiyun */
request_key(struct key_type * type,const char * description,const char * callout_info)340*4882a593Smuzhiyun static inline struct key *request_key(struct key_type *type,
341*4882a593Smuzhiyun const char *description,
342*4882a593Smuzhiyun const char *callout_info)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun return request_key_tag(type, description, NULL, callout_info);
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun #ifdef CONFIG_NET
348*4882a593Smuzhiyun /**
349*4882a593Smuzhiyun * request_key_net - Request a key for a net namespace and wait for construction
350*4882a593Smuzhiyun * @type: Type of key.
351*4882a593Smuzhiyun * @description: The searchable description of the key.
352*4882a593Smuzhiyun * @net: The network namespace that is the key's domain of operation.
353*4882a593Smuzhiyun * @callout_info: The data to pass to the instantiation upcall (or NULL).
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * As for request_key() except that it does not add the returned key to a
356*4882a593Smuzhiyun * keyring if found, new keys are always allocated in the user's quota, the
357*4882a593Smuzhiyun * callout_info must be a NUL-terminated string and no auxiliary data can be
358*4882a593Smuzhiyun * passed. Only keys that operate the specified network namespace are used.
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * Furthermore, it then works as wait_for_key_construction() to wait for the
361*4882a593Smuzhiyun * completion of keys undergoing construction with a non-interruptible wait.
362*4882a593Smuzhiyun */
363*4882a593Smuzhiyun #define request_key_net(type, description, net, callout_info) \
364*4882a593Smuzhiyun request_key_tag(type, description, net->key_domain, callout_info);
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /**
367*4882a593Smuzhiyun * request_key_net_rcu - Request a key for a net namespace under RCU conditions
368*4882a593Smuzhiyun * @type: Type of key.
369*4882a593Smuzhiyun * @description: The searchable description of the key.
370*4882a593Smuzhiyun * @net: The network namespace that is the key's domain of operation.
371*4882a593Smuzhiyun *
372*4882a593Smuzhiyun * As for request_key_rcu() except that only keys that operate the specified
373*4882a593Smuzhiyun * network namespace are used.
374*4882a593Smuzhiyun */
375*4882a593Smuzhiyun #define request_key_net_rcu(type, description, net) \
376*4882a593Smuzhiyun request_key_rcu(type, description, net->key_domain);
377*4882a593Smuzhiyun #endif /* CONFIG_NET */
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun extern int wait_for_key_construction(struct key *key, bool intr);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun extern int key_validate(const struct key *key);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun extern key_ref_t key_create_or_update(key_ref_t keyring,
384*4882a593Smuzhiyun const char *type,
385*4882a593Smuzhiyun const char *description,
386*4882a593Smuzhiyun const void *payload,
387*4882a593Smuzhiyun size_t plen,
388*4882a593Smuzhiyun key_perm_t perm,
389*4882a593Smuzhiyun unsigned long flags);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun extern int key_update(key_ref_t key,
392*4882a593Smuzhiyun const void *payload,
393*4882a593Smuzhiyun size_t plen);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun extern int key_link(struct key *keyring,
396*4882a593Smuzhiyun struct key *key);
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun extern int key_move(struct key *key,
399*4882a593Smuzhiyun struct key *from_keyring,
400*4882a593Smuzhiyun struct key *to_keyring,
401*4882a593Smuzhiyun unsigned int flags);
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun extern int key_unlink(struct key *keyring,
404*4882a593Smuzhiyun struct key *key);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
407*4882a593Smuzhiyun const struct cred *cred,
408*4882a593Smuzhiyun key_perm_t perm,
409*4882a593Smuzhiyun unsigned long flags,
410*4882a593Smuzhiyun struct key_restriction *restrict_link,
411*4882a593Smuzhiyun struct key *dest);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun extern int restrict_link_reject(struct key *keyring,
414*4882a593Smuzhiyun const struct key_type *type,
415*4882a593Smuzhiyun const union key_payload *payload,
416*4882a593Smuzhiyun struct key *restriction_key);
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun extern int keyring_clear(struct key *keyring);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun extern key_ref_t keyring_search(key_ref_t keyring,
421*4882a593Smuzhiyun struct key_type *type,
422*4882a593Smuzhiyun const char *description,
423*4882a593Smuzhiyun bool recurse);
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun extern int keyring_add_key(struct key *keyring,
426*4882a593Smuzhiyun struct key *key);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun extern int keyring_restrict(key_ref_t keyring, const char *type,
429*4882a593Smuzhiyun const char *restriction);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun extern struct key *key_lookup(key_serial_t id);
432*4882a593Smuzhiyun
key_serial(const struct key * key)433*4882a593Smuzhiyun static inline key_serial_t key_serial(const struct key *key)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun return key ? key->serial : 0;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun extern void key_set_timeout(struct key *, unsigned);
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
441*4882a593Smuzhiyun enum key_need_perm need_perm);
442*4882a593Smuzhiyun extern void key_free_user_ns(struct user_namespace *);
443*4882a593Smuzhiyun
key_read_state(const struct key * key)444*4882a593Smuzhiyun static inline short key_read_state(const struct key *key)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun /* Barrier versus mark_key_instantiated(). */
447*4882a593Smuzhiyun return smp_load_acquire(&key->state);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /**
451*4882a593Smuzhiyun * key_is_positive - Determine if a key has been positively instantiated
452*4882a593Smuzhiyun * @key: The key to check.
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * Return true if the specified key has been positively instantiated, false
455*4882a593Smuzhiyun * otherwise.
456*4882a593Smuzhiyun */
key_is_positive(const struct key * key)457*4882a593Smuzhiyun static inline bool key_is_positive(const struct key *key)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun return key_read_state(key) == KEY_IS_POSITIVE;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
key_is_negative(const struct key * key)462*4882a593Smuzhiyun static inline bool key_is_negative(const struct key *key)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun return key_read_state(key) < 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun #define dereference_key_rcu(KEY) \
468*4882a593Smuzhiyun (rcu_dereference((KEY)->payload.rcu_data0))
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun #define dereference_key_locked(KEY) \
471*4882a593Smuzhiyun (rcu_dereference_protected((KEY)->payload.rcu_data0, \
472*4882a593Smuzhiyun rwsem_is_locked(&((struct key *)(KEY))->sem)))
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun #define rcu_assign_keypointer(KEY, PAYLOAD) \
475*4882a593Smuzhiyun do { \
476*4882a593Smuzhiyun rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
477*4882a593Smuzhiyun } while (0)
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun #ifdef CONFIG_SYSCTL
480*4882a593Smuzhiyun extern struct ctl_table key_sysctls[];
481*4882a593Smuzhiyun #endif
482*4882a593Smuzhiyun /*
483*4882a593Smuzhiyun * the userspace interface
484*4882a593Smuzhiyun */
485*4882a593Smuzhiyun extern int install_thread_keyring_to_cred(struct cred *cred);
486*4882a593Smuzhiyun extern void key_fsuid_changed(struct cred *new_cred);
487*4882a593Smuzhiyun extern void key_fsgid_changed(struct cred *new_cred);
488*4882a593Smuzhiyun extern void key_init(void);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun #else /* CONFIG_KEYS */
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun #define key_validate(k) 0
493*4882a593Smuzhiyun #define key_serial(k) 0
494*4882a593Smuzhiyun #define key_get(k) ({ NULL; })
495*4882a593Smuzhiyun #define key_revoke(k) do { } while(0)
496*4882a593Smuzhiyun #define key_invalidate(k) do { } while(0)
497*4882a593Smuzhiyun #define key_put(k) do { } while(0)
498*4882a593Smuzhiyun #define key_ref_put(k) do { } while(0)
499*4882a593Smuzhiyun #define make_key_ref(k, p) NULL
500*4882a593Smuzhiyun #define key_ref_to_ptr(k) NULL
501*4882a593Smuzhiyun #define is_key_possessed(k) 0
502*4882a593Smuzhiyun #define key_fsuid_changed(c) do { } while(0)
503*4882a593Smuzhiyun #define key_fsgid_changed(c) do { } while(0)
504*4882a593Smuzhiyun #define key_init() do { } while(0)
505*4882a593Smuzhiyun #define key_free_user_ns(ns) do { } while(0)
506*4882a593Smuzhiyun #define key_remove_domain(d) do { } while(0)
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun #endif /* CONFIG_KEYS */
509*4882a593Smuzhiyun #endif /* __KERNEL__ */
510*4882a593Smuzhiyun #endif /* _LINUX_KEY_H */
511