blob: 1c34f0eb125b5b94704f5a4d6d50629fca6322ee [file] [log] [blame]
Michael Halcrowb30ab0e2015-04-12 00:43:56 -04001/*
2 * linux/fs/ext4/crypto.c
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains encryption functions for ext4
7 *
8 * Written by Michael Halcrow, 2014.
9 *
10 * Filename encryption additions
11 * Uday Savagaonkar, 2014
12 * Encryption policy handling additions
13 * Ildar Muslukhov, 2014
14 *
15 * This has not yet undergone a rigorous security audit.
16 *
17 * The usage of AES-XTS should conform to recommendations in NIST
18 * Special Publication 800-38E and IEEE P1619/D16.
19 */
20
21#include <crypto/hash.h>
22#include <crypto/sha.h>
23#include <keys/user-type.h>
24#include <keys/encrypted-type.h>
25#include <linux/crypto.h>
26#include <linux/ecryptfs.h>
27#include <linux/gfp.h>
28#include <linux/kernel.h>
29#include <linux/key.h>
30#include <linux/list.h>
31#include <linux/mempool.h>
32#include <linux/module.h>
33#include <linux/mutex.h>
34#include <linux/random.h>
35#include <linux/scatterlist.h>
36#include <linux/spinlock_types.h>
37
38#include "ext4_extents.h"
39#include "xattr.h"
40
41/* Encryption added and removed here! (L: */
42
43static unsigned int num_prealloc_crypto_pages = 32;
44static unsigned int num_prealloc_crypto_ctxs = 128;
45
46module_param(num_prealloc_crypto_pages, uint, 0444);
47MODULE_PARM_DESC(num_prealloc_crypto_pages,
48 "Number of crypto pages to preallocate");
49module_param(num_prealloc_crypto_ctxs, uint, 0444);
50MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
51 "Number of crypto contexts to preallocate");
52
53static mempool_t *ext4_bounce_page_pool;
54
55static LIST_HEAD(ext4_free_crypto_ctxs);
56static DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
57
Theodore Ts'o8ee037142015-05-18 13:19:47 -040058static struct kmem_cache *ext4_crypto_ctx_cachep;
59struct kmem_cache *ext4_crypt_info_cachep;
60
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040061/**
62 * ext4_release_crypto_ctx() - Releases an encryption context
63 * @ctx: The encryption context to release.
64 *
65 * If the encryption context was allocated from the pre-allocated pool, returns
66 * it to that pool. Else, frees it.
67 *
68 * If there's a bounce page in the context, this frees that.
69 */
70void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
71{
72 unsigned long flags;
73
74 if (ctx->bounce_page) {
75 if (ctx->flags & EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL)
76 __free_page(ctx->bounce_page);
77 else
78 mempool_free(ctx->bounce_page, ext4_bounce_page_pool);
79 ctx->bounce_page = NULL;
80 }
81 ctx->control_page = NULL;
82 if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
83 if (ctx->tfm)
84 crypto_free_tfm(ctx->tfm);
Theodore Ts'o8ee037142015-05-18 13:19:47 -040085 kmem_cache_free(ext4_crypto_ctx_cachep, ctx);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040086 } else {
87 spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
88 list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
89 spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
90 }
91}
92
93/**
Michael Halcrowb30ab0e2015-04-12 00:43:56 -040094 * ext4_get_crypto_ctx() - Gets an encryption context
95 * @inode: The inode for which we are doing the crypto
96 *
97 * Allocates and initializes an encryption context.
98 *
99 * Return: An allocated and initialized encryption context on success; error
100 * value or NULL otherwise.
101 */
102struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
103{
104 struct ext4_crypto_ctx *ctx = NULL;
105 int res = 0;
106 unsigned long flags;
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400107 struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400108
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400109 BUG_ON(ci == NULL);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400110
111 /*
112 * We first try getting the ctx from a free list because in
113 * the common case the ctx will have an allocated and
114 * initialized crypto tfm, so it's probably a worthwhile
115 * optimization. For the bounce page, we first try getting it
116 * from the kernel allocator because that's just about as fast
117 * as getting it from a list and because a cache of free pages
118 * should generally be a "last resort" option for a filesystem
119 * to be able to do its job.
120 */
121 spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
122 ctx = list_first_entry_or_null(&ext4_free_crypto_ctxs,
123 struct ext4_crypto_ctx, free_list);
124 if (ctx)
125 list_del(&ctx->free_list);
126 spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
127 if (!ctx) {
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400128 ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
129 if (!ctx) {
130 res = -ENOMEM;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400131 goto out;
132 }
133 ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
134 } else {
135 ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
136 }
137
138 /* Allocate a new Crypto API context if we don't already have
139 * one or if it isn't the right mode. */
Theodore Ts'oe2881b1b2015-05-18 13:16:47 -0400140 BUG_ON(ci->ci_mode == EXT4_ENCRYPTION_MODE_INVALID);
141 if (ctx->tfm && (ctx->mode != ci->ci_mode)) {
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400142 crypto_free_tfm(ctx->tfm);
143 ctx->tfm = NULL;
144 ctx->mode = EXT4_ENCRYPTION_MODE_INVALID;
145 }
146 if (!ctx->tfm) {
Theodore Ts'oe2881b1b2015-05-18 13:16:47 -0400147 switch (ci->ci_mode) {
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400148 case EXT4_ENCRYPTION_MODE_AES_256_XTS:
149 ctx->tfm = crypto_ablkcipher_tfm(
150 crypto_alloc_ablkcipher("xts(aes)", 0, 0));
151 break;
152 case EXT4_ENCRYPTION_MODE_AES_256_GCM:
153 /* TODO(mhalcrow): AEAD w/ gcm(aes);
154 * crypto_aead_setauthsize() */
155 ctx->tfm = ERR_PTR(-ENOTSUPP);
156 break;
157 default:
158 BUG();
159 }
160 if (IS_ERR_OR_NULL(ctx->tfm)) {
161 res = PTR_ERR(ctx->tfm);
162 ctx->tfm = NULL;
163 goto out;
164 }
Theodore Ts'oe2881b1b2015-05-18 13:16:47 -0400165 ctx->mode = ci->ci_mode;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400166 }
Theodore Ts'oe2881b1b2015-05-18 13:16:47 -0400167 BUG_ON(ci->ci_size != ext4_encryption_key_size(ci->ci_mode));
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400168
169 /* There shouldn't be a bounce page attached to the crypto
170 * context at this point. */
171 BUG_ON(ctx->bounce_page);
172
173out:
174 if (res) {
175 if (!IS_ERR_OR_NULL(ctx))
176 ext4_release_crypto_ctx(ctx);
177 ctx = ERR_PTR(res);
178 }
179 return ctx;
180}
181
182struct workqueue_struct *ext4_read_workqueue;
183static DEFINE_MUTEX(crypto_init);
184
185/**
186 * ext4_exit_crypto() - Shutdown the ext4 encryption system
187 */
188void ext4_exit_crypto(void)
189{
190 struct ext4_crypto_ctx *pos, *n;
191
192 list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list) {
193 if (pos->bounce_page) {
194 if (pos->flags &
195 EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) {
196 __free_page(pos->bounce_page);
197 } else {
198 mempool_free(pos->bounce_page,
199 ext4_bounce_page_pool);
200 }
201 }
202 if (pos->tfm)
203 crypto_free_tfm(pos->tfm);
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400204 kmem_cache_free(ext4_crypto_ctx_cachep, pos);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400205 }
206 INIT_LIST_HEAD(&ext4_free_crypto_ctxs);
207 if (ext4_bounce_page_pool)
208 mempool_destroy(ext4_bounce_page_pool);
209 ext4_bounce_page_pool = NULL;
210 if (ext4_read_workqueue)
211 destroy_workqueue(ext4_read_workqueue);
212 ext4_read_workqueue = NULL;
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400213 if (ext4_crypto_ctx_cachep)
214 kmem_cache_destroy(ext4_crypto_ctx_cachep);
215 ext4_crypto_ctx_cachep = NULL;
216 if (ext4_crypt_info_cachep)
217 kmem_cache_destroy(ext4_crypt_info_cachep);
218 ext4_crypt_info_cachep = NULL;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400219}
220
221/**
222 * ext4_init_crypto() - Set up for ext4 encryption.
223 *
224 * We only call this when we start accessing encrypted files, since it
225 * results in memory getting allocated that wouldn't otherwise be used.
226 *
227 * Return: Zero on success, non-zero otherwise.
228 */
229int ext4_init_crypto(void)
230{
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400231 int i, res = -ENOMEM;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400232
233 mutex_lock(&crypto_init);
234 if (ext4_read_workqueue)
235 goto already_initialized;
236 ext4_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400237 if (!ext4_read_workqueue)
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400238 goto fail;
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400239
240 ext4_crypto_ctx_cachep = KMEM_CACHE(ext4_crypto_ctx,
241 SLAB_RECLAIM_ACCOUNT);
242 if (!ext4_crypto_ctx_cachep)
243 goto fail;
244
245 ext4_crypt_info_cachep = KMEM_CACHE(ext4_crypt_info,
246 SLAB_RECLAIM_ACCOUNT);
247 if (!ext4_crypt_info_cachep)
248 goto fail;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400249
250 for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
251 struct ext4_crypto_ctx *ctx;
252
Theodore Ts'o8ee037142015-05-18 13:19:47 -0400253 ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
254 if (!ctx) {
255 res = -ENOMEM;
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400256 goto fail;
257 }
258 list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
259 }
260
261 ext4_bounce_page_pool =
262 mempool_create_page_pool(num_prealloc_crypto_pages, 0);
263 if (!ext4_bounce_page_pool) {
264 res = -ENOMEM;
265 goto fail;
266 }
267already_initialized:
268 mutex_unlock(&crypto_init);
269 return 0;
270fail:
271 ext4_exit_crypto();
272 mutex_unlock(&crypto_init);
273 return res;
274}
275
276void ext4_restore_control_page(struct page *data_page)
277{
278 struct ext4_crypto_ctx *ctx =
279 (struct ext4_crypto_ctx *)page_private(data_page);
280
281 set_page_private(data_page, (unsigned long)NULL);
282 ClearPagePrivate(data_page);
283 unlock_page(data_page);
284 ext4_release_crypto_ctx(ctx);
285}
286
287/**
288 * ext4_crypt_complete() - The completion callback for page encryption
289 * @req: The asynchronous encryption request context
290 * @res: The result of the encryption operation
291 */
292static void ext4_crypt_complete(struct crypto_async_request *req, int res)
293{
294 struct ext4_completion_result *ecr = req->data;
295
296 if (res == -EINPROGRESS)
297 return;
298 ecr->res = res;
299 complete(&ecr->completion);
300}
301
302typedef enum {
303 EXT4_DECRYPT = 0,
304 EXT4_ENCRYPT,
305} ext4_direction_t;
306
307static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
308 struct inode *inode,
309 ext4_direction_t rw,
310 pgoff_t index,
311 struct page *src_page,
312 struct page *dest_page)
313
314{
315 u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
316 struct ablkcipher_request *req = NULL;
317 DECLARE_EXT4_COMPLETION_RESULT(ecr);
318 struct scatterlist dst, src;
319 struct ext4_inode_info *ei = EXT4_I(inode);
320 struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm);
321 int res = 0;
322
323 BUG_ON(!ctx->tfm);
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400324 BUG_ON(ctx->mode != ei->i_crypt_info->ci_mode);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400325
326 if (ctx->mode != EXT4_ENCRYPTION_MODE_AES_256_XTS) {
327 printk_ratelimited(KERN_ERR
328 "%s: unsupported crypto algorithm: %d\n",
329 __func__, ctx->mode);
330 return -ENOTSUPP;
331 }
332
333 crypto_ablkcipher_clear_flags(atfm, ~0);
334 crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
335
Theodore Ts'ob7236e22015-05-18 13:17:47 -0400336 res = crypto_ablkcipher_setkey(atfm, ei->i_crypt_info->ci_raw,
337 ei->i_crypt_info->ci_size);
Michael Halcrowb30ab0e2015-04-12 00:43:56 -0400338 if (res) {
339 printk_ratelimited(KERN_ERR
340 "%s: crypto_ablkcipher_setkey() failed\n",
341 __func__);
342 return res;
343 }
344 req = ablkcipher_request_alloc(atfm, GFP_NOFS);
345 if (!req) {
346 printk_ratelimited(KERN_ERR
347 "%s: crypto_request_alloc() failed\n",
348 __func__);
349 return -ENOMEM;
350 }
351 ablkcipher_request_set_callback(
352 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
353 ext4_crypt_complete, &ecr);
354
355 BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE < sizeof(index));
356 memcpy(xts_tweak, &index, sizeof(index));
357 memset(&xts_tweak[sizeof(index)], 0,
358 EXT4_XTS_TWEAK_SIZE - sizeof(index));
359
360 sg_init_table(&dst, 1);
361 sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0);
362 sg_init_table(&src, 1);
363 sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0);
364 ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE,
365 xts_tweak);
366 if (rw == EXT4_DECRYPT)
367 res = crypto_ablkcipher_decrypt(req);
368 else
369 res = crypto_ablkcipher_encrypt(req);
370 if (res == -EINPROGRESS || res == -EBUSY) {
371 BUG_ON(req->base.data != &ecr);
372 wait_for_completion(&ecr.completion);
373 res = ecr.res;
374 }
375 ablkcipher_request_free(req);
376 if (res) {
377 printk_ratelimited(
378 KERN_ERR
379 "%s: crypto_ablkcipher_encrypt() returned %d\n",
380 __func__, res);
381 return res;
382 }
383 return 0;
384}
385
386/**
387 * ext4_encrypt() - Encrypts a page
388 * @inode: The inode for which the encryption should take place
389 * @plaintext_page: The page to encrypt. Must be locked.
390 *
391 * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
392 * encryption context.
393 *
394 * Called on the page write path. The caller must call
395 * ext4_restore_control_page() on the returned ciphertext page to
396 * release the bounce buffer and the encryption context.
397 *
398 * Return: An allocated page with the encrypted content on success. Else, an
399 * error value or NULL.
400 */
401struct page *ext4_encrypt(struct inode *inode,
402 struct page *plaintext_page)
403{
404 struct ext4_crypto_ctx *ctx;
405 struct page *ciphertext_page = NULL;
406 int err;
407
408 BUG_ON(!PageLocked(plaintext_page));
409
410 ctx = ext4_get_crypto_ctx(inode);
411 if (IS_ERR(ctx))
412 return (struct page *) ctx;
413
414 /* The encryption operation will require a bounce page. */
415 ciphertext_page = alloc_page(GFP_NOFS);
416 if (!ciphertext_page) {
417 /* This is a potential bottleneck, but at least we'll have
418 * forward progress. */
419 ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
420 GFP_NOFS);
421 if (WARN_ON_ONCE(!ciphertext_page)) {
422 ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
423 GFP_NOFS | __GFP_WAIT);
424 }
425 ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
426 } else {
427 ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
428 }
429 ctx->bounce_page = ciphertext_page;
430 ctx->control_page = plaintext_page;
431 err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, plaintext_page->index,
432 plaintext_page, ciphertext_page);
433 if (err) {
434 ext4_release_crypto_ctx(ctx);
435 return ERR_PTR(err);
436 }
437 SetPagePrivate(ciphertext_page);
438 set_page_private(ciphertext_page, (unsigned long)ctx);
439 lock_page(ciphertext_page);
440 return ciphertext_page;
441}
442
443/**
444 * ext4_decrypt() - Decrypts a page in-place
445 * @ctx: The encryption context.
446 * @page: The page to decrypt. Must be locked.
447 *
448 * Decrypts page in-place using the ctx encryption context.
449 *
450 * Called from the read completion callback.
451 *
452 * Return: Zero on success, non-zero otherwise.
453 */
454int ext4_decrypt(struct ext4_crypto_ctx *ctx, struct page *page)
455{
456 BUG_ON(!PageLocked(page));
457
458 return ext4_page_crypto(ctx, page->mapping->host,
459 EXT4_DECRYPT, page->index, page, page);
460}
461
462/*
463 * Convenience function which takes care of allocating and
464 * deallocating the encryption context
465 */
466int ext4_decrypt_one(struct inode *inode, struct page *page)
467{
468 int ret;
469
470 struct ext4_crypto_ctx *ctx = ext4_get_crypto_ctx(inode);
471
472 if (!ctx)
473 return -ENOMEM;
474 ret = ext4_decrypt(ctx, page);
475 ext4_release_crypto_ctx(ctx);
476 return ret;
477}
478
479int ext4_encrypted_zeroout(struct inode *inode, struct ext4_extent *ex)
480{
481 struct ext4_crypto_ctx *ctx;
482 struct page *ciphertext_page = NULL;
483 struct bio *bio;
484 ext4_lblk_t lblk = ex->ee_block;
485 ext4_fsblk_t pblk = ext4_ext_pblock(ex);
486 unsigned int len = ext4_ext_get_actual_len(ex);
487 int err = 0;
488
489 BUG_ON(inode->i_sb->s_blocksize != PAGE_CACHE_SIZE);
490
491 ctx = ext4_get_crypto_ctx(inode);
492 if (IS_ERR(ctx))
493 return PTR_ERR(ctx);
494
495 ciphertext_page = alloc_page(GFP_NOFS);
496 if (!ciphertext_page) {
497 /* This is a potential bottleneck, but at least we'll have
498 * forward progress. */
499 ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
500 GFP_NOFS);
501 if (WARN_ON_ONCE(!ciphertext_page)) {
502 ciphertext_page = mempool_alloc(ext4_bounce_page_pool,
503 GFP_NOFS | __GFP_WAIT);
504 }
505 ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
506 } else {
507 ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
508 }
509 ctx->bounce_page = ciphertext_page;
510
511 while (len--) {
512 err = ext4_page_crypto(ctx, inode, EXT4_ENCRYPT, lblk,
513 ZERO_PAGE(0), ciphertext_page);
514 if (err)
515 goto errout;
516
517 bio = bio_alloc(GFP_KERNEL, 1);
518 if (!bio) {
519 err = -ENOMEM;
520 goto errout;
521 }
522 bio->bi_bdev = inode->i_sb->s_bdev;
523 bio->bi_iter.bi_sector = pblk;
524 err = bio_add_page(bio, ciphertext_page,
525 inode->i_sb->s_blocksize, 0);
526 if (err) {
527 bio_put(bio);
528 goto errout;
529 }
530 err = submit_bio_wait(WRITE, bio);
531 if (err)
532 goto errout;
533 }
534 err = 0;
535errout:
536 ext4_release_crypto_ctx(ctx);
537 return err;
538}
539
540bool ext4_valid_contents_enc_mode(uint32_t mode)
541{
542 return (mode == EXT4_ENCRYPTION_MODE_AES_256_XTS);
543}
544
545/**
546 * ext4_validate_encryption_key_size() - Validate the encryption key size
547 * @mode: The key mode.
548 * @size: The key size to validate.
549 *
550 * Return: The validated key size for @mode. Zero if invalid.
551 */
552uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
553{
554 if (size == ext4_encryption_key_size(mode))
555 return size;
556 return 0;
557}