1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/random.h>
14#include <linux/seq_file.h>
15#include <linux/string.h>
16#include <linux/mount.h>
17#include "fscrypt_private.h"
18
19
20
21
22
23
24
25
26bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
27 const union fscrypt_policy *policy2)
28{
29 if (policy1->version != policy2->version)
30 return false;
31
32 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
33}
34
35static const union fscrypt_policy *
36fscrypt_get_dummy_policy(struct super_block *sb)
37{
38 if (!sb->s_cop->get_dummy_policy)
39 return NULL;
40 return sb->s_cop->get_dummy_policy(sb);
41}
42
43static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
44{
45 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
46 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
47 return true;
48
49 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
50 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
51 return true;
52
53 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
54 filenames_mode == FSCRYPT_MODE_ADIANTUM)
55 return true;
56
57 return false;
58}
59
60static bool supported_direct_key_modes(const struct inode *inode,
61 u32 contents_mode, u32 filenames_mode)
62{
63 const struct fscrypt_mode *mode;
64
65 if (contents_mode != filenames_mode) {
66 fscrypt_warn(inode,
67 "Direct key flag not allowed with different contents and filenames modes");
68 return false;
69 }
70 mode = &fscrypt_modes[contents_mode];
71
72 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
73 fscrypt_warn(inode, "Direct key flag not allowed with %s",
74 mode->friendly_name);
75 return false;
76 }
77 return true;
78}
79
80static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
81 const struct inode *inode,
82 const char *type,
83 int max_ino_bits, int max_lblk_bits)
84{
85 struct super_block *sb = inode->i_sb;
86 int ino_bits = 64, lblk_bits = 64;
87
88
89
90
91
92
93
94
95 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
96 fscrypt_warn(inode,
97 "Can't use %s policy with contents mode other than AES-256-XTS",
98 type);
99 return false;
100 }
101
102
103
104
105
106 if (!sb->s_cop->has_stable_inodes ||
107 !sb->s_cop->has_stable_inodes(sb)) {
108 fscrypt_warn(inode,
109 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
110 type, sb->s_id);
111 return false;
112 }
113 if (sb->s_cop->get_ino_and_lblk_bits)
114 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
115 if (ino_bits > max_ino_bits) {
116 fscrypt_warn(inode,
117 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
118 type, sb->s_id);
119 return false;
120 }
121 if (lblk_bits > max_lblk_bits) {
122 fscrypt_warn(inode,
123 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
124 type, sb->s_id);
125 return false;
126 }
127 return true;
128}
129
130static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
131 const struct inode *inode)
132{
133 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
134 policy->filenames_encryption_mode)) {
135 fscrypt_warn(inode,
136 "Unsupported encryption modes (contents %d, filenames %d)",
137 policy->contents_encryption_mode,
138 policy->filenames_encryption_mode);
139 return false;
140 }
141
142 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
143 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
144 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
145 policy->flags);
146 return false;
147 }
148
149 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
150 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
151 policy->filenames_encryption_mode))
152 return false;
153
154 if (IS_CASEFOLDED(inode)) {
155
156 fscrypt_warn(inode,
157 "v1 policies can't be used on casefolded directories");
158 return false;
159 }
160
161 return true;
162}
163
164static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
165 const struct inode *inode)
166{
167 int count = 0;
168
169 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
170 policy->filenames_encryption_mode)) {
171 fscrypt_warn(inode,
172 "Unsupported encryption modes (contents %d, filenames %d)",
173 policy->contents_encryption_mode,
174 policy->filenames_encryption_mode);
175 return false;
176 }
177
178 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
179 FSCRYPT_POLICY_FLAG_DIRECT_KEY |
180 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
181 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
182 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
183 policy->flags);
184 return false;
185 }
186
187 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
188 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
189 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
190 if (count > 1) {
191 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
192 policy->flags);
193 return false;
194 }
195
196 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
197 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
198 policy->filenames_encryption_mode))
199 return false;
200
201 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
202 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
203 32, 32))
204 return false;
205
206
207
208
209
210
211
212 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
213 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
214 32, 32))
215 return false;
216
217 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
218 fscrypt_warn(inode, "Reserved bits set in encryption policy");
219 return false;
220 }
221
222 return true;
223}
224
225
226
227
228
229
230
231
232
233
234
235
236
237bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
238 const struct inode *inode)
239{
240 switch (policy_u->version) {
241 case FSCRYPT_POLICY_V1:
242 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
243 case FSCRYPT_POLICY_V2:
244 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
245 }
246 return false;
247}
248
249
250
251
252
253
254
255
256
257
258
259
260static int fscrypt_new_context(union fscrypt_context *ctx_u,
261 const union fscrypt_policy *policy_u,
262 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
263{
264 memset(ctx_u, 0, sizeof(*ctx_u));
265
266 switch (policy_u->version) {
267 case FSCRYPT_POLICY_V1: {
268 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
269 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
270
271 ctx->version = FSCRYPT_CONTEXT_V1;
272 ctx->contents_encryption_mode =
273 policy->contents_encryption_mode;
274 ctx->filenames_encryption_mode =
275 policy->filenames_encryption_mode;
276 ctx->flags = policy->flags;
277 memcpy(ctx->master_key_descriptor,
278 policy->master_key_descriptor,
279 sizeof(ctx->master_key_descriptor));
280 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
281 return sizeof(*ctx);
282 }
283 case FSCRYPT_POLICY_V2: {
284 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
285 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
286
287 ctx->version = FSCRYPT_CONTEXT_V2;
288 ctx->contents_encryption_mode =
289 policy->contents_encryption_mode;
290 ctx->filenames_encryption_mode =
291 policy->filenames_encryption_mode;
292 ctx->flags = policy->flags;
293 memcpy(ctx->master_key_identifier,
294 policy->master_key_identifier,
295 sizeof(ctx->master_key_identifier));
296 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
297 return sizeof(*ctx);
298 }
299 }
300 BUG();
301}
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
319 const union fscrypt_context *ctx_u,
320 int ctx_size)
321{
322 memset(policy_u, 0, sizeof(*policy_u));
323
324 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
325 return -EINVAL;
326
327 switch (ctx_u->version) {
328 case FSCRYPT_CONTEXT_V1: {
329 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
330 struct fscrypt_policy_v1 *policy = &policy_u->v1;
331
332 policy->version = FSCRYPT_POLICY_V1;
333 policy->contents_encryption_mode =
334 ctx->contents_encryption_mode;
335 policy->filenames_encryption_mode =
336 ctx->filenames_encryption_mode;
337 policy->flags = ctx->flags;
338 memcpy(policy->master_key_descriptor,
339 ctx->master_key_descriptor,
340 sizeof(policy->master_key_descriptor));
341 return 0;
342 }
343 case FSCRYPT_CONTEXT_V2: {
344 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
345 struct fscrypt_policy_v2 *policy = &policy_u->v2;
346
347 policy->version = FSCRYPT_POLICY_V2;
348 policy->contents_encryption_mode =
349 ctx->contents_encryption_mode;
350 policy->filenames_encryption_mode =
351 ctx->filenames_encryption_mode;
352 policy->flags = ctx->flags;
353 memcpy(policy->__reserved, ctx->__reserved,
354 sizeof(policy->__reserved));
355 memcpy(policy->master_key_identifier,
356 ctx->master_key_identifier,
357 sizeof(policy->master_key_identifier));
358 return 0;
359 }
360 }
361
362 return -EINVAL;
363}
364
365
366static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
367{
368 const struct fscrypt_info *ci;
369 union fscrypt_context ctx;
370 int ret;
371
372 ci = fscrypt_get_info(inode);
373 if (ci) {
374
375 *policy = ci->ci_policy;
376 return 0;
377 }
378
379 if (!IS_ENCRYPTED(inode))
380 return -ENODATA;
381
382 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
383 if (ret < 0)
384 return (ret == -ERANGE) ? -EINVAL : ret;
385
386 return fscrypt_policy_from_context(policy, &ctx, ret);
387}
388
389static int set_encryption_policy(struct inode *inode,
390 const union fscrypt_policy *policy)
391{
392 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
393 union fscrypt_context ctx;
394 int ctxsize;
395 int err;
396
397 if (!fscrypt_supported_policy(policy, inode))
398 return -EINVAL;
399
400 switch (policy->version) {
401 case FSCRYPT_POLICY_V1:
402
403
404
405
406
407
408
409
410
411
412
413 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
414 current->comm, current->pid);
415 break;
416 case FSCRYPT_POLICY_V2:
417 err = fscrypt_verify_key_added(inode->i_sb,
418 policy->v2.master_key_identifier);
419 if (err)
420 return err;
421 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
422 pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n",
423 current->comm, current->pid);
424 break;
425 default:
426 WARN_ON(1);
427 return -EINVAL;
428 }
429
430 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
431 ctxsize = fscrypt_new_context(&ctx, policy, nonce);
432
433 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
434}
435
436int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
437{
438 union fscrypt_policy policy;
439 union fscrypt_policy existing_policy;
440 struct inode *inode = file_inode(filp);
441 u8 version;
442 int size;
443 int ret;
444
445 if (get_user(policy.version, (const u8 __user *)arg))
446 return -EFAULT;
447
448 size = fscrypt_policy_size(&policy);
449 if (size <= 0)
450 return -EINVAL;
451
452
453
454
455
456
457
458
459
460
461
462
463 version = policy.version;
464 if (copy_from_user(&policy, arg, size))
465 return -EFAULT;
466 policy.version = version;
467
468 if (!inode_owner_or_capable(&init_user_ns, inode))
469 return -EACCES;
470
471 ret = mnt_want_write_file(filp);
472 if (ret)
473 return ret;
474
475 inode_lock(inode);
476
477 ret = fscrypt_get_policy(inode, &existing_policy);
478 if (ret == -ENODATA) {
479 if (!S_ISDIR(inode->i_mode))
480 ret = -ENOTDIR;
481 else if (IS_DEADDIR(inode))
482 ret = -ENOENT;
483 else if (!inode->i_sb->s_cop->empty_dir(inode))
484 ret = -ENOTEMPTY;
485 else
486 ret = set_encryption_policy(inode, &policy);
487 } else if (ret == -EINVAL ||
488 (ret == 0 && !fscrypt_policies_equal(&policy,
489 &existing_policy))) {
490
491 ret = -EEXIST;
492 }
493
494 inode_unlock(inode);
495
496 mnt_drop_write_file(filp);
497 return ret;
498}
499EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
500
501
502int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
503{
504 union fscrypt_policy policy;
505 int err;
506
507 err = fscrypt_get_policy(file_inode(filp), &policy);
508 if (err)
509 return err;
510
511 if (policy.version != FSCRYPT_POLICY_V1)
512 return -EINVAL;
513
514 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
515 return -EFAULT;
516 return 0;
517}
518EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
519
520
521int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
522{
523 struct fscrypt_get_policy_ex_arg arg;
524 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
525 size_t policy_size;
526 int err;
527
528
529 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
530 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
531 offsetof(typeof(arg), policy));
532 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
533
534 err = fscrypt_get_policy(file_inode(filp), policy);
535 if (err)
536 return err;
537 policy_size = fscrypt_policy_size(policy);
538
539 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
540 return -EFAULT;
541
542 if (policy_size > arg.policy_size)
543 return -EOVERFLOW;
544 arg.policy_size = policy_size;
545
546 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
547 return -EFAULT;
548 return 0;
549}
550EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
551
552
553int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
554{
555 struct inode *inode = file_inode(filp);
556 union fscrypt_context ctx;
557 int ret;
558
559 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
560 if (ret < 0)
561 return ret;
562 if (!fscrypt_context_is_valid(&ctx, ret))
563 return -EINVAL;
564 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
565 FSCRYPT_FILE_NONCE_SIZE))
566 return -EFAULT;
567 return 0;
568}
569EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
591{
592 union fscrypt_policy parent_policy, child_policy;
593 int err, err1, err2;
594
595
596 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
597 !S_ISLNK(child->i_mode))
598 return 1;
599
600
601 if (!IS_ENCRYPTED(parent))
602 return 1;
603
604
605 if (!IS_ENCRYPTED(child))
606 return 0;
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623 err = fscrypt_get_encryption_info(parent, true);
624 if (err)
625 return 0;
626 err = fscrypt_get_encryption_info(child, true);
627 if (err)
628 return 0;
629
630 err1 = fscrypt_get_policy(parent, &parent_policy);
631 err2 = fscrypt_get_policy(child, &child_policy);
632
633
634
635
636
637
638 if (err1 == -EINVAL && err2 == -EINVAL)
639 return 1;
640
641 if (err1 || err2)
642 return 0;
643
644 return fscrypt_policies_equal(&parent_policy, &child_policy);
645}
646EXPORT_SYMBOL(fscrypt_has_permitted_context);
647
648
649
650
651
652
653const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
654{
655 int err;
656
657 if (IS_ENCRYPTED(dir)) {
658 err = fscrypt_require_key(dir);
659 if (err)
660 return ERR_PTR(err);
661 return &dir->i_crypt_info->ci_policy;
662 }
663
664 return fscrypt_get_dummy_policy(dir->i_sb);
665}
666
667
668
669
670
671
672
673
674
675
676
677int fscrypt_set_context(struct inode *inode, void *fs_data)
678{
679 struct fscrypt_info *ci = inode->i_crypt_info;
680 union fscrypt_context ctx;
681 int ctxsize;
682
683
684 if (WARN_ON_ONCE(!ci))
685 return -ENOKEY;
686
687 BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
688 ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
689
690
691
692
693
694 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
695 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
696 const struct fscrypt_master_key *mk =
697 ci->ci_master_key->payload.data[0];
698
699 fscrypt_hash_inode_number(ci, mk);
700 }
701
702 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
703}
704EXPORT_SYMBOL_GPL(fscrypt_set_context);
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
724 struct fscrypt_dummy_policy *dummy_policy)
725{
726 struct fscrypt_key_specifier key_spec = { 0 };
727 int version;
728 union fscrypt_policy *policy = NULL;
729 int err;
730
731 if (!arg)
732 arg = "v2";
733
734 if (!strcmp(arg, "v1")) {
735 version = FSCRYPT_POLICY_V1;
736 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
737 memset(key_spec.u.descriptor, 0x42,
738 FSCRYPT_KEY_DESCRIPTOR_SIZE);
739 } else if (!strcmp(arg, "v2")) {
740 version = FSCRYPT_POLICY_V2;
741 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
742
743 } else {
744 err = -EINVAL;
745 goto out;
746 }
747
748 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
749 if (!policy) {
750 err = -ENOMEM;
751 goto out;
752 }
753
754 err = fscrypt_add_test_dummy_key(sb, &key_spec);
755 if (err)
756 goto out;
757
758 policy->version = version;
759 switch (policy->version) {
760 case FSCRYPT_POLICY_V1:
761 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
762 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
763 memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
764 FSCRYPT_KEY_DESCRIPTOR_SIZE);
765 break;
766 case FSCRYPT_POLICY_V2:
767 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
768 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
769 memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
770 FSCRYPT_KEY_IDENTIFIER_SIZE);
771 break;
772 default:
773 WARN_ON(1);
774 err = -EINVAL;
775 goto out;
776 }
777
778 if (dummy_policy->policy) {
779 if (fscrypt_policies_equal(policy, dummy_policy->policy))
780 err = 0;
781 else
782 err = -EEXIST;
783 goto out;
784 }
785 dummy_policy->policy = policy;
786 policy = NULL;
787 err = 0;
788out:
789 kfree(policy);
790 return err;
791}
792EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
793
794
795
796
797
798
799
800
801
802
803void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
804 struct super_block *sb)
805{
806 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
807 int vers;
808
809 if (!policy)
810 return;
811
812 vers = policy->version;
813 if (vers == FSCRYPT_POLICY_V1)
814 vers = 1;
815
816 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
817}
818EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);
819