1 /*
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
11 #include <keys/user-type.h>
12 #include <linux/scatterlist.h>
13 #include <linux/ratelimit.h>
14 #include <crypto/aes.h>
15 #include <crypto/sha.h>
16 #include "fscrypt_private.h"
17
18 static struct crypto_shash *essiv_hash_tfm;
19
20 /**
21 * derive_key_aes() - Derive a key using AES-128-ECB
22 * @deriving_key: Encryption key used for derivation.
23 * @source_key: Source key to which to apply derivation.
24 * @derived_raw_key: Derived raw key.
25 *
26 * Return: Zero on success; non-zero otherwise.
27 */
derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],const struct fscrypt_key * source_key,u8 derived_raw_key[FS_MAX_KEY_SIZE])28 static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
29 const struct fscrypt_key *source_key,
30 u8 derived_raw_key[FS_MAX_KEY_SIZE])
31 {
32 int res = 0;
33 struct ablkcipher_request *req = NULL;
34 DECLARE_CRYPTO_WAIT(wait);
35 struct scatterlist src_sg, dst_sg;
36 struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0, 0);
37
38 if (IS_ERR(tfm)) {
39 res = PTR_ERR(tfm);
40 tfm = NULL;
41 goto out;
42 }
43 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
44 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
45 if (!req) {
46 res = -ENOMEM;
47 goto out;
48 }
49 ablkcipher_request_set_callback(req,
50 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
51 crypto_req_done, &wait);
52 res = crypto_ablkcipher_setkey(tfm, deriving_key,
53 FS_AES_128_ECB_KEY_SIZE);
54 if (res < 0)
55 goto out;
56
57 sg_init_one(&src_sg, source_key->raw, source_key->size);
58 sg_init_one(&dst_sg, derived_raw_key, source_key->size);
59 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
60 NULL);
61 res = crypto_wait_req(crypto_ablkcipher_encrypt(req), &wait);
62 out:
63 if (req)
64 ablkcipher_request_free(req);
65 if (tfm)
66 crypto_free_ablkcipher(tfm);
67 return res;
68 }
69
validate_user_key(struct fscrypt_info * crypt_info,struct fscrypt_context * ctx,u8 * raw_key,const char * prefix,int min_keysize)70 static int validate_user_key(struct fscrypt_info *crypt_info,
71 struct fscrypt_context *ctx, u8 *raw_key,
72 const char *prefix, int min_keysize)
73 {
74 char *description;
75 struct key *keyring_key;
76 struct fscrypt_key *master_key;
77 const struct user_key_payload *ukp;
78 int prefix_size = strlen(prefix);
79 int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1;
80 int res;
81
82 /* FIXME: 3.18 causes kernel panic.
83 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
84 FS_KEY_DESCRIPTOR_SIZE,
85 ctx->master_key_descriptor);
86 */
87 description = kmalloc(full_key_len, GFP_NOFS);
88 if (!description)
89 return -ENOMEM;
90
91 memcpy(description, prefix, prefix_size);
92 sprintf(description + prefix_size,
93 "%*phN", FS_KEY_DESCRIPTOR_SIZE,
94 ctx->master_key_descriptor);
95 description[full_key_len - 1] = '\0';
96
97 keyring_key = request_key(&key_type_logon, description, NULL);
98 kfree(description);
99 if (IS_ERR(keyring_key))
100 return PTR_ERR(keyring_key);
101 down_read(&keyring_key->sem);
102
103 if (keyring_key->type != &key_type_logon) {
104 printk_once(KERN_WARNING
105 "%s: key type must be logon\n", __func__);
106 res = -ENOKEY;
107 goto out;
108 }
109 ukp = user_key_payload(keyring_key);
110 if (!ukp) {
111 /* key was revoked before we acquired its semaphore */
112 res = -EKEYREVOKED;
113 goto out;
114 }
115 if (ukp->datalen != sizeof(struct fscrypt_key)) {
116 res = -EINVAL;
117 goto out;
118 }
119 master_key = (struct fscrypt_key *)ukp->data;
120 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
121
122 if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
123 || master_key->size % AES_BLOCK_SIZE != 0) {
124 printk_once(KERN_WARNING
125 "%s: key size incorrect: %d\n",
126 __func__, master_key->size);
127 res = -ENOKEY;
128 goto out;
129 }
130 res = derive_key_aes(ctx->nonce, master_key, raw_key);
131 out:
132 up_read(&keyring_key->sem);
133 key_put(keyring_key);
134 return res;
135 }
136
137 static const struct {
138 const char *cipher_str;
139 int keysize;
140 } available_modes[] = {
141 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
142 FS_AES_256_XTS_KEY_SIZE },
143 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
144 FS_AES_256_CTS_KEY_SIZE },
145 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
146 FS_AES_128_CBC_KEY_SIZE },
147 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
148 FS_AES_128_CTS_KEY_SIZE },
149 };
150
determine_cipher_type(struct fscrypt_info * ci,struct inode * inode,const char ** cipher_str_ret,int * keysize_ret)151 static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
152 const char **cipher_str_ret, int *keysize_ret)
153 {
154 u32 mode;
155
156 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
157 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
158 inode->i_ino,
159 ci->ci_data_mode, ci->ci_filename_mode);
160 return -EINVAL;
161 }
162
163 if (S_ISREG(inode->i_mode)) {
164 mode = ci->ci_data_mode;
165 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
166 mode = ci->ci_filename_mode;
167 } else {
168 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
169 inode->i_ino, (inode->i_mode & S_IFMT));
170 return -EINVAL;
171 }
172
173 *cipher_str_ret = available_modes[mode].cipher_str;
174 *keysize_ret = available_modes[mode].keysize;
175 return 0;
176 }
177
put_crypt_info(struct fscrypt_info * ci)178 static void put_crypt_info(struct fscrypt_info *ci)
179 {
180 if (!ci)
181 return;
182
183 crypto_free_ablkcipher(ci->ci_ctfm);
184 crypto_free_cipher(ci->ci_essiv_tfm);
185 kmem_cache_free(fscrypt_info_cachep, ci);
186 }
187
derive_essiv_salt(const u8 * key,int keysize,u8 * salt)188 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
189 {
190 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
191
192 /* init hash transform on demand */
193 if (unlikely(!tfm)) {
194 struct crypto_shash *prev_tfm;
195
196 tfm = crypto_alloc_shash("sha256", 0, 0);
197 if (IS_ERR(tfm)) {
198 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
199 PTR_ERR(tfm));
200 return PTR_ERR(tfm);
201 }
202 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
203 if (prev_tfm) {
204 crypto_free_shash(tfm);
205 tfm = prev_tfm;
206 }
207 }
208
209 {
210 SHASH_DESC_ON_STACK(desc, tfm);
211 desc->tfm = tfm;
212 desc->flags = 0;
213
214 return crypto_shash_digest(desc, key, keysize, salt);
215 }
216 }
217
init_essiv_generator(struct fscrypt_info * ci,const u8 * raw_key,int keysize)218 static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
219 int keysize)
220 {
221 int err;
222 struct crypto_cipher *essiv_tfm;
223 u8 salt[SHA256_DIGEST_SIZE];
224
225 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
226 if (IS_ERR(essiv_tfm))
227 return PTR_ERR(essiv_tfm);
228
229 ci->ci_essiv_tfm = essiv_tfm;
230
231 err = derive_essiv_salt(raw_key, keysize, salt);
232 if (err)
233 goto out;
234
235 /*
236 * Using SHA256 to derive the salt/key will result in AES-256 being
237 * used for IV generation. File contents encryption will still use the
238 * configured keysize (AES-128) nevertheless.
239 */
240 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
241 if (err)
242 goto out;
243
244 out:
245 memzero_explicit(salt, sizeof(salt));
246 return err;
247 }
248
fscrypt_essiv_cleanup(void)249 void __exit fscrypt_essiv_cleanup(void)
250 {
251 crypto_free_shash(essiv_hash_tfm);
252 }
253
fscrypt_get_encryption_info(struct inode * inode)254 int fscrypt_get_encryption_info(struct inode *inode)
255 {
256 struct fscrypt_info *crypt_info;
257 struct fscrypt_context ctx;
258 struct crypto_ablkcipher *ctfm;
259 const char *cipher_str;
260 int keysize;
261 u8 *raw_key = NULL;
262 int res;
263
264 if (inode->i_crypt_info)
265 return 0;
266
267 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
268 if (res)
269 return res;
270
271 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
272 if (res < 0) {
273 if (!fscrypt_dummy_context_enabled(inode) ||
274 IS_ENCRYPTED(inode))
275 return res;
276 /* Fake up a context for an unencrypted directory */
277 memset(&ctx, 0, sizeof(ctx));
278 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
279 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
280 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
281 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
282 } else if (res != sizeof(ctx)) {
283 return -EINVAL;
284 }
285
286 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
287 return -EINVAL;
288
289 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
290 return -EINVAL;
291
292 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
293 if (!crypt_info)
294 return -ENOMEM;
295
296 crypt_info->ci_flags = ctx.flags;
297 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
298 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
299 crypt_info->ci_ctfm = NULL;
300 crypt_info->ci_essiv_tfm = NULL;
301 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
302 sizeof(crypt_info->ci_master_key));
303
304 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
305 if (res)
306 goto out;
307
308 /*
309 * This cannot be a stack buffer because it is passed to the scatterlist
310 * crypto API as part of key derivation.
311 */
312 res = -ENOMEM;
313 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
314 if (!raw_key)
315 goto out;
316
317 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
318 keysize);
319 if (res && inode->i_sb->s_cop->key_prefix) {
320 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
321 inode->i_sb->s_cop->key_prefix,
322 keysize);
323 if (res2) {
324 if (res2 == -ENOKEY)
325 res = -ENOKEY;
326 goto out;
327 }
328 } else if (res) {
329 goto out;
330 }
331 ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
332 if (!ctfm || IS_ERR(ctfm)) {
333 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
334 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
335 __func__, res, inode->i_ino);
336 goto out;
337 }
338 crypt_info->ci_ctfm = ctfm;
339 crypto_ablkcipher_clear_flags(ctfm, ~0);
340 crypto_ablkcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
341 /*
342 * if the provided key is longer than keysize, we use the first
343 * keysize bytes of the derived key only
344 */
345 res = crypto_ablkcipher_setkey(ctfm, raw_key, keysize);
346 if (res)
347 goto out;
348
349 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) {
353 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
354 __func__, res, inode->i_ino);
355 goto out;
356 }
357 }
358 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
359 crypt_info = NULL;
360 out:
361 if (res == -ENOKEY)
362 res = 0;
363 put_crypt_info(crypt_info);
364 kzfree(raw_key);
365 return res;
366 }
367 EXPORT_SYMBOL(fscrypt_get_encryption_info);
368
fscrypt_put_encryption_info(struct inode * inode,struct fscrypt_info * ci)369 void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
370 {
371 struct fscrypt_info *prev;
372
373 if (ci == NULL)
374 ci = ACCESS_ONCE(inode->i_crypt_info);
375 if (ci == NULL)
376 return;
377
378 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
379 if (prev != ci)
380 return;
381
382 put_crypt_info(ci);
383 }
384 EXPORT_SYMBOL(fscrypt_put_encryption_info);
385