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 #include <crypto/sha.h>
17 #include <crypto/skcipher.h>
18 #include <linux/key.h>
20 #include "llcrypt_private.h"
22 static struct crypto_shash *essiv_hash_tfm;
24 static struct llcrypt_mode available_modes[] = {
25 [LLCRYPT_MODE_AES_256_XTS] = {
26 .friendly_name = "AES-256-XTS",
27 .cipher_str = "xts(aes)",
31 [LLCRYPT_MODE_AES_256_CTS] = {
32 .friendly_name = "AES-256-CTS-CBC",
33 .cipher_str = "cts(cbc(aes))",
37 [LLCRYPT_MODE_AES_128_CBC] = {
38 .friendly_name = "AES-128-CBC",
39 .cipher_str = "cbc(aes)",
44 [LLCRYPT_MODE_AES_128_CTS] = {
45 .friendly_name = "AES-128-CTS-CBC",
46 .cipher_str = "cts(cbc(aes))",
50 [LLCRYPT_MODE_ADIANTUM] = {
51 .friendly_name = "Adiantum",
52 .cipher_str = "adiantum(xchacha12,aes)",
58 static struct llcrypt_mode *
59 select_encryption_mode(const union llcrypt_policy *policy,
60 const struct inode *inode)
62 if (S_ISREG(inode->i_mode))
63 return &available_modes[llcrypt_policy_contents_mode(policy)];
65 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
66 return &available_modes[llcrypt_policy_fnames_mode(policy)];
68 WARN_ONCE(1, "llcrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
69 inode->i_ino, (inode->i_mode & S_IFMT));
70 return ERR_PTR(-EINVAL);
73 /* Create a symmetric cipher object for the given encryption mode and key */
74 struct crypto_skcipher *llcrypt_allocate_skcipher(struct llcrypt_mode *mode,
76 const struct inode *inode)
78 struct crypto_skcipher *tfm;
81 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
83 if (PTR_ERR(tfm) == -ENOENT) {
85 "Missing crypto API support for %s (API name: \"%s\")",
86 mode->friendly_name, mode->cipher_str);
87 return ERR_PTR(-ENOPKG);
89 llcrypt_err(inode, "Error allocating '%s' transform: %ld",
90 mode->cipher_str, PTR_ERR(tfm));
93 if (unlikely(!mode->logged_impl_name)) {
95 * llcrypt performance can vary greatly depending on which
96 * crypto algorithm implementation is used. Help people debug
97 * performance problems by logging the ->cra_driver_name the
98 * first time a mode is used. Note that multiple threads can
99 * race here, but it doesn't really matter.
101 mode->logged_impl_name = true;
102 pr_info("llcrypt: %s using implementation \"%s\"\n",
104 crypto_skcipher_alg(tfm)->base.cra_driver_name);
106 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
107 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
114 crypto_free_skcipher(tfm);
118 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
120 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
122 /* init hash transform on demand */
123 if (unlikely(!tfm)) {
124 struct crypto_shash *prev_tfm;
126 tfm = crypto_alloc_shash("sha256", 0, 0);
128 if (PTR_ERR(tfm) == -ENOENT) {
130 "Missing crypto API support for SHA-256");
134 "Error allocating SHA-256 transform: %ld",
138 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
140 crypto_free_shash(tfm);
146 SHASH_DESC_ON_STACK(desc, tfm);
149 return crypto_shash_digest(desc, key, keysize, salt);
153 static int init_essiv_generator(struct llcrypt_info *ci, const u8 *raw_key,
157 struct crypto_cipher *essiv_tfm;
158 u8 salt[SHA256_DIGEST_SIZE];
160 if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
163 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
164 if (IS_ERR(essiv_tfm))
165 return PTR_ERR(essiv_tfm);
167 ci->ci_essiv_tfm = essiv_tfm;
169 err = derive_essiv_salt(raw_key, keysize, salt);
174 * Using SHA256 to derive the salt/key will result in AES-256 being
175 * used for IV generation. File contents encryption will still use the
176 * configured keysize (AES-128) nevertheless.
178 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
183 memzero_explicit(salt, sizeof(salt));
187 /* Given the per-file key, set up the file's crypto transform object(s) */
188 int llcrypt_set_derived_key(struct llcrypt_info *ci, const u8 *derived_key)
190 struct llcrypt_mode *mode = ci->ci_mode;
191 struct crypto_skcipher *ctfm;
194 ctfm = llcrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
196 return PTR_ERR(ctfm);
200 if (mode->needs_essiv) {
201 err = init_essiv_generator(ci, derived_key, mode->keysize);
203 llcrypt_warn(ci->ci_inode,
204 "Error initializing ESSIV generator: %d",
212 static int setup_per_mode_key(struct llcrypt_info *ci,
213 struct llcrypt_master_key *mk)
215 struct llcrypt_mode *mode = ci->ci_mode;
216 u8 mode_num = mode - available_modes;
217 struct crypto_skcipher *tfm, *prev_tfm;
218 u8 mode_key[LLCRYPT_MAX_KEY_SIZE];
221 if (WARN_ON(mode_num >= ARRAY_SIZE(mk->mk_mode_keys)))
224 /* pairs with cmpxchg() below */
225 tfm = READ_ONCE(mk->mk_mode_keys[mode_num]);
226 if (likely(tfm != NULL))
229 BUILD_BUG_ON(sizeof(mode_num) != 1);
230 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
231 HKDF_CONTEXT_PER_MODE_KEY,
232 &mode_num, sizeof(mode_num),
233 mode_key, mode->keysize);
236 tfm = llcrypt_allocate_skcipher(mode, mode_key, ci->ci_inode);
237 memzero_explicit(mode_key, mode->keysize);
241 /* pairs with READ_ONCE() above */
242 prev_tfm = cmpxchg(&mk->mk_mode_keys[mode_num], NULL, tfm);
243 if (prev_tfm != NULL) {
244 crypto_free_skcipher(tfm);
252 static int llcrypt_setup_v2_file_key(struct llcrypt_info *ci,
253 struct llcrypt_master_key *mk)
255 u8 derived_key[LLCRYPT_MAX_KEY_SIZE];
258 if (ci->ci_policy.v2.flags & LLCRYPT_POLICY_FLAG_DIRECT_KEY) {
260 * DIRECT_KEY: instead of deriving per-file keys, the per-file
261 * nonce will be included in all the IVs. But unlike v1
262 * policies, for v2 policies in this case we don't encrypt with
263 * the master key directly but rather derive a per-mode key.
264 * This ensures that the master key is consistently used only
265 * for HKDF, avoiding key reuse issues.
267 if (!llcrypt_mode_supports_direct_key(ci->ci_mode)) {
268 llcrypt_warn(ci->ci_inode,
269 "Direct key flag not allowed with %s",
270 ci->ci_mode->friendly_name);
273 return setup_per_mode_key(ci, mk);
276 err = llcrypt_hkdf_expand(&mk->mk_secret.hkdf,
277 HKDF_CONTEXT_PER_FILE_KEY,
278 ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
279 derived_key, ci->ci_mode->keysize);
283 err = llcrypt_set_derived_key(ci, derived_key);
284 memzero_explicit(derived_key, ci->ci_mode->keysize);
289 * Find the master key, then set up the inode's actual encryption key.
291 * If the master key is found in the filesystem-level keyring, then the
292 * corresponding 'struct key' is returned in *master_key_ret with
293 * ->mk_secret_sem read-locked. This is needed to ensure that only one task
294 * links the llcrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
295 * to create an llcrypt_info for the same inode), and to synchronize the master
296 * key being removed with a new inode starting to use it.
298 static int setup_file_encryption_key(struct llcrypt_info *ci,
299 struct key **master_key_ret)
302 struct llcrypt_master_key *mk = NULL;
303 struct llcrypt_key_specifier mk_spec;
306 switch (ci->ci_policy.version) {
307 case LLCRYPT_POLICY_V1:
308 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
309 memcpy(mk_spec.u.descriptor,
310 ci->ci_policy.v1.master_key_descriptor,
311 LLCRYPT_KEY_DESCRIPTOR_SIZE);
313 case LLCRYPT_POLICY_V2:
314 mk_spec.type = LLCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
315 memcpy(mk_spec.u.identifier,
316 ci->ci_policy.v2.master_key_identifier,
317 LLCRYPT_KEY_IDENTIFIER_SIZE);
324 key = llcrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
326 if (key != ERR_PTR(-ENOKEY) ||
327 ci->ci_policy.version != LLCRYPT_POLICY_V1)
331 * As a legacy fallback for v1 policies, search for the key in
332 * the current task's subscribed keyrings too. Don't move this
333 * to before the search of ->lsi_master_keys, since users
334 * shouldn't be able to override filesystem-level keys.
336 return llcrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
339 mk = key->payload.data[0];
340 down_read(&mk->mk_secret_sem);
342 /* Has the secret been removed (via LL_IOC_REMOVE_ENCRYPTION_KEY)? */
343 if (!is_master_key_secret_present(&mk->mk_secret)) {
345 goto out_release_key;
349 * Require that the master key be at least as long as the derived key.
350 * Otherwise, the derived key cannot possibly contain as much entropy as
351 * that required by the encryption mode it will be used for. For v1
352 * policies it's also required for the KDF to work at all.
354 if (mk->mk_secret.size < ci->ci_mode->keysize) {
356 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
357 master_key_spec_type(&mk_spec),
358 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
359 mk->mk_secret.size, ci->ci_mode->keysize);
361 goto out_release_key;
364 switch (ci->ci_policy.version) {
365 case LLCRYPT_POLICY_V1:
366 err = llcrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
368 case LLCRYPT_POLICY_V2:
369 err = llcrypt_setup_v2_file_key(ci, mk);
377 goto out_release_key;
379 *master_key_ret = key;
383 up_read(&mk->mk_secret_sem);
388 static void put_crypt_info(struct llcrypt_info *ci)
395 if (ci->ci_direct_key) {
396 llcrypt_put_direct_key(ci->ci_direct_key);
397 } else if ((ci->ci_ctfm != NULL || ci->ci_essiv_tfm != NULL) &&
398 !llcrypt_is_direct_key_policy(&ci->ci_policy)) {
399 crypto_free_skcipher(ci->ci_ctfm);
400 crypto_free_cipher(ci->ci_essiv_tfm);
403 key = ci->ci_master_key;
405 struct llcrypt_master_key *mk = key->payload.data[0];
408 * Remove this inode from the list of inodes that were unlocked
409 * with the master key.
411 * In addition, if we're removing the last inode from a key that
412 * already had its secret removed, invalidate the key so that it
413 * gets removed from ->lsi_master_keys.
415 spin_lock(&mk->mk_decrypted_inodes_lock);
416 list_del(&ci->ci_master_key_link);
417 spin_unlock(&mk->mk_decrypted_inodes_lock);
418 if (refcount_dec_and_test(&mk->mk_refcount))
422 kmem_cache_free(llcrypt_info_cachep, ci);
425 int llcrypt_get_encryption_info(struct inode *inode)
427 struct llcrypt_info *crypt_info;
428 union llcrypt_context ctx;
429 struct llcrypt_mode *mode;
430 struct key *master_key = NULL;
431 struct lustre_sb_info *lsi = s2lsi(inode->i_sb);
434 if (llcrypt_has_encryption_key(inode))
440 res = llcrypt_initialize(lsi->lsi_cop->flags);
444 res = lsi->lsi_cop->get_context(inode, &ctx, sizeof(ctx));
446 if (!llcrypt_dummy_context_enabled(inode) ||
447 IS_ENCRYPTED(inode)) {
449 "Error %d getting encryption context",
453 /* Fake up a context for an unencrypted directory */
454 memset(&ctx, 0, sizeof(ctx));
455 ctx.version = LLCRYPT_CONTEXT_V1;
456 ctx.v1.contents_encryption_mode = LLCRYPT_MODE_AES_256_XTS;
457 ctx.v1.filenames_encryption_mode = LLCRYPT_MODE_AES_256_CTS;
458 memset(ctx.v1.master_key_descriptor, 0x42,
459 LLCRYPT_KEY_DESCRIPTOR_SIZE);
460 res = sizeof(ctx.v1);
463 crypt_info = kmem_cache_zalloc(llcrypt_info_cachep, GFP_NOFS);
467 crypt_info->ci_inode = inode;
469 res = llcrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
472 "Unrecognized or corrupt encryption context");
476 switch (ctx.version) {
477 case LLCRYPT_CONTEXT_V1:
478 memcpy(crypt_info->ci_nonce, ctx.v1.nonce,
479 FS_KEY_DERIVATION_NONCE_SIZE);
481 case LLCRYPT_CONTEXT_V2:
482 memcpy(crypt_info->ci_nonce, ctx.v2.nonce,
483 FS_KEY_DERIVATION_NONCE_SIZE);
491 if (!llcrypt_supported_policy(&crypt_info->ci_policy, inode)) {
496 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
501 WARN_ON(mode->ivsize > LLCRYPT_MAX_IV_SIZE);
502 crypt_info->ci_mode = mode;
504 res = setup_file_encryption_key(crypt_info, &master_key);
508 if (cmpxchg_release(&(llcrypt_info_nocast(inode)), NULL,
509 crypt_info) == NULL) {
511 struct llcrypt_master_key *mk =
512 master_key->payload.data[0];
514 refcount_inc(&mk->mk_refcount);
515 crypt_info->ci_master_key = key_get(master_key);
516 spin_lock(&mk->mk_decrypted_inodes_lock);
517 list_add(&crypt_info->ci_master_key_link,
518 &mk->mk_decrypted_inodes);
519 spin_unlock(&mk->mk_decrypted_inodes_lock);
526 struct llcrypt_master_key *mk = master_key->payload.data[0];
528 up_read(&mk->mk_secret_sem);
533 put_crypt_info(crypt_info);
536 EXPORT_SYMBOL(llcrypt_get_encryption_info);
539 * llcrypt_put_encryption_info - free most of an inode's llcrypt data
541 * Free the inode's llcrypt_info. Filesystems must call this when the inode is
542 * being evicted. An RCU grace period need not have elapsed yet.
544 void llcrypt_put_encryption_info(struct inode *inode)
546 put_crypt_info(llcrypt_info(inode));
547 llcrypt_info_nocast(inode) = NULL;
549 EXPORT_SYMBOL(llcrypt_put_encryption_info);
552 * llcrypt_free_inode - free an inode's llcrypt data requiring RCU delay
554 * Free the inode's cached decrypted symlink target, if any. Filesystems must
555 * call this after an RCU grace period, just before they free the inode.
557 void llcrypt_free_inode(struct inode *inode)
559 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
560 kfree(inode->i_link);
561 inode->i_link = NULL;
564 EXPORT_SYMBOL(llcrypt_free_inode);
567 * llcrypt_drop_inode - check whether the inode's master key has been removed
569 * Filesystems supporting llcrypt must call this from their ->drop_inode()
570 * method so that encrypted inodes are evicted as soon as they're no longer in
571 * use and their master key has been removed.
573 * Return: 1 if llcrypt wants the inode to be evicted now, otherwise 0
575 int llcrypt_drop_inode(struct inode *inode)
577 const struct llcrypt_info *ci;
578 const struct llcrypt_master_key *mk;
580 ci = (struct llcrypt_info *)READ_ONCE(llcrypt_info_nocast(inode));
582 * If ci is NULL, then the inode doesn't have an encryption key set up
583 * so it's irrelevant. If ci_master_key is NULL, then the master key
584 * was provided via the legacy mechanism of the process-subscribed
585 * keyrings, so we don't know whether it's been removed or not.
587 if (!ci || !ci->ci_master_key)
589 mk = ci->ci_master_key->payload.data[0];
592 * Note: since we aren't holding ->mk_secret_sem, the result here can
593 * immediately become outdated. But there's no correctness problem with
594 * unnecessarily evicting. Nor is there a correctness problem with not
595 * evicting while iput() is racing with the key being removed, since
596 * then the thread removing the key will either evict the inode itself
597 * or will correctly detect that it wasn't evicted due to the race.
599 return !is_master_key_secret_present(&mk->mk_secret);
601 EXPORT_SYMBOL_GPL(llcrypt_drop_inode);
603 inline bool llcrypt_has_encryption_key(const struct inode *inode)
605 /* pairs with cmpxchg_release() in llcrypt_get_encryption_info() */
606 return READ_ONCE(llcrypt_info_nocast(inode)) != NULL;
608 EXPORT_SYMBOL_GPL(llcrypt_has_encryption_key);