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