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