1 // SPDX-License-Identifier: GPL-2.0
3 * Encryption policy functions for per-file encryption support.
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility.
8 * Originally written by Michael Halcrow, 2015.
9 * Modified by Jaegeuk Kim, 2015.
10 * Modified by Eric Biggers, 2019 for v2 policy support.
13 * Linux commit 219d54332a09
17 #include <linux/random.h>
18 #include <linux/string.h>
19 #include <linux/mount.h>
20 #include <lustre_compat.h>
21 #include "llcrypt_private.h"
24 * llcrypt_policies_equal - check whether two encryption policies are the same
26 * Return: %true if equal, else %false
28 bool llcrypt_policies_equal(const union llcrypt_policy *policy1,
29 const union llcrypt_policy *policy2)
31 if (policy1->version != policy2->version)
34 return !memcmp(policy1, policy2, llcrypt_policy_size(policy1));
38 * llcrypt_supported_policy - check whether an encryption policy is supported
40 * Given an encryption policy, check whether all its encryption modes and other
41 * settings are supported by this kernel. (But we don't currently don't check
42 * for crypto API support here, so attempting to use an algorithm not configured
43 * into the crypto API will still fail later.)
45 * Return: %true if supported, else %false
47 bool llcrypt_supported_policy(const union llcrypt_policy *policy_u,
48 const struct inode *inode)
50 switch (policy_u->version) {
51 case LLCRYPT_POLICY_V1: {
52 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
54 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
55 policy->filenames_encryption_mode)) {
57 "Unsupported encryption modes (contents %d, filenames %d)",
58 policy->contents_encryption_mode,
59 policy->filenames_encryption_mode);
63 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
65 "Unsupported encryption flags (0x%02x)",
72 case LLCRYPT_POLICY_V2: {
73 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
75 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
76 policy->filenames_encryption_mode)) {
78 "Unsupported encryption modes (contents %d, filenames %d)",
79 policy->contents_encryption_mode,
80 policy->filenames_encryption_mode);
84 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
86 "Unsupported encryption flags (0x%02x)",
91 if (memchr_inv(policy->__reserved, 0,
92 sizeof(policy->__reserved))) {
94 "Reserved bits set in encryption policy");
105 * llcrypt_new_context_from_policy - create a new llcrypt_context from a policy
107 * Create an llcrypt_context for an inode that is being assigned the given
108 * encryption policy. A new nonce is randomly generated.
110 * Return: the size of the new context in bytes.
112 static int llcrypt_new_context_from_policy(union llcrypt_context *ctx_u,
113 const union llcrypt_policy *policy_u)
115 memset(ctx_u, 0, sizeof(*ctx_u));
117 switch (policy_u->version) {
118 case LLCRYPT_POLICY_V1: {
119 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
120 struct llcrypt_context_v1 *ctx = &ctx_u->v1;
122 ctx->version = LLCRYPT_CONTEXT_V1;
123 ctx->contents_encryption_mode =
124 policy->contents_encryption_mode;
125 ctx->filenames_encryption_mode =
126 policy->filenames_encryption_mode;
127 ctx->flags = policy->flags;
128 memcpy(ctx->master_key_descriptor,
129 policy->master_key_descriptor,
130 sizeof(ctx->master_key_descriptor));
131 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
134 case LLCRYPT_POLICY_V2: {
135 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
136 struct llcrypt_context_v2 *ctx = &ctx_u->v2;
138 ctx->version = LLCRYPT_CONTEXT_V2;
139 ctx->contents_encryption_mode =
140 policy->contents_encryption_mode;
141 ctx->filenames_encryption_mode =
142 policy->filenames_encryption_mode;
143 ctx->flags = policy->flags;
144 memcpy(ctx->master_key_identifier,
145 policy->master_key_identifier,
146 sizeof(ctx->master_key_identifier));
147 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
155 * llcrypt_policy_from_context - convert an llcrypt_context to an llcrypt_policy
157 * Given an llcrypt_context, build the corresponding llcrypt_policy.
159 * Return: 0 on success, or -EINVAL if the llcrypt_context has an unrecognized
160 * version number or size.
162 * This does *not* validate the settings within the policy itself, e.g. the
163 * modes, flags, and reserved bits. Use llcrypt_supported_policy() for that.
165 int llcrypt_policy_from_context(union llcrypt_policy *policy_u,
166 const union llcrypt_context *ctx_u,
169 memset(policy_u, 0, sizeof(*policy_u));
171 if (ctx_size <= 0 || ctx_size != llcrypt_context_size(ctx_u))
174 switch (ctx_u->version) {
175 case LLCRYPT_CONTEXT_V1: {
176 const struct llcrypt_context_v1 *ctx = &ctx_u->v1;
177 struct llcrypt_policy_v1 *policy = &policy_u->v1;
179 policy->version = LLCRYPT_POLICY_V1;
180 policy->contents_encryption_mode =
181 ctx->contents_encryption_mode;
182 policy->filenames_encryption_mode =
183 ctx->filenames_encryption_mode;
184 policy->flags = ctx->flags;
185 memcpy(policy->master_key_descriptor,
186 ctx->master_key_descriptor,
187 sizeof(policy->master_key_descriptor));
190 case LLCRYPT_CONTEXT_V2: {
191 const struct llcrypt_context_v2 *ctx = &ctx_u->v2;
192 struct llcrypt_policy_v2 *policy = &policy_u->v2;
194 policy->version = LLCRYPT_POLICY_V2;
195 policy->contents_encryption_mode =
196 ctx->contents_encryption_mode;
197 policy->filenames_encryption_mode =
198 ctx->filenames_encryption_mode;
199 policy->flags = ctx->flags;
200 memcpy(policy->__reserved, ctx->__reserved,
201 sizeof(policy->__reserved));
202 memcpy(policy->master_key_identifier,
203 ctx->master_key_identifier,
204 sizeof(policy->master_key_identifier));
212 /* Retrieve an inode's encryption policy */
213 static int llcrypt_get_policy(struct inode *inode, union llcrypt_policy *policy)
215 const struct llcrypt_info *ci;
216 union llcrypt_context ctx;
217 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
220 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
222 /* key available, use the cached policy */
223 *policy = ci->ci_policy;
227 if (!IS_ENCRYPTED(inode))
233 ret = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
235 return (ret == -ERANGE) ? -EINVAL : ret;
237 return llcrypt_policy_from_context(policy, &ctx, ret);
240 static int set_encryption_policy(struct inode *inode,
241 const union llcrypt_policy *policy)
243 union llcrypt_context ctx;
245 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
248 if (!llcrypt_supported_policy(policy, inode))
251 switch (policy->version) {
252 case LLCRYPT_POLICY_V1:
254 * The original encryption policy version provided no way of
255 * verifying that the correct master key was supplied, which was
256 * insecure in scenarios where multiple users have access to the
257 * same encrypted files (even just read-only access). The new
258 * encryption policy version fixes this and also implies use of
259 * an improved key derivation function and allows non-root users
260 * to securely remove keys. So as long as compatibility with
261 * old kernels isn't required, it is recommended to use the new
262 * policy version for all new encrypted directories.
264 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
265 current->comm, current->pid);
267 case LLCRYPT_POLICY_V2:
268 err = llcrypt_verify_key_added(inode->i_sb,
269 policy->v2.master_key_identifier);
278 ctxsize = llcrypt_new_context_from_policy(&ctx, policy);
283 return lsi->lsi_cop->set_context(inode, &ctx, ctxsize, NULL);
286 /* Tell if an inode's encryption policy has filename encryption */
287 bool llcrypt_policy_has_filename_enc(struct inode *inode)
289 union llcrypt_policy policy;
292 err = llcrypt_get_policy(inode, &policy);
296 if ((policy.version == LLCRYPT_POLICY_V1 &&
297 policy.v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) ||
298 (policy.version == LLCRYPT_POLICY_V2 &&
299 policy.v2.filenames_encryption_mode == LLCRYPT_MODE_NULL))
303 EXPORT_SYMBOL(llcrypt_policy_has_filename_enc);
305 int llcrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
307 union llcrypt_policy policy;
308 union llcrypt_policy existing_policy;
309 struct inode *inode = file_inode(filp);
314 if (get_user(policy.version, (const u8 __user *)arg))
317 size = llcrypt_policy_size(&policy);
322 * We should just copy the remaining 'size - 1' bytes here, but a
323 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
324 * think that size can be 0 here (despite the check above!) *and* that
325 * it's a compile-time constant. Thus it would think copy_from_user()
326 * is passed compile-time constant ULONG_MAX, causing the compile-time
327 * buffer overflow check to fail, breaking the build. This only occurred
328 * when building an i386 kernel with -Os and branch profiling enabled.
330 * Work around it by just copying the first byte again...
332 version = policy.version;
333 if (copy_from_user(&policy, arg, size))
335 policy.version = version;
337 if (!inode_owner_or_capable(&init_user_ns, inode))
340 ret = mnt_want_write_file(filp);
346 ret = llcrypt_get_policy(inode, &existing_policy);
347 if (ret == -ENODATA) {
348 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
350 if (!S_ISDIR(inode->i_mode))
352 else if (IS_DEADDIR(inode))
354 else if (lsi && !lsi->lsi_cop->empty_dir(inode))
357 ret = set_encryption_policy(inode, &policy);
358 } else if (ret == -EINVAL ||
359 (ret == 0 && !llcrypt_policies_equal(&policy,
360 &existing_policy))) {
361 /* The file already uses a different encryption policy. */
367 mnt_drop_write_file(filp);
370 EXPORT_SYMBOL(llcrypt_ioctl_set_policy);
372 /* Original ioctl version; can only get the original policy version */
373 int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
375 union llcrypt_policy policy;
378 err = llcrypt_get_policy(file_inode(filp), &policy);
382 if (policy.version != LLCRYPT_POLICY_V1)
385 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
389 EXPORT_SYMBOL(llcrypt_ioctl_get_policy);
391 /* Valid filenames_encryption_mode associated with contents_encryption_mode,
392 * as imposed by llcrypt_valid_enc_modes()
394 static inline u8 contents2filenames_encmode(u8 contents_encryption_mode)
396 if (contents_encryption_mode == LLCRYPT_MODE_AES_128_CBC)
397 return LLCRYPT_MODE_AES_128_CTS;
398 if (contents_encryption_mode == LLCRYPT_MODE_AES_256_XTS)
399 return LLCRYPT_MODE_AES_256_CTS;
400 if (contents_encryption_mode == LLCRYPT_MODE_ADIANTUM)
401 return LLCRYPT_MODE_ADIANTUM;
402 return LLCRYPT_MODE_NULL;
405 /* Extended ioctl version; can get policies of any version */
406 int llcrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
408 struct llcrypt_get_policy_ex_arg arg;
409 union llcrypt_policy *policy = (union llcrypt_policy *)&arg.policy;
411 struct inode *inode = file_inode(filp);
414 /* arg is policy_size, then policy */
415 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
416 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
417 offsetof(typeof(arg), policy));
418 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
420 err = llcrypt_get_policy(file_inode(filp), policy);
423 policy_size = llcrypt_policy_size(policy);
425 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
428 if (policy_size > arg.policy_size)
430 arg.policy_size = policy_size;
432 /* Do not return null filenames_encryption_mode to userspace, as it is
433 * unknown. Instead, return valid mode associated with
434 * contents_encryption_mode, as imposed by llcrypt_valid_enc_modes().
436 switch (policy->version) {
437 case LLCRYPT_POLICY_V1:
438 if (policy->v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
439 policy->v1.filenames_encryption_mode =
440 contents2filenames_encmode(
441 policy->v1.contents_encryption_mode);
442 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
444 policy->v1.filenames_encryption_mode);
447 case LLCRYPT_POLICY_V2:
448 if (policy->v2.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
449 policy->v2.filenames_encryption_mode =
450 contents2filenames_encmode(
451 policy->v2.contents_encryption_mode);
452 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
454 policy->v2.filenames_encryption_mode);
459 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
463 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_policy_ex);
466 * llcrypt_has_permitted_context() - is a file's encryption policy permitted
467 * within its directory?
469 * @parent: inode for parent directory
470 * @child: inode for file being looked up, opened, or linked into @parent
472 * Filesystems must call this before permitting access to an inode in a
473 * situation where the parent directory is encrypted (either before allowing
474 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
475 * and before any operation that involves linking an inode into an encrypted
476 * directory, including link, rename, and cross rename. It enforces the
477 * constraint that within a given encrypted directory tree, all files use the
478 * same encryption policy. The pre-access check is needed to detect potentially
479 * malicious offline violations of this constraint, while the link and rename
480 * checks are needed to prevent online violations of this constraint.
482 * Return: 1 if permitted, 0 if forbidden.
484 int llcrypt_has_permitted_context(struct inode *parent, struct inode *child)
486 union llcrypt_policy parent_policy, child_policy;
489 /* No restrictions on file types which are never encrypted */
490 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
491 !S_ISLNK(child->i_mode))
494 /* No restrictions if the parent directory is unencrypted */
495 if (!IS_ENCRYPTED(parent))
498 /* Encrypted directories must not contain unencrypted files */
499 if (!IS_ENCRYPTED(child))
503 * Both parent and child are encrypted, so verify they use the same
504 * encryption policy. Compare the llcrypt_info structs if the keys are
505 * available, otherwise retrieve and compare the llcrypt_contexts.
507 * Note that the llcrypt_context retrieval will be required frequently
508 * when accessing an encrypted directory tree without the key.
509 * Performance-wise this is not a big deal because we already don't
510 * really optimize for file access without the key (to the extent that
511 * such access is even possible), given that any attempted access
512 * already causes a llcrypt_context retrieval and keyring search.
514 * In any case, if an unexpected error occurs, fall back to "forbidden".
517 err = llcrypt_get_encryption_info(parent);
520 err = llcrypt_get_encryption_info(child);
524 err = llcrypt_get_policy(parent, &parent_policy);
528 err = llcrypt_get_policy(child, &child_policy);
532 return llcrypt_policies_equal(&parent_policy, &child_policy);
534 EXPORT_SYMBOL(llcrypt_has_permitted_context);
537 * llcrypt_inherit_context() - Sets a child context from its parent
538 * @parent: Parent inode from which the context is inherited.
539 * @child: Child inode that inherits the context from @parent.
540 * @fs_data: private data given by FS.
541 * @preload: preload child crypt info if true
543 * Return: 0 on success, -errno on failure
545 int llcrypt_inherit_context(struct inode *parent, struct inode *child,
546 void *fs_data, bool preload)
548 union llcrypt_context ctx;
550 struct llcrypt_info *ci;
551 struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
554 res = llcrypt_get_encryption_info(parent);
558 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
565 ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
567 BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
568 res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
571 return preload ? llcrypt_get_encryption_info(child): 0;
573 EXPORT_SYMBOL(llcrypt_inherit_context);