Merge tag 'for-6.2-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-microblaze.git] / fs / crypto / keyring.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Filesystem-level keyring for fscrypt
4  *
5  * Copyright 2019 Google LLC
6  */
7
8 /*
9  * This file implements management of fscrypt master keys in the
10  * filesystem-level keyring, including the ioctls:
11  *
12  * - FS_IOC_ADD_ENCRYPTION_KEY
13  * - FS_IOC_REMOVE_ENCRYPTION_KEY
14  * - FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
15  * - FS_IOC_GET_ENCRYPTION_KEY_STATUS
16  *
17  * See the "User API" section of Documentation/filesystems/fscrypt.rst for more
18  * information about these ioctls.
19  */
20
21 #include <asm/unaligned.h>
22 #include <crypto/skcipher.h>
23 #include <linux/key-type.h>
24 #include <linux/random.h>
25 #include <linux/seq_file.h>
26
27 #include "fscrypt_private.h"
28
29 /* The master encryption keys for a filesystem (->s_master_keys) */
30 struct fscrypt_keyring {
31         /*
32          * Lock that protects ->key_hashtable.  It does *not* protect the
33          * fscrypt_master_key structs themselves.
34          */
35         spinlock_t lock;
36
37         /* Hash table that maps fscrypt_key_specifier to fscrypt_master_key */
38         struct hlist_head key_hashtable[128];
39 };
40
41 static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
42 {
43         fscrypt_destroy_hkdf(&secret->hkdf);
44         memzero_explicit(secret, sizeof(*secret));
45 }
46
47 static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
48                                    struct fscrypt_master_key_secret *src)
49 {
50         memcpy(dst, src, sizeof(*dst));
51         memzero_explicit(src, sizeof(*src));
52 }
53
54 static void fscrypt_free_master_key(struct rcu_head *head)
55 {
56         struct fscrypt_master_key *mk =
57                 container_of(head, struct fscrypt_master_key, mk_rcu_head);
58         /*
59          * The master key secret and any embedded subkeys should have already
60          * been wiped when the last active reference to the fscrypt_master_key
61          * struct was dropped; doing it here would be unnecessarily late.
62          * Nevertheless, use kfree_sensitive() in case anything was missed.
63          */
64         kfree_sensitive(mk);
65 }
66
67 void fscrypt_put_master_key(struct fscrypt_master_key *mk)
68 {
69         if (!refcount_dec_and_test(&mk->mk_struct_refs))
70                 return;
71         /*
72          * No structural references left, so free ->mk_users, and also free the
73          * fscrypt_master_key struct itself after an RCU grace period ensures
74          * that concurrent keyring lookups can no longer find it.
75          */
76         WARN_ON(refcount_read(&mk->mk_active_refs) != 0);
77         key_put(mk->mk_users);
78         mk->mk_users = NULL;
79         call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
80 }
81
82 void fscrypt_put_master_key_activeref(struct super_block *sb,
83                                       struct fscrypt_master_key *mk)
84 {
85         size_t i;
86
87         if (!refcount_dec_and_test(&mk->mk_active_refs))
88                 return;
89         /*
90          * No active references left, so complete the full removal of this
91          * fscrypt_master_key struct by removing it from the keyring and
92          * destroying any subkeys embedded in it.
93          */
94
95         spin_lock(&sb->s_master_keys->lock);
96         hlist_del_rcu(&mk->mk_node);
97         spin_unlock(&sb->s_master_keys->lock);
98
99         /*
100          * ->mk_active_refs == 0 implies that ->mk_secret is not present and
101          * that ->mk_decrypted_inodes is empty.
102          */
103         WARN_ON(is_master_key_secret_present(&mk->mk_secret));
104         WARN_ON(!list_empty(&mk->mk_decrypted_inodes));
105
106         for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
107                 fscrypt_destroy_prepared_key(
108                                 sb, &mk->mk_direct_keys[i]);
109                 fscrypt_destroy_prepared_key(
110                                 sb, &mk->mk_iv_ino_lblk_64_keys[i]);
111                 fscrypt_destroy_prepared_key(
112                                 sb, &mk->mk_iv_ino_lblk_32_keys[i]);
113         }
114         memzero_explicit(&mk->mk_ino_hash_key,
115                          sizeof(mk->mk_ino_hash_key));
116         mk->mk_ino_hash_key_initialized = false;
117
118         /* Drop the structural ref associated with the active refs. */
119         fscrypt_put_master_key(mk);
120 }
121
122 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
123 {
124         if (spec->__reserved)
125                 return false;
126         return master_key_spec_len(spec) != 0;
127 }
128
129 static int fscrypt_user_key_instantiate(struct key *key,
130                                         struct key_preparsed_payload *prep)
131 {
132         /*
133          * We just charge FSCRYPT_MAX_KEY_SIZE bytes to the user's key quota for
134          * each key, regardless of the exact key size.  The amount of memory
135          * actually used is greater than the size of the raw key anyway.
136          */
137         return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
138 }
139
140 static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
141 {
142         seq_puts(m, key->description);
143 }
144
145 /*
146  * Type of key in ->mk_users.  Each key of this type represents a particular
147  * user who has added a particular master key.
148  *
149  * Note that the name of this key type really should be something like
150  * ".fscrypt-user" instead of simply ".fscrypt".  But the shorter name is chosen
151  * mainly for simplicity of presentation in /proc/keys when read by a non-root
152  * user.  And it is expected to be rare that a key is actually added by multiple
153  * users, since users should keep their encryption keys confidential.
154  */
155 static struct key_type key_type_fscrypt_user = {
156         .name                   = ".fscrypt",
157         .instantiate            = fscrypt_user_key_instantiate,
158         .describe               = fscrypt_user_key_describe,
159 };
160
161 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE       \
162         (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
163          CONST_STRLEN("-users") + 1)
164
165 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE        \
166         (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
167
168 static void format_mk_users_keyring_description(
169                         char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
170                         const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
171 {
172         sprintf(description, "fscrypt-%*phN-users",
173                 FSCRYPT_KEY_IDENTIFIER_SIZE, mk_identifier);
174 }
175
176 static void format_mk_user_description(
177                         char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE],
178                         const u8 mk_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
179 {
180
181         sprintf(description, "%*phN.uid.%u", FSCRYPT_KEY_IDENTIFIER_SIZE,
182                 mk_identifier, __kuid_val(current_fsuid()));
183 }
184
185 /* Create ->s_master_keys if needed.  Synchronized by fscrypt_add_key_mutex. */
186 static int allocate_filesystem_keyring(struct super_block *sb)
187 {
188         struct fscrypt_keyring *keyring;
189
190         if (sb->s_master_keys)
191                 return 0;
192
193         keyring = kzalloc(sizeof(*keyring), GFP_KERNEL);
194         if (!keyring)
195                 return -ENOMEM;
196         spin_lock_init(&keyring->lock);
197         /*
198          * Pairs with the smp_load_acquire() in fscrypt_find_master_key().
199          * I.e., here we publish ->s_master_keys with a RELEASE barrier so that
200          * concurrent tasks can ACQUIRE it.
201          */
202         smp_store_release(&sb->s_master_keys, keyring);
203         return 0;
204 }
205
206 /*
207  * Release all encryption keys that have been added to the filesystem, along
208  * with the keyring that contains them.
209  *
210  * This is called at unmount time.  The filesystem's underlying block device(s)
211  * are still available at this time; this is important because after user file
212  * accesses have been allowed, this function may need to evict keys from the
213  * keyslots of an inline crypto engine, which requires the block device(s).
214  *
215  * This is also called when the super_block is being freed.  This is needed to
216  * avoid a memory leak if mounting fails after the "test_dummy_encryption"
217  * option was processed, as in that case the unmount-time call isn't made.
218  */
219 void fscrypt_destroy_keyring(struct super_block *sb)
220 {
221         struct fscrypt_keyring *keyring = sb->s_master_keys;
222         size_t i;
223
224         if (!keyring)
225                 return;
226
227         for (i = 0; i < ARRAY_SIZE(keyring->key_hashtable); i++) {
228                 struct hlist_head *bucket = &keyring->key_hashtable[i];
229                 struct fscrypt_master_key *mk;
230                 struct hlist_node *tmp;
231
232                 hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) {
233                         /*
234                          * Since all inodes were already evicted, every key
235                          * remaining in the keyring should have an empty inode
236                          * list, and should only still be in the keyring due to
237                          * the single active ref associated with ->mk_secret.
238                          * There should be no structural refs beyond the one
239                          * associated with the active ref.
240                          */
241                         WARN_ON(refcount_read(&mk->mk_active_refs) != 1);
242                         WARN_ON(refcount_read(&mk->mk_struct_refs) != 1);
243                         WARN_ON(!is_master_key_secret_present(&mk->mk_secret));
244                         wipe_master_key_secret(&mk->mk_secret);
245                         fscrypt_put_master_key_activeref(sb, mk);
246                 }
247         }
248         kfree_sensitive(keyring);
249         sb->s_master_keys = NULL;
250 }
251
252 static struct hlist_head *
253 fscrypt_mk_hash_bucket(struct fscrypt_keyring *keyring,
254                        const struct fscrypt_key_specifier *mk_spec)
255 {
256         /*
257          * Since key specifiers should be "random" values, it is sufficient to
258          * use a trivial hash function that just takes the first several bits of
259          * the key specifier.
260          */
261         unsigned long i = get_unaligned((unsigned long *)&mk_spec->u);
262
263         return &keyring->key_hashtable[i % ARRAY_SIZE(keyring->key_hashtable)];
264 }
265
266 /*
267  * Find the specified master key struct in ->s_master_keys and take a structural
268  * ref to it.  The structural ref guarantees that the key struct continues to
269  * exist, but it does *not* guarantee that ->s_master_keys continues to contain
270  * the key struct.  The structural ref needs to be dropped by
271  * fscrypt_put_master_key().  Returns NULL if the key struct is not found.
272  */
273 struct fscrypt_master_key *
274 fscrypt_find_master_key(struct super_block *sb,
275                         const struct fscrypt_key_specifier *mk_spec)
276 {
277         struct fscrypt_keyring *keyring;
278         struct hlist_head *bucket;
279         struct fscrypt_master_key *mk;
280
281         /*
282          * Pairs with the smp_store_release() in allocate_filesystem_keyring().
283          * I.e., another task can publish ->s_master_keys concurrently,
284          * executing a RELEASE barrier.  We need to use smp_load_acquire() here
285          * to safely ACQUIRE the memory the other task published.
286          */
287         keyring = smp_load_acquire(&sb->s_master_keys);
288         if (keyring == NULL)
289                 return NULL; /* No keyring yet, so no keys yet. */
290
291         bucket = fscrypt_mk_hash_bucket(keyring, mk_spec);
292         rcu_read_lock();
293         switch (mk_spec->type) {
294         case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
295                 hlist_for_each_entry_rcu(mk, bucket, mk_node) {
296                         if (mk->mk_spec.type ==
297                                 FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
298                             memcmp(mk->mk_spec.u.descriptor,
299                                    mk_spec->u.descriptor,
300                                    FSCRYPT_KEY_DESCRIPTOR_SIZE) == 0 &&
301                             refcount_inc_not_zero(&mk->mk_struct_refs))
302                                 goto out;
303                 }
304                 break;
305         case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
306                 hlist_for_each_entry_rcu(mk, bucket, mk_node) {
307                         if (mk->mk_spec.type ==
308                                 FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
309                             memcmp(mk->mk_spec.u.identifier,
310                                    mk_spec->u.identifier,
311                                    FSCRYPT_KEY_IDENTIFIER_SIZE) == 0 &&
312                             refcount_inc_not_zero(&mk->mk_struct_refs))
313                                 goto out;
314                 }
315                 break;
316         }
317         mk = NULL;
318 out:
319         rcu_read_unlock();
320         return mk;
321 }
322
323 static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
324 {
325         char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
326         struct key *keyring;
327
328         format_mk_users_keyring_description(description,
329                                             mk->mk_spec.u.identifier);
330         keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
331                                 current_cred(), KEY_POS_SEARCH |
332                                   KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
333                                 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
334         if (IS_ERR(keyring))
335                 return PTR_ERR(keyring);
336
337         mk->mk_users = keyring;
338         return 0;
339 }
340
341 /*
342  * Find the current user's "key" in the master key's ->mk_users.
343  * Returns ERR_PTR(-ENOKEY) if not found.
344  */
345 static struct key *find_master_key_user(struct fscrypt_master_key *mk)
346 {
347         char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
348         key_ref_t keyref;
349
350         format_mk_user_description(description, mk->mk_spec.u.identifier);
351
352         /*
353          * We need to mark the keyring reference as "possessed" so that we
354          * acquire permission to search it, via the KEY_POS_SEARCH permission.
355          */
356         keyref = keyring_search(make_key_ref(mk->mk_users, true /*possessed*/),
357                                 &key_type_fscrypt_user, description, false);
358         if (IS_ERR(keyref)) {
359                 if (PTR_ERR(keyref) == -EAGAIN || /* not found */
360                     PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
361                         keyref = ERR_PTR(-ENOKEY);
362                 return ERR_CAST(keyref);
363         }
364         return key_ref_to_ptr(keyref);
365 }
366
367 /*
368  * Give the current user a "key" in ->mk_users.  This charges the user's quota
369  * and marks the master key as added by the current user, so that it cannot be
370  * removed by another user with the key.  Either ->mk_sem must be held for
371  * write, or the master key must be still undergoing initialization.
372  */
373 static int add_master_key_user(struct fscrypt_master_key *mk)
374 {
375         char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
376         struct key *mk_user;
377         int err;
378
379         format_mk_user_description(description, mk->mk_spec.u.identifier);
380         mk_user = key_alloc(&key_type_fscrypt_user, description,
381                             current_fsuid(), current_gid(), current_cred(),
382                             KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
383         if (IS_ERR(mk_user))
384                 return PTR_ERR(mk_user);
385
386         err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
387         key_put(mk_user);
388         return err;
389 }
390
391 /*
392  * Remove the current user's "key" from ->mk_users.
393  * ->mk_sem must be held for write.
394  *
395  * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
396  */
397 static int remove_master_key_user(struct fscrypt_master_key *mk)
398 {
399         struct key *mk_user;
400         int err;
401
402         mk_user = find_master_key_user(mk);
403         if (IS_ERR(mk_user))
404                 return PTR_ERR(mk_user);
405         err = key_unlink(mk->mk_users, mk_user);
406         key_put(mk_user);
407         return err;
408 }
409
410 /*
411  * Allocate a new fscrypt_master_key, transfer the given secret over to it, and
412  * insert it into sb->s_master_keys.
413  */
414 static int add_new_master_key(struct super_block *sb,
415                               struct fscrypt_master_key_secret *secret,
416                               const struct fscrypt_key_specifier *mk_spec)
417 {
418         struct fscrypt_keyring *keyring = sb->s_master_keys;
419         struct fscrypt_master_key *mk;
420         int err;
421
422         mk = kzalloc(sizeof(*mk), GFP_KERNEL);
423         if (!mk)
424                 return -ENOMEM;
425
426         init_rwsem(&mk->mk_sem);
427         refcount_set(&mk->mk_struct_refs, 1);
428         mk->mk_spec = *mk_spec;
429
430         INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
431         spin_lock_init(&mk->mk_decrypted_inodes_lock);
432
433         if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
434                 err = allocate_master_key_users_keyring(mk);
435                 if (err)
436                         goto out_put;
437                 err = add_master_key_user(mk);
438                 if (err)
439                         goto out_put;
440         }
441
442         move_master_key_secret(&mk->mk_secret, secret);
443         refcount_set(&mk->mk_active_refs, 1); /* ->mk_secret is present */
444
445         spin_lock(&keyring->lock);
446         hlist_add_head_rcu(&mk->mk_node,
447                            fscrypt_mk_hash_bucket(keyring, mk_spec));
448         spin_unlock(&keyring->lock);
449         return 0;
450
451 out_put:
452         fscrypt_put_master_key(mk);
453         return err;
454 }
455
456 #define KEY_DEAD        1
457
458 static int add_existing_master_key(struct fscrypt_master_key *mk,
459                                    struct fscrypt_master_key_secret *secret)
460 {
461         int err;
462
463         /*
464          * If the current user is already in ->mk_users, then there's nothing to
465          * do.  Otherwise, we need to add the user to ->mk_users.  (Neither is
466          * applicable for v1 policy keys, which have NULL ->mk_users.)
467          */
468         if (mk->mk_users) {
469                 struct key *mk_user = find_master_key_user(mk);
470
471                 if (mk_user != ERR_PTR(-ENOKEY)) {
472                         if (IS_ERR(mk_user))
473                                 return PTR_ERR(mk_user);
474                         key_put(mk_user);
475                         return 0;
476                 }
477                 err = add_master_key_user(mk);
478                 if (err)
479                         return err;
480         }
481
482         /* Re-add the secret if needed. */
483         if (!is_master_key_secret_present(&mk->mk_secret)) {
484                 if (!refcount_inc_not_zero(&mk->mk_active_refs))
485                         return KEY_DEAD;
486                 move_master_key_secret(&mk->mk_secret, secret);
487         }
488
489         return 0;
490 }
491
492 static int do_add_master_key(struct super_block *sb,
493                              struct fscrypt_master_key_secret *secret,
494                              const struct fscrypt_key_specifier *mk_spec)
495 {
496         static DEFINE_MUTEX(fscrypt_add_key_mutex);
497         struct fscrypt_master_key *mk;
498         int err;
499
500         mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
501
502         mk = fscrypt_find_master_key(sb, mk_spec);
503         if (!mk) {
504                 /* Didn't find the key in ->s_master_keys.  Add it. */
505                 err = allocate_filesystem_keyring(sb);
506                 if (!err)
507                         err = add_new_master_key(sb, secret, mk_spec);
508         } else {
509                 /*
510                  * Found the key in ->s_master_keys.  Re-add the secret if
511                  * needed, and add the user to ->mk_users if needed.
512                  */
513                 down_write(&mk->mk_sem);
514                 err = add_existing_master_key(mk, secret);
515                 up_write(&mk->mk_sem);
516                 if (err == KEY_DEAD) {
517                         /*
518                          * We found a key struct, but it's already been fully
519                          * removed.  Ignore the old struct and add a new one.
520                          * fscrypt_add_key_mutex means we don't need to worry
521                          * about concurrent adds.
522                          */
523                         err = add_new_master_key(sb, secret, mk_spec);
524                 }
525                 fscrypt_put_master_key(mk);
526         }
527         mutex_unlock(&fscrypt_add_key_mutex);
528         return err;
529 }
530
531 static int add_master_key(struct super_block *sb,
532                           struct fscrypt_master_key_secret *secret,
533                           struct fscrypt_key_specifier *key_spec)
534 {
535         int err;
536
537         if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
538                 err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
539                                         secret->size);
540                 if (err)
541                         return err;
542
543                 /*
544                  * Now that the HKDF context is initialized, the raw key is no
545                  * longer needed.
546                  */
547                 memzero_explicit(secret->raw, secret->size);
548
549                 /* Calculate the key identifier */
550                 err = fscrypt_hkdf_expand(&secret->hkdf,
551                                           HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
552                                           key_spec->u.identifier,
553                                           FSCRYPT_KEY_IDENTIFIER_SIZE);
554                 if (err)
555                         return err;
556         }
557         return do_add_master_key(sb, secret, key_spec);
558 }
559
560 static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
561 {
562         const struct fscrypt_provisioning_key_payload *payload = prep->data;
563
564         if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
565             prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
566                 return -EINVAL;
567
568         if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
569             payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
570                 return -EINVAL;
571
572         if (payload->__reserved)
573                 return -EINVAL;
574
575         prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
576         if (!prep->payload.data[0])
577                 return -ENOMEM;
578
579         prep->quotalen = prep->datalen;
580         return 0;
581 }
582
583 static void fscrypt_provisioning_key_free_preparse(
584                                         struct key_preparsed_payload *prep)
585 {
586         kfree_sensitive(prep->payload.data[0]);
587 }
588
589 static void fscrypt_provisioning_key_describe(const struct key *key,
590                                               struct seq_file *m)
591 {
592         seq_puts(m, key->description);
593         if (key_is_positive(key)) {
594                 const struct fscrypt_provisioning_key_payload *payload =
595                         key->payload.data[0];
596
597                 seq_printf(m, ": %u [%u]", key->datalen, payload->type);
598         }
599 }
600
601 static void fscrypt_provisioning_key_destroy(struct key *key)
602 {
603         kfree_sensitive(key->payload.data[0]);
604 }
605
606 static struct key_type key_type_fscrypt_provisioning = {
607         .name                   = "fscrypt-provisioning",
608         .preparse               = fscrypt_provisioning_key_preparse,
609         .free_preparse          = fscrypt_provisioning_key_free_preparse,
610         .instantiate            = generic_key_instantiate,
611         .describe               = fscrypt_provisioning_key_describe,
612         .destroy                = fscrypt_provisioning_key_destroy,
613 };
614
615 /*
616  * Retrieve the raw key from the Linux keyring key specified by 'key_id', and
617  * store it into 'secret'.
618  *
619  * The key must be of type "fscrypt-provisioning" and must have the field
620  * fscrypt_provisioning_key_payload::type set to 'type', indicating that it's
621  * only usable with fscrypt with the particular KDF version identified by
622  * 'type'.  We don't use the "logon" key type because there's no way to
623  * completely restrict the use of such keys; they can be used by any kernel API
624  * that accepts "logon" keys and doesn't require a specific service prefix.
625  *
626  * The ability to specify the key via Linux keyring key is intended for cases
627  * where userspace needs to re-add keys after the filesystem is unmounted and
628  * re-mounted.  Most users should just provide the raw key directly instead.
629  */
630 static int get_keyring_key(u32 key_id, u32 type,
631                            struct fscrypt_master_key_secret *secret)
632 {
633         key_ref_t ref;
634         struct key *key;
635         const struct fscrypt_provisioning_key_payload *payload;
636         int err;
637
638         ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
639         if (IS_ERR(ref))
640                 return PTR_ERR(ref);
641         key = key_ref_to_ptr(ref);
642
643         if (key->type != &key_type_fscrypt_provisioning)
644                 goto bad_key;
645         payload = key->payload.data[0];
646
647         /* Don't allow fscrypt v1 keys to be used as v2 keys and vice versa. */
648         if (payload->type != type)
649                 goto bad_key;
650
651         secret->size = key->datalen - sizeof(*payload);
652         memcpy(secret->raw, payload->raw, secret->size);
653         err = 0;
654         goto out_put;
655
656 bad_key:
657         err = -EKEYREJECTED;
658 out_put:
659         key_ref_put(ref);
660         return err;
661 }
662
663 /*
664  * Add a master encryption key to the filesystem, causing all files which were
665  * encrypted with it to appear "unlocked" (decrypted) when accessed.
666  *
667  * When adding a key for use by v1 encryption policies, this ioctl is
668  * privileged, and userspace must provide the 'key_descriptor'.
669  *
670  * When adding a key for use by v2+ encryption policies, this ioctl is
671  * unprivileged.  This is needed, in general, to allow non-root users to use
672  * encryption without encountering the visibility problems of process-subscribed
673  * keyrings and the inability to properly remove keys.  This works by having
674  * each key identified by its cryptographically secure hash --- the
675  * 'key_identifier'.  The cryptographic hash ensures that a malicious user
676  * cannot add the wrong key for a given identifier.  Furthermore, each added key
677  * is charged to the appropriate user's quota for the keyrings service, which
678  * prevents a malicious user from adding too many keys.  Finally, we forbid a
679  * user from removing a key while other users have added it too, which prevents
680  * a user who knows another user's key from causing a denial-of-service by
681  * removing it at an inopportune time.  (We tolerate that a user who knows a key
682  * can prevent other users from removing it.)
683  *
684  * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of
685  * Documentation/filesystems/fscrypt.rst.
686  */
687 int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
688 {
689         struct super_block *sb = file_inode(filp)->i_sb;
690         struct fscrypt_add_key_arg __user *uarg = _uarg;
691         struct fscrypt_add_key_arg arg;
692         struct fscrypt_master_key_secret secret;
693         int err;
694
695         if (copy_from_user(&arg, uarg, sizeof(arg)))
696                 return -EFAULT;
697
698         if (!valid_key_spec(&arg.key_spec))
699                 return -EINVAL;
700
701         if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
702                 return -EINVAL;
703
704         /*
705          * Only root can add keys that are identified by an arbitrary descriptor
706          * rather than by a cryptographic hash --- since otherwise a malicious
707          * user could add the wrong key.
708          */
709         if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
710             !capable(CAP_SYS_ADMIN))
711                 return -EACCES;
712
713         memset(&secret, 0, sizeof(secret));
714         if (arg.key_id) {
715                 if (arg.raw_size != 0)
716                         return -EINVAL;
717                 err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
718                 if (err)
719                         goto out_wipe_secret;
720         } else {
721                 if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
722                     arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
723                         return -EINVAL;
724                 secret.size = arg.raw_size;
725                 err = -EFAULT;
726                 if (copy_from_user(secret.raw, uarg->raw, secret.size))
727                         goto out_wipe_secret;
728         }
729
730         err = add_master_key(sb, &secret, &arg.key_spec);
731         if (err)
732                 goto out_wipe_secret;
733
734         /* Return the key identifier to userspace, if applicable */
735         err = -EFAULT;
736         if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
737             copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
738                          FSCRYPT_KEY_IDENTIFIER_SIZE))
739                 goto out_wipe_secret;
740         err = 0;
741 out_wipe_secret:
742         wipe_master_key_secret(&secret);
743         return err;
744 }
745 EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
746
747 static void
748 fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
749 {
750         static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
751
752         get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
753
754         memset(secret, 0, sizeof(*secret));
755         secret->size = FSCRYPT_MAX_KEY_SIZE;
756         memcpy(secret->raw, test_key, FSCRYPT_MAX_KEY_SIZE);
757 }
758
759 int fscrypt_get_test_dummy_key_identifier(
760                                 u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
761 {
762         struct fscrypt_master_key_secret secret;
763         int err;
764
765         fscrypt_get_test_dummy_secret(&secret);
766
767         err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size);
768         if (err)
769                 goto out;
770         err = fscrypt_hkdf_expand(&secret.hkdf, HKDF_CONTEXT_KEY_IDENTIFIER,
771                                   NULL, 0, key_identifier,
772                                   FSCRYPT_KEY_IDENTIFIER_SIZE);
773 out:
774         wipe_master_key_secret(&secret);
775         return err;
776 }
777
778 /**
779  * fscrypt_add_test_dummy_key() - add the test dummy encryption key
780  * @sb: the filesystem instance to add the key to
781  * @dummy_policy: the encryption policy for test_dummy_encryption
782  *
783  * If needed, add the key for the test_dummy_encryption mount option to the
784  * filesystem.  To prevent misuse of this mount option, a per-boot random key is
785  * used instead of a hardcoded one.  This makes it so that any encrypted files
786  * created using this option won't be accessible after a reboot.
787  *
788  * Return: 0 on success, -errno on failure
789  */
790 int fscrypt_add_test_dummy_key(struct super_block *sb,
791                                const struct fscrypt_dummy_policy *dummy_policy)
792 {
793         const union fscrypt_policy *policy = dummy_policy->policy;
794         struct fscrypt_key_specifier key_spec;
795         struct fscrypt_master_key_secret secret;
796         int err;
797
798         if (!policy)
799                 return 0;
800         err = fscrypt_policy_to_key_spec(policy, &key_spec);
801         if (err)
802                 return err;
803         fscrypt_get_test_dummy_secret(&secret);
804         err = add_master_key(sb, &secret, &key_spec);
805         wipe_master_key_secret(&secret);
806         return err;
807 }
808 EXPORT_SYMBOL_GPL(fscrypt_add_test_dummy_key);
809
810 /*
811  * Verify that the current user has added a master key with the given identifier
812  * (returns -ENOKEY if not).  This is needed to prevent a user from encrypting
813  * their files using some other user's key which they don't actually know.
814  * Cryptographically this isn't much of a problem, but the semantics of this
815  * would be a bit weird, so it's best to just forbid it.
816  *
817  * The system administrator (CAP_FOWNER) can override this, which should be
818  * enough for any use cases where encryption policies are being set using keys
819  * that were chosen ahead of time but aren't available at the moment.
820  *
821  * Note that the key may have already removed by the time this returns, but
822  * that's okay; we just care whether the key was there at some point.
823  *
824  * Return: 0 if the key is added, -ENOKEY if it isn't, or another -errno code
825  */
826 int fscrypt_verify_key_added(struct super_block *sb,
827                              const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
828 {
829         struct fscrypt_key_specifier mk_spec;
830         struct fscrypt_master_key *mk;
831         struct key *mk_user;
832         int err;
833
834         mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
835         memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
836
837         mk = fscrypt_find_master_key(sb, &mk_spec);
838         if (!mk) {
839                 err = -ENOKEY;
840                 goto out;
841         }
842         down_read(&mk->mk_sem);
843         mk_user = find_master_key_user(mk);
844         if (IS_ERR(mk_user)) {
845                 err = PTR_ERR(mk_user);
846         } else {
847                 key_put(mk_user);
848                 err = 0;
849         }
850         up_read(&mk->mk_sem);
851         fscrypt_put_master_key(mk);
852 out:
853         if (err == -ENOKEY && capable(CAP_FOWNER))
854                 err = 0;
855         return err;
856 }
857
858 /*
859  * Try to evict the inode's dentries from the dentry cache.  If the inode is a
860  * directory, then it can have at most one dentry; however, that dentry may be
861  * pinned by child dentries, so first try to evict the children too.
862  */
863 static void shrink_dcache_inode(struct inode *inode)
864 {
865         struct dentry *dentry;
866
867         if (S_ISDIR(inode->i_mode)) {
868                 dentry = d_find_any_alias(inode);
869                 if (dentry) {
870                         shrink_dcache_parent(dentry);
871                         dput(dentry);
872                 }
873         }
874         d_prune_aliases(inode);
875 }
876
877 static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
878 {
879         struct fscrypt_info *ci;
880         struct inode *inode;
881         struct inode *toput_inode = NULL;
882
883         spin_lock(&mk->mk_decrypted_inodes_lock);
884
885         list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
886                 inode = ci->ci_inode;
887                 spin_lock(&inode->i_lock);
888                 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
889                         spin_unlock(&inode->i_lock);
890                         continue;
891                 }
892                 __iget(inode);
893                 spin_unlock(&inode->i_lock);
894                 spin_unlock(&mk->mk_decrypted_inodes_lock);
895
896                 shrink_dcache_inode(inode);
897                 iput(toput_inode);
898                 toput_inode = inode;
899
900                 spin_lock(&mk->mk_decrypted_inodes_lock);
901         }
902
903         spin_unlock(&mk->mk_decrypted_inodes_lock);
904         iput(toput_inode);
905 }
906
907 static int check_for_busy_inodes(struct super_block *sb,
908                                  struct fscrypt_master_key *mk)
909 {
910         struct list_head *pos;
911         size_t busy_count = 0;
912         unsigned long ino;
913         char ino_str[50] = "";
914
915         spin_lock(&mk->mk_decrypted_inodes_lock);
916
917         list_for_each(pos, &mk->mk_decrypted_inodes)
918                 busy_count++;
919
920         if (busy_count == 0) {
921                 spin_unlock(&mk->mk_decrypted_inodes_lock);
922                 return 0;
923         }
924
925         {
926                 /* select an example file to show for debugging purposes */
927                 struct inode *inode =
928                         list_first_entry(&mk->mk_decrypted_inodes,
929                                          struct fscrypt_info,
930                                          ci_master_key_link)->ci_inode;
931                 ino = inode->i_ino;
932         }
933         spin_unlock(&mk->mk_decrypted_inodes_lock);
934
935         /* If the inode is currently being created, ino may still be 0. */
936         if (ino)
937                 snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
938
939         fscrypt_warn(NULL,
940                      "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
941                      sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
942                      master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
943                      ino_str);
944         return -EBUSY;
945 }
946
947 static int try_to_lock_encrypted_files(struct super_block *sb,
948                                        struct fscrypt_master_key *mk)
949 {
950         int err1;
951         int err2;
952
953         /*
954          * An inode can't be evicted while it is dirty or has dirty pages.
955          * Thus, we first have to clean the inodes in ->mk_decrypted_inodes.
956          *
957          * Just do it the easy way: call sync_filesystem().  It's overkill, but
958          * it works, and it's more important to minimize the amount of caches we
959          * drop than the amount of data we sync.  Also, unprivileged users can
960          * already call sync_filesystem() via sys_syncfs() or sys_sync().
961          */
962         down_read(&sb->s_umount);
963         err1 = sync_filesystem(sb);
964         up_read(&sb->s_umount);
965         /* If a sync error occurs, still try to evict as much as possible. */
966
967         /*
968          * Inodes are pinned by their dentries, so we have to evict their
969          * dentries.  shrink_dcache_sb() would suffice, but would be overkill
970          * and inappropriate for use by unprivileged users.  So instead go
971          * through the inodes' alias lists and try to evict each dentry.
972          */
973         evict_dentries_for_decrypted_inodes(mk);
974
975         /*
976          * evict_dentries_for_decrypted_inodes() already iput() each inode in
977          * the list; any inodes for which that dropped the last reference will
978          * have been evicted due to fscrypt_drop_inode() detecting the key
979          * removal and telling the VFS to evict the inode.  So to finish, we
980          * just need to check whether any inodes couldn't be evicted.
981          */
982         err2 = check_for_busy_inodes(sb, mk);
983
984         return err1 ?: err2;
985 }
986
987 /*
988  * Try to remove an fscrypt master encryption key.
989  *
990  * FS_IOC_REMOVE_ENCRYPTION_KEY (all_users=false) removes the current user's
991  * claim to the key, then removes the key itself if no other users have claims.
992  * FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS (all_users=true) always removes the
993  * key itself.
994  *
995  * To "remove the key itself", first we wipe the actual master key secret, so
996  * that no more inodes can be unlocked with it.  Then we try to evict all cached
997  * inodes that had been unlocked with the key.
998  *
999  * If all inodes were evicted, then we unlink the fscrypt_master_key from the
1000  * keyring.  Otherwise it remains in the keyring in the "incompletely removed"
1001  * state (without the actual secret key) where it tracks the list of remaining
1002  * inodes.  Userspace can execute the ioctl again later to retry eviction, or
1003  * alternatively can re-add the secret key again.
1004  *
1005  * For more details, see the "Removing keys" section of
1006  * Documentation/filesystems/fscrypt.rst.
1007  */
1008 static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
1009 {
1010         struct super_block *sb = file_inode(filp)->i_sb;
1011         struct fscrypt_remove_key_arg __user *uarg = _uarg;
1012         struct fscrypt_remove_key_arg arg;
1013         struct fscrypt_master_key *mk;
1014         u32 status_flags = 0;
1015         int err;
1016         bool inodes_remain;
1017
1018         if (copy_from_user(&arg, uarg, sizeof(arg)))
1019                 return -EFAULT;
1020
1021         if (!valid_key_spec(&arg.key_spec))
1022                 return -EINVAL;
1023
1024         if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1025                 return -EINVAL;
1026
1027         /*
1028          * Only root can add and remove keys that are identified by an arbitrary
1029          * descriptor rather than by a cryptographic hash.
1030          */
1031         if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
1032             !capable(CAP_SYS_ADMIN))
1033                 return -EACCES;
1034
1035         /* Find the key being removed. */
1036         mk = fscrypt_find_master_key(sb, &arg.key_spec);
1037         if (!mk)
1038                 return -ENOKEY;
1039         down_write(&mk->mk_sem);
1040
1041         /* If relevant, remove current user's (or all users) claim to the key */
1042         if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
1043                 if (all_users)
1044                         err = keyring_clear(mk->mk_users);
1045                 else
1046                         err = remove_master_key_user(mk);
1047                 if (err) {
1048                         up_write(&mk->mk_sem);
1049                         goto out_put_key;
1050                 }
1051                 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
1052                         /*
1053                          * Other users have still added the key too.  We removed
1054                          * the current user's claim to the key, but we still
1055                          * can't remove the key itself.
1056                          */
1057                         status_flags |=
1058                                 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
1059                         err = 0;
1060                         up_write(&mk->mk_sem);
1061                         goto out_put_key;
1062                 }
1063         }
1064
1065         /* No user claims remaining.  Go ahead and wipe the secret. */
1066         err = -ENOKEY;
1067         if (is_master_key_secret_present(&mk->mk_secret)) {
1068                 wipe_master_key_secret(&mk->mk_secret);
1069                 fscrypt_put_master_key_activeref(sb, mk);
1070                 err = 0;
1071         }
1072         inodes_remain = refcount_read(&mk->mk_active_refs) > 0;
1073         up_write(&mk->mk_sem);
1074
1075         if (inodes_remain) {
1076                 /* Some inodes still reference this key; try to evict them. */
1077                 err = try_to_lock_encrypted_files(sb, mk);
1078                 if (err == -EBUSY) {
1079                         status_flags |=
1080                                 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
1081                         err = 0;
1082                 }
1083         }
1084         /*
1085          * We return 0 if we successfully did something: removed a claim to the
1086          * key, wiped the secret, or tried locking the files again.  Users need
1087          * to check the informational status flags if they care whether the key
1088          * has been fully removed including all files locked.
1089          */
1090 out_put_key:
1091         fscrypt_put_master_key(mk);
1092         if (err == 0)
1093                 err = put_user(status_flags, &uarg->removal_status_flags);
1094         return err;
1095 }
1096
1097 int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
1098 {
1099         return do_remove_key(filp, uarg, false);
1100 }
1101 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1102
1103 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
1104 {
1105         if (!capable(CAP_SYS_ADMIN))
1106                 return -EACCES;
1107         return do_remove_key(filp, uarg, true);
1108 }
1109 EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
1110
1111 /*
1112  * Retrieve the status of an fscrypt master encryption key.
1113  *
1114  * We set ->status to indicate whether the key is absent, present, or
1115  * incompletely removed.  "Incompletely removed" means that the master key
1116  * secret has been removed, but some files which had been unlocked with it are
1117  * still in use.  This field allows applications to easily determine the state
1118  * of an encrypted directory without using a hack such as trying to open a
1119  * regular file in it (which can confuse the "incompletely removed" state with
1120  * absent or present).
1121  *
1122  * In addition, for v2 policy keys we allow applications to determine, via
1123  * ->status_flags and ->user_count, whether the key has been added by the
1124  * current user, by other users, or by both.  Most applications should not need
1125  * this, since ordinarily only one user should know a given key.  However, if a
1126  * secret key is shared by multiple users, applications may wish to add an
1127  * already-present key to prevent other users from removing it.  This ioctl can
1128  * be used to check whether that really is the case before the work is done to
1129  * add the key --- which might e.g. require prompting the user for a passphrase.
1130  *
1131  * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of
1132  * Documentation/filesystems/fscrypt.rst.
1133  */
1134 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
1135 {
1136         struct super_block *sb = file_inode(filp)->i_sb;
1137         struct fscrypt_get_key_status_arg arg;
1138         struct fscrypt_master_key *mk;
1139         int err;
1140
1141         if (copy_from_user(&arg, uarg, sizeof(arg)))
1142                 return -EFAULT;
1143
1144         if (!valid_key_spec(&arg.key_spec))
1145                 return -EINVAL;
1146
1147         if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1148                 return -EINVAL;
1149
1150         arg.status_flags = 0;
1151         arg.user_count = 0;
1152         memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
1153
1154         mk = fscrypt_find_master_key(sb, &arg.key_spec);
1155         if (!mk) {
1156                 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
1157                 err = 0;
1158                 goto out;
1159         }
1160         down_read(&mk->mk_sem);
1161
1162         if (!is_master_key_secret_present(&mk->mk_secret)) {
1163                 arg.status = refcount_read(&mk->mk_active_refs) > 0 ?
1164                         FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED :
1165                         FSCRYPT_KEY_STATUS_ABSENT /* raced with full removal */;
1166                 err = 0;
1167                 goto out_release_key;
1168         }
1169
1170         arg.status = FSCRYPT_KEY_STATUS_PRESENT;
1171         if (mk->mk_users) {
1172                 struct key *mk_user;
1173
1174                 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
1175                 mk_user = find_master_key_user(mk);
1176                 if (!IS_ERR(mk_user)) {
1177                         arg.status_flags |=
1178                                 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
1179                         key_put(mk_user);
1180                 } else if (mk_user != ERR_PTR(-ENOKEY)) {
1181                         err = PTR_ERR(mk_user);
1182                         goto out_release_key;
1183                 }
1184         }
1185         err = 0;
1186 out_release_key:
1187         up_read(&mk->mk_sem);
1188         fscrypt_put_master_key(mk);
1189 out:
1190         if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
1191                 err = -EFAULT;
1192         return err;
1193 }
1194 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
1195
1196 int __init fscrypt_init_keyring(void)
1197 {
1198         int err;
1199
1200         err = register_key_type(&key_type_fscrypt_user);
1201         if (err)
1202                 return err;
1203
1204         err = register_key_type(&key_type_fscrypt_provisioning);
1205         if (err)
1206                 goto err_unregister_fscrypt_user;
1207
1208         return 0;
1209
1210 err_unregister_fscrypt_user:
1211         unregister_key_type(&key_type_fscrypt_user);
1212         return err;
1213 }