]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
69664cf1 | 2 | /* Keyring handling |
1da177e4 | 3 | * |
b2a4df20 | 4 | * Copyright (C) 2004-2005, 2008, 2013 Red Hat, Inc. All Rights Reserved. |
1da177e4 | 5 | * Written by David Howells (dhowells@redhat.com) |
1da177e4 LT |
6 | */ |
7 | ||
876979c9 | 8 | #include <linux/export.h> |
1da177e4 LT |
9 | #include <linux/init.h> |
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
29db9190 | 12 | #include <linux/security.h> |
1da177e4 LT |
13 | #include <linux/seq_file.h> |
14 | #include <linux/err.h> | |
e9e349b0 | 15 | #include <keys/keyring-type.h> |
b2a4df20 DH |
16 | #include <keys/user-type.h> |
17 | #include <linux/assoc_array_priv.h> | |
512ea3bc | 18 | #include <linux/uaccess.h> |
1da177e4 LT |
19 | #include "internal.h" |
20 | ||
21 | /* | |
973c9f4f DH |
22 | * When plumbing the depths of the key tree, this sets a hard limit |
23 | * set on how deep we're willing to go. | |
1da177e4 LT |
24 | */ |
25 | #define KEYRING_SEARCH_MAX_DEPTH 6 | |
26 | ||
27 | /* | |
973c9f4f | 28 | * We keep all named keyrings in a hash to speed looking them up. |
1da177e4 LT |
29 | */ |
30 | #define KEYRING_NAME_HASH_SIZE (1 << 5) | |
31 | ||
b2a4df20 DH |
32 | /* |
33 | * We mark pointers we pass to the associative array with bit 1 set if | |
34 | * they're keyrings and clear otherwise. | |
35 | */ | |
36 | #define KEYRING_PTR_SUBTYPE 0x2UL | |
37 | ||
38 | static inline bool keyring_ptr_is_keyring(const struct assoc_array_ptr *x) | |
39 | { | |
40 | return (unsigned long)x & KEYRING_PTR_SUBTYPE; | |
41 | } | |
42 | static inline struct key *keyring_ptr_to_key(const struct assoc_array_ptr *x) | |
43 | { | |
44 | void *object = assoc_array_ptr_to_leaf(x); | |
45 | return (struct key *)((unsigned long)object & ~KEYRING_PTR_SUBTYPE); | |
46 | } | |
47 | static inline void *keyring_key_to_ptr(struct key *key) | |
48 | { | |
49 | if (key->type == &key_type_keyring) | |
50 | return (void *)((unsigned long)key | KEYRING_PTR_SUBTYPE); | |
51 | return key; | |
52 | } | |
53 | ||
1da177e4 LT |
54 | static struct list_head keyring_name_hash[KEYRING_NAME_HASH_SIZE]; |
55 | static DEFINE_RWLOCK(keyring_name_lock); | |
56 | ||
57 | static inline unsigned keyring_hash(const char *desc) | |
58 | { | |
59 | unsigned bucket = 0; | |
60 | ||
61 | for (; *desc; desc++) | |
c5b60b5e | 62 | bucket += (unsigned char)*desc; |
1da177e4 LT |
63 | |
64 | return bucket & (KEYRING_NAME_HASH_SIZE - 1); | |
65 | } | |
66 | ||
67 | /* | |
973c9f4f DH |
68 | * The keyring key type definition. Keyrings are simply keys of this type and |
69 | * can be treated as ordinary keys in addition to having their own special | |
70 | * operations. | |
1da177e4 | 71 | */ |
5d19e20b DH |
72 | static int keyring_preparse(struct key_preparsed_payload *prep); |
73 | static void keyring_free_preparse(struct key_preparsed_payload *prep); | |
1da177e4 | 74 | static int keyring_instantiate(struct key *keyring, |
cf7f601c | 75 | struct key_preparsed_payload *prep); |
31204ed9 | 76 | static void keyring_revoke(struct key *keyring); |
1da177e4 LT |
77 | static void keyring_destroy(struct key *keyring); |
78 | static void keyring_describe(const struct key *keyring, struct seq_file *m); | |
79 | static long keyring_read(const struct key *keyring, | |
80 | char __user *buffer, size_t buflen); | |
81 | ||
82 | struct key_type key_type_keyring = { | |
83 | .name = "keyring", | |
b2a4df20 | 84 | .def_datalen = 0, |
5d19e20b DH |
85 | .preparse = keyring_preparse, |
86 | .free_preparse = keyring_free_preparse, | |
1da177e4 | 87 | .instantiate = keyring_instantiate, |
31204ed9 | 88 | .revoke = keyring_revoke, |
1da177e4 LT |
89 | .destroy = keyring_destroy, |
90 | .describe = keyring_describe, | |
91 | .read = keyring_read, | |
92 | }; | |
7318226e DH |
93 | EXPORT_SYMBOL(key_type_keyring); |
94 | ||
1da177e4 | 95 | /* |
973c9f4f DH |
96 | * Semaphore to serialise link/link calls to prevent two link calls in parallel |
97 | * introducing a cycle. | |
1da177e4 | 98 | */ |
1ae8f407 | 99 | static DECLARE_RWSEM(keyring_serialise_link_sem); |
1da177e4 | 100 | |
1da177e4 | 101 | /* |
973c9f4f DH |
102 | * Publish the name of a keyring so that it can be found by name (if it has |
103 | * one). | |
1da177e4 | 104 | */ |
69664cf1 | 105 | static void keyring_publish_name(struct key *keyring) |
1da177e4 LT |
106 | { |
107 | int bucket; | |
108 | ||
109 | if (keyring->description) { | |
110 | bucket = keyring_hash(keyring->description); | |
111 | ||
112 | write_lock(&keyring_name_lock); | |
113 | ||
114 | if (!keyring_name_hash[bucket].next) | |
115 | INIT_LIST_HEAD(&keyring_name_hash[bucket]); | |
116 | ||
146aa8b1 | 117 | list_add_tail(&keyring->name_link, |
1da177e4 LT |
118 | &keyring_name_hash[bucket]); |
119 | ||
120 | write_unlock(&keyring_name_lock); | |
121 | } | |
a8b17ed0 | 122 | } |
1da177e4 | 123 | |
5d19e20b DH |
124 | /* |
125 | * Preparse a keyring payload | |
126 | */ | |
127 | static int keyring_preparse(struct key_preparsed_payload *prep) | |
128 | { | |
129 | return prep->datalen != 0 ? -EINVAL : 0; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Free a preparse of a user defined key payload | |
134 | */ | |
135 | static void keyring_free_preparse(struct key_preparsed_payload *prep) | |
136 | { | |
137 | } | |
138 | ||
1da177e4 | 139 | /* |
973c9f4f DH |
140 | * Initialise a keyring. |
141 | * | |
142 | * Returns 0 on success, -EINVAL if given any data. | |
1da177e4 LT |
143 | */ |
144 | static int keyring_instantiate(struct key *keyring, | |
cf7f601c | 145 | struct key_preparsed_payload *prep) |
1da177e4 | 146 | { |
5d19e20b DH |
147 | assoc_array_init(&keyring->keys); |
148 | /* make the keyring available by name if it has one */ | |
149 | keyring_publish_name(keyring); | |
150 | return 0; | |
a8b17ed0 | 151 | } |
1da177e4 | 152 | |
1da177e4 | 153 | /* |
b2a4df20 DH |
154 | * Multiply 64-bits by 32-bits to 96-bits and fold back to 64-bit. Ideally we'd |
155 | * fold the carry back too, but that requires inline asm. | |
156 | */ | |
157 | static u64 mult_64x32_and_fold(u64 x, u32 y) | |
158 | { | |
159 | u64 hi = (u64)(u32)(x >> 32) * y; | |
160 | u64 lo = (u64)(u32)(x) * y; | |
161 | return lo + ((u64)(u32)hi << 32) + (u32)(hi >> 32); | |
162 | } | |
163 | ||
164 | /* | |
165 | * Hash a key type and description. | |
166 | */ | |
167 | static unsigned long hash_key_type_and_desc(const struct keyring_index_key *index_key) | |
168 | { | |
169 | const unsigned level_shift = ASSOC_ARRAY_LEVEL_STEP; | |
d54e58b7 | 170 | const unsigned long fan_mask = ASSOC_ARRAY_FAN_MASK; |
b2a4df20 DH |
171 | const char *description = index_key->description; |
172 | unsigned long hash, type; | |
173 | u32 piece; | |
174 | u64 acc; | |
175 | int n, desc_len = index_key->desc_len; | |
176 | ||
177 | type = (unsigned long)index_key->type; | |
178 | ||
179 | acc = mult_64x32_and_fold(type, desc_len + 13); | |
180 | acc = mult_64x32_and_fold(acc, 9207); | |
181 | for (;;) { | |
182 | n = desc_len; | |
183 | if (n <= 0) | |
184 | break; | |
185 | if (n > 4) | |
186 | n = 4; | |
187 | piece = 0; | |
188 | memcpy(&piece, description, n); | |
189 | description += n; | |
190 | desc_len -= n; | |
191 | acc = mult_64x32_and_fold(acc, piece); | |
192 | acc = mult_64x32_and_fold(acc, 9207); | |
193 | } | |
194 | ||
195 | /* Fold the hash down to 32 bits if need be. */ | |
196 | hash = acc; | |
197 | if (ASSOC_ARRAY_KEY_CHUNK_SIZE == 32) | |
198 | hash ^= acc >> 32; | |
199 | ||
200 | /* Squidge all the keyrings into a separate part of the tree to | |
201 | * ordinary keys by making sure the lowest level segment in the hash is | |
202 | * zero for keyrings and non-zero otherwise. | |
203 | */ | |
d54e58b7 | 204 | if (index_key->type != &key_type_keyring && (hash & fan_mask) == 0) |
b2a4df20 | 205 | return hash | (hash >> (ASSOC_ARRAY_KEY_CHUNK_SIZE - level_shift)) | 1; |
d54e58b7 DH |
206 | if (index_key->type == &key_type_keyring && (hash & fan_mask) != 0) |
207 | return (hash + (hash << level_shift)) & ~fan_mask; | |
b2a4df20 DH |
208 | return hash; |
209 | } | |
210 | ||
211 | /* | |
212 | * Build the next index key chunk. | |
213 | * | |
214 | * On 32-bit systems the index key is laid out as: | |
215 | * | |
216 | * 0 4 5 9... | |
217 | * hash desclen typeptr desc[] | |
218 | * | |
219 | * On 64-bit systems: | |
220 | * | |
221 | * 0 8 9 17... | |
222 | * hash desclen typeptr desc[] | |
223 | * | |
224 | * We return it one word-sized chunk at a time. | |
1da177e4 | 225 | */ |
b2a4df20 DH |
226 | static unsigned long keyring_get_key_chunk(const void *data, int level) |
227 | { | |
228 | const struct keyring_index_key *index_key = data; | |
229 | unsigned long chunk = 0; | |
230 | long offset = 0; | |
231 | int desc_len = index_key->desc_len, n = sizeof(chunk); | |
232 | ||
233 | level /= ASSOC_ARRAY_KEY_CHUNK_SIZE; | |
234 | switch (level) { | |
235 | case 0: | |
236 | return hash_key_type_and_desc(index_key); | |
237 | case 1: | |
238 | return ((unsigned long)index_key->type << 8) | desc_len; | |
239 | case 2: | |
240 | if (desc_len == 0) | |
241 | return (u8)((unsigned long)index_key->type >> | |
242 | (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8)); | |
243 | n--; | |
244 | offset = 1; | |
5b73262a | 245 | /* fall through */ |
b2a4df20 DH |
246 | default: |
247 | offset += sizeof(chunk) - 1; | |
248 | offset += (level - 3) * sizeof(chunk); | |
249 | if (offset >= desc_len) | |
250 | return 0; | |
251 | desc_len -= offset; | |
252 | if (desc_len > n) | |
253 | desc_len = n; | |
254 | offset += desc_len; | |
255 | do { | |
256 | chunk <<= 8; | |
257 | chunk |= ((u8*)index_key->description)[--offset]; | |
258 | } while (--desc_len > 0); | |
259 | ||
260 | if (level == 2) { | |
261 | chunk <<= 8; | |
262 | chunk |= (u8)((unsigned long)index_key->type >> | |
263 | (ASSOC_ARRAY_KEY_CHUNK_SIZE - 8)); | |
264 | } | |
265 | return chunk; | |
266 | } | |
267 | } | |
268 | ||
269 | static unsigned long keyring_get_object_key_chunk(const void *object, int level) | |
270 | { | |
271 | const struct key *key = keyring_ptr_to_key(object); | |
272 | return keyring_get_key_chunk(&key->index_key, level); | |
273 | } | |
274 | ||
275 | static bool keyring_compare_object(const void *object, const void *data) | |
1da177e4 | 276 | { |
b2a4df20 DH |
277 | const struct keyring_index_key *index_key = data; |
278 | const struct key *key = keyring_ptr_to_key(object); | |
279 | ||
280 | return key->index_key.type == index_key->type && | |
281 | key->index_key.desc_len == index_key->desc_len && | |
282 | memcmp(key->index_key.description, index_key->description, | |
283 | index_key->desc_len) == 0; | |
a8b17ed0 | 284 | } |
1da177e4 | 285 | |
b2a4df20 DH |
286 | /* |
287 | * Compare the index keys of a pair of objects and determine the bit position | |
288 | * at which they differ - if they differ. | |
289 | */ | |
23fd78d7 | 290 | static int keyring_diff_objects(const void *object, const void *data) |
b2a4df20 | 291 | { |
23fd78d7 | 292 | const struct key *key_a = keyring_ptr_to_key(object); |
b2a4df20 | 293 | const struct keyring_index_key *a = &key_a->index_key; |
23fd78d7 | 294 | const struct keyring_index_key *b = data; |
b2a4df20 DH |
295 | unsigned long seg_a, seg_b; |
296 | int level, i; | |
297 | ||
298 | level = 0; | |
299 | seg_a = hash_key_type_and_desc(a); | |
300 | seg_b = hash_key_type_and_desc(b); | |
301 | if ((seg_a ^ seg_b) != 0) | |
302 | goto differ; | |
303 | ||
304 | /* The number of bits contributed by the hash is controlled by a | |
305 | * constant in the assoc_array headers. Everything else thereafter we | |
306 | * can deal with as being machine word-size dependent. | |
307 | */ | |
308 | level += ASSOC_ARRAY_KEY_CHUNK_SIZE / 8; | |
309 | seg_a = a->desc_len; | |
310 | seg_b = b->desc_len; | |
311 | if ((seg_a ^ seg_b) != 0) | |
312 | goto differ; | |
313 | ||
314 | /* The next bit may not work on big endian */ | |
315 | level++; | |
316 | seg_a = (unsigned long)a->type; | |
317 | seg_b = (unsigned long)b->type; | |
318 | if ((seg_a ^ seg_b) != 0) | |
319 | goto differ; | |
320 | ||
321 | level += sizeof(unsigned long); | |
322 | if (a->desc_len == 0) | |
323 | goto same; | |
324 | ||
325 | i = 0; | |
326 | if (((unsigned long)a->description | (unsigned long)b->description) & | |
327 | (sizeof(unsigned long) - 1)) { | |
328 | do { | |
329 | seg_a = *(unsigned long *)(a->description + i); | |
330 | seg_b = *(unsigned long *)(b->description + i); | |
331 | if ((seg_a ^ seg_b) != 0) | |
332 | goto differ_plus_i; | |
333 | i += sizeof(unsigned long); | |
334 | } while (i < (a->desc_len & (sizeof(unsigned long) - 1))); | |
335 | } | |
336 | ||
337 | for (; i < a->desc_len; i++) { | |
338 | seg_a = *(unsigned char *)(a->description + i); | |
339 | seg_b = *(unsigned char *)(b->description + i); | |
340 | if ((seg_a ^ seg_b) != 0) | |
341 | goto differ_plus_i; | |
342 | } | |
343 | ||
344 | same: | |
345 | return -1; | |
346 | ||
347 | differ_plus_i: | |
348 | level += i; | |
349 | differ: | |
350 | i = level * 8 + __ffs(seg_a ^ seg_b); | |
351 | return i; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Free an object after stripping the keyring flag off of the pointer. | |
356 | */ | |
357 | static void keyring_free_object(void *object) | |
358 | { | |
359 | key_put(keyring_ptr_to_key(object)); | |
360 | } | |
361 | ||
362 | /* | |
363 | * Operations for keyring management by the index-tree routines. | |
364 | */ | |
365 | static const struct assoc_array_ops keyring_assoc_array_ops = { | |
366 | .get_key_chunk = keyring_get_key_chunk, | |
367 | .get_object_key_chunk = keyring_get_object_key_chunk, | |
368 | .compare_object = keyring_compare_object, | |
369 | .diff_objects = keyring_diff_objects, | |
370 | .free_object = keyring_free_object, | |
371 | }; | |
372 | ||
1da177e4 | 373 | /* |
973c9f4f DH |
374 | * Clean up a keyring when it is destroyed. Unpublish its name if it had one |
375 | * and dispose of its data. | |
233e4735 DH |
376 | * |
377 | * The garbage collector detects the final key_put(), removes the keyring from | |
378 | * the serial number tree and then does RCU synchronisation before coming here, | |
379 | * so we shouldn't need to worry about code poking around here with the RCU | |
380 | * readlock held by this time. | |
1da177e4 LT |
381 | */ |
382 | static void keyring_destroy(struct key *keyring) | |
383 | { | |
1da177e4 LT |
384 | if (keyring->description) { |
385 | write_lock(&keyring_name_lock); | |
94efe72f | 386 | |
146aa8b1 DH |
387 | if (keyring->name_link.next != NULL && |
388 | !list_empty(&keyring->name_link)) | |
389 | list_del(&keyring->name_link); | |
94efe72f | 390 | |
1da177e4 LT |
391 | write_unlock(&keyring_name_lock); |
392 | } | |
393 | ||
2b6aa412 MM |
394 | if (keyring->restrict_link) { |
395 | struct key_restriction *keyres = keyring->restrict_link; | |
396 | ||
397 | key_put(keyres->key); | |
398 | kfree(keyres); | |
399 | } | |
400 | ||
b2a4df20 | 401 | assoc_array_destroy(&keyring->keys, &keyring_assoc_array_ops); |
a8b17ed0 | 402 | } |
1da177e4 | 403 | |
1da177e4 | 404 | /* |
973c9f4f | 405 | * Describe a keyring for /proc. |
1da177e4 LT |
406 | */ |
407 | static void keyring_describe(const struct key *keyring, struct seq_file *m) | |
408 | { | |
c8563473 | 409 | if (keyring->description) |
1da177e4 | 410 | seq_puts(m, keyring->description); |
c8563473 | 411 | else |
1da177e4 | 412 | seq_puts(m, "[anon]"); |
1da177e4 | 413 | |
363b02da | 414 | if (key_is_positive(keyring)) { |
b2a4df20 DH |
415 | if (keyring->keys.nr_leaves_on_tree != 0) |
416 | seq_printf(m, ": %lu", keyring->keys.nr_leaves_on_tree); | |
78b7280c DH |
417 | else |
418 | seq_puts(m, ": empty"); | |
78b7280c | 419 | } |
a8b17ed0 | 420 | } |
1da177e4 | 421 | |
b2a4df20 | 422 | struct keyring_read_iterator_context { |
e645016a | 423 | size_t buflen; |
b2a4df20 DH |
424 | size_t count; |
425 | key_serial_t __user *buffer; | |
426 | }; | |
427 | ||
428 | static int keyring_read_iterator(const void *object, void *data) | |
429 | { | |
430 | struct keyring_read_iterator_context *ctx = data; | |
431 | const struct key *key = keyring_ptr_to_key(object); | |
432 | int ret; | |
433 | ||
434 | kenter("{%s,%d},,{%zu/%zu}", | |
e645016a | 435 | key->type->name, key->serial, ctx->count, ctx->buflen); |
b2a4df20 | 436 | |
e645016a | 437 | if (ctx->count >= ctx->buflen) |
b2a4df20 DH |
438 | return 1; |
439 | ||
440 | ret = put_user(key->serial, ctx->buffer); | |
441 | if (ret < 0) | |
442 | return ret; | |
443 | ctx->buffer++; | |
444 | ctx->count += sizeof(key->serial); | |
445 | return 0; | |
446 | } | |
447 | ||
1da177e4 | 448 | /* |
973c9f4f DH |
449 | * Read a list of key IDs from the keyring's contents in binary form |
450 | * | |
b2a4df20 DH |
451 | * The keyring's semaphore is read-locked by the caller. This prevents someone |
452 | * from modifying it under us - which could cause us to read key IDs multiple | |
453 | * times. | |
1da177e4 LT |
454 | */ |
455 | static long keyring_read(const struct key *keyring, | |
456 | char __user *buffer, size_t buflen) | |
457 | { | |
b2a4df20 | 458 | struct keyring_read_iterator_context ctx; |
3239b6f2 | 459 | long ret; |
1da177e4 | 460 | |
b2a4df20 DH |
461 | kenter("{%d},,%zu", key_serial(keyring), buflen); |
462 | ||
463 | if (buflen & (sizeof(key_serial_t) - 1)) | |
464 | return -EINVAL; | |
465 | ||
3239b6f2 EB |
466 | /* Copy as many key IDs as fit into the buffer */ |
467 | if (buffer && buflen) { | |
468 | ctx.buffer = (key_serial_t __user *)buffer; | |
469 | ctx.buflen = buflen; | |
470 | ctx.count = 0; | |
471 | ret = assoc_array_iterate(&keyring->keys, | |
472 | keyring_read_iterator, &ctx); | |
473 | if (ret < 0) { | |
474 | kleave(" = %ld [iterate]", ret); | |
475 | return ret; | |
476 | } | |
1da177e4 LT |
477 | } |
478 | ||
3239b6f2 EB |
479 | /* Return the size of the buffer needed */ |
480 | ret = keyring->keys.nr_leaves_on_tree * sizeof(key_serial_t); | |
481 | if (ret <= buflen) | |
482 | kleave("= %ld [ok]", ret); | |
483 | else | |
484 | kleave("= %ld [buffer too small]", ret); | |
485 | return ret; | |
a8b17ed0 | 486 | } |
1da177e4 | 487 | |
1da177e4 | 488 | /* |
973c9f4f | 489 | * Allocate a keyring and link into the destination keyring. |
1da177e4 | 490 | */ |
9a56c2db | 491 | struct key *keyring_alloc(const char *description, kuid_t uid, kgid_t gid, |
96b5c8fe | 492 | const struct cred *cred, key_perm_t perm, |
5ac7eace | 493 | unsigned long flags, |
2b6aa412 | 494 | struct key_restriction *restrict_link, |
5ac7eace | 495 | struct key *dest) |
1da177e4 LT |
496 | { |
497 | struct key *keyring; | |
498 | int ret; | |
499 | ||
500 | keyring = key_alloc(&key_type_keyring, description, | |
5ac7eace | 501 | uid, gid, cred, perm, flags, restrict_link); |
1da177e4 | 502 | if (!IS_ERR(keyring)) { |
3e30148c | 503 | ret = key_instantiate_and_link(keyring, NULL, 0, dest, NULL); |
1da177e4 LT |
504 | if (ret < 0) { |
505 | key_put(keyring); | |
506 | keyring = ERR_PTR(ret); | |
507 | } | |
508 | } | |
509 | ||
510 | return keyring; | |
a8b17ed0 | 511 | } |
f8aa23a5 | 512 | EXPORT_SYMBOL(keyring_alloc); |
1da177e4 | 513 | |
5ac7eace DH |
514 | /** |
515 | * restrict_link_reject - Give -EPERM to restrict link | |
516 | * @keyring: The keyring being added to. | |
517 | * @type: The type of key being added. | |
5ac7eace | 518 | * @payload: The payload of the key intended to be added. |
aaf66c88 | 519 | * @data: Additional data for evaluating restriction. |
5ac7eace DH |
520 | * |
521 | * Reject the addition of any links to a keyring. It can be overridden by | |
522 | * passing KEY_ALLOC_BYPASS_RESTRICTION to key_instantiate_and_link() when | |
523 | * adding a key to a keyring. | |
524 | * | |
2b6aa412 MM |
525 | * This is meant to be stored in a key_restriction structure which is passed |
526 | * in the restrict_link parameter to keyring_alloc(). | |
5ac7eace DH |
527 | */ |
528 | int restrict_link_reject(struct key *keyring, | |
529 | const struct key_type *type, | |
aaf66c88 MM |
530 | const union key_payload *payload, |
531 | struct key *restriction_key) | |
5ac7eace DH |
532 | { |
533 | return -EPERM; | |
534 | } | |
535 | ||
c06cfb08 DH |
536 | /* |
537 | * By default, we keys found by getting an exact match on their descriptions. | |
538 | */ | |
0c903ab6 DH |
539 | bool key_default_cmp(const struct key *key, |
540 | const struct key_match_data *match_data) | |
c06cfb08 DH |
541 | { |
542 | return strcmp(key->description, match_data->raw_data) == 0; | |
543 | } | |
544 | ||
b2a4df20 DH |
545 | /* |
546 | * Iteration function to consider each key found. | |
1da177e4 | 547 | */ |
b2a4df20 | 548 | static int keyring_search_iterator(const void *object, void *iterator_data) |
1da177e4 | 549 | { |
b2a4df20 DH |
550 | struct keyring_search_context *ctx = iterator_data; |
551 | const struct key *key = keyring_ptr_to_key(object); | |
363b02da DH |
552 | unsigned long kflags = READ_ONCE(key->flags); |
553 | short state = READ_ONCE(key->state); | |
1da177e4 | 554 | |
b2a4df20 | 555 | kenter("{%d}", key->serial); |
1da177e4 | 556 | |
b2a4df20 DH |
557 | /* ignore keys not of this type */ |
558 | if (key->type != ctx->index_key.type) { | |
559 | kleave(" = 0 [!type]"); | |
560 | return 0; | |
29db9190 | 561 | } |
1da177e4 | 562 | |
b2a4df20 DH |
563 | /* skip invalidated, revoked and expired keys */ |
564 | if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) { | |
074d5898 | 565 | time64_t expiry = READ_ONCE(key->expiry); |
9d6c8711 | 566 | |
b2a4df20 DH |
567 | if (kflags & ((1 << KEY_FLAG_INVALIDATED) | |
568 | (1 << KEY_FLAG_REVOKED))) { | |
569 | ctx->result = ERR_PTR(-EKEYREVOKED); | |
570 | kleave(" = %d [invrev]", ctx->skipped_ret); | |
571 | goto skipped; | |
572 | } | |
1da177e4 | 573 | |
074d5898 | 574 | if (expiry && ctx->now >= expiry) { |
0b0a8415 DH |
575 | if (!(ctx->flags & KEYRING_SEARCH_SKIP_EXPIRED)) |
576 | ctx->result = ERR_PTR(-EKEYEXPIRED); | |
b2a4df20 DH |
577 | kleave(" = %d [expire]", ctx->skipped_ret); |
578 | goto skipped; | |
579 | } | |
580 | } | |
664cceb0 | 581 | |
b2a4df20 | 582 | /* keys that don't match */ |
46291959 | 583 | if (!ctx->match_data.cmp(key, &ctx->match_data)) { |
b2a4df20 DH |
584 | kleave(" = 0 [!match]"); |
585 | return 0; | |
586 | } | |
dceba994 | 587 | |
b2a4df20 DH |
588 | /* key must have search permissions */ |
589 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) && | |
590 | key_task_permission(make_key_ref(key, ctx->possessed), | |
f5895943 | 591 | ctx->cred, KEY_NEED_SEARCH) < 0) { |
b2a4df20 DH |
592 | ctx->result = ERR_PTR(-EACCES); |
593 | kleave(" = %d [!perm]", ctx->skipped_ret); | |
594 | goto skipped; | |
dceba994 KC |
595 | } |
596 | ||
b2a4df20 DH |
597 | if (ctx->flags & KEYRING_SEARCH_DO_STATE_CHECK) { |
598 | /* we set a different error code if we pass a negative key */ | |
363b02da DH |
599 | if (state < 0) { |
600 | ctx->result = ERR_PTR(state); | |
b2a4df20 DH |
601 | kleave(" = %d [neg]", ctx->skipped_ret); |
602 | goto skipped; | |
603 | } | |
604 | } | |
1da177e4 | 605 | |
b2a4df20 DH |
606 | /* Found */ |
607 | ctx->result = make_key_ref(key, ctx->possessed); | |
608 | kleave(" = 1 [found]"); | |
609 | return 1; | |
1da177e4 | 610 | |
b2a4df20 DH |
611 | skipped: |
612 | return ctx->skipped_ret; | |
613 | } | |
1da177e4 | 614 | |
b2a4df20 DH |
615 | /* |
616 | * Search inside a keyring for a key. We can search by walking to it | |
617 | * directly based on its index-key or we can iterate over the entire | |
618 | * tree looking for it, based on the match function. | |
619 | */ | |
620 | static int search_keyring(struct key *keyring, struct keyring_search_context *ctx) | |
621 | { | |
46291959 | 622 | if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_DIRECT) { |
b2a4df20 DH |
623 | const void *object; |
624 | ||
625 | object = assoc_array_find(&keyring->keys, | |
626 | &keyring_assoc_array_ops, | |
627 | &ctx->index_key); | |
628 | return object ? ctx->iterator(object, ctx) : 0; | |
629 | } | |
630 | return assoc_array_iterate(&keyring->keys, ctx->iterator, ctx); | |
631 | } | |
1da177e4 | 632 | |
b2a4df20 DH |
633 | /* |
634 | * Search a tree of keyrings that point to other keyrings up to the maximum | |
635 | * depth. | |
636 | */ | |
637 | static bool search_nested_keyrings(struct key *keyring, | |
638 | struct keyring_search_context *ctx) | |
639 | { | |
640 | struct { | |
641 | struct key *keyring; | |
642 | struct assoc_array_node *node; | |
643 | int slot; | |
644 | } stack[KEYRING_SEARCH_MAX_DEPTH]; | |
1da177e4 | 645 | |
b2a4df20 DH |
646 | struct assoc_array_shortcut *shortcut; |
647 | struct assoc_array_node *node; | |
648 | struct assoc_array_ptr *ptr; | |
649 | struct key *key; | |
650 | int sp = 0, slot; | |
1da177e4 | 651 | |
b2a4df20 DH |
652 | kenter("{%d},{%s,%s}", |
653 | keyring->serial, | |
654 | ctx->index_key.type->name, | |
655 | ctx->index_key.description); | |
1da177e4 | 656 | |
054f6180 DH |
657 | #define STATE_CHECKS (KEYRING_SEARCH_NO_STATE_CHECK | KEYRING_SEARCH_DO_STATE_CHECK) |
658 | BUG_ON((ctx->flags & STATE_CHECKS) == 0 || | |
659 | (ctx->flags & STATE_CHECKS) == STATE_CHECKS); | |
660 | ||
b2a4df20 DH |
661 | /* Check to see if this top-level keyring is what we are looking for |
662 | * and whether it is valid or not. | |
663 | */ | |
46291959 | 664 | if (ctx->match_data.lookup_type == KEYRING_SEARCH_LOOKUP_ITERATE || |
b2a4df20 DH |
665 | keyring_compare_object(keyring, &ctx->index_key)) { |
666 | ctx->skipped_ret = 2; | |
b2a4df20 DH |
667 | switch (ctx->iterator(keyring_key_to_ptr(keyring), ctx)) { |
668 | case 1: | |
78b7280c | 669 | goto found; |
b2a4df20 DH |
670 | case 2: |
671 | return false; | |
672 | default: | |
673 | break; | |
1da177e4 | 674 | } |
b2a4df20 | 675 | } |
1da177e4 | 676 | |
b2a4df20 | 677 | ctx->skipped_ret = 0; |
b2a4df20 DH |
678 | |
679 | /* Start processing a new keyring */ | |
680 | descend_to_keyring: | |
681 | kdebug("descend to %d", keyring->serial); | |
682 | if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) | | |
683 | (1 << KEY_FLAG_REVOKED))) | |
684 | goto not_this_keyring; | |
685 | ||
686 | /* Search through the keys in this keyring before its searching its | |
687 | * subtrees. | |
688 | */ | |
689 | if (search_keyring(keyring, ctx)) | |
1da177e4 | 690 | goto found; |
1da177e4 | 691 | |
b2a4df20 DH |
692 | /* Then manually iterate through the keyrings nested in this one. |
693 | * | |
694 | * Start from the root node of the index tree. Because of the way the | |
695 | * hash function has been set up, keyrings cluster on the leftmost | |
696 | * branch of the root node (root slot 0) or in the root node itself. | |
697 | * Non-keyrings avoid the leftmost branch of the root entirely (root | |
698 | * slots 1-15). | |
699 | */ | |
381f20fc | 700 | ptr = READ_ONCE(keyring->keys.root); |
b2a4df20 DH |
701 | if (!ptr) |
702 | goto not_this_keyring; | |
1da177e4 | 703 | |
b2a4df20 DH |
704 | if (assoc_array_ptr_is_shortcut(ptr)) { |
705 | /* If the root is a shortcut, either the keyring only contains | |
706 | * keyring pointers (everything clusters behind root slot 0) or | |
707 | * doesn't contain any keyring pointers. | |
1da177e4 | 708 | */ |
b2a4df20 | 709 | shortcut = assoc_array_ptr_to_shortcut(ptr); |
b2a4df20 DH |
710 | if ((shortcut->index_key[0] & ASSOC_ARRAY_FAN_MASK) != 0) |
711 | goto not_this_keyring; | |
712 | ||
381f20fc | 713 | ptr = READ_ONCE(shortcut->next_node); |
b2a4df20 DH |
714 | node = assoc_array_ptr_to_node(ptr); |
715 | goto begin_node; | |
716 | } | |
717 | ||
718 | node = assoc_array_ptr_to_node(ptr); | |
b2a4df20 DH |
719 | ptr = node->slots[0]; |
720 | if (!assoc_array_ptr_is_meta(ptr)) | |
721 | goto begin_node; | |
722 | ||
723 | descend_to_node: | |
724 | /* Descend to a more distal node in this keyring's content tree and go | |
725 | * through that. | |
726 | */ | |
727 | kdebug("descend"); | |
728 | if (assoc_array_ptr_is_shortcut(ptr)) { | |
729 | shortcut = assoc_array_ptr_to_shortcut(ptr); | |
381f20fc | 730 | ptr = READ_ONCE(shortcut->next_node); |
b2a4df20 | 731 | BUG_ON(!assoc_array_ptr_is_node(ptr)); |
b2a4df20 | 732 | } |
9c5e45df | 733 | node = assoc_array_ptr_to_node(ptr); |
b2a4df20 DH |
734 | |
735 | begin_node: | |
736 | kdebug("begin_node"); | |
b2a4df20 DH |
737 | slot = 0; |
738 | ascend_to_node: | |
739 | /* Go through the slots in a node */ | |
740 | for (; slot < ASSOC_ARRAY_FAN_OUT; slot++) { | |
381f20fc | 741 | ptr = READ_ONCE(node->slots[slot]); |
b2a4df20 DH |
742 | |
743 | if (assoc_array_ptr_is_meta(ptr) && node->back_pointer) | |
744 | goto descend_to_node; | |
745 | ||
746 | if (!keyring_ptr_is_keyring(ptr)) | |
76d8aeab | 747 | continue; |
1da177e4 | 748 | |
b2a4df20 DH |
749 | key = keyring_ptr_to_key(ptr); |
750 | ||
751 | if (sp >= KEYRING_SEARCH_MAX_DEPTH) { | |
752 | if (ctx->flags & KEYRING_SEARCH_DETECT_TOO_DEEP) { | |
753 | ctx->result = ERR_PTR(-ELOOP); | |
754 | return false; | |
755 | } | |
756 | goto not_this_keyring; | |
757 | } | |
758 | ||
759 | /* Search a nested keyring */ | |
760 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM) && | |
761 | key_task_permission(make_key_ref(key, ctx->possessed), | |
f5895943 | 762 | ctx->cred, KEY_NEED_SEARCH) < 0) |
76d8aeab | 763 | continue; |
1da177e4 LT |
764 | |
765 | /* stack the current position */ | |
31d5a79d | 766 | stack[sp].keyring = keyring; |
b2a4df20 DH |
767 | stack[sp].node = node; |
768 | stack[sp].slot = slot; | |
1da177e4 LT |
769 | sp++; |
770 | ||
771 | /* begin again with the new keyring */ | |
772 | keyring = key; | |
b2a4df20 DH |
773 | goto descend_to_keyring; |
774 | } | |
775 | ||
776 | /* We've dealt with all the slots in the current node, so now we need | |
777 | * to ascend to the parent and continue processing there. | |
778 | */ | |
381f20fc | 779 | ptr = READ_ONCE(node->back_pointer); |
b2a4df20 DH |
780 | slot = node->parent_slot; |
781 | ||
782 | if (ptr && assoc_array_ptr_is_shortcut(ptr)) { | |
783 | shortcut = assoc_array_ptr_to_shortcut(ptr); | |
381f20fc | 784 | ptr = READ_ONCE(shortcut->back_pointer); |
b2a4df20 DH |
785 | slot = shortcut->parent_slot; |
786 | } | |
787 | if (!ptr) | |
788 | goto not_this_keyring; | |
789 | node = assoc_array_ptr_to_node(ptr); | |
b2a4df20 DH |
790 | slot++; |
791 | ||
792 | /* If we've ascended to the root (zero backpointer), we must have just | |
793 | * finished processing the leftmost branch rather than the root slots - | |
794 | * so there can't be any more keyrings for us to find. | |
795 | */ | |
796 | if (node->back_pointer) { | |
797 | kdebug("ascend %d", slot); | |
798 | goto ascend_to_node; | |
1da177e4 LT |
799 | } |
800 | ||
b2a4df20 DH |
801 | /* The keyring we're looking at was disqualified or didn't contain a |
802 | * matching key. | |
803 | */ | |
664cceb0 | 804 | not_this_keyring: |
b2a4df20 DH |
805 | kdebug("not_this_keyring %d", sp); |
806 | if (sp <= 0) { | |
807 | kleave(" = false"); | |
808 | return false; | |
1da177e4 LT |
809 | } |
810 | ||
b2a4df20 DH |
811 | /* Resume the processing of a keyring higher up in the tree */ |
812 | sp--; | |
813 | keyring = stack[sp].keyring; | |
814 | node = stack[sp].node; | |
815 | slot = stack[sp].slot + 1; | |
816 | kdebug("ascend to %d [%d]", keyring->serial, slot); | |
817 | goto ascend_to_node; | |
1da177e4 | 818 | |
b2a4df20 | 819 | /* We found a viable match */ |
664cceb0 | 820 | found: |
b2a4df20 | 821 | key = key_ref_to_ptr(ctx->result); |
1da177e4 | 822 | key_check(key); |
b2a4df20 | 823 | if (!(ctx->flags & KEYRING_SEARCH_NO_UPDATE_TIME)) { |
074d5898 BW |
824 | key->last_used_at = ctx->now; |
825 | keyring->last_used_at = ctx->now; | |
b2a4df20 | 826 | while (sp > 0) |
074d5898 | 827 | stack[--sp].keyring->last_used_at = ctx->now; |
b2a4df20 DH |
828 | } |
829 | kleave(" = true"); | |
830 | return true; | |
831 | } | |
832 | ||
833 | /** | |
834 | * keyring_search_aux - Search a keyring tree for a key matching some criteria | |
835 | * @keyring_ref: A pointer to the keyring with possession indicator. | |
836 | * @ctx: The keyring search context. | |
837 | * | |
838 | * Search the supplied keyring tree for a key that matches the criteria given. | |
839 | * The root keyring and any linked keyrings must grant Search permission to the | |
840 | * caller to be searchable and keys can only be found if they too grant Search | |
841 | * to the caller. The possession flag on the root keyring pointer controls use | |
842 | * of the possessor bits in permissions checking of the entire tree. In | |
843 | * addition, the LSM gets to forbid keyring searches and key matches. | |
844 | * | |
845 | * The search is performed as a breadth-then-depth search up to the prescribed | |
846 | * limit (KEYRING_SEARCH_MAX_DEPTH). | |
847 | * | |
848 | * Keys are matched to the type provided and are then filtered by the match | |
849 | * function, which is given the description to use in any way it sees fit. The | |
850 | * match function may use any attributes of a key that it wishes to to | |
851 | * determine the match. Normally the match function from the key type would be | |
852 | * used. | |
853 | * | |
854 | * RCU can be used to prevent the keyring key lists from disappearing without | |
855 | * the need to take lots of locks. | |
856 | * | |
857 | * Returns a pointer to the found key and increments the key usage count if | |
858 | * successful; -EAGAIN if no matching keys were found, or if expired or revoked | |
859 | * keys were found; -ENOKEY if only negative keys were found; -ENOTDIR if the | |
860 | * specified keyring wasn't a keyring. | |
861 | * | |
862 | * In the case of a successful return, the possession attribute from | |
863 | * @keyring_ref is propagated to the returned key reference. | |
864 | */ | |
865 | key_ref_t keyring_search_aux(key_ref_t keyring_ref, | |
866 | struct keyring_search_context *ctx) | |
867 | { | |
868 | struct key *keyring; | |
869 | long err; | |
870 | ||
871 | ctx->iterator = keyring_search_iterator; | |
872 | ctx->possessed = is_key_possessed(keyring_ref); | |
873 | ctx->result = ERR_PTR(-EAGAIN); | |
874 | ||
875 | keyring = key_ref_to_ptr(keyring_ref); | |
876 | key_check(keyring); | |
877 | ||
878 | if (keyring->type != &key_type_keyring) | |
879 | return ERR_PTR(-ENOTDIR); | |
880 | ||
881 | if (!(ctx->flags & KEYRING_SEARCH_NO_CHECK_PERM)) { | |
f5895943 | 882 | err = key_task_permission(keyring_ref, ctx->cred, KEY_NEED_SEARCH); |
b2a4df20 DH |
883 | if (err < 0) |
884 | return ERR_PTR(err); | |
885 | } | |
886 | ||
887 | rcu_read_lock(); | |
074d5898 | 888 | ctx->now = ktime_get_real_seconds(); |
b2a4df20 DH |
889 | if (search_nested_keyrings(keyring, ctx)) |
890 | __key_get(key_ref_to_ptr(ctx->result)); | |
76d8aeab | 891 | rcu_read_unlock(); |
b2a4df20 | 892 | return ctx->result; |
a8b17ed0 | 893 | } |
1da177e4 | 894 | |
973c9f4f DH |
895 | /** |
896 | * keyring_search - Search the supplied keyring tree for a matching key | |
897 | * @keyring: The root of the keyring tree to be searched. | |
898 | * @type: The type of keyring we want to find. | |
899 | * @description: The name of the keyring we want to find. | |
900 | * | |
901 | * As keyring_search_aux() above, but using the current task's credentials and | |
b2a4df20 | 902 | * type's default matching function and preferred search method. |
1da177e4 | 903 | */ |
664cceb0 DH |
904 | key_ref_t keyring_search(key_ref_t keyring, |
905 | struct key_type *type, | |
906 | const char *description) | |
1da177e4 | 907 | { |
4bdf0bc3 DH |
908 | struct keyring_search_context ctx = { |
909 | .index_key.type = type, | |
910 | .index_key.description = description, | |
ede0fa98 | 911 | .index_key.desc_len = strlen(description), |
4bdf0bc3 | 912 | .cred = current_cred(), |
c06cfb08 | 913 | .match_data.cmp = key_default_cmp, |
46291959 DH |
914 | .match_data.raw_data = description, |
915 | .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, | |
916 | .flags = KEYRING_SEARCH_DO_STATE_CHECK, | |
4bdf0bc3 | 917 | }; |
46291959 DH |
918 | key_ref_t key; |
919 | int ret; | |
4bdf0bc3 | 920 | |
46291959 DH |
921 | if (type->match_preparse) { |
922 | ret = type->match_preparse(&ctx.match_data); | |
923 | if (ret < 0) | |
924 | return ERR_PTR(ret); | |
925 | } | |
926 | ||
927 | key = keyring_search_aux(keyring, &ctx); | |
928 | ||
929 | if (type->match_free) | |
930 | type->match_free(&ctx.match_data); | |
931 | return key; | |
a8b17ed0 | 932 | } |
1da177e4 LT |
933 | EXPORT_SYMBOL(keyring_search); |
934 | ||
6563c91f MM |
935 | static struct key_restriction *keyring_restriction_alloc( |
936 | key_restrict_link_func_t check) | |
937 | { | |
938 | struct key_restriction *keyres = | |
939 | kzalloc(sizeof(struct key_restriction), GFP_KERNEL); | |
940 | ||
941 | if (!keyres) | |
942 | return ERR_PTR(-ENOMEM); | |
943 | ||
944 | keyres->check = check; | |
945 | ||
946 | return keyres; | |
947 | } | |
948 | ||
949 | /* | |
950 | * Semaphore to serialise restriction setup to prevent reference count | |
951 | * cycles through restriction key pointers. | |
952 | */ | |
953 | static DECLARE_RWSEM(keyring_serialise_restrict_sem); | |
954 | ||
955 | /* | |
956 | * Check for restriction cycles that would prevent keyring garbage collection. | |
957 | * keyring_serialise_restrict_sem must be held. | |
958 | */ | |
959 | static bool keyring_detect_restriction_cycle(const struct key *dest_keyring, | |
960 | struct key_restriction *keyres) | |
961 | { | |
962 | while (keyres && keyres->key && | |
963 | keyres->key->type == &key_type_keyring) { | |
964 | if (keyres->key == dest_keyring) | |
965 | return true; | |
966 | ||
967 | keyres = keyres->key->restrict_link; | |
968 | } | |
969 | ||
970 | return false; | |
971 | } | |
972 | ||
973 | /** | |
974 | * keyring_restrict - Look up and apply a restriction to a keyring | |
975 | * | |
976 | * @keyring: The keyring to be restricted | |
977 | * @restriction: The restriction options to apply to the keyring | |
978 | */ | |
979 | int keyring_restrict(key_ref_t keyring_ref, const char *type, | |
980 | const char *restriction) | |
981 | { | |
982 | struct key *keyring; | |
983 | struct key_type *restrict_type = NULL; | |
984 | struct key_restriction *restrict_link; | |
985 | int ret = 0; | |
986 | ||
987 | keyring = key_ref_to_ptr(keyring_ref); | |
988 | key_check(keyring); | |
989 | ||
990 | if (keyring->type != &key_type_keyring) | |
991 | return -ENOTDIR; | |
992 | ||
993 | if (!type) { | |
994 | restrict_link = keyring_restriction_alloc(restrict_link_reject); | |
995 | } else { | |
996 | restrict_type = key_type_lookup(type); | |
997 | ||
998 | if (IS_ERR(restrict_type)) | |
999 | return PTR_ERR(restrict_type); | |
1000 | ||
1001 | if (!restrict_type->lookup_restriction) { | |
1002 | ret = -ENOENT; | |
1003 | goto error; | |
1004 | } | |
1005 | ||
1006 | restrict_link = restrict_type->lookup_restriction(restriction); | |
1007 | } | |
1008 | ||
1009 | if (IS_ERR(restrict_link)) { | |
1010 | ret = PTR_ERR(restrict_link); | |
1011 | goto error; | |
1012 | } | |
1013 | ||
1014 | down_write(&keyring->sem); | |
1015 | down_write(&keyring_serialise_restrict_sem); | |
1016 | ||
1017 | if (keyring->restrict_link) | |
1018 | ret = -EEXIST; | |
1019 | else if (keyring_detect_restriction_cycle(keyring, restrict_link)) | |
1020 | ret = -EDEADLK; | |
1021 | else | |
1022 | keyring->restrict_link = restrict_link; | |
1023 | ||
1024 | up_write(&keyring_serialise_restrict_sem); | |
1025 | up_write(&keyring->sem); | |
1026 | ||
1027 | if (ret < 0) { | |
1028 | key_put(restrict_link->key); | |
1029 | kfree(restrict_link); | |
1030 | } | |
1031 | ||
1032 | error: | |
1033 | if (restrict_type) | |
1034 | key_type_put(restrict_type); | |
1035 | ||
1036 | return ret; | |
1037 | } | |
1038 | EXPORT_SYMBOL(keyring_restrict); | |
1039 | ||
1da177e4 | 1040 | /* |
b2a4df20 | 1041 | * Search the given keyring for a key that might be updated. |
973c9f4f DH |
1042 | * |
1043 | * The caller must guarantee that the keyring is a keyring and that the | |
b2a4df20 DH |
1044 | * permission is granted to modify the keyring as no check is made here. The |
1045 | * caller must also hold a lock on the keyring semaphore. | |
973c9f4f DH |
1046 | * |
1047 | * Returns a pointer to the found key with usage count incremented if | |
b2a4df20 DH |
1048 | * successful and returns NULL if not found. Revoked and invalidated keys are |
1049 | * skipped over. | |
973c9f4f DH |
1050 | * |
1051 | * If successful, the possession indicator is propagated from the keyring ref | |
1052 | * to the returned key reference. | |
1da177e4 | 1053 | */ |
b2a4df20 DH |
1054 | key_ref_t find_key_to_update(key_ref_t keyring_ref, |
1055 | const struct keyring_index_key *index_key) | |
1da177e4 | 1056 | { |
664cceb0 | 1057 | struct key *keyring, *key; |
b2a4df20 | 1058 | const void *object; |
1da177e4 | 1059 | |
664cceb0 | 1060 | keyring = key_ref_to_ptr(keyring_ref); |
664cceb0 | 1061 | |
b2a4df20 DH |
1062 | kenter("{%d},{%s,%s}", |
1063 | keyring->serial, index_key->type->name, index_key->description); | |
76d8aeab | 1064 | |
b2a4df20 DH |
1065 | object = assoc_array_find(&keyring->keys, &keyring_assoc_array_ops, |
1066 | index_key); | |
1da177e4 | 1067 | |
b2a4df20 DH |
1068 | if (object) |
1069 | goto found; | |
1070 | ||
1071 | kleave(" = NULL"); | |
1072 | return NULL; | |
1da177e4 | 1073 | |
c5b60b5e | 1074 | found: |
b2a4df20 DH |
1075 | key = keyring_ptr_to_key(object); |
1076 | if (key->flags & ((1 << KEY_FLAG_INVALIDATED) | | |
1077 | (1 << KEY_FLAG_REVOKED))) { | |
1078 | kleave(" = NULL [x]"); | |
1079 | return NULL; | |
1080 | } | |
ccc3e6d9 | 1081 | __key_get(key); |
b2a4df20 DH |
1082 | kleave(" = {%d}", key->serial); |
1083 | return make_key_ref(key, is_key_possessed(keyring_ref)); | |
a8b17ed0 | 1084 | } |
1da177e4 | 1085 | |
1da177e4 | 1086 | /* |
973c9f4f DH |
1087 | * Find a keyring with the specified name. |
1088 | * | |
237bbd29 EB |
1089 | * Only keyrings that have nonzero refcount, are not revoked, and are owned by a |
1090 | * user in the current user namespace are considered. If @uid_keyring is %true, | |
1091 | * the keyring additionally must have been allocated as a user or user session | |
1092 | * keyring; otherwise, it must grant Search permission directly to the caller. | |
973c9f4f DH |
1093 | * |
1094 | * Returns a pointer to the keyring with the keyring's refcount having being | |
1095 | * incremented on success. -ENOKEY is returned if a key could not be found. | |
1da177e4 | 1096 | */ |
237bbd29 | 1097 | struct key *find_keyring_by_name(const char *name, bool uid_keyring) |
1da177e4 LT |
1098 | { |
1099 | struct key *keyring; | |
1100 | int bucket; | |
1101 | ||
1da177e4 | 1102 | if (!name) |
cea7daa3 | 1103 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
1104 | |
1105 | bucket = keyring_hash(name); | |
1106 | ||
1107 | read_lock(&keyring_name_lock); | |
1108 | ||
1109 | if (keyring_name_hash[bucket].next) { | |
1110 | /* search this hash bucket for a keyring with a matching name | |
1111 | * that's readable and that hasn't been revoked */ | |
1112 | list_for_each_entry(keyring, | |
1113 | &keyring_name_hash[bucket], | |
146aa8b1 | 1114 | name_link |
1da177e4 | 1115 | ) { |
9a56c2db | 1116 | if (!kuid_has_mapping(current_user_ns(), keyring->user->uid)) |
2ea190d0 SH |
1117 | continue; |
1118 | ||
76d8aeab | 1119 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) |
1da177e4 LT |
1120 | continue; |
1121 | ||
1122 | if (strcmp(keyring->description, name) != 0) | |
1123 | continue; | |
1124 | ||
237bbd29 EB |
1125 | if (uid_keyring) { |
1126 | if (!test_bit(KEY_FLAG_UID_KEYRING, | |
1127 | &keyring->flags)) | |
1128 | continue; | |
1129 | } else { | |
1130 | if (key_permission(make_key_ref(keyring, 0), | |
1131 | KEY_NEED_SEARCH) < 0) | |
1132 | continue; | |
1133 | } | |
1da177e4 | 1134 | |
cea7daa3 TO |
1135 | /* we've got a match but we might end up racing with |
1136 | * key_cleanup() if the keyring is currently 'dead' | |
1137 | * (ie. it has a zero usage count) */ | |
fff29291 | 1138 | if (!refcount_inc_not_zero(&keyring->usage)) |
cea7daa3 | 1139 | continue; |
074d5898 | 1140 | keyring->last_used_at = ktime_get_real_seconds(); |
cea7daa3 | 1141 | goto out; |
1da177e4 LT |
1142 | } |
1143 | } | |
1144 | ||
1da177e4 | 1145 | keyring = ERR_PTR(-ENOKEY); |
cea7daa3 TO |
1146 | out: |
1147 | read_unlock(&keyring_name_lock); | |
1da177e4 | 1148 | return keyring; |
a8b17ed0 | 1149 | } |
1da177e4 | 1150 | |
b2a4df20 DH |
1151 | static int keyring_detect_cycle_iterator(const void *object, |
1152 | void *iterator_data) | |
1153 | { | |
1154 | struct keyring_search_context *ctx = iterator_data; | |
1155 | const struct key *key = keyring_ptr_to_key(object); | |
1156 | ||
1157 | kenter("{%d}", key->serial); | |
1158 | ||
979e0d74 DH |
1159 | /* We might get a keyring with matching index-key that is nonetheless a |
1160 | * different keyring. */ | |
46291959 | 1161 | if (key != ctx->match_data.raw_data) |
979e0d74 DH |
1162 | return 0; |
1163 | ||
b2a4df20 DH |
1164 | ctx->result = ERR_PTR(-EDEADLK); |
1165 | return 1; | |
1166 | } | |
1167 | ||
1da177e4 | 1168 | /* |
973c9f4f DH |
1169 | * See if a cycle will will be created by inserting acyclic tree B in acyclic |
1170 | * tree A at the topmost level (ie: as a direct child of A). | |
1171 | * | |
1172 | * Since we are adding B to A at the top level, checking for cycles should just | |
1173 | * be a matter of seeing if node A is somewhere in tree B. | |
1da177e4 LT |
1174 | */ |
1175 | static int keyring_detect_cycle(struct key *A, struct key *B) | |
1176 | { | |
b2a4df20 | 1177 | struct keyring_search_context ctx = { |
46291959 DH |
1178 | .index_key = A->index_key, |
1179 | .match_data.raw_data = A, | |
1180 | .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT, | |
1181 | .iterator = keyring_detect_cycle_iterator, | |
1182 | .flags = (KEYRING_SEARCH_NO_STATE_CHECK | | |
1183 | KEYRING_SEARCH_NO_UPDATE_TIME | | |
1184 | KEYRING_SEARCH_NO_CHECK_PERM | | |
1185 | KEYRING_SEARCH_DETECT_TOO_DEEP), | |
b2a4df20 | 1186 | }; |
1da177e4 | 1187 | |
76d8aeab | 1188 | rcu_read_lock(); |
b2a4df20 | 1189 | search_nested_keyrings(B, &ctx); |
76d8aeab | 1190 | rcu_read_unlock(); |
b2a4df20 | 1191 | return PTR_ERR(ctx.result) == -EAGAIN ? 0 : PTR_ERR(ctx.result); |
f70e2e06 | 1192 | } |
cab8eb59 | 1193 | |
1da177e4 | 1194 | /* |
973c9f4f | 1195 | * Preallocate memory so that a key can be linked into to a keyring. |
1da177e4 | 1196 | */ |
b2a4df20 DH |
1197 | int __key_link_begin(struct key *keyring, |
1198 | const struct keyring_index_key *index_key, | |
1199 | struct assoc_array_edit **_edit) | |
f70e2e06 | 1200 | __acquires(&keyring->sem) |
423b9788 | 1201 | __acquires(&keyring_serialise_link_sem) |
1da177e4 | 1202 | { |
b2a4df20 DH |
1203 | struct assoc_array_edit *edit; |
1204 | int ret; | |
1da177e4 | 1205 | |
16feef43 | 1206 | kenter("%d,%s,%s,", |
b2a4df20 DH |
1207 | keyring->serial, index_key->type->name, index_key->description); |
1208 | ||
1209 | BUG_ON(index_key->desc_len == 0); | |
1da177e4 | 1210 | |
1da177e4 | 1211 | if (keyring->type != &key_type_keyring) |
f70e2e06 DH |
1212 | return -ENOTDIR; |
1213 | ||
1214 | down_write(&keyring->sem); | |
1215 | ||
1216 | ret = -EKEYREVOKED; | |
1217 | if (test_bit(KEY_FLAG_REVOKED, &keyring->flags)) | |
1218 | goto error_krsem; | |
1da177e4 | 1219 | |
f70e2e06 DH |
1220 | /* serialise link/link calls to prevent parallel calls causing a cycle |
1221 | * when linking two keyring in opposite orders */ | |
16feef43 | 1222 | if (index_key->type == &key_type_keyring) |
553d603c DH |
1223 | down_write(&keyring_serialise_link_sem); |
1224 | ||
b2a4df20 DH |
1225 | /* Create an edit script that will insert/replace the key in the |
1226 | * keyring tree. | |
1227 | */ | |
1228 | edit = assoc_array_insert(&keyring->keys, | |
1229 | &keyring_assoc_array_ops, | |
1230 | index_key, | |
1231 | NULL); | |
1232 | if (IS_ERR(edit)) { | |
1233 | ret = PTR_ERR(edit); | |
034faeb9 DH |
1234 | goto error_sem; |
1235 | } | |
1236 | ||
1237 | /* If we're not replacing a link in-place then we're going to need some | |
1238 | * extra quota. | |
1239 | */ | |
1240 | if (!edit->dead_leaf) { | |
1241 | ret = key_payload_reserve(keyring, | |
1242 | keyring->datalen + KEYQUOTA_LINK_BYTES); | |
1243 | if (ret < 0) | |
1244 | goto error_cancel; | |
1da177e4 LT |
1245 | } |
1246 | ||
b2a4df20 | 1247 | *_edit = edit; |
f70e2e06 DH |
1248 | kleave(" = 0"); |
1249 | return 0; | |
1da177e4 | 1250 | |
034faeb9 DH |
1251 | error_cancel: |
1252 | assoc_array_cancel_edit(edit); | |
f70e2e06 | 1253 | error_sem: |
16feef43 | 1254 | if (index_key->type == &key_type_keyring) |
f70e2e06 DH |
1255 | up_write(&keyring_serialise_link_sem); |
1256 | error_krsem: | |
1257 | up_write(&keyring->sem); | |
1258 | kleave(" = %d", ret); | |
1259 | return ret; | |
1260 | } | |
1da177e4 | 1261 | |
f70e2e06 | 1262 | /* |
973c9f4f DH |
1263 | * Check already instantiated keys aren't going to be a problem. |
1264 | * | |
1265 | * The caller must have called __key_link_begin(). Don't need to call this for | |
1266 | * keys that were created since __key_link_begin() was called. | |
f70e2e06 DH |
1267 | */ |
1268 | int __key_link_check_live_key(struct key *keyring, struct key *key) | |
1269 | { | |
1270 | if (key->type == &key_type_keyring) | |
1271 | /* check that we aren't going to create a cycle by linking one | |
1272 | * keyring to another */ | |
1273 | return keyring_detect_cycle(keyring, key); | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | /* | |
973c9f4f DH |
1278 | * Link a key into to a keyring. |
1279 | * | |
1280 | * Must be called with __key_link_begin() having being called. Discards any | |
1281 | * already extant link to matching key if there is one, so that each keyring | |
1282 | * holds at most one link to any given key of a particular type+description | |
1283 | * combination. | |
f70e2e06 | 1284 | */ |
b2a4df20 | 1285 | void __key_link(struct key *key, struct assoc_array_edit **_edit) |
f70e2e06 | 1286 | { |
ccc3e6d9 | 1287 | __key_get(key); |
b2a4df20 DH |
1288 | assoc_array_insert_set_object(*_edit, keyring_key_to_ptr(key)); |
1289 | assoc_array_apply_edit(*_edit); | |
1290 | *_edit = NULL; | |
f70e2e06 DH |
1291 | } |
1292 | ||
1293 | /* | |
973c9f4f DH |
1294 | * Finish linking a key into to a keyring. |
1295 | * | |
1296 | * Must be called with __key_link_begin() having being called. | |
f70e2e06 | 1297 | */ |
16feef43 DH |
1298 | void __key_link_end(struct key *keyring, |
1299 | const struct keyring_index_key *index_key, | |
b2a4df20 | 1300 | struct assoc_array_edit *edit) |
f70e2e06 | 1301 | __releases(&keyring->sem) |
423b9788 | 1302 | __releases(&keyring_serialise_link_sem) |
f70e2e06 | 1303 | { |
16feef43 | 1304 | BUG_ON(index_key->type == NULL); |
b2a4df20 | 1305 | kenter("%d,%s,", keyring->serial, index_key->type->name); |
f70e2e06 | 1306 | |
16feef43 | 1307 | if (index_key->type == &key_type_keyring) |
f70e2e06 DH |
1308 | up_write(&keyring_serialise_link_sem); |
1309 | ||
ca4da5dd CIK |
1310 | if (edit) { |
1311 | if (!edit->dead_leaf) { | |
1312 | key_payload_reserve(keyring, | |
1313 | keyring->datalen - KEYQUOTA_LINK_BYTES); | |
1314 | } | |
b2a4df20 | 1315 | assoc_array_cancel_edit(edit); |
f70e2e06 DH |
1316 | } |
1317 | up_write(&keyring->sem); | |
1318 | } | |
1da177e4 | 1319 | |
5ac7eace DH |
1320 | /* |
1321 | * Check addition of keys to restricted keyrings. | |
1322 | */ | |
1323 | static int __key_link_check_restriction(struct key *keyring, struct key *key) | |
1324 | { | |
2b6aa412 | 1325 | if (!keyring->restrict_link || !keyring->restrict_link->check) |
5ac7eace | 1326 | return 0; |
2b6aa412 MM |
1327 | return keyring->restrict_link->check(keyring, key->type, &key->payload, |
1328 | keyring->restrict_link->key); | |
5ac7eace DH |
1329 | } |
1330 | ||
973c9f4f DH |
1331 | /** |
1332 | * key_link - Link a key to a keyring | |
1333 | * @keyring: The keyring to make the link in. | |
1334 | * @key: The key to link to. | |
1335 | * | |
1336 | * Make a link in a keyring to a key, such that the keyring holds a reference | |
1337 | * on that key and the key can potentially be found by searching that keyring. | |
1338 | * | |
1339 | * This function will write-lock the keyring's semaphore and will consume some | |
1340 | * of the user's key data quota to hold the link. | |
1341 | * | |
1342 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, | |
1343 | * -EKEYREVOKED if the keyring has been revoked, -ENFILE if the keyring is | |
1344 | * full, -EDQUOT if there is insufficient key data quota remaining to add | |
1345 | * another link or -ENOMEM if there's insufficient memory. | |
1346 | * | |
1347 | * It is assumed that the caller has checked that it is permitted for a link to | |
1348 | * be made (the keyring should have Write permission and the key Link | |
1349 | * permission). | |
1da177e4 LT |
1350 | */ |
1351 | int key_link(struct key *keyring, struct key *key) | |
1352 | { | |
b2a4df20 | 1353 | struct assoc_array_edit *edit; |
1da177e4 LT |
1354 | int ret; |
1355 | ||
fff29291 | 1356 | kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage)); |
b2a4df20 | 1357 | |
1da177e4 LT |
1358 | key_check(keyring); |
1359 | key_check(key); | |
1360 | ||
b2a4df20 | 1361 | ret = __key_link_begin(keyring, &key->index_key, &edit); |
f70e2e06 | 1362 | if (ret == 0) { |
fff29291 | 1363 | kdebug("begun {%d,%d}", keyring->serial, refcount_read(&keyring->usage)); |
5ac7eace DH |
1364 | ret = __key_link_check_restriction(keyring, key); |
1365 | if (ret == 0) | |
1366 | ret = __key_link_check_live_key(keyring, key); | |
f70e2e06 | 1367 | if (ret == 0) |
b2a4df20 DH |
1368 | __key_link(key, &edit); |
1369 | __key_link_end(keyring, &key->index_key, edit); | |
f70e2e06 | 1370 | } |
1da177e4 | 1371 | |
fff29291 | 1372 | kleave(" = %d {%d,%d}", ret, keyring->serial, refcount_read(&keyring->usage)); |
1da177e4 | 1373 | return ret; |
f70e2e06 | 1374 | } |
1da177e4 LT |
1375 | EXPORT_SYMBOL(key_link); |
1376 | ||
973c9f4f DH |
1377 | /** |
1378 | * key_unlink - Unlink the first link to a key from a keyring. | |
1379 | * @keyring: The keyring to remove the link from. | |
1380 | * @key: The key the link is to. | |
1381 | * | |
1382 | * Remove a link from a keyring to a key. | |
1383 | * | |
1384 | * This function will write-lock the keyring's semaphore. | |
1385 | * | |
1386 | * Returns 0 if successful, -ENOTDIR if the keyring isn't a keyring, -ENOENT if | |
1387 | * the key isn't linked to by the keyring or -ENOMEM if there's insufficient | |
1388 | * memory. | |
1389 | * | |
1390 | * It is assumed that the caller has checked that it is permitted for a link to | |
1391 | * be removed (the keyring should have Write permission; no permissions are | |
1392 | * required on the key). | |
1da177e4 LT |
1393 | */ |
1394 | int key_unlink(struct key *keyring, struct key *key) | |
1395 | { | |
b2a4df20 DH |
1396 | struct assoc_array_edit *edit; |
1397 | int ret; | |
1da177e4 LT |
1398 | |
1399 | key_check(keyring); | |
1400 | key_check(key); | |
1401 | ||
1da177e4 | 1402 | if (keyring->type != &key_type_keyring) |
b2a4df20 | 1403 | return -ENOTDIR; |
1da177e4 LT |
1404 | |
1405 | down_write(&keyring->sem); | |
1406 | ||
b2a4df20 DH |
1407 | edit = assoc_array_delete(&keyring->keys, &keyring_assoc_array_ops, |
1408 | &key->index_key); | |
1409 | if (IS_ERR(edit)) { | |
1410 | ret = PTR_ERR(edit); | |
1411 | goto error; | |
1da177e4 | 1412 | } |
1da177e4 | 1413 | ret = -ENOENT; |
b2a4df20 DH |
1414 | if (edit == NULL) |
1415 | goto error; | |
1da177e4 | 1416 | |
b2a4df20 | 1417 | assoc_array_apply_edit(edit); |
034faeb9 | 1418 | key_payload_reserve(keyring, keyring->datalen - KEYQUOTA_LINK_BYTES); |
1da177e4 LT |
1419 | ret = 0; |
1420 | ||
76d8aeab | 1421 | error: |
76d8aeab | 1422 | up_write(&keyring->sem); |
b2a4df20 | 1423 | return ret; |
a8b17ed0 | 1424 | } |
1da177e4 LT |
1425 | EXPORT_SYMBOL(key_unlink); |
1426 | ||
973c9f4f DH |
1427 | /** |
1428 | * keyring_clear - Clear a keyring | |
1429 | * @keyring: The keyring to clear. | |
1430 | * | |
1431 | * Clear the contents of the specified keyring. | |
1432 | * | |
1433 | * Returns 0 if successful or -ENOTDIR if the keyring isn't a keyring. | |
1da177e4 LT |
1434 | */ |
1435 | int keyring_clear(struct key *keyring) | |
1436 | { | |
b2a4df20 | 1437 | struct assoc_array_edit *edit; |
76d8aeab | 1438 | int ret; |
1da177e4 | 1439 | |
b2a4df20 DH |
1440 | if (keyring->type != &key_type_keyring) |
1441 | return -ENOTDIR; | |
1da177e4 | 1442 | |
b2a4df20 | 1443 | down_write(&keyring->sem); |
1da177e4 | 1444 | |
b2a4df20 DH |
1445 | edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops); |
1446 | if (IS_ERR(edit)) { | |
1447 | ret = PTR_ERR(edit); | |
1448 | } else { | |
1449 | if (edit) | |
1450 | assoc_array_apply_edit(edit); | |
1451 | key_payload_reserve(keyring, 0); | |
1da177e4 LT |
1452 | ret = 0; |
1453 | } | |
1454 | ||
b2a4df20 | 1455 | up_write(&keyring->sem); |
1da177e4 | 1456 | return ret; |
a8b17ed0 | 1457 | } |
1da177e4 | 1458 | EXPORT_SYMBOL(keyring_clear); |
31204ed9 | 1459 | |
31204ed9 | 1460 | /* |
973c9f4f DH |
1461 | * Dispose of the links from a revoked keyring. |
1462 | * | |
1463 | * This is called with the key sem write-locked. | |
31204ed9 DH |
1464 | */ |
1465 | static void keyring_revoke(struct key *keyring) | |
1466 | { | |
b2a4df20 | 1467 | struct assoc_array_edit *edit; |
f0641cba | 1468 | |
b2a4df20 DH |
1469 | edit = assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops); |
1470 | if (!IS_ERR(edit)) { | |
1471 | if (edit) | |
1472 | assoc_array_apply_edit(edit); | |
1473 | key_payload_reserve(keyring, 0); | |
1474 | } | |
1475 | } | |
31204ed9 | 1476 | |
62fe3182 | 1477 | static bool keyring_gc_select_iterator(void *object, void *iterator_data) |
b2a4df20 DH |
1478 | { |
1479 | struct key *key = keyring_ptr_to_key(object); | |
074d5898 | 1480 | time64_t *limit = iterator_data; |
31204ed9 | 1481 | |
b2a4df20 DH |
1482 | if (key_is_dead(key, *limit)) |
1483 | return false; | |
1484 | key_get(key); | |
1485 | return true; | |
a8b17ed0 | 1486 | } |
5d135440 | 1487 | |
62fe3182 DH |
1488 | static int keyring_gc_check_iterator(const void *object, void *iterator_data) |
1489 | { | |
1490 | const struct key *key = keyring_ptr_to_key(object); | |
074d5898 | 1491 | time64_t *limit = iterator_data; |
62fe3182 DH |
1492 | |
1493 | key_check(key); | |
1494 | return key_is_dead(key, *limit); | |
1495 | } | |
1496 | ||
5d135440 | 1497 | /* |
62fe3182 | 1498 | * Garbage collect pointers from a keyring. |
973c9f4f | 1499 | * |
62fe3182 DH |
1500 | * Not called with any locks held. The keyring's key struct will not be |
1501 | * deallocated under us as only our caller may deallocate it. | |
5d135440 | 1502 | */ |
074d5898 | 1503 | void keyring_gc(struct key *keyring, time64_t limit) |
5d135440 | 1504 | { |
62fe3182 DH |
1505 | int result; |
1506 | ||
1507 | kenter("%x{%s}", keyring->serial, keyring->description ?: ""); | |
5d135440 | 1508 | |
62fe3182 DH |
1509 | if (keyring->flags & ((1 << KEY_FLAG_INVALIDATED) | |
1510 | (1 << KEY_FLAG_REVOKED))) | |
1511 | goto dont_gc; | |
1512 | ||
1513 | /* scan the keyring looking for dead keys */ | |
1514 | rcu_read_lock(); | |
1515 | result = assoc_array_iterate(&keyring->keys, | |
1516 | keyring_gc_check_iterator, &limit); | |
1517 | rcu_read_unlock(); | |
1518 | if (result == true) | |
1519 | goto do_gc; | |
1520 | ||
1521 | dont_gc: | |
1522 | kleave(" [no gc]"); | |
1523 | return; | |
1524 | ||
1525 | do_gc: | |
5d135440 | 1526 | down_write(&keyring->sem); |
b2a4df20 | 1527 | assoc_array_gc(&keyring->keys, &keyring_assoc_array_ops, |
62fe3182 | 1528 | keyring_gc_select_iterator, &limit); |
c08ef808 | 1529 | up_write(&keyring->sem); |
62fe3182 | 1530 | kleave(" [gc]"); |
5d135440 | 1531 | } |
2b6aa412 MM |
1532 | |
1533 | /* | |
1534 | * Garbage collect restriction pointers from a keyring. | |
1535 | * | |
1536 | * Keyring restrictions are associated with a key type, and must be cleaned | |
1537 | * up if the key type is unregistered. The restriction is altered to always | |
1538 | * reject additional keys so a keyring cannot be opened up by unregistering | |
1539 | * a key type. | |
1540 | * | |
1541 | * Not called with any keyring locks held. The keyring's key struct will not | |
1542 | * be deallocated under us as only our caller may deallocate it. | |
1543 | * | |
1544 | * The caller is required to hold key_types_sem and dead_type->sem. This is | |
1545 | * fulfilled by key_gc_keytype() holding the locks on behalf of | |
1546 | * key_garbage_collector(), which it invokes on a workqueue. | |
1547 | */ | |
1548 | void keyring_restriction_gc(struct key *keyring, struct key_type *dead_type) | |
1549 | { | |
1550 | struct key_restriction *keyres; | |
1551 | ||
1552 | kenter("%x{%s}", keyring->serial, keyring->description ?: ""); | |
1553 | ||
1554 | /* | |
1555 | * keyring->restrict_link is only assigned at key allocation time | |
1556 | * or with the key type locked, so the only values that could be | |
1557 | * concurrently assigned to keyring->restrict_link are for key | |
1558 | * types other than dead_type. Given this, it's ok to check | |
1559 | * the key type before acquiring keyring->sem. | |
1560 | */ | |
1561 | if (!dead_type || !keyring->restrict_link || | |
1562 | keyring->restrict_link->keytype != dead_type) { | |
1563 | kleave(" [no restriction gc]"); | |
1564 | return; | |
1565 | } | |
1566 | ||
1567 | /* Lock the keyring to ensure that a link is not in progress */ | |
1568 | down_write(&keyring->sem); | |
1569 | ||
1570 | keyres = keyring->restrict_link; | |
1571 | ||
1572 | keyres->check = restrict_link_reject; | |
1573 | ||
1574 | key_put(keyres->key); | |
1575 | keyres->key = NULL; | |
1576 | keyres->keytype = NULL; | |
1577 | ||
1578 | up_write(&keyring->sem); | |
1579 | ||
1580 | kleave(" [restriction gc]"); | |
1581 | } |