Merge tag 'ubifs-for-linus-6.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / fs / crypto / policy.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
0b81d077
JK
2/*
3 * Encryption policy functions for per-file encryption support.
4 *
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility.
7 *
5dae460c 8 * Originally written by Michael Halcrow, 2015.
0b81d077 9 * Modified by Jaegeuk Kim, 2015.
5dae460c 10 * Modified by Eric Biggers, 2019 for v2 policy support.
0b81d077
JK
11 */
12
218d921b 13#include <linux/fs_context.h>
0b81d077 14#include <linux/random.h>
ed318a6c 15#include <linux/seq_file.h>
0b81d077 16#include <linux/string.h>
ba63f23d 17#include <linux/mount.h>
cc4e0df0 18#include "fscrypt_private.h"
0b81d077 19
5dae460c 20/**
d2fe9754
EB
21 * fscrypt_policies_equal() - check whether two encryption policies are the same
22 * @policy1: the first policy
23 * @policy2: the second policy
5dae460c
EB
24 *
25 * Return: %true if equal, else %false
0b81d077 26 */
5dae460c
EB
27bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
28 const union fscrypt_policy *policy2)
0b81d077 29{
5dae460c
EB
30 if (policy1->version != policy2->version)
31 return false;
32
33 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
0b81d077
JK
34}
35
bfb9700b
EB
36int fscrypt_policy_to_key_spec(const union fscrypt_policy *policy,
37 struct fscrypt_key_specifier *key_spec)
38{
39 switch (policy->version) {
40 case FSCRYPT_POLICY_V1:
41 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
42 memcpy(key_spec->u.descriptor, policy->v1.master_key_descriptor,
43 FSCRYPT_KEY_DESCRIPTOR_SIZE);
44 return 0;
45 case FSCRYPT_POLICY_V2:
46 key_spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
47 memcpy(key_spec->u.identifier, policy->v2.master_key_identifier,
48 FSCRYPT_KEY_IDENTIFIER_SIZE);
49 return 0;
50 default:
41b2ad80 51 WARN_ON_ONCE(1);
bfb9700b
EB
52 return -EINVAL;
53 }
54}
55
60e463f0 56const union fscrypt_policy *fscrypt_get_dummy_policy(struct super_block *sb)
ac4acb1f
EB
57{
58 if (!sb->s_cop->get_dummy_policy)
59 return NULL;
60 return sb->s_cop->get_dummy_policy(sb);
61}
62
aa997990
EB
63/*
64 * Return %true if the given combination of encryption modes is supported for v1
65 * (and later) encryption policies.
66 *
67 * Do *not* add anything new here, since v1 encryption policies are deprecated.
68 * New combinations of modes should go in fscrypt_valid_enc_modes_v2() only.
69 */
6b2a51ff 70static bool fscrypt_valid_enc_modes_v1(u32 contents_mode, u32 filenames_mode)
ef5b18b0
EB
71{
72 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
73 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
74 return true;
75
76 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
77 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
78 return true;
79
80 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
81 filenames_mode == FSCRYPT_MODE_ADIANTUM)
82 return true;
83
84 return false;
85}
86
6b2a51ff
NH
87static bool fscrypt_valid_enc_modes_v2(u32 contents_mode, u32 filenames_mode)
88{
89 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
90 filenames_mode == FSCRYPT_MODE_AES_256_HCTR2)
91 return true;
e0cefada
TZ
92
93 if (contents_mode == FSCRYPT_MODE_SM4_XTS &&
94 filenames_mode == FSCRYPT_MODE_SM4_CTS)
95 return true;
96
6b2a51ff
NH
97 return fscrypt_valid_enc_modes_v1(contents_mode, filenames_mode);
98}
99
85af90e5
EB
100static bool supported_direct_key_modes(const struct inode *inode,
101 u32 contents_mode, u32 filenames_mode)
102{
103 const struct fscrypt_mode *mode;
104
105 if (contents_mode != filenames_mode) {
106 fscrypt_warn(inode,
107 "Direct key flag not allowed with different contents and filenames modes");
108 return false;
109 }
110 mode = &fscrypt_modes[contents_mode];
111
112 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
113 fscrypt_warn(inode, "Direct key flag not allowed with %s",
114 mode->friendly_name);
115 return false;
116 }
117 return true;
118}
119
e3b1078b
EB
120static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
121 const struct inode *inode,
122 const char *type,
123 int max_ino_bits, int max_lblk_bits)
b103fb76
EB
124{
125 struct super_block *sb = inode->i_sb;
126 int ino_bits = 64, lblk_bits = 64;
127
f000223c
EB
128 /*
129 * IV_INO_LBLK_* exist only because of hardware limitations, and
130 * currently the only known use case for them involves AES-256-XTS.
131 * That's also all we test currently. For these reasons, for now only
132 * allow AES-256-XTS here. This can be relaxed later if a use case for
133 * IV_INO_LBLK_* with other encryption modes arises.
134 */
135 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
136 fscrypt_warn(inode,
137 "Can't use %s policy with contents mode other than AES-256-XTS",
138 type);
139 return false;
140 }
141
b103fb76
EB
142 /*
143 * It's unsafe to include inode numbers in the IVs if the filesystem can
144 * potentially renumber inodes, e.g. via filesystem shrinking.
145 */
146 if (!sb->s_cop->has_stable_inodes ||
147 !sb->s_cop->has_stable_inodes(sb)) {
148 fscrypt_warn(inode,
e3b1078b
EB
149 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
150 type, sb->s_id);
b103fb76
EB
151 return false;
152 }
153 if (sb->s_cop->get_ino_and_lblk_bits)
154 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
e3b1078b
EB
155 if (ino_bits > max_ino_bits) {
156 fscrypt_warn(inode,
157 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
158 type, sb->s_id);
159 return false;
160 }
161 if (lblk_bits > max_lblk_bits) {
b103fb76 162 fscrypt_warn(inode,
e3b1078b
EB
163 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
164 type, sb->s_id);
b103fb76
EB
165 return false;
166 }
167 return true;
168}
169
393a24a7
EB
170static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
171 const struct inode *inode)
172{
6b2a51ff 173 if (!fscrypt_valid_enc_modes_v1(policy->contents_encryption_mode,
393a24a7
EB
174 policy->filenames_encryption_mode)) {
175 fscrypt_warn(inode,
176 "Unsupported encryption modes (contents %d, filenames %d)",
177 policy->contents_encryption_mode,
178 policy->filenames_encryption_mode);
179 return false;
180 }
181
182 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
183 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
184 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
185 policy->flags);
186 return false;
187 }
188
85af90e5
EB
189 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
190 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
191 policy->filenames_encryption_mode))
192 return false;
193
6e1918cf
DR
194 if (IS_CASEFOLDED(inode)) {
195 /* With v1, there's no way to derive dirhash keys. */
196 fscrypt_warn(inode,
197 "v1 policies can't be used on casefolded directories");
198 return false;
199 }
200
393a24a7
EB
201 return true;
202}
203
204static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
205 const struct inode *inode)
206{
e3b1078b
EB
207 int count = 0;
208
6b2a51ff 209 if (!fscrypt_valid_enc_modes_v2(policy->contents_encryption_mode,
393a24a7
EB
210 policy->filenames_encryption_mode)) {
211 fscrypt_warn(inode,
212 "Unsupported encryption modes (contents %d, filenames %d)",
213 policy->contents_encryption_mode,
214 policy->filenames_encryption_mode);
215 return false;
216 }
217
3ceb6543
EB
218 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
219 FSCRYPT_POLICY_FLAG_DIRECT_KEY |
220 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
221 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
393a24a7
EB
222 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
223 policy->flags);
224 return false;
225 }
226
e3b1078b
EB
227 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
228 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
229 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
230 if (count > 1) {
231 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
232 policy->flags);
233 return false;
234 }
235
85af90e5
EB
236 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
237 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
238 policy->filenames_encryption_mode))
239 return false;
240
393a24a7 241 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
e3b1078b
EB
242 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
243 32, 32))
244 return false;
245
5e895bd4
EB
246 /*
247 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
248 * support any ino_bits. However, currently the inode number is gotten
249 * from inode::i_ino which is 'unsigned long'. So for now the
250 * implementation limit is 32 bits.
251 */
e3b1078b 252 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
e3b1078b 253 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
5e895bd4 254 32, 32))
393a24a7
EB
255 return false;
256
257 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
258 fscrypt_warn(inode, "Reserved bits set in encryption policy");
259 return false;
260 }
261
262 return true;
263}
264
5dae460c 265/**
d2fe9754
EB
266 * fscrypt_supported_policy() - check whether an encryption policy is supported
267 * @policy_u: the encryption policy
268 * @inode: the inode on which the policy will be used
5dae460c
EB
269 *
270 * Given an encryption policy, check whether all its encryption modes and other
393a24a7
EB
271 * settings are supported by this kernel on the given inode. (But we don't
272 * currently don't check for crypto API support here, so attempting to use an
273 * algorithm not configured into the crypto API will still fail later.)
5dae460c
EB
274 *
275 * Return: %true if supported, else %false
276 */
277bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
278 const struct inode *inode)
0b81d077 279{
5dae460c 280 switch (policy_u->version) {
393a24a7
EB
281 case FSCRYPT_POLICY_V1:
282 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
283 case FSCRYPT_POLICY_V2:
284 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
5dae460c
EB
285 }
286 return false;
287}
288
289/**
a992b20c 290 * fscrypt_new_context() - create a new fscrypt_context
d2fe9754
EB
291 * @ctx_u: output context
292 * @policy_u: input policy
a992b20c 293 * @nonce: nonce to use
5dae460c
EB
294 *
295 * Create an fscrypt_context for an inode that is being assigned the given
a992b20c 296 * encryption policy. @nonce must be a new random nonce.
5dae460c
EB
297 *
298 * Return: the size of the new context in bytes.
299 */
a992b20c
EB
300static int fscrypt_new_context(union fscrypt_context *ctx_u,
301 const union fscrypt_policy *policy_u,
302 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
5dae460c
EB
303{
304 memset(ctx_u, 0, sizeof(*ctx_u));
305
306 switch (policy_u->version) {
307 case FSCRYPT_POLICY_V1: {
308 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
309 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
310
311 ctx->version = FSCRYPT_CONTEXT_V1;
312 ctx->contents_encryption_mode =
313 policy->contents_encryption_mode;
314 ctx->filenames_encryption_mode =
315 policy->filenames_encryption_mode;
316 ctx->flags = policy->flags;
317 memcpy(ctx->master_key_descriptor,
318 policy->master_key_descriptor,
319 sizeof(ctx->master_key_descriptor));
a992b20c 320 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
5dae460c
EB
321 return sizeof(*ctx);
322 }
323 case FSCRYPT_POLICY_V2: {
324 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
325 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
326
327 ctx->version = FSCRYPT_CONTEXT_V2;
328 ctx->contents_encryption_mode =
329 policy->contents_encryption_mode;
330 ctx->filenames_encryption_mode =
331 policy->filenames_encryption_mode;
332 ctx->flags = policy->flags;
333 memcpy(ctx->master_key_identifier,
334 policy->master_key_identifier,
335 sizeof(ctx->master_key_identifier));
a992b20c 336 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
5dae460c
EB
337 return sizeof(*ctx);
338 }
339 }
340 BUG();
341}
0b81d077 342
5dae460c 343/**
d2fe9754
EB
344 * fscrypt_policy_from_context() - convert an fscrypt_context to
345 * an fscrypt_policy
346 * @policy_u: output policy
347 * @ctx_u: input context
348 * @ctx_size: size of input context in bytes
5dae460c
EB
349 *
350 * Given an fscrypt_context, build the corresponding fscrypt_policy.
351 *
352 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
353 * version number or size.
354 *
355 * This does *not* validate the settings within the policy itself, e.g. the
356 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
357 */
358int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
359 const union fscrypt_context *ctx_u,
360 int ctx_size)
361{
362 memset(policy_u, 0, sizeof(*policy_u));
0b81d077 363
e98ad464 364 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
0b81d077 365 return -EINVAL;
0b81d077 366
5dae460c
EB
367 switch (ctx_u->version) {
368 case FSCRYPT_CONTEXT_V1: {
369 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
370 struct fscrypt_policy_v1 *policy = &policy_u->v1;
371
372 policy->version = FSCRYPT_POLICY_V1;
373 policy->contents_encryption_mode =
374 ctx->contents_encryption_mode;
375 policy->filenames_encryption_mode =
376 ctx->filenames_encryption_mode;
377 policy->flags = ctx->flags;
378 memcpy(policy->master_key_descriptor,
379 ctx->master_key_descriptor,
380 sizeof(policy->master_key_descriptor));
381 return 0;
382 }
383 case FSCRYPT_CONTEXT_V2: {
384 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
385 struct fscrypt_policy_v2 *policy = &policy_u->v2;
386
387 policy->version = FSCRYPT_POLICY_V2;
388 policy->contents_encryption_mode =
389 ctx->contents_encryption_mode;
390 policy->filenames_encryption_mode =
391 ctx->filenames_encryption_mode;
392 policy->flags = ctx->flags;
393 memcpy(policy->__reserved, ctx->__reserved,
394 sizeof(policy->__reserved));
395 memcpy(policy->master_key_identifier,
396 ctx->master_key_identifier,
397 sizeof(policy->master_key_identifier));
398 return 0;
399 }
400 }
401 /* unreachable */
402 return -EINVAL;
403}
404
405/* Retrieve an inode's encryption policy */
406static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
407{
408 const struct fscrypt_info *ci;
409 union fscrypt_context ctx;
410 int ret;
411
ab673b98 412 ci = fscrypt_get_info(inode);
5dae460c
EB
413 if (ci) {
414 /* key available, use the cached policy */
415 *policy = ci->ci_policy;
416 return 0;
417 }
418
419 if (!IS_ENCRYPTED(inode))
420 return -ENODATA;
421
422 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
423 if (ret < 0)
424 return (ret == -ERANGE) ? -EINVAL : ret;
425
426 return fscrypt_policy_from_context(policy, &ctx, ret);
427}
428
429static int set_encryption_policy(struct inode *inode,
430 const union fscrypt_policy *policy)
431{
a992b20c 432 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
5dae460c
EB
433 union fscrypt_context ctx;
434 int ctxsize;
5ab7189a 435 int err;
5dae460c
EB
436
437 if (!fscrypt_supported_policy(policy, inode))
0b81d077
JK
438 return -EINVAL;
439
5ab7189a
EB
440 switch (policy->version) {
441 case FSCRYPT_POLICY_V1:
5dae460c
EB
442 /*
443 * The original encryption policy version provided no way of
444 * verifying that the correct master key was supplied, which was
445 * insecure in scenarios where multiple users have access to the
446 * same encrypted files (even just read-only access). The new
447 * encryption policy version fixes this and also implies use of
448 * an improved key derivation function and allows non-root users
449 * to securely remove keys. So as long as compatibility with
450 * old kernels isn't required, it is recommended to use the new
451 * policy version for all new encrypted directories.
452 */
453 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
454 current->comm, current->pid);
5ab7189a
EB
455 break;
456 case FSCRYPT_POLICY_V2:
457 err = fscrypt_verify_key_added(inode->i_sb,
458 policy->v2.master_key_identifier);
459 if (err)
460 return err;
e3b1078b
EB
461 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
462 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",
463 current->comm, current->pid);
5ab7189a
EB
464 break;
465 default:
41b2ad80 466 WARN_ON_ONCE(1);
5ab7189a 467 return -EINVAL;
5dae460c 468 }
0b81d077 469
a992b20c
EB
470 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
471 ctxsize = fscrypt_new_context(&ctx, policy, nonce);
5dae460c
EB
472
473 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
0b81d077
JK
474}
475
db717d8e 476int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
0b81d077 477{
5dae460c
EB
478 union fscrypt_policy policy;
479 union fscrypt_policy existing_policy;
ba63f23d 480 struct inode *inode = file_inode(filp);
5dae460c
EB
481 u8 version;
482 int size;
ba63f23d
EB
483 int ret;
484
5dae460c 485 if (get_user(policy.version, (const u8 __user *)arg))
db717d8e
EB
486 return -EFAULT;
487
5dae460c
EB
488 size = fscrypt_policy_size(&policy);
489 if (size <= 0)
490 return -EINVAL;
491
492 /*
493 * We should just copy the remaining 'size - 1' bytes here, but a
494 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
495 * think that size can be 0 here (despite the check above!) *and* that
496 * it's a compile-time constant. Thus it would think copy_from_user()
497 * is passed compile-time constant ULONG_MAX, causing the compile-time
498 * buffer overflow check to fail, breaking the build. This only occurred
499 * when building an i386 kernel with -Os and branch profiling enabled.
500 *
501 * Work around it by just copying the first byte again...
502 */
503 version = policy.version;
504 if (copy_from_user(&policy, arg, size))
505 return -EFAULT;
506 policy.version = version;
507
01beba79 508 if (!inode_owner_or_capable(&nop_mnt_idmap, inode))
163ae1c6
EB
509 return -EACCES;
510
ba63f23d
EB
511 ret = mnt_want_write_file(filp);
512 if (ret)
513 return ret;
514
8906a822
EB
515 inode_lock(inode);
516
5dae460c 517 ret = fscrypt_get_policy(inode, &existing_policy);
efee590e 518 if (ret == -ENODATA) {
002ced4b 519 if (!S_ISDIR(inode->i_mode))
dffd0cfa 520 ret = -ENOTDIR;
5858bdad
HF
521 else if (IS_DEADDIR(inode))
522 ret = -ENOENT;
ba63f23d
EB
523 else if (!inode->i_sb->s_cop->empty_dir(inode))
524 ret = -ENOTEMPTY;
525 else
5dae460c
EB
526 ret = set_encryption_policy(inode, &policy);
527 } else if (ret == -EINVAL ||
528 (ret == 0 && !fscrypt_policies_equal(&policy,
529 &existing_policy))) {
efee590e 530 /* The file already uses a different encryption policy. */
8488cd96 531 ret = -EEXIST;
0b81d077
JK
532 }
533
8906a822
EB
534 inode_unlock(inode);
535
ba63f23d
EB
536 mnt_drop_write_file(filp);
537 return ret;
0b81d077 538}
db717d8e 539EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
0b81d077 540
5dae460c 541/* Original ioctl version; can only get the original policy version */
db717d8e 542int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
0b81d077 543{
5dae460c
EB
544 union fscrypt_policy policy;
545 int err;
0b81d077 546
5dae460c
EB
547 err = fscrypt_get_policy(file_inode(filp), &policy);
548 if (err)
549 return err;
0b81d077 550
5dae460c 551 if (policy.version != FSCRYPT_POLICY_V1)
0b81d077
JK
552 return -EINVAL;
553
5dae460c 554 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
db717d8e 555 return -EFAULT;
0b81d077
JK
556 return 0;
557}
db717d8e 558EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
0b81d077 559
5dae460c
EB
560/* Extended ioctl version; can get policies of any version */
561int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
562{
563 struct fscrypt_get_policy_ex_arg arg;
564 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
565 size_t policy_size;
566 int err;
567
568 /* arg is policy_size, then policy */
569 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
570 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
571 offsetof(typeof(arg), policy));
572 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
573
574 err = fscrypt_get_policy(file_inode(filp), policy);
575 if (err)
576 return err;
577 policy_size = fscrypt_policy_size(policy);
578
579 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
580 return -EFAULT;
581
582 if (policy_size > arg.policy_size)
583 return -EOVERFLOW;
584 arg.policy_size = policy_size;
585
586 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
587 return -EFAULT;
588 return 0;
589}
590EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
591
e98ad464
EB
592/* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
593int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
594{
595 struct inode *inode = file_inode(filp);
596 union fscrypt_context ctx;
597 int ret;
598
599 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
600 if (ret < 0)
601 return ret;
602 if (!fscrypt_context_is_valid(&ctx, ret))
603 return -EINVAL;
604 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
1d6217a4 605 FSCRYPT_FILE_NONCE_SIZE))
e98ad464
EB
606 return -EFAULT;
607 return 0;
608}
609EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
610
272f98f6
EB
611/**
612 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
613 * within its directory?
614 *
615 * @parent: inode for parent directory
616 * @child: inode for file being looked up, opened, or linked into @parent
617 *
618 * Filesystems must call this before permitting access to an inode in a
619 * situation where the parent directory is encrypted (either before allowing
620 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
621 * and before any operation that involves linking an inode into an encrypted
622 * directory, including link, rename, and cross rename. It enforces the
623 * constraint that within a given encrypted directory tree, all files use the
624 * same encryption policy. The pre-access check is needed to detect potentially
625 * malicious offline violations of this constraint, while the link and rename
626 * checks are needed to prevent online violations of this constraint.
627 *
f5e55e77 628 * Return: 1 if permitted, 0 if forbidden.
272f98f6 629 */
0b81d077
JK
630int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
631{
5dae460c 632 union fscrypt_policy parent_policy, child_policy;
a14d0b67 633 int err, err1, err2;
0b81d077 634
42d97eb0
EB
635 /* No restrictions on file types which are never encrypted */
636 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
637 !S_ISLNK(child->i_mode))
638 return 1;
639
272f98f6 640 /* No restrictions if the parent directory is unencrypted */
e0428a26 641 if (!IS_ENCRYPTED(parent))
0b81d077 642 return 1;
272f98f6
EB
643
644 /* Encrypted directories must not contain unencrypted files */
e0428a26 645 if (!IS_ENCRYPTED(child))
0b81d077 646 return 0;
272f98f6
EB
647
648 /*
649 * Both parent and child are encrypted, so verify they use the same
650 * encryption policy. Compare the fscrypt_info structs if the keys are
651 * available, otherwise retrieve and compare the fscrypt_contexts.
652 *
653 * Note that the fscrypt_context retrieval will be required frequently
654 * when accessing an encrypted directory tree without the key.
655 * Performance-wise this is not a big deal because we already don't
656 * really optimize for file access without the key (to the extent that
657 * such access is even possible), given that any attempted access
658 * already causes a fscrypt_context retrieval and keyring search.
659 *
660 * In any case, if an unexpected error occurs, fall back to "forbidden".
661 */
662
a14d0b67 663 err = fscrypt_get_encryption_info(parent, true);
5dae460c 664 if (err)
0b81d077 665 return 0;
a14d0b67 666 err = fscrypt_get_encryption_info(child, true);
5dae460c 667 if (err)
0b81d077 668 return 0;
272f98f6 669
a14d0b67
EB
670 err1 = fscrypt_get_policy(parent, &parent_policy);
671 err2 = fscrypt_get_policy(child, &child_policy);
272f98f6 672
a14d0b67
EB
673 /*
674 * Allow the case where the parent and child both have an unrecognized
675 * encryption policy, so that files with an unrecognized encryption
676 * policy can be deleted.
677 */
678 if (err1 == -EINVAL && err2 == -EINVAL)
679 return 1;
680
681 if (err1 || err2)
0b81d077
JK
682 return 0;
683
5dae460c 684 return fscrypt_policies_equal(&parent_policy, &child_policy);
0b81d077
JK
685}
686EXPORT_SYMBOL(fscrypt_has_permitted_context);
687
ac4acb1f
EB
688/*
689 * Return the encryption policy that new files in the directory will inherit, or
690 * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also
691 * ensure that its key is set up, so that the new filename can be encrypted.
692 */
693const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
694{
695 int err;
696
697 if (IS_ENCRYPTED(dir)) {
698 err = fscrypt_require_key(dir);
699 if (err)
700 return ERR_PTR(err);
701 return &dir->i_crypt_info->ci_policy;
702 }
703
704 return fscrypt_get_dummy_policy(dir->i_sb);
705}
706
637fa738
JL
707/**
708 * fscrypt_context_for_new_inode() - create an encryption context for a new inode
709 * @ctx: where context should be written
710 * @inode: inode from which to fetch policy and nonce
711 *
712 * Given an in-core "prepared" (via fscrypt_prepare_new_inode) inode,
713 * generate a new context and write it to ctx. ctx _must_ be at least
714 * FSCRYPT_SET_CONTEXT_MAX_SIZE bytes.
715 *
716 * Return: size of the resulting context or a negative error code.
717 */
718int fscrypt_context_for_new_inode(void *ctx, struct inode *inode)
719{
720 struct fscrypt_info *ci = inode->i_crypt_info;
721
722 BUILD_BUG_ON(sizeof(union fscrypt_context) !=
723 FSCRYPT_SET_CONTEXT_MAX_SIZE);
724
725 /* fscrypt_prepare_new_inode() should have set up the key already. */
726 if (WARN_ON_ONCE(!ci))
727 return -ENOKEY;
728
729 return fscrypt_new_context(ctx, &ci->ci_policy, ci->ci_nonce);
730}
731EXPORT_SYMBOL_GPL(fscrypt_context_for_new_inode);
732
a992b20c
EB
733/**
734 * fscrypt_set_context() - Set the fscrypt context of a new inode
735 * @inode: a new inode
736 * @fs_data: private data given by FS and passed to ->set_context()
737 *
738 * This should be called after fscrypt_prepare_new_inode(), generally during a
739 * filesystem transaction. Everything here must be %GFP_NOFS-safe.
740 *
741 * Return: 0 on success, -errno on failure
742 */
743int fscrypt_set_context(struct inode *inode, void *fs_data)
744{
745 struct fscrypt_info *ci = inode->i_crypt_info;
746 union fscrypt_context ctx;
747 int ctxsize;
748
637fa738
JL
749 ctxsize = fscrypt_context_for_new_inode(&ctx, inode);
750 if (ctxsize < 0)
751 return ctxsize;
a992b20c
EB
752
753 /*
754 * This may be the first time the inode number is available, so do any
755 * delayed key setup that requires the inode number.
756 */
757 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
d7e7b9af
EB
758 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))
759 fscrypt_hash_inode_number(ci, ci->ci_master_key);
a992b20c
EB
760
761 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
762}
763EXPORT_SYMBOL_GPL(fscrypt_set_context);
764
ed318a6c 765/**
218d921b
EB
766 * fscrypt_parse_test_dummy_encryption() - parse the test_dummy_encryption mount option
767 * @param: the mount option
768 * @dummy_policy: (input/output) the place to write the dummy policy that will
769 * result from parsing the option. Zero-initialize this. If a policy is
770 * already set here (due to test_dummy_encryption being given multiple
771 * times), then this function will verify that the policies are the same.
ed318a6c 772 *
218d921b
EB
773 * Return: 0 on success; -EINVAL if the argument is invalid; -EEXIST if the
774 * argument conflicts with one already specified; or -ENOMEM.
ed318a6c 775 */
218d921b
EB
776int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
777 struct fscrypt_dummy_policy *dummy_policy)
ed318a6c 778{
218d921b
EB
779 const char *arg = "v2";
780 union fscrypt_policy *policy;
ed318a6c
EB
781 int err;
782
218d921b
EB
783 if (param->type == fs_value_is_string && *param->string)
784 arg = param->string;
ed318a6c 785
ac4acb1f 786 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
218d921b
EB
787 if (!policy)
788 return -ENOMEM;
ed318a6c 789
218d921b
EB
790 if (!strcmp(arg, "v1")) {
791 policy->version = FSCRYPT_POLICY_V1;
ac4acb1f
EB
792 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
793 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
218d921b 794 memset(policy->v1.master_key_descriptor, 0x42,
ed318a6c 795 FSCRYPT_KEY_DESCRIPTOR_SIZE);
218d921b
EB
796 } else if (!strcmp(arg, "v2")) {
797 policy->version = FSCRYPT_POLICY_V2;
ac4acb1f
EB
798 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
799 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
218d921b
EB
800 err = fscrypt_get_test_dummy_key_identifier(
801 policy->v2.master_key_identifier);
802 if (err)
803 goto out;
804 } else {
ed318a6c
EB
805 err = -EINVAL;
806 goto out;
807 }
ac4acb1f
EB
808
809 if (dummy_policy->policy) {
810 if (fscrypt_policies_equal(policy, dummy_policy->policy))
811 err = 0;
812 else
813 err = -EEXIST;
814 goto out;
815 }
816 dummy_policy->policy = policy;
817 policy = NULL;
ed318a6c
EB
818 err = 0;
819out:
ac4acb1f 820 kfree(policy);
ed318a6c
EB
821 return err;
822}
218d921b
EB
823EXPORT_SYMBOL_GPL(fscrypt_parse_test_dummy_encryption);
824
825/**
826 * fscrypt_dummy_policies_equal() - check whether two dummy policies are equal
827 * @p1: the first test dummy policy (may be unset)
828 * @p2: the second test dummy policy (may be unset)
829 *
830 * Return: %true if the dummy policies are both set and equal, or both unset.
831 */
832bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
833 const struct fscrypt_dummy_policy *p2)
834{
835 if (!p1->policy && !p2->policy)
836 return true;
837 if (!p1->policy || !p2->policy)
838 return false;
839 return fscrypt_policies_equal(p1->policy, p2->policy);
840}
841EXPORT_SYMBOL_GPL(fscrypt_dummy_policies_equal);
842
ed318a6c
EB
843/**
844 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
845 * @seq: the seq_file to print the option to
846 * @sep: the separator character to use
847 * @sb: the filesystem whose options are being shown
848 *
849 * Show the test_dummy_encryption mount option, if it was specified.
850 * This is mainly used for /proc/mounts.
851 */
852void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
853 struct super_block *sb)
854{
ac4acb1f
EB
855 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
856 int vers;
ed318a6c 857
ac4acb1f 858 if (!policy)
ed318a6c 859 return;
ac4acb1f
EB
860
861 vers = policy->version;
862 if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
863 vers = 1;
864
865 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
ed318a6c
EB
866}
867EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);