xref: /openbmc/linux/security/keys/internal.h (revision 2b6aa412)
1973c9f4fSDavid Howells /* Authentication token and access key management internal defs
21da177e4SLinus Torvalds  *
376181c13SDavid Howells  * Copyright (C) 2003-5, 2007 Red Hat, Inc. All Rights Reserved.
41da177e4SLinus Torvalds  * Written by David Howells (dhowells@redhat.com)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  * modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  * as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  * 2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #ifndef _INTERNAL_H
131da177e4SLinus Torvalds #define _INTERNAL_H
141da177e4SLinus Torvalds 
15d84f4f99SDavid Howells #include <linux/sched.h>
165b825c3aSIngo Molnar #include <linux/cred.h>
1776181c13SDavid Howells #include <linux/key-type.h>
18413cd3d9SOleg Nesterov #include <linux/task_work.h>
19ddbb4114SMat Martineau #include <linux/keyctl.h>
20ddb99e11SElena Reshetova #include <linux/refcount.h>
211da177e4SLinus Torvalds 
22a27bb332SKent Overstreet struct iovec;
23a27bb332SKent Overstreet 
2476181c13SDavid Howells #ifdef __KDEBUG
2576181c13SDavid Howells #define kenter(FMT, ...) \
26dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
2776181c13SDavid Howells #define kleave(FMT, ...) \
28dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
2976181c13SDavid Howells #define kdebug(FMT, ...) \
30d84f4f99SDavid Howells 	printk(KERN_DEBUG "   "FMT"\n", ##__VA_ARGS__)
313e30148cSDavid Howells #else
3276181c13SDavid Howells #define kenter(FMT, ...) \
33dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
3476181c13SDavid Howells #define kleave(FMT, ...) \
35dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
3676181c13SDavid Howells #define kdebug(FMT, ...) \
3776181c13SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
383e30148cSDavid Howells #endif
393e30148cSDavid Howells 
400c061b57SDavid Howells extern struct key_type key_type_dead;
411da177e4SLinus Torvalds extern struct key_type key_type_user;
429f6ed2caSJeff Layton extern struct key_type key_type_logon;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /*****************************************************************************/
451da177e4SLinus Torvalds /*
46973c9f4fSDavid Howells  * Keep track of keys for a user.
47973c9f4fSDavid Howells  *
48973c9f4fSDavid Howells  * This needs to be separate to user_struct to avoid a refcount-loop
49973c9f4fSDavid Howells  * (user_struct pins some keyrings which pin this struct).
50973c9f4fSDavid Howells  *
51973c9f4fSDavid Howells  * We also keep track of keys under request from userspace for this UID here.
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds struct key_user {
541da177e4SLinus Torvalds 	struct rb_node		node;
5576181c13SDavid Howells 	struct mutex		cons_lock;	/* construction initiation lock */
561da177e4SLinus Torvalds 	spinlock_t		lock;
57ddb99e11SElena Reshetova 	refcount_t		usage;		/* for accessing qnkeys & qnbytes */
581da177e4SLinus Torvalds 	atomic_t		nkeys;		/* number of keys */
591da177e4SLinus Torvalds 	atomic_t		nikeys;		/* number of instantiated keys */
609a56c2dbSEric W. Biederman 	kuid_t			uid;
611da177e4SLinus Torvalds 	int			qnkeys;		/* number of keys allocated to this user */
621da177e4SLinus Torvalds 	int			qnbytes;	/* number of bytes allocated to this user */
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds extern struct rb_root	key_user_tree;
661da177e4SLinus Torvalds extern spinlock_t	key_user_lock;
671da177e4SLinus Torvalds extern struct key_user	root_key_user;
681da177e4SLinus Torvalds 
699a56c2dbSEric W. Biederman extern struct key_user *key_user_lookup(kuid_t uid);
701da177e4SLinus Torvalds extern void key_user_put(struct key_user *user);
711da177e4SLinus Torvalds 
720b77f5bfSDavid Howells /*
73973c9f4fSDavid Howells  * Key quota limits.
740b77f5bfSDavid Howells  * - root has its own separate limits to everyone else
750b77f5bfSDavid Howells  */
760b77f5bfSDavid Howells extern unsigned key_quota_root_maxkeys;
770b77f5bfSDavid Howells extern unsigned key_quota_root_maxbytes;
780b77f5bfSDavid Howells extern unsigned key_quota_maxkeys;
790b77f5bfSDavid Howells extern unsigned key_quota_maxbytes;
800b77f5bfSDavid Howells 
810b77f5bfSDavid Howells #define KEYQUOTA_LINK_BYTES	4		/* a link in a keyring is worth 4 bytes */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 
848bc16deaSDavid Howells extern struct kmem_cache *key_jar;
851da177e4SLinus Torvalds extern struct rb_root key_serial_tree;
861da177e4SLinus Torvalds extern spinlock_t key_serial_lock;
8776181c13SDavid Howells extern struct mutex key_construction_mutex;
881da177e4SLinus Torvalds extern wait_queue_head_t request_key_conswq;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 
91e9e349b0SDavid Howells extern struct key_type *key_type_lookup(const char *type);
92e9e349b0SDavid Howells extern void key_type_put(struct key_type *ktype);
93e9e349b0SDavid Howells 
94f70e2e06SDavid Howells extern int __key_link_begin(struct key *keyring,
9516feef43SDavid Howells 			    const struct keyring_index_key *index_key,
96b2a4df20SDavid Howells 			    struct assoc_array_edit **_edit);
97f70e2e06SDavid Howells extern int __key_link_check_live_key(struct key *keyring, struct key *key);
98b2a4df20SDavid Howells extern void __key_link(struct key *key, struct assoc_array_edit **_edit);
99f70e2e06SDavid Howells extern void __key_link_end(struct key *keyring,
10016feef43SDavid Howells 			   const struct keyring_index_key *index_key,
101b2a4df20SDavid Howells 			   struct assoc_array_edit *edit);
1021da177e4SLinus Torvalds 
103b2a4df20SDavid Howells extern key_ref_t find_key_to_update(key_ref_t keyring_ref,
104e57e8669SDavid Howells 				    const struct keyring_index_key *index_key);
1051da177e4SLinus Torvalds 
1063e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring,
1073e30148cSDavid Howells 					  key_serial_t target_id);
1083e30148cSDavid Howells 
109b2a4df20SDavid Howells extern int iterate_over_keyring(const struct key *keyring,
110b2a4df20SDavid Howells 				int (*func)(const struct key *key, void *data),
111b2a4df20SDavid Howells 				void *data);
112b2a4df20SDavid Howells 
1134bdf0bc3SDavid Howells struct keyring_search_context {
1144bdf0bc3SDavid Howells 	struct keyring_index_key index_key;
1154bdf0bc3SDavid Howells 	const struct cred	*cred;
11646291959SDavid Howells 	struct key_match_data	match_data;
1174bdf0bc3SDavid Howells 	unsigned		flags;
118614d8c39SDavid Howells #define KEYRING_SEARCH_NO_STATE_CHECK	0x0001	/* Skip state checks */
119614d8c39SDavid Howells #define KEYRING_SEARCH_DO_STATE_CHECK	0x0002	/* Override NO_STATE_CHECK */
120614d8c39SDavid Howells #define KEYRING_SEARCH_NO_UPDATE_TIME	0x0004	/* Don't update times */
121614d8c39SDavid Howells #define KEYRING_SEARCH_NO_CHECK_PERM	0x0008	/* Don't check permissions */
122614d8c39SDavid Howells #define KEYRING_SEARCH_DETECT_TOO_DEEP	0x0010	/* Give an error on excessive depth */
1230b0a8415SDavid Howells #define KEYRING_SEARCH_SKIP_EXPIRED	0x0020	/* Ignore expired keys (intention to replace) */
1241da177e4SLinus Torvalds 
125b2a4df20SDavid Howells 	int (*iterator)(const void *object, void *iterator_data);
126b2a4df20SDavid Howells 
1274bdf0bc3SDavid Howells 	/* Internal stuff */
1284bdf0bc3SDavid Howells 	int			skipped_ret;
1294bdf0bc3SDavid Howells 	bool			possessed;
1304bdf0bc3SDavid Howells 	key_ref_t		result;
1314bdf0bc3SDavid Howells 	struct timespec		now;
1324bdf0bc3SDavid Howells };
1334bdf0bc3SDavid Howells 
1340c903ab6SDavid Howells extern bool key_default_cmp(const struct key *key,
135c06cfb08SDavid Howells 			    const struct key_match_data *match_data);
1364bdf0bc3SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
1374bdf0bc3SDavid Howells 				    struct keyring_search_context *ctx);
1384bdf0bc3SDavid Howells 
1394bdf0bc3SDavid Howells extern key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx);
1404bdf0bc3SDavid Howells extern key_ref_t search_process_keyrings(struct keyring_search_context *ctx);
1411da177e4SLinus Torvalds 
14269664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1431da177e4SLinus Torvalds 
1448bbf4976SDavid Howells extern int install_user_keyrings(void);
145d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *);
146d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *);
147685bfd2cSOleg Nesterov extern int install_session_keyring_to_cred(struct cred *, struct key *);
1483e30148cSDavid Howells 
1493e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type,
1503e30148cSDavid Howells 					const char *description,
1514a38e122SDavid Howells 					const void *callout_info,
1524a38e122SDavid Howells 					size_t callout_len,
1534e54f085SDavid Howells 					void *aux,
1547e047ef5SDavid Howells 					struct key *dest_keyring,
1557e047ef5SDavid Howells 					unsigned long flags);
1563e30148cSDavid Howells 
1570c903ab6SDavid Howells extern bool lookup_user_key_possessed(const struct key *key,
15846291959SDavid Howells 				      const struct key_match_data *match_data);
1595593122eSDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
160e9e349b0SDavid Howells 				 key_perm_t perm);
1615593122eSDavid Howells #define KEY_LOOKUP_CREATE	0x01
1625593122eSDavid Howells #define KEY_LOOKUP_PARTIAL	0x02
1635593122eSDavid Howells #define KEY_LOOKUP_FOR_UNLINK	0x04
164e9e349b0SDavid Howells 
165e9e349b0SDavid Howells extern long join_session_keyring(const char *name);
16667d12145SAl Viro extern void key_change_session_keyring(struct callback_head *twork);
167e9e349b0SDavid Howells 
1680c061b57SDavid Howells extern struct work_struct key_gc_work;
1695d135440SDavid Howells extern unsigned key_gc_delay;
1705d135440SDavid Howells extern void keyring_gc(struct key *keyring, time_t limit);
1712b6aa412SMat Martineau extern void keyring_restriction_gc(struct key *keyring,
1722b6aa412SMat Martineau 				   struct key_type *dead_type);
173fd75815fSDavid Howells extern void key_schedule_gc(time_t gc_at);
174fd75815fSDavid Howells extern void key_schedule_gc_links(void);
1750c061b57SDavid Howells extern void key_gc_keytype(struct key_type *ktype);
1765d135440SDavid Howells 
177e9e349b0SDavid Howells extern int key_task_permission(const key_ref_t key_ref,
178d84f4f99SDavid Howells 			       const struct cred *cred,
179e9e349b0SDavid Howells 			       key_perm_t perm);
180e9e349b0SDavid Howells 
181973c9f4fSDavid Howells /*
182973c9f4fSDavid Howells  * Check to see whether permission is granted to use a key in the desired way.
183973c9f4fSDavid Howells  */
184f5895943SDavid Howells static inline int key_permission(const key_ref_t key_ref, unsigned perm)
185e9e349b0SDavid Howells {
186d84f4f99SDavid Howells 	return key_task_permission(key_ref, current_cred(), perm);
187e9e349b0SDavid Howells }
188e9e349b0SDavid Howells 
1893e30148cSDavid Howells /*
190973c9f4fSDavid Howells  * Authorisation record for request_key().
1913e30148cSDavid Howells  */
1923e30148cSDavid Howells struct request_key_auth {
1933e30148cSDavid Howells 	struct key		*target_key;
1948bbf4976SDavid Howells 	struct key		*dest_keyring;
195d84f4f99SDavid Howells 	const struct cred	*cred;
1964a38e122SDavid Howells 	void			*callout_info;
1974a38e122SDavid Howells 	size_t			callout_len;
1983e30148cSDavid Howells 	pid_t			pid;
1993e30148cSDavid Howells };
2003e30148cSDavid Howells 
2013e30148cSDavid Howells extern struct key_type key_type_request_key_auth;
2023e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target,
2034a38e122SDavid Howells 					const void *callout_info,
2048bbf4976SDavid Howells 					size_t callout_len,
2058bbf4976SDavid Howells 					struct key *dest_keyring);
2063e30148cSDavid Howells 
2073e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /*
210fd75815fSDavid Howells  * Determine whether a key is dead.
211fd75815fSDavid Howells  */
2127e55ca6dSDavid Howells static inline bool key_is_dead(const struct key *key, time_t limit)
213fd75815fSDavid Howells {
214fd75815fSDavid Howells 	return
215fd75815fSDavid Howells 		key->flags & ((1 << KEY_FLAG_DEAD) |
216fd75815fSDavid Howells 			      (1 << KEY_FLAG_INVALIDATED)) ||
217fd75815fSDavid Howells 		(key->expiry > 0 && key->expiry <= limit);
218fd75815fSDavid Howells }
219fd75815fSDavid Howells 
220fd75815fSDavid Howells /*
221973c9f4fSDavid Howells  * keyctl() functions
2221da177e4SLinus Torvalds  */
2231da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int);
2241da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *);
2251da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
2261da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t);
2271da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t);
2281da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t);
2291da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
2301da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
2311da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *,
2321da177e4SLinus Torvalds 				  const char __user *, key_serial_t);
2331da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t);
2341da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
2351da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t);
2361da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *,
2371da177e4SLinus Torvalds 				   size_t, key_serial_t);
2381da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
2393e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int);
240017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned);
241b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t);
24270a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
24370a5bb72SDavid Howells 				size_t buflen);
244ee18d64cSDavid Howells extern long keyctl_session_to_parent(void);
245fdd1b945SDavid Howells extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t);
246ee009e4aSDavid Howells extern long keyctl_instantiate_key_iov(key_serial_t,
247ee009e4aSDavid Howells 				       const struct iovec __user *,
248ee009e4aSDavid Howells 				       unsigned, key_serial_t);
249fd75815fSDavid Howells extern long keyctl_invalidate_key(key_serial_t);
250ee009e4aSDavid Howells 
251b353a1f7SAl Viro struct iov_iter;
252ee009e4aSDavid Howells extern long keyctl_instantiate_key_common(key_serial_t,
253b353a1f7SAl Viro 					  struct iov_iter *,
254b353a1f7SAl Viro 					  key_serial_t);
255f36f8c75SDavid Howells #ifdef CONFIG_PERSISTENT_KEYRINGS
256f36f8c75SDavid Howells extern long keyctl_get_persistent(uid_t, key_serial_t);
257f36f8c75SDavid Howells extern unsigned persistent_keyring_expiry;
258f36f8c75SDavid Howells #else
259f36f8c75SDavid Howells static inline long keyctl_get_persistent(uid_t uid, key_serial_t destring)
260f36f8c75SDavid Howells {
261f36f8c75SDavid Howells 	return -EOPNOTSUPP;
262f36f8c75SDavid Howells }
263f36f8c75SDavid Howells #endif
2641da177e4SLinus Torvalds 
265ddbb4114SMat Martineau #ifdef CONFIG_KEY_DH_OPERATIONS
266ddbb4114SMat Martineau extern long keyctl_dh_compute(struct keyctl_dh_params __user *, char __user *,
2674693fc73SStephan Mueller 			      size_t, void __user *);
268ddbb4114SMat Martineau #else
269ddbb4114SMat Martineau static inline long keyctl_dh_compute(struct keyctl_dh_params __user *params,
2704693fc73SStephan Mueller 				     char __user *buffer, size_t buflen,
2714693fc73SStephan Mueller 				     void __user *reserved)
272ddbb4114SMat Martineau {
273ddbb4114SMat Martineau 	return -EOPNOTSUPP;
274ddbb4114SMat Martineau }
275ddbb4114SMat Martineau #endif
276ddbb4114SMat Martineau 
2771da177e4SLinus Torvalds /*
278973c9f4fSDavid Howells  * Debugging key validation
2791da177e4SLinus Torvalds  */
2801da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2811da177e4SLinus Torvalds extern void __key_check(const struct key *);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static inline void key_check(const struct key *key)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
2861da177e4SLinus Torvalds 		__key_check(key);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds #else
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds #define key_check(key) do {} while(0)
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds #endif
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds #endif /* _INTERNAL_H */
296