xref: /linux-6.15/include/linux/key.h (revision 75845c6c)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
276181c13SDavid Howells /* Authentication token and access key management
31da177e4SLinus Torvalds  *
476181c13SDavid Howells  * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds  * Written by David Howells ([email protected])
61da177e4SLinus Torvalds  *
7b68101a1SKees Cook  * See Documentation/security/keys/core.rst for information on keys/keyrings.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #ifndef _LINUX_KEY_H
111da177e4SLinus Torvalds #define _LINUX_KEY_H
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/list.h>
151da177e4SLinus Torvalds #include <linux/rbtree.h>
1676d8aeabSDavid Howells #include <linux/rcupdate.h>
170b77f5bfSDavid Howells #include <linux/sysctl.h>
18aa84442dSPekka Enberg #include <linux/rwsem.h>
1960063497SArun Sharma #include <linux/atomic.h>
20b2a4df20SDavid Howells #include <linux/assoc_array.h>
21fff29291SElena Reshetova #include <linux/refcount.h>
22074d5898SBaolin Wang #include <linux/time64.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #ifdef __KERNEL__
259a56c2dbSEric W. Biederman #include <linux/uidgid.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /* key handle serial number */
281da177e4SLinus Torvalds typedef int32_t key_serial_t;
291da177e4SLinus Torvalds 
30028db3e2SLinus Torvalds /* key handle permissions mask */
31028db3e2SLinus Torvalds typedef uint32_t key_perm_t;
32028db3e2SLinus Torvalds 
331da177e4SLinus Torvalds struct key;
34a58946c1SDavid Howells struct net;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #ifdef CONFIG_KEYS
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #undef KEY_DEBUGGING
391da177e4SLinus Torvalds 
40028db3e2SLinus Torvalds #define KEY_POS_VIEW	0x01000000	/* possessor can view a key's attributes */
41028db3e2SLinus Torvalds #define KEY_POS_READ	0x02000000	/* possessor can read key payload / view keyring */
42028db3e2SLinus Torvalds #define KEY_POS_WRITE	0x04000000	/* possessor can update key payload / add link to keyring */
43028db3e2SLinus Torvalds #define KEY_POS_SEARCH	0x08000000	/* possessor can find a key in search / search a keyring */
44028db3e2SLinus Torvalds #define KEY_POS_LINK	0x10000000	/* possessor can create a link to a key/keyring */
45028db3e2SLinus Torvalds #define KEY_POS_SETATTR	0x20000000	/* possessor can set key attributes */
46028db3e2SLinus Torvalds #define KEY_POS_ALL	0x3f000000
47028db3e2SLinus Torvalds 
48028db3e2SLinus Torvalds #define KEY_USR_VIEW	0x00010000	/* user permissions... */
49028db3e2SLinus Torvalds #define KEY_USR_READ	0x00020000
50028db3e2SLinus Torvalds #define KEY_USR_WRITE	0x00040000
51028db3e2SLinus Torvalds #define KEY_USR_SEARCH	0x00080000
52028db3e2SLinus Torvalds #define KEY_USR_LINK	0x00100000
53028db3e2SLinus Torvalds #define KEY_USR_SETATTR	0x00200000
54028db3e2SLinus Torvalds #define KEY_USR_ALL	0x003f0000
55028db3e2SLinus Torvalds 
56028db3e2SLinus Torvalds #define KEY_GRP_VIEW	0x00000100	/* group permissions... */
57028db3e2SLinus Torvalds #define KEY_GRP_READ	0x00000200
58028db3e2SLinus Torvalds #define KEY_GRP_WRITE	0x00000400
59028db3e2SLinus Torvalds #define KEY_GRP_SEARCH	0x00000800
60028db3e2SLinus Torvalds #define KEY_GRP_LINK	0x00001000
61028db3e2SLinus Torvalds #define KEY_GRP_SETATTR	0x00002000
62028db3e2SLinus Torvalds #define KEY_GRP_ALL	0x00003f00
63028db3e2SLinus Torvalds 
64028db3e2SLinus Torvalds #define KEY_OTH_VIEW	0x00000001	/* third party permissions... */
65028db3e2SLinus Torvalds #define KEY_OTH_READ	0x00000002
66028db3e2SLinus Torvalds #define KEY_OTH_WRITE	0x00000004
67028db3e2SLinus Torvalds #define KEY_OTH_SEARCH	0x00000008
68028db3e2SLinus Torvalds #define KEY_OTH_LINK	0x00000010
69028db3e2SLinus Torvalds #define KEY_OTH_SETATTR	0x00000020
70028db3e2SLinus Torvalds #define KEY_OTH_ALL	0x0000003f
71028db3e2SLinus Torvalds 
72028db3e2SLinus Torvalds #define KEY_PERM_UNDEF	0xffffffff
73028db3e2SLinus Torvalds 
748c0637e9SDavid Howells /*
758c0637e9SDavid Howells  * The permissions required on a key that we're looking up.
768c0637e9SDavid Howells  */
778c0637e9SDavid Howells enum key_need_perm {
788c0637e9SDavid Howells 	KEY_NEED_UNSPECIFIED,	/* Needed permission unspecified */
798c0637e9SDavid Howells 	KEY_NEED_VIEW,		/* Require permission to view attributes */
808c0637e9SDavid Howells 	KEY_NEED_READ,		/* Require permission to read content */
818c0637e9SDavid Howells 	KEY_NEED_WRITE,		/* Require permission to update / modify */
828c0637e9SDavid Howells 	KEY_NEED_SEARCH,	/* Require permission to search (keyring) or find (key) */
838c0637e9SDavid Howells 	KEY_NEED_LINK,		/* Require permission to link */
848c0637e9SDavid Howells 	KEY_NEED_SETATTR,	/* Require permission to change attributes */
858c0637e9SDavid Howells 	KEY_NEED_UNLINK,	/* Require permission to unlink key */
868c0637e9SDavid Howells 	KEY_SYSADMIN_OVERRIDE,	/* Special: override by CAP_SYS_ADMIN */
878c0637e9SDavid Howells 	KEY_AUTHTOKEN_OVERRIDE,	/* Special: override by possession of auth token */
888c0637e9SDavid Howells 	KEY_DEFER_PERM_CHECK,	/* Special: permission check is deferred */
898c0637e9SDavid Howells };
908c0637e9SDavid Howells 
9190fd8f26SRoberto Sassu enum key_lookup_flag {
9290fd8f26SRoberto Sassu 	KEY_LOOKUP_CREATE = 0x01,
9390fd8f26SRoberto Sassu 	KEY_LOOKUP_PARTIAL = 0x02,
9490fd8f26SRoberto Sassu 	KEY_LOOKUP_ALL = (KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL),
9590fd8f26SRoberto Sassu };
9690fd8f26SRoberto Sassu 
971da177e4SLinus Torvalds struct seq_file;
981da177e4SLinus Torvalds struct user_struct;
991da177e4SLinus Torvalds struct signal_struct;
100d84f4f99SDavid Howells struct cred;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds struct key_type;
1031da177e4SLinus Torvalds struct key_owner;
1043b6e4de0SDavid Howells struct key_tag;
1051da177e4SLinus Torvalds struct keyring_list;
1061da177e4SLinus Torvalds struct keyring_name;
1071da177e4SLinus Torvalds 
1083b6e4de0SDavid Howells struct key_tag {
1093b6e4de0SDavid Howells 	struct rcu_head		rcu;
1103b6e4de0SDavid Howells 	refcount_t		usage;
1113b6e4de0SDavid Howells 	bool			removed;	/* T when subject removed */
1123b6e4de0SDavid Howells };
1133b6e4de0SDavid Howells 
11416feef43SDavid Howells struct keyring_index_key {
115355ef8e1SDavid Howells 	/* [!] If this structure is altered, the union in struct key must change too! */
116355ef8e1SDavid Howells 	unsigned long		hash;			/* Hash value */
117f771fde8SDavid Howells 	union {
118f771fde8SDavid Howells 		struct {
119f771fde8SDavid Howells #ifdef __LITTLE_ENDIAN /* Put desc_len at the LSB of x */
120555df336SDavid Howells 			u16	desc_len;
121555df336SDavid Howells 			char	desc[sizeof(long) - 2];	/* First few chars of description */
122f771fde8SDavid Howells #else
123555df336SDavid Howells 			char	desc[sizeof(long) - 2];	/* First few chars of description */
124555df336SDavid Howells 			u16	desc_len;
125f771fde8SDavid Howells #endif
126f771fde8SDavid Howells 		};
127f771fde8SDavid Howells 		unsigned long x;
128f771fde8SDavid Howells 	};
12916feef43SDavid Howells 	struct key_type		*type;
1303b6e4de0SDavid Howells 	struct key_tag		*domain_tag;	/* Domain of operation */
13116feef43SDavid Howells 	const char		*description;
13216feef43SDavid Howells };
13316feef43SDavid Howells 
134146aa8b1SDavid Howells union key_payload {
135146aa8b1SDavid Howells 	void __rcu		*rcu_data0;
136146aa8b1SDavid Howells 	void			*data[4];
137146aa8b1SDavid Howells };
138146aa8b1SDavid Howells 
1391da177e4SLinus Torvalds /*****************************************************************************/
1401da177e4SLinus Torvalds /*
141664cceb0SDavid Howells  * key reference with possession attribute handling
142664cceb0SDavid Howells  *
143664cceb0SDavid Howells  * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
144664cceb0SDavid Howells  * defined. This is because we abuse the bottom bit of the reference to carry a
145664cceb0SDavid Howells  * flag to indicate whether the calling process possesses that key in one of
146664cceb0SDavid Howells  * its keyrings.
147664cceb0SDavid Howells  *
148664cceb0SDavid Howells  * the key_ref_t has been made a separate type so that the compiler can reject
149664cceb0SDavid Howells  * attempts to dereference it without proper conversion.
150664cceb0SDavid Howells  *
151664cceb0SDavid Howells  * the three functions are used to assemble and disassemble references
152664cceb0SDavid Howells  */
153664cceb0SDavid Howells typedef struct __key_reference_with_attributes *key_ref_t;
154664cceb0SDavid Howells 
make_key_ref(const struct key * key,bool possession)155664cceb0SDavid Howells static inline key_ref_t make_key_ref(const struct key *key,
156a5b4bd28SDavid Howells 				     bool possession)
157664cceb0SDavid Howells {
158664cceb0SDavid Howells 	return (key_ref_t) ((unsigned long) key | possession);
159664cceb0SDavid Howells }
160664cceb0SDavid Howells 
key_ref_to_ptr(const key_ref_t key_ref)161664cceb0SDavid Howells static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
162664cceb0SDavid Howells {
163664cceb0SDavid Howells 	return (struct key *) ((unsigned long) key_ref & ~1UL);
164664cceb0SDavid Howells }
165664cceb0SDavid Howells 
is_key_possessed(const key_ref_t key_ref)166a5b4bd28SDavid Howells static inline bool is_key_possessed(const key_ref_t key_ref)
167664cceb0SDavid Howells {
168664cceb0SDavid Howells 	return (unsigned long) key_ref & 1UL;
169664cceb0SDavid Howells }
170664cceb0SDavid Howells 
171aaf66c88SMat Martineau typedef int (*key_restrict_link_func_t)(struct key *dest_keyring,
172469ff8f7SMat Martineau 					const struct key_type *type,
173aaf66c88SMat Martineau 					const union key_payload *payload,
174aaf66c88SMat Martineau 					struct key *restriction_key);
175469ff8f7SMat Martineau 
176e9cc0f68SMat Martineau struct key_restriction {
177e9cc0f68SMat Martineau 	key_restrict_link_func_t check;
178e9cc0f68SMat Martineau 	struct key *key;
179e9cc0f68SMat Martineau 	struct key_type *keytype;
180e9cc0f68SMat Martineau };
181e9cc0f68SMat Martineau 
182363b02daSDavid Howells enum key_state {
183363b02daSDavid Howells 	KEY_IS_UNINSTANTIATED,
184363b02daSDavid Howells 	KEY_IS_POSITIVE,		/* Positively instantiated */
185363b02daSDavid Howells };
186363b02daSDavid Howells 
187664cceb0SDavid Howells /*****************************************************************************/
188664cceb0SDavid Howells /*
1891da177e4SLinus Torvalds  * authentication token / access credential / keyring
1901da177e4SLinus Torvalds  * - types of key include:
1911da177e4SLinus Torvalds  *   - keyrings
1921da177e4SLinus Torvalds  *   - disk encryption IDs
1931da177e4SLinus Torvalds  *   - Kerberos TGTs and tickets
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds struct key {
196fff29291SElena Reshetova 	refcount_t		usage;		/* number of references */
1971da177e4SLinus Torvalds 	key_serial_t		serial;		/* key serial number */
19865d87fe6SDavid Howells 	union {
19965d87fe6SDavid Howells 		struct list_head graveyard_link;
2001da177e4SLinus Torvalds 		struct rb_node	serial_node;
20165d87fe6SDavid Howells 	};
202f7e47677SDavid Howells #ifdef CONFIG_KEY_NOTIFICATIONS
203f7e47677SDavid Howells 	struct watch_list	*watchers;	/* Entities watching this key for changes */
204f7e47677SDavid Howells #endif
2051da177e4SLinus Torvalds 	struct rw_semaphore	sem;		/* change vs change sem */
2061da177e4SLinus Torvalds 	struct key_user		*user;		/* owner of this key */
20729db9190SDavid Howells 	void			*security;	/* security data for this key */
2085d135440SDavid Howells 	union {
209074d5898SBaolin Wang 		time64_t	expiry;		/* time at which key expires (or 0) */
210074d5898SBaolin Wang 		time64_t	revoked_at;	/* time at which key was revoked */
2115d135440SDavid Howells 	};
212074d5898SBaolin Wang 	time64_t		last_used_at;	/* last time used for LRU keyring discard */
2139a56c2dbSEric W. Biederman 	kuid_t			uid;
2149a56c2dbSEric W. Biederman 	kgid_t			gid;
215028db3e2SLinus Torvalds 	key_perm_t		perm;		/* access permissions */
2161da177e4SLinus Torvalds 	unsigned short		quotalen;	/* length added to quota */
21776d8aeabSDavid Howells 	unsigned short		datalen;	/* payload data length
21876d8aeabSDavid Howells 						 * - may not match RCU dereferenced payload
21976d8aeabSDavid Howells 						 * - payload should contain own length
22076d8aeabSDavid Howells 						 */
221363b02daSDavid Howells 	short			state;		/* Key state (+) or rejection error (-) */
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2241da177e4SLinus Torvalds 	unsigned		magic;
2251da177e4SLinus Torvalds #define KEY_DEBUG_MAGIC		0x18273645u
2261da177e4SLinus Torvalds #endif
2271da177e4SLinus Torvalds 
22876d8aeabSDavid Howells 	unsigned long		flags;		/* status flags (change with bitops) */
229363b02daSDavid Howells #define KEY_FLAG_DEAD		0	/* set if key type has been deleted */
230363b02daSDavid Howells #define KEY_FLAG_REVOKED	1	/* set if key had been revoked */
231363b02daSDavid Howells #define KEY_FLAG_IN_QUOTA	2	/* set if key consumes quota */
232363b02daSDavid Howells #define KEY_FLAG_USER_CONSTRUCT	3	/* set if key is being constructed in userspace */
233363b02daSDavid Howells #define KEY_FLAG_ROOT_CAN_CLEAR	4	/* set if key can be cleared by root without permission */
234363b02daSDavid Howells #define KEY_FLAG_INVALIDATED	5	/* set if key has been invalidated */
235363b02daSDavid Howells #define KEY_FLAG_BUILTIN	6	/* set if key is built in to the kernel */
236363b02daSDavid Howells #define KEY_FLAG_ROOT_CAN_INVAL	7	/* set if key can be invalidated by root without permission */
237363b02daSDavid Howells #define KEY_FLAG_KEEP		8	/* set if key should not be removed */
238363b02daSDavid Howells #define KEY_FLAG_UID_KEYRING	9	/* set if key is a user or user session keyring */
239*75845c6cSDavid Howells #define KEY_FLAG_FINAL_PUT	10	/* set if final put has happened on key */
24076d8aeabSDavid Howells 
24116feef43SDavid Howells 	/* the key type and key description string
24216feef43SDavid Howells 	 * - the desc is used to match a key against search criteria
24316feef43SDavid Howells 	 * - it should be a printable string
2441da177e4SLinus Torvalds 	 * - eg: for krb5 AFS, this might be "[email protected]"
2451da177e4SLinus Torvalds 	 */
24616feef43SDavid Howells 	union {
24716feef43SDavid Howells 		struct keyring_index_key index_key;
24816feef43SDavid Howells 		struct {
249355ef8e1SDavid Howells 			unsigned long	hash;
250f771fde8SDavid Howells 			unsigned long	len_desc;
25116feef43SDavid Howells 			struct key_type	*type;		/* type of key */
2523b6e4de0SDavid Howells 			struct key_tag	*domain_tag;	/* Domain of operation */
2531da177e4SLinus Torvalds 			char		*description;
25416feef43SDavid Howells 		};
25516feef43SDavid Howells 	};
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* key data
2581da177e4SLinus Torvalds 	 * - this is used to hold the data actually used in cryptography or
2591da177e4SLinus Torvalds 	 *   whatever
2601da177e4SLinus Torvalds 	 */
2611da177e4SLinus Torvalds 	union {
262146aa8b1SDavid Howells 		union key_payload payload;
263146aa8b1SDavid Howells 		struct {
264146aa8b1SDavid Howells 			/* Keyring bits */
265146aa8b1SDavid Howells 			struct list_head name_link;
266b2a4df20SDavid Howells 			struct assoc_array keys;
267b2a4df20SDavid Howells 		};
268146aa8b1SDavid Howells 	};
2695ac7eaceSDavid Howells 
2705ac7eaceSDavid Howells 	/* This is set on a keyring to restrict the addition of a link to a key
2712b6aa412SMat Martineau 	 * to it.  If this structure isn't provided then it is assumed that the
2725ac7eaceSDavid Howells 	 * keyring is open to any addition.  It is ignored for non-keyring
2736563c91fSMat Martineau 	 * keys. Only set this value using keyring_restrict(), keyring_alloc(),
2746563c91fSMat Martineau 	 * or key_alloc().
2755ac7eaceSDavid Howells 	 *
2765ac7eaceSDavid Howells 	 * This is intended for use with rings of trusted keys whereby addition
2775ac7eaceSDavid Howells 	 * to the keyring needs to be controlled.  KEY_ALLOC_BYPASS_RESTRICTION
2785ac7eaceSDavid Howells 	 * overrides this, allowing the kernel to add extra keys without
2795ac7eaceSDavid Howells 	 * restriction.
2805ac7eaceSDavid Howells 	 */
2812b6aa412SMat Martineau 	struct key_restriction *restrict_link;
2821da177e4SLinus Torvalds };
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds extern struct key *key_alloc(struct key_type *type,
2851da177e4SLinus Torvalds 			     const char *desc,
2869a56c2dbSEric W. Biederman 			     kuid_t uid, kgid_t gid,
287d84f4f99SDavid Howells 			     const struct cred *cred,
288028db3e2SLinus Torvalds 			     key_perm_t perm,
2895ac7eaceSDavid Howells 			     unsigned long flags,
2902b6aa412SMat Martineau 			     struct key_restriction *restrict_link);
2917e047ef5SDavid Howells 
2927e047ef5SDavid Howells 
2937e047ef5SDavid Howells #define KEY_ALLOC_IN_QUOTA		0x0000	/* add to quota, reject if would overrun */
2947e047ef5SDavid Howells #define KEY_ALLOC_QUOTA_OVERRUN		0x0001	/* add to quota, permit even if overrun */
2957e047ef5SDavid Howells #define KEY_ALLOC_NOT_IN_QUOTA		0x0002	/* not in quota */
29677f68bacSDavid Howells #define KEY_ALLOC_BUILT_IN		0x0004	/* Key is built into kernel */
29777f68bacSDavid Howells #define KEY_ALLOC_BYPASS_RESTRICTION	0x0008	/* Override the check on restricted keyrings */
298237bbd29SEric Biggers #define KEY_ALLOC_UID_KEYRING		0x0010	/* allocating a user or user session keyring */
2994993e1f9SDavid Howells #define KEY_ALLOC_SET_KEEP		0x0020	/* Set the KEEP flag on the key/keyring */
3007e047ef5SDavid Howells 
3011da177e4SLinus Torvalds extern void key_revoke(struct key *key);
302fd75815fSDavid Howells extern void key_invalidate(struct key *key);
3031da177e4SLinus Torvalds extern void key_put(struct key *key);
3043b6e4de0SDavid Howells extern bool key_put_tag(struct key_tag *tag);
305218e6424SDavid Howells extern void key_remove_domain(struct key_tag *domain_tag);
3061da177e4SLinus Torvalds 
__key_get(struct key * key)307ccc3e6d9SDavid Howells static inline struct key *__key_get(struct key *key)
3081da177e4SLinus Torvalds {
309fff29291SElena Reshetova 	refcount_inc(&key->usage);
3101da177e4SLinus Torvalds 	return key;
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
key_get(struct key * key)313ccc3e6d9SDavid Howells static inline struct key *key_get(struct key *key)
314ccc3e6d9SDavid Howells {
315ccc3e6d9SDavid Howells 	return key ? __key_get(key) : key;
316ccc3e6d9SDavid Howells }
317ccc3e6d9SDavid Howells 
key_ref_put(key_ref_t key_ref)318664cceb0SDavid Howells static inline void key_ref_put(key_ref_t key_ref)
319664cceb0SDavid Howells {
320664cceb0SDavid Howells 	key_put(key_ref_to_ptr(key_ref));
321664cceb0SDavid Howells }
322664cceb0SDavid Howells 
323a58946c1SDavid Howells extern struct key *request_key_tag(struct key_type *type,
3241da177e4SLinus Torvalds 				   const char *description,
325a58946c1SDavid Howells 				   struct key_tag *domain_tag,
326028db3e2SLinus Torvalds 				   const char *callout_info);
3271da177e4SLinus Torvalds 
328896f1950SDavid Howells extern struct key *request_key_rcu(struct key_type *type,
329a58946c1SDavid Howells 				   const char *description,
330a58946c1SDavid Howells 				   struct key_tag *domain_tag);
331896f1950SDavid Howells 
3324e54f085SDavid Howells extern struct key *request_key_with_auxdata(struct key_type *type,
3334e54f085SDavid Howells 					    const char *description,
334a58946c1SDavid Howells 					    struct key_tag *domain_tag,
3354a38e122SDavid Howells 					    const void *callout_info,
3364a38e122SDavid Howells 					    size_t callout_len,
337028db3e2SLinus Torvalds 					    void *aux);
3384e54f085SDavid Howells 
339a58946c1SDavid Howells /**
340a58946c1SDavid Howells  * request_key - Request a key and wait for construction
341a58946c1SDavid Howells  * @type: Type of key.
342a58946c1SDavid Howells  * @description: The searchable description of the key.
343a58946c1SDavid Howells  * @callout_info: The data to pass to the instantiation upcall (or NULL).
344a58946c1SDavid Howells  *
345a58946c1SDavid Howells  * As for request_key_tag(), but with the default global domain tag.
346a58946c1SDavid Howells  */
request_key(struct key_type * type,const char * description,const char * callout_info)347a58946c1SDavid Howells static inline struct key *request_key(struct key_type *type,
348a58946c1SDavid Howells 				      const char *description,
349028db3e2SLinus Torvalds 				      const char *callout_info)
350a58946c1SDavid Howells {
351028db3e2SLinus Torvalds 	return request_key_tag(type, description, NULL, callout_info);
352a58946c1SDavid Howells }
353a58946c1SDavid Howells 
354a58946c1SDavid Howells #ifdef CONFIG_NET
3558b6a666aSDavid Howells /**
356a58946c1SDavid Howells  * request_key_net - Request a key for a net namespace and wait for construction
357a58946c1SDavid Howells  * @type: Type of key.
358a58946c1SDavid Howells  * @description: The searchable description of the key.
359a58946c1SDavid Howells  * @net: The network namespace that is the key's domain of operation.
360a58946c1SDavid Howells  * @callout_info: The data to pass to the instantiation upcall (or NULL).
361a58946c1SDavid Howells  *
362a58946c1SDavid Howells  * As for request_key() except that it does not add the returned key to a
363a58946c1SDavid Howells  * keyring if found, new keys are always allocated in the user's quota, the
364a58946c1SDavid Howells  * callout_info must be a NUL-terminated string and no auxiliary data can be
365a58946c1SDavid Howells  * passed.  Only keys that operate the specified network namespace are used.
366a58946c1SDavid Howells  *
367a58946c1SDavid Howells  * Furthermore, it then works as wait_for_key_construction() to wait for the
368a58946c1SDavid Howells  * completion of keys undergoing construction with a non-interruptible wait.
369a58946c1SDavid Howells  */
370028db3e2SLinus Torvalds #define request_key_net(type, description, net, callout_info) \
371464e96aeSTom Rix 	request_key_tag(type, description, net->key_domain, callout_info)
3728b6a666aSDavid Howells 
3738b6a666aSDavid Howells /**
3748b6a666aSDavid Howells  * request_key_net_rcu - Request a key for a net namespace under RCU conditions
3758b6a666aSDavid Howells  * @type: Type of key.
3768b6a666aSDavid Howells  * @description: The searchable description of the key.
3778b6a666aSDavid Howells  * @net: The network namespace that is the key's domain of operation.
3788b6a666aSDavid Howells  *
3798b6a666aSDavid Howells  * As for request_key_rcu() except that only keys that operate the specified
3808b6a666aSDavid Howells  * network namespace are used.
3818b6a666aSDavid Howells  */
3828b6a666aSDavid Howells #define request_key_net_rcu(type, description, net) \
383464e96aeSTom Rix 	request_key_rcu(type, description, net->key_domain)
384a58946c1SDavid Howells #endif /* CONFIG_NET */
385a58946c1SDavid Howells 
38676181c13SDavid Howells extern int wait_for_key_construction(struct key *key, bool intr);
38776181c13SDavid Howells 
388b404aef7SDavid Howells extern int key_validate(const struct key *key);
3891da177e4SLinus Torvalds 
3906c1976adSThomas Weißschuh extern key_ref_t key_create(key_ref_t keyring,
3916c1976adSThomas Weißschuh 			    const char *type,
3926c1976adSThomas Weißschuh 			    const char *description,
3936c1976adSThomas Weißschuh 			    const void *payload,
3946c1976adSThomas Weißschuh 			    size_t plen,
3956c1976adSThomas Weißschuh 			    key_perm_t perm,
3966c1976adSThomas Weißschuh 			    unsigned long flags);
3976c1976adSThomas Weißschuh 
398664cceb0SDavid Howells extern key_ref_t key_create_or_update(key_ref_t keyring,
3991da177e4SLinus Torvalds 				      const char *type,
4001da177e4SLinus Torvalds 				      const char *description,
4011da177e4SLinus Torvalds 				      const void *payload,
4021da177e4SLinus Torvalds 				      size_t plen,
403028db3e2SLinus Torvalds 				      key_perm_t perm,
4047e047ef5SDavid Howells 				      unsigned long flags);
4051da177e4SLinus Torvalds 
406664cceb0SDavid Howells extern int key_update(key_ref_t key,
4071da177e4SLinus Torvalds 		      const void *payload,
4081da177e4SLinus Torvalds 		      size_t plen);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds extern int key_link(struct key *keyring,
4111da177e4SLinus Torvalds 		    struct key *key);
4121da177e4SLinus Torvalds 
413ed0ac5c7SDavid Howells extern int key_move(struct key *key,
414ed0ac5c7SDavid Howells 		    struct key *from_keyring,
415ed0ac5c7SDavid Howells 		    struct key *to_keyring,
416ed0ac5c7SDavid Howells 		    unsigned int flags);
417ed0ac5c7SDavid Howells 
4181da177e4SLinus Torvalds extern int key_unlink(struct key *keyring,
4191da177e4SLinus Torvalds 		      struct key *key);
4201da177e4SLinus Torvalds 
4219a56c2dbSEric W. Biederman extern struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid,
422d84f4f99SDavid Howells 				 const struct cred *cred,
423028db3e2SLinus Torvalds 				 key_perm_t perm,
4247e047ef5SDavid Howells 				 unsigned long flags,
4252b6aa412SMat Martineau 				 struct key_restriction *restrict_link,
426d720024eSMichael LeMay 				 struct key *dest);
4271da177e4SLinus Torvalds 
4285ac7eaceSDavid Howells extern int restrict_link_reject(struct key *keyring,
4295ac7eaceSDavid Howells 				const struct key_type *type,
430aaf66c88SMat Martineau 				const union key_payload *payload,
431aaf66c88SMat Martineau 				struct key *restriction_key);
4325ac7eaceSDavid Howells 
4331da177e4SLinus Torvalds extern int keyring_clear(struct key *keyring);
4341da177e4SLinus Torvalds 
435664cceb0SDavid Howells extern key_ref_t keyring_search(key_ref_t keyring,
4361da177e4SLinus Torvalds 				struct key_type *type,
437dcf49dbcSDavid Howells 				const char *description,
438dcf49dbcSDavid Howells 				bool recurse);
4391da177e4SLinus Torvalds 
4406563c91fSMat Martineau extern int keyring_restrict(key_ref_t keyring, const char *type,
4416563c91fSMat Martineau 			    const char *restriction);
4426563c91fSMat Martineau 
4431da177e4SLinus Torvalds extern struct key *key_lookup(key_serial_t id);
4441da177e4SLinus Torvalds 
key_serial(const struct key * key)445456a8167SDavid Howells static inline key_serial_t key_serial(const struct key *key)
4467249db2cSDavid Howells {
4477249db2cSDavid Howells 	return key ? key->serial : 0;
4487249db2cSDavid Howells }
4491da177e4SLinus Torvalds 
45059e6b9c1SBryan Schumaker extern void key_set_timeout(struct key *, unsigned);
45159e6b9c1SBryan Schumaker 
45276ef5e17SDave Jiang extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
4538c0637e9SDavid Howells 				 enum key_need_perm need_perm);
454b206f281SDavid Howells extern void key_free_user_ns(struct user_namespace *);
45576ef5e17SDave Jiang 
key_read_state(const struct key * key)456363b02daSDavid Howells static inline short key_read_state(const struct key *key)
457363b02daSDavid Howells {
458363b02daSDavid Howells 	/* Barrier versus mark_key_instantiated(). */
459363b02daSDavid Howells 	return smp_load_acquire(&key->state);
460363b02daSDavid Howells }
461363b02daSDavid Howells 
46278b7280cSDavid Howells /**
463363b02daSDavid Howells  * key_is_positive - Determine if a key has been positively instantiated
46478b7280cSDavid Howells  * @key: The key to check.
46578b7280cSDavid Howells  *
46678b7280cSDavid Howells  * Return true if the specified key has been positively instantiated, false
46778b7280cSDavid Howells  * otherwise.
46878b7280cSDavid Howells  */
key_is_positive(const struct key * key)469363b02daSDavid Howells static inline bool key_is_positive(const struct key *key)
47078b7280cSDavid Howells {
471363b02daSDavid Howells 	return key_read_state(key) == KEY_IS_POSITIVE;
472363b02daSDavid Howells }
473363b02daSDavid Howells 
key_is_negative(const struct key * key)474363b02daSDavid Howells static inline bool key_is_negative(const struct key *key)
475363b02daSDavid Howells {
476363b02daSDavid Howells 	return key_read_state(key) < 0;
47778b7280cSDavid Howells }
47878b7280cSDavid Howells 
4790837e49aSDavid Howells #define dereference_key_rcu(KEY)					\
4800837e49aSDavid Howells 	(rcu_dereference((KEY)->payload.rcu_data0))
4810837e49aSDavid Howells 
4820837e49aSDavid Howells #define dereference_key_locked(KEY)					\
483146aa8b1SDavid Howells 	(rcu_dereference_protected((KEY)->payload.rcu_data0,		\
484633e804eSDavid Howells 				   rwsem_is_locked(&((struct key *)(KEY))->sem)))
485633e804eSDavid Howells 
486ee0b31a2SMimi Zohar #define rcu_assign_keypointer(KEY, PAYLOAD)				\
487e5c1f444SPaul E. McKenney do {									\
488146aa8b1SDavid Howells 	rcu_assign_pointer((KEY)->payload.rcu_data0, (PAYLOAD));	\
489e5c1f444SPaul E. McKenney } while (0)
490ee0b31a2SMimi Zohar 
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds  * the userspace interface
4931da177e4SLinus Torvalds  */
494d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *cred);
4952e21865fSDavid Howells extern void key_fsuid_changed(struct cred *new_cred);
4962e21865fSDavid Howells extern void key_fsgid_changed(struct cred *new_cred);
4971da177e4SLinus Torvalds extern void key_init(void);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds #else /* CONFIG_KEYS */
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds #define key_validate(k)			0
5021da177e4SLinus Torvalds #define key_serial(k)			0
5037888e7ffSDavid Howells #define key_get(k) 			({ NULL; })
5040dab9cfaSAdrian Bunk #define key_revoke(k)			do { } while(0)
505fd75815fSDavid Howells #define key_invalidate(k)		do { } while(0)
5061da177e4SLinus Torvalds #define key_put(k)			do { } while(0)
507664cceb0SDavid Howells #define key_ref_put(k)			do { } while(0)
5088bbf4976SDavid Howells #define make_key_ref(k, p)		NULL
5098bbf4976SDavid Howells #define key_ref_to_ptr(k)		NULL
510664cceb0SDavid Howells #define is_key_possessed(k)		0
5112e21865fSDavid Howells #define key_fsuid_changed(c)		do { } while(0)
5122e21865fSDavid Howells #define key_fsgid_changed(c)		do { } while(0)
5131da177e4SLinus Torvalds #define key_init()			do { } while(0)
514b206f281SDavid Howells #define key_free_user_ns(ns)		do { } while(0)
515218e6424SDavid Howells #define key_remove_domain(d)		do { } while(0)
516037c3431SHannes Reinecke #define key_lookup(k)			NULL
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds #endif /* CONFIG_KEYS */
5191da177e4SLinus Torvalds #endif /* __KERNEL__ */
5201da177e4SLinus Torvalds #endif /* _LINUX_KEY_H */
521