xref: /openbmc/linux/fs/crypto/policy.c (revision 70a59dd8)
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 const union fscrypt_policy *
36 fscrypt_get_dummy_policy(struct super_block *sb)
37 {
38 	if (!sb->s_cop->get_dummy_policy)
39 		return NULL;
40 	return sb->s_cop->get_dummy_policy(sb);
41 }
42 
43 static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
44 {
45 	if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
46 	    filenames_mode == FSCRYPT_MODE_AES_256_CTS)
47 		return true;
48 
49 	if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
50 	    filenames_mode == FSCRYPT_MODE_AES_128_CTS)
51 		return true;
52 
53 	if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
54 	    filenames_mode == FSCRYPT_MODE_ADIANTUM)
55 		return true;
56 
57 	return false;
58 }
59 
60 static bool supported_direct_key_modes(const struct inode *inode,
61 				       u32 contents_mode, u32 filenames_mode)
62 {
63 	const struct fscrypt_mode *mode;
64 
65 	if (contents_mode != filenames_mode) {
66 		fscrypt_warn(inode,
67 			     "Direct key flag not allowed with different contents and filenames modes");
68 		return false;
69 	}
70 	mode = &fscrypt_modes[contents_mode];
71 
72 	if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
73 		fscrypt_warn(inode, "Direct key flag not allowed with %s",
74 			     mode->friendly_name);
75 		return false;
76 	}
77 	return true;
78 }
79 
80 static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
81 					 const struct inode *inode,
82 					 const char *type,
83 					 int max_ino_bits, int max_lblk_bits)
84 {
85 	struct super_block *sb = inode->i_sb;
86 	int ino_bits = 64, lblk_bits = 64;
87 
88 	/*
89 	 * IV_INO_LBLK_* exist only because of hardware limitations, and
90 	 * currently the only known use case for them involves AES-256-XTS.
91 	 * That's also all we test currently.  For these reasons, for now only
92 	 * allow AES-256-XTS here.  This can be relaxed later if a use case for
93 	 * IV_INO_LBLK_* with other encryption modes arises.
94 	 */
95 	if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
96 		fscrypt_warn(inode,
97 			     "Can't use %s policy with contents mode other than AES-256-XTS",
98 			     type);
99 		return false;
100 	}
101 
102 	/*
103 	 * It's unsafe to include inode numbers in the IVs if the filesystem can
104 	 * potentially renumber inodes, e.g. via filesystem shrinking.
105 	 */
106 	if (!sb->s_cop->has_stable_inodes ||
107 	    !sb->s_cop->has_stable_inodes(sb)) {
108 		fscrypt_warn(inode,
109 			     "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
110 			     type, sb->s_id);
111 		return false;
112 	}
113 	if (sb->s_cop->get_ino_and_lblk_bits)
114 		sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
115 	if (ino_bits > max_ino_bits) {
116 		fscrypt_warn(inode,
117 			     "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
118 			     type, sb->s_id);
119 		return false;
120 	}
121 	if (lblk_bits > max_lblk_bits) {
122 		fscrypt_warn(inode,
123 			     "Can't use %s policy on filesystem '%s' because its block numbers are too long",
124 			     type, sb->s_id);
125 		return false;
126 	}
127 	return true;
128 }
129 
130 static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
131 					const struct inode *inode)
132 {
133 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
134 				     policy->filenames_encryption_mode)) {
135 		fscrypt_warn(inode,
136 			     "Unsupported encryption modes (contents %d, filenames %d)",
137 			     policy->contents_encryption_mode,
138 			     policy->filenames_encryption_mode);
139 		return false;
140 	}
141 
142 	if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
143 			      FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
144 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
145 			     policy->flags);
146 		return false;
147 	}
148 
149 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
150 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
151 					policy->filenames_encryption_mode))
152 		return false;
153 
154 	if (IS_CASEFOLDED(inode)) {
155 		/* With v1, there's no way to derive dirhash keys. */
156 		fscrypt_warn(inode,
157 			     "v1 policies can't be used on casefolded directories");
158 		return false;
159 	}
160 
161 	return true;
162 }
163 
164 static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
165 					const struct inode *inode)
166 {
167 	int count = 0;
168 
169 	if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
170 				     policy->filenames_encryption_mode)) {
171 		fscrypt_warn(inode,
172 			     "Unsupported encryption modes (contents %d, filenames %d)",
173 			     policy->contents_encryption_mode,
174 			     policy->filenames_encryption_mode);
175 		return false;
176 	}
177 
178 	if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
179 		fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
180 			     policy->flags);
181 		return false;
182 	}
183 
184 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
185 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
186 	count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
187 	if (count > 1) {
188 		fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
189 			     policy->flags);
190 		return false;
191 	}
192 
193 	if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
194 	    !supported_direct_key_modes(inode, policy->contents_encryption_mode,
195 					policy->filenames_encryption_mode))
196 		return false;
197 
198 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
199 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
200 					  32, 32))
201 		return false;
202 
203 	/*
204 	 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
205 	 * support any ino_bits.  However, currently the inode number is gotten
206 	 * from inode::i_ino which is 'unsigned long'.  So for now the
207 	 * implementation limit is 32 bits.
208 	 */
209 	if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
210 	    !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
211 					  32, 32))
212 		return false;
213 
214 	if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
215 		fscrypt_warn(inode, "Reserved bits set in encryption policy");
216 		return false;
217 	}
218 
219 	return true;
220 }
221 
222 /**
223  * fscrypt_supported_policy() - check whether an encryption policy is supported
224  * @policy_u: the encryption policy
225  * @inode: the inode on which the policy will be used
226  *
227  * Given an encryption policy, check whether all its encryption modes and other
228  * settings are supported by this kernel on the given inode.  (But we don't
229  * currently don't check for crypto API support here, so attempting to use an
230  * algorithm not configured into the crypto API will still fail later.)
231  *
232  * Return: %true if supported, else %false
233  */
234 bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
235 			      const struct inode *inode)
236 {
237 	switch (policy_u->version) {
238 	case FSCRYPT_POLICY_V1:
239 		return fscrypt_supported_v1_policy(&policy_u->v1, inode);
240 	case FSCRYPT_POLICY_V2:
241 		return fscrypt_supported_v2_policy(&policy_u->v2, inode);
242 	}
243 	return false;
244 }
245 
246 /**
247  * fscrypt_new_context() - create a new fscrypt_context
248  * @ctx_u: output context
249  * @policy_u: input policy
250  * @nonce: nonce to use
251  *
252  * Create an fscrypt_context for an inode that is being assigned the given
253  * encryption policy.  @nonce must be a new random nonce.
254  *
255  * Return: the size of the new context in bytes.
256  */
257 static int fscrypt_new_context(union fscrypt_context *ctx_u,
258 			       const union fscrypt_policy *policy_u,
259 			       const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
260 {
261 	memset(ctx_u, 0, sizeof(*ctx_u));
262 
263 	switch (policy_u->version) {
264 	case FSCRYPT_POLICY_V1: {
265 		const struct fscrypt_policy_v1 *policy = &policy_u->v1;
266 		struct fscrypt_context_v1 *ctx = &ctx_u->v1;
267 
268 		ctx->version = FSCRYPT_CONTEXT_V1;
269 		ctx->contents_encryption_mode =
270 			policy->contents_encryption_mode;
271 		ctx->filenames_encryption_mode =
272 			policy->filenames_encryption_mode;
273 		ctx->flags = policy->flags;
274 		memcpy(ctx->master_key_descriptor,
275 		       policy->master_key_descriptor,
276 		       sizeof(ctx->master_key_descriptor));
277 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
278 		return sizeof(*ctx);
279 	}
280 	case FSCRYPT_POLICY_V2: {
281 		const struct fscrypt_policy_v2 *policy = &policy_u->v2;
282 		struct fscrypt_context_v2 *ctx = &ctx_u->v2;
283 
284 		ctx->version = FSCRYPT_CONTEXT_V2;
285 		ctx->contents_encryption_mode =
286 			policy->contents_encryption_mode;
287 		ctx->filenames_encryption_mode =
288 			policy->filenames_encryption_mode;
289 		ctx->flags = policy->flags;
290 		memcpy(ctx->master_key_identifier,
291 		       policy->master_key_identifier,
292 		       sizeof(ctx->master_key_identifier));
293 		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
294 		return sizeof(*ctx);
295 	}
296 	}
297 	BUG();
298 }
299 
300 /**
301  * fscrypt_policy_from_context() - convert an fscrypt_context to
302  *				   an fscrypt_policy
303  * @policy_u: output policy
304  * @ctx_u: input context
305  * @ctx_size: size of input context in bytes
306  *
307  * Given an fscrypt_context, build the corresponding fscrypt_policy.
308  *
309  * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
310  * version number or size.
311  *
312  * This does *not* validate the settings within the policy itself, e.g. the
313  * modes, flags, and reserved bits.  Use fscrypt_supported_policy() for that.
314  */
315 int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
316 				const union fscrypt_context *ctx_u,
317 				int ctx_size)
318 {
319 	memset(policy_u, 0, sizeof(*policy_u));
320 
321 	if (!fscrypt_context_is_valid(ctx_u, ctx_size))
322 		return -EINVAL;
323 
324 	switch (ctx_u->version) {
325 	case FSCRYPT_CONTEXT_V1: {
326 		const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
327 		struct fscrypt_policy_v1 *policy = &policy_u->v1;
328 
329 		policy->version = FSCRYPT_POLICY_V1;
330 		policy->contents_encryption_mode =
331 			ctx->contents_encryption_mode;
332 		policy->filenames_encryption_mode =
333 			ctx->filenames_encryption_mode;
334 		policy->flags = ctx->flags;
335 		memcpy(policy->master_key_descriptor,
336 		       ctx->master_key_descriptor,
337 		       sizeof(policy->master_key_descriptor));
338 		return 0;
339 	}
340 	case FSCRYPT_CONTEXT_V2: {
341 		const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
342 		struct fscrypt_policy_v2 *policy = &policy_u->v2;
343 
344 		policy->version = FSCRYPT_POLICY_V2;
345 		policy->contents_encryption_mode =
346 			ctx->contents_encryption_mode;
347 		policy->filenames_encryption_mode =
348 			ctx->filenames_encryption_mode;
349 		policy->flags = ctx->flags;
350 		memcpy(policy->__reserved, ctx->__reserved,
351 		       sizeof(policy->__reserved));
352 		memcpy(policy->master_key_identifier,
353 		       ctx->master_key_identifier,
354 		       sizeof(policy->master_key_identifier));
355 		return 0;
356 	}
357 	}
358 	/* unreachable */
359 	return -EINVAL;
360 }
361 
362 /* Retrieve an inode's encryption policy */
363 static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
364 {
365 	const struct fscrypt_info *ci;
366 	union fscrypt_context ctx;
367 	int ret;
368 
369 	ci = fscrypt_get_info(inode);
370 	if (ci) {
371 		/* key available, use the cached policy */
372 		*policy = ci->ci_policy;
373 		return 0;
374 	}
375 
376 	if (!IS_ENCRYPTED(inode))
377 		return -ENODATA;
378 
379 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
380 	if (ret < 0)
381 		return (ret == -ERANGE) ? -EINVAL : ret;
382 
383 	return fscrypt_policy_from_context(policy, &ctx, ret);
384 }
385 
386 static int set_encryption_policy(struct inode *inode,
387 				 const union fscrypt_policy *policy)
388 {
389 	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
390 	union fscrypt_context ctx;
391 	int ctxsize;
392 	int err;
393 
394 	if (!fscrypt_supported_policy(policy, inode))
395 		return -EINVAL;
396 
397 	switch (policy->version) {
398 	case FSCRYPT_POLICY_V1:
399 		/*
400 		 * The original encryption policy version provided no way of
401 		 * verifying that the correct master key was supplied, which was
402 		 * insecure in scenarios where multiple users have access to the
403 		 * same encrypted files (even just read-only access).  The new
404 		 * encryption policy version fixes this and also implies use of
405 		 * an improved key derivation function and allows non-root users
406 		 * to securely remove keys.  So as long as compatibility with
407 		 * old kernels isn't required, it is recommended to use the new
408 		 * policy version for all new encrypted directories.
409 		 */
410 		pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
411 			     current->comm, current->pid);
412 		break;
413 	case FSCRYPT_POLICY_V2:
414 		err = fscrypt_verify_key_added(inode->i_sb,
415 					       policy->v2.master_key_identifier);
416 		if (err)
417 			return err;
418 		if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
419 			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",
420 				     current->comm, current->pid);
421 		break;
422 	default:
423 		WARN_ON(1);
424 		return -EINVAL;
425 	}
426 
427 	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
428 	ctxsize = fscrypt_new_context(&ctx, policy, nonce);
429 
430 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
431 }
432 
433 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
434 {
435 	union fscrypt_policy policy;
436 	union fscrypt_policy existing_policy;
437 	struct inode *inode = file_inode(filp);
438 	u8 version;
439 	int size;
440 	int ret;
441 
442 	if (get_user(policy.version, (const u8 __user *)arg))
443 		return -EFAULT;
444 
445 	size = fscrypt_policy_size(&policy);
446 	if (size <= 0)
447 		return -EINVAL;
448 
449 	/*
450 	 * We should just copy the remaining 'size - 1' bytes here, but a
451 	 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
452 	 * think that size can be 0 here (despite the check above!) *and* that
453 	 * it's a compile-time constant.  Thus it would think copy_from_user()
454 	 * is passed compile-time constant ULONG_MAX, causing the compile-time
455 	 * buffer overflow check to fail, breaking the build. This only occurred
456 	 * when building an i386 kernel with -Os and branch profiling enabled.
457 	 *
458 	 * Work around it by just copying the first byte again...
459 	 */
460 	version = policy.version;
461 	if (copy_from_user(&policy, arg, size))
462 		return -EFAULT;
463 	policy.version = version;
464 
465 	if (!inode_owner_or_capable(inode))
466 		return -EACCES;
467 
468 	ret = mnt_want_write_file(filp);
469 	if (ret)
470 		return ret;
471 
472 	inode_lock(inode);
473 
474 	ret = fscrypt_get_policy(inode, &existing_policy);
475 	if (ret == -ENODATA) {
476 		if (!S_ISDIR(inode->i_mode))
477 			ret = -ENOTDIR;
478 		else if (IS_DEADDIR(inode))
479 			ret = -ENOENT;
480 		else if (!inode->i_sb->s_cop->empty_dir(inode))
481 			ret = -ENOTEMPTY;
482 		else
483 			ret = set_encryption_policy(inode, &policy);
484 	} else if (ret == -EINVAL ||
485 		   (ret == 0 && !fscrypt_policies_equal(&policy,
486 							&existing_policy))) {
487 		/* The file already uses a different encryption policy. */
488 		ret = -EEXIST;
489 	}
490 
491 	inode_unlock(inode);
492 
493 	mnt_drop_write_file(filp);
494 	return ret;
495 }
496 EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
497 
498 /* Original ioctl version; can only get the original policy version */
499 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
500 {
501 	union fscrypt_policy policy;
502 	int err;
503 
504 	err = fscrypt_get_policy(file_inode(filp), &policy);
505 	if (err)
506 		return err;
507 
508 	if (policy.version != FSCRYPT_POLICY_V1)
509 		return -EINVAL;
510 
511 	if (copy_to_user(arg, &policy, sizeof(policy.v1)))
512 		return -EFAULT;
513 	return 0;
514 }
515 EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
516 
517 /* Extended ioctl version; can get policies of any version */
518 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
519 {
520 	struct fscrypt_get_policy_ex_arg arg;
521 	union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
522 	size_t policy_size;
523 	int err;
524 
525 	/* arg is policy_size, then policy */
526 	BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
527 	BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
528 		     offsetof(typeof(arg), policy));
529 	BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
530 
531 	err = fscrypt_get_policy(file_inode(filp), policy);
532 	if (err)
533 		return err;
534 	policy_size = fscrypt_policy_size(policy);
535 
536 	if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
537 		return -EFAULT;
538 
539 	if (policy_size > arg.policy_size)
540 		return -EOVERFLOW;
541 	arg.policy_size = policy_size;
542 
543 	if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
544 		return -EFAULT;
545 	return 0;
546 }
547 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
548 
549 /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
550 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
551 {
552 	struct inode *inode = file_inode(filp);
553 	union fscrypt_context ctx;
554 	int ret;
555 
556 	ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
557 	if (ret < 0)
558 		return ret;
559 	if (!fscrypt_context_is_valid(&ctx, ret))
560 		return -EINVAL;
561 	if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
562 			 FSCRYPT_FILE_NONCE_SIZE))
563 		return -EFAULT;
564 	return 0;
565 }
566 EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
567 
568 /**
569  * fscrypt_has_permitted_context() - is a file's encryption policy permitted
570  *				     within its directory?
571  *
572  * @parent: inode for parent directory
573  * @child: inode for file being looked up, opened, or linked into @parent
574  *
575  * Filesystems must call this before permitting access to an inode in a
576  * situation where the parent directory is encrypted (either before allowing
577  * ->lookup() to succeed, or for a regular file before allowing it to be opened)
578  * and before any operation that involves linking an inode into an encrypted
579  * directory, including link, rename, and cross rename.  It enforces the
580  * constraint that within a given encrypted directory tree, all files use the
581  * same encryption policy.  The pre-access check is needed to detect potentially
582  * malicious offline violations of this constraint, while the link and rename
583  * checks are needed to prevent online violations of this constraint.
584  *
585  * Return: 1 if permitted, 0 if forbidden.
586  */
587 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
588 {
589 	union fscrypt_policy parent_policy, child_policy;
590 	int err;
591 
592 	/* No restrictions on file types which are never encrypted */
593 	if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
594 	    !S_ISLNK(child->i_mode))
595 		return 1;
596 
597 	/* No restrictions if the parent directory is unencrypted */
598 	if (!IS_ENCRYPTED(parent))
599 		return 1;
600 
601 	/* Encrypted directories must not contain unencrypted files */
602 	if (!IS_ENCRYPTED(child))
603 		return 0;
604 
605 	/*
606 	 * Both parent and child are encrypted, so verify they use the same
607 	 * encryption policy.  Compare the fscrypt_info structs if the keys are
608 	 * available, otherwise retrieve and compare the fscrypt_contexts.
609 	 *
610 	 * Note that the fscrypt_context retrieval will be required frequently
611 	 * when accessing an encrypted directory tree without the key.
612 	 * Performance-wise this is not a big deal because we already don't
613 	 * really optimize for file access without the key (to the extent that
614 	 * such access is even possible), given that any attempted access
615 	 * already causes a fscrypt_context retrieval and keyring search.
616 	 *
617 	 * In any case, if an unexpected error occurs, fall back to "forbidden".
618 	 */
619 
620 	err = fscrypt_get_encryption_info(parent);
621 	if (err)
622 		return 0;
623 	err = fscrypt_get_encryption_info(child);
624 	if (err)
625 		return 0;
626 
627 	err = fscrypt_get_policy(parent, &parent_policy);
628 	if (err)
629 		return 0;
630 
631 	err = fscrypt_get_policy(child, &child_policy);
632 	if (err)
633 		return 0;
634 
635 	return fscrypt_policies_equal(&parent_policy, &child_policy);
636 }
637 EXPORT_SYMBOL(fscrypt_has_permitted_context);
638 
639 /*
640  * Return the encryption policy that new files in the directory will inherit, or
641  * NULL if none, or an ERR_PTR() on error.  If the directory is encrypted, also
642  * ensure that its key is set up, so that the new filename can be encrypted.
643  */
644 const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
645 {
646 	int err;
647 
648 	if (IS_ENCRYPTED(dir)) {
649 		err = fscrypt_require_key(dir);
650 		if (err)
651 			return ERR_PTR(err);
652 		return &dir->i_crypt_info->ci_policy;
653 	}
654 
655 	return fscrypt_get_dummy_policy(dir->i_sb);
656 }
657 
658 /**
659  * fscrypt_set_context() - Set the fscrypt context of a new inode
660  * @inode: a new inode
661  * @fs_data: private data given by FS and passed to ->set_context()
662  *
663  * This should be called after fscrypt_prepare_new_inode(), generally during a
664  * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
665  *
666  * Return: 0 on success, -errno on failure
667  */
668 int fscrypt_set_context(struct inode *inode, void *fs_data)
669 {
670 	struct fscrypt_info *ci = inode->i_crypt_info;
671 	union fscrypt_context ctx;
672 	int ctxsize;
673 
674 	/* fscrypt_prepare_new_inode() should have set up the key already. */
675 	if (WARN_ON_ONCE(!ci))
676 		return -ENOKEY;
677 
678 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
679 	ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
680 
681 	/*
682 	 * This may be the first time the inode number is available, so do any
683 	 * delayed key setup that requires the inode number.
684 	 */
685 	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
686 	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
687 		const struct fscrypt_master_key *mk =
688 			ci->ci_master_key->payload.data[0];
689 
690 		fscrypt_hash_inode_number(ci, mk);
691 	}
692 
693 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
694 }
695 EXPORT_SYMBOL_GPL(fscrypt_set_context);
696 
697 /**
698  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
699  * @sb: the filesystem on which test_dummy_encryption is being specified
700  * @arg: the argument to the test_dummy_encryption option.  May be NULL.
701  * @dummy_policy: the filesystem's current dummy policy (input/output, see
702  *		  below)
703  *
704  * Handle the test_dummy_encryption mount option by creating a dummy encryption
705  * policy, saving it in @dummy_policy, and adding the corresponding dummy
706  * encryption key to the filesystem.  If the @dummy_policy is already set, then
707  * instead validate that it matches @arg.  Don't support changing it via
708  * remount, as that is difficult to do safely.
709  *
710  * Return: 0 on success (dummy policy set, or the same policy is already set);
711  *         -EEXIST if a different dummy policy is already set;
712  *         or another -errno value.
713  */
714 int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
715 				      struct fscrypt_dummy_policy *dummy_policy)
716 {
717 	struct fscrypt_key_specifier key_spec = { 0 };
718 	int version;
719 	union fscrypt_policy *policy = NULL;
720 	int err;
721 
722 	if (!arg)
723 		arg = "v2";
724 
725 	if (!strcmp(arg, "v1")) {
726 		version = FSCRYPT_POLICY_V1;
727 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
728 		memset(key_spec.u.descriptor, 0x42,
729 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
730 	} else if (!strcmp(arg, "v2")) {
731 		version = FSCRYPT_POLICY_V2;
732 		key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
733 		/* key_spec.u.identifier gets filled in when adding the key */
734 	} else {
735 		err = -EINVAL;
736 		goto out;
737 	}
738 
739 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
740 	if (!policy) {
741 		err = -ENOMEM;
742 		goto out;
743 	}
744 
745 	err = fscrypt_add_test_dummy_key(sb, &key_spec);
746 	if (err)
747 		goto out;
748 
749 	policy->version = version;
750 	switch (policy->version) {
751 	case FSCRYPT_POLICY_V1:
752 		policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
753 		policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
754 		memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
755 		       FSCRYPT_KEY_DESCRIPTOR_SIZE);
756 		break;
757 	case FSCRYPT_POLICY_V2:
758 		policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
759 		policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
760 		memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
761 		       FSCRYPT_KEY_IDENTIFIER_SIZE);
762 		break;
763 	default:
764 		WARN_ON(1);
765 		err = -EINVAL;
766 		goto out;
767 	}
768 
769 	if (dummy_policy->policy) {
770 		if (fscrypt_policies_equal(policy, dummy_policy->policy))
771 			err = 0;
772 		else
773 			err = -EEXIST;
774 		goto out;
775 	}
776 	dummy_policy->policy = policy;
777 	policy = NULL;
778 	err = 0;
779 out:
780 	kfree(policy);
781 	return err;
782 }
783 EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
784 
785 /**
786  * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
787  * @seq: the seq_file to print the option to
788  * @sep: the separator character to use
789  * @sb: the filesystem whose options are being shown
790  *
791  * Show the test_dummy_encryption mount option, if it was specified.
792  * This is mainly used for /proc/mounts.
793  */
794 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
795 					struct super_block *sb)
796 {
797 	const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
798 	int vers;
799 
800 	if (!policy)
801 		return;
802 
803 	vers = policy->version;
804 	if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
805 		vers = 1;
806 
807 	seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
808 }
809 EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);
810