blob: 7f1ee9e8fc70c45bcd5a1964a5159a3e0ab5ec02 [file] [log] [blame]
Jaegeuk Kim57e50552015-04-20 19:52:47 -07001/*
2 * linux/fs/f2fs/crypto.c
3 *
4 * Copied from linux/fs/ext4/crypto.c
5 *
6 * Copyright (C) 2015, Google, Inc.
7 * Copyright (C) 2015, Motorola Mobility
8 *
9 * This contains encryption functions for f2fs
10 *
11 * Written by Michael Halcrow, 2014.
12 *
13 * Filename encryption additions
14 * Uday Savagaonkar, 2014
15 * Encryption policy handling additions
16 * Ildar Muslukhov, 2014
17 * Remove ext4_encrypted_zeroout(),
18 * add f2fs_restore_and_release_control_page()
19 * Jaegeuk Kim, 2015.
20 *
21 * This has not yet undergone a rigorous security audit.
22 *
23 * The usage of AES-XTS should conform to recommendations in NIST
24 * Special Publication 800-38E and IEEE P1619/D16.
25 */
26#include <crypto/hash.h>
27#include <crypto/sha.h>
28#include <keys/user-type.h>
29#include <keys/encrypted-type.h>
30#include <linux/crypto.h>
31#include <linux/ecryptfs.h>
32#include <linux/gfp.h>
33#include <linux/kernel.h>
34#include <linux/key.h>
35#include <linux/list.h>
36#include <linux/mempool.h>
37#include <linux/module.h>
38#include <linux/mutex.h>
39#include <linux/random.h>
40#include <linux/scatterlist.h>
41#include <linux/spinlock_types.h>
42#include <linux/f2fs_fs.h>
43#include <linux/ratelimit.h>
44#include <linux/bio.h>
45
46#include "f2fs.h"
47#include "xattr.h"
48
49/* Encryption added and removed here! (L: */
50
51static unsigned int num_prealloc_crypto_pages = 32;
52static unsigned int num_prealloc_crypto_ctxs = 128;
53
54module_param(num_prealloc_crypto_pages, uint, 0444);
55MODULE_PARM_DESC(num_prealloc_crypto_pages,
56 "Number of crypto pages to preallocate");
57module_param(num_prealloc_crypto_ctxs, uint, 0444);
58MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
59 "Number of crypto contexts to preallocate");
60
61static mempool_t *f2fs_bounce_page_pool;
62
63static LIST_HEAD(f2fs_free_crypto_ctxs);
64static DEFINE_SPINLOCK(f2fs_crypto_ctx_lock);
65
66struct workqueue_struct *f2fs_read_workqueue;
67static DEFINE_MUTEX(crypto_init);
68
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -070069static struct kmem_cache *f2fs_crypto_ctx_cachep;
70struct kmem_cache *f2fs_crypt_info_cachep;
71
Jaegeuk Kim57e50552015-04-20 19:52:47 -070072/**
73 * f2fs_release_crypto_ctx() - Releases an encryption context
74 * @ctx: The encryption context to release.
75 *
76 * If the encryption context was allocated from the pre-allocated pool, returns
77 * it to that pool. Else, frees it.
78 *
79 * If there's a bounce page in the context, this frees that.
80 */
81void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *ctx)
82{
83 unsigned long flags;
84
Jaegeuk Kimca40b032015-05-12 13:40:20 -070085 if (ctx->flags & F2FS_WRITE_PATH_FL && ctx->w.bounce_page) {
Jaegeuk Kim57e50552015-04-20 19:52:47 -070086 if (ctx->flags & F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL)
Jaegeuk Kimca40b032015-05-12 13:40:20 -070087 __free_page(ctx->w.bounce_page);
Jaegeuk Kim57e50552015-04-20 19:52:47 -070088 else
Jaegeuk Kimca40b032015-05-12 13:40:20 -070089 mempool_free(ctx->w.bounce_page, f2fs_bounce_page_pool);
90 ctx->w.bounce_page = NULL;
Jaegeuk Kim57e50552015-04-20 19:52:47 -070091 }
Jaegeuk Kimca40b032015-05-12 13:40:20 -070092 ctx->w.control_page = NULL;
Jaegeuk Kim57e50552015-04-20 19:52:47 -070093 if (ctx->flags & F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL) {
94 if (ctx->tfm)
95 crypto_free_tfm(ctx->tfm);
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -070096 kmem_cache_free(f2fs_crypto_ctx_cachep, ctx);
Jaegeuk Kim57e50552015-04-20 19:52:47 -070097 } else {
98 spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags);
99 list_add(&ctx->free_list, &f2fs_free_crypto_ctxs);
100 spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags);
101 }
102}
103
104/**
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700105 * f2fs_get_crypto_ctx() - Gets an encryption context
106 * @inode: The inode for which we are doing the crypto
107 *
108 * Allocates and initializes an encryption context.
109 *
110 * Return: An allocated and initialized encryption context on success; error
111 * value or NULL otherwise.
112 */
113struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *inode)
114{
115 struct f2fs_crypto_ctx *ctx = NULL;
116 int res = 0;
117 unsigned long flags;
118 struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
119
Jaegeuk Kimedf3fb82015-05-05 20:20:29 -0700120 if (ci == NULL)
121 return ERR_PTR(-EACCES);
122
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700123 /*
124 * We first try getting the ctx from a free list because in
125 * the common case the ctx will have an allocated and
126 * initialized crypto tfm, so it's probably a worthwhile
127 * optimization. For the bounce page, we first try getting it
128 * from the kernel allocator because that's just about as fast
129 * as getting it from a list and because a cache of free pages
130 * should generally be a "last resort" option for a filesystem
131 * to be able to do its job.
132 */
133 spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags);
134 ctx = list_first_entry_or_null(&f2fs_free_crypto_ctxs,
135 struct f2fs_crypto_ctx, free_list);
136 if (ctx)
137 list_del(&ctx->free_list);
138 spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags);
139 if (!ctx) {
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700140 ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_NOFS);
141 if (!ctx) {
142 res = -ENOMEM;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700143 goto out;
144 }
145 ctx->flags |= F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
146 } else {
147 ctx->flags &= ~F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
148 }
Jaegeuk Kimca40b032015-05-12 13:40:20 -0700149 ctx->flags &= ~F2FS_WRITE_PATH_FL;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700150
151 /*
152 * Allocate a new Crypto API context if we don't already have
153 * one or if it isn't the right mode.
154 */
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700155 if (ctx->tfm && (ctx->mode != ci->ci_data_mode)) {
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700156 crypto_free_tfm(ctx->tfm);
157 ctx->tfm = NULL;
158 ctx->mode = F2FS_ENCRYPTION_MODE_INVALID;
159 }
160 if (!ctx->tfm) {
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700161 switch (ci->ci_data_mode) {
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700162 case F2FS_ENCRYPTION_MODE_AES_256_XTS:
163 ctx->tfm = crypto_ablkcipher_tfm(
164 crypto_alloc_ablkcipher("xts(aes)", 0, 0));
165 break;
166 case F2FS_ENCRYPTION_MODE_AES_256_GCM:
167 /*
168 * TODO(mhalcrow): AEAD w/ gcm(aes);
169 * crypto_aead_setauthsize()
170 */
171 ctx->tfm = ERR_PTR(-ENOTSUPP);
172 break;
173 default:
174 BUG();
175 }
176 if (IS_ERR_OR_NULL(ctx->tfm)) {
177 res = PTR_ERR(ctx->tfm);
178 ctx->tfm = NULL;
179 goto out;
180 }
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700181 ctx->mode = ci->ci_data_mode;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700182 }
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700183 BUG_ON(ci->ci_size != f2fs_encryption_key_size(ci->ci_data_mode));
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700184
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700185out:
186 if (res) {
187 if (!IS_ERR_OR_NULL(ctx))
188 f2fs_release_crypto_ctx(ctx);
189 ctx = ERR_PTR(res);
190 }
191 return ctx;
192}
193
194/*
195 * Call f2fs_decrypt on every single page, reusing the encryption
196 * context.
197 */
198static void completion_pages(struct work_struct *work)
199{
200 struct f2fs_crypto_ctx *ctx =
Jaegeuk Kimca40b032015-05-12 13:40:20 -0700201 container_of(work, struct f2fs_crypto_ctx, r.work);
202 struct bio *bio = ctx->r.bio;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700203 struct bio_vec *bv;
204 int i;
205
206 bio_for_each_segment_all(bv, bio, i) {
207 struct page *page = bv->bv_page;
208 int ret = f2fs_decrypt(ctx, page);
209
210 if (ret) {
211 WARN_ON_ONCE(1);
212 SetPageError(page);
213 } else
214 SetPageUptodate(page);
215 unlock_page(page);
216 }
217 f2fs_release_crypto_ctx(ctx);
218 bio_put(bio);
219}
220
221void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *ctx, struct bio *bio)
222{
Jaegeuk Kimca40b032015-05-12 13:40:20 -0700223 INIT_WORK(&ctx->r.work, completion_pages);
224 ctx->r.bio = bio;
225 queue_work(f2fs_read_workqueue, &ctx->r.work);
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700226}
227
228/**
229 * f2fs_exit_crypto() - Shutdown the f2fs encryption system
230 */
231void f2fs_exit_crypto(void)
232{
233 struct f2fs_crypto_ctx *pos, *n;
234
235 list_for_each_entry_safe(pos, n, &f2fs_free_crypto_ctxs, free_list) {
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700236 if (pos->tfm)
237 crypto_free_tfm(pos->tfm);
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700238 kmem_cache_free(f2fs_crypto_ctx_cachep, pos);
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700239 }
240 INIT_LIST_HEAD(&f2fs_free_crypto_ctxs);
241 if (f2fs_bounce_page_pool)
242 mempool_destroy(f2fs_bounce_page_pool);
243 f2fs_bounce_page_pool = NULL;
244 if (f2fs_read_workqueue)
245 destroy_workqueue(f2fs_read_workqueue);
246 f2fs_read_workqueue = NULL;
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700247 if (f2fs_crypto_ctx_cachep)
248 kmem_cache_destroy(f2fs_crypto_ctx_cachep);
249 f2fs_crypto_ctx_cachep = NULL;
250 if (f2fs_crypt_info_cachep)
251 kmem_cache_destroy(f2fs_crypt_info_cachep);
252 f2fs_crypt_info_cachep = NULL;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700253}
254
255/**
256 * f2fs_init_crypto() - Set up for f2fs encryption.
257 *
258 * We only call this when we start accessing encrypted files, since it
259 * results in memory getting allocated that wouldn't otherwise be used.
260 *
261 * Return: Zero on success, non-zero otherwise.
262 */
263int f2fs_init_crypto(void)
264{
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700265 int i, res = -ENOMEM;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700266
267 mutex_lock(&crypto_init);
268 if (f2fs_read_workqueue)
269 goto already_initialized;
270
271 f2fs_read_workqueue = alloc_workqueue("f2fs_crypto", WQ_HIGHPRI, 0);
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700272 if (!f2fs_read_workqueue)
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700273 goto fail;
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700274
275 f2fs_crypto_ctx_cachep = KMEM_CACHE(f2fs_crypto_ctx,
276 SLAB_RECLAIM_ACCOUNT);
277 if (!f2fs_crypto_ctx_cachep)
278 goto fail;
279
280 f2fs_crypt_info_cachep = KMEM_CACHE(f2fs_crypt_info,
281 SLAB_RECLAIM_ACCOUNT);
282 if (!f2fs_crypt_info_cachep)
283 goto fail;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700284
285 for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
286 struct f2fs_crypto_ctx *ctx;
287
Jaegeuk Kim8bacf6de2015-05-12 13:26:54 -0700288 ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_KERNEL);
289 if (!ctx) {
290 res = -ENOMEM;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700291 goto fail;
292 }
293 list_add(&ctx->free_list, &f2fs_free_crypto_ctxs);
294 }
295
296 f2fs_bounce_page_pool =
297 mempool_create_page_pool(num_prealloc_crypto_pages, 0);
298 if (!f2fs_bounce_page_pool) {
299 res = -ENOMEM;
300 goto fail;
301 }
302already_initialized:
303 mutex_unlock(&crypto_init);
304 return 0;
305fail:
306 f2fs_exit_crypto();
307 mutex_unlock(&crypto_init);
308 return res;
309}
310
311void f2fs_restore_and_release_control_page(struct page **page)
312{
313 struct f2fs_crypto_ctx *ctx;
314 struct page *bounce_page;
315
316 /* The bounce data pages are unmapped. */
317 if ((*page)->mapping)
318 return;
319
320 /* The bounce data page is unmapped. */
321 bounce_page = *page;
322 ctx = (struct f2fs_crypto_ctx *)page_private(bounce_page);
323
324 /* restore control page */
Jaegeuk Kimca40b032015-05-12 13:40:20 -0700325 *page = ctx->w.control_page;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700326
327 f2fs_restore_control_page(bounce_page);
328}
329
330void f2fs_restore_control_page(struct page *data_page)
331{
332 struct f2fs_crypto_ctx *ctx =
333 (struct f2fs_crypto_ctx *)page_private(data_page);
334
335 set_page_private(data_page, (unsigned long)NULL);
336 ClearPagePrivate(data_page);
337 unlock_page(data_page);
338 f2fs_release_crypto_ctx(ctx);
339}
340
341/**
342 * f2fs_crypt_complete() - The completion callback for page encryption
343 * @req: The asynchronous encryption request context
344 * @res: The result of the encryption operation
345 */
346static void f2fs_crypt_complete(struct crypto_async_request *req, int res)
347{
348 struct f2fs_completion_result *ecr = req->data;
349
350 if (res == -EINPROGRESS)
351 return;
352 ecr->res = res;
353 complete(&ecr->completion);
354}
355
356typedef enum {
357 F2FS_DECRYPT = 0,
358 F2FS_ENCRYPT,
359} f2fs_direction_t;
360
361static int f2fs_page_crypto(struct f2fs_crypto_ctx *ctx,
362 struct inode *inode,
363 f2fs_direction_t rw,
364 pgoff_t index,
365 struct page *src_page,
366 struct page *dest_page)
367{
368 u8 xts_tweak[F2FS_XTS_TWEAK_SIZE];
369 struct ablkcipher_request *req = NULL;
370 DECLARE_F2FS_COMPLETION_RESULT(ecr);
371 struct scatterlist dst, src;
372 struct f2fs_inode_info *fi = F2FS_I(inode);
373 struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm);
374 int res = 0;
375
376 BUG_ON(!ctx->tfm);
Jaegeuk Kim640778f2015-05-12 13:33:00 -0700377 BUG_ON(ctx->mode != fi->i_crypt_info->ci_data_mode);
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700378
379 if (ctx->mode != F2FS_ENCRYPTION_MODE_AES_256_XTS) {
380 printk_ratelimited(KERN_ERR
381 "%s: unsupported crypto algorithm: %d\n",
382 __func__, ctx->mode);
383 return -ENOTSUPP;
384 }
385
386 crypto_ablkcipher_clear_flags(atfm, ~0);
387 crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
388
389 res = crypto_ablkcipher_setkey(atfm, fi->i_crypt_info->ci_raw,
390 fi->i_crypt_info->ci_size);
391 if (res) {
392 printk_ratelimited(KERN_ERR
393 "%s: crypto_ablkcipher_setkey() failed\n",
394 __func__);
395 return res;
396 }
397 req = ablkcipher_request_alloc(atfm, GFP_NOFS);
398 if (!req) {
399 printk_ratelimited(KERN_ERR
400 "%s: crypto_request_alloc() failed\n",
401 __func__);
402 return -ENOMEM;
403 }
404 ablkcipher_request_set_callback(
405 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
406 f2fs_crypt_complete, &ecr);
407
408 BUILD_BUG_ON(F2FS_XTS_TWEAK_SIZE < sizeof(index));
409 memcpy(xts_tweak, &index, sizeof(index));
410 memset(&xts_tweak[sizeof(index)], 0,
411 F2FS_XTS_TWEAK_SIZE - sizeof(index));
412
413 sg_init_table(&dst, 1);
414 sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0);
415 sg_init_table(&src, 1);
416 sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0);
417 ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE,
418 xts_tweak);
419 if (rw == F2FS_DECRYPT)
420 res = crypto_ablkcipher_decrypt(req);
421 else
422 res = crypto_ablkcipher_encrypt(req);
423 if (res == -EINPROGRESS || res == -EBUSY) {
424 BUG_ON(req->base.data != &ecr);
425 wait_for_completion(&ecr.completion);
426 res = ecr.res;
427 }
428 ablkcipher_request_free(req);
429 if (res) {
430 printk_ratelimited(KERN_ERR
431 "%s: crypto_ablkcipher_encrypt() returned %d\n",
432 __func__, res);
433 return res;
434 }
435 return 0;
436}
437
438/**
439 * f2fs_encrypt() - Encrypts a page
440 * @inode: The inode for which the encryption should take place
441 * @plaintext_page: The page to encrypt. Must be locked.
442 *
443 * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
444 * encryption context.
445 *
446 * Called on the page write path. The caller must call
447 * f2fs_restore_control_page() on the returned ciphertext page to
448 * release the bounce buffer and the encryption context.
449 *
450 * Return: An allocated page with the encrypted content on success. Else, an
451 * error value or NULL.
452 */
453struct page *f2fs_encrypt(struct inode *inode,
454 struct page *plaintext_page)
455{
456 struct f2fs_crypto_ctx *ctx;
457 struct page *ciphertext_page = NULL;
458 int err;
459
460 BUG_ON(!PageLocked(plaintext_page));
461
462 ctx = f2fs_get_crypto_ctx(inode);
463 if (IS_ERR(ctx))
464 return (struct page *)ctx;
465
466 /* The encryption operation will require a bounce page. */
467 ciphertext_page = alloc_page(GFP_NOFS);
468 if (!ciphertext_page) {
469 /*
470 * This is a potential bottleneck, but at least we'll have
471 * forward progress.
472 */
473 ciphertext_page = mempool_alloc(f2fs_bounce_page_pool,
474 GFP_NOFS);
475 if (WARN_ON_ONCE(!ciphertext_page))
476 ciphertext_page = mempool_alloc(f2fs_bounce_page_pool,
477 GFP_NOFS | __GFP_WAIT);
478 ctx->flags &= ~F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
479 } else {
480 ctx->flags |= F2FS_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
481 }
Jaegeuk Kimca40b032015-05-12 13:40:20 -0700482 ctx->flags |= F2FS_WRITE_PATH_FL;
483 ctx->w.bounce_page = ciphertext_page;
484 ctx->w.control_page = plaintext_page;
Jaegeuk Kim57e50552015-04-20 19:52:47 -0700485 err = f2fs_page_crypto(ctx, inode, F2FS_ENCRYPT, plaintext_page->index,
486 plaintext_page, ciphertext_page);
487 if (err) {
488 f2fs_release_crypto_ctx(ctx);
489 return ERR_PTR(err);
490 }
491 SetPagePrivate(ciphertext_page);
492 set_page_private(ciphertext_page, (unsigned long)ctx);
493 lock_page(ciphertext_page);
494 return ciphertext_page;
495}
496
497/**
498 * f2fs_decrypt() - Decrypts a page in-place
499 * @ctx: The encryption context.
500 * @page: The page to decrypt. Must be locked.
501 *
502 * Decrypts page in-place using the ctx encryption context.
503 *
504 * Called from the read completion callback.
505 *
506 * Return: Zero on success, non-zero otherwise.
507 */
508int f2fs_decrypt(struct f2fs_crypto_ctx *ctx, struct page *page)
509{
510 BUG_ON(!PageLocked(page));
511
512 return f2fs_page_crypto(ctx, page->mapping->host,
513 F2FS_DECRYPT, page->index, page, page);
514}
515
516/*
517 * Convenience function which takes care of allocating and
518 * deallocating the encryption context
519 */
520int f2fs_decrypt_one(struct inode *inode, struct page *page)
521{
522 struct f2fs_crypto_ctx *ctx = f2fs_get_crypto_ctx(inode);
523 int ret;
524
525 if (!ctx)
526 return -ENOMEM;
527 ret = f2fs_decrypt(ctx, page);
528 f2fs_release_crypto_ctx(ctx);
529 return ret;
530}
531
532bool f2fs_valid_contents_enc_mode(uint32_t mode)
533{
534 return (mode == F2FS_ENCRYPTION_MODE_AES_256_XTS);
535}
536
537/**
538 * f2fs_validate_encryption_key_size() - Validate the encryption key size
539 * @mode: The key mode.
540 * @size: The key size to validate.
541 *
542 * Return: The validated key size for @mode. Zero if invalid.
543 */
544uint32_t f2fs_validate_encryption_key_size(uint32_t mode, uint32_t size)
545{
546 if (size == f2fs_encryption_key_size(mode))
547 return size;
548 return 0;
549}