Commit | Line | Data |
---|---|---|
76181c13 | 1 | /* Basic authentication token and access key management |
1da177e4 | 2 | * |
69664cf1 | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
a7807a32 | 14 | #include <linux/poison.h> |
1da177e4 LT |
15 | #include <linux/sched.h> |
16 | #include <linux/slab.h> | |
29db9190 | 17 | #include <linux/security.h> |
1da177e4 | 18 | #include <linux/workqueue.h> |
e51f6d34 | 19 | #include <linux/random.h> |
1da177e4 LT |
20 | #include <linux/err.h> |
21 | #include "internal.h" | |
22 | ||
e18b890b | 23 | static struct kmem_cache *key_jar; |
1da177e4 LT |
24 | struct rb_root key_serial_tree; /* tree of keys indexed by serial */ |
25 | DEFINE_SPINLOCK(key_serial_lock); | |
26 | ||
27 | struct rb_root key_user_tree; /* tree of quota records indexed by UID */ | |
28 | DEFINE_SPINLOCK(key_user_lock); | |
29 | ||
30 | static LIST_HEAD(key_types_list); | |
31 | static DECLARE_RWSEM(key_types_sem); | |
32 | ||
65f27f38 DH |
33 | static void key_cleanup(struct work_struct *work); |
34 | static DECLARE_WORK(key_cleanup_task, key_cleanup); | |
1da177e4 LT |
35 | |
36 | /* we serialise key instantiation and link */ | |
76181c13 | 37 | DEFINE_MUTEX(key_construction_mutex); |
1da177e4 LT |
38 | |
39 | /* any key who's type gets unegistered will be re-typed to this */ | |
1ae8f407 | 40 | static struct key_type key_type_dead = { |
1da177e4 LT |
41 | .name = "dead", |
42 | }; | |
43 | ||
44 | #ifdef KEY_DEBUGGING | |
45 | void __key_check(const struct key *key) | |
46 | { | |
47 | printk("__key_check: key %p {%08x} should be {%08x}\n", | |
48 | key, key->magic, KEY_DEBUG_MAGIC); | |
49 | BUG(); | |
50 | } | |
51 | #endif | |
52 | ||
53 | /*****************************************************************************/ | |
54 | /* | |
55 | * get the key quota record for a user, allocating a new record if one doesn't | |
56 | * already exist | |
57 | */ | |
58 | struct key_user *key_user_lookup(uid_t uid) | |
59 | { | |
60 | struct key_user *candidate = NULL, *user; | |
61 | struct rb_node *parent = NULL; | |
62 | struct rb_node **p; | |
63 | ||
64 | try_again: | |
65 | p = &key_user_tree.rb_node; | |
66 | spin_lock(&key_user_lock); | |
67 | ||
68 | /* search the tree for a user record with a matching UID */ | |
69 | while (*p) { | |
70 | parent = *p; | |
71 | user = rb_entry(parent, struct key_user, node); | |
72 | ||
73 | if (uid < user->uid) | |
74 | p = &(*p)->rb_left; | |
75 | else if (uid > user->uid) | |
76 | p = &(*p)->rb_right; | |
77 | else | |
78 | goto found; | |
79 | } | |
80 | ||
81 | /* if we get here, we failed to find a match in the tree */ | |
82 | if (!candidate) { | |
83 | /* allocate a candidate user record if we don't already have | |
84 | * one */ | |
85 | spin_unlock(&key_user_lock); | |
86 | ||
87 | user = NULL; | |
88 | candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL); | |
89 | if (unlikely(!candidate)) | |
90 | goto out; | |
91 | ||
92 | /* the allocation may have scheduled, so we need to repeat the | |
93 | * search lest someone else added the record whilst we were | |
94 | * asleep */ | |
95 | goto try_again; | |
96 | } | |
97 | ||
98 | /* if we get here, then the user record still hadn't appeared on the | |
99 | * second pass - so we use the candidate record */ | |
100 | atomic_set(&candidate->usage, 1); | |
101 | atomic_set(&candidate->nkeys, 0); | |
102 | atomic_set(&candidate->nikeys, 0); | |
103 | candidate->uid = uid; | |
104 | candidate->qnkeys = 0; | |
105 | candidate->qnbytes = 0; | |
106 | spin_lock_init(&candidate->lock); | |
76181c13 | 107 | mutex_init(&candidate->cons_lock); |
1da177e4 LT |
108 | |
109 | rb_link_node(&candidate->node, parent, p); | |
110 | rb_insert_color(&candidate->node, &key_user_tree); | |
111 | spin_unlock(&key_user_lock); | |
112 | user = candidate; | |
113 | goto out; | |
114 | ||
115 | /* okay - we found a user record for this UID */ | |
116 | found: | |
117 | atomic_inc(&user->usage); | |
118 | spin_unlock(&key_user_lock); | |
a7f988ba | 119 | kfree(candidate); |
1da177e4 LT |
120 | out: |
121 | return user; | |
122 | ||
123 | } /* end key_user_lookup() */ | |
124 | ||
125 | /*****************************************************************************/ | |
126 | /* | |
127 | * dispose of a user structure | |
128 | */ | |
129 | void key_user_put(struct key_user *user) | |
130 | { | |
131 | if (atomic_dec_and_lock(&user->usage, &key_user_lock)) { | |
132 | rb_erase(&user->node, &key_user_tree); | |
133 | spin_unlock(&key_user_lock); | |
134 | ||
135 | kfree(user); | |
136 | } | |
137 | ||
138 | } /* end key_user_put() */ | |
139 | ||
1da177e4 LT |
140 | /*****************************************************************************/ |
141 | /* | |
142 | * assign a key the next unique serial number | |
e51f6d34 ML |
143 | * - these are assigned randomly to avoid security issues through covert |
144 | * channel problems | |
1da177e4 LT |
145 | */ |
146 | static inline void key_alloc_serial(struct key *key) | |
147 | { | |
148 | struct rb_node *parent, **p; | |
149 | struct key *xkey; | |
150 | ||
e51f6d34 | 151 | /* propose a random serial number and look for a hole for it in the |
1da177e4 | 152 | * serial number tree */ |
e51f6d34 ML |
153 | do { |
154 | get_random_bytes(&key->serial, sizeof(key->serial)); | |
155 | ||
156 | key->serial >>= 1; /* negative numbers are not permitted */ | |
157 | } while (key->serial < 3); | |
158 | ||
159 | spin_lock(&key_serial_lock); | |
1da177e4 | 160 | |
9ad0830f | 161 | attempt_insertion: |
1da177e4 LT |
162 | parent = NULL; |
163 | p = &key_serial_tree.rb_node; | |
164 | ||
165 | while (*p) { | |
166 | parent = *p; | |
167 | xkey = rb_entry(parent, struct key, serial_node); | |
168 | ||
169 | if (key->serial < xkey->serial) | |
170 | p = &(*p)->rb_left; | |
171 | else if (key->serial > xkey->serial) | |
172 | p = &(*p)->rb_right; | |
173 | else | |
174 | goto serial_exists; | |
175 | } | |
9ad0830f DH |
176 | |
177 | /* we've found a suitable hole - arrange for this key to occupy it */ | |
178 | rb_link_node(&key->serial_node, parent, p); | |
179 | rb_insert_color(&key->serial_node, &key_serial_tree); | |
180 | ||
181 | spin_unlock(&key_serial_lock); | |
182 | return; | |
1da177e4 LT |
183 | |
184 | /* we found a key with the proposed serial number - walk the tree from | |
185 | * that point looking for the next unused serial number */ | |
e51f6d34 | 186 | serial_exists: |
1da177e4 | 187 | for (;;) { |
e51f6d34 | 188 | key->serial++; |
9ad0830f DH |
189 | if (key->serial < 3) { |
190 | key->serial = 3; | |
191 | goto attempt_insertion; | |
192 | } | |
1da177e4 LT |
193 | |
194 | parent = rb_next(parent); | |
195 | if (!parent) | |
9ad0830f | 196 | goto attempt_insertion; |
1da177e4 LT |
197 | |
198 | xkey = rb_entry(parent, struct key, serial_node); | |
199 | if (key->serial < xkey->serial) | |
9ad0830f | 200 | goto attempt_insertion; |
1da177e4 LT |
201 | } |
202 | ||
1da177e4 LT |
203 | } /* end key_alloc_serial() */ |
204 | ||
205 | /*****************************************************************************/ | |
206 | /* | |
207 | * allocate a key of the specified type | |
208 | * - update the user's quota to reflect the existence of the key | |
8d9067bd DH |
209 | * - called from a key-type operation with key_types_sem read-locked by |
210 | * key_create_or_update() | |
211 | * - this prevents unregistration of the key type | |
1da177e4 LT |
212 | * - upon return the key is as yet uninstantiated; the caller needs to either |
213 | * instantiate the key or discard it before returning | |
214 | */ | |
215 | struct key *key_alloc(struct key_type *type, const char *desc, | |
d720024e | 216 | uid_t uid, gid_t gid, struct task_struct *ctx, |
7e047ef5 | 217 | key_perm_t perm, unsigned long flags) |
1da177e4 LT |
218 | { |
219 | struct key_user *user = NULL; | |
220 | struct key *key; | |
221 | size_t desclen, quotalen; | |
29db9190 | 222 | int ret; |
1da177e4 LT |
223 | |
224 | key = ERR_PTR(-EINVAL); | |
225 | if (!desc || !*desc) | |
226 | goto error; | |
227 | ||
228 | desclen = strlen(desc) + 1; | |
229 | quotalen = desclen + type->def_datalen; | |
230 | ||
231 | /* get hold of the key tracking for this user */ | |
232 | user = key_user_lookup(uid); | |
233 | if (!user) | |
234 | goto no_memory_1; | |
235 | ||
236 | /* check that the user's quota permits allocation of another key and | |
237 | * its description */ | |
7e047ef5 | 238 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
1da177e4 | 239 | spin_lock(&user->lock); |
7e047ef5 DH |
240 | if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) { |
241 | if (user->qnkeys + 1 >= KEYQUOTA_MAX_KEYS || | |
242 | user->qnbytes + quotalen >= KEYQUOTA_MAX_BYTES | |
243 | ) | |
244 | goto no_quota; | |
245 | } | |
1da177e4 LT |
246 | |
247 | user->qnkeys++; | |
248 | user->qnbytes += quotalen; | |
249 | spin_unlock(&user->lock); | |
250 | } | |
251 | ||
252 | /* allocate and initialise the key and its description */ | |
e94b1766 | 253 | key = kmem_cache_alloc(key_jar, GFP_KERNEL); |
1da177e4 LT |
254 | if (!key) |
255 | goto no_memory_2; | |
256 | ||
257 | if (desc) { | |
48ad504e | 258 | key->description = kmemdup(desc, desclen, GFP_KERNEL); |
1da177e4 LT |
259 | if (!key->description) |
260 | goto no_memory_3; | |
1da177e4 LT |
261 | } |
262 | ||
263 | atomic_set(&key->usage, 1); | |
1da177e4 LT |
264 | init_rwsem(&key->sem); |
265 | key->type = type; | |
266 | key->user = user; | |
267 | key->quotalen = quotalen; | |
268 | key->datalen = type->def_datalen; | |
269 | key->uid = uid; | |
270 | key->gid = gid; | |
271 | key->perm = perm; | |
272 | key->flags = 0; | |
273 | key->expiry = 0; | |
274 | key->payload.data = NULL; | |
29db9190 | 275 | key->security = NULL; |
1da177e4 | 276 | |
7e047ef5 | 277 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) |
76d8aeab | 278 | key->flags |= 1 << KEY_FLAG_IN_QUOTA; |
1da177e4 LT |
279 | |
280 | memset(&key->type_data, 0, sizeof(key->type_data)); | |
281 | ||
282 | #ifdef KEY_DEBUGGING | |
283 | key->magic = KEY_DEBUG_MAGIC; | |
284 | #endif | |
285 | ||
29db9190 | 286 | /* let the security module know about the key */ |
7e047ef5 | 287 | ret = security_key_alloc(key, ctx, flags); |
29db9190 DH |
288 | if (ret < 0) |
289 | goto security_error; | |
290 | ||
1da177e4 LT |
291 | /* publish the key by giving it a serial number */ |
292 | atomic_inc(&user->nkeys); | |
293 | key_alloc_serial(key); | |
294 | ||
29db9190 | 295 | error: |
1da177e4 LT |
296 | return key; |
297 | ||
29db9190 DH |
298 | security_error: |
299 | kfree(key->description); | |
1da177e4 | 300 | kmem_cache_free(key_jar, key); |
7e047ef5 | 301 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
1da177e4 LT |
302 | spin_lock(&user->lock); |
303 | user->qnkeys--; | |
304 | user->qnbytes -= quotalen; | |
305 | spin_unlock(&user->lock); | |
306 | } | |
307 | key_user_put(user); | |
29db9190 DH |
308 | key = ERR_PTR(ret); |
309 | goto error; | |
310 | ||
311 | no_memory_3: | |
312 | kmem_cache_free(key_jar, key); | |
313 | no_memory_2: | |
7e047ef5 | 314 | if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) { |
29db9190 DH |
315 | spin_lock(&user->lock); |
316 | user->qnkeys--; | |
317 | user->qnbytes -= quotalen; | |
318 | spin_unlock(&user->lock); | |
319 | } | |
320 | key_user_put(user); | |
321 | no_memory_1: | |
1da177e4 LT |
322 | key = ERR_PTR(-ENOMEM); |
323 | goto error; | |
324 | ||
29db9190 | 325 | no_quota: |
1da177e4 LT |
326 | spin_unlock(&user->lock); |
327 | key_user_put(user); | |
328 | key = ERR_PTR(-EDQUOT); | |
329 | goto error; | |
330 | ||
331 | } /* end key_alloc() */ | |
332 | ||
333 | EXPORT_SYMBOL(key_alloc); | |
334 | ||
335 | /*****************************************************************************/ | |
336 | /* | |
337 | * reserve an amount of quota for the key's payload | |
338 | */ | |
339 | int key_payload_reserve(struct key *key, size_t datalen) | |
340 | { | |
341 | int delta = (int) datalen - key->datalen; | |
342 | int ret = 0; | |
343 | ||
344 | key_check(key); | |
345 | ||
346 | /* contemplate the quota adjustment */ | |
76d8aeab | 347 | if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
1da177e4 LT |
348 | spin_lock(&key->user->lock); |
349 | ||
350 | if (delta > 0 && | |
351 | key->user->qnbytes + delta > KEYQUOTA_MAX_BYTES | |
352 | ) { | |
353 | ret = -EDQUOT; | |
354 | } | |
355 | else { | |
356 | key->user->qnbytes += delta; | |
357 | key->quotalen += delta; | |
358 | } | |
359 | spin_unlock(&key->user->lock); | |
360 | } | |
361 | ||
362 | /* change the recorded data length if that didn't generate an error */ | |
363 | if (ret == 0) | |
364 | key->datalen = datalen; | |
365 | ||
366 | return ret; | |
367 | ||
368 | } /* end key_payload_reserve() */ | |
369 | ||
370 | EXPORT_SYMBOL(key_payload_reserve); | |
371 | ||
372 | /*****************************************************************************/ | |
373 | /* | |
374 | * instantiate a key and link it into the target keyring atomically | |
375 | * - called with the target keyring's semaphore writelocked | |
376 | */ | |
377 | static int __key_instantiate_and_link(struct key *key, | |
378 | const void *data, | |
379 | size_t datalen, | |
3e30148c DH |
380 | struct key *keyring, |
381 | struct key *instkey) | |
1da177e4 LT |
382 | { |
383 | int ret, awaken; | |
384 | ||
385 | key_check(key); | |
386 | key_check(keyring); | |
387 | ||
388 | awaken = 0; | |
389 | ret = -EBUSY; | |
390 | ||
76181c13 | 391 | mutex_lock(&key_construction_mutex); |
1da177e4 LT |
392 | |
393 | /* can't instantiate twice */ | |
76d8aeab | 394 | if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
1da177e4 LT |
395 | /* instantiate the key */ |
396 | ret = key->type->instantiate(key, data, datalen); | |
397 | ||
398 | if (ret == 0) { | |
399 | /* mark the key as being instantiated */ | |
1da177e4 | 400 | atomic_inc(&key->user->nikeys); |
76d8aeab | 401 | set_bit(KEY_FLAG_INSTANTIATED, &key->flags); |
1da177e4 | 402 | |
76d8aeab | 403 | if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) |
1da177e4 | 404 | awaken = 1; |
1da177e4 LT |
405 | |
406 | /* and link it into the destination keyring */ | |
407 | if (keyring) | |
408 | ret = __key_link(keyring, key); | |
3e30148c DH |
409 | |
410 | /* disable the authorisation key */ | |
411 | if (instkey) | |
412 | key_revoke(instkey); | |
1da177e4 LT |
413 | } |
414 | } | |
415 | ||
76181c13 | 416 | mutex_unlock(&key_construction_mutex); |
1da177e4 LT |
417 | |
418 | /* wake up anyone waiting for a key to be constructed */ | |
419 | if (awaken) | |
76181c13 | 420 | wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); |
1da177e4 LT |
421 | |
422 | return ret; | |
423 | ||
424 | } /* end __key_instantiate_and_link() */ | |
425 | ||
426 | /*****************************************************************************/ | |
427 | /* | |
428 | * instantiate a key and link it into the target keyring atomically | |
429 | */ | |
430 | int key_instantiate_and_link(struct key *key, | |
431 | const void *data, | |
432 | size_t datalen, | |
3e30148c DH |
433 | struct key *keyring, |
434 | struct key *instkey) | |
1da177e4 LT |
435 | { |
436 | int ret; | |
437 | ||
438 | if (keyring) | |
439 | down_write(&keyring->sem); | |
440 | ||
3e30148c | 441 | ret = __key_instantiate_and_link(key, data, datalen, keyring, instkey); |
1da177e4 LT |
442 | |
443 | if (keyring) | |
444 | up_write(&keyring->sem); | |
445 | ||
446 | return ret; | |
3e30148c | 447 | |
1da177e4 LT |
448 | } /* end key_instantiate_and_link() */ |
449 | ||
450 | EXPORT_SYMBOL(key_instantiate_and_link); | |
451 | ||
452 | /*****************************************************************************/ | |
453 | /* | |
454 | * negatively instantiate a key and link it into the target keyring atomically | |
455 | */ | |
456 | int key_negate_and_link(struct key *key, | |
457 | unsigned timeout, | |
3e30148c DH |
458 | struct key *keyring, |
459 | struct key *instkey) | |
1da177e4 LT |
460 | { |
461 | struct timespec now; | |
462 | int ret, awaken; | |
463 | ||
464 | key_check(key); | |
465 | key_check(keyring); | |
466 | ||
467 | awaken = 0; | |
468 | ret = -EBUSY; | |
469 | ||
470 | if (keyring) | |
471 | down_write(&keyring->sem); | |
472 | ||
76181c13 | 473 | mutex_lock(&key_construction_mutex); |
1da177e4 LT |
474 | |
475 | /* can't instantiate twice */ | |
76d8aeab | 476 | if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
1da177e4 | 477 | /* mark the key as being negatively instantiated */ |
1da177e4 | 478 | atomic_inc(&key->user->nikeys); |
76d8aeab DH |
479 | set_bit(KEY_FLAG_NEGATIVE, &key->flags); |
480 | set_bit(KEY_FLAG_INSTANTIATED, &key->flags); | |
1da177e4 LT |
481 | now = current_kernel_time(); |
482 | key->expiry = now.tv_sec + timeout; | |
483 | ||
76d8aeab | 484 | if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags)) |
1da177e4 | 485 | awaken = 1; |
1da177e4 | 486 | |
1da177e4 LT |
487 | ret = 0; |
488 | ||
489 | /* and link it into the destination keyring */ | |
490 | if (keyring) | |
491 | ret = __key_link(keyring, key); | |
3e30148c DH |
492 | |
493 | /* disable the authorisation key */ | |
494 | if (instkey) | |
495 | key_revoke(instkey); | |
1da177e4 LT |
496 | } |
497 | ||
76181c13 | 498 | mutex_unlock(&key_construction_mutex); |
1da177e4 LT |
499 | |
500 | if (keyring) | |
501 | up_write(&keyring->sem); | |
502 | ||
503 | /* wake up anyone waiting for a key to be constructed */ | |
504 | if (awaken) | |
76181c13 | 505 | wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT); |
1da177e4 LT |
506 | |
507 | return ret; | |
508 | ||
509 | } /* end key_negate_and_link() */ | |
510 | ||
511 | EXPORT_SYMBOL(key_negate_and_link); | |
512 | ||
513 | /*****************************************************************************/ | |
514 | /* | |
515 | * do cleaning up in process context so that we don't have to disable | |
516 | * interrupts all over the place | |
517 | */ | |
65f27f38 | 518 | static void key_cleanup(struct work_struct *work) |
1da177e4 LT |
519 | { |
520 | struct rb_node *_n; | |
521 | struct key *key; | |
522 | ||
523 | go_again: | |
524 | /* look for a dead key in the tree */ | |
525 | spin_lock(&key_serial_lock); | |
526 | ||
527 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { | |
528 | key = rb_entry(_n, struct key, serial_node); | |
529 | ||
530 | if (atomic_read(&key->usage) == 0) | |
531 | goto found_dead_key; | |
532 | } | |
533 | ||
534 | spin_unlock(&key_serial_lock); | |
535 | return; | |
536 | ||
537 | found_dead_key: | |
538 | /* we found a dead key - once we've removed it from the tree, we can | |
539 | * drop the lock */ | |
540 | rb_erase(&key->serial_node, &key_serial_tree); | |
541 | spin_unlock(&key_serial_lock); | |
542 | ||
76d8aeab DH |
543 | key_check(key); |
544 | ||
29db9190 DH |
545 | security_key_free(key); |
546 | ||
1da177e4 | 547 | /* deal with the user's key tracking and quota */ |
76d8aeab | 548 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
1da177e4 LT |
549 | spin_lock(&key->user->lock); |
550 | key->user->qnkeys--; | |
551 | key->user->qnbytes -= key->quotalen; | |
552 | spin_unlock(&key->user->lock); | |
553 | } | |
554 | ||
555 | atomic_dec(&key->user->nkeys); | |
76d8aeab | 556 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) |
1da177e4 LT |
557 | atomic_dec(&key->user->nikeys); |
558 | ||
559 | key_user_put(key->user); | |
560 | ||
561 | /* now throw away the key memory */ | |
562 | if (key->type->destroy) | |
563 | key->type->destroy(key); | |
564 | ||
565 | kfree(key->description); | |
566 | ||
567 | #ifdef KEY_DEBUGGING | |
568 | key->magic = KEY_DEBUG_MAGIC_X; | |
569 | #endif | |
570 | kmem_cache_free(key_jar, key); | |
571 | ||
572 | /* there may, of course, be more than one key to destroy */ | |
573 | goto go_again; | |
574 | ||
575 | } /* end key_cleanup() */ | |
576 | ||
577 | /*****************************************************************************/ | |
578 | /* | |
579 | * dispose of a reference to a key | |
580 | * - when all the references are gone, we schedule the cleanup task to come and | |
581 | * pull it out of the tree in definite process context | |
582 | */ | |
583 | void key_put(struct key *key) | |
584 | { | |
585 | if (key) { | |
586 | key_check(key); | |
587 | ||
588 | if (atomic_dec_and_test(&key->usage)) | |
589 | schedule_work(&key_cleanup_task); | |
590 | } | |
591 | ||
592 | } /* end key_put() */ | |
593 | ||
594 | EXPORT_SYMBOL(key_put); | |
595 | ||
596 | /*****************************************************************************/ | |
597 | /* | |
598 | * find a key by its serial number | |
599 | */ | |
600 | struct key *key_lookup(key_serial_t id) | |
601 | { | |
602 | struct rb_node *n; | |
603 | struct key *key; | |
604 | ||
605 | spin_lock(&key_serial_lock); | |
606 | ||
607 | /* search the tree for the specified key */ | |
608 | n = key_serial_tree.rb_node; | |
609 | while (n) { | |
610 | key = rb_entry(n, struct key, serial_node); | |
611 | ||
612 | if (id < key->serial) | |
613 | n = n->rb_left; | |
614 | else if (id > key->serial) | |
615 | n = n->rb_right; | |
616 | else | |
617 | goto found; | |
618 | } | |
619 | ||
620 | not_found: | |
621 | key = ERR_PTR(-ENOKEY); | |
622 | goto error; | |
623 | ||
624 | found: | |
76d8aeab | 625 | /* pretend it doesn't exist if it's dead */ |
1da177e4 | 626 | if (atomic_read(&key->usage) == 0 || |
76d8aeab | 627 | test_bit(KEY_FLAG_DEAD, &key->flags) || |
1da177e4 LT |
628 | key->type == &key_type_dead) |
629 | goto not_found; | |
630 | ||
631 | /* this races with key_put(), but that doesn't matter since key_put() | |
632 | * doesn't actually change the key | |
633 | */ | |
634 | atomic_inc(&key->usage); | |
635 | ||
636 | error: | |
637 | spin_unlock(&key_serial_lock); | |
638 | return key; | |
639 | ||
640 | } /* end key_lookup() */ | |
641 | ||
642 | /*****************************************************************************/ | |
643 | /* | |
644 | * find and lock the specified key type against removal | |
645 | * - we return with the sem readlocked | |
646 | */ | |
647 | struct key_type *key_type_lookup(const char *type) | |
648 | { | |
649 | struct key_type *ktype; | |
650 | ||
651 | down_read(&key_types_sem); | |
652 | ||
653 | /* look up the key type to see if it's one of the registered kernel | |
654 | * types */ | |
655 | list_for_each_entry(ktype, &key_types_list, link) { | |
656 | if (strcmp(ktype->name, type) == 0) | |
657 | goto found_kernel_type; | |
658 | } | |
659 | ||
660 | up_read(&key_types_sem); | |
661 | ktype = ERR_PTR(-ENOKEY); | |
662 | ||
663 | found_kernel_type: | |
664 | return ktype; | |
665 | ||
666 | } /* end key_type_lookup() */ | |
667 | ||
668 | /*****************************************************************************/ | |
669 | /* | |
670 | * unlock a key type | |
671 | */ | |
672 | void key_type_put(struct key_type *ktype) | |
673 | { | |
674 | up_read(&key_types_sem); | |
675 | ||
676 | } /* end key_type_put() */ | |
677 | ||
678 | /*****************************************************************************/ | |
679 | /* | |
680 | * attempt to update an existing key | |
681 | * - the key has an incremented refcount | |
682 | * - we need to put the key if we get an error | |
683 | */ | |
664cceb0 DH |
684 | static inline key_ref_t __key_update(key_ref_t key_ref, |
685 | const void *payload, size_t plen) | |
1da177e4 | 686 | { |
664cceb0 | 687 | struct key *key = key_ref_to_ptr(key_ref); |
1da177e4 LT |
688 | int ret; |
689 | ||
690 | /* need write permission on the key to update it */ | |
29db9190 DH |
691 | ret = key_permission(key_ref, KEY_WRITE); |
692 | if (ret < 0) | |
1da177e4 LT |
693 | goto error; |
694 | ||
695 | ret = -EEXIST; | |
696 | if (!key->type->update) | |
697 | goto error; | |
698 | ||
699 | down_write(&key->sem); | |
700 | ||
701 | ret = key->type->update(key, payload, plen); | |
76d8aeab | 702 | if (ret == 0) |
1da177e4 | 703 | /* updating a negative key instantiates it */ |
76d8aeab | 704 | clear_bit(KEY_FLAG_NEGATIVE, &key->flags); |
1da177e4 LT |
705 | |
706 | up_write(&key->sem); | |
707 | ||
708 | if (ret < 0) | |
709 | goto error; | |
664cceb0 DH |
710 | out: |
711 | return key_ref; | |
1da177e4 | 712 | |
664cceb0 | 713 | error: |
1da177e4 | 714 | key_put(key); |
664cceb0 | 715 | key_ref = ERR_PTR(ret); |
1da177e4 LT |
716 | goto out; |
717 | ||
718 | } /* end __key_update() */ | |
719 | ||
720 | /*****************************************************************************/ | |
721 | /* | |
722 | * search the specified keyring for a key of the same description; if one is | |
723 | * found, update it, otherwise add a new one | |
724 | */ | |
664cceb0 DH |
725 | key_ref_t key_create_or_update(key_ref_t keyring_ref, |
726 | const char *type, | |
727 | const char *description, | |
728 | const void *payload, | |
729 | size_t plen, | |
6b79ccb5 | 730 | key_perm_t perm, |
7e047ef5 | 731 | unsigned long flags) |
1da177e4 LT |
732 | { |
733 | struct key_type *ktype; | |
664cceb0 | 734 | struct key *keyring, *key = NULL; |
664cceb0 | 735 | key_ref_t key_ref; |
1da177e4 LT |
736 | int ret; |
737 | ||
1da177e4 LT |
738 | /* look up the key type to see if it's one of the registered kernel |
739 | * types */ | |
740 | ktype = key_type_lookup(type); | |
741 | if (IS_ERR(ktype)) { | |
664cceb0 | 742 | key_ref = ERR_PTR(-ENODEV); |
1da177e4 LT |
743 | goto error; |
744 | } | |
745 | ||
664cceb0 | 746 | key_ref = ERR_PTR(-EINVAL); |
1da177e4 LT |
747 | if (!ktype->match || !ktype->instantiate) |
748 | goto error_2; | |
749 | ||
664cceb0 DH |
750 | keyring = key_ref_to_ptr(keyring_ref); |
751 | ||
752 | key_check(keyring); | |
753 | ||
c3a9d654 DH |
754 | key_ref = ERR_PTR(-ENOTDIR); |
755 | if (keyring->type != &key_type_keyring) | |
756 | goto error_2; | |
757 | ||
664cceb0 DH |
758 | down_write(&keyring->sem); |
759 | ||
760 | /* if we're going to allocate a new key, we're going to have | |
761 | * to modify the keyring */ | |
29db9190 DH |
762 | ret = key_permission(keyring_ref, KEY_WRITE); |
763 | if (ret < 0) { | |
764 | key_ref = ERR_PTR(ret); | |
664cceb0 | 765 | goto error_3; |
29db9190 | 766 | } |
664cceb0 | 767 | |
1d9b7d97 DH |
768 | /* if it's possible to update this type of key, search for an existing |
769 | * key of the same type and description in the destination keyring and | |
770 | * update that instead if possible | |
1da177e4 | 771 | */ |
1d9b7d97 DH |
772 | if (ktype->update) { |
773 | key_ref = __keyring_search_one(keyring_ref, ktype, description, | |
774 | 0); | |
775 | if (!IS_ERR(key_ref)) | |
776 | goto found_matching_key; | |
777 | } | |
1da177e4 | 778 | |
6b79ccb5 AR |
779 | /* if the client doesn't provide, decide on the permissions we want */ |
780 | if (perm == KEY_PERM_UNDEF) { | |
781 | perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR; | |
782 | perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR; | |
1da177e4 | 783 | |
6b79ccb5 AR |
784 | if (ktype->read) |
785 | perm |= KEY_POS_READ | KEY_USR_READ; | |
1da177e4 | 786 | |
6b79ccb5 AR |
787 | if (ktype == &key_type_keyring || ktype->update) |
788 | perm |= KEY_USR_WRITE; | |
789 | } | |
1da177e4 LT |
790 | |
791 | /* allocate a new key */ | |
792 | key = key_alloc(ktype, description, current->fsuid, current->fsgid, | |
7e047ef5 | 793 | current, perm, flags); |
1da177e4 | 794 | if (IS_ERR(key)) { |
e231c2ee | 795 | key_ref = ERR_CAST(key); |
1da177e4 LT |
796 | goto error_3; |
797 | } | |
798 | ||
799 | /* instantiate it and link it into the target keyring */ | |
3e30148c | 800 | ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL); |
1da177e4 LT |
801 | if (ret < 0) { |
802 | key_put(key); | |
664cceb0 DH |
803 | key_ref = ERR_PTR(ret); |
804 | goto error_3; | |
1da177e4 LT |
805 | } |
806 | ||
664cceb0 DH |
807 | key_ref = make_key_ref(key, is_key_possessed(keyring_ref)); |
808 | ||
1da177e4 LT |
809 | error_3: |
810 | up_write(&keyring->sem); | |
811 | error_2: | |
812 | key_type_put(ktype); | |
813 | error: | |
664cceb0 | 814 | return key_ref; |
1da177e4 LT |
815 | |
816 | found_matching_key: | |
817 | /* we found a matching key, so we're going to try to update it | |
818 | * - we can drop the locks first as we have the key pinned | |
819 | */ | |
820 | up_write(&keyring->sem); | |
821 | key_type_put(ktype); | |
822 | ||
664cceb0 | 823 | key_ref = __key_update(key_ref, payload, plen); |
1da177e4 LT |
824 | goto error; |
825 | ||
826 | } /* end key_create_or_update() */ | |
827 | ||
828 | EXPORT_SYMBOL(key_create_or_update); | |
829 | ||
830 | /*****************************************************************************/ | |
831 | /* | |
832 | * update a key | |
833 | */ | |
664cceb0 | 834 | int key_update(key_ref_t key_ref, const void *payload, size_t plen) |
1da177e4 | 835 | { |
664cceb0 | 836 | struct key *key = key_ref_to_ptr(key_ref); |
1da177e4 LT |
837 | int ret; |
838 | ||
839 | key_check(key); | |
840 | ||
841 | /* the key must be writable */ | |
29db9190 DH |
842 | ret = key_permission(key_ref, KEY_WRITE); |
843 | if (ret < 0) | |
1da177e4 LT |
844 | goto error; |
845 | ||
846 | /* attempt to update it if supported */ | |
847 | ret = -EOPNOTSUPP; | |
848 | if (key->type->update) { | |
849 | down_write(&key->sem); | |
1da177e4 | 850 | |
29db9190 | 851 | ret = key->type->update(key, payload, plen); |
76d8aeab | 852 | if (ret == 0) |
1da177e4 | 853 | /* updating a negative key instantiates it */ |
76d8aeab | 854 | clear_bit(KEY_FLAG_NEGATIVE, &key->flags); |
1da177e4 LT |
855 | |
856 | up_write(&key->sem); | |
857 | } | |
858 | ||
859 | error: | |
860 | return ret; | |
861 | ||
862 | } /* end key_update() */ | |
863 | ||
864 | EXPORT_SYMBOL(key_update); | |
865 | ||
1da177e4 LT |
866 | /*****************************************************************************/ |
867 | /* | |
868 | * revoke a key | |
869 | */ | |
870 | void key_revoke(struct key *key) | |
871 | { | |
872 | key_check(key); | |
873 | ||
76181c13 DH |
874 | /* make sure no one's trying to change or use the key when we mark it |
875 | * - we tell lockdep that we might nest because we might be revoking an | |
876 | * authorisation key whilst holding the sem on a key we've just | |
877 | * instantiated | |
878 | */ | |
879 | down_write_nested(&key->sem, 1); | |
880 | if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) && | |
881 | key->type->revoke) | |
04c567d9 DH |
882 | key->type->revoke(key); |
883 | ||
1da177e4 LT |
884 | up_write(&key->sem); |
885 | ||
886 | } /* end key_revoke() */ | |
887 | ||
888 | EXPORT_SYMBOL(key_revoke); | |
889 | ||
890 | /*****************************************************************************/ | |
891 | /* | |
892 | * register a type of key | |
893 | */ | |
894 | int register_key_type(struct key_type *ktype) | |
895 | { | |
896 | struct key_type *p; | |
897 | int ret; | |
898 | ||
899 | ret = -EEXIST; | |
900 | down_write(&key_types_sem); | |
901 | ||
902 | /* disallow key types with the same name */ | |
903 | list_for_each_entry(p, &key_types_list, link) { | |
904 | if (strcmp(p->name, ktype->name) == 0) | |
905 | goto out; | |
906 | } | |
907 | ||
908 | /* store the type */ | |
909 | list_add(&ktype->link, &key_types_list); | |
910 | ret = 0; | |
911 | ||
912 | out: | |
913 | up_write(&key_types_sem); | |
914 | return ret; | |
915 | ||
916 | } /* end register_key_type() */ | |
917 | ||
918 | EXPORT_SYMBOL(register_key_type); | |
919 | ||
920 | /*****************************************************************************/ | |
921 | /* | |
922 | * unregister a type of key | |
923 | */ | |
924 | void unregister_key_type(struct key_type *ktype) | |
925 | { | |
926 | struct rb_node *_n; | |
927 | struct key *key; | |
928 | ||
929 | down_write(&key_types_sem); | |
930 | ||
931 | /* withdraw the key type */ | |
932 | list_del_init(&ktype->link); | |
933 | ||
76d8aeab | 934 | /* mark all the keys of this type dead */ |
1da177e4 LT |
935 | spin_lock(&key_serial_lock); |
936 | ||
937 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { | |
938 | key = rb_entry(_n, struct key, serial_node); | |
939 | ||
76d8aeab DH |
940 | if (key->type == ktype) |
941 | key->type = &key_type_dead; | |
942 | } | |
943 | ||
944 | spin_unlock(&key_serial_lock); | |
945 | ||
946 | /* make sure everyone revalidates their keys */ | |
b2b18660 | 947 | synchronize_rcu(); |
76d8aeab DH |
948 | |
949 | /* we should now be able to destroy the payloads of all the keys of | |
950 | * this type with impunity */ | |
951 | spin_lock(&key_serial_lock); | |
1da177e4 | 952 | |
76d8aeab DH |
953 | for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) { |
954 | key = rb_entry(_n, struct key, serial_node); | |
1da177e4 | 955 | |
76d8aeab DH |
956 | if (key->type == ktype) { |
957 | if (ktype->destroy) | |
958 | ktype->destroy(key); | |
a7807a32 | 959 | memset(&key->payload, KEY_DESTROY, sizeof(key->payload)); |
76d8aeab | 960 | } |
1da177e4 LT |
961 | } |
962 | ||
963 | spin_unlock(&key_serial_lock); | |
964 | up_write(&key_types_sem); | |
965 | ||
966 | } /* end unregister_key_type() */ | |
967 | ||
968 | EXPORT_SYMBOL(unregister_key_type); | |
969 | ||
970 | /*****************************************************************************/ | |
971 | /* | |
972 | * initialise the key management stuff | |
973 | */ | |
974 | void __init key_init(void) | |
975 | { | |
976 | /* allocate a slab in which we can store keys */ | |
977 | key_jar = kmem_cache_create("key_jar", sizeof(struct key), | |
20c2df83 | 978 | 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); |
1da177e4 LT |
979 | |
980 | /* add the special key types */ | |
981 | list_add_tail(&key_type_keyring.link, &key_types_list); | |
982 | list_add_tail(&key_type_dead.link, &key_types_list); | |
983 | list_add_tail(&key_type_user.link, &key_types_list); | |
984 | ||
985 | /* record the root user tracking */ | |
986 | rb_link_node(&root_key_user.node, | |
987 | NULL, | |
988 | &key_user_tree.rb_node); | |
989 | ||
990 | rb_insert_color(&root_key_user.node, | |
991 | &key_user_tree); | |
992 | ||
1da177e4 | 993 | } /* end key_init() */ |