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