Commit | Line | Data |
---|---|---|
69664cf1 | 1 | /* Keyring handling |
1da177e4 | 2 | * |
69664cf1 | 3 | * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
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> | |
29db9190 | 16 | #include <linux/security.h> |
1da177e4 LT |
17 | #include <linux/seq_file.h> |
18 | #include <linux/err.h> | |
e9e349b0 | 19 | #include <keys/keyring-type.h> |
512ea3bc | 20 | #include <linux/uaccess.h> |
1da177e4 LT |
21 | #include "internal.h" |
22 | ||
f0641cba DH |
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 | ||
1da177e4 | 28 | /* |
973c9f4f DH |
29 | * When plumbing the depths of the key tree, this sets a hard limit |
30 | * set on how deep we're willing to go. | |
1da177e4 LT |
31 | */ |
32 | #define KEYRING_SEARCH_MAX_DEPTH 6 | |
33 | ||
34 | /* | |
973c9f4f | 35 | * We keep all named keyrings in a hash to speed looking them up. |
1da177e4 LT |
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++) | |
c5b60b5e | 47 | bucket += (unsigned char)*desc; |
1da177e4 LT |
48 | |
49 | return bucket & (KEYRING_NAME_HASH_SIZE - 1); | |
50 | } | |
51 | ||
52 | /* | |
973c9f4f DH |
53 | * The keyring key type definition. Keyrings are simply keys of this type and |
54 | * can be treated as ordinary keys in addition to having their own special | |
55 | * operations. | |
1da177e4 LT |
56 | */ |
57 | static int keyring_instantiate(struct key *keyring, | |
58 | const void *data, size_t datalen); | |
1da177e4 | 59 | static int keyring_match(const struct key *keyring, const void *criterion); |
31204ed9 | 60 | static void keyring_revoke(struct key *keyring); |
1da177e4 LT |
61 | static void keyring_destroy(struct key *keyring); |
62 | static void keyring_describe(const struct key *keyring, struct seq_file *m); | |
63 | static long keyring_read(const struct key *keyring, | |
64 | char __user *buffer, size_t buflen); | |
65 | ||
66 | struct key_type key_type_keyring = { | |
67 | .name = "keyring", | |
68 | .def_datalen = sizeof(struct keyring_list), | |
69 | .instantiate = keyring_instantiate, | |
1da177e4 | 70 | .match = keyring_match, |
31204ed9 | 71 | .revoke = keyring_revoke, |
1da177e4 LT |
72 | .destroy = keyring_destroy, |
73 | .describe = keyring_describe, | |
74 | .read = keyring_read, | |
75 | }; | |
7318226e DH |
76 | EXPORT_SYMBOL(key_type_keyring); |
77 | ||
1da177e4 | 78 | /* |
973c9f4f DH |
79 | * Semaphore to serialise link/link calls to prevent two link calls in parallel |
80 | * introducing a cycle. | |
1da177e4 | 81 | */ |
1ae8f407 | 82 | static DECLARE_RWSEM(keyring_serialise_link_sem); |
1da177e4 | 83 | |
1da177e4 | 84 | /* |
973c9f4f DH |
85 | * Publish the name of a keyring so that it can be found by name (if it has |
86 | * one). | |
1da177e4 | 87 | */ |
69664cf1 | 88 | static void keyring_publish_name(struct key *keyring) |
1da177e4 LT |
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 | } | |
a8b17ed0 | 105 | } |
1da177e4 | 106 | |
1da177e4 | 107 | /* |
973c9f4f DH |
108 | * Initialise a keyring. |
109 | * | |
110 | * Returns 0 on success, -EINVAL if given any data. | |
1da177e4 LT |
111 | */ |
112 | static int keyring_instantiate(struct key *keyring, | |
113 | const void *data, size_t datalen) | |
114 | { | |
115 | int ret; | |
116 | ||
117 | ret = -EINVAL; | |
118 | if (datalen == 0) { | |
119 | /* make the keyring available by name if it has one */ | |
120 | keyring_publish_name(keyring); | |
121 | ret = 0; | |
122 | } | |
123 | ||
124 | return ret; | |
a8b17ed0 | 125 | } |
1da177e4 | 126 | |
1da177e4 | 127 | /* |
973c9f4f | 128 | * Match keyrings on their name |
1da177e4 LT |
129 | */ |
130 | static int keyring_match(const struct key *keyring, const void *description) | |
131 | { | |
132 | return keyring->description && | |
133 | strcmp(keyring->description, description) == 0; | |
a8b17ed0 | 134 | } |
1da177e4 | 135 | |
1da177e4 | 136 | /* |
973c9f4f DH |
137 | * Clean up a keyring when it is destroyed. Unpublish its name if it had one |
138 | * and dispose of its data. | |
1da177e4 LT |
139 | */ |
140 | static void keyring_destroy(struct key *keyring) | |
141 | { | |
142 | struct keyring_list *klist; | |
143 | int loop; | |
144 | ||
145 | if (keyring->description) { | |
146 | write_lock(&keyring_name_lock); | |
94efe72f DH |
147 | |
148 | if (keyring->type_data.link.next != NULL && | |
149 | !list_empty(&keyring->type_data.link)) | |
150 | list_del(&keyring->type_data.link); | |
151 | ||
1da177e4 LT |
152 | write_unlock(&keyring_name_lock); |
153 | } | |
154 | ||
e7b0a61b PM |
155 | klist = rcu_dereference_check(keyring->payload.subscriptions, |
156 | rcu_read_lock_held() || | |
157 | atomic_read(&keyring->usage) == 0); | |
1da177e4 LT |
158 | if (klist) { |
159 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
160 | key_put(klist->keys[loop]); | |
161 | kfree(klist); | |
162 | } | |
a8b17ed0 | 163 | } |
1da177e4 | 164 | |
1da177e4 | 165 | /* |
973c9f4f | 166 | * Describe a keyring for /proc. |
1da177e4 LT |
167 | */ |
168 | static void keyring_describe(const struct key *keyring, struct seq_file *m) | |
169 | { | |
170 | struct keyring_list *klist; | |
171 | ||
c8563473 | 172 | if (keyring->description) |
1da177e4 | 173 | seq_puts(m, keyring->description); |
c8563473 | 174 | else |
1da177e4 | 175 | seq_puts(m, "[anon]"); |
1da177e4 | 176 | |
76d8aeab DH |
177 | rcu_read_lock(); |
178 | klist = rcu_dereference(keyring->payload.subscriptions); | |
1da177e4 LT |
179 | if (klist) |
180 | seq_printf(m, ": %u/%u", klist->nkeys, klist->maxkeys); | |
181 | else | |
182 | seq_puts(m, ": empty"); | |
76d8aeab | 183 | rcu_read_unlock(); |
a8b17ed0 | 184 | } |
1da177e4 | 185 | |
1da177e4 | 186 | /* |
973c9f4f DH |
187 | * Read a list of key IDs from the keyring's contents in binary form |
188 | * | |
189 | * The keyring's semaphore is read-locked by the caller. | |
1da177e4 LT |
190 | */ |
191 | static long keyring_read(const struct key *keyring, | |
192 | char __user *buffer, size_t buflen) | |
193 | { | |
194 | struct keyring_list *klist; | |
195 | struct key *key; | |
196 | size_t qty, tmp; | |
197 | int loop, ret; | |
198 | ||
199 | ret = 0; | |
f0641cba | 200 | klist = rcu_dereference_locked_keyring(keyring); |
1da177e4 LT |
201 | if (klist) { |
202 | /* calculate how much data we could return */ | |
203 | qty = klist->nkeys * sizeof(key_serial_t); | |
204 | ||
205 | if (buffer && buflen > 0) { | |
206 | if (buflen > qty) | |
207 | buflen = qty; | |
208 | ||
209 | /* copy the IDs of the subscribed keys into the | |
210 | * buffer */ | |
211 | ret = -EFAULT; | |
212 | ||
213 | for (loop = 0; loop < klist->nkeys; loop++) { | |
214 | key = klist->keys[loop]; | |
215 | ||
216 | tmp = sizeof(key_serial_t); | |
217 | if (tmp > buflen) | |
218 | tmp = buflen; | |
219 | ||
220 | if (copy_to_user(buffer, | |
221 | &key->serial, | |
222 | tmp) != 0) | |
223 | goto error; | |
224 | ||
225 | buflen -= tmp; | |
226 | if (buflen == 0) | |
227 | break; | |
228 | buffer += tmp; | |
229 | } | |
230 | } | |
231 | ||
232 | ret = qty; | |
233 | } | |
234 | ||
c5b60b5e | 235 | error: |
1da177e4 | 236 | return ret; |
a8b17ed0 | 237 | } |
1da177e4 | 238 | |
1da177e4 | 239 | /* |
973c9f4f | 240 | * Allocate a keyring and link into the destination keyring. |
1da177e4 LT |
241 | */ |
242 | struct key *keyring_alloc(const char *description, uid_t uid, gid_t gid, | |
d84f4f99 | 243 | const struct cred *cred, unsigned long flags, |
d720024e | 244 | struct key *dest) |
1da177e4 LT |
245 | { |
246 | struct key *keyring; | |
247 | int ret; | |
248 | ||
249 | keyring = key_alloc(&key_type_keyring, description, | |
d84f4f99 | 250 | uid, gid, cred, |
29db9190 | 251 | (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL, |
7e047ef5 | 252 | flags); |
1da177e4 LT |
253 | |
254 | if (!IS_ERR(keyring)) { | |
3e30148c | 255 | ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL); |
1da177e4 LT |
256 | if (ret < 0) { |
257 | key_put(keyring); | |
258 | keyring = ERR_PTR(ret); | |
259 | } | |
260 | } | |
261 | ||
262 | return keyring; | |
a8b17ed0 | 263 | } |
1da177e4 | 264 | |
973c9f4f DH |
265 | /** |
266 | * keyring_search_aux - Search a keyring tree for a key matching some criteria | |
267 | * @keyring_ref: A pointer to the keyring with possession indicator. | |
268 | * @cred: The credentials to use for permissions checks. | |
269 | * @type: The type of key to search for. | |
270 | * @description: Parameter for @match. | |
271 | * @match: Function to rule on whether or not a key is the one required. | |
272 | * | |
273 | * Search the supplied keyring tree for a key that matches the criteria given. | |
274 | * The root keyring and any linked keyrings must grant Search permission to the | |
275 | * caller to be searchable and keys can only be found if they too grant Search | |
276 | * to the caller. The possession flag on the root keyring pointer controls use | |
277 | * of the possessor bits in permissions checking of the entire tree. In | |
278 | * addition, the LSM gets to forbid keyring searches and key matches. | |
279 | * | |
280 | * The search is performed as a breadth-then-depth search up to the prescribed | |
281 | * limit (KEYRING_SEARCH_MAX_DEPTH). | |
282 | * | |
283 | * Keys are matched to the type provided and are then filtered by the match | |
284 | * function, which is given the description to use in any way it sees fit. The | |
285 | * match function may use any attributes of a key that it wishes to to | |
286 | * determine the match. Normally the match function from the key type would be | |
287 | * used. | |
288 | * | |
289 | * RCU is used to prevent the keyring key lists from disappearing without the | |
290 | * need to take lots of locks. | |
291 | * | |
292 | * Returns a pointer to the found key and increments the key usage count if | |
293 | * successful; -EAGAIN if no matching keys were found, or if expired or revoked | |
294 | * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the | |
295 | * specified keyring wasn't a keyring. | |
296 | * | |
297 | * In the case of a successful return, the possession attribute from | |
298 | * @keyring_ref is propagated to the returned key reference. | |
1da177e4 | 299 | */ |
664cceb0 | 300 | key_ref_t keyring_search_aux(key_ref_t keyring_ref, |
d84f4f99 | 301 | const struct cred *cred, |
664cceb0 DH |
302 | struct key_type *type, |
303 | const void *description, | |
304 | key_match_func_t match) | |
1da177e4 LT |
305 | { |
306 | struct { | |
76d8aeab | 307 | struct keyring_list *keylist; |
1da177e4 LT |
308 | int kix; |
309 | } stack[KEYRING_SEARCH_MAX_DEPTH]; | |
310 | ||
311 | struct keyring_list *keylist; | |
312 | struct timespec now; | |
dceba994 | 313 | unsigned long possessed, kflags; |
664cceb0 DH |
314 | struct key *keyring, *key; |
315 | key_ref_t key_ref; | |
1da177e4 | 316 | long err; |
76d8aeab | 317 | int sp, kix; |
1da177e4 | 318 | |
664cceb0 DH |
319 | keyring = key_ref_to_ptr(keyring_ref); |
320 | possessed = is_key_possessed(keyring_ref); | |
1da177e4 LT |
321 | key_check(keyring); |
322 | ||
323 | /* top keyring must have search permission to begin the search */ | |
512ea3bc | 324 | err = key_task_permission(keyring_ref, cred, KEY_SEARCH); |
29db9190 DH |
325 | if (err < 0) { |
326 | key_ref = ERR_PTR(err); | |
1da177e4 | 327 | goto error; |
29db9190 | 328 | } |
1da177e4 | 329 | |
664cceb0 | 330 | key_ref = ERR_PTR(-ENOTDIR); |
1da177e4 LT |
331 | if (keyring->type != &key_type_keyring) |
332 | goto error; | |
333 | ||
664cceb0 DH |
334 | rcu_read_lock(); |
335 | ||
1da177e4 LT |
336 | now = current_kernel_time(); |
337 | err = -EAGAIN; | |
338 | sp = 0; | |
339 | ||
dceba994 KC |
340 | /* firstly we should check to see if this top-level keyring is what we |
341 | * are looking for */ | |
342 | key_ref = ERR_PTR(-EAGAIN); | |
343 | kflags = keyring->flags; | |
344 | if (keyring->type == type && match(keyring, description)) { | |
345 | key = keyring; | |
346 | ||
347 | /* check it isn't negative and hasn't expired or been | |
348 | * revoked */ | |
349 | if (kflags & (1 << KEY_FLAG_REVOKED)) | |
350 | goto error_2; | |
351 | if (key->expiry && now.tv_sec >= key->expiry) | |
352 | goto error_2; | |
353 | key_ref = ERR_PTR(-ENOKEY); | |
354 | if (kflags & (1 << KEY_FLAG_NEGATIVE)) | |
355 | goto error_2; | |
356 | goto found; | |
357 | } | |
358 | ||
359 | /* otherwise, the top keyring must not be revoked, expired, or | |
360 | * negatively instantiated if we are to search it */ | |
361 | key_ref = ERR_PTR(-EAGAIN); | |
362 | if (kflags & ((1 << KEY_FLAG_REVOKED) | (1 << KEY_FLAG_NEGATIVE)) || | |
363 | (keyring->expiry && now.tv_sec >= keyring->expiry)) | |
364 | goto error_2; | |
365 | ||
1da177e4 | 366 | /* start processing a new keyring */ |
664cceb0 | 367 | descend: |
76d8aeab | 368 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
369 | goto not_this_keyring; |
370 | ||
76d8aeab | 371 | keylist = rcu_dereference(keyring->payload.subscriptions); |
1da177e4 LT |
372 | if (!keylist) |
373 | goto not_this_keyring; | |
374 | ||
375 | /* iterate through the keys in this keyring first */ | |
376 | for (kix = 0; kix < keylist->nkeys; kix++) { | |
377 | key = keylist->keys[kix]; | |
dceba994 | 378 | kflags = key->flags; |
1da177e4 LT |
379 | |
380 | /* ignore keys not of this type */ | |
381 | if (key->type != type) | |
382 | continue; | |
383 | ||
384 | /* skip revoked keys and expired keys */ | |
dceba994 | 385 | if (kflags & (1 << KEY_FLAG_REVOKED)) |
1da177e4 LT |
386 | continue; |
387 | ||
388 | if (key->expiry && now.tv_sec >= key->expiry) | |
389 | continue; | |
390 | ||
391 | /* keys that don't match */ | |
392 | if (!match(key, description)) | |
393 | continue; | |
394 | ||
395 | /* key must have search permissions */ | |
29db9190 | 396 | if (key_task_permission(make_key_ref(key, possessed), |
d84f4f99 | 397 | cred, KEY_SEARCH) < 0) |
1da177e4 LT |
398 | continue; |
399 | ||
dceba994 KC |
400 | /* we set a different error code if we pass a negative key */ |
401 | if (kflags & (1 << KEY_FLAG_NEGATIVE)) { | |
1da177e4 LT |
402 | err = -ENOKEY; |
403 | continue; | |
404 | } | |
405 | ||
406 | goto found; | |
407 | } | |
408 | ||
409 | /* search through the keyrings nested in this one */ | |
410 | kix = 0; | |
664cceb0 | 411 | ascend: |
76d8aeab | 412 | for (; kix < keylist->nkeys; kix++) { |
1da177e4 LT |
413 | key = keylist->keys[kix]; |
414 | if (key->type != &key_type_keyring) | |
76d8aeab | 415 | continue; |
1da177e4 LT |
416 | |
417 | /* recursively search nested keyrings | |
418 | * - only search keyrings for which we have search permission | |
419 | */ | |
420 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) | |
76d8aeab | 421 | continue; |
1da177e4 | 422 | |
0f6ed7c2 | 423 | if (key_task_permission(make_key_ref(key, possessed), |
d84f4f99 | 424 | cred, KEY_SEARCH) < 0) |
76d8aeab | 425 | continue; |
1da177e4 LT |
426 | |
427 | /* stack the current position */ | |
76d8aeab | 428 | stack[sp].keylist = keylist; |
1da177e4 LT |
429 | stack[sp].kix = kix; |
430 | sp++; | |
431 | ||
432 | /* begin again with the new keyring */ | |
433 | keyring = key; | |
434 | goto descend; | |
1da177e4 LT |
435 | } |
436 | ||
437 | /* the keyring we're looking at was disqualified or didn't contain a | |
438 | * matching key */ | |
664cceb0 | 439 | not_this_keyring: |
1da177e4 LT |
440 | if (sp > 0) { |
441 | /* resume the processing of a keyring higher up in the tree */ | |
442 | sp--; | |
76d8aeab | 443 | keylist = stack[sp].keylist; |
1da177e4 LT |
444 | kix = stack[sp].kix + 1; |
445 | goto ascend; | |
446 | } | |
447 | ||
664cceb0 DH |
448 | key_ref = ERR_PTR(err); |
449 | goto error_2; | |
1da177e4 LT |
450 | |
451 | /* we found a viable match */ | |
664cceb0 | 452 | found: |
1da177e4 | 453 | atomic_inc(&key->usage); |
1da177e4 | 454 | key_check(key); |
664cceb0 DH |
455 | key_ref = make_key_ref(key, possessed); |
456 | error_2: | |
76d8aeab | 457 | rcu_read_unlock(); |
664cceb0 DH |
458 | error: |
459 | return key_ref; | |
a8b17ed0 | 460 | } |
1da177e4 | 461 | |
973c9f4f DH |
462 | /** |
463 | * keyring_search - Search the supplied keyring tree for a matching key | |
464 | * @keyring: The root of the keyring tree to be searched. | |
465 | * @type: The type of keyring we want to find. | |
466 | * @description: The name of the keyring we want to find. | |
467 | * | |
468 | * As keyring_search_aux() above, but using the current task's credentials and | |
469 | * type's default matching function. | |
1da177e4 | 470 | */ |
664cceb0 DH |
471 | key_ref_t keyring_search(key_ref_t keyring, |
472 | struct key_type *type, | |
473 | const char *description) | |
1da177e4 | 474 | { |
3e30148c DH |
475 | if (!type->match) |
476 | return ERR_PTR(-ENOKEY); | |
477 | ||
d84f4f99 | 478 | return keyring_search_aux(keyring, current->cred, |
3e30148c | 479 | type, description, type->match); |
a8b17ed0 | 480 | } |
1da177e4 LT |
481 | EXPORT_SYMBOL(keyring_search); |
482 | ||
1da177e4 | 483 | /* |
973c9f4f DH |
484 | * Search the given keyring only (no recursion). |
485 | * | |
486 | * The caller must guarantee that the keyring is a keyring and that the | |
487 | * permission is granted to search the keyring as no check is made here. | |
488 | * | |
489 | * RCU is used to make it unnecessary to lock the keyring key list here. | |
490 | * | |
491 | * Returns a pointer to the found key with usage count incremented if | |
492 | * successful and returns -ENOKEY if not found. Revoked keys and keys not | |
493 | * providing the requested permission are skipped over. | |
494 | * | |
495 | * If successful, the possession indicator is propagated from the keyring ref | |
496 | * to the returned key reference. | |
1da177e4 | 497 | */ |
664cceb0 DH |
498 | key_ref_t __keyring_search_one(key_ref_t keyring_ref, |
499 | const struct key_type *ktype, | |
500 | const char *description, | |
501 | key_perm_t perm) | |
1da177e4 LT |
502 | { |
503 | struct keyring_list *klist; | |
664cceb0 DH |
504 | unsigned long possessed; |
505 | struct key *keyring, *key; | |
1da177e4 LT |
506 | int loop; |
507 | ||
664cceb0 DH |
508 | keyring = key_ref_to_ptr(keyring_ref); |
509 | possessed = is_key_possessed(keyring_ref); | |
510 | ||
76d8aeab DH |
511 | rcu_read_lock(); |
512 | ||
513 | klist = rcu_dereference(keyring->payload.subscriptions); | |
1da177e4 LT |
514 | if (klist) { |
515 | for (loop = 0; loop < klist->nkeys; loop++) { | |
516 | key = klist->keys[loop]; | |
517 | ||
518 | if (key->type == ktype && | |
3e30148c DH |
519 | (!key->type->match || |
520 | key->type->match(key, description)) && | |
664cceb0 | 521 | key_permission(make_key_ref(key, possessed), |
db1d1d57 | 522 | perm) == 0 && |
76d8aeab | 523 | !test_bit(KEY_FLAG_REVOKED, &key->flags) |
1da177e4 LT |
524 | ) |
525 | goto found; | |
526 | } | |
527 | } | |
528 | ||
664cceb0 DH |
529 | rcu_read_unlock(); |
530 | return ERR_PTR(-ENOKEY); | |
1da177e4 | 531 | |
c5b60b5e | 532 | found: |
1da177e4 | 533 | atomic_inc(&key->usage); |
76d8aeab | 534 | rcu_read_unlock(); |
664cceb0 | 535 | return make_key_ref(key, possessed); |
a8b17ed0 | 536 | } |
1da177e4 | 537 | |
1da177e4 | 538 | /* |
973c9f4f DH |
539 | * Find a keyring with the specified name. |
540 | * | |
541 | * All named keyrings in the current user namespace are searched, provided they | |
542 | * grant Search permission directly to the caller (unless this check is | |
543 | * skipped). Keyrings whose usage points have reached zero or who have been | |
544 | * revoked are skipped. | |
545 | * | |
546 | * Returns a pointer to the keyring with the keyring's refcount having being | |
547 | * incremented on success. -ENOKEY is returned if a key could not be found. | |
1da177e4 | 548 | */ |
69664cf1 | 549 | struct key *find_keyring_by_name(const char *name, bool skip_perm_check) |
1da177e4 LT |
550 | { |
551 | struct key *keyring; | |
552 | int bucket; | |
553 | ||
1da177e4 | 554 | if (!name) |
cea7daa3 | 555 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
556 | |
557 | bucket = keyring_hash(name); | |
558 | ||
559 | read_lock(&keyring_name_lock); | |
560 | ||
561 | if (keyring_name_hash[bucket].next) { | |
562 | /* search this hash bucket for a keyring with a matching name | |
563 | * that's readable and that hasn't been revoked */ | |
564 | list_for_each_entry(keyring, | |
565 | &keyring_name_hash[bucket], | |
566 | type_data.link | |
567 | ) { | |
2ea190d0 SH |
568 | if (keyring->user->user_ns != current_user_ns()) |
569 | continue; | |
570 | ||
76d8aeab | 571 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
572 | continue; |
573 | ||
574 | if (strcmp(keyring->description, name) != 0) | |
575 | continue; | |
576 | ||
69664cf1 DH |
577 | if (!skip_perm_check && |
578 | key_permission(make_key_ref(keyring, 0), | |
0f6ed7c2 | 579 | KEY_SEARCH) < 0) |
1da177e4 LT |
580 | continue; |
581 | ||
cea7daa3 TO |
582 | /* we've got a match but we might end up racing with |
583 | * key_cleanup() if the keyring is currently 'dead' | |
584 | * (ie. it has a zero usage count) */ | |
585 | if (!atomic_inc_not_zero(&keyring->usage)) | |
586 | continue; | |
587 | goto out; | |
1da177e4 LT |
588 | } |
589 | } | |
590 | ||
1da177e4 | 591 | keyring = ERR_PTR(-ENOKEY); |
cea7daa3 TO |
592 | out: |
593 | read_unlock(&keyring_name_lock); | |
1da177e4 | 594 | return keyring; |
a8b17ed0 | 595 | } |
1da177e4 | 596 | |
1da177e4 | 597 | /* |
973c9f4f DH |
598 | * See if a cycle will will be created by inserting acyclic tree B in acyclic |
599 | * tree A at the topmost level (ie: as a direct child of A). | |
600 | * | |
601 | * Since we are adding B to A at the top level, checking for cycles should just | |
602 | * be a matter of seeing if node A is somewhere in tree B. | |
1da177e4 LT |
603 | */ |
604 | static int keyring_detect_cycle(struct key *A, struct key *B) | |
605 | { | |
606 | struct { | |
76d8aeab | 607 | struct keyring_list *keylist; |
1da177e4 LT |
608 | int kix; |
609 | } stack[KEYRING_SEARCH_MAX_DEPTH]; | |
610 | ||
611 | struct keyring_list *keylist; | |
612 | struct key *subtree, *key; | |
613 | int sp, kix, ret; | |
614 | ||
76d8aeab DH |
615 | rcu_read_lock(); |
616 | ||
1da177e4 LT |
617 | ret = -EDEADLK; |
618 | if (A == B) | |
76d8aeab | 619 | goto cycle_detected; |
1da177e4 LT |
620 | |
621 | subtree = B; | |
622 | sp = 0; | |
623 | ||
624 | /* start processing a new keyring */ | |
c5b60b5e | 625 | descend: |
76d8aeab | 626 | if (test_bit(KEY_FLAG_REVOKED, &subtree->flags)) |
1da177e4 LT |
627 | goto not_this_keyring; |
628 | ||
76d8aeab | 629 | keylist = rcu_dereference(subtree->payload.subscriptions); |
1da177e4 LT |
630 | if (!keylist) |
631 | goto not_this_keyring; | |
632 | kix = 0; | |
633 | ||
c5b60b5e | 634 | ascend: |
1da177e4 LT |
635 | /* iterate through the remaining keys in this keyring */ |
636 | for (; kix < keylist->nkeys; kix++) { | |
637 | key = keylist->keys[kix]; | |
638 | ||
639 | if (key == A) | |
640 | goto cycle_detected; | |
641 | ||
642 | /* recursively check nested keyrings */ | |
643 | if (key->type == &key_type_keyring) { | |
644 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) | |
645 | goto too_deep; | |
646 | ||
647 | /* stack the current position */ | |
76d8aeab | 648 | stack[sp].keylist = keylist; |
1da177e4 LT |
649 | stack[sp].kix = kix; |
650 | sp++; | |
651 | ||
652 | /* begin again with the new keyring */ | |
653 | subtree = key; | |
654 | goto descend; | |
655 | } | |
656 | } | |
657 | ||
658 | /* the keyring we're looking at was disqualified or didn't contain a | |
659 | * matching key */ | |
c5b60b5e | 660 | not_this_keyring: |
1da177e4 LT |
661 | if (sp > 0) { |
662 | /* resume the checking of a keyring higher up in the tree */ | |
663 | sp--; | |
76d8aeab | 664 | keylist = stack[sp].keylist; |
1da177e4 LT |
665 | kix = stack[sp].kix + 1; |
666 | goto ascend; | |
667 | } | |
668 | ||
669 | ret = 0; /* no cycles detected */ | |
670 | ||
c5b60b5e | 671 | error: |
76d8aeab | 672 | rcu_read_unlock(); |
1da177e4 LT |
673 | return ret; |
674 | ||
c5b60b5e | 675 | too_deep: |
1da177e4 | 676 | ret = -ELOOP; |
76d8aeab DH |
677 | goto error; |
678 | ||
c5b60b5e | 679 | cycle_detected: |
1da177e4 | 680 | ret = -EDEADLK; |
1da177e4 | 681 | goto error; |
a8b17ed0 | 682 | } |
1da177e4 | 683 | |
cab8eb59 | 684 | /* |
973c9f4f | 685 | * Dispose of a keyring list after the RCU grace period, freeing the unlinked |
cab8eb59 DH |
686 | * key |
687 | */ | |
688 | static void keyring_unlink_rcu_disposal(struct rcu_head *rcu) | |
689 | { | |
690 | struct keyring_list *klist = | |
691 | container_of(rcu, struct keyring_list, rcu); | |
692 | ||
4be929be | 693 | if (klist->delkey != USHRT_MAX) |
f70e2e06 | 694 | key_put(klist->keys[klist->delkey]); |
cab8eb59 | 695 | kfree(klist); |
f70e2e06 | 696 | } |
cab8eb59 | 697 | |
1da177e4 | 698 | /* |
973c9f4f | 699 | * Preallocate memory so that a key can be linked into to a keyring. |
1da177e4 | 700 | */ |
f70e2e06 DH |
701 | int __key_link_begin(struct key *keyring, const struct key_type *type, |
702 | const char *description, | |
703 | struct keyring_list **_prealloc) | |
704 | __acquires(&keyring->sem) | |
1da177e4 LT |
705 | { |
706 | struct keyring_list *klist, *nklist; | |
707 | unsigned max; | |
708 | size_t size; | |
cab8eb59 | 709 | int loop, ret; |
1da177e4 | 710 | |
f70e2e06 | 711 | kenter("%d,%s,%s,", key_serial(keyring), type->name, description); |
1da177e4 | 712 | |
1da177e4 | 713 | if (keyring->type != &key_type_keyring) |
f70e2e06 DH |
714 | return -ENOTDIR; |
715 | ||
716 | down_write(&keyring->sem); | |
717 | ||
718 | ret = -EKEYREVOKED; | |
719 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) | |
720 | goto error_krsem; | |
1da177e4 | 721 | |
f70e2e06 DH |
722 | /* serialise link/link calls to prevent parallel calls causing a cycle |
723 | * when linking two keyring in opposite orders */ | |
724 | if (type == &key_type_keyring) | |
553d603c DH |
725 | down_write(&keyring_serialise_link_sem); |
726 | ||
f70e2e06 | 727 | klist = rcu_dereference_locked_keyring(keyring); |
1da177e4 | 728 | |
cab8eb59 | 729 | /* see if there's a matching key we can displace */ |
cab8eb59 | 730 | if (klist && klist->nkeys > 0) { |
cab8eb59 DH |
731 | for (loop = klist->nkeys - 1; loop >= 0; loop--) { |
732 | if (klist->keys[loop]->type == type && | |
733 | strcmp(klist->keys[loop]->description, | |
f70e2e06 | 734 | description) == 0 |
cab8eb59 | 735 | ) { |
f70e2e06 DH |
736 | /* found a match - we'll replace this one with |
737 | * the new key */ | |
cab8eb59 DH |
738 | size = sizeof(struct key *) * klist->maxkeys; |
739 | size += sizeof(*klist); | |
740 | BUG_ON(size > PAGE_SIZE); | |
741 | ||
742 | ret = -ENOMEM; | |
48ad504e | 743 | nklist = kmemdup(klist, size, GFP_KERNEL); |
cab8eb59 | 744 | if (!nklist) |
f70e2e06 | 745 | goto error_sem; |
cab8eb59 | 746 | |
f70e2e06 DH |
747 | /* note replacement slot */ |
748 | klist->delkey = nklist->delkey = loop; | |
cab8eb59 DH |
749 | goto done; |
750 | } | |
751 | } | |
752 | } | |
753 | ||
1da177e4 LT |
754 | /* check that we aren't going to overrun the user's quota */ |
755 | ret = key_payload_reserve(keyring, | |
756 | keyring->datalen + KEYQUOTA_LINK_BYTES); | |
757 | if (ret < 0) | |
f70e2e06 | 758 | goto error_sem; |
1da177e4 | 759 | |
1da177e4 | 760 | if (klist && klist->nkeys < klist->maxkeys) { |
f70e2e06 DH |
761 | /* there's sufficient slack space to append directly */ |
762 | nklist = NULL; | |
512ea3bc | 763 | } else { |
1da177e4 LT |
764 | /* grow the key list */ |
765 | max = 4; | |
766 | if (klist) | |
767 | max += klist->maxkeys; | |
768 | ||
769 | ret = -ENFILE; | |
4be929be | 770 | if (max > USHRT_MAX - 1) |
f70e2e06 | 771 | goto error_quota; |
a4014d8f | 772 | size = sizeof(*klist) + sizeof(struct key *) * max; |
1da177e4 | 773 | if (size > PAGE_SIZE) |
f70e2e06 | 774 | goto error_quota; |
1da177e4 LT |
775 | |
776 | ret = -ENOMEM; | |
777 | nklist = kmalloc(size, GFP_KERNEL); | |
778 | if (!nklist) | |
f70e2e06 | 779 | goto error_quota; |
1da177e4 | 780 | |
f70e2e06 | 781 | nklist->maxkeys = max; |
1da177e4 | 782 | if (klist) { |
f70e2e06 | 783 | memcpy(nklist->keys, klist->keys, |
1da177e4 | 784 | sizeof(struct key *) * klist->nkeys); |
f70e2e06 DH |
785 | nklist->delkey = klist->nkeys; |
786 | nklist->nkeys = klist->nkeys + 1; | |
4be929be | 787 | klist->delkey = USHRT_MAX; |
f70e2e06 DH |
788 | } else { |
789 | nklist->nkeys = 1; | |
790 | nklist->delkey = 0; | |
1da177e4 LT |
791 | } |
792 | ||
793 | /* add the key into the new space */ | |
f70e2e06 | 794 | nklist->keys[nklist->delkey] = NULL; |
1da177e4 LT |
795 | } |
796 | ||
cab8eb59 | 797 | done: |
f70e2e06 DH |
798 | *_prealloc = nklist; |
799 | kleave(" = 0"); | |
800 | return 0; | |
1da177e4 | 801 | |
f70e2e06 | 802 | error_quota: |
1da177e4 LT |
803 | /* undo the quota changes */ |
804 | key_payload_reserve(keyring, | |
805 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
f70e2e06 DH |
806 | error_sem: |
807 | if (type == &key_type_keyring) | |
808 | up_write(&keyring_serialise_link_sem); | |
809 | error_krsem: | |
810 | up_write(&keyring->sem); | |
811 | kleave(" = %d", ret); | |
812 | return ret; | |
813 | } | |
1da177e4 | 814 | |
f70e2e06 | 815 | /* |
973c9f4f DH |
816 | * Check already instantiated keys aren't going to be a problem. |
817 | * | |
818 | * The caller must have called __key_link_begin(). Don't need to call this for | |
819 | * keys that were created since __key_link_begin() was called. | |
f70e2e06 DH |
820 | */ |
821 | int __key_link_check_live_key(struct key *keyring, struct key *key) | |
822 | { | |
823 | if (key->type == &key_type_keyring) | |
824 | /* check that we aren't going to create a cycle by linking one | |
825 | * keyring to another */ | |
826 | return keyring_detect_cycle(keyring, key); | |
827 | return 0; | |
828 | } | |
829 | ||
830 | /* | |
973c9f4f DH |
831 | * Link a key into to a keyring. |
832 | * | |
833 | * Must be called with __key_link_begin() having being called. Discards any | |
834 | * already extant link to matching key if there is one, so that each keyring | |
835 | * holds at most one link to any given key of a particular type+description | |
836 | * combination. | |
f70e2e06 DH |
837 | */ |
838 | void __key_link(struct key *keyring, struct key *key, | |
839 | struct keyring_list **_prealloc) | |
840 | { | |
841 | struct keyring_list *klist, *nklist; | |
842 | ||
843 | nklist = *_prealloc; | |
844 | *_prealloc = NULL; | |
845 | ||
846 | kenter("%d,%d,%p", keyring->serial, key->serial, nklist); | |
847 | ||
848 | klist = rcu_dereference_protected(keyring->payload.subscriptions, | |
849 | rwsem_is_locked(&keyring->sem)); | |
850 | ||
851 | atomic_inc(&key->usage); | |
852 | ||
853 | /* there's a matching key we can displace or an empty slot in a newly | |
854 | * allocated list we can fill */ | |
855 | if (nklist) { | |
856 | kdebug("replace %hu/%hu/%hu", | |
857 | nklist->delkey, nklist->nkeys, nklist->maxkeys); | |
858 | ||
859 | nklist->keys[nklist->delkey] = key; | |
860 | ||
861 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); | |
862 | ||
863 | /* dispose of the old keyring list and, if there was one, the | |
864 | * displaced key */ | |
865 | if (klist) { | |
866 | kdebug("dispose %hu/%hu/%hu", | |
867 | klist->delkey, klist->nkeys, klist->maxkeys); | |
868 | call_rcu(&klist->rcu, keyring_unlink_rcu_disposal); | |
869 | } | |
870 | } else { | |
871 | /* there's sufficient slack space to append directly */ | |
872 | klist->keys[klist->nkeys] = key; | |
873 | smp_wmb(); | |
874 | klist->nkeys++; | |
875 | } | |
876 | } | |
877 | ||
878 | /* | |
973c9f4f DH |
879 | * Finish linking a key into to a keyring. |
880 | * | |
881 | * Must be called with __key_link_begin() having being called. | |
f70e2e06 DH |
882 | */ |
883 | void __key_link_end(struct key *keyring, struct key_type *type, | |
884 | struct keyring_list *prealloc) | |
885 | __releases(&keyring->sem) | |
886 | { | |
887 | BUG_ON(type == NULL); | |
888 | BUG_ON(type->name == NULL); | |
889 | kenter("%d,%s,%p", keyring->serial, type->name, prealloc); | |
890 | ||
891 | if (type == &key_type_keyring) | |
892 | up_write(&keyring_serialise_link_sem); | |
893 | ||
894 | if (prealloc) { | |
895 | kfree(prealloc); | |
896 | key_payload_reserve(keyring, | |
897 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
898 | } | |
899 | up_write(&keyring->sem); | |
900 | } | |
1da177e4 | 901 | |
973c9f4f DH |
902 | /** |
903 | * key_link - Link a key to a keyring | |
904 | * @keyring: The keyring to make the link in. | |
905 | * @key: The key to link to. | |
906 | * | |
907 | * Make a link in a keyring to a key, such that the keyring holds a reference | |
908 | * on that key and the key can potentially be found by searching that keyring. | |
909 | * | |
910 | * This function will write-lock the keyring's semaphore and will consume some | |
911 | * of the user's key data quota to hold the link. | |
912 | * | |
913 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, | |
914 | * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is | |
915 | * full, -EDQUOT if there is insufficient key data quota remaining to add | |
916 | * another link or -ENOMEM if there's insufficient memory. | |
917 | * | |
918 | * It is assumed that the caller has checked that it is permitted for a link to | |
919 | * be made (the keyring should have Write permission and the key Link | |
920 | * permission). | |
1da177e4 LT |
921 | */ |
922 | int key_link(struct key *keyring, struct key *key) | |
923 | { | |
f70e2e06 | 924 | struct keyring_list *prealloc; |
1da177e4 LT |
925 | int ret; |
926 | ||
927 | key_check(keyring); | |
928 | key_check(key); | |
929 | ||
f70e2e06 DH |
930 | ret = __key_link_begin(keyring, key->type, key->description, &prealloc); |
931 | if (ret == 0) { | |
932 | ret = __key_link_check_live_key(keyring, key); | |
933 | if (ret == 0) | |
934 | __key_link(keyring, key, &prealloc); | |
935 | __key_link_end(keyring, key->type, prealloc); | |
936 | } | |
1da177e4 LT |
937 | |
938 | return ret; | |
f70e2e06 | 939 | } |
1da177e4 LT |
940 | EXPORT_SYMBOL(key_link); |
941 | ||
973c9f4f DH |
942 | /** |
943 | * key_unlink - Unlink the first link to a key from a keyring. | |
944 | * @keyring: The keyring to remove the link from. | |
945 | * @key: The key the link is to. | |
946 | * | |
947 | * Remove a link from a keyring to a key. | |
948 | * | |
949 | * This function will write-lock the keyring's semaphore. | |
950 | * | |
951 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if | |
952 | * the key isn't linked to by the keyring or -ENOMEM if there's insufficient | |
953 | * memory. | |
954 | * | |
955 | * It is assumed that the caller has checked that it is permitted for a link to | |
956 | * be removed (the keyring should have Write permission; no permissions are | |
957 | * required on the key). | |
1da177e4 LT |
958 | */ |
959 | int key_unlink(struct key *keyring, struct key *key) | |
960 | { | |
76d8aeab | 961 | struct keyring_list *klist, *nklist; |
1da177e4 LT |
962 | int loop, ret; |
963 | ||
964 | key_check(keyring); | |
965 | key_check(key); | |
966 | ||
967 | ret = -ENOTDIR; | |
968 | if (keyring->type != &key_type_keyring) | |
969 | goto error; | |
970 | ||
971 | down_write(&keyring->sem); | |
972 | ||
f0641cba | 973 | klist = rcu_dereference_locked_keyring(keyring); |
1da177e4 LT |
974 | if (klist) { |
975 | /* search the keyring for the key */ | |
976 | for (loop = 0; loop < klist->nkeys; loop++) | |
977 | if (klist->keys[loop] == key) | |
978 | goto key_is_present; | |
979 | } | |
980 | ||
981 | up_write(&keyring->sem); | |
982 | ret = -ENOENT; | |
983 | goto error; | |
984 | ||
76d8aeab DH |
985 | key_is_present: |
986 | /* we need to copy the key list for RCU purposes */ | |
a4014d8f DH |
987 | nklist = kmalloc(sizeof(*klist) + |
988 | sizeof(struct key *) * klist->maxkeys, | |
76d8aeab DH |
989 | GFP_KERNEL); |
990 | if (!nklist) | |
991 | goto nomem; | |
992 | nklist->maxkeys = klist->maxkeys; | |
993 | nklist->nkeys = klist->nkeys - 1; | |
994 | ||
995 | if (loop > 0) | |
996 | memcpy(&nklist->keys[0], | |
997 | &klist->keys[0], | |
a4014d8f | 998 | loop * sizeof(struct key *)); |
76d8aeab DH |
999 | |
1000 | if (loop < nklist->nkeys) | |
1001 | memcpy(&nklist->keys[loop], | |
1002 | &klist->keys[loop + 1], | |
a4014d8f | 1003 | (nklist->nkeys - loop) * sizeof(struct key *)); |
76d8aeab | 1004 | |
1da177e4 LT |
1005 | /* adjust the user's quota */ |
1006 | key_payload_reserve(keyring, | |
1007 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
1008 | ||
76d8aeab | 1009 | rcu_assign_pointer(keyring->payload.subscriptions, nklist); |
1da177e4 | 1010 | |
76d8aeab | 1011 | up_write(&keyring->sem); |
1da177e4 | 1012 | |
76d8aeab DH |
1013 | /* schedule for later cleanup */ |
1014 | klist->delkey = loop; | |
1015 | call_rcu(&klist->rcu, keyring_unlink_rcu_disposal); | |
1da177e4 | 1016 | |
1da177e4 LT |
1017 | ret = 0; |
1018 | ||
76d8aeab | 1019 | error: |
1da177e4 | 1020 | return ret; |
76d8aeab DH |
1021 | nomem: |
1022 | ret = -ENOMEM; | |
1023 | up_write(&keyring->sem); | |
1024 | goto error; | |
a8b17ed0 | 1025 | } |
1da177e4 LT |
1026 | EXPORT_SYMBOL(key_unlink); |
1027 | ||
76d8aeab | 1028 | /* |
973c9f4f DH |
1029 | * Dispose of a keyring list after the RCU grace period, releasing the keys it |
1030 | * links to. | |
76d8aeab DH |
1031 | */ |
1032 | static void keyring_clear_rcu_disposal(struct rcu_head *rcu) | |
1033 | { | |
1034 | struct keyring_list *klist; | |
1035 | int loop; | |
1036 | ||
1037 | klist = container_of(rcu, struct keyring_list, rcu); | |
1038 | ||
1039 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
1040 | key_put(klist->keys[loop]); | |
1041 | ||
1042 | kfree(klist); | |
a8b17ed0 | 1043 | } |
76d8aeab | 1044 | |
973c9f4f DH |
1045 | /** |
1046 | * keyring_clear - Clear a keyring | |
1047 | * @keyring: The keyring to clear. | |
1048 | * | |
1049 | * Clear the contents of the specified keyring. | |
1050 | * | |
1051 | * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring. | |
1da177e4 LT |
1052 | */ |
1053 | int keyring_clear(struct key *keyring) | |
1054 | { | |
1055 | struct keyring_list *klist; | |
76d8aeab | 1056 | int ret; |
1da177e4 LT |
1057 | |
1058 | ret = -ENOTDIR; | |
1059 | if (keyring->type == &key_type_keyring) { | |
1060 | /* detach the pointer block with the locks held */ | |
1061 | down_write(&keyring->sem); | |
1062 | ||
f0641cba | 1063 | klist = rcu_dereference_locked_keyring(keyring); |
1da177e4 LT |
1064 | if (klist) { |
1065 | /* adjust the quota */ | |
1066 | key_payload_reserve(keyring, | |
1067 | sizeof(struct keyring_list)); | |
1068 | ||
76d8aeab DH |
1069 | rcu_assign_pointer(keyring->payload.subscriptions, |
1070 | NULL); | |
1da177e4 LT |
1071 | } |
1072 | ||
1073 | up_write(&keyring->sem); | |
1074 | ||
1075 | /* free the keys after the locks have been dropped */ | |
76d8aeab DH |
1076 | if (klist) |
1077 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); | |
1da177e4 LT |
1078 | |
1079 | ret = 0; | |
1080 | } | |
1081 | ||
1082 | return ret; | |
a8b17ed0 | 1083 | } |
1da177e4 | 1084 | EXPORT_SYMBOL(keyring_clear); |
31204ed9 | 1085 | |
31204ed9 | 1086 | /* |
973c9f4f DH |
1087 | * Dispose of the links from a revoked keyring. |
1088 | * | |
1089 | * This is called with the key sem write-locked. | |
31204ed9 DH |
1090 | */ |
1091 | static void keyring_revoke(struct key *keyring) | |
1092 | { | |
f0641cba DH |
1093 | struct keyring_list *klist; |
1094 | ||
1095 | klist = rcu_dereference_locked_keyring(keyring); | |
31204ed9 DH |
1096 | |
1097 | /* adjust the quota */ | |
1098 | key_payload_reserve(keyring, 0); | |
1099 | ||
1100 | if (klist) { | |
1101 | rcu_assign_pointer(keyring->payload.subscriptions, NULL); | |
1102 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); | |
1103 | } | |
a8b17ed0 | 1104 | } |
5d135440 DH |
1105 | |
1106 | /* | |
973c9f4f | 1107 | * Determine whether a key is dead. |
5d135440 DH |
1108 | */ |
1109 | static bool key_is_dead(struct key *key, time_t limit) | |
1110 | { | |
1111 | return test_bit(KEY_FLAG_DEAD, &key->flags) || | |
1112 | (key->expiry > 0 && key->expiry <= limit); | |
1113 | } | |
1114 | ||
1115 | /* | |
973c9f4f DH |
1116 | * Collect garbage from the contents of a keyring, replacing the old list with |
1117 | * a new one with the pointers all shuffled down. | |
1118 | * | |
1119 | * Dead keys are classed as oned that are flagged as being dead or are revoked, | |
1120 | * expired or negative keys that were revoked or expired before the specified | |
1121 | * limit. | |
5d135440 DH |
1122 | */ |
1123 | void keyring_gc(struct key *keyring, time_t limit) | |
1124 | { | |
1125 | struct keyring_list *klist, *new; | |
1126 | struct key *key; | |
1127 | int loop, keep, max; | |
1128 | ||
c08ef808 | 1129 | kenter("{%x,%s}", key_serial(keyring), keyring->description); |
5d135440 DH |
1130 | |
1131 | down_write(&keyring->sem); | |
1132 | ||
f0641cba | 1133 | klist = rcu_dereference_locked_keyring(keyring); |
5d135440 | 1134 | if (!klist) |
c08ef808 | 1135 | goto no_klist; |
5d135440 DH |
1136 | |
1137 | /* work out how many subscriptions we're keeping */ | |
1138 | keep = 0; | |
1139 | for (loop = klist->nkeys - 1; loop >= 0; loop--) | |
c08ef808 | 1140 | if (!key_is_dead(klist->keys[loop], limit)) |
5d135440 DH |
1141 | keep++; |
1142 | ||
1143 | if (keep == klist->nkeys) | |
1144 | goto just_return; | |
1145 | ||
1146 | /* allocate a new keyring payload */ | |
1147 | max = roundup(keep, 4); | |
1148 | new = kmalloc(sizeof(struct keyring_list) + max * sizeof(struct key *), | |
1149 | GFP_KERNEL); | |
1150 | if (!new) | |
c08ef808 | 1151 | goto nomem; |
5d135440 DH |
1152 | new->maxkeys = max; |
1153 | new->nkeys = 0; | |
1154 | new->delkey = 0; | |
1155 | ||
1156 | /* install the live keys | |
1157 | * - must take care as expired keys may be updated back to life | |
1158 | */ | |
1159 | keep = 0; | |
1160 | for (loop = klist->nkeys - 1; loop >= 0; loop--) { | |
1161 | key = klist->keys[loop]; | |
1162 | if (!key_is_dead(key, limit)) { | |
1163 | if (keep >= max) | |
1164 | goto discard_new; | |
1165 | new->keys[keep++] = key_get(key); | |
1166 | } | |
1167 | } | |
1168 | new->nkeys = keep; | |
1169 | ||
1170 | /* adjust the quota */ | |
1171 | key_payload_reserve(keyring, | |
1172 | sizeof(struct keyring_list) + | |
1173 | KEYQUOTA_LINK_BYTES * keep); | |
1174 | ||
1175 | if (keep == 0) { | |
1176 | rcu_assign_pointer(keyring->payload.subscriptions, NULL); | |
1177 | kfree(new); | |
1178 | } else { | |
1179 | rcu_assign_pointer(keyring->payload.subscriptions, new); | |
1180 | } | |
1181 | ||
1182 | up_write(&keyring->sem); | |
1183 | ||
1184 | call_rcu(&klist->rcu, keyring_clear_rcu_disposal); | |
1185 | kleave(" [yes]"); | |
1186 | return; | |
1187 | ||
1188 | discard_new: | |
1189 | new->nkeys = keep; | |
1190 | keyring_clear_rcu_disposal(&new->rcu); | |
c08ef808 DH |
1191 | up_write(&keyring->sem); |
1192 | kleave(" [discard]"); | |
1193 | return; | |
1194 | ||
5d135440 DH |
1195 | just_return: |
1196 | up_write(&keyring->sem); | |
c08ef808 DH |
1197 | kleave(" [no dead]"); |
1198 | return; | |
1199 | ||
1200 | no_klist: | |
1201 | up_write(&keyring->sem); | |
1202 | kleave(" [no_klist]"); | |
1203 | return; | |
1204 | ||
1205 | nomem: | |
1206 | up_write(&keyring->sem); | |
1207 | kleave(" [oom]"); | |
5d135440 | 1208 | } |