Commit | Line | Data |
---|---|---|
57e5055b JK |
1 | /* |
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 | */ | |
2731a944 | 26 | #include <crypto/skcipher.h> |
57e5055b JK |
27 | #include <keys/user-type.h> |
28 | #include <keys/encrypted-type.h> | |
57e5055b JK |
29 | #include <linux/ecryptfs.h> |
30 | #include <linux/gfp.h> | |
31 | #include <linux/kernel.h> | |
32 | #include <linux/key.h> | |
33 | #include <linux/list.h> | |
34 | #include <linux/mempool.h> | |
35 | #include <linux/module.h> | |
36 | #include <linux/mutex.h> | |
37 | #include <linux/random.h> | |
38 | #include <linux/scatterlist.h> | |
39 | #include <linux/spinlock_types.h> | |
40 | #include <linux/f2fs_fs.h> | |
41 | #include <linux/ratelimit.h> | |
42 | #include <linux/bio.h> | |
43 | ||
44 | #include "f2fs.h" | |
45 | #include "xattr.h" | |
46 | ||
47 | /* Encryption added and removed here! (L: */ | |
48 | ||
49 | static unsigned int num_prealloc_crypto_pages = 32; | |
50 | static unsigned int num_prealloc_crypto_ctxs = 128; | |
51 | ||
52 | module_param(num_prealloc_crypto_pages, uint, 0444); | |
53 | MODULE_PARM_DESC(num_prealloc_crypto_pages, | |
54 | "Number of crypto pages to preallocate"); | |
55 | module_param(num_prealloc_crypto_ctxs, uint, 0444); | |
56 | MODULE_PARM_DESC(num_prealloc_crypto_ctxs, | |
57 | "Number of crypto contexts to preallocate"); | |
58 | ||
59 | static mempool_t *f2fs_bounce_page_pool; | |
60 | ||
61 | static LIST_HEAD(f2fs_free_crypto_ctxs); | |
62 | static DEFINE_SPINLOCK(f2fs_crypto_ctx_lock); | |
63 | ||
cfc4d971 | 64 | static struct workqueue_struct *f2fs_read_workqueue; |
57e5055b JK |
65 | static DEFINE_MUTEX(crypto_init); |
66 | ||
8bacf6de JK |
67 | static struct kmem_cache *f2fs_crypto_ctx_cachep; |
68 | struct kmem_cache *f2fs_crypt_info_cachep; | |
69 | ||
57e5055b JK |
70 | /** |
71 | * f2fs_release_crypto_ctx() - Releases an encryption context | |
72 | * @ctx: The encryption context to release. | |
73 | * | |
74 | * If the encryption context was allocated from the pre-allocated pool, returns | |
75 | * it to that pool. Else, frees it. | |
76 | * | |
77 | * If there's a bounce page in the context, this frees that. | |
78 | */ | |
79 | void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *ctx) | |
80 | { | |
81 | unsigned long flags; | |
82 | ||
ca40b030 | 83 | if (ctx->flags & F2FS_WRITE_PATH_FL && ctx->w.bounce_page) { |
4683ff83 | 84 | mempool_free(ctx->w.bounce_page, f2fs_bounce_page_pool); |
ca40b030 | 85 | ctx->w.bounce_page = NULL; |
57e5055b | 86 | } |
ca40b030 | 87 | ctx->w.control_page = NULL; |
57e5055b | 88 | if (ctx->flags & F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL) { |
8bacf6de | 89 | kmem_cache_free(f2fs_crypto_ctx_cachep, ctx); |
57e5055b JK |
90 | } else { |
91 | spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags); | |
92 | list_add(&ctx->free_list, &f2fs_free_crypto_ctxs); | |
93 | spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags); | |
94 | } | |
95 | } | |
96 | ||
57e5055b JK |
97 | /** |
98 | * f2fs_get_crypto_ctx() - Gets an encryption context | |
99 | * @inode: The inode for which we are doing the crypto | |
100 | * | |
101 | * Allocates and initializes an encryption context. | |
102 | * | |
103 | * Return: An allocated and initialized encryption context on success; error | |
104 | * value or NULL otherwise. | |
105 | */ | |
106 | struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *inode) | |
107 | { | |
108 | struct f2fs_crypto_ctx *ctx = NULL; | |
57e5055b JK |
109 | unsigned long flags; |
110 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; | |
111 | ||
edf3fb8e | 112 | if (ci == NULL) |
7e8e754a | 113 | return ERR_PTR(-ENOKEY); |
edf3fb8e | 114 | |
57e5055b JK |
115 | /* |
116 | * We first try getting the ctx from a free list because in | |
117 | * the common case the ctx will have an allocated and | |
118 | * initialized crypto tfm, so it's probably a worthwhile | |
119 | * optimization. For the bounce page, we first try getting it | |
120 | * from the kernel allocator because that's just about as fast | |
121 | * as getting it from a list and because a cache of free pages | |
122 | * should generally be a "last resort" option for a filesystem | |
123 | * to be able to do its job. | |
124 | */ | |
125 | spin_lock_irqsave(&f2fs_crypto_ctx_lock, flags); | |
126 | ctx = list_first_entry_or_null(&f2fs_free_crypto_ctxs, | |
127 | struct f2fs_crypto_ctx, free_list); | |
128 | if (ctx) | |
129 | list_del(&ctx->free_list); | |
130 | spin_unlock_irqrestore(&f2fs_crypto_ctx_lock, flags); | |
131 | if (!ctx) { | |
8bacf6de | 132 | ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_NOFS); |
26bf3dc7 JK |
133 | if (!ctx) |
134 | return ERR_PTR(-ENOMEM); | |
57e5055b JK |
135 | ctx->flags |= F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL; |
136 | } else { | |
137 | ctx->flags &= ~F2FS_CTX_REQUIRES_FREE_ENCRYPT_FL; | |
138 | } | |
ca40b030 | 139 | ctx->flags &= ~F2FS_WRITE_PATH_FL; |
57e5055b JK |
140 | return ctx; |
141 | } | |
142 | ||
143 | /* | |
144 | * Call f2fs_decrypt on every single page, reusing the encryption | |
145 | * context. | |
146 | */ | |
147 | static void completion_pages(struct work_struct *work) | |
148 | { | |
149 | struct f2fs_crypto_ctx *ctx = | |
ca40b030 JK |
150 | container_of(work, struct f2fs_crypto_ctx, r.work); |
151 | struct bio *bio = ctx->r.bio; | |
57e5055b JK |
152 | struct bio_vec *bv; |
153 | int i; | |
154 | ||
155 | bio_for_each_segment_all(bv, bio, i) { | |
156 | struct page *page = bv->bv_page; | |
157 | int ret = f2fs_decrypt(ctx, page); | |
158 | ||
159 | if (ret) { | |
160 | WARN_ON_ONCE(1); | |
161 | SetPageError(page); | |
162 | } else | |
163 | SetPageUptodate(page); | |
164 | unlock_page(page); | |
165 | } | |
166 | f2fs_release_crypto_ctx(ctx); | |
167 | bio_put(bio); | |
168 | } | |
169 | ||
170 | void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *ctx, struct bio *bio) | |
171 | { | |
ca40b030 JK |
172 | INIT_WORK(&ctx->r.work, completion_pages); |
173 | ctx->r.bio = bio; | |
174 | queue_work(f2fs_read_workqueue, &ctx->r.work); | |
57e5055b JK |
175 | } |
176 | ||
cfc4d971 | 177 | static void f2fs_crypto_destroy(void) |
57e5055b JK |
178 | { |
179 | struct f2fs_crypto_ctx *pos, *n; | |
180 | ||
26bf3dc7 | 181 | list_for_each_entry_safe(pos, n, &f2fs_free_crypto_ctxs, free_list) |
8bacf6de | 182 | kmem_cache_free(f2fs_crypto_ctx_cachep, pos); |
57e5055b JK |
183 | INIT_LIST_HEAD(&f2fs_free_crypto_ctxs); |
184 | if (f2fs_bounce_page_pool) | |
185 | mempool_destroy(f2fs_bounce_page_pool); | |
186 | f2fs_bounce_page_pool = NULL; | |
57e5055b JK |
187 | } |
188 | ||
189 | /** | |
cfc4d971 | 190 | * f2fs_crypto_initialize() - Set up for f2fs encryption. |
57e5055b JK |
191 | * |
192 | * We only call this when we start accessing encrypted files, since it | |
193 | * results in memory getting allocated that wouldn't otherwise be used. | |
194 | * | |
195 | * Return: Zero on success, non-zero otherwise. | |
196 | */ | |
cfc4d971 | 197 | int f2fs_crypto_initialize(void) |
57e5055b | 198 | { |
8bacf6de | 199 | int i, res = -ENOMEM; |
57e5055b | 200 | |
cfc4d971 JK |
201 | if (f2fs_bounce_page_pool) |
202 | return 0; | |
203 | ||
57e5055b | 204 | mutex_lock(&crypto_init); |
cfc4d971 | 205 | if (f2fs_bounce_page_pool) |
57e5055b JK |
206 | goto already_initialized; |
207 | ||
57e5055b JK |
208 | for (i = 0; i < num_prealloc_crypto_ctxs; i++) { |
209 | struct f2fs_crypto_ctx *ctx; | |
210 | ||
8bacf6de | 211 | ctx = kmem_cache_zalloc(f2fs_crypto_ctx_cachep, GFP_KERNEL); |
cfc4d971 | 212 | if (!ctx) |
57e5055b | 213 | goto fail; |
57e5055b JK |
214 | list_add(&ctx->free_list, &f2fs_free_crypto_ctxs); |
215 | } | |
216 | ||
cfc4d971 | 217 | /* must be allocated at the last step to avoid race condition above */ |
57e5055b JK |
218 | f2fs_bounce_page_pool = |
219 | mempool_create_page_pool(num_prealloc_crypto_pages, 0); | |
cfc4d971 | 220 | if (!f2fs_bounce_page_pool) |
57e5055b | 221 | goto fail; |
cfc4d971 | 222 | |
57e5055b JK |
223 | already_initialized: |
224 | mutex_unlock(&crypto_init); | |
225 | return 0; | |
226 | fail: | |
cfc4d971 | 227 | f2fs_crypto_destroy(); |
57e5055b JK |
228 | mutex_unlock(&crypto_init); |
229 | return res; | |
230 | } | |
231 | ||
cfc4d971 JK |
232 | /** |
233 | * f2fs_exit_crypto() - Shutdown the f2fs encryption system | |
234 | */ | |
235 | void f2fs_exit_crypto(void) | |
236 | { | |
237 | f2fs_crypto_destroy(); | |
238 | ||
239 | if (f2fs_read_workqueue) | |
240 | destroy_workqueue(f2fs_read_workqueue); | |
241 | if (f2fs_crypto_ctx_cachep) | |
242 | kmem_cache_destroy(f2fs_crypto_ctx_cachep); | |
243 | if (f2fs_crypt_info_cachep) | |
244 | kmem_cache_destroy(f2fs_crypt_info_cachep); | |
245 | } | |
246 | ||
247 | int __init f2fs_init_crypto(void) | |
248 | { | |
249 | int res = -ENOMEM; | |
250 | ||
251 | f2fs_read_workqueue = alloc_workqueue("f2fs_crypto", WQ_HIGHPRI, 0); | |
252 | if (!f2fs_read_workqueue) | |
253 | goto fail; | |
254 | ||
255 | f2fs_crypto_ctx_cachep = KMEM_CACHE(f2fs_crypto_ctx, | |
256 | SLAB_RECLAIM_ACCOUNT); | |
257 | if (!f2fs_crypto_ctx_cachep) | |
258 | goto fail; | |
259 | ||
260 | f2fs_crypt_info_cachep = KMEM_CACHE(f2fs_crypt_info, | |
261 | SLAB_RECLAIM_ACCOUNT); | |
262 | if (!f2fs_crypt_info_cachep) | |
263 | goto fail; | |
264 | ||
265 | return 0; | |
266 | fail: | |
267 | f2fs_exit_crypto(); | |
268 | return res; | |
269 | } | |
270 | ||
57e5055b JK |
271 | void f2fs_restore_and_release_control_page(struct page **page) |
272 | { | |
273 | struct f2fs_crypto_ctx *ctx; | |
274 | struct page *bounce_page; | |
275 | ||
276 | /* The bounce data pages are unmapped. */ | |
277 | if ((*page)->mapping) | |
278 | return; | |
279 | ||
280 | /* The bounce data page is unmapped. */ | |
281 | bounce_page = *page; | |
282 | ctx = (struct f2fs_crypto_ctx *)page_private(bounce_page); | |
283 | ||
284 | /* restore control page */ | |
ca40b030 | 285 | *page = ctx->w.control_page; |
57e5055b JK |
286 | |
287 | f2fs_restore_control_page(bounce_page); | |
288 | } | |
289 | ||
290 | void f2fs_restore_control_page(struct page *data_page) | |
291 | { | |
292 | struct f2fs_crypto_ctx *ctx = | |
293 | (struct f2fs_crypto_ctx *)page_private(data_page); | |
294 | ||
295 | set_page_private(data_page, (unsigned long)NULL); | |
296 | ClearPagePrivate(data_page); | |
297 | unlock_page(data_page); | |
298 | f2fs_release_crypto_ctx(ctx); | |
299 | } | |
300 | ||
301 | /** | |
302 | * f2fs_crypt_complete() - The completion callback for page encryption | |
303 | * @req: The asynchronous encryption request context | |
304 | * @res: The result of the encryption operation | |
305 | */ | |
306 | static void f2fs_crypt_complete(struct crypto_async_request *req, int res) | |
307 | { | |
308 | struct f2fs_completion_result *ecr = req->data; | |
309 | ||
310 | if (res == -EINPROGRESS) | |
311 | return; | |
312 | ecr->res = res; | |
313 | complete(&ecr->completion); | |
314 | } | |
315 | ||
316 | typedef enum { | |
317 | F2FS_DECRYPT = 0, | |
318 | F2FS_ENCRYPT, | |
319 | } f2fs_direction_t; | |
320 | ||
321 | static int f2fs_page_crypto(struct f2fs_crypto_ctx *ctx, | |
322 | struct inode *inode, | |
323 | f2fs_direction_t rw, | |
324 | pgoff_t index, | |
325 | struct page *src_page, | |
326 | struct page *dest_page) | |
327 | { | |
328 | u8 xts_tweak[F2FS_XTS_TWEAK_SIZE]; | |
2731a944 | 329 | struct skcipher_request *req = NULL; |
57e5055b JK |
330 | DECLARE_F2FS_COMPLETION_RESULT(ecr); |
331 | struct scatterlist dst, src; | |
26bf3dc7 | 332 | struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info; |
2731a944 | 333 | struct crypto_skcipher *tfm = ci->ci_ctfm; |
57e5055b JK |
334 | int res = 0; |
335 | ||
2731a944 | 336 | req = skcipher_request_alloc(tfm, GFP_NOFS); |
57e5055b JK |
337 | if (!req) { |
338 | printk_ratelimited(KERN_ERR | |
339 | "%s: crypto_request_alloc() failed\n", | |
340 | __func__); | |
341 | return -ENOMEM; | |
342 | } | |
2731a944 | 343 | skcipher_request_set_callback( |
57e5055b JK |
344 | req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
345 | f2fs_crypt_complete, &ecr); | |
346 | ||
347 | BUILD_BUG_ON(F2FS_XTS_TWEAK_SIZE < sizeof(index)); | |
348 | memcpy(xts_tweak, &index, sizeof(index)); | |
349 | memset(&xts_tweak[sizeof(index)], 0, | |
350 | F2FS_XTS_TWEAK_SIZE - sizeof(index)); | |
351 | ||
352 | sg_init_table(&dst, 1); | |
353 | sg_set_page(&dst, dest_page, PAGE_CACHE_SIZE, 0); | |
354 | sg_init_table(&src, 1); | |
355 | sg_set_page(&src, src_page, PAGE_CACHE_SIZE, 0); | |
2731a944 HX |
356 | skcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE, |
357 | xts_tweak); | |
57e5055b | 358 | if (rw == F2FS_DECRYPT) |
2731a944 | 359 | res = crypto_skcipher_decrypt(req); |
57e5055b | 360 | else |
2731a944 | 361 | res = crypto_skcipher_encrypt(req); |
57e5055b JK |
362 | if (res == -EINPROGRESS || res == -EBUSY) { |
363 | BUG_ON(req->base.data != &ecr); | |
364 | wait_for_completion(&ecr.completion); | |
365 | res = ecr.res; | |
366 | } | |
2731a944 | 367 | skcipher_request_free(req); |
57e5055b JK |
368 | if (res) { |
369 | printk_ratelimited(KERN_ERR | |
2731a944 | 370 | "%s: crypto_skcipher_encrypt() returned %d\n", |
57e5055b JK |
371 | __func__, res); |
372 | return res; | |
373 | } | |
374 | return 0; | |
375 | } | |
376 | ||
43f54cd5 JK |
377 | static struct page *alloc_bounce_page(struct f2fs_crypto_ctx *ctx) |
378 | { | |
379 | ctx->w.bounce_page = mempool_alloc(f2fs_bounce_page_pool, GFP_NOWAIT); | |
380 | if (ctx->w.bounce_page == NULL) | |
381 | return ERR_PTR(-ENOMEM); | |
382 | ctx->flags |= F2FS_WRITE_PATH_FL; | |
383 | return ctx->w.bounce_page; | |
384 | } | |
385 | ||
57e5055b JK |
386 | /** |
387 | * f2fs_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 | * f2fs_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 | */ | |
401 | struct page *f2fs_encrypt(struct inode *inode, | |
402 | struct page *plaintext_page) | |
403 | { | |
404 | struct f2fs_crypto_ctx *ctx; | |
405 | struct page *ciphertext_page = NULL; | |
406 | int err; | |
407 | ||
408 | BUG_ON(!PageLocked(plaintext_page)); | |
409 | ||
410 | ctx = f2fs_get_crypto_ctx(inode); | |
411 | if (IS_ERR(ctx)) | |
412 | return (struct page *)ctx; | |
413 | ||
414 | /* The encryption operation will require a bounce page. */ | |
43f54cd5 JK |
415 | ciphertext_page = alloc_bounce_page(ctx); |
416 | if (IS_ERR(ciphertext_page)) | |
4683ff83 | 417 | goto err_out; |
4683ff83 | 418 | |
ca40b030 | 419 | ctx->w.control_page = plaintext_page; |
57e5055b JK |
420 | err = f2fs_page_crypto(ctx, inode, F2FS_ENCRYPT, plaintext_page->index, |
421 | plaintext_page, ciphertext_page); | |
43f54cd5 JK |
422 | if (err) { |
423 | ciphertext_page = ERR_PTR(err); | |
4683ff83 | 424 | goto err_out; |
43f54cd5 | 425 | } |
4683ff83 | 426 | |
57e5055b JK |
427 | SetPagePrivate(ciphertext_page); |
428 | set_page_private(ciphertext_page, (unsigned long)ctx); | |
429 | lock_page(ciphertext_page); | |
430 | return ciphertext_page; | |
4683ff83 JK |
431 | |
432 | err_out: | |
433 | f2fs_release_crypto_ctx(ctx); | |
43f54cd5 | 434 | return ciphertext_page; |
57e5055b JK |
435 | } |
436 | ||
437 | /** | |
438 | * f2fs_decrypt() - Decrypts a page in-place | |
439 | * @ctx: The encryption context. | |
440 | * @page: The page to decrypt. Must be locked. | |
441 | * | |
442 | * Decrypts page in-place using the ctx encryption context. | |
443 | * | |
444 | * Called from the read completion callback. | |
445 | * | |
446 | * Return: Zero on success, non-zero otherwise. | |
447 | */ | |
448 | int f2fs_decrypt(struct f2fs_crypto_ctx *ctx, struct page *page) | |
449 | { | |
450 | BUG_ON(!PageLocked(page)); | |
451 | ||
452 | return f2fs_page_crypto(ctx, page->mapping->host, | |
453 | F2FS_DECRYPT, page->index, page, page); | |
454 | } | |
455 | ||
456 | /* | |
457 | * Convenience function which takes care of allocating and | |
458 | * deallocating the encryption context | |
459 | */ | |
460 | int f2fs_decrypt_one(struct inode *inode, struct page *page) | |
461 | { | |
462 | struct f2fs_crypto_ctx *ctx = f2fs_get_crypto_ctx(inode); | |
463 | int ret; | |
464 | ||
7e8e754a JK |
465 | if (IS_ERR(ctx)) |
466 | return PTR_ERR(ctx); | |
57e5055b JK |
467 | ret = f2fs_decrypt(ctx, page); |
468 | f2fs_release_crypto_ctx(ctx); | |
469 | return ret; | |
470 | } | |
471 | ||
472 | bool f2fs_valid_contents_enc_mode(uint32_t mode) | |
473 | { | |
474 | return (mode == F2FS_ENCRYPTION_MODE_AES_256_XTS); | |
475 | } | |
476 | ||
477 | /** | |
478 | * f2fs_validate_encryption_key_size() - Validate the encryption key size | |
479 | * @mode: The key mode. | |
480 | * @size: The key size to validate. | |
481 | * | |
482 | * Return: The validated key size for @mode. Zero if invalid. | |
483 | */ | |
484 | uint32_t f2fs_validate_encryption_key_size(uint32_t mode, uint32_t size) | |
485 | { | |
486 | if (size == f2fs_encryption_key_size(mode)) | |
487 | return size; | |
488 | return 0; | |
489 | } |