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/random.h>
14 #include <linux/seq_file.h>
15 #include <linux/string.h>
16 #include <linux/mount.h>
17 #include "fscrypt_private.h"
20 * fscrypt_policies_equal() - check whether two encryption policies are the same
21 * @policy1: the first policy
22 * @policy2: the second policy
24 * Return: %true if equal, else %false
26 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
27 const union fscrypt_policy *policy2)
29 if (policy1->version != policy2->version)
32 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
35 static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
37 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
38 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
41 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
42 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
45 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
46 filenames_mode == FSCRYPT_MODE_ADIANTUM)
52 static bool supported_direct_key_modes(const struct inode *inode,
53 u32 contents_mode, u32 filenames_mode)
55 const struct fscrypt_mode *mode;
57 if (contents_mode != filenames_mode) {
59 "Direct key flag not allowed with different contents and filenames modes");
62 mode = &fscrypt_modes[contents_mode];
64 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
65 fscrypt_warn(inode, "Direct key flag not allowed with %s",
72 static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
73 const struct inode *inode,
75 int max_ino_bits, int max_lblk_bits)
77 struct super_block *sb = inode->i_sb;
78 int ino_bits = 64, lblk_bits = 64;
81 * It's unsafe to include inode numbers in the IVs if the filesystem can
82 * potentially renumber inodes, e.g. via filesystem shrinking.
84 if (!sb->s_cop->has_stable_inodes ||
85 !sb->s_cop->has_stable_inodes(sb)) {
87 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
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) {
95 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
99 if (lblk_bits > max_lblk_bits) {
101 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
108 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
109 const struct inode *inode)
111 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
112 policy->filenames_encryption_mode)) {
114 "Unsupported encryption modes (contents %d, filenames %d)",
115 policy->contents_encryption_mode,
116 policy->filenames_encryption_mode);
120 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
121 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
122 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
127 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
128 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
129 policy->filenames_encryption_mode))
132 if (IS_CASEFOLDED(inode)) {
133 /* With v1, there's no way to derive dirhash keys. */
135 "v1 policies can't be used on casefolded directories");
142 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
143 const struct inode *inode)
147 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
148 policy->filenames_encryption_mode)) {
150 "Unsupported encryption modes (contents %d, filenames %d)",
151 policy->contents_encryption_mode,
152 policy->filenames_encryption_mode);
156 if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
157 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
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);
166 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
171 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
172 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
173 policy->filenames_encryption_mode))
176 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
177 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
181 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
182 /* This uses hashed inode numbers, so ino_bits doesn't matter. */
183 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
187 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
188 fscrypt_warn(inode, "Reserved bits set in encryption policy");
196 * fscrypt_supported_policy() - check whether an encryption policy is supported
197 * @policy_u: the encryption policy
198 * @inode: the inode on which the policy will be used
200 * Given an encryption policy, check whether all its encryption modes and other
201 * settings are supported by this kernel on the given inode. (But we don't
202 * currently don't check for crypto API support here, so attempting to use an
203 * algorithm not configured into the crypto API will still fail later.)
205 * Return: %true if supported, else %false
207 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
208 const struct inode *inode)
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);
220 * fscrypt_new_context_from_policy() - create a new fscrypt_context from
222 * @ctx_u: output context
223 * @policy_u: input policy
225 * Create an fscrypt_context for an inode that is being assigned the given
226 * encryption policy. A new nonce is randomly generated.
228 * Return: the size of the new context in bytes.
230 static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
231 const union fscrypt_policy *policy_u)
233 memset(ctx_u, 0, sizeof(*ctx_u));
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;
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));
252 case FSCRYPT_POLICY_V2: {
253 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
254 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
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));
273 * fscrypt_policy_from_context() - convert an fscrypt_context to
275 * @policy_u: output policy
276 * @ctx_u: input context
277 * @ctx_size: size of input context in bytes
279 * Given an fscrypt_context, build the corresponding fscrypt_policy.
281 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
282 * version number or size.
284 * This does *not* validate the settings within the policy itself, e.g. the
285 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
287 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
288 const union fscrypt_context *ctx_u,
291 memset(policy_u, 0, sizeof(*policy_u));
293 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
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;
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));
312 case FSCRYPT_CONTEXT_V2: {
313 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
314 struct fscrypt_policy_v2 *policy = &policy_u->v2;
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));
334 /* Retrieve an inode's encryption policy */
335 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
337 const struct fscrypt_info *ci;
338 union fscrypt_context ctx;
341 ci = READ_ONCE(inode->i_crypt_info);
343 /* key available, use the cached policy */
344 *policy = ci->ci_policy;
348 if (!IS_ENCRYPTED(inode))
351 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
353 return (ret == -ERANGE) ? -EINVAL : ret;
355 return fscrypt_policy_from_context(policy, &ctx, ret);
358 static int set_encryption_policy(struct inode *inode,
359 const union fscrypt_policy *policy)
361 union fscrypt_context ctx;
365 if (!fscrypt_supported_policy(policy, inode))
368 switch (policy->version) {
369 case FSCRYPT_POLICY_V1:
371 * The original encryption policy version provided no way of
372 * verifying that the correct master key was supplied, which was
373 * insecure in scenarios where multiple users have access to the
374 * same encrypted files (even just read-only access). The new
375 * encryption policy version fixes this and also implies use of
376 * an improved key derivation function and allows non-root users
377 * to securely remove keys. So as long as compatibility with
378 * old kernels isn't required, it is recommended to use the new
379 * policy version for all new encrypted directories.
381 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
382 current->comm, current->pid);
384 case FSCRYPT_POLICY_V2:
385 err = fscrypt_verify_key_added(inode->i_sb,
386 policy->v2.master_key_identifier);
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);
398 ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
400 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
403 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
405 union fscrypt_policy policy;
406 union fscrypt_policy existing_policy;
407 struct inode *inode = file_inode(filp);
412 if (get_user(policy.version, (const u8 __user *)arg))
415 size = fscrypt_policy_size(&policy);
420 * We should just copy the remaining 'size - 1' bytes here, but a
421 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
422 * think that size can be 0 here (despite the check above!) *and* that
423 * it's a compile-time constant. Thus it would think copy_from_user()
424 * is passed compile-time constant ULONG_MAX, causing the compile-time
425 * buffer overflow check to fail, breaking the build. This only occurred
426 * when building an i386 kernel with -Os and branch profiling enabled.
428 * Work around it by just copying the first byte again...
430 version = policy.version;
431 if (copy_from_user(&policy, arg, size))
433 policy.version = version;
435 if (!inode_owner_or_capable(inode))
438 ret = mnt_want_write_file(filp);
444 ret = fscrypt_get_policy(inode, &existing_policy);
445 if (ret == -ENODATA) {
446 if (!S_ISDIR(inode->i_mode))
448 else if (IS_DEADDIR(inode))
450 else if (!inode->i_sb->s_cop->empty_dir(inode))
453 ret = set_encryption_policy(inode, &policy);
454 } else if (ret == -EINVAL ||
455 (ret == 0 && !fscrypt_policies_equal(&policy,
456 &existing_policy))) {
457 /* The file already uses a different encryption policy. */
463 mnt_drop_write_file(filp);
466 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
468 /* Original ioctl version; can only get the original policy version */
469 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
471 union fscrypt_policy policy;
474 err = fscrypt_get_policy(file_inode(filp), &policy);
478 if (policy.version != FSCRYPT_POLICY_V1)
481 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
485 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
487 /* Extended ioctl version; can get policies of any version */
488 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
490 struct fscrypt_get_policy_ex_arg arg;
491 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
495 /* arg is policy_size, then policy */
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));
501 err = fscrypt_get_policy(file_inode(filp), policy);
504 policy_size = fscrypt_policy_size(policy);
506 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
509 if (policy_size > arg.policy_size)
511 arg.policy_size = policy_size;
513 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
517 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
519 /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
520 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
522 struct inode *inode = file_inode(filp);
523 union fscrypt_context ctx;
526 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
529 if (!fscrypt_context_is_valid(&ctx, ret))
531 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
532 FS_KEY_DERIVATION_NONCE_SIZE))
536 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
539 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
540 * within its directory?
542 * @parent: inode for parent directory
543 * @child: inode for file being looked up, opened, or linked into @parent
545 * Filesystems must call this before permitting access to an inode in a
546 * situation where the parent directory is encrypted (either before allowing
547 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
548 * and before any operation that involves linking an inode into an encrypted
549 * directory, including link, rename, and cross rename. It enforces the
550 * constraint that within a given encrypted directory tree, all files use the
551 * same encryption policy. The pre-access check is needed to detect potentially
552 * malicious offline violations of this constraint, while the link and rename
553 * checks are needed to prevent online violations of this constraint.
555 * Return: 1 if permitted, 0 if forbidden.
557 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
559 union fscrypt_policy parent_policy, child_policy;
562 /* No restrictions on file types which are never encrypted */
563 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
564 !S_ISLNK(child->i_mode))
567 /* No restrictions if the parent directory is unencrypted */
568 if (!IS_ENCRYPTED(parent))
571 /* Encrypted directories must not contain unencrypted files */
572 if (!IS_ENCRYPTED(child))
576 * Both parent and child are encrypted, so verify they use the same
577 * encryption policy. Compare the fscrypt_info structs if the keys are
578 * available, otherwise retrieve and compare the fscrypt_contexts.
580 * Note that the fscrypt_context retrieval will be required frequently
581 * when accessing an encrypted directory tree without the key.
582 * Performance-wise this is not a big deal because we already don't
583 * really optimize for file access without the key (to the extent that
584 * such access is even possible), given that any attempted access
585 * already causes a fscrypt_context retrieval and keyring search.
587 * In any case, if an unexpected error occurs, fall back to "forbidden".
590 err = fscrypt_get_encryption_info(parent);
593 err = fscrypt_get_encryption_info(child);
597 err = fscrypt_get_policy(parent, &parent_policy);
601 err = fscrypt_get_policy(child, &child_policy);
605 return fscrypt_policies_equal(&parent_policy, &child_policy);
607 EXPORT_SYMBOL(fscrypt_has_permitted_context);
610 * fscrypt_inherit_context() - Sets a child context from its parent
611 * @parent: Parent inode from which the context is inherited.
612 * @child: Child inode that inherits the context from @parent.
613 * @fs_data: private data given by FS.
614 * @preload: preload child i_crypt_info if true
616 * Return: 0 on success, -errno on failure
618 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
619 void *fs_data, bool preload)
621 union fscrypt_context ctx;
623 struct fscrypt_info *ci;
626 res = fscrypt_get_encryption_info(parent);
630 ci = READ_ONCE(parent->i_crypt_info);
634 ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
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);
640 return preload ? fscrypt_get_encryption_info(child): 0;
642 EXPORT_SYMBOL(fscrypt_inherit_context);
645 * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
646 * @sb: the filesystem on which test_dummy_encryption is being specified
647 * @arg: the argument to the test_dummy_encryption option.
648 * If no argument was specified, then @arg->from == NULL.
649 * @dummy_ctx: the filesystem's current dummy context (input/output, see below)
651 * Handle the test_dummy_encryption mount option by creating a dummy encryption
652 * context, saving it in @dummy_ctx, and adding the corresponding dummy
653 * encryption key to the filesystem. If the @dummy_ctx is already set, then
654 * instead validate that it matches @arg. Don't support changing it via
655 * remount, as that is difficult to do safely.
657 * The reason we use an fscrypt_context rather than an fscrypt_policy is because
658 * we mustn't generate a new nonce each time we access a dummy-encrypted
659 * directory, as that would change the way filenames are encrypted.
661 * Return: 0 on success (dummy context set, or the same context is already set);
662 * -EEXIST if a different dummy context is already set;
663 * or another -errno value.
665 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
666 const substring_t *arg,
667 struct fscrypt_dummy_context *dummy_ctx)
669 const char *argstr = "v2";
670 const char *argstr_to_free = NULL;
671 struct fscrypt_key_specifier key_spec = { 0 };
673 union fscrypt_context *ctx = NULL;
677 argstr = argstr_to_free = match_strdup(arg);
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 /* key_spec.u.identifier gets filled in when adding the key */
696 if (dummy_ctx->ctx) {
698 * Note: if we ever make test_dummy_encryption support
699 * specifying other encryption settings, such as the encryption
700 * modes, we'll need to compare those settings here.
702 if (dummy_ctx->ctx->version == version)
709 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
715 err = fscrypt_add_test_dummy_key(sb, &key_spec);
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);
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);
738 dummy_ctx->ctx = ctx;
743 kfree(argstr_to_free);
746 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
749 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
750 * @seq: the seq_file to print the option to
751 * @sep: the separator character to use
752 * @sb: the filesystem whose options are being shown
754 * Show the test_dummy_encryption mount option, if it was specified.
755 * This is mainly used for /proc/mounts.
757 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
758 struct super_block *sb)
760 const union fscrypt_context *ctx = fscrypt_get_dummy_context(sb);
764 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, ctx->version);
766 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);