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