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 #include <crypto/skcipher.h>
12 #include <linux/key.h>
13 #include <linux/random.h>
15 #include "fscrypt_private.h"
17 struct fscrypt_mode fscrypt_modes
[] = {
18 [FSCRYPT_MODE_AES_256_XTS
] = {
19 .friendly_name
= "AES-256-XTS",
20 .cipher_str
= "xts(aes)",
23 .blk_crypto_mode
= BLK_ENCRYPTION_MODE_AES_256_XTS
,
25 [FSCRYPT_MODE_AES_256_CTS
] = {
26 .friendly_name
= "AES-256-CTS-CBC",
27 .cipher_str
= "cts(cbc(aes))",
31 [FSCRYPT_MODE_AES_128_CBC
] = {
32 .friendly_name
= "AES-128-CBC-ESSIV",
33 .cipher_str
= "essiv(cbc(aes),sha256)",
36 .blk_crypto_mode
= BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV
,
38 [FSCRYPT_MODE_AES_128_CTS
] = {
39 .friendly_name
= "AES-128-CTS-CBC",
40 .cipher_str
= "cts(cbc(aes))",
44 [FSCRYPT_MODE_ADIANTUM
] = {
45 .friendly_name
= "Adiantum",
46 .cipher_str
= "adiantum(xchacha12,aes)",
49 .blk_crypto_mode
= BLK_ENCRYPTION_MODE_ADIANTUM
,
53 static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex
);
55 static struct fscrypt_mode
*
56 select_encryption_mode(const union fscrypt_policy
*policy
,
57 const struct inode
*inode
)
59 BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes
) != FSCRYPT_MODE_MAX
+ 1);
61 if (S_ISREG(inode
->i_mode
))
62 return &fscrypt_modes
[fscrypt_policy_contents_mode(policy
)];
64 if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
))
65 return &fscrypt_modes
[fscrypt_policy_fnames_mode(policy
)];
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
);
72 /* Create a symmetric cipher object for the given encryption mode and key */
73 static struct crypto_skcipher
*
74 fscrypt_allocate_skcipher(struct fscrypt_mode
*mode
, const u8
*raw_key
,
75 const struct inode
*inode
)
77 struct crypto_skcipher
*tfm
;
80 tfm
= crypto_alloc_skcipher(mode
->cipher_str
, 0, 0);
82 if (PTR_ERR(tfm
) == -ENOENT
) {
84 "Missing crypto API support for %s (API name: \"%s\")",
85 mode
->friendly_name
, mode
->cipher_str
);
86 return ERR_PTR(-ENOPKG
);
88 fscrypt_err(inode
, "Error allocating '%s' transform: %ld",
89 mode
->cipher_str
, PTR_ERR(tfm
));
92 if (!xchg(&mode
->logged_impl_name
, 1)) {
94 * fscrypt performance can vary greatly depending on which
95 * crypto algorithm implementation is used. Help people debug
96 * performance problems by logging the ->cra_driver_name the
97 * first time a mode is used.
99 pr_info("fscrypt: %s using implementation \"%s\"\n",
100 mode
->friendly_name
, crypto_skcipher_driver_name(tfm
));
102 if (WARN_ON(crypto_skcipher_ivsize(tfm
) != mode
->ivsize
)) {
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
);
119 * Prepare the crypto transform object or blk-crypto key in @prep_key, given the
120 * raw key, encryption mode, and flag indicating which encryption implementation
121 * (fs-layer or blk-crypto) will be used.
123 int fscrypt_prepare_key(struct fscrypt_prepared_key
*prep_key
,
124 const u8
*raw_key
, const struct fscrypt_info
*ci
)
126 struct crypto_skcipher
*tfm
;
128 if (fscrypt_using_inline_encryption(ci
))
129 return fscrypt_prepare_inline_crypt_key(prep_key
, raw_key
, ci
);
131 tfm
= fscrypt_allocate_skcipher(ci
->ci_mode
, raw_key
, ci
->ci_inode
);
135 * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
136 * I.e., here we publish ->tfm with a RELEASE barrier so that
137 * concurrent tasks can ACQUIRE it. Note that this concurrency is only
138 * possible for per-mode keys, not for per-file keys.
140 smp_store_release(&prep_key
->tfm
, tfm
);
144 /* Destroy a crypto transform object and/or blk-crypto key. */
145 void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key
*prep_key
)
147 crypto_free_skcipher(prep_key
->tfm
);
148 fscrypt_destroy_inline_crypt_key(prep_key
);
151 /* Given a per-file encryption key, set up the file's crypto transform object */
152 int fscrypt_set_per_file_enc_key(struct fscrypt_info
*ci
, const u8
*raw_key
)
154 ci
->ci_owns_key
= true;
155 return fscrypt_prepare_key(&ci
->ci_enc_key
, raw_key
, ci
);
158 static 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
)
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;
173 if (WARN_ON(mode_num
> FSCRYPT_MODE_MAX
))
176 prep_key
= &keys
[mode_num
];
177 if (fscrypt_is_key_prepared(prep_key
, ci
)) {
178 ci
->ci_enc_key
= *prep_key
;
182 mutex_lock(&fscrypt_mode_key_setup_mutex
);
184 if (fscrypt_is_key_prepared(prep_key
, ci
))
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
,
194 hkdf_infolen
+= sizeof(sb
->s_uuid
);
196 err
= fscrypt_hkdf_expand(&mk
->mk_secret
.hkdf
,
197 hkdf_context
, hkdf_info
, hkdf_infolen
,
198 mode_key
, mode
->keysize
);
201 err
= fscrypt_prepare_key(prep_key
, mode_key
, ci
);
202 memzero_explicit(mode_key
, mode
->keysize
);
206 ci
->ci_enc_key
= *prep_key
;
209 mutex_unlock(&fscrypt_mode_key_setup_mutex
);
214 * Derive a SipHash key from the given fscrypt master key and the given
215 * application-specific information string.
217 * Note that the KDF produces a byte array, but the SipHash APIs expect the key
218 * as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an
219 * endianness swap in order to get the same results as on little endian CPUs.
221 static 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
)
227 err
= fscrypt_hkdf_expand(&mk
->mk_secret
.hkdf
, context
, info
, infolen
,
228 (u8
*)key
, sizeof(*key
));
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]);
239 int fscrypt_derive_dirhash_key(struct fscrypt_info
*ci
,
240 const struct fscrypt_master_key
*mk
)
244 err
= fscrypt_derive_siphash_key(mk
, HKDF_CONTEXT_DIRHASH_KEY
,
245 ci
->ci_nonce
, FSCRYPT_FILE_NONCE_SIZE
,
246 &ci
->ci_dirhash_key
);
249 ci
->ci_dirhash_key_initialized
= true;
253 void fscrypt_hash_inode_number(struct fscrypt_info
*ci
,
254 const struct fscrypt_master_key
*mk
)
256 WARN_ON(ci
->ci_inode
->i_ino
== 0);
257 WARN_ON(!mk
->mk_ino_hash_key_initialized
);
259 ci
->ci_hashed_ino
= (u32
)siphash_1u64(ci
->ci_inode
->i_ino
,
260 &mk
->mk_ino_hash_key
);
263 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info
*ci
,
264 struct fscrypt_master_key
*mk
)
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);
273 /* pairs with smp_store_release() below */
274 if (!smp_load_acquire(&mk
->mk_ino_hash_key_initialized
)) {
276 mutex_lock(&fscrypt_mode_key_setup_mutex
);
278 if (mk
->mk_ino_hash_key_initialized
)
281 err
= fscrypt_derive_siphash_key(mk
,
282 HKDF_CONTEXT_INODE_HASH_KEY
,
283 NULL
, 0, &mk
->mk_ino_hash_key
);
286 /* pairs with smp_load_acquire() above */
287 smp_store_release(&mk
->mk_ino_hash_key_initialized
, true);
289 mutex_unlock(&fscrypt_mode_key_setup_mutex
);
295 * New inodes may not have an inode number assigned yet.
296 * Hashing their inode number is delayed until later.
298 if (ci
->ci_inode
->i_ino
)
299 fscrypt_hash_inode_number(ci
, mk
);
303 static int fscrypt_setup_v2_file_key(struct fscrypt_info
*ci
,
304 struct fscrypt_master_key
*mk
,
305 bool need_dirhash_key
)
309 if (ci
->ci_policy
.v2
.flags
& FSCRYPT_POLICY_FLAG_DIRECT_KEY
) {
311 * DIRECT_KEY: instead of deriving per-file encryption keys, the
312 * per-file nonce will be included in all the IVs. But unlike
313 * v1 policies, for v2 policies in this case we don't encrypt
314 * with the master key directly but rather derive a per-mode
315 * encryption key. This ensures that the master key is
316 * consistently used only for HKDF, avoiding key reuse issues.
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
) {
323 * IV_INO_LBLK_64: encryption keys are derived from (master_key,
324 * mode_num, filesystem_uuid), and inode number is included in
325 * the IVs. This format is optimized for use with inline
326 * encryption hardware compliant with the UFS standard.
328 err
= setup_per_mode_enc_key(ci
, mk
, mk
->mk_iv_ino_lblk_64_keys
,
329 HKDF_CONTEXT_IV_INO_LBLK_64_KEY
,
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
);
335 u8 derived_key
[FSCRYPT_MAX_KEY_SIZE
];
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
);
344 err
= fscrypt_set_per_file_enc_key(ci
, derived_key
);
345 memzero_explicit(derived_key
, ci
->ci_mode
->keysize
);
350 /* Derive a secret dirhash key for directories that need it. */
351 if (need_dirhash_key
) {
352 err
= fscrypt_derive_dirhash_key(ci
, mk
);
361 * Find the master key, then set up the inode's actual encryption key.
363 * If the master key is found in the filesystem-level keyring, then the
364 * corresponding 'struct key' is returned in *master_key_ret with its semaphore
365 * read-locked. This is needed to ensure that only one task links the
366 * fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race to create
367 * an fscrypt_info for the same inode), and to synchronize the master key being
368 * removed with a new inode starting to use it.
370 static int setup_file_encryption_key(struct fscrypt_info
*ci
,
371 bool need_dirhash_key
,
372 struct key
**master_key_ret
)
375 struct fscrypt_master_key
*mk
= NULL
;
376 struct fscrypt_key_specifier mk_spec
;
379 err
= fscrypt_select_encryption_impl(ci
);
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
);
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
);
401 key
= fscrypt_find_master_key(ci
->ci_inode
->i_sb
, &mk_spec
);
403 if (key
!= ERR_PTR(-ENOKEY
) ||
404 ci
->ci_policy
.version
!= FSCRYPT_POLICY_V1
)
408 * As a legacy fallback for v1 policies, search for the key in
409 * the current task's subscribed keyrings too. Don't move this
410 * to before the search of ->s_master_keys, since users
411 * shouldn't be able to override filesystem-level keys.
413 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci
);
416 mk
= key
->payload
.data
[0];
417 down_read(&key
->sem
);
419 /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
420 if (!is_master_key_secret_present(&mk
->mk_secret
)) {
422 goto out_release_key
;
426 * Require that the master key be at least as long as the derived key.
427 * Otherwise, the derived key cannot possibly contain as much entropy as
428 * that required by the encryption mode it will be used for. For v1
429 * policies it's also required for the KDF to work at all.
431 if (mk
->mk_secret
.size
< ci
->ci_mode
->keysize
) {
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
);
438 goto out_release_key
;
441 switch (ci
->ci_policy
.version
) {
442 case FSCRYPT_POLICY_V1
:
443 err
= fscrypt_setup_v1_file_key(ci
, mk
->mk_secret
.raw
);
445 case FSCRYPT_POLICY_V2
:
446 err
= fscrypt_setup_v2_file_key(ci
, mk
, need_dirhash_key
);
454 goto out_release_key
;
456 *master_key_ret
= key
;
465 static void put_crypt_info(struct fscrypt_info
*ci
)
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
);
477 key
= ci
->ci_master_key
;
479 struct fscrypt_master_key
*mk
= key
->payload
.data
[0];
482 * Remove this inode from the list of inodes that were unlocked
483 * with the master key.
485 * In addition, if we're removing the last inode from a key that
486 * already had its secret removed, invalidate the key so that it
487 * gets removed from ->s_master_keys.
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
))
496 memzero_explicit(ci
, sizeof(*ci
));
497 kmem_cache_free(fscrypt_info_cachep
, ci
);
501 fscrypt_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
)
506 struct fscrypt_info
*crypt_info
;
507 struct fscrypt_mode
*mode
;
508 struct key
*master_key
= NULL
;
511 res
= fscrypt_initialize(inode
->i_sb
->s_cop
->flags
);
515 crypt_info
= kmem_cache_zalloc(fscrypt_info_cachep
, GFP_KERNEL
);
519 crypt_info
->ci_inode
= inode
;
520 crypt_info
->ci_policy
= *policy
;
521 memcpy(crypt_info
->ci_nonce
, nonce
, FSCRYPT_FILE_NONCE_SIZE
);
523 mode
= select_encryption_mode(&crypt_info
->ci_policy
, inode
);
528 WARN_ON(mode
->ivsize
> FSCRYPT_MAX_IV_SIZE
);
529 crypt_info
->ci_mode
= mode
;
531 res
= setup_file_encryption_key(crypt_info
, need_dirhash_key
,
537 * For existing inodes, multiple tasks may race to set ->i_crypt_info.
538 * So use cmpxchg_release(). This pairs with the smp_load_acquire() in
539 * fscrypt_get_info(). I.e., here we publish ->i_crypt_info with a
540 * RELEASE barrier so that other tasks can ACQUIRE it.
542 if (cmpxchg_release(&inode
->i_crypt_info
, NULL
, crypt_info
) == NULL
) {
544 * We won the race and set ->i_crypt_info to our crypt_info.
545 * Now link it into the master key's inode list.
548 struct fscrypt_master_key
*mk
=
549 master_key
->payload
.data
[0];
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
);
563 up_read(&master_key
->sem
);
566 put_crypt_info(crypt_info
);
571 * fscrypt_get_encryption_info() - set up an inode's encryption key
572 * @inode: the inode to set up the key for. Must be encrypted.
573 * @allow_unsupported: if %true, treat an unsupported encryption policy (or
574 * unrecognized encryption context) the same way as the key
575 * being unavailable, instead of returning an error. Use
576 * %false unless the operation being performed is needed in
577 * order for files (or directories) to be deleted.
579 * Set up ->i_crypt_info, if it hasn't already been done.
581 * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe. So
582 * generally this shouldn't be called from within a filesystem transaction.
584 * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
585 * encryption key is unavailable. (Use fscrypt_has_encryption_key() to
586 * distinguish these cases.) Also can return another -errno code.
588 int fscrypt_get_encryption_info(struct inode
*inode
, bool allow_unsupported
)
591 union fscrypt_context ctx
;
592 union fscrypt_policy policy
;
594 if (fscrypt_has_encryption_key(inode
))
597 res
= inode
->i_sb
->s_cop
->get_context(inode
, &ctx
, sizeof(ctx
));
599 if (res
== -ERANGE
&& allow_unsupported
)
601 fscrypt_warn(inode
, "Error %d getting encryption context", res
);
605 res
= fscrypt_policy_from_context(&policy
, &ctx
, res
);
607 if (allow_unsupported
)
610 "Unrecognized or corrupt encryption context");
614 if (!fscrypt_supported_policy(&policy
, inode
)) {
615 if (allow_unsupported
)
620 res
= fscrypt_setup_encryption_info(inode
, &policy
,
621 fscrypt_context_nonce(&ctx
),
622 IS_CASEFOLDED(inode
) &&
623 S_ISDIR(inode
->i_mode
));
625 if (res
== -ENOPKG
&& allow_unsupported
) /* Algorithm unavailable? */
633 * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
634 * @dir: a possibly-encrypted directory
635 * @inode: the new inode. ->i_mode must be set already.
636 * ->i_ino doesn't need to be set yet.
637 * @encrypt_ret: (output) set to %true if the new inode will be encrypted
639 * If the directory is encrypted, set up its ->i_crypt_info in preparation for
640 * encrypting the name of the new file. Also, if the new inode will be
641 * encrypted, set up its ->i_crypt_info and set *encrypt_ret=true.
643 * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
644 * any filesystem transaction to create the inode. For this reason, ->i_ino
645 * isn't required to be set yet, as the filesystem may not have set it yet.
647 * This doesn't persist the new inode's encryption context. That still needs to
648 * be done later by calling fscrypt_set_context().
650 * Return: 0 on success, -ENOKEY if the encryption key is missing, or another
653 int fscrypt_prepare_new_inode(struct inode
*dir
, struct inode
*inode
,
656 const union fscrypt_policy
*policy
;
657 u8 nonce
[FSCRYPT_FILE_NONCE_SIZE
];
659 policy
= fscrypt_policy_to_inherit(dir
);
663 return PTR_ERR(policy
);
665 if (WARN_ON_ONCE(inode
->i_mode
== 0))
669 * Only regular files, directories, and symlinks are encrypted.
670 * Special files like device nodes and named pipes aren't.
672 if (!S_ISREG(inode
->i_mode
) &&
673 !S_ISDIR(inode
->i_mode
) &&
674 !S_ISLNK(inode
->i_mode
))
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
));
684 EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode
);
687 * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
688 * @inode: an inode being evicted
690 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
691 * being evicted. An RCU grace period need not have elapsed yet.
693 void fscrypt_put_encryption_info(struct inode
*inode
)
695 put_crypt_info(inode
->i_crypt_info
);
696 inode
->i_crypt_info
= NULL
;
698 EXPORT_SYMBOL(fscrypt_put_encryption_info
);
701 * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay
702 * @inode: an inode being freed
704 * Free the inode's cached decrypted symlink target, if any. Filesystems must
705 * call this after an RCU grace period, just before they free the inode.
707 void fscrypt_free_inode(struct inode
*inode
)
709 if (IS_ENCRYPTED(inode
) && S_ISLNK(inode
->i_mode
)) {
710 kfree(inode
->i_link
);
711 inode
->i_link
= NULL
;
714 EXPORT_SYMBOL(fscrypt_free_inode
);
717 * fscrypt_drop_inode() - check whether the inode's master key has been removed
718 * @inode: an inode being considered for eviction
720 * Filesystems supporting fscrypt must call this from their ->drop_inode()
721 * method so that encrypted inodes are evicted as soon as they're no longer in
722 * use and their master key has been removed.
724 * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
726 int fscrypt_drop_inode(struct inode
*inode
)
728 const struct fscrypt_info
*ci
= fscrypt_get_info(inode
);
729 const struct fscrypt_master_key
*mk
;
732 * If ci is NULL, then the inode doesn't have an encryption key set up
733 * so it's irrelevant. If ci_master_key is NULL, then the master key
734 * was provided via the legacy mechanism of the process-subscribed
735 * keyrings, so we don't know whether it's been removed or not.
737 if (!ci
|| !ci
->ci_master_key
)
739 mk
= ci
->ci_master_key
->payload
.data
[0];
742 * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes
743 * protected by the key were cleaned by sync_filesystem(). But if
744 * userspace is still using the files, inodes can be dirtied between
745 * then and now. We mustn't lose any writes, so skip dirty inodes here.
747 if (inode
->i_state
& I_DIRTY_ALL
)
751 * Note: since we aren't holding the key semaphore, the result here can
752 * immediately become outdated. But there's no correctness problem with
753 * unnecessarily evicting. Nor is there a correctness problem with not
754 * evicting while iput() is racing with the key being removed, since
755 * then the thread removing the key will either evict the inode itself
756 * or will correctly detect that it wasn't evicted due to the race.
758 return !is_master_key_secret_present(&mk
->mk_secret
);
760 EXPORT_SYMBOL_GPL(fscrypt_drop_inode
);