2 * linux/fs/ext4/crypto.c
4 * Copyright (C) 2015, Google, Inc.
6 * This contains encryption functions for ext4
8 * Written by Michael Halcrow, 2014.
10 * Filename encryption additions
11 * Uday Savagaonkar, 2014
12 * Encryption policy handling additions
13 * Ildar Muslukhov, 2014
15 * This has not yet undergone a rigorous security audit.
17 * The usage of AES-XTS should conform to recommendations in NIST
18 * Special Publication 800-38E and IEEE P1619/D16.
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>
38 #include "ext4_extents.h"
41 /* Encryption added and removed here! (L: */
43 static unsigned int num_prealloc_crypto_pages
= 32;
44 static unsigned int num_prealloc_crypto_ctxs
= 128;
46 module_param(num_prealloc_crypto_pages
, uint
, 0444);
47 MODULE_PARM_DESC(num_prealloc_crypto_pages
,
48 "Number of crypto pages to preallocate");
49 module_param(num_prealloc_crypto_ctxs
, uint
, 0444);
50 MODULE_PARM_DESC(num_prealloc_crypto_ctxs
,
51 "Number of crypto contexts to preallocate");
53 static mempool_t
*ext4_bounce_page_pool
;
55 static LIST_HEAD(ext4_free_crypto_ctxs
);
56 static DEFINE_SPINLOCK(ext4_crypto_ctx_lock
);
59 * ext4_release_crypto_ctx() - Releases an encryption context
60 * @ctx: The encryption context to release.
62 * If the encryption context was allocated from the pre-allocated pool, returns
63 * it to that pool. Else, frees it.
65 * If there's a bounce page in the context, this frees that.
67 void ext4_release_crypto_ctx(struct ext4_crypto_ctx
*ctx
)
71 if (ctx
->bounce_page
) {
72 if (ctx
->flags
& EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
)
73 __free_page(ctx
->bounce_page
);
75 mempool_free(ctx
->bounce_page
, ext4_bounce_page_pool
);
76 ctx
->bounce_page
= NULL
;
78 ctx
->control_page
= NULL
;
79 if (ctx
->flags
& EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL
) {
81 crypto_free_tfm(ctx
->tfm
);
84 spin_lock_irqsave(&ext4_crypto_ctx_lock
, flags
);
85 list_add(&ctx
->free_list
, &ext4_free_crypto_ctxs
);
86 spin_unlock_irqrestore(&ext4_crypto_ctx_lock
, flags
);
91 * ext4_alloc_and_init_crypto_ctx() - Allocates and inits an encryption context
92 * @mask: The allocation mask.
94 * Return: An allocated and initialized encryption context on success. An error
95 * value or NULL otherwise.
97 static struct ext4_crypto_ctx
*ext4_alloc_and_init_crypto_ctx(gfp_t mask
)
99 struct ext4_crypto_ctx
*ctx
= kzalloc(sizeof(struct ext4_crypto_ctx
),
103 return ERR_PTR(-ENOMEM
);
108 * ext4_get_crypto_ctx() - Gets an encryption context
109 * @inode: The inode for which we are doing the crypto
111 * Allocates and initializes an encryption context.
113 * Return: An allocated and initialized encryption context on success; error
114 * value or NULL otherwise.
116 struct ext4_crypto_ctx
*ext4_get_crypto_ctx(struct inode
*inode
)
118 struct ext4_crypto_ctx
*ctx
= NULL
;
121 struct ext4_encryption_key
*key
= &EXT4_I(inode
)->i_encryption_key
;
123 if (!ext4_read_workqueue
)
127 * We first try getting the ctx from a free list because in
128 * the common case the ctx will have an allocated and
129 * initialized crypto tfm, so it's probably a worthwhile
130 * optimization. For the bounce page, we first try getting it
131 * from the kernel allocator because that's just about as fast
132 * as getting it from a list and because a cache of free pages
133 * should generally be a "last resort" option for a filesystem
134 * to be able to do its job.
136 spin_lock_irqsave(&ext4_crypto_ctx_lock
, flags
);
137 ctx
= list_first_entry_or_null(&ext4_free_crypto_ctxs
,
138 struct ext4_crypto_ctx
, free_list
);
140 list_del(&ctx
->free_list
);
141 spin_unlock_irqrestore(&ext4_crypto_ctx_lock
, flags
);
143 ctx
= ext4_alloc_and_init_crypto_ctx(GFP_NOFS
);
148 ctx
->flags
|= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL
;
150 ctx
->flags
&= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL
;
153 /* Allocate a new Crypto API context if we don't already have
154 * one or if it isn't the right mode. */
155 BUG_ON(key
->mode
== EXT4_ENCRYPTION_MODE_INVALID
);
156 if (ctx
->tfm
&& (ctx
->mode
!= key
->mode
)) {
157 crypto_free_tfm(ctx
->tfm
);
159 ctx
->mode
= EXT4_ENCRYPTION_MODE_INVALID
;
163 case EXT4_ENCRYPTION_MODE_AES_256_XTS
:
164 ctx
->tfm
= crypto_ablkcipher_tfm(
165 crypto_alloc_ablkcipher("xts(aes)", 0, 0));
167 case EXT4_ENCRYPTION_MODE_AES_256_GCM
:
168 /* TODO(mhalcrow): AEAD w/ gcm(aes);
169 * crypto_aead_setauthsize() */
170 ctx
->tfm
= ERR_PTR(-ENOTSUPP
);
175 if (IS_ERR_OR_NULL(ctx
->tfm
)) {
176 res
= PTR_ERR(ctx
->tfm
);
180 ctx
->mode
= key
->mode
;
182 BUG_ON(key
->size
!= ext4_encryption_key_size(key
->mode
));
184 /* There shouldn't be a bounce page attached to the crypto
185 * context at this point. */
186 BUG_ON(ctx
->bounce_page
);
190 if (!IS_ERR_OR_NULL(ctx
))
191 ext4_release_crypto_ctx(ctx
);
197 struct workqueue_struct
*ext4_read_workqueue
;
198 static DEFINE_MUTEX(crypto_init
);
201 * ext4_exit_crypto() - Shutdown the ext4 encryption system
203 void ext4_exit_crypto(void)
205 struct ext4_crypto_ctx
*pos
, *n
;
207 list_for_each_entry_safe(pos
, n
, &ext4_free_crypto_ctxs
, free_list
) {
208 if (pos
->bounce_page
) {
210 EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
) {
211 __free_page(pos
->bounce_page
);
213 mempool_free(pos
->bounce_page
,
214 ext4_bounce_page_pool
);
218 crypto_free_tfm(pos
->tfm
);
221 INIT_LIST_HEAD(&ext4_free_crypto_ctxs
);
222 if (ext4_bounce_page_pool
)
223 mempool_destroy(ext4_bounce_page_pool
);
224 ext4_bounce_page_pool
= NULL
;
225 if (ext4_read_workqueue
)
226 destroy_workqueue(ext4_read_workqueue
);
227 ext4_read_workqueue
= NULL
;
231 * ext4_init_crypto() - Set up for ext4 encryption.
233 * We only call this when we start accessing encrypted files, since it
234 * results in memory getting allocated that wouldn't otherwise be used.
236 * Return: Zero on success, non-zero otherwise.
238 int ext4_init_crypto(void)
242 mutex_lock(&crypto_init
);
243 if (ext4_read_workqueue
)
244 goto already_initialized
;
245 ext4_read_workqueue
= alloc_workqueue("ext4_crypto", WQ_HIGHPRI
, 0);
246 if (!ext4_read_workqueue
) {
251 for (i
= 0; i
< num_prealloc_crypto_ctxs
; i
++) {
252 struct ext4_crypto_ctx
*ctx
;
254 ctx
= ext4_alloc_and_init_crypto_ctx(GFP_KERNEL
);
259 list_add(&ctx
->free_list
, &ext4_free_crypto_ctxs
);
262 ext4_bounce_page_pool
=
263 mempool_create_page_pool(num_prealloc_crypto_pages
, 0);
264 if (!ext4_bounce_page_pool
) {
269 mutex_unlock(&crypto_init
);
273 mutex_unlock(&crypto_init
);
277 void ext4_restore_control_page(struct page
*data_page
)
279 struct ext4_crypto_ctx
*ctx
=
280 (struct ext4_crypto_ctx
*)page_private(data_page
);
282 set_page_private(data_page
, (unsigned long)NULL
);
283 ClearPagePrivate(data_page
);
284 unlock_page(data_page
);
285 ext4_release_crypto_ctx(ctx
);
289 * ext4_crypt_complete() - The completion callback for page encryption
290 * @req: The asynchronous encryption request context
291 * @res: The result of the encryption operation
293 static void ext4_crypt_complete(struct crypto_async_request
*req
, int res
)
295 struct ext4_completion_result
*ecr
= req
->data
;
297 if (res
== -EINPROGRESS
)
300 complete(&ecr
->completion
);
308 static int ext4_page_crypto(struct ext4_crypto_ctx
*ctx
,
312 struct page
*src_page
,
313 struct page
*dest_page
)
316 u8 xts_tweak
[EXT4_XTS_TWEAK_SIZE
];
317 struct ablkcipher_request
*req
= NULL
;
318 DECLARE_EXT4_COMPLETION_RESULT(ecr
);
319 struct scatterlist dst
, src
;
320 struct ext4_inode_info
*ei
= EXT4_I(inode
);
321 struct crypto_ablkcipher
*atfm
= __crypto_ablkcipher_cast(ctx
->tfm
);
325 BUG_ON(ctx
->mode
!= ei
->i_encryption_key
.mode
);
327 if (ctx
->mode
!= EXT4_ENCRYPTION_MODE_AES_256_XTS
) {
328 printk_ratelimited(KERN_ERR
329 "%s: unsupported crypto algorithm: %d\n",
330 __func__
, ctx
->mode
);
334 crypto_ablkcipher_clear_flags(atfm
, ~0);
335 crypto_tfm_set_flags(ctx
->tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
337 res
= crypto_ablkcipher_setkey(atfm
, ei
->i_encryption_key
.raw
,
338 ei
->i_encryption_key
.size
);
340 printk_ratelimited(KERN_ERR
341 "%s: crypto_ablkcipher_setkey() failed\n",
345 req
= ablkcipher_request_alloc(atfm
, GFP_NOFS
);
347 printk_ratelimited(KERN_ERR
348 "%s: crypto_request_alloc() failed\n",
352 ablkcipher_request_set_callback(
353 req
, CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
354 ext4_crypt_complete
, &ecr
);
356 BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE
< sizeof(index
));
357 memcpy(xts_tweak
, &index
, sizeof(index
));
358 memset(&xts_tweak
[sizeof(index
)], 0,
359 EXT4_XTS_TWEAK_SIZE
- sizeof(index
));
361 sg_init_table(&dst
, 1);
362 sg_set_page(&dst
, dest_page
, PAGE_CACHE_SIZE
, 0);
363 sg_init_table(&src
, 1);
364 sg_set_page(&src
, src_page
, PAGE_CACHE_SIZE
, 0);
365 ablkcipher_request_set_crypt(req
, &src
, &dst
, PAGE_CACHE_SIZE
,
367 if (rw
== EXT4_DECRYPT
)
368 res
= crypto_ablkcipher_decrypt(req
);
370 res
= crypto_ablkcipher_encrypt(req
);
371 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
372 BUG_ON(req
->base
.data
!= &ecr
);
373 wait_for_completion(&ecr
.completion
);
376 ablkcipher_request_free(req
);
380 "%s: crypto_ablkcipher_encrypt() returned %d\n",
388 * ext4_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 * ext4_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
*ext4_encrypt(struct inode
*inode
,
403 struct page
*plaintext_page
)
405 struct ext4_crypto_ctx
*ctx
;
406 struct page
*ciphertext_page
= NULL
;
409 BUG_ON(!PageLocked(plaintext_page
));
411 ctx
= ext4_get_crypto_ctx(inode
);
413 return (struct page
*) ctx
;
415 /* The encryption operation will require a bounce page. */
416 ciphertext_page
= alloc_page(GFP_NOFS
);
417 if (!ciphertext_page
) {
418 /* This is a potential bottleneck, but at least we'll have
419 * forward progress. */
420 ciphertext_page
= mempool_alloc(ext4_bounce_page_pool
,
422 if (WARN_ON_ONCE(!ciphertext_page
)) {
423 ciphertext_page
= mempool_alloc(ext4_bounce_page_pool
,
424 GFP_NOFS
| __GFP_WAIT
);
426 ctx
->flags
&= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
;
428 ctx
->flags
|= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
;
430 ctx
->bounce_page
= ciphertext_page
;
431 ctx
->control_page
= plaintext_page
;
432 err
= ext4_page_crypto(ctx
, inode
, EXT4_ENCRYPT
, plaintext_page
->index
,
433 plaintext_page
, ciphertext_page
);
435 ext4_release_crypto_ctx(ctx
);
438 SetPagePrivate(ciphertext_page
);
439 set_page_private(ciphertext_page
, (unsigned long)ctx
);
440 lock_page(ciphertext_page
);
441 return ciphertext_page
;
445 * ext4_decrypt() - Decrypts a page in-place
446 * @ctx: The encryption context.
447 * @page: The page to decrypt. Must be locked.
449 * Decrypts page in-place using the ctx encryption context.
451 * Called from the read completion callback.
453 * Return: Zero on success, non-zero otherwise.
455 int ext4_decrypt(struct ext4_crypto_ctx
*ctx
, struct page
*page
)
457 BUG_ON(!PageLocked(page
));
459 return ext4_page_crypto(ctx
, page
->mapping
->host
,
460 EXT4_DECRYPT
, page
->index
, page
, page
);
464 * Convenience function which takes care of allocating and
465 * deallocating the encryption context
467 int ext4_decrypt_one(struct inode
*inode
, struct page
*page
)
471 struct ext4_crypto_ctx
*ctx
= ext4_get_crypto_ctx(inode
);
475 ret
= ext4_decrypt(ctx
, page
);
476 ext4_release_crypto_ctx(ctx
);
480 int ext4_encrypted_zeroout(struct inode
*inode
, struct ext4_extent
*ex
)
482 struct ext4_crypto_ctx
*ctx
;
483 struct page
*ciphertext_page
= NULL
;
485 ext4_lblk_t lblk
= ex
->ee_block
;
486 ext4_fsblk_t pblk
= ext4_ext_pblock(ex
);
487 unsigned int len
= ext4_ext_get_actual_len(ex
);
490 BUG_ON(inode
->i_sb
->s_blocksize
!= PAGE_CACHE_SIZE
);
492 ctx
= ext4_get_crypto_ctx(inode
);
496 ciphertext_page
= alloc_page(GFP_NOFS
);
497 if (!ciphertext_page
) {
498 /* This is a potential bottleneck, but at least we'll have
499 * forward progress. */
500 ciphertext_page
= mempool_alloc(ext4_bounce_page_pool
,
502 if (WARN_ON_ONCE(!ciphertext_page
)) {
503 ciphertext_page
= mempool_alloc(ext4_bounce_page_pool
,
504 GFP_NOFS
| __GFP_WAIT
);
506 ctx
->flags
&= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
;
508 ctx
->flags
|= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL
;
510 ctx
->bounce_page
= ciphertext_page
;
513 err
= ext4_page_crypto(ctx
, inode
, EXT4_ENCRYPT
, lblk
,
514 ZERO_PAGE(0), ciphertext_page
);
518 bio
= bio_alloc(GFP_KERNEL
, 1);
523 bio
->bi_bdev
= inode
->i_sb
->s_bdev
;
524 bio
->bi_iter
.bi_sector
= pblk
;
525 err
= bio_add_page(bio
, ciphertext_page
,
526 inode
->i_sb
->s_blocksize
, 0);
531 err
= submit_bio_wait(WRITE
, bio
);
537 ext4_release_crypto_ctx(ctx
);
541 bool ext4_valid_contents_enc_mode(uint32_t mode
)
543 return (mode
== EXT4_ENCRYPTION_MODE_AES_256_XTS
);
547 * ext4_validate_encryption_key_size() - Validate the encryption key size
548 * @mode: The key mode.
549 * @size: The key size to validate.
551 * Return: The validated key size for @mode. Zero if invalid.
553 uint32_t ext4_validate_encryption_key_size(uint32_t mode
, uint32_t size
)
555 if (size
== ext4_encryption_key_size(mode
))