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);
310 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
315 if (get_user(policy.version, (const u8 __user *)arg))
318 size = llcrypt_policy_size(&policy);
323 * We should just copy the remaining 'size - 1' bytes here, but a
324 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
325 * think that size can be 0 here (despite the check above!) *and* that
326 * it's a compile-time constant. Thus it would think copy_from_user()
327 * is passed compile-time constant ULONG_MAX, causing the compile-time
328 * buffer overflow check to fail, breaking the build. This only occurred
329 * when building an i386 kernel with -Os and branch profiling enabled.
331 * Work around it by just copying the first byte again...
333 version = policy.version;
334 if (copy_from_user(&policy, arg, size))
336 policy.version = version;
338 /* Force file/directory name encryption policy to null if
339 * LSI_FILENAME_ENC flag is not set on sb.
340 * This allows enabling filename encryption separately from data
341 * encryption, and can be useful for interoperability with
342 * encryption-unaware clients.
344 if (!(lsi->lsi_flags & LSI_FILENAME_ENC)) {
345 CWARN("inode %lu: forcing policy filenames_encryption_mode to null\n",
347 cfs_tty_write_msg("\n\nForcing policy filenames_encryption_mode to null.\n\n");
348 switch (policy.version) {
349 case LLCRYPT_POLICY_V1:
350 policy.v1.filenames_encryption_mode = LLCRYPT_MODE_NULL;
352 case LLCRYPT_POLICY_V2:
353 policy.v2.filenames_encryption_mode = LLCRYPT_MODE_NULL;
358 if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
361 ret = mnt_want_write_file(filp);
367 ret = llcrypt_get_policy(inode, &existing_policy);
368 if (ret == -ENODATA) {
369 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
371 if (!S_ISDIR(inode->i_mode))
373 else if (IS_DEADDIR(inode))
375 else if (lsi && !lsi->lsi_cop->empty_dir(inode))
378 ret = set_encryption_policy(inode, &policy);
379 } else if (ret == -EINVAL ||
380 (ret == 0 && !llcrypt_policies_equal(&policy,
381 &existing_policy))) {
382 /* The file already uses a different encryption policy. */
388 mnt_drop_write_file(filp);
391 EXPORT_SYMBOL(llcrypt_ioctl_set_policy);
393 /* Original ioctl version; can only get the original policy version */
394 int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
396 union llcrypt_policy policy;
399 err = llcrypt_get_policy(file_inode(filp), &policy);
403 if (policy.version != LLCRYPT_POLICY_V1)
406 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
410 EXPORT_SYMBOL(llcrypt_ioctl_get_policy);
412 /* Valid filenames_encryption_mode associated with contents_encryption_mode,
413 * as imposed by llcrypt_valid_enc_modes()
415 static inline u8 contents2filenames_encmode(u8 contents_encryption_mode)
417 if (contents_encryption_mode == LLCRYPT_MODE_AES_128_CBC)
418 return LLCRYPT_MODE_AES_128_CTS;
419 if (contents_encryption_mode == LLCRYPT_MODE_AES_256_XTS)
420 return LLCRYPT_MODE_AES_256_CTS;
421 if (contents_encryption_mode == LLCRYPT_MODE_ADIANTUM)
422 return LLCRYPT_MODE_ADIANTUM;
423 return LLCRYPT_MODE_NULL;
426 /* Extended ioctl version; can get policies of any version */
427 int llcrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
429 struct llcrypt_get_policy_ex_arg arg;
430 union llcrypt_policy *policy = (union llcrypt_policy *)&arg.policy;
432 struct inode *inode = file_inode(filp);
435 /* arg is policy_size, then policy */
436 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
437 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
438 offsetof(typeof(arg), policy));
439 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
441 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
444 err = llcrypt_get_policy(file_inode(filp), policy);
447 policy_size = llcrypt_policy_size(policy);
449 if (policy_size > arg.policy_size)
451 arg.policy_size = policy_size;
453 /* Do not return null filenames_encryption_mode to userspace, as it is
454 * unknown. Instead, return valid mode associated with
455 * contents_encryption_mode, as imposed by llcrypt_valid_enc_modes().
457 switch (policy->version) {
458 case LLCRYPT_POLICY_V1:
459 if (policy->v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
460 policy->v1.filenames_encryption_mode =
461 contents2filenames_encmode(
462 policy->v1.contents_encryption_mode);
463 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
465 policy->v1.filenames_encryption_mode);
468 case LLCRYPT_POLICY_V2:
469 if (policy->v2.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
470 policy->v2.filenames_encryption_mode =
471 contents2filenames_encmode(
472 policy->v2.contents_encryption_mode);
473 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
475 policy->v2.filenames_encryption_mode);
480 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
484 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_policy_ex);
487 * llcrypt_has_permitted_context() - is a file's encryption policy permitted
488 * within its directory?
490 * @parent: inode for parent directory
491 * @child: inode for file being looked up, opened, or linked into @parent
493 * Filesystems must call this before permitting access to an inode in a
494 * situation where the parent directory is encrypted (either before allowing
495 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
496 * and before any operation that involves linking an inode into an encrypted
497 * directory, including link, rename, and cross rename. It enforces the
498 * constraint that within a given encrypted directory tree, all files use the
499 * same encryption policy. The pre-access check is needed to detect potentially
500 * malicious offline violations of this constraint, while the link and rename
501 * checks are needed to prevent online violations of this constraint.
503 * Return: 1 if permitted, 0 if forbidden.
505 int llcrypt_has_permitted_context(struct inode *parent, struct inode *child)
507 union llcrypt_policy parent_policy, child_policy;
510 /* No restrictions on file types which are never encrypted */
511 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
512 !S_ISLNK(child->i_mode))
515 /* No restrictions if the parent directory is unencrypted */
516 if (!IS_ENCRYPTED(parent))
519 /* Encrypted directories must not contain unencrypted files */
520 if (!IS_ENCRYPTED(child))
524 * Both parent and child are encrypted, so verify they use the same
525 * encryption policy. Compare the llcrypt_info structs if the keys are
526 * available, otherwise retrieve and compare the llcrypt_contexts.
528 * Note that the llcrypt_context retrieval will be required frequently
529 * when accessing an encrypted directory tree without the key.
530 * Performance-wise this is not a big deal because we already don't
531 * really optimize for file access without the key (to the extent that
532 * such access is even possible), given that any attempted access
533 * already causes a llcrypt_context retrieval and keyring search.
535 * In any case, if an unexpected error occurs, fall back to "forbidden".
538 err = llcrypt_get_encryption_info(parent);
541 err = llcrypt_get_encryption_info(child);
545 err = llcrypt_get_policy(parent, &parent_policy);
549 err = llcrypt_get_policy(child, &child_policy);
553 return llcrypt_policies_equal(&parent_policy, &child_policy);
555 EXPORT_SYMBOL(llcrypt_has_permitted_context);
558 * llcrypt_inherit_context() - Sets a child context from its parent
559 * @parent: Parent inode from which the context is inherited.
560 * @child: Child inode that inherits the context from @parent.
561 * @fs_data: private data given by FS.
562 * @preload: preload child crypt info if true
564 * Return: 0 on success, -errno on failure
566 int llcrypt_inherit_context(struct inode *parent, struct inode *child,
567 void *fs_data, bool preload)
569 union llcrypt_context ctx;
571 struct llcrypt_info *ci;
572 struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
575 res = llcrypt_get_encryption_info(parent);
579 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
586 ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
588 BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
589 res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
592 return preload ? llcrypt_get_encryption_info(child): 0;
594 EXPORT_SYMBOL(llcrypt_inherit_context);