2 * linux/fs/f2fs/crypto.c
4 * Copied from linux/fs/ext4/crypto.c
6 * Copyright (C) 2015, Google, Inc.
7 * Copyright (C) 2015, Motorola Mobility
9 * This contains encryption functions for f2fs
11 * Written by Michael Halcrow, 2014.
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()
21 * This has not yet undergone a rigorous security audit.
23 * The usage of AES-XTS should conform to recommendations in NIST
24 * Special Publication 800-38E and IEEE P1619/D16.
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>
49 /* Encryption added and removed here! (L: */
51 static unsigned int num_prealloc_crypto_pages
= 32;
52 static unsigned int num_prealloc_crypto_ctxs
= 128;
54 module_param(num_prealloc_crypto_pages
, uint
, 0444);
55 MODULE_PARM_DESC(num_prealloc_crypto_pages
,
56 "Number of crypto pages to preallocate");
57 module_param(num_prealloc_crypto_ctxs
, uint
, 0444);
58 MODULE_PARM_DESC(num_prealloc_crypto_ctxs
,
59 "Number of crypto contexts to preallocate");
61 static mempool_t
*f2fs_bounce_page_pool
;
63 static LIST_HEAD(f2fs_free_crypto_ctxs
);
64 static DEFINE_SPINLOCK(f2fs_crypto_ctx_lock
);
66 static struct workqueue_struct
*f2fs_read_workqueue
;
67 static DEFINE_MUTEX(crypto_init
);
69 static struct kmem_cache
*f2fs_crypto_ctx_cachep
;
70 struct kmem_cache
*f2fs_crypt_info_cachep
;
73 * f2fs_release_crypto_ctx() - Releases an encryption context
74 * @ctx: The encryption context to release.
76 * If the encryption context was allocated from the pre-allocated pool, returns
77 * it to that pool. Else, frees it.
79 * If there's a bounce page in the context, this frees that.
81 void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx
*ctx
)
85 if (ctx
->flags
& F2FS_WRITE_PATH_FL
&& ctx
->w
.bounce_page
) {
86 mempool_free(ctx
->w
.bounce_page
, f2fs_bounce_page_pool
);
87 ctx
->w
.bounce_page
= NULL
;
89 ctx
->w
.control_page
= NULL
;
90 if (ctx
->flags
& F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL
) {
91 kmem_cache_free(f2fs_crypto_ctx_cachep
, ctx
);
93 spin_lock_irqsave(&f2fs_crypto_ctx_lock
, flags
);
94 list_add(&ctx
->free_list
, &f2fs_free_crypto_ctxs
);
95 spin_unlock_irqrestore(&f2fs_crypto_ctx_lock
, flags
);
100 * f2fs_get_crypto_ctx() - Gets an encryption context
101 * @inode: The inode for which we are doing the crypto
103 * Allocates and initializes an encryption context.
105 * Return: An allocated and initialized encryption context on success; error
106 * value or NULL otherwise.
108 struct f2fs_crypto_ctx
*f2fs_get_crypto_ctx(struct inode
*inode
)
110 struct f2fs_crypto_ctx
*ctx
= NULL
;
112 struct f2fs_crypt_info
*ci
= F2FS_I(inode
)->i_crypt_info
;
115 return ERR_PTR(-ENOKEY
);
118 * We first try getting the ctx from a free list because in
119 * the common case the ctx will have an allocated and
120 * initialized crypto tfm, so it's probably a worthwhile
121 * optimization. For the bounce page, we first try getting it
122 * from the kernel allocator because that's just about as fast
123 * as getting it from a list and because a cache of free pages
124 * should generally be a "last resort" option for a filesystem
125 * to be able to do its job.
127 spin_lock_irqsave(&f2fs_crypto_ctx_lock
, flags
);
128 ctx
= list_first_entry_or_null(&f2fs_free_crypto_ctxs
,
129 struct f2fs_crypto_ctx
, free_list
);
131 list_del(&ctx
->free_list
);
132 spin_unlock_irqrestore(&f2fs_crypto_ctx_lock
, flags
);
134 ctx
= kmem_cache_zalloc(f2fs_crypto_ctx_cachep
, GFP_NOFS
);
136 return ERR_PTR(-ENOMEM
);
137 ctx
->flags
|= F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL
;
139 ctx
->flags
&= ~F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL
;
141 ctx
->flags
&= ~F2FS_WRITE_PATH_FL
;
146 * Call f2fs_decrypt on every single page, reusing the encryption
149 static void completion_pages(struct work_struct
*work
)
151 struct f2fs_crypto_ctx
*ctx
=
152 container_of(work
, struct f2fs_crypto_ctx
, r
.work
);
153 struct bio
*bio
= ctx
->r
.bio
;
157 bio_for_each_segment_all(bv
, bio
, i
) {
158 struct page
*page
= bv
->bv_page
;
159 int ret
= f2fs_decrypt(ctx
, page
);
165 SetPageUptodate(page
);
168 f2fs_release_crypto_ctx(ctx
);
172 void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx
*ctx
, struct bio
*bio
)
174 INIT_WORK(&ctx
->r
.work
, completion_pages
);
176 queue_work(f2fs_read_workqueue
, &ctx
->r
.work
);
179 static void f2fs_crypto_destroy(void)
181 struct f2fs_crypto_ctx
*pos
, *n
;
183 list_for_each_entry_safe(pos
, n
, &f2fs_free_crypto_ctxs
, free_list
)
184 kmem_cache_free(f2fs_crypto_ctx_cachep
, pos
);
185 INIT_LIST_HEAD(&f2fs_free_crypto_ctxs
);
186 if (f2fs_bounce_page_pool
)
187 mempool_destroy(f2fs_bounce_page_pool
);
188 f2fs_bounce_page_pool
= NULL
;
192 * f2fs_crypto_initialize() - Set up for f2fs encryption.
194 * We only call this when we start accessing encrypted files, since it
195 * results in memory getting allocated that wouldn't otherwise be used.
197 * Return: Zero on success, non-zero otherwise.
199 int f2fs_crypto_initialize(void)
201 int i
, res
= -ENOMEM
;
203 if (f2fs_bounce_page_pool
)
206 mutex_lock(&crypto_init
);
207 if (f2fs_bounce_page_pool
)
208 goto already_initialized
;
210 for (i
= 0; i
< num_prealloc_crypto_ctxs
; i
++) {
211 struct f2fs_crypto_ctx
*ctx
;
213 ctx
= kmem_cache_zalloc(f2fs_crypto_ctx_cachep
, GFP_KERNEL
);
216 list_add(&ctx
->free_list
, &f2fs_free_crypto_ctxs
);
219 /* must be allocated at the last step to avoid race condition above */
220 f2fs_bounce_page_pool
=
221 mempool_create_page_pool(num_prealloc_crypto_pages
, 0);
222 if (!f2fs_bounce_page_pool
)
226 mutex_unlock(&crypto_init
);
229 f2fs_crypto_destroy();
230 mutex_unlock(&crypto_init
);
235 * f2fs_exit_crypto() - Shutdown the f2fs encryption system
237 void f2fs_exit_crypto(void)
239 f2fs_crypto_destroy();
241 if (f2fs_read_workqueue
)
242 destroy_workqueue(f2fs_read_workqueue
);
243 if (f2fs_crypto_ctx_cachep
)
244 kmem_cache_destroy(f2fs_crypto_ctx_cachep
);
245 if (f2fs_crypt_info_cachep
)
246 kmem_cache_destroy(f2fs_crypt_info_cachep
);
249 int __init
f2fs_init_crypto(void)
253 f2fs_read_workqueue
= alloc_workqueue("f2fs_crypto", WQ_HIGHPRI
, 0);
254 if (!f2fs_read_workqueue
)
257 f2fs_crypto_ctx_cachep
= KMEM_CACHE(f2fs_crypto_ctx
,
258 SLAB_RECLAIM_ACCOUNT
);
259 if (!f2fs_crypto_ctx_cachep
)
262 f2fs_crypt_info_cachep
= KMEM_CACHE(f2fs_crypt_info
,
263 SLAB_RECLAIM_ACCOUNT
);
264 if (!f2fs_crypt_info_cachep
)
273 void f2fs_restore_and_release_control_page(struct page
**page
)
275 struct f2fs_crypto_ctx
*ctx
;
276 struct page
*bounce_page
;
278 /* The bounce data pages are unmapped. */
279 if ((*page
)->mapping
)
282 /* The bounce data page is unmapped. */
284 ctx
= (struct f2fs_crypto_ctx
*)page_private(bounce_page
);
286 /* restore control page */
287 *page
= ctx
->w
.control_page
;
289 f2fs_restore_control_page(bounce_page
);
292 void f2fs_restore_control_page(struct page
*data_page
)
294 struct f2fs_crypto_ctx
*ctx
=
295 (struct f2fs_crypto_ctx
*)page_private(data_page
);
297 set_page_private(data_page
, (unsigned long)NULL
);
298 ClearPagePrivate(data_page
);
299 unlock_page(data_page
);
300 f2fs_release_crypto_ctx(ctx
);
304 * f2fs_crypt_complete() - The completion callback for page encryption
305 * @req: The asynchronous encryption request context
306 * @res: The result of the encryption operation
308 static void f2fs_crypt_complete(struct crypto_async_request
*req
, int res
)
310 struct f2fs_completion_result
*ecr
= req
->data
;
312 if (res
== -EINPROGRESS
)
315 complete(&ecr
->completion
);
323 static int f2fs_page_crypto(struct f2fs_crypto_ctx
*ctx
,
327 struct page
*src_page
,
328 struct page
*dest_page
)
330 u8 xts_tweak
[F2FS_XTS_TWEAK_SIZE
];
331 struct ablkcipher_request
*req
= NULL
;
332 DECLARE_F2FS_COMPLETION_RESULT(ecr
);
333 struct scatterlist dst
, src
;
334 struct f2fs_crypt_info
*ci
= F2FS_I(inode
)->i_crypt_info
;
335 struct crypto_ablkcipher
*tfm
= ci
->ci_ctfm
;
338 req
= ablkcipher_request_alloc(tfm
, GFP_NOFS
);
340 printk_ratelimited(KERN_ERR
341 "%s: crypto_request_alloc() failed\n",
345 ablkcipher_request_set_callback(
346 req
, CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
347 f2fs_crypt_complete
, &ecr
);
349 BUILD_BUG_ON(F2FS_XTS_TWEAK_SIZE
< sizeof(index
));
350 memcpy(xts_tweak
, &index
, sizeof(index
));
351 memset(&xts_tweak
[sizeof(index
)], 0,
352 F2FS_XTS_TWEAK_SIZE
- sizeof(index
));
354 sg_init_table(&dst
, 1);
355 sg_set_page(&dst
, dest_page
, PAGE_CACHE_SIZE
, 0);
356 sg_init_table(&src
, 1);
357 sg_set_page(&src
, src_page
, PAGE_CACHE_SIZE
, 0);
358 ablkcipher_request_set_crypt(req
, &src
, &dst
, PAGE_CACHE_SIZE
,
360 if (rw
== F2FS_DECRYPT
)
361 res
= crypto_ablkcipher_decrypt(req
);
363 res
= crypto_ablkcipher_encrypt(req
);
364 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
365 wait_for_completion(&ecr
.completion
);
368 ablkcipher_request_free(req
);
370 printk_ratelimited(KERN_ERR
371 "%s: crypto_ablkcipher_encrypt() returned %d\n",
378 static struct page
*alloc_bounce_page(struct f2fs_crypto_ctx
*ctx
)
380 ctx
->w
.bounce_page
= mempool_alloc(f2fs_bounce_page_pool
, GFP_NOWAIT
);
381 if (ctx
->w
.bounce_page
== NULL
)
382 return ERR_PTR(-ENOMEM
);
383 ctx
->flags
|= F2FS_WRITE_PATH_FL
;
384 return ctx
->w
.bounce_page
;
388 * f2fs_encrypt() - Encrypts a page
389 * @inode: The inode for which the encryption should take place
390 * @plaintext_page: The page to encrypt. Must be locked.
392 * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
393 * encryption context.
395 * Called on the page write path. The caller must call
396 * f2fs_restore_control_page() on the returned ciphertext page to
397 * release the bounce buffer and the encryption context.
399 * Return: An allocated page with the encrypted content on success. Else, an
400 * error value or NULL.
402 struct page
*f2fs_encrypt(struct inode
*inode
,
403 struct page
*plaintext_page
)
405 struct f2fs_crypto_ctx
*ctx
;
406 struct page
*ciphertext_page
= NULL
;
409 BUG_ON(!PageLocked(plaintext_page
));
411 ctx
= f2fs_get_crypto_ctx(inode
);
413 return (struct page
*)ctx
;
415 /* The encryption operation will require a bounce page. */
416 ciphertext_page
= alloc_bounce_page(ctx
);
417 if (IS_ERR(ciphertext_page
))
420 ctx
->w
.control_page
= plaintext_page
;
421 err
= f2fs_page_crypto(ctx
, inode
, F2FS_ENCRYPT
, plaintext_page
->index
,
422 plaintext_page
, ciphertext_page
);
424 ciphertext_page
= ERR_PTR(err
);
428 SetPagePrivate(ciphertext_page
);
429 set_page_private(ciphertext_page
, (unsigned long)ctx
);
430 lock_page(ciphertext_page
);
431 return ciphertext_page
;
434 f2fs_release_crypto_ctx(ctx
);
435 return ciphertext_page
;
439 * f2fs_decrypt() - Decrypts a page in-place
440 * @ctx: The encryption context.
441 * @page: The page to decrypt. Must be locked.
443 * Decrypts page in-place using the ctx encryption context.
445 * Called from the read completion callback.
447 * Return: Zero on success, non-zero otherwise.
449 int f2fs_decrypt(struct f2fs_crypto_ctx
*ctx
, struct page
*page
)
451 BUG_ON(!PageLocked(page
));
453 return f2fs_page_crypto(ctx
, page
->mapping
->host
,
454 F2FS_DECRYPT
, page
->index
, page
, page
);
458 * Convenience function which takes care of allocating and
459 * deallocating the encryption context
461 int f2fs_decrypt_one(struct inode
*inode
, struct page
*page
)
463 struct f2fs_crypto_ctx
*ctx
= f2fs_get_crypto_ctx(inode
);
468 ret
= f2fs_decrypt(ctx
, page
);
469 f2fs_release_crypto_ctx(ctx
);
473 bool f2fs_valid_contents_enc_mode(uint32_t mode
)
475 return (mode
== F2FS_ENCRYPTION_MODE_AES_256_XTS
);
479 * f2fs_validate_encryption_key_size() - Validate the encryption key size
480 * @mode: The key mode.
481 * @size: The key size to validate.
483 * Return: The validated key size for @mode. Zero if invalid.
485 uint32_t f2fs_validate_encryption_key_size(uint32_t mode
, uint32_t size
)
487 if (size
== f2fs_encryption_key_size(mode
))