blob: 50028338a4cc081dc0f9dfcda8394804962dc1de [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Howells76181c12007-10-16 23:29:46 -07002/* Authentication token and access key management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells76181c12007-10-16 23:29:46 -07004 * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells ([email protected])
6 *
Kees Cookb68101a2017-05-13 04:51:50 -07007 * See Documentation/security/keys/core.rst for information on keys/keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#ifndef _LINUX_KEY_H
11#define _LINUX_KEY_H
12
13#include <linux/types.h>
14#include <linux/list.h>
15#include <linux/rbtree.h>
David Howells76d8aea2005-06-23 22:00:49 -070016#include <linux/rcupdate.h>
David Howells0b77f5b2008-04-29 01:01:32 -070017#include <linux/sysctl.h>
Pekka Enbergaa844422009-03-24 10:54:46 +020018#include <linux/rwsem.h>
Arun Sharma600634972011-07-26 16:09:06 -070019#include <linux/atomic.h>
David Howellsb2a4df22013-09-24 10:35:18 +010020#include <linux/assoc_array.h>
Elena Reshetovafff29292017-03-31 15:20:48 +030021#include <linux/refcount.h>
Baolin Wang074d5892017-11-15 16:38:45 +000022#include <linux/time64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#ifdef __KERNEL__
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080025#include <linux/uidgid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/* key handle serial number */
28typedef int32_t key_serial_t;
29
Linus Torvalds028db3e2019-07-10 18:43:43 -070030/* key handle permissions mask */
31typedef uint32_t key_perm_t;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct key;
David Howellsa58946c2019-06-26 21:02:33 +010034struct net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#ifdef CONFIG_KEYS
37
38#undef KEY_DEBUGGING
39
Linus Torvalds028db3e2019-07-10 18:43:43 -070040#define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
41#define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
42#define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
43#define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
44#define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
45#define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
46#define KEY_POS_ALL 0x3f000000
47
48#define KEY_USR_VIEW 0x00010000 /* user permissions... */
49#define KEY_USR_READ 0x00020000
50#define KEY_USR_WRITE 0x00040000
51#define KEY_USR_SEARCH 0x00080000
52#define KEY_USR_LINK 0x00100000
53#define KEY_USR_SETATTR 0x00200000
54#define KEY_USR_ALL 0x003f0000
55
56#define KEY_GRP_VIEW 0x00000100 /* group permissions... */
57#define KEY_GRP_READ 0x00000200
58#define KEY_GRP_WRITE 0x00000400
59#define KEY_GRP_SEARCH 0x00000800
60#define KEY_GRP_LINK 0x00001000
61#define KEY_GRP_SETATTR 0x00002000
62#define KEY_GRP_ALL 0x00003f00
63
64#define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
65#define KEY_OTH_READ 0x00000002
66#define KEY_OTH_WRITE 0x00000004
67#define KEY_OTH_SEARCH 0x00000008
68#define KEY_OTH_LINK 0x00000010
69#define KEY_OTH_SETATTR 0x00000020
70#define KEY_OTH_ALL 0x0000003f
71
72#define KEY_PERM_UNDEF 0xffffffff
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct seq_file;
75struct user_struct;
76struct signal_struct;
David Howellsd84f4f92008-11-14 10:39:23 +110077struct cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79struct key_type;
80struct key_owner;
David Howells3b6e4de2019-06-26 21:02:32 +010081struct key_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082struct keyring_list;
83struct keyring_name;
84
David Howells3b6e4de2019-06-26 21:02:32 +010085struct key_tag {
86 struct rcu_head rcu;
87 refcount_t usage;
88 bool removed; /* T when subject removed */
89};
90
David Howells16feef42013-09-24 10:35:15 +010091struct keyring_index_key {
David Howells355ef8e2019-06-26 21:02:32 +010092 /* [!] If this structure is altered, the union in struct key must change too! */
93 unsigned long hash; /* Hash value */
David Howellsf771fde2019-06-26 21:02:31 +010094 union {
95 struct {
96#ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
David Howells555df332019-08-19 16:02:01 +010097 u16 desc_len;
98 char desc[sizeof(long) - 2]; /* First few chars of description */
David Howellsf771fde2019-06-26 21:02:31 +010099#else
David Howells555df332019-08-19 16:02:01 +0100100 char desc[sizeof(long) - 2]; /* First few chars of description */
101 u16 desc_len;
David Howellsf771fde2019-06-26 21:02:31 +0100102#endif
103 };
104 unsigned long x;
105 };
David Howells16feef42013-09-24 10:35:15 +0100106 struct key_type *type;
David Howells3b6e4de2019-06-26 21:02:32 +0100107 struct key_tag *domain_tag; /* Domain of operation */
David Howells16feef42013-09-24 10:35:15 +0100108 const char *description;
David Howells16feef42013-09-24 10:35:15 +0100109};
110
David Howells146aa8b2015-10-21 14:04:48 +0100111union key_payload {
112 void __rcu *rcu_data0;
113 void *data[4];
114};
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*****************************************************************************/
117/*
David Howells664cceb2005-09-28 17:03:15 +0100118 * key reference with possession attribute handling
119 *
120 * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
121 * defined. This is because we abuse the bottom bit of the reference to carry a
122 * flag to indicate whether the calling process possesses that key in one of
123 * its keyrings.
124 *
125 * the key_ref_t has been made a separate type so that the compiler can reject
126 * attempts to dereference it without proper conversion.
127 *
128 * the three functions are used to assemble and disassemble references
129 */
130typedef struct __key_reference_with_attributes *key_ref_t;
131
132static inline key_ref_t make_key_ref(const struct key *key,
David Howellsa5b4bd22013-09-24 10:35:14 +0100133 bool possession)
David Howells664cceb2005-09-28 17:03:15 +0100134{
135 return (key_ref_t) ((unsigned long) key | possession);
136}
137
138static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
139{
140 return (struct key *) ((unsigned long) key_ref & ~1UL);
141}
142
David Howellsa5b4bd22013-09-24 10:35:14 +0100143static inline bool is_key_possessed(const key_ref_t key_ref)
David Howells664cceb2005-09-28 17:03:15 +0100144{
145 return (unsigned long) key_ref & 1UL;
146}
147
Mat Martineauaaf66c82016-08-30 11:33:13 -0700148typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
Mat Martineau469ff8f2016-04-25 11:30:39 -0700149 const struct key_type *type,
Mat Martineauaaf66c82016-08-30 11:33:13 -0700150 const union key_payload *payload,
151 struct key *restriction_key);
Mat Martineau469ff8f2016-04-25 11:30:39 -0700152
Mat Martineaue9cc0f62016-06-27 16:10:59 -0700153struct key_restriction {
154 key_restrict_link_func_t check;
155 struct key *key;
156 struct key_type *keytype;
157};
158
David Howells363b02d2017-10-04 16:43:25 +0100159enum key_state {
160 KEY_IS_UNINSTANTIATED,
161 KEY_IS_POSITIVE, /* Positively instantiated */
162};
163
David Howells664cceb2005-09-28 17:03:15 +0100164/*****************************************************************************/
165/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * authentication token / access credential / keyring
167 * - types of key include:
168 * - keyrings
169 * - disk encryption IDs
170 * - Kerberos TGTs and tickets
171 */
172struct key {
Elena Reshetovafff29292017-03-31 15:20:48 +0300173 refcount_t usage; /* number of references */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 key_serial_t serial; /* key serial number */
David Howells65d87fe2012-05-11 10:56:56 +0100175 union {
176 struct list_head graveyard_link;
177 struct rb_node serial_node;
178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct rw_semaphore sem; /* change vs change sem */
180 struct key_user *user; /* owner of this key */
David Howells29db9192005-10-30 15:02:44 -0800181 void *security; /* security data for this key */
David Howells5d135442009-09-02 09:14:00 +0100182 union {
Baolin Wang074d5892017-11-15 16:38:45 +0000183 time64_t expiry; /* time at which key expires (or 0) */
184 time64_t revoked_at; /* time at which key was revoked */
David Howells5d135442009-09-02 09:14:00 +0100185 };
Baolin Wang074d5892017-11-15 16:38:45 +0000186 time64_t last_used_at; /* last time used for LRU keyring discard */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800187 kuid_t uid;
188 kgid_t gid;
Linus Torvalds028db3e2019-07-10 18:43:43 -0700189 key_perm_t perm; /* access permissions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned short quotalen; /* length added to quota */
David Howells76d8aea2005-06-23 22:00:49 -0700191 unsigned short datalen; /* payload data length
192 * - may not match RCU dereferenced payload
193 * - payload should contain own length
194 */
David Howells363b02d2017-10-04 16:43:25 +0100195 short state; /* Key state (+) or rejection error (-) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#ifdef KEY_DEBUGGING
198 unsigned magic;
199#define KEY_DEBUG_MAGIC 0x18273645u
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif
201
David Howells76d8aea2005-06-23 22:00:49 -0700202 unsigned long flags; /* status flags (change with bitops) */
David Howells363b02d2017-10-04 16:43:25 +0100203#define KEY_FLAG_DEAD 0 /* set if key type has been deleted */
204#define KEY_FLAG_REVOKED 1 /* set if key had been revoked */
205#define KEY_FLAG_IN_QUOTA 2 /* set if key consumes quota */
206#define KEY_FLAG_USER_CONSTRUCT 3 /* set if key is being constructed in userspace */
207#define KEY_FLAG_ROOT_CAN_CLEAR 4 /* set if key can be cleared by root without permission */
208#define KEY_FLAG_INVALIDATED 5 /* set if key has been invalidated */
209#define KEY_FLAG_BUILTIN 6 /* set if key is built in to the kernel */
210#define KEY_FLAG_ROOT_CAN_INVAL 7 /* set if key can be invalidated by root without permission */
211#define KEY_FLAG_KEEP 8 /* set if key should not be removed */
212#define KEY_FLAG_UID_KEYRING 9 /* set if key is a user or user session keyring */
David Howells76d8aea2005-06-23 22:00:49 -0700213
David Howells16feef42013-09-24 10:35:15 +0100214 /* the key type and key description string
215 * - the desc is used to match a key against search criteria
216 * - it should be a printable string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * - eg: for krb5 AFS, this might be "[email protected]"
218 */
David Howells16feef42013-09-24 10:35:15 +0100219 union {
220 struct keyring_index_key index_key;
221 struct {
David Howells355ef8e2019-06-26 21:02:32 +0100222 unsigned long hash;
David Howellsf771fde2019-06-26 21:02:31 +0100223 unsigned long len_desc;
David Howells16feef42013-09-24 10:35:15 +0100224 struct key_type *type; /* type of key */
David Howells3b6e4de2019-06-26 21:02:32 +0100225 struct key_tag *domain_tag; /* Domain of operation */
David Howells16feef42013-09-24 10:35:15 +0100226 char *description;
227 };
228 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* key data
231 * - this is used to hold the data actually used in cryptography or
232 * whatever
233 */
234 union {
David Howells146aa8b2015-10-21 14:04:48 +0100235 union key_payload payload;
236 struct {
237 /* Keyring bits */
238 struct list_head name_link;
239 struct assoc_array keys;
240 };
David Howellsb2a4df22013-09-24 10:35:18 +0100241 };
David Howells5ac7eac2016-04-06 16:14:24 +0100242
243 /* This is set on a keyring to restrict the addition of a link to a key
Mat Martineau2b6aa412016-08-31 16:05:43 -0700244 * to it. If this structure isn't provided then it is assumed that the
David Howells5ac7eac2016-04-06 16:14:24 +0100245 * keyring is open to any addition. It is ignored for non-keyring
Mat Martineau6563c912017-03-01 16:44:09 -0800246 * keys. Only set this value using keyring_restrict(), keyring_alloc(),
247 * or key_alloc().
David Howells5ac7eac2016-04-06 16:14:24 +0100248 *
249 * This is intended for use with rings of trusted keys whereby addition
250 * to the keyring needs to be controlled. KEY_ALLOC_BYPASS_RESTRICTION
251 * overrides this, allowing the kernel to add extra keys without
252 * restriction.
253 */
Mat Martineau2b6aa412016-08-31 16:05:43 -0700254 struct key_restriction *restrict_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257extern struct key *key_alloc(struct key_type *type,
258 const char *desc,
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800259 kuid_t uid, kgid_t gid,
David Howellsd84f4f92008-11-14 10:39:23 +1100260 const struct cred *cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700261 key_perm_t perm,
David Howells5ac7eac2016-04-06 16:14:24 +0100262 unsigned long flags,
Mat Martineau2b6aa412016-08-31 16:05:43 -0700263 struct key_restriction *restrict_link);
David Howells7e047ef2006-06-26 00:24:50 -0700264
265
David Howells5ac7eac2016-04-06 16:14:24 +0100266#define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
267#define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
268#define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
David Howells77f68bac2016-04-06 16:14:26 +0100269#define KEY_ALLOC_BUILT_IN 0x0004 /* Key is built into kernel */
270#define KEY_ALLOC_BYPASS_RESTRICTION 0x0008 /* Override the check on restricted keyrings */
Eric Biggers237bbd22017-09-18 11:37:03 -0700271#define KEY_ALLOC_UID_KEYRING 0x0010 /* allocating a user or user session keyring */
David Howells7e047ef2006-06-26 00:24:50 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273extern void key_revoke(struct key *key);
David Howellsfd758152012-05-11 10:56:56 +0100274extern void key_invalidate(struct key *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275extern void key_put(struct key *key);
David Howells3b6e4de2019-06-26 21:02:32 +0100276extern bool key_put_tag(struct key_tag *tag);
David Howells218e6422019-06-26 21:02:32 +0100277extern void key_remove_domain(struct key_tag *domain_tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David Howellsccc3e6d2013-09-24 10:35:16 +0100279static inline struct key *__key_get(struct key *key)
280{
Elena Reshetovafff29292017-03-31 15:20:48 +0300281 refcount_inc(&key->usage);
David Howellsccc3e6d2013-09-24 10:35:16 +0100282 return key;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static inline struct key *key_get(struct key *key)
286{
David Howellsccc3e6d2013-09-24 10:35:16 +0100287 return key ? __key_get(key) : key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
David Howells664cceb2005-09-28 17:03:15 +0100290static inline void key_ref_put(key_ref_t key_ref)
291{
292 key_put(key_ref_to_ptr(key_ref));
293}
294
David Howellsa58946c2019-06-26 21:02:33 +0100295extern struct key *request_key_tag(struct key_type *type,
296 const char *description,
297 struct key_tag *domain_tag,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700298 const char *callout_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David Howells896f1952019-06-19 16:10:15 +0100300extern struct key *request_key_rcu(struct key_type *type,
David Howellsa58946c2019-06-26 21:02:33 +0100301 const char *description,
302 struct key_tag *domain_tag);
David Howells896f1952019-06-19 16:10:15 +0100303
David Howells4e54f082006-06-29 02:24:28 -0700304extern struct key *request_key_with_auxdata(struct key_type *type,
305 const char *description,
David Howellsa58946c2019-06-26 21:02:33 +0100306 struct key_tag *domain_tag,
David Howells4a38e122008-04-29 01:01:24 -0700307 const void *callout_info,
308 size_t callout_len,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700309 void *aux);
David Howells4e54f082006-06-29 02:24:28 -0700310
David Howellsa58946c2019-06-26 21:02:33 +0100311/**
312 * request_key - Request a key and wait for construction
313 * @type: Type of key.
314 * @description: The searchable description of the key.
315 * @callout_info: The data to pass to the instantiation upcall (or NULL).
316 *
317 * As for request_key_tag(), but with the default global domain tag.
318 */
319static inline struct key *request_key(struct key_type *type,
320 const char *description,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700321 const char *callout_info)
David Howellsa58946c2019-06-26 21:02:33 +0100322{
Linus Torvalds028db3e2019-07-10 18:43:43 -0700323 return request_key_tag(type, description, NULL, callout_info);
David Howellsa58946c2019-06-26 21:02:33 +0100324}
325
326#ifdef CONFIG_NET
327/*
328 * request_key_net - Request a key for a net namespace and wait for construction
329 * @type: Type of key.
330 * @description: The searchable description of the key.
331 * @net: The network namespace that is the key's domain of operation.
332 * @callout_info: The data to pass to the instantiation upcall (or NULL).
333 *
334 * As for request_key() except that it does not add the returned key to a
335 * keyring if found, new keys are always allocated in the user's quota, the
336 * callout_info must be a NUL-terminated string and no auxiliary data can be
337 * passed. Only keys that operate the specified network namespace are used.
338 *
339 * Furthermore, it then works as wait_for_key_construction() to wait for the
340 * completion of keys undergoing construction with a non-interruptible wait.
341 */
Linus Torvalds028db3e2019-07-10 18:43:43 -0700342#define request_key_net(type, description, net, callout_info) \
343 request_key_tag(type, description, net->key_domain, callout_info);
David Howellsa58946c2019-06-26 21:02:33 +0100344#endif /* CONFIG_NET */
345
David Howells76181c12007-10-16 23:29:46 -0700346extern int wait_for_key_construction(struct key *key, bool intr);
347
David Howellsb404aef2012-05-15 14:11:11 +0100348extern int key_validate(const struct key *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Howells664cceb2005-09-28 17:03:15 +0100350extern key_ref_t key_create_or_update(key_ref_t keyring,
351 const char *type,
352 const char *description,
353 const void *payload,
354 size_t plen,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700355 key_perm_t perm,
David Howells7e047ef2006-06-26 00:24:50 -0700356 unsigned long flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howells664cceb2005-09-28 17:03:15 +0100358extern int key_update(key_ref_t key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 const void *payload,
360 size_t plen);
361
362extern int key_link(struct key *keyring,
363 struct key *key);
364
David Howellsed0ac5c2019-05-20 21:51:50 +0100365extern int key_move(struct key *key,
366 struct key *from_keyring,
367 struct key *to_keyring,
368 unsigned int flags);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370extern int key_unlink(struct key *keyring,
371 struct key *key);
372
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800373extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
David Howellsd84f4f92008-11-14 10:39:23 +1100374 const struct cred *cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700375 key_perm_t perm,
David Howells7e047ef2006-06-26 00:24:50 -0700376 unsigned long flags,
Mat Martineau2b6aa412016-08-31 16:05:43 -0700377 struct key_restriction *restrict_link,
Michael LeMayd7200242006-06-22 14:47:17 -0700378 struct key *dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howells5ac7eac2016-04-06 16:14:24 +0100380extern int restrict_link_reject(struct key *keyring,
381 const struct key_type *type,
Mat Martineauaaf66c82016-08-30 11:33:13 -0700382 const union key_payload *payload,
383 struct key *restriction_key);
David Howells5ac7eac2016-04-06 16:14:24 +0100384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385extern int keyring_clear(struct key *keyring);
386
David Howells664cceb2005-09-28 17:03:15 +0100387extern key_ref_t keyring_search(key_ref_t keyring,
388 struct key_type *type,
David Howellsdcf49db2019-06-26 21:02:32 +0100389 const char *description,
390 bool recurse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392extern int keyring_add_key(struct key *keyring,
393 struct key *key);
394
Mat Martineau6563c912017-03-01 16:44:09 -0800395extern int keyring_restrict(key_ref_t keyring, const char *type,
396 const char *restriction);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398extern struct key *key_lookup(key_serial_t id);
399
David Howells456a8162012-01-18 10:04:29 +0000400static inline key_serial_t key_serial(const struct key *key)
David Howells7249db22008-04-29 01:01:34 -0700401{
402 return key ? key->serial : 0;
403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Bryan Schumaker59e6b9c2012-02-24 14:14:50 -0500405extern void key_set_timeout(struct key *, unsigned);
406
Dave Jiang76ef5e12018-12-04 10:31:27 -0800407extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700408 key_perm_t perm);
David Howellsb206f282019-06-26 21:02:32 +0100409extern void key_free_user_ns(struct user_namespace *);
Dave Jiang76ef5e12018-12-04 10:31:27 -0800410
David Howellsf5895942014-03-14 17:44:49 +0000411/*
412 * The permissions required on a key that we're looking up.
413 */
Linus Torvalds028db3e2019-07-10 18:43:43 -0700414#define KEY_NEED_VIEW 0x01 /* Require permission to view attributes */
415#define KEY_NEED_READ 0x02 /* Require permission to read content */
416#define KEY_NEED_WRITE 0x04 /* Require permission to update / modify */
417#define KEY_NEED_SEARCH 0x08 /* Require permission to search (keyring) or find (key) */
418#define KEY_NEED_LINK 0x10 /* Require permission to link */
419#define KEY_NEED_SETATTR 0x20 /* Require permission to change attributes */
420#define KEY_NEED_ALL 0x3f /* All the above permissions */
David Howellsf5895942014-03-14 17:44:49 +0000421
David Howells363b02d2017-10-04 16:43:25 +0100422static inline short key_read_state(const struct key *key)
423{
424 /* Barrier versus mark_key_instantiated(). */
425 return smp_load_acquire(&key->state);
426}
427
David Howells78b72802011-03-11 17:57:23 +0000428/**
David Howells363b02d2017-10-04 16:43:25 +0100429 * key_is_positive - Determine if a key has been positively instantiated
David Howells78b72802011-03-11 17:57:23 +0000430 * @key: The key to check.
431 *
432 * Return true if the specified key has been positively instantiated, false
433 * otherwise.
434 */
David Howells363b02d2017-10-04 16:43:25 +0100435static inline bool key_is_positive(const struct key *key)
David Howells78b72802011-03-11 17:57:23 +0000436{
David Howells363b02d2017-10-04 16:43:25 +0100437 return key_read_state(key) == KEY_IS_POSITIVE;
438}
439
440static inline bool key_is_negative(const struct key *key)
441{
442 return key_read_state(key) < 0;
David Howells78b72802011-03-11 17:57:23 +0000443}
444
David Howells0837e492017-03-01 15:11:23 +0000445#define dereference_key_rcu(KEY) \
446 (rcu_dereference((KEY)->payload.rcu_data0))
447
448#define dereference_key_locked(KEY) \
David Howells146aa8b2015-10-21 14:04:48 +0100449 (rcu_dereference_protected((KEY)->payload.rcu_data0, \
David Howells633e8042011-03-07 15:05:51 +0000450 rwsem_is_locked(&((struct key *)(KEY))->sem)))
451
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000452#define rcu_assign_keypointer(KEY, PAYLOAD) \
Paul E. McKenneye5c1f442012-05-16 16:31:38 -0700453do { \
David Howells146aa8b2015-10-21 14:04:48 +0100454 rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD)); \
Paul E. McKenneye5c1f442012-05-16 16:31:38 -0700455} while (0)
Mimi Zoharee0b31a2012-01-17 20:39:51 +0000456
David Howells0b77f5b2008-04-29 01:01:32 -0700457#ifdef CONFIG_SYSCTL
Joe Perchesd6f50c92014-06-06 14:38:06 -0700458extern struct ctl_table key_sysctls[];
David Howells0b77f5b2008-04-29 01:01:32 -0700459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/*
461 * the userspace interface
462 */
David Howellsd84f4f92008-11-14 10:39:23 +1100463extern int install_thread_keyring_to_cred(struct cred *cred);
David Howells2e218652019-05-22 14:06:51 +0100464extern void key_fsuid_changed(struct cred *new_cred);
465extern void key_fsgid_changed(struct cred *new_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466extern void key_init(void);
467
468#else /* CONFIG_KEYS */
469
470#define key_validate(k) 0
471#define key_serial(k) 0
David Howells7888e7f2005-06-23 22:00:51 -0700472#define key_get(k) ({ NULL; })
Adrian Bunk0dab9cf2008-10-12 07:10:50 +0300473#define key_revoke(k) do { } while(0)
David Howellsfd758152012-05-11 10:56:56 +0100474#define key_invalidate(k) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#define key_put(k) do { } while(0)
David Howells664cceb2005-09-28 17:03:15 +0100476#define key_ref_put(k) do { } while(0)
David Howells8bbf49762008-11-14 10:39:14 +1100477#define make_key_ref(k, p) NULL
478#define key_ref_to_ptr(k) NULL
David Howells664cceb2005-09-28 17:03:15 +0100479#define is_key_possessed(k) 0
David Howells2e218652019-05-22 14:06:51 +0100480#define key_fsuid_changed(c) do { } while(0)
481#define key_fsgid_changed(c) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#define key_init() do { } while(0)
David Howellsb206f282019-06-26 21:02:32 +0100483#define key_free_user_ns(ns) do { } while(0)
David Howells218e6422019-06-26 21:02:32 +0100484#define key_remove_domain(d) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#endif /* CONFIG_KEYS */
487#endif /* __KERNEL__ */
488#endif /* _LINUX_KEY_H */