xref: /openbmc/linux/security/keys/internal.h (revision f70e2e06)
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 
90f70e2e06SDavid Howells extern int __key_link_begin(struct key *keyring,
91f70e2e06SDavid Howells 			    const struct key_type *type,
92f70e2e06SDavid Howells 			    const char *description,
93f70e2e06SDavid Howells 			    struct keyring_list **_prealloc);
94f70e2e06SDavid Howells extern int __key_link_check_live_key(struct key *keyring, struct key *key);
95f70e2e06SDavid Howells extern void __key_link(struct key *keyring, struct key *key,
96f70e2e06SDavid Howells 		       struct keyring_list **_prealloc);
97f70e2e06SDavid Howells extern void __key_link_end(struct key *keyring,
98f70e2e06SDavid Howells 			   struct key_type *type,
99f70e2e06SDavid Howells 			   struct keyring_list *prealloc);
1001da177e4SLinus Torvalds 
101664cceb0SDavid Howells extern key_ref_t __keyring_search_one(key_ref_t keyring_ref,
1021da177e4SLinus Torvalds 				      const struct key_type *type,
1031da177e4SLinus Torvalds 				      const char *description,
1041da177e4SLinus Torvalds 				      key_perm_t perm);
1051da177e4SLinus Torvalds 
1063e30148cSDavid Howells extern struct key *keyring_search_instkey(struct key *keyring,
1073e30148cSDavid Howells 					  key_serial_t target_id);
1083e30148cSDavid Howells 
1091da177e4SLinus Torvalds typedef int (*key_match_func_t)(const struct key *, const void *);
1101da177e4SLinus Torvalds 
111664cceb0SDavid Howells extern key_ref_t keyring_search_aux(key_ref_t keyring_ref,
112d84f4f99SDavid Howells 				    const struct cred *cred,
1131da177e4SLinus Torvalds 				    struct key_type *type,
1141da177e4SLinus Torvalds 				    const void *description,
1151da177e4SLinus Torvalds 				    key_match_func_t match);
1161da177e4SLinus Torvalds 
117664cceb0SDavid Howells extern key_ref_t search_process_keyrings(struct key_type *type,
1181da177e4SLinus Torvalds 					 const void *description,
1193e30148cSDavid Howells 					 key_match_func_t match,
120d84f4f99SDavid Howells 					 const struct cred *cred);
1211da177e4SLinus Torvalds 
12269664cf1SDavid Howells extern struct key *find_keyring_by_name(const char *name, bool skip_perm_check);
1231da177e4SLinus Torvalds 
1248bbf4976SDavid Howells extern int install_user_keyrings(void);
125d84f4f99SDavid Howells extern int install_thread_keyring_to_cred(struct cred *);
126d84f4f99SDavid Howells extern int install_process_keyring_to_cred(struct cred *);
1273e30148cSDavid Howells 
1283e30148cSDavid Howells extern struct key *request_key_and_link(struct key_type *type,
1293e30148cSDavid Howells 					const char *description,
1304a38e122SDavid Howells 					const void *callout_info,
1314a38e122SDavid Howells 					size_t callout_len,
1324e54f085SDavid Howells 					void *aux,
1337e047ef5SDavid Howells 					struct key *dest_keyring,
1347e047ef5SDavid Howells 					unsigned long flags);
1353e30148cSDavid Howells 
1365593122eSDavid Howells extern key_ref_t lookup_user_key(key_serial_t id, unsigned long flags,
137e9e349b0SDavid Howells 				 key_perm_t perm);
1385593122eSDavid Howells #define KEY_LOOKUP_CREATE	0x01
1395593122eSDavid Howells #define KEY_LOOKUP_PARTIAL	0x02
1405593122eSDavid Howells #define KEY_LOOKUP_FOR_UNLINK	0x04
141e9e349b0SDavid Howells 
142e9e349b0SDavid Howells extern long join_session_keyring(const char *name);
143e9e349b0SDavid Howells 
1445d135440SDavid Howells extern unsigned key_gc_delay;
1455d135440SDavid Howells extern void keyring_gc(struct key *keyring, time_t limit);
1465d135440SDavid Howells extern void key_schedule_gc(time_t expiry_at);
1475d135440SDavid Howells 
148e9e349b0SDavid Howells /*
149e9e349b0SDavid Howells  * check to see whether permission is granted to use a key in the desired way
150e9e349b0SDavid 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 
155e9e349b0SDavid Howells static inline int key_permission(const key_ref_t key_ref, key_perm_t perm)
156e9e349b0SDavid Howells {
157d84f4f99SDavid Howells 	return key_task_permission(key_ref, current_cred(), perm);
158e9e349b0SDavid Howells }
159e9e349b0SDavid Howells 
160e9e349b0SDavid Howells /* required permissions */
161e9e349b0SDavid Howells #define	KEY_VIEW	0x01	/* require permission to view attributes */
162e9e349b0SDavid Howells #define	KEY_READ	0x02	/* require permission to read content */
163e9e349b0SDavid Howells #define	KEY_WRITE	0x04	/* require permission to update / modify */
164e9e349b0SDavid Howells #define	KEY_SEARCH	0x08	/* require permission to search (keyring) or find (key) */
165e9e349b0SDavid Howells #define	KEY_LINK	0x10	/* require permission to link */
166e9e349b0SDavid Howells #define	KEY_SETATTR	0x20	/* require permission to change attributes */
167e9e349b0SDavid Howells #define	KEY_ALL		0x3f	/* all the above permissions */
168e9e349b0SDavid Howells 
1693e30148cSDavid Howells /*
1703e30148cSDavid Howells  * request_key authorisation
1713e30148cSDavid Howells  */
1723e30148cSDavid Howells struct request_key_auth {
1733e30148cSDavid Howells 	struct key		*target_key;
1748bbf4976SDavid Howells 	struct key		*dest_keyring;
175d84f4f99SDavid Howells 	const struct cred	*cred;
1764a38e122SDavid Howells 	void			*callout_info;
1774a38e122SDavid Howells 	size_t			callout_len;
1783e30148cSDavid Howells 	pid_t			pid;
1793e30148cSDavid Howells };
1803e30148cSDavid Howells 
1813e30148cSDavid Howells extern struct key_type key_type_request_key_auth;
1823e30148cSDavid Howells extern struct key *request_key_auth_new(struct key *target,
1834a38e122SDavid Howells 					const void *callout_info,
1848bbf4976SDavid Howells 					size_t callout_len,
1858bbf4976SDavid Howells 					struct key *dest_keyring);
1863e30148cSDavid Howells 
1873e30148cSDavid Howells extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  * keyctl functions
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds extern long keyctl_get_keyring_ID(key_serial_t, int);
1931da177e4SLinus Torvalds extern long keyctl_join_session_keyring(const char __user *);
1941da177e4SLinus Torvalds extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
1951da177e4SLinus Torvalds extern long keyctl_revoke_key(key_serial_t);
1961da177e4SLinus Torvalds extern long keyctl_keyring_clear(key_serial_t);
1971da177e4SLinus Torvalds extern long keyctl_keyring_link(key_serial_t, key_serial_t);
1981da177e4SLinus Torvalds extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
1991da177e4SLinus Torvalds extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
2001da177e4SLinus Torvalds extern long keyctl_keyring_search(key_serial_t, const char __user *,
2011da177e4SLinus Torvalds 				  const char __user *, key_serial_t);
2021da177e4SLinus Torvalds extern long keyctl_read_key(key_serial_t, char __user *, size_t);
2031da177e4SLinus Torvalds extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
2041da177e4SLinus Torvalds extern long keyctl_setperm_key(key_serial_t, key_perm_t);
2051da177e4SLinus Torvalds extern long keyctl_instantiate_key(key_serial_t, const void __user *,
2061da177e4SLinus Torvalds 				   size_t, key_serial_t);
2071da177e4SLinus Torvalds extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
2083e30148cSDavid Howells extern long keyctl_set_reqkey_keyring(int);
209017679c4SDavid Howells extern long keyctl_set_timeout(key_serial_t, unsigned);
210b5f545c8SDavid Howells extern long keyctl_assume_authority(key_serial_t);
21170a5bb72SDavid Howells extern long keyctl_get_security(key_serial_t keyid, char __user *buffer,
21270a5bb72SDavid Howells 				size_t buflen);
213ee18d64cSDavid Howells extern long keyctl_session_to_parent(void);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds /*
2161da177e4SLinus Torvalds  * debugging key validation
2171da177e4SLinus Torvalds  */
2181da177e4SLinus Torvalds #ifdef KEY_DEBUGGING
2191da177e4SLinus Torvalds extern void __key_check(const struct key *);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static inline void key_check(const struct key *key)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
2241da177e4SLinus Torvalds 		__key_check(key);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds #else
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #define key_check(key) do {} while(0)
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds #endif
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds #endif /* _INTERNAL_H */
234