1 ext4 crypto: add ext4 encryption facilities
3 From: Michael Halcrow <mhalcrow@google.com>
5 We encrypt into bounce pages and schedule them for block I/O. We
6 decrypt in-place in the newly added read completion callback.
8 The current encryption mode, AES-256-XTS, is the first of several
9 encryption modes on the roadmap. In-plan modes include HMAC-SHA1 for
10 integrity-only and AES-256-GCM for authenticated encryption. These
11 future modes depend on anticipated functionality for storing per-block
14 Signed-off-by: Michael Halcrow <mhalcrow@google.com>
15 Signed-off-by: Ildar Muslukhov <ildarm@google.com>
16 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
18 fs/ext4/Makefile | 9 +-
19 fs/ext4/crypto.c | 1133 ++++++++++++++++++++++++++++++++++++++++++++++++++
20 fs/ext4/ext4.h | 29 ++
21 fs/ext4/ext4_crypto.h | 172 ++++++++
22 fs/ext4/extents.c | 4 +-
23 fs/ext4/super.c | 38 +-
25 7 files changed, 1379 insertions(+), 7 deletions(-)
27 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
28 index 0310fec..de4de1c 100644
29 --- a/fs/ext4/Makefile
30 +++ b/fs/ext4/Makefile
33 obj-$(CONFIG_EXT4_FS) += ext4.o
35 -ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
36 - ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
37 - ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
38 - mmp.o indirect.o extents_status.o xattr.o xattr_user.o \
39 +ext4-y := balloc.o bitmap.o crypto.o dir.o file.o fsync.o ialloc.o \
40 + inode.o page-io.o ioctl.o namei.o super.o symlink.o \
41 + hash.o resize.o extents.o ext4_jbd2.o migrate.o \
42 + mballoc.o block_validity.o move_extent.o mmp.o \
43 + indirect.o extents_status.o xattr.o xattr_user.o \
44 xattr_trusted.o inline.o
46 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
47 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
49 index 0000000..80e6fac
51 +++ b/fs/ext4/crypto.c
54 + * linux/fs/ext4/crypto.c
56 + * This contains encryption functions for ext4
58 + * Written by Michael Halcrow, 2014.
60 + * This has not yet undergone a rigorous security audit.
62 + * The usage of AES-XTS should conform to recommendations in NIST
63 + * Special Publication 800-38E. The usage of AES-GCM should conform to
64 + * the recommendations in NIST Special Publication 800-38D. Further
65 + * guidance for block-oriented storage is in IEEE P1619/D16. The key
66 + * derivation code implements an HKDF (see RFC 5869).
69 +#include <crypto/hash.h>
70 +#include <crypto/sha.h>
71 +#include <keys/user-type.h>
72 +#include <keys/encrypted-type.h>
73 +#include <linux/crypto.h>
74 +#include <linux/gfp.h>
75 +#include <linux/kernel.h>
76 +#include <linux/key.h>
77 +#include <linux/list.h>
78 +#include <linux/mempool.h>
79 +#include <linux/random.h>
80 +#include <linux/scatterlist.h>
81 +#include <linux/spinlock_types.h>
86 +/* Encryption added and removed here! (L: */
88 +mempool_t *ext4_bounce_page_pool = NULL;
90 +LIST_HEAD(ext4_free_crypto_ctxs);
91 +DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
94 + * ext4_release_crypto_ctx() - Releases an encryption context
95 + * @ctx: The encryption context to release.
97 + * If the encryption context was allocated from the pre-allocated pool, returns
98 + * it to that pool. Else, frees it.
100 + * If there's a bounce page in the context, this frees that.
102 +void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
104 + unsigned long flags;
106 + atomic_dec(&ctx->dbg_refcnt);
107 + if (ctx->bounce_page) {
108 + if (ctx->flags & EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) {
109 + __free_page(ctx->bounce_page);
111 + mempool_free(ctx->bounce_page, ext4_bounce_page_pool);
113 + ctx->bounce_page = NULL;
115 + ctx->control_page = NULL;
116 + if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
118 + crypto_free_tfm(ctx->tfm);
121 + spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
122 + list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
123 + spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
128 + * ext4_alloc_and_init_crypto_ctx() - Allocates and inits an encryption context
129 + * @mask: The allocation mask.
131 + * Return: An allocated and initialized encryption context on success. An error
132 + * value or NULL otherwise.
134 +static struct ext4_crypto_ctx *ext4_alloc_and_init_crypto_ctx(u32 mask)
136 + struct ext4_crypto_ctx *ctx = kzalloc(sizeof(struct ext4_crypto_ctx),
140 + return ERR_PTR(-ENOMEM);
145 + * ext4_get_crypto_ctx() - Gets an encryption context
146 + * @with_page: If true, allocates and attaches a bounce page.
147 + * @key: The encryption key for the context.
149 + * Allocates and initializes an encryption context.
151 + * Return: An allocated and initialized encryption context on success; error
152 + * value or NULL otherwise.
154 +struct ext4_crypto_ctx *ext4_get_crypto_ctx(
155 + bool with_page, const struct ext4_encryption_key *key)
157 + struct ext4_crypto_ctx *ctx = NULL;
159 + unsigned long flags;
161 + /* We first try getting the ctx from a free list because in the common
162 + * case the ctx will have an allocated and initialized crypto tfm, so
163 + * it's probably a worthwhile optimization. For the bounce page, we
164 + * first try getting it from the kernel allocator because that's just
165 + * about as fast as getting it from a list and because a cache of free
166 + * pages should generally be a "last resort" option for a filesystem to
167 + * be able to do its job. */
168 + spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
169 + ctx = list_first_entry_or_null(&ext4_free_crypto_ctxs,
170 + struct ext4_crypto_ctx, free_list);
172 + list_del(&ctx->free_list);
173 + spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
175 + ctx = ext4_alloc_and_init_crypto_ctx(GFP_NOFS);
177 + res = PTR_ERR(ctx);
180 + ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
182 + ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
184 + atomic_set(&ctx->dbg_refcnt, 0);
186 + /* Allocate a new Crypto API context if we don't already have one or if
187 + * it isn't the right mode. */
188 + BUG_ON(key->mode == EXT4_ENCRYPTION_MODE_INVALID);
189 + if (ctx->tfm && (ctx->mode != key->mode)) {
190 + crypto_free_tfm(ctx->tfm);
192 + ctx->mode = EXT4_ENCRYPTION_MODE_INVALID;
195 + switch (key->mode) {
196 + case EXT4_ENCRYPTION_MODE_AES_256_XTS:
197 + ctx->tfm = crypto_ablkcipher_tfm(
198 + crypto_alloc_ablkcipher("xts(aes)", 0, 0));
200 + case EXT4_ENCRYPTION_MODE_AES_256_GCM:
201 + /* TODO(mhalcrow): AEAD w/ gcm(aes);
202 + * crypto_aead_setauthsize() */
203 + case EXT4_ENCRYPTION_MODE_HMAC_SHA1:
204 + /* TODO(mhalcrow): AHASH w/ hmac(sha1) */
205 + case EXT4_ENCRYPTION_MODE_AES_256_XTS_RANDOM_IV_HMAC_SHA1:
206 + ctx->tfm = ERR_PTR(-ENOTSUPP);
211 + if (IS_ERR_OR_NULL(ctx->tfm)) {
212 + res = PTR_ERR(ctx->tfm);
216 + ctx->mode = key->mode;
218 + BUG_ON(key->size != ext4_encryption_key_size(key->mode));
220 + /* There shouldn't be a bounce page attached to the crypto
221 + * context at this point. */
222 + BUG_ON(ctx->bounce_page);
226 + /* The encryption operation will require a bounce page. */
227 + ctx->bounce_page = alloc_page(GFP_NOFS);
228 + if (!ctx->bounce_page) {
229 + /* This is a potential bottleneck, but at least we'll have
230 + * forward progress. */
231 + ctx->bounce_page = mempool_alloc(ext4_bounce_page_pool,
233 + if (WARN_ON_ONCE(!ctx->bounce_page)) {
234 + ctx->bounce_page = mempool_alloc(ext4_bounce_page_pool,
235 + GFP_NOFS | __GFP_WAIT);
237 + ctx->flags &= ~EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
239 + ctx->flags |= EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL;
243 + if (!IS_ERR_OR_NULL(ctx))
244 + ext4_release_crypto_ctx(ctx);
245 + ctx = ERR_PTR(res);
250 +struct workqueue_struct *mpage_read_workqueue;
253 + * ext4_delete_crypto_ctxs() - Deletes/frees all encryption contexts
255 +static void ext4_delete_crypto_ctxs(void)
257 + struct ext4_crypto_ctx *pos, *n;
259 + list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list) {
260 + if (pos->bounce_page) {
262 + EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL) {
263 + __free_page(pos->bounce_page);
265 + mempool_free(pos->bounce_page,
266 + ext4_bounce_page_pool);
270 + crypto_free_tfm(pos->tfm);
276 + * ext4_allocate_crypto_ctxs() - Allocates a pool of encryption contexts
277 + * @num_to_allocate: The number of encryption contexts to allocate.
279 + * Return: Zero on success, non-zero otherwise.
281 +static int __init ext4_allocate_crypto_ctxs(size_t num_to_allocate)
283 + struct ext4_crypto_ctx *ctx = NULL;
285 + while (num_to_allocate > 0) {
286 + ctx = ext4_alloc_and_init_crypto_ctx(GFP_KERNEL);
289 + list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
293 + ext4_delete_crypto_ctxs();
294 + return PTR_ERR_OR_ZERO(ctx);
298 + * ext4_delete_crypto() - Frees all allocated encryption objects
300 +void ext4_delete_crypto(void)
302 + ext4_delete_crypto_ctxs();
303 + mempool_destroy(ext4_bounce_page_pool);
304 + destroy_workqueue(mpage_read_workqueue);
308 + * ext4_allocate_crypto() - Allocates encryption objects for later use
309 + * @num_crypto_pages: The number of bounce pages to allocate for encryption.
310 + * @num_crypto_ctxs: The number of encryption contexts to allocate.
312 + * Return: Zero on success, non-zero otherwise.
314 +int __init ext4_allocate_crypto(size_t num_crypto_pages, size_t num_crypto_ctxs)
318 + mpage_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
319 + if (!mpage_read_workqueue) {
323 + res = ext4_allocate_crypto_ctxs(num_crypto_ctxs);
326 + ext4_bounce_page_pool = mempool_create_page_pool(num_crypto_pages, 0);
327 + if (!ext4_bounce_page_pool)
331 + ext4_delete_crypto();
336 + * ext4_xts_tweak_for_page() - Generates an XTS tweak for a page
337 + * @xts_tweak: Buffer into which this writes the XTS tweak.
338 + * @page: The page for which this generates a tweak.
340 + * Generates an XTS tweak value for the given page.
342 +static void ext4_xts_tweak_for_page(u8 xts_tweak[EXT4_XTS_TWEAK_SIZE],
343 + const struct page *page)
345 + /* Only do this for XTS tweak values. For other modes (CBC,
346 + * GCM, etc.), you most like will need to do something
348 + BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE < sizeof(page->index));
349 + memcpy(xts_tweak, &page->index, sizeof(page->index));
350 + memset(&xts_tweak[sizeof(page->index)], 0,
351 + EXT4_XTS_TWEAK_SIZE - sizeof(page->index));
355 + * set_bh_to_page() - Re-assigns the pages for a set of buffer heads
356 + * @head: The head of the buffer list to reassign.
357 + * @page: The page to which to re-assign the buffer heads.
359 +void set_bh_to_page(struct buffer_head *head, struct page *page)
361 + struct buffer_head *bh = head;
364 + set_bh_page(bh, page, bh_offset(bh));
365 + if (PageDirty(page))
366 + set_buffer_dirty(bh);
367 + if (!bh->b_this_page)
368 + bh->b_this_page = head;
369 + } while ((bh = bh->b_this_page) != head);
372 +struct ext4_crypt_result {
373 + struct completion completion;
378 + * ext4_crypt_complete() - The completion callback for page encryption
379 + * @req: The asynchronous encryption request context
380 + * @res: The result of the encryption operation
382 +static void ext4_crypt_complete(struct crypto_async_request *req, int res)
384 + struct ext4_crypt_result *ecr = req->data;
386 + if (res == -EINPROGRESS)
389 + complete(&ecr->completion);
393 + * ext4_prep_pages_for_write() - Prepares pages for write
394 + * @ciphertext_page: Ciphertext page that will actually be written.
395 + * @plaintext_page: Plaintext page that acts as a control page.
396 + * @ctx: Encryption context for the pages.
398 +static void ext4_prep_pages_for_write(struct page *ciphertext_page,
399 + struct page *plaintext_page,
400 + struct ext4_crypto_ctx *ctx)
402 + SetPageDirty(ciphertext_page);
403 + SetPagePrivate(ciphertext_page);
404 + ctx->control_page = plaintext_page;
405 + set_page_private(ciphertext_page, (unsigned long)ctx);
406 + set_bh_to_page(page_buffers(plaintext_page), ciphertext_page);
410 + * ext4_xts_encrypt() - Encrypts a page using AES-256-XTS
411 + * @ctx: The encryption context.
412 + * @plaintext_page: The page to encrypt. Must be locked.
414 + * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
415 + * encryption context. Uses AES-256-XTS.
417 + * Called on the page write path.
419 + * Return: An allocated page with the encrypted content on success. Else, an
420 + * error value or NULL.
422 +struct page *ext4_xts_encrypt(struct ext4_crypto_ctx *ctx,
423 + struct page *plaintext_page)
425 + struct page *ciphertext_page = ctx->bounce_page;
426 + u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
427 + struct ablkcipher_request *req = NULL;
428 + struct ext4_crypt_result ecr;
429 + struct scatterlist dst, src;
430 + struct ext4_inode_info *ei = EXT4_I(plaintext_page->mapping->host);
431 + struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm);
434 + BUG_ON(!ciphertext_page);
436 + BUG_ON(ei->i_encryption_key.mode != EXT4_ENCRYPTION_MODE_AES_256_XTS);
437 + crypto_ablkcipher_clear_flags(atfm, ~0);
438 + crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
440 + /* Since in AES-256-XTS mode we only perform one cryptographic operation
441 + * on each block and there are no constraints about how many blocks a
442 + * single key can encrypt, we directly use the inode master key */
443 + res = crypto_ablkcipher_setkey(atfm, ei->i_encryption_key.raw,
444 + ei->i_encryption_key.size);
445 + req = ablkcipher_request_alloc(atfm, GFP_NOFS);
447 + printk_ratelimited(KERN_ERR
448 + "%s: crypto_request_alloc() failed\n",
450 + ciphertext_page = ERR_PTR(-ENOMEM);
453 + ablkcipher_request_set_callback(
454 + req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
455 + ext4_crypt_complete, &ecr);
456 + ext4_xts_tweak_for_page(xts_tweak, plaintext_page);
457 + sg_init_table(&dst, 1);
458 + sg_set_page(&dst, ciphertext_page, PAGE_CACHE_SIZE, 0);
459 + sg_init_table(&src, 1);
460 + sg_set_page(&src, plaintext_page, PAGE_CACHE_SIZE, 0);
461 + ablkcipher_request_set_crypt(req, &src, &dst, PAGE_CACHE_SIZE,
463 + res = crypto_ablkcipher_encrypt(req);
464 + if (res == -EINPROGRESS || res == -EBUSY) {
465 + BUG_ON(req->base.data != &ecr);
466 + wait_for_completion(&ecr.completion);
469 + ablkcipher_request_free(req);
471 + printk_ratelimited(
473 + "%s: crypto_ablkcipher_encrypt() returned %d\n",
475 + ciphertext_page = ERR_PTR(res);
479 + return ciphertext_page;
483 + * ext4_encrypt() - Encrypts a page
484 + * @ctx: The encryption context.
485 + * @plaintext_page: The page to encrypt. Must be locked.
487 + * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
488 + * encryption context.
490 + * Called on the page write path.
492 + * Return: An allocated page with the encrypted content on success. Else, an
493 + * error value or NULL.
495 +struct page *ext4_encrypt(struct ext4_crypto_ctx *ctx,
496 + struct page *plaintext_page)
498 + struct page *ciphertext_page = NULL;
500 + BUG_ON(!PageLocked(plaintext_page));
501 + switch (ctx->mode) {
502 + case EXT4_ENCRYPTION_MODE_AES_256_XTS:
503 + ciphertext_page = ext4_xts_encrypt(ctx, plaintext_page);
505 + case EXT4_ENCRYPTION_MODE_AES_256_GCM:
506 + /* TODO(mhalcrow): We'll need buffers for the
507 + * generated IV and/or auth tag for this mode and the
509 + case EXT4_ENCRYPTION_MODE_HMAC_SHA1:
510 + case EXT4_ENCRYPTION_MODE_AES_256_XTS_RANDOM_IV_HMAC_SHA1:
511 + ciphertext_page = ERR_PTR(-ENOTSUPP);
516 + if (!IS_ERR_OR_NULL(ciphertext_page))
517 + ext4_prep_pages_for_write(ciphertext_page, plaintext_page, ctx);
518 + return ciphertext_page;
522 + * ext4_xts_decrypt() - Decrypts a page using AES-256-XTS
523 + * @ctx: The encryption context.
524 + * @page: The page to decrypt. Must be locked.
526 + * Return: Zero on success, non-zero otherwise.
528 +int ext4_xts_decrypt(struct ext4_crypto_ctx *ctx, struct page *page)
530 + u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
531 + struct ablkcipher_request *req = NULL;
532 + struct ext4_crypt_result ecr;
533 + struct scatterlist sg;
534 + struct ext4_inode_info *ei = EXT4_I(page->mapping->host);
535 + struct crypto_ablkcipher *atfm = __crypto_ablkcipher_cast(ctx->tfm);
539 + BUG_ON(ei->i_encryption_key.mode != EXT4_ENCRYPTION_MODE_AES_256_XTS);
540 + crypto_ablkcipher_clear_flags(atfm, ~0);
541 + crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
543 + /* Since in AES-256-XTS mode we only perform one cryptographic operation
544 + * on each block and there are no constraints about how many blocks a
545 + * single key can encrypt, we directly use the inode master key */
546 + res = crypto_ablkcipher_setkey(atfm, ei->i_encryption_key.raw,
547 + ei->i_encryption_key.size);
548 + req = ablkcipher_request_alloc(atfm, GFP_NOFS);
553 + ablkcipher_request_set_callback(
554 + req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
555 + ext4_crypt_complete, &ecr);
556 + ext4_xts_tweak_for_page(xts_tweak, page);
557 + sg_init_table(&sg, 1);
558 + sg_set_page(&sg, page, PAGE_CACHE_SIZE, 0);
559 + ablkcipher_request_set_crypt(req, &sg, &sg, PAGE_CACHE_SIZE, xts_tweak);
560 + res = crypto_ablkcipher_decrypt(req);
561 + if (res == -EINPROGRESS || res == -EBUSY) {
562 + BUG_ON(req->base.data != &ecr);
563 + wait_for_completion(&ecr.completion);
566 + ablkcipher_request_free(req);
569 + printk_ratelimited(KERN_ERR "%s: res = [%d]\n", __func__, res);
574 + * ext4_decrypt() - Decrypts a page in-place
575 + * @ctx: The encryption context.
576 + * @page: The page to decrypt. Must be locked.
578 + * Decrypts page in-place using the ctx encryption context.
580 + * Called from the read completion callback.
582 + * Return: Zero on success, non-zero otherwise.
584 +int ext4_decrypt(struct ext4_crypto_ctx *ctx, struct page *page)
588 + BUG_ON(!PageLocked(page));
589 + switch (ctx->mode) {
590 + case EXT4_ENCRYPTION_MODE_AES_256_XTS:
591 + res = ext4_xts_decrypt(ctx, page);
593 + case EXT4_ENCRYPTION_MODE_AES_256_GCM:
594 + case EXT4_ENCRYPTION_MODE_HMAC_SHA1:
595 + case EXT4_ENCRYPTION_MODE_AES_256_XTS_RANDOM_IV_HMAC_SHA1:
605 + * ext4_get_wrapping_key_from_keyring() - Gets a wrapping key from the keyring
606 + * @wrapping_key: Buffer into which this writes the wrapping key.
607 + * @sig: The signature for the wrapping key.
609 + * Return: Zero on success, non-zero otherwise.
611 +static int ext4_get_wrapping_key_from_keyring(
612 + char wrapping_key[EXT4_MAX_KEY_SIZE],
613 + const char sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE])
615 + struct key *create_key;
616 + struct encrypted_key_payload *payload;
617 + struct ecryptfs_auth_tok *auth_tok;
619 + create_key = request_key(&key_type_user, sig, NULL);
620 + if (WARN_ON_ONCE(IS_ERR(create_key)))
622 + payload = (struct encrypted_key_payload *)create_key->payload.data;
623 + if (WARN_ON_ONCE(create_key->datalen !=
624 + sizeof(struct ecryptfs_auth_tok))) {
627 + auth_tok = (struct ecryptfs_auth_tok *)(&(payload)->payload_data);
628 + if (WARN_ON_ONCE(!(auth_tok->token.password.flags &
629 + ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))) {
632 + BUILD_BUG_ON(EXT4_MAX_KEY_SIZE < EXT4_AES_256_XTS_KEY_SIZE);
633 + BUILD_BUG_ON(ECRYPTFS_MAX_KEY_BYTES < EXT4_AES_256_XTS_KEY_SIZE);
634 + memcpy(wrapping_key,
635 + auth_tok->token.password.session_key_encryption_key,
636 + EXT4_AES_256_XTS_KEY_SIZE);
641 + * ext4_wrapping_key_sig_for_parent_dir() - Gets the key signature for
642 + * the parent directory
643 + * @sig: Buffer into which this writes the wrapping key signature.
645 + * Return: Zero on success, non-zero otherwise.
647 +static int ext4_wrapping_key_sig_for_parent_dir(
648 + char sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE])
650 + /* TODO(mhalcrow): Here's where we can check for wrapping key
651 + * specifier in parent directory xattr. */
656 + * ext4_get_wrapping_key() - Gets the wrapping key from the user session keyring
657 + * @wrapping_key: Buffer into which this writes the wrapping key.
658 + * @sig: Buffer into which this writes the wrapping key signature.
659 + * @inode: The inode for the wrapping key.
661 + * Return: Zero on success, non-zero otherwise.
663 +static int ext4_get_wrapping_key(
664 + char wrapping_key[EXT4_AES_256_XTS_KEY_SIZE],
665 + char sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE],
666 + const struct inode *inode)
668 + struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
669 + int res = ext4_wrapping_key_sig_for_parent_dir(sig);
672 + BUILD_BUG_ON(ECRYPTFS_SIG_SIZE_HEX + 1 !=
673 + EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE);
675 + sbi->s_default_encryption_wrapper_desc.wrapping_key_sig,
676 + EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE);
678 + BUG_ON(sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE - 1] != '\0');
679 + res = ext4_get_wrapping_key_from_keyring(wrapping_key, sig);
684 + * ext4_validate_encryption_mode() - Validates the encryption key mode
685 + * @mode: The key mode to validate.
687 + * Return: The validated key mode. EXT4_ENCRYPTION_MODE_INVALID if invalid.
689 +static uint32_t ext4_validate_encryption_mode(uint32_t mode)
692 + case EXT4_ENCRYPTION_MODE_AES_256_XTS:
697 + return EXT4_ENCRYPTION_MODE_INVALID;
701 + * ext4_validate_encryption_key_size() - Validate the encryption key size
702 + * @mode: The key mode.
703 + * @size: The key size to validate.
705 + * Return: The validated key size for @mode. Zero if invalid.
707 +static uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
709 + if (size == ext4_encryption_key_size(mode))
714 +struct ext4_hmac_result {
715 + struct completion completion;
720 + * ext4_hmac_complete() - Completion for async HMAC
721 + * @req: The async request.
722 + * @res: The result of the HMAC operation.
724 +static void ext4_hmac_complete(struct crypto_async_request *req, int res)
726 + struct ext4_hmac_result *ehr = req->data;
728 + if (res == -EINPROGRESS)
731 + complete(&ehr->completion);
735 + * ext4_hmac() - Generates an HMAC
736 + * @derivation: If true, derive a key. Else, generate an integrity HMAC.
737 + * @key: The HMAC key.
738 + * @key_size: The size of @key.
739 + * @src: The data to HMAC.
740 + * @src_size: The size of @src.
741 + * @dst: The target buffer for the generated HMAC.
742 + * @dst_size: The size of @dst.
744 + * Return: Zero on success, non-zero otherwise.
746 +static int ext4_hmac(bool derivation, const char *key, size_t key_size,
747 + const char *src, size_t src_size, char *dst,
750 + struct scatterlist sg;
751 + struct ahash_request *req = NULL;
752 + struct ext4_hmac_result ehr;
753 + char hmac[SHA512_DIGEST_SIZE];
754 + struct crypto_ahash *tfm = crypto_alloc_ahash(derivation ?
756 + "hmac(sha1)", 0, 0);
759 + BUG_ON(dst_size > SHA512_DIGEST_SIZE);
761 + return PTR_ERR(tfm);
762 + req = ahash_request_alloc(tfm, GFP_NOFS);
767 + ahash_request_set_callback(req,
768 + (CRYPTO_TFM_REQ_MAY_BACKLOG |
769 + CRYPTO_TFM_REQ_MAY_SLEEP),
770 + ext4_hmac_complete, &ehr);
772 + res = crypto_ahash_setkey(tfm, key, key_size);
775 + sg_init_one(&sg, src, src_size);
776 + ahash_request_set_crypt(req, &sg, hmac, src_size);
777 + init_completion(&ehr.completion);
778 + res = crypto_ahash_digest(req);
779 + if (res == -EINPROGRESS || res == -EBUSY) {
780 + BUG_ON(req->base.data != &ehr);
781 + wait_for_completion(&ehr.completion);
786 + memcpy(dst, hmac, dst_size);
788 + crypto_free_ahash(tfm);
790 + ahash_request_free(req);
795 + * ext4_hmac_derive_key() - Generates an HMAC for an key derivation (HKDF)
796 + * @key: The master key.
797 + * @key_size: The size of @key.
798 + * @src: The derivation data.
799 + * @src_size: The size of @src.
800 + * @dst: The target buffer for the derived key.
801 + * @dst_size: The size of @dst.
803 + * Return: Zero on success, non-zero otherwise.
805 +static int ext4_hmac_derive_key(const char *key, size_t key_size,
806 + const char *src, size_t src_size, char *dst,
809 + return ext4_hmac(true, key, key_size, src, src_size, dst, dst_size);
813 + * ext4_hmac_integrity() - Generates an HMAC for an integrity measurement
814 + * @key: The HMAC key.
815 + * @key_size: The size of @key.
816 + * @src: The data to generate the HMAC over.
817 + * @src_size: The size of @src.
818 + * @dst: The target buffer for the HMAC.
819 + * @dst_size: The size of @dst.
821 + * Return: Zero on success, non-zero otherwise.
823 +static int ext4_hmac_integrity(const char *key, size_t key_size,
824 + const char *src, size_t src_size, char *dst,
827 + return ext4_hmac(false, key, key_size, src, src_size, dst, dst_size);
831 + * ext4_crypt_wrapper_virt() - Encrypts a key
832 + * @enc_key: The wrapping key.
833 + * @iv: The initialization vector for the key encryption.
834 + * @src_virt: The source key object to wrap.
835 + * @dst_virt: The buffer for the wrapped key object.
836 + * @size: The size of the key object (identical for wrapped or unwrapped).
837 + * @enc: If 0, decrypt. Else, encrypt.
839 + * Uses the wrapped key to unwrap the encryption key.
841 + * Return: Zero on success, non-zero otherwise.
843 +static int ext4_crypt_wrapper_virt(const char *enc_key, const char *iv,
844 + const char *src_virt, char *dst_virt,
845 + size_t size, bool enc)
847 + struct scatterlist dst, src;
848 + struct blkcipher_desc desc = {
849 + .flags = CRYPTO_TFM_REQ_MAY_SLEEP
853 + desc.tfm = crypto_alloc_blkcipher("ctr(aes)", 0, CRYPTO_ALG_ASYNC);
854 + if (IS_ERR(desc.tfm))
855 + return PTR_ERR(desc.tfm);
858 + crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
859 + sg_init_one(&dst, dst_virt, size);
860 + sg_init_one(&src, src_virt, size);
861 + crypto_blkcipher_set_iv(desc.tfm, iv, EXT4_WRAPPING_IV_SIZE);
862 + res = crypto_blkcipher_setkey(desc.tfm, enc_key,
863 + EXT4_AES_256_CTR_KEY_SIZE);
867 + res = crypto_blkcipher_encrypt(&desc, &dst, &src, size);
869 + res = crypto_blkcipher_decrypt(&desc, &dst, &src, size);
871 + crypto_free_blkcipher(desc.tfm);
876 + * ext4_unwrap_key() - Unwraps the encryption key for the inode
877 + * @wrapped_key_packet: The wrapped encryption key packet.
878 + * @wrapped_key_packet_size: The wrapped encryption key packet size.
879 + * @key: The encryption key to fill in with unwrapped data.
881 + * Uses the wrapped key to unwrap the encryption key.
883 + * Return: Zero on success, non-zero otherwise.
885 +static int ext4_unwrap_key(const char *wrapped_key_packet,
886 + size_t wrapped_key_packet_size,
887 + struct ext4_encryption_key *key)
889 + struct ext4_wrapped_key_packet *packet =
890 + (struct ext4_wrapped_key_packet *)wrapped_key_packet;
891 + uint32_t packet_size = ntohl(*(uint32_t *)packet->size);
892 + struct ext4_encryption_key_packet key_packet;
893 + char wrapping_key[EXT4_AES_256_XTS_KEY_SIZE];
894 + char enc_key[EXT4_AES_256_CTR_KEY_SIZE];
895 + char int_key[EXT4_HMAC_KEY_SIZE];
896 + char hmac[EXT4_HMAC_SIZE];
897 + char hmac_invalid = 0;
901 + if (wrapped_key_packet_size < sizeof(packet_size))
903 + BUILD_BUG_ON(sizeof(struct ext4_wrapped_key_packet) !=
904 + EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE);
905 + if (packet_size != sizeof(struct ext4_wrapped_key_packet))
907 + if (wrapped_key_packet_size != packet_size)
909 + if (packet->type != EXT4_KEY_PACKET_TYPE_WRAPPED_KEY_V0)
911 + if (packet->sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE - 1] != '\0')
913 + res = ext4_get_wrapping_key_from_keyring(wrapping_key, packet->sig);
917 + /* Always validate the HMAC as soon as we get the key to do so */
918 + packet->nonce[EXT4_NONCE_SIZE] = EXT4_WRAPPING_INT_DERIVATION_TWEAK;
919 + res = ext4_hmac_derive_key(wrapping_key, EXT4_AES_256_XTS_KEY_SIZE,
921 + EXT4_DERIVATION_TWEAK_NONCE_SIZE, int_key,
922 + EXT4_HMAC_KEY_SIZE);
925 + res = ext4_hmac_integrity(int_key, EXT4_HMAC_KEY_SIZE,
926 + wrapped_key_packet,
927 + (EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE -
928 + EXT4_HMAC_SIZE), hmac, EXT4_HMAC_SIZE);
929 + memset(int_key, 0, EXT4_HMAC_KEY_SIZE);
930 + for (i = 0; i < EXT4_HMAC_SIZE; ++i)
931 + hmac_invalid |= (packet->hmac[i] ^ hmac[i]);
932 + if (hmac_invalid) {
933 + printk_ratelimited(
935 + "%s: Security warning: Wrapped key HMAC check failed\n",
941 + /* The HMAC validated. Decrypt the key packet. */
942 + packet->nonce[EXT4_NONCE_SIZE] = EXT4_WRAPPING_ENC_DERIVATION_TWEAK;
943 + res = ext4_hmac_derive_key(wrapping_key, EXT4_AES_256_XTS_KEY_SIZE,
945 + EXT4_DERIVATION_TWEAK_NONCE_SIZE, enc_key,
946 + EXT4_AES_256_CTR_KEY_SIZE);
949 + res = ext4_crypt_wrapper_virt(enc_key, packet->iv,
950 + packet->wrapped_key_packet,
951 + (char *)&key_packet,
952 + EXT4_V0_SERIALIZED_KEY_SIZE, false);
953 + memset(enc_key, 0, EXT4_AES_256_CTR_KEY_SIZE);
956 + key->mode = ext4_validate_encryption_mode(
957 + ntohl(*((uint32_t *)key_packet.mode)));
958 + if (key->mode == EXT4_ENCRYPTION_MODE_INVALID) {
962 + memcpy(key->raw, key_packet.raw, EXT4_MAX_KEY_SIZE);
963 + memset(key_packet.raw, 0, EXT4_MAX_KEY_SIZE);
964 + key->size = ext4_validate_encryption_key_size(
965 + key->mode, ntohl(*((uint32_t *)key_packet.size)));
972 + key->mode = EXT4_ENCRYPTION_MODE_INVALID;
973 + memset(wrapping_key, 0, EXT4_AES_256_XTS_KEY_SIZE);
978 + * ext4_wrap_key() - Wraps the encryption key for the inode
979 + * @wrapped_crypto_key: The buffer into which this writes the wrapped key.
980 + * @key_packet_size: The size of the packet.
981 + * @key: The encryption key.
982 + * @inode: The inode for the encryption key.
984 + * Generates a wrapped key packet from an encryption key and a wrapping key for
987 + * Return: Zero on success, non-zero otherwise.
989 +static int ext4_wrap_key(char *wrapped_key_packet, size_t *key_packet_size,
990 + const struct ext4_encryption_key *key,
991 + const struct inode *inode)
993 + struct ext4_wrapped_key_packet *packet =
994 + (struct ext4_wrapped_key_packet *)wrapped_key_packet;
995 + struct ext4_encryption_key_packet key_packet;
996 + char wrapping_key[EXT4_AES_256_XTS_KEY_SIZE];
997 + char enc_key[EXT4_AES_256_CTR_KEY_SIZE];
998 + char int_key[EXT4_HMAC_KEY_SIZE];
1001 + BUILD_BUG_ON(sizeof(struct ext4_wrapped_key_packet) !=
1002 + EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE);
1003 + if (!wrapped_key_packet) {
1004 + *key_packet_size = EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE;
1007 + res = ext4_get_wrapping_key(wrapping_key, packet->sig, inode);
1010 + BUG_ON(*key_packet_size != EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE);
1012 + /* Size, type, nonce, and IV */
1013 + *((uint32_t *)packet->size) =
1014 + htonl(EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE);
1015 + packet->type = EXT4_KEY_PACKET_TYPE_WRAPPED_KEY_V0;
1016 + get_random_bytes(packet->nonce, EXT4_NONCE_SIZE);
1017 + get_random_bytes(packet->iv, EXT4_WRAPPING_IV_SIZE);
1019 + /* Derive the wrapping encryption key from the wrapping key */
1020 + packet->nonce[EXT4_NONCE_SIZE] = EXT4_WRAPPING_ENC_DERIVATION_TWEAK;
1021 + res = ext4_hmac_derive_key(wrapping_key, EXT4_AES_256_XTS_KEY_SIZE,
1023 + EXT4_DERIVATION_TWEAK_NONCE_SIZE,
1024 + enc_key, EXT4_AES_256_CTR_KEY_SIZE);
1028 + /* Wrap the data key with the wrapping encryption key */
1029 + *((uint32_t *)key_packet.mode) = htonl(key->mode);
1030 + memcpy(key_packet.raw, key->raw, EXT4_MAX_KEY_SIZE);
1031 + *((uint32_t *)key_packet.size) = htonl(key->size);
1032 + BUILD_BUG_ON(sizeof(struct ext4_encryption_key_packet) !=
1033 + EXT4_V0_SERIALIZED_KEY_SIZE);
1034 + res = ext4_crypt_wrapper_virt(enc_key, packet->iv, (char *)&key_packet,
1035 + (char *)&packet->wrapped_key_packet,
1036 + EXT4_V0_SERIALIZED_KEY_SIZE, true);
1037 + memset(enc_key, 0, EXT4_AES_256_CTR_KEY_SIZE);
1038 + memset(key_packet.raw, 0, EXT4_MAX_KEY_SIZE);
1042 + /* Calculate the HMAC over the entire packet (except, of
1043 + * course, the HMAC buffer at the end) */
1044 + packet->nonce[EXT4_NONCE_SIZE] = EXT4_WRAPPING_INT_DERIVATION_TWEAK;
1045 + res = ext4_hmac_derive_key(wrapping_key, EXT4_AES_256_XTS_KEY_SIZE,
1047 + EXT4_DERIVATION_TWEAK_NONCE_SIZE,
1048 + int_key, EXT4_HMAC_KEY_SIZE);
1051 + BUILD_BUG_ON(EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE < EXT4_HMAC_SIZE);
1052 + res = ext4_hmac_integrity(int_key, EXT4_HMAC_KEY_SIZE,
1053 + wrapped_key_packet,
1054 + (EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE -
1055 + EXT4_HMAC_SIZE), packet->hmac,
1057 + packet->nonce[EXT4_NONCE_SIZE] = 0; /* to catch decryption bugs */
1058 + memset(int_key, 0, EXT4_HMAC_KEY_SIZE);
1060 + memset(wrapping_key, 0, EXT4_AES_256_XTS_KEY_SIZE);
1065 + * ext4_generate_encryption_key() - Generates an encryption key
1066 + * @dentry: The dentry containing the encryption key this will set.
1068 +static void ext4_generate_encryption_key(const struct dentry *dentry)
1070 + struct ext4_inode_info *ei = EXT4_I(dentry->d_inode);
1071 + struct ext4_sb_info *sbi = EXT4_SB(dentry->d_sb);
1072 + struct ext4_encryption_key *key = &ei->i_encryption_key;
1074 + key->mode = sbi->s_default_encryption_mode;
1075 + key->size = ext4_encryption_key_size(key->mode);
1076 + BUG_ON(!key->size);
1077 + get_random_bytes(key->raw, key->size);
1081 + * ext4_set_crypto_key() - Generates and sets the encryption key for the inode
1082 + * @dentry: The dentry for the encryption key.
1084 + * Generates the encryption key for the inode. Generates and writes the
1085 + * encryption metadata for the inode.
1087 + * Return: Zero on success, non-zero otherwise.
1089 +int ext4_set_crypto_key(struct dentry *dentry)
1091 + char root_packet[EXT4_PACKET_SET_V0_MAX_SIZE];
1092 + char *wrapped_key_packet = &root_packet[EXT4_PACKET_HEADER_SIZE];
1093 + size_t wrapped_key_packet_size = EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE;
1094 + size_t root_packet_size = (EXT4_PACKET_HEADER_SIZE +
1095 + wrapped_key_packet_size);
1096 + struct inode *inode = dentry->d_inode;
1097 + struct ext4_inode_info *ei = EXT4_I(inode);
1101 + ext4_generate_encryption_key(dentry);
1102 + res = ext4_wrap_key(wrapped_key_packet, &wrapped_key_packet_size,
1103 + &ei->i_encryption_key, inode);
1106 + root_packet[0] = EXT4_PACKET_SET_VERSION_V0;
1107 + BUILD_BUG_ON(EXT4_PACKET_SET_V0_MAX_SIZE !=
1108 + (EXT4_PACKET_HEADER_SIZE +
1109 + EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE));
1110 + BUG_ON(sizeof(root_packet) != root_packet_size);
1111 + res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION_METADATA, "",
1112 + root_packet, root_packet_size, 0);
1115 + if (res == -EINTR)
1117 + ei->i_encryption_key.mode = EXT4_ENCRYPTION_MODE_INVALID;
1118 + printk_ratelimited(KERN_ERR "%s: res = [%d]\n", __func__, res);
1124 + * ext4_get_root_packet() - Reads the root packet
1125 + * @inode: The inode containing the root packet.
1126 + * @root_packet: The root packet.
1127 + * @root_packet_size: The size of the root packet. Set by this if
1128 + * root_packet == NULL.
1130 + * Return: Zero on success, non-zero otherwise.
1132 +static int ext4_get_root_packet(struct inode *inode, char *root_packet,
1133 + size_t *root_packet_size)
1135 + int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION_METADATA,
1139 + if (!root_packet) {
1140 + *root_packet_size = res;
1143 + if (res != *root_packet_size)
1145 + res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION_METADATA, "",
1146 + root_packet, res);
1147 + if (root_packet[0] != EXT4_PACKET_SET_VERSION_V0) {
1148 + printk_ratelimited(
1150 + "%s: Expected root packet version [%d]; got [%d]\n",
1151 + __func__, EXT4_PACKET_SET_VERSION_V0, root_packet[0]);
1158 + * ext4_get_crypto_key() - Gets the encryption key for the inode
1159 + * @file: The file for the encryption key.
1161 + * Return: Zero on success, non-zero otherwise.
1163 +int ext4_get_crypto_key(const struct file *file)
1165 + char root_packet[EXT4_PACKET_SET_V0_MAX_SIZE];
1166 + char *wrapped_key_packet = &root_packet[EXT4_PACKET_HEADER_SIZE];
1167 + size_t wrapped_key_packet_size = EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE;
1168 + size_t root_packet_size = (EXT4_PACKET_HEADER_SIZE +
1169 + wrapped_key_packet_size);
1170 + struct inode *inode = file->f_mapping->host;
1171 + struct ext4_inode_info *ei = EXT4_I(inode);
1172 + int res = ext4_get_root_packet(inode, root_packet, &root_packet_size);
1176 + res = ext4_unwrap_key(wrapped_key_packet,
1177 + EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE,
1178 + &ei->i_encryption_key);
1183 + ei->i_encryption_key.mode = EXT4_ENCRYPTION_MODE_INVALID;
1186 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
1187 index c24665e..bdedbe8 100644
1188 --- a/fs/ext4/ext4.h
1189 +++ b/fs/ext4/ext4.h
1191 #include <linux/ratelimit.h>
1192 #include <crypto/hash.h>
1193 #include <linux/falloc.h>
1194 +#include <linux/ecryptfs.h>
1196 #include <linux/compat.h>
1198 @@ -797,6 +798,8 @@ do { \
1200 #endif /* defined(__KERNEL__) || defined(__linux__) */
1202 +#include "ext4_crypto.h"
1204 #include "extents_status.h"
1207 @@ -934,6 +937,10 @@ struct ext4_inode_info {
1209 /* Precomputed uuid+inum+igen checksum for seeding inode checksums */
1212 + /* Encryption params */
1213 + struct ext4_encryption_key i_encryption_key;
1214 + struct ext4_encryption_wrapper_desc i_encryption_wrapper_desc;
1218 @@ -1334,6 +1341,10 @@ struct ext4_sb_info {
1219 struct ratelimit_state s_err_ratelimit_state;
1220 struct ratelimit_state s_warning_ratelimit_state;
1221 struct ratelimit_state s_msg_ratelimit_state;
1224 + uint32_t s_default_encryption_mode;
1225 + struct ext4_encryption_wrapper_desc s_default_encryption_wrapper_desc;
1228 static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
1229 @@ -2798,6 +2809,24 @@ static inline void set_bitmap_uptodate(struct buffer_head *bh)
1230 set_bit(BH_BITMAP_UPTODATE, &(bh)->b_state);
1234 +extern struct workqueue_struct *mpage_read_workqueue;
1235 +int ext4_allocate_crypto(size_t num_crypto_pages, size_t num_crypto_ctxs);
1236 +void ext4_delete_crypto(void);
1237 +struct ext4_crypto_ctx *ext4_get_crypto_ctx(
1238 + bool with_page, const struct ext4_encryption_key *key);
1239 +void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx);
1240 +void set_bh_to_page(struct buffer_head *head, struct page *page);
1241 +struct page *ext4_encrypt(struct ext4_crypto_ctx *ctx,
1242 + struct page *plaintext_page);
1243 +int ext4_decrypt(struct ext4_crypto_ctx *ctx, struct page *page);
1244 +int ext4_get_crypto_key(const struct file *file);
1245 +int ext4_set_crypto_key(struct dentry *dentry);
1246 +static inline bool ext4_is_encryption_enabled(struct ext4_inode_info *ei)
1248 + return ei->i_encryption_key.mode != EXT4_ENCRYPTION_MODE_INVALID;
1252 * Disable DIO read nolock optimization, so new dioreaders will be forced
1254 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
1255 new file mode 100644
1256 index 0000000..6cb5ba9
1258 +++ b/fs/ext4/ext4_crypto.h
1261 + * linux/fs/ext4/ext4_crypto.h
1263 + * This contains encryption header content for ext4
1265 + * Written by Michael Halcrow, 2014.
1268 +#ifndef _EXT4_CRYPTO_H
1269 +#define _EXT4_CRYPTO_H
1271 +/* Encryption parameters */
1272 +#define EXT4_AES_256_XTS_KEY_SIZE 64
1273 +#define EXT4_XTS_TWEAK_SIZE 16
1274 +#define EXT4_AES_256_CTR_KEY_SIZE 32
1275 +#define EXT4_AES_256_ECB_KEY_SIZE 32
1276 +#define EXT4_HMAC_KEY_SIZE 12
1277 +#define EXT4_HMAC_SIZE 12
1278 +#define EXT4_NONCE_SIZE 12
1279 +#define EXT4_DERIVATION_TWEAK_SIZE 1
1280 +#define EXT4_DERIVATION_TWEAK_NONCE_SIZE (EXT4_NONCE_SIZE + \
1281 + EXT4_DERIVATION_TWEAK_SIZE)
1282 +#define EXT4_WRAPPING_ENC_DERIVATION_TWEAK 'e'
1283 +#define EXT4_WRAPPING_INT_DERIVATION_TWEAK 'i'
1284 +#define EXT4_AES_256_XTS_RANDOMIV_HMAC_SHA1_KEY_SIZE \
1285 + (EXT4_AES_256_XTS_KEY_SIZE + EXT4_HMAC_KEY_SIZE)
1286 +#define EXT4_AES_256_GCM_KEY_SIZE 32
1287 +#define EXT4_AES_256_GCM_AUTH_SIZE 16
1288 +#define EXT4_GCM_ASSOC_DATA_SIZE sizeof(pgoff_t)
1289 +#define EXT4_PAGE_REGION_INDEX_SHIFT 16 /* 2**16-sized regions */
1290 +#define EXT4_MAX_KEY_SIZE EXT4_AES_256_XTS_RANDOMIV_HMAC_SHA1_KEY_SIZE
1291 +#define EXT4_MAX_IV_SIZE AES_BLOCK_SIZE
1292 +#define EXT4_MAX_AUTH_SIZE EXT4_AES_256_GCM_AUTH_SIZE
1294 +/* The metadata directory is only necessary only for the sibling file
1295 + * directory under the mount root, which will be replaced by per-block
1296 + * metadata when it's ready. */
1297 +#define EXT4_METADATA_DIRECTORY_NAME ".ext4_crypt_data"
1298 +#define EXT4_METADATA_DIRECTORY_NAME_SIZE 16
1302 + * 4 bytes: Size of packet (inclusive of these 4 bytes)
1303 + * 1 byte: Packet type/version
1304 + * Variable bytes: Packet content (may contain nested packets)
1306 + * Packets may be nested. The top-level packet is the "packet set".
1308 +#define EXT4_PACKET_SET_VERSION_V0 ((char)0x00)
1309 +#define EXT4_PACKET_SET_VERSION_SIZE 1
1310 +#define EXT4_PACKET_SIZE_SIZE 4
1311 +#define EXT4_PACKET_TYPE_SIZE 1
1312 +#define EXT4_PACKET_HEADER_SIZE (EXT4_PACKET_SIZE_SIZE + EXT4_PACKET_TYPE_SIZE)
1315 + * Wrapped key packet format:
1316 + * 4 bytes: Size of packet (inclusive of these 4 bytes)
1317 + * 1 byte: Packet type/version (0x00)
1318 + * 17 bytes: NULL-terminated wrapping key signature (printable)
1319 + * 13 bytes: Derivation nonce (last byte ignored)
1321 + * Variable bytes: Serialized key, AES-256-CTR encrypted
1322 + * 12 bytes: HMAC-SHA1(everything preceding)
1324 +#define EXT4_KEY_PACKET_TYPE_WRAPPED_KEY_V0 ((char)0x00)
1325 +#define EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE (ECRYPTFS_SIG_SIZE_HEX + 1)
1326 +#define EXT4_WRAPPING_IV_SIZE 16
1328 +/* These #defines may seem redundant to the sizeof the structs below
1329 + * them. Since naively changing the structs can result in nasty bugs
1330 + * that might have security implications, we use the explict sizes
1331 + * together with BUILD_BUG_ON() to help avoid mistakes. */
1332 +#define EXT4_V0_SERIALIZED_KEY_SIZE (sizeof(uint32_t) + \
1333 + EXT4_MAX_KEY_SIZE + \
1335 +#define EXT4_WRAPPED_KEY_PACKET_V0_SIZE ( \
1336 + EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE + \
1337 + EXT4_DERIVATION_TWEAK_NONCE_SIZE + \
1338 + EXT4_WRAPPING_IV_SIZE + \
1339 + EXT4_V0_SERIALIZED_KEY_SIZE + \
1342 +#define EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE ((uint32_t)( \
1343 + EXT4_PACKET_HEADER_SIZE + \
1344 + EXT4_WRAPPED_KEY_PACKET_V0_SIZE))
1346 +/* V0 supports only one key in a fixed xattr space. If/when compelling
1347 + * requirements come along, future versions may be able to use
1348 + * (non-xattr) metadata storage to store an arbitrary number of
1349 + * wrapped keys. In the meantime, we won't spend the code complexity
1350 + * budget on supporting multiple wrapped keys. */
1351 +#define EXT4_PACKET_SET_V0_MAX_WRAPPED_KEYS 1
1352 +#define EXT4_PACKET_SET_V0_MAX_SIZE ((uint32_t)( \
1353 + EXT4_PACKET_HEADER_SIZE + \
1354 + (EXT4_FULL_WRAPPED_KEY_PACKET_V0_SIZE * \
1355 + EXT4_PACKET_SET_V0_MAX_WRAPPED_KEYS)))
1357 +/* Don't change this without also changing the packet type. Serialized
1358 + * packets are cast directly into this struct. */
1359 +struct ext4_encryption_key_packet {
1360 + char mode[sizeof(uint32_t)]; /* Network byte order */
1361 + char raw[EXT4_MAX_KEY_SIZE];
1362 + char size[sizeof(uint32_t)]; /* Network byte order */
1363 +} __attribute__((__packed__));
1366 + * If you change the existing modes (order or type), you'll need to
1367 + * change the packet type too.
1369 +enum ext4_encryption_mode {
1370 + EXT4_ENCRYPTION_MODE_INVALID = 0,
1371 + EXT4_ENCRYPTION_MODE_AES_256_XTS,
1372 + EXT4_ENCRYPTION_MODE_AES_256_GCM,
1373 + EXT4_ENCRYPTION_MODE_HMAC_SHA1,
1374 + EXT4_ENCRYPTION_MODE_AES_256_XTS_RANDOM_IV_HMAC_SHA1,
1377 +struct ext4_encryption_key {
1379 + char raw[EXT4_MAX_KEY_SIZE];
1383 +/* Don't change this without also changing the packet type. Serialized
1384 + * packets are cast directly into this struct. */
1385 +struct ext4_wrapped_key_packet {
1386 + char size[sizeof(uint32_t)]; /* Network byte order */
1388 + char sig[EXT4_WRAPPING_KEY_SIG_NULL_TERMINATED_SIZE];
1389 + char nonce[EXT4_DERIVATION_TWEAK_NONCE_SIZE];
1390 + char iv[EXT4_WRAPPING_IV_SIZE];
1391 + char wrapped_key_packet[sizeof(struct ext4_encryption_key_packet)];
1392 + char hmac[EXT4_HMAC_SIZE];
1393 +} __attribute__((__packed__));
1395 +struct ext4_encryption_wrapper_desc {
1396 + char wrapping_key_sig[ECRYPTFS_SIG_SIZE_HEX + 1];
1399 +#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
1400 +#define EXT4_BOUNCE_PAGE_REQUIRES_FREE_ENCRYPT_FL 0x00000002
1402 +struct ext4_crypto_ctx {
1403 + struct crypto_tfm *tfm; /* Crypto API context */
1404 + struct page *bounce_page; /* Ciphertext page on write path */
1405 + struct page *control_page; /* Original page on write path */
1406 + struct bio *bio; /* The bio for this context */
1407 + struct work_struct work; /* Work queue for read complete path */
1408 + struct list_head free_list; /* Free list */
1409 + int flags; /* Flags */
1410 + enum ext4_encryption_mode mode; /* Encryption mode for tfm */
1411 + atomic_t dbg_refcnt; /* TODO(mhalcrow): Remove for release */
1414 +static inline int ext4_encryption_key_size(enum ext4_encryption_mode mode)
1417 + case EXT4_ENCRYPTION_MODE_AES_256_XTS:
1418 + return EXT4_AES_256_XTS_KEY_SIZE;
1419 + case EXT4_ENCRYPTION_MODE_AES_256_GCM:
1420 + return EXT4_AES_256_GCM_KEY_SIZE;
1421 + case EXT4_ENCRYPTION_MODE_HMAC_SHA1:
1422 + return EXT4_HMAC_KEY_SIZE;
1423 + case EXT4_ENCRYPTION_MODE_AES_256_XTS_RANDOM_IV_HMAC_SHA1:
1424 + return EXT4_AES_256_XTS_RANDOMIV_HMAC_SHA1_KEY_SIZE;
1431 +#endif /* _EXT4_CRYPTO_H */
1432 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
1433 index e5d3ead..ad022e6 100644
1434 --- a/fs/ext4/extents.c
1435 +++ b/fs/ext4/extents.c
1436 @@ -4915,6 +4915,7 @@ out_mutex:
1437 long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1439 struct inode *inode = file_inode(file);
1440 + struct ext4_inode_info *ei = EXT4_I(inode);
1441 loff_t new_size = 0;
1442 unsigned int max_blocks;
1444 @@ -4924,7 +4925,8 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1446 /* Return error if mode is not supported */
1447 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1448 - FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
1449 + FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE) ||
1450 + ext4_is_encryption_enabled(ei))
1453 if (mode & FALLOC_FL_PUNCH_HOLE)
1454 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
1455 index 29a016d..cbcece1 100644
1456 --- a/fs/ext4/super.c
1457 +++ b/fs/ext4/super.c
1458 @@ -892,6 +892,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
1459 atomic_set(&ei->i_ioend_count, 0);
1460 atomic_set(&ei->i_unwritten, 0);
1461 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1462 + ei->i_encryption_key.mode = EXT4_ENCRYPTION_MODE_INVALID;
1464 return &ei->vfs_inode;
1466 @@ -1137,7 +1138,7 @@ enum {
1467 Opt_inode_readahead_blks, Opt_journal_ioprio,
1468 Opt_dioread_nolock, Opt_dioread_lock,
1469 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1470 - Opt_max_dir_size_kb, Opt_nojournal_checksum,
1471 + Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_encrypt_key_sig,
1474 static const match_table_t tokens = {
1475 @@ -1216,6 +1217,7 @@ static const match_table_t tokens = {
1476 {Opt_init_itable, "init_itable"},
1477 {Opt_noinit_itable, "noinit_itable"},
1478 {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1479 + {Opt_encrypt_key_sig, "encrypt_key_sig=%s"},
1480 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
1481 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
1482 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
1483 @@ -1416,6 +1418,7 @@ static const struct mount_opts {
1484 {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
1485 {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1486 {Opt_max_dir_size_kb, 0, MOPT_GTE0},
1487 + {Opt_encrypt_key_sig, 0, MOPT_STRING},
1491 @@ -1523,6 +1526,28 @@ static int handle_mount_opt(struct super_block *sb, char *opt, int token,
1492 sbi->s_li_wait_mult = arg;
1493 } else if (token == Opt_max_dir_size_kb) {
1494 sbi->s_max_dir_size_kb = arg;
1495 + } else if (token == Opt_encrypt_key_sig) {
1496 + char *encrypt_key_sig;
1498 + encrypt_key_sig = match_strdup(&args[0]);
1499 + if (!encrypt_key_sig) {
1500 + ext4_msg(sb, KERN_ERR,
1501 + "error: could not dup encryption key sig string");
1504 + if (strlen(encrypt_key_sig) != ECRYPTFS_SIG_SIZE_HEX) {
1505 + ext4_msg(sb, KERN_ERR,
1506 + "error: encryption key sig string must be length %d",
1507 + ECRYPTFS_SIG_SIZE_HEX);
1510 + sbi->s_default_encryption_mode =
1511 + EXT4_ENCRYPTION_MODE_AES_256_XTS;
1512 + memcpy(sbi->s_default_encryption_wrapper_desc.wrapping_key_sig,
1514 + ECRYPTFS_SIG_SIZE_HEX);
1515 + sbi->s_default_encryption_wrapper_desc.wrapping_key_sig[
1516 + ECRYPTFS_SIG_SIZE_HEX] = '\0';
1517 } else if (token == Opt_stripe) {
1518 sbi->s_stripe = arg;
1519 } else if (token == Opt_resuid) {
1520 @@ -5553,6 +5578,8 @@ struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
1521 static int __init ext4_init_fs(void)
1524 + static size_t num_prealloc_crypto_pages = 32;
1525 + static size_t num_prealloc_crypto_ctxs = 128;
1527 ext4_li_info = NULL;
1528 mutex_init(&ext4_li_mtx);
1529 @@ -5565,10 +5592,15 @@ static int __init ext4_init_fs(void)
1530 init_waitqueue_head(&ext4__ioend_wq[i]);
1533 - err = ext4_init_es();
1534 + err = ext4_allocate_crypto(num_prealloc_crypto_pages,
1535 + num_prealloc_crypto_ctxs);
1539 + err = ext4_init_es();
1543 err = ext4_init_pageio();
1546 @@ -5621,6 +5653,8 @@ out6:
1551 + ext4_delete_crypto();
1555 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
1556 index 29bedf5..29d47c7 100644
1557 --- a/fs/ext4/xattr.h
1558 +++ b/fs/ext4/xattr.h
1560 #define EXT4_XATTR_INDEX_SECURITY 6
1561 #define EXT4_XATTR_INDEX_SYSTEM 7
1562 #define EXT4_XATTR_INDEX_RICHACL 8
1563 +#define EXT4_XATTR_INDEX_ENCRYPTION_METADATA 9
1565 struct ext4_xattr_header {
1566 __le32 h_magic; /* magic number for identification */