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