1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Key setup facility for FS encryption support.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2015, Google, Inc.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8*4882a593Smuzhiyun * Heavily modified since then.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <crypto/skcipher.h>
12*4882a593Smuzhiyun #include <linux/random.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include "fscrypt_private.h"
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct fscrypt_mode fscrypt_modes[] = {
17*4882a593Smuzhiyun [FSCRYPT_MODE_AES_256_XTS] = {
18*4882a593Smuzhiyun .friendly_name = "AES-256-XTS",
19*4882a593Smuzhiyun .cipher_str = "xts(aes)",
20*4882a593Smuzhiyun .keysize = 64,
21*4882a593Smuzhiyun .security_strength = 32,
22*4882a593Smuzhiyun .ivsize = 16,
23*4882a593Smuzhiyun .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_256_XTS,
24*4882a593Smuzhiyun },
25*4882a593Smuzhiyun [FSCRYPT_MODE_AES_256_CTS] = {
26*4882a593Smuzhiyun .friendly_name = "AES-256-CTS-CBC",
27*4882a593Smuzhiyun .cipher_str = "cts(cbc(aes))",
28*4882a593Smuzhiyun .keysize = 32,
29*4882a593Smuzhiyun .security_strength = 32,
30*4882a593Smuzhiyun .ivsize = 16,
31*4882a593Smuzhiyun },
32*4882a593Smuzhiyun [FSCRYPT_MODE_AES_128_CBC] = {
33*4882a593Smuzhiyun .friendly_name = "AES-128-CBC-ESSIV",
34*4882a593Smuzhiyun .cipher_str = "essiv(cbc(aes),sha256)",
35*4882a593Smuzhiyun .keysize = 16,
36*4882a593Smuzhiyun .security_strength = 16,
37*4882a593Smuzhiyun .ivsize = 16,
38*4882a593Smuzhiyun .blk_crypto_mode = BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
39*4882a593Smuzhiyun },
40*4882a593Smuzhiyun [FSCRYPT_MODE_AES_128_CTS] = {
41*4882a593Smuzhiyun .friendly_name = "AES-128-CTS-CBC",
42*4882a593Smuzhiyun .cipher_str = "cts(cbc(aes))",
43*4882a593Smuzhiyun .keysize = 16,
44*4882a593Smuzhiyun .security_strength = 16,
45*4882a593Smuzhiyun .ivsize = 16,
46*4882a593Smuzhiyun },
47*4882a593Smuzhiyun [FSCRYPT_MODE_ADIANTUM] = {
48*4882a593Smuzhiyun .friendly_name = "Adiantum",
49*4882a593Smuzhiyun .cipher_str = "adiantum(xchacha12,aes)",
50*4882a593Smuzhiyun .keysize = 32,
51*4882a593Smuzhiyun .security_strength = 32,
52*4882a593Smuzhiyun .ivsize = 32,
53*4882a593Smuzhiyun .blk_crypto_mode = BLK_ENCRYPTION_MODE_ADIANTUM,
54*4882a593Smuzhiyun },
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun static DEFINE_MUTEX(fscrypt_mode_key_setup_mutex);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static struct fscrypt_mode *
select_encryption_mode(const union fscrypt_policy * policy,const struct inode * inode)60*4882a593Smuzhiyun select_encryption_mode(const union fscrypt_policy *policy,
61*4882a593Smuzhiyun const struct inode *inode)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun BUILD_BUG_ON(ARRAY_SIZE(fscrypt_modes) != FSCRYPT_MODE_MAX + 1);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun if (S_ISREG(inode->i_mode))
66*4882a593Smuzhiyun return &fscrypt_modes[fscrypt_policy_contents_mode(policy)];
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
69*4882a593Smuzhiyun return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)];
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
72*4882a593Smuzhiyun inode->i_ino, (inode->i_mode & S_IFMT));
73*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun /* Create a symmetric cipher object for the given encryption mode and key */
77*4882a593Smuzhiyun static struct crypto_skcipher *
fscrypt_allocate_skcipher(struct fscrypt_mode * mode,const u8 * raw_key,const struct inode * inode)78*4882a593Smuzhiyun fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
79*4882a593Smuzhiyun const struct inode *inode)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct crypto_skcipher *tfm;
82*4882a593Smuzhiyun int err;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
85*4882a593Smuzhiyun if (IS_ERR(tfm)) {
86*4882a593Smuzhiyun if (PTR_ERR(tfm) == -ENOENT) {
87*4882a593Smuzhiyun fscrypt_warn(inode,
88*4882a593Smuzhiyun "Missing crypto API support for %s (API name: \"%s\")",
89*4882a593Smuzhiyun mode->friendly_name, mode->cipher_str);
90*4882a593Smuzhiyun return ERR_PTR(-ENOPKG);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun fscrypt_err(inode, "Error allocating '%s' transform: %ld",
93*4882a593Smuzhiyun mode->cipher_str, PTR_ERR(tfm));
94*4882a593Smuzhiyun return tfm;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun if (!xchg(&mode->logged_impl_name, 1)) {
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * fscrypt performance can vary greatly depending on which
99*4882a593Smuzhiyun * crypto algorithm implementation is used. Help people debug
100*4882a593Smuzhiyun * performance problems by logging the ->cra_driver_name the
101*4882a593Smuzhiyun * first time a mode is used.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun pr_info("fscrypt: %s using implementation \"%s\"\n",
104*4882a593Smuzhiyun mode->friendly_name, crypto_skcipher_driver_name(tfm));
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
107*4882a593Smuzhiyun err = -EINVAL;
108*4882a593Smuzhiyun goto err_free_tfm;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
111*4882a593Smuzhiyun err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
112*4882a593Smuzhiyun if (err)
113*4882a593Smuzhiyun goto err_free_tfm;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun return tfm;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun err_free_tfm:
118*4882a593Smuzhiyun crypto_free_skcipher(tfm);
119*4882a593Smuzhiyun return ERR_PTR(err);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * Prepare the crypto transform object or blk-crypto key in @prep_key, given the
124*4882a593Smuzhiyun * raw key, encryption mode, and flag indicating which encryption implementation
125*4882a593Smuzhiyun * (fs-layer or blk-crypto) will be used.
126*4882a593Smuzhiyun */
fscrypt_prepare_key(struct fscrypt_prepared_key * prep_key,const u8 * raw_key,unsigned int raw_key_size,bool is_hw_wrapped,const struct fscrypt_info * ci)127*4882a593Smuzhiyun int fscrypt_prepare_key(struct fscrypt_prepared_key *prep_key,
128*4882a593Smuzhiyun const u8 *raw_key, unsigned int raw_key_size,
129*4882a593Smuzhiyun bool is_hw_wrapped, const struct fscrypt_info *ci)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct crypto_skcipher *tfm;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (fscrypt_using_inline_encryption(ci))
134*4882a593Smuzhiyun return fscrypt_prepare_inline_crypt_key(prep_key,
135*4882a593Smuzhiyun raw_key, raw_key_size, is_hw_wrapped, ci);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun if (WARN_ON(is_hw_wrapped || raw_key_size != ci->ci_mode->keysize))
138*4882a593Smuzhiyun return -EINVAL;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
141*4882a593Smuzhiyun if (IS_ERR(tfm))
142*4882a593Smuzhiyun return PTR_ERR(tfm);
143*4882a593Smuzhiyun /*
144*4882a593Smuzhiyun * Pairs with the smp_load_acquire() in fscrypt_is_key_prepared().
145*4882a593Smuzhiyun * I.e., here we publish ->tfm with a RELEASE barrier so that
146*4882a593Smuzhiyun * concurrent tasks can ACQUIRE it. Note that this concurrency is only
147*4882a593Smuzhiyun * possible for per-mode keys, not for per-file keys.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun smp_store_release(&prep_key->tfm, tfm);
150*4882a593Smuzhiyun return 0;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* Destroy a crypto transform object and/or blk-crypto key. */
fscrypt_destroy_prepared_key(struct fscrypt_prepared_key * prep_key)154*4882a593Smuzhiyun void fscrypt_destroy_prepared_key(struct fscrypt_prepared_key *prep_key)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun crypto_free_skcipher(prep_key->tfm);
157*4882a593Smuzhiyun fscrypt_destroy_inline_crypt_key(prep_key);
158*4882a593Smuzhiyun memzero_explicit(prep_key, sizeof(*prep_key));
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Given a per-file encryption key, set up the file's crypto transform object */
fscrypt_set_per_file_enc_key(struct fscrypt_info * ci,const u8 * raw_key)162*4882a593Smuzhiyun int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun ci->ci_owns_key = true;
165*4882a593Smuzhiyun return fscrypt_prepare_key(&ci->ci_enc_key, raw_key,
166*4882a593Smuzhiyun ci->ci_mode->keysize,
167*4882a593Smuzhiyun false /*is_hw_wrapped*/, ci);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
setup_per_mode_enc_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk,struct fscrypt_prepared_key * keys,u8 hkdf_context,bool include_fs_uuid)170*4882a593Smuzhiyun static int setup_per_mode_enc_key(struct fscrypt_info *ci,
171*4882a593Smuzhiyun struct fscrypt_master_key *mk,
172*4882a593Smuzhiyun struct fscrypt_prepared_key *keys,
173*4882a593Smuzhiyun u8 hkdf_context, bool include_fs_uuid)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun const struct inode *inode = ci->ci_inode;
176*4882a593Smuzhiyun const struct super_block *sb = inode->i_sb;
177*4882a593Smuzhiyun struct fscrypt_mode *mode = ci->ci_mode;
178*4882a593Smuzhiyun const u8 mode_num = mode - fscrypt_modes;
179*4882a593Smuzhiyun struct fscrypt_prepared_key *prep_key;
180*4882a593Smuzhiyun u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
181*4882a593Smuzhiyun u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
182*4882a593Smuzhiyun unsigned int hkdf_infolen = 0;
183*4882a593Smuzhiyun int err;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun if (WARN_ON(mode_num > FSCRYPT_MODE_MAX))
186*4882a593Smuzhiyun return -EINVAL;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun prep_key = &keys[mode_num];
189*4882a593Smuzhiyun if (fscrypt_is_key_prepared(prep_key, ci)) {
190*4882a593Smuzhiyun ci->ci_enc_key = *prep_key;
191*4882a593Smuzhiyun return 0;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun mutex_lock(&fscrypt_mode_key_setup_mutex);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (fscrypt_is_key_prepared(prep_key, ci))
197*4882a593Smuzhiyun goto done_unlock;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (mk->mk_secret.is_hw_wrapped && S_ISREG(inode->i_mode)) {
200*4882a593Smuzhiyun int i;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (!fscrypt_using_inline_encryption(ci)) {
203*4882a593Smuzhiyun fscrypt_warn(ci->ci_inode,
204*4882a593Smuzhiyun "Hardware-wrapped keys require inline encryption (-o inlinecrypt)");
205*4882a593Smuzhiyun err = -EINVAL;
206*4882a593Smuzhiyun goto out_unlock;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
209*4882a593Smuzhiyun if (fscrypt_is_key_prepared(&keys[i], ci)) {
210*4882a593Smuzhiyun fscrypt_warn(ci->ci_inode,
211*4882a593Smuzhiyun "Each hardware-wrapped key can only be used with one encryption mode");
212*4882a593Smuzhiyun err = -EINVAL;
213*4882a593Smuzhiyun goto out_unlock;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun err = fscrypt_prepare_key(prep_key, mk->mk_secret.raw,
217*4882a593Smuzhiyun mk->mk_secret.size, true, ci);
218*4882a593Smuzhiyun if (err)
219*4882a593Smuzhiyun goto out_unlock;
220*4882a593Smuzhiyun } else {
221*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(mode_num) != 1);
222*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
223*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(hkdf_info) != 17);
224*4882a593Smuzhiyun hkdf_info[hkdf_infolen++] = mode_num;
225*4882a593Smuzhiyun if (include_fs_uuid) {
226*4882a593Smuzhiyun memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
227*4882a593Smuzhiyun sizeof(sb->s_uuid));
228*4882a593Smuzhiyun hkdf_infolen += sizeof(sb->s_uuid);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
231*4882a593Smuzhiyun hkdf_context, hkdf_info, hkdf_infolen,
232*4882a593Smuzhiyun mode_key, mode->keysize);
233*4882a593Smuzhiyun if (err)
234*4882a593Smuzhiyun goto out_unlock;
235*4882a593Smuzhiyun err = fscrypt_prepare_key(prep_key, mode_key, mode->keysize,
236*4882a593Smuzhiyun false /*is_hw_wrapped*/, ci);
237*4882a593Smuzhiyun memzero_explicit(mode_key, mode->keysize);
238*4882a593Smuzhiyun if (err)
239*4882a593Smuzhiyun goto out_unlock;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun done_unlock:
242*4882a593Smuzhiyun ci->ci_enc_key = *prep_key;
243*4882a593Smuzhiyun err = 0;
244*4882a593Smuzhiyun out_unlock:
245*4882a593Smuzhiyun mutex_unlock(&fscrypt_mode_key_setup_mutex);
246*4882a593Smuzhiyun return err;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun * Derive a SipHash key from the given fscrypt master key and the given
251*4882a593Smuzhiyun * application-specific information string.
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun * Note that the KDF produces a byte array, but the SipHash APIs expect the key
254*4882a593Smuzhiyun * as a pair of 64-bit words. Therefore, on big endian CPUs we have to do an
255*4882a593Smuzhiyun * endianness swap in order to get the same results as on little endian CPUs.
256*4882a593Smuzhiyun */
fscrypt_derive_siphash_key(const struct fscrypt_master_key * mk,u8 context,const u8 * info,unsigned int infolen,siphash_key_t * key)257*4882a593Smuzhiyun static int fscrypt_derive_siphash_key(const struct fscrypt_master_key *mk,
258*4882a593Smuzhiyun u8 context, const u8 *info,
259*4882a593Smuzhiyun unsigned int infolen, siphash_key_t *key)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun int err;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, context, info, infolen,
264*4882a593Smuzhiyun (u8 *)key, sizeof(*key));
265*4882a593Smuzhiyun if (err)
266*4882a593Smuzhiyun return err;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(*key) != 16);
269*4882a593Smuzhiyun BUILD_BUG_ON(ARRAY_SIZE(key->key) != 2);
270*4882a593Smuzhiyun le64_to_cpus(&key->key[0]);
271*4882a593Smuzhiyun le64_to_cpus(&key->key[1]);
272*4882a593Smuzhiyun return 0;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
fscrypt_derive_dirhash_key(struct fscrypt_info * ci,const struct fscrypt_master_key * mk)275*4882a593Smuzhiyun int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
276*4882a593Smuzhiyun const struct fscrypt_master_key *mk)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun int err;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun err = fscrypt_derive_siphash_key(mk, HKDF_CONTEXT_DIRHASH_KEY,
281*4882a593Smuzhiyun ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
282*4882a593Smuzhiyun &ci->ci_dirhash_key);
283*4882a593Smuzhiyun if (err)
284*4882a593Smuzhiyun return err;
285*4882a593Smuzhiyun ci->ci_dirhash_key_initialized = true;
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
fscrypt_hash_inode_number(struct fscrypt_info * ci,const struct fscrypt_master_key * mk)289*4882a593Smuzhiyun void fscrypt_hash_inode_number(struct fscrypt_info *ci,
290*4882a593Smuzhiyun const struct fscrypt_master_key *mk)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun WARN_ON(ci->ci_inode->i_ino == 0);
293*4882a593Smuzhiyun WARN_ON(!mk->mk_ino_hash_key_initialized);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
296*4882a593Smuzhiyun &mk->mk_ino_hash_key);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk)299*4882a593Smuzhiyun static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
300*4882a593Smuzhiyun struct fscrypt_master_key *mk)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun int err;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_32_keys,
305*4882a593Smuzhiyun HKDF_CONTEXT_IV_INO_LBLK_32_KEY, true);
306*4882a593Smuzhiyun if (err)
307*4882a593Smuzhiyun return err;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* pairs with smp_store_release() below */
310*4882a593Smuzhiyun if (!smp_load_acquire(&mk->mk_ino_hash_key_initialized)) {
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun mutex_lock(&fscrypt_mode_key_setup_mutex);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun if (mk->mk_ino_hash_key_initialized)
315*4882a593Smuzhiyun goto unlock;
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun err = fscrypt_derive_siphash_key(mk,
318*4882a593Smuzhiyun HKDF_CONTEXT_INODE_HASH_KEY,
319*4882a593Smuzhiyun NULL, 0, &mk->mk_ino_hash_key);
320*4882a593Smuzhiyun if (err)
321*4882a593Smuzhiyun goto unlock;
322*4882a593Smuzhiyun /* pairs with smp_load_acquire() above */
323*4882a593Smuzhiyun smp_store_release(&mk->mk_ino_hash_key_initialized, true);
324*4882a593Smuzhiyun unlock:
325*4882a593Smuzhiyun mutex_unlock(&fscrypt_mode_key_setup_mutex);
326*4882a593Smuzhiyun if (err)
327*4882a593Smuzhiyun return err;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /*
331*4882a593Smuzhiyun * New inodes may not have an inode number assigned yet.
332*4882a593Smuzhiyun * Hashing their inode number is delayed until later.
333*4882a593Smuzhiyun */
334*4882a593Smuzhiyun if (ci->ci_inode->i_ino)
335*4882a593Smuzhiyun fscrypt_hash_inode_number(ci, mk);
336*4882a593Smuzhiyun return 0;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
fscrypt_setup_v2_file_key(struct fscrypt_info * ci,struct fscrypt_master_key * mk,bool need_dirhash_key)339*4882a593Smuzhiyun static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
340*4882a593Smuzhiyun struct fscrypt_master_key *mk,
341*4882a593Smuzhiyun bool need_dirhash_key)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun int err;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if (mk->mk_secret.is_hw_wrapped &&
346*4882a593Smuzhiyun !(ci->ci_policy.v2.flags & (FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 |
347*4882a593Smuzhiyun FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32))) {
348*4882a593Smuzhiyun fscrypt_warn(ci->ci_inode,
349*4882a593Smuzhiyun "Hardware-wrapped keys are only supported with IV_INO_LBLK policies");
350*4882a593Smuzhiyun return -EINVAL;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
354*4882a593Smuzhiyun /*
355*4882a593Smuzhiyun * DIRECT_KEY: instead of deriving per-file encryption keys, the
356*4882a593Smuzhiyun * per-file nonce will be included in all the IVs. But unlike
357*4882a593Smuzhiyun * v1 policies, for v2 policies in this case we don't encrypt
358*4882a593Smuzhiyun * with the master key directly but rather derive a per-mode
359*4882a593Smuzhiyun * encryption key. This ensures that the master key is
360*4882a593Smuzhiyun * consistently used only for HKDF, avoiding key reuse issues.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_keys,
363*4882a593Smuzhiyun HKDF_CONTEXT_DIRECT_KEY, false);
364*4882a593Smuzhiyun } else if (ci->ci_policy.v2.flags &
365*4882a593Smuzhiyun FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
366*4882a593Smuzhiyun /*
367*4882a593Smuzhiyun * IV_INO_LBLK_64: encryption keys are derived from (master_key,
368*4882a593Smuzhiyun * mode_num, filesystem_uuid), and inode number is included in
369*4882a593Smuzhiyun * the IVs. This format is optimized for use with inline
370*4882a593Smuzhiyun * encryption hardware compliant with the UFS standard.
371*4882a593Smuzhiyun */
372*4882a593Smuzhiyun err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_keys,
373*4882a593Smuzhiyun HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
374*4882a593Smuzhiyun true);
375*4882a593Smuzhiyun } else if (ci->ci_policy.v2.flags &
376*4882a593Smuzhiyun FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) {
377*4882a593Smuzhiyun err = fscrypt_setup_iv_ino_lblk_32_key(ci, mk);
378*4882a593Smuzhiyun } else {
379*4882a593Smuzhiyun u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
382*4882a593Smuzhiyun HKDF_CONTEXT_PER_FILE_ENC_KEY,
383*4882a593Smuzhiyun ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE,
384*4882a593Smuzhiyun derived_key, ci->ci_mode->keysize);
385*4882a593Smuzhiyun if (err)
386*4882a593Smuzhiyun return err;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun err = fscrypt_set_per_file_enc_key(ci, derived_key);
389*4882a593Smuzhiyun memzero_explicit(derived_key, ci->ci_mode->keysize);
390*4882a593Smuzhiyun }
391*4882a593Smuzhiyun if (err)
392*4882a593Smuzhiyun return err;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /* Derive a secret dirhash key for directories that need it. */
395*4882a593Smuzhiyun if (need_dirhash_key) {
396*4882a593Smuzhiyun err = fscrypt_derive_dirhash_key(ci, mk);
397*4882a593Smuzhiyun if (err)
398*4882a593Smuzhiyun return err;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun return 0;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /*
405*4882a593Smuzhiyun * Check whether the size of the given master key (@mk) is appropriate for the
406*4882a593Smuzhiyun * encryption settings which a particular file will use (@ci).
407*4882a593Smuzhiyun *
408*4882a593Smuzhiyun * If the file uses a v1 encryption policy, then the master key must be at least
409*4882a593Smuzhiyun * as long as the derived key, as this is a requirement of the v1 KDF.
410*4882a593Smuzhiyun *
411*4882a593Smuzhiyun * Otherwise, the KDF can accept any size key, so we enforce a slightly looser
412*4882a593Smuzhiyun * requirement: we require that the size of the master key be at least the
413*4882a593Smuzhiyun * maximum security strength of any algorithm whose key will be derived from it
414*4882a593Smuzhiyun * (but in practice we only need to consider @ci->ci_mode, since any other
415*4882a593Smuzhiyun * possible subkeys such as DIRHASH and INODE_HASH will never increase the
416*4882a593Smuzhiyun * required key size over @ci->ci_mode). This allows AES-256-XTS keys to be
417*4882a593Smuzhiyun * derived from a 256-bit master key, which is cryptographically sufficient,
418*4882a593Smuzhiyun * rather than requiring a 512-bit master key which is unnecessarily long. (We
419*4882a593Smuzhiyun * still allow 512-bit master keys if the user chooses to use them, though.)
420*4882a593Smuzhiyun */
fscrypt_valid_master_key_size(const struct fscrypt_master_key * mk,const struct fscrypt_info * ci)421*4882a593Smuzhiyun static bool fscrypt_valid_master_key_size(const struct fscrypt_master_key *mk,
422*4882a593Smuzhiyun const struct fscrypt_info *ci)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun unsigned int min_keysize;
425*4882a593Smuzhiyun
426*4882a593Smuzhiyun if (ci->ci_policy.version == FSCRYPT_POLICY_V1)
427*4882a593Smuzhiyun min_keysize = ci->ci_mode->keysize;
428*4882a593Smuzhiyun else
429*4882a593Smuzhiyun min_keysize = ci->ci_mode->security_strength;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun if (mk->mk_secret.size < min_keysize) {
432*4882a593Smuzhiyun fscrypt_warn(NULL,
433*4882a593Smuzhiyun "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
434*4882a593Smuzhiyun master_key_spec_type(&mk->mk_spec),
435*4882a593Smuzhiyun master_key_spec_len(&mk->mk_spec),
436*4882a593Smuzhiyun (u8 *)&mk->mk_spec.u,
437*4882a593Smuzhiyun mk->mk_secret.size, min_keysize);
438*4882a593Smuzhiyun return false;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun return true;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /*
444*4882a593Smuzhiyun * Find the master key, then set up the inode's actual encryption key.
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * If the master key is found in the filesystem-level keyring, then it is
447*4882a593Smuzhiyun * returned in *mk_ret with its semaphore read-locked. This is needed to ensure
448*4882a593Smuzhiyun * that only one task links the fscrypt_info into ->mk_decrypted_inodes (as
449*4882a593Smuzhiyun * multiple tasks may race to create an fscrypt_info for the same inode), and to
450*4882a593Smuzhiyun * synchronize the master key being removed with a new inode starting to use it.
451*4882a593Smuzhiyun */
setup_file_encryption_key(struct fscrypt_info * ci,bool need_dirhash_key,struct fscrypt_master_key ** mk_ret)452*4882a593Smuzhiyun static int setup_file_encryption_key(struct fscrypt_info *ci,
453*4882a593Smuzhiyun bool need_dirhash_key,
454*4882a593Smuzhiyun struct fscrypt_master_key **mk_ret)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun struct fscrypt_key_specifier mk_spec;
457*4882a593Smuzhiyun struct fscrypt_master_key *mk;
458*4882a593Smuzhiyun int err;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun switch (ci->ci_policy.version) {
461*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
462*4882a593Smuzhiyun mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
463*4882a593Smuzhiyun memcpy(mk_spec.u.descriptor,
464*4882a593Smuzhiyun ci->ci_policy.v1.master_key_descriptor,
465*4882a593Smuzhiyun FSCRYPT_KEY_DESCRIPTOR_SIZE);
466*4882a593Smuzhiyun break;
467*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
468*4882a593Smuzhiyun mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
469*4882a593Smuzhiyun memcpy(mk_spec.u.identifier,
470*4882a593Smuzhiyun ci->ci_policy.v2.master_key_identifier,
471*4882a593Smuzhiyun FSCRYPT_KEY_IDENTIFIER_SIZE);
472*4882a593Smuzhiyun break;
473*4882a593Smuzhiyun default:
474*4882a593Smuzhiyun WARN_ON(1);
475*4882a593Smuzhiyun return -EINVAL;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun mk = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
479*4882a593Smuzhiyun if (!mk) {
480*4882a593Smuzhiyun if (ci->ci_policy.version != FSCRYPT_POLICY_V1)
481*4882a593Smuzhiyun return -ENOKEY;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun err = fscrypt_select_encryption_impl(ci, false);
484*4882a593Smuzhiyun if (err)
485*4882a593Smuzhiyun return err;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /*
488*4882a593Smuzhiyun * As a legacy fallback for v1 policies, search for the key in
489*4882a593Smuzhiyun * the current task's subscribed keyrings too. Don't move this
490*4882a593Smuzhiyun * to before the search of ->s_master_keys, since users
491*4882a593Smuzhiyun * shouldn't be able to override filesystem-level keys.
492*4882a593Smuzhiyun */
493*4882a593Smuzhiyun return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun down_read(&mk->mk_sem);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
498*4882a593Smuzhiyun if (!is_master_key_secret_present(&mk->mk_secret)) {
499*4882a593Smuzhiyun err = -ENOKEY;
500*4882a593Smuzhiyun goto out_release_key;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun if (!fscrypt_valid_master_key_size(mk, ci)) {
504*4882a593Smuzhiyun err = -ENOKEY;
505*4882a593Smuzhiyun goto out_release_key;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun err = fscrypt_select_encryption_impl(ci, mk->mk_secret.is_hw_wrapped);
509*4882a593Smuzhiyun if (err)
510*4882a593Smuzhiyun goto out_release_key;
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun switch (ci->ci_policy.version) {
513*4882a593Smuzhiyun case FSCRYPT_POLICY_V1:
514*4882a593Smuzhiyun err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
515*4882a593Smuzhiyun break;
516*4882a593Smuzhiyun case FSCRYPT_POLICY_V2:
517*4882a593Smuzhiyun err = fscrypt_setup_v2_file_key(ci, mk, need_dirhash_key);
518*4882a593Smuzhiyun break;
519*4882a593Smuzhiyun default:
520*4882a593Smuzhiyun WARN_ON(1);
521*4882a593Smuzhiyun err = -EINVAL;
522*4882a593Smuzhiyun break;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun if (err)
525*4882a593Smuzhiyun goto out_release_key;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun *mk_ret = mk;
528*4882a593Smuzhiyun return 0;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun out_release_key:
531*4882a593Smuzhiyun up_read(&mk->mk_sem);
532*4882a593Smuzhiyun fscrypt_put_master_key(mk);
533*4882a593Smuzhiyun return err;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
put_crypt_info(struct fscrypt_info * ci)536*4882a593Smuzhiyun static void put_crypt_info(struct fscrypt_info *ci)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun struct fscrypt_master_key *mk;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (!ci)
541*4882a593Smuzhiyun return;
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if (ci->ci_direct_key)
544*4882a593Smuzhiyun fscrypt_put_direct_key(ci->ci_direct_key);
545*4882a593Smuzhiyun else if (ci->ci_owns_key)
546*4882a593Smuzhiyun fscrypt_destroy_prepared_key(&ci->ci_enc_key);
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun mk = ci->ci_master_key;
549*4882a593Smuzhiyun if (mk) {
550*4882a593Smuzhiyun /*
551*4882a593Smuzhiyun * Remove this inode from the list of inodes that were unlocked
552*4882a593Smuzhiyun * with the master key. In addition, if we're removing the last
553*4882a593Smuzhiyun * inode from a master key struct that already had its secret
554*4882a593Smuzhiyun * removed, then complete the full removal of the struct.
555*4882a593Smuzhiyun */
556*4882a593Smuzhiyun spin_lock(&mk->mk_decrypted_inodes_lock);
557*4882a593Smuzhiyun list_del(&ci->ci_master_key_link);
558*4882a593Smuzhiyun spin_unlock(&mk->mk_decrypted_inodes_lock);
559*4882a593Smuzhiyun fscrypt_put_master_key_activeref(mk);
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun memzero_explicit(ci, sizeof(*ci));
562*4882a593Smuzhiyun kmem_cache_free(fscrypt_info_cachep, ci);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun static int
fscrypt_setup_encryption_info(struct inode * inode,const union fscrypt_policy * policy,const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],bool need_dirhash_key)566*4882a593Smuzhiyun fscrypt_setup_encryption_info(struct inode *inode,
567*4882a593Smuzhiyun const union fscrypt_policy *policy,
568*4882a593Smuzhiyun const u8 nonce[FSCRYPT_FILE_NONCE_SIZE],
569*4882a593Smuzhiyun bool need_dirhash_key)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun struct fscrypt_info *crypt_info;
572*4882a593Smuzhiyun struct fscrypt_mode *mode;
573*4882a593Smuzhiyun struct fscrypt_master_key *mk = NULL;
574*4882a593Smuzhiyun int res;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun res = fscrypt_initialize(inode->i_sb->s_cop->flags);
577*4882a593Smuzhiyun if (res)
578*4882a593Smuzhiyun return res;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL);
581*4882a593Smuzhiyun if (!crypt_info)
582*4882a593Smuzhiyun return -ENOMEM;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun crypt_info->ci_inode = inode;
585*4882a593Smuzhiyun crypt_info->ci_policy = *policy;
586*4882a593Smuzhiyun memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun mode = select_encryption_mode(&crypt_info->ci_policy, inode);
589*4882a593Smuzhiyun if (IS_ERR(mode)) {
590*4882a593Smuzhiyun res = PTR_ERR(mode);
591*4882a593Smuzhiyun goto out;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
594*4882a593Smuzhiyun crypt_info->ci_mode = mode;
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun res = setup_file_encryption_key(crypt_info, need_dirhash_key, &mk);
597*4882a593Smuzhiyun if (res)
598*4882a593Smuzhiyun goto out;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun /*
601*4882a593Smuzhiyun * For existing inodes, multiple tasks may race to set ->i_crypt_info.
602*4882a593Smuzhiyun * So use cmpxchg_release(). This pairs with the smp_load_acquire() in
603*4882a593Smuzhiyun * fscrypt_get_info(). I.e., here we publish ->i_crypt_info with a
604*4882a593Smuzhiyun * RELEASE barrier so that other tasks can ACQUIRE it.
605*4882a593Smuzhiyun */
606*4882a593Smuzhiyun if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * We won the race and set ->i_crypt_info to our crypt_info.
609*4882a593Smuzhiyun * Now link it into the master key's inode list.
610*4882a593Smuzhiyun */
611*4882a593Smuzhiyun if (mk) {
612*4882a593Smuzhiyun crypt_info->ci_master_key = mk;
613*4882a593Smuzhiyun refcount_inc(&mk->mk_active_refs);
614*4882a593Smuzhiyun spin_lock(&mk->mk_decrypted_inodes_lock);
615*4882a593Smuzhiyun list_add(&crypt_info->ci_master_key_link,
616*4882a593Smuzhiyun &mk->mk_decrypted_inodes);
617*4882a593Smuzhiyun spin_unlock(&mk->mk_decrypted_inodes_lock);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun crypt_info = NULL;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun res = 0;
622*4882a593Smuzhiyun out:
623*4882a593Smuzhiyun if (mk) {
624*4882a593Smuzhiyun up_read(&mk->mk_sem);
625*4882a593Smuzhiyun fscrypt_put_master_key(mk);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun put_crypt_info(crypt_info);
628*4882a593Smuzhiyun return res;
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /**
632*4882a593Smuzhiyun * fscrypt_get_encryption_info() - set up an inode's encryption key
633*4882a593Smuzhiyun * @inode: the inode to set up the key for. Must be encrypted.
634*4882a593Smuzhiyun * @allow_unsupported: if %true, treat an unsupported encryption policy (or
635*4882a593Smuzhiyun * unrecognized encryption context) the same way as the key
636*4882a593Smuzhiyun * being unavailable, instead of returning an error. Use
637*4882a593Smuzhiyun * %false unless the operation being performed is needed in
638*4882a593Smuzhiyun * order for files (or directories) to be deleted.
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * Set up ->i_crypt_info, if it hasn't already been done.
641*4882a593Smuzhiyun *
642*4882a593Smuzhiyun * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe. So
643*4882a593Smuzhiyun * generally this shouldn't be called from within a filesystem transaction.
644*4882a593Smuzhiyun *
645*4882a593Smuzhiyun * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
646*4882a593Smuzhiyun * encryption key is unavailable. (Use fscrypt_has_encryption_key() to
647*4882a593Smuzhiyun * distinguish these cases.) Also can return another -errno code.
648*4882a593Smuzhiyun */
fscrypt_get_encryption_info(struct inode * inode,bool allow_unsupported)649*4882a593Smuzhiyun int fscrypt_get_encryption_info(struct inode *inode, bool allow_unsupported)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun int res;
652*4882a593Smuzhiyun union fscrypt_context ctx;
653*4882a593Smuzhiyun union fscrypt_policy policy;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (fscrypt_has_encryption_key(inode))
656*4882a593Smuzhiyun return 0;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
659*4882a593Smuzhiyun if (res < 0) {
660*4882a593Smuzhiyun if (res == -ERANGE && allow_unsupported)
661*4882a593Smuzhiyun return 0;
662*4882a593Smuzhiyun fscrypt_warn(inode, "Error %d getting encryption context", res);
663*4882a593Smuzhiyun return res;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun res = fscrypt_policy_from_context(&policy, &ctx, res);
667*4882a593Smuzhiyun if (res) {
668*4882a593Smuzhiyun if (allow_unsupported)
669*4882a593Smuzhiyun return 0;
670*4882a593Smuzhiyun fscrypt_warn(inode,
671*4882a593Smuzhiyun "Unrecognized or corrupt encryption context");
672*4882a593Smuzhiyun return res;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (!fscrypt_supported_policy(&policy, inode)) {
676*4882a593Smuzhiyun if (allow_unsupported)
677*4882a593Smuzhiyun return 0;
678*4882a593Smuzhiyun return -EINVAL;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun res = fscrypt_setup_encryption_info(inode, &policy,
682*4882a593Smuzhiyun fscrypt_context_nonce(&ctx),
683*4882a593Smuzhiyun IS_CASEFOLDED(inode) &&
684*4882a593Smuzhiyun S_ISDIR(inode->i_mode));
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun if (res == -ENOPKG && allow_unsupported) /* Algorithm unavailable? */
687*4882a593Smuzhiyun res = 0;
688*4882a593Smuzhiyun if (res == -ENOKEY)
689*4882a593Smuzhiyun res = 0;
690*4882a593Smuzhiyun return res;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /**
694*4882a593Smuzhiyun * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
695*4882a593Smuzhiyun * @dir: a possibly-encrypted directory
696*4882a593Smuzhiyun * @inode: the new inode. ->i_mode must be set already.
697*4882a593Smuzhiyun * ->i_ino doesn't need to be set yet.
698*4882a593Smuzhiyun * @encrypt_ret: (output) set to %true if the new inode will be encrypted
699*4882a593Smuzhiyun *
700*4882a593Smuzhiyun * If the directory is encrypted, set up its ->i_crypt_info in preparation for
701*4882a593Smuzhiyun * encrypting the name of the new file. Also, if the new inode will be
702*4882a593Smuzhiyun * encrypted, set up its ->i_crypt_info and set *encrypt_ret=true.
703*4882a593Smuzhiyun *
704*4882a593Smuzhiyun * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
705*4882a593Smuzhiyun * any filesystem transaction to create the inode. For this reason, ->i_ino
706*4882a593Smuzhiyun * isn't required to be set yet, as the filesystem may not have set it yet.
707*4882a593Smuzhiyun *
708*4882a593Smuzhiyun * This doesn't persist the new inode's encryption context. That still needs to
709*4882a593Smuzhiyun * be done later by calling fscrypt_set_context().
710*4882a593Smuzhiyun *
711*4882a593Smuzhiyun * Return: 0 on success, -ENOKEY if the encryption key is missing, or another
712*4882a593Smuzhiyun * -errno code
713*4882a593Smuzhiyun */
fscrypt_prepare_new_inode(struct inode * dir,struct inode * inode,bool * encrypt_ret)714*4882a593Smuzhiyun int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
715*4882a593Smuzhiyun bool *encrypt_ret)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun const union fscrypt_policy *policy;
718*4882a593Smuzhiyun u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun policy = fscrypt_policy_to_inherit(dir);
721*4882a593Smuzhiyun if (policy == NULL)
722*4882a593Smuzhiyun return 0;
723*4882a593Smuzhiyun if (IS_ERR(policy))
724*4882a593Smuzhiyun return PTR_ERR(policy);
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun if (WARN_ON_ONCE(inode->i_mode == 0))
727*4882a593Smuzhiyun return -EINVAL;
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun /*
730*4882a593Smuzhiyun * Only regular files, directories, and symlinks are encrypted.
731*4882a593Smuzhiyun * Special files like device nodes and named pipes aren't.
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun if (!S_ISREG(inode->i_mode) &&
734*4882a593Smuzhiyun !S_ISDIR(inode->i_mode) &&
735*4882a593Smuzhiyun !S_ISLNK(inode->i_mode))
736*4882a593Smuzhiyun return 0;
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun *encrypt_ret = true;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
741*4882a593Smuzhiyun return fscrypt_setup_encryption_info(inode, policy, nonce,
742*4882a593Smuzhiyun IS_CASEFOLDED(dir) &&
743*4882a593Smuzhiyun S_ISDIR(inode->i_mode));
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /**
748*4882a593Smuzhiyun * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
749*4882a593Smuzhiyun * @inode: an inode being evicted
750*4882a593Smuzhiyun *
751*4882a593Smuzhiyun * Free the inode's fscrypt_info. Filesystems must call this when the inode is
752*4882a593Smuzhiyun * being evicted. An RCU grace period need not have elapsed yet.
753*4882a593Smuzhiyun */
fscrypt_put_encryption_info(struct inode * inode)754*4882a593Smuzhiyun void fscrypt_put_encryption_info(struct inode *inode)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun put_crypt_info(inode->i_crypt_info);
757*4882a593Smuzhiyun inode->i_crypt_info = NULL;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun EXPORT_SYMBOL(fscrypt_put_encryption_info);
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /**
762*4882a593Smuzhiyun * fscrypt_free_inode() - free an inode's fscrypt data requiring RCU delay
763*4882a593Smuzhiyun * @inode: an inode being freed
764*4882a593Smuzhiyun *
765*4882a593Smuzhiyun * Free the inode's cached decrypted symlink target, if any. Filesystems must
766*4882a593Smuzhiyun * call this after an RCU grace period, just before they free the inode.
767*4882a593Smuzhiyun */
fscrypt_free_inode(struct inode * inode)768*4882a593Smuzhiyun void fscrypt_free_inode(struct inode *inode)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
771*4882a593Smuzhiyun kfree(inode->i_link);
772*4882a593Smuzhiyun inode->i_link = NULL;
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun EXPORT_SYMBOL(fscrypt_free_inode);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun /**
778*4882a593Smuzhiyun * fscrypt_drop_inode() - check whether the inode's master key has been removed
779*4882a593Smuzhiyun * @inode: an inode being considered for eviction
780*4882a593Smuzhiyun *
781*4882a593Smuzhiyun * Filesystems supporting fscrypt must call this from their ->drop_inode()
782*4882a593Smuzhiyun * method so that encrypted inodes are evicted as soon as they're no longer in
783*4882a593Smuzhiyun * use and their master key has been removed.
784*4882a593Smuzhiyun *
785*4882a593Smuzhiyun * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
786*4882a593Smuzhiyun */
fscrypt_drop_inode(struct inode * inode)787*4882a593Smuzhiyun int fscrypt_drop_inode(struct inode *inode)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun const struct fscrypt_info *ci = fscrypt_get_info(inode);
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun /*
792*4882a593Smuzhiyun * If ci is NULL, then the inode doesn't have an encryption key set up
793*4882a593Smuzhiyun * so it's irrelevant. If ci_master_key is NULL, then the master key
794*4882a593Smuzhiyun * was provided via the legacy mechanism of the process-subscribed
795*4882a593Smuzhiyun * keyrings, so we don't know whether it's been removed or not.
796*4882a593Smuzhiyun */
797*4882a593Smuzhiyun if (!ci || !ci->ci_master_key)
798*4882a593Smuzhiyun return 0;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * With proper, non-racy use of FS_IOC_REMOVE_ENCRYPTION_KEY, all inodes
802*4882a593Smuzhiyun * protected by the key were cleaned by sync_filesystem(). But if
803*4882a593Smuzhiyun * userspace is still using the files, inodes can be dirtied between
804*4882a593Smuzhiyun * then and now. We mustn't lose any writes, so skip dirty inodes here.
805*4882a593Smuzhiyun */
806*4882a593Smuzhiyun if (inode->i_state & I_DIRTY_ALL)
807*4882a593Smuzhiyun return 0;
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun /*
810*4882a593Smuzhiyun * Note: since we aren't holding the key semaphore, the result here can
811*4882a593Smuzhiyun * immediately become outdated. But there's no correctness problem with
812*4882a593Smuzhiyun * unnecessarily evicting. Nor is there a correctness problem with not
813*4882a593Smuzhiyun * evicting while iput() is racing with the key being removed, since
814*4882a593Smuzhiyun * then the thread removing the key will either evict the inode itself
815*4882a593Smuzhiyun * or will correctly detect that it wasn't evicted due to the race.
816*4882a593Smuzhiyun */
817*4882a593Smuzhiyun return !is_master_key_secret_present(&ci->ci_master_key->mk_secret);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(fscrypt_drop_inode);
820