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