0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/fs_context.h>
0014 #include <linux/random.h>
0015 #include <linux/seq_file.h>
0016 #include <linux/string.h>
0017 #include <linux/mount.h>
0018 #include "fscrypt_private.h"
0019
0020
0021
0022
0023
0024
0025
0026
0027 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
0028 const union fscrypt_policy *policy2)
0029 {
0030 if (policy1->version != policy2->version)
0031 return false;
0032
0033 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
0034 }
0035
0036 int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
0037 struct fscrypt_key_specifier *key_spec)
0038 {
0039 switch (policy->version) {
0040 case FSCRYPT_POLICY_V1:
0041 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
0042 memcpy(key_spec->u.descriptor, policy->v1.master_key_descriptor,
0043 FSCRYPT_KEY_DESCRIPTOR_SIZE);
0044 return 0;
0045 case FSCRYPT_POLICY_V2:
0046 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
0047 memcpy(key_spec->u.identifier, policy->v2.master_key_identifier,
0048 FSCRYPT_KEY_IDENTIFIER_SIZE);
0049 return 0;
0050 default:
0051 WARN_ON(1);
0052 return -EINVAL;
0053 }
0054 }
0055
0056 static const union fscrypt_policy *
0057 fscrypt_get_dummy_policy(struct super_block *sb)
0058 {
0059 if (!sb->s_cop->get_dummy_policy)
0060 return NULL;
0061 return sb->s_cop->get_dummy_policy(sb);
0062 }
0063
0064 static bool fscrypt_valid_enc_modes_v1(u32 contents_mode, u32 filenames_mode)
0065 {
0066 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
0067 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
0068 return true;
0069
0070 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
0071 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
0072 return true;
0073
0074 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
0075 filenames_mode == FSCRYPT_MODE_ADIANTUM)
0076 return true;
0077
0078 return false;
0079 }
0080
0081 static bool fscrypt_valid_enc_modes_v2(u32 contents_mode, u32 filenames_mode)
0082 {
0083 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
0084 filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
0085 return true;
0086 return fscrypt_valid_enc_modes_v1(contents_mode, filenames_mode);
0087 }
0088
0089 static bool supported_direct_key_modes(const struct inode *inode,
0090 u32 contents_mode, u32 filenames_mode)
0091 {
0092 const struct fscrypt_mode *mode;
0093
0094 if (contents_mode != filenames_mode) {
0095 fscrypt_warn(inode,
0096 "Direct key flag not allowed with different contents and filenames modes");
0097 return false;
0098 }
0099 mode = &fscrypt_modes[contents_mode];
0100
0101 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
0102 fscrypt_warn(inode, "Direct key flag not allowed with %s",
0103 mode->friendly_name);
0104 return false;
0105 }
0106 return true;
0107 }
0108
0109 static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
0110 const struct inode *inode,
0111 const char *type,
0112 int max_ino_bits, int max_lblk_bits)
0113 {
0114 struct super_block *sb = inode->i_sb;
0115 int ino_bits = 64, lblk_bits = 64;
0116
0117
0118
0119
0120
0121
0122
0123
0124 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
0125 fscrypt_warn(inode,
0126 "Can't use %s policy with contents mode other than AES-256-XTS",
0127 type);
0128 return false;
0129 }
0130
0131
0132
0133
0134
0135 if (!sb->s_cop->has_stable_inodes ||
0136 !sb->s_cop->has_stable_inodes(sb)) {
0137 fscrypt_warn(inode,
0138 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
0139 type, sb->s_id);
0140 return false;
0141 }
0142 if (sb->s_cop->get_ino_and_lblk_bits)
0143 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
0144 if (ino_bits > max_ino_bits) {
0145 fscrypt_warn(inode,
0146 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
0147 type, sb->s_id);
0148 return false;
0149 }
0150 if (lblk_bits > max_lblk_bits) {
0151 fscrypt_warn(inode,
0152 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
0153 type, sb->s_id);
0154 return false;
0155 }
0156 return true;
0157 }
0158
0159 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
0160 const struct inode *inode)
0161 {
0162 if (!fscrypt_valid_enc_modes_v1(policy->contents_encryption_mode,
0163 policy->filenames_encryption_mode)) {
0164 fscrypt_warn(inode,
0165 "Unsupported encryption modes (contents %d, filenames %d)",
0166 policy->contents_encryption_mode,
0167 policy->filenames_encryption_mode);
0168 return false;
0169 }
0170
0171 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
0172 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
0173 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
0174 policy->flags);
0175 return false;
0176 }
0177
0178 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
0179 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
0180 policy->filenames_encryption_mode))
0181 return false;
0182
0183 if (IS_CASEFOLDED(inode)) {
0184
0185 fscrypt_warn(inode,
0186 "v1 policies can't be used on casefolded directories");
0187 return false;
0188 }
0189
0190 return true;
0191 }
0192
0193 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
0194 const struct inode *inode)
0195 {
0196 int count = 0;
0197
0198 if (!fscrypt_valid_enc_modes_v2(policy->contents_encryption_mode,
0199 policy->filenames_encryption_mode)) {
0200 fscrypt_warn(inode,
0201 "Unsupported encryption modes (contents %d, filenames %d)",
0202 policy->contents_encryption_mode,
0203 policy->filenames_encryption_mode);
0204 return false;
0205 }
0206
0207 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
0208 FSCRYPT_POLICY_FLAG_DIRECT_KEY |
0209 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
0210 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
0211 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
0212 policy->flags);
0213 return false;
0214 }
0215
0216 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
0217 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
0218 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
0219 if (count > 1) {
0220 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
0221 policy->flags);
0222 return false;
0223 }
0224
0225 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
0226 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
0227 policy->filenames_encryption_mode))
0228 return false;
0229
0230 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
0231 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
0232 32, 32))
0233 return false;
0234
0235
0236
0237
0238
0239
0240
0241 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
0242 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
0243 32, 32))
0244 return false;
0245
0246 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
0247 fscrypt_warn(inode, "Reserved bits set in encryption policy");
0248 return false;
0249 }
0250
0251 return true;
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
0267 const struct inode *inode)
0268 {
0269 switch (policy_u->version) {
0270 case FSCRYPT_POLICY_V1:
0271 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
0272 case FSCRYPT_POLICY_V2:
0273 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
0274 }
0275 return false;
0276 }
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289 static int fscrypt_new_context(union fscrypt_context *ctx_u,
0290 const union fscrypt_policy *policy_u,
0291 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
0292 {
0293 memset(ctx_u, 0, sizeof(*ctx_u));
0294
0295 switch (policy_u->version) {
0296 case FSCRYPT_POLICY_V1: {
0297 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
0298 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
0299
0300 ctx->version = FSCRYPT_CONTEXT_V1;
0301 ctx->contents_encryption_mode =
0302 policy->contents_encryption_mode;
0303 ctx->filenames_encryption_mode =
0304 policy->filenames_encryption_mode;
0305 ctx->flags = policy->flags;
0306 memcpy(ctx->master_key_descriptor,
0307 policy->master_key_descriptor,
0308 sizeof(ctx->master_key_descriptor));
0309 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
0310 return sizeof(*ctx);
0311 }
0312 case FSCRYPT_POLICY_V2: {
0313 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
0314 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
0315
0316 ctx->version = FSCRYPT_CONTEXT_V2;
0317 ctx->contents_encryption_mode =
0318 policy->contents_encryption_mode;
0319 ctx->filenames_encryption_mode =
0320 policy->filenames_encryption_mode;
0321 ctx->flags = policy->flags;
0322 memcpy(ctx->master_key_identifier,
0323 policy->master_key_identifier,
0324 sizeof(ctx->master_key_identifier));
0325 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
0326 return sizeof(*ctx);
0327 }
0328 }
0329 BUG();
0330 }
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
0348 const union fscrypt_context *ctx_u,
0349 int ctx_size)
0350 {
0351 memset(policy_u, 0, sizeof(*policy_u));
0352
0353 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
0354 return -EINVAL;
0355
0356 switch (ctx_u->version) {
0357 case FSCRYPT_CONTEXT_V1: {
0358 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
0359 struct fscrypt_policy_v1 *policy = &policy_u->v1;
0360
0361 policy->version = FSCRYPT_POLICY_V1;
0362 policy->contents_encryption_mode =
0363 ctx->contents_encryption_mode;
0364 policy->filenames_encryption_mode =
0365 ctx->filenames_encryption_mode;
0366 policy->flags = ctx->flags;
0367 memcpy(policy->master_key_descriptor,
0368 ctx->master_key_descriptor,
0369 sizeof(policy->master_key_descriptor));
0370 return 0;
0371 }
0372 case FSCRYPT_CONTEXT_V2: {
0373 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
0374 struct fscrypt_policy_v2 *policy = &policy_u->v2;
0375
0376 policy->version = FSCRYPT_POLICY_V2;
0377 policy->contents_encryption_mode =
0378 ctx->contents_encryption_mode;
0379 policy->filenames_encryption_mode =
0380 ctx->filenames_encryption_mode;
0381 policy->flags = ctx->flags;
0382 memcpy(policy->__reserved, ctx->__reserved,
0383 sizeof(policy->__reserved));
0384 memcpy(policy->master_key_identifier,
0385 ctx->master_key_identifier,
0386 sizeof(policy->master_key_identifier));
0387 return 0;
0388 }
0389 }
0390
0391 return -EINVAL;
0392 }
0393
0394
0395 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
0396 {
0397 const struct fscrypt_info *ci;
0398 union fscrypt_context ctx;
0399 int ret;
0400
0401 ci = fscrypt_get_info(inode);
0402 if (ci) {
0403
0404 *policy = ci->ci_policy;
0405 return 0;
0406 }
0407
0408 if (!IS_ENCRYPTED(inode))
0409 return -ENODATA;
0410
0411 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
0412 if (ret < 0)
0413 return (ret == -ERANGE) ? -EINVAL : ret;
0414
0415 return fscrypt_policy_from_context(policy, &ctx, ret);
0416 }
0417
0418 static int set_encryption_policy(struct inode *inode,
0419 const union fscrypt_policy *policy)
0420 {
0421 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
0422 union fscrypt_context ctx;
0423 int ctxsize;
0424 int err;
0425
0426 if (!fscrypt_supported_policy(policy, inode))
0427 return -EINVAL;
0428
0429 switch (policy->version) {
0430 case FSCRYPT_POLICY_V1:
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
0443 current->comm, current->pid);
0444 break;
0445 case FSCRYPT_POLICY_V2:
0446 err = fscrypt_verify_key_added(inode->i_sb,
0447 policy->v2.master_key_identifier);
0448 if (err)
0449 return err;
0450 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
0451 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",
0452 current->comm, current->pid);
0453 break;
0454 default:
0455 WARN_ON(1);
0456 return -EINVAL;
0457 }
0458
0459 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
0460 ctxsize = fscrypt_new_context(&ctx, policy, nonce);
0461
0462 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
0463 }
0464
0465 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
0466 {
0467 union fscrypt_policy policy;
0468 union fscrypt_policy existing_policy;
0469 struct inode *inode = file_inode(filp);
0470 u8 version;
0471 int size;
0472 int ret;
0473
0474 if (get_user(policy.version, (const u8 __user *)arg))
0475 return -EFAULT;
0476
0477 size = fscrypt_policy_size(&policy);
0478 if (size <= 0)
0479 return -EINVAL;
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492 version = policy.version;
0493 if (copy_from_user(&policy, arg, size))
0494 return -EFAULT;
0495 policy.version = version;
0496
0497 if (!inode_owner_or_capable(&init_user_ns, inode))
0498 return -EACCES;
0499
0500 ret = mnt_want_write_file(filp);
0501 if (ret)
0502 return ret;
0503
0504 inode_lock(inode);
0505
0506 ret = fscrypt_get_policy(inode, &existing_policy);
0507 if (ret == -ENODATA) {
0508 if (!S_ISDIR(inode->i_mode))
0509 ret = -ENOTDIR;
0510 else if (IS_DEADDIR(inode))
0511 ret = -ENOENT;
0512 else if (!inode->i_sb->s_cop->empty_dir(inode))
0513 ret = -ENOTEMPTY;
0514 else
0515 ret = set_encryption_policy(inode, &policy);
0516 } else if (ret == -EINVAL ||
0517 (ret == 0 && !fscrypt_policies_equal(&policy,
0518 &existing_policy))) {
0519
0520 ret = -EEXIST;
0521 }
0522
0523 inode_unlock(inode);
0524
0525 mnt_drop_write_file(filp);
0526 return ret;
0527 }
0528 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
0529
0530
0531 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
0532 {
0533 union fscrypt_policy policy;
0534 int err;
0535
0536 err = fscrypt_get_policy(file_inode(filp), &policy);
0537 if (err)
0538 return err;
0539
0540 if (policy.version != FSCRYPT_POLICY_V1)
0541 return -EINVAL;
0542
0543 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
0544 return -EFAULT;
0545 return 0;
0546 }
0547 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
0548
0549
0550 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
0551 {
0552 struct fscrypt_get_policy_ex_arg arg;
0553 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
0554 size_t policy_size;
0555 int err;
0556
0557
0558 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
0559 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
0560 offsetof(typeof(arg), policy));
0561 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
0562
0563 err = fscrypt_get_policy(file_inode(filp), policy);
0564 if (err)
0565 return err;
0566 policy_size = fscrypt_policy_size(policy);
0567
0568 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
0569 return -EFAULT;
0570
0571 if (policy_size > arg.policy_size)
0572 return -EOVERFLOW;
0573 arg.policy_size = policy_size;
0574
0575 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
0576 return -EFAULT;
0577 return 0;
0578 }
0579 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
0580
0581
0582 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
0583 {
0584 struct inode *inode = file_inode(filp);
0585 union fscrypt_context ctx;
0586 int ret;
0587
0588 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
0589 if (ret < 0)
0590 return ret;
0591 if (!fscrypt_context_is_valid(&ctx, ret))
0592 return -EINVAL;
0593 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
0594 FSCRYPT_FILE_NONCE_SIZE))
0595 return -EFAULT;
0596 return 0;
0597 }
0598 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
0620 {
0621 union fscrypt_policy parent_policy, child_policy;
0622 int err, err1, err2;
0623
0624
0625 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
0626 !S_ISLNK(child->i_mode))
0627 return 1;
0628
0629
0630 if (!IS_ENCRYPTED(parent))
0631 return 1;
0632
0633
0634 if (!IS_ENCRYPTED(child))
0635 return 0;
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652 err = fscrypt_get_encryption_info(parent, true);
0653 if (err)
0654 return 0;
0655 err = fscrypt_get_encryption_info(child, true);
0656 if (err)
0657 return 0;
0658
0659 err1 = fscrypt_get_policy(parent, &parent_policy);
0660 err2 = fscrypt_get_policy(child, &child_policy);
0661
0662
0663
0664
0665
0666
0667 if (err1 == -EINVAL && err2 == -EINVAL)
0668 return 1;
0669
0670 if (err1 || err2)
0671 return 0;
0672
0673 return fscrypt_policies_equal(&parent_policy, &child_policy);
0674 }
0675 EXPORT_SYMBOL(fscrypt_has_permitted_context);
0676
0677
0678
0679
0680
0681
0682 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
0683 {
0684 int err;
0685
0686 if (IS_ENCRYPTED(dir)) {
0687 err = fscrypt_require_key(dir);
0688 if (err)
0689 return ERR_PTR(err);
0690 return &dir->i_crypt_info->ci_policy;
0691 }
0692
0693 return fscrypt_get_dummy_policy(dir->i_sb);
0694 }
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode)
0708 {
0709 struct fscrypt_info *ci = inode->i_crypt_info;
0710
0711 BUILD_BUG_ON(sizeof(union fscrypt_context) !=
0712 FSCRYPT_SET_CONTEXT_MAX_SIZE);
0713
0714
0715 if (WARN_ON_ONCE(!ci))
0716 return -ENOKEY;
0717
0718 return fscrypt_new_context(ctx, &ci->ci_policy, ci->ci_nonce);
0719 }
0720 EXPORT_SYMBOL_GPL(fscrypt_context_for_new_inode);
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732 int fscrypt_set_context(struct inode *inode, void *fs_data)
0733 {
0734 struct fscrypt_info *ci = inode->i_crypt_info;
0735 union fscrypt_context ctx;
0736 int ctxsize;
0737
0738 ctxsize = fscrypt_context_for_new_inode(&ctx, inode);
0739 if (ctxsize < 0)
0740 return ctxsize;
0741
0742
0743
0744
0745
0746 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
0747 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
0748 const struct fscrypt_master_key *mk =
0749 ci->ci_master_key->payload.data[0];
0750
0751 fscrypt_hash_inode_number(ci, mk);
0752 }
0753
0754 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
0755 }
0756 EXPORT_SYMBOL_GPL(fscrypt_set_context);
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
0770 struct fscrypt_dummy_policy *dummy_policy)
0771 {
0772 const char *arg = "v2";
0773 union fscrypt_policy *policy;
0774 int err;
0775
0776 if (param->type == fs_value_is_string && *param->string)
0777 arg = param->string;
0778
0779 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
0780 if (!policy)
0781 return -ENOMEM;
0782
0783 if (!strcmp(arg, "v1")) {
0784 policy->version = FSCRYPT_POLICY_V1;
0785 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
0786 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
0787 memset(policy->v1.master_key_descriptor, 0x42,
0788 FSCRYPT_KEY_DESCRIPTOR_SIZE);
0789 } else if (!strcmp(arg, "v2")) {
0790 policy->version = FSCRYPT_POLICY_V2;
0791 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
0792 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
0793 err = fscrypt_get_test_dummy_key_identifier(
0794 policy->v2.master_key_identifier);
0795 if (err)
0796 goto out;
0797 } else {
0798 err = -EINVAL;
0799 goto out;
0800 }
0801
0802 if (dummy_policy->policy) {
0803 if (fscrypt_policies_equal(policy, dummy_policy->policy))
0804 err = 0;
0805 else
0806 err = -EEXIST;
0807 goto out;
0808 }
0809 dummy_policy->policy = policy;
0810 policy = NULL;
0811 err = 0;
0812 out:
0813 kfree(policy);
0814 return err;
0815 }
0816 EXPORT_SYMBOL_GPL(fscrypt_parse_test_dummy_encryption);
0817
0818
0819
0820
0821
0822
0823
0824
0825 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
0826 const struct fscrypt_dummy_policy *p2)
0827 {
0828 if (!p1->policy && !p2->policy)
0829 return true;
0830 if (!p1->policy || !p2->policy)
0831 return false;
0832 return fscrypt_policies_equal(p1->policy, p2->policy);
0833 }
0834 EXPORT_SYMBOL_GPL(fscrypt_dummy_policies_equal);
0835
0836
0837 int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
0838 struct fscrypt_dummy_policy *dummy_policy)
0839 {
0840 struct fs_parameter param = {
0841 .type = fs_value_is_string,
0842 .string = arg ? (char *)arg : "",
0843 };
0844 return fscrypt_parse_test_dummy_encryption(¶m, dummy_policy) ?:
0845 fscrypt_add_test_dummy_key(sb, dummy_policy);
0846 }
0847 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
0859 struct super_block *sb)
0860 {
0861 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
0862 int vers;
0863
0864 if (!policy)
0865 return;
0866
0867 vers = policy->version;
0868 if (vers == FSCRYPT_POLICY_V1)
0869 vers = 1;
0870
0871 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
0872 }
0873 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);