xref: /openbmc/linux/fs/crypto/policy.c (revision 59b4412f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Encryption policy functions for per-file encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  * Copyright (C) 2015, Motorola Mobility.
7  *
8  * Originally written by Michael Halcrow, 2015.
9  * Modified by Jaegeuk Kim, 2015.
10  * Modified by Eric Biggers, 2019 for v2 policy support.
11  */
12 
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"
18 
19 /**
20  * fscrypt_policies_equal() - check whether two encryption policies are the same
21  * @policy1: the first policy
22  * @policy2: the second policy
23  *
24  * Return: %true if equal, else %false
25  */
26 bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
27 			    const union fscrypt_policy *policy2)
28 {
29 	if (policy1->version != policy2->version)
30 		return false;
31 
32 	return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
33 }
34 
35 static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
36 {
37 	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
38 	    filenames_mode == FSCRYPT_MODE_AES_256_CTS)
39 		return true;
40 
41 	if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
42 	    filenames_mode == FSCRYPT_MODE_AES_128_CTS)
43 		return true;
44 
45 	if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
46 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
47 		return true;
48 
49 	return false;
50 }
51 
52 static bool supported_direct_key_modes(const struct inode *inode,
53 				       u32 contents_mode, u32 filenames_mode)
54 {
55 	const struct fscrypt_mode *mode;
56 
57 	if (contents_mode != filenames_mode) {
58 		fscrypt_warn(inode,
59 			     "Direct key flag not allowed with different contents and filenames modes");
60 		return false;
61 	}
62 	mode = &fscrypt_modes[contents_mode];
63 
64 	if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
65 		fscrypt_warn(inode, "Direct key flag not allowed with %s",
66 			     mode->friendly_name);
67 		return false;
68 	}
69 	return true;
70 }
71 
72 static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
73 					 const struct inode *inode,
74 					 const char *type,
75 					 int max_ino_bits, int max_lblk_bits)
76 {
77 	struct super_block *sb = inode->i_sb;
78 	int ino_bits = 64, lblk_bits = 64;
79 
80 	/*
81 	 * It's unsafe to include inode numbers in the IVs if the filesystem can
82 	 * potentially renumber inodes, e.g. via filesystem shrinking.
83 	 */
84 	if (!sb->s_cop->has_stable_inodes ||
85 	    !sb->s_cop->has_stable_inodes(sb)) {
86 		fscrypt_warn(inode,
87 			     "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
88 			     type, sb->s_id);
89 		return false;
90 	}
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) {
94 		fscrypt_warn(inode,
95 			     "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
96 			     type, sb->s_id);
97 		return false;
98 	}
99 	if (lblk_bits > max_lblk_bits) {
100 		fscrypt_warn(inode,
101 			     "Can't use %s policy on filesystem '%s' because its block numbers are too long",
102 			     type, sb->s_id);
103 		return false;
104 	}
105 	return true;
106 }
107 
108 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
109 					const struct inode *inode)
110 {
111 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
112 				     policy->filenames_encryption_mode)) {
113 		fscrypt_warn(inode,
114 			     "Unsupported encryption modes (contents %d, filenames %d)",
115 			     policy->contents_encryption_mode,
116 			     policy->filenames_encryption_mode);
117 		return false;
118 	}
119 
120 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
121 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
122 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
123 			     policy->flags);
124 		return false;
125 	}
126 
127 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
128 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
129 					policy->filenames_encryption_mode))
130 		return false;
131 
132 	if (IS_CASEFOLDED(inode)) {
133 		/* With v1, there's no way to derive dirhash keys. */
134 		fscrypt_warn(inode,
135 			     "v1 policies can't be used on casefolded directories");
136 		return false;
137 	}
138 
139 	return true;
140 }
141 
142 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
143 					const struct inode *inode)
144 {
145 	int count = 0;
146 
147 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
148 				     policy->filenames_encryption_mode)) {
149 		fscrypt_warn(inode,
150 			     "Unsupported encryption modes (contents %d, filenames %d)",
151 			     policy->contents_encryption_mode,
152 			     policy->filenames_encryption_mode);
153 		return false;
154 	}
155 
156 	if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
157 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
158 			     policy->flags);
159 		return false;
160 	}
161 
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);
165 	if (count > 1) {
166 		fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
167 			     policy->flags);
168 		return false;
169 	}
170 
171 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
172 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
173 					policy->filenames_encryption_mode))
174 		return false;
175 
176 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
177 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
178 					  32, 32))
179 		return false;
180 
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",
184 					  INT_MAX, 32))
185 		return false;
186 
187 	if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
188 		fscrypt_warn(inode, "Reserved bits set in encryption policy");
189 		return false;
190 	}
191 
192 	return true;
193 }
194 
195 /**
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
199  *
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.)
204  *
205  * Return: %true if supported, else %false
206  */
207 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
208 			      const struct inode *inode)
209 {
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);
215 	}
216 	return false;
217 }
218 
219 /**
220  * fscrypt_new_context_from_policy() - create a new fscrypt_context from
221  *				       an fscrypt_policy
222  * @ctx_u: output context
223  * @policy_u: input policy
224  *
225  * Create an fscrypt_context for an inode that is being assigned the given
226  * encryption policy.  A new nonce is randomly generated.
227  *
228  * Return: the size of the new context in bytes.
229  */
230 static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
231 					   const union fscrypt_policy *policy_u)
232 {
233 	memset(ctx_u, 0, sizeof(*ctx_u));
234 
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;
239 
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));
250 		return sizeof(*ctx);
251 	}
252 	case FSCRYPT_POLICY_V2: {
253 		const struct fscrypt_policy_v2 *policy = &policy_u->v2;
254 		struct fscrypt_context_v2 *ctx = &ctx_u->v2;
255 
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));
266 		return sizeof(*ctx);
267 	}
268 	}
269 	BUG();
270 }
271 
272 /**
273  * fscrypt_policy_from_context() - convert an fscrypt_context to
274  *				   an fscrypt_policy
275  * @policy_u: output policy
276  * @ctx_u: input context
277  * @ctx_size: size of input context in bytes
278  *
279  * Given an fscrypt_context, build the corresponding fscrypt_policy.
280  *
281  * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
282  * version number or size.
283  *
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.
286  */
287 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
288 				const union fscrypt_context *ctx_u,
289 				int ctx_size)
290 {
291 	memset(policy_u, 0, sizeof(*policy_u));
292 
293 	if (!fscrypt_context_is_valid(ctx_u, ctx_size))
294 		return -EINVAL;
295 
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;
300 
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));
310 		return 0;
311 	}
312 	case FSCRYPT_CONTEXT_V2: {
313 		const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
314 		struct fscrypt_policy_v2 *policy = &policy_u->v2;
315 
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));
327 		return 0;
328 	}
329 	}
330 	/* unreachable */
331 	return -EINVAL;
332 }
333 
334 /* Retrieve an inode's encryption policy */
335 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
336 {
337 	const struct fscrypt_info *ci;
338 	union fscrypt_context ctx;
339 	int ret;
340 
341 	ci = READ_ONCE(inode->i_crypt_info);
342 	if (ci) {
343 		/* key available, use the cached policy */
344 		*policy = ci->ci_policy;
345 		return 0;
346 	}
347 
348 	if (!IS_ENCRYPTED(inode))
349 		return -ENODATA;
350 
351 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
352 	if (ret < 0)
353 		return (ret == -ERANGE) ? -EINVAL : ret;
354 
355 	return fscrypt_policy_from_context(policy, &ctx, ret);
356 }
357 
358 static int set_encryption_policy(struct inode *inode,
359 				 const union fscrypt_policy *policy)
360 {
361 	union fscrypt_context ctx;
362 	int ctxsize;
363 	int err;
364 
365 	if (!fscrypt_supported_policy(policy, inode))
366 		return -EINVAL;
367 
368 	switch (policy->version) {
369 	case FSCRYPT_POLICY_V1:
370 		/*
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.
380 		 */
381 		pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
382 			     current->comm, current->pid);
383 		break;
384 	case FSCRYPT_POLICY_V2:
385 		err = fscrypt_verify_key_added(inode->i_sb,
386 					       policy->v2.master_key_identifier);
387 		if (err)
388 			return err;
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);
392 		break;
393 	default:
394 		WARN_ON(1);
395 		return -EINVAL;
396 	}
397 
398 	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
399 
400 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
401 }
402 
403 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
404 {
405 	union fscrypt_policy policy;
406 	union fscrypt_policy existing_policy;
407 	struct inode *inode = file_inode(filp);
408 	u8 version;
409 	int size;
410 	int ret;
411 
412 	if (get_user(policy.version, (const u8 __user *)arg))
413 		return -EFAULT;
414 
415 	size = fscrypt_policy_size(&policy);
416 	if (size <= 0)
417 		return -EINVAL;
418 
419 	/*
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.
427 	 *
428 	 * Work around it by just copying the first byte again...
429 	 */
430 	version = policy.version;
431 	if (copy_from_user(&policy, arg, size))
432 		return -EFAULT;
433 	policy.version = version;
434 
435 	if (!inode_owner_or_capable(inode))
436 		return -EACCES;
437 
438 	ret = mnt_want_write_file(filp);
439 	if (ret)
440 		return ret;
441 
442 	inode_lock(inode);
443 
444 	ret = fscrypt_get_policy(inode, &existing_policy);
445 	if (ret == -ENODATA) {
446 		if (!S_ISDIR(inode->i_mode))
447 			ret = -ENOTDIR;
448 		else if (IS_DEADDIR(inode))
449 			ret = -ENOENT;
450 		else if (!inode->i_sb->s_cop->empty_dir(inode))
451 			ret = -ENOTEMPTY;
452 		else
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. */
458 		ret = -EEXIST;
459 	}
460 
461 	inode_unlock(inode);
462 
463 	mnt_drop_write_file(filp);
464 	return ret;
465 }
466 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
467 
468 /* Original ioctl version; can only get the original policy version */
469 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
470 {
471 	union fscrypt_policy policy;
472 	int err;
473 
474 	err = fscrypt_get_policy(file_inode(filp), &policy);
475 	if (err)
476 		return err;
477 
478 	if (policy.version != FSCRYPT_POLICY_V1)
479 		return -EINVAL;
480 
481 	if (copy_to_user(arg, &policy, sizeof(policy.v1)))
482 		return -EFAULT;
483 	return 0;
484 }
485 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
486 
487 /* Extended ioctl version; can get policies of any version */
488 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
489 {
490 	struct fscrypt_get_policy_ex_arg arg;
491 	union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
492 	size_t policy_size;
493 	int err;
494 
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));
500 
501 	err = fscrypt_get_policy(file_inode(filp), policy);
502 	if (err)
503 		return err;
504 	policy_size = fscrypt_policy_size(policy);
505 
506 	if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
507 		return -EFAULT;
508 
509 	if (policy_size > arg.policy_size)
510 		return -EOVERFLOW;
511 	arg.policy_size = policy_size;
512 
513 	if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
514 		return -EFAULT;
515 	return 0;
516 }
517 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
518 
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)
521 {
522 	struct inode *inode = file_inode(filp);
523 	union fscrypt_context ctx;
524 	int ret;
525 
526 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
527 	if (ret < 0)
528 		return ret;
529 	if (!fscrypt_context_is_valid(&ctx, ret))
530 		return -EINVAL;
531 	if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
532 			 FS_KEY_DERIVATION_NONCE_SIZE))
533 		return -EFAULT;
534 	return 0;
535 }
536 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
537 
538 /**
539  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
540  *				     within its directory?
541  *
542  * @parent: inode for parent directory
543  * @child: inode for file being looked up, opened, or linked into @parent
544  *
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.
554  *
555  * Return: 1 if permitted, 0 if forbidden.
556  */
557 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
558 {
559 	union fscrypt_policy parent_policy, child_policy;
560 	int err;
561 
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))
565 		return 1;
566 
567 	/* No restrictions if the parent directory is unencrypted */
568 	if (!IS_ENCRYPTED(parent))
569 		return 1;
570 
571 	/* Encrypted directories must not contain unencrypted files */
572 	if (!IS_ENCRYPTED(child))
573 		return 0;
574 
575 	/*
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.
579 	 *
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.
586 	 *
587 	 * In any case, if an unexpected error occurs, fall back to "forbidden".
588 	 */
589 
590 	err = fscrypt_get_encryption_info(parent);
591 	if (err)
592 		return 0;
593 	err = fscrypt_get_encryption_info(child);
594 	if (err)
595 		return 0;
596 
597 	err = fscrypt_get_policy(parent, &parent_policy);
598 	if (err)
599 		return 0;
600 
601 	err = fscrypt_get_policy(child, &child_policy);
602 	if (err)
603 		return 0;
604 
605 	return fscrypt_policies_equal(&parent_policy, &child_policy);
606 }
607 EXPORT_SYMBOL(fscrypt_has_permitted_context);
608 
609 /**
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
615  *
616  * Return: 0 on success, -errno on failure
617  */
618 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
619 						void *fs_data, bool preload)
620 {
621 	union fscrypt_context ctx;
622 	int ctxsize;
623 	struct fscrypt_info *ci;
624 	int res;
625 
626 	res = fscrypt_get_encryption_info(parent);
627 	if (res < 0)
628 		return res;
629 
630 	ci = READ_ONCE(parent->i_crypt_info);
631 	if (ci == NULL)
632 		return -ENOKEY;
633 
634 	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
635 
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);
638 	if (res)
639 		return res;
640 	return preload ? fscrypt_get_encryption_info(child): 0;
641 }
642 EXPORT_SYMBOL(fscrypt_inherit_context);
643 
644 /**
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)
650  *
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.
656  *
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.
660  *
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.
664  */
665 int fscrypt_set_test_dummy_encryption(struct super_block *sb,
666 				      const substring_t *arg,
667 				      struct fscrypt_dummy_context *dummy_ctx)
668 {
669 	const char *argstr = "v2";
670 	const char *argstr_to_free = NULL;
671 	struct fscrypt_key_specifier key_spec = { 0 };
672 	int version;
673 	union fscrypt_context *ctx = NULL;
674 	int err;
675 
676 	if (arg->from) {
677 		argstr = argstr_to_free = match_strdup(arg);
678 		if (!argstr)
679 			return -ENOMEM;
680 	}
681 
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 */
691 	} else {
692 		err = -EINVAL;
693 		goto out;
694 	}
695 
696 	if (dummy_ctx->ctx) {
697 		/*
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.
701 		 */
702 		if (dummy_ctx->ctx->version == version)
703 			err = 0;
704 		else
705 			err = -EEXIST;
706 		goto out;
707 	}
708 
709 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
710 	if (!ctx) {
711 		err = -ENOMEM;
712 		goto out;
713 	}
714 
715 	err = fscrypt_add_test_dummy_key(sb, &key_spec);
716 	if (err)
717 		goto out;
718 
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);
726 		break;
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);
732 		break;
733 	default:
734 		WARN_ON(1);
735 		err = -EINVAL;
736 		goto out;
737 	}
738 	dummy_ctx->ctx = ctx;
739 	ctx = NULL;
740 	err = 0;
741 out:
742 	kfree(ctx);
743 	kfree(argstr_to_free);
744 	return err;
745 }
746 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
747 
748 /**
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
753  *
754  * Show the test_dummy_encryption mount option, if it was specified.
755  * This is mainly used for /proc/mounts.
756  */
757 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
758 					struct super_block *sb)
759 {
760 	const union fscrypt_context *ctx = fscrypt_get_dummy_context(sb);
761 
762 	if (!ctx)
763 		return;
764 	seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, ctx->version);
765 }
766 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);
767