blob: 991a15f1e85faf6a94623f1c5b61347b606ded5c [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
David Howells8bc16de2011-08-22 14:09:11 +010024struct 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 Howells973c9f42011-01-20 16:38:33 +000039/* We serialise key instantiation and link */
David Howells76181c12007-10-16 23:29:46 -070040DEFINE_MUTEX(key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Howells973c9f42011-01-20 16:38:33 +000042/* Any key who's type gets unegistered will be re-typed to this */
Adrian Bunk1ae8f402006-01-06 00:11:25 -080043static struct key_type key_type_dead = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .name = "dead",
45};
46
47#ifdef KEY_DEBUGGING
48void __key_check(const struct key *key)
49{
50 printk("__key_check: key %p {%08x} should be {%08x}\n",
51 key, key->magic, KEY_DEBUG_MAGIC);
52 BUG();
53}
54#endif
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
David Howells973c9f42011-01-20 16:38:33 +000057 * Get the key quota record for a user, allocating a new record if one doesn't
58 * already exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060060struct key_user *key_user_lookup(uid_t uid, struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct key_user *candidate = NULL, *user;
63 struct rb_node *parent = NULL;
64 struct rb_node **p;
65
David Howells973c9f42011-01-20 16:38:33 +000066try_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 p = &key_user_tree.rb_node;
68 spin_lock(&key_user_lock);
69
70 /* search the tree for a user record with a matching UID */
71 while (*p) {
72 parent = *p;
73 user = rb_entry(parent, struct key_user, node);
74
75 if (uid < user->uid)
76 p = &(*p)->rb_left;
77 else if (uid > user->uid)
78 p = &(*p)->rb_right;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060079 else if (user_ns < user->user_ns)
80 p = &(*p)->rb_left;
81 else if (user_ns > user->user_ns)
82 p = &(*p)->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 else
84 goto found;
85 }
86
87 /* if we get here, we failed to find a match in the tree */
88 if (!candidate) {
89 /* allocate a candidate user record if we don't already have
90 * one */
91 spin_unlock(&key_user_lock);
92
93 user = NULL;
94 candidate = kmalloc(sizeof(struct key_user), GFP_KERNEL);
95 if (unlikely(!candidate))
96 goto out;
97
98 /* the allocation may have scheduled, so we need to repeat the
99 * search lest someone else added the record whilst we were
100 * asleep */
101 goto try_again;
102 }
103
104 /* if we get here, then the user record still hadn't appeared on the
105 * second pass - so we use the candidate record */
106 atomic_set(&candidate->usage, 1);
107 atomic_set(&candidate->nkeys, 0);
108 atomic_set(&candidate->nikeys, 0);
109 candidate->uid = uid;
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600110 candidate->user_ns = get_user_ns(user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 candidate->qnkeys = 0;
112 candidate->qnbytes = 0;
113 spin_lock_init(&candidate->lock);
David Howells76181c12007-10-16 23:29:46 -0700114 mutex_init(&candidate->cons_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 rb_link_node(&candidate->node, parent, p);
117 rb_insert_color(&candidate->node, &key_user_tree);
118 spin_unlock(&key_user_lock);
119 user = candidate;
120 goto out;
121
122 /* okay - we found a user record for this UID */
David Howells973c9f42011-01-20 16:38:33 +0000123found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 atomic_inc(&user->usage);
125 spin_unlock(&key_user_lock);
Jesper Juhla7f988b2005-11-07 01:01:35 -0800126 kfree(candidate);
David Howells973c9f42011-01-20 16:38:33 +0000127out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return user;
David Howellsa8b17ed2011-01-20 16:38:27 +0000129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
David Howells973c9f42011-01-20 16:38:33 +0000132 * Dispose of a user structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134void key_user_put(struct key_user *user)
135{
136 if (atomic_dec_and_lock(&user->usage, &key_user_lock)) {
137 rb_erase(&user->node, &key_user_tree);
138 spin_unlock(&key_user_lock);
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600139 put_user_ns(user->user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 kfree(user);
142 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
David Howells973c9f42011-01-20 16:38:33 +0000146 * Allocate a serial number for a key. These are assigned randomly to avoid
147 * security issues through covert channel problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
149static inline void key_alloc_serial(struct key *key)
150{
151 struct rb_node *parent, **p;
152 struct key *xkey;
153
Michael LeMaye51f6d32006-06-26 00:24:54 -0700154 /* propose a random serial number and look for a hole for it in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * serial number tree */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700156 do {
157 get_random_bytes(&key->serial, sizeof(key->serial));
158
159 key->serial >>= 1; /* negative numbers are not permitted */
160 } while (key->serial < 3);
161
162 spin_lock(&key_serial_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David Howells9ad08302007-02-06 13:45:51 +0000164attempt_insertion:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 parent = NULL;
166 p = &key_serial_tree.rb_node;
167
168 while (*p) {
169 parent = *p;
170 xkey = rb_entry(parent, struct key, serial_node);
171
172 if (key->serial < xkey->serial)
173 p = &(*p)->rb_left;
174 else if (key->serial > xkey->serial)
175 p = &(*p)->rb_right;
176 else
177 goto serial_exists;
178 }
David Howells9ad08302007-02-06 13:45:51 +0000179
180 /* we've found a suitable hole - arrange for this key to occupy it */
181 rb_link_node(&key->serial_node, parent, p);
182 rb_insert_color(&key->serial_node, &key_serial_tree);
183
184 spin_unlock(&key_serial_lock);
185 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* we found a key with the proposed serial number - walk the tree from
188 * that point looking for the next unused serial number */
Michael LeMaye51f6d32006-06-26 00:24:54 -0700189serial_exists:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 for (;;) {
Michael LeMaye51f6d32006-06-26 00:24:54 -0700191 key->serial++;
David Howells9ad08302007-02-06 13:45:51 +0000192 if (key->serial < 3) {
193 key->serial = 3;
194 goto attempt_insertion;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 parent = rb_next(parent);
198 if (!parent)
David Howells9ad08302007-02-06 13:45:51 +0000199 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 xkey = rb_entry(parent, struct key, serial_node);
202 if (key->serial < xkey->serial)
David Howells9ad08302007-02-06 13:45:51 +0000203 goto attempt_insertion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
David Howells973c9f42011-01-20 16:38:33 +0000207/**
208 * key_alloc - Allocate a key of the specified type.
209 * @type: The type of key to allocate.
210 * @desc: The key description to allow the key to be searched out.
211 * @uid: The owner of the new key.
212 * @gid: The group ID for the new key's group permissions.
213 * @cred: The credentials specifying UID namespace.
214 * @perm: The permissions mask of the new key.
215 * @flags: Flags specifying quota properties.
216 *
217 * Allocate a key of the specified type with the attributes given. The key is
218 * returned in an uninstantiated state and the caller needs to instantiate the
219 * key before returning.
220 *
221 * The user's key count quota is updated to reflect the creation of the key and
222 * the user's key data quota has the default for the key type reserved. The
223 * instantiation function should amend this as necessary. If insufficient
224 * quota is available, -EDQUOT will be returned.
225 *
226 * The LSM security modules can prevent a key being created, in which case
227 * -EACCES will be returned.
228 *
229 * Returns a pointer to the new key if successful and an error code otherwise.
230 *
231 * Note that the caller needs to ensure the key type isn't uninstantiated.
232 * Internally this can be done by locking key_types_sem. Externally, this can
233 * be done by either never unregistering the key type, or making sure
234 * key_alloc() calls don't race with module unloading.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
236struct key *key_alloc(struct key_type *type, const char *desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100237 uid_t uid, gid_t gid, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -0700238 key_perm_t perm, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct key_user *user = NULL;
241 struct key *key;
242 size_t desclen, quotalen;
David Howells29db9192005-10-30 15:02:44 -0800243 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 key = ERR_PTR(-EINVAL);
246 if (!desc || !*desc)
247 goto error;
248
David Howellsb9fffa32011-03-07 15:05:59 +0000249 if (type->vet_description) {
250 ret = type->vet_description(desc);
251 if (ret < 0) {
252 key = ERR_PTR(ret);
253 goto error;
254 }
255 }
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 desclen = strlen(desc) + 1;
258 quotalen = desclen + type->def_datalen;
259
260 /* get hold of the key tracking for this user */
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600261 user = key_user_lookup(uid, cred->user->user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!user)
263 goto no_memory_1;
264
265 /* check that the user's quota permits allocation of another key and
266 * its description */
David Howells7e047ef2006-06-26 00:24:50 -0700267 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700268 unsigned maxkeys = (uid == 0) ?
269 key_quota_root_maxkeys : key_quota_maxkeys;
270 unsigned maxbytes = (uid == 0) ?
271 key_quota_root_maxbytes : key_quota_maxbytes;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 spin_lock(&user->lock);
David Howells7e047ef2006-06-26 00:24:50 -0700274 if (!(flags & KEY_ALLOC_QUOTA_OVERRUN)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700275 if (user->qnkeys + 1 >= maxkeys ||
276 user->qnbytes + quotalen >= maxbytes ||
277 user->qnbytes + quotalen < user->qnbytes)
David Howells7e047ef2006-06-26 00:24:50 -0700278 goto no_quota;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 user->qnkeys++;
282 user->qnbytes += quotalen;
283 spin_unlock(&user->lock);
284 }
285
286 /* allocate and initialise the key and its description */
Christoph Lametere94b1762006-12-06 20:33:17 -0800287 key = kmem_cache_alloc(key_jar, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!key)
289 goto no_memory_2;
290
291 if (desc) {
Eric Sesterhenn48ad5042006-12-06 20:33:47 -0800292 key->description = kmemdup(desc, desclen, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (!key->description)
294 goto no_memory_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 atomic_set(&key->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 init_rwsem(&key->sem);
299 key->type = type;
300 key->user = user;
301 key->quotalen = quotalen;
302 key->datalen = type->def_datalen;
303 key->uid = uid;
304 key->gid = gid;
305 key->perm = perm;
306 key->flags = 0;
307 key->expiry = 0;
308 key->payload.data = NULL;
David Howells29db9192005-10-30 15:02:44 -0800309 key->security = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Howells7e047ef2006-06-26 00:24:50 -0700311 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA))
David Howells76d8aea2005-06-23 22:00:49 -0700312 key->flags |= 1 << KEY_FLAG_IN_QUOTA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 memset(&key->type_data, 0, sizeof(key->type_data));
315
316#ifdef KEY_DEBUGGING
317 key->magic = KEY_DEBUG_MAGIC;
318#endif
319
David Howells29db9192005-10-30 15:02:44 -0800320 /* let the security module know about the key */
David Howellsd84f4f92008-11-14 10:39:23 +1100321 ret = security_key_alloc(key, cred, flags);
David Howells29db9192005-10-30 15:02:44 -0800322 if (ret < 0)
323 goto security_error;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* publish the key by giving it a serial number */
326 atomic_inc(&user->nkeys);
327 key_alloc_serial(key);
328
David Howells29db9192005-10-30 15:02:44 -0800329error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return key;
331
David Howells29db9192005-10-30 15:02:44 -0800332security_error:
333 kfree(key->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 kmem_cache_free(key_jar, key);
David Howells7e047ef2006-06-26 00:24:50 -0700335 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock(&user->lock);
337 user->qnkeys--;
338 user->qnbytes -= quotalen;
339 spin_unlock(&user->lock);
340 }
341 key_user_put(user);
David Howells29db9192005-10-30 15:02:44 -0800342 key = ERR_PTR(ret);
343 goto error;
344
345no_memory_3:
346 kmem_cache_free(key_jar, key);
347no_memory_2:
David Howells7e047ef2006-06-26 00:24:50 -0700348 if (!(flags & KEY_ALLOC_NOT_IN_QUOTA)) {
David Howells29db9192005-10-30 15:02:44 -0800349 spin_lock(&user->lock);
350 user->qnkeys--;
351 user->qnbytes -= quotalen;
352 spin_unlock(&user->lock);
353 }
354 key_user_put(user);
355no_memory_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 key = ERR_PTR(-ENOMEM);
357 goto error;
358
David Howells29db9192005-10-30 15:02:44 -0800359no_quota:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 spin_unlock(&user->lock);
361 key_user_put(user);
362 key = ERR_PTR(-EDQUOT);
363 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365EXPORT_SYMBOL(key_alloc);
366
David Howells973c9f42011-01-20 16:38:33 +0000367/**
368 * key_payload_reserve - Adjust data quota reservation for the key's payload
369 * @key: The key to make the reservation for.
370 * @datalen: The amount of data payload the caller now wants.
371 *
372 * Adjust the amount of the owning user's key data quota that a key reserves.
373 * If the amount is increased, then -EDQUOT may be returned if there isn't
374 * enough free quota available.
375 *
376 * If successful, 0 is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
378int key_payload_reserve(struct key *key, size_t datalen)
379{
Justin P. Mattockc5b60b52010-04-21 00:02:11 -0700380 int delta = (int)datalen - key->datalen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int ret = 0;
382
383 key_check(key);
384
385 /* contemplate the quota adjustment */
David Howells76d8aea2005-06-23 22:00:49 -0700386 if (delta != 0 && test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
David Howells0b77f5b2008-04-29 01:01:32 -0700387 unsigned maxbytes = (key->user->uid == 0) ?
388 key_quota_root_maxbytes : key_quota_maxbytes;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_lock(&key->user->lock);
391
392 if (delta > 0 &&
David Howells0b77f5b2008-04-29 01:01:32 -0700393 (key->user->qnbytes + delta >= maxbytes ||
394 key->user->qnbytes + delta < key->user->qnbytes)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ret = -EDQUOT;
396 }
397 else {
398 key->user->qnbytes += delta;
399 key->quotalen += delta;
400 }
401 spin_unlock(&key->user->lock);
402 }
403
404 /* change the recorded data length if that didn't generate an error */
405 if (ret == 0)
406 key->datalen = datalen;
407
408 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410EXPORT_SYMBOL(key_payload_reserve);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
David Howells973c9f42011-01-20 16:38:33 +0000413 * Instantiate a key and link it into the target keyring atomically. Must be
414 * called with the target keyring's semaphore writelocked. The target key's
415 * semaphore need not be locked as instantiation is serialised by
416 * key_construction_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418static int __key_instantiate_and_link(struct key *key,
419 const void *data,
420 size_t datalen,
David Howells3e301482005-06-23 22:00:56 -0700421 struct key *keyring,
David Howellsf70e2e02010-04-30 14:32:39 +0100422 struct key *authkey,
David Howellsceb73c12011-01-25 16:34:28 +0000423 unsigned long *_prealloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int ret, awaken;
426
427 key_check(key);
428 key_check(keyring);
429
430 awaken = 0;
431 ret = -EBUSY;
432
David Howells76181c12007-10-16 23:29:46 -0700433 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* can't instantiate twice */
David Howells76d8aea2005-06-23 22:00:49 -0700436 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* instantiate the key */
438 ret = key->type->instantiate(key, data, datalen);
439
440 if (ret == 0) {
441 /* mark the key as being instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 atomic_inc(&key->user->nikeys);
David Howells76d8aea2005-06-23 22:00:49 -0700443 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells76d8aea2005-06-23 22:00:49 -0700445 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* and link it into the destination keyring */
449 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100450 __key_link(keyring, key, _prealloc);
David Howells3e301482005-06-23 22:00:56 -0700451
452 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100453 if (authkey)
454 key_revoke(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 }
457
David Howells76181c12007-10-16 23:29:46 -0700458 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* wake up anyone waiting for a key to be constructed */
461 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700462 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Howells973c9f42011-01-20 16:38:33 +0000467/**
468 * key_instantiate_and_link - Instantiate a key and link it into the keyring.
469 * @key: The key to instantiate.
470 * @data: The data to use to instantiate the keyring.
471 * @datalen: The length of @data.
472 * @keyring: Keyring to create a link in on success (or NULL).
473 * @authkey: The authorisation token permitting instantiation.
474 *
475 * Instantiate a key that's in the uninstantiated state using the provided data
476 * and, if successful, link it in to the destination keyring if one is
477 * supplied.
478 *
479 * If successful, 0 is returned, the authorisation token is revoked and anyone
480 * waiting for the key is woken up. If the key was already instantiated,
481 * -EBUSY will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483int key_instantiate_and_link(struct key *key,
484 const void *data,
485 size_t datalen,
David Howells3e301482005-06-23 22:00:56 -0700486 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100487 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
David Howellsceb73c12011-01-25 16:34:28 +0000489 unsigned long prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int ret;
491
David Howellsf70e2e02010-04-30 14:32:39 +0100492 if (keyring) {
493 ret = __key_link_begin(keyring, key->type, key->description,
494 &prealloc);
495 if (ret < 0)
496 return ret;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howellsf70e2e02010-04-30 14:32:39 +0100499 ret = __key_instantiate_and_link(key, data, datalen, keyring, authkey,
500 &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100503 __key_link_end(keyring, key->type, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000506}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508EXPORT_SYMBOL(key_instantiate_and_link);
509
David Howells973c9f42011-01-20 16:38:33 +0000510/**
David Howellsfdd1b942011-03-07 15:06:09 +0000511 * key_reject_and_link - Negatively instantiate a key and link it into the keyring.
David Howells973c9f42011-01-20 16:38:33 +0000512 * @key: The key to instantiate.
513 * @timeout: The timeout on the negative key.
David Howellsfdd1b942011-03-07 15:06:09 +0000514 * @error: The error to return when the key is hit.
David Howells973c9f42011-01-20 16:38:33 +0000515 * @keyring: Keyring to create a link in on success (or NULL).
516 * @authkey: The authorisation token permitting instantiation.
517 *
518 * Negatively instantiate a key that's in the uninstantiated state and, if
David Howellsfdd1b942011-03-07 15:06:09 +0000519 * successful, set its timeout and stored error and link it in to the
520 * destination keyring if one is supplied. The key and any links to the key
521 * will be automatically garbage collected after the timeout expires.
David Howells973c9f42011-01-20 16:38:33 +0000522 *
523 * Negative keys are used to rate limit repeated request_key() calls by causing
David Howellsfdd1b942011-03-07 15:06:09 +0000524 * them to return the stored error code (typically ENOKEY) until the negative
525 * key expires.
David Howells973c9f42011-01-20 16:38:33 +0000526 *
527 * If successful, 0 is returned, the authorisation token is revoked and anyone
528 * waiting for the key is woken up. If the key was already instantiated,
529 * -EBUSY will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
David Howellsfdd1b942011-03-07 15:06:09 +0000531int key_reject_and_link(struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned timeout,
David Howellsfdd1b942011-03-07 15:06:09 +0000533 unsigned error,
David Howells3e301482005-06-23 22:00:56 -0700534 struct key *keyring,
David Howellsd84f4f92008-11-14 10:39:23 +1100535 struct key *authkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
David Howellsceb73c12011-01-25 16:34:28 +0000537 unsigned long prealloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct timespec now;
David Howellsf70e2e02010-04-30 14:32:39 +0100539 int ret, awaken, link_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 key_check(key);
542 key_check(keyring);
543
544 awaken = 0;
545 ret = -EBUSY;
546
547 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100548 link_ret = __key_link_begin(keyring, key->type,
549 key->description, &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
David Howells76181c12007-10-16 23:29:46 -0700551 mutex_lock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* can't instantiate twice */
David Howells76d8aea2005-06-23 22:00:49 -0700554 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* mark the key as being negatively instantiated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 atomic_inc(&key->user->nikeys);
David Howells76d8aea2005-06-23 22:00:49 -0700557 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
558 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
David Howellsfdd1b942011-03-07 15:06:09 +0000559 key->type_data.reject_error = -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 now = current_kernel_time();
561 key->expiry = now.tv_sec + timeout;
David Howellsc08ef802009-09-14 17:26:13 +0100562 key_schedule_gc(key->expiry + key_gc_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
David Howells76d8aea2005-06-23 22:00:49 -0700564 if (test_and_clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 awaken = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ret = 0;
568
569 /* and link it into the destination keyring */
David Howellsf70e2e02010-04-30 14:32:39 +0100570 if (keyring && link_ret == 0)
571 __key_link(keyring, key, &prealloc);
David Howells3e301482005-06-23 22:00:56 -0700572
573 /* disable the authorisation key */
David Howellsd84f4f92008-11-14 10:39:23 +1100574 if (authkey)
575 key_revoke(authkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
David Howells76181c12007-10-16 23:29:46 -0700578 mutex_unlock(&key_construction_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100581 __key_link_end(keyring, key->type, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* wake up anyone waiting for a key to be constructed */
584 if (awaken)
David Howells76181c12007-10-16 23:29:46 -0700585 wake_up_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David Howellsf70e2e02010-04-30 14:32:39 +0100587 return ret == 0 ? link_ret : ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000588}
David Howellsfdd1b942011-03-07 15:06:09 +0000589EXPORT_SYMBOL(key_reject_and_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
David Howells973c9f42011-01-20 16:38:33 +0000591/**
592 * key_put - Discard a reference to a key.
593 * @key: The key to discard a reference from.
594 *
595 * Discard a reference to a key, and when all the references are gone, we
596 * schedule the cleanup task to come and pull it out of the tree in process
597 * context at some later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599void key_put(struct key *key)
600{
601 if (key) {
602 key_check(key);
603
604 if (atomic_dec_and_test(&key->usage))
David Howells8bc16de2011-08-22 14:09:11 +0100605 schedule_work(&key_gc_unused_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608EXPORT_SYMBOL(key_put);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
David Howells973c9f42011-01-20 16:38:33 +0000611 * Find a key by its serial number.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613struct key *key_lookup(key_serial_t id)
614{
615 struct rb_node *n;
616 struct key *key;
617
618 spin_lock(&key_serial_lock);
619
620 /* search the tree for the specified key */
621 n = key_serial_tree.rb_node;
622 while (n) {
623 key = rb_entry(n, struct key, serial_node);
624
625 if (id < key->serial)
626 n = n->rb_left;
627 else if (id > key->serial)
628 n = n->rb_right;
629 else
630 goto found;
631 }
632
David Howells973c9f42011-01-20 16:38:33 +0000633not_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 key = ERR_PTR(-ENOKEY);
635 goto error;
636
David Howells973c9f42011-01-20 16:38:33 +0000637found:
David Howells55931222009-09-02 09:13:45 +0100638 /* pretend it doesn't exist if it is awaiting deletion */
639 if (atomic_read(&key->usage) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto not_found;
641
642 /* this races with key_put(), but that doesn't matter since key_put()
643 * doesn't actually change the key
644 */
645 atomic_inc(&key->usage);
646
David Howells973c9f42011-01-20 16:38:33 +0000647error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_unlock(&key_serial_lock);
649 return key;
David Howellsa8b17ed2011-01-20 16:38:27 +0000650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/*
David Howells973c9f42011-01-20 16:38:33 +0000653 * Find and lock the specified key type against removal.
654 *
655 * We return with the sem read-locked if successful. If the type wasn't
656 * available -ENOKEY is returned instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658struct key_type *key_type_lookup(const char *type)
659{
660 struct key_type *ktype;
661
662 down_read(&key_types_sem);
663
664 /* look up the key type to see if it's one of the registered kernel
665 * types */
666 list_for_each_entry(ktype, &key_types_list, link) {
667 if (strcmp(ktype->name, type) == 0)
668 goto found_kernel_type;
669 }
670
671 up_read(&key_types_sem);
672 ktype = ERR_PTR(-ENOKEY);
673
David Howells973c9f42011-01-20 16:38:33 +0000674found_kernel_type:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return ktype;
David Howellsa8b17ed2011-01-20 16:38:27 +0000676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678/*
David Howells973c9f42011-01-20 16:38:33 +0000679 * Unlock a key type locked by key_type_lookup().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
681void key_type_put(struct key_type *ktype)
682{
683 up_read(&key_types_sem);
David Howellsa8b17ed2011-01-20 16:38:27 +0000684}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/*
David Howells973c9f42011-01-20 16:38:33 +0000687 * Attempt to update an existing key.
688 *
689 * The key is given to us with an incremented refcount that we need to discard
690 * if we get an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 */
David Howells664cceb2005-09-28 17:03:15 +0100692static inline key_ref_t __key_update(key_ref_t key_ref,
693 const void *payload, size_t plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David Howells664cceb2005-09-28 17:03:15 +0100695 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int ret;
697
698 /* need write permission on the key to update it */
David Howells29db9192005-10-30 15:02:44 -0800699 ret = key_permission(key_ref, KEY_WRITE);
700 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto error;
702
703 ret = -EEXIST;
704 if (!key->type->update)
705 goto error;
706
707 down_write(&key->sem);
708
709 ret = key->type->update(key, payload, plen);
David Howells76d8aea2005-06-23 22:00:49 -0700710 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* updating a negative key instantiates it */
David Howells76d8aea2005-06-23 22:00:49 -0700712 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 up_write(&key->sem);
715
716 if (ret < 0)
717 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100718out:
719 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
David Howells664cceb2005-09-28 17:03:15 +0100721error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100723 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto out;
David Howellsa8b17ed2011-01-20 16:38:27 +0000725}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
David Howells973c9f42011-01-20 16:38:33 +0000727/**
728 * key_create_or_update - Update or create and instantiate a key.
729 * @keyring_ref: A pointer to the destination keyring with possession flag.
730 * @type: The type of key.
731 * @description: The searchable description for the key.
732 * @payload: The data to use to instantiate or update the key.
733 * @plen: The length of @payload.
734 * @perm: The permissions mask for a new key.
735 * @flags: The quota flags for a new key.
736 *
737 * Search the destination keyring for a key of the same description and if one
738 * is found, update it, otherwise create and instantiate a new one and create a
739 * link to it from that keyring.
740 *
741 * If perm is KEY_PERM_UNDEF then an appropriate key permissions mask will be
742 * concocted.
743 *
744 * Returns a pointer to the new key if successful, -ENODEV if the key type
745 * wasn't available, -ENOTDIR if the keyring wasn't a keyring, -EACCES if the
746 * caller isn't permitted to modify the keyring or the LSM did not permit
747 * creation of the key.
748 *
749 * On success, the possession flag from the keyring ref will be tacked on to
750 * the key ref before it is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
David Howells664cceb2005-09-28 17:03:15 +0100752key_ref_t key_create_or_update(key_ref_t keyring_ref,
753 const char *type,
754 const char *description,
755 const void *payload,
756 size_t plen,
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700757 key_perm_t perm,
David Howells7e047ef2006-06-26 00:24:50 -0700758 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
David Howellsceb73c12011-01-25 16:34:28 +0000760 unsigned long prealloc;
David Howellsd84f4f92008-11-14 10:39:23 +1100761 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct key_type *ktype;
David Howells664cceb2005-09-28 17:03:15 +0100763 struct key *keyring, *key = NULL;
David Howells664cceb2005-09-28 17:03:15 +0100764 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int ret;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* look up the key type to see if it's one of the registered kernel
768 * types */
769 ktype = key_type_lookup(type);
770 if (IS_ERR(ktype)) {
David Howells664cceb2005-09-28 17:03:15 +0100771 key_ref = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto error;
773 }
774
David Howells664cceb2005-09-28 17:03:15 +0100775 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!ktype->match || !ktype->instantiate)
777 goto error_2;
778
David Howells664cceb2005-09-28 17:03:15 +0100779 keyring = key_ref_to_ptr(keyring_ref);
780
781 key_check(keyring);
782
David Howellsc3a9d652006-04-10 15:15:21 +0100783 key_ref = ERR_PTR(-ENOTDIR);
784 if (keyring->type != &key_type_keyring)
785 goto error_2;
786
David Howellsf70e2e02010-04-30 14:32:39 +0100787 ret = __key_link_begin(keyring, ktype, description, &prealloc);
788 if (ret < 0)
789 goto error_2;
David Howells664cceb2005-09-28 17:03:15 +0100790
791 /* if we're going to allocate a new key, we're going to have
792 * to modify the keyring */
David Howells29db9192005-10-30 15:02:44 -0800793 ret = key_permission(keyring_ref, KEY_WRITE);
794 if (ret < 0) {
795 key_ref = ERR_PTR(ret);
David Howells664cceb2005-09-28 17:03:15 +0100796 goto error_3;
David Howells29db9192005-10-30 15:02:44 -0800797 }
David Howells664cceb2005-09-28 17:03:15 +0100798
David Howells1d9b7d92006-03-25 03:06:52 -0800799 /* if it's possible to update this type of key, search for an existing
800 * key of the same type and description in the destination keyring and
801 * update that instead if possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
David Howells1d9b7d92006-03-25 03:06:52 -0800803 if (ktype->update) {
804 key_ref = __keyring_search_one(keyring_ref, ktype, description,
805 0);
806 if (!IS_ERR(key_ref))
807 goto found_matching_key;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700810 /* if the client doesn't provide, decide on the permissions we want */
811 if (perm == KEY_PERM_UNDEF) {
812 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
813 perm |= KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_LINK | KEY_USR_SETATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700815 if (ktype->read)
816 perm |= KEY_POS_READ | KEY_USR_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Arun Raghavan6b79ccb2008-04-29 01:01:28 -0700818 if (ktype == &key_type_keyring || ktype->update)
819 perm |= KEY_USR_WRITE;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* allocate a new key */
David Howellsd84f4f92008-11-14 10:39:23 +1100823 key = key_alloc(ktype, description, cred->fsuid, cred->fsgid, cred,
824 perm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800826 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto error_3;
828 }
829
830 /* instantiate it and link it into the target keyring */
David Howellsf70e2e02010-04-30 14:32:39 +0100831 ret = __key_instantiate_and_link(key, payload, plen, keyring, NULL,
832 &prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (ret < 0) {
834 key_put(key);
David Howells664cceb2005-09-28 17:03:15 +0100835 key_ref = ERR_PTR(ret);
836 goto error_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
David Howells664cceb2005-09-28 17:03:15 +0100839 key_ref = make_key_ref(key, is_key_possessed(keyring_ref));
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 error_3:
David Howellsf70e2e02010-04-30 14:32:39 +0100842 __key_link_end(keyring, ktype, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 error_2:
844 key_type_put(ktype);
845 error:
David Howells664cceb2005-09-28 17:03:15 +0100846 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 found_matching_key:
849 /* we found a matching key, so we're going to try to update it
850 * - we can drop the locks first as we have the key pinned
851 */
David Howellsf70e2e02010-04-30 14:32:39 +0100852 __key_link_end(keyring, ktype, prealloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 key_type_put(ktype);
854
David Howells664cceb2005-09-28 17:03:15 +0100855 key_ref = __key_update(key_ref, payload, plen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto error;
David Howellsa8b17ed2011-01-20 16:38:27 +0000857}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858EXPORT_SYMBOL(key_create_or_update);
859
David Howells973c9f42011-01-20 16:38:33 +0000860/**
861 * key_update - Update a key's contents.
862 * @key_ref: The pointer (plus possession flag) to the key.
863 * @payload: The data to be used to update the key.
864 * @plen: The length of @payload.
865 *
866 * Attempt to update the contents of a key with the given payload data. The
867 * caller must be granted Write permission on the key. Negative keys can be
868 * instantiated by this method.
869 *
870 * Returns 0 on success, -EACCES if not permitted and -EOPNOTSUPP if the key
871 * type does not support updating. The key type may return other errors.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
David Howells664cceb2005-09-28 17:03:15 +0100873int key_update(key_ref_t key_ref, const void *payload, size_t plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
David Howells664cceb2005-09-28 17:03:15 +0100875 struct key *key = key_ref_to_ptr(key_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int ret;
877
878 key_check(key);
879
880 /* the key must be writable */
David Howells29db9192005-10-30 15:02:44 -0800881 ret = key_permission(key_ref, KEY_WRITE);
882 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto error;
884
885 /* attempt to update it if supported */
886 ret = -EOPNOTSUPP;
887 if (key->type->update) {
888 down_write(&key->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
David Howells29db9192005-10-30 15:02:44 -0800890 ret = key->type->update(key, payload, plen);
David Howells76d8aea2005-06-23 22:00:49 -0700891 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* updating a negative key instantiates it */
David Howells76d8aea2005-06-23 22:00:49 -0700893 clear_bit(KEY_FLAG_NEGATIVE, &key->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 up_write(&key->sem);
896 }
897
898 error:
899 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000900}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(key_update);
902
David Howells973c9f42011-01-20 16:38:33 +0000903/**
904 * key_revoke - Revoke a key.
905 * @key: The key to be revoked.
906 *
907 * Mark a key as being revoked and ask the type to free up its resources. The
908 * revocation timeout is set and the key and all its links will be
909 * automatically garbage collected after key_gc_delay amount of time if they
910 * are not manually dealt with first.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912void key_revoke(struct key *key)
913{
David Howells5d135442009-09-02 09:14:00 +0100914 struct timespec now;
915 time_t time;
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 key_check(key);
918
David Howells76181c12007-10-16 23:29:46 -0700919 /* make sure no one's trying to change or use the key when we mark it
920 * - we tell lockdep that we might nest because we might be revoking an
921 * authorisation key whilst holding the sem on a key we've just
922 * instantiated
923 */
924 down_write_nested(&key->sem, 1);
925 if (!test_and_set_bit(KEY_FLAG_REVOKED, &key->flags) &&
926 key->type->revoke)
David Howells04c567d2006-06-22 14:47:18 -0700927 key->type->revoke(key);
928
David Howells5d135442009-09-02 09:14:00 +0100929 /* set the death time to no more than the expiry time */
930 now = current_kernel_time();
931 time = now.tv_sec;
932 if (key->revoked_at == 0 || key->revoked_at > time) {
933 key->revoked_at = time;
David Howellsc08ef802009-09-14 17:26:13 +0100934 key_schedule_gc(key->revoked_at + key_gc_delay);
David Howells5d135442009-09-02 09:14:00 +0100935 }
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 up_write(&key->sem);
David Howellsa8b17ed2011-01-20 16:38:27 +0000938}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939EXPORT_SYMBOL(key_revoke);
940
David Howells973c9f42011-01-20 16:38:33 +0000941/**
942 * register_key_type - Register a type of key.
943 * @ktype: The new key type.
944 *
945 * Register a new key type.
946 *
947 * Returns 0 on success or -EEXIST if a type of this name already exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
949int register_key_type(struct key_type *ktype)
950{
951 struct key_type *p;
952 int ret;
953
954 ret = -EEXIST;
955 down_write(&key_types_sem);
956
957 /* disallow key types with the same name */
958 list_for_each_entry(p, &key_types_list, link) {
959 if (strcmp(p->name, ktype->name) == 0)
960 goto out;
961 }
962
963 /* store the type */
964 list_add(&ktype->link, &key_types_list);
965 ret = 0;
966
David Howells973c9f42011-01-20 16:38:33 +0000967out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 up_write(&key_types_sem);
969 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971EXPORT_SYMBOL(register_key_type);
972
David Howells973c9f42011-01-20 16:38:33 +0000973/**
974 * unregister_key_type - Unregister a type of key.
975 * @ktype: The key type.
976 *
977 * Unregister a key type and mark all the extant keys of this type as dead.
978 * Those keys of this type are then destroyed to get rid of their payloads and
979 * they and their links will be garbage collected as soon as possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981void unregister_key_type(struct key_type *ktype)
982{
983 struct rb_node *_n;
984 struct key *key;
985
986 down_write(&key_types_sem);
987
988 /* withdraw the key type */
989 list_del_init(&ktype->link);
990
David Howells76d8aea2005-06-23 22:00:49 -0700991 /* mark all the keys of this type dead */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_lock(&key_serial_lock);
993
994 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
995 key = rb_entry(_n, struct key, serial_node);
996
David Howellsf041ae22009-09-02 09:13:55 +0100997 if (key->type == ktype) {
David Howells76d8aea2005-06-23 22:00:49 -0700998 key->type = &key_type_dead;
David Howellsf041ae22009-09-02 09:13:55 +0100999 set_bit(KEY_FLAG_DEAD, &key->flags);
1000 }
David Howells76d8aea2005-06-23 22:00:49 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
David Howells76d8aea2005-06-23 22:00:49 -07001003 spin_unlock(&key_serial_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
David Howells76d8aea2005-06-23 22:00:49 -07001005 /* make sure everyone revalidates their keys */
Paul E. McKenneyb2b18662005-06-25 14:55:38 -07001006 synchronize_rcu();
David Howells76d8aea2005-06-23 22:00:49 -07001007
1008 /* we should now be able to destroy the payloads of all the keys of
1009 * this type with impunity */
1010 spin_lock(&key_serial_lock);
1011
1012 for (_n = rb_first(&key_serial_tree); _n; _n = rb_next(_n)) {
1013 key = rb_entry(_n, struct key, serial_node);
1014
1015 if (key->type == ktype) {
1016 if (ktype->destroy)
1017 ktype->destroy(key);
Randy Dunlapa7807a32006-06-27 02:53:54 -07001018 memset(&key->payload, KEY_DESTROY, sizeof(key->payload));
David Howells76d8aea2005-06-23 22:00:49 -07001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
1022 spin_unlock(&key_serial_lock);
1023 up_write(&key_types_sem);
1024
David Howells5d135442009-09-02 09:14:00 +01001025 key_schedule_gc(0);
David Howellsa8b17ed2011-01-20 16:38:27 +00001026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027EXPORT_SYMBOL(unregister_key_type);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
David Howells973c9f42011-01-20 16:38:33 +00001030 * Initialise the key management state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
1032void __init key_init(void)
1033{
1034 /* allocate a slab in which we can store keys */
1035 key_jar = kmem_cache_create("key_jar", sizeof(struct key),
Paul Mundt20c2df82007-07-20 10:11:58 +09001036 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /* add the special key types */
1039 list_add_tail(&key_type_keyring.link, &key_types_list);
1040 list_add_tail(&key_type_dead.link, &key_types_list);
1041 list_add_tail(&key_type_user.link, &key_types_list);
1042
1043 /* record the root user tracking */
1044 rb_link_node(&root_key_user.node,
1045 NULL,
1046 &key_user_tree.rb_node);
1047
1048 rb_insert_color(&root_key_user.node,
1049 &key_user_tree);
David Howellsa8b17ed2011-01-20 16:38:27 +00001050}