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"
26 static struct crypto_shash *essiv_hash_tfm;
28 static struct llcrypt_mode available_modes[] = {
29 [LLCRYPT_MODE_NULL] = {
30 .friendly_name = "NULL",
35 [LLCRYPT_MODE_AES_256_XTS] = {
36 .friendly_name = "AES-256-XTS",
37 .cipher_str = "xts(aes)",
41 [LLCRYPT_MODE_AES_256_CTS] = {
42 .friendly_name = "AES-256-CTS-CBC",
43 .cipher_str = "cts(cbc(aes))",
47 [LLCRYPT_MODE_AES_128_CBC] = {
48 .friendly_name = "AES-128-CBC",
49 .cipher_str = "cbc(aes)",
54 [LLCRYPT_MODE_AES_128_CTS] = {
55 .friendly_name = "AES-128-CTS-CBC",
56 .cipher_str = "cts(cbc(aes))",
60 [LLCRYPT_MODE_ADIANTUM] = {
61 .friendly_name = "Adiantum",
62 .cipher_str = "adiantum(xchacha12,aes)",
68 static struct llcrypt_mode *
69 select_encryption_mode(const union llcrypt_policy *policy,
70 const struct inode *inode)
72 if (S_ISREG(inode->i_mode))
73 return &available_modes[llcrypt_policy_contents_mode(policy)];
75 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
76 return &available_modes[llcrypt_policy_fnames_mode(policy)];
78 WARN_ONCE(1, "llcrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
79 inode->i_ino, (inode->i_mode & S_IFMT));
80 return ERR_PTR(-EINVAL);
83 /* Create a symmetric cipher object for the given encryption mode and key */
84 struct crypto_skcipher *llcrypt_allocate_skcipher(struct llcrypt_mode *mode,
86 const struct inode *inode)
88 struct crypto_skcipher *tfm;
91 if (!strcmp(mode->cipher_str, "null"))
94 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
96 if (PTR_ERR(tfm) == -ENOENT) {
98 "Missing crypto API support for %s (API name: \"%s\")",
99 mode->friendly_name, mode->cipher_str);
100 return ERR_PTR(-ENOPKG);
102 llcrypt_err(inode, "Error allocating '%s' transform: %ld",
103 mode->cipher_str, PTR_ERR(tfm));
106 if (unlikely(!mode->logged_impl_name)) {
108 * llcrypt performance can vary greatly depending on which
109 * crypto algorithm implementation is used. Help people debug
110 * performance problems by logging the ->cra_driver_name the
111 * first time a mode is used. Note that multiple threads can
112 * race here, but it doesn't really matter.
114 mode->logged_impl_name = true;
115 pr_info("llcrypt: %s using implementation \"%s\"\n",
117 crypto_skcipher_alg(tfm)->base.cra_driver_name);
119 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
120 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
127 crypto_free_skcipher(tfm);
131 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
133 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
135 /* init hash transform on demand */
136 if (unlikely(!tfm)) {
137 struct crypto_shash *prev_tfm;
139 tfm = crypto_alloc_shash("sha256", 0, 0);
141 if (PTR_ERR(tfm) == -ENOENT) {
143 "Missing crypto API support for SHA-256");
147 "Error allocating SHA-256 transform: %ld",
151 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
153 crypto_free_shash(tfm);
159 SHASH_DESC_ON_STACK(desc, tfm);
162 return crypto_shash_digest(desc, key, keysize, salt);
166 static int init_essiv_generator(struct llcrypt_info *ci, const u8 *raw_key,
170 struct crypto_cipher *essiv_tfm;
171 u8 salt[SHA256_DIGEST_SIZE];
173 if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
176 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
177 if (IS_ERR(essiv_tfm))
178 return PTR_ERR(essiv_tfm);
180 ci->ci_essiv_tfm = essiv_tfm;
182 err = derive_essiv_salt(raw_key, keysize, salt);
187 * Using SHA256 to derive the salt/key will result in AES-256 being
188 * used for IV generation. File contents encryption will still use the
189 * configured keysize (AES-128) nevertheless.
191 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
196 memzero_explicit(salt, sizeof(salt));
200 /* Given the per-file key, set up the file's crypto transform object(s) */
201 int llcrypt_set_derived_key(struct llcrypt_info *ci, const u8 *derived_key)
203 struct llcrypt_mode *mode = ci->ci_mode;
204 struct crypto_skcipher *ctfm;
207 ctfm = llcrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
209 return PTR_ERR(ctfm);
213 if (mode->needs_essiv) {
214 err = init_essiv_generator(ci, derived_key, mode->keysize);
216 llcrypt_warn(ci->ci_inode,
217 "Error initializing ESSIV generator: %d",
225 static int setup_per_mode_key(struct llcrypt_info *ci,
226 struct llcrypt_master_key *mk)
228 struct llcrypt_mode *mode = ci->ci_mode;
229 u8 mode_num = mode - available_modes;
230 struct crypto_skcipher *tfm, *prev_tfm;
231 u8 mode_key[LLCRYPT_MAX_KEY_SIZE];
234 if (WARN_ON(mode_num >= ARRAY_SIZE(mk->mk_mode_keys)))
237 /* pairs with cmpxchg() below */
238 tfm = READ_ONCE(mk->mk_mode_keys[mode_num]);
239 if (likely(tfm != NULL))
242 BUILD_BUG_ON(sizeof(mode_num) != 1);
243 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
244 HKDF_CONTEXT_PER_MODE_KEY,
245 &mode_num, sizeof(mode_num),
246 mode_key, mode->keysize);
249 tfm = llcrypt_allocate_skcipher(mode, mode_key, ci->ci_inode);
250 memzero_explicit(mode_key, mode->keysize);
254 /* pairs with READ_ONCE() above */
255 prev_tfm = cmpxchg(&mk->mk_mode_keys[mode_num], NULL, tfm);
256 if (prev_tfm != NULL) {
257 crypto_free_skcipher(tfm);
265 static int llcrypt_setup_v2_file_key(struct llcrypt_info *ci,
266 struct llcrypt_master_key *mk)
268 u8 derived_key[LLCRYPT_MAX_KEY_SIZE];
271 if (ci->ci_policy.v2.flags & LLCRYPT_POLICY_FLAG_DIRECT_KEY) {
273 * DIRECT_KEY: instead of deriving per-file keys, the per-file
274 * nonce will be included in all the IVs. But unlike v1
275 * policies, for v2 policies in this case we don't encrypt with
276 * the master key directly but rather derive a per-mode key.
277 * This ensures that the master key is consistently used only
278 * for HKDF, avoiding key reuse issues.
280 if (!llcrypt_mode_supports_direct_key(ci->ci_mode)) {
281 llcrypt_warn(ci->ci_inode,
282 "Direct key flag not allowed with %s",
283 ci->ci_mode->friendly_name);
286 return setup_per_mode_key(ci, mk);
289 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
290 HKDF_CONTEXT_PER_FILE_KEY,
291 ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
292 derived_key, ci->ci_mode->keysize);
296 err = llcrypt_set_derived_key(ci, derived_key);
297 memzero_explicit(derived_key, ci->ci_mode->keysize);
302 * Find the master key, then set up the inode's actual encryption key.
304 * If the master key is found in the filesystem-level keyring, then the
305 * corresponding 'struct key' is returned in *master_key_ret with
306 * ->mk_secret_sem read-locked. This is needed to ensure that only one task
307 * links the llcrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
308 * to create an llcrypt_info for the same inode), and to synchronize the master
309 * key being removed with a new inode starting to use it.
311 static int setup_file_encryption_key(struct llcrypt_info *ci,
312 struct key **master_key_ret)
315 struct llcrypt_master_key *mk = NULL;
316 struct llcrypt_key_specifier mk_spec;
319 switch (ci->ci_policy.version) {
320 case LLCRYPT_POLICY_V1:
321 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
322 memcpy(mk_spec.u.descriptor,
323 ci->ci_policy.v1.master_key_descriptor,
324 LLCRYPT_KEY_DESCRIPTOR_SIZE);
326 case LLCRYPT_POLICY_V2:
327 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
328 memcpy(mk_spec.u.identifier,
329 ci->ci_policy.v2.master_key_identifier,
330 LLCRYPT_KEY_IDENTIFIER_SIZE);
337 key = llcrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
339 if (key != ERR_PTR(-ENOKEY) ||
340 ci->ci_policy.version != LLCRYPT_POLICY_V1)
344 * As a legacy fallback for v1 policies, search for the key in
345 * the current task's subscribed keyrings too. Don't move this
346 * to before the search of ->lsi_master_keys, since users
347 * shouldn't be able to override filesystem-level keys.
349 return llcrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
352 mk = key->payload.data[0];
353 down_read(&mk->mk_secret_sem);
355 /* Has the secret been removed (via LL_IOC_REMOVE_ENCRYPTION_KEY)? */
356 if (!is_master_key_secret_present(&mk->mk_secret)) {
358 goto out_release_key;
362 * Require that the master key be at least as long as the derived key.
363 * Otherwise, the derived key cannot possibly contain as much entropy as
364 * that required by the encryption mode it will be used for. For v1
365 * policies it's also required for the KDF to work at all.
367 if (mk->mk_secret.size < ci->ci_mode->keysize) {
369 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
370 master_key_spec_type(&mk_spec),
371 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
372 mk->mk_secret.size, ci->ci_mode->keysize);
374 goto out_release_key;
377 switch (ci->ci_policy.version) {
378 case LLCRYPT_POLICY_V1:
379 err = llcrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
381 case LLCRYPT_POLICY_V2:
382 err = llcrypt_setup_v2_file_key(ci, mk);
390 goto out_release_key;
392 *master_key_ret = key;
396 up_read(&mk->mk_secret_sem);
401 static void put_crypt_info(struct llcrypt_info *ci)
408 if (ci->ci_direct_key) {
409 llcrypt_put_direct_key(ci->ci_direct_key);
410 } else if ((ci->ci_ctfm != NULL || ci->ci_essiv_tfm != NULL) &&
411 !llcrypt_is_direct_key_policy(&ci->ci_policy)) {
413 crypto_free_skcipher(ci->ci_ctfm);
414 crypto_free_cipher(ci->ci_essiv_tfm);
417 key = ci->ci_master_key;
419 struct llcrypt_master_key *mk = key->payload.data[0];
422 * Remove this inode from the list of inodes that were unlocked
423 * with the master key.
425 * In addition, if we're removing the last inode from a key that
426 * already had its secret removed, invalidate the key so that it
427 * gets removed from ->lsi_master_keys.
429 spin_lock(&mk->mk_decrypted_inodes_lock);
430 list_del(&ci->ci_master_key_link);
431 spin_unlock(&mk->mk_decrypted_inodes_lock);
432 if (refcount_dec_and_test(&mk->mk_refcount))
436 kmem_cache_free(llcrypt_info_cachep, ci);
439 int llcrypt_get_encryption_info(struct inode *inode)
441 struct llcrypt_info *crypt_info;
442 union llcrypt_context ctx;
443 struct llcrypt_mode *mode;
444 struct key *master_key = NULL;
445 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
448 if (llcrypt_has_encryption_key(inode))
454 res = llcrypt_initialize(lsi->lsi_cop->flags);
458 res = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
460 if (!llcrypt_dummy_context_enabled(inode) ||
461 IS_ENCRYPTED(inode)) {
463 "Error %d getting encryption context",
467 /* Fake up a context for an unencrypted directory */
468 memset(&ctx, 0, sizeof(ctx));
469 ctx.version = LLCRYPT_CONTEXT_V1;
470 ctx.v1.contents_encryption_mode = LLCRYPT_MODE_AES_256_XTS;
471 ctx.v1.filenames_encryption_mode = LLCRYPT_MODE_AES_256_CTS;
472 memset(ctx.v1.master_key_descriptor, 0x42,
473 LLCRYPT_KEY_DESCRIPTOR_SIZE);
474 res = sizeof(ctx.v1);
477 crypt_info = kmem_cache_zalloc(llcrypt_info_cachep, GFP_NOFS);
481 crypt_info->ci_inode = inode;
483 res = llcrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
486 "Unrecognized or corrupt encryption context");
490 switch (ctx.version) {
491 case LLCRYPT_CONTEXT_V1:
492 memcpy(crypt_info->ci_nonce, ctx.v1.nonce,
493 FS_KEY_DERIVATION_NONCE_SIZE);
495 case LLCRYPT_CONTEXT_V2:
496 memcpy(crypt_info->ci_nonce, ctx.v2.nonce,
497 FS_KEY_DERIVATION_NONCE_SIZE);
505 if (!llcrypt_supported_policy(&crypt_info->ci_policy, inode)) {
510 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
515 WARN_ON(mode->ivsize > LLCRYPT_MAX_IV_SIZE);
516 crypt_info->ci_mode = mode;
518 res = setup_file_encryption_key(crypt_info, &master_key);
522 if (cmpxchg_release(&(llcrypt_info_nocast(inode)), NULL,
523 crypt_info) == NULL) {
525 struct llcrypt_master_key *mk =
526 master_key->payload.data[0];
528 refcount_inc(&mk->mk_refcount);
529 crypt_info->ci_master_key = key_get(master_key);
530 spin_lock(&mk->mk_decrypted_inodes_lock);
531 list_add(&crypt_info->ci_master_key_link,
532 &mk->mk_decrypted_inodes);
533 spin_unlock(&mk->mk_decrypted_inodes_lock);
540 struct llcrypt_master_key *mk = master_key->payload.data[0];
542 up_read(&mk->mk_secret_sem);
547 put_crypt_info(crypt_info);
550 EXPORT_SYMBOL(llcrypt_get_encryption_info);
553 * llcrypt_put_encryption_info - free most of an inode's llcrypt data
555 * Free the inode's llcrypt_info. Filesystems must call this when the inode is
556 * being evicted. An RCU grace period need not have elapsed yet.
558 void llcrypt_put_encryption_info(struct inode *inode)
560 put_crypt_info(llcrypt_info(inode));
561 llcrypt_info_nocast(inode) = NULL;
563 EXPORT_SYMBOL(llcrypt_put_encryption_info);
566 * llcrypt_free_inode - free an inode's llcrypt data requiring RCU delay
568 * Free the inode's cached decrypted symlink target, if any. Filesystems must
569 * call this after an RCU grace period, just before they free the inode.
571 void llcrypt_free_inode(struct inode *inode)
573 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
574 kfree(inode->i_link);
575 inode->i_link = NULL;
578 EXPORT_SYMBOL(llcrypt_free_inode);
581 * llcrypt_drop_inode - check whether the inode's master key has been removed
583 * Filesystems supporting llcrypt must call this from their ->drop_inode()
584 * method so that encrypted inodes are evicted as soon as they're no longer in
585 * use and their master key has been removed.
587 * Return: 1 if llcrypt wants the inode to be evicted now, otherwise 0
589 int llcrypt_drop_inode(struct inode *inode)
591 const struct llcrypt_info *ci;
592 const struct llcrypt_master_key *mk;
594 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
596 * If ci is NULL, then the inode doesn't have an encryption key set up
597 * so it's irrelevant. If ci_master_key is NULL, then the master key
598 * was provided via the legacy mechanism of the process-subscribed
599 * keyrings, so we don't know whether it's been removed or not.
601 if (!ci || !ci->ci_master_key)
603 mk = ci->ci_master_key->payload.data[0];
606 * Note: since we aren't holding ->mk_secret_sem, the result here can
607 * immediately become outdated. But there's no correctness problem with
608 * unnecessarily evicting. Nor is there a correctness problem with not
609 * evicting while iput() is racing with the key being removed, since
610 * then the thread removing the key will either evict the inode itself
611 * or will correctly detect that it wasn't evicted due to the race.
613 return !is_master_key_secret_present(&mk->mk_secret);
615 EXPORT_SYMBOL_GPL(llcrypt_drop_inode);
617 bool llcrypt_has_encryption_key(const struct inode *inode)
619 /* pairs with cmpxchg_release() in llcrypt_get_encryption_info() */
620 return READ_ONCE(llcrypt_info_nocast(inode)) != NULL;
622 EXPORT_SYMBOL_GPL(llcrypt_has_encryption_key);