xref: /openbmc/linux/security/keys/internal.h (revision 5593122e)
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;
561d1e9756SSerge E. Hallyn 	struct user_namespace	*user_ns;
571da177e4SLinus Torvalds 	int			qnkeys;		/* number of keys allocated to this user */
581da177e4SLinus Torvalds 	int			qnbytes;	/* number of bytes allocated to this user */
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds extern struct rb_root	key_user_tree;
621da177e4SLinus Torvalds extern spinlock_t	key_user_lock;
631da177e4SLinus Torvalds extern struct key_user	root_key_user;
641da177e4SLinus Torvalds 
651d1e9756SSerge E. Hallyn extern struct key_user *key_user_lookup(uid_t uid,
661d1e9756SSerge E. Hallyn 					struct user_namespace *user_ns);
671da177e4SLinus Torvalds extern void key_user_put(struct key_user *user);
681da177e4SLinus Torvalds 
690b77f5bfSDavid Howells /*
700b77f5bfSDavid Howells  * key quota limits
710b77f5bfSDavid Howells  * - root has its own separate limits to everyone else
720b77f5bfSDavid Howells  */
730b77f5bfSDavid Howells extern unsigned key_quota_root_maxkeys;
740b77f5bfSDavid Howells extern unsigned key_quota_root_maxbytes;
750b77f5bfSDavid Howells extern unsigned key_quota_maxkeys;
760b77f5bfSDavid Howells extern unsigned key_quota_maxbytes;
770b77f5bfSDavid Howells 
780b77f5bfSDavid Howells #define KEYQUOTA_LINK_BYTES	4		/* a link in a keyring is worth 4 bytes */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds extern struct rb_root key_serial_tree;
821da177e4SLinus Torvalds extern spinlock_t key_serial_lock;
8376181c13SDavid Howells extern struct mutex key_construction_mutex;
841da177e4SLinus Torvalds extern wait_queue_head_t request_key_conswq;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 
87e9e349b0SDavid Howells extern struct key_type *key_type_lookup(const char *type);
88e9e349b0SDavid Howells extern void key_type_put(struct key_type *ktype);
89e9e349b0SDavid Howells 
901da177e4SLinus Torvalds extern int __key_link(struct key *keyring, struct key *key);
911da177e4SLinus Torvalds 
92664cceb0SDavid Howells extern key_ref_t __keyring_search_one(key_ref_t keyring_ref,
931da177e4SLinus Torvalds 				      const struct key_type *type,
941da177e4SLinus Torvalds 				      const char *description,
951da177e4SLinus Torvalds 				      key_perm_t perm);
961da177e4SLinus Torvalds 
973e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring,
983e30148cSDavid Howells 					  key_serial_t target_id);
993e30148cSDavid Howells 
1001da177e4SLinus Torvalds typedef int (*key_match_func_t)(const struct key *, const void *);
1011da177e4SLinus Torvalds 
102664cceb0SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
103d84f4f99SDavid Howells 				    const struct cred *cred,
1041da177e4SLinus Torvalds 				    struct key_type *type,
1051da177e4SLinus Torvalds 				    const void *description,
1061da177e4SLinus Torvalds 				    key_match_func_t match);
1071da177e4SLinus Torvalds 
108664cceb0SDavid Howells extern key_ref_t search_process_keyrings(struct key_type *type,
1091da177e4SLinus Torvalds 					 const void *description,
1103e30148cSDavid Howells 					 key_match_func_t match,
111d84f4f99SDavid Howells 					 const struct cred *cred);
1121da177e4SLinus Torvalds 
11369664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1141da177e4SLinus Torvalds 
1158bbf4976SDavid Howells extern int install_user_keyrings(void);
116d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *);
117d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *);
1183e30148cSDavid Howells 
1193e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type,
1203e30148cSDavid Howells 					const char *description,
1214a38e122SDavid Howells 					const void *callout_info,
1224a38e122SDavid Howells 					size_t callout_len,
1234e54f085SDavid Howells 					void *aux,
1247e047ef5SDavid Howells 					struct key *dest_keyring,
1257e047ef5SDavid Howells 					unsigned long flags);
1263e30148cSDavid Howells 
1275593122eSDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
128e9e349b0SDavid Howells 				 key_perm_t perm);
1295593122eSDavid Howells #define KEY_LOOKUP_CREATE	0x01
1305593122eSDavid Howells #define KEY_LOOKUP_PARTIAL	0x02
1315593122eSDavid Howells #define KEY_LOOKUP_FOR_UNLINK	0x04
132e9e349b0SDavid Howells 
133e9e349b0SDavid Howells extern long join_session_keyring(const char *name);
134e9e349b0SDavid Howells 
135e9e349b0SDavid Howells /*
136e9e349b0SDavid Howells  * check to see whether permission is granted to use a key in the desired way
137e9e349b0SDavid Howells  */
138e9e349b0SDavid Howells extern int key_task_permission(const key_ref_t key_ref,
139d84f4f99SDavid Howells 			       const struct cred *cred,
140e9e349b0SDavid Howells 			       key_perm_t perm);
141e9e349b0SDavid Howells 
142e9e349b0SDavid Howells static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
143e9e349b0SDavid Howells {
144d84f4f99SDavid Howells 	return key_task_permission(key_ref, current_cred(), perm);
145e9e349b0SDavid Howells }
146e9e349b0SDavid Howells 
147e9e349b0SDavid Howells /* required permissions */
148e9e349b0SDavid Howells #define	KEY_VIEW	0x01	/* require permission to view attributes */
149e9e349b0SDavid Howells #define	KEY_READ	0x02	/* require permission to read content */
150e9e349b0SDavid Howells #define	KEY_WRITE	0x04	/* require permission to update / modify */
151e9e349b0SDavid Howells #define	KEY_SEARCH	0x08	/* require permission to search (keyring) or find (key) */
152e9e349b0SDavid Howells #define	KEY_LINK	0x10	/* require permission to link */
153e9e349b0SDavid Howells #define	KEY_SETATTR	0x20	/* require permission to change attributes */
154e9e349b0SDavid Howells #define	KEY_ALL		0x3f	/* all the above permissions */
155e9e349b0SDavid Howells 
1563e30148cSDavid Howells /*
1573e30148cSDavid Howells  * request_key authorisation
1583e30148cSDavid Howells  */
1593e30148cSDavid Howells struct request_key_auth {
1603e30148cSDavid Howells 	struct key		*target_key;
1618bbf4976SDavid Howells 	struct key		*dest_keyring;
162d84f4f99SDavid Howells 	const struct cred	*cred;
1634a38e122SDavid Howells 	void			*callout_info;
1644a38e122SDavid Howells 	size_t			callout_len;
1653e30148cSDavid Howells 	pid_t			pid;
1663e30148cSDavid Howells };
1673e30148cSDavid Howells 
1683e30148cSDavid Howells extern struct key_type key_type_request_key_auth;
1693e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target,
1704a38e122SDavid Howells 					const void *callout_info,
1718bbf4976SDavid Howells 					size_t callout_len,
1728bbf4976SDavid Howells 					struct key *dest_keyring);
1733e30148cSDavid Howells 
1743e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /*
1771da177e4SLinus Torvalds  * keyctl functions
1781da177e4SLinus Torvalds  */
1791da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int);
1801da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *);
1811da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
1821da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t);
1831da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t);
1841da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t);
1851da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
1861da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
1871da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *,
1881da177e4SLinus Torvalds 				  const char __user *, key_serial_t);
1891da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t);
1901da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
1911da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t);
1921da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *,
1931da177e4SLinus Torvalds 				   size_t, key_serial_t);
1941da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
1953e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int);
196017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned);
197b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t);
19870a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
19970a5bb72SDavid Howells 				size_t buflen);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /*
2021da177e4SLinus Torvalds  * debugging key validation
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2051da177e4SLinus Torvalds extern void __key_check(const struct key *);
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static inline void key_check(const struct key *key)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
2101da177e4SLinus Torvalds 		__key_check(key);
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds #else
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #define key_check(key) do {} while(0)
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds #endif
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds #endif /* _INTERNAL_H */
220