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 "llcrypt_private.h"
23 * llcrypt_policies_equal - check whether two encryption policies are the same
25 * Return: %true if equal, else %false
27 bool llcrypt_policies_equal(const union llcrypt_policy *policy1,
28 const union llcrypt_policy *policy2)
30 if (policy1->version != policy2->version)
33 return !memcmp(policy1, policy2, llcrypt_policy_size(policy1));
37 * llcrypt_supported_policy - check whether an encryption policy is supported
39 * Given an encryption policy, check whether all its encryption modes and other
40 * settings are supported by this kernel. (But we don't currently don't check
41 * for crypto API support here, so attempting to use an algorithm not configured
42 * into the crypto API will still fail later.)
44 * Return: %true if supported, else %false
46 bool llcrypt_supported_policy(const union llcrypt_policy *policy_u,
47 const struct inode *inode)
49 switch (policy_u->version) {
50 case LLCRYPT_POLICY_V1: {
51 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
53 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
54 policy->filenames_encryption_mode)) {
56 "Unsupported encryption modes (contents %d, filenames %d)",
57 policy->contents_encryption_mode,
58 policy->filenames_encryption_mode);
62 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
64 "Unsupported encryption flags (0x%02x)",
71 case LLCRYPT_POLICY_V2: {
72 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
74 if (!llcrypt_valid_enc_modes(policy->contents_encryption_mode,
75 policy->filenames_encryption_mode)) {
77 "Unsupported encryption modes (contents %d, filenames %d)",
78 policy->contents_encryption_mode,
79 policy->filenames_encryption_mode);
83 if (policy->flags & ~LLCRYPT_POLICY_FLAGS_VALID) {
85 "Unsupported encryption flags (0x%02x)",
90 if (memchr_inv(policy->__reserved, 0,
91 sizeof(policy->__reserved))) {
93 "Reserved bits set in encryption policy");
104 * llcrypt_new_context_from_policy - create a new llcrypt_context from a policy
106 * Create an llcrypt_context for an inode that is being assigned the given
107 * encryption policy. A new nonce is randomly generated.
109 * Return: the size of the new context in bytes.
111 static int llcrypt_new_context_from_policy(union llcrypt_context *ctx_u,
112 const union llcrypt_policy *policy_u)
114 memset(ctx_u, 0, sizeof(*ctx_u));
116 switch (policy_u->version) {
117 case LLCRYPT_POLICY_V1: {
118 const struct llcrypt_policy_v1 *policy = &policy_u->v1;
119 struct llcrypt_context_v1 *ctx = &ctx_u->v1;
121 ctx->version = LLCRYPT_CONTEXT_V1;
122 ctx->contents_encryption_mode =
123 policy->contents_encryption_mode;
124 ctx->filenames_encryption_mode =
125 policy->filenames_encryption_mode;
126 ctx->flags = policy->flags;
127 memcpy(ctx->master_key_descriptor,
128 policy->master_key_descriptor,
129 sizeof(ctx->master_key_descriptor));
130 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
133 case LLCRYPT_POLICY_V2: {
134 const struct llcrypt_policy_v2 *policy = &policy_u->v2;
135 struct llcrypt_context_v2 *ctx = &ctx_u->v2;
137 ctx->version = LLCRYPT_CONTEXT_V2;
138 ctx->contents_encryption_mode =
139 policy->contents_encryption_mode;
140 ctx->filenames_encryption_mode =
141 policy->filenames_encryption_mode;
142 ctx->flags = policy->flags;
143 memcpy(ctx->master_key_identifier,
144 policy->master_key_identifier,
145 sizeof(ctx->master_key_identifier));
146 get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
154 * llcrypt_policy_from_context - convert an llcrypt_context to an llcrypt_policy
156 * Given an llcrypt_context, build the corresponding llcrypt_policy.
158 * Return: 0 on success, or -EINVAL if the llcrypt_context has an unrecognized
159 * version number or size.
161 * This does *not* validate the settings within the policy itself, e.g. the
162 * modes, flags, and reserved bits. Use llcrypt_supported_policy() for that.
164 int llcrypt_policy_from_context(union llcrypt_policy *policy_u,
165 const union llcrypt_context *ctx_u,
168 memset(policy_u, 0, sizeof(*policy_u));
170 if (ctx_size <= 0 || ctx_size != llcrypt_context_size(ctx_u))
173 switch (ctx_u->version) {
174 case LLCRYPT_CONTEXT_V1: {
175 const struct llcrypt_context_v1 *ctx = &ctx_u->v1;
176 struct llcrypt_policy_v1 *policy = &policy_u->v1;
178 policy->version = LLCRYPT_POLICY_V1;
179 policy->contents_encryption_mode =
180 ctx->contents_encryption_mode;
181 policy->filenames_encryption_mode =
182 ctx->filenames_encryption_mode;
183 policy->flags = ctx->flags;
184 memcpy(policy->master_key_descriptor,
185 ctx->master_key_descriptor,
186 sizeof(policy->master_key_descriptor));
189 case LLCRYPT_CONTEXT_V2: {
190 const struct llcrypt_context_v2 *ctx = &ctx_u->v2;
191 struct llcrypt_policy_v2 *policy = &policy_u->v2;
193 policy->version = LLCRYPT_POLICY_V2;
194 policy->contents_encryption_mode =
195 ctx->contents_encryption_mode;
196 policy->filenames_encryption_mode =
197 ctx->filenames_encryption_mode;
198 policy->flags = ctx->flags;
199 memcpy(policy->__reserved, ctx->__reserved,
200 sizeof(policy->__reserved));
201 memcpy(policy->master_key_identifier,
202 ctx->master_key_identifier,
203 sizeof(policy->master_key_identifier));
211 /* Retrieve an inode's encryption policy */
212 static int llcrypt_get_policy(struct inode *inode, union llcrypt_policy *policy)
214 const struct llcrypt_info *ci;
215 union llcrypt_context ctx;
216 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
219 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
221 /* key available, use the cached policy */
222 *policy = ci->ci_policy;
226 if (!IS_ENCRYPTED(inode))
232 ret = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
234 return (ret == -ERANGE) ? -EINVAL : ret;
236 return llcrypt_policy_from_context(policy, &ctx, ret);
239 static int set_encryption_policy(struct inode *inode,
240 const union llcrypt_policy *policy)
242 union llcrypt_context ctx;
244 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
247 if (!llcrypt_supported_policy(policy, inode))
250 switch (policy->version) {
251 case LLCRYPT_POLICY_V1:
253 * The original encryption policy version provided no way of
254 * verifying that the correct master key was supplied, which was
255 * insecure in scenarios where multiple users have access to the
256 * same encrypted files (even just read-only access). The new
257 * encryption policy version fixes this and also implies use of
258 * an improved key derivation function and allows non-root users
259 * to securely remove keys. So as long as compatibility with
260 * old kernels isn't required, it is recommended to use the new
261 * policy version for all new encrypted directories.
263 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
264 current->comm, current->pid);
266 case LLCRYPT_POLICY_V2:
267 err = llcrypt_verify_key_added(inode->i_sb,
268 policy->v2.master_key_identifier);
277 ctxsize = llcrypt_new_context_from_policy(&ctx, policy);
282 return lsi->lsi_cop->set_context(inode, &ctx, ctxsize, NULL);
285 int llcrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
287 union llcrypt_policy policy;
288 union llcrypt_policy existing_policy;
289 struct inode *inode = file_inode(filp);
294 if (get_user(policy.version, (const u8 __user *)arg))
297 size = llcrypt_policy_size(&policy);
302 * We should just copy the remaining 'size - 1' bytes here, but a
303 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
304 * think that size can be 0 here (despite the check above!) *and* that
305 * it's a compile-time constant. Thus it would think copy_from_user()
306 * is passed compile-time constant ULONG_MAX, causing the compile-time
307 * buffer overflow check to fail, breaking the build. This only occurred
308 * when building an i386 kernel with -Os and branch profiling enabled.
310 * Work around it by just copying the first byte again...
312 version = policy.version;
313 if (copy_from_user(&policy, arg, size))
315 policy.version = version;
317 /* Force file/directory name encryption policy to null.
318 * This is needed for interoperability with future versions.
319 * Code to be removed when Lustre supports name encryption.
321 CWARN("inode %lu: forcing policy filenames_encryption_mode to null\n",
323 switch (policy.version) {
324 case LLCRYPT_POLICY_V1:
325 policy.v1.filenames_encryption_mode = LLCRYPT_MODE_NULL;
327 case LLCRYPT_POLICY_V2:
328 policy.v2.filenames_encryption_mode = LLCRYPT_MODE_NULL;
332 if (!inode_owner_or_capable(inode))
335 ret = mnt_want_write_file(filp);
341 ret = llcrypt_get_policy(inode, &existing_policy);
342 if (ret == -ENODATA) {
343 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
345 if (!S_ISDIR(inode->i_mode))
347 else if (IS_DEADDIR(inode))
349 else if (lsi && !lsi->lsi_cop->empty_dir(inode))
352 ret = set_encryption_policy(inode, &policy);
353 } else if (ret == -EINVAL ||
354 (ret == 0 && !llcrypt_policies_equal(&policy,
355 &existing_policy))) {
356 /* The file already uses a different encryption policy. */
362 mnt_drop_write_file(filp);
365 EXPORT_SYMBOL(llcrypt_ioctl_set_policy);
367 /* Original ioctl version; can only get the original policy version */
368 int llcrypt_ioctl_get_policy(struct file *filp, void __user *arg)
370 union llcrypt_policy policy;
373 err = llcrypt_get_policy(file_inode(filp), &policy);
377 if (policy.version != LLCRYPT_POLICY_V1)
380 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
384 EXPORT_SYMBOL(llcrypt_ioctl_get_policy);
386 /* Valid filenames_encryption_mode associated with contents_encryption_mode,
387 * as imposed by llcrypt_valid_enc_modes()
389 static inline u8 contents2filenames_encmode(u8 contents_encryption_mode)
391 if (contents_encryption_mode == LLCRYPT_MODE_AES_128_CBC)
392 return LLCRYPT_MODE_AES_128_CTS;
393 if (contents_encryption_mode == LLCRYPT_MODE_AES_256_XTS)
394 return LLCRYPT_MODE_AES_256_CTS;
395 if (contents_encryption_mode == LLCRYPT_MODE_ADIANTUM)
396 return LLCRYPT_MODE_ADIANTUM;
397 return LLCRYPT_MODE_NULL;
400 /* Extended ioctl version; can get policies of any version */
401 int llcrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
403 struct llcrypt_get_policy_ex_arg arg;
404 union llcrypt_policy *policy = (union llcrypt_policy *)&arg.policy;
406 struct inode *inode = file_inode(filp);
409 /* arg is policy_size, then policy */
410 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
411 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
412 offsetof(typeof(arg), policy));
413 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
415 err = llcrypt_get_policy(file_inode(filp), policy);
418 policy_size = llcrypt_policy_size(policy);
420 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
423 if (policy_size > arg.policy_size)
425 arg.policy_size = policy_size;
427 /* Do not return null filenames_encryption_mode to userspace, as it is
428 * unknown. Instead, return valid mode associated with
429 * contents_encryption_mode, as imposed by llcrypt_valid_enc_modes().
431 switch (policy->version) {
432 case LLCRYPT_POLICY_V1:
433 if (policy->v1.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
434 policy->v1.filenames_encryption_mode =
435 contents2filenames_encmode(
436 policy->v1.contents_encryption_mode);
437 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
439 policy->v1.filenames_encryption_mode);
442 case LLCRYPT_POLICY_V2:
443 if (policy->v2.filenames_encryption_mode == LLCRYPT_MODE_NULL) {
444 policy->v2.filenames_encryption_mode =
445 contents2filenames_encmode(
446 policy->v2.contents_encryption_mode);
447 CWARN("inode %lu: returning policy filenames_encryption_mode as %d, but is in fact null\n",
449 policy->v2.filenames_encryption_mode);
454 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
458 EXPORT_SYMBOL_GPL(llcrypt_ioctl_get_policy_ex);
461 * llcrypt_has_permitted_context() - is a file's encryption policy permitted
462 * within its directory?
464 * @parent: inode for parent directory
465 * @child: inode for file being looked up, opened, or linked into @parent
467 * Filesystems must call this before permitting access to an inode in a
468 * situation where the parent directory is encrypted (either before allowing
469 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
470 * and before any operation that involves linking an inode into an encrypted
471 * directory, including link, rename, and cross rename. It enforces the
472 * constraint that within a given encrypted directory tree, all files use the
473 * same encryption policy. The pre-access check is needed to detect potentially
474 * malicious offline violations of this constraint, while the link and rename
475 * checks are needed to prevent online violations of this constraint.
477 * Return: 1 if permitted, 0 if forbidden.
479 int llcrypt_has_permitted_context(struct inode *parent, struct inode *child)
481 union llcrypt_policy parent_policy, child_policy;
484 /* No restrictions on file types which are never encrypted */
485 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
486 !S_ISLNK(child->i_mode))
489 /* No restrictions if the parent directory is unencrypted */
490 if (!IS_ENCRYPTED(parent))
493 /* Encrypted directories must not contain unencrypted files */
494 if (!IS_ENCRYPTED(child))
498 * Both parent and child are encrypted, so verify they use the same
499 * encryption policy. Compare the llcrypt_info structs if the keys are
500 * available, otherwise retrieve and compare the llcrypt_contexts.
502 * Note that the llcrypt_context retrieval will be required frequently
503 * when accessing an encrypted directory tree without the key.
504 * Performance-wise this is not a big deal because we already don't
505 * really optimize for file access without the key (to the extent that
506 * such access is even possible), given that any attempted access
507 * already causes a llcrypt_context retrieval and keyring search.
509 * In any case, if an unexpected error occurs, fall back to "forbidden".
512 err = llcrypt_get_encryption_info(parent);
515 err = llcrypt_get_encryption_info(child);
519 err = llcrypt_get_policy(parent, &parent_policy);
523 err = llcrypt_get_policy(child, &child_policy);
527 return llcrypt_policies_equal(&parent_policy, &child_policy);
529 EXPORT_SYMBOL(llcrypt_has_permitted_context);
532 * llcrypt_inherit_context() - Sets a child context from its parent
533 * @parent: Parent inode from which the context is inherited.
534 * @child: Child inode that inherits the context from @parent.
535 * @fs_data: private data given by FS.
536 * @preload: preload child crypt info if true
538 * Return: 0 on success, -errno on failure
540 int llcrypt_inherit_context(struct inode *parent, struct inode *child,
541 void *fs_data, bool preload)
543 union llcrypt_context ctx;
545 struct llcrypt_info *ci;
546 struct lustre_sb_info *lsi = s2lsi(parent->i_sb);
549 res = llcrypt_get_encryption_info(parent);
553 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(parent));
560 ctxsize = llcrypt_new_context_from_policy(&ctx, &ci->ci_policy);
562 BUILD_BUG_ON(sizeof(ctx) != LLCRYPT_SET_CONTEXT_MAX_SIZE);
563 res = lsi->lsi_cop->set_context(child, &ctx, ctxsize, fs_data);
566 return preload ? llcrypt_get_encryption_info(child): 0;
568 EXPORT_SYMBOL(llcrypt_inherit_context);