1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Key setup facility for FS encryption support.
4 *
5 * Copyright (C) 2015, Google, Inc.
6 *
7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
9 */
10
11 #include <crypto/skcipher.h>
12 #include <linux/key.h>
13
14 #include "fscrypt_private.h"
15
16 struct fscrypt_mode fscrypt_modes[] = {
17 [FSCRYPT_MODE_AES_256_XTS] = {
18 .friendly_name = "AES-256-XTS",
19 .cipher_str = "xts(aes)",
20 .keysize = 64,
21 .ivsize = 16,
22 .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_256_XTS,
23 },
24 [FSCRYPT_MODE_AES_256_CTS] = {
25 .friendly_name = "AES-256-CTS-CBC",
26 .cipher_str = "cts(cbc(aes))",
27 .keysize = 32,
28 .ivsize = 16,
29 },
30 [FSCRYPT_MODE_AES_128_CBC] = {
31 .friendly_name = "AES-128-CBC-ESSIV",
32 .cipher_str = "essiv(cbc(aes),sha256)",
33 .keysize = 16,
34 .ivsize = 16,
35 .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
36 },
37 [FSCRYPT_MODE_AES_128_CTS] = {
38 .friendly_name = "AES-128-CTS-CBC",
39 .cipher_str = "cts(cbc(aes))",
40 .keysize = 16,
41 .ivsize = 16,
42 },
43 [FSCRYPT_MODE_ADIANTUM] = {
44 .friendly_name = "Adiantum",
45 .cipher_str = "adiantum(xchacha12,aes)",
46 .keysize = 32,
47 .ivsize = 32,
48 .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM,
49 },
50 };
51
52 static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex);
53
54 static struct fscrypt_mode *
select_encryption_mode(const union fscrypt_policy * policy,const struct inode * inode)55 select_encryption_mode(const union fscrypt_policy *policy,
56 const struct inode *inode)
57 {
58 BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes) != FSCRYPT_MODE_MAX + 1);
59
60 if (S_ISREG(inode->i_mode))
61 return &fscrypt_modes[fscrypt_policy_contents_mode(policy)];
62
63 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
64 return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)];
65
66 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
67 inode->i_ino, (inode->i_mode & S_IFMT));
68 return ERR_PTR(-EINVAL);
69 }
70
71 /* Create a symmetric cipher object for the given encryption mode and key */
72 static struct crypto_skcipher *
fscrypt_allocate_skcipher(struct fscrypt_mode * mode,const u8 * raw_key,const struct inode * inode)73 fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
74 const struct inode *inode)
75 {
76 struct crypto_skcipher *tfm;
77 int err;
78
79 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
80 if (IS_ERR(tfm)) {
81 if (PTR_ERR(tfm) == -ENOENT) {
82 fscrypt_warn(inode,
83 "Missing crypto API support for %s (API name: \"%s\")",
84 mode->friendly_name, mode->cipher_str);
85 return ERR_PTR(-ENOPKG);
86 }
87 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
88 mode->cipher_str, PTR_ERR(tfm));
89 return tfm;
90 }
91 if (!xchg(&mode->logged_impl_name, 1)) {
92 /*
93 * fscrypt performance can vary greatly depending on which
94 * crypto algorithm implementation is used. Help people debug
95 * performance problems by logging the ->cra_driver_name the
96 * first time a mode is used.
97 */
98 pr_info("fscrypt: %s using implementation \"%s\"\n",
99 mode->friendly_name, crypto_skcipher_driver_name(tfm));
100 }
101 if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
102 err = -EINVAL;
103 goto err_free_tfm;
104 }
105 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
106 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
107 if (err)
108 goto err_free_tfm;
109
110 return tfm;
111
112 err_free_tfm:
113 crypto_free_skcipher(tfm);
114 return ERR_PTR(err);
115 }
116
117 /*
118 * Prepare the crypto transform object or blk-crypto key in @prep_key, given the
119 * raw key, encryption mode, and flag indicating which encryption implementation
120 * (fs-layer or blk-crypto) will be used.
121 */
fscrypt_prepare_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,unsigned int raw_key_size,bool is_hw_wrapped,const struct fscrypt_info * ci)122 int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
123 const u8 *raw_key, unsigned int raw_key_size,
124 bool is_hw_wrapped, const struct fscrypt_info *ci)
125 {
126 struct crypto_skcipher *tfm;
127
128 if (fscrypt_using_inline_encryption(ci))
129 return fscrypt_prepare_inline_crypt_key(prep_key,
130 raw_key, raw_key_size, is_hw_wrapped, ci);
131
132 if (WARN_ON(is_hw_wrapped || raw_key_size != ci->ci_mode->keysize))
133 return -EINVAL;
134
135 tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
136 if (IS_ERR(tfm))
137 return PTR_ERR(tfm);
138 /*
139 * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
140 * I.e., here we publish ->tfm with a RELEASE barrier so that
141 * concurrent tasks can ACQUIRE it. Note that this concurrency is only
142 * possible for per-mode keys, not for per-file keys.
143 */
144 smp_store_release(&prep_key->tfm, tfm);
145 return 0;
146 }
147
148 /* Destroy a crypto transform object and/or blk-crypto key. */
fscrypt_destroy_prepared_key(struct fscrypt_prepared_key * prep_key)149 void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key)
150 {
151 crypto_free_skcipher(prep_key->tfm);
152 fscrypt_destroy_inline_crypt_key(prep_key);
153 }
154
155 /* Given a per-file encryption key, set up the file's crypto transform object */
fscrypt_set_per_file_enc_key(struct fscrypt_info * ci,const u8 * raw_key)156 int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key)
157 {
158 ci->ci_owns_key = true;
159 return fscrypt_prepare_key(&ci->ci_key, raw_key, ci->ci_mode->keysize,
160 false /*is_hw_wrapped*/, ci);
161 }
162
setup_per_mode_enc_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk,struct fscrypt_prepared_key * keys,u8 hkdf_context,bool include_fs_uuid)163 static int setup_per_mode_enc_key(struct fscrypt_info *ci,
164 struct fscrypt_master_key *mk,
165 struct fscrypt_prepared_key *keys,
166 u8 hkdf_context, bool include_fs_uuid)
167 {
168 const struct inode *inode = ci->ci_inode;
169 const struct super_block *sb = inode->i_sb;
170 struct fscrypt_mode *mode = ci->ci_mode;
171 const u8 mode_num = mode - fscrypt_modes;
172 struct fscrypt_prepared_key *prep_key;
173 u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
174 u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
175 unsigned int hkdf_infolen = 0;
176 int err;
177
178 if (WARN_ON(mode_num > FSCRYPT_MODE_MAX))
179 return -EINVAL;
180
181 prep_key = &keys[mode_num];
182 if (fscrypt_is_key_prepared(prep_key, ci)) {
183 ci->ci_key = *prep_key;
184 return 0;
185 }
186
187 mutex_lock(&fscrypt_mode_key_setup_mutex);
188
189 if (fscrypt_is_key_prepared(prep_key, ci))
190 goto done_unlock;
191
192 if (mk->mk_secret.is_hw_wrapped && S_ISREG(inode->i_mode)) {
193 int i;
194
195 if (!fscrypt_using_inline_encryption(ci)) {
196 fscrypt_warn(ci->ci_inode,
197 "Hardware-wrapped keys require inline encryption (-o inlinecrypt)");
198 err = -EINVAL;
199 goto out_unlock;
200 }
201 for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
202 if (fscrypt_is_key_prepared(&keys[i], ci)) {
203 fscrypt_warn(ci->ci_inode,
204 "Each hardware-wrapped key can only be used with one encryption mode");
205 err = -EINVAL;
206 goto out_unlock;
207 }
208 }
209 err = fscrypt_prepare_key(prep_key, mk->mk_secret.raw,
210 mk->mk_secret.size, true, ci);
211 if (err)
212 goto out_unlock;
213 } else {
214 BUILD_BUG_ON(sizeof(mode_num) != 1);
215 BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
216 BUILD_BUG_ON(sizeof(hkdf_info) != 17);
217 hkdf_info[hkdf_infolen++] = mode_num;
218 if (include_fs_uuid) {
219 memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
220 sizeof(sb->s_uuid));
221 hkdf_infolen += sizeof(sb->s_uuid);
222 }
223 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
224 hkdf_context, hkdf_info, hkdf_infolen,
225 mode_key, mode->keysize);
226 if (err)
227 goto out_unlock;
228 err = fscrypt_prepare_key(prep_key, mode_key, mode->keysize,
229 false /*is_hw_wrapped*/, ci);
230 memzero_explicit(mode_key, mode->keysize);
231 if (err)
232 goto out_unlock;
233 }
234 done_unlock:
235 ci->ci_key = *prep_key;
236
237 err = 0;
238 out_unlock:
239 mutex_unlock(&fscrypt_mode_key_setup_mutex);
240 return err;
241 }
242
243 /*
244 * Derive a SipHash key from the given fscrypt master key and the given
245 * application-specific information string.
246 *
247 * Note that the KDF produces a byte array, but the SipHash APIs expect the key
248 * as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an
249 * endianness swap in order to get the same results as on little endian CPUs.
250 */
fscrypt_derive_siphash_key(const struct fscrypt_master_key * mk,u8 context,const u8 * info,unsigned int infolen,siphash_key_t * key)251 static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk,
252 u8 context, const u8 *info,
253 unsigned int infolen, siphash_key_t *key)
254 {
255 int err;
256
257 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen,
258 (u8 *)key, sizeof(*key));
259 if (err)
260 return err;
261
262 BUILD_BUG_ON(sizeof(*key) != 16);
263 BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2);
264 le64_to_cpus(&key->key[0]);
265 le64_to_cpus(&key->key[1]);
266 return 0;
267 }
268
fscrypt_derive_dirhash_key(struct fscrypt_info * ci,const struct fscrypt_master_key * mk)269 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
270 const struct fscrypt_master_key *mk)
271 {
272 int err;
273
274 err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY,
275 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
276 &ci->ci_dirhash_key);
277 if (err)
278 return err;
279 ci->ci_dirhash_key_initialized = true;
280 return 0;
281 }
282
fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk)283 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
284 struct fscrypt_master_key *mk)
285 {
286 int err;
287
288 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys,
289 HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true);
290 if (err)
291 return err;
292
293 /* pairs with smp_store_release() below */
294 if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) {
295
296 mutex_lock(&fscrypt_mode_key_setup_mutex);
297
298 if (mk->mk_ino_hash_key_initialized)
299 goto unlock;
300
301 err = fscrypt_derive_siphash_key(mk,
302 HKDF_CONTEXT_INODE_HASH_KEY,
303 NULL, 0, &mk->mk_ino_hash_key);
304 if (err)
305 goto unlock;
306 /* pairs with smp_load_acquire() above */
307 smp_store_release(&mk->mk_ino_hash_key_initialized, true);
308 unlock:
309 mutex_unlock(&fscrypt_mode_key_setup_mutex);
310 if (err)
311 return err;
312 }
313
314 ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
315 &mk->mk_ino_hash_key);
316 return 0;
317 }
318
fscrypt_setup_v2_file_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk)319 static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
320 struct fscrypt_master_key *mk)
321 {
322 int err;
323
324 if (mk->mk_secret.is_hw_wrapped &&
325 !(ci->ci_policy.v2.flags & (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
326 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))) {
327 fscrypt_warn(ci->ci_inode,
328 "Hardware-wrapped keys are only supported with IV_INO_LBLK policies");
329 return -EINVAL;
330 }
331
332 if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
333 /*
334 * DIRECT_KEY: instead of deriving per-file encryption keys, the
335 * per-file nonce will be included in all the IVs. But unlike
336 * v1 policies, for v2 policies in this case we don't encrypt
337 * with the master key directly but rather derive a per-mode
338 * encryption key. This ensures that the master key is
339 * consistently used only for HKDF, avoiding key reuse issues.
340 */
341 err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys,
342 HKDF_CONTEXT_DIRECT_KEY, false);
343 } else if (ci->ci_policy.v2.flags &
344 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
345 /*
346 * IV_INO_LBLK_64: encryption keys are derived from (master_key,
347 * mode_num, filesystem_uuid), and inode number is included in
348 * the IVs. This format is optimized for use with inline
349 * encryption hardware compliant with the UFS standard.
350 */
351 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys,
352 HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
353 true);
354 } else if (ci->ci_policy.v2.flags &
355 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
356 err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk);
357 } else {
358 u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
359
360 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
361 HKDF_CONTEXT_PER_FILE_ENC_KEY,
362 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
363 derived_key, ci->ci_mode->keysize);
364 if (err)
365 return err;
366
367 err = fscrypt_set_per_file_enc_key(ci, derived_key);
368 memzero_explicit(derived_key, ci->ci_mode->keysize);
369 }
370 if (err)
371 return err;
372
373 /* Derive a secret dirhash key for directories that need it. */
374 if (S_ISDIR(ci->ci_inode->i_mode) && IS_CASEFOLDED(ci->ci_inode)) {
375 err = fscrypt_derive_dirhash_key(ci, mk);
376 if (err)
377 return err;
378 }
379
380 return 0;
381 }
382
383 /*
384 * Find the master key, then set up the inode's actual encryption key.
385 *
386 * If the master key is found in the filesystem-level keyring, then the
387 * corresponding 'struct key' is returned in *master_key_ret with
388 * ->mk_secret_sem read-locked. This is needed to ensure that only one task
389 * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
390 * to create an fscrypt_info for the same inode), and to synchronize the master
391 * key being removed with a new inode starting to use it.
392 */
setup_file_encryption_key(struct fscrypt_info * ci,struct key ** master_key_ret)393 static int setup_file_encryption_key(struct fscrypt_info *ci,
394 struct key **master_key_ret)
395 {
396 struct key *key;
397 struct fscrypt_master_key *mk = NULL;
398 struct fscrypt_key_specifier mk_spec;
399 int err;
400
401 switch (ci->ci_policy.version) {
402 case FSCRYPT_POLICY_V1:
403 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
404 memcpy(mk_spec.u.descriptor,
405 ci->ci_policy.v1.master_key_descriptor,
406 FSCRYPT_KEY_DESCRIPTOR_SIZE);
407 break;
408 case FSCRYPT_POLICY_V2:
409 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
410 memcpy(mk_spec.u.identifier,
411 ci->ci_policy.v2.master_key_identifier,
412 FSCRYPT_KEY_IDENTIFIER_SIZE);
413 break;
414 default:
415 WARN_ON(1);
416 return -EINVAL;
417 }
418
419 key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
420 if (IS_ERR(key)) {
421 if (key != ERR_PTR(-ENOKEY) ||
422 ci->ci_policy.version != FSCRYPT_POLICY_V1)
423 return PTR_ERR(key);
424
425 err = fscrypt_select_encryption_impl(ci, false);
426 if (err)
427 return err;
428
429 /*
430 * As a legacy fallback for v1 policies, search for the key in
431 * the current task's subscribed keyrings too. Don't move this
432 * to before the search of ->s_master_keys, since users
433 * shouldn't be able to override filesystem-level keys.
434 */
435 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
436 }
437
438 mk = key->payload.data[0];
439 down_read(&mk->mk_secret_sem);
440
441 /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
442 if (!is_master_key_secret_present(&mk->mk_secret)) {
443 err = -ENOKEY;
444 goto out_release_key;
445 }
446
447 /*
448 * Require that the master key be at least as long as the derived key.
449 * Otherwise, the derived key cannot possibly contain as much entropy as
450 * that required by the encryption mode it will be used for. For v1
451 * policies it's also required for the KDF to work at all.
452 */
453 if (mk->mk_secret.size < ci->ci_mode->keysize) {
454 fscrypt_warn(NULL,
455 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
456 master_key_spec_type(&mk_spec),
457 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
458 mk->mk_secret.size, ci->ci_mode->keysize);
459 err = -ENOKEY;
460 goto out_release_key;
461 }
462
463 err = fscrypt_select_encryption_impl(ci, mk->mk_secret.is_hw_wrapped);
464 if (err)
465 goto out_release_key;
466
467 switch (ci->ci_policy.version) {
468 case FSCRYPT_POLICY_V1:
469 err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
470 break;
471 case FSCRYPT_POLICY_V2:
472 err = fscrypt_setup_v2_file_key(ci, mk);
473 break;
474 default:
475 WARN_ON(1);
476 err = -EINVAL;
477 break;
478 }
479 if (err)
480 goto out_release_key;
481
482 *master_key_ret = key;
483 return 0;
484
485 out_release_key:
486 up_read(&mk->mk_secret_sem);
487 key_put(key);
488 return err;
489 }
490
put_crypt_info(struct fscrypt_info * ci)491 static void put_crypt_info(struct fscrypt_info *ci)
492 {
493 struct key *key;
494
495 if (!ci)
496 return;
497
498 if (ci->ci_direct_key)
499 fscrypt_put_direct_key(ci->ci_direct_key);
500 else if (ci->ci_owns_key)
501 fscrypt_destroy_prepared_key(&ci->ci_key);
502
503 key = ci->ci_master_key;
504 if (key) {
505 struct fscrypt_master_key *mk = key->payload.data[0];
506
507 /*
508 * Remove this inode from the list of inodes that were unlocked
509 * with the master key.
510 *
511 * In addition, if we're removing the last inode from a key that
512 * already had its secret removed, invalidate the key so that it
513 * gets removed from ->s_master_keys.
514 */
515 spin_lock(&mk->mk_decrypted_inodes_lock);
516 list_del(&ci->ci_master_key_link);
517 spin_unlock(&mk->mk_decrypted_inodes_lock);
518 if (refcount_dec_and_test(&mk->mk_refcount))
519 key_invalidate(key);
520 key_put(key);
521 }
522 memzero_explicit(ci, sizeof(*ci));
523 kmem_cache_free(fscrypt_info_cachep, ci);
524 }
525
fscrypt_get_encryption_info(struct inode * inode)526 int fscrypt_get_encryption_info(struct inode *inode)
527 {
528 struct fscrypt_info *crypt_info;
529 union fscrypt_context ctx;
530 struct fscrypt_mode *mode;
531 struct key *master_key = NULL;
532 int res;
533
534 if (fscrypt_has_encryption_key(inode))
535 return 0;
536
537 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
538 if (res)
539 return res;
540
541 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
542 if (res < 0) {
543 const union fscrypt_context *dummy_ctx =
544 fscrypt_get_dummy_context(inode->i_sb);
545
546 if (IS_ENCRYPTED(inode) || !dummy_ctx) {
547 fscrypt_warn(inode,
548 "Error %d getting encryption context",
549 res);
550 return res;
551 }
552 /* Fake up a context for an unencrypted directory */
553 res = fscrypt_context_size(dummy_ctx);
554 memcpy(&ctx, dummy_ctx, res);
555 }
556
557 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
558 if (!crypt_info)
559 return -ENOMEM;
560
561 crypt_info->ci_inode = inode;
562
563 res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
564 if (res) {
565 fscrypt_warn(inode,
566 "Unrecognized or corrupt encryption context");
567 goto out;
568 }
569
570 memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
571 FSCRYPT_FILE_NONCE_SIZE);
572
573 if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
574 res = -EINVAL;
575 goto out;
576 }
577
578 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
579 if (IS_ERR(mode)) {
580 res = PTR_ERR(mode);
581 goto out;
582 }
583 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
584 crypt_info->ci_mode = mode;
585
586 res = setup_file_encryption_key(crypt_info, &master_key);
587 if (res)
588 goto out;
589
590 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
591 if (master_key) {
592 struct fscrypt_master_key *mk =
593 master_key->payload.data[0];
594
595 refcount_inc(&mk->mk_refcount);
596 crypt_info->ci_master_key = key_get(master_key);
597 spin_lock(&mk->mk_decrypted_inodes_lock);
598 list_add(&crypt_info->ci_master_key_link,
599 &mk->mk_decrypted_inodes);
600 spin_unlock(&mk->mk_decrypted_inodes_lock);
601 }
602 crypt_info = NULL;
603 }
604 res = 0;
605 out:
606 if (master_key) {
607 struct fscrypt_master_key *mk = master_key->payload.data[0];
608
609 up_read(&mk->mk_secret_sem);
610 key_put(master_key);
611 }
612 if (res == -ENOKEY)
613 res = 0;
614 put_crypt_info(crypt_info);
615 return res;
616 }
617 EXPORT_SYMBOL(fscrypt_get_encryption_info);
618
619 /**
620 * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
621 * @inode: an inode being evicted
622 *
623 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
624 * being evicted. An RCU grace period need not have elapsed yet.
625 */
fscrypt_put_encryption_info(struct inode * inode)626 void fscrypt_put_encryption_info(struct inode *inode)
627 {
628 put_crypt_info(inode->i_crypt_info);
629 inode->i_crypt_info = NULL;
630 }
631 EXPORT_SYMBOL(fscrypt_put_encryption_info);
632
633 /**
634 * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay
635 * @inode: an inode being freed
636 *
637 * Free the inode's cached decrypted symlink target, if any. Filesystems must
638 * call this after an RCU grace period, just before they free the inode.
639 */
fscrypt_free_inode(struct inode * inode)640 void fscrypt_free_inode(struct inode *inode)
641 {
642 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
643 kfree(inode->i_link);
644 inode->i_link = NULL;
645 }
646 }
647 EXPORT_SYMBOL(fscrypt_free_inode);
648
649 /**
650 * fscrypt_drop_inode() - check whether the inode's master key has been removed
651 * @inode: an inode being considered for eviction
652 *
653 * Filesystems supporting fscrypt must call this from their ->drop_inode()
654 * method so that encrypted inodes are evicted as soon as they're no longer in
655 * use and their master key has been removed.
656 *
657 * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
658 */
fscrypt_drop_inode(struct inode * inode)659 int fscrypt_drop_inode(struct inode *inode)
660 {
661 const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info);
662 const struct fscrypt_master_key *mk;
663
664 /*
665 * If ci is NULL, then the inode doesn't have an encryption key set up
666 * so it's irrelevant. If ci_master_key is NULL, then the master key
667 * was provided via the legacy mechanism of the process-subscribed
668 * keyrings, so we don't know whether it's been removed or not.
669 */
670 if (!ci || !ci->ci_master_key)
671 return 0;
672 mk = ci->ci_master_key->payload.data[0];
673
674 /*
675 * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes
676 * protected by the key were cleaned by sync_filesystem(). But if
677 * userspace is still using the files, inodes can be dirtied between
678 * then and now. We mustn't lose any writes, so skip dirty inodes here.
679 */
680 if (inode->i_state & I_DIRTY_ALL)
681 return 0;
682
683 /*
684 * Note: since we aren't holding ->mk_secret_sem, the result here can
685 * immediately become outdated. But there's no correctness problem with
686 * unnecessarily evicting. Nor is there a correctness problem with not
687 * evicting while iput() is racing with the key being removed, since
688 * then the thread removing the key will either evict the inode itself
689 * or will correctly detect that it wasn't evicted due to the race.
690 */
691 return !is_master_key_secret_present(&mk->mk_secret);
692 }
693 EXPORT_SYMBOL_GPL(fscrypt_drop_inode);
694