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