David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 1 | /* Keyring handling |
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-2005, 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> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/slab.h> |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 16 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/err.h> |
David Howells | e9e349b | 2008-11-14 10:39:13 +1100 | [diff] [blame] | 19 | #include <keys/keyring-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/uaccess.h> |
| 21 | #include "internal.h" |
| 22 | |
| 23 | /* |
| 24 | * when plumbing the depths of the key tree, this sets a hard limit set on how |
| 25 | * deep we're willing to go |
| 26 | */ |
| 27 | #define KEYRING_SEARCH_MAX_DEPTH 6 |
| 28 | |
| 29 | /* |
| 30 | * we keep all named keyrings in a hash to speed looking them up |
| 31 | */ |
| 32 | #define KEYRING_NAME_HASH_SIZE (1 << 5) |
| 33 | |
| 34 | static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE]; |
| 35 | static DEFINE_RWLOCK(keyring_name_lock); |
| 36 | |
| 37 | static inline unsigned keyring_hash(const char *desc) |
| 38 | { |
| 39 | unsigned bucket = 0; |
| 40 | |
| 41 | for (; *desc; desc++) |
| 42 | bucket += (unsigned char) *desc; |
| 43 | |
| 44 | return bucket & (KEYRING_NAME_HASH_SIZE - 1); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * the keyring type definition |
| 49 | */ |
| 50 | static int keyring_instantiate(struct key *keyring, |
| 51 | const void *data, size_t datalen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static int keyring_match(const struct key *keyring, const void *criterion); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 53 | static void keyring_revoke(struct key *keyring); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static void keyring_destroy(struct key *keyring); |
| 55 | static void keyring_describe(const struct key *keyring, struct seq_file *m); |
| 56 | static long keyring_read(const struct key *keyring, |
| 57 | char __user *buffer, size_t buflen); |
| 58 | |
| 59 | struct key_type key_type_keyring = { |
| 60 | .name = "keyring", |
| 61 | .def_datalen = sizeof(struct keyring_list), |
| 62 | .instantiate = keyring_instantiate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | .match = keyring_match, |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 64 | .revoke = keyring_revoke, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | .destroy = keyring_destroy, |
| 66 | .describe = keyring_describe, |
| 67 | .read = keyring_read, |
| 68 | }; |
| 69 | |
David Howells | 7318226 | 2007-04-26 15:46:23 -0700 | [diff] [blame] | 70 | EXPORT_SYMBOL(key_type_keyring); |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
| 73 | * semaphore to serialise link/link calls to prevent two link calls in parallel |
| 74 | * introducing a cycle |
| 75 | */ |
Adrian Bunk | 1ae8f40 | 2006-01-06 00:11:25 -0800 | [diff] [blame] | 76 | static DECLARE_RWSEM(keyring_serialise_link_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | /*****************************************************************************/ |
| 79 | /* |
| 80 | * publish the name of a keyring so that it can be found by name (if it has |
| 81 | * one) |
| 82 | */ |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 83 | static void keyring_publish_name(struct key *keyring) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | int bucket; |
| 86 | |
| 87 | if (keyring->description) { |
| 88 | bucket = keyring_hash(keyring->description); |
| 89 | |
| 90 | write_lock(&keyring_name_lock); |
| 91 | |
| 92 | if (!keyring_name_hash[bucket].next) |
| 93 | INIT_LIST_HEAD(&keyring_name_hash[bucket]); |
| 94 | |
| 95 | list_add_tail(&keyring->type_data.link, |
| 96 | &keyring_name_hash[bucket]); |
| 97 | |
| 98 | write_unlock(&keyring_name_lock); |
| 99 | } |
| 100 | |
| 101 | } /* end keyring_publish_name() */ |
| 102 | |
| 103 | /*****************************************************************************/ |
| 104 | /* |
| 105 | * initialise a keyring |
| 106 | * - we object if we were given any data |
| 107 | */ |
| 108 | static int keyring_instantiate(struct key *keyring, |
| 109 | const void *data, size_t datalen) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | ret = -EINVAL; |
| 114 | if (datalen == 0) { |
| 115 | /* make the keyring available by name if it has one */ |
| 116 | keyring_publish_name(keyring); |
| 117 | ret = 0; |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | |
| 122 | } /* end keyring_instantiate() */ |
| 123 | |
| 124 | /*****************************************************************************/ |
| 125 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * match keyrings on their name |
| 127 | */ |
| 128 | static int keyring_match(const struct key *keyring, const void *description) |
| 129 | { |
| 130 | return keyring->description && |
| 131 | strcmp(keyring->description, description) == 0; |
| 132 | |
| 133 | } /* end keyring_match() */ |
| 134 | |
| 135 | /*****************************************************************************/ |
| 136 | /* |
| 137 | * dispose of the data dangling from the corpse of a keyring |
| 138 | */ |
| 139 | static void keyring_destroy(struct key *keyring) |
| 140 | { |
| 141 | struct keyring_list *klist; |
| 142 | int loop; |
| 143 | |
| 144 | if (keyring->description) { |
| 145 | write_lock(&keyring_name_lock); |
David Howells | 94efe72 | 2005-08-04 13:07:07 -0700 | [diff] [blame] | 146 | |
| 147 | if (keyring->type_data.link.next != NULL && |
| 148 | !list_empty(&keyring->type_data.link)) |
| 149 | list_del(&keyring->type_data.link); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | write_unlock(&keyring_name_lock); |
| 152 | } |
| 153 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 154 | klist = rcu_dereference(keyring->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (klist) { |
| 156 | for (loop = klist->nkeys - 1; loop >= 0; loop--) |
| 157 | key_put(klist->keys[loop]); |
| 158 | kfree(klist); |
| 159 | } |
| 160 | |
| 161 | } /* end keyring_destroy() */ |
| 162 | |
| 163 | /*****************************************************************************/ |
| 164 | /* |
| 165 | * describe the keyring |
| 166 | */ |
| 167 | static void keyring_describe(const struct key *keyring, struct seq_file *m) |
| 168 | { |
| 169 | struct keyring_list *klist; |
| 170 | |
| 171 | if (keyring->description) { |
| 172 | seq_puts(m, keyring->description); |
| 173 | } |
| 174 | else { |
| 175 | seq_puts(m, "[anon]"); |
| 176 | } |
| 177 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 178 | rcu_read_lock(); |
| 179 | klist = rcu_dereference(keyring->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (klist) |
| 181 | seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys); |
| 182 | else |
| 183 | seq_puts(m, ": empty"); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 184 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | } /* end keyring_describe() */ |
| 187 | |
| 188 | /*****************************************************************************/ |
| 189 | /* |
| 190 | * read a list of key IDs from the keyring's contents |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 191 | * - the keyring's semaphore is read-locked |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | */ |
| 193 | static long keyring_read(const struct key *keyring, |
| 194 | char __user *buffer, size_t buflen) |
| 195 | { |
| 196 | struct keyring_list *klist; |
| 197 | struct key *key; |
| 198 | size_t qty, tmp; |
| 199 | int loop, ret; |
| 200 | |
| 201 | ret = 0; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 202 | klist = rcu_dereference(keyring->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | if (klist) { |
| 205 | /* calculate how much data we could return */ |
| 206 | qty = klist->nkeys * sizeof(key_serial_t); |
| 207 | |
| 208 | if (buffer && buflen > 0) { |
| 209 | if (buflen > qty) |
| 210 | buflen = qty; |
| 211 | |
| 212 | /* copy the IDs of the subscribed keys into the |
| 213 | * buffer */ |
| 214 | ret = -EFAULT; |
| 215 | |
| 216 | for (loop = 0; loop < klist->nkeys; loop++) { |
| 217 | key = klist->keys[loop]; |
| 218 | |
| 219 | tmp = sizeof(key_serial_t); |
| 220 | if (tmp > buflen) |
| 221 | tmp = buflen; |
| 222 | |
| 223 | if (copy_to_user(buffer, |
| 224 | &key->serial, |
| 225 | tmp) != 0) |
| 226 | goto error; |
| 227 | |
| 228 | buflen -= tmp; |
| 229 | if (buflen == 0) |
| 230 | break; |
| 231 | buffer += tmp; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | ret = qty; |
| 236 | } |
| 237 | |
| 238 | error: |
| 239 | return ret; |
| 240 | |
| 241 | } /* end keyring_read() */ |
| 242 | |
| 243 | /*****************************************************************************/ |
| 244 | /* |
| 245 | * allocate a keyring and link into the destination keyring |
| 246 | */ |
| 247 | struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 248 | const struct cred *cred, unsigned long flags, |
Michael LeMay | d720024 | 2006-06-22 14:47:17 -0700 | [diff] [blame] | 249 | struct key *dest) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | struct key *keyring; |
| 252 | int ret; |
| 253 | |
| 254 | keyring = key_alloc(&key_type_keyring, description, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 255 | uid, gid, cred, |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 256 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 257 | flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | if (!IS_ERR(keyring)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 260 | ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (ret < 0) { |
| 262 | key_put(keyring); |
| 263 | keyring = ERR_PTR(ret); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return keyring; |
| 268 | |
| 269 | } /* end keyring_alloc() */ |
| 270 | |
| 271 | /*****************************************************************************/ |
| 272 | /* |
| 273 | * search the supplied keyring tree for a key that matches the criterion |
| 274 | * - perform a breadth-then-depth search up to the prescribed limit |
| 275 | * - we only find keys on which we have search permission |
| 276 | * - we use the supplied match function to see if the description (or other |
| 277 | * feature of interest) matches |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 278 | * - we rely on RCU to prevent the keyring lists from disappearing on us |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | * - we return -EAGAIN if we didn't find any matching key |
| 280 | * - we return -ENOKEY if we only found negative matching keys |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 281 | * - we propagate the possession attribute from the keyring ref to the key ref |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 283 | key_ref_t keyring_search_aux(key_ref_t keyring_ref, |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 284 | const struct cred *cred, |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 285 | struct key_type *type, |
| 286 | const void *description, |
| 287 | key_match_func_t match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 290 | struct keyring_list *keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | int kix; |
| 292 | } stack[KEYRING_SEARCH_MAX_DEPTH]; |
| 293 | |
| 294 | struct keyring_list *keylist; |
| 295 | struct timespec now; |
Kevin Coffman | dceba99 | 2008-04-29 01:01:22 -0700 | [diff] [blame] | 296 | unsigned long possessed, kflags; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 297 | struct key *keyring, *key; |
| 298 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | long err; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 300 | int sp, kix; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 302 | keyring = key_ref_to_ptr(keyring_ref); |
| 303 | possessed = is_key_possessed(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | key_check(keyring); |
| 305 | |
| 306 | /* top keyring must have search permission to begin the search */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 307 | err = key_task_permission(keyring_ref, cred, KEY_SEARCH); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 308 | if (err < 0) { |
| 309 | key_ref = ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | goto error; |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 313 | key_ref = ERR_PTR(-ENOTDIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (keyring->type != &key_type_keyring) |
| 315 | goto error; |
| 316 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 317 | rcu_read_lock(); |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | now = current_kernel_time(); |
| 320 | err = -EAGAIN; |
| 321 | sp = 0; |
| 322 | |
Kevin Coffman | dceba99 | 2008-04-29 01:01:22 -0700 | [diff] [blame] | 323 | /* firstly we should check to see if this top-level keyring is what we |
| 324 | * are looking for */ |
| 325 | key_ref = ERR_PTR(-EAGAIN); |
| 326 | kflags = keyring->flags; |
| 327 | if (keyring->type == type && match(keyring, description)) { |
| 328 | key = keyring; |
| 329 | |
| 330 | /* check it isn't negative and hasn't expired or been |
| 331 | * revoked */ |
| 332 | if (kflags & (1 << KEY_FLAG_REVOKED)) |
| 333 | goto error_2; |
| 334 | if (key->expiry && now.tv_sec >= key->expiry) |
| 335 | goto error_2; |
| 336 | key_ref = ERR_PTR(-ENOKEY); |
| 337 | if (kflags & (1 << KEY_FLAG_NEGATIVE)) |
| 338 | goto error_2; |
| 339 | goto found; |
| 340 | } |
| 341 | |
| 342 | /* otherwise, the top keyring must not be revoked, expired, or |
| 343 | * negatively instantiated if we are to search it */ |
| 344 | key_ref = ERR_PTR(-EAGAIN); |
| 345 | if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) || |
| 346 | (keyring->expiry && now.tv_sec >= keyring->expiry)) |
| 347 | goto error_2; |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* start processing a new keyring */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 350 | descend: |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 351 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | goto not_this_keyring; |
| 353 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 354 | keylist = rcu_dereference(keyring->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (!keylist) |
| 356 | goto not_this_keyring; |
| 357 | |
| 358 | /* iterate through the keys in this keyring first */ |
| 359 | for (kix = 0; kix < keylist->nkeys; kix++) { |
| 360 | key = keylist->keys[kix]; |
Kevin Coffman | dceba99 | 2008-04-29 01:01:22 -0700 | [diff] [blame] | 361 | kflags = key->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | /* ignore keys not of this type */ |
| 364 | if (key->type != type) |
| 365 | continue; |
| 366 | |
| 367 | /* skip revoked keys and expired keys */ |
Kevin Coffman | dceba99 | 2008-04-29 01:01:22 -0700 | [diff] [blame] | 368 | if (kflags & (1 << KEY_FLAG_REVOKED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | continue; |
| 370 | |
| 371 | if (key->expiry && now.tv_sec >= key->expiry) |
| 372 | continue; |
| 373 | |
| 374 | /* keys that don't match */ |
| 375 | if (!match(key, description)) |
| 376 | continue; |
| 377 | |
| 378 | /* key must have search permissions */ |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 379 | if (key_task_permission(make_key_ref(key, possessed), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 380 | cred, KEY_SEARCH) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | continue; |
| 382 | |
Kevin Coffman | dceba99 | 2008-04-29 01:01:22 -0700 | [diff] [blame] | 383 | /* we set a different error code if we pass a negative key */ |
| 384 | if (kflags & (1 << KEY_FLAG_NEGATIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | err = -ENOKEY; |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | goto found; |
| 390 | } |
| 391 | |
| 392 | /* search through the keyrings nested in this one */ |
| 393 | kix = 0; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 394 | ascend: |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 395 | for (; kix < keylist->nkeys; kix++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | key = keylist->keys[kix]; |
| 397 | if (key->type != &key_type_keyring) |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 398 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /* recursively search nested keyrings |
| 401 | * - only search keyrings for which we have search permission |
| 402 | */ |
| 403 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 404 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
David Howells | 0f6ed7c | 2005-11-07 00:59:30 -0800 | [diff] [blame] | 406 | if (key_task_permission(make_key_ref(key, possessed), |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 407 | cred, KEY_SEARCH) < 0) |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 408 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | /* stack the current position */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 411 | stack[sp].keylist = keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | stack[sp].kix = kix; |
| 413 | sp++; |
| 414 | |
| 415 | /* begin again with the new keyring */ |
| 416 | keyring = key; |
| 417 | goto descend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* the keyring we're looking at was disqualified or didn't contain a |
| 421 | * matching key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 422 | not_this_keyring: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | if (sp > 0) { |
| 424 | /* resume the processing of a keyring higher up in the tree */ |
| 425 | sp--; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 426 | keylist = stack[sp].keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | kix = stack[sp].kix + 1; |
| 428 | goto ascend; |
| 429 | } |
| 430 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 431 | key_ref = ERR_PTR(err); |
| 432 | goto error_2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | /* we found a viable match */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 435 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | atomic_inc(&key->usage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | key_check(key); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 438 | key_ref = make_key_ref(key, possessed); |
| 439 | error_2: |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 440 | rcu_read_unlock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 441 | error: |
| 442 | return key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | } /* end keyring_search_aux() */ |
| 445 | |
| 446 | /*****************************************************************************/ |
| 447 | /* |
| 448 | * search the supplied keyring tree for a key that matches the criterion |
| 449 | * - perform a breadth-then-depth search up to the prescribed limit |
| 450 | * - we only find keys on which we have search permission |
| 451 | * - we readlock the keyrings as we search down the tree |
| 452 | * - we return -EAGAIN if we didn't find any matching key |
| 453 | * - we return -ENOKEY if we only found negative matching keys |
| 454 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 455 | key_ref_t keyring_search(key_ref_t keyring, |
| 456 | struct key_type *type, |
| 457 | const char *description) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 459 | if (!type->match) |
| 460 | return ERR_PTR(-ENOKEY); |
| 461 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 462 | return keyring_search_aux(keyring, current->cred, |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 463 | type, description, type->match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | } /* end keyring_search() */ |
| 466 | |
| 467 | EXPORT_SYMBOL(keyring_search); |
| 468 | |
| 469 | /*****************************************************************************/ |
| 470 | /* |
| 471 | * search the given keyring only (no recursion) |
| 472 | * - keyring must be locked by caller |
David Howells | c3a9d65 | 2006-04-10 15:15:21 +0100 | [diff] [blame] | 473 | * - caller must guarantee that the keyring is a keyring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 475 | key_ref_t __keyring_search_one(key_ref_t keyring_ref, |
| 476 | const struct key_type *ktype, |
| 477 | const char *description, |
| 478 | key_perm_t perm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
| 480 | struct keyring_list *klist; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 481 | unsigned long possessed; |
| 482 | struct key *keyring, *key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | int loop; |
| 484 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 485 | keyring = key_ref_to_ptr(keyring_ref); |
| 486 | possessed = is_key_possessed(keyring_ref); |
| 487 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 488 | rcu_read_lock(); |
| 489 | |
| 490 | klist = rcu_dereference(keyring->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (klist) { |
| 492 | for (loop = 0; loop < klist->nkeys; loop++) { |
| 493 | key = klist->keys[loop]; |
| 494 | |
| 495 | if (key->type == ktype && |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 496 | (!key->type->match || |
| 497 | key->type->match(key, description)) && |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 498 | key_permission(make_key_ref(key, possessed), |
David Howells | db1d1d5 | 2005-12-01 00:51:18 -0800 | [diff] [blame] | 499 | perm) == 0 && |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 500 | !test_bit(KEY_FLAG_REVOKED, &key->flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | ) |
| 502 | goto found; |
| 503 | } |
| 504 | } |
| 505 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 506 | rcu_read_unlock(); |
| 507 | return ERR_PTR(-ENOKEY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | found: |
| 510 | atomic_inc(&key->usage); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 511 | rcu_read_unlock(); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 512 | return make_key_ref(key, possessed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | } /* end __keyring_search_one() */ |
| 515 | |
| 516 | /*****************************************************************************/ |
| 517 | /* |
| 518 | * find a keyring with the specified name |
| 519 | * - all named keyrings are searched |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 520 | * - normally only finds keyrings with search permission for the current process |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | */ |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 522 | struct key *find_keyring_by_name(const char *name, bool skip_perm_check) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | struct key *keyring; |
| 525 | int bucket; |
| 526 | |
| 527 | keyring = ERR_PTR(-EINVAL); |
| 528 | if (!name) |
| 529 | goto error; |
| 530 | |
| 531 | bucket = keyring_hash(name); |
| 532 | |
| 533 | read_lock(&keyring_name_lock); |
| 534 | |
| 535 | if (keyring_name_hash[bucket].next) { |
| 536 | /* search this hash bucket for a keyring with a matching name |
| 537 | * that's readable and that hasn't been revoked */ |
| 538 | list_for_each_entry(keyring, |
| 539 | &keyring_name_hash[bucket], |
| 540 | type_data.link |
| 541 | ) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 542 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | continue; |
| 544 | |
| 545 | if (strcmp(keyring->description, name) != 0) |
| 546 | continue; |
| 547 | |
David Howells | 69664cf | 2008-04-29 01:01:31 -0700 | [diff] [blame] | 548 | if (!skip_perm_check && |
| 549 | key_permission(make_key_ref(keyring, 0), |
David Howells | 0f6ed7c | 2005-11-07 00:59:30 -0800 | [diff] [blame] | 550 | KEY_SEARCH) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | continue; |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | /* we've got a match */ |
| 554 | atomic_inc(&keyring->usage); |
| 555 | read_unlock(&keyring_name_lock); |
| 556 | goto error; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | read_unlock(&keyring_name_lock); |
| 561 | keyring = ERR_PTR(-ENOKEY); |
| 562 | |
| 563 | error: |
| 564 | return keyring; |
| 565 | |
| 566 | } /* end find_keyring_by_name() */ |
| 567 | |
| 568 | /*****************************************************************************/ |
| 569 | /* |
| 570 | * see if a cycle will will be created by inserting acyclic tree B in acyclic |
| 571 | * tree A at the topmost level (ie: as a direct child of A) |
| 572 | * - since we are adding B to A at the top level, checking for cycles should |
| 573 | * just be a matter of seeing if node A is somewhere in tree B |
| 574 | */ |
| 575 | static int keyring_detect_cycle(struct key *A, struct key *B) |
| 576 | { |
| 577 | struct { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 578 | struct keyring_list *keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | int kix; |
| 580 | } stack[KEYRING_SEARCH_MAX_DEPTH]; |
| 581 | |
| 582 | struct keyring_list *keylist; |
| 583 | struct key *subtree, *key; |
| 584 | int sp, kix, ret; |
| 585 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 586 | rcu_read_lock(); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | ret = -EDEADLK; |
| 589 | if (A == B) |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 590 | goto cycle_detected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | subtree = B; |
| 593 | sp = 0; |
| 594 | |
| 595 | /* start processing a new keyring */ |
| 596 | descend: |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 597 | if (test_bit(KEY_FLAG_REVOKED, &subtree->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | goto not_this_keyring; |
| 599 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 600 | keylist = rcu_dereference(subtree->payload.subscriptions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (!keylist) |
| 602 | goto not_this_keyring; |
| 603 | kix = 0; |
| 604 | |
| 605 | ascend: |
| 606 | /* iterate through the remaining keys in this keyring */ |
| 607 | for (; kix < keylist->nkeys; kix++) { |
| 608 | key = keylist->keys[kix]; |
| 609 | |
| 610 | if (key == A) |
| 611 | goto cycle_detected; |
| 612 | |
| 613 | /* recursively check nested keyrings */ |
| 614 | if (key->type == &key_type_keyring) { |
| 615 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) |
| 616 | goto too_deep; |
| 617 | |
| 618 | /* stack the current position */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 619 | stack[sp].keylist = keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | stack[sp].kix = kix; |
| 621 | sp++; |
| 622 | |
| 623 | /* begin again with the new keyring */ |
| 624 | subtree = key; |
| 625 | goto descend; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /* the keyring we're looking at was disqualified or didn't contain a |
| 630 | * matching key */ |
| 631 | not_this_keyring: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if (sp > 0) { |
| 633 | /* resume the checking of a keyring higher up in the tree */ |
| 634 | sp--; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 635 | keylist = stack[sp].keylist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | kix = stack[sp].kix + 1; |
| 637 | goto ascend; |
| 638 | } |
| 639 | |
| 640 | ret = 0; /* no cycles detected */ |
| 641 | |
| 642 | error: |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 643 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return ret; |
| 645 | |
| 646 | too_deep: |
| 647 | ret = -ELOOP; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 648 | goto error; |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | cycle_detected: |
| 651 | ret = -EDEADLK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | goto error; |
| 653 | |
| 654 | } /* end keyring_detect_cycle() */ |
| 655 | |
| 656 | /*****************************************************************************/ |
| 657 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 658 | * dispose of a keyring list after the RCU grace period |
| 659 | */ |
| 660 | static void keyring_link_rcu_disposal(struct rcu_head *rcu) |
| 661 | { |
| 662 | struct keyring_list *klist = |
| 663 | container_of(rcu, struct keyring_list, rcu); |
| 664 | |
| 665 | kfree(klist); |
| 666 | |
| 667 | } /* end keyring_link_rcu_disposal() */ |
| 668 | |
| 669 | /*****************************************************************************/ |
| 670 | /* |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 671 | * dispose of a keyring list after the RCU grace period, freeing the unlinked |
| 672 | * key |
| 673 | */ |
| 674 | static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) |
| 675 | { |
| 676 | struct keyring_list *klist = |
| 677 | container_of(rcu, struct keyring_list, rcu); |
| 678 | |
| 679 | key_put(klist->keys[klist->delkey]); |
| 680 | kfree(klist); |
| 681 | |
| 682 | } /* end keyring_unlink_rcu_disposal() */ |
| 683 | |
| 684 | /*****************************************************************************/ |
| 685 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | * link a key into to a keyring |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 687 | * - must be called with the keyring's semaphore write-locked |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 688 | * - discard already extant link to matching key if there is one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | */ |
| 690 | int __key_link(struct key *keyring, struct key *key) |
| 691 | { |
| 692 | struct keyring_list *klist, *nklist; |
| 693 | unsigned max; |
| 694 | size_t size; |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 695 | int loop, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | ret = -EKEYREVOKED; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 698 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | goto error; |
| 700 | |
| 701 | ret = -ENOTDIR; |
| 702 | if (keyring->type != &key_type_keyring) |
| 703 | goto error; |
| 704 | |
| 705 | /* serialise link/link calls to prevent parallel calls causing a |
| 706 | * cycle when applied to two keyring in opposite orders */ |
| 707 | down_write(&keyring_serialise_link_sem); |
| 708 | |
| 709 | /* check that we aren't going to create a cycle adding one keyring to |
| 710 | * another */ |
| 711 | if (key->type == &key_type_keyring) { |
| 712 | ret = keyring_detect_cycle(keyring, key); |
| 713 | if (ret < 0) |
| 714 | goto error2; |
| 715 | } |
| 716 | |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 717 | /* see if there's a matching key we can displace */ |
| 718 | klist = keyring->payload.subscriptions; |
| 719 | |
| 720 | if (klist && klist->nkeys > 0) { |
| 721 | struct key_type *type = key->type; |
| 722 | |
| 723 | for (loop = klist->nkeys - 1; loop >= 0; loop--) { |
| 724 | if (klist->keys[loop]->type == type && |
| 725 | strcmp(klist->keys[loop]->description, |
| 726 | key->description) == 0 |
| 727 | ) { |
| 728 | /* found a match - replace with new key */ |
| 729 | size = sizeof(struct key *) * klist->maxkeys; |
| 730 | size += sizeof(*klist); |
| 731 | BUG_ON(size > PAGE_SIZE); |
| 732 | |
| 733 | ret = -ENOMEM; |
Eric Sesterhenn | 48ad504 | 2006-12-06 20:33:47 -0800 | [diff] [blame] | 734 | nklist = kmemdup(klist, size, GFP_KERNEL); |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 735 | if (!nklist) |
| 736 | goto error2; |
| 737 | |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 738 | /* replace matched key */ |
| 739 | atomic_inc(&key->usage); |
| 740 | nklist->keys[loop] = key; |
| 741 | |
| 742 | rcu_assign_pointer( |
| 743 | keyring->payload.subscriptions, |
| 744 | nklist); |
| 745 | |
| 746 | /* dispose of the old keyring list and the |
| 747 | * displaced key */ |
| 748 | klist->delkey = loop; |
| 749 | call_rcu(&klist->rcu, |
| 750 | keyring_unlink_rcu_disposal); |
| 751 | |
| 752 | goto done; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | /* check that we aren't going to overrun the user's quota */ |
| 758 | ret = key_payload_reserve(keyring, |
| 759 | keyring->datalen + KEYQUOTA_LINK_BYTES); |
| 760 | if (ret < 0) |
| 761 | goto error2; |
| 762 | |
| 763 | klist = keyring->payload.subscriptions; |
| 764 | |
| 765 | if (klist && klist->nkeys < klist->maxkeys) { |
| 766 | /* there's sufficient slack space to add directly */ |
| 767 | atomic_inc(&key->usage); |
| 768 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 769 | klist->keys[klist->nkeys] = key; |
| 770 | smp_wmb(); |
| 771 | klist->nkeys++; |
| 772 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | else { |
| 775 | /* grow the key list */ |
| 776 | max = 4; |
| 777 | if (klist) |
| 778 | max += klist->maxkeys; |
| 779 | |
| 780 | ret = -ENFILE; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 781 | if (max > 65535) |
| 782 | goto error3; |
David Howells | a4014d8 | 2005-07-07 17:57:03 -0700 | [diff] [blame] | 783 | size = sizeof(*klist) + sizeof(struct key *) * max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (size > PAGE_SIZE) |
| 785 | goto error3; |
| 786 | |
| 787 | ret = -ENOMEM; |
| 788 | nklist = kmalloc(size, GFP_KERNEL); |
| 789 | if (!nklist) |
| 790 | goto error3; |
| 791 | nklist->maxkeys = max; |
| 792 | nklist->nkeys = 0; |
| 793 | |
| 794 | if (klist) { |
| 795 | nklist->nkeys = klist->nkeys; |
| 796 | memcpy(nklist->keys, |
| 797 | klist->keys, |
| 798 | sizeof(struct key *) * klist->nkeys); |
| 799 | } |
| 800 | |
| 801 | /* add the key into the new space */ |
| 802 | atomic_inc(&key->usage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | nklist->keys[nklist->nkeys++] = key; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 804 | |
| 805 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
| 807 | /* dispose of the old keyring list */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 808 | if (klist) |
| 809 | call_rcu(&klist->rcu, keyring_link_rcu_disposal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 812 | done: |
| 813 | ret = 0; |
| 814 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | up_write(&keyring_serialise_link_sem); |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 816 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | return ret; |
| 818 | |
David Howells | cab8eb5 | 2006-01-08 01:02:45 -0800 | [diff] [blame] | 819 | error3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | /* undo the quota changes */ |
| 821 | key_payload_reserve(keyring, |
| 822 | keyring->datalen - KEYQUOTA_LINK_BYTES); |
| 823 | goto error2; |
| 824 | |
| 825 | } /* end __key_link() */ |
| 826 | |
| 827 | /*****************************************************************************/ |
| 828 | /* |
| 829 | * link a key to a keyring |
| 830 | */ |
| 831 | int key_link(struct key *keyring, struct key *key) |
| 832 | { |
| 833 | int ret; |
| 834 | |
| 835 | key_check(keyring); |
| 836 | key_check(key); |
| 837 | |
| 838 | down_write(&keyring->sem); |
| 839 | ret = __key_link(keyring, key); |
| 840 | up_write(&keyring->sem); |
| 841 | |
| 842 | return ret; |
| 843 | |
| 844 | } /* end key_link() */ |
| 845 | |
| 846 | EXPORT_SYMBOL(key_link); |
| 847 | |
| 848 | /*****************************************************************************/ |
| 849 | /* |
| 850 | * unlink the first link to a key from a keyring |
| 851 | */ |
| 852 | int key_unlink(struct key *keyring, struct key *key) |
| 853 | { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 854 | struct keyring_list *klist, *nklist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | int loop, ret; |
| 856 | |
| 857 | key_check(keyring); |
| 858 | key_check(key); |
| 859 | |
| 860 | ret = -ENOTDIR; |
| 861 | if (keyring->type != &key_type_keyring) |
| 862 | goto error; |
| 863 | |
| 864 | down_write(&keyring->sem); |
| 865 | |
| 866 | klist = keyring->payload.subscriptions; |
| 867 | if (klist) { |
| 868 | /* search the keyring for the key */ |
| 869 | for (loop = 0; loop < klist->nkeys; loop++) |
| 870 | if (klist->keys[loop] == key) |
| 871 | goto key_is_present; |
| 872 | } |
| 873 | |
| 874 | up_write(&keyring->sem); |
| 875 | ret = -ENOENT; |
| 876 | goto error; |
| 877 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 878 | key_is_present: |
| 879 | /* we need to copy the key list for RCU purposes */ |
David Howells | a4014d8 | 2005-07-07 17:57:03 -0700 | [diff] [blame] | 880 | nklist = kmalloc(sizeof(*klist) + |
| 881 | sizeof(struct key *) * klist->maxkeys, |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 882 | GFP_KERNEL); |
| 883 | if (!nklist) |
| 884 | goto nomem; |
| 885 | nklist->maxkeys = klist->maxkeys; |
| 886 | nklist->nkeys = klist->nkeys - 1; |
| 887 | |
| 888 | if (loop > 0) |
| 889 | memcpy(&nklist->keys[0], |
| 890 | &klist->keys[0], |
David Howells | a4014d8 | 2005-07-07 17:57:03 -0700 | [diff] [blame] | 891 | loop * sizeof(struct key *)); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 892 | |
| 893 | if (loop < nklist->nkeys) |
| 894 | memcpy(&nklist->keys[loop], |
| 895 | &klist->keys[loop + 1], |
David Howells | a4014d8 | 2005-07-07 17:57:03 -0700 | [diff] [blame] | 896 | (nklist->nkeys - loop) * sizeof(struct key *)); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | /* adjust the user's quota */ |
| 899 | key_payload_reserve(keyring, |
| 900 | keyring->datalen - KEYQUOTA_LINK_BYTES); |
| 901 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 902 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | up_write(&keyring->sem); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 905 | |
| 906 | /* schedule for later cleanup */ |
| 907 | klist->delkey = loop; |
| 908 | call_rcu(&klist->rcu, keyring_unlink_rcu_disposal); |
| 909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | ret = 0; |
| 911 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 912 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | return ret; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 914 | nomem: |
| 915 | ret = -ENOMEM; |
| 916 | up_write(&keyring->sem); |
| 917 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
| 919 | } /* end key_unlink() */ |
| 920 | |
| 921 | EXPORT_SYMBOL(key_unlink); |
| 922 | |
| 923 | /*****************************************************************************/ |
| 924 | /* |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 925 | * dispose of a keyring list after the RCU grace period, releasing the keys it |
| 926 | * links to |
| 927 | */ |
| 928 | static void keyring_clear_rcu_disposal(struct rcu_head *rcu) |
| 929 | { |
| 930 | struct keyring_list *klist; |
| 931 | int loop; |
| 932 | |
| 933 | klist = container_of(rcu, struct keyring_list, rcu); |
| 934 | |
| 935 | for (loop = klist->nkeys - 1; loop >= 0; loop--) |
| 936 | key_put(klist->keys[loop]); |
| 937 | |
| 938 | kfree(klist); |
| 939 | |
| 940 | } /* end keyring_clear_rcu_disposal() */ |
| 941 | |
| 942 | /*****************************************************************************/ |
| 943 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | * clear the specified process keyring |
| 945 | * - implements keyctl(KEYCTL_CLEAR) |
| 946 | */ |
| 947 | int keyring_clear(struct key *keyring) |
| 948 | { |
| 949 | struct keyring_list *klist; |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 950 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | ret = -ENOTDIR; |
| 953 | if (keyring->type == &key_type_keyring) { |
| 954 | /* detach the pointer block with the locks held */ |
| 955 | down_write(&keyring->sem); |
| 956 | |
| 957 | klist = keyring->payload.subscriptions; |
| 958 | if (klist) { |
| 959 | /* adjust the quota */ |
| 960 | key_payload_reserve(keyring, |
| 961 | sizeof(struct keyring_list)); |
| 962 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 963 | rcu_assign_pointer(keyring->payload.subscriptions, |
| 964 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | up_write(&keyring->sem); |
| 968 | |
| 969 | /* free the keys after the locks have been dropped */ |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 970 | if (klist) |
| 971 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
| 973 | ret = 0; |
| 974 | } |
| 975 | |
| 976 | return ret; |
| 977 | |
| 978 | } /* end keyring_clear() */ |
| 979 | |
| 980 | EXPORT_SYMBOL(keyring_clear); |
David Howells | 31204ed | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 981 | |
| 982 | /*****************************************************************************/ |
| 983 | /* |
| 984 | * dispose of the links from a revoked keyring |
| 985 | * - called with the key sem write-locked |
| 986 | */ |
| 987 | static void keyring_revoke(struct key *keyring) |
| 988 | { |
| 989 | struct keyring_list *klist = keyring->payload.subscriptions; |
| 990 | |
| 991 | /* adjust the quota */ |
| 992 | key_payload_reserve(keyring, 0); |
| 993 | |
| 994 | if (klist) { |
| 995 | rcu_assign_pointer(keyring->payload.subscriptions, NULL); |
| 996 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); |
| 997 | } |
| 998 | |
| 999 | } /* end keyring_revoke() */ |