blob: ed3d623724cddbc0cef5b06948db83726f87b574 [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>
Eric Biggersed318a62020-05-12 16:32:50 -070014#include <linux/seq_file.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070015#include <linux/string.h>
Eric Biggersba63f232016-09-08 14:20:38 -070016#include <linux/mount.h>
Theodore Ts'occ4e0df2016-11-26 22:05:18 -050017#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070018
Eric Biggers5dae4602019-08-04 19:35:47 -070019/**
Eric Biggersd2fe9752020-05-11 12:13:56 -070020 * fscrypt_policies_equal() - check whether two encryption policies are the same
21 * @policy1: the first policy
22 * @policy2: the second policy
Eric Biggers5dae4602019-08-04 19:35:47 -070023 *
24 * Return: %true if equal, else %false
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070025 */
Eric Biggers5dae4602019-08-04 19:35:47 -070026bool fscrypt_policies_equal(const union fscrypt_policy *policy1,
27 const union fscrypt_policy *policy2)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070028{
Eric Biggers5dae4602019-08-04 19:35:47 -070029 if (policy1->version != policy2->version)
30 return false;
31
32 return !memcmp(policy1, policy2, fscrypt_policy_size(policy1));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070033}
34
Eric Biggersac4acb12020-09-16 21:11:35 -070035static const union fscrypt_policy *
36fscrypt_get_dummy_policy(struct super_block *sb)
37{
38 if (!sb->s_cop->get_dummy_policy)
39 return NULL;
40 return sb->s_cop->get_dummy_policy(sb);
41}
42
Eric Biggersef5b18b2019-12-09 13:18:28 -080043static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode)
44{
45 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
46 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
47 return true;
48
49 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
50 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
51 return true;
52
53 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
54 filenames_mode == FSCRYPT_MODE_ADIANTUM)
55 return true;
56
57 return false;
58}
59
Eric Biggers85af90e2019-12-09 13:18:27 -080060static bool supported_direct_key_modes(const struct inode *inode,
61 u32 contents_mode, u32 filenames_mode)
62{
63 const struct fscrypt_mode *mode;
64
65 if (contents_mode != filenames_mode) {
66 fscrypt_warn(inode,
67 "Direct key flag not allowed with different contents and filenames modes");
68 return false;
69 }
70 mode = &fscrypt_modes[contents_mode];
71
72 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
73 fscrypt_warn(inode, "Direct key flag not allowed with %s",
74 mode->friendly_name);
75 return false;
76 }
77 return true;
78}
79
Eric Biggerse3b10782020-05-15 13:41:41 -070080static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy,
81 const struct inode *inode,
82 const char *type,
83 int max_ino_bits, int max_lblk_bits)
Eric Biggersb103fb72019-10-24 14:54:36 -070084{
85 struct super_block *sb = inode->i_sb;
86 int ino_bits = 64, lblk_bits = 64;
87
Eric Biggersb103fb72019-10-24 14:54:36 -070088 /*
Eric Biggersf0002232020-07-21 11:10:12 -070089 * IV_INO_LBLK_* exist only because of hardware limitations, and
90 * currently the only known use case for them involves AES-256-XTS.
91 * That's also all we test currently. For these reasons, for now only
92 * allow AES-256-XTS here. This can be relaxed later if a use case for
93 * IV_INO_LBLK_* with other encryption modes arises.
94 */
95 if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) {
96 fscrypt_warn(inode,
97 "Can't use %s policy with contents mode other than AES-256-XTS",
98 type);
99 return false;
100 }
101
102 /*
Eric Biggersb103fb72019-10-24 14:54:36 -0700103 * It's unsafe to include inode numbers in the IVs if the filesystem can
104 * potentially renumber inodes, e.g. via filesystem shrinking.
105 */
106 if (!sb->s_cop->has_stable_inodes ||
107 !sb->s_cop->has_stable_inodes(sb)) {
108 fscrypt_warn(inode,
Eric Biggerse3b10782020-05-15 13:41:41 -0700109 "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers",
110 type, sb->s_id);
Eric Biggersb103fb72019-10-24 14:54:36 -0700111 return false;
112 }
113 if (sb->s_cop->get_ino_and_lblk_bits)
114 sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits);
Eric Biggerse3b10782020-05-15 13:41:41 -0700115 if (ino_bits > max_ino_bits) {
Eric Biggersb103fb72019-10-24 14:54:36 -0700116 fscrypt_warn(inode,
Eric Biggerse3b10782020-05-15 13:41:41 -0700117 "Can't use %s policy on filesystem '%s' because its inode numbers are too long",
118 type, sb->s_id);
119 return false;
120 }
121 if (lblk_bits > max_lblk_bits) {
122 fscrypt_warn(inode,
123 "Can't use %s policy on filesystem '%s' because its block numbers are too long",
124 type, sb->s_id);
Eric Biggersb103fb72019-10-24 14:54:36 -0700125 return false;
126 }
127 return true;
128}
129
Eric Biggers393a24a2019-12-09 13:18:26 -0800130static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
131 const struct inode *inode)
132{
133 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
134 policy->filenames_encryption_mode)) {
135 fscrypt_warn(inode,
136 "Unsupported encryption modes (contents %d, filenames %d)",
137 policy->contents_encryption_mode,
138 policy->filenames_encryption_mode);
139 return false;
140 }
141
142 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
143 FSCRYPT_POLICY_FLAG_DIRECT_KEY)) {
144 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
145 policy->flags);
146 return false;
147 }
148
Eric Biggers85af90e2019-12-09 13:18:27 -0800149 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
150 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
151 policy->filenames_encryption_mode))
152 return false;
153
Daniel Rosenberg6e1918c2020-01-20 14:31:56 -0800154 if (IS_CASEFOLDED(inode)) {
155 /* With v1, there's no way to derive dirhash keys. */
156 fscrypt_warn(inode,
157 "v1 policies can't be used on casefolded directories");
158 return false;
159 }
160
Eric Biggers393a24a2019-12-09 13:18:26 -0800161 return true;
162}
163
164static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy,
165 const struct inode *inode)
166{
Eric Biggerse3b10782020-05-15 13:41:41 -0700167 int count = 0;
168
Eric Biggers393a24a2019-12-09 13:18:26 -0800169 if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode,
170 policy->filenames_encryption_mode)) {
171 fscrypt_warn(inode,
172 "Unsupported encryption modes (contents %d, filenames %d)",
173 policy->contents_encryption_mode,
174 policy->filenames_encryption_mode);
175 return false;
176 }
177
Eric Biggers3ceb6542020-10-23 17:51:31 -0700178 if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK |
179 FSCRYPT_POLICY_FLAG_DIRECT_KEY |
180 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
181 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
Eric Biggers393a24a2019-12-09 13:18:26 -0800182 fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)",
183 policy->flags);
184 return false;
185 }
186
Eric Biggerse3b10782020-05-15 13:41:41 -0700187 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY);
188 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64);
189 count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32);
190 if (count > 1) {
191 fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)",
192 policy->flags);
193 return false;
194 }
195
Eric Biggers85af90e2019-12-09 13:18:27 -0800196 if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) &&
197 !supported_direct_key_modes(inode, policy->contents_encryption_mode,
198 policy->filenames_encryption_mode))
199 return false;
200
Eric Biggers393a24a2019-12-09 13:18:26 -0800201 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) &&
Eric Biggerse3b10782020-05-15 13:41:41 -0700202 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64",
203 32, 32))
204 return false;
205
Eric Biggers5e895bd2020-08-24 13:38:41 -0700206 /*
207 * IV_INO_LBLK_32 hashes the inode number, so in principle it can
208 * support any ino_bits. However, currently the inode number is gotten
209 * from inode::i_ino which is 'unsigned long'. So for now the
210 * implementation limit is 32 bits.
211 */
Eric Biggerse3b10782020-05-15 13:41:41 -0700212 if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) &&
Eric Biggerse3b10782020-05-15 13:41:41 -0700213 !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32",
Eric Biggers5e895bd2020-08-24 13:38:41 -0700214 32, 32))
Eric Biggers393a24a2019-12-09 13:18:26 -0800215 return false;
216
217 if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) {
218 fscrypt_warn(inode, "Reserved bits set in encryption policy");
219 return false;
220 }
221
222 return true;
223}
224
Eric Biggers5dae4602019-08-04 19:35:47 -0700225/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700226 * fscrypt_supported_policy() - check whether an encryption policy is supported
227 * @policy_u: the encryption policy
228 * @inode: the inode on which the policy will be used
Eric Biggers5dae4602019-08-04 19:35:47 -0700229 *
230 * Given an encryption policy, check whether all its encryption modes and other
Eric Biggers393a24a2019-12-09 13:18:26 -0800231 * settings are supported by this kernel on the given inode. (But we don't
232 * currently don't check for crypto API support here, so attempting to use an
233 * algorithm not configured into the crypto API will still fail later.)
Eric Biggers5dae4602019-08-04 19:35:47 -0700234 *
235 * Return: %true if supported, else %false
236 */
237bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
238 const struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700239{
Eric Biggers5dae4602019-08-04 19:35:47 -0700240 switch (policy_u->version) {
Eric Biggers393a24a2019-12-09 13:18:26 -0800241 case FSCRYPT_POLICY_V1:
242 return fscrypt_supported_v1_policy(&policy_u->v1, inode);
243 case FSCRYPT_POLICY_V2:
244 return fscrypt_supported_v2_policy(&policy_u->v2, inode);
Eric Biggers5dae4602019-08-04 19:35:47 -0700245 }
246 return false;
247}
248
249/**
Eric Biggersa992b202020-09-16 21:11:24 -0700250 * fscrypt_new_context() - create a new fscrypt_context
Eric Biggersd2fe9752020-05-11 12:13:56 -0700251 * @ctx_u: output context
252 * @policy_u: input policy
Eric Biggersa992b202020-09-16 21:11:24 -0700253 * @nonce: nonce to use
Eric Biggers5dae4602019-08-04 19:35:47 -0700254 *
255 * Create an fscrypt_context for an inode that is being assigned the given
Eric Biggersa992b202020-09-16 21:11:24 -0700256 * encryption policy. @nonce must be a new random nonce.
Eric Biggers5dae4602019-08-04 19:35:47 -0700257 *
258 * Return: the size of the new context in bytes.
259 */
Eric Biggersa992b202020-09-16 21:11:24 -0700260static int fscrypt_new_context(union fscrypt_context *ctx_u,
261 const union fscrypt_policy *policy_u,
262 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
Eric Biggers5dae4602019-08-04 19:35:47 -0700263{
264 memset(ctx_u, 0, sizeof(*ctx_u));
265
266 switch (policy_u->version) {
267 case FSCRYPT_POLICY_V1: {
268 const struct fscrypt_policy_v1 *policy = &policy_u->v1;
269 struct fscrypt_context_v1 *ctx = &ctx_u->v1;
270
271 ctx->version = FSCRYPT_CONTEXT_V1;
272 ctx->contents_encryption_mode =
273 policy->contents_encryption_mode;
274 ctx->filenames_encryption_mode =
275 policy->filenames_encryption_mode;
276 ctx->flags = policy->flags;
277 memcpy(ctx->master_key_descriptor,
278 policy->master_key_descriptor,
279 sizeof(ctx->master_key_descriptor));
Eric Biggersa992b202020-09-16 21:11:24 -0700280 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
Eric Biggers5dae4602019-08-04 19:35:47 -0700281 return sizeof(*ctx);
282 }
283 case FSCRYPT_POLICY_V2: {
284 const struct fscrypt_policy_v2 *policy = &policy_u->v2;
285 struct fscrypt_context_v2 *ctx = &ctx_u->v2;
286
287 ctx->version = FSCRYPT_CONTEXT_V2;
288 ctx->contents_encryption_mode =
289 policy->contents_encryption_mode;
290 ctx->filenames_encryption_mode =
291 policy->filenames_encryption_mode;
292 ctx->flags = policy->flags;
293 memcpy(ctx->master_key_identifier,
294 policy->master_key_identifier,
295 sizeof(ctx->master_key_identifier));
Eric Biggersa992b202020-09-16 21:11:24 -0700296 memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
Eric Biggers5dae4602019-08-04 19:35:47 -0700297 return sizeof(*ctx);
298 }
299 }
300 BUG();
301}
302
303/**
Eric Biggersd2fe9752020-05-11 12:13:56 -0700304 * fscrypt_policy_from_context() - convert an fscrypt_context to
305 * an fscrypt_policy
306 * @policy_u: output policy
307 * @ctx_u: input context
308 * @ctx_size: size of input context in bytes
Eric Biggers5dae4602019-08-04 19:35:47 -0700309 *
310 * Given an fscrypt_context, build the corresponding fscrypt_policy.
311 *
312 * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized
313 * version number or size.
314 *
315 * This does *not* validate the settings within the policy itself, e.g. the
316 * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that.
317 */
318int fscrypt_policy_from_context(union fscrypt_policy *policy_u,
319 const union fscrypt_context *ctx_u,
320 int ctx_size)
321{
322 memset(policy_u, 0, sizeof(*policy_u));
323
Eric Biggerse98ad462020-03-14 13:50:49 -0700324 if (!fscrypt_context_is_valid(ctx_u, ctx_size))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700325 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700326
Eric Biggers5dae4602019-08-04 19:35:47 -0700327 switch (ctx_u->version) {
328 case FSCRYPT_CONTEXT_V1: {
329 const struct fscrypt_context_v1 *ctx = &ctx_u->v1;
330 struct fscrypt_policy_v1 *policy = &policy_u->v1;
331
332 policy->version = FSCRYPT_POLICY_V1;
333 policy->contents_encryption_mode =
334 ctx->contents_encryption_mode;
335 policy->filenames_encryption_mode =
336 ctx->filenames_encryption_mode;
337 policy->flags = ctx->flags;
338 memcpy(policy->master_key_descriptor,
339 ctx->master_key_descriptor,
340 sizeof(policy->master_key_descriptor));
341 return 0;
342 }
343 case FSCRYPT_CONTEXT_V2: {
344 const struct fscrypt_context_v2 *ctx = &ctx_u->v2;
345 struct fscrypt_policy_v2 *policy = &policy_u->v2;
346
347 policy->version = FSCRYPT_POLICY_V2;
348 policy->contents_encryption_mode =
349 ctx->contents_encryption_mode;
350 policy->filenames_encryption_mode =
351 ctx->filenames_encryption_mode;
352 policy->flags = ctx->flags;
353 memcpy(policy->__reserved, ctx->__reserved,
354 sizeof(policy->__reserved));
355 memcpy(policy->master_key_identifier,
356 ctx->master_key_identifier,
357 sizeof(policy->master_key_identifier));
358 return 0;
359 }
360 }
361 /* unreachable */
362 return -EINVAL;
363}
364
365/* Retrieve an inode's encryption policy */
366static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
367{
368 const struct fscrypt_info *ci;
369 union fscrypt_context ctx;
370 int ret;
371
Eric Biggersab673b92020-07-21 15:59:19 -0700372 ci = fscrypt_get_info(inode);
Eric Biggers5dae4602019-08-04 19:35:47 -0700373 if (ci) {
374 /* key available, use the cached policy */
375 *policy = ci->ci_policy;
376 return 0;
377 }
378
379 if (!IS_ENCRYPTED(inode))
380 return -ENODATA;
381
382 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
383 if (ret < 0)
384 return (ret == -ERANGE) ? -EINVAL : ret;
385
386 return fscrypt_policy_from_context(policy, &ctx, ret);
387}
388
389static int set_encryption_policy(struct inode *inode,
390 const union fscrypt_policy *policy)
391{
Eric Biggersa992b202020-09-16 21:11:24 -0700392 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
Eric Biggers5dae4602019-08-04 19:35:47 -0700393 union fscrypt_context ctx;
394 int ctxsize;
Eric Biggers5ab71892019-08-04 19:35:48 -0700395 int err;
Eric Biggers5dae4602019-08-04 19:35:47 -0700396
397 if (!fscrypt_supported_policy(policy, inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700398 return -EINVAL;
399
Eric Biggers5ab71892019-08-04 19:35:48 -0700400 switch (policy->version) {
401 case FSCRYPT_POLICY_V1:
Eric Biggers5dae4602019-08-04 19:35:47 -0700402 /*
403 * The original encryption policy version provided no way of
404 * verifying that the correct master key was supplied, which was
405 * insecure in scenarios where multiple users have access to the
406 * same encrypted files (even just read-only access). The new
407 * encryption policy version fixes this and also implies use of
408 * an improved key derivation function and allows non-root users
409 * to securely remove keys. So as long as compatibility with
410 * old kernels isn't required, it is recommended to use the new
411 * policy version for all new encrypted directories.
412 */
413 pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n",
414 current->comm, current->pid);
Eric Biggers5ab71892019-08-04 19:35:48 -0700415 break;
416 case FSCRYPT_POLICY_V2:
417 err = fscrypt_verify_key_added(inode->i_sb,
418 policy->v2.master_key_identifier);
419 if (err)
420 return err;
Eric Biggerse3b10782020-05-15 13:41:41 -0700421 if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)
422 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",
423 current->comm, current->pid);
Eric Biggers5ab71892019-08-04 19:35:48 -0700424 break;
425 default:
426 WARN_ON(1);
427 return -EINVAL;
Eric Biggers5dae4602019-08-04 19:35:47 -0700428 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700429
Eric Biggersa992b202020-09-16 21:11:24 -0700430 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
431 ctxsize = fscrypt_new_context(&ctx, policy, nonce);
Eric Biggers5dae4602019-08-04 19:35:47 -0700432
433 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700434}
435
Eric Biggersdb717d82016-11-26 19:07:49 -0500436int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700437{
Eric Biggers5dae4602019-08-04 19:35:47 -0700438 union fscrypt_policy policy;
439 union fscrypt_policy existing_policy;
Eric Biggersba63f232016-09-08 14:20:38 -0700440 struct inode *inode = file_inode(filp);
Eric Biggers5dae4602019-08-04 19:35:47 -0700441 u8 version;
442 int size;
Eric Biggersba63f232016-09-08 14:20:38 -0700443 int ret;
444
Eric Biggers5dae4602019-08-04 19:35:47 -0700445 if (get_user(policy.version, (const u8 __user *)arg))
Eric Biggersdb717d82016-11-26 19:07:49 -0500446 return -EFAULT;
447
Eric Biggers5dae4602019-08-04 19:35:47 -0700448 size = fscrypt_policy_size(&policy);
449 if (size <= 0)
450 return -EINVAL;
451
452 /*
453 * We should just copy the remaining 'size - 1' bytes here, but a
454 * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to
455 * think that size can be 0 here (despite the check above!) *and* that
456 * it's a compile-time constant. Thus it would think copy_from_user()
457 * is passed compile-time constant ULONG_MAX, causing the compile-time
458 * buffer overflow check to fail, breaking the build. This only occurred
459 * when building an i386 kernel with -Os and branch profiling enabled.
460 *
461 * Work around it by just copying the first byte again...
462 */
463 version = policy.version;
464 if (copy_from_user(&policy, arg, size))
465 return -EFAULT;
466 policy.version = version;
467
Christian Brauner21cb47b2021-01-21 14:19:25 +0100468 if (!inode_owner_or_capable(&init_user_ns, inode))
Eric Biggers163ae1c2016-09-08 10:57:08 -0700469 return -EACCES;
470
Eric Biggersba63f232016-09-08 14:20:38 -0700471 ret = mnt_want_write_file(filp);
472 if (ret)
473 return ret;
474
Eric Biggers8906a822016-10-15 09:48:50 -0400475 inode_lock(inode);
476
Eric Biggers5dae4602019-08-04 19:35:47 -0700477 ret = fscrypt_get_policy(inode, &existing_policy);
Eric Biggersefee590e2016-12-05 11:12:48 -0800478 if (ret == -ENODATA) {
Eric Biggers002ced42016-09-08 11:36:39 -0700479 if (!S_ISDIR(inode->i_mode))
Eric Biggersdffd0cf2016-12-05 11:12:45 -0800480 ret = -ENOTDIR;
Hongjie Fang5858bda2019-05-22 10:02:53 +0800481 else if (IS_DEADDIR(inode))
482 ret = -ENOENT;
Eric Biggersba63f232016-09-08 14:20:38 -0700483 else if (!inode->i_sb->s_cop->empty_dir(inode))
484 ret = -ENOTEMPTY;
485 else
Eric Biggers5dae4602019-08-04 19:35:47 -0700486 ret = set_encryption_policy(inode, &policy);
487 } else if (ret == -EINVAL ||
488 (ret == 0 && !fscrypt_policies_equal(&policy,
489 &existing_policy))) {
Eric Biggersefee590e2016-12-05 11:12:48 -0800490 /* The file already uses a different encryption policy. */
Eric Biggers8488cd92016-12-05 11:12:46 -0800491 ret = -EEXIST;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700492 }
493
Eric Biggers8906a822016-10-15 09:48:50 -0400494 inode_unlock(inode);
495
Eric Biggersba63f232016-09-08 14:20:38 -0700496 mnt_drop_write_file(filp);
497 return ret;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700498}
Eric Biggersdb717d82016-11-26 19:07:49 -0500499EXPORT_SYMBOL(fscrypt_ioctl_set_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700500
Eric Biggers5dae4602019-08-04 19:35:47 -0700501/* Original ioctl version; can only get the original policy version */
Eric Biggersdb717d82016-11-26 19:07:49 -0500502int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700503{
Eric Biggers5dae4602019-08-04 19:35:47 -0700504 union fscrypt_policy policy;
505 int err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700506
Eric Biggers5dae4602019-08-04 19:35:47 -0700507 err = fscrypt_get_policy(file_inode(filp), &policy);
508 if (err)
509 return err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700510
Eric Biggers5dae4602019-08-04 19:35:47 -0700511 if (policy.version != FSCRYPT_POLICY_V1)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700512 return -EINVAL;
513
Eric Biggers5dae4602019-08-04 19:35:47 -0700514 if (copy_to_user(arg, &policy, sizeof(policy.v1)))
Eric Biggersdb717d82016-11-26 19:07:49 -0500515 return -EFAULT;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700516 return 0;
517}
Eric Biggersdb717d82016-11-26 19:07:49 -0500518EXPORT_SYMBOL(fscrypt_ioctl_get_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700519
Eric Biggers5dae4602019-08-04 19:35:47 -0700520/* Extended ioctl version; can get policies of any version */
521int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg)
522{
523 struct fscrypt_get_policy_ex_arg arg;
524 union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy;
525 size_t policy_size;
526 int err;
527
528 /* arg is policy_size, then policy */
529 BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0);
530 BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) !=
531 offsetof(typeof(arg), policy));
532 BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy));
533
534 err = fscrypt_get_policy(file_inode(filp), policy);
535 if (err)
536 return err;
537 policy_size = fscrypt_policy_size(policy);
538
539 if (copy_from_user(&arg, uarg, sizeof(arg.policy_size)))
540 return -EFAULT;
541
542 if (policy_size > arg.policy_size)
543 return -EOVERFLOW;
544 arg.policy_size = policy_size;
545
546 if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size))
547 return -EFAULT;
548 return 0;
549}
550EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex);
551
Eric Biggerse98ad462020-03-14 13:50:49 -0700552/* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */
553int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
554{
555 struct inode *inode = file_inode(filp);
556 union fscrypt_context ctx;
557 int ret;
558
559 ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
560 if (ret < 0)
561 return ret;
562 if (!fscrypt_context_is_valid(&ctx, ret))
563 return -EINVAL;
564 if (copy_to_user(arg, fscrypt_context_nonce(&ctx),
Eric Biggers1d6217a42020-07-08 14:57:22 -0700565 FSCRYPT_FILE_NONCE_SIZE))
Eric Biggerse98ad462020-03-14 13:50:49 -0700566 return -EFAULT;
567 return 0;
568}
569EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce);
570
Eric Biggers272f98f2017-04-07 10:58:37 -0700571/**
572 * fscrypt_has_permitted_context() - is a file's encryption policy permitted
573 * within its directory?
574 *
575 * @parent: inode for parent directory
576 * @child: inode for file being looked up, opened, or linked into @parent
577 *
578 * Filesystems must call this before permitting access to an inode in a
579 * situation where the parent directory is encrypted (either before allowing
580 * ->lookup() to succeed, or for a regular file before allowing it to be opened)
581 * and before any operation that involves linking an inode into an encrypted
582 * directory, including link, rename, and cross rename. It enforces the
583 * constraint that within a given encrypted directory tree, all files use the
584 * same encryption policy. The pre-access check is needed to detect potentially
585 * malicious offline violations of this constraint, while the link and rename
586 * checks are needed to prevent online violations of this constraint.
587 *
Eric Biggersf5e55e72019-01-22 16:20:21 -0800588 * Return: 1 if permitted, 0 if forbidden.
Eric Biggers272f98f2017-04-07 10:58:37 -0700589 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700590int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
591{
Eric Biggers5dae4602019-08-04 19:35:47 -0700592 union fscrypt_policy parent_policy, child_policy;
Eric Biggersa14d0b62020-12-02 18:20:41 -0800593 int err, err1, err2;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700594
Eric Biggers42d97eb2016-12-19 14:20:13 -0800595 /* No restrictions on file types which are never encrypted */
596 if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) &&
597 !S_ISLNK(child->i_mode))
598 return 1;
599
Eric Biggers272f98f2017-04-07 10:58:37 -0700600 /* No restrictions if the parent directory is unencrypted */
Eric Biggerse0428a22017-10-09 12:15:36 -0700601 if (!IS_ENCRYPTED(parent))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700602 return 1;
Eric Biggers272f98f2017-04-07 10:58:37 -0700603
604 /* Encrypted directories must not contain unencrypted files */
Eric Biggerse0428a22017-10-09 12:15:36 -0700605 if (!IS_ENCRYPTED(child))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700606 return 0;
Eric Biggers272f98f2017-04-07 10:58:37 -0700607
608 /*
609 * Both parent and child are encrypted, so verify they use the same
610 * encryption policy. Compare the fscrypt_info structs if the keys are
611 * available, otherwise retrieve and compare the fscrypt_contexts.
612 *
613 * Note that the fscrypt_context retrieval will be required frequently
614 * when accessing an encrypted directory tree without the key.
615 * Performance-wise this is not a big deal because we already don't
616 * really optimize for file access without the key (to the extent that
617 * such access is even possible), given that any attempted access
618 * already causes a fscrypt_context retrieval and keyring search.
619 *
620 * In any case, if an unexpected error occurs, fall back to "forbidden".
621 */
622
Eric Biggersa14d0b62020-12-02 18:20:41 -0800623 err = fscrypt_get_encryption_info(parent, true);
Eric Biggers5dae4602019-08-04 19:35:47 -0700624 if (err)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700625 return 0;
Eric Biggersa14d0b62020-12-02 18:20:41 -0800626 err = fscrypt_get_encryption_info(child, true);
Eric Biggers5dae4602019-08-04 19:35:47 -0700627 if (err)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700628 return 0;
629
Eric Biggersa14d0b62020-12-02 18:20:41 -0800630 err1 = fscrypt_get_policy(parent, &parent_policy);
631 err2 = fscrypt_get_policy(child, &child_policy);
Eric Biggers272f98f2017-04-07 10:58:37 -0700632
Eric Biggersa14d0b62020-12-02 18:20:41 -0800633 /*
634 * Allow the case where the parent and child both have an unrecognized
635 * encryption policy, so that files with an unrecognized encryption
636 * policy can be deleted.
637 */
638 if (err1 == -EINVAL && err2 == -EINVAL)
639 return 1;
640
641 if (err1 || err2)
Eric Biggers5dae4602019-08-04 19:35:47 -0700642 return 0;
643
644 return fscrypt_policies_equal(&parent_policy, &child_policy);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700645}
646EXPORT_SYMBOL(fscrypt_has_permitted_context);
647
Eric Biggersac4acb12020-09-16 21:11:35 -0700648/*
649 * Return the encryption policy that new files in the directory will inherit, or
650 * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also
651 * ensure that its key is set up, so that the new filename can be encrypted.
652 */
653const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir)
654{
655 int err;
656
657 if (IS_ENCRYPTED(dir)) {
658 err = fscrypt_require_key(dir);
659 if (err)
660 return ERR_PTR(err);
661 return &dir->i_crypt_info->ci_policy;
662 }
663
664 return fscrypt_get_dummy_policy(dir->i_sb);
665}
666
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700667/**
Eric Biggersa992b202020-09-16 21:11:24 -0700668 * fscrypt_set_context() - Set the fscrypt context of a new inode
669 * @inode: a new inode
670 * @fs_data: private data given by FS and passed to ->set_context()
671 *
672 * This should be called after fscrypt_prepare_new_inode(), generally during a
673 * filesystem transaction. Everything here must be %GFP_NOFS-safe.
674 *
675 * Return: 0 on success, -errno on failure
676 */
677int fscrypt_set_context(struct inode *inode, void *fs_data)
678{
679 struct fscrypt_info *ci = inode->i_crypt_info;
680 union fscrypt_context ctx;
681 int ctxsize;
682
683 /* fscrypt_prepare_new_inode() should have set up the key already. */
684 if (WARN_ON_ONCE(!ci))
685 return -ENOKEY;
686
687 BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
688 ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce);
689
690 /*
691 * This may be the first time the inode number is available, so do any
692 * delayed key setup that requires the inode number.
693 */
694 if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
695 (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
696 const struct fscrypt_master_key *mk =
697 ci->ci_master_key->payload.data[0];
698
699 fscrypt_hash_inode_number(ci, mk);
700 }
701
702 return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
703}
704EXPORT_SYMBOL_GPL(fscrypt_set_context);
705
706/**
Eric Biggersed318a62020-05-12 16:32:50 -0700707 * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
708 * @sb: the filesystem on which test_dummy_encryption is being specified
Eric Biggersc8c868a2020-09-16 21:11:36 -0700709 * @arg: the argument to the test_dummy_encryption option. May be NULL.
Eric Biggersac4acb12020-09-16 21:11:35 -0700710 * @dummy_policy: the filesystem's current dummy policy (input/output, see
711 * below)
Eric Biggersed318a62020-05-12 16:32:50 -0700712 *
713 * Handle the test_dummy_encryption mount option by creating a dummy encryption
Eric Biggersac4acb12020-09-16 21:11:35 -0700714 * policy, saving it in @dummy_policy, and adding the corresponding dummy
715 * encryption key to the filesystem. If the @dummy_policy is already set, then
Eric Biggersed318a62020-05-12 16:32:50 -0700716 * instead validate that it matches @arg. Don't support changing it via
717 * remount, as that is difficult to do safely.
718 *
Eric Biggersac4acb12020-09-16 21:11:35 -0700719 * Return: 0 on success (dummy policy set, or the same policy is already set);
720 * -EEXIST if a different dummy policy is already set;
Eric Biggersed318a62020-05-12 16:32:50 -0700721 * or another -errno value.
722 */
Eric Biggersc8c868a2020-09-16 21:11:36 -0700723int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
Eric Biggersac4acb12020-09-16 21:11:35 -0700724 struct fscrypt_dummy_policy *dummy_policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700725{
Eric Biggersed318a62020-05-12 16:32:50 -0700726 struct fscrypt_key_specifier key_spec = { 0 };
727 int version;
Eric Biggersac4acb12020-09-16 21:11:35 -0700728 union fscrypt_policy *policy = NULL;
Eric Biggersed318a62020-05-12 16:32:50 -0700729 int err;
730
Eric Biggersc8c868a2020-09-16 21:11:36 -0700731 if (!arg)
732 arg = "v2";
Eric Biggersed318a62020-05-12 16:32:50 -0700733
Eric Biggersc8c868a2020-09-16 21:11:36 -0700734 if (!strcmp(arg, "v1")) {
Eric Biggersac4acb12020-09-16 21:11:35 -0700735 version = FSCRYPT_POLICY_V1;
Eric Biggersed318a62020-05-12 16:32:50 -0700736 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
737 memset(key_spec.u.descriptor, 0x42,
738 FSCRYPT_KEY_DESCRIPTOR_SIZE);
Eric Biggersc8c868a2020-09-16 21:11:36 -0700739 } else if (!strcmp(arg, "v2")) {
Eric Biggersac4acb12020-09-16 21:11:35 -0700740 version = FSCRYPT_POLICY_V2;
Eric Biggersed318a62020-05-12 16:32:50 -0700741 key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
742 /* key_spec.u.identifier gets filled in when adding the key */
743 } else {
744 err = -EINVAL;
745 goto out;
746 }
747
Eric Biggersac4acb12020-09-16 21:11:35 -0700748 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
749 if (!policy) {
Eric Biggersed318a62020-05-12 16:32:50 -0700750 err = -ENOMEM;
751 goto out;
752 }
753
754 err = fscrypt_add_test_dummy_key(sb, &key_spec);
755 if (err)
756 goto out;
757
Eric Biggersac4acb12020-09-16 21:11:35 -0700758 policy->version = version;
759 switch (policy->version) {
760 case FSCRYPT_POLICY_V1:
761 policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
762 policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
763 memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor,
Eric Biggersed318a62020-05-12 16:32:50 -0700764 FSCRYPT_KEY_DESCRIPTOR_SIZE);
765 break;
Eric Biggersac4acb12020-09-16 21:11:35 -0700766 case FSCRYPT_POLICY_V2:
767 policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
768 policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
769 memcpy(policy->v2.master_key_identifier, key_spec.u.identifier,
Eric Biggersed318a62020-05-12 16:32:50 -0700770 FSCRYPT_KEY_IDENTIFIER_SIZE);
771 break;
772 default:
773 WARN_ON(1);
774 err = -EINVAL;
775 goto out;
776 }
Eric Biggersac4acb12020-09-16 21:11:35 -0700777
778 if (dummy_policy->policy) {
779 if (fscrypt_policies_equal(policy, dummy_policy->policy))
780 err = 0;
781 else
782 err = -EEXIST;
783 goto out;
784 }
785 dummy_policy->policy = policy;
786 policy = NULL;
Eric Biggersed318a62020-05-12 16:32:50 -0700787 err = 0;
788out:
Eric Biggersac4acb12020-09-16 21:11:35 -0700789 kfree(policy);
Eric Biggersed318a62020-05-12 16:32:50 -0700790 return err;
791}
792EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption);
793
794/**
795 * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption'
796 * @seq: the seq_file to print the option to
797 * @sep: the separator character to use
798 * @sb: the filesystem whose options are being shown
799 *
800 * Show the test_dummy_encryption mount option, if it was specified.
801 * This is mainly used for /proc/mounts.
802 */
803void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
804 struct super_block *sb)
805{
Eric Biggersac4acb12020-09-16 21:11:35 -0700806 const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb);
807 int vers;
Eric Biggersed318a62020-05-12 16:32:50 -0700808
Eric Biggersac4acb12020-09-16 21:11:35 -0700809 if (!policy)
Eric Biggersed318a62020-05-12 16:32:50 -0700810 return;
Eric Biggersac4acb12020-09-16 21:11:35 -0700811
812 vers = policy->version;
813 if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */
814 vers = 1;
815
816 seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers);
Eric Biggersed318a62020-05-12 16:32:50 -0700817}
818EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption);