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