xref: /openbmc/linux/security/keys/internal.h (revision fdd1b945)
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>
1676181c13SDavid Howells #include <linux/key-type.h>
171da177e4SLinus Torvalds 
1876181c13SDavid Howells #ifdef __KDEBUG
1976181c13SDavid Howells #define kenter(FMT, ...) \
20dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
2176181c13SDavid Howells #define kleave(FMT, ...) \
22dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
2376181c13SDavid Howells #define kdebug(FMT, ...) \
24d84f4f99SDavid Howells 	printk(KERN_DEBUG "   "FMT"\n", ##__VA_ARGS__)
253e30148cSDavid Howells #else
2676181c13SDavid Howells #define kenter(FMT, ...) \
27dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
2876181c13SDavid Howells #define kleave(FMT, ...) \
29dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
3076181c13SDavid Howells #define kdebug(FMT, ...) \
3176181c13SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
323e30148cSDavid Howells #endif
333e30148cSDavid Howells 
341da177e4SLinus Torvalds extern struct key_type key_type_user;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*****************************************************************************/
371da177e4SLinus Torvalds /*
38973c9f4fSDavid Howells  * Keep track of keys for a user.
39973c9f4fSDavid Howells  *
40973c9f4fSDavid Howells  * This needs to be separate to user_struct to avoid a refcount-loop
41973c9f4fSDavid Howells  * (user_struct pins some keyrings which pin this struct).
42973c9f4fSDavid Howells  *
43973c9f4fSDavid Howells  * We also keep track of keys under request from userspace for this UID here.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds struct key_user {
461da177e4SLinus Torvalds 	struct rb_node		node;
4776181c13SDavid Howells 	struct mutex		cons_lock;	/* construction initiation lock */
481da177e4SLinus Torvalds 	spinlock_t		lock;
491da177e4SLinus Torvalds 	atomic_t		usage;		/* for accessing qnkeys & qnbytes */
501da177e4SLinus Torvalds 	atomic_t		nkeys;		/* number of keys */
511da177e4SLinus Torvalds 	atomic_t		nikeys;		/* number of instantiated keys */
521da177e4SLinus Torvalds 	uid_t			uid;
531d1e9756SSerge E. Hallyn 	struct user_namespace	*user_ns;
541da177e4SLinus Torvalds 	int			qnkeys;		/* number of keys allocated to this user */
551da177e4SLinus Torvalds 	int			qnbytes;	/* number of bytes allocated to this user */
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds extern struct rb_root	key_user_tree;
591da177e4SLinus Torvalds extern spinlock_t	key_user_lock;
601da177e4SLinus Torvalds extern struct key_user	root_key_user;
611da177e4SLinus Torvalds 
621d1e9756SSerge E. Hallyn extern struct key_user *key_user_lookup(uid_t uid,
631d1e9756SSerge E. Hallyn 					struct user_namespace *user_ns);
641da177e4SLinus Torvalds extern void key_user_put(struct key_user *user);
651da177e4SLinus Torvalds 
660b77f5bfSDavid Howells /*
67973c9f4fSDavid Howells  * Key quota limits.
680b77f5bfSDavid Howells  * - root has its own separate limits to everyone else
690b77f5bfSDavid Howells  */
700b77f5bfSDavid Howells extern unsigned key_quota_root_maxkeys;
710b77f5bfSDavid Howells extern unsigned key_quota_root_maxbytes;
720b77f5bfSDavid Howells extern unsigned key_quota_maxkeys;
730b77f5bfSDavid Howells extern unsigned key_quota_maxbytes;
740b77f5bfSDavid Howells 
750b77f5bfSDavid Howells #define KEYQUOTA_LINK_BYTES	4		/* a link in a keyring is worth 4 bytes */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds extern struct rb_root key_serial_tree;
791da177e4SLinus Torvalds extern spinlock_t key_serial_lock;
8076181c13SDavid Howells extern struct mutex key_construction_mutex;
811da177e4SLinus Torvalds extern wait_queue_head_t request_key_conswq;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 
84e9e349b0SDavid Howells extern struct key_type *key_type_lookup(const char *type);
85e9e349b0SDavid Howells extern void key_type_put(struct key_type *ktype);
86e9e349b0SDavid Howells 
87f70e2e06SDavid Howells extern int __key_link_begin(struct key *keyring,
88f70e2e06SDavid Howells 			    const struct key_type *type,
89f70e2e06SDavid Howells 			    const char *description,
90ceb73c12SDavid Howells 			    unsigned long *_prealloc);
91f70e2e06SDavid Howells extern int __key_link_check_live_key(struct key *keyring, struct key *key);
92f70e2e06SDavid Howells extern void __key_link(struct key *keyring, struct key *key,
93ceb73c12SDavid Howells 		       unsigned long *_prealloc);
94f70e2e06SDavid Howells extern void __key_link_end(struct key *keyring,
95f70e2e06SDavid Howells 			   struct key_type *type,
96ceb73c12SDavid Howells 			   unsigned long prealloc);
971da177e4SLinus Torvalds 
98664cceb0SDavid Howells extern key_ref_t __keyring_search_one(key_ref_t keyring_ref,
991da177e4SLinus Torvalds 				      const struct key_type *type,
1001da177e4SLinus Torvalds 				      const char *description,
1011da177e4SLinus Torvalds 				      key_perm_t perm);
1021da177e4SLinus Torvalds 
1033e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring,
1043e30148cSDavid Howells 					  key_serial_t target_id);
1053e30148cSDavid Howells 
1061da177e4SLinus Torvalds typedef int (*key_match_func_t)(const struct key *, const void *);
1071da177e4SLinus Torvalds 
108664cceb0SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
109d84f4f99SDavid Howells 				    const struct cred *cred,
1101da177e4SLinus Torvalds 				    struct key_type *type,
1111da177e4SLinus Torvalds 				    const void *description,
1121da177e4SLinus Torvalds 				    key_match_func_t match);
1131da177e4SLinus Torvalds 
114927942aaSDavid Howells extern key_ref_t search_my_process_keyrings(struct key_type *type,
115927942aaSDavid Howells 					    const void *description,
116927942aaSDavid Howells 					    key_match_func_t match,
117927942aaSDavid Howells 					    const struct cred *cred);
118664cceb0SDavid Howells extern key_ref_t search_process_keyrings(struct key_type *type,
1191da177e4SLinus Torvalds 					 const void *description,
1203e30148cSDavid Howells 					 key_match_func_t match,
121d84f4f99SDavid Howells 					 const struct cred *cred);
1221da177e4SLinus Torvalds 
12369664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1241da177e4SLinus Torvalds 
1258bbf4976SDavid Howells extern int install_user_keyrings(void);
126d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *);
127d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *);
128685bfd2cSOleg Nesterov extern int install_session_keyring_to_cred(struct cred *, struct key *);
1293e30148cSDavid Howells 
1303e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type,
1313e30148cSDavid Howells 					const char *description,
1324a38e122SDavid Howells 					const void *callout_info,
1334a38e122SDavid Howells 					size_t callout_len,
1344e54f085SDavid Howells 					void *aux,
1357e047ef5SDavid Howells 					struct key *dest_keyring,
1367e047ef5SDavid Howells 					unsigned long flags);
1373e30148cSDavid Howells 
138927942aaSDavid Howells extern int lookup_user_key_possessed(const struct key *key, const void *target);
1395593122eSDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
140e9e349b0SDavid Howells 				 key_perm_t perm);
1415593122eSDavid Howells #define KEY_LOOKUP_CREATE	0x01
1425593122eSDavid Howells #define KEY_LOOKUP_PARTIAL	0x02
1435593122eSDavid Howells #define KEY_LOOKUP_FOR_UNLINK	0x04
144e9e349b0SDavid Howells 
145e9e349b0SDavid Howells extern long join_session_keyring(const char *name);
146e9e349b0SDavid Howells 
1475d135440SDavid Howells extern unsigned key_gc_delay;
1485d135440SDavid Howells extern void keyring_gc(struct key *keyring, time_t limit);
1495d135440SDavid Howells extern void key_schedule_gc(time_t expiry_at);
1505d135440SDavid Howells 
151e9e349b0SDavid Howells extern int key_task_permission(const key_ref_t key_ref,
152d84f4f99SDavid Howells 			       const struct cred *cred,
153e9e349b0SDavid Howells 			       key_perm_t perm);
154e9e349b0SDavid Howells 
155973c9f4fSDavid Howells /*
156973c9f4fSDavid Howells  * Check to see whether permission is granted to use a key in the desired way.
157973c9f4fSDavid Howells  */
158e9e349b0SDavid Howells static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
159e9e349b0SDavid Howells {
160d84f4f99SDavid Howells 	return key_task_permission(key_ref, current_cred(), perm);
161e9e349b0SDavid Howells }
162e9e349b0SDavid Howells 
163e9e349b0SDavid Howells /* required permissions */
164e9e349b0SDavid Howells #define	KEY_VIEW	0x01	/* require permission to view attributes */
165e9e349b0SDavid Howells #define	KEY_READ	0x02	/* require permission to read content */
166e9e349b0SDavid Howells #define	KEY_WRITE	0x04	/* require permission to update / modify */
167e9e349b0SDavid Howells #define	KEY_SEARCH	0x08	/* require permission to search (keyring) or find (key) */
168e9e349b0SDavid Howells #define	KEY_LINK	0x10	/* require permission to link */
169e9e349b0SDavid Howells #define	KEY_SETATTR	0x20	/* require permission to change attributes */
170e9e349b0SDavid Howells #define	KEY_ALL		0x3f	/* all the above permissions */
171e9e349b0SDavid Howells 
1723e30148cSDavid Howells /*
173973c9f4fSDavid Howells  * Authorisation record for request_key().
1743e30148cSDavid Howells  */
1753e30148cSDavid Howells struct request_key_auth {
1763e30148cSDavid Howells 	struct key		*target_key;
1778bbf4976SDavid Howells 	struct key		*dest_keyring;
178d84f4f99SDavid Howells 	const struct cred	*cred;
1794a38e122SDavid Howells 	void			*callout_info;
1804a38e122SDavid Howells 	size_t			callout_len;
1813e30148cSDavid Howells 	pid_t			pid;
1823e30148cSDavid Howells };
1833e30148cSDavid Howells 
1843e30148cSDavid Howells extern struct key_type key_type_request_key_auth;
1853e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target,
1864a38e122SDavid Howells 					const void *callout_info,
1878bbf4976SDavid Howells 					size_t callout_len,
1888bbf4976SDavid Howells 					struct key *dest_keyring);
1893e30148cSDavid Howells 
1903e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
193973c9f4fSDavid Howells  * keyctl() functions
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int);
1961da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *);
1971da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
1981da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t);
1991da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t);
2001da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t);
2011da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
2021da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
2031da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *,
2041da177e4SLinus Torvalds 				  const char __user *, key_serial_t);
2051da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t);
2061da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
2071da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t);
2081da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *,
2091da177e4SLinus Torvalds 				   size_t, key_serial_t);
2101da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
2113e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int);
212017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned);
213b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t);
21470a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
21570a5bb72SDavid Howells 				size_t buflen);
216ee18d64cSDavid Howells extern long keyctl_session_to_parent(void);
217fdd1b945SDavid Howells extern long keyctl_reject_key(key_serial_t, unsigned, unsigned, key_serial_t);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds /*
220973c9f4fSDavid Howells  * Debugging key validation
2211da177e4SLinus Torvalds  */
2221da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2231da177e4SLinus Torvalds extern void __key_check(const struct key *);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds static inline void key_check(const struct key *key)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
2281da177e4SLinus Torvalds 		__key_check(key);
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds #else
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds #define key_check(key) do {} while(0)
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds #endif
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds #endif /* _INTERNAL_H */
238