1 // SPDX-License-Identifier: GPL-2.0
3 * Encryption policy functions for per-file encryption support.
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility.
8 * Originally written by Michael Halcrow, 2015.
9 * Modified by Jaegeuk Kim, 2015.
10 * Modified by Eric Biggers, 2019 for v2 policy support.
13 #include <linux/fs_context.h>
14 #include <linux/random.h>
15 #include <linux/seq_file.h>
16 #include <linux/string.h>
17 #include <linux/mount.h>
18 #include "fscrypt_private.h"
21 * fscrypt_policies_equal() - check whether two encryption policies are the same
22 * @policy1: the first policy
23 * @policy2: the second policy
25 * Return: %true if equal, else %false
27 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
28 const union fscrypt_policy *policy2)
30 if (policy1->version != policy2->version)
33 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
36 int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
37 struct fscrypt_key_specifier *key_spec)
39 switch (policy->version) {
40 case FSCRYPT_POLICY_V1:
41 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
42 memcpy(key_spec->u.descriptor, policy->v1.master_key_descriptor,
43 FSCRYPT_KEY_DESCRIPTOR_SIZE);
45 case FSCRYPT_POLICY_V2:
46 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
47 memcpy(key_spec->u.identifier, policy->v2.master_key_identifier,
48 FSCRYPT_KEY_IDENTIFIER_SIZE);
56 static const union fscrypt_policy *
57 fscrypt_get_dummy_policy(struct super_block *sb)
59 if (!sb->s_cop->get_dummy_policy)
61 return sb->s_cop->get_dummy_policy(sb);
64 static bool fscrypt_valid_enc_modes_v1(u32 contents_mode, u32 filenames_mode)
66 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
67 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
70 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
71 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
74 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
75 filenames_mode == FSCRYPT_MODE_ADIANTUM)
81 static bool fscrypt_valid_enc_modes_v2(u32 contents_mode, u32 filenames_mode)
83 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
84 filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
86 return fscrypt_valid_enc_modes_v1(contents_mode, filenames_mode);
89 static bool supported_direct_key_modes(const struct inode *inode,
90 u32 contents_mode, u32 filenames_mode)
92 const struct fscrypt_mode *mode;
94 if (contents_mode != filenames_mode) {
96 "Direct key flag not allowed with different contents and filenames modes");
99 mode = &fscrypt_modes[contents_mode];
101 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
102 fscrypt_warn(inode, "Direct key flag not allowed with %s",
103 mode->friendly_name);
109 static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
110 const struct inode *inode,
112 int max_ino_bits, int max_lblk_bits)
114 struct super_block *sb = inode->i_sb;
115 int ino_bits = 64, lblk_bits = 64;
118 * IV_INO_LBLK_* exist only because of hardware limitations, and
119 * currently the only known use case for them involves AES-256-XTS.
120 * That's also all we test currently. For these reasons, for now only
121 * allow AES-256-XTS here. This can be relaxed later if a use case for
122 * IV_INO_LBLK_* with other encryption modes arises.
124 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
126 "Can't use %s policy with contents mode other than AES-256-XTS",
132 * It's unsafe to include inode numbers in the IVs if the filesystem can
133 * potentially renumber inodes, e.g. via filesystem shrinking.
135 if (!sb->s_cop->has_stable_inodes ||
136 !sb->s_cop->has_stable_inodes(sb)) {
138 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
142 if (sb->s_cop->get_ino_and_lblk_bits)
143 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
144 if (ino_bits > max_ino_bits) {
146 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
150 if (lblk_bits > max_lblk_bits) {
152 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
159 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
160 const struct inode *inode)
162 if (!fscrypt_valid_enc_modes_v1(policy->contents_encryption_mode,
163 policy->filenames_encryption_mode)) {
165 "Unsupported encryption modes (contents %d, filenames %d)",
166 policy->contents_encryption_mode,
167 policy->filenames_encryption_mode);
171 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
172 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
173 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
178 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
179 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
180 policy->filenames_encryption_mode))
183 if (IS_CASEFOLDED(inode)) {
184 /* With v1, there's no way to derive dirhash keys. */
186 "v1 policies can't be used on casefolded directories");
193 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
194 const struct inode *inode)
198 if (!fscrypt_valid_enc_modes_v2(policy->contents_encryption_mode,
199 policy->filenames_encryption_mode)) {
201 "Unsupported encryption modes (contents %d, filenames %d)",
202 policy->contents_encryption_mode,
203 policy->filenames_encryption_mode);
207 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
208 FSCRYPT_POLICY_FLAG_DIRECT_KEY |
209 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
210 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
211 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
216 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
217 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
218 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
220 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
225 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
226 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
227 policy->filenames_encryption_mode))
230 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
231 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
236 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
237 * support any ino_bits. However, currently the inode number is gotten
238 * from inode::i_ino which is 'unsigned long'. So for now the
239 * implementation limit is 32 bits.
241 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
242 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
246 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
247 fscrypt_warn(inode, "Reserved bits set in encryption policy");
255 * fscrypt_supported_policy() - check whether an encryption policy is supported
256 * @policy_u: the encryption policy
257 * @inode: the inode on which the policy will be used
259 * Given an encryption policy, check whether all its encryption modes and other
260 * settings are supported by this kernel on the given inode. (But we don't
261 * currently don't check for crypto API support here, so attempting to use an
262 * algorithm not configured into the crypto API will still fail later.)
264 * Return: %true if supported, else %false
266 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
267 const struct inode *inode)
269 switch (policy_u->version) {
270 case FSCRYPT_POLICY_V1:
271 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
272 case FSCRYPT_POLICY_V2:
273 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
279 * fscrypt_new_context() - create a new fscrypt_context
280 * @ctx_u: output context
281 * @policy_u: input policy
282 * @nonce: nonce to use
284 * Create an fscrypt_context for an inode that is being assigned the given
285 * encryption policy. @nonce must be a new random nonce.
287 * Return: the size of the new context in bytes.
289 static int fscrypt_new_context(union fscrypt_context *ctx_u,
290 const union fscrypt_policy *policy_u,
291 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
293 memset(ctx_u, 0, sizeof(*ctx_u));
295 switch (policy_u->version) {
296 case FSCRYPT_POLICY_V1: {
297 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
298 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
300 ctx->version = FSCRYPT_CONTEXT_V1;
301 ctx->contents_encryption_mode =
302 policy->contents_encryption_mode;
303 ctx->filenames_encryption_mode =
304 policy->filenames_encryption_mode;
305 ctx->flags = policy->flags;
306 memcpy(ctx->master_key_descriptor,
307 policy->master_key_descriptor,
308 sizeof(ctx->master_key_descriptor));
309 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
312 case FSCRYPT_POLICY_V2: {
313 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
314 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
316 ctx->version = FSCRYPT_CONTEXT_V2;
317 ctx->contents_encryption_mode =
318 policy->contents_encryption_mode;
319 ctx->filenames_encryption_mode =
320 policy->filenames_encryption_mode;
321 ctx->flags = policy->flags;
322 memcpy(ctx->master_key_identifier,
323 policy->master_key_identifier,
324 sizeof(ctx->master_key_identifier));
325 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
333 * fscrypt_policy_from_context() - convert an fscrypt_context to
335 * @policy_u: output policy
336 * @ctx_u: input context
337 * @ctx_size: size of input context in bytes
339 * Given an fscrypt_context, build the corresponding fscrypt_policy.
341 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
342 * version number or size.
344 * This does *not* validate the settings within the policy itself, e.g. the
345 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
347 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
348 const union fscrypt_context *ctx_u,
351 memset(policy_u, 0, sizeof(*policy_u));
353 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
356 switch (ctx_u->version) {
357 case FSCRYPT_CONTEXT_V1: {
358 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
359 struct fscrypt_policy_v1 *policy = &policy_u->v1;
361 policy->version = FSCRYPT_POLICY_V1;
362 policy->contents_encryption_mode =
363 ctx->contents_encryption_mode;
364 policy->filenames_encryption_mode =
365 ctx->filenames_encryption_mode;
366 policy->flags = ctx->flags;
367 memcpy(policy->master_key_descriptor,
368 ctx->master_key_descriptor,
369 sizeof(policy->master_key_descriptor));
372 case FSCRYPT_CONTEXT_V2: {
373 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
374 struct fscrypt_policy_v2 *policy = &policy_u->v2;
376 policy->version = FSCRYPT_POLICY_V2;
377 policy->contents_encryption_mode =
378 ctx->contents_encryption_mode;
379 policy->filenames_encryption_mode =
380 ctx->filenames_encryption_mode;
381 policy->flags = ctx->flags;
382 memcpy(policy->__reserved, ctx->__reserved,
383 sizeof(policy->__reserved));
384 memcpy(policy->master_key_identifier,
385 ctx->master_key_identifier,
386 sizeof(policy->master_key_identifier));
394 /* Retrieve an inode's encryption policy */
395 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
397 const struct fscrypt_info *ci;
398 union fscrypt_context ctx;
401 ci = fscrypt_get_info(inode);
403 /* key available, use the cached policy */
404 *policy = ci->ci_policy;
408 if (!IS_ENCRYPTED(inode))
411 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
413 return (ret == -ERANGE) ? -EINVAL : ret;
415 return fscrypt_policy_from_context(policy, &ctx, ret);
418 static int set_encryption_policy(struct inode *inode,
419 const union fscrypt_policy *policy)
421 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
422 union fscrypt_context ctx;
426 if (!fscrypt_supported_policy(policy, inode))
429 switch (policy->version) {
430 case FSCRYPT_POLICY_V1:
432 * The original encryption policy version provided no way of
433 * verifying that the correct master key was supplied, which was
434 * insecure in scenarios where multiple users have access to the
435 * same encrypted files (even just read-only access). The new
436 * encryption policy version fixes this and also implies use of
437 * an improved key derivation function and allows non-root users
438 * to securely remove keys. So as long as compatibility with
439 * old kernels isn't required, it is recommended to use the new
440 * policy version for all new encrypted directories.
442 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
443 current->comm, current->pid);
445 case FSCRYPT_POLICY_V2:
446 err = fscrypt_verify_key_added(inode->i_sb,
447 policy->v2.master_key_identifier);
450 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
451 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",
452 current->comm, current->pid);
459 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
460 ctxsize = fscrypt_new_context(&ctx, policy, nonce);
462 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
465 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
467 union fscrypt_policy policy;
468 union fscrypt_policy existing_policy;
469 struct inode *inode = file_inode(filp);
474 if (get_user(policy.version, (const u8 __user *)arg))
477 size = fscrypt_policy_size(&policy);
482 * We should just copy the remaining 'size - 1' bytes here, but a
483 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
484 * think that size can be 0 here (despite the check above!) *and* that
485 * it's a compile-time constant. Thus it would think copy_from_user()
486 * is passed compile-time constant ULONG_MAX, causing the compile-time
487 * buffer overflow check to fail, breaking the build. This only occurred
488 * when building an i386 kernel with -Os and branch profiling enabled.
490 * Work around it by just copying the first byte again...
492 version = policy.version;
493 if (copy_from_user(&policy, arg, size))
495 policy.version = version;
497 if (!inode_owner_or_capable(&init_user_ns, inode))
500 ret = mnt_want_write_file(filp);
506 ret = fscrypt_get_policy(inode, &existing_policy);
507 if (ret == -ENODATA) {
508 if (!S_ISDIR(inode->i_mode))
510 else if (IS_DEADDIR(inode))
512 else if (!inode->i_sb->s_cop->empty_dir(inode))
515 ret = set_encryption_policy(inode, &policy);
516 } else if (ret == -EINVAL ||
517 (ret == 0 && !fscrypt_policies_equal(&policy,
518 &existing_policy))) {
519 /* The file already uses a different encryption policy. */
525 mnt_drop_write_file(filp);
528 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
530 /* Original ioctl version; can only get the original policy version */
531 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
533 union fscrypt_policy policy;
536 err = fscrypt_get_policy(file_inode(filp), &policy);
540 if (policy.version != FSCRYPT_POLICY_V1)
543 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
547 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
549 /* Extended ioctl version; can get policies of any version */
550 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
552 struct fscrypt_get_policy_ex_arg arg;
553 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
557 /* arg is policy_size, then policy */
558 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
559 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
560 offsetof(typeof(arg), policy));
561 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
563 err = fscrypt_get_policy(file_inode(filp), policy);
566 policy_size = fscrypt_policy_size(policy);
568 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
571 if (policy_size > arg.policy_size)
573 arg.policy_size = policy_size;
575 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
579 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
581 /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
582 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
584 struct inode *inode = file_inode(filp);
585 union fscrypt_context ctx;
588 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
591 if (!fscrypt_context_is_valid(&ctx, ret))
593 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
594 FSCRYPT_FILE_NONCE_SIZE))
598 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
601 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
602 * within its directory?
604 * @parent: inode for parent directory
605 * @child: inode for file being looked up, opened, or linked into @parent
607 * Filesystems must call this before permitting access to an inode in a
608 * situation where the parent directory is encrypted (either before allowing
609 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
610 * and before any operation that involves linking an inode into an encrypted
611 * directory, including link, rename, and cross rename. It enforces the
612 * constraint that within a given encrypted directory tree, all files use the
613 * same encryption policy. The pre-access check is needed to detect potentially
614 * malicious offline violations of this constraint, while the link and rename
615 * checks are needed to prevent online violations of this constraint.
617 * Return: 1 if permitted, 0 if forbidden.
619 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
621 union fscrypt_policy parent_policy, child_policy;
624 /* No restrictions on file types which are never encrypted */
625 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
626 !S_ISLNK(child->i_mode))
629 /* No restrictions if the parent directory is unencrypted */
630 if (!IS_ENCRYPTED(parent))
633 /* Encrypted directories must not contain unencrypted files */
634 if (!IS_ENCRYPTED(child))
638 * Both parent and child are encrypted, so verify they use the same
639 * encryption policy. Compare the fscrypt_info structs if the keys are
640 * available, otherwise retrieve and compare the fscrypt_contexts.
642 * Note that the fscrypt_context retrieval will be required frequently
643 * when accessing an encrypted directory tree without the key.
644 * Performance-wise this is not a big deal because we already don't
645 * really optimize for file access without the key (to the extent that
646 * such access is even possible), given that any attempted access
647 * already causes a fscrypt_context retrieval and keyring search.
649 * In any case, if an unexpected error occurs, fall back to "forbidden".
652 err = fscrypt_get_encryption_info(parent, true);
655 err = fscrypt_get_encryption_info(child, true);
659 err1 = fscrypt_get_policy(parent, &parent_policy);
660 err2 = fscrypt_get_policy(child, &child_policy);
663 * Allow the case where the parent and child both have an unrecognized
664 * encryption policy, so that files with an unrecognized encryption
665 * policy can be deleted.
667 if (err1 == -EINVAL && err2 == -EINVAL)
673 return fscrypt_policies_equal(&parent_policy, &child_policy);
675 EXPORT_SYMBOL(fscrypt_has_permitted_context);
678 * Return the encryption policy that new files in the directory will inherit, or
679 * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also
680 * ensure that its key is set up, so that the new filename can be encrypted.
682 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
686 if (IS_ENCRYPTED(dir)) {
687 err = fscrypt_require_key(dir);
690 return &dir->i_crypt_info->ci_policy;
693 return fscrypt_get_dummy_policy(dir->i_sb);
697 * fscrypt_context_for_new_inode() - create an encryption context for a new inode
698 * @ctx: where context should be written
699 * @inode: inode from which to fetch policy and nonce
701 * Given an in-core "prepared" (via fscrypt_prepare_new_inode) inode,
702 * generate a new context and write it to ctx. ctx _must_ be at least
703 * FSCRYPT_SET_CONTEXT_MAX_SIZE bytes.
705 * Return: size of the resulting context or a negative error code.
707 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode)
709 struct fscrypt_info *ci = inode->i_crypt_info;
711 BUILD_BUG_ON(sizeof(union fscrypt_context) !=
712 FSCRYPT_SET_CONTEXT_MAX_SIZE);
714 /* fscrypt_prepare_new_inode() should have set up the key already. */
715 if (WARN_ON_ONCE(!ci))
718 return fscrypt_new_context(ctx, &ci->ci_policy, ci->ci_nonce);
720 EXPORT_SYMBOL_GPL(fscrypt_context_for_new_inode);
723 * fscrypt_set_context() - Set the fscrypt context of a new inode
724 * @inode: a new inode
725 * @fs_data: private data given by FS and passed to ->set_context()
727 * This should be called after fscrypt_prepare_new_inode(), generally during a
728 * filesystem transaction. Everything here must be %GFP_NOFS-safe.
730 * Return: 0 on success, -errno on failure
732 int fscrypt_set_context(struct inode *inode, void *fs_data)
734 struct fscrypt_info *ci = inode->i_crypt_info;
735 union fscrypt_context ctx;
738 ctxsize = fscrypt_context_for_new_inode(&ctx, inode);
743 * This may be the first time the inode number is available, so do any
744 * delayed key setup that requires the inode number.
746 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
747 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
748 const struct fscrypt_master_key *mk =
749 ci->ci_master_key->payload.data[0];
751 fscrypt_hash_inode_number(ci, mk);
754 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
756 EXPORT_SYMBOL_GPL(fscrypt_set_context);
759 * fscrypt_parse_test_dummy_encryption() - parse the test_dummy_encryption mount option
760 * @param: the mount option
761 * @dummy_policy: (input/output) the place to write the dummy policy that will
762 * result from parsing the option. Zero-initialize this. If a policy is
763 * already set here (due to test_dummy_encryption being given multiple
764 * times), then this function will verify that the policies are the same.
766 * Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the
767 * argument conflicts with one already specified; or -ENOMEM.
769 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
770 struct fscrypt_dummy_policy *dummy_policy)
772 const char *arg = "v2";
773 union fscrypt_policy *policy;
776 if (param->type == fs_value_is_string && *param->string)
779 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
783 if (!strcmp(arg, "v1")) {
784 policy->version = FSCRYPT_POLICY_V1;
785 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
786 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
787 memset(policy->v1.master_key_descriptor, 0x42,
788 FSCRYPT_KEY_DESCRIPTOR_SIZE);
789 } else if (!strcmp(arg, "v2")) {
790 policy->version = FSCRYPT_POLICY_V2;
791 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
792 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
793 err = fscrypt_get_test_dummy_key_identifier(
794 policy->v2.master_key_identifier);
802 if (dummy_policy->policy) {
803 if (fscrypt_policies_equal(policy, dummy_policy->policy))
809 dummy_policy->policy = policy;
816 EXPORT_SYMBOL_GPL(fscrypt_parse_test_dummy_encryption);
819 * fscrypt_dummy_policies_equal() - check whether two dummy policies are equal
820 * @p1: the first test dummy policy (may be unset)
821 * @p2: the second test dummy policy (may be unset)
823 * Return: %true if the dummy policies are both set and equal, or both unset.
825 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
826 const struct fscrypt_dummy_policy *p2)
828 if (!p1->policy && !p2->policy)
830 if (!p1->policy || !p2->policy)
832 return fscrypt_policies_equal(p1->policy, p2->policy);
834 EXPORT_SYMBOL_GPL(fscrypt_dummy_policies_equal);
836 /* Deprecated, do not use */
837 int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
838 struct fscrypt_dummy_policy *dummy_policy)
840 struct fs_parameter param = {
841 .type = fs_value_is_string,
842 .string = arg ? (char *)arg : "",
844 return fscrypt_parse_test_dummy_encryption(¶m, dummy_policy) ?:
845 fscrypt_add_test_dummy_key(sb, dummy_policy);
847 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
850 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
851 * @seq: the seq_file to print the option to
852 * @sep: the separator character to use
853 * @sb: the filesystem whose options are being shown
855 * Show the test_dummy_encryption mount option, if it was specified.
856 * This is mainly used for /proc/mounts.
858 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
859 struct super_block *sb)
861 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
867 vers = policy->version;
868 if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
871 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
873 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);