blob: 3a92d73658705e1a43dd5883798955f3083dc797 [file] [log] [blame]
David Howells76181c12007-10-16 23:29:46 -07001/* Basic authentication token and access key management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells69664cf2008-04-29 01:01:31 -07003 * Copyright (C) 2004-2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Randy Dunlapa7807a32006-06-27 02:53:54 -070014#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/slab.h>
David Howells29db9192005-10-30 15:02:44 -080017#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/workqueue.h>
Michael LeMaye51f6d32006-06-26 00:24:54 -070019#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/err.h>
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060021#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "internal.h"
23
Christoph Lametere18b8902006-12-06 20:33:20 -080024static struct kmem_cache *key_jar;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct rb_root key_serial_tree; /* tree of keys indexed by serial */
26DEFINE_SPINLOCK(key_serial_lock);
27
28struct rb_root key_user_tree; /* tree of quota records indexed by UID */
29DEFINE_SPINLOCK(key_user_lock);
30
David Howells0b77f5b2008-04-29 01:01:32 -070031unsigned int key_quota_root_maxkeys = 200; /* root's key count quota */
32unsigned int key_quota_root_maxbytes = 20000; /* root's key space quota */
33unsigned int key_quota_maxkeys = 200; /* general key count quota */
34unsigned int key_quota_maxbytes = 20000; /* general key space quota */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static LIST_HEAD(key_types_list);
37static DECLARE_RWSEM(key_types_sem);
38
David Howells65f27f32006-11-22 14:55:48 +000039static void key_cleanup(struct work_struct *work);
40static DECLARE_WORK(key_cleanup_task, key_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* we serialise key instantiation and link */
David Howells76181c12007-10-16 23:29:46 -070043DEFINE_MUTEX(key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* any key who's type gets unegistered will be re-typed to this */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080046static struct key_type key_type_dead = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .name = "dead",
48};
49
50#ifdef KEY_DEBUGGING
51void __key_check(const struct key *key)
52{
53 printk("__key_check: key %p {%08x} should be {%08x}\n",
54 key, key->magic, KEY_DEBUG_MAGIC);
55 BUG();
56}
57#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * get the key quota record for a user, allocating a new record if one doesn't
61 * already exist
62 */
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060063struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct key_user *candidate = NULL, *user;
66 struct rb_node *parent = NULL;
67 struct rb_node **p;
68
69 try_again:
70 p = &key_user_tree.rb_node;
71 spin_lock(&key_user_lock);
72
73 /* search the tree for a user record with a matching UID */
74 while (*p) {
75 parent = *p;
76 user = rb_entry(parent, struct key_user, node);
77
78 if (uid < user->uid)
79 p = &(*p)->rb_left;
80 else if (uid > user->uid)
81 p = &(*p)->rb_right;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060082 else if (user_ns < user->user_ns)
83 p = &(*p)->rb_left;
84 else if (user_ns > user->user_ns)
85 p = &(*p)->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 else
87 goto found;
88 }
89
90 /* if we get here, we failed to find a match in the tree */
91 if (!candidate) {
92 /* allocate a candidate user record if we don't already have
93 * one */
94 spin_unlock(&key_user_lock);
95
96 user = NULL;
97 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
98 if (unlikely(!candidate))
99 goto out;
100
101 /* the allocation may have scheduled, so we need to repeat the
102 * search lest someone else added the record whilst we were
103 * asleep */
104 goto try_again;
105 }
106
107 /* if we get here, then the user record still hadn't appeared on the
108 * second pass - so we use the candidate record */
109 atomic_set(&candidate->usage, 1);
110 atomic_set(&candidate->nkeys, 0);
111 atomic_set(&candidate->nikeys, 0);
112 candidate->uid = uid;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600113 candidate->user_ns = get_user_ns(user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 candidate->qnkeys = 0;
115 candidate->qnbytes = 0;
116 spin_lock_init(&candidate->lock);
David Howells76181c12007-10-16 23:29:46 -0700117 mutex_init(&candidate->cons_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 rb_link_node(&candidate->node, parent, p);
120 rb_insert_color(&candidate->node, &key_user_tree);
121 spin_unlock(&key_user_lock);
122 user = candidate;
123 goto out;
124
125 /* okay - we found a user record for this UID */
126 found:
127 atomic_inc(&user->usage);
128 spin_unlock(&key_user_lock);
Jesper Juhla7f988b2005-11-07 01:01:35 -0800129 kfree(candidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 out:
131 return user;
David Howellsa8b17ed2011-01-20 16:38:27 +0000132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/*
135 * dispose of a user structure
136 */
137void key_user_put(struct key_user *user)
138{
139 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
140 rb_erase(&user->node, &key_user_tree);
141 spin_unlock(&key_user_lock);
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600142 put_user_ns(user->user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 kfree(user);
145 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * assign a key the next unique serial number
Michael LeMaye51f6d32006-06-26 00:24:54 -0700150 * - these are assigned randomly to avoid security issues through covert
151 * channel problems
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
153static inline void key_alloc_serial(struct key *key)
154{
155 struct rb_node *parent, **p;
156 struct key *xkey;
157
Michael LeMaye51f6d32006-06-26 00:24:54 -0700158 /* propose a random serial number and look for a hole for it in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * serial number tree */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700160 do {
161 get_random_bytes(&key->serial, sizeof(key->serial));
162
163 key->serial >>= 1; /* negative numbers are not permitted */
164 } while (key->serial < 3);
165
166 spin_lock(&key_serial_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howells9ad08302007-02-06 13:45:51 +0000168attempt_insertion:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 parent = NULL;
170 p = &key_serial_tree.rb_node;
171
172 while (*p) {
173 parent = *p;
174 xkey = rb_entry(parent, struct key, serial_node);
175
176 if (key->serial < xkey->serial)
177 p = &(*p)->rb_left;
178 else if (key->serial > xkey->serial)
179 p = &(*p)->rb_right;
180 else
181 goto serial_exists;
182 }
David Howells9ad08302007-02-06 13:45:51 +0000183
184 /* we've found a suitable hole - arrange for this key to occupy it */
185 rb_link_node(&key->serial_node, parent, p);
186 rb_insert_color(&key->serial_node, &key_serial_tree);
187
188 spin_unlock(&key_serial_lock);
189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* we found a key with the proposed serial number - walk the tree from
192 * that point looking for the next unused serial number */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700193serial_exists:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 for (;;) {
Michael LeMaye51f6d32006-06-26 00:24:54 -0700195 key->serial++;
David Howells9ad08302007-02-06 13:45:51 +0000196 if (key->serial < 3) {
197 key->serial = 3;
198 goto attempt_insertion;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 parent = rb_next(parent);
202 if (!parent)
David Howells9ad08302007-02-06 13:45:51 +0000203 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 xkey = rb_entry(parent, struct key, serial_node);
206 if (key->serial < xkey->serial)
David Howells9ad08302007-02-06 13:45:51 +0000207 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * allocate a key of the specified type
213 * - update the user's quota to reflect the existence of the key
David Howells8d9067b2006-01-06 00:11:24 -0800214 * - called from a key-type operation with key_types_sem read-locked by
215 * key_create_or_update()
216 * - this prevents unregistration of the key type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * - upon return the key is as yet uninstantiated; the caller needs to either
218 * instantiate the key or discard it before returning
219 */
220struct key *key_alloc(struct key_type *type, const char *desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100221 uid_t uid, gid_t gid, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -0700222 key_perm_t perm, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct key_user *user = NULL;
225 struct key *key;
226 size_t desclen, quotalen;
David Howells29db9192005-10-30 15:02:44 -0800227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 key = ERR_PTR(-EINVAL);
230 if (!desc || !*desc)
231 goto error;
232
233 desclen = strlen(desc) + 1;
234 quotalen = desclen + type->def_datalen;
235
236 /* get hold of the key tracking for this user */
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600237 user = key_user_lookup(uid, cred->user->user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!user)
239 goto no_memory_1;
240
241 /* check that the user's quota permits allocation of another key and
242 * its description */
David Howells7e047ef2006-06-26 00:24:50 -0700243 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700244 unsigned maxkeys = (uid == 0) ?
245 key_quota_root_maxkeys : key_quota_maxkeys;
246 unsigned maxbytes = (uid == 0) ?
247 key_quota_root_maxbytes : key_quota_maxbytes;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_lock(&user->lock);
David Howells7e047ef2006-06-26 00:24:50 -0700250 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700251 if (user->qnkeys + 1 >= maxkeys ||
252 user->qnbytes + quotalen >= maxbytes ||
253 user->qnbytes + quotalen < user->qnbytes)
David Howells7e047ef2006-06-26 00:24:50 -0700254 goto no_quota;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 user->qnkeys++;
258 user->qnbytes += quotalen;
259 spin_unlock(&user->lock);
260 }
261
262 /* allocate and initialise the key and its description */
Christoph Lametere94b1762006-12-06 20:33:17 -0800263 key = kmem_cache_alloc(key_jar, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!key)
265 goto no_memory_2;
266
267 if (desc) {
Eric Sesterhenn48ad5042006-12-06 20:33:47 -0800268 key->description = kmemdup(desc, desclen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!key->description)
270 goto no_memory_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 atomic_set(&key->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 init_rwsem(&key->sem);
275 key->type = type;
276 key->user = user;
277 key->quotalen = quotalen;
278 key->datalen = type->def_datalen;
279 key->uid = uid;
280 key->gid = gid;
281 key->perm = perm;
282 key->flags = 0;
283 key->expiry = 0;
284 key->payload.data = NULL;
David Howells29db9192005-10-30 15:02:44 -0800285 key->security = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
David Howells7e047ef2006-06-26 00:24:50 -0700287 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
David Howells76d8aea2005-06-23 22:00:49 -0700288 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 memset(&key->type_data, 0, sizeof(key->type_data));
291
292#ifdef KEY_DEBUGGING
293 key->magic = KEY_DEBUG_MAGIC;
294#endif
295
David Howells29db9192005-10-30 15:02:44 -0800296 /* let the security module know about the key */
David Howellsd84f4f92008-11-14 10:39:23 +1100297 ret = security_key_alloc(key, cred, flags);
David Howells29db9192005-10-30 15:02:44 -0800298 if (ret < 0)
299 goto security_error;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* publish the key by giving it a serial number */
302 atomic_inc(&user->nkeys);
303 key_alloc_serial(key);
304
David Howells29db9192005-10-30 15:02:44 -0800305error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return key;
307
David Howells29db9192005-10-30 15:02:44 -0800308security_error:
309 kfree(key->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 kmem_cache_free(key_jar, key);
David Howells7e047ef2006-06-26 00:24:50 -0700311 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_lock(&user->lock);
313 user->qnkeys--;
314 user->qnbytes -= quotalen;
315 spin_unlock(&user->lock);
316 }
317 key_user_put(user);
David Howells29db9192005-10-30 15:02:44 -0800318 key = ERR_PTR(ret);
319 goto error;
320
321no_memory_3:
322 kmem_cache_free(key_jar, key);
323no_memory_2:
David Howells7e047ef2006-06-26 00:24:50 -0700324 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
David Howells29db9192005-10-30 15:02:44 -0800325 spin_lock(&user->lock);
326 user->qnkeys--;
327 user->qnbytes -= quotalen;
328 spin_unlock(&user->lock);
329 }
330 key_user_put(user);
331no_memory_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 key = ERR_PTR(-ENOMEM);
333 goto error;
334
David Howells29db9192005-10-30 15:02:44 -0800335no_quota:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_unlock(&user->lock);
337 key_user_put(user);
338 key = ERR_PTR(-EDQUOT);
339 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342EXPORT_SYMBOL(key_alloc);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/*
345 * reserve an amount of quota for the key's payload
346 */
347int key_payload_reserve(struct key *key, size_t datalen)
348{
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700349 int delta = (int)datalen - key->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int ret = 0;
351
352 key_check(key);
353
354 /* contemplate the quota adjustment */
David Howells76d8aea2005-06-23 22:00:49 -0700355 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700356 unsigned maxbytes = (key->user->uid == 0) ?
357 key_quota_root_maxbytes : key_quota_maxbytes;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 spin_lock(&key->user->lock);
360
361 if (delta > 0 &&
David Howells0b77f5b2008-04-29 01:01:32 -0700362 (key->user->qnbytes + delta >= maxbytes ||
363 key->user->qnbytes + delta < key->user->qnbytes)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ret = -EDQUOT;
365 }
366 else {
367 key->user->qnbytes += delta;
368 key->quotalen += delta;
369 }
370 spin_unlock(&key->user->lock);
371 }
372
373 /* change the recorded data length if that didn't generate an error */
374 if (ret == 0)
375 key->datalen = datalen;
376
377 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380EXPORT_SYMBOL(key_payload_reserve);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * instantiate a key and link it into the target keyring atomically
384 * - called with the target keyring's semaphore writelocked
385 */
386static int __key_instantiate_and_link(struct key *key,
387 const void *data,
388 size_t datalen,
David Howells3e301482005-06-23 22:00:56 -0700389 struct key *keyring,
David Howellsf70e2e02010-04-30 14:32:39 +0100390 struct key *authkey,
391 struct keyring_list **_prealloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int ret, awaken;
394
395 key_check(key);
396 key_check(keyring);
397
398 awaken = 0;
399 ret = -EBUSY;
400
David Howells76181c12007-10-16 23:29:46 -0700401 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* can't instantiate twice */
David Howells76d8aea2005-06-23 22:00:49 -0700404 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* instantiate the key */
406 ret = key->type->instantiate(key, data, datalen);
407
408 if (ret == 0) {
409 /* mark the key as being instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 atomic_inc(&key->user->nikeys);
David Howells76d8aea2005-06-23 22:00:49 -0700411 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Howells76d8aea2005-06-23 22:00:49 -0700413 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* and link it into the destination keyring */
417 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100418 __key_link(keyring, key, _prealloc);
David Howells3e301482005-06-23 22:00:56 -0700419
420 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100421 if (authkey)
422 key_revoke(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
425
David Howells76181c12007-10-16 23:29:46 -0700426 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* wake up anyone waiting for a key to be constructed */
429 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700430 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
436 * instantiate a key and link it into the target keyring atomically
437 */
438int key_instantiate_and_link(struct key *key,
439 const void *data,
440 size_t datalen,
David Howells3e301482005-06-23 22:00:56 -0700441 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100442 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
David Howellsf70e2e02010-04-30 14:32:39 +0100444 struct keyring_list *prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int ret;
446
David Howellsf70e2e02010-04-30 14:32:39 +0100447 if (keyring) {
448 ret = __key_link_begin(keyring, key->type, key->description,
449 &prealloc);
450 if (ret < 0)
451 return ret;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
David Howellsf70e2e02010-04-30 14:32:39 +0100454 ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey,
455 &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100458 __key_link_end(keyring, key->type, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463EXPORT_SYMBOL(key_instantiate_and_link);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
466 * negatively instantiate a key and link it into the target keyring atomically
467 */
468int key_negate_and_link(struct key *key,
469 unsigned timeout,
David Howells3e301482005-06-23 22:00:56 -0700470 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100471 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Howellsf70e2e02010-04-30 14:32:39 +0100473 struct keyring_list *prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct timespec now;
David Howellsf70e2e02010-04-30 14:32:39 +0100475 int ret, awaken, link_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 key_check(key);
478 key_check(keyring);
479
480 awaken = 0;
481 ret = -EBUSY;
482
483 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100484 link_ret = __key_link_begin(keyring, key->type,
485 key->description, &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David Howells76181c12007-10-16 23:29:46 -0700487 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* can't instantiate twice */
David Howells76d8aea2005-06-23 22:00:49 -0700490 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* mark the key as being negatively instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 atomic_inc(&key->user->nikeys);
David Howells76d8aea2005-06-23 22:00:49 -0700493 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
494 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 now = current_kernel_time();
496 key->expiry = now.tv_sec + timeout;
David Howellsc08ef802009-09-14 17:26:13 +0100497 key_schedule_gc(key->expiry + key_gc_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howells76d8aea2005-06-23 22:00:49 -0700499 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ret = 0;
503
504 /* and link it into the destination keyring */
David Howellsf70e2e02010-04-30 14:32:39 +0100505 if (keyring && link_ret == 0)
506 __key_link(keyring, key, &prealloc);
David Howells3e301482005-06-23 22:00:56 -0700507
508 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100509 if (authkey)
510 key_revoke(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
David Howells76181c12007-10-16 23:29:46 -0700513 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100516 __key_link_end(keyring, key->type, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* wake up anyone waiting for a key to be constructed */
519 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700520 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David Howellsf70e2e02010-04-30 14:32:39 +0100522 return ret == 0 ? link_ret : ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525EXPORT_SYMBOL(key_negate_and_link);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * do cleaning up in process context so that we don't have to disable
529 * interrupts all over the place
530 */
David Howells65f27f32006-11-22 14:55:48 +0000531static void key_cleanup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct rb_node *_n;
534 struct key *key;
535
536 go_again:
537 /* look for a dead key in the tree */
538 spin_lock(&key_serial_lock);
539
540 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
541 key = rb_entry(_n, struct key, serial_node);
542
543 if (atomic_read(&key->usage) == 0)
544 goto found_dead_key;
545 }
546
547 spin_unlock(&key_serial_lock);
548 return;
549
550 found_dead_key:
551 /* we found a dead key - once we've removed it from the tree, we can
552 * drop the lock */
553 rb_erase(&key->serial_node, &key_serial_tree);
554 spin_unlock(&key_serial_lock);
555
David Howells76d8aea2005-06-23 22:00:49 -0700556 key_check(key);
557
David Howells29db9192005-10-30 15:02:44 -0800558 security_key_free(key);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* deal with the user's key tracking and quota */
David Howells76d8aea2005-06-23 22:00:49 -0700561 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 spin_lock(&key->user->lock);
563 key->user->qnkeys--;
564 key->user->qnbytes -= key->quotalen;
565 spin_unlock(&key->user->lock);
566 }
567
568 atomic_dec(&key->user->nkeys);
David Howells76d8aea2005-06-23 22:00:49 -0700569 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 atomic_dec(&key->user->nikeys);
571
572 key_user_put(key->user);
573
574 /* now throw away the key memory */
575 if (key->type->destroy)
576 key->type->destroy(key);
577
578 kfree(key->description);
579
580#ifdef KEY_DEBUGGING
581 key->magic = KEY_DEBUG_MAGIC_X;
582#endif
583 kmem_cache_free(key_jar, key);
584
585 /* there may, of course, be more than one key to destroy */
586 goto go_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/*
590 * dispose of a reference to a key
591 * - when all the references are gone, we schedule the cleanup task to come and
592 * pull it out of the tree in definite process context
593 */
594void key_put(struct key *key)
595{
596 if (key) {
597 key_check(key);
598
599 if (atomic_dec_and_test(&key->usage))
600 schedule_work(&key_cleanup_task);
601 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604EXPORT_SYMBOL(key_put);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * find a key by its serial number
608 */
609struct key *key_lookup(key_serial_t id)
610{
611 struct rb_node *n;
612 struct key *key;
613
614 spin_lock(&key_serial_lock);
615
616 /* search the tree for the specified key */
617 n = key_serial_tree.rb_node;
618 while (n) {
619 key = rb_entry(n, struct key, serial_node);
620
621 if (id < key->serial)
622 n = n->rb_left;
623 else if (id > key->serial)
624 n = n->rb_right;
625 else
626 goto found;
627 }
628
629 not_found:
630 key = ERR_PTR(-ENOKEY);
631 goto error;
632
633 found:
David Howells55931222009-09-02 09:13:45 +0100634 /* pretend it doesn't exist if it is awaiting deletion */
635 if (atomic_read(&key->usage) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 goto not_found;
637
638 /* this races with key_put(), but that doesn't matter since key_put()
639 * doesn't actually change the key
640 */
641 atomic_inc(&key->usage);
642
643 error:
644 spin_unlock(&key_serial_lock);
645 return key;
David Howellsa8b17ed2011-01-20 16:38:27 +0000646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * find and lock the specified key type against removal
650 * - we return with the sem readlocked
651 */
652struct key_type *key_type_lookup(const char *type)
653{
654 struct key_type *ktype;
655
656 down_read(&key_types_sem);
657
658 /* look up the key type to see if it's one of the registered kernel
659 * types */
660 list_for_each_entry(ktype, &key_types_list, link) {
661 if (strcmp(ktype->name, type) == 0)
662 goto found_kernel_type;
663 }
664
665 up_read(&key_types_sem);
666 ktype = ERR_PTR(-ENOKEY);
667
668 found_kernel_type:
669 return ktype;
David Howellsa8b17ed2011-01-20 16:38:27 +0000670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/*
673 * unlock a key type
674 */
675void key_type_put(struct key_type *ktype)
676{
677 up_read(&key_types_sem);
David Howellsa8b17ed2011-01-20 16:38:27 +0000678}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/*
681 * attempt to update an existing key
682 * - the key has an incremented refcount
683 * - we need to put the key if we get an error
684 */
David Howells664cceb2005-09-28 17:03:15 +0100685static inline key_ref_t __key_update(key_ref_t key_ref,
686 const void *payload, size_t plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
David Howells664cceb2005-09-28 17:03:15 +0100688 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int ret;
690
691 /* need write permission on the key to update it */
David Howells29db9192005-10-30 15:02:44 -0800692 ret = key_permission(key_ref, KEY_WRITE);
693 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto error;
695
696 ret = -EEXIST;
697 if (!key->type->update)
698 goto error;
699
700 down_write(&key->sem);
701
702 ret = key->type->update(key, payload, plen);
David Howells76d8aea2005-06-23 22:00:49 -0700703 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* updating a negative key instantiates it */
David Howells76d8aea2005-06-23 22:00:49 -0700705 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 up_write(&key->sem);
708
709 if (ret < 0)
710 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100711out:
712 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
David Howells664cceb2005-09-28 17:03:15 +0100714error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100716 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto out;
David Howellsa8b17ed2011-01-20 16:38:27 +0000718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * search the specified keyring for a key of the same description; if one is
722 * found, update it, otherwise add a new one
723 */
David Howells664cceb2005-09-28 17:03:15 +0100724key_ref_t key_create_or_update(key_ref_t keyring_ref,
725 const char *type,
726 const char *description,
727 const void *payload,
728 size_t plen,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700729 key_perm_t perm,
David Howells7e047ef2006-06-26 00:24:50 -0700730 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
David Howellsf70e2e02010-04-30 14:32:39 +0100732 struct keyring_list *prealloc;
David Howellsd84f4f92008-11-14 10:39:23 +1100733 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100735 struct key *keyring, *key = NULL;
David Howells664cceb2005-09-28 17:03:15 +0100736 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int ret;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* look up the key type to see if it's one of the registered kernel
740 * types */
741 ktype = key_type_lookup(type);
742 if (IS_ERR(ktype)) {
David Howells664cceb2005-09-28 17:03:15 +0100743 key_ref = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto error;
745 }
746
David Howells664cceb2005-09-28 17:03:15 +0100747 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!ktype->match || !ktype->instantiate)
749 goto error_2;
750
David Howells664cceb2005-09-28 17:03:15 +0100751 keyring = key_ref_to_ptr(keyring_ref);
752
753 key_check(keyring);
754
David Howellsc3a9d652006-04-10 15:15:21 +0100755 key_ref = ERR_PTR(-ENOTDIR);
756 if (keyring->type != &key_type_keyring)
757 goto error_2;
758
David Howellsf70e2e02010-04-30 14:32:39 +0100759 ret = __key_link_begin(keyring, ktype, description, &prealloc);
760 if (ret < 0)
761 goto error_2;
David Howells664cceb2005-09-28 17:03:15 +0100762
763 /* if we're going to allocate a new key, we're going to have
764 * to modify the keyring */
David Howells29db9192005-10-30 15:02:44 -0800765 ret = key_permission(keyring_ref, KEY_WRITE);
766 if (ret < 0) {
767 key_ref = ERR_PTR(ret);
David Howells664cceb2005-09-28 17:03:15 +0100768 goto error_3;
David Howells29db9192005-10-30 15:02:44 -0800769 }
David Howells664cceb2005-09-28 17:03:15 +0100770
David Howells1d9b7d92006-03-25 03:06:52 -0800771 /* if it's possible to update this type of key, search for an existing
772 * key of the same type and description in the destination keyring and
773 * update that instead if possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
David Howells1d9b7d92006-03-25 03:06:52 -0800775 if (ktype->update) {
776 key_ref = __keyring_search_one(keyring_ref, ktype, description,
777 0);
778 if (!IS_ERR(key_ref))
779 goto found_matching_key;
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700782 /* if the client doesn't provide, decide on the permissions we want */
783 if (perm == KEY_PERM_UNDEF) {
784 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
785 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700787 if (ktype->read)
788 perm |= KEY_POS_READ | KEY_USR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700790 if (ktype == &key_type_keyring || ktype->update)
791 perm |= KEY_USR_WRITE;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* allocate a new key */
David Howellsd84f4f92008-11-14 10:39:23 +1100795 key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
796 perm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800798 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto error_3;
800 }
801
802 /* instantiate it and link it into the target keyring */
David Howellsf70e2e02010-04-30 14:32:39 +0100803 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL,
804 &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (ret < 0) {
806 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100807 key_ref = ERR_PTR(ret);
808 goto error_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
David Howells664cceb2005-09-28 17:03:15 +0100811 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 error_3:
David Howellsf70e2e02010-04-30 14:32:39 +0100814 __key_link_end(keyring, ktype, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 error_2:
816 key_type_put(ktype);
817 error:
David Howells664cceb2005-09-28 17:03:15 +0100818 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 found_matching_key:
821 /* we found a matching key, so we're going to try to update it
822 * - we can drop the locks first as we have the key pinned
823 */
David Howellsf70e2e02010-04-30 14:32:39 +0100824 __key_link_end(keyring, ktype, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 key_type_put(ktype);
826
David Howells664cceb2005-09-28 17:03:15 +0100827 key_ref = __key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831EXPORT_SYMBOL(key_create_or_update);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/*
834 * update a key
835 */
David Howells664cceb2005-09-28 17:03:15 +0100836int key_update(key_ref_t key_ref, const void *payload, size_t plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
David Howells664cceb2005-09-28 17:03:15 +0100838 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int ret;
840
841 key_check(key);
842
843 /* the key must be writable */
David Howells29db9192005-10-30 15:02:44 -0800844 ret = key_permission(key_ref, KEY_WRITE);
845 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 goto error;
847
848 /* attempt to update it if supported */
849 ret = -EOPNOTSUPP;
850 if (key->type->update) {
851 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
David Howells29db9192005-10-30 15:02:44 -0800853 ret = key->type->update(key, payload, plen);
David Howells76d8aea2005-06-23 22:00:49 -0700854 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* updating a negative key instantiates it */
David Howells76d8aea2005-06-23 22:00:49 -0700856 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 up_write(&key->sem);
859 }
860
861 error:
862 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865EXPORT_SYMBOL(key_update);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * revoke a key
869 */
870void key_revoke(struct key *key)
871{
David Howells5d135442009-09-02 09:14:00 +0100872 struct timespec now;
873 time_t time;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 key_check(key);
876
David Howells76181c12007-10-16 23:29:46 -0700877 /* make sure no one's trying to change or use the key when we mark it
878 * - we tell lockdep that we might nest because we might be revoking an
879 * authorisation key whilst holding the sem on a key we've just
880 * instantiated
881 */
882 down_write_nested(&key->sem, 1);
883 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) &&
884 key->type->revoke)
David Howells04c567d2006-06-22 14:47:18 -0700885 key->type->revoke(key);
886
David Howells5d135442009-09-02 09:14:00 +0100887 /* set the death time to no more than the expiry time */
888 now = current_kernel_time();
889 time = now.tv_sec;
890 if (key->revoked_at == 0 || key->revoked_at > time) {
891 key->revoked_at = time;
David Howellsc08ef802009-09-14 17:26:13 +0100892 key_schedule_gc(key->revoked_at + key_gc_delay);
David Howells5d135442009-09-02 09:14:00 +0100893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 up_write(&key->sem);
David Howellsa8b17ed2011-01-20 16:38:27 +0000896}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898EXPORT_SYMBOL(key_revoke);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900/*
901 * register a type of key
902 */
903int register_key_type(struct key_type *ktype)
904{
905 struct key_type *p;
906 int ret;
907
908 ret = -EEXIST;
909 down_write(&key_types_sem);
910
911 /* disallow key types with the same name */
912 list_for_each_entry(p, &key_types_list, link) {
913 if (strcmp(p->name, ktype->name) == 0)
914 goto out;
915 }
916
917 /* store the type */
918 list_add(&ktype->link, &key_types_list);
919 ret = 0;
920
921 out:
922 up_write(&key_types_sem);
923 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926EXPORT_SYMBOL(register_key_type);
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
929 * unregister a type of key
930 */
931void unregister_key_type(struct key_type *ktype)
932{
933 struct rb_node *_n;
934 struct key *key;
935
936 down_write(&key_types_sem);
937
938 /* withdraw the key type */
939 list_del_init(&ktype->link);
940
David Howells76d8aea2005-06-23 22:00:49 -0700941 /* mark all the keys of this type dead */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 spin_lock(&key_serial_lock);
943
944 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
945 key = rb_entry(_n, struct key, serial_node);
946
David Howellsf041ae22009-09-02 09:13:55 +0100947 if (key->type == ktype) {
David Howells76d8aea2005-06-23 22:00:49 -0700948 key->type = &key_type_dead;
David Howellsf041ae22009-09-02 09:13:55 +0100949 set_bit(KEY_FLAG_DEAD, &key->flags);
950 }
David Howells76d8aea2005-06-23 22:00:49 -0700951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
David Howells76d8aea2005-06-23 22:00:49 -0700953 spin_unlock(&key_serial_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
David Howells76d8aea2005-06-23 22:00:49 -0700955 /* make sure everyone revalidates their keys */
Paul E. McKenneyb2b18662005-06-25 14:55:38 -0700956 synchronize_rcu();
David Howells76d8aea2005-06-23 22:00:49 -0700957
958 /* we should now be able to destroy the payloads of all the keys of
959 * this type with impunity */
960 spin_lock(&key_serial_lock);
961
962 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
963 key = rb_entry(_n, struct key, serial_node);
964
965 if (key->type == ktype) {
966 if (ktype->destroy)
967 ktype->destroy(key);
Randy Dunlapa7807a32006-06-27 02:53:54 -0700968 memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
David Howells76d8aea2005-06-23 22:00:49 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
972 spin_unlock(&key_serial_lock);
973 up_write(&key_types_sem);
974
David Howells5d135442009-09-02 09:14:00 +0100975 key_schedule_gc(0);
David Howellsa8b17ed2011-01-20 16:38:27 +0000976}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978EXPORT_SYMBOL(unregister_key_type);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * initialise the key management stuff
982 */
983void __init key_init(void)
984{
985 /* allocate a slab in which we can store keys */
986 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
Paul Mundt20c2df82007-07-20 10:11:58 +0900987 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* add the special key types */
990 list_add_tail(&key_type_keyring.link, &key_types_list);
991 list_add_tail(&key_type_dead.link, &key_types_list);
992 list_add_tail(&key_type_user.link, &key_types_list);
993
994 /* record the root user tracking */
995 rb_link_node(&root_key_user.node,
996 NULL,
997 &key_user_tree.rb_node);
998
999 rb_insert_color(&root_key_user.node,
1000 &key_user_tree);
David Howellsa8b17ed2011-01-20 16:38:27 +00001001}