Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Encryption policy functions for per-file encryption support.
0004  *
0005  * Copyright (C) 2015, Google, Inc.
0006  * Copyright (C) 2015, Motorola Mobility.
0007  *
0008  * Originally written by Michael Halcrow, 2015.
0009  * Modified by Jaegeuk Kim, 2015.
0010  * Modified by Eric Biggers, 2019 for v2 policy support.
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  * fscrypt_policies_equal() - check whether two encryption policies are the same
0022  * @policy1: the first policy
0023  * @policy2: the second policy
0024  *
0025  * Return: %true if equal, else %false
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      * IV_INO_LBLK_* exist only because of hardware limitations, and
0119      * currently the only known use case for them involves AES-256-XTS.
0120      * That's also all we test currently.  For these reasons, for now only
0121      * allow AES-256-XTS here.  This can be relaxed later if a use case for
0122      * IV_INO_LBLK_* with other encryption modes arises.
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      * It's unsafe to include inode numbers in the IVs if the filesystem can
0133      * potentially renumber inodes, e.g. via filesystem shrinking.
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         /* With v1, there's no way to derive dirhash keys. */
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      * IV_INO_LBLK_32 hashes the inode number, so in principle it can
0237      * support any ino_bits.  However, currently the inode number is gotten
0238      * from inode::i_ino which is 'unsigned long'.  So for now the
0239      * implementation limit is 32 bits.
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  * fscrypt_supported_policy() - check whether an encryption policy is supported
0256  * @policy_u: the encryption policy
0257  * @inode: the inode on which the policy will be used
0258  *
0259  * Given an encryption policy, check whether all its encryption modes and other
0260  * settings are supported by this kernel on the given inode.  (But we don't
0261  * currently don't check for crypto API support here, so attempting to use an
0262  * algorithm not configured into the crypto API will still fail later.)
0263  *
0264  * Return: %true if supported, else %false
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  * fscrypt_new_context() - create a new fscrypt_context
0280  * @ctx_u: output context
0281  * @policy_u: input policy
0282  * @nonce: nonce to use
0283  *
0284  * Create an fscrypt_context for an inode that is being assigned the given
0285  * encryption policy.  @nonce must be a new random nonce.
0286  *
0287  * Return: the size of the new context in bytes.
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  * fscrypt_policy_from_context() - convert an fscrypt_context to
0334  *                 an fscrypt_policy
0335  * @policy_u: output policy
0336  * @ctx_u: input context
0337  * @ctx_size: size of input context in bytes
0338  *
0339  * Given an fscrypt_context, build the corresponding fscrypt_policy.
0340  *
0341  * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
0342  * version number or size.
0343  *
0344  * This does *not* validate the settings within the policy itself, e.g. the
0345  * modes, flags, and reserved bits.  Use fscrypt_supported_policy() for that.
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     /* unreachable */
0391     return -EINVAL;
0392 }
0393 
0394 /* Retrieve an inode's encryption policy */
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         /* key available, use the cached policy */
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          * The original encryption policy version provided no way of
0433          * verifying that the correct master key was supplied, which was
0434          * insecure in scenarios where multiple users have access to the
0435          * same encrypted files (even just read-only access).  The new
0436          * encryption policy version fixes this and also implies use of
0437          * an improved key derivation function and allows non-root users
0438          * to securely remove keys.  So as long as compatibility with
0439          * old kernels isn't required, it is recommended to use the new
0440          * policy version for all new encrypted directories.
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      * We should just copy the remaining 'size - 1' bytes here, but a
0483      * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
0484      * think that size can be 0 here (despite the check above!) *and* that
0485      * it's a compile-time constant.  Thus it would think copy_from_user()
0486      * is passed compile-time constant ULONG_MAX, causing the compile-time
0487      * buffer overflow check to fail, breaking the build. This only occurred
0488      * when building an i386 kernel with -Os and branch profiling enabled.
0489      *
0490      * Work around it by just copying the first byte again...
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         /* The file already uses a different encryption policy. */
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 /* Original ioctl version; can only get the original policy version */
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 /* Extended ioctl version; can get policies of any version */
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     /* arg is policy_size, then policy */
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 /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
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  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
0602  *                   within its directory?
0603  *
0604  * @parent: inode for parent directory
0605  * @child: inode for file being looked up, opened, or linked into @parent
0606  *
0607  * Filesystems must call this before permitting access to an inode in a
0608  * situation where the parent directory is encrypted (either before allowing
0609  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
0610  * and before any operation that involves linking an inode into an encrypted
0611  * directory, including link, rename, and cross rename.  It enforces the
0612  * constraint that within a given encrypted directory tree, all files use the
0613  * same encryption policy.  The pre-access check is needed to detect potentially
0614  * malicious offline violations of this constraint, while the link and rename
0615  * checks are needed to prevent online violations of this constraint.
0616  *
0617  * Return: 1 if permitted, 0 if forbidden.
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     /* No restrictions on file types which are never encrypted */
0625     if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
0626         !S_ISLNK(child->i_mode))
0627         return 1;
0628 
0629     /* No restrictions if the parent directory is unencrypted */
0630     if (!IS_ENCRYPTED(parent))
0631         return 1;
0632 
0633     /* Encrypted directories must not contain unencrypted files */
0634     if (!IS_ENCRYPTED(child))
0635         return 0;
0636 
0637     /*
0638      * Both parent and child are encrypted, so verify they use the same
0639      * encryption policy.  Compare the fscrypt_info structs if the keys are
0640      * available, otherwise retrieve and compare the fscrypt_contexts.
0641      *
0642      * Note that the fscrypt_context retrieval will be required frequently
0643      * when accessing an encrypted directory tree without the key.
0644      * Performance-wise this is not a big deal because we already don't
0645      * really optimize for file access without the key (to the extent that
0646      * such access is even possible), given that any attempted access
0647      * already causes a fscrypt_context retrieval and keyring search.
0648      *
0649      * In any case, if an unexpected error occurs, fall back to "forbidden".
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      * Allow the case where the parent and child both have an unrecognized
0664      * encryption policy, so that files with an unrecognized encryption
0665      * policy can be deleted.
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  * Return the encryption policy that new files in the directory will inherit, or
0679  * NULL if none, or an ERR_PTR() on error.  If the directory is encrypted, also
0680  * ensure that its key is set up, so that the new filename can be encrypted.
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  * fscrypt_context_for_new_inode() - create an encryption context for a new inode
0698  * @ctx: where context should be written
0699  * @inode: inode from which to fetch policy and nonce
0700  *
0701  * Given an in-core "prepared" (via fscrypt_prepare_new_inode) inode,
0702  * generate a new context and write it to ctx. ctx _must_ be at least
0703  * FSCRYPT_SET_CONTEXT_MAX_SIZE bytes.
0704  *
0705  * Return: size of the resulting context or a negative error code.
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     /* fscrypt_prepare_new_inode() should have set up the key already. */
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  * fscrypt_set_context() - Set the fscrypt context of a new inode
0724  * @inode: a new inode
0725  * @fs_data: private data given by FS and passed to ->set_context()
0726  *
0727  * This should be called after fscrypt_prepare_new_inode(), generally during a
0728  * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
0729  *
0730  * Return: 0 on success, -errno on failure
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      * This may be the first time the inode number is available, so do any
0744      * delayed key setup that requires the inode number.
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  * fscrypt_parse_test_dummy_encryption() - parse the test_dummy_encryption mount option
0760  * @param: the mount option
0761  * @dummy_policy: (input/output) the place to write the dummy policy that will
0762  *  result from parsing the option.  Zero-initialize this.  If a policy is
0763  *  already set here (due to test_dummy_encryption being given multiple
0764  *  times), then this function will verify that the policies are the same.
0765  *
0766  * Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the
0767  *     argument conflicts with one already specified; or -ENOMEM.
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  * fscrypt_dummy_policies_equal() - check whether two dummy policies are equal
0820  * @p1: the first test dummy policy (may be unset)
0821  * @p2: the second test dummy policy (may be unset)
0822  *
0823  * Return: %true if the dummy policies are both set and equal, or both unset.
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 /* Deprecated, do not use */
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(&param, dummy_policy) ?:
0845         fscrypt_add_test_dummy_key(sb, dummy_policy);
0846 }
0847 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
0848 
0849 /**
0850  * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
0851  * @seq: the seq_file to print the option to
0852  * @sep: the separator character to use
0853  * @sb: the filesystem whose options are being shown
0854  *
0855  * Show the test_dummy_encryption mount option, if it was specified.
0856  * This is mainly used for /proc/mounts.
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) /* Handle numbering quirk */
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);