blob: 41f6025d5d7a5260063e0fb35123b3c24503a107 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002/*
3 * key management facility for FS encryption support.
4 *
5 * Copyright (C) 2015, Google, Inc.
6 *
7 * This contains encryption key functions.
8 *
9 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
10 */
11
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070012#include <keys/user-type.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070013#include <linux/scatterlist.h>
Daniel Walterb7e7cf72017-06-19 09:27:58 +020014#include <linux/ratelimit.h>
15#include <crypto/aes.h>
16#include <crypto/sha.h>
Eric Biggersa5757842018-01-05 10:45:00 -080017#include <crypto/skcipher.h>
Theodore Ts'o3325bea2016-11-26 20:32:46 -050018#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070019
Daniel Walterb7e7cf72017-06-19 09:27:58 +020020static struct crypto_shash *essiv_hash_tfm;
21
Eric Biggers646b7d42018-04-30 15:51:49 -070022/*
23 * Key derivation function. This generates the derived key by encrypting the
24 * master key with AES-128-ECB using the inode's nonce as the AES key.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070025 *
Eric Biggers646b7d42018-04-30 15:51:49 -070026 * The master key must be at least as long as the derived key. If the master
27 * key is longer, then only the first 'derived_keysize' bytes are used.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070028 */
Eric Biggers646b7d42018-04-30 15:51:49 -070029static int derive_key_aes(const u8 *master_key,
30 const struct fscrypt_context *ctx,
31 u8 *derived_key, unsigned int derived_keysize)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070032{
33 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070034 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010035 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070036 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070037 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070038
39 if (IS_ERR(tfm)) {
40 res = PTR_ERR(tfm);
41 tfm = NULL;
42 goto out;
43 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070044 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
45 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070046 if (!req) {
47 res = -ENOMEM;
48 goto out;
49 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070050 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070051 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010052 crypto_req_done, &wait);
Eric Biggers646b7d42018-04-30 15:51:49 -070053 res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070054 if (res < 0)
55 goto out;
56
Eric Biggers646b7d42018-04-30 15:51:49 -070057 sg_init_one(&src_sg, master_key, derived_keysize);
58 sg_init_one(&dst_sg, derived_key, derived_keysize);
59 skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
Daniel Walterb7e7cf72017-06-19 09:27:58 +020060 NULL);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010061 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070062out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070063 skcipher_request_free(req);
64 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070065 return res;
66}
67
Eric Biggers590f4972018-04-30 15:51:48 -070068/*
69 * Search the current task's subscribed keyrings for a "logon" key with
70 * description prefix:descriptor, and if found acquire a read lock on it and
71 * return a pointer to its validated payload in *payload_ret.
72 */
73static struct key *
74find_and_lock_process_key(const char *prefix,
75 const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
76 unsigned int min_keysize,
77 const struct fscrypt_key **payload_ret)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070078{
Eric Biggersa5d431e2017-01-05 13:51:18 -080079 char *description;
Eric Biggers590f4972018-04-30 15:51:48 -070080 struct key *key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070081 const struct user_key_payload *ukp;
Eric Biggers590f4972018-04-30 15:51:48 -070082 const struct fscrypt_key *payload;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070083
Eric Biggersa5d431e2017-01-05 13:51:18 -080084 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
Eric Biggers590f4972018-04-30 15:51:48 -070085 FS_KEY_DESCRIPTOR_SIZE, descriptor);
Eric Biggersa5d431e2017-01-05 13:51:18 -080086 if (!description)
Eric Biggers590f4972018-04-30 15:51:48 -070087 return ERR_PTR(-ENOMEM);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070088
Eric Biggers590f4972018-04-30 15:51:48 -070089 key = request_key(&key_type_logon, description, NULL);
Eric Biggersa5d431e2017-01-05 13:51:18 -080090 kfree(description);
Eric Biggers590f4972018-04-30 15:51:48 -070091 if (IS_ERR(key))
92 return key;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070093
Eric Biggers590f4972018-04-30 15:51:48 -070094 down_read(&key->sem);
95 ukp = user_key_payload_locked(key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070096
Eric Biggers590f4972018-04-30 15:51:48 -070097 if (!ukp) /* was the key revoked before we acquired its semaphore? */
98 goto invalid;
99
100 payload = (const struct fscrypt_key *)ukp->data;
101
102 if (ukp->datalen != sizeof(struct fscrypt_key) ||
103 payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
104 fscrypt_warn(NULL,
105 "key with description '%s' has invalid payload",
106 key->description);
107 goto invalid;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700108 }
Eric Biggers590f4972018-04-30 15:51:48 -0700109
Eric Biggers646b7d42018-04-30 15:51:49 -0700110 if (payload->size < min_keysize) {
Eric Biggers590f4972018-04-30 15:51:48 -0700111 fscrypt_warn(NULL,
Eric Biggers646b7d42018-04-30 15:51:49 -0700112 "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
Eric Biggers590f4972018-04-30 15:51:48 -0700113 key->description, payload->size, min_keysize);
114 goto invalid;
115 }
116
117 *payload_ret = payload;
118 return key;
119
120invalid:
121 up_read(&key->sem);
122 key_put(key);
123 return ERR_PTR(-ENOKEY);
124}
125
126/* Find the master key, then derive the inode's actual encryption key */
127static int find_and_derive_key(const struct inode *inode,
128 const struct fscrypt_context *ctx,
129 u8 *derived_key, unsigned int derived_keysize)
130{
131 struct key *key;
132 const struct fscrypt_key *payload;
133 int err;
134
135 key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
136 ctx->master_key_descriptor,
137 derived_keysize, &payload);
138 if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
139 key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
140 ctx->master_key_descriptor,
141 derived_keysize, &payload);
142 }
143 if (IS_ERR(key))
144 return PTR_ERR(key);
Eric Biggers646b7d42018-04-30 15:51:49 -0700145 err = derive_key_aes(payload->raw, ctx, derived_key, derived_keysize);
Eric Biggers590f4972018-04-30 15:51:48 -0700146 up_read(&key->sem);
147 key_put(key);
148 return err;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700149}
150
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200151static const struct {
152 const char *cipher_str;
153 int keysize;
154} available_modes[] = {
Eric Biggers11b88182018-04-30 15:51:46 -0700155 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)", 64 },
156 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))", 32 },
157 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)", 16 },
158 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))", 16 },
Eric Biggers12d28f72018-05-07 17:22:08 -0700159 [FS_ENCRYPTION_MODE_SPECK128_256_XTS] = { "xts(speck128)", 64 },
160 [FS_ENCRYPTION_MODE_SPECK128_256_CTS] = { "cts(cbc(speck128))", 32 },
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200161};
162
Eric Biggers8f398502016-09-15 13:32:11 -0400163static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
164 const char **cipher_str_ret, int *keysize_ret)
165{
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200166 u32 mode;
167
168 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
Eric Biggers544d08f2018-04-30 15:51:47 -0700169 fscrypt_warn(inode->i_sb,
170 "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
171 inode->i_ino, ci->ci_data_mode,
172 ci->ci_filename_mode);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200173 return -EINVAL;
174 }
175
Eric Biggers8f398502016-09-15 13:32:11 -0400176 if (S_ISREG(inode->i_mode)) {
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200177 mode = ci->ci_data_mode;
178 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
179 mode = ci->ci_filename_mode;
180 } else {
181 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
182 inode->i_ino, (inode->i_mode & S_IFMT));
183 return -EINVAL;
Eric Biggers8f398502016-09-15 13:32:11 -0400184 }
185
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200186 *cipher_str_ret = available_modes[mode].cipher_str;
187 *keysize_ret = available_modes[mode].keysize;
188 return 0;
Eric Biggers8f398502016-09-15 13:32:11 -0400189}
190
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700191static void put_crypt_info(struct fscrypt_info *ci)
192{
193 if (!ci)
194 return;
195
Linus Torvaldsd4075742016-03-21 11:03:02 -0700196 crypto_free_skcipher(ci->ci_ctfm);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200197 crypto_free_cipher(ci->ci_essiv_tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700198 kmem_cache_free(fscrypt_info_cachep, ci);
199}
200
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200201static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
202{
203 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
204
205 /* init hash transform on demand */
206 if (unlikely(!tfm)) {
207 struct crypto_shash *prev_tfm;
208
209 tfm = crypto_alloc_shash("sha256", 0, 0);
210 if (IS_ERR(tfm)) {
Eric Biggers544d08f2018-04-30 15:51:47 -0700211 fscrypt_warn(NULL,
212 "error allocating SHA-256 transform: %ld",
213 PTR_ERR(tfm));
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200214 return PTR_ERR(tfm);
215 }
216 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
217 if (prev_tfm) {
218 crypto_free_shash(tfm);
219 tfm = prev_tfm;
220 }
221 }
222
223 {
224 SHASH_DESC_ON_STACK(desc, tfm);
225 desc->tfm = tfm;
226 desc->flags = 0;
227
228 return crypto_shash_digest(desc, key, keysize, salt);
229 }
230}
231
232static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
233 int keysize)
234{
235 int err;
236 struct crypto_cipher *essiv_tfm;
237 u8 salt[SHA256_DIGEST_SIZE];
238
239 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
240 if (IS_ERR(essiv_tfm))
241 return PTR_ERR(essiv_tfm);
242
243 ci->ci_essiv_tfm = essiv_tfm;
244
245 err = derive_essiv_salt(raw_key, keysize, salt);
246 if (err)
247 goto out;
248
249 /*
250 * Using SHA256 to derive the salt/key will result in AES-256 being
251 * used for IV generation. File contents encryption will still use the
252 * configured keysize (AES-128) nevertheless.
253 */
254 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
255 if (err)
256 goto out;
257
258out:
259 memzero_explicit(salt, sizeof(salt));
260 return err;
261}
262
263void __exit fscrypt_essiv_cleanup(void)
264{
265 crypto_free_shash(essiv_hash_tfm);
266}
267
Eric Biggers1b53cf92017-02-21 15:07:11 -0800268int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700269{
270 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700271 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700272 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700273 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400274 int keysize;
Eric Biggersa6e08912016-11-13 20:41:09 -0500275 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700276 int res;
277
Eric Biggers1b53cf92017-02-21 15:07:11 -0800278 if (inode->i_crypt_info)
279 return 0;
280
David Gstirf32d7ac2016-12-06 23:53:57 +0100281 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700282 if (res)
283 return res;
284
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
286 if (res < 0) {
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500287 if (!fscrypt_dummy_context_enabled(inode) ||
Eric Biggerse0428a22017-10-09 12:15:36 -0700288 IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700289 return res;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500290 /* Fake up a context for an unencrypted directory */
291 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400292 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700293 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
294 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500295 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700296 } else if (res != sizeof(ctx)) {
297 return -EINVAL;
298 }
Eric Biggers8f398502016-09-15 13:32:11 -0400299
300 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
301 return -EINVAL;
302
303 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
304 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700305
306 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
307 if (!crypt_info)
308 return -ENOMEM;
309
310 crypt_info->ci_flags = ctx.flags;
311 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
312 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
313 crypt_info->ci_ctfm = NULL;
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200314 crypt_info->ci_essiv_tfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700315 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
316 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700317
Eric Biggers8f398502016-09-15 13:32:11 -0400318 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
319 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700320 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400321
Eric Biggersa6e08912016-11-13 20:41:09 -0500322 /*
323 * This cannot be a stack buffer because it is passed to the scatterlist
324 * crypto API as part of key derivation.
325 */
326 res = -ENOMEM;
Eric Biggers646b7d42018-04-30 15:51:49 -0700327 raw_key = kmalloc(keysize, GFP_NOFS);
Eric Biggersa6e08912016-11-13 20:41:09 -0500328 if (!raw_key)
329 goto out;
330
Eric Biggers590f4972018-04-30 15:51:48 -0700331 res = find_and_derive_key(inode, &ctx, raw_key, keysize);
332 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700333 goto out;
Eric Biggers590f4972018-04-30 15:51:48 -0700334
Linus Torvaldsd4075742016-03-21 11:03:02 -0700335 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Eric Biggers99194792018-04-30 15:51:37 -0700336 if (IS_ERR(ctfm)) {
337 res = PTR_ERR(ctfm);
Eric Biggers544d08f2018-04-30 15:51:47 -0700338 fscrypt_warn(inode->i_sb,
339 "error allocating '%s' transform for inode %lu: %d",
340 cipher_str, inode->i_ino, res);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700341 goto out;
342 }
343 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700344 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Eric Biggers8f398502016-09-15 13:32:11 -0400345 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700346 if (res)
347 goto out;
348
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200349 if (S_ISREG(inode->i_mode) &&
350 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
351 res = init_essiv_generator(crypt_info, raw_key, keysize);
352 if (res) {
Eric Biggers544d08f2018-04-30 15:51:47 -0700353 fscrypt_warn(inode->i_sb,
354 "error initializing ESSIV generator for inode %lu: %d",
355 inode->i_ino, res);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200356 goto out;
357 }
358 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800359 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
360 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700361out:
362 if (res == -ENOKEY)
363 res = 0;
364 put_crypt_info(crypt_info);
Eric Biggersa6e08912016-11-13 20:41:09 -0500365 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700366 return res;
367}
Eric Biggers1b53cf92017-02-21 15:07:11 -0800368EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700369
Eric Biggers3d204e22018-01-11 23:30:13 -0500370void fscrypt_put_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700371{
Eric Biggers3d204e22018-01-11 23:30:13 -0500372 put_crypt_info(inode->i_crypt_info);
373 inode->i_crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700374}
375EXPORT_SYMBOL(fscrypt_put_encryption_info);