blob: 74d61d827d91399149317530f7945454c58113b6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002/*
Eric Biggers3ec4f2a62019-08-04 19:35:45 -07003 * Key setup facility for FS encryption support.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07004 *
5 * Copyright (C) 2015, Google, Inc.
6 *
Eric Biggers3ec4f2a62019-08-04 19:35:45 -07007 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07009 */
10
Eric Biggersa5757842018-01-05 10:45:00 -080011#include <crypto/skcipher.h>
Eric Biggers0109ce762019-08-04 19:35:45 -070012#include <linux/key.h>
13
Theodore Ts'o3325bea2016-11-26 20:32:46 -050014#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070015
Eric Biggers85af90e2019-12-09 13:18:27 -080016struct fscrypt_mode fscrypt_modes[] = {
Eric Biggers3b6df592019-08-04 19:35:44 -070017 [FSCRYPT_MODE_AES_256_XTS] = {
Eric Biggerse1cc40e2018-05-18 10:58:14 -070018 .friendly_name = "AES-256-XTS",
19 .cipher_str = "xts(aes)",
20 .keysize = 64,
Eric Biggers8094c3c2019-01-06 08:36:21 -050021 .ivsize = 16,
Eric Biggerse1cc40e2018-05-18 10:58:14 -070022 },
Eric Biggers3b6df592019-08-04 19:35:44 -070023 [FSCRYPT_MODE_AES_256_CTS] = {
Eric Biggerse1cc40e2018-05-18 10:58:14 -070024 .friendly_name = "AES-256-CTS-CBC",
25 .cipher_str = "cts(cbc(aes))",
26 .keysize = 32,
Eric Biggers8094c3c2019-01-06 08:36:21 -050027 .ivsize = 16,
Eric Biggerse1cc40e2018-05-18 10:58:14 -070028 },
Eric Biggers3b6df592019-08-04 19:35:44 -070029 [FSCRYPT_MODE_AES_128_CBC] = {
Eric Biggers4006d792019-10-09 16:34:16 -070030 .friendly_name = "AES-128-CBC-ESSIV",
31 .cipher_str = "essiv(cbc(aes),sha256)",
Eric Biggerse1cc40e2018-05-18 10:58:14 -070032 .keysize = 16,
Eric Biggers8094c3c2019-01-06 08:36:21 -050033 .ivsize = 16,
Eric Biggerse1cc40e2018-05-18 10:58:14 -070034 },
Eric Biggers3b6df592019-08-04 19:35:44 -070035 [FSCRYPT_MODE_AES_128_CTS] = {
Eric Biggerse1cc40e2018-05-18 10:58:14 -070036 .friendly_name = "AES-128-CTS-CBC",
37 .cipher_str = "cts(cbc(aes))",
38 .keysize = 16,
Eric Biggers8094c3c2019-01-06 08:36:21 -050039 .ivsize = 16,
40 },
Eric Biggers3b6df592019-08-04 19:35:44 -070041 [FSCRYPT_MODE_ADIANTUM] = {
Eric Biggers8094c3c2019-01-06 08:36:21 -050042 .friendly_name = "Adiantum",
43 .cipher_str = "adiantum(xchacha12,aes)",
44 .keysize = 32,
45 .ivsize = 32,
Eric Biggerse1cc40e2018-05-18 10:58:14 -070046 },
Daniel Walterb7e7cf72017-06-19 09:27:58 +020047};
48
Eric Biggerse1cc40e2018-05-18 10:58:14 -070049static struct fscrypt_mode *
Eric Biggers5dae4602019-08-04 19:35:47 -070050select_encryption_mode(const union fscrypt_policy *policy,
51 const struct inode *inode)
Eric Biggers8f398502016-09-15 13:32:11 -040052{
Eric Biggerse1cc40e2018-05-18 10:58:14 -070053 if (S_ISREG(inode->i_mode))
Eric Biggers85af90e2019-12-09 13:18:27 -080054 return &fscrypt_modes[fscrypt_policy_contents_mode(policy)];
Eric Biggers8f398502016-09-15 13:32:11 -040055
Eric Biggerse1cc40e2018-05-18 10:58:14 -070056 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Eric Biggers85af90e2019-12-09 13:18:27 -080057 return &fscrypt_modes[fscrypt_policy_fnames_mode(policy)];
Eric Biggerse1cc40e2018-05-18 10:58:14 -070058
59 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
60 inode->i_ino, (inode->i_mode & S_IFMT));
61 return ERR_PTR(-EINVAL);
Eric Biggers8f398502016-09-15 13:32:11 -040062}
63
Eric Biggers3ec4f2a62019-08-04 19:35:45 -070064/* Create a symmetric cipher object for the given encryption mode and key */
Eric Biggers0109ce762019-08-04 19:35:45 -070065struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
66 const u8 *raw_key,
67 const struct inode *inode)
Eric Biggers8094c3c2019-01-06 08:36:21 -050068{
69 struct crypto_skcipher *tfm;
70 int err;
71
72 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
73 if (IS_ERR(tfm)) {
Eric Biggers29a98c12019-07-24 11:08:00 -070074 if (PTR_ERR(tfm) == -ENOENT) {
Eric Biggersa4d14e92019-07-24 11:07:59 -070075 fscrypt_warn(inode,
76 "Missing crypto API support for %s (API name: \"%s\")",
77 mode->friendly_name, mode->cipher_str);
Eric Biggers29a98c12019-07-24 11:08:00 -070078 return ERR_PTR(-ENOPKG);
79 }
80 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
81 mode->cipher_str, PTR_ERR(tfm));
Eric Biggers8094c3c2019-01-06 08:36:21 -050082 return tfm;
83 }
Eric Biggersff73c2c2019-10-21 13:49:03 -070084 if (!xchg(&mode->logged_impl_name, 1)) {
Eric Biggers8094c3c2019-01-06 08:36:21 -050085 /*
86 * fscrypt performance can vary greatly depending on which
87 * crypto algorithm implementation is used. Help people debug
88 * performance problems by logging the ->cra_driver_name the
Eric Biggersff73c2c2019-10-21 13:49:03 -070089 * first time a mode is used.
Eric Biggers8094c3c2019-01-06 08:36:21 -050090 */
Eric Biggers8094c3c2019-01-06 08:36:21 -050091 pr_info("fscrypt: %s using implementation \"%s\"\n",
Eric Biggers6e1adb82019-12-09 12:38:10 -080092 mode->friendly_name, crypto_skcipher_driver_name(tfm));
Eric Biggers8094c3c2019-01-06 08:36:21 -050093 }
Eric Biggersc64cfb92019-12-09 12:39:18 -080094 if (WARN_ON(crypto_skcipher_ivsize(tfm) != mode->ivsize)) {
95 err = -EINVAL;
96 goto err_free_tfm;
97 }
Eric Biggers231baec2019-01-18 22:48:00 -080098 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
Eric Biggers8094c3c2019-01-06 08:36:21 -050099 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
100 if (err)
101 goto err_free_tfm;
102
103 return tfm;
104
105err_free_tfm:
106 crypto_free_skcipher(tfm);
107 return ERR_PTR(err);
108}
109
Eric Biggers4006d792019-10-09 16:34:16 -0700110/* Given the per-file key, set up the file's crypto transform object */
Eric Biggers0109ce762019-08-04 19:35:45 -0700111int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
Eric Biggers8094c3c2019-01-06 08:36:21 -0500112{
Eric Biggers4006d792019-10-09 16:34:16 -0700113 struct crypto_skcipher *tfm;
Eric Biggers8094c3c2019-01-06 08:36:21 -0500114
Eric Biggers4006d792019-10-09 16:34:16 -0700115 tfm = fscrypt_allocate_skcipher(ci->ci_mode, derived_key, ci->ci_inode);
116 if (IS_ERR(tfm))
117 return PTR_ERR(tfm);
Eric Biggers3ec4f2a62019-08-04 19:35:45 -0700118
Eric Biggers4006d792019-10-09 16:34:16 -0700119 ci->ci_ctfm = tfm;
Eric Biggersb103fb72019-10-24 14:54:36 -0700120 ci->ci_owns_key = true;
Eric Biggers8094c3c2019-01-06 08:36:21 -0500121 return 0;
122}
123
Eric Biggers5dae4602019-08-04 19:35:47 -0700124static int setup_per_mode_key(struct fscrypt_info *ci,
Eric Biggersb103fb72019-10-24 14:54:36 -0700125 struct fscrypt_master_key *mk,
126 struct crypto_skcipher **tfms,
127 u8 hkdf_context, bool include_fs_uuid)
Eric Biggers5dae4602019-08-04 19:35:47 -0700128{
Eric Biggersb103fb72019-10-24 14:54:36 -0700129 const struct inode *inode = ci->ci_inode;
130 const struct super_block *sb = inode->i_sb;
Eric Biggers5dae4602019-08-04 19:35:47 -0700131 struct fscrypt_mode *mode = ci->ci_mode;
Eric Biggers85af90e2019-12-09 13:18:27 -0800132 const u8 mode_num = mode - fscrypt_modes;
Eric Biggers5dae4602019-08-04 19:35:47 -0700133 struct crypto_skcipher *tfm, *prev_tfm;
134 u8 mode_key[FSCRYPT_MAX_KEY_SIZE];
Eric Biggersb103fb72019-10-24 14:54:36 -0700135 u8 hkdf_info[sizeof(mode_num) + sizeof(sb->s_uuid)];
136 unsigned int hkdf_infolen = 0;
Eric Biggers5dae4602019-08-04 19:35:47 -0700137 int err;
138
Eric Biggersb103fb72019-10-24 14:54:36 -0700139 if (WARN_ON(mode_num > __FSCRYPT_MODE_MAX))
Eric Biggers5dae4602019-08-04 19:35:47 -0700140 return -EINVAL;
141
142 /* pairs with cmpxchg() below */
Eric Biggersb103fb72019-10-24 14:54:36 -0700143 tfm = READ_ONCE(tfms[mode_num]);
Eric Biggers5dae4602019-08-04 19:35:47 -0700144 if (likely(tfm != NULL))
145 goto done;
146
147 BUILD_BUG_ON(sizeof(mode_num) != 1);
Eric Biggersb103fb72019-10-24 14:54:36 -0700148 BUILD_BUG_ON(sizeof(sb->s_uuid) != 16);
149 BUILD_BUG_ON(sizeof(hkdf_info) != 17);
150 hkdf_info[hkdf_infolen++] = mode_num;
151 if (include_fs_uuid) {
152 memcpy(&hkdf_info[hkdf_infolen], &sb->s_uuid,
153 sizeof(sb->s_uuid));
154 hkdf_infolen += sizeof(sb->s_uuid);
155 }
Eric Biggers5dae4602019-08-04 19:35:47 -0700156 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
Eric Biggersb103fb72019-10-24 14:54:36 -0700157 hkdf_context, hkdf_info, hkdf_infolen,
Eric Biggers5dae4602019-08-04 19:35:47 -0700158 mode_key, mode->keysize);
159 if (err)
160 return err;
Eric Biggersb103fb72019-10-24 14:54:36 -0700161 tfm = fscrypt_allocate_skcipher(mode, mode_key, inode);
Eric Biggers5dae4602019-08-04 19:35:47 -0700162 memzero_explicit(mode_key, mode->keysize);
163 if (IS_ERR(tfm))
164 return PTR_ERR(tfm);
165
166 /* pairs with READ_ONCE() above */
Eric Biggersb103fb72019-10-24 14:54:36 -0700167 prev_tfm = cmpxchg(&tfms[mode_num], NULL, tfm);
Eric Biggers5dae4602019-08-04 19:35:47 -0700168 if (prev_tfm != NULL) {
169 crypto_free_skcipher(tfm);
170 tfm = prev_tfm;
171 }
172done:
173 ci->ci_ctfm = tfm;
174 return 0;
175}
176
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800177int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
178 const struct fscrypt_master_key *mk)
179{
180 int err;
181
182 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf, HKDF_CONTEXT_DIRHASH_KEY,
183 ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE,
184 (u8 *)&ci->ci_dirhash_key,
185 sizeof(ci->ci_dirhash_key));
186 if (err)
187 return err;
188 ci->ci_dirhash_key_initialized = true;
189 return 0;
190}
191
Eric Biggers5dae4602019-08-04 19:35:47 -0700192static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
193 struct fscrypt_master_key *mk)
194{
Eric Biggers5dae4602019-08-04 19:35:47 -0700195 int err;
196
197 if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
198 /*
199 * DIRECT_KEY: instead of deriving per-file keys, the per-file
200 * nonce will be included in all the IVs. But unlike v1
201 * policies, for v2 policies in this case we don't encrypt with
202 * the master key directly but rather derive a per-mode key.
203 * This ensures that the master key is consistently used only
204 * for HKDF, avoiding key reuse issues.
205 */
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800206 err = setup_per_mode_key(ci, mk, mk->mk_direct_tfms,
207 HKDF_CONTEXT_DIRECT_KEY, false);
Eric Biggersb103fb72019-10-24 14:54:36 -0700208 } else if (ci->ci_policy.v2.flags &
209 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
210 /*
211 * IV_INO_LBLK_64: encryption keys are derived from (master_key,
212 * mode_num, filesystem_uuid), and inode number is included in
213 * the IVs. This format is optimized for use with inline
214 * encryption hardware compliant with the UFS or eMMC standards.
215 */
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800216 err = setup_per_mode_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
217 HKDF_CONTEXT_IV_INO_LBLK_64_KEY, true);
218 } else {
219 u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
Eric Biggers5dae4602019-08-04 19:35:47 -0700220
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800221 err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
222 HKDF_CONTEXT_PER_FILE_KEY,
223 ci->ci_nonce,
224 FS_KEY_DERIVATION_NONCE_SIZE,
225 derived_key, ci->ci_mode->keysize);
226 if (err)
227 return err;
228
229 err = fscrypt_set_derived_key(ci, derived_key);
230 memzero_explicit(derived_key, ci->ci_mode->keysize);
231 }
Eric Biggers5dae4602019-08-04 19:35:47 -0700232 if (err)
233 return err;
234
Daniel Rosenbergaa408f82020-01-20 14:31:57 -0800235 /* Derive a secret dirhash key for directories that need it. */
236 if (S_ISDIR(ci->ci_inode->i_mode) && IS_CASEFOLDED(ci->ci_inode)) {
237 err = fscrypt_derive_dirhash_key(ci, mk);
238 if (err)
239 return err;
240 }
241
242 return 0;
Eric Biggers5dae4602019-08-04 19:35:47 -0700243}
244
Eric Biggers3ec4f2a62019-08-04 19:35:45 -0700245/*
246 * Find the master key, then set up the inode's actual encryption key.
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700247 *
248 * If the master key is found in the filesystem-level keyring, then the
249 * corresponding 'struct key' is returned in *master_key_ret with
Eric Biggers23c688b2019-08-04 19:35:47 -0700250 * ->mk_secret_sem read-locked. This is needed to ensure that only one task
251 * links the fscrypt_info into ->mk_decrypted_inodes (as multiple tasks may race
252 * to create an fscrypt_info for the same inode), and to synchronize the master
253 * key being removed with a new inode starting to use it.
Eric Biggers3ec4f2a62019-08-04 19:35:45 -0700254 */
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700255static int setup_file_encryption_key(struct fscrypt_info *ci,
256 struct key **master_key_ret)
Eric Biggers3ec4f2a62019-08-04 19:35:45 -0700257{
Eric Biggers22d94f42019-08-04 19:35:46 -0700258 struct key *key;
259 struct fscrypt_master_key *mk = NULL;
260 struct fscrypt_key_specifier mk_spec;
261 int err;
262
Eric Biggers5dae4602019-08-04 19:35:47 -0700263 switch (ci->ci_policy.version) {
264 case FSCRYPT_POLICY_V1:
265 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
266 memcpy(mk_spec.u.descriptor,
267 ci->ci_policy.v1.master_key_descriptor,
268 FSCRYPT_KEY_DESCRIPTOR_SIZE);
269 break;
270 case FSCRYPT_POLICY_V2:
271 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
272 memcpy(mk_spec.u.identifier,
273 ci->ci_policy.v2.master_key_identifier,
274 FSCRYPT_KEY_IDENTIFIER_SIZE);
275 break;
276 default:
277 WARN_ON(1);
278 return -EINVAL;
279 }
Eric Biggers22d94f42019-08-04 19:35:46 -0700280
281 key = fscrypt_find_master_key(ci->ci_inode->i_sb, &mk_spec);
282 if (IS_ERR(key)) {
Eric Biggers5dae4602019-08-04 19:35:47 -0700283 if (key != ERR_PTR(-ENOKEY) ||
284 ci->ci_policy.version != FSCRYPT_POLICY_V1)
Eric Biggers22d94f42019-08-04 19:35:46 -0700285 return PTR_ERR(key);
286
Eric Biggers5dae4602019-08-04 19:35:47 -0700287 /*
288 * As a legacy fallback for v1 policies, search for the key in
289 * the current task's subscribed keyrings too. Don't move this
290 * to before the search of ->s_master_keys, since users
291 * shouldn't be able to override filesystem-level keys.
292 */
Eric Biggers22d94f42019-08-04 19:35:46 -0700293 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
294 }
295
296 mk = key->payload.data[0];
Eric Biggers23c688b2019-08-04 19:35:47 -0700297 down_read(&mk->mk_secret_sem);
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700298
299 /* Has the secret been removed (via FS_IOC_REMOVE_ENCRYPTION_KEY)? */
300 if (!is_master_key_secret_present(&mk->mk_secret)) {
301 err = -ENOKEY;
302 goto out_release_key;
303 }
Eric Biggers22d94f42019-08-04 19:35:46 -0700304
Eric Biggers5dae4602019-08-04 19:35:47 -0700305 /*
306 * Require that the master key be at least as long as the derived key.
307 * Otherwise, the derived key cannot possibly contain as much entropy as
308 * that required by the encryption mode it will be used for. For v1
309 * policies it's also required for the KDF to work at all.
310 */
Eric Biggers22d94f42019-08-04 19:35:46 -0700311 if (mk->mk_secret.size < ci->ci_mode->keysize) {
312 fscrypt_warn(NULL,
313 "key with %s %*phN is too short (got %u bytes, need %u+ bytes)",
314 master_key_spec_type(&mk_spec),
315 master_key_spec_len(&mk_spec), (u8 *)&mk_spec.u,
316 mk->mk_secret.size, ci->ci_mode->keysize);
317 err = -ENOKEY;
318 goto out_release_key;
319 }
320
Eric Biggers5dae4602019-08-04 19:35:47 -0700321 switch (ci->ci_policy.version) {
322 case FSCRYPT_POLICY_V1:
323 err = fscrypt_setup_v1_file_key(ci, mk->mk_secret.raw);
324 break;
325 case FSCRYPT_POLICY_V2:
326 err = fscrypt_setup_v2_file_key(ci, mk);
327 break;
328 default:
329 WARN_ON(1);
330 err = -EINVAL;
331 break;
332 }
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700333 if (err)
334 goto out_release_key;
335
336 *master_key_ret = key;
337 return 0;
Eric Biggers22d94f42019-08-04 19:35:46 -0700338
339out_release_key:
Eric Biggers23c688b2019-08-04 19:35:47 -0700340 up_read(&mk->mk_secret_sem);
Eric Biggers22d94f42019-08-04 19:35:46 -0700341 key_put(key);
342 return err;
Eric Biggers3ec4f2a62019-08-04 19:35:45 -0700343}
344
Eric Biggers8094c3c2019-01-06 08:36:21 -0500345static void put_crypt_info(struct fscrypt_info *ci)
346{
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700347 struct key *key;
348
Eric Biggers8094c3c2019-01-06 08:36:21 -0500349 if (!ci)
350 return;
351
Eric Biggers4006d792019-10-09 16:34:16 -0700352 if (ci->ci_direct_key)
Eric Biggers0109ce762019-08-04 19:35:45 -0700353 fscrypt_put_direct_key(ci->ci_direct_key);
Eric Biggersb103fb72019-10-24 14:54:36 -0700354 else if (ci->ci_owns_key)
Eric Biggers8094c3c2019-01-06 08:36:21 -0500355 crypto_free_skcipher(ci->ci_ctfm);
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700356
357 key = ci->ci_master_key;
358 if (key) {
359 struct fscrypt_master_key *mk = key->payload.data[0];
360
361 /*
362 * Remove this inode from the list of inodes that were unlocked
363 * with the master key.
364 *
365 * In addition, if we're removing the last inode from a key that
366 * already had its secret removed, invalidate the key so that it
367 * gets removed from ->s_master_keys.
368 */
369 spin_lock(&mk->mk_decrypted_inodes_lock);
370 list_del(&ci->ci_master_key_link);
371 spin_unlock(&mk->mk_decrypted_inodes_lock);
372 if (refcount_dec_and_test(&mk->mk_refcount))
373 key_invalidate(key);
374 key_put(key);
375 }
Eric Biggers6f997562019-10-09 16:34:17 -0700376 memzero_explicit(ci, sizeof(*ci));
Eric Biggers8094c3c2019-01-06 08:36:21 -0500377 kmem_cache_free(fscrypt_info_cachep, ci);
378}
379
Eric Biggers1b53cf92017-02-21 15:07:11 -0800380int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700381{
382 struct fscrypt_info *crypt_info;
Eric Biggers5dae4602019-08-04 19:35:47 -0700383 union fscrypt_context ctx;
Eric Biggerse1cc40e2018-05-18 10:58:14 -0700384 struct fscrypt_mode *mode;
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700385 struct key *master_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700386 int res;
387
Eric Biggerse37a7842019-04-11 14:32:15 -0700388 if (fscrypt_has_encryption_key(inode))
Eric Biggers1b53cf92017-02-21 15:07:11 -0800389 return 0;
390
David Gstirf32d7ac2016-12-06 23:53:57 +0100391 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700392 if (res)
393 return res;
394
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700395 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
396 if (res < 0) {
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500397 if (!fscrypt_dummy_context_enabled(inode) ||
Eric Biggers63f668f2019-07-24 11:07:59 -0700398 IS_ENCRYPTED(inode)) {
399 fscrypt_warn(inode,
400 "Error %d getting encryption context",
401 res);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700402 return res;
Eric Biggers63f668f2019-07-24 11:07:59 -0700403 }
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500404 /* Fake up a context for an unencrypted directory */
405 memset(&ctx, 0, sizeof(ctx));
Eric Biggers5dae4602019-08-04 19:35:47 -0700406 ctx.version = FSCRYPT_CONTEXT_V1;
407 ctx.v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
408 ctx.v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
409 memset(ctx.v1.master_key_descriptor, 0x42,
Eric Biggers3b6df592019-08-04 19:35:44 -0700410 FSCRYPT_KEY_DESCRIPTOR_SIZE);
Eric Biggers5dae4602019-08-04 19:35:47 -0700411 res = sizeof(ctx.v1);
Eric Biggers63f668f2019-07-24 11:07:59 -0700412 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700413
Eric Biggers8094c3c2019-01-06 08:36:21 -0500414 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700415 if (!crypt_info)
416 return -ENOMEM;
417
Eric Biggers59dc6a82019-08-04 19:35:44 -0700418 crypt_info->ci_inode = inode;
419
Eric Biggers5dae4602019-08-04 19:35:47 -0700420 res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
421 if (res) {
422 fscrypt_warn(inode,
423 "Unrecognized or corrupt encryption context");
424 goto out;
425 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700426
Eric Biggers5dae4602019-08-04 19:35:47 -0700427 switch (ctx.version) {
428 case FSCRYPT_CONTEXT_V1:
429 memcpy(crypt_info->ci_nonce, ctx.v1.nonce,
430 FS_KEY_DERIVATION_NONCE_SIZE);
431 break;
432 case FSCRYPT_CONTEXT_V2:
433 memcpy(crypt_info->ci_nonce, ctx.v2.nonce,
434 FS_KEY_DERIVATION_NONCE_SIZE);
435 break;
436 default:
437 WARN_ON(1);
438 res = -EINVAL;
439 goto out;
440 }
441
442 if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
443 res = -EINVAL;
444 goto out;
445 }
446
447 mode = select_encryption_mode(&crypt_info->ci_policy, inode);
Eric Biggerse1cc40e2018-05-18 10:58:14 -0700448 if (IS_ERR(mode)) {
449 res = PTR_ERR(mode);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700450 goto out;
Eric Biggerse1cc40e2018-05-18 10:58:14 -0700451 }
Eric Biggers8094c3c2019-01-06 08:36:21 -0500452 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
453 crypt_info->ci_mode = mode;
Eric Biggers8f398502016-09-15 13:32:11 -0400454
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700455 res = setup_file_encryption_key(crypt_info, &master_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700456 if (res)
457 goto out;
458
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700459 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL) {
460 if (master_key) {
461 struct fscrypt_master_key *mk =
462 master_key->payload.data[0];
463
464 refcount_inc(&mk->mk_refcount);
465 crypt_info->ci_master_key = key_get(master_key);
466 spin_lock(&mk->mk_decrypted_inodes_lock);
467 list_add(&crypt_info->ci_master_key_link,
468 &mk->mk_decrypted_inodes);
469 spin_unlock(&mk->mk_decrypted_inodes_lock);
470 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800471 crypt_info = NULL;
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700472 }
473 res = 0;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700474out:
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700475 if (master_key) {
Eric Biggers23c688b2019-08-04 19:35:47 -0700476 struct fscrypt_master_key *mk = master_key->payload.data[0];
477
478 up_read(&mk->mk_secret_sem);
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700479 key_put(master_key);
480 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700481 if (res == -ENOKEY)
482 res = 0;
483 put_crypt_info(crypt_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700484 return res;
485}
Eric Biggers1b53cf92017-02-21 15:07:11 -0800486EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700487
Eric Biggers2c58d542019-04-10 13:21:15 -0700488/**
489 * fscrypt_put_encryption_info - free most of an inode's fscrypt data
490 *
491 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
492 * being evicted. An RCU grace period need not have elapsed yet.
493 */
Eric Biggers3d204e22018-01-11 23:30:13 -0500494void fscrypt_put_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700495{
Eric Biggers3d204e22018-01-11 23:30:13 -0500496 put_crypt_info(inode->i_crypt_info);
497 inode->i_crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700498}
499EXPORT_SYMBOL(fscrypt_put_encryption_info);
Eric Biggers2c58d542019-04-10 13:21:15 -0700500
501/**
502 * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
503 *
504 * Free the inode's cached decrypted symlink target, if any. Filesystems must
505 * call this after an RCU grace period, just before they free the inode.
506 */
507void fscrypt_free_inode(struct inode *inode)
508{
509 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
510 kfree(inode->i_link);
511 inode->i_link = NULL;
512 }
513}
514EXPORT_SYMBOL(fscrypt_free_inode);
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700515
516/**
517 * fscrypt_drop_inode - check whether the inode's master key has been removed
518 *
519 * Filesystems supporting fscrypt must call this from their ->drop_inode()
520 * method so that encrypted inodes are evicted as soon as they're no longer in
521 * use and their master key has been removed.
522 *
523 * Return: 1 if fscrypt wants the inode to be evicted now, otherwise 0
524 */
525int fscrypt_drop_inode(struct inode *inode)
526{
527 const struct fscrypt_info *ci = READ_ONCE(inode->i_crypt_info);
528 const struct fscrypt_master_key *mk;
529
530 /*
531 * If ci is NULL, then the inode doesn't have an encryption key set up
532 * so it's irrelevant. If ci_master_key is NULL, then the master key
533 * was provided via the legacy mechanism of the process-subscribed
534 * keyrings, so we don't know whether it's been removed or not.
535 */
536 if (!ci || !ci->ci_master_key)
537 return 0;
538 mk = ci->ci_master_key->payload.data[0];
539
540 /*
Eric Biggers23c688b2019-08-04 19:35:47 -0700541 * Note: since we aren't holding ->mk_secret_sem, the result here can
Eric Biggersb1c0ec32019-08-04 19:35:46 -0700542 * immediately become outdated. But there's no correctness problem with
543 * unnecessarily evicting. Nor is there a correctness problem with not
544 * evicting while iput() is racing with the key being removed, since
545 * then the thread removing the key will either evict the inode itself
546 * or will correctly detect that it wasn't evicted due to the race.
547 */
548 return !is_master_key_secret_present(&mk->mk_secret);
549}
550EXPORT_SYMBOL_GPL(fscrypt_drop_inode);