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