1 // SPDX-License-Identifier: GPL-2.0
3 * Key setup facility for FS encryption support.
5 * Copyright (C) 2015, Google, Inc.
7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
11 * Linux commit 219d54332a09
15 #include <crypto/aes.h>
16 #ifdef HAVE_CRYPTO_SHA2_HEADER
17 #include <crypto/sha2.h>
19 #include <crypto/sha.h>
21 #include <crypto/skcipher.h>
22 #include <linux/key.h>
24 #include "llcrypt_private.h"
27 #include <crypto/internal/cipher.h>
29 MODULE_IMPORT_NS(CRYPTO_INTERNAL);
32 static struct crypto_shash *essiv_hash_tfm;
34 static struct llcrypt_mode available_modes[] = {
35 [LLCRYPT_MODE_NULL] = {
36 .friendly_name = "NULL",
41 [LLCRYPT_MODE_AES_256_XTS] = {
42 .friendly_name = "AES-256-XTS",
43 .cipher_str = "xts(aes)",
47 [LLCRYPT_MODE_AES_256_CTS] = {
48 .friendly_name = "AES-256-CTS-CBC",
49 .cipher_str = "cts(cbc(aes))",
53 [LLCRYPT_MODE_AES_128_CBC] = {
54 .friendly_name = "AES-128-CBC",
55 .cipher_str = "cbc(aes)",
60 [LLCRYPT_MODE_AES_128_CTS] = {
61 .friendly_name = "AES-128-CTS-CBC",
62 .cipher_str = "cts(cbc(aes))",
66 [LLCRYPT_MODE_ADIANTUM] = {
67 .friendly_name = "Adiantum",
68 .cipher_str = "adiantum(xchacha12,aes)",
74 static struct llcrypt_mode *
75 select_encryption_mode(const union llcrypt_policy *policy,
76 const struct inode *inode)
78 if (S_ISREG(inode->i_mode))
79 return &available_modes[llcrypt_policy_contents_mode(policy)];
81 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
82 return &available_modes[llcrypt_policy_fnames_mode(policy)];
84 WARN_ONCE(1, "llcrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
85 inode->i_ino, (inode->i_mode & S_IFMT));
86 return ERR_PTR(-EINVAL);
89 /* Create a symmetric cipher object for the given encryption mode and key */
90 struct crypto_skcipher *llcrypt_allocate_skcipher(struct llcrypt_mode *mode,
92 const struct inode *inode)
94 struct crypto_skcipher *tfm;
97 if (!strcmp(mode->cipher_str, "null"))
100 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
102 if (PTR_ERR(tfm) == -ENOENT) {
104 "Missing crypto API support for %s (API name: \"%s\")",
105 mode->friendly_name, mode->cipher_str);
106 return ERR_PTR(-ENOPKG);
108 llcrypt_err(inode, "Error allocating '%s' transform: %ld",
109 mode->cipher_str, PTR_ERR(tfm));
112 if (unlikely(!mode->logged_impl_name)) {
114 * llcrypt performance can vary greatly depending on which
115 * crypto algorithm implementation is used. Help people debug
116 * performance problems by logging the ->cra_driver_name the
117 * first time a mode is used. Note that multiple threads can
118 * race here, but it doesn't really matter.
120 mode->logged_impl_name = true;
121 pr_info("llcrypt: %s using implementation \"%s\"\n",
123 crypto_skcipher_alg(tfm)->base.cra_driver_name);
125 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
126 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
133 crypto_free_skcipher(tfm);
137 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
139 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
141 /* init hash transform on demand */
142 if (unlikely(!tfm)) {
143 struct crypto_shash *prev_tfm;
145 tfm = crypto_alloc_shash("sha256", 0, 0);
147 if (PTR_ERR(tfm) == -ENOENT) {
149 "Missing crypto API support for SHA-256");
153 "Error allocating SHA-256 transform: %ld",
157 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
159 crypto_free_shash(tfm);
165 SHASH_DESC_ON_STACK(desc, tfm);
168 return crypto_shash_digest(desc, key, keysize, salt);
172 static int init_essiv_generator(struct llcrypt_info *ci, const u8 *raw_key,
176 struct crypto_cipher *essiv_tfm;
177 u8 salt[SHA256_DIGEST_SIZE];
179 if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
182 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
183 if (IS_ERR(essiv_tfm))
184 return PTR_ERR(essiv_tfm);
186 ci->ci_essiv_tfm = essiv_tfm;
188 err = derive_essiv_salt(raw_key, keysize, salt);
193 * Using SHA256 to derive the salt/key will result in AES-256 being
194 * used for IV generation. File contents encryption will still use the
195 * configured keysize (AES-128) nevertheless.
197 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
202 memzero_explicit(salt, sizeof(salt));
206 /* Given the per-file key, set up the file's crypto transform object(s) */
207 int llcrypt_set_derived_key(struct llcrypt_info *ci, const u8 *derived_key)
209 struct llcrypt_mode *mode = ci->ci_mode;
210 struct crypto_skcipher *ctfm;
213 ctfm = llcrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
215 return PTR_ERR(ctfm);
219 if (mode->needs_essiv) {
220 err = init_essiv_generator(ci, derived_key, mode->keysize);
222 llcrypt_warn(ci->ci_inode,
223 "Error initializing ESSIV generator: %d",
231 static int setup_per_mode_key(struct llcrypt_info *ci,
232 struct llcrypt_master_key *mk)
234 struct llcrypt_mode *mode = ci->ci_mode;
235 u8 mode_num = mode - available_modes;
236 struct crypto_skcipher *tfm, *prev_tfm;
237 u8 mode_key[LLCRYPT_MAX_KEY_SIZE];
240 if (WARN_ON(mode_num >= ARRAY_SIZE(mk->mk_mode_keys)))
243 /* pairs with cmpxchg() below */
244 tfm = READ_ONCE(mk->mk_mode_keys[mode_num]);
245 if (likely(tfm != NULL))
248 BUILD_BUG_ON(sizeof(mode_num) != 1);
249 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
250 HKDF_CONTEXT_PER_MODE_KEY,
251 &mode_num, sizeof(mode_num),
252 mode_key, mode->keysize);
255 tfm = llcrypt_allocate_skcipher(mode, mode_key, ci->ci_inode);
256 memzero_explicit(mode_key, mode->keysize);
260 /* pairs with READ_ONCE() above */
261 prev_tfm = cmpxchg(&mk->mk_mode_keys[mode_num], NULL, tfm);
262 if (prev_tfm != NULL) {
263 crypto_free_skcipher(tfm);
271 static int llcrypt_setup_v2_file_key(struct llcrypt_info *ci,
272 struct llcrypt_master_key *mk)
274 u8 derived_key[LLCRYPT_MAX_KEY_SIZE];
277 if (ci->ci_policy.v2.flags & LLCRYPT_POLICY_FLAG_DIRECT_KEY) {
279 * DIRECT_KEY: instead of deriving per-file keys, the per-file
280 * nonce will be included in all the IVs. But unlike v1
281 * policies, for v2 policies in this case we don't encrypt with
282 * the master key directly but rather derive a per-mode key.
283 * This ensures that the master key is consistently used only
284 * for HKDF, avoiding key reuse issues.
286 if (!llcrypt_mode_supports_direct_key(ci->ci_mode)) {
287 llcrypt_warn(ci->ci_inode,
288 "Direct key flag not allowed with %s",
289 ci->ci_mode->friendly_name);
292 return setup_per_mode_key(ci, mk);
295 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
296 HKDF_CONTEXT_PER_FILE_KEY,
297 ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
298 derived_key, ci->ci_mode->keysize);
302 err = llcrypt_set_derived_key(ci, derived_key);
303 memzero_explicit(derived_key, ci->ci_mode->keysize);
308 * Find the master key, then set up the inode's actual encryption key.
310 * If the master key is found in the filesystem-level keyring, then the
311 * corresponding 'struct key' is returned in *master_key_ret with
312 * ->mk_secret_sem read-locked. This is needed to ensure that only one task
313 * links the llcrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
314 * to create an llcrypt_info for the same inode), and to synchronize the master
315 * key being removed with a new inode starting to use it.
317 static int setup_file_encryption_key(struct llcrypt_info *ci,
318 struct key **master_key_ret)
321 struct llcrypt_master_key *mk = NULL;
322 struct llcrypt_key_specifier mk_spec;
325 switch (ci->ci_policy.version) {
326 case LLCRYPT_POLICY_V1:
327 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
328 memcpy(mk_spec.u.descriptor,
329 ci->ci_policy.v1.master_key_descriptor,
330 LLCRYPT_KEY_DESCRIPTOR_SIZE);
332 case LLCRYPT_POLICY_V2:
333 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
334 memcpy(mk_spec.u.identifier,
335 ci->ci_policy.v2.master_key_identifier,
336 LLCRYPT_KEY_IDENTIFIER_SIZE);
343 key = llcrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
345 if (key != ERR_PTR(-ENOKEY) ||
346 ci->ci_policy.version != LLCRYPT_POLICY_V1)
350 * As a legacy fallback for v1 policies, search for the key in
351 * the current task's subscribed keyrings too. Don't move this
352 * to before the search of ->lsi_master_keys, since users
353 * shouldn't be able to override filesystem-level keys.
355 return llcrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
358 mk = key->payload.data[0];
359 down_read(&mk->mk_secret_sem);
361 /* Has the secret been removed (via LL_IOC_REMOVE_ENCRYPTION_KEY)? */
362 if (!is_master_key_secret_present(&mk->mk_secret)) {
364 goto out_release_key;
368 * Require that the master key be at least as long as the derived key.
369 * Otherwise, the derived key cannot possibly contain as much entropy as
370 * that required by the encryption mode it will be used for. For v1
371 * policies it's also required for the KDF to work at all.
373 if (mk->mk_secret.size < ci->ci_mode->keysize) {
375 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
376 master_key_spec_type(&mk_spec),
377 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
378 mk->mk_secret.size, ci->ci_mode->keysize);
380 goto out_release_key;
383 switch (ci->ci_policy.version) {
384 case LLCRYPT_POLICY_V1:
385 err = llcrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
387 case LLCRYPT_POLICY_V2:
388 err = llcrypt_setup_v2_file_key(ci, mk);
396 goto out_release_key;
398 *master_key_ret = key;
402 up_read(&mk->mk_secret_sem);
407 static void put_crypt_info(struct llcrypt_info *ci)
414 if (ci->ci_direct_key) {
415 llcrypt_put_direct_key(ci->ci_direct_key);
416 } else if ((ci->ci_ctfm != NULL || ci->ci_essiv_tfm != NULL) &&
417 !llcrypt_is_direct_key_policy(&ci->ci_policy)) {
419 crypto_free_skcipher(ci->ci_ctfm);
420 crypto_free_cipher(ci->ci_essiv_tfm);
423 key = ci->ci_master_key;
425 struct llcrypt_master_key *mk = key->payload.data[0];
428 * Remove this inode from the list of inodes that were unlocked
429 * with the master key.
431 * In addition, if we're removing the last inode from a key that
432 * already had its secret removed, invalidate the key so that it
433 * gets removed from ->lsi_master_keys.
435 spin_lock(&mk->mk_decrypted_inodes_lock);
436 list_del(&ci->ci_master_key_link);
437 spin_unlock(&mk->mk_decrypted_inodes_lock);
438 if (refcount_dec_and_test(&mk->mk_refcount))
442 kmem_cache_free(llcrypt_info_cachep, ci);
445 int llcrypt_get_encryption_info(struct inode *inode)
447 struct llcrypt_info *crypt_info;
448 union llcrypt_context ctx;
449 struct llcrypt_mode *mode;
450 struct key *master_key = NULL;
451 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
454 if (llcrypt_has_encryption_key(inode))
460 res = llcrypt_initialize(lsi->lsi_cop->flags);
464 res = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
466 if (!llcrypt_dummy_context_enabled(inode) ||
467 IS_ENCRYPTED(inode)) {
469 "Error %d getting encryption context",
473 /* Fake up a context for an unencrypted directory */
474 memset(&ctx, 0, sizeof(ctx));
475 ctx.version = LLCRYPT_CONTEXT_V1;
476 ctx.v1.contents_encryption_mode = LLCRYPT_MODE_AES_256_XTS;
477 if (lsi->lsi_flags & LSI_FILENAME_ENC) {
478 ctx.v1.filenames_encryption_mode =
479 LLCRYPT_MODE_AES_256_CTS;
482 "dummy enc: forcing filenames_encryption_mode to null");
483 ctx.v1.filenames_encryption_mode = LLCRYPT_MODE_NULL;
485 memset(ctx.v1.master_key_descriptor, 0x42,
486 LLCRYPT_KEY_DESCRIPTOR_SIZE);
487 res = sizeof(ctx.v1);
490 crypt_info = kmem_cache_zalloc(llcrypt_info_cachep, GFP_NOFS);
494 crypt_info->ci_inode = inode;
496 res = llcrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
499 "Unrecognized or corrupt encryption context");
503 switch (ctx.version) {
504 case LLCRYPT_CONTEXT_V1:
505 memcpy(crypt_info->ci_nonce, ctx.v1.nonce,
506 FS_KEY_DERIVATION_NONCE_SIZE);
508 case LLCRYPT_CONTEXT_V2:
509 memcpy(crypt_info->ci_nonce, ctx.v2.nonce,
510 FS_KEY_DERIVATION_NONCE_SIZE);
518 if (!llcrypt_supported_policy(&crypt_info->ci_policy, inode)) {
523 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
528 WARN_ON(mode->ivsize > LLCRYPT_MAX_IV_SIZE);
529 crypt_info->ci_mode = mode;
531 res = setup_file_encryption_key(crypt_info, &master_key);
535 if (cmpxchg_release(&(llcrypt_info_nocast(inode)), NULL,
536 crypt_info) == NULL) {
538 struct llcrypt_master_key *mk =
539 master_key->payload.data[0];
541 refcount_inc(&mk->mk_refcount);
542 crypt_info->ci_master_key = key_get(master_key);
543 spin_lock(&mk->mk_decrypted_inodes_lock);
544 list_add(&crypt_info->ci_master_key_link,
545 &mk->mk_decrypted_inodes);
546 spin_unlock(&mk->mk_decrypted_inodes_lock);
553 struct llcrypt_master_key *mk = master_key->payload.data[0];
555 up_read(&mk->mk_secret_sem);
560 put_crypt_info(crypt_info);
563 EXPORT_SYMBOL(llcrypt_get_encryption_info);
566 * llcrypt_put_encryption_info - free most of an inode's llcrypt data
568 * Free the inode's llcrypt_info. Filesystems must call this when the inode is
569 * being evicted. An RCU grace period need not have elapsed yet.
571 void llcrypt_put_encryption_info(struct inode *inode)
573 put_crypt_info(llcrypt_info(inode));
574 llcrypt_info_nocast(inode) = NULL;
576 EXPORT_SYMBOL(llcrypt_put_encryption_info);
579 * llcrypt_free_inode - free an inode's llcrypt data requiring RCU delay
581 * Free the inode's cached decrypted symlink target, if any. Filesystems must
582 * call this after an RCU grace period, just before they free the inode.
584 void llcrypt_free_inode(struct inode *inode)
586 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
587 kfree(inode->i_link);
588 inode->i_link = NULL;
591 EXPORT_SYMBOL(llcrypt_free_inode);
594 * llcrypt_drop_inode - check whether the inode's master key has been removed
596 * Filesystems supporting llcrypt must call this from their ->drop_inode()
597 * method so that encrypted inodes are evicted as soon as they're no longer in
598 * use and their master key has been removed.
600 * Return: 1 if llcrypt wants the inode to be evicted now, otherwise 0
602 int llcrypt_drop_inode(struct inode *inode)
604 const struct llcrypt_info *ci;
605 const struct llcrypt_master_key *mk;
607 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
609 * If ci is NULL, then the inode doesn't have an encryption key set up
610 * so it's irrelevant. If ci_master_key is NULL, then the master key
611 * was provided via the legacy mechanism of the process-subscribed
612 * keyrings, so we don't know whether it's been removed or not.
614 if (!ci || !ci->ci_master_key)
616 mk = ci->ci_master_key->payload.data[0];
619 * Note: since we aren't holding ->mk_secret_sem, the result here can
620 * immediately become outdated. But there's no correctness problem with
621 * unnecessarily evicting. Nor is there a correctness problem with not
622 * evicting while iput() is racing with the key being removed, since
623 * then the thread removing the key will either evict the inode itself
624 * or will correctly detect that it wasn't evicted due to the race.
626 return !is_master_key_secret_present(&mk->mk_secret);
628 EXPORT_SYMBOL_GPL(llcrypt_drop_inode);
630 bool llcrypt_has_encryption_key(const struct inode *inode)
632 /* pairs with cmpxchg_release() in llcrypt_get_encryption_info() */
633 return READ_ONCE(llcrypt_info_nocast(inode)) != NULL;
635 EXPORT_SYMBOL_GPL(llcrypt_has_encryption_key);