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