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
351 refcount_set(&mk->mk_refcount, 1);
352 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
353 spin_lock_init(&mk->mk_decrypted_inodes_lock);
354
355 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
356 err = allocate_master_key_users_keyring(mk);
357 if (err)
358 goto out_free_mk;
359 err = add_master_key_user(mk);
360 if (err)
361 goto out_free_mk;
362 }
363
364
365
366
367
368
369 format_mk_description(description, mk_spec);
370 key = key_alloc(&key_type_fscrypt, description,
371 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
372 KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
373 KEY_ALLOC_NOT_IN_QUOTA, NULL);
374 if (IS_ERR(key)) {
375 err = PTR_ERR(key);
376 goto out_free_mk;
377 }
378 err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
379 key_put(key);
380 if (err)
381 goto out_free_mk;
382
383 return 0;
384
385out_free_mk:
386 free_master_key(mk);
387 return err;
388}
389
390#define KEY_DEAD 1
391
392static int add_existing_master_key(struct fscrypt_master_key *mk,
393 struct fscrypt_master_key_secret *secret)
394{
395 struct key *mk_user;
396 bool rekey;
397 int err;
398
399
400
401
402
403 if (mk->mk_users) {
404 mk_user = find_master_key_user(mk);
405 if (mk_user != ERR_PTR(-ENOKEY)) {
406 if (IS_ERR(mk_user))
407 return PTR_ERR(mk_user);
408 key_put(mk_user);
409 return 0;
410 }
411 }
412
413
414 rekey = !is_master_key_secret_present(&mk->mk_secret);
415 if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
416 return KEY_DEAD;
417
418
419 if (mk->mk_users) {
420 err = add_master_key_user(mk);
421 if (err) {
422 if (rekey && refcount_dec_and_test(&mk->mk_refcount))
423 return KEY_DEAD;
424 return err;
425 }
426 }
427
428
429 if (rekey)
430 move_master_key_secret(&mk->mk_secret, secret);
431 return 0;
432}
433
434static int do_add_master_key(struct super_block *sb,
435 struct fscrypt_master_key_secret *secret,
436 const struct fscrypt_key_specifier *mk_spec)
437{
438 static DEFINE_MUTEX(fscrypt_add_key_mutex);
439 struct key *key;
440 int err;
441
442 mutex_lock(&fscrypt_add_key_mutex);
443retry:
444 key = fscrypt_find_master_key(sb, mk_spec);
445 if (IS_ERR(key)) {
446 err = PTR_ERR(key);
447 if (err != -ENOKEY)
448 goto out_unlock;
449
450 err = allocate_filesystem_keyring(sb);
451 if (err)
452 goto out_unlock;
453 err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
454 } else {
455
456
457
458
459 down_write(&key->sem);
460 err = add_existing_master_key(key->payload.data[0], secret);
461 up_write(&key->sem);
462 if (err == KEY_DEAD) {
463
464 key_invalidate(key);
465 key_put(key);
466 goto retry;
467 }
468 key_put(key);
469 }
470out_unlock:
471 mutex_unlock(&fscrypt_add_key_mutex);
472 return err;
473}
474
475static int add_master_key(struct super_block *sb,
476 struct fscrypt_master_key_secret *secret,
477 struct fscrypt_key_specifier *key_spec)
478{
479 int err;
480
481 if (key_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
482 err = fscrypt_init_hkdf(&secret->hkdf, secret->raw,
483 secret->size);
484 if (err)
485 return err;
486
487
488
489
490
491 memzero_explicit(secret->raw, secret->size);
492
493
494 err = fscrypt_hkdf_expand(&secret->hkdf,
495 HKDF_CONTEXT_KEY_IDENTIFIER, NULL, 0,
496 key_spec->u.identifier,
497 FSCRYPT_KEY_IDENTIFIER_SIZE);
498 if (err)
499 return err;
500 }
501 return do_add_master_key(sb, secret, key_spec);
502}
503
504static int fscrypt_provisioning_key_preparse(struct key_preparsed_payload *prep)
505{
506 const struct fscrypt_provisioning_key_payload *payload = prep->data;
507
508 if (prep->datalen < sizeof(*payload) + FSCRYPT_MIN_KEY_SIZE ||
509 prep->datalen > sizeof(*payload) + FSCRYPT_MAX_KEY_SIZE)
510 return -EINVAL;
511
512 if (payload->type != FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
513 payload->type != FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER)
514 return -EINVAL;
515
516 if (payload->__reserved)
517 return -EINVAL;
518
519 prep->payload.data[0] = kmemdup(payload, prep->datalen, GFP_KERNEL);
520 if (!prep->payload.data[0])
521 return -ENOMEM;
522
523 prep->quotalen = prep->datalen;
524 return 0;
525}
526
527static void fscrypt_provisioning_key_free_preparse(
528 struct key_preparsed_payload *prep)
529{
530 kfree_sensitive(prep->payload.data[0]);
531}
532
533static void fscrypt_provisioning_key_describe(const struct key *key,
534 struct seq_file *m)
535{
536 seq_puts(m, key->description);
537 if (key_is_positive(key)) {
538 const struct fscrypt_provisioning_key_payload *payload =
539 key->payload.data[0];
540
541 seq_printf(m, ": %u [%u]", key->datalen, payload->type);
542 }
543}
544
545static void fscrypt_provisioning_key_destroy(struct key *key)
546{
547 kfree_sensitive(key->payload.data[0]);
548}
549
550static struct key_type key_type_fscrypt_provisioning = {
551 .name = "fscrypt-provisioning",
552 .preparse = fscrypt_provisioning_key_preparse,
553 .free_preparse = fscrypt_provisioning_key_free_preparse,
554 .instantiate = generic_key_instantiate,
555 .describe = fscrypt_provisioning_key_describe,
556 .destroy = fscrypt_provisioning_key_destroy,
557};
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574static int get_keyring_key(u32 key_id, u32 type,
575 struct fscrypt_master_key_secret *secret)
576{
577 key_ref_t ref;
578 struct key *key;
579 const struct fscrypt_provisioning_key_payload *payload;
580 int err;
581
582 ref = lookup_user_key(key_id, 0, KEY_NEED_SEARCH);
583 if (IS_ERR(ref))
584 return PTR_ERR(ref);
585 key = key_ref_to_ptr(ref);
586
587 if (key->type != &key_type_fscrypt_provisioning)
588 goto bad_key;
589 payload = key->payload.data[0];
590
591
592 if (payload->type != type)
593 goto bad_key;
594
595 secret->size = key->datalen - sizeof(*payload);
596 memcpy(secret->raw, payload->raw, secret->size);
597 err = 0;
598 goto out_put;
599
600bad_key:
601 err = -EKEYREJECTED;
602out_put:
603 key_ref_put(ref);
604 return err;
605}
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg)
632{
633 struct super_block *sb = file_inode(filp)->i_sb;
634 struct fscrypt_add_key_arg __user *uarg = _uarg;
635 struct fscrypt_add_key_arg arg;
636 struct fscrypt_master_key_secret secret;
637 int err;
638
639 if (copy_from_user(&arg, uarg, sizeof(arg)))
640 return -EFAULT;
641
642 if (!valid_key_spec(&arg.key_spec))
643 return -EINVAL;
644
645 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
646 return -EINVAL;
647
648
649
650
651
652
653 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
654 !capable(CAP_SYS_ADMIN))
655 return -EACCES;
656
657 memset(&secret, 0, sizeof(secret));
658 if (arg.key_id) {
659 if (arg.raw_size != 0)
660 return -EINVAL;
661 err = get_keyring_key(arg.key_id, arg.key_spec.type, &secret);
662 if (err)
663 goto out_wipe_secret;
664 } else {
665 if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE ||
666 arg.raw_size > FSCRYPT_MAX_KEY_SIZE)
667 return -EINVAL;
668 secret.size = arg.raw_size;
669 err = -EFAULT;
670 if (copy_from_user(secret.raw, uarg->raw, secret.size))
671 goto out_wipe_secret;
672 }
673
674 err = add_master_key(sb, &secret, &arg.key_spec);
675 if (err)
676 goto out_wipe_secret;
677
678
679 err = -EFAULT;
680 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
681 copy_to_user(uarg->key_spec.u.identifier, arg.key_spec.u.identifier,
682 FSCRYPT_KEY_IDENTIFIER_SIZE))
683 goto out_wipe_secret;
684 err = 0;
685out_wipe_secret:
686 wipe_master_key_secret(&secret);
687 return err;
688}
689EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key);
690
691
692
693
694
695
696int fscrypt_add_test_dummy_key(struct super_block *sb,
697 struct fscrypt_key_specifier *key_spec)
698{
699 static u8 test_key[FSCRYPT_MAX_KEY_SIZE];
700 struct fscrypt_master_key_secret secret;
701 int err;
702
703 get_random_once(test_key, FSCRYPT_MAX_KEY_SIZE);
704
705 memset(&secret, 0, sizeof(secret));
706 secret.size = FSCRYPT_MAX_KEY_SIZE;
707 memcpy(secret.raw, test_key, FSCRYPT_MAX_KEY_SIZE);
708
709 err = add_master_key(sb, &secret, key_spec);
710 wipe_master_key_secret(&secret);
711 return err;
712}
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730int fscrypt_verify_key_added(struct super_block *sb,
731 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
732{
733 struct fscrypt_key_specifier mk_spec;
734 struct key *key, *mk_user;
735 struct fscrypt_master_key *mk;
736 int err;
737
738 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
739 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
740
741 key = fscrypt_find_master_key(sb, &mk_spec);
742 if (IS_ERR(key)) {
743 err = PTR_ERR(key);
744 goto out;
745 }
746 mk = key->payload.data[0];
747 mk_user = find_master_key_user(mk);
748 if (IS_ERR(mk_user)) {
749 err = PTR_ERR(mk_user);
750 } else {
751 key_put(mk_user);
752 err = 0;
753 }
754 key_put(key);
755out:
756 if (err == -ENOKEY && capable(CAP_FOWNER))
757 err = 0;
758 return err;
759}
760
761
762
763
764
765
766static void shrink_dcache_inode(struct inode *inode)
767{
768 struct dentry *dentry;
769
770 if (S_ISDIR(inode->i_mode)) {
771 dentry = d_find_any_alias(inode);
772 if (dentry) {
773 shrink_dcache_parent(dentry);
774 dput(dentry);
775 }
776 }
777 d_prune_aliases(inode);
778}
779
780static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk)
781{
782 struct fscrypt_info *ci;
783 struct inode *inode;
784 struct inode *toput_inode = NULL;
785
786 spin_lock(&mk->mk_decrypted_inodes_lock);
787
788 list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) {
789 inode = ci->ci_inode;
790 spin_lock(&inode->i_lock);
791 if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) {
792 spin_unlock(&inode->i_lock);
793 continue;
794 }
795 __iget(inode);
796 spin_unlock(&inode->i_lock);
797 spin_unlock(&mk->mk_decrypted_inodes_lock);
798
799 shrink_dcache_inode(inode);
800 iput(toput_inode);
801 toput_inode = inode;
802
803 spin_lock(&mk->mk_decrypted_inodes_lock);
804 }
805
806 spin_unlock(&mk->mk_decrypted_inodes_lock);
807 iput(toput_inode);
808}
809
810static int check_for_busy_inodes(struct super_block *sb,
811 struct fscrypt_master_key *mk)
812{
813 struct list_head *pos;
814 size_t busy_count = 0;
815 unsigned long ino;
816 char ino_str[50] = "";
817
818 spin_lock(&mk->mk_decrypted_inodes_lock);
819
820 list_for_each(pos, &mk->mk_decrypted_inodes)
821 busy_count++;
822
823 if (busy_count == 0) {
824 spin_unlock(&mk->mk_decrypted_inodes_lock);
825 return 0;
826 }
827
828 {
829
830 struct inode *inode =
831 list_first_entry(&mk->mk_decrypted_inodes,
832 struct fscrypt_info,
833 ci_master_key_link)->ci_inode;
834 ino = inode->i_ino;
835 }
836 spin_unlock(&mk->mk_decrypted_inodes_lock);
837
838
839 if (ino)
840 snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
841
842 fscrypt_warn(NULL,
843 "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
844 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
845 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
846 ino_str);
847 return -EBUSY;
848}
849
850static int try_to_lock_encrypted_files(struct super_block *sb,
851 struct fscrypt_master_key *mk)
852{
853 int err1;
854 int err2;
855
856
857
858
859
860
861
862
863
864
865 down_read(&sb->s_umount);
866 err1 = sync_filesystem(sb);
867 up_read(&sb->s_umount);
868
869
870
871
872
873
874
875
876 evict_dentries_for_decrypted_inodes(mk);
877
878
879
880
881
882
883
884
885 err2 = check_for_busy_inodes(sb, mk);
886
887 return err1 ?: err2;
888}
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911static int do_remove_key(struct file *filp, void __user *_uarg, bool all_users)
912{
913 struct super_block *sb = file_inode(filp)->i_sb;
914 struct fscrypt_remove_key_arg __user *uarg = _uarg;
915 struct fscrypt_remove_key_arg arg;
916 struct key *key;
917 struct fscrypt_master_key *mk;
918 u32 status_flags = 0;
919 int err;
920 bool dead;
921
922 if (copy_from_user(&arg, uarg, sizeof(arg)))
923 return -EFAULT;
924
925 if (!valid_key_spec(&arg.key_spec))
926 return -EINVAL;
927
928 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
929 return -EINVAL;
930
931
932
933
934
935 if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
936 !capable(CAP_SYS_ADMIN))
937 return -EACCES;
938
939
940 key = fscrypt_find_master_key(sb, &arg.key_spec);
941 if (IS_ERR(key))
942 return PTR_ERR(key);
943 mk = key->payload.data[0];
944
945 down_write(&key->sem);
946
947
948 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
949 if (all_users)
950 err = keyring_clear(mk->mk_users);
951 else
952 err = remove_master_key_user(mk);
953 if (err) {
954 up_write(&key->sem);
955 goto out_put_key;
956 }
957 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
958
959
960
961
962
963 status_flags |=
964 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
965 err = 0;
966 up_write(&key->sem);
967 goto out_put_key;
968 }
969 }
970
971
972 dead = false;
973 if (is_master_key_secret_present(&mk->mk_secret)) {
974 wipe_master_key_secret(&mk->mk_secret);
975 dead = refcount_dec_and_test(&mk->mk_refcount);
976 }
977 up_write(&key->sem);
978 if (dead) {
979
980
981
982
983 key_invalidate(key);
984 err = 0;
985 } else {
986
987 err = try_to_lock_encrypted_files(sb, mk);
988 if (err == -EBUSY) {
989 status_flags |=
990 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY;
991 err = 0;
992 }
993 }
994
995
996
997
998
999
1000out_put_key:
1001 key_put(key);
1002 if (err == 0)
1003 err = put_user(status_flags, &uarg->removal_status_flags);
1004 return err;
1005}
1006
1007int fscrypt_ioctl_remove_key(struct file *filp, void __user *uarg)
1008{
1009 return do_remove_key(filp, uarg, false);
1010}
1011EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key);
1012
1013int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *uarg)
1014{
1015 if (!capable(CAP_SYS_ADMIN))
1016 return -EACCES;
1017 return do_remove_key(filp, uarg, true);
1018}
1019EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key_all_users);
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg)
1045{
1046 struct super_block *sb = file_inode(filp)->i_sb;
1047 struct fscrypt_get_key_status_arg arg;
1048 struct key *key;
1049 struct fscrypt_master_key *mk;
1050 int err;
1051
1052 if (copy_from_user(&arg, uarg, sizeof(arg)))
1053 return -EFAULT;
1054
1055 if (!valid_key_spec(&arg.key_spec))
1056 return -EINVAL;
1057
1058 if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved)))
1059 return -EINVAL;
1060
1061 arg.status_flags = 0;
1062 arg.user_count = 0;
1063 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
1064
1065 key = fscrypt_find_master_key(sb, &arg.key_spec);
1066 if (IS_ERR(key)) {
1067 if (key != ERR_PTR(-ENOKEY))
1068 return PTR_ERR(key);
1069 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
1070 err = 0;
1071 goto out;
1072 }
1073 mk = key->payload.data[0];
1074 down_read(&key->sem);
1075
1076 if (!is_master_key_secret_present(&mk->mk_secret)) {
1077 arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
1078 err = 0;
1079 goto out_release_key;
1080 }
1081
1082 arg.status = FSCRYPT_KEY_STATUS_PRESENT;
1083 if (mk->mk_users) {
1084 struct key *mk_user;
1085
1086 arg.user_count = mk->mk_users->keys.nr_leaves_on_tree;
1087 mk_user = find_master_key_user(mk);
1088 if (!IS_ERR(mk_user)) {
1089 arg.status_flags |=
1090 FSCRYPT_KEY_STATUS_FLAG_ADDED_BY_SELF;
1091 key_put(mk_user);
1092 } else if (mk_user != ERR_PTR(-ENOKEY)) {
1093 err = PTR_ERR(mk_user);
1094 goto out_release_key;
1095 }
1096 }
1097 err = 0;
1098out_release_key:
1099 up_read(&key->sem);
1100 key_put(key);
1101out:
1102 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
1103 err = -EFAULT;
1104 return err;
1105}
1106EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status);
1107
1108int __init fscrypt_init_keyring(void)
1109{
1110 int err;
1111
1112 err = register_key_type(&key_type_fscrypt);
1113 if (err)
1114 return err;
1115
1116 err = register_key_type(&key_type_fscrypt_user);
1117 if (err)
1118 goto err_unregister_fscrypt;
1119
1120 err = register_key_type(&key_type_fscrypt_provisioning);
1121 if (err)
1122 goto err_unregister_fscrypt_user;
1123
1124 return 0;
1125
1126err_unregister_fscrypt_user:
1127 unregister_key_type(&key_type_fscrypt_user);
1128err_unregister_fscrypt:
1129 unregister_key_type(&key_type_fscrypt);
1130 return err;
1131}
1132