projects/linux·blob master · refs

1251 lines · 37.4 KiB

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