1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <crypto/skcipher.h>
22#include <linux/key-type.h>
23#include <linux/random.h>
24#include <linux/seq_file.h>
25
26#include "fscrypt_private.h"
27
28static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
29{
30 fscrypt_destroy_hkdf(&secret->hkdf);
31 memzero_explicit(secret, sizeof(*secret));
32}
33
34static void move_master_key_secret(struct fscrypt_master_key_secret *dst,
35 struct fscrypt_master_key_secret *src)
36{
37 memcpy(dst, src, sizeof(*dst));
38 memzero_explicit(src, sizeof(*src));
39}
40
41static void free_master_key(struct fscrypt_master_key *mk)
42{
43 size_t i;
44
45 wipe_master_key_secret(&mk->mk_secret);
46
47 for (i = 0; i <= __FSCRYPT_MODE_MAX; i++) {
48 fscrypt_destroy_prepared_key(&mk->mk_direct_keys[i]);
49 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_64_keys[i]);
50 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_32_keys[i]);
51 }
52
53 key_put(mk->mk_users);
54 kfree_sensitive(mk);
55}
56
57static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
58{
59 if (spec->__reserved)
60 return false;
61 return master_key_spec_len(spec) != 0;
62}
63
64static int fscrypt_key_instantiate(struct key *key,
65 struct key_preparsed_payload *prep)
66{
67 key->payload.data[0] = (struct fscrypt_master_key *)prep->data;
68 return 0;
69}
70
71static void fscrypt_key_destroy(struct key *key)
72{
73 free_master_key(key->payload.data[0]);
74}
75
76static void fscrypt_key_describe(const struct key *key, struct seq_file *m)
77{
78 seq_puts(m, key->description);
79
80 if (key_is_positive(key)) {
81 const struct fscrypt_master_key *mk = key->payload.data[0];
82
83 if (!is_master_key_secret_present(&mk->mk_secret))
84 seq_puts(m, ": secret removed");
85 }
86}
87
88
89
90
91
92
93
94
95static struct key_type key_type_fscrypt = {
96 .name = "._fscrypt",
97 .instantiate = fscrypt_key_instantiate,
98 .destroy = fscrypt_key_destroy,
99 .describe = fscrypt_key_describe,
100};
101
102static int fscrypt_user_key_instantiate(struct key *key,
103 struct key_preparsed_payload *prep)
104{
105
106
107
108
109
110 return key_payload_reserve(key, FSCRYPT_MAX_KEY_SIZE);
111}
112
113static void fscrypt_user_key_describe(const struct key *key, struct seq_file *m)
114{
115 seq_puts(m, key->description);
116}
117
118
119
120
121
122
123
124
125
126
127
128static struct key_type key_type_fscrypt_user = {
129 .name = ".fscrypt",
130 .instantiate = fscrypt_user_key_instantiate,
131 .describe = fscrypt_user_key_describe,
132};
133
134
135static struct key *search_fscrypt_keyring(struct key *keyring,
136 struct key_type *type,
137 const char *description)
138{
139
140
141
142
143 key_ref_t keyref = make_key_ref(keyring, true );
144
145 keyref = keyring_search(keyref, type, description, false);
146 if (IS_ERR(keyref)) {
147 if (PTR_ERR(keyref) == -EAGAIN ||
148 PTR_ERR(keyref) == -EKEYREVOKED)
149 keyref = ERR_PTR(-ENOKEY);
150 return ERR_CAST(keyref);
151 }
152 return key_ref_to_ptr(keyref);
153}
154
155#define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
156 (CONST_STRLEN("fscrypt-") + sizeof_field(struct super_block, s_id))
157
158#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
159
160#define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
161 (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
162 CONST_STRLEN("-users") + 1)
163
164#define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
165 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
166
167static void format_fs_keyring_description(
168 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
169 const struct super_block *sb)
170{
171 sprintf(description, "fscrypt-%s", sb->s_id);
172}
173
174static void format_mk_description(
175 char description[FSCRYPT_MK_DESCRIPTION_SIZE],
176 const struct fscrypt_key_specifier *mk_spec)
177{
178 sprintf(description, "%*phN",
179 master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
180}
181
182static 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
190static 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
200static int allocate_filesystem_keyring(struct super_block *sb)
201{
202 char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
203 struct key *keyring;
204
205 if (sb->s_master_keys)
206 return 0;
207
208 format_fs_keyring_description(description, sb);
209 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
210 current_cred(), KEY_POS_SEARCH |
211 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
212 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
213 if (IS_ERR(keyring))
214 return PTR_ERR(keyring);
215
216
217
218
219
220
221 smp_store_release(&sb->s_master_keys, keyring);
222 return 0;
223}
224
225void fscrypt_sb_free(struct super_block *sb)
226{
227 key_put(sb->s_master_keys);
228 sb->s_master_keys = NULL;
229}
230
231
232
233
234
235struct key *fscrypt_find_master_key(struct super_block *sb,
236 const struct fscrypt_key_specifier *mk_spec)
237{
238 struct key *keyring;
239 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
240
241
242
243
244
245
246
247 keyring = smp_load_acquire(&sb->s_master_keys);
248 if (keyring == NULL)
249 return ERR_PTR(-ENOKEY);
250
251 format_mk_description(description, mk_spec);
252 return search_fscrypt_keyring(keyring, &key_type_fscrypt, description);
253}
254
255static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
256{
257 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE];
258 struct key *keyring;
259
260 format_mk_users_keyring_description(description,
261 mk->mk_spec.u.identifier);
262 keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
263 current_cred(), KEY_POS_SEARCH |
264 KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
265 KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
266 if (IS_ERR(keyring))
267 return PTR_ERR(keyring);
268
269 mk->mk_users = keyring;
270 return 0;
271}
272
273
274
275
276
277static struct key *find_master_key_user(struct fscrypt_master_key *mk)
278{
279 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
280
281 format_mk_user_description(description, mk->mk_spec.u.identifier);
282 return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user,
283 description);
284}
285
286
287
288
289
290
291
292static int add_master_key_user(struct fscrypt_master_key *mk)
293{
294 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
295 struct key *mk_user;
296 int err;
297
298 format_mk_user_description(description, mk->mk_spec.u.identifier);
299 mk_user = key_alloc(&key_type_fscrypt_user, description,
300 current_fsuid(), current_gid(), current_cred(),
301 KEY_POS_SEARCH | KEY_USR_VIEW, 0, NULL);
302 if (IS_ERR(mk_user))
303 return PTR_ERR(mk_user);
304
305 err = key_instantiate_and_link(mk_user, NULL, 0, mk->mk_users, NULL);
306 key_put(mk_user);
307 return err;
308}
309
310
311
312
313
314
315
316static int remove_master_key_user(struct fscrypt_master_key *mk)
317{
318 struct key *mk_user;
319 int err;
320
321 mk_user = find_master_key_user(mk);
322 if (IS_ERR(mk_user))
323 return PTR_ERR(mk_user);
324 err = key_unlink(mk->mk_users, mk_user);
325 key_put(mk_user);
326 return err;
327}
328
329
330
331
332
333
334static int add_new_master_key(struct fscrypt_master_key_secret *secret,
335 const struct fscrypt_key_specifier *mk_spec,
336 struct key *keyring)
337{
338 struct fscrypt_master_key *mk;
339 char description[FSCRYPT_MK_DESCRIPTION_SIZE];
340 struct key *key;
341 int err;
342
343 mk = kzalloc(sizeof(*mk), GFP_KERNEL);
344 if (!mk)
345 return -ENOMEM;
346
347 mk->mk_spec = *mk_spec;
348
349 move_master_key_secret(&mk->mk_secret, secret);
350 init_rwsem(&mk->mk_secret_sem);
351
352 refcount_set(&mk->mk_refcount, 1);
353 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
354 spin_lock_init(&mk->mk_decrypted_inodes_lock);
355
356 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
357 err = allocate_master_key_users_keyring(mk);
358 if (err)
359 goto out_free_mk;
360 err = add_master_key_user(mk);
361 if (err)
362 goto out_free_mk;
363 }
364
365
366
367
368
369
370 format_mk_description(description, mk_spec);
371 key = key_alloc(&key_type_fscrypt, description,
372 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
373 KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
374 KEY_ALLOC_NOT_IN_QUOTA, NULL);
375 if (IS_ERR(key)) {
376 err = PTR_ERR(key);
377 goto out_free_mk;
378 }
379 err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
380 key_put(key);
381 if (err)
382 goto out_free_mk;
383
384 return 0;
385
386out_free_mk:
387 free_master_key(mk);
388 return err;
389}
390
391#define KEY_DEAD 1
392
393static int add_existing_master_key(struct fscrypt_master_key *mk,
394 struct fscrypt_master_key_secret *secret)
395{
396 struct key *mk_user;
397 bool rekey;
398 int err;
399
400
401
402
403
404 if (mk->mk_users) {
405 mk_user = find_master_key_user(mk);
406 if (mk_user != ERR_PTR(-ENOKEY)) {
407 if (IS_ERR(mk_user))
408 return PTR_ERR(mk_user);
409 key_put(mk_user);
410 return 0;
411 }
412 }
413
414
415 rekey = !is_master_key_secret_present(&mk->mk_secret);
416 if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
417 return KEY_DEAD;
418
419
420 if (mk->mk_users) {
421 err = add_master_key_user(mk);
422 if (err) {
423 if (rekey && refcount_dec_and_test(&mk->mk_refcount))
424 return KEY_DEAD;
425 return err;
426 }
427 }
428
429
430 if (rekey) {
431 down_write(&mk->mk_secret_sem);
432 move_master_key_secret(&mk->mk_secret, secret);
433 up_write(&mk->mk_secret_sem);
434 }
435 return 0;
436}
437
438static int do_add_master_key(struct super_block *sb,
439 struct fscrypt_master_key_secret *secret,
440 const struct fscrypt_key_specifier *mk_spec)
441{
442 static DEFINE_MUTEX(fscrypt_add_key_mutex);
443 struct key *key;
444 int err;
445
446 mutex_lock(&fscrypt_add_key_mutex);
447retry:
448 key = fscrypt_find_master_key(sb, mk_spec);
449 if (IS_ERR(key)) {
450 err = PTR_ERR(key);
451 if (err != -ENOKEY)
452 goto out_unlock;
453
454 err = allocate_filesystem_keyring(sb);
455 if (err)
456 goto out_unlock;
457 err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
458 } else {
459
460
461
462
463 down_write(&key->sem);
464 err = add_existing_master_key(key->payload.data[0], secret);
465 up_write(&key->sem);
466 if (err == KEY_DEAD) {
467
468 key_invalidate(key);
469 key_put(key);
470 goto retry;
471 }
472 key_put(key);
473 }
474out_unlock:
475 mutex_unlock(&fscrypt_add_key_mutex);
476 return err;
477}
478
479static int add_master_key(struct super_block *sb,
480 struct fscrypt_master_key_secret *secret,
481 struct fscrypt_key_specifier *key_spec)
482{
483 int err;
484
485 if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
486 err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
487 secret->size);
488 if (err)
489 return err;
490
491
492
493
494
495 memzero_explicit(secret->raw, secret->size);
496
497
498 err = fscrypt_hkdf_expand(&secret->hkdf,
499 HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
500 key_spec->u.identifier,
501 FSCRYPT_KEY_IDENTIFIER_SIZE);
502 if (err)
503 return err;
504 }
505 return do_add_master_key(sb, secret, key_spec);
506}
507
508static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
509{
510 const struct fscrypt_provisioning_key_payload *payload = prep->data;
511
512 if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
513 prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
514 return -EINVAL;
515
516 if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
517 payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
518 return -EINVAL;
519
520 if (payload->__reserved)
521 return -EINVAL;
522
523 prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
524 if (!prep->payload.data[0])
525 return -ENOMEM;
526
527 prep->quotalen = prep->datalen;
528 return 0;
529}
530
531static void fscrypt_provisioning_key_free_preparse(
532 struct key_preparsed_payload *prep)
533{
534 kfree_sensitive(prep->payload.data[0]);
535}
536
537static void fscrypt_provisioning_key_describe(const struct key *key,
538 struct seq_file *m)
539{
540 seq_puts(m, key->description);
541 if (key_is_positive(key)) {
542 const struct fscrypt_provisioning_key_payload *payload =
543 key->payload.data[0];
544
545 seq_printf(m, ": %u [%u]", key->datalen, payload->type);
546 }
547}
548
549static void fscrypt_provisioning_key_destroy(struct key *key)
550{
551 kfree_sensitive(key->payload.data[0]);
552}
553
554static struct key_type key_type_fscrypt_provisioning = {
555 .name = "fscrypt-provisioning",
556 .preparse = fscrypt_provisioning_key_preparse,
557 .free_preparse = fscrypt_provisioning_key_free_preparse,
558 .instantiate = generic_key_instantiate,
559 .describe = fscrypt_provisioning_key_describe,
560 .destroy = fscrypt_provisioning_key_destroy,
561};
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578static int get_keyring_key(u32 key_id, u32 type,
579 struct fscrypt_master_key_secret *secret)
580{
581 key_ref_t ref;
582 struct key *key;
583 const struct fscrypt_provisioning_key_payload *payload;
584 int err;
585
586 ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
587 if (IS_ERR(ref))
588 return PTR_ERR(ref);
589 key = key_ref_to_ptr(ref);
590
591 if (key->type != &key_type_fscrypt_provisioning)
592 goto bad_key;
593 payload = key->payload.data[0];
594
595
596 if (payload->type != type)
597 goto bad_key;
598
599 secret->size = key->datalen - sizeof(*payload);
600 memcpy(secret->raw, payload->raw, secret->size);
601 err = 0;
602 goto out_put;
603
604bad_key:
605 err = -EKEYREJECTED;
606out_put:
607 key_ref_put(ref);
608 return err;
609}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
636{
637 struct super_block *sb = file_inode(filp)->i_sb;
638 struct fscrypt_add_key_arg __user *uarg = _uarg;
639 struct fscrypt_add_key_arg arg;
640 struct fscrypt_master_key_secret secret;
641 int err;
642
643 if (copy_from_user(&arg, uarg, sizeof(arg)))
644 return -EFAULT;
645
646 if (!valid_key_spec(&arg.key_spec))
647 return -EINVAL;
648
649 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
650 return -EINVAL;
651
652
653
654
655
656
657 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
658 !capable(CAP_SYS_ADMIN))
659 return -EACCES;
660
661 memset(&secret, 0, sizeof(secret));
662 if (arg.key_id) {
663 if (arg.raw_size != 0)
664 return -EINVAL;
665 err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
666 if (err)
667 goto out_wipe_secret;
668 } else {
669 if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
670 arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
671 return -EINVAL;
672 secret.size = arg.raw_size;
673 err = -EFAULT;
674 if (copy_from_user(secret.raw, uarg->raw, secret.size))
675 goto out_wipe_secret;
676 }
677
678 err = add_master_key(sb, &secret, &arg.key_spec);
679 if (err)
680 goto out_wipe_secret;
681
682
683 err = -EFAULT;
684 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
685 copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
686 FSCRYPT_KEY_IDENTIFIER_SIZE))
687 goto out_wipe_secret;
688 err = 0;
689out_wipe_secret:
690 wipe_master_key_secret(&secret);
691 return err;
692}
693EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
694
695
696
697
698
699
700int fscrypt_add_test_dummy_key(struct super_block *sb,
701 struct fscrypt_key_specifier *key_spec)
702{
703 static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
704 struct fscrypt_master_key_secret secret;
705 int err;
706
707 get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
708
709 memset(&secret, 0, sizeof(secret));
710 secret.size = FSCRYPT_MAX_KEY_SIZE;
711 memcpy(secret.raw, test_key, FSCRYPT_MAX_KEY_SIZE);
712
713 err = add_master_key(sb, &secret, key_spec);
714 wipe_master_key_secret(&secret);
715 return err;
716}
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734int fscrypt_verify_key_added(struct super_block *sb,
735 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
736{
737 struct fscrypt_key_specifier mk_spec;
738 struct key *key, *mk_user;
739 struct fscrypt_master_key *mk;
740 int err;
741
742 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
743 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
744
745 key = fscrypt_find_master_key(sb, &mk_spec);
746 if (IS_ERR(key)) {
747 err = PTR_ERR(key);
748 goto out;
749 }
750 mk = key->payload.data[0];
751 mk_user = find_master_key_user(mk);
752 if (IS_ERR(mk_user)) {
753 err = PTR_ERR(mk_user);
754 } else {
755 key_put(mk_user);
756 err = 0;
757 }
758 key_put(key);
759out:
760 if (err == -ENOKEY && capable(CAP_FOWNER))
761 err = 0;
762 return err;
763}
764
765
766
767
768
769
770static void shrink_dcache_inode(struct inode *inode)
771{
772 struct dentry *dentry;
773
774 if (S_ISDIR(inode->i_mode)) {
775 dentry = d_find_any_alias(inode);
776 if (dentry) {
777 shrink_dcache_parent(dentry);
778 dput(dentry);
779 }
780 }
781 d_prune_aliases(inode);
782}
783
784static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
785{
786 struct fscrypt_info *ci;
787 struct inode *inode;
788 struct inode *toput_inode = NULL;
789
790 spin_lock(&mk->mk_decrypted_inodes_lock);
791
792 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
793 inode = ci->ci_inode;
794 spin_lock(&inode->i_lock);
795 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
796 spin_unlock(&inode->i_lock);
797 continue;
798 }
799 __iget(inode);
800 spin_unlock(&inode->i_lock);
801 spin_unlock(&mk->mk_decrypted_inodes_lock);
802
803 shrink_dcache_inode(inode);
804 iput(toput_inode);
805 toput_inode = inode;
806
807 spin_lock(&mk->mk_decrypted_inodes_lock);
808 }
809
810 spin_unlock(&mk->mk_decrypted_inodes_lock);
811 iput(toput_inode);
812}
813
814static int check_for_busy_inodes(struct super_block *sb,
815 struct fscrypt_master_key *mk)
816{
817 struct list_head *pos;
818 size_t busy_count = 0;
819 unsigned long ino;
820 char ino_str[50] = "";
821
822 spin_lock(&mk->mk_decrypted_inodes_lock);
823
824 list_for_each(pos, &mk->mk_decrypted_inodes)
825 busy_count++;
826
827 if (busy_count == 0) {
828 spin_unlock(&mk->mk_decrypted_inodes_lock);
829 return 0;
830 }
831
832 {
833
834 struct inode *inode =
835 list_first_entry(&mk->mk_decrypted_inodes,
836 struct fscrypt_info,
837 ci_master_key_link)->ci_inode;
838 ino = inode->i_ino;
839 }
840 spin_unlock(&mk->mk_decrypted_inodes_lock);
841
842
843 if (ino)
844 snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
845
846 fscrypt_warn(NULL,
847 "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
848 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
849 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
850 ino_str);
851 return -EBUSY;
852}
853
854static int try_to_lock_encrypted_files(struct super_block *sb,
855 struct fscrypt_master_key *mk)
856{
857 int err1;
858 int err2;
859
860
861
862
863
864
865
866
867
868
869 down_read(&sb->s_umount);
870 err1 = sync_filesystem(sb);
871 up_read(&sb->s_umount);
872
873
874
875
876
877
878
879
880 evict_dentries_for_decrypted_inodes(mk);
881
882
883
884
885
886
887
888
889 err2 = check_for_busy_inodes(sb, mk);
890
891 return err1 ?: err2;
892}
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
916{
917 struct super_block *sb = file_inode(filp)->i_sb;
918 struct fscrypt_remove_key_arg __user *uarg = _uarg;
919 struct fscrypt_remove_key_arg arg;
920 struct key *key;
921 struct fscrypt_master_key *mk;
922 u32 status_flags = 0;
923 int err;
924 bool dead;
925
926 if (copy_from_user(&arg, uarg, sizeof(arg)))
927 return -EFAULT;
928
929 if (!valid_key_spec(&arg.key_spec))
930 return -EINVAL;
931
932 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
933 return -EINVAL;
934
935
936
937
938
939 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
940 !capable(CAP_SYS_ADMIN))
941 return -EACCES;
942
943
944 key = fscrypt_find_master_key(sb, &arg.key_spec);
945 if (IS_ERR(key))
946 return PTR_ERR(key);
947 mk = key->payload.data[0];
948
949 down_write(&key->sem);
950
951
952 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
953 if (all_users)
954 err = keyring_clear(mk->mk_users);
955 else
956 err = remove_master_key_user(mk);
957 if (err) {
958 up_write(&key->sem);
959 goto out_put_key;
960 }
961 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
962
963
964
965
966
967 status_flags |=
968 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
969 err = 0;
970 up_write(&key->sem);
971 goto out_put_key;
972 }
973 }
974
975
976 dead = false;
977 if (is_master_key_secret_present(&mk->mk_secret)) {
978 down_write(&mk->mk_secret_sem);
979 wipe_master_key_secret(&mk->mk_secret);
980 dead = refcount_dec_and_test(&mk->mk_refcount);
981 up_write(&mk->mk_secret_sem);
982 }
983 up_write(&key->sem);
984 if (dead) {
985
986
987
988
989 key_invalidate(key);
990 err = 0;
991 } else {
992
993 err = try_to_lock_encrypted_files(sb, mk);
994 if (err == -EBUSY) {
995 status_flags |=
996 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
997 err = 0;
998 }
999 }
1000
1001
1002
1003
1004
1005
1006out_put_key:
1007 key_put(key);
1008 if (err == 0)
1009 err = put_user(status_flags, &uarg->removal_status_flags);
1010 return err;
1011}
1012
1013int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
1014{
1015 return do_remove_key(filp, uarg, false);
1016}
1017EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1018
1019int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
1020{
1021 if (!capable(CAP_SYS_ADMIN))
1022 return -EACCES;
1023 return do_remove_key(filp, uarg, true);
1024}
1025EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
1051{
1052 struct super_block *sb = file_inode(filp)->i_sb;
1053 struct fscrypt_get_key_status_arg arg;
1054 struct key *key;
1055 struct fscrypt_master_key *mk;
1056 int err;
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 arg.status_flags = 0;
1068 arg.user_count = 0;
1069 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
1070
1071 key = fscrypt_find_master_key(sb, &arg.key_spec);
1072 if (IS_ERR(key)) {
1073 if (key != ERR_PTR(-ENOKEY))
1074 return PTR_ERR(key);
1075 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
1076 err = 0;
1077 goto out;
1078 }
1079 mk = key->payload.data[0];
1080 down_read(&key->sem);
1081
1082 if (!is_master_key_secret_present(&mk->mk_secret)) {
1083 arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
1084 err = 0;
1085 goto out_release_key;
1086 }
1087
1088 arg.status = FSCRYPT_KEY_STATUS_PRESENT;
1089 if (mk->mk_users) {
1090 struct key *mk_user;
1091
1092 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
1093 mk_user = find_master_key_user(mk);
1094 if (!IS_ERR(mk_user)) {
1095 arg.status_flags |=
1096 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
1097 key_put(mk_user);
1098 } else if (mk_user != ERR_PTR(-ENOKEY)) {
1099 err = PTR_ERR(mk_user);
1100 goto out_release_key;
1101 }
1102 }
1103 err = 0;
1104out_release_key:
1105 up_read(&key->sem);
1106 key_put(key);
1107out:
1108 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
1109 err = -EFAULT;
1110 return err;
1111}
1112EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
1113
1114int __init fscrypt_init_keyring(void)
1115{
1116 int err;
1117
1118 err = register_key_type(&key_type_fscrypt);
1119 if (err)
1120 return err;
1121
1122 err = register_key_type(&key_type_fscrypt_user);
1123 if (err)
1124 goto err_unregister_fscrypt;
1125
1126 err = register_key_type(&key_type_fscrypt_provisioning);
1127 if (err)
1128 goto err_unregister_fscrypt_user;
1129
1130 return 0;
1131
1132err_unregister_fscrypt_user:
1133 unregister_key_type(&key_type_fscrypt_user);
1134err_unregister_fscrypt:
1135 unregister_key_type(&key_type_fscrypt);
1136 return err;
1137}
1138