xref: /linux/security/keys/internal.h (revision d67b569f5f620c0fb95d5212642746b7ba9d29e4)
1 /* internal.h: authentication token and access key management internal defs
2  *
3  * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #ifndef _INTERNAL_H
13 #define _INTERNAL_H
14 
15 #include <linux/key.h>
16 #include <linux/key-ui.h>
17 
18 #if 0
19 #define kenter(FMT, a...)	printk("==> %s("FMT")\n",__FUNCTION__ , ## a)
20 #define kleave(FMT, a...)	printk("<== %s()"FMT"\n",__FUNCTION__ , ## a)
21 #define kdebug(FMT, a...)	printk(FMT"\n" , ## a)
22 #else
23 #define kenter(FMT, a...)	do {} while(0)
24 #define kleave(FMT, a...)	do {} while(0)
25 #define kdebug(FMT, a...)	do {} while(0)
26 #endif
27 
28 extern struct key_type key_type_dead;
29 extern struct key_type key_type_user;
30 
31 /*****************************************************************************/
32 /*
33  * keep track of keys for a user
34  * - this needs to be separate to user_struct to avoid a refcount-loop
35  *   (user_struct pins some keyrings which pin this struct)
36  * - this also keeps track of keys under request from userspace for this UID
37  */
38 struct key_user {
39 	struct rb_node		node;
40 	struct list_head	consq;		/* construction queue */
41 	spinlock_t		lock;
42 	atomic_t		usage;		/* for accessing qnkeys & qnbytes */
43 	atomic_t		nkeys;		/* number of keys */
44 	atomic_t		nikeys;		/* number of instantiated keys */
45 	uid_t			uid;
46 	int			qnkeys;		/* number of keys allocated to this user */
47 	int			qnbytes;	/* number of bytes allocated to this user */
48 };
49 
50 #define KEYQUOTA_MAX_KEYS	100
51 #define KEYQUOTA_MAX_BYTES	10000
52 #define KEYQUOTA_LINK_BYTES	4		/* a link in a keyring is worth 4 bytes */
53 
54 extern struct rb_root	key_user_tree;
55 extern spinlock_t	key_user_lock;
56 extern struct key_user	root_key_user;
57 
58 extern struct key_user *key_user_lookup(uid_t uid);
59 extern void key_user_put(struct key_user *user);
60 
61 
62 
63 extern struct rb_root key_serial_tree;
64 extern spinlock_t key_serial_lock;
65 extern struct semaphore key_alloc_sem;
66 extern struct rw_semaphore key_construction_sem;
67 extern wait_queue_head_t request_key_conswq;
68 
69 
70 extern void keyring_publish_name(struct key *keyring);
71 
72 extern int __key_link(struct key *keyring, struct key *key);
73 
74 extern struct key *__keyring_search_one(struct key *keyring,
75 					const struct key_type *type,
76 					const char *description,
77 					key_perm_t perm);
78 
79 extern struct key *keyring_search_instkey(struct key *keyring,
80 					  key_serial_t target_id);
81 
82 typedef int (*key_match_func_t)(const struct key *, const void *);
83 
84 extern struct key *keyring_search_aux(struct key *keyring,
85 				      struct task_struct *tsk,
86 				      struct key_type *type,
87 				      const void *description,
88 				      key_match_func_t match);
89 
90 extern struct key *search_process_keyrings(struct key_type *type,
91 					   const void *description,
92 					   key_match_func_t match,
93 					   struct task_struct *tsk);
94 
95 extern struct key *find_keyring_by_name(const char *name, key_serial_t bound);
96 
97 extern int install_thread_keyring(struct task_struct *tsk);
98 extern int install_process_keyring(struct task_struct *tsk);
99 
100 extern struct key *request_key_and_link(struct key_type *type,
101 					const char *description,
102 					const char *callout_info,
103 					struct key *dest_keyring);
104 
105 /*
106  * request_key authorisation
107  */
108 struct request_key_auth {
109 	struct key		*target_key;
110 	struct task_struct	*context;
111 	pid_t			pid;
112 };
113 
114 extern struct key_type key_type_request_key_auth;
115 extern struct key *request_key_auth_new(struct key *target,
116 					struct key **_rkakey);
117 
118 extern struct key *key_get_instantiation_authkey(key_serial_t target_id);
119 
120 /*
121  * keyctl functions
122  */
123 extern long keyctl_get_keyring_ID(key_serial_t, int);
124 extern long keyctl_join_session_keyring(const char __user *);
125 extern long keyctl_update_key(key_serial_t, const void __user *, size_t);
126 extern long keyctl_revoke_key(key_serial_t);
127 extern long keyctl_keyring_clear(key_serial_t);
128 extern long keyctl_keyring_link(key_serial_t, key_serial_t);
129 extern long keyctl_keyring_unlink(key_serial_t, key_serial_t);
130 extern long keyctl_describe_key(key_serial_t, char __user *, size_t);
131 extern long keyctl_keyring_search(key_serial_t, const char __user *,
132 				  const char __user *, key_serial_t);
133 extern long keyctl_read_key(key_serial_t, char __user *, size_t);
134 extern long keyctl_chown_key(key_serial_t, uid_t, gid_t);
135 extern long keyctl_setperm_key(key_serial_t, key_perm_t);
136 extern long keyctl_instantiate_key(key_serial_t, const void __user *,
137 				   size_t, key_serial_t);
138 extern long keyctl_negate_key(key_serial_t, unsigned, key_serial_t);
139 extern long keyctl_set_reqkey_keyring(int);
140 
141 
142 /*
143  * debugging key validation
144  */
145 #ifdef KEY_DEBUGGING
146 extern void __key_check(const struct key *);
147 
148 static inline void key_check(const struct key *key)
149 {
150 	if (key && (IS_ERR(key) || key->magic != KEY_DEBUG_MAGIC))
151 		__key_check(key);
152 }
153 
154 #else
155 
156 #define key_check(key) do {} while(0)
157 
158 #endif
159 
160 #endif /* _INTERNAL_H */
161