1 ext4 crypto: migrate into vfs's crypto engine
3 To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, tytso@mit.edu,
5 This patch removes the most parts of internal crypto codes.
6 And then, it modifies and adds some ext4-specific crypt codes to use the generic
9 Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
10 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
12 fs/ext4/Kconfig | 12 +-
13 fs/ext4/Makefile | 2 -
14 fs/ext4/crypto.c | 536 ------------------------------------------------
15 fs/ext4/crypto_fname.c | 468 ------------------------------------------
16 fs/ext4/crypto_key.c | 274 -------------------------
17 fs/ext4/crypto_policy.c | 229 ---------------------
18 fs/ext4/dir.c | 26 +--
19 fs/ext4/ext4.h | 197 ++++++------------
20 fs/ext4/ext4_crypto.h | 159 --------------
21 fs/ext4/file.c | 10 +-
22 fs/ext4/ialloc.c | 7 +-
23 fs/ext4/inline.c | 14 +-
24 fs/ext4/inode.c | 8 +-
25 fs/ext4/ioctl.c | 20 +-
26 fs/ext4/namei.c | 120 +++++------
27 fs/ext4/page-io.c | 13 +-
28 fs/ext4/readpage.c | 45 +---
29 fs/ext4/super.c | 85 +++++++-
30 fs/ext4/symlink.c | 33 ++-
31 19 files changed, 271 insertions(+), 1987 deletions(-)
32 delete mode 100644 fs/ext4/crypto.c
33 delete mode 100644 fs/ext4/crypto_fname.c
34 delete mode 100644 fs/ext4/crypto_key.c
35 delete mode 100644 fs/ext4/crypto_policy.c
36 delete mode 100644 fs/ext4/ext4_crypto.h
38 diff --git a/fs/ext4/Kconfig b/fs/ext4/Kconfig
39 index b46e9fc..e38039f 100644
42 @@ -99,17 +99,9 @@ config EXT4_FS_SECURITY
43 extended attributes for file security labels, say N.
45 config EXT4_ENCRYPTION
46 - tristate "Ext4 Encryption"
47 + bool "Ext4 Encryption"
55 - select CRYPTO_SHA256
57 - select ENCRYPTED_KEYS
58 + select FS_ENCRYPTION
60 Enable encryption of ext4 files and directories. This
61 feature is similar to ecryptfs, but it is more memory
62 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
63 index f52cf54..354103f 100644
64 --- a/fs/ext4/Makefile
65 +++ b/fs/ext4/Makefile
66 @@ -12,5 +12,3 @@ ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
68 ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
69 ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
70 -ext4-$(CONFIG_EXT4_FS_ENCRYPTION) += crypto_policy.o crypto.o \
71 - crypto_key.o crypto_fname.o
72 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
73 deleted file mode 100644
74 index 6a6c273..0000000
75 --- a/fs/ext4/crypto.c
79 - * linux/fs/ext4/crypto.c
81 - * Copyright (C) 2015, Google, Inc.
83 - * This contains encryption functions for ext4
85 - * Written by Michael Halcrow, 2014.
87 - * Filename encryption additions
88 - * Uday Savagaonkar, 2014
89 - * Encryption policy handling additions
90 - * Ildar Muslukhov, 2014
92 - * This has not yet undergone a rigorous security audit.
94 - * The usage of AES-XTS should conform to recommendations in NIST
95 - * Special Publication 800-38E and IEEE P1619/D16.
98 -#include <crypto/skcipher.h>
99 -#include <keys/user-type.h>
100 -#include <keys/encrypted-type.h>
101 -#include <linux/ecryptfs.h>
102 -#include <linux/gfp.h>
103 -#include <linux/kernel.h>
104 -#include <linux/key.h>
105 -#include <linux/list.h>
106 -#include <linux/mempool.h>
107 -#include <linux/module.h>
108 -#include <linux/mutex.h>
109 -#include <linux/random.h>
110 -#include <linux/scatterlist.h>
111 -#include <linux/spinlock_types.h>
112 -#include <linux/namei.h>
114 -#include "ext4_extents.h"
117 -/* Encryption added and removed here! (L: */
119 -static unsigned int num_prealloc_crypto_pages = 32;
120 -static unsigned int num_prealloc_crypto_ctxs = 128;
122 -module_param(num_prealloc_crypto_pages, uint, 0444);
123 -MODULE_PARM_DESC(num_prealloc_crypto_pages,
124 - "Number of crypto pages to preallocate");
125 -module_param(num_prealloc_crypto_ctxs, uint, 0444);
126 -MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
127 - "Number of crypto contexts to preallocate");
129 -static mempool_t *ext4_bounce_page_pool;
131 -static LIST_HEAD(ext4_free_crypto_ctxs);
132 -static DEFINE_SPINLOCK(ext4_crypto_ctx_lock);
134 -static struct kmem_cache *ext4_crypto_ctx_cachep;
135 -struct kmem_cache *ext4_crypt_info_cachep;
138 - * ext4_release_crypto_ctx() - Releases an encryption context
139 - * @ctx: The encryption context to release.
141 - * If the encryption context was allocated from the pre-allocated pool, returns
142 - * it to that pool. Else, frees it.
144 - * If there's a bounce page in the context, this frees that.
146 -void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx)
148 - unsigned long flags;
150 - if (ctx->flags & EXT4_WRITE_PATH_FL && ctx->w.bounce_page)
151 - mempool_free(ctx->w.bounce_page, ext4_bounce_page_pool);
152 - ctx->w.bounce_page = NULL;
153 - ctx->w.control_page = NULL;
154 - if (ctx->flags & EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL) {
155 - kmem_cache_free(ext4_crypto_ctx_cachep, ctx);
157 - spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
158 - list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
159 - spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
164 - * ext4_get_crypto_ctx() - Gets an encryption context
165 - * @inode: The inode for which we are doing the crypto
167 - * Allocates and initializes an encryption context.
169 - * Return: An allocated and initialized encryption context on success; error
170 - * value or NULL otherwise.
172 -struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
175 - struct ext4_crypto_ctx *ctx = NULL;
177 - unsigned long flags;
178 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
181 - return ERR_PTR(-ENOKEY);
184 - * We first try getting the ctx from a free list because in
185 - * the common case the ctx will have an allocated and
186 - * initialized crypto tfm, so it's probably a worthwhile
187 - * optimization. For the bounce page, we first try getting it
188 - * from the kernel allocator because that's just about as fast
189 - * as getting it from a list and because a cache of free pages
190 - * should generally be a "last resort" option for a filesystem
191 - * to be able to do its job.
193 - spin_lock_irqsave(&ext4_crypto_ctx_lock, flags);
194 - ctx = list_first_entry_or_null(&ext4_free_crypto_ctxs,
195 - struct ext4_crypto_ctx, free_list);
197 - list_del(&ctx->free_list);
198 - spin_unlock_irqrestore(&ext4_crypto_ctx_lock, flags);
200 - ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, gfp_flags);
205 - ctx->flags |= EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
207 - ctx->flags &= ~EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL;
209 - ctx->flags &= ~EXT4_WRITE_PATH_FL;
213 - if (!IS_ERR_OR_NULL(ctx))
214 - ext4_release_crypto_ctx(ctx);
215 - ctx = ERR_PTR(res);
220 -struct workqueue_struct *ext4_read_workqueue;
221 -static DEFINE_MUTEX(crypto_init);
224 - * ext4_exit_crypto() - Shutdown the ext4 encryption system
226 -void ext4_exit_crypto(void)
228 - struct ext4_crypto_ctx *pos, *n;
230 - list_for_each_entry_safe(pos, n, &ext4_free_crypto_ctxs, free_list)
231 - kmem_cache_free(ext4_crypto_ctx_cachep, pos);
232 - INIT_LIST_HEAD(&ext4_free_crypto_ctxs);
233 - if (ext4_bounce_page_pool)
234 - mempool_destroy(ext4_bounce_page_pool);
235 - ext4_bounce_page_pool = NULL;
236 - if (ext4_read_workqueue)
237 - destroy_workqueue(ext4_read_workqueue);
238 - ext4_read_workqueue = NULL;
239 - if (ext4_crypto_ctx_cachep)
240 - kmem_cache_destroy(ext4_crypto_ctx_cachep);
241 - ext4_crypto_ctx_cachep = NULL;
242 - if (ext4_crypt_info_cachep)
243 - kmem_cache_destroy(ext4_crypt_info_cachep);
244 - ext4_crypt_info_cachep = NULL;
248 - * ext4_init_crypto() - Set up for ext4 encryption.
250 - * We only call this when we start accessing encrypted files, since it
251 - * results in memory getting allocated that wouldn't otherwise be used.
253 - * Return: Zero on success, non-zero otherwise.
255 -int ext4_init_crypto(void)
257 - int i, res = -ENOMEM;
259 - mutex_lock(&crypto_init);
260 - if (ext4_read_workqueue)
261 - goto already_initialized;
262 - ext4_read_workqueue = alloc_workqueue("ext4_crypto", WQ_HIGHPRI, 0);
263 - if (!ext4_read_workqueue)
266 - ext4_crypto_ctx_cachep = KMEM_CACHE(ext4_crypto_ctx,
267 - SLAB_RECLAIM_ACCOUNT);
268 - if (!ext4_crypto_ctx_cachep)
271 - ext4_crypt_info_cachep = KMEM_CACHE(ext4_crypt_info,
272 - SLAB_RECLAIM_ACCOUNT);
273 - if (!ext4_crypt_info_cachep)
276 - for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
277 - struct ext4_crypto_ctx *ctx;
279 - ctx = kmem_cache_zalloc(ext4_crypto_ctx_cachep, GFP_NOFS);
284 - list_add(&ctx->free_list, &ext4_free_crypto_ctxs);
287 - ext4_bounce_page_pool =
288 - mempool_create_page_pool(num_prealloc_crypto_pages, 0);
289 - if (!ext4_bounce_page_pool) {
293 -already_initialized:
294 - mutex_unlock(&crypto_init);
297 - ext4_exit_crypto();
298 - mutex_unlock(&crypto_init);
302 -void ext4_restore_control_page(struct page *data_page)
304 - struct ext4_crypto_ctx *ctx =
305 - (struct ext4_crypto_ctx *)page_private(data_page);
307 - set_page_private(data_page, (unsigned long)NULL);
308 - ClearPagePrivate(data_page);
309 - unlock_page(data_page);
310 - ext4_release_crypto_ctx(ctx);
314 - * ext4_crypt_complete() - The completion callback for page encryption
315 - * @req: The asynchronous encryption request context
316 - * @res: The result of the encryption operation
318 -static void ext4_crypt_complete(struct crypto_async_request *req, int res)
320 - struct ext4_completion_result *ecr = req->data;
322 - if (res == -EINPROGRESS)
325 - complete(&ecr->completion);
333 -static int ext4_page_crypto(struct inode *inode,
334 - ext4_direction_t rw,
336 - struct page *src_page,
337 - struct page *dest_page,
341 - u8 xts_tweak[EXT4_XTS_TWEAK_SIZE];
342 - struct skcipher_request *req = NULL;
343 - DECLARE_EXT4_COMPLETION_RESULT(ecr);
344 - struct scatterlist dst, src;
345 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
346 - struct crypto_skcipher *tfm = ci->ci_ctfm;
349 - req = skcipher_request_alloc(tfm, gfp_flags);
351 - printk_ratelimited(KERN_ERR
352 - "%s: crypto_request_alloc() failed\n",
356 - skcipher_request_set_callback(
357 - req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
358 - ext4_crypt_complete, &ecr);
360 - BUILD_BUG_ON(EXT4_XTS_TWEAK_SIZE < sizeof(index));
361 - memcpy(xts_tweak, &index, sizeof(index));
362 - memset(&xts_tweak[sizeof(index)], 0,
363 - EXT4_XTS_TWEAK_SIZE - sizeof(index));
365 - sg_init_table(&dst, 1);
366 - sg_set_page(&dst, dest_page, PAGE_SIZE, 0);
367 - sg_init_table(&src, 1);
368 - sg_set_page(&src, src_page, PAGE_SIZE, 0);
369 - skcipher_request_set_crypt(req, &src, &dst, PAGE_SIZE,
371 - if (rw == EXT4_DECRYPT)
372 - res = crypto_skcipher_decrypt(req);
374 - res = crypto_skcipher_encrypt(req);
375 - if (res == -EINPROGRESS || res == -EBUSY) {
376 - wait_for_completion(&ecr.completion);
379 - skcipher_request_free(req);
381 - printk_ratelimited(
383 - "%s: crypto_skcipher_encrypt() returned %d\n",
390 -static struct page *alloc_bounce_page(struct ext4_crypto_ctx *ctx,
393 - ctx->w.bounce_page = mempool_alloc(ext4_bounce_page_pool, gfp_flags);
394 - if (ctx->w.bounce_page == NULL)
395 - return ERR_PTR(-ENOMEM);
396 - ctx->flags |= EXT4_WRITE_PATH_FL;
397 - return ctx->w.bounce_page;
401 - * ext4_encrypt() - Encrypts a page
402 - * @inode: The inode for which the encryption should take place
403 - * @plaintext_page: The page to encrypt. Must be locked.
405 - * Allocates a ciphertext page and encrypts plaintext_page into it using the ctx
406 - * encryption context.
408 - * Called on the page write path. The caller must call
409 - * ext4_restore_control_page() on the returned ciphertext page to
410 - * release the bounce buffer and the encryption context.
412 - * Return: An allocated page with the encrypted content on success. Else, an
413 - * error value or NULL.
415 -struct page *ext4_encrypt(struct inode *inode,
416 - struct page *plaintext_page,
419 - struct ext4_crypto_ctx *ctx;
420 - struct page *ciphertext_page = NULL;
423 - BUG_ON(!PageLocked(plaintext_page));
425 - ctx = ext4_get_crypto_ctx(inode, gfp_flags);
427 - return (struct page *) ctx;
429 - /* The encryption operation will require a bounce page. */
430 - ciphertext_page = alloc_bounce_page(ctx, gfp_flags);
431 - if (IS_ERR(ciphertext_page))
433 - ctx->w.control_page = plaintext_page;
434 - err = ext4_page_crypto(inode, EXT4_ENCRYPT, plaintext_page->index,
435 - plaintext_page, ciphertext_page, gfp_flags);
437 - ciphertext_page = ERR_PTR(err);
439 - ext4_release_crypto_ctx(ctx);
440 - return ciphertext_page;
442 - SetPagePrivate(ciphertext_page);
443 - set_page_private(ciphertext_page, (unsigned long)ctx);
444 - lock_page(ciphertext_page);
445 - return ciphertext_page;
449 - * ext4_decrypt() - Decrypts a page in-place
450 - * @ctx: The encryption context.
451 - * @page: The page to decrypt. Must be locked.
453 - * Decrypts page in-place using the ctx encryption context.
455 - * Called from the read completion callback.
457 - * Return: Zero on success, non-zero otherwise.
459 -int ext4_decrypt(struct page *page)
461 - BUG_ON(!PageLocked(page));
463 - return ext4_page_crypto(page->mapping->host, EXT4_DECRYPT,
464 - page->index, page, page, GFP_NOFS);
467 -int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
468 - ext4_fsblk_t pblk, ext4_lblk_t len)
470 - struct ext4_crypto_ctx *ctx;
471 - struct page *ciphertext_page = NULL;
476 - ext4_msg(inode->i_sb, KERN_CRIT,
477 - "ext4_encrypted_zeroout ino %lu lblk %u len %u",
478 - (unsigned long) inode->i_ino, lblk, len);
481 - BUG_ON(inode->i_sb->s_blocksize != PAGE_SIZE);
483 - ctx = ext4_get_crypto_ctx(inode, GFP_NOFS);
485 - return PTR_ERR(ctx);
487 - ciphertext_page = alloc_bounce_page(ctx, GFP_NOWAIT);
488 - if (IS_ERR(ciphertext_page)) {
489 - err = PTR_ERR(ciphertext_page);
494 - err = ext4_page_crypto(inode, EXT4_ENCRYPT, lblk,
495 - ZERO_PAGE(0), ciphertext_page,
500 - bio = bio_alloc(GFP_NOWAIT, 1);
505 - bio->bi_bdev = inode->i_sb->s_bdev;
506 - bio->bi_iter.bi_sector =
507 - pblk << (inode->i_sb->s_blocksize_bits - 9);
508 - ret = bio_add_page(bio, ciphertext_page,
509 - inode->i_sb->s_blocksize, 0);
510 - if (ret != inode->i_sb->s_blocksize) {
511 - /* should never happen! */
512 - ext4_msg(inode->i_sb, KERN_ERR,
513 - "bio_add_page failed: %d", ret);
519 - err = submit_bio_wait(WRITE, bio);
520 - if ((err == 0) && bio->bi_error)
529 - ext4_release_crypto_ctx(ctx);
533 -bool ext4_valid_contents_enc_mode(uint32_t mode)
535 - return (mode == EXT4_ENCRYPTION_MODE_AES_256_XTS);
539 - * ext4_validate_encryption_key_size() - Validate the encryption key size
540 - * @mode: The key mode.
541 - * @size: The key size to validate.
543 - * Return: The validated key size for @mode. Zero if invalid.
545 -uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
547 - if (size == ext4_encryption_key_size(mode))
553 - * Validate dentries for encrypted directories to make sure we aren't
554 - * potentially caching stale data after a key has been added or
557 -static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
559 - struct dentry *dir;
560 - struct ext4_crypt_info *ci;
561 - int dir_has_key, cached_with_key;
563 - if (flags & LOOKUP_RCU)
566 - dir = dget_parent(dentry);
567 - if (!ext4_encrypted_inode(d_inode(dir))) {
571 - ci = EXT4_I(d_inode(dir))->i_crypt_info;
572 - if (ci && ci->ci_keyring_key &&
573 - (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
574 - (1 << KEY_FLAG_REVOKED) |
575 - (1 << KEY_FLAG_DEAD))))
578 - /* this should eventually be an flag in d_flags */
579 - cached_with_key = dentry->d_fsdata != NULL;
580 - dir_has_key = (ci != NULL);
584 - * If the dentry was cached without the key, and it is a
585 - * negative dentry, it might be a valid name. We can't check
586 - * if the key has since been made available due to locking
587 - * reasons, so we fail the validation so ext4_lookup() can do
590 - * We also fail the validation if the dentry was created with
591 - * the key present, but we no longer have the key, or vice versa.
593 - if ((!cached_with_key && d_is_negative(dentry)) ||
594 - (!cached_with_key && dir_has_key) ||
595 - (cached_with_key && !dir_has_key)) {
596 -#if 0 /* Revalidation debug */
598 - char *cp = simple_dname(dentry, buf, sizeof(buf));
601 - cp = (char *) "???";
602 - pr_err("revalidate: %s %p %d %d %d\n", cp, dentry->d_fsdata,
603 - cached_with_key, d_is_negative(dentry),
611 -const struct dentry_operations ext4_encrypted_d_ops = {
612 - .d_revalidate = ext4_d_revalidate,
614 diff --git a/fs/ext4/crypto_fname.c b/fs/ext4/crypto_fname.c
615 deleted file mode 100644
616 index 1a2f360..0000000
617 --- a/fs/ext4/crypto_fname.c
621 - * linux/fs/ext4/crypto_fname.c
623 - * Copyright (C) 2015, Google, Inc.
625 - * This contains functions for filename crypto management in ext4
627 - * Written by Uday Savagaonkar, 2014.
629 - * This has not yet undergone a rigorous security audit.
633 -#include <crypto/skcipher.h>
634 -#include <keys/encrypted-type.h>
635 -#include <keys/user-type.h>
636 -#include <linux/gfp.h>
637 -#include <linux/kernel.h>
638 -#include <linux/key.h>
639 -#include <linux/list.h>
640 -#include <linux/mempool.h>
641 -#include <linux/random.h>
642 -#include <linux/scatterlist.h>
643 -#include <linux/spinlock_types.h>
646 -#include "ext4_crypto.h"
650 - * ext4_dir_crypt_complete() -
652 -static void ext4_dir_crypt_complete(struct crypto_async_request *req, int res)
654 - struct ext4_completion_result *ecr = req->data;
656 - if (res == -EINPROGRESS)
659 - complete(&ecr->completion);
662 -bool ext4_valid_filenames_enc_mode(uint32_t mode)
664 - return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
667 -static unsigned max_name_len(struct inode *inode)
669 - return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
674 - * ext4_fname_encrypt() -
676 - * This function encrypts the input filename, and returns the length of the
677 - * ciphertext. Errors are returned as negative numbers. We trust the caller to
678 - * allocate sufficient memory to oname string.
680 -static int ext4_fname_encrypt(struct inode *inode,
681 - const struct qstr *iname,
682 - struct ext4_str *oname)
684 - u32 ciphertext_len;
685 - struct skcipher_request *req = NULL;
686 - DECLARE_EXT4_COMPLETION_RESULT(ecr);
687 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
688 - struct crypto_skcipher *tfm = ci->ci_ctfm;
690 - char iv[EXT4_CRYPTO_BLOCK_SIZE];
691 - struct scatterlist src_sg, dst_sg;
692 - int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
693 - char *workbuf, buf[32], *alloc_buf = NULL;
694 - unsigned lim = max_name_len(inode);
696 - if (iname->len <= 0 || iname->len > lim)
699 - ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
700 - EXT4_CRYPTO_BLOCK_SIZE : iname->len;
701 - ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
702 - ciphertext_len = (ciphertext_len > lim)
703 - ? lim : ciphertext_len;
705 - if (ciphertext_len <= sizeof(buf)) {
708 - alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
711 - workbuf = alloc_buf;
714 - /* Allocate request */
715 - req = skcipher_request_alloc(tfm, GFP_NOFS);
717 - printk_ratelimited(
718 - KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
722 - skcipher_request_set_callback(req,
723 - CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
724 - ext4_dir_crypt_complete, &ecr);
726 - /* Copy the input */
727 - memcpy(workbuf, iname->name, iname->len);
728 - if (iname->len < ciphertext_len)
729 - memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
731 - /* Initialize IV */
732 - memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
734 - /* Create encryption request */
735 - sg_init_one(&src_sg, workbuf, ciphertext_len);
736 - sg_init_one(&dst_sg, oname->name, ciphertext_len);
737 - skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
738 - res = crypto_skcipher_encrypt(req);
739 - if (res == -EINPROGRESS || res == -EBUSY) {
740 - wait_for_completion(&ecr.completion);
744 - skcipher_request_free(req);
746 - printk_ratelimited(
747 - KERN_ERR "%s: Error (error code %d)\n", __func__, res);
749 - oname->len = ciphertext_len;
754 - * ext4_fname_decrypt()
755 - * This function decrypts the input filename, and returns
756 - * the length of the plaintext.
757 - * Errors are returned as negative numbers.
758 - * We trust the caller to allocate sufficient memory to oname string.
760 -static int ext4_fname_decrypt(struct inode *inode,
761 - const struct ext4_str *iname,
762 - struct ext4_str *oname)
764 - struct ext4_str tmp_in[2], tmp_out[1];
765 - struct skcipher_request *req = NULL;
766 - DECLARE_EXT4_COMPLETION_RESULT(ecr);
767 - struct scatterlist src_sg, dst_sg;
768 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
769 - struct crypto_skcipher *tfm = ci->ci_ctfm;
771 - char iv[EXT4_CRYPTO_BLOCK_SIZE];
772 - unsigned lim = max_name_len(inode);
774 - if (iname->len <= 0 || iname->len > lim)
777 - tmp_in[0].name = iname->name;
778 - tmp_in[0].len = iname->len;
779 - tmp_out[0].name = oname->name;
781 - /* Allocate request */
782 - req = skcipher_request_alloc(tfm, GFP_NOFS);
784 - printk_ratelimited(
785 - KERN_ERR "%s: crypto_request_alloc() failed\n", __func__);
788 - skcipher_request_set_callback(req,
789 - CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
790 - ext4_dir_crypt_complete, &ecr);
792 - /* Initialize IV */
793 - memset(iv, 0, EXT4_CRYPTO_BLOCK_SIZE);
795 - /* Create encryption request */
796 - sg_init_one(&src_sg, iname->name, iname->len);
797 - sg_init_one(&dst_sg, oname->name, oname->len);
798 - skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
799 - res = crypto_skcipher_decrypt(req);
800 - if (res == -EINPROGRESS || res == -EBUSY) {
801 - wait_for_completion(&ecr.completion);
804 - skcipher_request_free(req);
806 - printk_ratelimited(
807 - KERN_ERR "%s: Error in ext4_fname_encrypt (error code %d)\n",
812 - oname->len = strnlen(oname->name, iname->len);
816 -static const char *lookup_table =
817 - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
820 - * ext4_fname_encode_digest() -
822 - * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
823 - * The encoded string is roughly 4/3 times the size of the input string.
825 -static int digest_encode(const char *src, int len, char *dst)
827 - int i = 0, bits = 0, ac = 0;
831 - ac += (((unsigned char) src[i]) << bits);
834 - *cp++ = lookup_table[ac & 0x3f];
837 - } while (bits >= 6);
841 - *cp++ = lookup_table[ac & 0x3f];
845 -static int digest_decode(const char *src, int len, char *dst)
847 - int i = 0, bits = 0, ac = 0;
852 - p = strchr(lookup_table, src[i]);
853 - if (p == NULL || src[i] == 0)
855 - ac += (p - lookup_table) << bits;
870 - * ext4_fname_crypto_round_up() -
872 - * Return: The next multiple of block size
874 -u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
876 - return ((size+blksize-1)/blksize)*blksize;
879 -unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen)
881 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
885 - padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
886 - if (ilen < EXT4_CRYPTO_BLOCK_SIZE)
887 - ilen = EXT4_CRYPTO_BLOCK_SIZE;
888 - return ext4_fname_crypto_round_up(ilen, padding);
892 - * ext4_fname_crypto_alloc_buffer() -
894 - * Allocates an output buffer that is sufficient for the crypto operation
895 - * specified by the context and the direction.
897 -int ext4_fname_crypto_alloc_buffer(struct inode *inode,
898 - u32 ilen, struct ext4_str *crypto_str)
900 - unsigned int olen = ext4_fname_encrypted_size(inode, ilen);
902 - crypto_str->len = olen;
903 - if (olen < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2)
904 - olen = EXT4_FNAME_CRYPTO_DIGEST_SIZE*2;
905 - /* Allocated buffer can hold one more character to null-terminate the
907 - crypto_str->name = kmalloc(olen+1, GFP_NOFS);
908 - if (!(crypto_str->name))
914 - * ext4_fname_crypto_free_buffer() -
916 - * Frees the buffer allocated for crypto operation.
918 -void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
922 - kfree(crypto_str->name);
923 - crypto_str->name = NULL;
927 - * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
929 -int _ext4_fname_disk_to_usr(struct inode *inode,
930 - struct dx_hash_info *hinfo,
931 - const struct ext4_str *iname,
932 - struct ext4_str *oname)
937 - if (iname->len < 3) {
938 - /*Check for . and .. */
939 - if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
940 - oname->name[0] = '.';
941 - oname->name[iname->len-1] = '.';
942 - oname->len = iname->len;
946 - if (iname->len < EXT4_CRYPTO_BLOCK_SIZE) {
947 - EXT4_ERROR_INODE(inode, "encrypted inode too small");
950 - if (EXT4_I(inode)->i_crypt_info)
951 - return ext4_fname_decrypt(inode, iname, oname);
953 - if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
954 - ret = digest_encode(iname->name, iname->len, oname->name);
959 - memcpy(buf, &hinfo->hash, 4);
960 - memcpy(buf+4, &hinfo->minor_hash, 4);
963 - memcpy(buf + 8, iname->name + iname->len - 16, 16);
964 - oname->name[0] = '_';
965 - ret = digest_encode(buf, 24, oname->name+1);
966 - oname->len = ret + 1;
970 -int ext4_fname_disk_to_usr(struct inode *inode,
971 - struct dx_hash_info *hinfo,
972 - const struct ext4_dir_entry_2 *de,
973 - struct ext4_str *oname)
975 - struct ext4_str iname = {.name = (unsigned char *) de->name,
976 - .len = de->name_len };
978 - return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
983 - * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
985 -int ext4_fname_usr_to_disk(struct inode *inode,
986 - const struct qstr *iname,
987 - struct ext4_str *oname)
990 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
992 - if (iname->len < 3) {
993 - /*Check for . and .. */
994 - if (iname->name[0] == '.' &&
995 - iname->name[iname->len-1] == '.') {
996 - oname->name[0] = '.';
997 - oname->name[iname->len-1] = '.';
998 - oname->len = iname->len;
1003 - res = ext4_fname_encrypt(inode, iname, oname);
1006 - /* Without a proper key, a user is not allowed to modify the filenames
1007 - * in a directory. Consequently, a user space name cannot be mapped to
1008 - * a disk-space name */
1012 -int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
1013 - int lookup, struct ext4_filename *fname)
1015 - struct ext4_crypt_info *ci;
1016 - int ret = 0, bigname = 0;
1018 - memset(fname, 0, sizeof(struct ext4_filename));
1019 - fname->usr_fname = iname;
1021 - if (!ext4_encrypted_inode(dir) ||
1022 - ((iname->name[0] == '.') &&
1023 - ((iname->len == 1) ||
1024 - ((iname->name[1] == '.') && (iname->len == 2))))) {
1025 - fname->disk_name.name = (unsigned char *) iname->name;
1026 - fname->disk_name.len = iname->len;
1029 - ret = ext4_get_encryption_info(dir);
1032 - ci = EXT4_I(dir)->i_crypt_info;
1034 - ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
1035 - &fname->crypto_buf);
1038 - ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
1041 - fname->disk_name.name = fname->crypto_buf.name;
1042 - fname->disk_name.len = fname->crypto_buf.len;
1048 - /* We don't have the key and we are doing a lookup; decode the
1049 - * user-supplied name
1051 - if (iname->name[0] == '_')
1053 - if ((bigname && (iname->len != 33)) ||
1054 - (!bigname && (iname->len > 43)))
1057 - fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
1058 - if (fname->crypto_buf.name == NULL)
1060 - ret = digest_decode(iname->name + bigname, iname->len - bigname,
1061 - fname->crypto_buf.name);
1066 - fname->crypto_buf.len = ret;
1068 - memcpy(&fname->hinfo.hash, fname->crypto_buf.name, 4);
1069 - memcpy(&fname->hinfo.minor_hash, fname->crypto_buf.name + 4, 4);
1071 - fname->disk_name.name = fname->crypto_buf.name;
1072 - fname->disk_name.len = fname->crypto_buf.len;
1076 - kfree(fname->crypto_buf.name);
1077 - fname->crypto_buf.name = NULL;
1081 -void ext4_fname_free_filename(struct ext4_filename *fname)
1083 - kfree(fname->crypto_buf.name);
1084 - fname->crypto_buf.name = NULL;
1085 - fname->usr_fname = NULL;
1086 - fname->disk_name.name = NULL;
1088 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
1089 deleted file mode 100644
1090 index 0129d68..0000000
1091 --- a/fs/ext4/crypto_key.c
1095 - * linux/fs/ext4/crypto_key.c
1097 - * Copyright (C) 2015, Google, Inc.
1099 - * This contains encryption key functions for ext4
1101 - * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
1104 -#include <crypto/skcipher.h>
1105 -#include <keys/encrypted-type.h>
1106 -#include <keys/user-type.h>
1107 -#include <linux/random.h>
1108 -#include <linux/scatterlist.h>
1109 -#include <uapi/linux/keyctl.h>
1114 -static void derive_crypt_complete(struct crypto_async_request *req, int rc)
1116 - struct ext4_completion_result *ecr = req->data;
1118 - if (rc == -EINPROGRESS)
1122 - complete(&ecr->completion);
1126 - * ext4_derive_key_aes() - Derive a key using AES-128-ECB
1127 - * @deriving_key: Encryption key used for derivation.
1128 - * @source_key: Source key to which to apply derivation.
1129 - * @derived_key: Derived key.
1131 - * Return: Zero on success; non-zero otherwise.
1133 -static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
1134 - char source_key[EXT4_AES_256_XTS_KEY_SIZE],
1135 - char derived_key[EXT4_AES_256_XTS_KEY_SIZE])
1138 - struct skcipher_request *req = NULL;
1139 - DECLARE_EXT4_COMPLETION_RESULT(ecr);
1140 - struct scatterlist src_sg, dst_sg;
1141 - struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
1143 - if (IS_ERR(tfm)) {
1144 - res = PTR_ERR(tfm);
1148 - crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
1149 - req = skcipher_request_alloc(tfm, GFP_NOFS);
1154 - skcipher_request_set_callback(req,
1155 - CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1156 - derive_crypt_complete, &ecr);
1157 - res = crypto_skcipher_setkey(tfm, deriving_key,
1158 - EXT4_AES_128_ECB_KEY_SIZE);
1161 - sg_init_one(&src_sg, source_key, EXT4_AES_256_XTS_KEY_SIZE);
1162 - sg_init_one(&dst_sg, derived_key, EXT4_AES_256_XTS_KEY_SIZE);
1163 - skcipher_request_set_crypt(req, &src_sg, &dst_sg,
1164 - EXT4_AES_256_XTS_KEY_SIZE, NULL);
1165 - res = crypto_skcipher_encrypt(req);
1166 - if (res == -EINPROGRESS || res == -EBUSY) {
1167 - wait_for_completion(&ecr.completion);
1172 - skcipher_request_free(req);
1173 - crypto_free_skcipher(tfm);
1177 -void ext4_free_crypt_info(struct ext4_crypt_info *ci)
1182 - if (ci->ci_keyring_key)
1183 - key_put(ci->ci_keyring_key);
1184 - crypto_free_skcipher(ci->ci_ctfm);
1185 - kmem_cache_free(ext4_crypt_info_cachep, ci);
1188 -void ext4_free_encryption_info(struct inode *inode,
1189 - struct ext4_crypt_info *ci)
1191 - struct ext4_inode_info *ei = EXT4_I(inode);
1192 - struct ext4_crypt_info *prev;
1195 - ci = ACCESS_ONCE(ei->i_crypt_info);
1198 - prev = cmpxchg(&ei->i_crypt_info, ci, NULL);
1202 - ext4_free_crypt_info(ci);
1205 -int _ext4_get_encryption_info(struct inode *inode)
1207 - struct ext4_inode_info *ei = EXT4_I(inode);
1208 - struct ext4_crypt_info *crypt_info;
1209 - char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
1210 - (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
1211 - struct key *keyring_key = NULL;
1212 - struct ext4_encryption_key *master_key;
1213 - struct ext4_encryption_context ctx;
1214 - const struct user_key_payload *ukp;
1215 - struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1216 - struct crypto_skcipher *ctfm;
1217 - const char *cipher_str;
1218 - char raw_key[EXT4_MAX_KEY_SIZE];
1222 - if (!ext4_read_workqueue) {
1223 - res = ext4_init_crypto();
1229 - crypt_info = ACCESS_ONCE(ei->i_crypt_info);
1231 - if (!crypt_info->ci_keyring_key ||
1232 - key_validate(crypt_info->ci_keyring_key) == 0)
1234 - ext4_free_encryption_info(inode, crypt_info);
1238 - res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1239 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1240 - &ctx, sizeof(ctx));
1242 - if (!DUMMY_ENCRYPTION_ENABLED(sbi))
1244 - ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
1245 - ctx.filenames_encryption_mode =
1246 - EXT4_ENCRYPTION_MODE_AES_256_CTS;
1248 - } else if (res != sizeof(ctx))
1252 - crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
1256 - crypt_info->ci_flags = ctx.flags;
1257 - crypt_info->ci_data_mode = ctx.contents_encryption_mode;
1258 - crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
1259 - crypt_info->ci_ctfm = NULL;
1260 - crypt_info->ci_keyring_key = NULL;
1261 - memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
1262 - sizeof(crypt_info->ci_master_key));
1263 - if (S_ISREG(inode->i_mode))
1264 - mode = crypt_info->ci_data_mode;
1265 - else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1266 - mode = crypt_info->ci_filename_mode;
1270 - case EXT4_ENCRYPTION_MODE_AES_256_XTS:
1271 - cipher_str = "xts(aes)";
1273 - case EXT4_ENCRYPTION_MODE_AES_256_CTS:
1274 - cipher_str = "cts(cbc(aes))";
1277 - printk_once(KERN_WARNING
1278 - "ext4: unsupported key mode %d (ino %u)\n",
1279 - mode, (unsigned) inode->i_ino);
1283 - if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
1284 - memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
1287 - memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
1288 - EXT4_KEY_DESC_PREFIX_SIZE);
1289 - sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
1290 - "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
1291 - ctx.master_key_descriptor);
1292 - full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
1293 - (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
1294 - keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
1295 - if (IS_ERR(keyring_key)) {
1296 - res = PTR_ERR(keyring_key);
1297 - keyring_key = NULL;
1300 - crypt_info->ci_keyring_key = keyring_key;
1301 - if (keyring_key->type != &key_type_logon) {
1302 - printk_once(KERN_WARNING
1303 - "ext4: key type must be logon\n");
1307 - down_read(&keyring_key->sem);
1308 - ukp = user_key_payload(keyring_key);
1309 - if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
1311 - up_read(&keyring_key->sem);
1314 - master_key = (struct ext4_encryption_key *)ukp->data;
1315 - BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
1316 - EXT4_KEY_DERIVATION_NONCE_SIZE);
1317 - if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) {
1318 - printk_once(KERN_WARNING
1319 - "ext4: key size incorrect: %d\n",
1320 - master_key->size);
1322 - up_read(&keyring_key->sem);
1325 - res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
1327 - up_read(&keyring_key->sem);
1331 - ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
1332 - if (!ctfm || IS_ERR(ctfm)) {
1333 - res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
1335 - "%s: error %d (inode %u) allocating crypto tfm\n",
1336 - __func__, res, (unsigned) inode->i_ino);
1339 - crypt_info->ci_ctfm = ctfm;
1340 - crypto_skcipher_clear_flags(ctfm, ~0);
1341 - crypto_tfm_set_flags(crypto_skcipher_tfm(ctfm),
1342 - CRYPTO_TFM_REQ_WEAK_KEY);
1343 - res = crypto_skcipher_setkey(ctfm, raw_key,
1344 - ext4_encryption_key_size(mode));
1347 - memzero_explicit(raw_key, sizeof(raw_key));
1348 - if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) {
1349 - ext4_free_crypt_info(crypt_info);
1355 - if (res == -ENOKEY)
1357 - ext4_free_crypt_info(crypt_info);
1358 - memzero_explicit(raw_key, sizeof(raw_key));
1362 -int ext4_has_encryption_key(struct inode *inode)
1364 - struct ext4_inode_info *ei = EXT4_I(inode);
1366 - return (ei->i_crypt_info != NULL);
1368 diff --git a/fs/ext4/crypto_policy.c b/fs/ext4/crypto_policy.c
1369 deleted file mode 100644
1370 index ad05069..0000000
1371 --- a/fs/ext4/crypto_policy.c
1375 - * linux/fs/ext4/crypto_policy.c
1377 - * Copyright (C) 2015, Google, Inc.
1379 - * This contains encryption policy functions for ext4
1381 - * Written by Michael Halcrow, 2015.
1384 -#include <linux/random.h>
1385 -#include <linux/string.h>
1386 -#include <linux/types.h>
1388 -#include "ext4_jbd2.h"
1392 -static int ext4_inode_has_encryption_context(struct inode *inode)
1394 - int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1395 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, NULL, 0);
1400 - * check whether the policy is consistent with the encryption context
1403 -static int ext4_is_encryption_context_consistent_with_policy(
1404 - struct inode *inode, const struct ext4_encryption_policy *policy)
1406 - struct ext4_encryption_context ctx;
1407 - int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1408 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1410 - if (res != sizeof(ctx))
1412 - return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
1413 - EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
1416 - (ctx.contents_encryption_mode ==
1417 - policy->contents_encryption_mode) &&
1418 - (ctx.filenames_encryption_mode ==
1419 - policy->filenames_encryption_mode));
1422 -static int ext4_create_encryption_context_from_policy(
1423 - struct inode *inode, const struct ext4_encryption_policy *policy)
1425 - struct ext4_encryption_context ctx;
1429 - res = ext4_convert_inline_data(inode);
1433 - ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
1434 - memcpy(ctx.master_key_descriptor, policy->master_key_descriptor,
1435 - EXT4_KEY_DESCRIPTOR_SIZE);
1436 - if (!ext4_valid_contents_enc_mode(policy->contents_encryption_mode)) {
1437 - printk(KERN_WARNING
1438 - "%s: Invalid contents encryption mode %d\n", __func__,
1439 - policy->contents_encryption_mode);
1442 - if (!ext4_valid_filenames_enc_mode(policy->filenames_encryption_mode)) {
1443 - printk(KERN_WARNING
1444 - "%s: Invalid filenames encryption mode %d\n", __func__,
1445 - policy->filenames_encryption_mode);
1448 - if (policy->flags & ~EXT4_POLICY_FLAGS_VALID)
1450 - ctx.contents_encryption_mode = policy->contents_encryption_mode;
1451 - ctx.filenames_encryption_mode = policy->filenames_encryption_mode;
1452 - ctx.flags = policy->flags;
1453 - BUILD_BUG_ON(sizeof(ctx.nonce) != EXT4_KEY_DERIVATION_NONCE_SIZE);
1454 - get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
1456 - handle = ext4_journal_start(inode, EXT4_HT_MISC,
1457 - ext4_jbd2_credits_xattr(inode));
1458 - if (IS_ERR(handle))
1459 - return PTR_ERR(handle);
1460 - res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1461 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1464 - ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1465 - res = ext4_mark_inode_dirty(handle, inode);
1467 - EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1469 - res2 = ext4_journal_stop(handle);
1475 -int ext4_process_policy(const struct ext4_encryption_policy *policy,
1476 - struct inode *inode)
1478 - if (policy->version != 0)
1481 - if (!ext4_inode_has_encryption_context(inode)) {
1482 - if (!S_ISDIR(inode->i_mode))
1484 - if (!ext4_empty_dir(inode))
1485 - return -ENOTEMPTY;
1486 - return ext4_create_encryption_context_from_policy(inode,
1490 - if (ext4_is_encryption_context_consistent_with_policy(inode, policy))
1493 - printk(KERN_WARNING "%s: Policy inconsistent with encryption context\n",
1498 -int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
1500 - struct ext4_encryption_context ctx;
1502 - int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1503 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1504 - &ctx, sizeof(ctx));
1505 - if (res != sizeof(ctx))
1507 - if (ctx.format != EXT4_ENCRYPTION_CONTEXT_FORMAT_V1)
1509 - policy->version = 0;
1510 - policy->contents_encryption_mode = ctx.contents_encryption_mode;
1511 - policy->filenames_encryption_mode = ctx.filenames_encryption_mode;
1512 - policy->flags = ctx.flags;
1513 - memcpy(&policy->master_key_descriptor, ctx.master_key_descriptor,
1514 - EXT4_KEY_DESCRIPTOR_SIZE);
1518 -int ext4_is_child_context_consistent_with_parent(struct inode *parent,
1519 - struct inode *child)
1521 - struct ext4_crypt_info *parent_ci, *child_ci;
1524 - if ((parent == NULL) || (child == NULL)) {
1525 - pr_err("parent %p child %p\n", parent, child);
1526 - WARN_ON(1); /* Should never happen */
1529 - /* no restrictions if the parent directory is not encrypted */
1530 - if (!ext4_encrypted_inode(parent))
1532 - /* if the child directory is not encrypted, this is always a problem */
1533 - if (!ext4_encrypted_inode(child))
1535 - res = ext4_get_encryption_info(parent);
1538 - res = ext4_get_encryption_info(child);
1541 - parent_ci = EXT4_I(parent)->i_crypt_info;
1542 - child_ci = EXT4_I(child)->i_crypt_info;
1543 - if (!parent_ci && !child_ci)
1545 - if (!parent_ci || !child_ci)
1548 - return (memcmp(parent_ci->ci_master_key,
1549 - child_ci->ci_master_key,
1550 - EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
1551 - (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
1552 - (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
1553 - (parent_ci->ci_flags == child_ci->ci_flags));
1557 - * ext4_inherit_context() - Sets a child context from its parent
1558 - * @parent: Parent inode from which the context is inherited.
1559 - * @child: Child inode that inherits the context from @parent.
1561 - * Return: Zero on success, non-zero otherwise
1563 -int ext4_inherit_context(struct inode *parent, struct inode *child)
1565 - struct ext4_encryption_context ctx;
1566 - struct ext4_crypt_info *ci;
1569 - res = ext4_get_encryption_info(parent);
1572 - ci = EXT4_I(parent)->i_crypt_info;
1576 - ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
1577 - if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
1578 - ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
1579 - ctx.filenames_encryption_mode =
1580 - EXT4_ENCRYPTION_MODE_AES_256_CTS;
1582 - memset(ctx.master_key_descriptor, 0x42,
1583 - EXT4_KEY_DESCRIPTOR_SIZE);
1586 - ctx.contents_encryption_mode = ci->ci_data_mode;
1587 - ctx.filenames_encryption_mode = ci->ci_filename_mode;
1588 - ctx.flags = ci->ci_flags;
1589 - memcpy(ctx.master_key_descriptor, ci->ci_master_key,
1590 - EXT4_KEY_DESCRIPTOR_SIZE);
1592 - get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
1593 - res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
1594 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
1597 - ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
1598 - ext4_clear_inode_state(child, EXT4_STATE_MAY_INLINE_DATA);
1599 - res = ext4_get_encryption_info(child);
1603 diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
1604 index 561d730..301f189 100644
1607 @@ -109,10 +109,10 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1608 struct super_block *sb = inode->i_sb;
1609 struct buffer_head *bh = NULL;
1610 int dir_has_error = 0;
1611 - struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
1612 + struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1614 if (ext4_encrypted_inode(inode)) {
1615 - err = ext4_get_encryption_info(inode);
1616 + err = fscrypt_get_encryption_info(inode);
1617 if (err && err != -ENOKEY)
1620 @@ -139,8 +139,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1623 if (ext4_encrypted_inode(inode)) {
1624 - err = ext4_fname_crypto_alloc_buffer(inode, EXT4_NAME_LEN,
1625 - &fname_crypto_str);
1626 + err = fscrypt_fname_alloc_buffer(inode, EXT4_NAME_LEN, &fstr);
1630 @@ -248,16 +247,19 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
1631 get_dtype(sb, de->file_type)))
1634 - int save_len = fname_crypto_str.len;
1635 + int save_len = fstr.len;
1636 + struct fscrypt_str de_name =
1637 + FSTR_INIT(de->name,
1640 /* Directory is encrypted */
1641 - err = ext4_fname_disk_to_usr(inode,
1642 - NULL, de, &fname_crypto_str);
1643 - fname_crypto_str.len = save_len;
1644 + err = fscrypt_fname_disk_to_usr(inode,
1645 + 0, 0, &de_name, &fstr);
1646 + fstr.len = save_len;
1650 - fname_crypto_str.name, err,
1652 le32_to_cpu(de->inode),
1653 get_dtype(sb, de->file_type)))
1655 @@ -276,7 +278,7 @@ done:
1658 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1659 - ext4_fname_crypto_free_buffer(&fname_crypto_str);
1660 + fscrypt_fname_free_buffer(&fstr);
1664 @@ -427,7 +429,7 @@ void ext4_htree_free_dir_info(struct dir_private_info *p)
1665 int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
1667 struct ext4_dir_entry_2 *dirent,
1668 - struct ext4_str *ent_name)
1669 + struct fscrypt_str *ent_name)
1671 struct rb_node **p, *parent = NULL;
1672 struct fname *fname, *new_fn;
1673 @@ -604,7 +606,7 @@ finished:
1674 static int ext4_dir_open(struct inode * inode, struct file * filp)
1676 if (ext4_encrypted_inode(inode))
1677 - return ext4_get_encryption_info(inode) ? -EACCES : 0;
1678 + return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
1682 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
1683 index 349afeb..3b92225 100644
1684 --- a/fs/ext4/ext4.h
1685 +++ b/fs/ext4/ext4.h
1687 #include <linux/percpu_counter.h>
1688 #include <linux/ratelimit.h>
1689 #include <crypto/hash.h>
1690 +#include <linux/fscrypto.h>
1691 #include <linux/falloc.h>
1693 #include <linux/compat.h>
1694 @@ -604,15 +605,6 @@ enum {
1695 #define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER 0x0010
1696 #define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER 0x0020
1698 -/* Encryption algorithms */
1699 -#define EXT4_ENCRYPTION_MODE_INVALID 0
1700 -#define EXT4_ENCRYPTION_MODE_AES_256_XTS 1
1701 -#define EXT4_ENCRYPTION_MODE_AES_256_GCM 2
1702 -#define EXT4_ENCRYPTION_MODE_AES_256_CBC 3
1703 -#define EXT4_ENCRYPTION_MODE_AES_256_CTS 4
1705 -#include "ext4_crypto.h"
1710 @@ -634,9 +626,9 @@ enum {
1711 #define EXT4_IOC_RESIZE_FS _IOW('f', 16, __u64)
1712 #define EXT4_IOC_SWAP_BOOT _IO('f', 17)
1713 #define EXT4_IOC_PRECACHE_EXTENTS _IO('f', 18)
1714 -#define EXT4_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct ext4_encryption_policy)
1715 -#define EXT4_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
1716 -#define EXT4_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct ext4_encryption_policy)
1717 +#define EXT4_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
1718 +#define EXT4_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
1719 +#define EXT4_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
1721 #ifndef FS_IOC_FSGETXATTR
1722 /* Until the uapi changes get merged for project quota... */
1723 @@ -1078,10 +1070,6 @@ struct ext4_inode_info {
1724 /* Precomputed uuid+inum+igen checksum for seeding inode checksums */
1727 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1728 - /* Encryption params */
1729 - struct ext4_crypt_info *i_crypt_info;
1734 @@ -1604,15 +1592,6 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1736 * Returns true if the inode is inode is encrypted
1738 -static inline int ext4_encrypted_inode(struct inode *inode)
1740 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1741 - return ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT);
1747 #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
1750 @@ -2076,10 +2055,10 @@ struct dx_hash_info
1752 struct ext4_filename {
1753 const struct qstr *usr_fname;
1754 - struct ext4_str disk_name;
1755 + struct fscrypt_str disk_name;
1756 struct dx_hash_info hinfo;
1757 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1758 - struct ext4_str crypto_buf;
1759 + struct fscrypt_str crypto_buf;
1763 @@ -2290,132 +2269,82 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
1764 struct ext4_group_desc *gdp);
1765 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
1767 -/* crypto_policy.c */
1768 -int ext4_is_child_context_consistent_with_parent(struct inode *parent,
1769 - struct inode *child);
1770 -int ext4_inherit_context(struct inode *parent, struct inode *child);
1771 -void ext4_to_hex(char *dst, char *src, size_t src_size);
1772 -int ext4_process_policy(const struct ext4_encryption_policy *policy,
1773 - struct inode *inode);
1774 -int ext4_get_policy(struct inode *inode,
1775 - struct ext4_encryption_policy *policy);
1778 -extern struct kmem_cache *ext4_crypt_info_cachep;
1779 -bool ext4_valid_contents_enc_mode(uint32_t mode);
1780 -uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size);
1781 -extern struct workqueue_struct *ext4_read_workqueue;
1782 -struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode,
1784 -void ext4_release_crypto_ctx(struct ext4_crypto_ctx *ctx);
1785 -void ext4_restore_control_page(struct page *data_page);
1786 -struct page *ext4_encrypt(struct inode *inode,
1787 - struct page *plaintext_page,
1789 -int ext4_decrypt(struct page *page);
1790 -int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
1791 - ext4_fsblk_t pblk, ext4_lblk_t len);
1792 -extern const struct dentry_operations ext4_encrypted_d_ops;
1794 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1795 -int ext4_init_crypto(void);
1796 -void ext4_exit_crypto(void);
1797 static inline int ext4_sb_has_crypto(struct super_block *sb)
1799 return ext4_has_feature_encrypt(sb);
1802 -static inline int ext4_init_crypto(void) { return 0; }
1803 -static inline void ext4_exit_crypto(void) { }
1804 -static inline int ext4_sb_has_crypto(struct super_block *sb)
1806 +static inline bool ext4_encrypted_inode(struct inode *inode)
1809 + return ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT);
1813 -/* crypto_fname.c */
1814 -bool ext4_valid_filenames_enc_mode(uint32_t mode);
1815 -u32 ext4_fname_crypto_round_up(u32 size, u32 blksize);
1816 -unsigned ext4_fname_encrypted_size(struct inode *inode, u32 ilen);
1817 -int ext4_fname_crypto_alloc_buffer(struct inode *inode,
1818 - u32 ilen, struct ext4_str *crypto_str);
1819 -int _ext4_fname_disk_to_usr(struct inode *inode,
1820 - struct dx_hash_info *hinfo,
1821 - const struct ext4_str *iname,
1822 - struct ext4_str *oname);
1823 -int ext4_fname_disk_to_usr(struct inode *inode,
1824 - struct dx_hash_info *hinfo,
1825 - const struct ext4_dir_entry_2 *de,
1826 - struct ext4_str *oname);
1827 -int ext4_fname_usr_to_disk(struct inode *inode,
1828 - const struct qstr *iname,
1829 - struct ext4_str *oname);
1830 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1831 -void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str);
1832 -int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
1833 - int lookup, struct ext4_filename *fname);
1834 -void ext4_fname_free_filename(struct ext4_filename *fname);
1837 -int ext4_setup_fname_crypto(struct inode *inode)
1841 -static inline void ext4_fname_crypto_free_buffer(struct ext4_str *p) { }
1842 static inline int ext4_fname_setup_filename(struct inode *dir,
1843 - const struct qstr *iname,
1844 - int lookup, struct ext4_filename *fname)
1845 + const struct qstr *iname,
1846 + int lookup, struct ext4_filename *fname)
1848 - fname->usr_fname = iname;
1849 - fname->disk_name.name = (unsigned char *) iname->name;
1850 - fname->disk_name.len = iname->len;
1853 -static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
1856 + struct fscrypt_name name;
1860 -void ext4_free_crypt_info(struct ext4_crypt_info *ci);
1861 -void ext4_free_encryption_info(struct inode *inode, struct ext4_crypt_info *ci);
1862 -int _ext4_get_encryption_info(struct inode *inode);
1863 + memset(fname, 0, sizeof(struct ext4_filename));
1865 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
1866 -int ext4_has_encryption_key(struct inode *inode);
1867 + err = fscrypt_setup_filename(dir, iname, lookup, &name);
1869 -static inline int ext4_get_encryption_info(struct inode *inode)
1871 - struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
1874 - (ci->ci_keyring_key &&
1875 - (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
1876 - (1 << KEY_FLAG_REVOKED) |
1877 - (1 << KEY_FLAG_DEAD)))))
1878 - return _ext4_get_encryption_info(inode);
1880 + fname->usr_fname = name.usr_fname;
1881 + fname->disk_name = name.disk_name;
1882 + fname->hinfo.hash = name.hash;
1883 + fname->hinfo.minor_hash = name.minor_hash;
1884 + fname->crypto_buf = name.crypto_buf;
1888 -static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
1889 +static inline void ext4_fname_free_filename(struct ext4_filename *fname)
1891 - return EXT4_I(inode)->i_crypt_info;
1893 + struct fscrypt_name name;
1896 -static inline int ext4_has_encryption_key(struct inode *inode)
1899 + name.crypto_buf = fname->crypto_buf;
1900 + fscrypt_free_filename(&name);
1902 + fname->crypto_buf.name = NULL;
1903 + fname->usr_fname = NULL;
1904 + fname->disk_name.name = NULL;
1906 -static inline int ext4_get_encryption_info(struct inode *inode)
1908 +static inline int ext4_fname_setup_filename(struct inode *dir,
1909 + const struct qstr *iname,
1910 + int lookup, struct ext4_filename *fname)
1912 + fname->usr_fname = iname;
1913 + fname->disk_name.name = (unsigned char *) iname->name;
1914 + fname->disk_name.len = iname->len;
1917 -static inline struct ext4_crypt_info *ext4_encryption_info(struct inode *inode)
1922 +static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
1924 +#define fscrypt_set_d_op(i)
1925 +#define fscrypt_get_ctx fscrypt_notsupp_get_ctx
1926 +#define fscrypt_release_ctx fscrypt_notsupp_release_ctx
1927 +#define fscrypt_encrypt_page fscrypt_notsupp_encrypt_page
1928 +#define fscrypt_decrypt_page fscrypt_notsupp_decrypt_page
1929 +#define fscrypt_decrypt_bio_pages fscrypt_notsupp_decrypt_bio_pages
1930 +#define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page
1931 +#define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page
1932 +#define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range
1933 +#define fscrypt_process_policy fscrypt_notsupp_process_policy
1934 +#define fscrypt_get_policy fscrypt_notsupp_get_policy
1935 +#define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context
1936 +#define fscrypt_inherit_context fscrypt_notsupp_inherit_context
1937 +#define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info
1938 +#define fscrypt_put_encryption_info fscrypt_notsupp_put_encryption_info
1939 +#define fscrypt_setup_filename fscrypt_notsupp_setup_filename
1940 +#define fscrypt_free_filename fscrypt_notsupp_free_filename
1941 +#define fscrypt_fname_encrypted_size fscrypt_notsupp_fname_encrypted_size
1942 +#define fscrypt_fname_alloc_buffer fscrypt_notsupp_fname_alloc_buffer
1943 +#define fscrypt_fname_free_buffer fscrypt_notsupp_fname_free_buffer
1944 +#define fscrypt_fname_disk_to_usr fscrypt_notsupp_fname_disk_to_usr
1945 +#define fscrypt_fname_usr_to_disk fscrypt_notsupp_fname_usr_to_disk
1949 extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
1950 @@ -2429,7 +2358,7 @@ extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
1951 extern int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
1953 struct ext4_dir_entry_2 *dirent,
1954 - struct ext4_str *ent_name);
1955 + struct fscrypt_str *ent_name);
1956 extern void ext4_htree_free_dir_info(struct dir_private_info *p);
1957 extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1958 struct buffer_head *bh,
1959 @@ -2619,7 +2548,7 @@ extern int ext4_generic_delete_entry(handle_t *handle,
1963 -extern int ext4_empty_dir(struct inode *inode);
1964 +extern bool ext4_empty_dir(struct inode *inode);
1967 extern int ext4_group_add(struct super_block *sb,
1968 @@ -3101,7 +3030,7 @@ extern int ext4_delete_inline_entry(handle_t *handle,
1969 struct ext4_dir_entry_2 *de_del,
1970 struct buffer_head *bh,
1971 int *has_inline_data);
1972 -extern int empty_inline_dir(struct inode *dir, int *has_inline_data);
1973 +extern bool empty_inline_dir(struct inode *dir, int *has_inline_data);
1974 extern struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
1975 struct ext4_dir_entry_2 **parent_de,
1977 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
1978 deleted file mode 100644
1979 index 1f73c29..0000000
1980 --- a/fs/ext4/ext4_crypto.h
1984 - * linux/fs/ext4/ext4_crypto.h
1986 - * Copyright (C) 2015, Google, Inc.
1988 - * This contains encryption header content for ext4
1990 - * Written by Michael Halcrow, 2015.
1993 -#ifndef _EXT4_CRYPTO_H
1994 -#define _EXT4_CRYPTO_H
1996 -#include <linux/fs.h>
1998 -#define EXT4_KEY_DESCRIPTOR_SIZE 8
2000 -/* Policy provided via an ioctl on the topmost directory */
2001 -struct ext4_encryption_policy {
2003 - char contents_encryption_mode;
2004 - char filenames_encryption_mode;
2006 - char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
2007 -} __attribute__((__packed__));
2009 -#define EXT4_ENCRYPTION_CONTEXT_FORMAT_V1 1
2010 -#define EXT4_KEY_DERIVATION_NONCE_SIZE 16
2012 -#define EXT4_POLICY_FLAGS_PAD_4 0x00
2013 -#define EXT4_POLICY_FLAGS_PAD_8 0x01
2014 -#define EXT4_POLICY_FLAGS_PAD_16 0x02
2015 -#define EXT4_POLICY_FLAGS_PAD_32 0x03
2016 -#define EXT4_POLICY_FLAGS_PAD_MASK 0x03
2017 -#define EXT4_POLICY_FLAGS_VALID 0x03
2020 - * Encryption context for inode
2022 - * Protector format:
2023 - * 1 byte: Protector format (1 = this version)
2024 - * 1 byte: File contents encryption mode
2025 - * 1 byte: File names encryption mode
2026 - * 1 byte: Reserved
2027 - * 8 bytes: Master Key descriptor
2028 - * 16 bytes: Encryption Key derivation nonce
2030 -struct ext4_encryption_context {
2032 - char contents_encryption_mode;
2033 - char filenames_encryption_mode;
2035 - char master_key_descriptor[EXT4_KEY_DESCRIPTOR_SIZE];
2036 - char nonce[EXT4_KEY_DERIVATION_NONCE_SIZE];
2037 -} __attribute__((__packed__));
2039 -/* Encryption parameters */
2040 -#define EXT4_XTS_TWEAK_SIZE 16
2041 -#define EXT4_AES_128_ECB_KEY_SIZE 16
2042 -#define EXT4_AES_256_GCM_KEY_SIZE 32
2043 -#define EXT4_AES_256_CBC_KEY_SIZE 32
2044 -#define EXT4_AES_256_CTS_KEY_SIZE 32
2045 -#define EXT4_AES_256_XTS_KEY_SIZE 64
2046 -#define EXT4_MAX_KEY_SIZE 64
2048 -#define EXT4_KEY_DESC_PREFIX "ext4:"
2049 -#define EXT4_KEY_DESC_PREFIX_SIZE 5
2051 -/* This is passed in from userspace into the kernel keyring */
2052 -struct ext4_encryption_key {
2054 - char raw[EXT4_MAX_KEY_SIZE];
2056 -} __attribute__((__packed__));
2058 -struct ext4_crypt_info {
2059 - char ci_data_mode;
2060 - char ci_filename_mode;
2062 - struct crypto_skcipher *ci_ctfm;
2063 - struct key *ci_keyring_key;
2064 - char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
2067 -#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
2068 -#define EXT4_WRITE_PATH_FL 0x00000002
2070 -struct ext4_crypto_ctx {
2073 - struct page *bounce_page; /* Ciphertext page */
2074 - struct page *control_page; /* Original page */
2078 - struct work_struct work;
2080 - struct list_head free_list; /* Free list */
2082 - char flags; /* Flags */
2083 - char mode; /* Encryption mode for tfm */
2086 -struct ext4_completion_result {
2087 - struct completion completion;
2091 -#define DECLARE_EXT4_COMPLETION_RESULT(ecr) \
2092 - struct ext4_completion_result ecr = { \
2093 - COMPLETION_INITIALIZER((ecr).completion), 0 }
2095 -static inline int ext4_encryption_key_size(int mode)
2098 - case EXT4_ENCRYPTION_MODE_AES_256_XTS:
2099 - return EXT4_AES_256_XTS_KEY_SIZE;
2100 - case EXT4_ENCRYPTION_MODE_AES_256_GCM:
2101 - return EXT4_AES_256_GCM_KEY_SIZE;
2102 - case EXT4_ENCRYPTION_MODE_AES_256_CBC:
2103 - return EXT4_AES_256_CBC_KEY_SIZE;
2104 - case EXT4_ENCRYPTION_MODE_AES_256_CTS:
2105 - return EXT4_AES_256_CTS_KEY_SIZE;
2112 -#define EXT4_FNAME_NUM_SCATTER_ENTRIES 4
2113 -#define EXT4_CRYPTO_BLOCK_SIZE 16
2114 -#define EXT4_FNAME_CRYPTO_DIGEST_SIZE 32
2117 - unsigned char *name;
2122 - * For encrypted symlinks, the ciphertext length is stored at the beginning
2123 - * of the string in little-endian format.
2125 -struct ext4_encrypted_symlink_data {
2127 - char encrypted_path[1];
2128 -} __attribute__((__packed__));
2131 - * This function is used to calculate the disk space required to
2132 - * store a filename of length l in encrypted symlink format.
2134 -static inline u32 encrypted_symlink_data_len(u32 l)
2136 - if (l < EXT4_CRYPTO_BLOCK_SIZE)
2137 - l = EXT4_CRYPTO_BLOCK_SIZE;
2138 - return (l + sizeof(struct ext4_encrypted_symlink_data) - 1);
2141 -#endif /* _EXT4_CRYPTO_H */
2142 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
2143 index fa2208b..9d262b4 100644
2144 --- a/fs/ext4/file.c
2145 +++ b/fs/ext4/file.c
2146 @@ -308,10 +308,10 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
2147 struct inode *inode = file->f_mapping->host;
2149 if (ext4_encrypted_inode(inode)) {
2150 - int err = ext4_get_encryption_info(inode);
2151 + int err = fscrypt_get_encryption_info(inode);
2154 - if (ext4_encryption_info(inode) == NULL)
2155 + if (!fscrypt_has_encryption_key(inode))
2158 file_accessed(file);
2159 @@ -367,16 +367,16 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
2162 if (ext4_encrypted_inode(inode)) {
2163 - ret = ext4_get_encryption_info(inode);
2164 + ret = fscrypt_get_encryption_info(inode);
2167 - if (ext4_encryption_info(inode) == NULL)
2168 + if (!fscrypt_has_encryption_key(inode))
2172 dir = dget_parent(file_dentry(filp));
2173 if (ext4_encrypted_inode(d_inode(dir)) &&
2174 - !ext4_is_child_context_consistent_with_parent(d_inode(dir), inode)) {
2175 + !fscrypt_has_permitted_context(d_inode(dir), inode)) {
2176 ext4_warning(inode->i_sb,
2177 "Inconsistent encryption contexts: %lu/%lu\n",
2178 (unsigned long) d_inode(dir)->i_ino,
2179 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
2180 index 237b877..69bb343 100644
2181 --- a/fs/ext4/ialloc.c
2182 +++ b/fs/ext4/ialloc.c
2183 @@ -767,10 +767,10 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
2184 if ((ext4_encrypted_inode(dir) ||
2185 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb))) &&
2186 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
2187 - err = ext4_get_encryption_info(dir);
2188 + err = fscrypt_get_encryption_info(dir);
2190 return ERR_PTR(err);
2191 - if (ext4_encryption_info(dir) == NULL)
2192 + if (!fscrypt_has_encryption_key(dir))
2193 return ERR_PTR(-EPERM);
2195 nblocks += EXT4_DATA_TRANS_BLOCKS(dir->i_sb);
2196 @@ -1115,7 +1115,8 @@ got:
2200 - err = ext4_inherit_context(dir, inode);
2201 + /* give pointer to avoid set_context with journal ops. */
2202 + err = fscrypt_inherit_context(dir, inode, &encrypt, true);
2204 goto fail_free_drop;
2206 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
2207 index 7bc6c85..5ea6337 100644
2208 --- a/fs/ext4/inline.c
2209 +++ b/fs/ext4/inline.c
2210 @@ -1326,7 +1326,7 @@ int htree_inlinedir_to_tree(struct file *dir_file,
2211 struct ext4_iloc iloc;
2212 void *dir_buf = NULL;
2213 struct ext4_dir_entry_2 fake;
2214 - struct ext4_str tmp_str;
2215 + struct fscrypt_str tmp_str;
2217 ret = ext4_get_inode_loc(inode, &iloc);
2219 @@ -1739,20 +1739,20 @@ ext4_get_inline_entry(struct inode *inode,
2220 return (struct ext4_dir_entry_2 *)(inline_pos + offset);
2223 -int empty_inline_dir(struct inode *dir, int *has_inline_data)
2224 +bool empty_inline_dir(struct inode *dir, int *has_inline_data)
2226 int err, inline_size;
2227 struct ext4_iloc iloc;
2229 unsigned int offset;
2230 struct ext4_dir_entry_2 *de;
2234 err = ext4_get_inode_loc(dir, &iloc);
2236 EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
2242 down_read(&EXT4_I(dir)->xattr_sem);
2243 @@ -1766,7 +1766,7 @@ int empty_inline_dir(struct inode *dir, int *has_inline_data)
2244 ext4_warning(dir->i_sb,
2245 "bad inline directory (dir #%lu) - no `..'",
2252 @@ -1784,11 +1784,11 @@ int empty_inline_dir(struct inode *dir, int *has_inline_data)
2253 dir->i_ino, le32_to_cpu(de->inode),
2254 le16_to_cpu(de->rec_len), de->name_len,
2260 if (le32_to_cpu(de->inode)) {
2265 offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
2266 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
2267 index 981a1fc..8f44605 100644
2268 --- a/fs/ext4/inode.c
2269 +++ b/fs/ext4/inode.c
2270 @@ -386,7 +386,7 @@ int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
2273 if (ext4_encrypted_inode(inode))
2274 - return ext4_encrypted_zeroout(inode, lblk, pblk, len);
2275 + return fscrypt_zeroout_range(inode, lblk, pblk, len);
2277 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
2279 @@ -1134,7 +1134,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
2281 page_zero_new_buffers(page, from, to);
2283 - err = ext4_decrypt(page);
2284 + err = fscrypt_decrypt_page(page);
2288 @@ -3611,9 +3611,9 @@ static int __ext4_block_zero_page_range(handle_t *handle,
2289 if (S_ISREG(inode->i_mode) &&
2290 ext4_encrypted_inode(inode)) {
2291 /* We expect the key to be set. */
2292 - BUG_ON(!ext4_has_encryption_key(inode));
2293 + BUG_ON(!fscrypt_has_encryption_key(inode));
2294 BUG_ON(blocksize != PAGE_SIZE);
2295 - WARN_ON_ONCE(ext4_decrypt(page));
2296 + WARN_ON_ONCE(fscrypt_decrypt_page(page));
2299 if (ext4_should_journal_data(inode)) {
2300 diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
2301 index eae5917..1c4f52f 100644
2302 --- a/fs/ext4/ioctl.c
2303 +++ b/fs/ext4/ioctl.c
2304 @@ -772,19 +772,13 @@ resizefs_out:
2305 return ext4_ext_precache(inode);
2306 case EXT4_IOC_SET_ENCRYPTION_POLICY: {
2307 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2308 - struct ext4_encryption_policy policy;
2310 + struct fscrypt_policy policy;
2312 if (copy_from_user(&policy,
2313 - (struct ext4_encryption_policy __user *)arg,
2314 - sizeof(policy))) {
2316 - goto encryption_policy_out;
2319 - err = ext4_process_policy(&policy, inode);
2320 -encryption_policy_out:
2322 + (struct fscrypt_policy __user *)arg,
2325 + return fscrypt_process_policy(inode, &policy);
2329 @@ -827,12 +821,12 @@ encryption_policy_out:
2331 case EXT4_IOC_GET_ENCRYPTION_POLICY: {
2332 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2333 - struct ext4_encryption_policy policy;
2334 + struct fscrypt_policy policy;
2337 if (!ext4_encrypted_inode(inode))
2339 - err = ext4_get_policy(inode, &policy);
2340 + err = fscrypt_get_policy(inode, &policy);
2343 if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
2344 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
2345 index 48e4b89..9cab638 100644
2346 --- a/fs/ext4/namei.c
2347 +++ b/fs/ext4/namei.c
2348 @@ -612,19 +612,21 @@ static struct stats dx_show_leaf(struct inode *dir,
2349 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2352 - struct ext4_str fname_crypto_str
2353 - = {.name = NULL, .len = 0};
2354 + struct fscrypt_str fname_crypto_str =
2355 + FSTR_INIT(NULL, 0);
2356 + struct qstr istr =
2357 + QSTR_INIT(de->name, de->name_len);
2362 if (ext4_encrypted_inode(inode))
2363 - res = ext4_get_encryption_info(dir);
2364 + res = fscrypt_get_encryption_info(dir);
2366 printk(KERN_WARNING "Error setting up"
2367 " fname crypto: %d\n", res);
2369 - if (ctx == NULL) {
2370 + if (!fscrypt_has_encryption_key(inode)) {
2371 /* Directory is not encrypted */
2372 ext4fs_dirhash(de->name,
2374 @@ -633,9 +635,12 @@ static struct stats dx_show_leaf(struct inode *dir,
2375 (unsigned) ((char *) de
2378 + struct fscrypt_str de_name =
2379 + FSTR_INIT(name, len);
2381 /* Directory is encrypted */
2382 - res = ext4_fname_crypto_alloc_buffer(
2383 - ctx, de->name_len,
2384 + res = fscrypt_fname_alloc_buffer(
2388 printk(KERN_WARNING "Error "
2389 @@ -644,8 +649,9 @@ static struct stats dx_show_leaf(struct inode *dir,
2393 - res = ext4_fname_disk_to_usr(ctx, NULL, de,
2394 - &fname_crypto_str);
2395 + res = fscrypt_fname_disk_to_usr(inode,
2397 + &fname_crypto_str);
2399 printk(KERN_WARNING "Error "
2400 "converting filename "
2401 @@ -662,8 +668,8 @@ static struct stats dx_show_leaf(struct inode *dir,
2402 printk("%*.s:(E)%x.%u ", len, name,
2403 h.hash, (unsigned) ((char *) de
2405 - ext4_fname_crypto_free_buffer(
2406 - &fname_crypto_str);
2407 + fscrypt_fname_free_buffer(
2408 + &fname_crypto_str);
2411 int len = de->name_len;
2412 @@ -952,7 +958,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2413 struct buffer_head *bh;
2414 struct ext4_dir_entry_2 *de, *top;
2415 int err = 0, count = 0;
2416 - struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
2417 + struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str;
2419 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
2420 (unsigned long)block));
2421 @@ -967,12 +973,12 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2422 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2423 /* Check if the directory is encrypted */
2424 if (ext4_encrypted_inode(dir)) {
2425 - err = ext4_get_encryption_info(dir);
2426 + err = fscrypt_get_encryption_info(dir);
2431 - err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
2432 + err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN,
2436 @@ -1003,10 +1009,13 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2439 int save_len = fname_crypto_str.len;
2440 + struct fscrypt_str de_name = FSTR_INIT(de->name,
2443 /* Directory is encrypted */
2444 - err = ext4_fname_disk_to_usr(dir, hinfo, de,
2445 - &fname_crypto_str);
2446 + err = fscrypt_fname_disk_to_usr(dir, hinfo->hash,
2447 + hinfo->minor_hash, &de_name,
2448 + &fname_crypto_str);
2452 @@ -1025,7 +1034,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
2455 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2456 - ext4_fname_crypto_free_buffer(&fname_crypto_str);
2457 + fscrypt_fname_free_buffer(&fname_crypto_str);
2461 @@ -1050,7 +1059,7 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
2465 - struct ext4_str tmp_str;
2466 + struct fscrypt_str tmp_str;
2468 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
2469 start_hash, start_minor_hash));
2470 @@ -1558,26 +1567,23 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
2471 struct ext4_dir_entry_2 *de;
2472 struct buffer_head *bh;
2474 - if (ext4_encrypted_inode(dir)) {
2475 - int res = ext4_get_encryption_info(dir);
2476 + if (ext4_encrypted_inode(dir)) {
2477 + int res = fscrypt_get_encryption_info(dir);
2480 - * This should be a properly defined flag for
2481 - * dentry->d_flags when we uplift this to the VFS.
2482 - * d_fsdata is set to (void *) 1 if if the dentry is
2483 + * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is
2484 * created while the directory was encrypted and we
2485 * don't have access to the key.
2487 - dentry->d_fsdata = NULL;
2488 - if (ext4_encryption_info(dir))
2489 - dentry->d_fsdata = (void *) 1;
2490 - d_set_d_op(dentry, &ext4_encrypted_d_ops);
2491 - if (res && res != -ENOKEY)
2492 - return ERR_PTR(res);
2494 + if (fscrypt_has_encryption_key(dir))
2495 + fscrypt_set_encrypted_dentry(dentry);
2496 + fscrypt_set_d_op(dentry);
2497 + if (res && res != -ENOKEY)
2498 + return ERR_PTR(res);
2501 - if (dentry->d_name.len > EXT4_NAME_LEN)
2502 - return ERR_PTR(-ENAMETOOLONG);
2503 + if (dentry->d_name.len > EXT4_NAME_LEN)
2504 + return ERR_PTR(-ENAMETOOLONG);
2506 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2508 @@ -1604,11 +1610,9 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi
2510 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
2511 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
2512 - !ext4_is_child_context_consistent_with_parent(dir,
2514 + !fscrypt_has_permitted_context(dir, inode)) {
2515 int nokey = ext4_encrypted_inode(inode) &&
2516 - !ext4_encryption_info(inode);
2518 + !fscrypt_has_encryption_key(inode);
2521 return ERR_PTR(-ENOKEY);
2522 @@ -2685,13 +2689,13 @@ out_stop:
2524 * routine to check that the specified directory is empty (for rmdir)
2526 -int ext4_empty_dir(struct inode *inode)
2527 +bool ext4_empty_dir(struct inode *inode)
2529 unsigned int offset;
2530 struct buffer_head *bh;
2531 struct ext4_dir_entry_2 *de, *de1;
2532 struct super_block *sb;
2536 if (ext4_has_inline_data(inode)) {
2537 int has_inline_data = 1;
2538 @@ -2704,11 +2708,11 @@ int ext4_empty_dir(struct inode *inode)
2540 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) {
2541 EXT4_ERROR_INODE(inode, "invalid size");
2545 bh = ext4_read_dirblock(inode, 0, EITHER);
2550 de = (struct ext4_dir_entry_2 *) bh->b_data;
2551 de1 = ext4_next_entry(de, sb->s_blocksize);
2552 @@ -2717,7 +2721,7 @@ int ext4_empty_dir(struct inode *inode)
2553 strcmp(".", de->name) || strcmp("..", de1->name)) {
2554 ext4_warning_inode(inode, "directory missing '.' and/or '..'");
2559 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2560 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2561 @@ -2730,7 +2734,7 @@ int ext4_empty_dir(struct inode *inode)
2562 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2563 bh = ext4_read_dirblock(inode, lblock, EITHER);
2567 de = (struct ext4_dir_entry_2 *) bh->b_data;
2569 if (ext4_check_dir_entry(inode, NULL, de, bh,
2570 @@ -2742,13 +2746,13 @@ int ext4_empty_dir(struct inode *inode)
2572 if (le32_to_cpu(de->inode)) {
2577 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2578 de = ext4_next_entry(de, sb->s_blocksize);
2586 @@ -3071,8 +3075,8 @@ static int ext4_symlink(struct inode *dir,
2587 int err, len = strlen(symname);
2589 bool encryption_required;
2590 - struct ext4_str disk_link;
2591 - struct ext4_encrypted_symlink_data *sd = NULL;
2592 + struct fscrypt_str disk_link;
2593 + struct fscrypt_symlink_data *sd = NULL;
2595 disk_link.len = len + 1;
2596 disk_link.name = (char *) symname;
2597 @@ -3080,13 +3084,13 @@ static int ext4_symlink(struct inode *dir,
2598 encryption_required = (ext4_encrypted_inode(dir) ||
2599 DUMMY_ENCRYPTION_ENABLED(EXT4_SB(dir->i_sb)));
2600 if (encryption_required) {
2601 - err = ext4_get_encryption_info(dir);
2602 + err = fscrypt_get_encryption_info(dir);
2605 - if (ext4_encryption_info(dir) == NULL)
2606 + if (!fscrypt_has_encryption_key(dir))
2608 - disk_link.len = (ext4_fname_encrypted_size(dir, len) +
2609 - sizeof(struct ext4_encrypted_symlink_data));
2610 + disk_link.len = (fscrypt_fname_encrypted_size(dir, len) +
2611 + sizeof(struct fscrypt_symlink_data));
2612 sd = kzalloc(disk_link.len, GFP_KERNEL);
2615 @@ -3134,13 +3138,12 @@ static int ext4_symlink(struct inode *dir,
2617 if (encryption_required) {
2619 - struct ext4_str ostr;
2620 + struct fscrypt_str ostr =
2621 + FSTR_INIT(sd->encrypted_path, disk_link.len);
2623 istr.name = (const unsigned char *) symname;
2625 - ostr.name = sd->encrypted_path;
2626 - ostr.len = disk_link.len;
2627 - err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
2628 + err = fscrypt_fname_usr_to_disk(inode, &istr, &ostr);
2630 goto err_drop_inode;
2631 sd->len = cpu_to_le16(ostr.len);
2632 @@ -3229,7 +3232,7 @@ static int ext4_link(struct dentry *old_dentry,
2633 if (inode->i_nlink >= EXT4_LINK_MAX)
2635 if (ext4_encrypted_inode(dir) &&
2636 - !ext4_is_child_context_consistent_with_parent(dir, inode))
2637 + !fscrypt_has_permitted_context(dir, inode))
2640 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) &&
2641 @@ -3552,8 +3555,7 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2643 if ((old.dir != new.dir) &&
2644 ext4_encrypted_inode(new.dir) &&
2645 - !ext4_is_child_context_consistent_with_parent(new.dir,
2647 + !fscrypt_has_permitted_context(new.dir, old.inode)) {
2651 @@ -3725,10 +3727,8 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
2652 if ((ext4_encrypted_inode(old_dir) ||
2653 ext4_encrypted_inode(new_dir)) &&
2654 (old_dir != new_dir) &&
2655 - (!ext4_is_child_context_consistent_with_parent(new_dir,
2657 - !ext4_is_child_context_consistent_with_parent(old_dir,
2659 + (!fscrypt_has_permitted_context(new_dir, old.inode) ||
2660 + !fscrypt_has_permitted_context(old_dir, new.inode)))
2663 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) &&
2664 diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
2665 index e4fc8ea..f124b24 100644
2666 --- a/fs/ext4/page-io.c
2667 +++ b/fs/ext4/page-io.c
2669 #include <linux/slab.h>
2670 #include <linux/mm.h>
2671 #include <linux/backing-dev.h>
2672 +#include <linux/fscrypto.h>
2674 #include "ext4_jbd2.h"
2676 @@ -67,7 +68,6 @@ static void ext4_finish_bio(struct bio *bio)
2677 struct page *page = bvec->bv_page;
2678 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2679 struct page *data_page = NULL;
2680 - struct ext4_crypto_ctx *ctx = NULL;
2682 struct buffer_head *bh, *head;
2683 unsigned bio_start = bvec->bv_offset;
2684 @@ -82,8 +82,7 @@ static void ext4_finish_bio(struct bio *bio)
2685 if (!page->mapping) {
2686 /* The bounce data pages are unmapped. */
2688 - ctx = (struct ext4_crypto_ctx *)page_private(data_page);
2689 - page = ctx->w.control_page;
2690 + fscrypt_pullback_bio_page(&page, false);
2694 @@ -113,8 +112,8 @@ static void ext4_finish_bio(struct bio *bio)
2695 local_irq_restore(flags);
2697 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2699 - ext4_restore_control_page(data_page);
2701 + fscrypt_restore_control_page(data_page);
2703 end_page_writeback(page);
2705 @@ -474,7 +473,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
2706 gfp_t gfp_flags = GFP_NOFS;
2709 - data_page = ext4_encrypt(inode, page, gfp_flags);
2710 + data_page = fscrypt_encrypt_page(inode, page, gfp_flags);
2711 if (IS_ERR(data_page)) {
2712 ret = PTR_ERR(data_page);
2713 if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
2714 @@ -512,7 +511,7 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
2718 - ext4_restore_control_page(data_page);
2719 + fscrypt_restore_control_page(data_page);
2720 printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
2721 redirty_page_for_writepage(wbc, page);
2723 diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
2724 index dc54a4b..13f3476 100644
2725 --- a/fs/ext4/readpage.c
2726 +++ b/fs/ext4/readpage.c
2732 - * Call ext4_decrypt on every single page, reusing the encryption
2735 -static void completion_pages(struct work_struct *work)
2737 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2738 - struct ext4_crypto_ctx *ctx =
2739 - container_of(work, struct ext4_crypto_ctx, r.work);
2740 - struct bio *bio = ctx->r.bio;
2741 - struct bio_vec *bv;
2744 - bio_for_each_segment_all(bv, bio, i) {
2745 - struct page *page = bv->bv_page;
2747 - int ret = ext4_decrypt(page);
2750 - SetPageError(page);
2752 - SetPageUptodate(page);
2753 - unlock_page(page);
2755 - ext4_release_crypto_ctx(ctx);
2762 static inline bool ext4_bio_encrypted(struct bio *bio)
2764 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2765 @@ -104,14 +73,10 @@ static void mpage_end_io(struct bio *bio)
2768 if (ext4_bio_encrypted(bio)) {
2769 - struct ext4_crypto_ctx *ctx = bio->bi_private;
2771 if (bio->bi_error) {
2772 - ext4_release_crypto_ctx(ctx);
2773 + fscrypt_release_ctx(bio->bi_private);
2775 - INIT_WORK(&ctx->r.work, completion_pages);
2777 - queue_work(ext4_read_workqueue, &ctx->r.work);
2778 + fscrypt_decrypt_bio_pages(bio->bi_private, bio);
2782 @@ -275,11 +240,11 @@ int ext4_mpage_readpages(struct address_space *mapping,
2786 - struct ext4_crypto_ctx *ctx = NULL;
2787 + struct fscrypt_ctx *ctx = NULL;
2789 if (ext4_encrypted_inode(inode) &&
2790 S_ISREG(inode->i_mode)) {
2791 - ctx = ext4_get_crypto_ctx(inode, GFP_NOFS);
2792 + ctx = fscrypt_get_ctx(inode, GFP_NOFS);
2794 goto set_error_page;
2796 @@ -287,7 +252,7 @@ int ext4_mpage_readpages(struct address_space *mapping,
2797 min_t(int, nr_pages, BIO_MAX_PAGES));
2800 - ext4_release_crypto_ctx(ctx);
2801 + fscrypt_release_ctx(ctx);
2802 goto set_error_page;
2804 bio->bi_bdev = bdev;
2805 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
2806 index 304c712..80ee248 100644
2807 --- a/fs/ext4/super.c
2808 +++ b/fs/ext4/super.c
2809 @@ -944,9 +944,6 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
2810 ei->i_datasync_tid = 0;
2811 atomic_set(&ei->i_unwritten, 0);
2812 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
2813 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2814 - ei->i_crypt_info = NULL;
2816 return &ei->vfs_inode;
2819 @@ -1025,8 +1022,7 @@ void ext4_clear_inode(struct inode *inode)
2820 EXT4_I(inode)->jinode = NULL;
2822 #ifdef CONFIG_EXT4_FS_ENCRYPTION
2823 - if (EXT4_I(inode)->i_crypt_info)
2824 - ext4_free_encryption_info(inode, EXT4_I(inode)->i_crypt_info);
2825 + fscrypt_put_encryption_info(inode, NULL);
2829 @@ -1093,6 +1089,83 @@ static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
2830 return try_to_free_buffers(page);
2833 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
2834 +static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
2836 + return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2837 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
2840 +static int ext4_prepare_context(struct inode *inode)
2842 + return ext4_convert_inline_data(inode);
2845 +static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
2851 + /* fs_data is null when internally used. */
2853 + res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2854 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
2857 + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
2858 + ext4_clear_inode_state(inode,
2859 + EXT4_STATE_MAY_INLINE_DATA);
2864 + handle = ext4_journal_start(inode, EXT4_HT_MISC,
2865 + ext4_jbd2_credits_xattr(inode));
2866 + if (IS_ERR(handle))
2867 + return PTR_ERR(handle);
2869 + res = ext4_xattr_set(inode, EXT4_XATTR_INDEX_ENCRYPTION,
2870 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx,
2873 + ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
2874 + res = ext4_mark_inode_dirty(handle, inode);
2876 + EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
2878 + res2 = ext4_journal_stop(handle);
2884 +static int ext4_dummy_context(struct inode *inode)
2886 + return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
2889 +static unsigned ext4_max_namelen(struct inode *inode)
2891 + return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
2895 +static struct fscrypt_operations ext4_cryptops = {
2896 + .get_context = ext4_get_context,
2897 + .prepare_context = ext4_prepare_context,
2898 + .set_context = ext4_set_context,
2899 + .dummy_context = ext4_dummy_context,
2900 + .is_encrypted = ext4_encrypted_inode,
2901 + .empty_dir = ext4_empty_dir,
2902 + .max_namelen = ext4_max_namelen,
2905 +static struct fscrypt_operations ext4_cryptops = {
2906 + .is_encrypted = ext4_encrypted_inode,
2911 static char *quotatypes[] = INITQFNAMES;
2912 #define QTYPE2NAME(t) (quotatypes[t])
2913 @@ -3692,6 +3765,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
2914 sb->s_op = &ext4_sops;
2915 sb->s_export_op = &ext4_export_ops;
2916 sb->s_xattr = ext4_xattr_handlers;
2917 + sb->s_cop = &ext4_cryptops;
2919 sb->dq_op = &ext4_quota_operations;
2920 if (ext4_has_feature_quota(sb))
2921 @@ -5425,7 +5499,6 @@ out5:
2923 static void __exit ext4_exit_fs(void)
2925 - ext4_exit_crypto();
2926 ext4_destroy_lazyinit_thread();
2927 unregister_as_ext2();
2928 unregister_as_ext3();
2929 diff --git a/fs/ext4/symlink.c b/fs/ext4/symlink.c
2930 index 75ed5c2..1f4b19f 100644
2931 --- a/fs/ext4/symlink.c
2932 +++ b/fs/ext4/symlink.c
2937 -#ifdef CONFIG_EXT4_FS_ENCRYPTION
2938 static const char *ext4_encrypted_get_link(struct dentry *dentry,
2939 struct inode *inode,
2940 struct delayed_call *done)
2942 struct page *cpage = NULL;
2943 char *caddr, *paddr = NULL;
2944 - struct ext4_str cstr, pstr;
2945 - struct ext4_encrypted_symlink_data *sd;
2946 + struct fscrypt_str cstr, pstr;
2947 + struct fscrypt_symlink_data *sd;
2948 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
2950 - u32 plen, max_size = inode->i_sb->s_blocksize;
2951 + u32 max_size = inode->i_sb->s_blocksize;
2954 return ERR_PTR(-ECHILD);
2956 - res = ext4_get_encryption_info(inode);
2957 + res = fscrypt_get_encryption_info(inode);
2959 return ERR_PTR(res);
2961 @@ -54,30 +53,29 @@ static const char *ext4_encrypted_get_link(struct dentry *dentry,
2964 /* Symlink is encrypted */
2965 - sd = (struct ext4_encrypted_symlink_data *)caddr;
2966 + sd = (struct fscrypt_symlink_data *)caddr;
2967 cstr.name = sd->encrypted_path;
2968 cstr.len = le16_to_cpu(sd->len);
2970 - sizeof(struct ext4_encrypted_symlink_data) - 1) >
2971 + sizeof(struct fscrypt_symlink_data) - 1) >
2973 /* Symlink data on the disk is corrupted */
2974 res = -EFSCORRUPTED;
2977 - plen = (cstr.len < EXT4_FNAME_CRYPTO_DIGEST_SIZE*2) ?
2978 - EXT4_FNAME_CRYPTO_DIGEST_SIZE*2 : cstr.len;
2979 - paddr = kmalloc(plen + 1, GFP_NOFS);
2983 + res = fscrypt_fname_alloc_buffer(inode, cstr.len, &pstr);
2987 - pstr.name = paddr;
2989 - res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
2991 + res = fscrypt_fname_disk_to_usr(inode, 0, 0, &cstr, &pstr);
2995 + paddr = pstr.name;
2997 /* Null-terminate the name */
2999 + if (res <= pstr.len)
3003 @@ -99,7 +97,6 @@ const struct inode_operations ext4_encrypted_symlink_inode_operations = {
3004 .listxattr = ext4_listxattr,
3005 .removexattr = generic_removexattr,
3009 const struct inode_operations ext4_symlink_inode_operations = {
3010 .readlink = generic_readlink,