blob: 7eadf7ce73cf87ede5bfd105d25add9bbc03e9d0 [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>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070012#include <linux/scatterlist.h>
Daniel Walter8e989de2017-06-19 09:27:58 +020013#include <linux/ratelimit.h>
14#include <crypto/aes.h>
15#include <crypto/sha.h>
Eric Biggerseb9c5fd2018-01-05 10:45:00 -080016#include <crypto/skcipher.h>
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070017#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070018
Daniel Walter8e989de2017-06-19 09:27:58 +020019static struct crypto_shash *essiv_hash_tfm;
20
Eric Biggers08ac7222018-04-30 15:51:49 -070021/*
22 * Key derivation function. This generates the derived key by encrypting the
23 * master key with AES-128-ECB using the inode's nonce as the AES key.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070024 *
Eric Biggers08ac7222018-04-30 15:51:49 -070025 * The master key must be at least as long as the derived key. If the master
26 * key is longer, then only the first 'derived_keysize' bytes are used.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070027 */
Eric Biggers08ac7222018-04-30 15:51:49 -070028static int derive_key_aes(const u8 *master_key,
29 const struct fscrypt_context *ctx,
30 u8 *derived_key, unsigned int derived_keysize)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070031{
32 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070033 struct skcipher_request *req = NULL;
Gilad Ben-Yossef743205f2017-10-18 08:00:44 +010034 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070035 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070036 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037
38 if (IS_ERR(tfm)) {
39 res = PTR_ERR(tfm);
40 tfm = NULL;
41 goto out;
42 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070043 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
44 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070045 if (!req) {
46 res = -ENOMEM;
47 goto out;
48 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070049 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070050 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossef743205f2017-10-18 08:00:44 +010051 crypto_req_done, &wait);
Eric Biggers08ac7222018-04-30 15:51:49 -070052 res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070053 if (res < 0)
54 goto out;
55
Eric Biggers08ac7222018-04-30 15:51:49 -070056 sg_init_one(&src_sg, master_key, derived_keysize);
57 sg_init_one(&dst_sg, derived_key, derived_keysize);
58 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
Daniel Walter8e989de2017-06-19 09:27:58 +020059 NULL);
Gilad Ben-Yossef743205f2017-10-18 08:00:44 +010060 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070061out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070062 skcipher_request_free(req);
63 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070064 return res;
65}
66
Eric Biggers24cc7a82018-04-30 15:51:48 -070067/*
68 * Search the current task's subscribed keyrings for a "logon" key with
69 * description prefix:descriptor, and if found acquire a read lock on it and
70 * return a pointer to its validated payload in *payload_ret.
71 */
72static struct key *
73find_and_lock_process_key(const char *prefix,
74 const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
75 unsigned int min_keysize,
76 const struct fscrypt_key **payload_ret)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070077{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070078 char *description;
Eric Biggers24cc7a82018-04-30 15:51:48 -070079 struct key *key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070080 const struct user_key_payload *ukp;
Eric Biggers24cc7a82018-04-30 15:51:48 -070081 const struct fscrypt_key *payload;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070082
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070083 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
Eric Biggers24cc7a82018-04-30 15:51:48 -070084 FS_KEY_DESCRIPTOR_SIZE, descriptor);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070085 if (!description)
Eric Biggers24cc7a82018-04-30 15:51:48 -070086 return ERR_PTR(-ENOMEM);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070087
Eric Biggers24cc7a82018-04-30 15:51:48 -070088 key = request_key(&key_type_logon, description, NULL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070089 kfree(description);
Eric Biggers24cc7a82018-04-30 15:51:48 -070090 if (IS_ERR(key))
91 return key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070092
Eric Biggers24cc7a82018-04-30 15:51:48 -070093 down_read(&key->sem);
94 ukp = user_key_payload_locked(key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070095
Eric Biggers24cc7a82018-04-30 15:51:48 -070096 if (!ukp) /* was the key revoked before we acquired its semaphore? */
97 goto invalid;
98
99 payload = (const struct fscrypt_key *)ukp->data;
100
101 if (ukp->datalen != sizeof(struct fscrypt_key) ||
102 payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
103 fscrypt_warn(NULL,
104 "key with description '%s' has invalid payload",
105 key->description);
106 goto invalid;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700107 }
Eric Biggers24cc7a82018-04-30 15:51:48 -0700108
Eric Biggers08ac7222018-04-30 15:51:49 -0700109 if (payload->size < min_keysize) {
Eric Biggers24cc7a82018-04-30 15:51:48 -0700110 fscrypt_warn(NULL,
Eric Biggers08ac7222018-04-30 15:51:49 -0700111 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
Eric Biggers24cc7a82018-04-30 15:51:48 -0700112 key->description, payload->size, min_keysize);
113 goto invalid;
114 }
115
116 *payload_ret = payload;
117 return key;
118
119invalid:
120 up_read(&key->sem);
121 key_put(key);
122 return ERR_PTR(-ENOKEY);
123}
124
125/* Find the master key, then derive the inode's actual encryption key */
126static int find_and_derive_key(const struct inode *inode,
127 const struct fscrypt_context *ctx,
128 u8 *derived_key, unsigned int derived_keysize)
129{
130 struct key *key;
131 const struct fscrypt_key *payload;
132 int err;
133
134 key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
135 ctx->master_key_descriptor,
136 derived_keysize, &payload);
137 if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
138 key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
139 ctx->master_key_descriptor,
140 derived_keysize, &payload);
141 }
142 if (IS_ERR(key))
143 return PTR_ERR(key);
Eric Biggers08ac7222018-04-30 15:51:49 -0700144 err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize);
Eric Biggers24cc7a82018-04-30 15:51:48 -0700145 up_read(&key->sem);
146 key_put(key);
147 return err;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700148}
149
Eric Biggersa4842a12018-05-18 10:58:14 -0700150static struct fscrypt_mode {
151 const char *friendly_name;
Daniel Walter8e989de2017-06-19 09:27:58 +0200152 const char *cipher_str;
153 int keysize;
Eric Biggersa4842a12018-05-18 10:58:14 -0700154 bool logged_impl_name;
Daniel Walter8e989de2017-06-19 09:27:58 +0200155} available_modes[] = {
Eric Biggersa4842a12018-05-18 10:58:14 -0700156 [FS_ENCRYPTION_MODE_AES_256_XTS] = {
157 .friendly_name = "AES-256-XTS",
158 .cipher_str = "xts(aes)",
159 .keysize = 64,
160 },
161 [FS_ENCRYPTION_MODE_AES_256_CTS] = {
162 .friendly_name = "AES-256-CTS-CBC",
163 .cipher_str = "cts(cbc(aes))",
164 .keysize = 32,
165 },
166 [FS_ENCRYPTION_MODE_AES_128_CBC] = {
167 .friendly_name = "AES-128-CBC",
168 .cipher_str = "cbc(aes)",
169 .keysize = 16,
170 },
171 [FS_ENCRYPTION_MODE_AES_128_CTS] = {
172 .friendly_name = "AES-128-CTS-CBC",
173 .cipher_str = "cts(cbc(aes))",
174 .keysize = 16,
175 },
Daniel Walter8e989de2017-06-19 09:27:58 +0200176};
177
Eric Biggersa4842a12018-05-18 10:58:14 -0700178static struct fscrypt_mode *
179select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
Eric Biggers8f398502016-09-15 13:32:11 -0400180{
Daniel Walter8e989de2017-06-19 09:27:58 +0200181 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
Eric Biggers78275d82018-04-30 15:51:47 -0700182 fscrypt_warn(inode->i_sb,
183 "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
184 inode->i_ino, ci->ci_data_mode,
185 ci->ci_filename_mode);
Eric Biggersa4842a12018-05-18 10:58:14 -0700186 return ERR_PTR(-EINVAL);
Daniel Walter8e989de2017-06-19 09:27:58 +0200187 }
188
Eric Biggersa4842a12018-05-18 10:58:14 -0700189 if (S_ISREG(inode->i_mode))
190 return &available_modes[ci->ci_data_mode];
Eric Biggers8f398502016-09-15 13:32:11 -0400191
Eric Biggersa4842a12018-05-18 10:58:14 -0700192 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
193 return &available_modes[ci->ci_filename_mode];
194
195 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
196 inode->i_ino, (inode->i_mode & S_IFMT));
197 return ERR_PTR(-EINVAL);
Eric Biggers8f398502016-09-15 13:32:11 -0400198}
199
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700200static void put_crypt_info(struct fscrypt_info *ci)
201{
202 if (!ci)
203 return;
204
Linus Torvaldsd4075742016-03-21 11:03:02 -0700205 crypto_free_skcipher(ci->ci_ctfm);
Daniel Walter8e989de2017-06-19 09:27:58 +0200206 crypto_free_cipher(ci->ci_essiv_tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700207 kmem_cache_free(fscrypt_info_cachep, ci);
208}
209
Daniel Walter8e989de2017-06-19 09:27:58 +0200210static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
211{
212 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
213
214 /* init hash transform on demand */
215 if (unlikely(!tfm)) {
216 struct crypto_shash *prev_tfm;
217
218 tfm = crypto_alloc_shash("sha256", 0, 0);
219 if (IS_ERR(tfm)) {
Eric Biggers78275d82018-04-30 15:51:47 -0700220 fscrypt_warn(NULL,
221 "error allocating SHA-256 transform: %ld",
222 PTR_ERR(tfm));
Daniel Walter8e989de2017-06-19 09:27:58 +0200223 return PTR_ERR(tfm);
224 }
225 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
226 if (prev_tfm) {
227 crypto_free_shash(tfm);
228 tfm = prev_tfm;
229 }
230 }
231
232 {
233 SHASH_DESC_ON_STACK(desc, tfm);
234 desc->tfm = tfm;
235 desc->flags = 0;
236
237 return crypto_shash_digest(desc, key, keysize, salt);
238 }
239}
240
241static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
242 int keysize)
243{
244 int err;
245 struct crypto_cipher *essiv_tfm;
246 u8 salt[SHA256_DIGEST_SIZE];
247
248 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
249 if (IS_ERR(essiv_tfm))
250 return PTR_ERR(essiv_tfm);
251
252 ci->ci_essiv_tfm = essiv_tfm;
253
254 err = derive_essiv_salt(raw_key, keysize, salt);
255 if (err)
256 goto out;
257
258 /*
259 * Using SHA256 to derive the salt/key will result in AES-256 being
260 * used for IV generation. File contents encryption will still use the
261 * configured keysize (AES-128) nevertheless.
262 */
263 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
264 if (err)
265 goto out;
266
267out:
268 memzero_explicit(salt, sizeof(salt));
269 return err;
270}
271
272void __exit fscrypt_essiv_cleanup(void)
273{
274 crypto_free_shash(essiv_hash_tfm);
275}
276
Eric Biggers2984e522017-02-21 15:07:11 -0800277int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700278{
279 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700281 struct crypto_skcipher *ctfm;
Eric Biggersa4842a12018-05-18 10:58:14 -0700282 struct fscrypt_mode *mode;
Eric Biggers0f0909e2016-11-13 20:41:09 -0500283 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700284 int res;
285
Eric Biggers2984e522017-02-21 15:07:11 -0800286 if (inode->i_crypt_info)
287 return 0;
288
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700289 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700290 if (res)
291 return res;
292
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700293 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
294 if (res < 0) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700295 if (!fscrypt_dummy_context_enabled(inode) ||
Eric Biggersd750ec72017-10-09 12:15:36 -0700296 IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700297 return res;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700298 /* Fake up a context for an unencrypted directory */
299 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400300 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700301 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
302 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700303 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700304 } else if (res != sizeof(ctx)) {
305 return -EINVAL;
306 }
Eric Biggers8f398502016-09-15 13:32:11 -0400307
308 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
309 return -EINVAL;
310
311 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
312 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700313
314 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
315 if (!crypt_info)
316 return -ENOMEM;
317
318 crypt_info->ci_flags = ctx.flags;
319 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
320 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
321 crypt_info->ci_ctfm = NULL;
Daniel Walter8e989de2017-06-19 09:27:58 +0200322 crypt_info->ci_essiv_tfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700323 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
324 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700325
Eric Biggersa4842a12018-05-18 10:58:14 -0700326 mode = select_encryption_mode(crypt_info, inode);
327 if (IS_ERR(mode)) {
328 res = PTR_ERR(mode);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700329 goto out;
Eric Biggersa4842a12018-05-18 10:58:14 -0700330 }
Eric Biggers8f398502016-09-15 13:32:11 -0400331
Eric Biggers0f0909e2016-11-13 20:41:09 -0500332 /*
333 * This cannot be a stack buffer because it is passed to the scatterlist
334 * crypto API as part of key derivation.
335 */
336 res = -ENOMEM;
Eric Biggersa4842a12018-05-18 10:58:14 -0700337 raw_key = kmalloc(mode->keysize, GFP_NOFS);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500338 if (!raw_key)
339 goto out;
340
Eric Biggersa4842a12018-05-18 10:58:14 -0700341 res = find_and_derive_key(inode, &ctx, raw_key, mode->keysize);
Eric Biggers24cc7a82018-04-30 15:51:48 -0700342 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700343 goto out;
Eric Biggers24cc7a82018-04-30 15:51:48 -0700344
Eric Biggersa4842a12018-05-18 10:58:14 -0700345 ctfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
Eric Biggersc6b42b92018-04-30 15:51:37 -0700346 if (IS_ERR(ctfm)) {
347 res = PTR_ERR(ctfm);
Eric Biggers78275d82018-04-30 15:51:47 -0700348 fscrypt_warn(inode->i_sb,
349 "error allocating '%s' transform for inode %lu: %d",
Eric Biggersa4842a12018-05-18 10:58:14 -0700350 mode->cipher_str, inode->i_ino, res);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700351 goto out;
352 }
Eric Biggersa4842a12018-05-18 10:58:14 -0700353 if (unlikely(!mode->logged_impl_name)) {
354 /*
355 * fscrypt performance can vary greatly depending on which
356 * crypto algorithm implementation is used. Help people debug
357 * performance problems by logging the ->cra_driver_name the
358 * first time a mode is used. Note that multiple threads can
359 * race here, but it doesn't really matter.
360 */
361 mode->logged_impl_name = true;
362 pr_info("fscrypt: %s using implementation \"%s\"\n",
363 mode->friendly_name,
364 crypto_skcipher_alg(ctfm)->base.cra_driver_name);
365 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700366 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700367 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Eric Biggersa4842a12018-05-18 10:58:14 -0700368 res = crypto_skcipher_setkey(ctfm, raw_key, mode->keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700369 if (res)
370 goto out;
371
Daniel Walter8e989de2017-06-19 09:27:58 +0200372 if (S_ISREG(inode->i_mode) &&
373 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
Eric Biggersa4842a12018-05-18 10:58:14 -0700374 res = init_essiv_generator(crypt_info, raw_key, mode->keysize);
Daniel Walter8e989de2017-06-19 09:27:58 +0200375 if (res) {
Eric Biggers78275d82018-04-30 15:51:47 -0700376 fscrypt_warn(inode->i_sb,
377 "error initializing ESSIV generator for inode %lu: %d",
378 inode->i_ino, res);
Daniel Walter8e989de2017-06-19 09:27:58 +0200379 goto out;
380 }
381 }
Eric Biggers2984e522017-02-21 15:07:11 -0800382 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
383 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700384out:
385 if (res == -ENOKEY)
386 res = 0;
387 put_crypt_info(crypt_info);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500388 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700389 return res;
390}
Eric Biggers2984e522017-02-21 15:07:11 -0800391EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700392
Eric Biggers401052f2018-01-11 23:30:13 -0500393void fscrypt_put_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700394{
Eric Biggers401052f2018-01-11 23:30:13 -0500395 put_crypt_info(inode->i_crypt_info);
396 inode->i_crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700397}
398EXPORT_SYMBOL(fscrypt_put_encryption_info);