blob: fdb13ce69cd28854432e18e542752ad2fc48c88a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002/*
3 * Encryption policy functions for per-file encryption support.
4 *
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility.
7 *
Eric Biggers5dae4602019-08-04 19:35:47 -07008 * Originally written by Michael Halcrow, 2015.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07009 * Modified by Jaegeuk Kim, 2015.
Eric Biggers5dae4602019-08-04 19:35:47 -070010 * Modified by Eric Biggers, 2019 for v2 policy support.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070011 */
12
13#include <linux/random.h>
14#include <linux/string.h>
Eric Biggersba63f232016-09-08 14:20:38 -070015#include <linux/mount.h>
Theodore Ts'occ4e0df2016-11-26 22:05:18 -050016#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070017
Eric Biggers5dae4602019-08-04 19:35:47 -070018/**
19 * fscrypt_policies_equal - check whether two encryption policies are the same
20 *
21 * Return: %true if equal, else %false
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070022 */
Eric Biggers5dae4602019-08-04 19:35:47 -070023bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
24 const union fscrypt_policy *policy2)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070025{
Eric Biggers5dae4602019-08-04 19:35:47 -070026 if (policy1->version != policy2->version)
27 return false;
28
29 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070030}
31
Eric Biggersb103fb72019-10-24 14:54:36 -070032static bool supported_iv_ino_lblk_64_policy(
33 const struct fscrypt_policy_v2 *policy,
34 const struct inode *inode)
35{
36 struct super_block *sb = inode->i_sb;
37 int ino_bits = 64, lblk_bits = 64;
38
39 if (policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
40 fscrypt_warn(inode,
41 "The DIRECT_KEY and IV_INO_LBLK_64 flags are mutually exclusive");
42 return false;
43 }
44 /*
45 * It's unsafe to include inode numbers in the IVs if the filesystem can
46 * potentially renumber inodes, e.g. via filesystem shrinking.
47 */
48 if (!sb->s_cop->has_stable_inodes ||
49 !sb->s_cop->has_stable_inodes(sb)) {
50 fscrypt_warn(inode,
51 "Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't have stable inode numbers",
52 sb->s_id);
53 return false;
54 }
55 if (sb->s_cop->get_ino_and_lblk_bits)
56 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
57 if (ino_bits > 32 || lblk_bits > 32) {
58 fscrypt_warn(inode,
59 "Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't use 32-bit inode and block numbers",
60 sb->s_id);
61 return false;
62 }
63 return true;
64}
65
Eric Biggers393a24a2019-12-09 13:18:26 -080066static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
67 const struct inode *inode)
68{
69 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
70 policy->filenames_encryption_mode)) {
71 fscrypt_warn(inode,
72 "Unsupported encryption modes (contents %d, filenames %d)",
73 policy->contents_encryption_mode,
74 policy->filenames_encryption_mode);
75 return false;
76 }
77
78 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
79 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
80 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
81 policy->flags);
82 return false;
83 }
84
85 return true;
86}
87
88static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
89 const struct inode *inode)
90{
91 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
92 policy->filenames_encryption_mode)) {
93 fscrypt_warn(inode,
94 "Unsupported encryption modes (contents %d, filenames %d)",
95 policy->contents_encryption_mode,
96 policy->filenames_encryption_mode);
97 return false;
98 }
99
100 if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
101 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
102 policy->flags);
103 return false;
104 }
105
106 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
107 !supported_iv_ino_lblk_64_policy(policy, inode))
108 return false;
109
110 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
111 fscrypt_warn(inode, "Reserved bits set in encryption policy");
112 return false;
113 }
114
115 return true;
116}
117
Eric Biggers5dae4602019-08-04 19:35:47 -0700118/**
119 * fscrypt_supported_policy - check whether an encryption policy is supported
120 *
121 * Given an encryption policy, check whether all its encryption modes and other
Eric Biggers393a24a2019-12-09 13:18:26 -0800122 * settings are supported by this kernel on the given inode. (But we don't
123 * currently don't check for crypto API support here, so attempting to use an
124 * algorithm not configured into the crypto API will still fail later.)
Eric Biggers5dae4602019-08-04 19:35:47 -0700125 *
126 * Return: %true if supported, else %false
127 */
128bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
129 const struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700130{
Eric Biggers5dae4602019-08-04 19:35:47 -0700131 switch (policy_u->version) {
Eric Biggers393a24a2019-12-09 13:18:26 -0800132 case FSCRYPT_POLICY_V1:
133 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
134 case FSCRYPT_POLICY_V2:
135 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
Eric Biggers5dae4602019-08-04 19:35:47 -0700136 }
137 return false;
138}
139
140/**
141 * fscrypt_new_context_from_policy - create a new fscrypt_context from a policy
142 *
143 * Create an fscrypt_context for an inode that is being assigned the given
144 * encryption policy. A new nonce is randomly generated.
145 *
146 * Return: the size of the new context in bytes.
147 */
148static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
149 const union fscrypt_policy *policy_u)
150{
151 memset(ctx_u, 0, sizeof(*ctx_u));
152
153 switch (policy_u->version) {
154 case FSCRYPT_POLICY_V1: {
155 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
156 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
157
158 ctx->version = FSCRYPT_CONTEXT_V1;
159 ctx->contents_encryption_mode =
160 policy->contents_encryption_mode;
161 ctx->filenames_encryption_mode =
162 policy->filenames_encryption_mode;
163 ctx->flags = policy->flags;
164 memcpy(ctx->master_key_descriptor,
165 policy->master_key_descriptor,
166 sizeof(ctx->master_key_descriptor));
167 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
168 return sizeof(*ctx);
169 }
170 case FSCRYPT_POLICY_V2: {
171 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
172 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
173
174 ctx->version = FSCRYPT_CONTEXT_V2;
175 ctx->contents_encryption_mode =
176 policy->contents_encryption_mode;
177 ctx->filenames_encryption_mode =
178 policy->filenames_encryption_mode;
179 ctx->flags = policy->flags;
180 memcpy(ctx->master_key_identifier,
181 policy->master_key_identifier,
182 sizeof(ctx->master_key_identifier));
183 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
184 return sizeof(*ctx);
185 }
186 }
187 BUG();
188}
189
190/**
191 * fscrypt_policy_from_context - convert an fscrypt_context to an fscrypt_policy
192 *
193 * Given an fscrypt_context, build the corresponding fscrypt_policy.
194 *
195 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
196 * version number or size.
197 *
198 * This does *not* validate the settings within the policy itself, e.g. the
199 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
200 */
201int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
202 const union fscrypt_context *ctx_u,
203 int ctx_size)
204{
205 memset(policy_u, 0, sizeof(*policy_u));
206
207 if (ctx_size <= 0 || ctx_size != fscrypt_context_size(ctx_u))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700208 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700209
Eric Biggers5dae4602019-08-04 19:35:47 -0700210 switch (ctx_u->version) {
211 case FSCRYPT_CONTEXT_V1: {
212 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
213 struct fscrypt_policy_v1 *policy = &policy_u->v1;
214
215 policy->version = FSCRYPT_POLICY_V1;
216 policy->contents_encryption_mode =
217 ctx->contents_encryption_mode;
218 policy->filenames_encryption_mode =
219 ctx->filenames_encryption_mode;
220 policy->flags = ctx->flags;
221 memcpy(policy->master_key_descriptor,
222 ctx->master_key_descriptor,
223 sizeof(policy->master_key_descriptor));
224 return 0;
225 }
226 case FSCRYPT_CONTEXT_V2: {
227 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
228 struct fscrypt_policy_v2 *policy = &policy_u->v2;
229
230 policy->version = FSCRYPT_POLICY_V2;
231 policy->contents_encryption_mode =
232 ctx->contents_encryption_mode;
233 policy->filenames_encryption_mode =
234 ctx->filenames_encryption_mode;
235 policy->flags = ctx->flags;
236 memcpy(policy->__reserved, ctx->__reserved,
237 sizeof(policy->__reserved));
238 memcpy(policy->master_key_identifier,
239 ctx->master_key_identifier,
240 sizeof(policy->master_key_identifier));
241 return 0;
242 }
243 }
244 /* unreachable */
245 return -EINVAL;
246}
247
248/* Retrieve an inode's encryption policy */
249static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
250{
251 const struct fscrypt_info *ci;
252 union fscrypt_context ctx;
253 int ret;
254
255 ci = READ_ONCE(inode->i_crypt_info);
256 if (ci) {
257 /* key available, use the cached policy */
258 *policy = ci->ci_policy;
259 return 0;
260 }
261
262 if (!IS_ENCRYPTED(inode))
263 return -ENODATA;
264
265 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
266 if (ret < 0)
267 return (ret == -ERANGE) ? -EINVAL : ret;
268
269 return fscrypt_policy_from_context(policy, &ctx, ret);
270}
271
272static int set_encryption_policy(struct inode *inode,
273 const union fscrypt_policy *policy)
274{
275 union fscrypt_context ctx;
276 int ctxsize;
Eric Biggers5ab71892019-08-04 19:35:48 -0700277 int err;
Eric Biggers5dae4602019-08-04 19:35:47 -0700278
279 if (!fscrypt_supported_policy(policy, inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280 return -EINVAL;
281
Eric Biggers5ab71892019-08-04 19:35:48 -0700282 switch (policy->version) {
283 case FSCRYPT_POLICY_V1:
Eric Biggers5dae4602019-08-04 19:35:47 -0700284 /*
285 * The original encryption policy version provided no way of
286 * verifying that the correct master key was supplied, which was
287 * insecure in scenarios where multiple users have access to the
288 * same encrypted files (even just read-only access). The new
289 * encryption policy version fixes this and also implies use of
290 * an improved key derivation function and allows non-root users
291 * to securely remove keys. So as long as compatibility with
292 * old kernels isn't required, it is recommended to use the new
293 * policy version for all new encrypted directories.
294 */
295 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
296 current->comm, current->pid);
Eric Biggers5ab71892019-08-04 19:35:48 -0700297 break;
298 case FSCRYPT_POLICY_V2:
299 err = fscrypt_verify_key_added(inode->i_sb,
300 policy->v2.master_key_identifier);
301 if (err)
302 return err;
303 break;
304 default:
305 WARN_ON(1);
306 return -EINVAL;
Eric Biggers5dae4602019-08-04 19:35:47 -0700307 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700308
Eric Biggers5dae4602019-08-04 19:35:47 -0700309 ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
310
311 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700312}
313
Eric Biggersdb717d82016-11-26 19:07:49 -0500314int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700315{
Eric Biggers5dae4602019-08-04 19:35:47 -0700316 union fscrypt_policy policy;
317 union fscrypt_policy existing_policy;
Eric Biggersba63f232016-09-08 14:20:38 -0700318 struct inode *inode = file_inode(filp);
Eric Biggers5dae4602019-08-04 19:35:47 -0700319 u8 version;
320 int size;
Eric Biggersba63f232016-09-08 14:20:38 -0700321 int ret;
322
Eric Biggers5dae4602019-08-04 19:35:47 -0700323 if (get_user(policy.version, (const u8 __user *)arg))
Eric Biggersdb717d82016-11-26 19:07:49 -0500324 return -EFAULT;
325
Eric Biggers5dae4602019-08-04 19:35:47 -0700326 size = fscrypt_policy_size(&policy);
327 if (size <= 0)
328 return -EINVAL;
329
330 /*
331 * We should just copy the remaining 'size - 1' bytes here, but a
332 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
333 * think that size can be 0 here (despite the check above!) *and* that
334 * it's a compile-time constant. Thus it would think copy_from_user()
335 * is passed compile-time constant ULONG_MAX, causing the compile-time
336 * buffer overflow check to fail, breaking the build. This only occurred
337 * when building an i386 kernel with -Os and branch profiling enabled.
338 *
339 * Work around it by just copying the first byte again...
340 */
341 version = policy.version;
342 if (copy_from_user(&policy, arg, size))
343 return -EFAULT;
344 policy.version = version;
345
Eric Biggers163ae1c2016-09-08 10:57:08 -0700346 if (!inode_owner_or_capable(inode))
347 return -EACCES;
348
Eric Biggersba63f232016-09-08 14:20:38 -0700349 ret = mnt_want_write_file(filp);
350 if (ret)
351 return ret;
352
Eric Biggers8906a822016-10-15 09:48:50 -0400353 inode_lock(inode);
354
Eric Biggers5dae4602019-08-04 19:35:47 -0700355 ret = fscrypt_get_policy(inode, &existing_policy);
Eric Biggersefee590e2016-12-05 11:12:48 -0800356 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -0700357 if (!S_ISDIR(inode->i_mode))
Eric Biggersdffd0cf2016-12-05 11:12:45 -0800358 ret = -ENOTDIR;
Hongjie Fang5858bda2019-05-22 10:02:53 +0800359 else if (IS_DEADDIR(inode))
360 ret = -ENOENT;
Eric Biggersba63f232016-09-08 14:20:38 -0700361 else if (!inode->i_sb->s_cop->empty_dir(inode))
362 ret = -ENOTEMPTY;
363 else
Eric Biggers5dae4602019-08-04 19:35:47 -0700364 ret = set_encryption_policy(inode, &policy);
365 } else if (ret == -EINVAL ||
366 (ret == 0 && !fscrypt_policies_equal(&policy,
367 &existing_policy))) {
Eric Biggersefee590e2016-12-05 11:12:48 -0800368 /* The file already uses a different encryption policy. */
Eric Biggers8488cd92016-12-05 11:12:46 -0800369 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700370 }
371
Eric Biggers8906a822016-10-15 09:48:50 -0400372 inode_unlock(inode);
373
Eric Biggersba63f232016-09-08 14:20:38 -0700374 mnt_drop_write_file(filp);
375 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700376}
Eric Biggersdb717d82016-11-26 19:07:49 -0500377EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700378
Eric Biggers5dae4602019-08-04 19:35:47 -0700379/* Original ioctl version; can only get the original policy version */
Eric Biggersdb717d82016-11-26 19:07:49 -0500380int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700381{
Eric Biggers5dae4602019-08-04 19:35:47 -0700382 union fscrypt_policy policy;
383 int err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700384
Eric Biggers5dae4602019-08-04 19:35:47 -0700385 err = fscrypt_get_policy(file_inode(filp), &policy);
386 if (err)
387 return err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700388
Eric Biggers5dae4602019-08-04 19:35:47 -0700389 if (policy.version != FSCRYPT_POLICY_V1)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700390 return -EINVAL;
391
Eric Biggers5dae4602019-08-04 19:35:47 -0700392 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
Eric Biggersdb717d82016-11-26 19:07:49 -0500393 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700394 return 0;
395}
Eric Biggersdb717d82016-11-26 19:07:49 -0500396EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700397
Eric Biggers5dae4602019-08-04 19:35:47 -0700398/* Extended ioctl version; can get policies of any version */
399int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
400{
401 struct fscrypt_get_policy_ex_arg arg;
402 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
403 size_t policy_size;
404 int err;
405
406 /* arg is policy_size, then policy */
407 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
408 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
409 offsetof(typeof(arg), policy));
410 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
411
412 err = fscrypt_get_policy(file_inode(filp), policy);
413 if (err)
414 return err;
415 policy_size = fscrypt_policy_size(policy);
416
417 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
418 return -EFAULT;
419
420 if (policy_size > arg.policy_size)
421 return -EOVERFLOW;
422 arg.policy_size = policy_size;
423
424 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
425 return -EFAULT;
426 return 0;
427}
428EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
429
Eric Biggers272f98f2017-04-07 10:58:37 -0700430/**
431 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
432 * within its directory?
433 *
434 * @parent: inode for parent directory
435 * @child: inode for file being looked up, opened, or linked into @parent
436 *
437 * Filesystems must call this before permitting access to an inode in a
438 * situation where the parent directory is encrypted (either before allowing
439 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
440 * and before any operation that involves linking an inode into an encrypted
441 * directory, including link, rename, and cross rename. It enforces the
442 * constraint that within a given encrypted directory tree, all files use the
443 * same encryption policy. The pre-access check is needed to detect potentially
444 * malicious offline violations of this constraint, while the link and rename
445 * checks are needed to prevent online violations of this constraint.
446 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800447 * Return: 1 if permitted, 0 if forbidden.
Eric Biggers272f98f2017-04-07 10:58:37 -0700448 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700449int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
450{
Eric Biggers5dae4602019-08-04 19:35:47 -0700451 union fscrypt_policy parent_policy, child_policy;
452 int err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700453
Eric Biggers42d97eb2016-12-19 14:20:13 -0800454 /* No restrictions on file types which are never encrypted */
455 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
456 !S_ISLNK(child->i_mode))
457 return 1;
458
Eric Biggers272f98f2017-04-07 10:58:37 -0700459 /* No restrictions if the parent directory is unencrypted */
Eric Biggerse0428a22017-10-09 12:15:36 -0700460 if (!IS_ENCRYPTED(parent))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700461 return 1;
Eric Biggers272f98f2017-04-07 10:58:37 -0700462
463 /* Encrypted directories must not contain unencrypted files */
Eric Biggerse0428a22017-10-09 12:15:36 -0700464 if (!IS_ENCRYPTED(child))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700465 return 0;
Eric Biggers272f98f2017-04-07 10:58:37 -0700466
467 /*
468 * Both parent and child are encrypted, so verify they use the same
469 * encryption policy. Compare the fscrypt_info structs if the keys are
470 * available, otherwise retrieve and compare the fscrypt_contexts.
471 *
472 * Note that the fscrypt_context retrieval will be required frequently
473 * when accessing an encrypted directory tree without the key.
474 * Performance-wise this is not a big deal because we already don't
475 * really optimize for file access without the key (to the extent that
476 * such access is even possible), given that any attempted access
477 * already causes a fscrypt_context retrieval and keyring search.
478 *
479 * In any case, if an unexpected error occurs, fall back to "forbidden".
480 */
481
Eric Biggers5dae4602019-08-04 19:35:47 -0700482 err = fscrypt_get_encryption_info(parent);
483 if (err)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700484 return 0;
Eric Biggers5dae4602019-08-04 19:35:47 -0700485 err = fscrypt_get_encryption_info(child);
486 if (err)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700487 return 0;
488
Eric Biggers5dae4602019-08-04 19:35:47 -0700489 err = fscrypt_get_policy(parent, &parent_policy);
490 if (err)
Eric Biggers272f98f2017-04-07 10:58:37 -0700491 return 0;
492
Eric Biggers5dae4602019-08-04 19:35:47 -0700493 err = fscrypt_get_policy(child, &child_policy);
494 if (err)
495 return 0;
496
497 return fscrypt_policies_equal(&parent_policy, &child_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700498}
499EXPORT_SYMBOL(fscrypt_has_permitted_context);
500
501/**
502 * fscrypt_inherit_context() - Sets a child context from its parent
503 * @parent: Parent inode from which the context is inherited.
504 * @child: Child inode that inherits the context from @parent.
505 * @fs_data: private data given by FS.
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500506 * @preload: preload child i_crypt_info if true
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700507 *
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500508 * Return: 0 on success, -errno on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700509 */
510int fscrypt_inherit_context(struct inode *parent, struct inode *child,
511 void *fs_data, bool preload)
512{
Eric Biggers5dae4602019-08-04 19:35:47 -0700513 union fscrypt_context ctx;
514 int ctxsize;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700515 struct fscrypt_info *ci;
516 int res;
517
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700518 res = fscrypt_get_encryption_info(parent);
519 if (res < 0)
520 return res;
521
Eric Biggerse37a7842019-04-11 14:32:15 -0700522 ci = READ_ONCE(parent->i_crypt_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700523 if (ci == NULL)
524 return -ENOKEY;
525
Eric Biggers5dae4602019-08-04 19:35:47 -0700526 ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
527
Tahsin Erdoganaf652072017-07-06 00:01:59 -0400528 BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
Eric Biggers5dae4602019-08-04 19:35:47 -0700529 res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700530 if (res)
531 return res;
532 return preload ? fscrypt_get_encryption_info(child): 0;
533}
534EXPORT_SYMBOL(fscrypt_inherit_context);