fscrypt: refactor key setup code in preparation for v2 policies
[linux-block.git] / fs / crypto / keyinfo.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
0adda907 11#include <keys/user-type.h>
8094c3ce 12#include <linux/hashtable.h>
0adda907 13#include <linux/scatterlist.h>
b7e7cf7a 14#include <crypto/aes.h>
8094c3ce 15#include <crypto/algapi.h>
b7e7cf7a 16#include <crypto/sha.h>
a575784c 17#include <crypto/skcipher.h>
3325bea5 18#include "fscrypt_private.h"
0adda907 19
b7e7cf7a
DW
20static struct crypto_shash *essiv_hash_tfm;
21
3b6df59b 22/* Table of keys referenced by DIRECT_KEY policies */
a828daab
EB
23static DEFINE_HASHTABLE(fscrypt_direct_keys, 6); /* 6 bits = 64 buckets */
24static DEFINE_SPINLOCK(fscrypt_direct_keys_lock);
8094c3ce 25
646b7d4f 26/*
3ec4f2a6
EB
27 * v1 key derivation function. This generates the derived key by encrypting the
28 * master key with AES-128-ECB using the nonce as the AES key. This provides a
29 * unique derived key with sufficient entropy for each inode. However, it's
30 * nonstandard, non-extensible, doesn't evenly distribute the entropy from the
31 * master key, and is trivially reversible: an attacker who compromises a
32 * derived key can "decrypt" it to get back to the master key, then derive any
33 * other key. For all new code, use HKDF instead.
0adda907 34 *
646b7d4f
EB
35 * The master key must be at least as long as the derived key. If the master
36 * key is longer, then only the first 'derived_keysize' bytes are used.
0adda907 37 */
646b7d4f 38static int derive_key_aes(const u8 *master_key,
3ec4f2a6 39 const u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE],
646b7d4f 40 u8 *derived_key, unsigned int derived_keysize)
0adda907
JK
41{
42 int res = 0;
d407574e 43 struct skcipher_request *req = NULL;
d0082e1a 44 DECLARE_CRYPTO_WAIT(wait);
0adda907 45 struct scatterlist src_sg, dst_sg;
d407574e 46 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
0adda907
JK
47
48 if (IS_ERR(tfm)) {
49 res = PTR_ERR(tfm);
50 tfm = NULL;
51 goto out;
52 }
231baecd 53 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
d407574e 54 req = skcipher_request_alloc(tfm, GFP_NOFS);
0adda907
JK
55 if (!req) {
56 res = -ENOMEM;
57 goto out;
58 }
d407574e 59 skcipher_request_set_callback(req,
0adda907 60 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
d0082e1a 61 crypto_req_done, &wait);
3ec4f2a6 62 res = crypto_skcipher_setkey(tfm, nonce, FS_KEY_DERIVATION_NONCE_SIZE);
0adda907
JK
63 if (res < 0)
64 goto out;
65
646b7d4f
EB
66 sg_init_one(&src_sg, master_key, derived_keysize);
67 sg_init_one(&dst_sg, derived_key, derived_keysize);
68 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
b7e7cf7a 69 NULL);
d0082e1a 70 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
0adda907 71out:
d407574e
LT
72 skcipher_request_free(req);
73 crypto_free_skcipher(tfm);
0adda907
JK
74 return res;
75}
76
590f497d
EB
77/*
78 * Search the current task's subscribed keyrings for a "logon" key with
79 * description prefix:descriptor, and if found acquire a read lock on it and
80 * return a pointer to its validated payload in *payload_ret.
81 */
82static struct key *
83find_and_lock_process_key(const char *prefix,
3b6df59b 84 const u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE],
590f497d
EB
85 unsigned int min_keysize,
86 const struct fscrypt_key **payload_ret)
b5a7aef1 87{
a5d431ef 88 char *description;
590f497d 89 struct key *key;
b5a7aef1 90 const struct user_key_payload *ukp;
590f497d 91 const struct fscrypt_key *payload;
b5a7aef1 92
a5d431ef 93 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
3b6df59b 94 FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
a5d431ef 95 if (!description)
590f497d 96 return ERR_PTR(-ENOMEM);
b5a7aef1 97
590f497d 98 key = request_key(&key_type_logon, description, NULL);
a5d431ef 99 kfree(description);
590f497d
EB
100 if (IS_ERR(key))
101 return key;
102
103 down_read(&key->sem);
104 ukp = user_key_payload_locked(key);
105
106 if (!ukp) /* was the key revoked before we acquired its semaphore? */
107 goto invalid;
108
109 payload = (const struct fscrypt_key *)ukp->data;
110
111 if (ukp->datalen != sizeof(struct fscrypt_key) ||
3b6df59b 112 payload->size < 1 || payload->size > FSCRYPT_MAX_KEY_SIZE) {
590f497d
EB
113 fscrypt_warn(NULL,
114 "key with description '%s' has invalid payload",
115 key->description);
116 goto invalid;
d60b5b78 117 }
590f497d 118
646b7d4f 119 if (payload->size < min_keysize) {
590f497d 120 fscrypt_warn(NULL,
646b7d4f 121 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
590f497d
EB
122 key->description, payload->size, min_keysize);
123 goto invalid;
b5a7aef1 124 }
b5a7aef1 125
590f497d
EB
126 *payload_ret = payload;
127 return key;
128
129invalid:
130 up_read(&key->sem);
131 key_put(key);
132 return ERR_PTR(-ENOKEY);
133}
134
8094c3ce 135static struct fscrypt_mode available_modes[] = {
3b6df59b 136 [FSCRYPT_MODE_AES_256_XTS] = {
e1cc40e5
EB
137 .friendly_name = "AES-256-XTS",
138 .cipher_str = "xts(aes)",
139 .keysize = 64,
8094c3ce 140 .ivsize = 16,
e1cc40e5 141 },
3b6df59b 142 [FSCRYPT_MODE_AES_256_CTS] = {
e1cc40e5
EB
143 .friendly_name = "AES-256-CTS-CBC",
144 .cipher_str = "cts(cbc(aes))",
145 .keysize = 32,
8094c3ce 146 .ivsize = 16,
e1cc40e5 147 },
3b6df59b 148 [FSCRYPT_MODE_AES_128_CBC] = {
e1cc40e5
EB
149 .friendly_name = "AES-128-CBC",
150 .cipher_str = "cbc(aes)",
151 .keysize = 16,
8094c3ce
EB
152 .ivsize = 16,
153 .needs_essiv = true,
e1cc40e5 154 },
3b6df59b 155 [FSCRYPT_MODE_AES_128_CTS] = {
e1cc40e5
EB
156 .friendly_name = "AES-128-CTS-CBC",
157 .cipher_str = "cts(cbc(aes))",
158 .keysize = 16,
8094c3ce
EB
159 .ivsize = 16,
160 },
3b6df59b 161 [FSCRYPT_MODE_ADIANTUM] = {
8094c3ce
EB
162 .friendly_name = "Adiantum",
163 .cipher_str = "adiantum(xchacha12,aes)",
164 .keysize = 32,
165 .ivsize = 32,
e1cc40e5 166 },
b7e7cf7a
DW
167};
168
e1cc40e5
EB
169static struct fscrypt_mode *
170select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
8f39850d 171{
b7e7cf7a 172 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
886da8b3
EB
173 fscrypt_warn(inode,
174 "Unsupported encryption modes (contents mode %d, filenames mode %d)",
175 ci->ci_data_mode, ci->ci_filename_mode);
e1cc40e5 176 return ERR_PTR(-EINVAL);
8f39850d
EB
177 }
178
e1cc40e5
EB
179 if (S_ISREG(inode->i_mode))
180 return &available_modes[ci->ci_data_mode];
181
182 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
183 return &available_modes[ci->ci_filename_mode];
8f39850d 184
e1cc40e5
EB
185 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
186 inode->i_ino, (inode->i_mode & S_IFMT));
187 return ERR_PTR(-EINVAL);
8f39850d
EB
188}
189
3ec4f2a6 190/* Create a symmetric cipher object for the given encryption mode and key */
8094c3ce 191static struct crypto_skcipher *
3ec4f2a6
EB
192fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
193 const struct inode *inode)
8094c3ce
EB
194{
195 struct crypto_skcipher *tfm;
196 int err;
197
198 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
199 if (IS_ERR(tfm)) {
29a98c1c 200 if (PTR_ERR(tfm) == -ENOENT) {
a4d14e91
EB
201 fscrypt_warn(inode,
202 "Missing crypto API support for %s (API name: \"%s\")",
203 mode->friendly_name, mode->cipher_str);
29a98c1c
EB
204 return ERR_PTR(-ENOPKG);
205 }
206 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
207 mode->cipher_str, PTR_ERR(tfm));
8094c3ce
EB
208 return tfm;
209 }
210 if (unlikely(!mode->logged_impl_name)) {
211 /*
212 * fscrypt performance can vary greatly depending on which
213 * crypto algorithm implementation is used. Help people debug
214 * performance problems by logging the ->cra_driver_name the
215 * first time a mode is used. Note that multiple threads can
216 * race here, but it doesn't really matter.
217 */
218 mode->logged_impl_name = true;
219 pr_info("fscrypt: %s using implementation \"%s\"\n",
220 mode->friendly_name,
221 crypto_skcipher_alg(tfm)->base.cra_driver_name);
222 }
231baecd 223 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
8094c3ce
EB
224 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
225 if (err)
226 goto err_free_tfm;
227
228 return tfm;
229
230err_free_tfm:
231 crypto_free_skcipher(tfm);
232 return ERR_PTR(err);
233}
234
3b6df59b 235/* Master key referenced by DIRECT_KEY policy */
a828daab
EB
236struct fscrypt_direct_key {
237 struct hlist_node dk_node;
238 refcount_t dk_refcount;
239 const struct fscrypt_mode *dk_mode;
240 struct crypto_skcipher *dk_ctfm;
241 u8 dk_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
242 u8 dk_raw[FSCRYPT_MAX_KEY_SIZE];
8094c3ce
EB
243};
244
a828daab 245static void free_direct_key(struct fscrypt_direct_key *dk)
8094c3ce 246{
a828daab
EB
247 if (dk) {
248 crypto_free_skcipher(dk->dk_ctfm);
249 kzfree(dk);
8094c3ce
EB
250 }
251}
252
a828daab 253static void put_direct_key(struct fscrypt_direct_key *dk)
8094c3ce 254{
a828daab 255 if (!refcount_dec_and_lock(&dk->dk_refcount, &fscrypt_direct_keys_lock))
0adda907 256 return;
a828daab
EB
257 hash_del(&dk->dk_node);
258 spin_unlock(&fscrypt_direct_keys_lock);
0adda907 259
a828daab 260 free_direct_key(dk);
8094c3ce
EB
261}
262
263/*
a828daab
EB
264 * Find/insert the given key into the fscrypt_direct_keys table. If found, it
265 * is returned with elevated refcount, and 'to_insert' is freed if non-NULL. If
266 * not found, 'to_insert' is inserted and returned if it's non-NULL; otherwise
267 * NULL is returned.
8094c3ce 268 */
a828daab
EB
269static struct fscrypt_direct_key *
270find_or_insert_direct_key(struct fscrypt_direct_key *to_insert,
3ec4f2a6 271 const u8 *raw_key, const struct fscrypt_info *ci)
8094c3ce
EB
272{
273 unsigned long hash_key;
a828daab 274 struct fscrypt_direct_key *dk;
8094c3ce
EB
275
276 /*
277 * Careful: to avoid potentially leaking secret key bytes via timing
278 * information, we must key the hash table by descriptor rather than by
279 * raw key, and use crypto_memneq() when comparing raw keys.
280 */
281
3b6df59b 282 BUILD_BUG_ON(sizeof(hash_key) > FSCRYPT_KEY_DESCRIPTOR_SIZE);
8094c3ce
EB
283 memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key));
284
a828daab
EB
285 spin_lock(&fscrypt_direct_keys_lock);
286 hash_for_each_possible(fscrypt_direct_keys, dk, dk_node, hash_key) {
287 if (memcmp(ci->ci_master_key_descriptor, dk->dk_descriptor,
3b6df59b 288 FSCRYPT_KEY_DESCRIPTOR_SIZE) != 0)
8094c3ce 289 continue;
3ec4f2a6 290 if (ci->ci_mode != dk->dk_mode)
8094c3ce 291 continue;
3ec4f2a6 292 if (crypto_memneq(raw_key, dk->dk_raw, ci->ci_mode->keysize))
8094c3ce
EB
293 continue;
294 /* using existing tfm with same (descriptor, mode, raw_key) */
a828daab
EB
295 refcount_inc(&dk->dk_refcount);
296 spin_unlock(&fscrypt_direct_keys_lock);
297 free_direct_key(to_insert);
298 return dk;
8094c3ce
EB
299 }
300 if (to_insert)
a828daab
EB
301 hash_add(fscrypt_direct_keys, &to_insert->dk_node, hash_key);
302 spin_unlock(&fscrypt_direct_keys_lock);
8094c3ce
EB
303 return to_insert;
304}
305
306/* Prepare to encrypt directly using the master key in the given mode */
a828daab 307static struct fscrypt_direct_key *
3ec4f2a6 308fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
8094c3ce 309{
a828daab 310 struct fscrypt_direct_key *dk;
8094c3ce
EB
311 int err;
312
313 /* Is there already a tfm for this key? */
3ec4f2a6 314 dk = find_or_insert_direct_key(NULL, raw_key, ci);
a828daab
EB
315 if (dk)
316 return dk;
8094c3ce
EB
317
318 /* Nope, allocate one. */
a828daab
EB
319 dk = kzalloc(sizeof(*dk), GFP_NOFS);
320 if (!dk)
8094c3ce 321 return ERR_PTR(-ENOMEM);
a828daab 322 refcount_set(&dk->dk_refcount, 1);
3ec4f2a6
EB
323 dk->dk_mode = ci->ci_mode;
324 dk->dk_ctfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key,
325 ci->ci_inode);
a828daab
EB
326 if (IS_ERR(dk->dk_ctfm)) {
327 err = PTR_ERR(dk->dk_ctfm);
328 dk->dk_ctfm = NULL;
329 goto err_free_dk;
8094c3ce 330 }
a828daab 331 memcpy(dk->dk_descriptor, ci->ci_master_key_descriptor,
3b6df59b 332 FSCRYPT_KEY_DESCRIPTOR_SIZE);
3ec4f2a6 333 memcpy(dk->dk_raw, raw_key, ci->ci_mode->keysize);
8094c3ce 334
3ec4f2a6 335 return find_or_insert_direct_key(dk, raw_key, ci);
8094c3ce 336
a828daab
EB
337err_free_dk:
338 free_direct_key(dk);
8094c3ce 339 return ERR_PTR(err);
0adda907
JK
340}
341
b7e7cf7a
DW
342static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
343{
344 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
345
346 /* init hash transform on demand */
347 if (unlikely(!tfm)) {
348 struct crypto_shash *prev_tfm;
349
350 tfm = crypto_alloc_shash("sha256", 0, 0);
351 if (IS_ERR(tfm)) {
29a98c1c 352 if (PTR_ERR(tfm) == -ENOENT) {
a4d14e91
EB
353 fscrypt_warn(NULL,
354 "Missing crypto API support for SHA-256");
29a98c1c
EB
355 return -ENOPKG;
356 }
357 fscrypt_err(NULL,
358 "Error allocating SHA-256 transform: %ld",
359 PTR_ERR(tfm));
b7e7cf7a
DW
360 return PTR_ERR(tfm);
361 }
362 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
363 if (prev_tfm) {
364 crypto_free_shash(tfm);
365 tfm = prev_tfm;
366 }
367 }
368
369 {
370 SHASH_DESC_ON_STACK(desc, tfm);
371 desc->tfm = tfm;
b7e7cf7a
DW
372
373 return crypto_shash_digest(desc, key, keysize, salt);
374 }
375}
376
377static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
378 int keysize)
379{
380 int err;
381 struct crypto_cipher *essiv_tfm;
382 u8 salt[SHA256_DIGEST_SIZE];
383
3ec4f2a6
EB
384 if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
385 return -EINVAL;
386
b7e7cf7a
DW
387 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
388 if (IS_ERR(essiv_tfm))
389 return PTR_ERR(essiv_tfm);
390
391 ci->ci_essiv_tfm = essiv_tfm;
392
393 err = derive_essiv_salt(raw_key, keysize, salt);
394 if (err)
395 goto out;
396
397 /*
398 * Using SHA256 to derive the salt/key will result in AES-256 being
399 * used for IV generation. File contents encryption will still use the
400 * configured keysize (AES-128) nevertheless.
401 */
402 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
403 if (err)
404 goto out;
405
406out:
407 memzero_explicit(salt, sizeof(salt));
408 return err;
409}
410
3ec4f2a6
EB
411/* Given the per-file key, set up the file's crypto transform object(s) */
412static int fscrypt_set_derived_key(struct fscrypt_info *ci,
413 const u8 *derived_key)
8094c3ce 414{
3ec4f2a6 415 struct fscrypt_mode *mode = ci->ci_mode;
8094c3ce
EB
416 struct crypto_skcipher *ctfm;
417 int err;
418
3ec4f2a6
EB
419 ctfm = fscrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
420 if (IS_ERR(ctfm))
421 return PTR_ERR(ctfm);
422
8094c3ce
EB
423 ci->ci_ctfm = ctfm;
424
425 if (mode->needs_essiv) {
3ec4f2a6 426 err = init_essiv_generator(ci, derived_key, mode->keysize);
8094c3ce 427 if (err) {
3ec4f2a6 428 fscrypt_warn(ci->ci_inode,
886da8b3
EB
429 "Error initializing ESSIV generator: %d",
430 err);
8094c3ce
EB
431 return err;
432 }
433 }
434 return 0;
435}
436
3ec4f2a6
EB
437/* v1 policy, DIRECT_KEY: use the master key directly */
438static int setup_v1_file_key_direct(struct fscrypt_info *ci,
439 const u8 *raw_master_key)
440{
441 const struct fscrypt_mode *mode = ci->ci_mode;
442 struct fscrypt_direct_key *dk;
443
444 if (!fscrypt_mode_supports_direct_key(mode)) {
445 fscrypt_warn(ci->ci_inode,
446 "Direct key mode not allowed with %s",
447 mode->friendly_name);
448 return -EINVAL;
449 }
450
451 if (ci->ci_data_mode != ci->ci_filename_mode) {
452 fscrypt_warn(ci->ci_inode,
453 "Direct key mode not allowed with different contents and filenames modes");
454 return -EINVAL;
455 }
456
457 /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */
458 if (WARN_ON(mode->needs_essiv))
459 return -EINVAL;
460
461 dk = fscrypt_get_direct_key(ci, raw_master_key);
462 if (IS_ERR(dk))
463 return PTR_ERR(dk);
464 ci->ci_direct_key = dk;
465 ci->ci_ctfm = dk->dk_ctfm;
466 return 0;
467}
468
469/* v1 policy, !DIRECT_KEY: derive the file's encryption key */
470static int setup_v1_file_key_derived(struct fscrypt_info *ci,
471 const u8 *raw_master_key)
472{
473 u8 *derived_key;
474 int err;
475
476 /*
477 * This cannot be a stack buffer because it will be passed to the
478 * scatterlist crypto API during derive_key_aes().
479 */
480 derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
481 if (!derived_key)
482 return -ENOMEM;
483
484 err = derive_key_aes(raw_master_key, ci->ci_nonce,
485 derived_key, ci->ci_mode->keysize);
486 if (err)
487 goto out;
488
489 err = fscrypt_set_derived_key(ci, derived_key);
490out:
491 kzfree(derived_key);
492 return err;
493}
494
495static int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
496 const u8 *raw_master_key)
497{
498 if (ci->ci_flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY)
499 return setup_v1_file_key_direct(ci, raw_master_key);
500 else
501 return setup_v1_file_key_derived(ci, raw_master_key);
502}
503
504static int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
505 struct fscrypt_info *ci)
506{
507 struct key *key;
508 const struct fscrypt_key *payload;
509 int err;
510
511 key = find_and_lock_process_key(FSCRYPT_KEY_DESC_PREFIX,
512 ci->ci_master_key_descriptor,
513 ci->ci_mode->keysize, &payload);
514 if (key == ERR_PTR(-ENOKEY) && ci->ci_inode->i_sb->s_cop->key_prefix) {
515 key = find_and_lock_process_key(ci->ci_inode->i_sb->s_cop->key_prefix,
516 ci->ci_master_key_descriptor,
517 ci->ci_mode->keysize, &payload);
518 }
519 if (IS_ERR(key))
520 return PTR_ERR(key);
521
522 err = fscrypt_setup_v1_file_key(ci, payload->raw);
523 up_read(&key->sem);
524 key_put(key);
525 return err;
526}
527
528/*
529 * Find the master key, then set up the inode's actual encryption key.
530 */
531static int setup_file_encryption_key(struct fscrypt_info *ci)
532{
533 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
534}
535
8094c3ce
EB
536static void put_crypt_info(struct fscrypt_info *ci)
537{
538 if (!ci)
539 return;
540
a828daab
EB
541 if (ci->ci_direct_key) {
542 put_direct_key(ci->ci_direct_key);
8094c3ce
EB
543 } else {
544 crypto_free_skcipher(ci->ci_ctfm);
545 crypto_free_cipher(ci->ci_essiv_tfm);
546 }
547 kmem_cache_free(fscrypt_info_cachep, ci);
548}
549
1b53cf98 550int fscrypt_get_encryption_info(struct inode *inode)
0adda907 551{
0b81d077 552 struct fscrypt_info *crypt_info;
0b81d077 553 struct fscrypt_context ctx;
e1cc40e5 554 struct fscrypt_mode *mode;
0adda907
JK
555 int res;
556
e37a784d 557 if (fscrypt_has_encryption_key(inode))
1b53cf98
EB
558 return 0;
559
f32d7ac2 560 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
cfc4d971
JK
561 if (res)
562 return res;
0b81d077 563
0b81d077
JK
564 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
565 if (res < 0) {
5bbdcbbb 566 if (!fscrypt_dummy_context_enabled(inode) ||
63f668f0
EB
567 IS_ENCRYPTED(inode)) {
568 fscrypt_warn(inode,
569 "Error %d getting encryption context",
570 res);
0b81d077 571 return res;
63f668f0 572 }
5bbdcbbb
TT
573 /* Fake up a context for an unencrypted directory */
574 memset(&ctx, 0, sizeof(ctx));
8f39850d 575 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
3b6df59b
EB
576 ctx.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
577 ctx.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
578 memset(ctx.master_key_descriptor, 0x42,
579 FSCRYPT_KEY_DESCRIPTOR_SIZE);
0b81d077 580 } else if (res != sizeof(ctx)) {
63f668f0
EB
581 fscrypt_warn(inode,
582 "Unknown encryption context size (%d bytes)", res);
0adda907 583 return -EINVAL;
0b81d077 584 }
8f39850d 585
63f668f0
EB
586 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) {
587 fscrypt_warn(inode, "Unknown encryption context version (%d)",
588 ctx.format);
8f39850d 589 return -EINVAL;
63f668f0 590 }
8f39850d 591
3b6df59b 592 if (ctx.flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
63f668f0
EB
593 fscrypt_warn(inode, "Unknown encryption context flags (0x%02x)",
594 ctx.flags);
8f39850d 595 return -EINVAL;
63f668f0 596 }
0adda907 597
8094c3ce 598 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
0adda907
JK
599 if (!crypt_info)
600 return -ENOMEM;
601
59dc6a8e
EB
602 crypt_info->ci_inode = inode;
603
0adda907
JK
604 crypt_info->ci_flags = ctx.flags;
605 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
606 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
8094c3ce 607 memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor,
3b6df59b 608 FSCRYPT_KEY_DESCRIPTOR_SIZE);
8094c3ce 609 memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
640778fb 610
e1cc40e5
EB
611 mode = select_encryption_mode(crypt_info, inode);
612 if (IS_ERR(mode)) {
613 res = PTR_ERR(mode);
26bf3dc7 614 goto out;
e1cc40e5 615 }
8094c3ce
EB
616 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
617 crypt_info->ci_mode = mode;
8f39850d 618
3ec4f2a6 619 res = setup_file_encryption_key(crypt_info);
26bf3dc7
JK
620 if (res)
621 goto out;
622
e37a784d 623 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL)
1b53cf98 624 crypt_info = NULL;
26bf3dc7 625out:
0b81d077 626 if (res == -ENOKEY)
26bf3dc7 627 res = 0;
0b81d077 628 put_crypt_info(crypt_info);
0adda907
JK
629 return res;
630}
1b53cf98 631EXPORT_SYMBOL(fscrypt_get_encryption_info);
0adda907 632
2c58d548
EB
633/**
634 * fscrypt_put_encryption_info - free most of an inode's fscrypt data
635 *
636 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
637 * being evicted. An RCU grace period need not have elapsed yet.
638 */
3d204e24 639void fscrypt_put_encryption_info(struct inode *inode)
0adda907 640{
3d204e24
EB
641 put_crypt_info(inode->i_crypt_info);
642 inode->i_crypt_info = NULL;
0b81d077
JK
643}
644EXPORT_SYMBOL(fscrypt_put_encryption_info);
2c58d548
EB
645
646/**
647 * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
648 *
649 * Free the inode's cached decrypted symlink target, if any. Filesystems must
650 * call this after an RCU grace period, just before they free the inode.
651 */
652void fscrypt_free_inode(struct inode *inode)
653{
654 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
655 kfree(inode->i_link);
656 inode->i_link = NULL;
657 }
658}
659EXPORT_SYMBOL(fscrypt_free_inode);