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