1
2
3
4
5
6
7
8
9
10
11#include <crypto/skcipher.h>
12#include <linux/key.h>
13#include <linux/random.h>
14
15#include "fscrypt_private.h"
16
17struct fscrypt_mode fscrypt_modes[] = {
18 [FSCRYPT_MODE_AES_256_XTS] = {
19 .friendly_name = "AES-256-XTS",
20 .cipher_str = "xts(aes)",
21 .keysize = 64,
22 .ivsize = 16,
23 .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_256_XTS,
24 },
25 [FSCRYPT_MODE_AES_256_CTS] = {
26 .friendly_name = "AES-256-CTS-CBC",
27 .cipher_str = "cts(cbc(aes))",
28 .keysize = 32,
29 .ivsize = 16,
30 },
31 [FSCRYPT_MODE_AES_128_CBC] = {
32 .friendly_name = "AES-128-CBC-ESSIV",
33 .cipher_str = "essiv(cbc(aes),sha256)",
34 .keysize = 16,
35 .ivsize = 16,
36 .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
37 },
38 [FSCRYPT_MODE_AES_128_CTS] = {
39 .friendly_name = "AES-128-CTS-CBC",
40 .cipher_str = "cts(cbc(aes))",
41 .keysize = 16,
42 .ivsize = 16,
43 },
44 [FSCRYPT_MODE_ADIANTUM] = {
45 .friendly_name = "Adiantum",
46 .cipher_str = "adiantum(xchacha12,aes)",
47 .keysize = 32,
48 .ivsize = 32,
49 .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM,
50 },
51};
52
53static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex);
54
55static struct fscrypt_mode *
56select_encryption_mode(const union fscrypt_policy *policy,
57 const struct inode *inode)
58{
59 BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes) != FSCRYPT_MODE_MAX + 1);
60
61 if (S_ISREG(inode->i_mode))
62 return &fscrypt_modes[fscrypt_policy_contents_mode(policy)];
63
64 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
65 return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)];
66
67 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
68 inode->i_ino, (inode->i_mode & S_IFMT));
69 return ERR_PTR(-EINVAL);
70}
71
72
73static struct crypto_skcipher *
74fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
75 const struct inode *inode)
76{
77 struct crypto_skcipher *tfm;
78 int err;
79
80 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
81 if (IS_ERR(tfm)) {
82 if (PTR_ERR(tfm) == -ENOENT) {
83 fscrypt_warn(inode,
84 "Missing crypto API support for %s (API name: \"%s\")",
85 mode->friendly_name, mode->cipher_str);
86 return ERR_PTR(-ENOPKG);
87 }
88 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
89 mode->cipher_str, PTR_ERR(tfm));
90 return tfm;
91 }
92 if (!xchg(&mode->logged_impl_name, 1)) {
93
94
95
96
97
98
99 pr_info("fscrypt: %s using implementation \"%s\"\n",
100 mode->friendly_name, crypto_skcipher_driver_name(tfm));
101 }
102 if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
103 err = -EINVAL;
104 goto err_free_tfm;
105 }
106 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
107 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
108 if (err)
109 goto err_free_tfm;
110
111 return tfm;
112
113err_free_tfm:
114 crypto_free_skcipher(tfm);
115 return ERR_PTR(err);
116}
117
118
119
120
121
122
123int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
124 const u8 *raw_key, 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, raw_key, ci);
130
131 tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
132 if (IS_ERR(tfm))
133 return PTR_ERR(tfm);
134
135
136
137
138
139
140 smp_store_release(&prep_key->tfm, tfm);
141 return 0;
142}
143
144
145void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key)
146{
147 crypto_free_skcipher(prep_key->tfm);
148 fscrypt_destroy_inline_crypt_key(prep_key);
149}
150
151
152int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key)
153{
154 ci->ci_owns_key = true;
155 return fscrypt_prepare_key(&ci->ci_enc_key, raw_key, ci);
156}
157
158static int setup_per_mode_enc_key(struct fscrypt_info *ci,
159 struct fscrypt_master_key *mk,
160 struct fscrypt_prepared_key *keys,
161 u8 hkdf_context, bool include_fs_uuid)
162{
163 const struct inode *inode = ci->ci_inode;
164 const struct super_block *sb = inode->i_sb;
165 struct fscrypt_mode *mode = ci->ci_mode;
166 const u8 mode_num = mode - fscrypt_modes;
167 struct fscrypt_prepared_key *prep_key;
168 u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
169 u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
170 unsigned int hkdf_infolen = 0;
171 int err;
172
173 if (WARN_ON(mode_num > FSCRYPT_MODE_MAX))
174 return -EINVAL;
175
176 prep_key = &keys[mode_num];
177 if (fscrypt_is_key_prepared(prep_key, ci)) {
178 ci->ci_enc_key = *prep_key;
179 return 0;
180 }
181
182 mutex_lock(&fscrypt_mode_key_setup_mutex);
183
184 if (fscrypt_is_key_prepared(prep_key, ci))
185 goto done_unlock;
186
187 BUILD_BUG_ON(sizeof(mode_num) != 1);
188 BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
189 BUILD_BUG_ON(sizeof(hkdf_info) != 17);
190 hkdf_info[hkdf_infolen++] = mode_num;
191 if (include_fs_uuid) {
192 memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
193 sizeof(sb->s_uuid));
194 hkdf_infolen += sizeof(sb->s_uuid);
195 }
196 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
197 hkdf_context, hkdf_info, hkdf_infolen,
198 mode_key, mode->keysize);
199 if (err)
200 goto out_unlock;
201 err = fscrypt_prepare_key(prep_key, mode_key, ci);
202 memzero_explicit(mode_key, mode->keysize);
203 if (err)
204 goto out_unlock;
205done_unlock:
206 ci->ci_enc_key = *prep_key;
207 err = 0;
208out_unlock:
209 mutex_unlock(&fscrypt_mode_key_setup_mutex);
210 return err;
211}
212
213
214
215
216
217
218
219
220
221static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk,
222 u8 context, const u8 *info,
223 unsigned int infolen, siphash_key_t *key)
224{
225 int err;
226
227 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen,
228 (u8 *)key, sizeof(*key));
229 if (err)
230 return err;
231
232 BUILD_BUG_ON(sizeof(*key) != 16);
233 BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2);
234 le64_to_cpus(&key->key[0]);
235 le64_to_cpus(&key->key[1]);
236 return 0;
237}
238
239int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
240 const struct fscrypt_master_key *mk)
241{
242 int err;
243
244 err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY,
245 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
246 &ci->ci_dirhash_key);
247 if (err)
248 return err;
249 ci->ci_dirhash_key_initialized = true;
250 return 0;
251}
252
253void fscrypt_hash_inode_number(struct fscrypt_info *ci,
254 const struct fscrypt_master_key *mk)
255{
256 WARN_ON(ci->ci_inode->i_ino == 0);
257 WARN_ON(!mk->mk_ino_hash_key_initialized);
258
259 ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
260 &mk->mk_ino_hash_key);
261}
262
263static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
264 struct fscrypt_master_key *mk)
265{
266 int err;
267
268 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys,
269 HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true);
270 if (err)
271 return err;
272
273
274 if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) {
275
276 mutex_lock(&fscrypt_mode_key_setup_mutex);
277
278 if (mk->mk_ino_hash_key_initialized)
279 goto unlock;
280
281 err = fscrypt_derive_siphash_key(mk,
282 HKDF_CONTEXT_INODE_HASH_KEY,
283 NULL, 0, &mk->mk_ino_hash_key);
284 if (err)
285 goto unlock;
286
287 smp_store_release(&mk->mk_ino_hash_key_initialized, true);
288unlock:
289 mutex_unlock(&fscrypt_mode_key_setup_mutex);
290 if (err)
291 return err;
292 }
293
294
295
296
297
298 if (ci->ci_inode->i_ino)
299 fscrypt_hash_inode_number(ci, mk);
300 return 0;
301}
302
303static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
304 struct fscrypt_master_key *mk,
305 bool need_dirhash_key)
306{
307 int err;
308
309 if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
310
311
312
313
314
315
316
317
318 err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys,
319 HKDF_CONTEXT_DIRECT_KEY, false);
320 } else if (ci->ci_policy.v2.flags &
321 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
322
323
324
325
326
327
328 err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys,
329 HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
330 true);
331 } else if (ci->ci_policy.v2.flags &
332 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
333 err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk);
334 } else {
335 u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
336
337 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
338 HKDF_CONTEXT_PER_FILE_ENC_KEY,
339 ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
340 derived_key, ci->ci_mode->keysize);
341 if (err)
342 return err;
343
344 err = fscrypt_set_per_file_enc_key(ci, derived_key);
345 memzero_explicit(derived_key, ci->ci_mode->keysize);
346 }
347 if (err)
348 return err;
349
350
351 if (need_dirhash_key) {
352 err = fscrypt_derive_dirhash_key(ci, mk);
353 if (err)
354 return err;
355 }
356
357 return 0;
358}
359
360
361
362
363
364
365
366
367
368
369
370static int setup_file_encryption_key(struct fscrypt_info *ci,
371 bool need_dirhash_key,
372 struct key **master_key_ret)
373{
374 struct key *key;
375 struct fscrypt_master_key *mk = NULL;
376 struct fscrypt_key_specifier mk_spec;
377 int err;
378
379 err = fscrypt_select_encryption_impl(ci);
380 if (err)
381 return err;
382
383 switch (ci->ci_policy.version) {
384 case FSCRYPT_POLICY_V1:
385 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
386 memcpy(mk_spec.u.descriptor,
387 ci->ci_policy.v1.master_key_descriptor,
388 FSCRYPT_KEY_DESCRIPTOR_SIZE);
389 break;
390 case FSCRYPT_POLICY_V2:
391 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
392 memcpy(mk_spec.u.identifier,
393 ci->ci_policy.v2.master_key_identifier,
394 FSCRYPT_KEY_IDENTIFIER_SIZE);
395 break;
396 default:
397 WARN_ON(1);
398 return -EINVAL;
399 }
400
401 key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
402 if (IS_ERR(key)) {
403 if (key != ERR_PTR(-ENOKEY) ||
404 ci->ci_policy.version != FSCRYPT_POLICY_V1)
405 return PTR_ERR(key);
406
407
408
409
410
411
412
413 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
414 }
415
416 mk = key->payload.data[0];
417 down_read(&key->sem);
418
419
420 if (!is_master_key_secret_present(&mk->mk_secret)) {
421 err = -ENOKEY;
422 goto out_release_key;
423 }
424
425
426
427
428
429
430
431 if (mk->mk_secret.size < ci->ci_mode->keysize) {
432 fscrypt_warn(NULL,
433 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
434 master_key_spec_type(&mk_spec),
435 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
436 mk->mk_secret.size, ci->ci_mode->keysize);
437 err = -ENOKEY;
438 goto out_release_key;
439 }
440
441 switch (ci->ci_policy.version) {
442 case FSCRYPT_POLICY_V1:
443 err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
444 break;
445 case FSCRYPT_POLICY_V2:
446 err = fscrypt_setup_v2_file_key(ci, mk, need_dirhash_key);
447 break;
448 default:
449 WARN_ON(1);
450 err = -EINVAL;
451 break;
452 }
453 if (err)
454 goto out_release_key;
455
456 *master_key_ret = key;
457 return 0;
458
459out_release_key:
460 up_read(&key->sem);
461 key_put(key);
462 return err;
463}
464
465static void put_crypt_info(struct fscrypt_info *ci)
466{
467 struct key *key;
468
469 if (!ci)
470 return;
471
472 if (ci->ci_direct_key)
473 fscrypt_put_direct_key(ci->ci_direct_key);
474 else if (ci->ci_owns_key)
475 fscrypt_destroy_prepared_key(&ci->ci_enc_key);
476
477 key = ci->ci_master_key;
478 if (key) {
479 struct fscrypt_master_key *mk = key->payload.data[0];
480
481
482
483
484
485
486
487
488
489 spin_lock(&mk->mk_decrypted_inodes_lock);
490 list_del(&ci->ci_master_key_link);
491 spin_unlock(&mk->mk_decrypted_inodes_lock);
492 if (refcount_dec_and_test(&mk->mk_refcount))
493 key_invalidate(key);
494 key_put(key);
495 }
496 memzero_explicit(ci, sizeof(*ci));
497 kmem_cache_free(fscrypt_info_cachep, ci);
498}
499
500static int
501fscrypt_setup_encryption_info(struct inode *inode,
502 const union fscrypt_policy *policy,
503 const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],
504 bool need_dirhash_key)
505{
506 struct fscrypt_info *crypt_info;
507 struct fscrypt_mode *mode;
508 struct key *master_key = NULL;
509 int res;
510
511 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
512 if (res)
513 return res;
514
515 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL);
516 if (!crypt_info)
517 return -ENOMEM;
518
519 crypt_info->ci_inode = inode;
520 crypt_info->ci_policy = *policy;
521 memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
522
523 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
524 if (IS_ERR(mode)) {
525 res = PTR_ERR(mode);
526 goto out;
527 }
528 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
529 crypt_info->ci_mode = mode;
530
531 res = setup_file_encryption_key(crypt_info, need_dirhash_key,
532 &master_key);
533 if (res)
534 goto out;
535
536
537
538
539
540
541
542 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
543
544
545
546
547 if (master_key) {
548 struct fscrypt_master_key *mk =
549 master_key->payload.data[0];
550
551 refcount_inc(&mk->mk_refcount);
552 crypt_info->ci_master_key = key_get(master_key);
553 spin_lock(&mk->mk_decrypted_inodes_lock);
554 list_add(&crypt_info->ci_master_key_link,
555 &mk->mk_decrypted_inodes);
556 spin_unlock(&mk->mk_decrypted_inodes_lock);
557 }
558 crypt_info = NULL;
559 }
560 res = 0;
561out:
562 if (master_key) {
563 up_read(&master_key->sem);
564 key_put(master_key);
565 }
566 put_crypt_info(crypt_info);
567 return res;
568}
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported)
589{
590 int res;
591 union fscrypt_context ctx;
592 union fscrypt_policy policy;
593
594 if (fscrypt_has_encryption_key(inode))
595 return 0;
596
597 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
598 if (res < 0) {
599 if (res == -ERANGE && allow_unsupported)
600 return 0;
601 fscrypt_warn(inode, "Error %d getting encryption context", res);
602 return res;
603 }
604
605 res = fscrypt_policy_from_context(&policy, &ctx, res);
606 if (res) {
607 if (allow_unsupported)
608 return 0;
609 fscrypt_warn(inode,
610 "Unrecognized or corrupt encryption context");
611 return res;
612 }
613
614 if (!fscrypt_supported_policy(&policy, inode)) {
615 if (allow_unsupported)
616 return 0;
617 return -EINVAL;
618 }
619
620 res = fscrypt_setup_encryption_info(inode, &policy,
621 fscrypt_context_nonce(&ctx),
622 IS_CASEFOLDED(inode) &&
623 S_ISDIR(inode->i_mode));
624
625 if (res == -ENOPKG && allow_unsupported)
626 res = 0;
627 if (res == -ENOKEY)
628 res = 0;
629 return res;
630}
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
654 bool *encrypt_ret)
655{
656 const union fscrypt_policy *policy;
657 u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
658
659 policy = fscrypt_policy_to_inherit(dir);
660 if (policy == NULL)
661 return 0;
662 if (IS_ERR(policy))
663 return PTR_ERR(policy);
664
665 if (WARN_ON_ONCE(inode->i_mode == 0))
666 return -EINVAL;
667
668
669
670
671
672 if (!S_ISREG(inode->i_mode) &&
673 !S_ISDIR(inode->i_mode) &&
674 !S_ISLNK(inode->i_mode))
675 return 0;
676
677 *encrypt_ret = true;
678
679 get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
680 return fscrypt_setup_encryption_info(inode, policy, nonce,
681 IS_CASEFOLDED(dir) &&
682 S_ISDIR(inode->i_mode));
683}
684EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
685
686
687
688
689
690
691
692
693void fscrypt_put_encryption_info(struct inode *inode)
694{
695 put_crypt_info(inode->i_crypt_info);
696 inode->i_crypt_info = NULL;
697}
698EXPORT_SYMBOL(fscrypt_put_encryption_info);
699
700
701
702
703
704
705
706
707void fscrypt_free_inode(struct inode *inode)
708{
709 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
710 kfree(inode->i_link);
711 inode->i_link = NULL;
712 }
713}
714EXPORT_SYMBOL(fscrypt_free_inode);
715
716
717
718
719
720
721
722
723
724
725
726int fscrypt_drop_inode(struct inode *inode)
727{
728 const struct fscrypt_info *ci = fscrypt_get_info(inode);
729 const struct fscrypt_master_key *mk;
730
731
732
733
734
735
736
737 if (!ci || !ci->ci_master_key)
738 return 0;
739 mk = ci->ci_master_key->payload.data[0];
740
741
742
743
744
745
746
747 if (inode->i_state & I_DIRTY_ALL)
748 return 0;
749
750
751
752
753
754
755
756
757
758 return !is_master_key_secret_present(&mk->mk_secret);
759}
760EXPORT_SYMBOL_GPL(fscrypt_drop_inode);
761