]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | #include <linux/random.h> | |
ed318a6c | 14 | #include <linux/seq_file.h> |
0b81d077 | 15 | #include <linux/string.h> |
ba63f23d | 16 | #include <linux/mount.h> |
cc4e0df0 | 17 | #include "fscrypt_private.h" |
0b81d077 | 18 | |
5dae460c | 19 | /** |
d2fe9754 EB |
20 | * fscrypt_policies_equal() - check whether two encryption policies are the same |
21 | * @policy1: the first policy | |
22 | * @policy2: the second policy | |
5dae460c EB |
23 | * |
24 | * Return: %true if equal, else %false | |
0b81d077 | 25 | */ |
5dae460c EB |
26 | bool fscrypt_policies_equal(const union fscrypt_policy *policy1, |
27 | const union fscrypt_policy *policy2) | |
0b81d077 | 28 | { |
5dae460c EB |
29 | if (policy1->version != policy2->version) |
30 | return false; | |
31 | ||
32 | return !memcmp(policy1, policy2, fscrypt_policy_size(policy1)); | |
0b81d077 JK |
33 | } |
34 | ||
ac4acb1f EB |
35 | static const union fscrypt_policy * |
36 | fscrypt_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 | ||
ef5b18b0 EB |
43 | static 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 | ||
85af90e5 EB |
60 | static 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 | ||
e3b1078b EB |
80 | static 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) | |
b103fb76 EB |
84 | { |
85 | struct super_block *sb = inode->i_sb; | |
86 | int ino_bits = 64, lblk_bits = 64; | |
87 | ||
f000223c EB |
88 | /* |
89 | * 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 | ||
b103fb76 EB |
102 | /* |
103 | * 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, | |
e3b1078b EB |
109 | "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers", |
110 | type, sb->s_id); | |
b103fb76 EB |
111 | 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); | |
e3b1078b EB |
115 | if (ino_bits > max_ino_bits) { |
116 | fscrypt_warn(inode, | |
117 | "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) { | |
b103fb76 | 122 | fscrypt_warn(inode, |
e3b1078b EB |
123 | "Can't use %s policy on filesystem '%s' because its block numbers are too long", |
124 | type, sb->s_id); | |
b103fb76 EB |
125 | return false; |
126 | } | |
127 | return true; | |
128 | } | |
129 | ||
393a24a7 EB |
130 | static 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 | ||
85af90e5 EB |
149 | 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 | ||
6e1918cf DR |
154 | 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 | ||
393a24a7 EB |
161 | return true; |
162 | } | |
163 | ||
164 | static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy, | |
165 | const struct inode *inode) | |
166 | { | |
e3b1078b EB |
167 | int count = 0; |
168 | ||
393a24a7 EB |
169 | 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 | ||
178 | if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) { | |
179 | fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", | |
180 | policy->flags); | |
181 | return false; | |
182 | } | |
183 | ||
e3b1078b EB |
184 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY); |
185 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64); | |
186 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32); | |
187 | if (count > 1) { | |
188 | fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)", | |
189 | policy->flags); | |
190 | return false; | |
191 | } | |
192 | ||
85af90e5 EB |
193 | if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && |
194 | !supported_direct_key_modes(inode, policy->contents_encryption_mode, | |
195 | policy->filenames_encryption_mode)) | |
196 | return false; | |
197 | ||
393a24a7 | 198 | if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) && |
e3b1078b EB |
199 | !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64", |
200 | 32, 32)) | |
201 | return false; | |
202 | ||
5e895bd4 EB |
203 | /* |
204 | * IV_INO_LBLK_32 hashes the inode number, so in principle it can | |
205 | * support any ino_bits. However, currently the inode number is gotten | |
206 | * from inode::i_ino which is 'unsigned long'. So for now the | |
207 | * implementation limit is 32 bits. | |
208 | */ | |
e3b1078b | 209 | if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) && |
e3b1078b | 210 | !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32", |
5e895bd4 | 211 | 32, 32)) |
393a24a7 EB |
212 | return false; |
213 | ||
214 | if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) { | |
215 | fscrypt_warn(inode, "Reserved bits set in encryption policy"); | |
216 | return false; | |
217 | } | |
218 | ||
219 | return true; | |
220 | } | |
221 | ||
5dae460c | 222 | /** |
d2fe9754 EB |
223 | * fscrypt_supported_policy() - check whether an encryption policy is supported |
224 | * @policy_u: the encryption policy | |
225 | * @inode: the inode on which the policy will be used | |
5dae460c EB |
226 | * |
227 | * Given an encryption policy, check whether all its encryption modes and other | |
393a24a7 EB |
228 | * settings are supported by this kernel on the given inode. (But we don't |
229 | * currently don't check for crypto API support here, so attempting to use an | |
230 | * algorithm not configured into the crypto API will still fail later.) | |
5dae460c EB |
231 | * |
232 | * Return: %true if supported, else %false | |
233 | */ | |
234 | bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, | |
235 | const struct inode *inode) | |
0b81d077 | 236 | { |
5dae460c | 237 | switch (policy_u->version) { |
393a24a7 EB |
238 | case FSCRYPT_POLICY_V1: |
239 | return fscrypt_supported_v1_policy(&policy_u->v1, inode); | |
240 | case FSCRYPT_POLICY_V2: | |
241 | return fscrypt_supported_v2_policy(&policy_u->v2, inode); | |
5dae460c EB |
242 | } |
243 | return false; | |
244 | } | |
245 | ||
246 | /** | |
a992b20c | 247 | * fscrypt_new_context() - create a new fscrypt_context |
d2fe9754 EB |
248 | * @ctx_u: output context |
249 | * @policy_u: input policy | |
a992b20c | 250 | * @nonce: nonce to use |
5dae460c EB |
251 | * |
252 | * Create an fscrypt_context for an inode that is being assigned the given | |
a992b20c | 253 | * encryption policy. @nonce must be a new random nonce. |
5dae460c EB |
254 | * |
255 | * Return: the size of the new context in bytes. | |
256 | */ | |
a992b20c EB |
257 | static int fscrypt_new_context(union fscrypt_context *ctx_u, |
258 | const union fscrypt_policy *policy_u, | |
259 | const u8 nonce[FSCRYPT_FILE_NONCE_SIZE]) | |
5dae460c EB |
260 | { |
261 | memset(ctx_u, 0, sizeof(*ctx_u)); | |
262 | ||
263 | switch (policy_u->version) { | |
264 | case FSCRYPT_POLICY_V1: { | |
265 | const struct fscrypt_policy_v1 *policy = &policy_u->v1; | |
266 | struct fscrypt_context_v1 *ctx = &ctx_u->v1; | |
267 | ||
268 | ctx->version = FSCRYPT_CONTEXT_V1; | |
269 | ctx->contents_encryption_mode = | |
270 | policy->contents_encryption_mode; | |
271 | ctx->filenames_encryption_mode = | |
272 | policy->filenames_encryption_mode; | |
273 | ctx->flags = policy->flags; | |
274 | memcpy(ctx->master_key_descriptor, | |
275 | policy->master_key_descriptor, | |
276 | sizeof(ctx->master_key_descriptor)); | |
a992b20c | 277 | memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); |
5dae460c EB |
278 | return sizeof(*ctx); |
279 | } | |
280 | case FSCRYPT_POLICY_V2: { | |
281 | const struct fscrypt_policy_v2 *policy = &policy_u->v2; | |
282 | struct fscrypt_context_v2 *ctx = &ctx_u->v2; | |
283 | ||
284 | ctx->version = FSCRYPT_CONTEXT_V2; | |
285 | ctx->contents_encryption_mode = | |
286 | policy->contents_encryption_mode; | |
287 | ctx->filenames_encryption_mode = | |
288 | policy->filenames_encryption_mode; | |
289 | ctx->flags = policy->flags; | |
290 | memcpy(ctx->master_key_identifier, | |
291 | policy->master_key_identifier, | |
292 | sizeof(ctx->master_key_identifier)); | |
a992b20c | 293 | memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); |
5dae460c EB |
294 | return sizeof(*ctx); |
295 | } | |
296 | } | |
297 | BUG(); | |
298 | } | |
0b81d077 | 299 | |
5dae460c | 300 | /** |
d2fe9754 EB |
301 | * fscrypt_policy_from_context() - convert an fscrypt_context to |
302 | * an fscrypt_policy | |
303 | * @policy_u: output policy | |
304 | * @ctx_u: input context | |
305 | * @ctx_size: size of input context in bytes | |
5dae460c EB |
306 | * |
307 | * Given an fscrypt_context, build the corresponding fscrypt_policy. | |
308 | * | |
309 | * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized | |
310 | * version number or size. | |
311 | * | |
312 | * This does *not* validate the settings within the policy itself, e.g. the | |
313 | * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that. | |
314 | */ | |
315 | int fscrypt_policy_from_context(union fscrypt_policy *policy_u, | |
316 | const union fscrypt_context *ctx_u, | |
317 | int ctx_size) | |
318 | { | |
319 | memset(policy_u, 0, sizeof(*policy_u)); | |
0b81d077 | 320 | |
e98ad464 | 321 | if (!fscrypt_context_is_valid(ctx_u, ctx_size)) |
0b81d077 | 322 | return -EINVAL; |
0b81d077 | 323 | |
5dae460c EB |
324 | switch (ctx_u->version) { |
325 | case FSCRYPT_CONTEXT_V1: { | |
326 | const struct fscrypt_context_v1 *ctx = &ctx_u->v1; | |
327 | struct fscrypt_policy_v1 *policy = &policy_u->v1; | |
328 | ||
329 | policy->version = FSCRYPT_POLICY_V1; | |
330 | policy->contents_encryption_mode = | |
331 | ctx->contents_encryption_mode; | |
332 | policy->filenames_encryption_mode = | |
333 | ctx->filenames_encryption_mode; | |
334 | policy->flags = ctx->flags; | |
335 | memcpy(policy->master_key_descriptor, | |
336 | ctx->master_key_descriptor, | |
337 | sizeof(policy->master_key_descriptor)); | |
338 | return 0; | |
339 | } | |
340 | case FSCRYPT_CONTEXT_V2: { | |
341 | const struct fscrypt_context_v2 *ctx = &ctx_u->v2; | |
342 | struct fscrypt_policy_v2 *policy = &policy_u->v2; | |
343 | ||
344 | policy->version = FSCRYPT_POLICY_V2; | |
345 | policy->contents_encryption_mode = | |
346 | ctx->contents_encryption_mode; | |
347 | policy->filenames_encryption_mode = | |
348 | ctx->filenames_encryption_mode; | |
349 | policy->flags = ctx->flags; | |
350 | memcpy(policy->__reserved, ctx->__reserved, | |
351 | sizeof(policy->__reserved)); | |
352 | memcpy(policy->master_key_identifier, | |
353 | ctx->master_key_identifier, | |
354 | sizeof(policy->master_key_identifier)); | |
355 | return 0; | |
356 | } | |
357 | } | |
358 | /* unreachable */ | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | /* Retrieve an inode's encryption policy */ | |
363 | static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy) | |
364 | { | |
365 | const struct fscrypt_info *ci; | |
366 | union fscrypt_context ctx; | |
367 | int ret; | |
368 | ||
ab673b98 | 369 | ci = fscrypt_get_info(inode); |
5dae460c EB |
370 | if (ci) { |
371 | /* key available, use the cached policy */ | |
372 | *policy = ci->ci_policy; | |
373 | return 0; | |
374 | } | |
375 | ||
376 | if (!IS_ENCRYPTED(inode)) | |
377 | return -ENODATA; | |
378 | ||
379 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | |
380 | if (ret < 0) | |
381 | return (ret == -ERANGE) ? -EINVAL : ret; | |
382 | ||
383 | return fscrypt_policy_from_context(policy, &ctx, ret); | |
384 | } | |
385 | ||
386 | static int set_encryption_policy(struct inode *inode, | |
387 | const union fscrypt_policy *policy) | |
388 | { | |
a992b20c | 389 | u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; |
5dae460c EB |
390 | union fscrypt_context ctx; |
391 | int ctxsize; | |
5ab7189a | 392 | int err; |
5dae460c EB |
393 | |
394 | if (!fscrypt_supported_policy(policy, inode)) | |
0b81d077 JK |
395 | return -EINVAL; |
396 | ||
5ab7189a EB |
397 | switch (policy->version) { |
398 | case FSCRYPT_POLICY_V1: | |
5dae460c EB |
399 | /* |
400 | * The original encryption policy version provided no way of | |
401 | * verifying that the correct master key was supplied, which was | |
402 | * insecure in scenarios where multiple users have access to the | |
403 | * same encrypted files (even just read-only access). The new | |
404 | * encryption policy version fixes this and also implies use of | |
405 | * an improved key derivation function and allows non-root users | |
406 | * to securely remove keys. So as long as compatibility with | |
407 | * old kernels isn't required, it is recommended to use the new | |
408 | * policy version for all new encrypted directories. | |
409 | */ | |
410 | pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n", | |
411 | current->comm, current->pid); | |
5ab7189a EB |
412 | break; |
413 | case FSCRYPT_POLICY_V2: | |
414 | err = fscrypt_verify_key_added(inode->i_sb, | |
415 | policy->v2.master_key_identifier); | |
416 | if (err) | |
417 | return err; | |
e3b1078b EB |
418 | if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) |
419 | 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", | |
420 | current->comm, current->pid); | |
5ab7189a EB |
421 | break; |
422 | default: | |
423 | WARN_ON(1); | |
424 | return -EINVAL; | |
5dae460c | 425 | } |
0b81d077 | 426 | |
a992b20c EB |
427 | get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE); |
428 | ctxsize = fscrypt_new_context(&ctx, policy, nonce); | |
5dae460c EB |
429 | |
430 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); | |
0b81d077 JK |
431 | } |
432 | ||
db717d8e | 433 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
0b81d077 | 434 | { |
5dae460c EB |
435 | union fscrypt_policy policy; |
436 | union fscrypt_policy existing_policy; | |
ba63f23d | 437 | struct inode *inode = file_inode(filp); |
5dae460c EB |
438 | u8 version; |
439 | int size; | |
ba63f23d EB |
440 | int ret; |
441 | ||
5dae460c | 442 | if (get_user(policy.version, (const u8 __user *)arg)) |
db717d8e EB |
443 | return -EFAULT; |
444 | ||
5dae460c EB |
445 | size = fscrypt_policy_size(&policy); |
446 | if (size <= 0) | |
447 | return -EINVAL; | |
448 | ||
449 | /* | |
450 | * We should just copy the remaining 'size - 1' bytes here, but a | |
451 | * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to | |
452 | * think that size can be 0 here (despite the check above!) *and* that | |
453 | * it's a compile-time constant. Thus it would think copy_from_user() | |
454 | * is passed compile-time constant ULONG_MAX, causing the compile-time | |
455 | * buffer overflow check to fail, breaking the build. This only occurred | |
456 | * when building an i386 kernel with -Os and branch profiling enabled. | |
457 | * | |
458 | * Work around it by just copying the first byte again... | |
459 | */ | |
460 | version = policy.version; | |
461 | if (copy_from_user(&policy, arg, size)) | |
462 | return -EFAULT; | |
463 | policy.version = version; | |
464 | ||
163ae1c6 EB |
465 | if (!inode_owner_or_capable(inode)) |
466 | return -EACCES; | |
467 | ||
ba63f23d EB |
468 | ret = mnt_want_write_file(filp); |
469 | if (ret) | |
470 | return ret; | |
471 | ||
8906a822 EB |
472 | inode_lock(inode); |
473 | ||
5dae460c | 474 | ret = fscrypt_get_policy(inode, &existing_policy); |
efee590e | 475 | if (ret == -ENODATA) { |
002ced4b | 476 | if (!S_ISDIR(inode->i_mode)) |
dffd0cfa | 477 | ret = -ENOTDIR; |
5858bdad HF |
478 | else if (IS_DEADDIR(inode)) |
479 | ret = -ENOENT; | |
ba63f23d EB |
480 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
481 | ret = -ENOTEMPTY; | |
482 | else | |
5dae460c EB |
483 | ret = set_encryption_policy(inode, &policy); |
484 | } else if (ret == -EINVAL || | |
485 | (ret == 0 && !fscrypt_policies_equal(&policy, | |
486 | &existing_policy))) { | |
efee590e | 487 | /* The file already uses a different encryption policy. */ |
8488cd96 | 488 | ret = -EEXIST; |
0b81d077 JK |
489 | } |
490 | ||
8906a822 EB |
491 | inode_unlock(inode); |
492 | ||
ba63f23d EB |
493 | mnt_drop_write_file(filp); |
494 | return ret; | |
0b81d077 | 495 | } |
db717d8e | 496 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
0b81d077 | 497 | |
5dae460c | 498 | /* Original ioctl version; can only get the original policy version */ |
db717d8e | 499 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
0b81d077 | 500 | { |
5dae460c EB |
501 | union fscrypt_policy policy; |
502 | int err; | |
0b81d077 | 503 | |
5dae460c EB |
504 | err = fscrypt_get_policy(file_inode(filp), &policy); |
505 | if (err) | |
506 | return err; | |
0b81d077 | 507 | |
5dae460c | 508 | if (policy.version != FSCRYPT_POLICY_V1) |
0b81d077 JK |
509 | return -EINVAL; |
510 | ||
5dae460c | 511 | if (copy_to_user(arg, &policy, sizeof(policy.v1))) |
db717d8e | 512 | return -EFAULT; |
0b81d077 JK |
513 | return 0; |
514 | } | |
db717d8e | 515 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
0b81d077 | 516 | |
5dae460c EB |
517 | /* Extended ioctl version; can get policies of any version */ |
518 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg) | |
519 | { | |
520 | struct fscrypt_get_policy_ex_arg arg; | |
521 | union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy; | |
522 | size_t policy_size; | |
523 | int err; | |
524 | ||
525 | /* arg is policy_size, then policy */ | |
526 | BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0); | |
527 | BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) != | |
528 | offsetof(typeof(arg), policy)); | |
529 | BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy)); | |
530 | ||
531 | err = fscrypt_get_policy(file_inode(filp), policy); | |
532 | if (err) | |
533 | return err; | |
534 | policy_size = fscrypt_policy_size(policy); | |
535 | ||
536 | if (copy_from_user(&arg, uarg, sizeof(arg.policy_size))) | |
537 | return -EFAULT; | |
538 | ||
539 | if (policy_size > arg.policy_size) | |
540 | return -EOVERFLOW; | |
541 | arg.policy_size = policy_size; | |
542 | ||
543 | if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size)) | |
544 | return -EFAULT; | |
545 | return 0; | |
546 | } | |
547 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex); | |
548 | ||
e98ad464 EB |
549 | /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */ |
550 | int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) | |
551 | { | |
552 | struct inode *inode = file_inode(filp); | |
553 | union fscrypt_context ctx; | |
554 | int ret; | |
555 | ||
556 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | |
557 | if (ret < 0) | |
558 | return ret; | |
559 | if (!fscrypt_context_is_valid(&ctx, ret)) | |
560 | return -EINVAL; | |
561 | if (copy_to_user(arg, fscrypt_context_nonce(&ctx), | |
1d6217a4 | 562 | FSCRYPT_FILE_NONCE_SIZE)) |
e98ad464 EB |
563 | return -EFAULT; |
564 | return 0; | |
565 | } | |
566 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce); | |
567 | ||
272f98f6 EB |
568 | /** |
569 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted | |
570 | * within its directory? | |
571 | * | |
572 | * @parent: inode for parent directory | |
573 | * @child: inode for file being looked up, opened, or linked into @parent | |
574 | * | |
575 | * Filesystems must call this before permitting access to an inode in a | |
576 | * situation where the parent directory is encrypted (either before allowing | |
577 | * ->lookup() to succeed, or for a regular file before allowing it to be opened) | |
578 | * and before any operation that involves linking an inode into an encrypted | |
579 | * directory, including link, rename, and cross rename. It enforces the | |
580 | * constraint that within a given encrypted directory tree, all files use the | |
581 | * same encryption policy. The pre-access check is needed to detect potentially | |
582 | * malicious offline violations of this constraint, while the link and rename | |
583 | * checks are needed to prevent online violations of this constraint. | |
584 | * | |
f5e55e77 | 585 | * Return: 1 if permitted, 0 if forbidden. |
272f98f6 | 586 | */ |
0b81d077 JK |
587 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
588 | { | |
5dae460c EB |
589 | union fscrypt_policy parent_policy, child_policy; |
590 | int err; | |
0b81d077 | 591 | |
42d97eb0 EB |
592 | /* No restrictions on file types which are never encrypted */ |
593 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && | |
594 | !S_ISLNK(child->i_mode)) | |
595 | return 1; | |
596 | ||
272f98f6 | 597 | /* No restrictions if the parent directory is unencrypted */ |
e0428a26 | 598 | if (!IS_ENCRYPTED(parent)) |
0b81d077 | 599 | return 1; |
272f98f6 EB |
600 | |
601 | /* Encrypted directories must not contain unencrypted files */ | |
e0428a26 | 602 | if (!IS_ENCRYPTED(child)) |
0b81d077 | 603 | return 0; |
272f98f6 EB |
604 | |
605 | /* | |
606 | * Both parent and child are encrypted, so verify they use the same | |
607 | * encryption policy. Compare the fscrypt_info structs if the keys are | |
608 | * available, otherwise retrieve and compare the fscrypt_contexts. | |
609 | * | |
610 | * Note that the fscrypt_context retrieval will be required frequently | |
611 | * when accessing an encrypted directory tree without the key. | |
612 | * Performance-wise this is not a big deal because we already don't | |
613 | * really optimize for file access without the key (to the extent that | |
614 | * such access is even possible), given that any attempted access | |
615 | * already causes a fscrypt_context retrieval and keyring search. | |
616 | * | |
617 | * In any case, if an unexpected error occurs, fall back to "forbidden". | |
618 | */ | |
619 | ||
5dae460c EB |
620 | err = fscrypt_get_encryption_info(parent); |
621 | if (err) | |
0b81d077 | 622 | return 0; |
5dae460c EB |
623 | err = fscrypt_get_encryption_info(child); |
624 | if (err) | |
0b81d077 | 625 | return 0; |
272f98f6 | 626 | |
5dae460c EB |
627 | err = fscrypt_get_policy(parent, &parent_policy); |
628 | if (err) | |
272f98f6 EB |
629 | return 0; |
630 | ||
5dae460c EB |
631 | err = fscrypt_get_policy(child, &child_policy); |
632 | if (err) | |
0b81d077 JK |
633 | return 0; |
634 | ||
5dae460c | 635 | return fscrypt_policies_equal(&parent_policy, &child_policy); |
0b81d077 JK |
636 | } |
637 | EXPORT_SYMBOL(fscrypt_has_permitted_context); | |
638 | ||
ac4acb1f EB |
639 | /* |
640 | * Return the encryption policy that new files in the directory will inherit, or | |
641 | * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also | |
642 | * ensure that its key is set up, so that the new filename can be encrypted. | |
643 | */ | |
644 | const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir) | |
645 | { | |
646 | int err; | |
647 | ||
648 | if (IS_ENCRYPTED(dir)) { | |
649 | err = fscrypt_require_key(dir); | |
650 | if (err) | |
651 | return ERR_PTR(err); | |
652 | return &dir->i_crypt_info->ci_policy; | |
653 | } | |
654 | ||
655 | return fscrypt_get_dummy_policy(dir->i_sb); | |
656 | } | |
657 | ||
a992b20c EB |
658 | /** |
659 | * fscrypt_set_context() - Set the fscrypt context of a new inode | |
660 | * @inode: a new inode | |
661 | * @fs_data: private data given by FS and passed to ->set_context() | |
662 | * | |
663 | * This should be called after fscrypt_prepare_new_inode(), generally during a | |
664 | * filesystem transaction. Everything here must be %GFP_NOFS-safe. | |
665 | * | |
666 | * Return: 0 on success, -errno on failure | |
667 | */ | |
668 | int fscrypt_set_context(struct inode *inode, void *fs_data) | |
669 | { | |
670 | struct fscrypt_info *ci = inode->i_crypt_info; | |
671 | union fscrypt_context ctx; | |
672 | int ctxsize; | |
673 | ||
674 | /* fscrypt_prepare_new_inode() should have set up the key already. */ | |
675 | if (WARN_ON_ONCE(!ci)) | |
676 | return -ENOKEY; | |
677 | ||
678 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); | |
679 | ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce); | |
680 | ||
681 | /* | |
682 | * This may be the first time the inode number is available, so do any | |
683 | * delayed key setup that requires the inode number. | |
684 | */ | |
685 | if (ci->ci_policy.version == FSCRYPT_POLICY_V2 && | |
686 | (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { | |
687 | const struct fscrypt_master_key *mk = | |
688 | ci->ci_master_key->payload.data[0]; | |
689 | ||
690 | fscrypt_hash_inode_number(ci, mk); | |
691 | } | |
692 | ||
693 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data); | |
694 | } | |
695 | EXPORT_SYMBOL_GPL(fscrypt_set_context); | |
696 | ||
ed318a6c EB |
697 | /** |
698 | * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption' | |
699 | * @sb: the filesystem on which test_dummy_encryption is being specified | |
c8c868ab | 700 | * @arg: the argument to the test_dummy_encryption option. May be NULL. |
ac4acb1f EB |
701 | * @dummy_policy: the filesystem's current dummy policy (input/output, see |
702 | * below) | |
ed318a6c EB |
703 | * |
704 | * Handle the test_dummy_encryption mount option by creating a dummy encryption | |
ac4acb1f EB |
705 | * policy, saving it in @dummy_policy, and adding the corresponding dummy |
706 | * encryption key to the filesystem. If the @dummy_policy is already set, then | |
ed318a6c EB |
707 | * instead validate that it matches @arg. Don't support changing it via |
708 | * remount, as that is difficult to do safely. | |
709 | * | |
ac4acb1f EB |
710 | * Return: 0 on success (dummy policy set, or the same policy is already set); |
711 | * -EEXIST if a different dummy policy is already set; | |
ed318a6c EB |
712 | * or another -errno value. |
713 | */ | |
c8c868ab | 714 | int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg, |
ac4acb1f | 715 | struct fscrypt_dummy_policy *dummy_policy) |
ed318a6c | 716 | { |
ed318a6c EB |
717 | struct fscrypt_key_specifier key_spec = { 0 }; |
718 | int version; | |
ac4acb1f | 719 | union fscrypt_policy *policy = NULL; |
ed318a6c EB |
720 | int err; |
721 | ||
c8c868ab EB |
722 | if (!arg) |
723 | arg = "v2"; | |
ed318a6c | 724 | |
c8c868ab | 725 | if (!strcmp(arg, "v1")) { |
ac4acb1f | 726 | version = FSCRYPT_POLICY_V1; |
ed318a6c EB |
727 | key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
728 | memset(key_spec.u.descriptor, 0x42, | |
729 | FSCRYPT_KEY_DESCRIPTOR_SIZE); | |
c8c868ab | 730 | } else if (!strcmp(arg, "v2")) { |
ac4acb1f | 731 | version = FSCRYPT_POLICY_V2; |
ed318a6c EB |
732 | key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
733 | /* key_spec.u.identifier gets filled in when adding the key */ | |
734 | } else { | |
735 | err = -EINVAL; | |
736 | goto out; | |
737 | } | |
738 | ||
ac4acb1f EB |
739 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
740 | if (!policy) { | |
ed318a6c EB |
741 | err = -ENOMEM; |
742 | goto out; | |
743 | } | |
744 | ||
745 | err = fscrypt_add_test_dummy_key(sb, &key_spec); | |
746 | if (err) | |
747 | goto out; | |
748 | ||
ac4acb1f EB |
749 | policy->version = version; |
750 | switch (policy->version) { | |
751 | case FSCRYPT_POLICY_V1: | |
752 | policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; | |
753 | policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; | |
754 | memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor, | |
ed318a6c EB |
755 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
756 | break; | |
ac4acb1f EB |
757 | case FSCRYPT_POLICY_V2: |
758 | policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; | |
759 | policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; | |
760 | memcpy(policy->v2.master_key_identifier, key_spec.u.identifier, | |
ed318a6c EB |
761 | FSCRYPT_KEY_IDENTIFIER_SIZE); |
762 | break; | |
763 | default: | |
764 | WARN_ON(1); | |
765 | err = -EINVAL; | |
766 | goto out; | |
767 | } | |
ac4acb1f EB |
768 | |
769 | if (dummy_policy->policy) { | |
770 | if (fscrypt_policies_equal(policy, dummy_policy->policy)) | |
771 | err = 0; | |
772 | else | |
773 | err = -EEXIST; | |
774 | goto out; | |
775 | } | |
776 | dummy_policy->policy = policy; | |
777 | policy = NULL; | |
ed318a6c EB |
778 | err = 0; |
779 | out: | |
ac4acb1f | 780 | kfree(policy); |
ed318a6c EB |
781 | return err; |
782 | } | |
783 | EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption); | |
784 | ||
785 | /** | |
786 | * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption' | |
787 | * @seq: the seq_file to print the option to | |
788 | * @sep: the separator character to use | |
789 | * @sb: the filesystem whose options are being shown | |
790 | * | |
791 | * Show the test_dummy_encryption mount option, if it was specified. | |
792 | * This is mainly used for /proc/mounts. | |
793 | */ | |
794 | void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, | |
795 | struct super_block *sb) | |
796 | { | |
ac4acb1f EB |
797 | const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb); |
798 | int vers; | |
ed318a6c | 799 | |
ac4acb1f | 800 | if (!policy) |
ed318a6c | 801 | return; |
ac4acb1f EB |
802 | |
803 | vers = policy->version; | |
804 | if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */ | |
805 | vers = 1; | |
806 | ||
807 | seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers); | |
ed318a6c EB |
808 | } |
809 | EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption); |