blob: 566713426fa097eeced3f0b45f37e80368a32b37 [file] [log] [blame]
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001/*
2 * key management facility for FS encryption support.
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains encryption key functions.
7 *
8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
9 */
10
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070011#include <keys/user-type.h>
Eric Biggerse33fa312018-11-26 11:27:37 -080012#include <linux/hashtable.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070013#include <linux/scatterlist.h>
Hyojun Kim63da4202017-10-06 17:10:08 -070014#include <linux/ratelimit.h>
15#include <crypto/aes.h>
Eric Biggerse33fa312018-11-26 11:27:37 -080016#include <crypto/algapi.h>
Hyojun Kim63da4202017-10-06 17:10:08 -070017#include <crypto/sha.h>
Jaegeuk Kimd9197652018-01-05 10:44:52 -080018#include <crypto/skcipher.h>
Hyojun Kim63da4202017-10-06 17:10:08 -070019#include "fscrypt_private.h"
Neeraj Soni36c65122018-04-18 21:04:46 +053020#include "fscrypt_ice.h"
Hyojun Kim63da4202017-10-06 17:10:08 -070021
22static struct crypto_shash *essiv_hash_tfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070023
Eric Biggerse33fa312018-11-26 11:27:37 -080024/* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */
25static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */
26static DEFINE_SPINLOCK(fscrypt_master_keys_lock);
27
Eric Biggers08ac7222018-04-30 15:51:49 -070028/*
29 * Key derivation function. This generates the derived key by encrypting the
30 * master key with AES-128-ECB using the inode's nonce as the AES key.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070031 *
Eric Biggers08ac7222018-04-30 15:51:49 -070032 * The master key must be at least as long as the derived key. If the master
33 * key is longer, then only the first 'derived_keysize' bytes are used.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070034 */
Eric Biggers08ac7222018-04-30 15:51:49 -070035static int derive_key_aes(const u8 *master_key,
36 const struct fscrypt_context *ctx,
37 u8 *derived_key, unsigned int derived_keysize)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070038{
39 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070040 struct skcipher_request *req = NULL;
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070041 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070042 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070043 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070044
45 if (IS_ERR(tfm)) {
46 res = PTR_ERR(tfm);
47 tfm = NULL;
48 goto out;
49 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070050 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
51 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070052 if (!req) {
53 res = -ENOMEM;
54 goto out;
55 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070056 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070057 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070058 crypto_req_done, &wait);
Eric Biggers08ac7222018-04-30 15:51:49 -070059 res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070060 if (res < 0)
61 goto out;
62
Eric Biggers08ac7222018-04-30 15:51:49 -070063 sg_init_one(&src_sg, master_key, derived_keysize);
64 sg_init_one(&dst_sg, derived_key, derived_keysize);
65 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
Hyojun Kim63da4202017-10-06 17:10:08 -070066 NULL);
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070067 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070068out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070069 skcipher_request_free(req);
70 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070071 return res;
72}
73
Eric Biggers24cc7a82018-04-30 15:51:48 -070074/*
75 * Search the current task's subscribed keyrings for a "logon" key with
76 * description prefix:descriptor, and if found acquire a read lock on it and
77 * return a pointer to its validated payload in *payload_ret.
78 */
79static struct key *
80find_and_lock_process_key(const char *prefix,
81 const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
82 unsigned int min_keysize,
83 const struct fscrypt_key **payload_ret)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070084{
Hyojun Kim63da4202017-10-06 17:10:08 -070085 char *description;
Eric Biggers24cc7a82018-04-30 15:51:48 -070086 struct key *key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070087 const struct user_key_payload *ukp;
Eric Biggers24cc7a82018-04-30 15:51:48 -070088 const struct fscrypt_key *payload;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070089
Hyojun Kim63da4202017-10-06 17:10:08 -070090 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
Eric Biggers24cc7a82018-04-30 15:51:48 -070091 FS_KEY_DESCRIPTOR_SIZE, descriptor);
Hyojun Kim63da4202017-10-06 17:10:08 -070092 if (!description)
Eric Biggers24cc7a82018-04-30 15:51:48 -070093 return ERR_PTR(-ENOMEM);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070094
Eric Biggers24cc7a82018-04-30 15:51:48 -070095 key = request_key(&key_type_logon, description, NULL);
Hyojun Kim63da4202017-10-06 17:10:08 -070096 kfree(description);
Eric Biggers24cc7a82018-04-30 15:51:48 -070097 if (IS_ERR(key))
98 return key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070099
Eric Biggers24cc7a82018-04-30 15:51:48 -0700100 down_read(&key->sem);
101 ukp = user_key_payload_locked(key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700102
Eric Biggers24cc7a82018-04-30 15:51:48 -0700103 if (!ukp) /* was the key revoked before we acquired its semaphore? */
104 goto invalid;
Neeraj Soni36c65122018-04-18 21:04:46 +0530105
Eric Biggers24cc7a82018-04-30 15:51:48 -0700106 payload = (const struct fscrypt_key *)ukp->data;
107
108 if (ukp->datalen != sizeof(struct fscrypt_key) ||
109 payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
110 fscrypt_warn(NULL,
111 "key with description '%s' has invalid payload",
112 key->description);
113 goto invalid;
Neeraj Soni36c65122018-04-18 21:04:46 +0530114 }
Eric Biggers24cc7a82018-04-30 15:51:48 -0700115
Eric Biggers08ac7222018-04-30 15:51:49 -0700116 if (payload->size < min_keysize) {
Eric Biggers24cc7a82018-04-30 15:51:48 -0700117 fscrypt_warn(NULL,
Eric Biggers08ac7222018-04-30 15:51:49 -0700118 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
Eric Biggers24cc7a82018-04-30 15:51:48 -0700119 key->description, payload->size, min_keysize);
120 goto invalid;
121 }
122
123 *payload_ret = payload;
124 return key;
125
126invalid:
127 up_read(&key->sem);
128 key_put(key);
129 return ERR_PTR(-ENOKEY);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700130}
131
Eric Biggerse33fa312018-11-26 11:27:37 -0800132static struct fscrypt_mode available_modes[] = {
Eric Biggersa4842a12018-05-18 10:58:14 -0700133 [FS_ENCRYPTION_MODE_AES_256_XTS] = {
134 .friendly_name = "AES-256-XTS",
135 .cipher_str = "xts(aes)",
136 .keysize = 64,
Eric Biggerse33fa312018-11-26 11:27:37 -0800137 .ivsize = 16,
Eric Biggersa4842a12018-05-18 10:58:14 -0700138 },
139 [FS_ENCRYPTION_MODE_AES_256_CTS] = {
140 .friendly_name = "AES-256-CTS-CBC",
141 .cipher_str = "cts(cbc(aes))",
142 .keysize = 32,
Eric Biggerse33fa312018-11-26 11:27:37 -0800143 .ivsize = 16,
Eric Biggersa4842a12018-05-18 10:58:14 -0700144 },
145 [FS_ENCRYPTION_MODE_AES_128_CBC] = {
146 .friendly_name = "AES-128-CBC",
147 .cipher_str = "cbc(aes)",
148 .keysize = 16,
Eric Biggerse33fa312018-11-26 11:27:37 -0800149 .ivsize = 16,
150 .needs_essiv = true,
Eric Biggersa4842a12018-05-18 10:58:14 -0700151 },
152 [FS_ENCRYPTION_MODE_AES_128_CTS] = {
153 .friendly_name = "AES-128-CTS-CBC",
154 .cipher_str = "cts(cbc(aes))",
155 .keysize = 16,
Eric Biggerse33fa312018-11-26 11:27:37 -0800156 .ivsize = 16,
157 },
158 [FS_ENCRYPTION_MODE_ADIANTUM] = {
159 .friendly_name = "Adiantum",
160 .cipher_str = "adiantum(xchacha12,aes)",
161 .keysize = 32,
162 .ivsize = 32,
Eric Biggersa4842a12018-05-18 10:58:14 -0700163 },
Blagovest Kolenichevf56989b2018-09-28 03:15:02 -0700164 [FS_ENCRYPTION_MODE_PRIVATE] = {
165 .friendly_name = "ICE",
166 .cipher_str = "bugon",
167 .keysize = 64,
168 },
Hyojun Kim63da4202017-10-06 17:10:08 -0700169};
170
Eric Biggersa4842a12018-05-18 10:58:14 -0700171static struct fscrypt_mode *
jianzhoub62aeea2019-01-16 16:11:43 +0800172select_encryption_mode(struct fscrypt_info *ci, const struct inode *inode)
Eric Biggers8f398502016-09-15 13:32:11 -0400173{
Hyojun Kim63da4202017-10-06 17:10:08 -0700174 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
Eric Biggers78275d82018-04-30 15:51:47 -0700175 fscrypt_warn(inode->i_sb,
176 "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
177 inode->i_ino, ci->ci_data_mode,
178 ci->ci_filename_mode);
Eric Biggersa4842a12018-05-18 10:58:14 -0700179 return ERR_PTR(-EINVAL);
Hyojun Kim63da4202017-10-06 17:10:08 -0700180 }
181
jianzhoub62aeea2019-01-16 16:11:43 +0800182 if (S_ISREG(inode->i_mode)) {
183 ci->ci_type = CI_DATA_TYPE;
Eric Biggersa4842a12018-05-18 10:58:14 -0700184 return &available_modes[ci->ci_data_mode];
jianzhoub62aeea2019-01-16 16:11:43 +0800185 }
Eric Biggers8f398502016-09-15 13:32:11 -0400186
jianzhoub62aeea2019-01-16 16:11:43 +0800187 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
188 ci->ci_type = CI_FNAME_TYPE;
Eric Biggersa4842a12018-05-18 10:58:14 -0700189 return &available_modes[ci->ci_filename_mode];
jianzhoub62aeea2019-01-16 16:11:43 +0800190 }
Eric Biggersa4842a12018-05-18 10:58:14 -0700191
192 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
193 inode->i_ino, (inode->i_mode & S_IFMT));
194 return ERR_PTR(-EINVAL);
Eric Biggers8f398502016-09-15 13:32:11 -0400195}
196
Eric Biggerse33fa312018-11-26 11:27:37 -0800197/* Find the master key, then derive the inode's actual encryption key */
198static int find_and_derive_key(const struct inode *inode,
199 const struct fscrypt_context *ctx,
200 u8 *derived_key, const struct fscrypt_mode *mode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700201{
Eric Biggerse33fa312018-11-26 11:27:37 -0800202 struct key *key;
203 const struct fscrypt_key *payload;
204 int err;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700205
Eric Biggerse33fa312018-11-26 11:27:37 -0800206 key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
207 ctx->master_key_descriptor,
208 mode->keysize, &payload);
209 if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
210 key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
211 ctx->master_key_descriptor,
212 mode->keysize, &payload);
213 }
214 if (IS_ERR(key))
215 return PTR_ERR(key);
216
217 if (ctx->flags & FS_POLICY_FLAG_DIRECT_KEY) {
218 if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
219 fscrypt_warn(inode->i_sb,
220 "direct key mode not allowed with %s",
221 mode->friendly_name);
222 err = -EINVAL;
223 } else if (ctx->contents_encryption_mode !=
224 ctx->filenames_encryption_mode) {
225 fscrypt_warn(inode->i_sb,
226 "direct key mode not allowed with different contents and filenames modes");
227 err = -EINVAL;
228 } else {
229 memcpy(derived_key, payload->raw, mode->keysize);
230 err = 0;
231 }
232 } else {
233 err = derive_key_aes(payload->raw, ctx, derived_key,
234 mode->keysize);
235 }
236 up_read(&key->sem);
237 key_put(key);
238 return err;
239}
240
241/* Allocate and key a symmetric cipher object for the given encryption mode */
242static struct crypto_skcipher *
243allocate_skcipher_for_mode(struct fscrypt_mode *mode, const u8 *raw_key,
244 const struct inode *inode)
245{
246 struct crypto_skcipher *tfm;
247 int err;
248
249 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
250 if (IS_ERR(tfm)) {
251 fscrypt_warn(inode->i_sb,
252 "error allocating '%s' transform for inode %lu: %ld",
253 mode->cipher_str, inode->i_ino, PTR_ERR(tfm));
254 return tfm;
255 }
256 if (unlikely(!mode->logged_impl_name)) {
257 /*
258 * fscrypt performance can vary greatly depending on which
259 * crypto algorithm implementation is used. Help people debug
260 * performance problems by logging the ->cra_driver_name the
261 * first time a mode is used. Note that multiple threads can
262 * race here, but it doesn't really matter.
263 */
264 mode->logged_impl_name = true;
265 pr_info("fscrypt: %s using implementation \"%s\"\n",
266 mode->friendly_name,
267 crypto_skcipher_alg(tfm)->base.cra_driver_name);
268 }
269 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
270 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
271 if (err)
272 goto err_free_tfm;
273
274 return tfm;
275
276err_free_tfm:
277 crypto_free_skcipher(tfm);
278 return ERR_PTR(err);
279}
280
281/* Master key referenced by FS_POLICY_FLAG_DIRECT_KEY policy */
282struct fscrypt_master_key {
283 struct hlist_node mk_node;
284 atomic_t mk_refcount;
285 const struct fscrypt_mode *mk_mode;
286 struct crypto_skcipher *mk_ctfm;
287 u8 mk_descriptor[FS_KEY_DESCRIPTOR_SIZE];
288 u8 mk_raw[FS_MAX_KEY_SIZE];
289};
290
291static void free_master_key(struct fscrypt_master_key *mk)
292{
293 if (mk) {
294 crypto_free_skcipher(mk->mk_ctfm);
295 kzfree(mk);
296 }
297}
298
299static void put_master_key(struct fscrypt_master_key *mk)
300{
301 if (!atomic_dec_and_lock(&mk->mk_refcount, &fscrypt_master_keys_lock))
302 return;
303 hash_del(&mk->mk_node);
304 spin_unlock(&fscrypt_master_keys_lock);
Eric Biggerse33fa312018-11-26 11:27:37 -0800305 free_master_key(mk);
306}
307
308/*
309 * Find/insert the given master key into the fscrypt_master_keys table. If
310 * found, it is returned with elevated refcount, and 'to_insert' is freed if
311 * non-NULL. If not found, 'to_insert' is inserted and returned if it's
312 * non-NULL; otherwise NULL is returned.
313 */
314static struct fscrypt_master_key *
315find_or_insert_master_key(struct fscrypt_master_key *to_insert,
316 const u8 *raw_key, const struct fscrypt_mode *mode,
317 const struct fscrypt_info *ci)
318{
319 unsigned long hash_key;
320 struct fscrypt_master_key *mk;
321
322 /*
323 * Careful: to avoid potentially leaking secret key bytes via timing
324 * information, we must key the hash table by descriptor rather than by
325 * raw key, and use crypto_memneq() when comparing raw keys.
326 */
327
328 BUILD_BUG_ON(sizeof(hash_key) > FS_KEY_DESCRIPTOR_SIZE);
329 memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key));
330
331 spin_lock(&fscrypt_master_keys_lock);
332 hash_for_each_possible(fscrypt_master_keys, mk, mk_node, hash_key) {
333 if (memcmp(ci->ci_master_key_descriptor, mk->mk_descriptor,
334 FS_KEY_DESCRIPTOR_SIZE) != 0)
335 continue;
336 if (mode != mk->mk_mode)
337 continue;
338 if (crypto_memneq(raw_key, mk->mk_raw, mode->keysize))
339 continue;
340 /* using existing tfm with same (descriptor, mode, raw_key) */
341 atomic_inc(&mk->mk_refcount);
342 spin_unlock(&fscrypt_master_keys_lock);
343 free_master_key(to_insert);
344 return mk;
345 }
346 if (to_insert)
347 hash_add(fscrypt_master_keys, &to_insert->mk_node, hash_key);
348 spin_unlock(&fscrypt_master_keys_lock);
349 return to_insert;
350}
351
352/* Prepare to encrypt directly using the master key in the given mode */
353static struct fscrypt_master_key *
354fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode,
355 const u8 *raw_key, const struct inode *inode)
356{
357 struct fscrypt_master_key *mk;
358 int err;
359
360 /* Is there already a tfm for this key? */
361 mk = find_or_insert_master_key(NULL, raw_key, mode, ci);
362 if (mk)
363 return mk;
364
365 /* Nope, allocate one. */
366 mk = kzalloc(sizeof(*mk), GFP_NOFS);
367 if (!mk)
368 return ERR_PTR(-ENOMEM);
369 atomic_set(&mk->mk_refcount, 1);
370 mk->mk_mode = mode;
371 mk->mk_ctfm = allocate_skcipher_for_mode(mode, raw_key, inode);
372 if (IS_ERR(mk->mk_ctfm)) {
373 err = PTR_ERR(mk->mk_ctfm);
374 mk->mk_ctfm = NULL;
375 goto err_free_mk;
376 }
377 memcpy(mk->mk_descriptor, ci->ci_master_key_descriptor,
378 FS_KEY_DESCRIPTOR_SIZE);
379 memcpy(mk->mk_raw, raw_key, mode->keysize);
380
381 return find_or_insert_master_key(mk, raw_key, mode, ci);
382
383err_free_mk:
384 free_master_key(mk);
385 return ERR_PTR(err);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700386}
387
Hyojun Kim63da4202017-10-06 17:10:08 -0700388static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
389{
390 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
391
392 /* init hash transform on demand */
393 if (unlikely(!tfm)) {
394 struct crypto_shash *prev_tfm;
395
396 tfm = crypto_alloc_shash("sha256", 0, 0);
397 if (IS_ERR(tfm)) {
Eric Biggers78275d82018-04-30 15:51:47 -0700398 fscrypt_warn(NULL,
399 "error allocating SHA-256 transform: %ld",
400 PTR_ERR(tfm));
Hyojun Kim63da4202017-10-06 17:10:08 -0700401 return PTR_ERR(tfm);
402 }
403 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
404 if (prev_tfm) {
405 crypto_free_shash(tfm);
406 tfm = prev_tfm;
407 }
408 }
409
410 {
411 SHASH_DESC_ON_STACK(desc, tfm);
412 desc->tfm = tfm;
413 desc->flags = 0;
414
415 return crypto_shash_digest(desc, key, keysize, salt);
416 }
417}
418
419static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
420 int keysize)
421{
422 int err;
423 struct crypto_cipher *essiv_tfm;
424 u8 salt[SHA256_DIGEST_SIZE];
425
426 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
427 if (IS_ERR(essiv_tfm))
428 return PTR_ERR(essiv_tfm);
429
430 ci->ci_essiv_tfm = essiv_tfm;
431
432 err = derive_essiv_salt(raw_key, keysize, salt);
433 if (err)
434 goto out;
435
436 /*
437 * Using SHA256 to derive the salt/key will result in AES-256 being
438 * used for IV generation. File contents encryption will still use the
439 * configured keysize (AES-128) nevertheless.
440 */
441 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
442 if (err)
443 goto out;
444
445out:
446 memzero_explicit(salt, sizeof(salt));
447 return err;
448}
449
450void __exit fscrypt_essiv_cleanup(void)
451{
452 crypto_free_shash(essiv_hash_tfm);
453}
454
Neeraj Soni36c65122018-04-18 21:04:46 +0530455static int fscrypt_data_encryption_mode(struct inode *inode)
Neeraj Sonic692cb92018-04-18 17:20:22 +0530456{
Neeraj Soni07c3e612018-07-23 14:26:53 +0530457 return fscrypt_is_ice_capable(inode->i_sb) ?
Neeraj Soni36c65122018-04-18 21:04:46 +0530458 FS_ENCRYPTION_MODE_PRIVATE : FS_ENCRYPTION_MODE_AES_256_XTS;
Neeraj Sonic692cb92018-04-18 17:20:22 +0530459}
460
Blagovest Kolenichevf56989b2018-09-28 03:15:02 -0700461int fscrypt_get_mode_key_size(int mode)
462{
463 return available_modes[mode].keysize;
464}
465EXPORT_SYMBOL(fscrypt_get_mode_key_size);
Eric Biggerse33fa312018-11-26 11:27:37 -0800466/*
467 * Given the encryption mode and key (normally the derived key, but for
468 * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's
469 * symmetric cipher transform object(s).
470 */
471static int setup_crypto_transform(struct fscrypt_info *ci,
472 struct fscrypt_mode *mode,
473 const u8 *raw_key, const struct inode *inode)
474{
475 struct fscrypt_master_key *mk;
476 struct crypto_skcipher *ctfm;
477 int err;
478
479 if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) {
480 mk = fscrypt_get_master_key(ci, mode, raw_key, inode);
481 if (IS_ERR(mk))
482 return PTR_ERR(mk);
483 ctfm = mk->mk_ctfm;
484 } else {
485 mk = NULL;
486 ctfm = allocate_skcipher_for_mode(mode, raw_key, inode);
487 if (IS_ERR(ctfm))
488 return PTR_ERR(ctfm);
489 }
490 ci->ci_master_key = mk;
491 ci->ci_ctfm = ctfm;
492
493 if (mode->needs_essiv) {
494 /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */
495 WARN_ON(mode->ivsize != AES_BLOCK_SIZE);
496 WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY);
497
498 err = init_essiv_generator(ci, raw_key, mode->keysize);
499 if (err) {
500 fscrypt_warn(inode->i_sb,
501 "error initializing ESSIV generator for inode %lu: %d",
502 inode->i_ino, err);
503 return err;
504 }
505 }
506 return 0;
507}
508
509static void put_crypt_info(struct fscrypt_info *ci)
510{
511 if (!ci)
512 return;
513
514 if (ci->ci_master_key) {
515 put_master_key(ci->ci_master_key);
516 } else {
517 crypto_free_skcipher(ci->ci_ctfm);
518 crypto_free_cipher(ci->ci_essiv_tfm);
519 }
jianzhoub62aeea2019-01-16 16:11:43 +0800520 memset(ci, 0, sizeof(*ci)); /* sanitizes ->ci_raw_key */
Eric Biggerse33fa312018-11-26 11:27:37 -0800521 kmem_cache_free(fscrypt_info_cachep, ci);
522}
Blagovest Kolenichevf56989b2018-09-28 03:15:02 -0700523
Eric Biggers2984e522017-02-21 15:07:11 -0800524int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700525{
526 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700527 struct fscrypt_context ctx;
Eric Biggersa4842a12018-05-18 10:58:14 -0700528 struct fscrypt_mode *mode;
Eric Biggers0f0909e2016-11-13 20:41:09 -0500529 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700530 int res;
531
Eric Biggers70e22212019-04-11 14:32:15 -0700532 if (fscrypt_has_encryption_key(inode))
Eric Biggers2984e522017-02-21 15:07:11 -0800533 return 0;
534
Hyojun Kim63da4202017-10-06 17:10:08 -0700535 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700536 if (res)
537 return res;
538
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700539 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
540 if (res < 0) {
Hyojun Kim63da4202017-10-06 17:10:08 -0700541 if (!fscrypt_dummy_context_enabled(inode) ||
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700542 IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700543 return res;
Hyojun Kim63da4202017-10-06 17:10:08 -0700544 /* Fake up a context for an unencrypted directory */
545 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400546 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Neeraj Soni36c65122018-04-18 21:04:46 +0530547 ctx.contents_encryption_mode =
548 fscrypt_data_encryption_mode(inode);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700549 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Hyojun Kim63da4202017-10-06 17:10:08 -0700550 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700551 } else if (res != sizeof(ctx)) {
552 return -EINVAL;
553 }
Eric Biggers8f398502016-09-15 13:32:11 -0400554
555 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
556 return -EINVAL;
557
558 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
559 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700560
Eric Biggerse33fa312018-11-26 11:27:37 -0800561 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700562 if (!crypt_info)
563 return -ENOMEM;
564
565 crypt_info->ci_flags = ctx.flags;
566 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
567 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
Eric Biggerse33fa312018-11-26 11:27:37 -0800568 memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor,
569 FS_KEY_DESCRIPTOR_SIZE);
570 memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700571
Eric Biggersa4842a12018-05-18 10:58:14 -0700572 mode = select_encryption_mode(crypt_info, inode);
573 if (IS_ERR(mode)) {
574 res = PTR_ERR(mode);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700575 goto out;
Eric Biggersa4842a12018-05-18 10:58:14 -0700576 }
Eric Biggerse33fa312018-11-26 11:27:37 -0800577 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
578 crypt_info->ci_mode = mode;
Eric Biggers8f398502016-09-15 13:32:11 -0400579
Eric Biggers0f0909e2016-11-13 20:41:09 -0500580 /*
Eric Biggerse33fa312018-11-26 11:27:37 -0800581 * This cannot be a stack buffer because it may be passed to the
582 * scatterlist crypto API as part of key derivation.
Eric Biggers0f0909e2016-11-13 20:41:09 -0500583 */
584 res = -ENOMEM;
Eric Biggersa4842a12018-05-18 10:58:14 -0700585 raw_key = kmalloc(mode->keysize, GFP_NOFS);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500586 if (!raw_key)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700587 goto out;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700588
Eric Biggerse33fa312018-11-26 11:27:37 -0800589 res = find_and_derive_key(inode, &ctx, raw_key, mode);
Eric Biggers24cc7a82018-04-30 15:51:48 -0700590 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700591 goto out;
Neeraj Soni36c65122018-04-18 21:04:46 +0530592
593 if (is_private_data_mode(crypt_info)) {
594 if (!fscrypt_is_ice_capable(inode->i_sb)) {
595 pr_warn("%s: ICE support not available\n",
Blagovest Kolenichevf56989b2018-09-28 03:15:02 -0700596 __func__);
Neeraj Soni36c65122018-04-18 21:04:46 +0530597 res = -EINVAL;
598 goto out;
599 }
600 /* Let's encrypt/decrypt by ICE */
Blagovest Kolenichevf56989b2018-09-28 03:15:02 -0700601 memcpy(crypt_info->ci_raw_key, raw_key, mode->keysize);
Neeraj Soni36c65122018-04-18 21:04:46 +0530602 goto do_ice;
603 }
604
Eric Biggerse33fa312018-11-26 11:27:37 -0800605 res = setup_crypto_transform(crypt_info, mode, raw_key, inode);
Neeraj Soni36c65122018-04-18 21:04:46 +0530606 if (res)
607 goto out;
608
Neeraj Soni36c65122018-04-18 21:04:46 +0530609do_ice:
Eric Biggers70e22212019-04-11 14:32:15 -0700610 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL)
Eric Biggers2984e522017-02-21 15:07:11 -0800611 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700612out:
613 if (res == -ENOKEY)
614 res = 0;
615 put_crypt_info(crypt_info);
Neeraj Soni0ebb2de2019-08-20 13:52:32 +0530616 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700617 return res;
618}
Eric Biggers2984e522017-02-21 15:07:11 -0800619EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700620
Eric Biggersac8894a2019-04-10 13:21:15 -0700621/**
622 * fscrypt_put_encryption_info - free most of an inode's fscrypt data
623 *
624 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
625 * being evicted. An RCU grace period need not have elapsed yet.
626 */
Jaegeuk Kimd9197652018-01-05 10:44:52 -0800627void fscrypt_put_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700628{
Jaegeuk Kimd9197652018-01-05 10:44:52 -0800629 put_crypt_info(inode->i_crypt_info);
630 inode->i_crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700631}
632EXPORT_SYMBOL(fscrypt_put_encryption_info);
Eric Biggersac8894a2019-04-10 13:21:15 -0700633
634/**
635 * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
636 *
637 * Free the inode's cached decrypted symlink target, if any. Filesystems must
638 * call this after an RCU grace period, just before they free the inode.
639 */
640void fscrypt_free_inode(struct inode *inode)
641{
642 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
643 kfree(inode->i_link);
644 inode->i_link = NULL;
645 }
646}
647EXPORT_SYMBOL(fscrypt_free_inode);