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 * IV_INO_LBLK_* exist only because of hardware limitations, and
82 * currently the only known use case for them involves AES-256-XTS.
83 * That's also all we test currently. For these reasons, for now only
84 * allow AES-256-XTS here. This can be relaxed later if a use case for
85 * IV_INO_LBLK_* with other encryption modes arises.
87 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
89 "Can't use %s policy with contents mode other than AES-256-XTS",
95 * It's unsafe to include inode numbers in the IVs if the filesystem can
96 * potentially renumber inodes, e.g. via filesystem shrinking.
98 if (!sb->s_cop->has_stable_inodes ||
99 !sb->s_cop->has_stable_inodes(sb)) {
101 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
105 if (sb->s_cop->get_ino_and_lblk_bits)
106 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
107 if (ino_bits > max_ino_bits) {
109 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
113 if (lblk_bits > max_lblk_bits) {
115 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
122 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
123 const struct inode *inode)
125 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
126 policy->filenames_encryption_mode)) {
128 "Unsupported encryption modes (contents %d, filenames %d)",
129 policy->contents_encryption_mode,
130 policy->filenames_encryption_mode);
134 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
135 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
136 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
141 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
142 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
143 policy->filenames_encryption_mode))
146 if (IS_CASEFOLDED(inode)) {
147 /* With v1, there's no way to derive dirhash keys. */
149 "v1 policies can't be used on casefolded directories");
156 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
157 const struct inode *inode)
161 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
162 policy->filenames_encryption_mode)) {
164 "Unsupported encryption modes (contents %d, filenames %d)",
165 policy->contents_encryption_mode,
166 policy->filenames_encryption_mode);
170 if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
171 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
176 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
177 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
178 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
180 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
185 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
186 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
187 policy->filenames_encryption_mode))
190 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
191 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
195 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
196 /* This uses hashed inode numbers, so ino_bits doesn't matter. */
197 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
201 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
202 fscrypt_warn(inode, "Reserved bits set in encryption policy");
210 * fscrypt_supported_policy() - check whether an encryption policy is supported
211 * @policy_u: the encryption policy
212 * @inode: the inode on which the policy will be used
214 * Given an encryption policy, check whether all its encryption modes and other
215 * settings are supported by this kernel on the given inode. (But we don't
216 * currently don't check for crypto API support here, so attempting to use an
217 * algorithm not configured into the crypto API will still fail later.)
219 * Return: %true if supported, else %false
221 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
222 const struct inode *inode)
224 switch (policy_u->version) {
225 case FSCRYPT_POLICY_V1:
226 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
227 case FSCRYPT_POLICY_V2:
228 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
234 * fscrypt_new_context_from_policy() - create a new fscrypt_context from
236 * @ctx_u: output context
237 * @policy_u: input policy
239 * Create an fscrypt_context for an inode that is being assigned the given
240 * encryption policy. A new nonce is randomly generated.
242 * Return: the size of the new context in bytes.
244 static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
245 const union fscrypt_policy *policy_u)
247 memset(ctx_u, 0, sizeof(*ctx_u));
249 switch (policy_u->version) {
250 case FSCRYPT_POLICY_V1: {
251 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
252 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
254 ctx->version = FSCRYPT_CONTEXT_V1;
255 ctx->contents_encryption_mode =
256 policy->contents_encryption_mode;
257 ctx->filenames_encryption_mode =
258 policy->filenames_encryption_mode;
259 ctx->flags = policy->flags;
260 memcpy(ctx->master_key_descriptor,
261 policy->master_key_descriptor,
262 sizeof(ctx->master_key_descriptor));
263 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
266 case FSCRYPT_POLICY_V2: {
267 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
268 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
270 ctx->version = FSCRYPT_CONTEXT_V2;
271 ctx->contents_encryption_mode =
272 policy->contents_encryption_mode;
273 ctx->filenames_encryption_mode =
274 policy->filenames_encryption_mode;
275 ctx->flags = policy->flags;
276 memcpy(ctx->master_key_identifier,
277 policy->master_key_identifier,
278 sizeof(ctx->master_key_identifier));
279 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
287 * fscrypt_policy_from_context() - convert an fscrypt_context to
289 * @policy_u: output policy
290 * @ctx_u: input context
291 * @ctx_size: size of input context in bytes
293 * Given an fscrypt_context, build the corresponding fscrypt_policy.
295 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
296 * version number or size.
298 * This does *not* validate the settings within the policy itself, e.g. the
299 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
301 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
302 const union fscrypt_context *ctx_u,
305 memset(policy_u, 0, sizeof(*policy_u));
307 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
310 switch (ctx_u->version) {
311 case FSCRYPT_CONTEXT_V1: {
312 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
313 struct fscrypt_policy_v1 *policy = &policy_u->v1;
315 policy->version = FSCRYPT_POLICY_V1;
316 policy->contents_encryption_mode =
317 ctx->contents_encryption_mode;
318 policy->filenames_encryption_mode =
319 ctx->filenames_encryption_mode;
320 policy->flags = ctx->flags;
321 memcpy(policy->master_key_descriptor,
322 ctx->master_key_descriptor,
323 sizeof(policy->master_key_descriptor));
326 case FSCRYPT_CONTEXT_V2: {
327 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
328 struct fscrypt_policy_v2 *policy = &policy_u->v2;
330 policy->version = FSCRYPT_POLICY_V2;
331 policy->contents_encryption_mode =
332 ctx->contents_encryption_mode;
333 policy->filenames_encryption_mode =
334 ctx->filenames_encryption_mode;
335 policy->flags = ctx->flags;
336 memcpy(policy->__reserved, ctx->__reserved,
337 sizeof(policy->__reserved));
338 memcpy(policy->master_key_identifier,
339 ctx->master_key_identifier,
340 sizeof(policy->master_key_identifier));
348 /* Retrieve an inode's encryption policy */
349 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
351 const struct fscrypt_info *ci;
352 union fscrypt_context ctx;
355 ci = fscrypt_get_info(inode);
357 /* key available, use the cached policy */
358 *policy = ci->ci_policy;
362 if (!IS_ENCRYPTED(inode))
365 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
367 return (ret == -ERANGE) ? -EINVAL : ret;
369 return fscrypt_policy_from_context(policy, &ctx, ret);
372 static int set_encryption_policy(struct inode *inode,
373 const union fscrypt_policy *policy)
375 union fscrypt_context ctx;
379 if (!fscrypt_supported_policy(policy, inode))
382 switch (policy->version) {
383 case FSCRYPT_POLICY_V1:
385 * The original encryption policy version provided no way of
386 * verifying that the correct master key was supplied, which was
387 * insecure in scenarios where multiple users have access to the
388 * same encrypted files (even just read-only access). The new
389 * encryption policy version fixes this and also implies use of
390 * an improved key derivation function and allows non-root users
391 * to securely remove keys. So as long as compatibility with
392 * old kernels isn't required, it is recommended to use the new
393 * policy version for all new encrypted directories.
395 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
396 current->comm, current->pid);
398 case FSCRYPT_POLICY_V2:
399 err = fscrypt_verify_key_added(inode->i_sb,
400 policy->v2.master_key_identifier);
403 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
404 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",
405 current->comm, current->pid);
412 ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
414 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
417 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
419 union fscrypt_policy policy;
420 union fscrypt_policy existing_policy;
421 struct inode *inode = file_inode(filp);
426 if (get_user(policy.version, (const u8 __user *)arg))
429 size = fscrypt_policy_size(&policy);
434 * We should just copy the remaining 'size - 1' bytes here, but a
435 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
436 * think that size can be 0 here (despite the check above!) *and* that
437 * it's a compile-time constant. Thus it would think copy_from_user()
438 * is passed compile-time constant ULONG_MAX, causing the compile-time
439 * buffer overflow check to fail, breaking the build. This only occurred
440 * when building an i386 kernel with -Os and branch profiling enabled.
442 * Work around it by just copying the first byte again...
444 version = policy.version;
445 if (copy_from_user(&policy, arg, size))
447 policy.version = version;
449 if (!inode_owner_or_capable(inode))
452 ret = mnt_want_write_file(filp);
458 ret = fscrypt_get_policy(inode, &existing_policy);
459 if (ret == -ENODATA) {
460 if (!S_ISDIR(inode->i_mode))
462 else if (IS_DEADDIR(inode))
464 else if (!inode->i_sb->s_cop->empty_dir(inode))
467 ret = set_encryption_policy(inode, &policy);
468 } else if (ret == -EINVAL ||
469 (ret == 0 && !fscrypt_policies_equal(&policy,
470 &existing_policy))) {
471 /* The file already uses a different encryption policy. */
477 mnt_drop_write_file(filp);
480 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
482 /* Original ioctl version; can only get the original policy version */
483 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
485 union fscrypt_policy policy;
488 err = fscrypt_get_policy(file_inode(filp), &policy);
492 if (policy.version != FSCRYPT_POLICY_V1)
495 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
499 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
501 /* Extended ioctl version; can get policies of any version */
502 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
504 struct fscrypt_get_policy_ex_arg arg;
505 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
509 /* arg is policy_size, then policy */
510 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
511 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
512 offsetof(typeof(arg), policy));
513 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
515 err = fscrypt_get_policy(file_inode(filp), policy);
518 policy_size = fscrypt_policy_size(policy);
520 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
523 if (policy_size > arg.policy_size)
525 arg.policy_size = policy_size;
527 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
531 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
533 /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
534 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
536 struct inode *inode = file_inode(filp);
537 union fscrypt_context ctx;
540 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
543 if (!fscrypt_context_is_valid(&ctx, ret))
545 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
546 FSCRYPT_FILE_NONCE_SIZE))
550 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
553 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
554 * within its directory?
556 * @parent: inode for parent directory
557 * @child: inode for file being looked up, opened, or linked into @parent
559 * Filesystems must call this before permitting access to an inode in a
560 * situation where the parent directory is encrypted (either before allowing
561 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
562 * and before any operation that involves linking an inode into an encrypted
563 * directory, including link, rename, and cross rename. It enforces the
564 * constraint that within a given encrypted directory tree, all files use the
565 * same encryption policy. The pre-access check is needed to detect potentially
566 * malicious offline violations of this constraint, while the link and rename
567 * checks are needed to prevent online violations of this constraint.
569 * Return: 1 if permitted, 0 if forbidden.
571 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
573 union fscrypt_policy parent_policy, child_policy;
576 /* No restrictions on file types which are never encrypted */
577 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
578 !S_ISLNK(child->i_mode))
581 /* No restrictions if the parent directory is unencrypted */
582 if (!IS_ENCRYPTED(parent))
585 /* Encrypted directories must not contain unencrypted files */
586 if (!IS_ENCRYPTED(child))
590 * Both parent and child are encrypted, so verify they use the same
591 * encryption policy. Compare the fscrypt_info structs if the keys are
592 * available, otherwise retrieve and compare the fscrypt_contexts.
594 * Note that the fscrypt_context retrieval will be required frequently
595 * when accessing an encrypted directory tree without the key.
596 * Performance-wise this is not a big deal because we already don't
597 * really optimize for file access without the key (to the extent that
598 * such access is even possible), given that any attempted access
599 * already causes a fscrypt_context retrieval and keyring search.
601 * In any case, if an unexpected error occurs, fall back to "forbidden".
604 err = fscrypt_get_encryption_info(parent);
607 err = fscrypt_get_encryption_info(child);
611 err = fscrypt_get_policy(parent, &parent_policy);
615 err = fscrypt_get_policy(child, &child_policy);
619 return fscrypt_policies_equal(&parent_policy, &child_policy);
621 EXPORT_SYMBOL(fscrypt_has_permitted_context);
624 * fscrypt_inherit_context() - Sets a child context from its parent
625 * @parent: Parent inode from which the context is inherited.
626 * @child: Child inode that inherits the context from @parent.
627 * @fs_data: private data given by FS.
628 * @preload: preload child i_crypt_info if true
630 * Return: 0 on success, -errno on failure
632 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
633 void *fs_data, bool preload)
635 union fscrypt_context ctx;
637 struct fscrypt_info *ci;
640 res = fscrypt_get_encryption_info(parent);
644 ci = fscrypt_get_info(parent);
648 ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
650 BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
651 res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
654 return preload ? fscrypt_get_encryption_info(child): 0;
656 EXPORT_SYMBOL(fscrypt_inherit_context);
659 * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
660 * @sb: the filesystem on which test_dummy_encryption is being specified
661 * @arg: the argument to the test_dummy_encryption option.
662 * If no argument was specified, then @arg->from == NULL.
663 * @dummy_ctx: the filesystem's current dummy context (input/output, see below)
665 * Handle the test_dummy_encryption mount option by creating a dummy encryption
666 * context, saving it in @dummy_ctx, and adding the corresponding dummy
667 * encryption key to the filesystem. If the @dummy_ctx is already set, then
668 * instead validate that it matches @arg. Don't support changing it via
669 * remount, as that is difficult to do safely.
671 * The reason we use an fscrypt_context rather than an fscrypt_policy is because
672 * we mustn't generate a new nonce each time we access a dummy-encrypted
673 * directory, as that would change the way filenames are encrypted.
675 * Return: 0 on success (dummy context set, or the same context is already set);
676 * -EEXIST if a different dummy context is already set;
677 * or another -errno value.
679 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
680 const substring_t *arg,
681 struct fscrypt_dummy_context *dummy_ctx)
683 const char *argstr = "v2";
684 const char *argstr_to_free = NULL;
685 struct fscrypt_key_specifier key_spec = { 0 };
687 union fscrypt_context *ctx = NULL;
691 argstr = argstr_to_free = match_strdup(arg);
696 if (!strcmp(argstr, "v1")) {
697 version = FSCRYPT_CONTEXT_V1;
698 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
699 memset(key_spec.u.descriptor, 0x42,
700 FSCRYPT_KEY_DESCRIPTOR_SIZE);
701 } else if (!strcmp(argstr, "v2")) {
702 version = FSCRYPT_CONTEXT_V2;
703 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
704 /* key_spec.u.identifier gets filled in when adding the key */
710 if (dummy_ctx->ctx) {
712 * Note: if we ever make test_dummy_encryption support
713 * specifying other encryption settings, such as the encryption
714 * modes, we'll need to compare those settings here.
716 if (dummy_ctx->ctx->version == version)
723 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
729 err = fscrypt_add_test_dummy_key(sb, &key_spec);
733 ctx->version = version;
734 switch (ctx->version) {
735 case FSCRYPT_CONTEXT_V1:
736 ctx->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
737 ctx->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
738 memcpy(ctx->v1.master_key_descriptor, key_spec.u.descriptor,
739 FSCRYPT_KEY_DESCRIPTOR_SIZE);
741 case FSCRYPT_CONTEXT_V2:
742 ctx->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
743 ctx->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
744 memcpy(ctx->v2.master_key_identifier, key_spec.u.identifier,
745 FSCRYPT_KEY_IDENTIFIER_SIZE);
752 dummy_ctx->ctx = ctx;
757 kfree(argstr_to_free);
760 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
763 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
764 * @seq: the seq_file to print the option to
765 * @sep: the separator character to use
766 * @sb: the filesystem whose options are being shown
768 * Show the test_dummy_encryption mount option, if it was specified.
769 * This is mainly used for /proc/mounts.
771 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
772 struct super_block *sb)
774 const union fscrypt_context *ctx = fscrypt_get_dummy_context(sb);
778 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, ctx->version);
780 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);