xref: /openbmc/linux/security/keys/internal.h (revision d84f4f99)
11da177e4SLinus Torvalds /* internal.h: 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 static inline __attribute__((format(printf, 1, 2)))
1976181c13SDavid Howells void no_printk(const char *fmt, ...)
2076181c13SDavid Howells {
2176181c13SDavid Howells }
2276181c13SDavid Howells 
2376181c13SDavid Howells #ifdef __KDEBUG
2476181c13SDavid Howells #define kenter(FMT, ...) \
25dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
2676181c13SDavid Howells #define kleave(FMT, ...) \
27dd6f953aSHarvey Harrison 	printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
2876181c13SDavid Howells #define kdebug(FMT, ...) \
29d84f4f99SDavid Howells 	printk(KERN_DEBUG "   "FMT"\n", ##__VA_ARGS__)
303e30148cSDavid Howells #else
3176181c13SDavid Howells #define kenter(FMT, ...) \
32dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "==> %s("FMT")\n", __func__, ##__VA_ARGS__)
3376181c13SDavid Howells #define kleave(FMT, ...) \
34dd6f953aSHarvey Harrison 	no_printk(KERN_DEBUG "<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
3576181c13SDavid Howells #define kdebug(FMT, ...) \
3676181c13SDavid Howells 	no_printk(KERN_DEBUG FMT"\n", ##__VA_ARGS__)
373e30148cSDavid Howells #endif
383e30148cSDavid Howells 
391da177e4SLinus Torvalds extern struct key_type key_type_user;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*****************************************************************************/
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * keep track of keys for a user
441da177e4SLinus Torvalds  * - this needs to be separate to user_struct to avoid a refcount-loop
451da177e4SLinus Torvalds  *   (user_struct pins some keyrings which pin this struct)
461da177e4SLinus Torvalds  * - this also keeps track of keys under request from userspace for this UID
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds struct key_user {
491da177e4SLinus Torvalds 	struct rb_node		node;
5076181c13SDavid Howells 	struct mutex		cons_lock;	/* construction initiation lock */
511da177e4SLinus Torvalds 	spinlock_t		lock;
521da177e4SLinus Torvalds 	atomic_t		usage;		/* for accessing qnkeys & qnbytes */
531da177e4SLinus Torvalds 	atomic_t		nkeys;		/* number of keys */
541da177e4SLinus Torvalds 	atomic_t		nikeys;		/* number of instantiated keys */
551da177e4SLinus Torvalds 	uid_t			uid;
561da177e4SLinus Torvalds 	int			qnkeys;		/* number of keys allocated to this user */
571da177e4SLinus Torvalds 	int			qnbytes;	/* number of bytes allocated to this user */
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds extern struct rb_root	key_user_tree;
611da177e4SLinus Torvalds extern spinlock_t	key_user_lock;
621da177e4SLinus Torvalds extern struct key_user	root_key_user;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds extern struct key_user *key_user_lookup(uid_t uid);
651da177e4SLinus Torvalds extern void key_user_put(struct key_user *user);
661da177e4SLinus Torvalds 
670b77f5bfSDavid Howells /*
680b77f5bfSDavid Howells  * key quota limits
690b77f5bfSDavid Howells  * - root has its own separate limits to everyone else
700b77f5bfSDavid Howells  */
710b77f5bfSDavid Howells extern unsigned key_quota_root_maxkeys;
720b77f5bfSDavid Howells extern unsigned key_quota_root_maxbytes;
730b77f5bfSDavid Howells extern unsigned key_quota_maxkeys;
740b77f5bfSDavid Howells extern unsigned key_quota_maxbytes;
750b77f5bfSDavid Howells 
760b77f5bfSDavid Howells #define KEYQUOTA_LINK_BYTES	4		/* a link in a keyring is worth 4 bytes */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds extern struct rb_root key_serial_tree;
801da177e4SLinus Torvalds extern spinlock_t key_serial_lock;
8176181c13SDavid Howells extern struct mutex key_construction_mutex;
821da177e4SLinus Torvalds extern wait_queue_head_t request_key_conswq;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 
85e9e349b0SDavid Howells extern struct key_type *key_type_lookup(const char *type);
86e9e349b0SDavid Howells extern void key_type_put(struct key_type *ktype);
87e9e349b0SDavid Howells 
881da177e4SLinus Torvalds extern int __key_link(struct key *keyring, struct key *key);
891da177e4SLinus Torvalds 
90664cceb0SDavid Howells extern key_ref_t __keyring_search_one(key_ref_t keyring_ref,
911da177e4SLinus Torvalds 				      const struct key_type *type,
921da177e4SLinus Torvalds 				      const char *description,
931da177e4SLinus Torvalds 				      key_perm_t perm);
941da177e4SLinus Torvalds 
953e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring,
963e30148cSDavid Howells 					  key_serial_t target_id);
973e30148cSDavid Howells 
981da177e4SLinus Torvalds typedef int (*key_match_func_t)(const struct key *, const void *);
991da177e4SLinus Torvalds 
100664cceb0SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
101d84f4f99SDavid Howells 				    const struct cred *cred,
1021da177e4SLinus Torvalds 				    struct key_type *type,
1031da177e4SLinus Torvalds 				    const void *description,
1041da177e4SLinus Torvalds 				    key_match_func_t match);
1051da177e4SLinus Torvalds 
106664cceb0SDavid Howells extern key_ref_t search_process_keyrings(struct key_type *type,
1071da177e4SLinus Torvalds 					 const void *description,
1083e30148cSDavid Howells 					 key_match_func_t match,
109d84f4f99SDavid Howells 					 const struct cred *cred);
1101da177e4SLinus Torvalds 
11169664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1121da177e4SLinus Torvalds 
1138bbf4976SDavid Howells extern int install_user_keyrings(void);
114d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *);
115d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *);
1163e30148cSDavid Howells 
1173e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type,
1183e30148cSDavid Howells 					const char *description,
1194a38e122SDavid Howells 					const void *callout_info,
1204a38e122SDavid Howells 					size_t callout_len,
1214e54f085SDavid Howells 					void *aux,
1227e047ef5SDavid Howells 					struct key *dest_keyring,
1237e047ef5SDavid Howells 					unsigned long flags);
1243e30148cSDavid Howells 
1258bbf4976SDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
126e9e349b0SDavid Howells 				 key_perm_t perm);
127e9e349b0SDavid Howells 
128e9e349b0SDavid Howells extern long join_session_keyring(const char *name);
129e9e349b0SDavid Howells 
130e9e349b0SDavid Howells /*
131e9e349b0SDavid Howells  * check to see whether permission is granted to use a key in the desired way
132e9e349b0SDavid Howells  */
133e9e349b0SDavid Howells extern int key_task_permission(const key_ref_t key_ref,
134d84f4f99SDavid Howells 			       const struct cred *cred,
135e9e349b0SDavid Howells 			       key_perm_t perm);
136e9e349b0SDavid Howells 
137e9e349b0SDavid Howells static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
138e9e349b0SDavid Howells {
139d84f4f99SDavid Howells 	return key_task_permission(key_ref, current_cred(), perm);
140e9e349b0SDavid Howells }
141e9e349b0SDavid Howells 
142e9e349b0SDavid Howells /* required permissions */
143e9e349b0SDavid Howells #define	KEY_VIEW	0x01	/* require permission to view attributes */
144e9e349b0SDavid Howells #define	KEY_READ	0x02	/* require permission to read content */
145e9e349b0SDavid Howells #define	KEY_WRITE	0x04	/* require permission to update / modify */
146e9e349b0SDavid Howells #define	KEY_SEARCH	0x08	/* require permission to search (keyring) or find (key) */
147e9e349b0SDavid Howells #define	KEY_LINK	0x10	/* require permission to link */
148e9e349b0SDavid Howells #define	KEY_SETATTR	0x20	/* require permission to change attributes */
149e9e349b0SDavid Howells #define	KEY_ALL		0x3f	/* all the above permissions */
150e9e349b0SDavid Howells 
1513e30148cSDavid Howells /*
1523e30148cSDavid Howells  * request_key authorisation
1533e30148cSDavid Howells  */
1543e30148cSDavid Howells struct request_key_auth {
1553e30148cSDavid Howells 	struct key		*target_key;
1568bbf4976SDavid Howells 	struct key		*dest_keyring;
157d84f4f99SDavid Howells 	const struct cred	*cred;
1584a38e122SDavid Howells 	void			*callout_info;
1594a38e122SDavid Howells 	size_t			callout_len;
1603e30148cSDavid Howells 	pid_t			pid;
1613e30148cSDavid Howells };
1623e30148cSDavid Howells 
1633e30148cSDavid Howells extern struct key_type key_type_request_key_auth;
1643e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target,
1654a38e122SDavid Howells 					const void *callout_info,
1668bbf4976SDavid Howells 					size_t callout_len,
1678bbf4976SDavid Howells 					struct key *dest_keyring);
1683e30148cSDavid Howells 
1693e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /*
1721da177e4SLinus Torvalds  * keyctl functions
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int);
1751da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *);
1761da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
1771da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t);
1781da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t);
1791da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t);
1801da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
1811da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
1821da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *,
1831da177e4SLinus Torvalds 				  const char __user *, key_serial_t);
1841da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t);
1851da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
1861da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t);
1871da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *,
1881da177e4SLinus Torvalds 				   size_t, key_serial_t);
1891da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
1903e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int);
191017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned);
192b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t);
19370a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
19470a5bb72SDavid Howells 				size_t buflen);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  * debugging key validation
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2001da177e4SLinus Torvalds extern void __key_check(const struct key *);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static inline void key_check(const struct key *key)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
2051da177e4SLinus Torvalds 		__key_check(key);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds #else
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds #define key_check(key) do {} while(0)
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #endif
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds #endif /* _INTERNAL_H */
215