Commit 59e6b9c11341e3b8ac5925427c903d4eae435bd8

Authored by Bryan Schumaker
Committed by Trond Myklebust
1 parent 0cb3284b53

Created a function for setting timeouts on keys

The keyctl_set_timeout function isn't exported to other parts of the
kernel, but I want to use it for the NFS idmapper.  I already have the
key, but I wanted a generic way to set the timeout.

Signed-off-by: Bryan Schumaker <bjschuma@netapp.com>
Acked-by: David Howells <dhowells@redhat.com>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>

Showing 3 changed files with 24 additions and 16 deletions Inline Diff

1 /* Authentication token and access key management 1 /* Authentication token and access key management
2 * 2 *
3 * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved. 3 * Copyright (C) 2004, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com) 4 * Written by David Howells (dhowells@redhat.com)
5 * 5 *
6 * This program is free software; you can redistribute it and/or 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License 7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version. 9 * 2 of the License, or (at your option) any later version.
10 * 10 *
11 * 11 *
12 * See Documentation/security/keys.txt for information on keys/keyrings. 12 * See Documentation/security/keys.txt for information on keys/keyrings.
13 */ 13 */
14 14
15 #ifndef _LINUX_KEY_H 15 #ifndef _LINUX_KEY_H
16 #define _LINUX_KEY_H 16 #define _LINUX_KEY_H
17 17
18 #include <linux/types.h> 18 #include <linux/types.h>
19 #include <linux/list.h> 19 #include <linux/list.h>
20 #include <linux/rbtree.h> 20 #include <linux/rbtree.h>
21 #include <linux/rcupdate.h> 21 #include <linux/rcupdate.h>
22 #include <linux/sysctl.h> 22 #include <linux/sysctl.h>
23 #include <linux/rwsem.h> 23 #include <linux/rwsem.h>
24 #include <linux/atomic.h> 24 #include <linux/atomic.h>
25 25
26 #ifdef __KERNEL__ 26 #ifdef __KERNEL__
27 27
28 /* key handle serial number */ 28 /* key handle serial number */
29 typedef int32_t key_serial_t; 29 typedef int32_t key_serial_t;
30 30
31 /* key handle permissions mask */ 31 /* key handle permissions mask */
32 typedef uint32_t key_perm_t; 32 typedef uint32_t key_perm_t;
33 33
34 struct key; 34 struct key;
35 35
36 #ifdef CONFIG_KEYS 36 #ifdef CONFIG_KEYS
37 37
38 #undef KEY_DEBUGGING 38 #undef KEY_DEBUGGING
39 39
40 #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */ 40 #define KEY_POS_VIEW 0x01000000 /* possessor can view a key's attributes */
41 #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */ 41 #define KEY_POS_READ 0x02000000 /* possessor can read key payload / view keyring */
42 #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */ 42 #define KEY_POS_WRITE 0x04000000 /* possessor can update key payload / add link to keyring */
43 #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */ 43 #define KEY_POS_SEARCH 0x08000000 /* possessor can find a key in search / search a keyring */
44 #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */ 44 #define KEY_POS_LINK 0x10000000 /* possessor can create a link to a key/keyring */
45 #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */ 45 #define KEY_POS_SETATTR 0x20000000 /* possessor can set key attributes */
46 #define KEY_POS_ALL 0x3f000000 46 #define KEY_POS_ALL 0x3f000000
47 47
48 #define KEY_USR_VIEW 0x00010000 /* user permissions... */ 48 #define KEY_USR_VIEW 0x00010000 /* user permissions... */
49 #define KEY_USR_READ 0x00020000 49 #define KEY_USR_READ 0x00020000
50 #define KEY_USR_WRITE 0x00040000 50 #define KEY_USR_WRITE 0x00040000
51 #define KEY_USR_SEARCH 0x00080000 51 #define KEY_USR_SEARCH 0x00080000
52 #define KEY_USR_LINK 0x00100000 52 #define KEY_USR_LINK 0x00100000
53 #define KEY_USR_SETATTR 0x00200000 53 #define KEY_USR_SETATTR 0x00200000
54 #define KEY_USR_ALL 0x003f0000 54 #define KEY_USR_ALL 0x003f0000
55 55
56 #define KEY_GRP_VIEW 0x00000100 /* group permissions... */ 56 #define KEY_GRP_VIEW 0x00000100 /* group permissions... */
57 #define KEY_GRP_READ 0x00000200 57 #define KEY_GRP_READ 0x00000200
58 #define KEY_GRP_WRITE 0x00000400 58 #define KEY_GRP_WRITE 0x00000400
59 #define KEY_GRP_SEARCH 0x00000800 59 #define KEY_GRP_SEARCH 0x00000800
60 #define KEY_GRP_LINK 0x00001000 60 #define KEY_GRP_LINK 0x00001000
61 #define KEY_GRP_SETATTR 0x00002000 61 #define KEY_GRP_SETATTR 0x00002000
62 #define KEY_GRP_ALL 0x00003f00 62 #define KEY_GRP_ALL 0x00003f00
63 63
64 #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */ 64 #define KEY_OTH_VIEW 0x00000001 /* third party permissions... */
65 #define KEY_OTH_READ 0x00000002 65 #define KEY_OTH_READ 0x00000002
66 #define KEY_OTH_WRITE 0x00000004 66 #define KEY_OTH_WRITE 0x00000004
67 #define KEY_OTH_SEARCH 0x00000008 67 #define KEY_OTH_SEARCH 0x00000008
68 #define KEY_OTH_LINK 0x00000010 68 #define KEY_OTH_LINK 0x00000010
69 #define KEY_OTH_SETATTR 0x00000020 69 #define KEY_OTH_SETATTR 0x00000020
70 #define KEY_OTH_ALL 0x0000003f 70 #define KEY_OTH_ALL 0x0000003f
71 71
72 #define KEY_PERM_UNDEF 0xffffffff 72 #define KEY_PERM_UNDEF 0xffffffff
73 73
74 struct seq_file; 74 struct seq_file;
75 struct user_struct; 75 struct user_struct;
76 struct signal_struct; 76 struct signal_struct;
77 struct cred; 77 struct cred;
78 78
79 struct key_type; 79 struct key_type;
80 struct key_owner; 80 struct key_owner;
81 struct keyring_list; 81 struct keyring_list;
82 struct keyring_name; 82 struct keyring_name;
83 83
84 /*****************************************************************************/ 84 /*****************************************************************************/
85 /* 85 /*
86 * key reference with possession attribute handling 86 * key reference with possession attribute handling
87 * 87 *
88 * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually 88 * NOTE! key_ref_t is a typedef'd pointer to a type that is not actually
89 * defined. This is because we abuse the bottom bit of the reference to carry a 89 * defined. This is because we abuse the bottom bit of the reference to carry a
90 * flag to indicate whether the calling process possesses that key in one of 90 * flag to indicate whether the calling process possesses that key in one of
91 * its keyrings. 91 * its keyrings.
92 * 92 *
93 * the key_ref_t has been made a separate type so that the compiler can reject 93 * the key_ref_t has been made a separate type so that the compiler can reject
94 * attempts to dereference it without proper conversion. 94 * attempts to dereference it without proper conversion.
95 * 95 *
96 * the three functions are used to assemble and disassemble references 96 * the three functions are used to assemble and disassemble references
97 */ 97 */
98 typedef struct __key_reference_with_attributes *key_ref_t; 98 typedef struct __key_reference_with_attributes *key_ref_t;
99 99
100 static inline key_ref_t make_key_ref(const struct key *key, 100 static inline key_ref_t make_key_ref(const struct key *key,
101 unsigned long possession) 101 unsigned long possession)
102 { 102 {
103 return (key_ref_t) ((unsigned long) key | possession); 103 return (key_ref_t) ((unsigned long) key | possession);
104 } 104 }
105 105
106 static inline struct key *key_ref_to_ptr(const key_ref_t key_ref) 106 static inline struct key *key_ref_to_ptr(const key_ref_t key_ref)
107 { 107 {
108 return (struct key *) ((unsigned long) key_ref & ~1UL); 108 return (struct key *) ((unsigned long) key_ref & ~1UL);
109 } 109 }
110 110
111 static inline unsigned long is_key_possessed(const key_ref_t key_ref) 111 static inline unsigned long is_key_possessed(const key_ref_t key_ref)
112 { 112 {
113 return (unsigned long) key_ref & 1UL; 113 return (unsigned long) key_ref & 1UL;
114 } 114 }
115 115
116 /*****************************************************************************/ 116 /*****************************************************************************/
117 /* 117 /*
118 * authentication token / access credential / keyring 118 * authentication token / access credential / keyring
119 * - types of key include: 119 * - types of key include:
120 * - keyrings 120 * - keyrings
121 * - disk encryption IDs 121 * - disk encryption IDs
122 * - Kerberos TGTs and tickets 122 * - Kerberos TGTs and tickets
123 */ 123 */
124 struct key { 124 struct key {
125 atomic_t usage; /* number of references */ 125 atomic_t usage; /* number of references */
126 key_serial_t serial; /* key serial number */ 126 key_serial_t serial; /* key serial number */
127 struct rb_node serial_node; 127 struct rb_node serial_node;
128 struct key_type *type; /* type of key */ 128 struct key_type *type; /* type of key */
129 struct rw_semaphore sem; /* change vs change sem */ 129 struct rw_semaphore sem; /* change vs change sem */
130 struct key_user *user; /* owner of this key */ 130 struct key_user *user; /* owner of this key */
131 void *security; /* security data for this key */ 131 void *security; /* security data for this key */
132 union { 132 union {
133 time_t expiry; /* time at which key expires (or 0) */ 133 time_t expiry; /* time at which key expires (or 0) */
134 time_t revoked_at; /* time at which key was revoked */ 134 time_t revoked_at; /* time at which key was revoked */
135 }; 135 };
136 uid_t uid; 136 uid_t uid;
137 gid_t gid; 137 gid_t gid;
138 key_perm_t perm; /* access permissions */ 138 key_perm_t perm; /* access permissions */
139 unsigned short quotalen; /* length added to quota */ 139 unsigned short quotalen; /* length added to quota */
140 unsigned short datalen; /* payload data length 140 unsigned short datalen; /* payload data length
141 * - may not match RCU dereferenced payload 141 * - may not match RCU dereferenced payload
142 * - payload should contain own length 142 * - payload should contain own length
143 */ 143 */
144 144
145 #ifdef KEY_DEBUGGING 145 #ifdef KEY_DEBUGGING
146 unsigned magic; 146 unsigned magic;
147 #define KEY_DEBUG_MAGIC 0x18273645u 147 #define KEY_DEBUG_MAGIC 0x18273645u
148 #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu 148 #define KEY_DEBUG_MAGIC_X 0xf8e9dacbu
149 #endif 149 #endif
150 150
151 unsigned long flags; /* status flags (change with bitops) */ 151 unsigned long flags; /* status flags (change with bitops) */
152 #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */ 152 #define KEY_FLAG_INSTANTIATED 0 /* set if key has been instantiated */
153 #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */ 153 #define KEY_FLAG_DEAD 1 /* set if key type has been deleted */
154 #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */ 154 #define KEY_FLAG_REVOKED 2 /* set if key had been revoked */
155 #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */ 155 #define KEY_FLAG_IN_QUOTA 3 /* set if key consumes quota */
156 #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */ 156 #define KEY_FLAG_USER_CONSTRUCT 4 /* set if key is being constructed in userspace */
157 #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */ 157 #define KEY_FLAG_NEGATIVE 5 /* set if key is negative */
158 158
159 /* the description string 159 /* the description string
160 * - this is used to match a key against search criteria 160 * - this is used to match a key against search criteria
161 * - this should be a printable string 161 * - this should be a printable string
162 * - eg: for krb5 AFS, this might be "afs@REDHAT.COM" 162 * - eg: for krb5 AFS, this might be "afs@REDHAT.COM"
163 */ 163 */
164 char *description; 164 char *description;
165 165
166 /* type specific data 166 /* type specific data
167 * - this is used by the keyring type to index the name 167 * - this is used by the keyring type to index the name
168 */ 168 */
169 union { 169 union {
170 struct list_head link; 170 struct list_head link;
171 unsigned long x[2]; 171 unsigned long x[2];
172 void *p[2]; 172 void *p[2];
173 int reject_error; 173 int reject_error;
174 } type_data; 174 } type_data;
175 175
176 /* key data 176 /* key data
177 * - this is used to hold the data actually used in cryptography or 177 * - this is used to hold the data actually used in cryptography or
178 * whatever 178 * whatever
179 */ 179 */
180 union { 180 union {
181 unsigned long value; 181 unsigned long value;
182 void __rcu *rcudata; 182 void __rcu *rcudata;
183 void *data; 183 void *data;
184 struct keyring_list __rcu *subscriptions; 184 struct keyring_list __rcu *subscriptions;
185 } payload; 185 } payload;
186 }; 186 };
187 187
188 extern struct key *key_alloc(struct key_type *type, 188 extern struct key *key_alloc(struct key_type *type,
189 const char *desc, 189 const char *desc,
190 uid_t uid, gid_t gid, 190 uid_t uid, gid_t gid,
191 const struct cred *cred, 191 const struct cred *cred,
192 key_perm_t perm, 192 key_perm_t perm,
193 unsigned long flags); 193 unsigned long flags);
194 194
195 195
196 #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */ 196 #define KEY_ALLOC_IN_QUOTA 0x0000 /* add to quota, reject if would overrun */
197 #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */ 197 #define KEY_ALLOC_QUOTA_OVERRUN 0x0001 /* add to quota, permit even if overrun */
198 #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */ 198 #define KEY_ALLOC_NOT_IN_QUOTA 0x0002 /* not in quota */
199 199
200 extern void key_revoke(struct key *key); 200 extern void key_revoke(struct key *key);
201 extern void key_put(struct key *key); 201 extern void key_put(struct key *key);
202 202
203 static inline struct key *key_get(struct key *key) 203 static inline struct key *key_get(struct key *key)
204 { 204 {
205 if (key) 205 if (key)
206 atomic_inc(&key->usage); 206 atomic_inc(&key->usage);
207 return key; 207 return key;
208 } 208 }
209 209
210 static inline void key_ref_put(key_ref_t key_ref) 210 static inline void key_ref_put(key_ref_t key_ref)
211 { 211 {
212 key_put(key_ref_to_ptr(key_ref)); 212 key_put(key_ref_to_ptr(key_ref));
213 } 213 }
214 214
215 extern struct key *request_key(struct key_type *type, 215 extern struct key *request_key(struct key_type *type,
216 const char *description, 216 const char *description,
217 const char *callout_info); 217 const char *callout_info);
218 218
219 extern struct key *request_key_with_auxdata(struct key_type *type, 219 extern struct key *request_key_with_auxdata(struct key_type *type,
220 const char *description, 220 const char *description,
221 const void *callout_info, 221 const void *callout_info,
222 size_t callout_len, 222 size_t callout_len,
223 void *aux); 223 void *aux);
224 224
225 extern struct key *request_key_async(struct key_type *type, 225 extern struct key *request_key_async(struct key_type *type,
226 const char *description, 226 const char *description,
227 const void *callout_info, 227 const void *callout_info,
228 size_t callout_len); 228 size_t callout_len);
229 229
230 extern struct key *request_key_async_with_auxdata(struct key_type *type, 230 extern struct key *request_key_async_with_auxdata(struct key_type *type,
231 const char *description, 231 const char *description,
232 const void *callout_info, 232 const void *callout_info,
233 size_t callout_len, 233 size_t callout_len,
234 void *aux); 234 void *aux);
235 235
236 extern int wait_for_key_construction(struct key *key, bool intr); 236 extern int wait_for_key_construction(struct key *key, bool intr);
237 237
238 extern int key_validate(struct key *key); 238 extern int key_validate(struct key *key);
239 239
240 extern key_ref_t key_create_or_update(key_ref_t keyring, 240 extern key_ref_t key_create_or_update(key_ref_t keyring,
241 const char *type, 241 const char *type,
242 const char *description, 242 const char *description,
243 const void *payload, 243 const void *payload,
244 size_t plen, 244 size_t plen,
245 key_perm_t perm, 245 key_perm_t perm,
246 unsigned long flags); 246 unsigned long flags);
247 247
248 extern int key_update(key_ref_t key, 248 extern int key_update(key_ref_t key,
249 const void *payload, 249 const void *payload,
250 size_t plen); 250 size_t plen);
251 251
252 extern int key_link(struct key *keyring, 252 extern int key_link(struct key *keyring,
253 struct key *key); 253 struct key *key);
254 254
255 extern int key_unlink(struct key *keyring, 255 extern int key_unlink(struct key *keyring,
256 struct key *key); 256 struct key *key);
257 257
258 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, 258 extern struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid,
259 const struct cred *cred, 259 const struct cred *cred,
260 unsigned long flags, 260 unsigned long flags,
261 struct key *dest); 261 struct key *dest);
262 262
263 extern int keyring_clear(struct key *keyring); 263 extern int keyring_clear(struct key *keyring);
264 264
265 extern key_ref_t keyring_search(key_ref_t keyring, 265 extern key_ref_t keyring_search(key_ref_t keyring,
266 struct key_type *type, 266 struct key_type *type,
267 const char *description); 267 const char *description);
268 268
269 extern int keyring_add_key(struct key *keyring, 269 extern int keyring_add_key(struct key *keyring,
270 struct key *key); 270 struct key *key);
271 271
272 extern struct key *key_lookup(key_serial_t id); 272 extern struct key *key_lookup(key_serial_t id);
273 273
274 static inline key_serial_t key_serial(const struct key *key) 274 static inline key_serial_t key_serial(const struct key *key)
275 { 275 {
276 return key ? key->serial : 0; 276 return key ? key->serial : 0;
277 } 277 }
278 278
279 extern void key_set_timeout(struct key *, unsigned);
280
279 /** 281 /**
280 * key_is_instantiated - Determine if a key has been positively instantiated 282 * key_is_instantiated - Determine if a key has been positively instantiated
281 * @key: The key to check. 283 * @key: The key to check.
282 * 284 *
283 * Return true if the specified key has been positively instantiated, false 285 * Return true if the specified key has been positively instantiated, false
284 * otherwise. 286 * otherwise.
285 */ 287 */
286 static inline bool key_is_instantiated(const struct key *key) 288 static inline bool key_is_instantiated(const struct key *key)
287 { 289 {
288 return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) && 290 return test_bit(KEY_FLAG_INSTANTIATED, &key->flags) &&
289 !test_bit(KEY_FLAG_NEGATIVE, &key->flags); 291 !test_bit(KEY_FLAG_NEGATIVE, &key->flags);
290 } 292 }
291 293
292 #define rcu_dereference_key(KEY) \ 294 #define rcu_dereference_key(KEY) \
293 (rcu_dereference_protected((KEY)->payload.rcudata, \ 295 (rcu_dereference_protected((KEY)->payload.rcudata, \
294 rwsem_is_locked(&((struct key *)(KEY))->sem))) 296 rwsem_is_locked(&((struct key *)(KEY))->sem)))
295 297
296 #define rcu_assign_keypointer(KEY, PAYLOAD) \ 298 #define rcu_assign_keypointer(KEY, PAYLOAD) \
297 (rcu_assign_pointer((KEY)->payload.rcudata, PAYLOAD)) 299 (rcu_assign_pointer((KEY)->payload.rcudata, PAYLOAD))
298 300
299 #ifdef CONFIG_SYSCTL 301 #ifdef CONFIG_SYSCTL
300 extern ctl_table key_sysctls[]; 302 extern ctl_table key_sysctls[];
301 #endif 303 #endif
302 304
303 extern void key_replace_session_keyring(void); 305 extern void key_replace_session_keyring(void);
304 306
305 /* 307 /*
306 * the userspace interface 308 * the userspace interface
307 */ 309 */
308 extern int install_thread_keyring_to_cred(struct cred *cred); 310 extern int install_thread_keyring_to_cred(struct cred *cred);
309 extern void key_fsuid_changed(struct task_struct *tsk); 311 extern void key_fsuid_changed(struct task_struct *tsk);
310 extern void key_fsgid_changed(struct task_struct *tsk); 312 extern void key_fsgid_changed(struct task_struct *tsk);
311 extern void key_init(void); 313 extern void key_init(void);
312 314
313 #else /* CONFIG_KEYS */ 315 #else /* CONFIG_KEYS */
314 316
315 #define key_validate(k) 0 317 #define key_validate(k) 0
316 #define key_serial(k) 0 318 #define key_serial(k) 0
317 #define key_get(k) ({ NULL; }) 319 #define key_get(k) ({ NULL; })
318 #define key_revoke(k) do { } while(0) 320 #define key_revoke(k) do { } while(0)
319 #define key_put(k) do { } while(0) 321 #define key_put(k) do { } while(0)
320 #define key_ref_put(k) do { } while(0) 322 #define key_ref_put(k) do { } while(0)
321 #define make_key_ref(k, p) NULL 323 #define make_key_ref(k, p) NULL
322 #define key_ref_to_ptr(k) NULL 324 #define key_ref_to_ptr(k) NULL
323 #define is_key_possessed(k) 0 325 #define is_key_possessed(k) 0
324 #define key_fsuid_changed(t) do { } while(0) 326 #define key_fsuid_changed(t) do { } while(0)
325 #define key_fsgid_changed(t) do { } while(0) 327 #define key_fsgid_changed(t) do { } while(0)
326 #define key_init() do { } while(0) 328 #define key_init() do { } while(0)
327 #define key_replace_session_keyring() do { } while(0) 329 #define key_replace_session_keyring() do { } while(0)
328 330
329 #endif /* CONFIG_KEYS */ 331 #endif /* CONFIG_KEYS */
330 #endif /* __KERNEL__ */ 332 #endif /* __KERNEL__ */
331 #endif /* _LINUX_KEY_H */ 333 #endif /* _LINUX_KEY_H */
332 334
1 /* Basic authentication token and access key management 1 /* Basic authentication token and access key management
2 * 2 *
3 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved. 3 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com) 4 * Written by David Howells (dhowells@redhat.com)
5 * 5 *
6 * This program is free software; you can redistribute it and/or 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License 7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version. 9 * 2 of the License, or (at your option) any later version.
10 */ 10 */
11 11
12 #include <linux/module.h> 12 #include <linux/module.h>
13 #include <linux/init.h> 13 #include <linux/init.h>
14 #include <linux/poison.h> 14 #include <linux/poison.h>
15 #include <linux/sched.h> 15 #include <linux/sched.h>
16 #include <linux/slab.h> 16 #include <linux/slab.h>
17 #include <linux/security.h> 17 #include <linux/security.h>
18 #include <linux/workqueue.h> 18 #include <linux/workqueue.h>
19 #include <linux/random.h> 19 #include <linux/random.h>
20 #include <linux/err.h> 20 #include <linux/err.h>
21 #include <linux/user_namespace.h> 21 #include <linux/user_namespace.h>
22 #include "internal.h" 22 #include "internal.h"
23 23
24 struct kmem_cache *key_jar; 24 struct kmem_cache *key_jar;
25 struct rb_root key_serial_tree; /* tree of keys indexed by serial */ 25 struct rb_root key_serial_tree; /* tree of keys indexed by serial */
26 DEFINE_SPINLOCK(key_serial_lock); 26 DEFINE_SPINLOCK(key_serial_lock);
27 27
28 struct rb_root key_user_tree; /* tree of quota records indexed by UID */ 28 struct rb_root key_user_tree; /* tree of quota records indexed by UID */
29 DEFINE_SPINLOCK(key_user_lock); 29 DEFINE_SPINLOCK(key_user_lock);
30 30
31 unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */ 31 unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */
32 unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */ 32 unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */
33 unsigned int key_quota_maxkeys = 200; /* general key count quota */ 33 unsigned int key_quota_maxkeys = 200; /* general key count quota */
34 unsigned int key_quota_maxbytes = 20000; /* general key space quota */ 34 unsigned int key_quota_maxbytes = 20000; /* general key space quota */
35 35
36 static LIST_HEAD(key_types_list); 36 static LIST_HEAD(key_types_list);
37 static DECLARE_RWSEM(key_types_sem); 37 static DECLARE_RWSEM(key_types_sem);
38 38
39 /* We serialise key instantiation and link */ 39 /* We serialise key instantiation and link */
40 DEFINE_MUTEX(key_construction_mutex); 40 DEFINE_MUTEX(key_construction_mutex);
41 41
42 #ifdef KEY_DEBUGGING 42 #ifdef KEY_DEBUGGING
43 void __key_check(const struct key *key) 43 void __key_check(const struct key *key)
44 { 44 {
45 printk("__key_check: key %p {%08x} should be {%08x}\n", 45 printk("__key_check: key %p {%08x} should be {%08x}\n",
46 key, key->magic, KEY_DEBUG_MAGIC); 46 key, key->magic, KEY_DEBUG_MAGIC);
47 BUG(); 47 BUG();
48 } 48 }
49 #endif 49 #endif
50 50
51 /* 51 /*
52 * Get the key quota record for a user, allocating a new record if one doesn't 52 * Get the key quota record for a user, allocating a new record if one doesn't
53 * already exist. 53 * already exist.
54 */ 54 */
55 struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns) 55 struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
56 { 56 {
57 struct key_user *candidate = NULL, *user; 57 struct key_user *candidate = NULL, *user;
58 struct rb_node *parent = NULL; 58 struct rb_node *parent = NULL;
59 struct rb_node **p; 59 struct rb_node **p;
60 60
61 try_again: 61 try_again:
62 p = &key_user_tree.rb_node; 62 p = &key_user_tree.rb_node;
63 spin_lock(&key_user_lock); 63 spin_lock(&key_user_lock);
64 64
65 /* search the tree for a user record with a matching UID */ 65 /* search the tree for a user record with a matching UID */
66 while (*p) { 66 while (*p) {
67 parent = *p; 67 parent = *p;
68 user = rb_entry(parent, struct key_user, node); 68 user = rb_entry(parent, struct key_user, node);
69 69
70 if (uid < user->uid) 70 if (uid < user->uid)
71 p = &(*p)->rb_left; 71 p = &(*p)->rb_left;
72 else if (uid > user->uid) 72 else if (uid > user->uid)
73 p = &(*p)->rb_right; 73 p = &(*p)->rb_right;
74 else if (user_ns < user->user_ns) 74 else if (user_ns < user->user_ns)
75 p = &(*p)->rb_left; 75 p = &(*p)->rb_left;
76 else if (user_ns > user->user_ns) 76 else if (user_ns > user->user_ns)
77 p = &(*p)->rb_right; 77 p = &(*p)->rb_right;
78 else 78 else
79 goto found; 79 goto found;
80 } 80 }
81 81
82 /* if we get here, we failed to find a match in the tree */ 82 /* if we get here, we failed to find a match in the tree */
83 if (!candidate) { 83 if (!candidate) {
84 /* allocate a candidate user record if we don't already have 84 /* allocate a candidate user record if we don't already have
85 * one */ 85 * one */
86 spin_unlock(&key_user_lock); 86 spin_unlock(&key_user_lock);
87 87
88 user = NULL; 88 user = NULL;
89 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL); 89 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
90 if (unlikely(!candidate)) 90 if (unlikely(!candidate))
91 goto out; 91 goto out;
92 92
93 /* the allocation may have scheduled, so we need to repeat the 93 /* the allocation may have scheduled, so we need to repeat the
94 * search lest someone else added the record whilst we were 94 * search lest someone else added the record whilst we were
95 * asleep */ 95 * asleep */
96 goto try_again; 96 goto try_again;
97 } 97 }
98 98
99 /* if we get here, then the user record still hadn't appeared on the 99 /* if we get here, then the user record still hadn't appeared on the
100 * second pass - so we use the candidate record */ 100 * second pass - so we use the candidate record */
101 atomic_set(&candidate->usage, 1); 101 atomic_set(&candidate->usage, 1);
102 atomic_set(&candidate->nkeys, 0); 102 atomic_set(&candidate->nkeys, 0);
103 atomic_set(&candidate->nikeys, 0); 103 atomic_set(&candidate->nikeys, 0);
104 candidate->uid = uid; 104 candidate->uid = uid;
105 candidate->user_ns = get_user_ns(user_ns); 105 candidate->user_ns = get_user_ns(user_ns);
106 candidate->qnkeys = 0; 106 candidate->qnkeys = 0;
107 candidate->qnbytes = 0; 107 candidate->qnbytes = 0;
108 spin_lock_init(&candidate->lock); 108 spin_lock_init(&candidate->lock);
109 mutex_init(&candidate->cons_lock); 109 mutex_init(&candidate->cons_lock);
110 110
111 rb_link_node(&candidate->node, parent, p); 111 rb_link_node(&candidate->node, parent, p);
112 rb_insert_color(&candidate->node, &key_user_tree); 112 rb_insert_color(&candidate->node, &key_user_tree);
113 spin_unlock(&key_user_lock); 113 spin_unlock(&key_user_lock);
114 user = candidate; 114 user = candidate;
115 goto out; 115 goto out;
116 116
117 /* okay - we found a user record for this UID */ 117 /* okay - we found a user record for this UID */
118 found: 118 found:
119 atomic_inc(&user->usage); 119 atomic_inc(&user->usage);
120 spin_unlock(&key_user_lock); 120 spin_unlock(&key_user_lock);
121 kfree(candidate); 121 kfree(candidate);
122 out: 122 out:
123 return user; 123 return user;
124 } 124 }
125 125
126 /* 126 /*
127 * Dispose of a user structure 127 * Dispose of a user structure
128 */ 128 */
129 void key_user_put(struct key_user *user) 129 void key_user_put(struct key_user *user)
130 { 130 {
131 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) { 131 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
132 rb_erase(&user->node, &key_user_tree); 132 rb_erase(&user->node, &key_user_tree);
133 spin_unlock(&key_user_lock); 133 spin_unlock(&key_user_lock);
134 put_user_ns(user->user_ns); 134 put_user_ns(user->user_ns);
135 135
136 kfree(user); 136 kfree(user);
137 } 137 }
138 } 138 }
139 139
140 /* 140 /*
141 * Allocate a serial number for a key. These are assigned randomly to avoid 141 * Allocate a serial number for a key. These are assigned randomly to avoid
142 * security issues through covert channel problems. 142 * security issues through covert channel problems.
143 */ 143 */
144 static inline void key_alloc_serial(struct key *key) 144 static inline void key_alloc_serial(struct key *key)
145 { 145 {
146 struct rb_node *parent, **p; 146 struct rb_node *parent, **p;
147 struct key *xkey; 147 struct key *xkey;
148 148
149 /* propose a random serial number and look for a hole for it in the 149 /* propose a random serial number and look for a hole for it in the
150 * serial number tree */ 150 * serial number tree */
151 do { 151 do {
152 get_random_bytes(&key->serial, sizeof(key->serial)); 152 get_random_bytes(&key->serial, sizeof(key->serial));
153 153
154 key->serial >>= 1; /* negative numbers are not permitted */ 154 key->serial >>= 1; /* negative numbers are not permitted */
155 } while (key->serial < 3); 155 } while (key->serial < 3);
156 156
157 spin_lock(&key_serial_lock); 157 spin_lock(&key_serial_lock);
158 158
159 attempt_insertion: 159 attempt_insertion:
160 parent = NULL; 160 parent = NULL;
161 p = &key_serial_tree.rb_node; 161 p = &key_serial_tree.rb_node;
162 162
163 while (*p) { 163 while (*p) {
164 parent = *p; 164 parent = *p;
165 xkey = rb_entry(parent, struct key, serial_node); 165 xkey = rb_entry(parent, struct key, serial_node);
166 166
167 if (key->serial < xkey->serial) 167 if (key->serial < xkey->serial)
168 p = &(*p)->rb_left; 168 p = &(*p)->rb_left;
169 else if (key->serial > xkey->serial) 169 else if (key->serial > xkey->serial)
170 p = &(*p)->rb_right; 170 p = &(*p)->rb_right;
171 else 171 else
172 goto serial_exists; 172 goto serial_exists;
173 } 173 }
174 174
175 /* we've found a suitable hole - arrange for this key to occupy it */ 175 /* we've found a suitable hole - arrange for this key to occupy it */
176 rb_link_node(&key->serial_node, parent, p); 176 rb_link_node(&key->serial_node, parent, p);
177 rb_insert_color(&key->serial_node, &key_serial_tree); 177 rb_insert_color(&key->serial_node, &key_serial_tree);
178 178
179 spin_unlock(&key_serial_lock); 179 spin_unlock(&key_serial_lock);
180 return; 180 return;
181 181
182 /* we found a key with the proposed serial number - walk the tree from 182 /* we found a key with the proposed serial number - walk the tree from
183 * that point looking for the next unused serial number */ 183 * that point looking for the next unused serial number */
184 serial_exists: 184 serial_exists:
185 for (;;) { 185 for (;;) {
186 key->serial++; 186 key->serial++;
187 if (key->serial < 3) { 187 if (key->serial < 3) {
188 key->serial = 3; 188 key->serial = 3;
189 goto attempt_insertion; 189 goto attempt_insertion;
190 } 190 }
191 191
192 parent = rb_next(parent); 192 parent = rb_next(parent);
193 if (!parent) 193 if (!parent)
194 goto attempt_insertion; 194 goto attempt_insertion;
195 195
196 xkey = rb_entry(parent, struct key, serial_node); 196 xkey = rb_entry(parent, struct key, serial_node);
197 if (key->serial < xkey->serial) 197 if (key->serial < xkey->serial)
198 goto attempt_insertion; 198 goto attempt_insertion;
199 } 199 }
200 } 200 }
201 201
202 /** 202 /**
203 * key_alloc - Allocate a key of the specified type. 203 * key_alloc - Allocate a key of the specified type.
204 * @type: The type of key to allocate. 204 * @type: The type of key to allocate.
205 * @desc: The key description to allow the key to be searched out. 205 * @desc: The key description to allow the key to be searched out.
206 * @uid: The owner of the new key. 206 * @uid: The owner of the new key.
207 * @gid: The group ID for the new key's group permissions. 207 * @gid: The group ID for the new key's group permissions.
208 * @cred: The credentials specifying UID namespace. 208 * @cred: The credentials specifying UID namespace.
209 * @perm: The permissions mask of the new key. 209 * @perm: The permissions mask of the new key.
210 * @flags: Flags specifying quota properties. 210 * @flags: Flags specifying quota properties.
211 * 211 *
212 * Allocate a key of the specified type with the attributes given. The key is 212 * Allocate a key of the specified type with the attributes given. The key is
213 * returned in an uninstantiated state and the caller needs to instantiate the 213 * returned in an uninstantiated state and the caller needs to instantiate the
214 * key before returning. 214 * key before returning.
215 * 215 *
216 * The user's key count quota is updated to reflect the creation of the key and 216 * The user's key count quota is updated to reflect the creation of the key and
217 * the user's key data quota has the default for the key type reserved. The 217 * the user's key data quota has the default for the key type reserved. The
218 * instantiation function should amend this as necessary. If insufficient 218 * instantiation function should amend this as necessary. If insufficient
219 * quota is available, -EDQUOT will be returned. 219 * quota is available, -EDQUOT will be returned.
220 * 220 *
221 * The LSM security modules can prevent a key being created, in which case 221 * The LSM security modules can prevent a key being created, in which case
222 * -EACCES will be returned. 222 * -EACCES will be returned.
223 * 223 *
224 * Returns a pointer to the new key if successful and an error code otherwise. 224 * Returns a pointer to the new key if successful and an error code otherwise.
225 * 225 *
226 * Note that the caller needs to ensure the key type isn't uninstantiated. 226 * Note that the caller needs to ensure the key type isn't uninstantiated.
227 * Internally this can be done by locking key_types_sem. Externally, this can 227 * Internally this can be done by locking key_types_sem. Externally, this can
228 * be done by either never unregistering the key type, or making sure 228 * be done by either never unregistering the key type, or making sure
229 * key_alloc() calls don't race with module unloading. 229 * key_alloc() calls don't race with module unloading.
230 */ 230 */
231 struct key *key_alloc(struct key_type *type, const char *desc, 231 struct key *key_alloc(struct key_type *type, const char *desc,
232 uid_t uid, gid_t gid, const struct cred *cred, 232 uid_t uid, gid_t gid, const struct cred *cred,
233 key_perm_t perm, unsigned long flags) 233 key_perm_t perm, unsigned long flags)
234 { 234 {
235 struct key_user *user = NULL; 235 struct key_user *user = NULL;
236 struct key *key; 236 struct key *key;
237 size_t desclen, quotalen; 237 size_t desclen, quotalen;
238 int ret; 238 int ret;
239 239
240 key = ERR_PTR(-EINVAL); 240 key = ERR_PTR(-EINVAL);
241 if (!desc || !*desc) 241 if (!desc || !*desc)
242 goto error; 242 goto error;
243 243
244 if (type->vet_description) { 244 if (type->vet_description) {
245 ret = type->vet_description(desc); 245 ret = type->vet_description(desc);
246 if (ret < 0) { 246 if (ret < 0) {
247 key = ERR_PTR(ret); 247 key = ERR_PTR(ret);
248 goto error; 248 goto error;
249 } 249 }
250 } 250 }
251 251
252 desclen = strlen(desc) + 1; 252 desclen = strlen(desc) + 1;
253 quotalen = desclen + type->def_datalen; 253 quotalen = desclen + type->def_datalen;
254 254
255 /* get hold of the key tracking for this user */ 255 /* get hold of the key tracking for this user */
256 user = key_user_lookup(uid, cred->user->user_ns); 256 user = key_user_lookup(uid, cred->user->user_ns);
257 if (!user) 257 if (!user)
258 goto no_memory_1; 258 goto no_memory_1;
259 259
260 /* check that the user's quota permits allocation of another key and 260 /* check that the user's quota permits allocation of another key and
261 * its description */ 261 * its description */
262 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { 262 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
263 unsigned maxkeys = (uid == 0) ? 263 unsigned maxkeys = (uid == 0) ?
264 key_quota_root_maxkeys : key_quota_maxkeys; 264 key_quota_root_maxkeys : key_quota_maxkeys;
265 unsigned maxbytes = (uid == 0) ? 265 unsigned maxbytes = (uid == 0) ?
266 key_quota_root_maxbytes : key_quota_maxbytes; 266 key_quota_root_maxbytes : key_quota_maxbytes;
267 267
268 spin_lock(&user->lock); 268 spin_lock(&user->lock);
269 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) { 269 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
270 if (user->qnkeys + 1 >= maxkeys || 270 if (user->qnkeys + 1 >= maxkeys ||
271 user->qnbytes + quotalen >= maxbytes || 271 user->qnbytes + quotalen >= maxbytes ||
272 user->qnbytes + quotalen < user->qnbytes) 272 user->qnbytes + quotalen < user->qnbytes)
273 goto no_quota; 273 goto no_quota;
274 } 274 }
275 275
276 user->qnkeys++; 276 user->qnkeys++;
277 user->qnbytes += quotalen; 277 user->qnbytes += quotalen;
278 spin_unlock(&user->lock); 278 spin_unlock(&user->lock);
279 } 279 }
280 280
281 /* allocate and initialise the key and its description */ 281 /* allocate and initialise the key and its description */
282 key = kmem_cache_alloc(key_jar, GFP_KERNEL); 282 key = kmem_cache_alloc(key_jar, GFP_KERNEL);
283 if (!key) 283 if (!key)
284 goto no_memory_2; 284 goto no_memory_2;
285 285
286 if (desc) { 286 if (desc) {
287 key->description = kmemdup(desc, desclen, GFP_KERNEL); 287 key->description = kmemdup(desc, desclen, GFP_KERNEL);
288 if (!key->description) 288 if (!key->description)
289 goto no_memory_3; 289 goto no_memory_3;
290 } 290 }
291 291
292 atomic_set(&key->usage, 1); 292 atomic_set(&key->usage, 1);
293 init_rwsem(&key->sem); 293 init_rwsem(&key->sem);
294 lockdep_set_class(&key->sem, &type->lock_class); 294 lockdep_set_class(&key->sem, &type->lock_class);
295 key->type = type; 295 key->type = type;
296 key->user = user; 296 key->user = user;
297 key->quotalen = quotalen; 297 key->quotalen = quotalen;
298 key->datalen = type->def_datalen; 298 key->datalen = type->def_datalen;
299 key->uid = uid; 299 key->uid = uid;
300 key->gid = gid; 300 key->gid = gid;
301 key->perm = perm; 301 key->perm = perm;
302 key->flags = 0; 302 key->flags = 0;
303 key->expiry = 0; 303 key->expiry = 0;
304 key->payload.data = NULL; 304 key->payload.data = NULL;
305 key->security = NULL; 305 key->security = NULL;
306 306
307 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) 307 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
308 key->flags |= 1 << KEY_FLAG_IN_QUOTA; 308 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
309 309
310 memset(&key->type_data, 0, sizeof(key->type_data)); 310 memset(&key->type_data, 0, sizeof(key->type_data));
311 311
312 #ifdef KEY_DEBUGGING 312 #ifdef KEY_DEBUGGING
313 key->magic = KEY_DEBUG_MAGIC; 313 key->magic = KEY_DEBUG_MAGIC;
314 #endif 314 #endif
315 315
316 /* let the security module know about the key */ 316 /* let the security module know about the key */
317 ret = security_key_alloc(key, cred, flags); 317 ret = security_key_alloc(key, cred, flags);
318 if (ret < 0) 318 if (ret < 0)
319 goto security_error; 319 goto security_error;
320 320
321 /* publish the key by giving it a serial number */ 321 /* publish the key by giving it a serial number */
322 atomic_inc(&user->nkeys); 322 atomic_inc(&user->nkeys);
323 key_alloc_serial(key); 323 key_alloc_serial(key);
324 324
325 error: 325 error:
326 return key; 326 return key;
327 327
328 security_error: 328 security_error:
329 kfree(key->description); 329 kfree(key->description);
330 kmem_cache_free(key_jar, key); 330 kmem_cache_free(key_jar, key);
331 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { 331 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
332 spin_lock(&user->lock); 332 spin_lock(&user->lock);
333 user->qnkeys--; 333 user->qnkeys--;
334 user->qnbytes -= quotalen; 334 user->qnbytes -= quotalen;
335 spin_unlock(&user->lock); 335 spin_unlock(&user->lock);
336 } 336 }
337 key_user_put(user); 337 key_user_put(user);
338 key = ERR_PTR(ret); 338 key = ERR_PTR(ret);
339 goto error; 339 goto error;
340 340
341 no_memory_3: 341 no_memory_3:
342 kmem_cache_free(key_jar, key); 342 kmem_cache_free(key_jar, key);
343 no_memory_2: 343 no_memory_2:
344 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { 344 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
345 spin_lock(&user->lock); 345 spin_lock(&user->lock);
346 user->qnkeys--; 346 user->qnkeys--;
347 user->qnbytes -= quotalen; 347 user->qnbytes -= quotalen;
348 spin_unlock(&user->lock); 348 spin_unlock(&user->lock);
349 } 349 }
350 key_user_put(user); 350 key_user_put(user);
351 no_memory_1: 351 no_memory_1:
352 key = ERR_PTR(-ENOMEM); 352 key = ERR_PTR(-ENOMEM);
353 goto error; 353 goto error;
354 354
355 no_quota: 355 no_quota:
356 spin_unlock(&user->lock); 356 spin_unlock(&user->lock);
357 key_user_put(user); 357 key_user_put(user);
358 key = ERR_PTR(-EDQUOT); 358 key = ERR_PTR(-EDQUOT);
359 goto error; 359 goto error;
360 } 360 }
361 EXPORT_SYMBOL(key_alloc); 361 EXPORT_SYMBOL(key_alloc);
362 362
363 /** 363 /**
364 * key_payload_reserve - Adjust data quota reservation for the key's payload 364 * key_payload_reserve - Adjust data quota reservation for the key's payload
365 * @key: The key to make the reservation for. 365 * @key: The key to make the reservation for.
366 * @datalen: The amount of data payload the caller now wants. 366 * @datalen: The amount of data payload the caller now wants.
367 * 367 *
368 * Adjust the amount of the owning user's key data quota that a key reserves. 368 * Adjust the amount of the owning user's key data quota that a key reserves.
369 * If the amount is increased, then -EDQUOT may be returned if there isn't 369 * If the amount is increased, then -EDQUOT may be returned if there isn't
370 * enough free quota available. 370 * enough free quota available.
371 * 371 *
372 * If successful, 0 is returned. 372 * If successful, 0 is returned.
373 */ 373 */
374 int key_payload_reserve(struct key *key, size_t datalen) 374 int key_payload_reserve(struct key *key, size_t datalen)
375 { 375 {
376 int delta = (int)datalen - key->datalen; 376 int delta = (int)datalen - key->datalen;
377 int ret = 0; 377 int ret = 0;
378 378
379 key_check(key); 379 key_check(key);
380 380
381 /* contemplate the quota adjustment */ 381 /* contemplate the quota adjustment */
382 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 382 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
383 unsigned maxbytes = (key->user->uid == 0) ? 383 unsigned maxbytes = (key->user->uid == 0) ?
384 key_quota_root_maxbytes : key_quota_maxbytes; 384 key_quota_root_maxbytes : key_quota_maxbytes;
385 385
386 spin_lock(&key->user->lock); 386 spin_lock(&key->user->lock);
387 387
388 if (delta > 0 && 388 if (delta > 0 &&
389 (key->user->qnbytes + delta >= maxbytes || 389 (key->user->qnbytes + delta >= maxbytes ||
390 key->user->qnbytes + delta < key->user->qnbytes)) { 390 key->user->qnbytes + delta < key->user->qnbytes)) {
391 ret = -EDQUOT; 391 ret = -EDQUOT;
392 } 392 }
393 else { 393 else {
394 key->user->qnbytes += delta; 394 key->user->qnbytes += delta;
395 key->quotalen += delta; 395 key->quotalen += delta;
396 } 396 }
397 spin_unlock(&key->user->lock); 397 spin_unlock(&key->user->lock);
398 } 398 }
399 399
400 /* change the recorded data length if that didn't generate an error */ 400 /* change the recorded data length if that didn't generate an error */
401 if (ret == 0) 401 if (ret == 0)
402 key->datalen = datalen; 402 key->datalen = datalen;
403 403
404 return ret; 404 return ret;
405 } 405 }
406 EXPORT_SYMBOL(key_payload_reserve); 406 EXPORT_SYMBOL(key_payload_reserve);
407 407
408 /* 408 /*
409 * Instantiate a key and link it into the target keyring atomically. Must be 409 * Instantiate a key and link it into the target keyring atomically. Must be
410 * called with the target keyring's semaphore writelocked. The target key's 410 * called with the target keyring's semaphore writelocked. The target key's
411 * semaphore need not be locked as instantiation is serialised by 411 * semaphore need not be locked as instantiation is serialised by
412 * key_construction_mutex. 412 * key_construction_mutex.
413 */ 413 */
414 static int __key_instantiate_and_link(struct key *key, 414 static int __key_instantiate_and_link(struct key *key,
415 const void *data, 415 const void *data,
416 size_t datalen, 416 size_t datalen,
417 struct key *keyring, 417 struct key *keyring,
418 struct key *authkey, 418 struct key *authkey,
419 unsigned long *_prealloc) 419 unsigned long *_prealloc)
420 { 420 {
421 int ret, awaken; 421 int ret, awaken;
422 422
423 key_check(key); 423 key_check(key);
424 key_check(keyring); 424 key_check(keyring);
425 425
426 awaken = 0; 426 awaken = 0;
427 ret = -EBUSY; 427 ret = -EBUSY;
428 428
429 mutex_lock(&key_construction_mutex); 429 mutex_lock(&key_construction_mutex);
430 430
431 /* can't instantiate twice */ 431 /* can't instantiate twice */
432 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 432 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
433 /* instantiate the key */ 433 /* instantiate the key */
434 ret = key->type->instantiate(key, data, datalen); 434 ret = key->type->instantiate(key, data, datalen);
435 435
436 if (ret == 0) { 436 if (ret == 0) {
437 /* mark the key as being instantiated */ 437 /* mark the key as being instantiated */
438 atomic_inc(&key->user->nikeys); 438 atomic_inc(&key->user->nikeys);
439 set_bit(KEY_FLAG_INSTANTIATED, &key->flags); 439 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
440 440
441 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) 441 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
442 awaken = 1; 442 awaken = 1;
443 443
444 /* and link it into the destination keyring */ 444 /* and link it into the destination keyring */
445 if (keyring) 445 if (keyring)
446 __key_link(keyring, key, _prealloc); 446 __key_link(keyring, key, _prealloc);
447 447
448 /* disable the authorisation key */ 448 /* disable the authorisation key */
449 if (authkey) 449 if (authkey)
450 key_revoke(authkey); 450 key_revoke(authkey);
451 } 451 }
452 } 452 }
453 453
454 mutex_unlock(&key_construction_mutex); 454 mutex_unlock(&key_construction_mutex);
455 455
456 /* wake up anyone waiting for a key to be constructed */ 456 /* wake up anyone waiting for a key to be constructed */
457 if (awaken) 457 if (awaken)
458 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); 458 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
459 459
460 return ret; 460 return ret;
461 } 461 }
462 462
463 /** 463 /**
464 * key_instantiate_and_link - Instantiate a key and link it into the keyring. 464 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
465 * @key: The key to instantiate. 465 * @key: The key to instantiate.
466 * @data: The data to use to instantiate the keyring. 466 * @data: The data to use to instantiate the keyring.
467 * @datalen: The length of @data. 467 * @datalen: The length of @data.
468 * @keyring: Keyring to create a link in on success (or NULL). 468 * @keyring: Keyring to create a link in on success (or NULL).
469 * @authkey: The authorisation token permitting instantiation. 469 * @authkey: The authorisation token permitting instantiation.
470 * 470 *
471 * Instantiate a key that's in the uninstantiated state using the provided data 471 * Instantiate a key that's in the uninstantiated state using the provided data
472 * and, if successful, link it in to the destination keyring if one is 472 * and, if successful, link it in to the destination keyring if one is
473 * supplied. 473 * supplied.
474 * 474 *
475 * If successful, 0 is returned, the authorisation token is revoked and anyone 475 * If successful, 0 is returned, the authorisation token is revoked and anyone
476 * waiting for the key is woken up. If the key was already instantiated, 476 * waiting for the key is woken up. If the key was already instantiated,
477 * -EBUSY will be returned. 477 * -EBUSY will be returned.
478 */ 478 */
479 int key_instantiate_and_link(struct key *key, 479 int key_instantiate_and_link(struct key *key,
480 const void *data, 480 const void *data,
481 size_t datalen, 481 size_t datalen,
482 struct key *keyring, 482 struct key *keyring,
483 struct key *authkey) 483 struct key *authkey)
484 { 484 {
485 unsigned long prealloc; 485 unsigned long prealloc;
486 int ret; 486 int ret;
487 487
488 if (keyring) { 488 if (keyring) {
489 ret = __key_link_begin(keyring, key->type, key->description, 489 ret = __key_link_begin(keyring, key->type, key->description,
490 &prealloc); 490 &prealloc);
491 if (ret < 0) 491 if (ret < 0)
492 return ret; 492 return ret;
493 } 493 }
494 494
495 ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey, 495 ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey,
496 &prealloc); 496 &prealloc);
497 497
498 if (keyring) 498 if (keyring)
499 __key_link_end(keyring, key->type, prealloc); 499 __key_link_end(keyring, key->type, prealloc);
500 500
501 return ret; 501 return ret;
502 } 502 }
503 503
504 EXPORT_SYMBOL(key_instantiate_and_link); 504 EXPORT_SYMBOL(key_instantiate_and_link);
505 505
506 /** 506 /**
507 * key_reject_and_link - Negatively instantiate a key and link it into the keyring. 507 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
508 * @key: The key to instantiate. 508 * @key: The key to instantiate.
509 * @timeout: The timeout on the negative key. 509 * @timeout: The timeout on the negative key.
510 * @error: The error to return when the key is hit. 510 * @error: The error to return when the key is hit.
511 * @keyring: Keyring to create a link in on success (or NULL). 511 * @keyring: Keyring to create a link in on success (or NULL).
512 * @authkey: The authorisation token permitting instantiation. 512 * @authkey: The authorisation token permitting instantiation.
513 * 513 *
514 * Negatively instantiate a key that's in the uninstantiated state and, if 514 * Negatively instantiate a key that's in the uninstantiated state and, if
515 * successful, set its timeout and stored error and link it in to the 515 * successful, set its timeout and stored error and link it in to the
516 * destination keyring if one is supplied. The key and any links to the key 516 * destination keyring if one is supplied. The key and any links to the key
517 * will be automatically garbage collected after the timeout expires. 517 * will be automatically garbage collected after the timeout expires.
518 * 518 *
519 * Negative keys are used to rate limit repeated request_key() calls by causing 519 * Negative keys are used to rate limit repeated request_key() calls by causing
520 * them to return the stored error code (typically ENOKEY) until the negative 520 * them to return the stored error code (typically ENOKEY) until the negative
521 * key expires. 521 * key expires.
522 * 522 *
523 * If successful, 0 is returned, the authorisation token is revoked and anyone 523 * If successful, 0 is returned, the authorisation token is revoked and anyone
524 * waiting for the key is woken up. If the key was already instantiated, 524 * waiting for the key is woken up. If the key was already instantiated,
525 * -EBUSY will be returned. 525 * -EBUSY will be returned.
526 */ 526 */
527 int key_reject_and_link(struct key *key, 527 int key_reject_and_link(struct key *key,
528 unsigned timeout, 528 unsigned timeout,
529 unsigned error, 529 unsigned error,
530 struct key *keyring, 530 struct key *keyring,
531 struct key *authkey) 531 struct key *authkey)
532 { 532 {
533 unsigned long prealloc; 533 unsigned long prealloc;
534 struct timespec now; 534 struct timespec now;
535 int ret, awaken, link_ret = 0; 535 int ret, awaken, link_ret = 0;
536 536
537 key_check(key); 537 key_check(key);
538 key_check(keyring); 538 key_check(keyring);
539 539
540 awaken = 0; 540 awaken = 0;
541 ret = -EBUSY; 541 ret = -EBUSY;
542 542
543 if (keyring) 543 if (keyring)
544 link_ret = __key_link_begin(keyring, key->type, 544 link_ret = __key_link_begin(keyring, key->type,
545 key->description, &prealloc); 545 key->description, &prealloc);
546 546
547 mutex_lock(&key_construction_mutex); 547 mutex_lock(&key_construction_mutex);
548 548
549 /* can't instantiate twice */ 549 /* can't instantiate twice */
550 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 550 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
551 /* mark the key as being negatively instantiated */ 551 /* mark the key as being negatively instantiated */
552 atomic_inc(&key->user->nikeys); 552 atomic_inc(&key->user->nikeys);
553 set_bit(KEY_FLAG_NEGATIVE, &key->flags); 553 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
554 set_bit(KEY_FLAG_INSTANTIATED, &key->flags); 554 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
555 key->type_data.reject_error = -error; 555 key->type_data.reject_error = -error;
556 now = current_kernel_time(); 556 now = current_kernel_time();
557 key->expiry = now.tv_sec + timeout; 557 key->expiry = now.tv_sec + timeout;
558 key_schedule_gc(key->expiry + key_gc_delay); 558 key_schedule_gc(key->expiry + key_gc_delay);
559 559
560 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) 560 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
561 awaken = 1; 561 awaken = 1;
562 562
563 ret = 0; 563 ret = 0;
564 564
565 /* and link it into the destination keyring */ 565 /* and link it into the destination keyring */
566 if (keyring && link_ret == 0) 566 if (keyring && link_ret == 0)
567 __key_link(keyring, key, &prealloc); 567 __key_link(keyring, key, &prealloc);
568 568
569 /* disable the authorisation key */ 569 /* disable the authorisation key */
570 if (authkey) 570 if (authkey)
571 key_revoke(authkey); 571 key_revoke(authkey);
572 } 572 }
573 573
574 mutex_unlock(&key_construction_mutex); 574 mutex_unlock(&key_construction_mutex);
575 575
576 if (keyring) 576 if (keyring)
577 __key_link_end(keyring, key->type, prealloc); 577 __key_link_end(keyring, key->type, prealloc);
578 578
579 /* wake up anyone waiting for a key to be constructed */ 579 /* wake up anyone waiting for a key to be constructed */
580 if (awaken) 580 if (awaken)
581 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); 581 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
582 582
583 return ret == 0 ? link_ret : ret; 583 return ret == 0 ? link_ret : ret;
584 } 584 }
585 EXPORT_SYMBOL(key_reject_and_link); 585 EXPORT_SYMBOL(key_reject_and_link);
586 586
587 /** 587 /**
588 * key_put - Discard a reference to a key. 588 * key_put - Discard a reference to a key.
589 * @key: The key to discard a reference from. 589 * @key: The key to discard a reference from.
590 * 590 *
591 * Discard a reference to a key, and when all the references are gone, we 591 * Discard a reference to a key, and when all the references are gone, we
592 * schedule the cleanup task to come and pull it out of the tree in process 592 * schedule the cleanup task to come and pull it out of the tree in process
593 * context at some later time. 593 * context at some later time.
594 */ 594 */
595 void key_put(struct key *key) 595 void key_put(struct key *key)
596 { 596 {
597 if (key) { 597 if (key) {
598 key_check(key); 598 key_check(key);
599 599
600 if (atomic_dec_and_test(&key->usage)) 600 if (atomic_dec_and_test(&key->usage))
601 queue_work(system_nrt_wq, &key_gc_work); 601 queue_work(system_nrt_wq, &key_gc_work);
602 } 602 }
603 } 603 }
604 EXPORT_SYMBOL(key_put); 604 EXPORT_SYMBOL(key_put);
605 605
606 /* 606 /*
607 * Find a key by its serial number. 607 * Find a key by its serial number.
608 */ 608 */
609 struct key *key_lookup(key_serial_t id) 609 struct key *key_lookup(key_serial_t id)
610 { 610 {
611 struct rb_node *n; 611 struct rb_node *n;
612 struct key *key; 612 struct key *key;
613 613
614 spin_lock(&key_serial_lock); 614 spin_lock(&key_serial_lock);
615 615
616 /* search the tree for the specified key */ 616 /* search the tree for the specified key */
617 n = key_serial_tree.rb_node; 617 n = key_serial_tree.rb_node;
618 while (n) { 618 while (n) {
619 key = rb_entry(n, struct key, serial_node); 619 key = rb_entry(n, struct key, serial_node);
620 620
621 if (id < key->serial) 621 if (id < key->serial)
622 n = n->rb_left; 622 n = n->rb_left;
623 else if (id > key->serial) 623 else if (id > key->serial)
624 n = n->rb_right; 624 n = n->rb_right;
625 else 625 else
626 goto found; 626 goto found;
627 } 627 }
628 628
629 not_found: 629 not_found:
630 key = ERR_PTR(-ENOKEY); 630 key = ERR_PTR(-ENOKEY);
631 goto error; 631 goto error;
632 632
633 found: 633 found:
634 /* pretend it doesn't exist if it is awaiting deletion */ 634 /* pretend it doesn't exist if it is awaiting deletion */
635 if (atomic_read(&key->usage) == 0) 635 if (atomic_read(&key->usage) == 0)
636 goto not_found; 636 goto not_found;
637 637
638 /* this races with key_put(), but that doesn't matter since key_put() 638 /* this races with key_put(), but that doesn't matter since key_put()
639 * doesn't actually change the key 639 * doesn't actually change the key
640 */ 640 */
641 atomic_inc(&key->usage); 641 atomic_inc(&key->usage);
642 642
643 error: 643 error:
644 spin_unlock(&key_serial_lock); 644 spin_unlock(&key_serial_lock);
645 return key; 645 return key;
646 } 646 }
647 647
648 /* 648 /*
649 * Find and lock the specified key type against removal. 649 * Find and lock the specified key type against removal.
650 * 650 *
651 * We return with the sem read-locked if successful. If the type wasn't 651 * We return with the sem read-locked if successful. If the type wasn't
652 * available -ENOKEY is returned instead. 652 * available -ENOKEY is returned instead.
653 */ 653 */
654 struct key_type *key_type_lookup(const char *type) 654 struct key_type *key_type_lookup(const char *type)
655 { 655 {
656 struct key_type *ktype; 656 struct key_type *ktype;
657 657
658 down_read(&key_types_sem); 658 down_read(&key_types_sem);
659 659
660 /* look up the key type to see if it's one of the registered kernel 660 /* look up the key type to see if it's one of the registered kernel
661 * types */ 661 * types */
662 list_for_each_entry(ktype, &key_types_list, link) { 662 list_for_each_entry(ktype, &key_types_list, link) {
663 if (strcmp(ktype->name, type) == 0) 663 if (strcmp(ktype->name, type) == 0)
664 goto found_kernel_type; 664 goto found_kernel_type;
665 } 665 }
666 666
667 up_read(&key_types_sem); 667 up_read(&key_types_sem);
668 ktype = ERR_PTR(-ENOKEY); 668 ktype = ERR_PTR(-ENOKEY);
669 669
670 found_kernel_type: 670 found_kernel_type:
671 return ktype; 671 return ktype;
672 } 672 }
673 673
674 void key_set_timeout(struct key *key, unsigned timeout)
675 {
676 struct timespec now;
677 time_t expiry = 0;
678
679 /* make the changes with the locks held to prevent races */
680 down_write(&key->sem);
681
682 if (timeout > 0) {
683 now = current_kernel_time();
684 expiry = now.tv_sec + timeout;
685 }
686
687 key->expiry = expiry;
688 key_schedule_gc(key->expiry + key_gc_delay);
689
690 up_write(&key->sem);
691 }
692 EXPORT_SYMBOL_GPL(key_set_timeout);
693
674 /* 694 /*
675 * Unlock a key type locked by key_type_lookup(). 695 * Unlock a key type locked by key_type_lookup().
676 */ 696 */
677 void key_type_put(struct key_type *ktype) 697 void key_type_put(struct key_type *ktype)
678 { 698 {
679 up_read(&key_types_sem); 699 up_read(&key_types_sem);
680 } 700 }
681 701
682 /* 702 /*
683 * Attempt to update an existing key. 703 * Attempt to update an existing key.
684 * 704 *
685 * The key is given to us with an incremented refcount that we need to discard 705 * The key is given to us with an incremented refcount that we need to discard
686 * if we get an error. 706 * if we get an error.
687 */ 707 */
688 static inline key_ref_t __key_update(key_ref_t key_ref, 708 static inline key_ref_t __key_update(key_ref_t key_ref,
689 const void *payload, size_t plen) 709 const void *payload, size_t plen)
690 { 710 {
691 struct key *key = key_ref_to_ptr(key_ref); 711 struct key *key = key_ref_to_ptr(key_ref);
692 int ret; 712 int ret;
693 713
694 /* need write permission on the key to update it */ 714 /* need write permission on the key to update it */
695 ret = key_permission(key_ref, KEY_WRITE); 715 ret = key_permission(key_ref, KEY_WRITE);
696 if (ret < 0) 716 if (ret < 0)
697 goto error; 717 goto error;
698 718
699 ret = -EEXIST; 719 ret = -EEXIST;
700 if (!key->type->update) 720 if (!key->type->update)
701 goto error; 721 goto error;
702 722
703 down_write(&key->sem); 723 down_write(&key->sem);
704 724
705 ret = key->type->update(key, payload, plen); 725 ret = key->type->update(key, payload, plen);
706 if (ret == 0) 726 if (ret == 0)
707 /* updating a negative key instantiates it */ 727 /* updating a negative key instantiates it */
708 clear_bit(KEY_FLAG_NEGATIVE, &key->flags); 728 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
709 729
710 up_write(&key->sem); 730 up_write(&key->sem);
711 731
712 if (ret < 0) 732 if (ret < 0)
713 goto error; 733 goto error;
714 out: 734 out:
715 return key_ref; 735 return key_ref;
716 736
717 error: 737 error:
718 key_put(key); 738 key_put(key);
719 key_ref = ERR_PTR(ret); 739 key_ref = ERR_PTR(ret);
720 goto out; 740 goto out;
721 } 741 }
722 742
723 /** 743 /**
724 * key_create_or_update - Update or create and instantiate a key. 744 * key_create_or_update - Update or create and instantiate a key.
725 * @keyring_ref: A pointer to the destination keyring with possession flag. 745 * @keyring_ref: A pointer to the destination keyring with possession flag.
726 * @type: The type of key. 746 * @type: The type of key.
727 * @description: The searchable description for the key. 747 * @description: The searchable description for the key.
728 * @payload: The data to use to instantiate or update the key. 748 * @payload: The data to use to instantiate or update the key.
729 * @plen: The length of @payload. 749 * @plen: The length of @payload.
730 * @perm: The permissions mask for a new key. 750 * @perm: The permissions mask for a new key.
731 * @flags: The quota flags for a new key. 751 * @flags: The quota flags for a new key.
732 * 752 *
733 * Search the destination keyring for a key of the same description and if one 753 * Search the destination keyring for a key of the same description and if one
734 * is found, update it, otherwise create and instantiate a new one and create a 754 * is found, update it, otherwise create and instantiate a new one and create a
735 * link to it from that keyring. 755 * link to it from that keyring.
736 * 756 *
737 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be 757 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
738 * concocted. 758 * concocted.
739 * 759 *
740 * Returns a pointer to the new key if successful, -ENODEV if the key type 760 * Returns a pointer to the new key if successful, -ENODEV if the key type
741 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the 761 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
742 * caller isn't permitted to modify the keyring or the LSM did not permit 762 * caller isn't permitted to modify the keyring or the LSM did not permit
743 * creation of the key. 763 * creation of the key.
744 * 764 *
745 * On success, the possession flag from the keyring ref will be tacked on to 765 * On success, the possession flag from the keyring ref will be tacked on to
746 * the key ref before it is returned. 766 * the key ref before it is returned.
747 */ 767 */
748 key_ref_t key_create_or_update(key_ref_t keyring_ref, 768 key_ref_t key_create_or_update(key_ref_t keyring_ref,
749 const char *type, 769 const char *type,
750 const char *description, 770 const char *description,
751 const void *payload, 771 const void *payload,
752 size_t plen, 772 size_t plen,
753 key_perm_t perm, 773 key_perm_t perm,
754 unsigned long flags) 774 unsigned long flags)
755 { 775 {
756 unsigned long prealloc; 776 unsigned long prealloc;
757 const struct cred *cred = current_cred(); 777 const struct cred *cred = current_cred();
758 struct key_type *ktype; 778 struct key_type *ktype;
759 struct key *keyring, *key = NULL; 779 struct key *keyring, *key = NULL;
760 key_ref_t key_ref; 780 key_ref_t key_ref;
761 int ret; 781 int ret;
762 782
763 /* look up the key type to see if it's one of the registered kernel 783 /* look up the key type to see if it's one of the registered kernel
764 * types */ 784 * types */
765 ktype = key_type_lookup(type); 785 ktype = key_type_lookup(type);
766 if (IS_ERR(ktype)) { 786 if (IS_ERR(ktype)) {
767 key_ref = ERR_PTR(-ENODEV); 787 key_ref = ERR_PTR(-ENODEV);
768 goto error; 788 goto error;
769 } 789 }
770 790
771 key_ref = ERR_PTR(-EINVAL); 791 key_ref = ERR_PTR(-EINVAL);
772 if (!ktype->match || !ktype->instantiate) 792 if (!ktype->match || !ktype->instantiate)
773 goto error_2; 793 goto error_2;
774 794
775 keyring = key_ref_to_ptr(keyring_ref); 795 keyring = key_ref_to_ptr(keyring_ref);
776 796
777 key_check(keyring); 797 key_check(keyring);
778 798
779 key_ref = ERR_PTR(-ENOTDIR); 799 key_ref = ERR_PTR(-ENOTDIR);
780 if (keyring->type != &key_type_keyring) 800 if (keyring->type != &key_type_keyring)
781 goto error_2; 801 goto error_2;
782 802
783 ret = __key_link_begin(keyring, ktype, description, &prealloc); 803 ret = __key_link_begin(keyring, ktype, description, &prealloc);
784 if (ret < 0) 804 if (ret < 0)
785 goto error_2; 805 goto error_2;
786 806
787 /* if we're going to allocate a new key, we're going to have 807 /* if we're going to allocate a new key, we're going to have
788 * to modify the keyring */ 808 * to modify the keyring */
789 ret = key_permission(keyring_ref, KEY_WRITE); 809 ret = key_permission(keyring_ref, KEY_WRITE);
790 if (ret < 0) { 810 if (ret < 0) {
791 key_ref = ERR_PTR(ret); 811 key_ref = ERR_PTR(ret);
792 goto error_3; 812 goto error_3;
793 } 813 }
794 814
795 /* if it's possible to update this type of key, search for an existing 815 /* if it's possible to update this type of key, search for an existing
796 * key of the same type and description in the destination keyring and 816 * key of the same type and description in the destination keyring and
797 * update that instead if possible 817 * update that instead if possible
798 */ 818 */
799 if (ktype->update) { 819 if (ktype->update) {
800 key_ref = __keyring_search_one(keyring_ref, ktype, description, 820 key_ref = __keyring_search_one(keyring_ref, ktype, description,
801 0); 821 0);
802 if (!IS_ERR(key_ref)) 822 if (!IS_ERR(key_ref))
803 goto found_matching_key; 823 goto found_matching_key;
804 } 824 }
805 825
806 /* if the client doesn't provide, decide on the permissions we want */ 826 /* if the client doesn't provide, decide on the permissions we want */
807 if (perm == KEY_PERM_UNDEF) { 827 if (perm == KEY_PERM_UNDEF) {
808 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR; 828 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
809 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR; 829 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
810 830
811 if (ktype->read) 831 if (ktype->read)
812 perm |= KEY_POS_READ | KEY_USR_READ; 832 perm |= KEY_POS_READ | KEY_USR_READ;
813 833
814 if (ktype == &key_type_keyring || ktype->update) 834 if (ktype == &key_type_keyring || ktype->update)
815 perm |= KEY_USR_WRITE; 835 perm |= KEY_USR_WRITE;
816 } 836 }
817 837
818 /* allocate a new key */ 838 /* allocate a new key */
819 key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred, 839 key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
820 perm, flags); 840 perm, flags);
821 if (IS_ERR(key)) { 841 if (IS_ERR(key)) {
822 key_ref = ERR_CAST(key); 842 key_ref = ERR_CAST(key);
823 goto error_3; 843 goto error_3;
824 } 844 }
825 845
826 /* instantiate it and link it into the target keyring */ 846 /* instantiate it and link it into the target keyring */
827 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL, 847 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL,
828 &prealloc); 848 &prealloc);
829 if (ret < 0) { 849 if (ret < 0) {
830 key_put(key); 850 key_put(key);
831 key_ref = ERR_PTR(ret); 851 key_ref = ERR_PTR(ret);
832 goto error_3; 852 goto error_3;
833 } 853 }
834 854
835 key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); 855 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
836 856
837 error_3: 857 error_3:
838 __key_link_end(keyring, ktype, prealloc); 858 __key_link_end(keyring, ktype, prealloc);
839 error_2: 859 error_2:
840 key_type_put(ktype); 860 key_type_put(ktype);
841 error: 861 error:
842 return key_ref; 862 return key_ref;
843 863
844 found_matching_key: 864 found_matching_key:
845 /* we found a matching key, so we're going to try to update it 865 /* we found a matching key, so we're going to try to update it
846 * - we can drop the locks first as we have the key pinned 866 * - we can drop the locks first as we have the key pinned
847 */ 867 */
848 __key_link_end(keyring, ktype, prealloc); 868 __key_link_end(keyring, ktype, prealloc);
849 key_type_put(ktype); 869 key_type_put(ktype);
850 870
851 key_ref = __key_update(key_ref, payload, plen); 871 key_ref = __key_update(key_ref, payload, plen);
852 goto error; 872 goto error;
853 } 873 }
854 EXPORT_SYMBOL(key_create_or_update); 874 EXPORT_SYMBOL(key_create_or_update);
855 875
856 /** 876 /**
857 * key_update - Update a key's contents. 877 * key_update - Update a key's contents.
858 * @key_ref: The pointer (plus possession flag) to the key. 878 * @key_ref: The pointer (plus possession flag) to the key.
859 * @payload: The data to be used to update the key. 879 * @payload: The data to be used to update the key.
860 * @plen: The length of @payload. 880 * @plen: The length of @payload.
861 * 881 *
862 * Attempt to update the contents of a key with the given payload data. The 882 * Attempt to update the contents of a key with the given payload data. The
863 * caller must be granted Write permission on the key. Negative keys can be 883 * caller must be granted Write permission on the key. Negative keys can be
864 * instantiated by this method. 884 * instantiated by this method.
865 * 885 *
866 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key 886 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
867 * type does not support updating. The key type may return other errors. 887 * type does not support updating. The key type may return other errors.
868 */ 888 */
869 int key_update(key_ref_t key_ref, const void *payload, size_t plen) 889 int key_update(key_ref_t key_ref, const void *payload, size_t plen)
870 { 890 {
871 struct key *key = key_ref_to_ptr(key_ref); 891 struct key *key = key_ref_to_ptr(key_ref);
872 int ret; 892 int ret;
873 893
874 key_check(key); 894 key_check(key);
875 895
876 /* the key must be writable */ 896 /* the key must be writable */
877 ret = key_permission(key_ref, KEY_WRITE); 897 ret = key_permission(key_ref, KEY_WRITE);
878 if (ret < 0) 898 if (ret < 0)
879 goto error; 899 goto error;
880 900
881 /* attempt to update it if supported */ 901 /* attempt to update it if supported */
882 ret = -EOPNOTSUPP; 902 ret = -EOPNOTSUPP;
883 if (key->type->update) { 903 if (key->type->update) {
884 down_write(&key->sem); 904 down_write(&key->sem);
885 905
886 ret = key->type->update(key, payload, plen); 906 ret = key->type->update(key, payload, plen);
887 if (ret == 0) 907 if (ret == 0)
888 /* updating a negative key instantiates it */ 908 /* updating a negative key instantiates it */
889 clear_bit(KEY_FLAG_NEGATIVE, &key->flags); 909 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
890 910
891 up_write(&key->sem); 911 up_write(&key->sem);
892 } 912 }
893 913
894 error: 914 error:
895 return ret; 915 return ret;
896 } 916 }
897 EXPORT_SYMBOL(key_update); 917 EXPORT_SYMBOL(key_update);
898 918
899 /** 919 /**
900 * key_revoke - Revoke a key. 920 * key_revoke - Revoke a key.
901 * @key: The key to be revoked. 921 * @key: The key to be revoked.
902 * 922 *
903 * Mark a key as being revoked and ask the type to free up its resources. The 923 * Mark a key as being revoked and ask the type to free up its resources. The
904 * revocation timeout is set and the key and all its links will be 924 * revocation timeout is set and the key and all its links will be
905 * automatically garbage collected after key_gc_delay amount of time if they 925 * automatically garbage collected after key_gc_delay amount of time if they
906 * are not manually dealt with first. 926 * are not manually dealt with first.
907 */ 927 */
908 void key_revoke(struct key *key) 928 void key_revoke(struct key *key)
909 { 929 {
910 struct timespec now; 930 struct timespec now;
911 time_t time; 931 time_t time;
912 932
913 key_check(key); 933 key_check(key);
914 934
915 /* make sure no one's trying to change or use the key when we mark it 935 /* make sure no one's trying to change or use the key when we mark it
916 * - we tell lockdep that we might nest because we might be revoking an 936 * - we tell lockdep that we might nest because we might be revoking an
917 * authorisation key whilst holding the sem on a key we've just 937 * authorisation key whilst holding the sem on a key we've just
918 * instantiated 938 * instantiated
919 */ 939 */
920 down_write_nested(&key->sem, 1); 940 down_write_nested(&key->sem, 1);
921 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) && 941 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) &&
922 key->type->revoke) 942 key->type->revoke)
923 key->type->revoke(key); 943 key->type->revoke(key);
924 944
925 /* set the death time to no more than the expiry time */ 945 /* set the death time to no more than the expiry time */
926 now = current_kernel_time(); 946 now = current_kernel_time();
927 time = now.tv_sec; 947 time = now.tv_sec;
928 if (key->revoked_at == 0 || key->revoked_at > time) { 948 if (key->revoked_at == 0 || key->revoked_at > time) {
929 key->revoked_at = time; 949 key->revoked_at = time;
930 key_schedule_gc(key->revoked_at + key_gc_delay); 950 key_schedule_gc(key->revoked_at + key_gc_delay);
931 } 951 }
932 952
933 up_write(&key->sem); 953 up_write(&key->sem);
934 } 954 }
935 EXPORT_SYMBOL(key_revoke); 955 EXPORT_SYMBOL(key_revoke);
936 956
937 /** 957 /**
938 * register_key_type - Register a type of key. 958 * register_key_type - Register a type of key.
939 * @ktype: The new key type. 959 * @ktype: The new key type.
940 * 960 *
941 * Register a new key type. 961 * Register a new key type.
942 * 962 *
943 * Returns 0 on success or -EEXIST if a type of this name already exists. 963 * Returns 0 on success or -EEXIST if a type of this name already exists.
944 */ 964 */
945 int register_key_type(struct key_type *ktype) 965 int register_key_type(struct key_type *ktype)
946 { 966 {
947 struct key_type *p; 967 struct key_type *p;
948 int ret; 968 int ret;
949 969
950 memset(&ktype->lock_class, 0, sizeof(ktype->lock_class)); 970 memset(&ktype->lock_class, 0, sizeof(ktype->lock_class));
951 971
952 ret = -EEXIST; 972 ret = -EEXIST;
953 down_write(&key_types_sem); 973 down_write(&key_types_sem);
954 974
955 /* disallow key types with the same name */ 975 /* disallow key types with the same name */
956 list_for_each_entry(p, &key_types_list, link) { 976 list_for_each_entry(p, &key_types_list, link) {
957 if (strcmp(p->name, ktype->name) == 0) 977 if (strcmp(p->name, ktype->name) == 0)
958 goto out; 978 goto out;
959 } 979 }
960 980
961 /* store the type */ 981 /* store the type */
962 list_add(&ktype->link, &key_types_list); 982 list_add(&ktype->link, &key_types_list);
963 ret = 0; 983 ret = 0;
964 984
965 out: 985 out:
966 up_write(&key_types_sem); 986 up_write(&key_types_sem);
967 return ret; 987 return ret;
968 } 988 }
969 EXPORT_SYMBOL(register_key_type); 989 EXPORT_SYMBOL(register_key_type);
970 990
971 /** 991 /**
972 * unregister_key_type - Unregister a type of key. 992 * unregister_key_type - Unregister a type of key.
973 * @ktype: The key type. 993 * @ktype: The key type.
974 * 994 *
975 * Unregister a key type and mark all the extant keys of this type as dead. 995 * Unregister a key type and mark all the extant keys of this type as dead.
976 * Those keys of this type are then destroyed to get rid of their payloads and 996 * Those keys of this type are then destroyed to get rid of their payloads and
977 * they and their links will be garbage collected as soon as possible. 997 * they and their links will be garbage collected as soon as possible.
978 */ 998 */
979 void unregister_key_type(struct key_type *ktype) 999 void unregister_key_type(struct key_type *ktype)
980 { 1000 {
981 down_write(&key_types_sem); 1001 down_write(&key_types_sem);
982 list_del_init(&ktype->link); 1002 list_del_init(&ktype->link);
983 downgrade_write(&key_types_sem); 1003 downgrade_write(&key_types_sem);
984 key_gc_keytype(ktype); 1004 key_gc_keytype(ktype);
985 up_read(&key_types_sem); 1005 up_read(&key_types_sem);
986 } 1006 }
987 EXPORT_SYMBOL(unregister_key_type); 1007 EXPORT_SYMBOL(unregister_key_type);
988 1008
989 /* 1009 /*
990 * Initialise the key management state. 1010 * Initialise the key management state.
991 */ 1011 */
992 void __init key_init(void) 1012 void __init key_init(void)
993 { 1013 {
994 /* allocate a slab in which we can store keys */ 1014 /* allocate a slab in which we can store keys */
995 key_jar = kmem_cache_create("key_jar", sizeof(struct key), 1015 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
996 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 1016 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
997 1017
998 /* add the special key types */ 1018 /* add the special key types */
999 list_add_tail(&key_type_keyring.link, &key_types_list); 1019 list_add_tail(&key_type_keyring.link, &key_types_list);
1000 list_add_tail(&key_type_dead.link, &key_types_list); 1020 list_add_tail(&key_type_dead.link, &key_types_list);
1001 list_add_tail(&key_type_user.link, &key_types_list); 1021 list_add_tail(&key_type_user.link, &key_types_list);
1002 list_add_tail(&key_type_logon.link, &key_types_list); 1022 list_add_tail(&key_type_logon.link, &key_types_list);
1003 1023
1004 /* record the root user tracking */ 1024 /* record the root user tracking */
1005 rb_link_node(&root_key_user.node, 1025 rb_link_node(&root_key_user.node,
1006 NULL, 1026 NULL,
1007 &key_user_tree.rb_node); 1027 &key_user_tree.rb_node);
1008 1028
1009 rb_insert_color(&root_key_user.node, 1029 rb_insert_color(&root_key_user.node,
1010 &key_user_tree); 1030 &key_user_tree);
1011 } 1031 }
1012 1032
security/keys/keyctl.c
1 /* Userspace key control operations 1 /* Userspace key control operations
2 * 2 *
3 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. 3 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com) 4 * Written by David Howells (dhowells@redhat.com)
5 * 5 *
6 * This program is free software; you can redistribute it and/or 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License 7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version. 9 * 2 of the License, or (at your option) any later version.
10 */ 10 */
11 11
12 #include <linux/module.h> 12 #include <linux/module.h>
13 #include <linux/init.h> 13 #include <linux/init.h>
14 #include <linux/sched.h> 14 #include <linux/sched.h>
15 #include <linux/slab.h> 15 #include <linux/slab.h>
16 #include <linux/syscalls.h> 16 #include <linux/syscalls.h>
17 #include <linux/key.h>
17 #include <linux/keyctl.h> 18 #include <linux/keyctl.h>
18 #include <linux/fs.h> 19 #include <linux/fs.h>
19 #include <linux/capability.h> 20 #include <linux/capability.h>
20 #include <linux/string.h> 21 #include <linux/string.h>
21 #include <linux/err.h> 22 #include <linux/err.h>
22 #include <linux/vmalloc.h> 23 #include <linux/vmalloc.h>
23 #include <linux/security.h> 24 #include <linux/security.h>
24 #include <asm/uaccess.h> 25 #include <asm/uaccess.h>
25 #include "internal.h" 26 #include "internal.h"
26 27
27 static int key_get_type_from_user(char *type, 28 static int key_get_type_from_user(char *type,
28 const char __user *_type, 29 const char __user *_type,
29 unsigned len) 30 unsigned len)
30 { 31 {
31 int ret; 32 int ret;
32 33
33 ret = strncpy_from_user(type, _type, len); 34 ret = strncpy_from_user(type, _type, len);
34 if (ret < 0) 35 if (ret < 0)
35 return ret; 36 return ret;
36 if (ret == 0 || ret >= len) 37 if (ret == 0 || ret >= len)
37 return -EINVAL; 38 return -EINVAL;
38 if (type[0] == '.') 39 if (type[0] == '.')
39 return -EPERM; 40 return -EPERM;
40 type[len - 1] = '\0'; 41 type[len - 1] = '\0';
41 return 0; 42 return 0;
42 } 43 }
43 44
44 /* 45 /*
45 * Extract the description of a new key from userspace and either add it as a 46 * Extract the description of a new key from userspace and either add it as a
46 * new key to the specified keyring or update a matching key in that keyring. 47 * new key to the specified keyring or update a matching key in that keyring.
47 * 48 *
48 * The keyring must be writable so that we can attach the key to it. 49 * The keyring must be writable so that we can attach the key to it.
49 * 50 *
50 * If successful, the new key's serial number is returned, otherwise an error 51 * If successful, the new key's serial number is returned, otherwise an error
51 * code is returned. 52 * code is returned.
52 */ 53 */
53 SYSCALL_DEFINE5(add_key, const char __user *, _type, 54 SYSCALL_DEFINE5(add_key, const char __user *, _type,
54 const char __user *, _description, 55 const char __user *, _description,
55 const void __user *, _payload, 56 const void __user *, _payload,
56 size_t, plen, 57 size_t, plen,
57 key_serial_t, ringid) 58 key_serial_t, ringid)
58 { 59 {
59 key_ref_t keyring_ref, key_ref; 60 key_ref_t keyring_ref, key_ref;
60 char type[32], *description; 61 char type[32], *description;
61 void *payload; 62 void *payload;
62 long ret; 63 long ret;
63 bool vm; 64 bool vm;
64 65
65 ret = -EINVAL; 66 ret = -EINVAL;
66 if (plen > 1024 * 1024 - 1) 67 if (plen > 1024 * 1024 - 1)
67 goto error; 68 goto error;
68 69
69 /* draw all the data into kernel space */ 70 /* draw all the data into kernel space */
70 ret = key_get_type_from_user(type, _type, sizeof(type)); 71 ret = key_get_type_from_user(type, _type, sizeof(type));
71 if (ret < 0) 72 if (ret < 0)
72 goto error; 73 goto error;
73 74
74 description = strndup_user(_description, PAGE_SIZE); 75 description = strndup_user(_description, PAGE_SIZE);
75 if (IS_ERR(description)) { 76 if (IS_ERR(description)) {
76 ret = PTR_ERR(description); 77 ret = PTR_ERR(description);
77 goto error; 78 goto error;
78 } 79 }
79 80
80 /* pull the payload in if one was supplied */ 81 /* pull the payload in if one was supplied */
81 payload = NULL; 82 payload = NULL;
82 83
83 vm = false; 84 vm = false;
84 if (_payload) { 85 if (_payload) {
85 ret = -ENOMEM; 86 ret = -ENOMEM;
86 payload = kmalloc(plen, GFP_KERNEL); 87 payload = kmalloc(plen, GFP_KERNEL);
87 if (!payload) { 88 if (!payload) {
88 if (plen <= PAGE_SIZE) 89 if (plen <= PAGE_SIZE)
89 goto error2; 90 goto error2;
90 vm = true; 91 vm = true;
91 payload = vmalloc(plen); 92 payload = vmalloc(plen);
92 if (!payload) 93 if (!payload)
93 goto error2; 94 goto error2;
94 } 95 }
95 96
96 ret = -EFAULT; 97 ret = -EFAULT;
97 if (copy_from_user(payload, _payload, plen) != 0) 98 if (copy_from_user(payload, _payload, plen) != 0)
98 goto error3; 99 goto error3;
99 } 100 }
100 101
101 /* find the target keyring (which must be writable) */ 102 /* find the target keyring (which must be writable) */
102 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 103 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
103 if (IS_ERR(keyring_ref)) { 104 if (IS_ERR(keyring_ref)) {
104 ret = PTR_ERR(keyring_ref); 105 ret = PTR_ERR(keyring_ref);
105 goto error3; 106 goto error3;
106 } 107 }
107 108
108 /* create or update the requested key and add it to the target 109 /* create or update the requested key and add it to the target
109 * keyring */ 110 * keyring */
110 key_ref = key_create_or_update(keyring_ref, type, description, 111 key_ref = key_create_or_update(keyring_ref, type, description,
111 payload, plen, KEY_PERM_UNDEF, 112 payload, plen, KEY_PERM_UNDEF,
112 KEY_ALLOC_IN_QUOTA); 113 KEY_ALLOC_IN_QUOTA);
113 if (!IS_ERR(key_ref)) { 114 if (!IS_ERR(key_ref)) {
114 ret = key_ref_to_ptr(key_ref)->serial; 115 ret = key_ref_to_ptr(key_ref)->serial;
115 key_ref_put(key_ref); 116 key_ref_put(key_ref);
116 } 117 }
117 else { 118 else {
118 ret = PTR_ERR(key_ref); 119 ret = PTR_ERR(key_ref);
119 } 120 }
120 121
121 key_ref_put(keyring_ref); 122 key_ref_put(keyring_ref);
122 error3: 123 error3:
123 if (!vm) 124 if (!vm)
124 kfree(payload); 125 kfree(payload);
125 else 126 else
126 vfree(payload); 127 vfree(payload);
127 error2: 128 error2:
128 kfree(description); 129 kfree(description);
129 error: 130 error:
130 return ret; 131 return ret;
131 } 132 }
132 133
133 /* 134 /*
134 * Search the process keyrings and keyring trees linked from those for a 135 * Search the process keyrings and keyring trees linked from those for a
135 * matching key. Keyrings must have appropriate Search permission to be 136 * matching key. Keyrings must have appropriate Search permission to be
136 * searched. 137 * searched.
137 * 138 *
138 * If a key is found, it will be attached to the destination keyring if there's 139 * If a key is found, it will be attached to the destination keyring if there's
139 * one specified and the serial number of the key will be returned. 140 * one specified and the serial number of the key will be returned.
140 * 141 *
141 * If no key is found, /sbin/request-key will be invoked if _callout_info is 142 * If no key is found, /sbin/request-key will be invoked if _callout_info is
142 * non-NULL in an attempt to create a key. The _callout_info string will be 143 * non-NULL in an attempt to create a key. The _callout_info string will be
143 * passed to /sbin/request-key to aid with completing the request. If the 144 * passed to /sbin/request-key to aid with completing the request. If the
144 * _callout_info string is "" then it will be changed to "-". 145 * _callout_info string is "" then it will be changed to "-".
145 */ 146 */
146 SYSCALL_DEFINE4(request_key, const char __user *, _type, 147 SYSCALL_DEFINE4(request_key, const char __user *, _type,
147 const char __user *, _description, 148 const char __user *, _description,
148 const char __user *, _callout_info, 149 const char __user *, _callout_info,
149 key_serial_t, destringid) 150 key_serial_t, destringid)
150 { 151 {
151 struct key_type *ktype; 152 struct key_type *ktype;
152 struct key *key; 153 struct key *key;
153 key_ref_t dest_ref; 154 key_ref_t dest_ref;
154 size_t callout_len; 155 size_t callout_len;
155 char type[32], *description, *callout_info; 156 char type[32], *description, *callout_info;
156 long ret; 157 long ret;
157 158
158 /* pull the type into kernel space */ 159 /* pull the type into kernel space */
159 ret = key_get_type_from_user(type, _type, sizeof(type)); 160 ret = key_get_type_from_user(type, _type, sizeof(type));
160 if (ret < 0) 161 if (ret < 0)
161 goto error; 162 goto error;
162 163
163 /* pull the description into kernel space */ 164 /* pull the description into kernel space */
164 description = strndup_user(_description, PAGE_SIZE); 165 description = strndup_user(_description, PAGE_SIZE);
165 if (IS_ERR(description)) { 166 if (IS_ERR(description)) {
166 ret = PTR_ERR(description); 167 ret = PTR_ERR(description);
167 goto error; 168 goto error;
168 } 169 }
169 170
170 /* pull the callout info into kernel space */ 171 /* pull the callout info into kernel space */
171 callout_info = NULL; 172 callout_info = NULL;
172 callout_len = 0; 173 callout_len = 0;
173 if (_callout_info) { 174 if (_callout_info) {
174 callout_info = strndup_user(_callout_info, PAGE_SIZE); 175 callout_info = strndup_user(_callout_info, PAGE_SIZE);
175 if (IS_ERR(callout_info)) { 176 if (IS_ERR(callout_info)) {
176 ret = PTR_ERR(callout_info); 177 ret = PTR_ERR(callout_info);
177 goto error2; 178 goto error2;
178 } 179 }
179 callout_len = strlen(callout_info); 180 callout_len = strlen(callout_info);
180 } 181 }
181 182
182 /* get the destination keyring if specified */ 183 /* get the destination keyring if specified */
183 dest_ref = NULL; 184 dest_ref = NULL;
184 if (destringid) { 185 if (destringid) {
185 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 186 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
186 KEY_WRITE); 187 KEY_WRITE);
187 if (IS_ERR(dest_ref)) { 188 if (IS_ERR(dest_ref)) {
188 ret = PTR_ERR(dest_ref); 189 ret = PTR_ERR(dest_ref);
189 goto error3; 190 goto error3;
190 } 191 }
191 } 192 }
192 193
193 /* find the key type */ 194 /* find the key type */
194 ktype = key_type_lookup(type); 195 ktype = key_type_lookup(type);
195 if (IS_ERR(ktype)) { 196 if (IS_ERR(ktype)) {
196 ret = PTR_ERR(ktype); 197 ret = PTR_ERR(ktype);
197 goto error4; 198 goto error4;
198 } 199 }
199 200
200 /* do the search */ 201 /* do the search */
201 key = request_key_and_link(ktype, description, callout_info, 202 key = request_key_and_link(ktype, description, callout_info,
202 callout_len, NULL, key_ref_to_ptr(dest_ref), 203 callout_len, NULL, key_ref_to_ptr(dest_ref),
203 KEY_ALLOC_IN_QUOTA); 204 KEY_ALLOC_IN_QUOTA);
204 if (IS_ERR(key)) { 205 if (IS_ERR(key)) {
205 ret = PTR_ERR(key); 206 ret = PTR_ERR(key);
206 goto error5; 207 goto error5;
207 } 208 }
208 209
209 /* wait for the key to finish being constructed */ 210 /* wait for the key to finish being constructed */
210 ret = wait_for_key_construction(key, 1); 211 ret = wait_for_key_construction(key, 1);
211 if (ret < 0) 212 if (ret < 0)
212 goto error6; 213 goto error6;
213 214
214 ret = key->serial; 215 ret = key->serial;
215 216
216 error6: 217 error6:
217 key_put(key); 218 key_put(key);
218 error5: 219 error5:
219 key_type_put(ktype); 220 key_type_put(ktype);
220 error4: 221 error4:
221 key_ref_put(dest_ref); 222 key_ref_put(dest_ref);
222 error3: 223 error3:
223 kfree(callout_info); 224 kfree(callout_info);
224 error2: 225 error2:
225 kfree(description); 226 kfree(description);
226 error: 227 error:
227 return ret; 228 return ret;
228 } 229 }
229 230
230 /* 231 /*
231 * Get the ID of the specified process keyring. 232 * Get the ID of the specified process keyring.
232 * 233 *
233 * The requested keyring must have search permission to be found. 234 * The requested keyring must have search permission to be found.
234 * 235 *
235 * If successful, the ID of the requested keyring will be returned. 236 * If successful, the ID of the requested keyring will be returned.
236 */ 237 */
237 long keyctl_get_keyring_ID(key_serial_t id, int create) 238 long keyctl_get_keyring_ID(key_serial_t id, int create)
238 { 239 {
239 key_ref_t key_ref; 240 key_ref_t key_ref;
240 unsigned long lflags; 241 unsigned long lflags;
241 long ret; 242 long ret;
242 243
243 lflags = create ? KEY_LOOKUP_CREATE : 0; 244 lflags = create ? KEY_LOOKUP_CREATE : 0;
244 key_ref = lookup_user_key(id, lflags, KEY_SEARCH); 245 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
245 if (IS_ERR(key_ref)) { 246 if (IS_ERR(key_ref)) {
246 ret = PTR_ERR(key_ref); 247 ret = PTR_ERR(key_ref);
247 goto error; 248 goto error;
248 } 249 }
249 250
250 ret = key_ref_to_ptr(key_ref)->serial; 251 ret = key_ref_to_ptr(key_ref)->serial;
251 key_ref_put(key_ref); 252 key_ref_put(key_ref);
252 error: 253 error:
253 return ret; 254 return ret;
254 } 255 }
255 256
256 /* 257 /*
257 * Join a (named) session keyring. 258 * Join a (named) session keyring.
258 * 259 *
259 * Create and join an anonymous session keyring or join a named session 260 * Create and join an anonymous session keyring or join a named session
260 * keyring, creating it if necessary. A named session keyring must have Search 261 * keyring, creating it if necessary. A named session keyring must have Search
261 * permission for it to be joined. Session keyrings without this permit will 262 * permission for it to be joined. Session keyrings without this permit will
262 * be skipped over. 263 * be skipped over.
263 * 264 *
264 * If successful, the ID of the joined session keyring will be returned. 265 * If successful, the ID of the joined session keyring will be returned.
265 */ 266 */
266 long keyctl_join_session_keyring(const char __user *_name) 267 long keyctl_join_session_keyring(const char __user *_name)
267 { 268 {
268 char *name; 269 char *name;
269 long ret; 270 long ret;
270 271
271 /* fetch the name from userspace */ 272 /* fetch the name from userspace */
272 name = NULL; 273 name = NULL;
273 if (_name) { 274 if (_name) {
274 name = strndup_user(_name, PAGE_SIZE); 275 name = strndup_user(_name, PAGE_SIZE);
275 if (IS_ERR(name)) { 276 if (IS_ERR(name)) {
276 ret = PTR_ERR(name); 277 ret = PTR_ERR(name);
277 goto error; 278 goto error;
278 } 279 }
279 } 280 }
280 281
281 /* join the session */ 282 /* join the session */
282 ret = join_session_keyring(name); 283 ret = join_session_keyring(name);
283 kfree(name); 284 kfree(name);
284 285
285 error: 286 error:
286 return ret; 287 return ret;
287 } 288 }
288 289
289 /* 290 /*
290 * Update a key's data payload from the given data. 291 * Update a key's data payload from the given data.
291 * 292 *
292 * The key must grant the caller Write permission and the key type must support 293 * The key must grant the caller Write permission and the key type must support
293 * updating for this to work. A negative key can be positively instantiated 294 * updating for this to work. A negative key can be positively instantiated
294 * with this call. 295 * with this call.
295 * 296 *
296 * If successful, 0 will be returned. If the key type does not support 297 * If successful, 0 will be returned. If the key type does not support
297 * updating, then -EOPNOTSUPP will be returned. 298 * updating, then -EOPNOTSUPP will be returned.
298 */ 299 */
299 long keyctl_update_key(key_serial_t id, 300 long keyctl_update_key(key_serial_t id,
300 const void __user *_payload, 301 const void __user *_payload,
301 size_t plen) 302 size_t plen)
302 { 303 {
303 key_ref_t key_ref; 304 key_ref_t key_ref;
304 void *payload; 305 void *payload;
305 long ret; 306 long ret;
306 307
307 ret = -EINVAL; 308 ret = -EINVAL;
308 if (plen > PAGE_SIZE) 309 if (plen > PAGE_SIZE)
309 goto error; 310 goto error;
310 311
311 /* pull the payload in if one was supplied */ 312 /* pull the payload in if one was supplied */
312 payload = NULL; 313 payload = NULL;
313 if (_payload) { 314 if (_payload) {
314 ret = -ENOMEM; 315 ret = -ENOMEM;
315 payload = kmalloc(plen, GFP_KERNEL); 316 payload = kmalloc(plen, GFP_KERNEL);
316 if (!payload) 317 if (!payload)
317 goto error; 318 goto error;
318 319
319 ret = -EFAULT; 320 ret = -EFAULT;
320 if (copy_from_user(payload, _payload, plen) != 0) 321 if (copy_from_user(payload, _payload, plen) != 0)
321 goto error2; 322 goto error2;
322 } 323 }
323 324
324 /* find the target key (which must be writable) */ 325 /* find the target key (which must be writable) */
325 key_ref = lookup_user_key(id, 0, KEY_WRITE); 326 key_ref = lookup_user_key(id, 0, KEY_WRITE);
326 if (IS_ERR(key_ref)) { 327 if (IS_ERR(key_ref)) {
327 ret = PTR_ERR(key_ref); 328 ret = PTR_ERR(key_ref);
328 goto error2; 329 goto error2;
329 } 330 }
330 331
331 /* update the key */ 332 /* update the key */
332 ret = key_update(key_ref, payload, plen); 333 ret = key_update(key_ref, payload, plen);
333 334
334 key_ref_put(key_ref); 335 key_ref_put(key_ref);
335 error2: 336 error2:
336 kfree(payload); 337 kfree(payload);
337 error: 338 error:
338 return ret; 339 return ret;
339 } 340 }
340 341
341 /* 342 /*
342 * Revoke a key. 343 * Revoke a key.
343 * 344 *
344 * The key must be grant the caller Write or Setattr permission for this to 345 * The key must be grant the caller Write or Setattr permission for this to
345 * work. The key type should give up its quota claim when revoked. The key 346 * work. The key type should give up its quota claim when revoked. The key
346 * and any links to the key will be automatically garbage collected after a 347 * and any links to the key will be automatically garbage collected after a
347 * certain amount of time (/proc/sys/kernel/keys/gc_delay). 348 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
348 * 349 *
349 * If successful, 0 is returned. 350 * If successful, 0 is returned.
350 */ 351 */
351 long keyctl_revoke_key(key_serial_t id) 352 long keyctl_revoke_key(key_serial_t id)
352 { 353 {
353 key_ref_t key_ref; 354 key_ref_t key_ref;
354 long ret; 355 long ret;
355 356
356 key_ref = lookup_user_key(id, 0, KEY_WRITE); 357 key_ref = lookup_user_key(id, 0, KEY_WRITE);
357 if (IS_ERR(key_ref)) { 358 if (IS_ERR(key_ref)) {
358 ret = PTR_ERR(key_ref); 359 ret = PTR_ERR(key_ref);
359 if (ret != -EACCES) 360 if (ret != -EACCES)
360 goto error; 361 goto error;
361 key_ref = lookup_user_key(id, 0, KEY_SETATTR); 362 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
362 if (IS_ERR(key_ref)) { 363 if (IS_ERR(key_ref)) {
363 ret = PTR_ERR(key_ref); 364 ret = PTR_ERR(key_ref);
364 goto error; 365 goto error;
365 } 366 }
366 } 367 }
367 368
368 key_revoke(key_ref_to_ptr(key_ref)); 369 key_revoke(key_ref_to_ptr(key_ref));
369 ret = 0; 370 ret = 0;
370 371
371 key_ref_put(key_ref); 372 key_ref_put(key_ref);
372 error: 373 error:
373 return ret; 374 return ret;
374 } 375 }
375 376
376 /* 377 /*
377 * Clear the specified keyring, creating an empty process keyring if one of the 378 * Clear the specified keyring, creating an empty process keyring if one of the
378 * special keyring IDs is used. 379 * special keyring IDs is used.
379 * 380 *
380 * The keyring must grant the caller Write permission for this to work. If 381 * The keyring must grant the caller Write permission for this to work. If
381 * successful, 0 will be returned. 382 * successful, 0 will be returned.
382 */ 383 */
383 long keyctl_keyring_clear(key_serial_t ringid) 384 long keyctl_keyring_clear(key_serial_t ringid)
384 { 385 {
385 key_ref_t keyring_ref; 386 key_ref_t keyring_ref;
386 long ret; 387 long ret;
387 388
388 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 389 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
389 if (IS_ERR(keyring_ref)) { 390 if (IS_ERR(keyring_ref)) {
390 ret = PTR_ERR(keyring_ref); 391 ret = PTR_ERR(keyring_ref);
391 goto error; 392 goto error;
392 } 393 }
393 394
394 ret = keyring_clear(key_ref_to_ptr(keyring_ref)); 395 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
395 396
396 key_ref_put(keyring_ref); 397 key_ref_put(keyring_ref);
397 error: 398 error:
398 return ret; 399 return ret;
399 } 400 }
400 401
401 /* 402 /*
402 * Create a link from a keyring to a key if there's no matching key in the 403 * Create a link from a keyring to a key if there's no matching key in the
403 * keyring, otherwise replace the link to the matching key with a link to the 404 * keyring, otherwise replace the link to the matching key with a link to the
404 * new key. 405 * new key.
405 * 406 *
406 * The key must grant the caller Link permission and the the keyring must grant 407 * The key must grant the caller Link permission and the the keyring must grant
407 * the caller Write permission. Furthermore, if an additional link is created, 408 * the caller Write permission. Furthermore, if an additional link is created,
408 * the keyring's quota will be extended. 409 * the keyring's quota will be extended.
409 * 410 *
410 * If successful, 0 will be returned. 411 * If successful, 0 will be returned.
411 */ 412 */
412 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) 413 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
413 { 414 {
414 key_ref_t keyring_ref, key_ref; 415 key_ref_t keyring_ref, key_ref;
415 long ret; 416 long ret;
416 417
417 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 418 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
418 if (IS_ERR(keyring_ref)) { 419 if (IS_ERR(keyring_ref)) {
419 ret = PTR_ERR(keyring_ref); 420 ret = PTR_ERR(keyring_ref);
420 goto error; 421 goto error;
421 } 422 }
422 423
423 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); 424 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
424 if (IS_ERR(key_ref)) { 425 if (IS_ERR(key_ref)) {
425 ret = PTR_ERR(key_ref); 426 ret = PTR_ERR(key_ref);
426 goto error2; 427 goto error2;
427 } 428 }
428 429
429 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 430 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
430 431
431 key_ref_put(key_ref); 432 key_ref_put(key_ref);
432 error2: 433 error2:
433 key_ref_put(keyring_ref); 434 key_ref_put(keyring_ref);
434 error: 435 error:
435 return ret; 436 return ret;
436 } 437 }
437 438
438 /* 439 /*
439 * Unlink a key from a keyring. 440 * Unlink a key from a keyring.
440 * 441 *
441 * The keyring must grant the caller Write permission for this to work; the key 442 * The keyring must grant the caller Write permission for this to work; the key
442 * itself need not grant the caller anything. If the last link to a key is 443 * itself need not grant the caller anything. If the last link to a key is
443 * removed then that key will be scheduled for destruction. 444 * removed then that key will be scheduled for destruction.
444 * 445 *
445 * If successful, 0 will be returned. 446 * If successful, 0 will be returned.
446 */ 447 */
447 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) 448 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
448 { 449 {
449 key_ref_t keyring_ref, key_ref; 450 key_ref_t keyring_ref, key_ref;
450 long ret; 451 long ret;
451 452
452 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); 453 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
453 if (IS_ERR(keyring_ref)) { 454 if (IS_ERR(keyring_ref)) {
454 ret = PTR_ERR(keyring_ref); 455 ret = PTR_ERR(keyring_ref);
455 goto error; 456 goto error;
456 } 457 }
457 458
458 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); 459 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
459 if (IS_ERR(key_ref)) { 460 if (IS_ERR(key_ref)) {
460 ret = PTR_ERR(key_ref); 461 ret = PTR_ERR(key_ref);
461 goto error2; 462 goto error2;
462 } 463 }
463 464
464 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); 465 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
465 466
466 key_ref_put(key_ref); 467 key_ref_put(key_ref);
467 error2: 468 error2:
468 key_ref_put(keyring_ref); 469 key_ref_put(keyring_ref);
469 error: 470 error:
470 return ret; 471 return ret;
471 } 472 }
472 473
473 /* 474 /*
474 * Return a description of a key to userspace. 475 * Return a description of a key to userspace.
475 * 476 *
476 * The key must grant the caller View permission for this to work. 477 * The key must grant the caller View permission for this to work.
477 * 478 *
478 * If there's a buffer, we place up to buflen bytes of data into it formatted 479 * If there's a buffer, we place up to buflen bytes of data into it formatted
479 * in the following way: 480 * in the following way:
480 * 481 *
481 * type;uid;gid;perm;description<NUL> 482 * type;uid;gid;perm;description<NUL>
482 * 483 *
483 * If successful, we return the amount of description available, irrespective 484 * If successful, we return the amount of description available, irrespective
484 * of how much we may have copied into the buffer. 485 * of how much we may have copied into the buffer.
485 */ 486 */
486 long keyctl_describe_key(key_serial_t keyid, 487 long keyctl_describe_key(key_serial_t keyid,
487 char __user *buffer, 488 char __user *buffer,
488 size_t buflen) 489 size_t buflen)
489 { 490 {
490 struct key *key, *instkey; 491 struct key *key, *instkey;
491 key_ref_t key_ref; 492 key_ref_t key_ref;
492 char *tmpbuf; 493 char *tmpbuf;
493 long ret; 494 long ret;
494 495
495 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 496 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
496 if (IS_ERR(key_ref)) { 497 if (IS_ERR(key_ref)) {
497 /* viewing a key under construction is permitted if we have the 498 /* viewing a key under construction is permitted if we have the
498 * authorisation token handy */ 499 * authorisation token handy */
499 if (PTR_ERR(key_ref) == -EACCES) { 500 if (PTR_ERR(key_ref) == -EACCES) {
500 instkey = key_get_instantiation_authkey(keyid); 501 instkey = key_get_instantiation_authkey(keyid);
501 if (!IS_ERR(instkey)) { 502 if (!IS_ERR(instkey)) {
502 key_put(instkey); 503 key_put(instkey);
503 key_ref = lookup_user_key(keyid, 504 key_ref = lookup_user_key(keyid,
504 KEY_LOOKUP_PARTIAL, 505 KEY_LOOKUP_PARTIAL,
505 0); 506 0);
506 if (!IS_ERR(key_ref)) 507 if (!IS_ERR(key_ref))
507 goto okay; 508 goto okay;
508 } 509 }
509 } 510 }
510 511
511 ret = PTR_ERR(key_ref); 512 ret = PTR_ERR(key_ref);
512 goto error; 513 goto error;
513 } 514 }
514 515
515 okay: 516 okay:
516 /* calculate how much description we're going to return */ 517 /* calculate how much description we're going to return */
517 ret = -ENOMEM; 518 ret = -ENOMEM;
518 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); 519 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
519 if (!tmpbuf) 520 if (!tmpbuf)
520 goto error2; 521 goto error2;
521 522
522 key = key_ref_to_ptr(key_ref); 523 key = key_ref_to_ptr(key_ref);
523 524
524 ret = snprintf(tmpbuf, PAGE_SIZE - 1, 525 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
525 "%s;%d;%d;%08x;%s", 526 "%s;%d;%d;%08x;%s",
526 key->type->name, 527 key->type->name,
527 key->uid, 528 key->uid,
528 key->gid, 529 key->gid,
529 key->perm, 530 key->perm,
530 key->description ?: ""); 531 key->description ?: "");
531 532
532 /* include a NUL char at the end of the data */ 533 /* include a NUL char at the end of the data */
533 if (ret > PAGE_SIZE - 1) 534 if (ret > PAGE_SIZE - 1)
534 ret = PAGE_SIZE - 1; 535 ret = PAGE_SIZE - 1;
535 tmpbuf[ret] = 0; 536 tmpbuf[ret] = 0;
536 ret++; 537 ret++;
537 538
538 /* consider returning the data */ 539 /* consider returning the data */
539 if (buffer && buflen > 0) { 540 if (buffer && buflen > 0) {
540 if (buflen > ret) 541 if (buflen > ret)
541 buflen = ret; 542 buflen = ret;
542 543
543 if (copy_to_user(buffer, tmpbuf, buflen) != 0) 544 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
544 ret = -EFAULT; 545 ret = -EFAULT;
545 } 546 }
546 547
547 kfree(tmpbuf); 548 kfree(tmpbuf);
548 error2: 549 error2:
549 key_ref_put(key_ref); 550 key_ref_put(key_ref);
550 error: 551 error:
551 return ret; 552 return ret;
552 } 553 }
553 554
554 /* 555 /*
555 * Search the specified keyring and any keyrings it links to for a matching 556 * Search the specified keyring and any keyrings it links to for a matching
556 * key. Only keyrings that grant the caller Search permission will be searched 557 * key. Only keyrings that grant the caller Search permission will be searched
557 * (this includes the starting keyring). Only keys with Search permission can 558 * (this includes the starting keyring). Only keys with Search permission can
558 * be found. 559 * be found.
559 * 560 *
560 * If successful, the found key will be linked to the destination keyring if 561 * If successful, the found key will be linked to the destination keyring if
561 * supplied and the key has Link permission, and the found key ID will be 562 * supplied and the key has Link permission, and the found key ID will be
562 * returned. 563 * returned.
563 */ 564 */
564 long keyctl_keyring_search(key_serial_t ringid, 565 long keyctl_keyring_search(key_serial_t ringid,
565 const char __user *_type, 566 const char __user *_type,
566 const char __user *_description, 567 const char __user *_description,
567 key_serial_t destringid) 568 key_serial_t destringid)
568 { 569 {
569 struct key_type *ktype; 570 struct key_type *ktype;
570 key_ref_t keyring_ref, key_ref, dest_ref; 571 key_ref_t keyring_ref, key_ref, dest_ref;
571 char type[32], *description; 572 char type[32], *description;
572 long ret; 573 long ret;
573 574
574 /* pull the type and description into kernel space */ 575 /* pull the type and description into kernel space */
575 ret = key_get_type_from_user(type, _type, sizeof(type)); 576 ret = key_get_type_from_user(type, _type, sizeof(type));
576 if (ret < 0) 577 if (ret < 0)
577 goto error; 578 goto error;
578 579
579 description = strndup_user(_description, PAGE_SIZE); 580 description = strndup_user(_description, PAGE_SIZE);
580 if (IS_ERR(description)) { 581 if (IS_ERR(description)) {
581 ret = PTR_ERR(description); 582 ret = PTR_ERR(description);
582 goto error; 583 goto error;
583 } 584 }
584 585
585 /* get the keyring at which to begin the search */ 586 /* get the keyring at which to begin the search */
586 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); 587 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
587 if (IS_ERR(keyring_ref)) { 588 if (IS_ERR(keyring_ref)) {
588 ret = PTR_ERR(keyring_ref); 589 ret = PTR_ERR(keyring_ref);
589 goto error2; 590 goto error2;
590 } 591 }
591 592
592 /* get the destination keyring if specified */ 593 /* get the destination keyring if specified */
593 dest_ref = NULL; 594 dest_ref = NULL;
594 if (destringid) { 595 if (destringid) {
595 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, 596 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
596 KEY_WRITE); 597 KEY_WRITE);
597 if (IS_ERR(dest_ref)) { 598 if (IS_ERR(dest_ref)) {
598 ret = PTR_ERR(dest_ref); 599 ret = PTR_ERR(dest_ref);
599 goto error3; 600 goto error3;
600 } 601 }
601 } 602 }
602 603
603 /* find the key type */ 604 /* find the key type */
604 ktype = key_type_lookup(type); 605 ktype = key_type_lookup(type);
605 if (IS_ERR(ktype)) { 606 if (IS_ERR(ktype)) {
606 ret = PTR_ERR(ktype); 607 ret = PTR_ERR(ktype);
607 goto error4; 608 goto error4;
608 } 609 }
609 610
610 /* do the search */ 611 /* do the search */
611 key_ref = keyring_search(keyring_ref, ktype, description); 612 key_ref = keyring_search(keyring_ref, ktype, description);
612 if (IS_ERR(key_ref)) { 613 if (IS_ERR(key_ref)) {
613 ret = PTR_ERR(key_ref); 614 ret = PTR_ERR(key_ref);
614 615
615 /* treat lack or presence of a negative key the same */ 616 /* treat lack or presence of a negative key the same */
616 if (ret == -EAGAIN) 617 if (ret == -EAGAIN)
617 ret = -ENOKEY; 618 ret = -ENOKEY;
618 goto error5; 619 goto error5;
619 } 620 }
620 621
621 /* link the resulting key to the destination keyring if we can */ 622 /* link the resulting key to the destination keyring if we can */
622 if (dest_ref) { 623 if (dest_ref) {
623 ret = key_permission(key_ref, KEY_LINK); 624 ret = key_permission(key_ref, KEY_LINK);
624 if (ret < 0) 625 if (ret < 0)
625 goto error6; 626 goto error6;
626 627
627 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); 628 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
628 if (ret < 0) 629 if (ret < 0)
629 goto error6; 630 goto error6;
630 } 631 }
631 632
632 ret = key_ref_to_ptr(key_ref)->serial; 633 ret = key_ref_to_ptr(key_ref)->serial;
633 634
634 error6: 635 error6:
635 key_ref_put(key_ref); 636 key_ref_put(key_ref);
636 error5: 637 error5:
637 key_type_put(ktype); 638 key_type_put(ktype);
638 error4: 639 error4:
639 key_ref_put(dest_ref); 640 key_ref_put(dest_ref);
640 error3: 641 error3:
641 key_ref_put(keyring_ref); 642 key_ref_put(keyring_ref);
642 error2: 643 error2:
643 kfree(description); 644 kfree(description);
644 error: 645 error:
645 return ret; 646 return ret;
646 } 647 }
647 648
648 /* 649 /*
649 * Read a key's payload. 650 * Read a key's payload.
650 * 651 *
651 * The key must either grant the caller Read permission, or it must grant the 652 * The key must either grant the caller Read permission, or it must grant the
652 * caller Search permission when searched for from the process keyrings. 653 * caller Search permission when searched for from the process keyrings.
653 * 654 *
654 * If successful, we place up to buflen bytes of data into the buffer, if one 655 * If successful, we place up to buflen bytes of data into the buffer, if one
655 * is provided, and return the amount of data that is available in the key, 656 * is provided, and return the amount of data that is available in the key,
656 * irrespective of how much we copied into the buffer. 657 * irrespective of how much we copied into the buffer.
657 */ 658 */
658 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) 659 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
659 { 660 {
660 struct key *key; 661 struct key *key;
661 key_ref_t key_ref; 662 key_ref_t key_ref;
662 long ret; 663 long ret;
663 664
664 /* find the key first */ 665 /* find the key first */
665 key_ref = lookup_user_key(keyid, 0, 0); 666 key_ref = lookup_user_key(keyid, 0, 0);
666 if (IS_ERR(key_ref)) { 667 if (IS_ERR(key_ref)) {
667 ret = -ENOKEY; 668 ret = -ENOKEY;
668 goto error; 669 goto error;
669 } 670 }
670 671
671 key = key_ref_to_ptr(key_ref); 672 key = key_ref_to_ptr(key_ref);
672 673
673 /* see if we can read it directly */ 674 /* see if we can read it directly */
674 ret = key_permission(key_ref, KEY_READ); 675 ret = key_permission(key_ref, KEY_READ);
675 if (ret == 0) 676 if (ret == 0)
676 goto can_read_key; 677 goto can_read_key;
677 if (ret != -EACCES) 678 if (ret != -EACCES)
678 goto error; 679 goto error;
679 680
680 /* we can't; see if it's searchable from this process's keyrings 681 /* we can't; see if it's searchable from this process's keyrings
681 * - we automatically take account of the fact that it may be 682 * - we automatically take account of the fact that it may be
682 * dangling off an instantiation key 683 * dangling off an instantiation key
683 */ 684 */
684 if (!is_key_possessed(key_ref)) { 685 if (!is_key_possessed(key_ref)) {
685 ret = -EACCES; 686 ret = -EACCES;
686 goto error2; 687 goto error2;
687 } 688 }
688 689
689 /* the key is probably readable - now try to read it */ 690 /* the key is probably readable - now try to read it */
690 can_read_key: 691 can_read_key:
691 ret = key_validate(key); 692 ret = key_validate(key);
692 if (ret == 0) { 693 if (ret == 0) {
693 ret = -EOPNOTSUPP; 694 ret = -EOPNOTSUPP;
694 if (key->type->read) { 695 if (key->type->read) {
695 /* read the data with the semaphore held (since we 696 /* read the data with the semaphore held (since we
696 * might sleep) */ 697 * might sleep) */
697 down_read(&key->sem); 698 down_read(&key->sem);
698 ret = key->type->read(key, buffer, buflen); 699 ret = key->type->read(key, buffer, buflen);
699 up_read(&key->sem); 700 up_read(&key->sem);
700 } 701 }
701 } 702 }
702 703
703 error2: 704 error2:
704 key_put(key); 705 key_put(key);
705 error: 706 error:
706 return ret; 707 return ret;
707 } 708 }
708 709
709 /* 710 /*
710 * Change the ownership of a key 711 * Change the ownership of a key
711 * 712 *
712 * The key must grant the caller Setattr permission for this to work, though 713 * The key must grant the caller Setattr permission for this to work, though
713 * the key need not be fully instantiated yet. For the UID to be changed, or 714 * the key need not be fully instantiated yet. For the UID to be changed, or
714 * for the GID to be changed to a group the caller is not a member of, the 715 * for the GID to be changed to a group the caller is not a member of, the
715 * caller must have sysadmin capability. If either uid or gid is -1 then that 716 * caller must have sysadmin capability. If either uid or gid is -1 then that
716 * attribute is not changed. 717 * attribute is not changed.
717 * 718 *
718 * If the UID is to be changed, the new user must have sufficient quota to 719 * If the UID is to be changed, the new user must have sufficient quota to
719 * accept the key. The quota deduction will be removed from the old user to 720 * accept the key. The quota deduction will be removed from the old user to
720 * the new user should the attribute be changed. 721 * the new user should the attribute be changed.
721 * 722 *
722 * If successful, 0 will be returned. 723 * If successful, 0 will be returned.
723 */ 724 */
724 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) 725 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
725 { 726 {
726 struct key_user *newowner, *zapowner = NULL; 727 struct key_user *newowner, *zapowner = NULL;
727 struct key *key; 728 struct key *key;
728 key_ref_t key_ref; 729 key_ref_t key_ref;
729 long ret; 730 long ret;
730 731
731 ret = 0; 732 ret = 0;
732 if (uid == (uid_t) -1 && gid == (gid_t) -1) 733 if (uid == (uid_t) -1 && gid == (gid_t) -1)
733 goto error; 734 goto error;
734 735
735 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 736 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
736 KEY_SETATTR); 737 KEY_SETATTR);
737 if (IS_ERR(key_ref)) { 738 if (IS_ERR(key_ref)) {
738 ret = PTR_ERR(key_ref); 739 ret = PTR_ERR(key_ref);
739 goto error; 740 goto error;
740 } 741 }
741 742
742 key = key_ref_to_ptr(key_ref); 743 key = key_ref_to_ptr(key_ref);
743 744
744 /* make the changes with the locks held to prevent chown/chown races */ 745 /* make the changes with the locks held to prevent chown/chown races */
745 ret = -EACCES; 746 ret = -EACCES;
746 down_write(&key->sem); 747 down_write(&key->sem);
747 748
748 if (!capable(CAP_SYS_ADMIN)) { 749 if (!capable(CAP_SYS_ADMIN)) {
749 /* only the sysadmin can chown a key to some other UID */ 750 /* only the sysadmin can chown a key to some other UID */
750 if (uid != (uid_t) -1 && key->uid != uid) 751 if (uid != (uid_t) -1 && key->uid != uid)
751 goto error_put; 752 goto error_put;
752 753
753 /* only the sysadmin can set the key's GID to a group other 754 /* only the sysadmin can set the key's GID to a group other
754 * than one of those that the current process subscribes to */ 755 * than one of those that the current process subscribes to */
755 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) 756 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
756 goto error_put; 757 goto error_put;
757 } 758 }
758 759
759 /* change the UID */ 760 /* change the UID */
760 if (uid != (uid_t) -1 && uid != key->uid) { 761 if (uid != (uid_t) -1 && uid != key->uid) {
761 ret = -ENOMEM; 762 ret = -ENOMEM;
762 newowner = key_user_lookup(uid, current_user_ns()); 763 newowner = key_user_lookup(uid, current_user_ns());
763 if (!newowner) 764 if (!newowner)
764 goto error_put; 765 goto error_put;
765 766
766 /* transfer the quota burden to the new user */ 767 /* transfer the quota burden to the new user */
767 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { 768 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
768 unsigned maxkeys = (uid == 0) ? 769 unsigned maxkeys = (uid == 0) ?
769 key_quota_root_maxkeys : key_quota_maxkeys; 770 key_quota_root_maxkeys : key_quota_maxkeys;
770 unsigned maxbytes = (uid == 0) ? 771 unsigned maxbytes = (uid == 0) ?
771 key_quota_root_maxbytes : key_quota_maxbytes; 772 key_quota_root_maxbytes : key_quota_maxbytes;
772 773
773 spin_lock(&newowner->lock); 774 spin_lock(&newowner->lock);
774 if (newowner->qnkeys + 1 >= maxkeys || 775 if (newowner->qnkeys + 1 >= maxkeys ||
775 newowner->qnbytes + key->quotalen >= maxbytes || 776 newowner->qnbytes + key->quotalen >= maxbytes ||
776 newowner->qnbytes + key->quotalen < 777 newowner->qnbytes + key->quotalen <
777 newowner->qnbytes) 778 newowner->qnbytes)
778 goto quota_overrun; 779 goto quota_overrun;
779 780
780 newowner->qnkeys++; 781 newowner->qnkeys++;
781 newowner->qnbytes += key->quotalen; 782 newowner->qnbytes += key->quotalen;
782 spin_unlock(&newowner->lock); 783 spin_unlock(&newowner->lock);
783 784
784 spin_lock(&key->user->lock); 785 spin_lock(&key->user->lock);
785 key->user->qnkeys--; 786 key->user->qnkeys--;
786 key->user->qnbytes -= key->quotalen; 787 key->user->qnbytes -= key->quotalen;
787 spin_unlock(&key->user->lock); 788 spin_unlock(&key->user->lock);
788 } 789 }
789 790
790 atomic_dec(&key->user->nkeys); 791 atomic_dec(&key->user->nkeys);
791 atomic_inc(&newowner->nkeys); 792 atomic_inc(&newowner->nkeys);
792 793
793 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { 794 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
794 atomic_dec(&key->user->nikeys); 795 atomic_dec(&key->user->nikeys);
795 atomic_inc(&newowner->nikeys); 796 atomic_inc(&newowner->nikeys);
796 } 797 }
797 798
798 zapowner = key->user; 799 zapowner = key->user;
799 key->user = newowner; 800 key->user = newowner;
800 key->uid = uid; 801 key->uid = uid;
801 } 802 }
802 803
803 /* change the GID */ 804 /* change the GID */
804 if (gid != (gid_t) -1) 805 if (gid != (gid_t) -1)
805 key->gid = gid; 806 key->gid = gid;
806 807
807 ret = 0; 808 ret = 0;
808 809
809 error_put: 810 error_put:
810 up_write(&key->sem); 811 up_write(&key->sem);
811 key_put(key); 812 key_put(key);
812 if (zapowner) 813 if (zapowner)
813 key_user_put(zapowner); 814 key_user_put(zapowner);
814 error: 815 error:
815 return ret; 816 return ret;
816 817
817 quota_overrun: 818 quota_overrun:
818 spin_unlock(&newowner->lock); 819 spin_unlock(&newowner->lock);
819 zapowner = newowner; 820 zapowner = newowner;
820 ret = -EDQUOT; 821 ret = -EDQUOT;
821 goto error_put; 822 goto error_put;
822 } 823 }
823 824
824 /* 825 /*
825 * Change the permission mask on a key. 826 * Change the permission mask on a key.
826 * 827 *
827 * The key must grant the caller Setattr permission for this to work, though 828 * The key must grant the caller Setattr permission for this to work, though
828 * the key need not be fully instantiated yet. If the caller does not have 829 * the key need not be fully instantiated yet. If the caller does not have
829 * sysadmin capability, it may only change the permission on keys that it owns. 830 * sysadmin capability, it may only change the permission on keys that it owns.
830 */ 831 */
831 long keyctl_setperm_key(key_serial_t id, key_perm_t perm) 832 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
832 { 833 {
833 struct key *key; 834 struct key *key;
834 key_ref_t key_ref; 835 key_ref_t key_ref;
835 long ret; 836 long ret;
836 837
837 ret = -EINVAL; 838 ret = -EINVAL;
838 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) 839 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
839 goto error; 840 goto error;
840 841
841 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 842 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
842 KEY_SETATTR); 843 KEY_SETATTR);
843 if (IS_ERR(key_ref)) { 844 if (IS_ERR(key_ref)) {
844 ret = PTR_ERR(key_ref); 845 ret = PTR_ERR(key_ref);
845 goto error; 846 goto error;
846 } 847 }
847 848
848 key = key_ref_to_ptr(key_ref); 849 key = key_ref_to_ptr(key_ref);
849 850
850 /* make the changes with the locks held to prevent chown/chmod races */ 851 /* make the changes with the locks held to prevent chown/chmod races */
851 ret = -EACCES; 852 ret = -EACCES;
852 down_write(&key->sem); 853 down_write(&key->sem);
853 854
854 /* if we're not the sysadmin, we can only change a key that we own */ 855 /* if we're not the sysadmin, we can only change a key that we own */
855 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { 856 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
856 key->perm = perm; 857 key->perm = perm;
857 ret = 0; 858 ret = 0;
858 } 859 }
859 860
860 up_write(&key->sem); 861 up_write(&key->sem);
861 key_put(key); 862 key_put(key);
862 error: 863 error:
863 return ret; 864 return ret;
864 } 865 }
865 866
866 /* 867 /*
867 * Get the destination keyring for instantiation and check that the caller has 868 * Get the destination keyring for instantiation and check that the caller has
868 * Write permission on it. 869 * Write permission on it.
869 */ 870 */
870 static long get_instantiation_keyring(key_serial_t ringid, 871 static long get_instantiation_keyring(key_serial_t ringid,
871 struct request_key_auth *rka, 872 struct request_key_auth *rka,
872 struct key **_dest_keyring) 873 struct key **_dest_keyring)
873 { 874 {
874 key_ref_t dkref; 875 key_ref_t dkref;
875 876
876 *_dest_keyring = NULL; 877 *_dest_keyring = NULL;
877 878
878 /* just return a NULL pointer if we weren't asked to make a link */ 879 /* just return a NULL pointer if we weren't asked to make a link */
879 if (ringid == 0) 880 if (ringid == 0)
880 return 0; 881 return 0;
881 882
882 /* if a specific keyring is nominated by ID, then use that */ 883 /* if a specific keyring is nominated by ID, then use that */
883 if (ringid > 0) { 884 if (ringid > 0) {
884 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); 885 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
885 if (IS_ERR(dkref)) 886 if (IS_ERR(dkref))
886 return PTR_ERR(dkref); 887 return PTR_ERR(dkref);
887 *_dest_keyring = key_ref_to_ptr(dkref); 888 *_dest_keyring = key_ref_to_ptr(dkref);
888 return 0; 889 return 0;
889 } 890 }
890 891
891 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) 892 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
892 return -EINVAL; 893 return -EINVAL;
893 894
894 /* otherwise specify the destination keyring recorded in the 895 /* otherwise specify the destination keyring recorded in the
895 * authorisation key (any KEY_SPEC_*_KEYRING) */ 896 * authorisation key (any KEY_SPEC_*_KEYRING) */
896 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { 897 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
897 *_dest_keyring = key_get(rka->dest_keyring); 898 *_dest_keyring = key_get(rka->dest_keyring);
898 return 0; 899 return 0;
899 } 900 }
900 901
901 return -ENOKEY; 902 return -ENOKEY;
902 } 903 }
903 904
904 /* 905 /*
905 * Change the request_key authorisation key on the current process. 906 * Change the request_key authorisation key on the current process.
906 */ 907 */
907 static int keyctl_change_reqkey_auth(struct key *key) 908 static int keyctl_change_reqkey_auth(struct key *key)
908 { 909 {
909 struct cred *new; 910 struct cred *new;
910 911
911 new = prepare_creds(); 912 new = prepare_creds();
912 if (!new) 913 if (!new)
913 return -ENOMEM; 914 return -ENOMEM;
914 915
915 key_put(new->request_key_auth); 916 key_put(new->request_key_auth);
916 new->request_key_auth = key_get(key); 917 new->request_key_auth = key_get(key);
917 918
918 return commit_creds(new); 919 return commit_creds(new);
919 } 920 }
920 921
921 /* 922 /*
922 * Copy the iovec data from userspace 923 * Copy the iovec data from userspace
923 */ 924 */
924 static long copy_from_user_iovec(void *buffer, const struct iovec *iov, 925 static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
925 unsigned ioc) 926 unsigned ioc)
926 { 927 {
927 for (; ioc > 0; ioc--) { 928 for (; ioc > 0; ioc--) {
928 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0) 929 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
929 return -EFAULT; 930 return -EFAULT;
930 buffer += iov->iov_len; 931 buffer += iov->iov_len;
931 iov++; 932 iov++;
932 } 933 }
933 return 0; 934 return 0;
934 } 935 }
935 936
936 /* 937 /*
937 * Instantiate a key with the specified payload and link the key into the 938 * Instantiate a key with the specified payload and link the key into the
938 * destination keyring if one is given. 939 * destination keyring if one is given.
939 * 940 *
940 * The caller must have the appropriate instantiation permit set for this to 941 * The caller must have the appropriate instantiation permit set for this to
941 * work (see keyctl_assume_authority). No other permissions are required. 942 * work (see keyctl_assume_authority). No other permissions are required.
942 * 943 *
943 * If successful, 0 will be returned. 944 * If successful, 0 will be returned.
944 */ 945 */
945 long keyctl_instantiate_key_common(key_serial_t id, 946 long keyctl_instantiate_key_common(key_serial_t id,
946 const struct iovec *payload_iov, 947 const struct iovec *payload_iov,
947 unsigned ioc, 948 unsigned ioc,
948 size_t plen, 949 size_t plen,
949 key_serial_t ringid) 950 key_serial_t ringid)
950 { 951 {
951 const struct cred *cred = current_cred(); 952 const struct cred *cred = current_cred();
952 struct request_key_auth *rka; 953 struct request_key_auth *rka;
953 struct key *instkey, *dest_keyring; 954 struct key *instkey, *dest_keyring;
954 void *payload; 955 void *payload;
955 long ret; 956 long ret;
956 bool vm = false; 957 bool vm = false;
957 958
958 kenter("%d,,%zu,%d", id, plen, ringid); 959 kenter("%d,,%zu,%d", id, plen, ringid);
959 960
960 ret = -EINVAL; 961 ret = -EINVAL;
961 if (plen > 1024 * 1024 - 1) 962 if (plen > 1024 * 1024 - 1)
962 goto error; 963 goto error;
963 964
964 /* the appropriate instantiation authorisation key must have been 965 /* the appropriate instantiation authorisation key must have been
965 * assumed before calling this */ 966 * assumed before calling this */
966 ret = -EPERM; 967 ret = -EPERM;
967 instkey = cred->request_key_auth; 968 instkey = cred->request_key_auth;
968 if (!instkey) 969 if (!instkey)
969 goto error; 970 goto error;
970 971
971 rka = instkey->payload.data; 972 rka = instkey->payload.data;
972 if (rka->target_key->serial != id) 973 if (rka->target_key->serial != id)
973 goto error; 974 goto error;
974 975
975 /* pull the payload in if one was supplied */ 976 /* pull the payload in if one was supplied */
976 payload = NULL; 977 payload = NULL;
977 978
978 if (payload_iov) { 979 if (payload_iov) {
979 ret = -ENOMEM; 980 ret = -ENOMEM;
980 payload = kmalloc(plen, GFP_KERNEL); 981 payload = kmalloc(plen, GFP_KERNEL);
981 if (!payload) { 982 if (!payload) {
982 if (plen <= PAGE_SIZE) 983 if (plen <= PAGE_SIZE)
983 goto error; 984 goto error;
984 vm = true; 985 vm = true;
985 payload = vmalloc(plen); 986 payload = vmalloc(plen);
986 if (!payload) 987 if (!payload)
987 goto error; 988 goto error;
988 } 989 }
989 990
990 ret = copy_from_user_iovec(payload, payload_iov, ioc); 991 ret = copy_from_user_iovec(payload, payload_iov, ioc);
991 if (ret < 0) 992 if (ret < 0)
992 goto error2; 993 goto error2;
993 } 994 }
994 995
995 /* find the destination keyring amongst those belonging to the 996 /* find the destination keyring amongst those belonging to the
996 * requesting task */ 997 * requesting task */
997 ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 998 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
998 if (ret < 0) 999 if (ret < 0)
999 goto error2; 1000 goto error2;
1000 1001
1001 /* instantiate the key and link it into a keyring */ 1002 /* instantiate the key and link it into a keyring */
1002 ret = key_instantiate_and_link(rka->target_key, payload, plen, 1003 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1003 dest_keyring, instkey); 1004 dest_keyring, instkey);
1004 1005
1005 key_put(dest_keyring); 1006 key_put(dest_keyring);
1006 1007
1007 /* discard the assumed authority if it's just been disabled by 1008 /* discard the assumed authority if it's just been disabled by
1008 * instantiation of the key */ 1009 * instantiation of the key */
1009 if (ret == 0) 1010 if (ret == 0)
1010 keyctl_change_reqkey_auth(NULL); 1011 keyctl_change_reqkey_auth(NULL);
1011 1012
1012 error2: 1013 error2:
1013 if (!vm) 1014 if (!vm)
1014 kfree(payload); 1015 kfree(payload);
1015 else 1016 else
1016 vfree(payload); 1017 vfree(payload);
1017 error: 1018 error:
1018 return ret; 1019 return ret;
1019 } 1020 }
1020 1021
1021 /* 1022 /*
1022 * Instantiate a key with the specified payload and link the key into the 1023 * Instantiate a key with the specified payload and link the key into the
1023 * destination keyring if one is given. 1024 * destination keyring if one is given.
1024 * 1025 *
1025 * The caller must have the appropriate instantiation permit set for this to 1026 * The caller must have the appropriate instantiation permit set for this to
1026 * work (see keyctl_assume_authority). No other permissions are required. 1027 * work (see keyctl_assume_authority). No other permissions are required.
1027 * 1028 *
1028 * If successful, 0 will be returned. 1029 * If successful, 0 will be returned.
1029 */ 1030 */
1030 long keyctl_instantiate_key(key_serial_t id, 1031 long keyctl_instantiate_key(key_serial_t id,
1031 const void __user *_payload, 1032 const void __user *_payload,
1032 size_t plen, 1033 size_t plen,
1033 key_serial_t ringid) 1034 key_serial_t ringid)
1034 { 1035 {
1035 if (_payload && plen) { 1036 if (_payload && plen) {
1036 struct iovec iov[1] = { 1037 struct iovec iov[1] = {
1037 [0].iov_base = (void __user *)_payload, 1038 [0].iov_base = (void __user *)_payload,
1038 [0].iov_len = plen 1039 [0].iov_len = plen
1039 }; 1040 };
1040 1041
1041 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid); 1042 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1042 } 1043 }
1043 1044
1044 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); 1045 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1045 } 1046 }
1046 1047
1047 /* 1048 /*
1048 * Instantiate a key with the specified multipart payload and link the key into 1049 * Instantiate a key with the specified multipart payload and link the key into
1049 * the destination keyring if one is given. 1050 * the destination keyring if one is given.
1050 * 1051 *
1051 * The caller must have the appropriate instantiation permit set for this to 1052 * The caller must have the appropriate instantiation permit set for this to
1052 * work (see keyctl_assume_authority). No other permissions are required. 1053 * work (see keyctl_assume_authority). No other permissions are required.
1053 * 1054 *
1054 * If successful, 0 will be returned. 1055 * If successful, 0 will be returned.
1055 */ 1056 */
1056 long keyctl_instantiate_key_iov(key_serial_t id, 1057 long keyctl_instantiate_key_iov(key_serial_t id,
1057 const struct iovec __user *_payload_iov, 1058 const struct iovec __user *_payload_iov,
1058 unsigned ioc, 1059 unsigned ioc,
1059 key_serial_t ringid) 1060 key_serial_t ringid)
1060 { 1061 {
1061 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1062 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1062 long ret; 1063 long ret;
1063 1064
1064 if (_payload_iov == 0 || ioc == 0) 1065 if (_payload_iov == 0 || ioc == 0)
1065 goto no_payload; 1066 goto no_payload;
1066 1067
1067 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc, 1068 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1068 ARRAY_SIZE(iovstack), iovstack, &iov, 1); 1069 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
1069 if (ret < 0) 1070 if (ret < 0)
1070 return ret; 1071 return ret;
1071 if (ret == 0) 1072 if (ret == 0)
1072 goto no_payload_free; 1073 goto no_payload_free;
1073 1074
1074 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid); 1075 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1075 1076
1076 if (iov != iovstack) 1077 if (iov != iovstack)
1077 kfree(iov); 1078 kfree(iov);
1078 return ret; 1079 return ret;
1079 1080
1080 no_payload_free: 1081 no_payload_free:
1081 if (iov != iovstack) 1082 if (iov != iovstack)
1082 kfree(iov); 1083 kfree(iov);
1083 no_payload: 1084 no_payload:
1084 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); 1085 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1085 } 1086 }
1086 1087
1087 /* 1088 /*
1088 * Negatively instantiate the key with the given timeout (in seconds) and link 1089 * Negatively instantiate the key with the given timeout (in seconds) and link
1089 * the key into the destination keyring if one is given. 1090 * the key into the destination keyring if one is given.
1090 * 1091 *
1091 * The caller must have the appropriate instantiation permit set for this to 1092 * The caller must have the appropriate instantiation permit set for this to
1092 * work (see keyctl_assume_authority). No other permissions are required. 1093 * work (see keyctl_assume_authority). No other permissions are required.
1093 * 1094 *
1094 * The key and any links to the key will be automatically garbage collected 1095 * The key and any links to the key will be automatically garbage collected
1095 * after the timeout expires. 1096 * after the timeout expires.
1096 * 1097 *
1097 * Negative keys are used to rate limit repeated request_key() calls by causing 1098 * Negative keys are used to rate limit repeated request_key() calls by causing
1098 * them to return -ENOKEY until the negative key expires. 1099 * them to return -ENOKEY until the negative key expires.
1099 * 1100 *
1100 * If successful, 0 will be returned. 1101 * If successful, 0 will be returned.
1101 */ 1102 */
1102 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) 1103 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1103 { 1104 {
1104 return keyctl_reject_key(id, timeout, ENOKEY, ringid); 1105 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1105 } 1106 }
1106 1107
1107 /* 1108 /*
1108 * Negatively instantiate the key with the given timeout (in seconds) and error 1109 * Negatively instantiate the key with the given timeout (in seconds) and error
1109 * code and link the key into the destination keyring if one is given. 1110 * code and link the key into the destination keyring if one is given.
1110 * 1111 *
1111 * The caller must have the appropriate instantiation permit set for this to 1112 * The caller must have the appropriate instantiation permit set for this to
1112 * work (see keyctl_assume_authority). No other permissions are required. 1113 * work (see keyctl_assume_authority). No other permissions are required.
1113 * 1114 *
1114 * The key and any links to the key will be automatically garbage collected 1115 * The key and any links to the key will be automatically garbage collected
1115 * after the timeout expires. 1116 * after the timeout expires.
1116 * 1117 *
1117 * Negative keys are used to rate limit repeated request_key() calls by causing 1118 * Negative keys are used to rate limit repeated request_key() calls by causing
1118 * them to return the specified error code until the negative key expires. 1119 * them to return the specified error code until the negative key expires.
1119 * 1120 *
1120 * If successful, 0 will be returned. 1121 * If successful, 0 will be returned.
1121 */ 1122 */
1122 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, 1123 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1123 key_serial_t ringid) 1124 key_serial_t ringid)
1124 { 1125 {
1125 const struct cred *cred = current_cred(); 1126 const struct cred *cred = current_cred();
1126 struct request_key_auth *rka; 1127 struct request_key_auth *rka;
1127 struct key *instkey, *dest_keyring; 1128 struct key *instkey, *dest_keyring;
1128 long ret; 1129 long ret;
1129 1130
1130 kenter("%d,%u,%u,%d", id, timeout, error, ringid); 1131 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1131 1132
1132 /* must be a valid error code and mustn't be a kernel special */ 1133 /* must be a valid error code and mustn't be a kernel special */
1133 if (error <= 0 || 1134 if (error <= 0 ||
1134 error >= MAX_ERRNO || 1135 error >= MAX_ERRNO ||
1135 error == ERESTARTSYS || 1136 error == ERESTARTSYS ||
1136 error == ERESTARTNOINTR || 1137 error == ERESTARTNOINTR ||
1137 error == ERESTARTNOHAND || 1138 error == ERESTARTNOHAND ||
1138 error == ERESTART_RESTARTBLOCK) 1139 error == ERESTART_RESTARTBLOCK)
1139 return -EINVAL; 1140 return -EINVAL;
1140 1141
1141 /* the appropriate instantiation authorisation key must have been 1142 /* the appropriate instantiation authorisation key must have been
1142 * assumed before calling this */ 1143 * assumed before calling this */
1143 ret = -EPERM; 1144 ret = -EPERM;
1144 instkey = cred->request_key_auth; 1145 instkey = cred->request_key_auth;
1145 if (!instkey) 1146 if (!instkey)
1146 goto error; 1147 goto error;
1147 1148
1148 rka = instkey->payload.data; 1149 rka = instkey->payload.data;
1149 if (rka->target_key->serial != id) 1150 if (rka->target_key->serial != id)
1150 goto error; 1151 goto error;
1151 1152
1152 /* find the destination keyring if present (which must also be 1153 /* find the destination keyring if present (which must also be
1153 * writable) */ 1154 * writable) */
1154 ret = get_instantiation_keyring(ringid, rka, &dest_keyring); 1155 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1155 if (ret < 0) 1156 if (ret < 0)
1156 goto error; 1157 goto error;
1157 1158
1158 /* instantiate the key and link it into a keyring */ 1159 /* instantiate the key and link it into a keyring */
1159 ret = key_reject_and_link(rka->target_key, timeout, error, 1160 ret = key_reject_and_link(rka->target_key, timeout, error,
1160 dest_keyring, instkey); 1161 dest_keyring, instkey);
1161 1162
1162 key_put(dest_keyring); 1163 key_put(dest_keyring);
1163 1164
1164 /* discard the assumed authority if it's just been disabled by 1165 /* discard the assumed authority if it's just been disabled by
1165 * instantiation of the key */ 1166 * instantiation of the key */
1166 if (ret == 0) 1167 if (ret == 0)
1167 keyctl_change_reqkey_auth(NULL); 1168 keyctl_change_reqkey_auth(NULL);
1168 1169
1169 error: 1170 error:
1170 return ret; 1171 return ret;
1171 } 1172 }
1172 1173
1173 /* 1174 /*
1174 * Read or set the default keyring in which request_key() will cache keys and 1175 * Read or set the default keyring in which request_key() will cache keys and
1175 * return the old setting. 1176 * return the old setting.
1176 * 1177 *
1177 * If a process keyring is specified then this will be created if it doesn't 1178 * If a process keyring is specified then this will be created if it doesn't
1178 * yet exist. The old setting will be returned if successful. 1179 * yet exist. The old setting will be returned if successful.
1179 */ 1180 */
1180 long keyctl_set_reqkey_keyring(int reqkey_defl) 1181 long keyctl_set_reqkey_keyring(int reqkey_defl)
1181 { 1182 {
1182 struct cred *new; 1183 struct cred *new;
1183 int ret, old_setting; 1184 int ret, old_setting;
1184 1185
1185 old_setting = current_cred_xxx(jit_keyring); 1186 old_setting = current_cred_xxx(jit_keyring);
1186 1187
1187 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) 1188 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1188 return old_setting; 1189 return old_setting;
1189 1190
1190 new = prepare_creds(); 1191 new = prepare_creds();
1191 if (!new) 1192 if (!new)
1192 return -ENOMEM; 1193 return -ENOMEM;
1193 1194
1194 switch (reqkey_defl) { 1195 switch (reqkey_defl) {
1195 case KEY_REQKEY_DEFL_THREAD_KEYRING: 1196 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1196 ret = install_thread_keyring_to_cred(new); 1197 ret = install_thread_keyring_to_cred(new);
1197 if (ret < 0) 1198 if (ret < 0)
1198 goto error; 1199 goto error;
1199 goto set; 1200 goto set;
1200 1201
1201 case KEY_REQKEY_DEFL_PROCESS_KEYRING: 1202 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1202 ret = install_process_keyring_to_cred(new); 1203 ret = install_process_keyring_to_cred(new);
1203 if (ret < 0) { 1204 if (ret < 0) {
1204 if (ret != -EEXIST) 1205 if (ret != -EEXIST)
1205 goto error; 1206 goto error;
1206 ret = 0; 1207 ret = 0;
1207 } 1208 }
1208 goto set; 1209 goto set;
1209 1210
1210 case KEY_REQKEY_DEFL_DEFAULT: 1211 case KEY_REQKEY_DEFL_DEFAULT:
1211 case KEY_REQKEY_DEFL_SESSION_KEYRING: 1212 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1212 case KEY_REQKEY_DEFL_USER_KEYRING: 1213 case KEY_REQKEY_DEFL_USER_KEYRING:
1213 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: 1214 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1214 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: 1215 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1215 goto set; 1216 goto set;
1216 1217
1217 case KEY_REQKEY_DEFL_NO_CHANGE: 1218 case KEY_REQKEY_DEFL_NO_CHANGE:
1218 case KEY_REQKEY_DEFL_GROUP_KEYRING: 1219 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1219 default: 1220 default:
1220 ret = -EINVAL; 1221 ret = -EINVAL;
1221 goto error; 1222 goto error;
1222 } 1223 }
1223 1224
1224 set: 1225 set:
1225 new->jit_keyring = reqkey_defl; 1226 new->jit_keyring = reqkey_defl;
1226 commit_creds(new); 1227 commit_creds(new);
1227 return old_setting; 1228 return old_setting;
1228 error: 1229 error:
1229 abort_creds(new); 1230 abort_creds(new);
1230 return ret; 1231 return ret;
1231 } 1232 }
1232 1233
1233 /* 1234 /*
1234 * Set or clear the timeout on a key. 1235 * Set or clear the timeout on a key.
1235 * 1236 *
1236 * Either the key must grant the caller Setattr permission or else the caller 1237 * Either the key must grant the caller Setattr permission or else the caller
1237 * must hold an instantiation authorisation token for the key. 1238 * must hold an instantiation authorisation token for the key.
1238 * 1239 *
1239 * The timeout is either 0 to clear the timeout, or a number of seconds from 1240 * The timeout is either 0 to clear the timeout, or a number of seconds from
1240 * the current time. The key and any links to the key will be automatically 1241 * the current time. The key and any links to the key will be automatically
1241 * garbage collected after the timeout expires. 1242 * garbage collected after the timeout expires.
1242 * 1243 *
1243 * If successful, 0 is returned. 1244 * If successful, 0 is returned.
1244 */ 1245 */
1245 long keyctl_set_timeout(key_serial_t id, unsigned timeout) 1246 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1246 { 1247 {
1247 struct timespec now;
1248 struct key *key, *instkey; 1248 struct key *key, *instkey;
1249 key_ref_t key_ref; 1249 key_ref_t key_ref;
1250 time_t expiry;
1251 long ret; 1250 long ret;
1252 1251
1253 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, 1252 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1254 KEY_SETATTR); 1253 KEY_SETATTR);
1255 if (IS_ERR(key_ref)) { 1254 if (IS_ERR(key_ref)) {
1256 /* setting the timeout on a key under construction is permitted 1255 /* setting the timeout on a key under construction is permitted
1257 * if we have the authorisation token handy */ 1256 * if we have the authorisation token handy */
1258 if (PTR_ERR(key_ref) == -EACCES) { 1257 if (PTR_ERR(key_ref) == -EACCES) {
1259 instkey = key_get_instantiation_authkey(id); 1258 instkey = key_get_instantiation_authkey(id);
1260 if (!IS_ERR(instkey)) { 1259 if (!IS_ERR(instkey)) {
1261 key_put(instkey); 1260 key_put(instkey);
1262 key_ref = lookup_user_key(id, 1261 key_ref = lookup_user_key(id,
1263 KEY_LOOKUP_PARTIAL, 1262 KEY_LOOKUP_PARTIAL,
1264 0); 1263 0);
1265 if (!IS_ERR(key_ref)) 1264 if (!IS_ERR(key_ref))
1266 goto okay; 1265 goto okay;
1267 } 1266 }
1268 } 1267 }
1269 1268
1270 ret = PTR_ERR(key_ref); 1269 ret = PTR_ERR(key_ref);
1271 goto error; 1270 goto error;
1272 } 1271 }
1273 1272
1274 okay: 1273 okay:
1275 key = key_ref_to_ptr(key_ref); 1274 key = key_ref_to_ptr(key_ref);
1276 1275 key_set_timeout(key, timeout);
1277 /* make the changes with the locks held to prevent races */
1278 down_write(&key->sem);
1279
1280 expiry = 0;
1281 if (timeout > 0) {
1282 now = current_kernel_time();
1283 expiry = now.tv_sec + timeout;
1284 }
1285
1286 key->expiry = expiry;
1287 key_schedule_gc(key->expiry + key_gc_delay);
1288
1289 up_write(&key->sem);
1290 key_put(key); 1276 key_put(key);
1291 1277
1292 ret = 0; 1278 ret = 0;
1293 error: 1279 error:
1294 return ret; 1280 return ret;
1295 } 1281 }
1296 1282
1297 /* 1283 /*
1298 * Assume (or clear) the authority to instantiate the specified key. 1284 * Assume (or clear) the authority to instantiate the specified key.
1299 * 1285 *
1300 * This sets the authoritative token currently in force for key instantiation. 1286 * This sets the authoritative token currently in force for key instantiation.
1301 * This must be done for a key to be instantiated. It has the effect of making 1287 * This must be done for a key to be instantiated. It has the effect of making
1302 * available all the keys from the caller of the request_key() that created a 1288 * available all the keys from the caller of the request_key() that created a
1303 * key to request_key() calls made by the caller of this function. 1289 * key to request_key() calls made by the caller of this function.
1304 * 1290 *
1305 * The caller must have the instantiation key in their process keyrings with a 1291 * The caller must have the instantiation key in their process keyrings with a
1306 * Search permission grant available to the caller. 1292 * Search permission grant available to the caller.
1307 * 1293 *
1308 * If the ID given is 0, then the setting will be cleared and 0 returned. 1294 * If the ID given is 0, then the setting will be cleared and 0 returned.
1309 * 1295 *
1310 * If the ID given has a matching an authorisation key, then that key will be 1296 * If the ID given has a matching an authorisation key, then that key will be
1311 * set and its ID will be returned. The authorisation key can be read to get 1297 * set and its ID will be returned. The authorisation key can be read to get
1312 * the callout information passed to request_key(). 1298 * the callout information passed to request_key().
1313 */ 1299 */
1314 long keyctl_assume_authority(key_serial_t id) 1300 long keyctl_assume_authority(key_serial_t id)
1315 { 1301 {
1316 struct key *authkey; 1302 struct key *authkey;
1317 long ret; 1303 long ret;
1318 1304
1319 /* special key IDs aren't permitted */ 1305 /* special key IDs aren't permitted */
1320 ret = -EINVAL; 1306 ret = -EINVAL;
1321 if (id < 0) 1307 if (id < 0)
1322 goto error; 1308 goto error;
1323 1309
1324 /* we divest ourselves of authority if given an ID of 0 */ 1310 /* we divest ourselves of authority if given an ID of 0 */
1325 if (id == 0) { 1311 if (id == 0) {
1326 ret = keyctl_change_reqkey_auth(NULL); 1312 ret = keyctl_change_reqkey_auth(NULL);
1327 goto error; 1313 goto error;
1328 } 1314 }
1329 1315
1330 /* attempt to assume the authority temporarily granted to us whilst we 1316 /* attempt to assume the authority temporarily granted to us whilst we
1331 * instantiate the specified key 1317 * instantiate the specified key
1332 * - the authorisation key must be in the current task's keyrings 1318 * - the authorisation key must be in the current task's keyrings
1333 * somewhere 1319 * somewhere
1334 */ 1320 */
1335 authkey = key_get_instantiation_authkey(id); 1321 authkey = key_get_instantiation_authkey(id);
1336 if (IS_ERR(authkey)) { 1322 if (IS_ERR(authkey)) {
1337 ret = PTR_ERR(authkey); 1323 ret = PTR_ERR(authkey);
1338 goto error; 1324 goto error;
1339 } 1325 }
1340 1326
1341 ret = keyctl_change_reqkey_auth(authkey); 1327 ret = keyctl_change_reqkey_auth(authkey);
1342 if (ret < 0) 1328 if (ret < 0)
1343 goto error; 1329 goto error;
1344 key_put(authkey); 1330 key_put(authkey);
1345 1331
1346 ret = authkey->serial; 1332 ret = authkey->serial;
1347 error: 1333 error:
1348 return ret; 1334 return ret;
1349 } 1335 }
1350 1336
1351 /* 1337 /*
1352 * Get a key's the LSM security label. 1338 * Get a key's the LSM security label.
1353 * 1339 *
1354 * The key must grant the caller View permission for this to work. 1340 * The key must grant the caller View permission for this to work.
1355 * 1341 *
1356 * If there's a buffer, then up to buflen bytes of data will be placed into it. 1342 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1357 * 1343 *
1358 * If successful, the amount of information available will be returned, 1344 * If successful, the amount of information available will be returned,
1359 * irrespective of how much was copied (including the terminal NUL). 1345 * irrespective of how much was copied (including the terminal NUL).
1360 */ 1346 */
1361 long keyctl_get_security(key_serial_t keyid, 1347 long keyctl_get_security(key_serial_t keyid,
1362 char __user *buffer, 1348 char __user *buffer,
1363 size_t buflen) 1349 size_t buflen)
1364 { 1350 {
1365 struct key *key, *instkey; 1351 struct key *key, *instkey;
1366 key_ref_t key_ref; 1352 key_ref_t key_ref;
1367 char *context; 1353 char *context;
1368 long ret; 1354 long ret;
1369 1355
1370 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); 1356 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1371 if (IS_ERR(key_ref)) { 1357 if (IS_ERR(key_ref)) {
1372 if (PTR_ERR(key_ref) != -EACCES) 1358 if (PTR_ERR(key_ref) != -EACCES)
1373 return PTR_ERR(key_ref); 1359 return PTR_ERR(key_ref);
1374 1360
1375 /* viewing a key under construction is also permitted if we 1361 /* viewing a key under construction is also permitted if we
1376 * have the authorisation token handy */ 1362 * have the authorisation token handy */
1377 instkey = key_get_instantiation_authkey(keyid); 1363 instkey = key_get_instantiation_authkey(keyid);
1378 if (IS_ERR(instkey)) 1364 if (IS_ERR(instkey))
1379 return PTR_ERR(instkey); 1365 return PTR_ERR(instkey);
1380 key_put(instkey); 1366 key_put(instkey);
1381 1367
1382 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); 1368 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1383 if (IS_ERR(key_ref)) 1369 if (IS_ERR(key_ref))
1384 return PTR_ERR(key_ref); 1370 return PTR_ERR(key_ref);
1385 } 1371 }
1386 1372
1387 key = key_ref_to_ptr(key_ref); 1373 key = key_ref_to_ptr(key_ref);
1388 ret = security_key_getsecurity(key, &context); 1374 ret = security_key_getsecurity(key, &context);
1389 if (ret == 0) { 1375 if (ret == 0) {
1390 /* if no information was returned, give userspace an empty 1376 /* if no information was returned, give userspace an empty
1391 * string */ 1377 * string */
1392 ret = 1; 1378 ret = 1;
1393 if (buffer && buflen > 0 && 1379 if (buffer && buflen > 0 &&
1394 copy_to_user(buffer, "", 1) != 0) 1380 copy_to_user(buffer, "", 1) != 0)
1395 ret = -EFAULT; 1381 ret = -EFAULT;
1396 } else if (ret > 0) { 1382 } else if (ret > 0) {
1397 /* return as much data as there's room for */ 1383 /* return as much data as there's room for */
1398 if (buffer && buflen > 0) { 1384 if (buffer && buflen > 0) {
1399 if (buflen > ret) 1385 if (buflen > ret)
1400 buflen = ret; 1386 buflen = ret;
1401 1387
1402 if (copy_to_user(buffer, context, buflen) != 0) 1388 if (copy_to_user(buffer, context, buflen) != 0)
1403 ret = -EFAULT; 1389 ret = -EFAULT;
1404 } 1390 }
1405 1391
1406 kfree(context); 1392 kfree(context);
1407 } 1393 }
1408 1394
1409 key_ref_put(key_ref); 1395 key_ref_put(key_ref);
1410 return ret; 1396 return ret;
1411 } 1397 }
1412 1398
1413 /* 1399 /*
1414 * Attempt to install the calling process's session keyring on the process's 1400 * Attempt to install the calling process's session keyring on the process's
1415 * parent process. 1401 * parent process.
1416 * 1402 *
1417 * The keyring must exist and must grant the caller LINK permission, and the 1403 * The keyring must exist and must grant the caller LINK permission, and the
1418 * parent process must be single-threaded and must have the same effective 1404 * parent process must be single-threaded and must have the same effective
1419 * ownership as this process and mustn't be SUID/SGID. 1405 * ownership as this process and mustn't be SUID/SGID.
1420 * 1406 *
1421 * The keyring will be emplaced on the parent when it next resumes userspace. 1407 * The keyring will be emplaced on the parent when it next resumes userspace.
1422 * 1408 *
1423 * If successful, 0 will be returned. 1409 * If successful, 0 will be returned.
1424 */ 1410 */
1425 long keyctl_session_to_parent(void) 1411 long keyctl_session_to_parent(void)
1426 { 1412 {
1427 #ifdef TIF_NOTIFY_RESUME 1413 #ifdef TIF_NOTIFY_RESUME
1428 struct task_struct *me, *parent; 1414 struct task_struct *me, *parent;
1429 const struct cred *mycred, *pcred; 1415 const struct cred *mycred, *pcred;
1430 struct cred *cred, *oldcred; 1416 struct cred *cred, *oldcred;
1431 key_ref_t keyring_r; 1417 key_ref_t keyring_r;
1432 int ret; 1418 int ret;
1433 1419
1434 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK); 1420 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1435 if (IS_ERR(keyring_r)) 1421 if (IS_ERR(keyring_r))
1436 return PTR_ERR(keyring_r); 1422 return PTR_ERR(keyring_r);
1437 1423
1438 /* our parent is going to need a new cred struct, a new tgcred struct 1424 /* our parent is going to need a new cred struct, a new tgcred struct
1439 * and new security data, so we allocate them here to prevent ENOMEM in 1425 * and new security data, so we allocate them here to prevent ENOMEM in
1440 * our parent */ 1426 * our parent */
1441 ret = -ENOMEM; 1427 ret = -ENOMEM;
1442 cred = cred_alloc_blank(); 1428 cred = cred_alloc_blank();
1443 if (!cred) 1429 if (!cred)
1444 goto error_keyring; 1430 goto error_keyring;
1445 1431
1446 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r); 1432 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1447 keyring_r = NULL; 1433 keyring_r = NULL;
1448 1434
1449 me = current; 1435 me = current;
1450 rcu_read_lock(); 1436 rcu_read_lock();
1451 write_lock_irq(&tasklist_lock); 1437 write_lock_irq(&tasklist_lock);
1452 1438
1453 parent = me->real_parent; 1439 parent = me->real_parent;
1454 ret = -EPERM; 1440 ret = -EPERM;
1455 1441
1456 /* the parent mustn't be init and mustn't be a kernel thread */ 1442 /* the parent mustn't be init and mustn't be a kernel thread */
1457 if (parent->pid <= 1 || !parent->mm) 1443 if (parent->pid <= 1 || !parent->mm)
1458 goto not_permitted; 1444 goto not_permitted;
1459 1445
1460 /* the parent must be single threaded */ 1446 /* the parent must be single threaded */
1461 if (!thread_group_empty(parent)) 1447 if (!thread_group_empty(parent))
1462 goto not_permitted; 1448 goto not_permitted;
1463 1449
1464 /* the parent and the child must have different session keyrings or 1450 /* the parent and the child must have different session keyrings or
1465 * there's no point */ 1451 * there's no point */
1466 mycred = current_cred(); 1452 mycred = current_cred();
1467 pcred = __task_cred(parent); 1453 pcred = __task_cred(parent);
1468 if (mycred == pcred || 1454 if (mycred == pcred ||
1469 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) 1455 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1470 goto already_same; 1456 goto already_same;
1471 1457
1472 /* the parent must have the same effective ownership and mustn't be 1458 /* the parent must have the same effective ownership and mustn't be
1473 * SUID/SGID */ 1459 * SUID/SGID */
1474 if (pcred->uid != mycred->euid || 1460 if (pcred->uid != mycred->euid ||
1475 pcred->euid != mycred->euid || 1461 pcred->euid != mycred->euid ||
1476 pcred->suid != mycred->euid || 1462 pcred->suid != mycred->euid ||
1477 pcred->gid != mycred->egid || 1463 pcred->gid != mycred->egid ||
1478 pcred->egid != mycred->egid || 1464 pcred->egid != mycred->egid ||
1479 pcred->sgid != mycred->egid) 1465 pcred->sgid != mycred->egid)
1480 goto not_permitted; 1466 goto not_permitted;
1481 1467
1482 /* the keyrings must have the same UID */ 1468 /* the keyrings must have the same UID */
1483 if ((pcred->tgcred->session_keyring && 1469 if ((pcred->tgcred->session_keyring &&
1484 pcred->tgcred->session_keyring->uid != mycred->euid) || 1470 pcred->tgcred->session_keyring->uid != mycred->euid) ||
1485 mycred->tgcred->session_keyring->uid != mycred->euid) 1471 mycred->tgcred->session_keyring->uid != mycred->euid)
1486 goto not_permitted; 1472 goto not_permitted;
1487 1473
1488 /* if there's an already pending keyring replacement, then we replace 1474 /* if there's an already pending keyring replacement, then we replace
1489 * that */ 1475 * that */
1490 oldcred = parent->replacement_session_keyring; 1476 oldcred = parent->replacement_session_keyring;
1491 1477
1492 /* the replacement session keyring is applied just prior to userspace 1478 /* the replacement session keyring is applied just prior to userspace
1493 * restarting */ 1479 * restarting */
1494 parent->replacement_session_keyring = cred; 1480 parent->replacement_session_keyring = cred;
1495 cred = NULL; 1481 cred = NULL;
1496 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME); 1482 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1497 1483
1498 write_unlock_irq(&tasklist_lock); 1484 write_unlock_irq(&tasklist_lock);
1499 rcu_read_unlock(); 1485 rcu_read_unlock();
1500 if (oldcred) 1486 if (oldcred)
1501 put_cred(oldcred); 1487 put_cred(oldcred);
1502 return 0; 1488 return 0;
1503 1489
1504 already_same: 1490 already_same:
1505 ret = 0; 1491 ret = 0;
1506 not_permitted: 1492 not_permitted:
1507 write_unlock_irq(&tasklist_lock); 1493 write_unlock_irq(&tasklist_lock);
1508 rcu_read_unlock(); 1494 rcu_read_unlock();
1509 put_cred(cred); 1495 put_cred(cred);
1510 return ret; 1496 return ret;
1511 1497
1512 error_keyring: 1498 error_keyring:
1513 key_ref_put(keyring_r); 1499 key_ref_put(keyring_r);
1514 return ret; 1500 return ret;
1515 1501
1516 #else /* !TIF_NOTIFY_RESUME */ 1502 #else /* !TIF_NOTIFY_RESUME */
1517 /* 1503 /*
1518 * To be removed when TIF_NOTIFY_RESUME has been implemented on 1504 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1519 * m68k/xtensa 1505 * m68k/xtensa
1520 */ 1506 */
1521 #warning TIF_NOTIFY_RESUME not implemented 1507 #warning TIF_NOTIFY_RESUME not implemented
1522 return -EOPNOTSUPP; 1508 return -EOPNOTSUPP;
1523 #endif /* !TIF_NOTIFY_RESUME */ 1509 #endif /* !TIF_NOTIFY_RESUME */
1524 } 1510 }
1525 1511
1526 /* 1512 /*
1527 * The key control system call 1513 * The key control system call
1528 */ 1514 */
1529 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, 1515 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1530 unsigned long, arg4, unsigned long, arg5) 1516 unsigned long, arg4, unsigned long, arg5)
1531 { 1517 {
1532 switch (option) { 1518 switch (option) {
1533 case KEYCTL_GET_KEYRING_ID: 1519 case KEYCTL_GET_KEYRING_ID:
1534 return keyctl_get_keyring_ID((key_serial_t) arg2, 1520 return keyctl_get_keyring_ID((key_serial_t) arg2,
1535 (int) arg3); 1521 (int) arg3);
1536 1522
1537 case KEYCTL_JOIN_SESSION_KEYRING: 1523 case KEYCTL_JOIN_SESSION_KEYRING:
1538 return keyctl_join_session_keyring((const char __user *) arg2); 1524 return keyctl_join_session_keyring((const char __user *) arg2);
1539 1525
1540 case KEYCTL_UPDATE: 1526 case KEYCTL_UPDATE:
1541 return keyctl_update_key((key_serial_t) arg2, 1527 return keyctl_update_key((key_serial_t) arg2,
1542 (const void __user *) arg3, 1528 (const void __user *) arg3,
1543 (size_t) arg4); 1529 (size_t) arg4);
1544 1530
1545 case KEYCTL_REVOKE: 1531 case KEYCTL_REVOKE:
1546 return keyctl_revoke_key((key_serial_t) arg2); 1532 return keyctl_revoke_key((key_serial_t) arg2);
1547 1533
1548 case KEYCTL_DESCRIBE: 1534 case KEYCTL_DESCRIBE:
1549 return keyctl_describe_key((key_serial_t) arg2, 1535 return keyctl_describe_key((key_serial_t) arg2,
1550 (char __user *) arg3, 1536 (char __user *) arg3,
1551 (unsigned) arg4); 1537 (unsigned) arg4);
1552 1538
1553 case KEYCTL_CLEAR: 1539 case KEYCTL_CLEAR:
1554 return keyctl_keyring_clear((key_serial_t) arg2); 1540 return keyctl_keyring_clear((key_serial_t) arg2);
1555 1541
1556 case KEYCTL_LINK: 1542 case KEYCTL_LINK:
1557 return keyctl_keyring_link((key_serial_t) arg2, 1543 return keyctl_keyring_link((key_serial_t) arg2,
1558 (key_serial_t) arg3); 1544 (key_serial_t) arg3);
1559 1545
1560 case KEYCTL_UNLINK: 1546 case KEYCTL_UNLINK:
1561 return keyctl_keyring_unlink((key_serial_t) arg2, 1547 return keyctl_keyring_unlink((key_serial_t) arg2,
1562 (key_serial_t) arg3); 1548 (key_serial_t) arg3);
1563 1549
1564 case KEYCTL_SEARCH: 1550 case KEYCTL_SEARCH:
1565 return keyctl_keyring_search((key_serial_t) arg2, 1551 return keyctl_keyring_search((key_serial_t) arg2,
1566 (const char __user *) arg3, 1552 (const char __user *) arg3,
1567 (const char __user *) arg4, 1553 (const char __user *) arg4,
1568 (key_serial_t) arg5); 1554 (key_serial_t) arg5);
1569 1555
1570 case KEYCTL_READ: 1556 case KEYCTL_READ:
1571 return keyctl_read_key((key_serial_t) arg2, 1557 return keyctl_read_key((key_serial_t) arg2,
1572 (char __user *) arg3, 1558 (char __user *) arg3,
1573 (size_t) arg4); 1559 (size_t) arg4);
1574 1560
1575 case KEYCTL_CHOWN: 1561 case KEYCTL_CHOWN:
1576 return keyctl_chown_key((key_serial_t) arg2, 1562 return keyctl_chown_key((key_serial_t) arg2,
1577 (uid_t) arg3, 1563 (uid_t) arg3,
1578 (gid_t) arg4); 1564 (gid_t) arg4);
1579 1565
1580 case KEYCTL_SETPERM: 1566 case KEYCTL_SETPERM:
1581 return keyctl_setperm_key((key_serial_t) arg2, 1567 return keyctl_setperm_key((key_serial_t) arg2,
1582 (key_perm_t) arg3); 1568 (key_perm_t) arg3);
1583 1569
1584 case KEYCTL_INSTANTIATE: 1570 case KEYCTL_INSTANTIATE:
1585 return keyctl_instantiate_key((key_serial_t) arg2, 1571 return keyctl_instantiate_key((key_serial_t) arg2,
1586 (const void __user *) arg3, 1572 (const void __user *) arg3,
1587 (size_t) arg4, 1573 (size_t) arg4,
1588 (key_serial_t) arg5); 1574 (key_serial_t) arg5);
1589 1575
1590 case KEYCTL_NEGATE: 1576 case KEYCTL_NEGATE:
1591 return keyctl_negate_key((key_serial_t) arg2, 1577 return keyctl_negate_key((key_serial_t) arg2,
1592 (unsigned) arg3, 1578 (unsigned) arg3,
1593 (key_serial_t) arg4); 1579 (key_serial_t) arg4);
1594 1580
1595 case KEYCTL_SET_REQKEY_KEYRING: 1581 case KEYCTL_SET_REQKEY_KEYRING:
1596 return keyctl_set_reqkey_keyring(arg2); 1582 return keyctl_set_reqkey_keyring(arg2);
1597 1583
1598 case KEYCTL_SET_TIMEOUT: 1584 case KEYCTL_SET_TIMEOUT:
1599 return keyctl_set_timeout((key_serial_t) arg2, 1585 return keyctl_set_timeout((key_serial_t) arg2,
1600 (unsigned) arg3); 1586 (unsigned) arg3);
1601 1587
1602 case KEYCTL_ASSUME_AUTHORITY: 1588 case KEYCTL_ASSUME_AUTHORITY:
1603 return keyctl_assume_authority((key_serial_t) arg2); 1589 return keyctl_assume_authority((key_serial_t) arg2);
1604 1590
1605 case KEYCTL_GET_SECURITY: 1591 case KEYCTL_GET_SECURITY:
1606 return keyctl_get_security((key_serial_t) arg2, 1592 return keyctl_get_security((key_serial_t) arg2,
1607 (char __user *) arg3, 1593 (char __user *) arg3,
1608 (size_t) arg4); 1594 (size_t) arg4);
1609 1595
1610 case KEYCTL_SESSION_TO_PARENT: 1596 case KEYCTL_SESSION_TO_PARENT:
1611 return keyctl_session_to_parent(); 1597 return keyctl_session_to_parent();
1612 1598
1613 case KEYCTL_REJECT: 1599 case KEYCTL_REJECT:
1614 return keyctl_reject_key((key_serial_t) arg2, 1600 return keyctl_reject_key((key_serial_t) arg2,
1615 (unsigned) arg3, 1601 (unsigned) arg3,
1616 (unsigned) arg4, 1602 (unsigned) arg4,
1617 (key_serial_t) arg5); 1603 (key_serial_t) arg5);
1618 1604
1619 case KEYCTL_INSTANTIATE_IOV: 1605 case KEYCTL_INSTANTIATE_IOV:
1620 return keyctl_instantiate_key_iov( 1606 return keyctl_instantiate_key_iov(
1621 (key_serial_t) arg2, 1607 (key_serial_t) arg2,
1622 (const struct iovec __user *) arg3, 1608 (const struct iovec __user *) arg3,
1623 (unsigned) arg4, 1609 (unsigned) arg4,
1624 (key_serial_t) arg5); 1610 (key_serial_t) arg5);
1625 1611
1626 default: 1612 default:
1627 return -EOPNOTSUPP; 1613 return -EOPNOTSUPP;
1628 } 1614 }
1629 } 1615 }