blob: d09df8f751df047d2c5d5b826e61cf2f295c6c50 [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
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070022/**
23 * derive_key_aes() - Derive a key using AES-128-ECB
24 * @deriving_key: Encryption key used for derivation.
25 * @source_key: Source key to which to apply derivation.
Daniel Walterb7e7cf72017-06-19 09:27:58 +020026 * @derived_raw_key: Derived raw key.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070027 *
28 * Return: Zero on success; non-zero otherwise.
29 */
30static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
Daniel Walterb7e7cf72017-06-19 09:27:58 +020031 const struct fscrypt_key *source_key,
32 u8 derived_raw_key[FS_MAX_KEY_SIZE])
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070033{
34 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070035 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010036 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070038 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070039
40 if (IS_ERR(tfm)) {
41 res = PTR_ERR(tfm);
42 tfm = NULL;
43 goto out;
44 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070045 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
46 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070047 if (!req) {
48 res = -ENOMEM;
49 goto out;
50 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070051 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070052 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010053 crypto_req_done, &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070054 res = crypto_skcipher_setkey(tfm, deriving_key,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070055 FS_AES_128_ECB_KEY_SIZE);
56 if (res < 0)
57 goto out;
58
Daniel Walterb7e7cf72017-06-19 09:27:58 +020059 sg_init_one(&src_sg, source_key->raw, source_key->size);
60 sg_init_one(&dst_sg, derived_raw_key, source_key->size);
61 skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
62 NULL);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010063 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070064out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070065 skcipher_request_free(req);
66 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 return res;
68}
69
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070070static int validate_user_key(struct fscrypt_info *crypt_info,
71 struct fscrypt_context *ctx, u8 *raw_key,
Daniel Walterb7e7cf72017-06-19 09:27:58 +020072 const char *prefix, int min_keysize)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070073{
Eric Biggersa5d431e2017-01-05 13:51:18 -080074 char *description;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070075 struct key *keyring_key;
76 struct fscrypt_key *master_key;
77 const struct user_key_payload *ukp;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070078 int res;
79
Eric Biggersa5d431e2017-01-05 13:51:18 -080080 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
81 FS_KEY_DESCRIPTOR_SIZE,
82 ctx->master_key_descriptor);
83 if (!description)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070084 return -ENOMEM;
85
Eric Biggersa5d431e2017-01-05 13:51:18 -080086 keyring_key = request_key(&key_type_logon, description, NULL);
87 kfree(description);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070088 if (IS_ERR(keyring_key))
89 return PTR_ERR(keyring_key);
Eric Biggers1b53cf92017-02-21 15:07:11 -080090 down_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070091
92 if (keyring_key->type != &key_type_logon) {
93 printk_once(KERN_WARNING
94 "%s: key type must be logon\n", __func__);
95 res = -ENOKEY;
96 goto out;
97 }
David Howells0837e492017-03-01 15:11:23 +000098 ukp = user_key_payload_locked(keyring_key);
Eric Biggersd60b5b72017-10-09 12:46:18 -070099 if (!ukp) {
100 /* key was revoked before we acquired its semaphore */
101 res = -EKEYREVOKED;
102 goto out;
103 }
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700104 if (ukp->datalen != sizeof(struct fscrypt_key)) {
105 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700106 goto out;
107 }
108 master_key = (struct fscrypt_key *)ukp->data;
109 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
110
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200111 if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
112 || master_key->size % AES_BLOCK_SIZE != 0) {
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700113 printk_once(KERN_WARNING
114 "%s: key size incorrect: %d\n",
115 __func__, master_key->size);
116 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700117 goto out;
118 }
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200119 res = derive_key_aes(ctx->nonce, master_key, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700120out:
Eric Biggers1b53cf92017-02-21 15:07:11 -0800121 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700122 key_put(keyring_key);
123 return res;
124}
125
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200126static const struct {
127 const char *cipher_str;
128 int keysize;
129} available_modes[] = {
130 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
131 FS_AES_256_XTS_KEY_SIZE },
132 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
133 FS_AES_256_CTS_KEY_SIZE },
134 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
135 FS_AES_128_CBC_KEY_SIZE },
136 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
137 FS_AES_128_CTS_KEY_SIZE },
138};
139
Eric Biggers8f398502016-09-15 13:32:11 -0400140static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
141 const char **cipher_str_ret, int *keysize_ret)
142{
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200143 u32 mode;
144
145 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
146 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
147 inode->i_ino,
148 ci->ci_data_mode, ci->ci_filename_mode);
149 return -EINVAL;
150 }
151
Eric Biggers8f398502016-09-15 13:32:11 -0400152 if (S_ISREG(inode->i_mode)) {
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200153 mode = ci->ci_data_mode;
154 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
155 mode = ci->ci_filename_mode;
156 } else {
157 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
158 inode->i_ino, (inode->i_mode & S_IFMT));
159 return -EINVAL;
Eric Biggers8f398502016-09-15 13:32:11 -0400160 }
161
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200162 *cipher_str_ret = available_modes[mode].cipher_str;
163 *keysize_ret = available_modes[mode].keysize;
164 return 0;
Eric Biggers8f398502016-09-15 13:32:11 -0400165}
166
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700167static void put_crypt_info(struct fscrypt_info *ci)
168{
169 if (!ci)
170 return;
171
Linus Torvaldsd4075742016-03-21 11:03:02 -0700172 crypto_free_skcipher(ci->ci_ctfm);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200173 crypto_free_cipher(ci->ci_essiv_tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700174 kmem_cache_free(fscrypt_info_cachep, ci);
175}
176
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200177static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
178{
179 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
180
181 /* init hash transform on demand */
182 if (unlikely(!tfm)) {
183 struct crypto_shash *prev_tfm;
184
185 tfm = crypto_alloc_shash("sha256", 0, 0);
186 if (IS_ERR(tfm)) {
187 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
188 PTR_ERR(tfm));
189 return PTR_ERR(tfm);
190 }
191 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
192 if (prev_tfm) {
193 crypto_free_shash(tfm);
194 tfm = prev_tfm;
195 }
196 }
197
198 {
199 SHASH_DESC_ON_STACK(desc, tfm);
200 desc->tfm = tfm;
201 desc->flags = 0;
202
203 return crypto_shash_digest(desc, key, keysize, salt);
204 }
205}
206
207static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
208 int keysize)
209{
210 int err;
211 struct crypto_cipher *essiv_tfm;
212 u8 salt[SHA256_DIGEST_SIZE];
213
214 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
215 if (IS_ERR(essiv_tfm))
216 return PTR_ERR(essiv_tfm);
217
218 ci->ci_essiv_tfm = essiv_tfm;
219
220 err = derive_essiv_salt(raw_key, keysize, salt);
221 if (err)
222 goto out;
223
224 /*
225 * Using SHA256 to derive the salt/key will result in AES-256 being
226 * used for IV generation. File contents encryption will still use the
227 * configured keysize (AES-128) nevertheless.
228 */
229 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
230 if (err)
231 goto out;
232
233out:
234 memzero_explicit(salt, sizeof(salt));
235 return err;
236}
237
238void __exit fscrypt_essiv_cleanup(void)
239{
240 crypto_free_shash(essiv_hash_tfm);
241}
242
Eric Biggers1b53cf92017-02-21 15:07:11 -0800243int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700244{
245 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700246 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700247 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700248 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400249 int keysize;
Eric Biggersa6e08912016-11-13 20:41:09 -0500250 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700251 int res;
252
Eric Biggers1b53cf92017-02-21 15:07:11 -0800253 if (inode->i_crypt_info)
254 return 0;
255
David Gstirf32d7ac2016-12-06 23:53:57 +0100256 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700257 if (res)
258 return res;
259
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700260 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
261 if (res < 0) {
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500262 if (!fscrypt_dummy_context_enabled(inode) ||
Eric Biggerse0428a22017-10-09 12:15:36 -0700263 IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 return res;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500265 /* Fake up a context for an unencrypted directory */
266 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400267 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700268 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
269 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500270 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700271 } else if (res != sizeof(ctx)) {
272 return -EINVAL;
273 }
Eric Biggers8f398502016-09-15 13:32:11 -0400274
275 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
276 return -EINVAL;
277
278 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
279 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280
281 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
282 if (!crypt_info)
283 return -ENOMEM;
284
285 crypt_info->ci_flags = ctx.flags;
286 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
287 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
288 crypt_info->ci_ctfm = NULL;
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200289 crypt_info->ci_essiv_tfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700290 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
291 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700292
Eric Biggers8f398502016-09-15 13:32:11 -0400293 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
294 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700295 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400296
Eric Biggersa6e08912016-11-13 20:41:09 -0500297 /*
298 * This cannot be a stack buffer because it is passed to the scatterlist
299 * crypto API as part of key derivation.
300 */
301 res = -ENOMEM;
302 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
303 if (!raw_key)
304 goto out;
305
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200306 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
307 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700308 if (res && inode->i_sb->s_cop->key_prefix) {
Eric Biggersa5d431e2017-01-05 13:51:18 -0800309 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200310 inode->i_sb->s_cop->key_prefix,
311 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700312 if (res2) {
313 if (res2 == -ENOKEY)
314 res = -ENOKEY;
315 goto out;
316 }
317 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700318 goto out;
319 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700320 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Eric Biggers99194792018-04-30 15:51:37 -0700321 if (IS_ERR(ctfm)) {
322 res = PTR_ERR(ctfm);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200323 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
324 __func__, res, inode->i_ino);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700325 goto out;
326 }
327 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700328 crypto_skcipher_clear_flags(ctfm, ~0);
329 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200330 /*
331 * if the provided key is longer than keysize, we use the first
332 * keysize bytes of the derived key only
333 */
Eric Biggers8f398502016-09-15 13:32:11 -0400334 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700335 if (res)
336 goto out;
337
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200338 if (S_ISREG(inode->i_mode) &&
339 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
340 res = init_essiv_generator(crypt_info, raw_key, keysize);
341 if (res) {
342 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
343 __func__, res, inode->i_ino);
344 goto out;
345 }
346 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800347 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
348 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700349out:
350 if (res == -ENOKEY)
351 res = 0;
352 put_crypt_info(crypt_info);
Eric Biggersa6e08912016-11-13 20:41:09 -0500353 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700354 return res;
355}
Eric Biggers1b53cf92017-02-21 15:07:11 -0800356EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700357
Eric Biggers3d204e22018-01-11 23:30:13 -0500358void fscrypt_put_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700359{
Eric Biggers3d204e22018-01-11 23:30:13 -0500360 put_crypt_info(inode->i_crypt_info);
361 inode->i_crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700362}
363EXPORT_SYMBOL(fscrypt_put_encryption_info);