1 ext4 crypto: reorganize how we store keys in the inode
3 This is a pretty massive patch which does a number of different things:
5 1) The per-inode encryption information is now stored in an allocated
6 data structure, ext4_crypt_info, instead of directly in the node.
7 This reduces the size usage of an in-memory inode when it is not
10 2) We drop the ext4_fname_crypto_ctx entirely, and use the per-inode
11 encryption structure instead. This remove an unnecessary memory
12 allocation and free for the fname_crypto_ctx as well as allowing us
13 to reuse the ctfm in a directory for multiple lookups and file
16 3) We also cache the inode's policy information in the ext4_crypt_info
17 structure so we don't have to continually read it out of the
20 4) We now keep the keyring key in the inode's encryption structure
21 instead of releasing it after we are done using it to derive the
22 per-inode key. This allows us to test to see if the key has been
23 revoked; if it has, we prevent the use of the derived key and free
26 5) When an inode is released (or when the derived key is freed), we
27 will use memset_explicit() to zero out the derived key, so it's not
28 left hanging around in memory. This implies that when a user logs
29 out, it is important to first revoke the key, and then unlink it,
30 and then finally, to use "echo 3 > /proc/sys/vm/drop_caches" to
31 release any decrypted pages and dcache entries from the system
34 6) All this, and we also shrink the number of lines of code by around
37 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
39 fs/ext4/crypto.c | 9 +--
40 fs/ext4/crypto_fname.c | 284 ++++++++++++++++++++------------------------------------------------------
41 fs/ext4/crypto_key.c | 74 +++++++++++++++-----
42 fs/ext4/crypto_policy.c | 76 +++++++++++---------
43 fs/ext4/dir.c | 21 +++---
44 fs/ext4/ext4.h | 44 +++++++-----
45 fs/ext4/ext4_crypto.h | 16 ++---
47 fs/ext4/namei.c | 42 ++++-------
48 fs/ext4/super.c | 7 +-
49 fs/ext4/symlink.c | 15 ++--
50 11 files changed, 246 insertions(+), 346 deletions(-)
52 diff --git a/fs/ext4/crypto.c b/fs/ext4/crypto.c
53 index 918200e..3a25aa4 100644
54 --- a/fs/ext4/crypto.c
55 +++ b/fs/ext4/crypto.c
56 @@ -118,8 +118,9 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
57 struct ext4_crypto_ctx *ctx = NULL;
60 - struct ext4_crypt_info *ci = &EXT4_I(inode)->i_crypt_info;
61 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
64 if (!ext4_read_workqueue)
67 @@ -322,7 +323,7 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
71 - BUG_ON(ctx->mode != ei->i_crypt_info.ci_mode);
72 + BUG_ON(ctx->mode != ei->i_crypt_info->ci_mode);
74 if (ctx->mode != EXT4_ENCRYPTION_MODE_AES_256_XTS) {
75 printk_ratelimited(KERN_ERR
76 @@ -334,8 +335,8 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
77 crypto_ablkcipher_clear_flags(atfm, ~0);
78 crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
80 - res = crypto_ablkcipher_setkey(atfm, ei->i_crypt_info.ci_raw,
81 - ei->i_crypt_info.ci_size);
82 + res = crypto_ablkcipher_setkey(atfm, ei->i_crypt_info->ci_raw,
83 + ei->i_crypt_info->ci_size);
85 printk_ratelimited(KERN_ERR
86 "%s: crypto_ablkcipher_setkey() failed\n",
87 diff --git a/fs/ext4/crypto_fname.c b/fs/ext4/crypto_fname.c
88 index d9f08dd..374d0e7 100644
89 --- a/fs/ext4/crypto_fname.c
90 +++ b/fs/ext4/crypto_fname.c
91 @@ -48,6 +48,12 @@ bool ext4_valid_filenames_enc_mode(uint32_t mode)
92 return (mode == EXT4_ENCRYPTION_MODE_AES_256_CTS);
95 +static unsigned max_name_len(struct inode *inode)
97 + return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
102 * ext4_fname_encrypt() -
104 @@ -55,28 +61,30 @@ bool ext4_valid_filenames_enc_mode(uint32_t mode)
105 * ciphertext. Errors are returned as negative numbers. We trust the caller to
106 * allocate sufficient memory to oname string.
108 -static int ext4_fname_encrypt(struct ext4_fname_crypto_ctx *ctx,
109 +static int ext4_fname_encrypt(struct inode *inode,
110 const struct qstr *iname,
111 struct ext4_str *oname)
114 struct ablkcipher_request *req = NULL;
115 DECLARE_EXT4_COMPLETION_RESULT(ecr);
116 - struct crypto_ablkcipher *tfm = ctx->ctfm;
117 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
118 + struct crypto_ablkcipher *tfm = ci->ci_ctfm;
120 char iv[EXT4_CRYPTO_BLOCK_SIZE];
121 struct scatterlist src_sg, dst_sg;
122 - int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
123 + int padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
124 char *workbuf, buf[32], *alloc_buf = NULL;
125 + unsigned lim = max_name_len(inode);
127 - if (iname->len <= 0 || iname->len > ctx->lim)
128 + if (iname->len <= 0 || iname->len > lim)
131 ciphertext_len = (iname->len < EXT4_CRYPTO_BLOCK_SIZE) ?
132 EXT4_CRYPTO_BLOCK_SIZE : iname->len;
133 ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
134 - ciphertext_len = (ciphertext_len > ctx->lim)
135 - ? ctx->lim : ciphertext_len;
136 + ciphertext_len = (ciphertext_len > lim)
137 + ? lim : ciphertext_len;
139 if (ciphertext_len <= sizeof(buf)) {
141 @@ -134,7 +142,7 @@ static int ext4_fname_encrypt(struct ext4_fname_crypto_ctx *ctx,
142 * Errors are returned as negative numbers.
143 * We trust the caller to allocate sufficient memory to oname string.
145 -static int ext4_fname_decrypt(struct ext4_fname_crypto_ctx *ctx,
146 +static int ext4_fname_decrypt(struct inode *inode,
147 const struct ext4_str *iname,
148 struct ext4_str *oname)
150 @@ -142,11 +150,13 @@ static int ext4_fname_decrypt(struct ext4_fname_crypto_ctx *ctx,
151 struct ablkcipher_request *req = NULL;
152 DECLARE_EXT4_COMPLETION_RESULT(ecr);
153 struct scatterlist src_sg, dst_sg;
154 - struct crypto_ablkcipher *tfm = ctx->ctfm;
155 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
156 + struct crypto_ablkcipher *tfm = ci->ci_ctfm;
158 char iv[EXT4_CRYPTO_BLOCK_SIZE];
159 + unsigned lim = max_name_len(inode);
161 - if (iname->len <= 0 || iname->len > ctx->lim)
162 + if (iname->len <= 0 || iname->len > lim)
165 tmp_in[0].name = iname->name;
166 @@ -242,171 +252,50 @@ static int digest_decode(const char *src, int len, char *dst)
171 - * ext4_free_fname_crypto_ctx() -
173 - * Frees up a crypto context.
175 -void ext4_free_fname_crypto_ctx(struct ext4_fname_crypto_ctx *ctx)
177 - if (ctx == NULL || IS_ERR(ctx))
180 - if (ctx->ctfm && !IS_ERR(ctx->ctfm))
181 - crypto_free_ablkcipher(ctx->ctfm);
182 - if (ctx->htfm && !IS_ERR(ctx->htfm))
183 - crypto_free_hash(ctx->htfm);
188 - * ext4_put_fname_crypto_ctx() -
190 - * Return: The crypto context onto free list. If the free list is above a
191 - * threshold, completely frees up the context, and returns the memory.
193 - * TODO: Currently we directly free the crypto context. Eventually we should
194 - * add code it to return to free list. Such an approach will increase
195 - * efficiency of directory lookup.
197 -void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx)
198 +int ext4_setup_fname_crypto(struct inode *inode)
200 - if (*ctx == NULL || IS_ERR(*ctx))
202 - ext4_free_fname_crypto_ctx(*ctx);
207 - * ext4_alloc_fname_crypto_ctx() -
209 -struct ext4_fname_crypto_ctx *ext4_alloc_fname_crypto_ctx(
210 - const struct ext4_crypt_info *ci)
212 - struct ext4_fname_crypto_ctx *ctx;
214 - ctx = kmalloc(sizeof(struct ext4_fname_crypto_ctx), GFP_NOFS);
216 - return ERR_PTR(-ENOMEM);
217 - if (ci->ci_mode == EXT4_ENCRYPTION_MODE_INVALID) {
218 - /* This will automatically set key mode to invalid
219 - * As enum for ENCRYPTION_MODE_INVALID is zero */
220 - memset(&ctx->ci, 0, sizeof(ctx->ci));
222 - memcpy(&ctx->ci, ci, sizeof(struct ext4_crypt_info));
224 - ctx->has_valid_key = (EXT4_ENCRYPTION_MODE_INVALID == ci->ci_mode)
226 - ctx->ctfm_key_is_ready = 0;
233 - * ext4_get_fname_crypto_ctx() -
235 - * Allocates a free crypto context and initializes it to hold
236 - * the crypto material for the inode.
238 - * Return: NULL if not encrypted. Error value on error. Valid pointer otherwise.
240 -struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(
241 - struct inode *inode, u32 max_ciphertext_len)
243 - struct ext4_fname_crypto_ctx *ctx;
244 struct ext4_inode_info *ei = EXT4_I(inode);
245 + struct ext4_crypt_info *ci = ei->i_crypt_info;
246 + struct crypto_ablkcipher *ctfm;
249 /* Check if the crypto policy is set on the inode */
250 res = ext4_encrypted_inode(inode);
254 - if (!ext4_has_encryption_key(inode))
255 - ext4_generate_encryption_key(inode);
257 - /* Get a crypto context based on the key. */
258 - ctx = ext4_alloc_fname_crypto_ctx(&(ei->i_crypt_info));
262 - ctx->flags = ei->i_crypt_policy_flags;
263 - if (ctx->has_valid_key) {
264 - if (ctx->ci.ci_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS) {
265 - printk_once(KERN_WARNING
266 - "ext4: unsupported key mode %d\n",
268 - return ERR_PTR(-ENOKEY);
272 - /* As a first cut, we will allocate new tfm in every call.
273 - * later, we will keep the tfm around, in case the key gets
275 - if (ctx->ctfm == NULL) {
276 - ctx->ctfm = crypto_alloc_ablkcipher("cts(cbc(aes))",
279 - if (IS_ERR(ctx->ctfm)) {
280 - res = PTR_ERR(ctx->ctfm);
282 - KERN_DEBUG "%s: error (%d) allocating crypto tfm\n",
285 - ext4_put_fname_crypto_ctx(&ctx);
286 - return ERR_PTR(res);
288 - if (ctx->ctfm == NULL) {
290 - KERN_DEBUG "%s: could not allocate crypto tfm\n",
292 - ext4_put_fname_crypto_ctx(&ctx);
293 - return ERR_PTR(-ENOMEM);
295 - ctx->lim = max_ciphertext_len;
296 - crypto_ablkcipher_clear_flags(ctx->ctfm, ~0);
297 - crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctx->ctfm),
298 - CRYPTO_TFM_REQ_WEAK_KEY);
300 - /* If we are lucky, we will get a context that is already
301 - * set up with the right key. Else, we will have to
303 - if (!ctx->ctfm_key_is_ready) {
304 - /* Since our crypto objectives for filename encryption
306 - * we directly use the inode master key */
307 - res = crypto_ablkcipher_setkey(ctx->ctfm,
308 - ctx->ci.ci_raw, ctx->ci.ci_size);
310 - ext4_put_fname_crypto_ctx(&ctx);
311 - return ERR_PTR(-EIO);
313 - ctx->ctfm_key_is_ready = 1;
315 - /* In the current implementation, key should never be
316 - * marked "ready" for a context that has just been
317 - * allocated. So we should never reach here */
321 - if (ctx->htfm == NULL)
322 - ctx->htfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
323 - if (IS_ERR(ctx->htfm)) {
324 - res = PTR_ERR(ctx->htfm);
325 - printk(KERN_DEBUG "%s: error (%d) allocating hash tfm\n",
328 - ext4_put_fname_crypto_ctx(&ctx);
329 - return ERR_PTR(res);
330 + res = ext4_get_encryption_info(inode);
333 + ci = ei->i_crypt_info;
335 + if (!ci || ci->ci_ctfm)
338 + if (ci->ci_mode != EXT4_ENCRYPTION_MODE_AES_256_CTS) {
339 + printk_once(KERN_WARNING "ext4: unsupported key mode %d\n",
343 - if (ctx->htfm == NULL) {
344 - printk(KERN_DEBUG "%s: could not allocate hash tfm\n",
346 - ext4_put_fname_crypto_ctx(&ctx);
347 - return ERR_PTR(-ENOMEM);
349 + ctfm = crypto_alloc_ablkcipher("cts(cbc(aes))", 0, 0);
350 + if (!ctfm || IS_ERR(ctfm)) {
351 + res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
352 + printk(KERN_DEBUG "%s: error (%d) allocating crypto tfm\n",
356 + crypto_ablkcipher_clear_flags(ctfm, ~0);
357 + crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
358 + CRYPTO_TFM_REQ_WEAK_KEY);
361 + res = crypto_ablkcipher_setkey(ctfm, ci->ci_raw, ci->ci_size);
363 + crypto_free_ablkcipher(ctfm);
366 + ci->ci_ctfm = ctfm;
371 @@ -420,40 +309,20 @@ u32 ext4_fname_crypto_round_up(u32 size, u32 blksize)
375 - * ext4_fname_crypto_namelen_on_disk() -
377 -int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx,
380 - u32 ciphertext_len;
381 - int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
385 - if (!(ctx->has_valid_key))
387 - ciphertext_len = (namelen < EXT4_CRYPTO_BLOCK_SIZE) ?
388 - EXT4_CRYPTO_BLOCK_SIZE : namelen;
389 - ciphertext_len = ext4_fname_crypto_round_up(ciphertext_len, padding);
390 - ciphertext_len = (ciphertext_len > ctx->lim)
391 - ? ctx->lim : ciphertext_len;
392 - return (int) ciphertext_len;
396 * ext4_fname_crypto_alloc_obuff() -
398 * Allocates an output buffer that is sufficient for the crypto operation
399 * specified by the context and the direction.
401 -int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx,
402 +int ext4_fname_crypto_alloc_buffer(struct inode *inode,
403 u32 ilen, struct ext4_str *crypto_str)
406 - int padding = 4 << (ctx->flags & EXT4_POLICY_FLAGS_PAD_MASK);
408 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
413 + padding = 4 << (ci->ci_flags & EXT4_POLICY_FLAGS_PAD_MASK);
414 if (padding < EXT4_CRYPTO_BLOCK_SIZE)
415 padding = EXT4_CRYPTO_BLOCK_SIZE;
416 olen = ext4_fname_crypto_round_up(ilen, padding);
417 @@ -484,7 +353,7 @@ void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str)
419 * ext4_fname_disk_to_usr() - converts a filename from disk space to user space
421 -int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
422 +int _ext4_fname_disk_to_usr(struct inode *inode,
423 struct dx_hash_info *hinfo,
424 const struct ext4_str *iname,
425 struct ext4_str *oname)
426 @@ -492,8 +361,6 @@ int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
432 if (iname->len < 3) {
433 /*Check for . and .. */
434 if (iname->name[0] == '.' && iname->name[iname->len-1] == '.') {
435 @@ -503,8 +370,8 @@ int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
439 - if (ctx->has_valid_key)
440 - return ext4_fname_decrypt(ctx, iname, oname);
441 + if (EXT4_I(inode)->i_crypt_info)
442 + return ext4_fname_decrypt(inode, iname, oname);
444 if (iname->len <= EXT4_FNAME_CRYPTO_DIGEST_SIZE) {
445 ret = digest_encode(iname->name, iname->len, oname->name);
446 @@ -523,7 +390,7 @@ int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
450 -int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
451 +int ext4_fname_disk_to_usr(struct inode *inode,
452 struct dx_hash_info *hinfo,
453 const struct ext4_dir_entry_2 *de,
454 struct ext4_str *oname)
455 @@ -531,21 +398,20 @@ int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
456 struct ext4_str iname = {.name = (unsigned char *) de->name,
457 .len = de->name_len };
459 - return _ext4_fname_disk_to_usr(ctx, hinfo, &iname, oname);
460 + return _ext4_fname_disk_to_usr(inode, hinfo, &iname, oname);
465 * ext4_fname_usr_to_disk() - converts a filename from user space to disk space
467 -int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
468 +int ext4_fname_usr_to_disk(struct inode *inode,
469 const struct qstr *iname,
470 struct ext4_str *oname)
473 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
477 if (iname->len < 3) {
478 /*Check for . and .. */
479 if (iname->name[0] == '.' &&
480 @@ -556,8 +422,8 @@ int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
484 - if (ctx->has_valid_key) {
485 - res = ext4_fname_encrypt(ctx, iname, oname);
487 + res = ext4_fname_encrypt(inode, iname, oname);
490 /* Without a proper key, a user is not allowed to modify the filenames
491 @@ -569,16 +435,13 @@ int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
492 int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
493 int lookup, struct ext4_filename *fname)
495 - struct ext4_fname_crypto_ctx *ctx;
496 + struct ext4_crypt_info *ci;
497 int ret = 0, bigname = 0;
499 memset(fname, 0, sizeof(struct ext4_filename));
500 fname->usr_fname = iname;
502 - ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
504 - return PTR_ERR(ctx);
505 - if ((ctx == NULL) ||
506 + if (!ext4_encrypted_inode(dir) ||
507 ((iname->name[0] == '.') &&
508 ((iname->len == 1) ||
509 ((iname->name[1] == '.') && (iname->len == 2))))) {
510 @@ -586,12 +449,16 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
511 fname->disk_name.len = iname->len;
514 - if (ctx->has_valid_key) {
515 - ret = ext4_fname_crypto_alloc_buffer(ctx, iname->len,
516 + ret = ext4_setup_fname_crypto(dir);
519 + ci = EXT4_I(dir)->i_crypt_info;
521 + ret = ext4_fname_crypto_alloc_buffer(dir, iname->len,
525 - ret = ext4_fname_encrypt(ctx, iname, &fname->crypto_buf);
526 + ret = ext4_fname_encrypt(dir, iname, &fname->crypto_buf);
529 fname->disk_name.name = fname->crypto_buf.name;
530 @@ -634,7 +501,6 @@ int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
534 - ext4_put_fname_crypto_ctx(&ctx);
538 diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
539 index ec6635d..0075e43 100644
540 --- a/fs/ext4/crypto_key.c
541 +++ b/fs/ext4/crypto_key.c
542 @@ -84,14 +84,26 @@ out:
547 - * ext4_generate_encryption_key() - generates an encryption key
548 - * @inode: The inode to generate the encryption key for.
550 -int ext4_generate_encryption_key(struct inode *inode)
551 +void ext4_free_encryption_info(struct inode *inode)
553 + struct ext4_inode_info *ei = EXT4_I(inode);
554 + struct ext4_crypt_info *ci = ei->i_crypt_info;
559 + if (ci->ci_keyring_key)
560 + key_put(ci->ci_keyring_key);
561 + crypto_free_ablkcipher(ci->ci_ctfm);
562 + memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
564 + ei->i_crypt_info = NULL;
567 +int _ext4_get_encryption_info(struct inode *inode)
569 struct ext4_inode_info *ei = EXT4_I(inode);
570 - struct ext4_crypt_info *crypt_info = &ei->i_crypt_info;
571 + struct ext4_crypt_info *crypt_info;
572 char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
573 (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
574 struct key *keyring_key = NULL;
575 @@ -99,18 +111,40 @@ int ext4_generate_encryption_key(struct inode *inode)
576 struct ext4_encryption_context ctx;
577 struct user_key_payload *ukp;
578 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
579 - int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
580 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
581 - &ctx, sizeof(ctx));
584 - if (res != sizeof(ctx)) {
588 + if (ei->i_crypt_info) {
589 + if (!ei->i_crypt_info->ci_keyring_key ||
590 + key_validate(ei->i_crypt_info->ci_keyring_key) == 0)
592 + ext4_free_encryption_info(inode);
595 + res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
596 + EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
597 + &ctx, sizeof(ctx));
599 + if (!DUMMY_ENCRYPTION_ENABLED(sbi))
601 + ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
602 + ctx.filenames_encryption_mode =
603 + EXT4_ENCRYPTION_MODE_AES_256_CTS;
605 + } else if (res != sizeof(ctx))
609 + crypt_info = kmalloc(sizeof(struct ext4_crypt_info), GFP_KERNEL);
613 ei->i_crypt_policy_flags = ctx.flags;
614 + crypt_info->ci_flags = ctx.flags;
615 + crypt_info->ci_data_mode = ctx.contents_encryption_mode;
616 + crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
617 + crypt_info->ci_ctfm = NULL;
618 + memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
619 + sizeof(crypt_info->ci_master_key));
620 if (S_ISREG(inode->i_mode))
621 crypt_info->ci_mode = ctx.contents_encryption_mode;
622 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
623 @@ -151,17 +185,23 @@ int ext4_generate_encryption_key(struct inode *inode)
624 res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
628 + if (res == -ENOKEY)
632 + ei->i_crypt_info = crypt_info;
633 + crypt_info->ci_keyring_key = keyring_key;
634 + keyring_key = NULL;
637 key_put(keyring_key);
639 - crypt_info->ci_mode = EXT4_ENCRYPTION_MODE_INVALID;
643 int ext4_has_encryption_key(struct inode *inode)
645 struct ext4_inode_info *ei = EXT4_I(inode);
646 - struct ext4_crypt_info *crypt_info = &ei->i_crypt_info;
648 - return (crypt_info->ci_mode != EXT4_ENCRYPTION_MODE_INVALID);
649 + return (ei->i_crypt_info != NULL);
651 diff --git a/fs/ext4/crypto_policy.c b/fs/ext4/crypto_policy.c
652 index a6d6291..370d3aa 100644
653 --- a/fs/ext4/crypto_policy.c
654 +++ b/fs/ext4/crypto_policy.c
655 @@ -126,7 +126,7 @@ int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
656 int ext4_is_child_context_consistent_with_parent(struct inode *parent,
659 - struct ext4_encryption_context parent_ctx, child_ctx;
660 + struct ext4_crypt_info *parent_ci, *child_ci;
663 if ((parent == NULL) || (child == NULL)) {
664 @@ -136,26 +136,28 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent,
665 /* no restrictions if the parent directory is not encrypted */
666 if (!ext4_encrypted_inode(parent))
668 - res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
669 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
670 - &parent_ctx, sizeof(parent_ctx));
671 - if (res != sizeof(parent_ctx))
673 /* if the child directory is not encrypted, this is always a problem */
674 if (!ext4_encrypted_inode(child))
676 - res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION,
677 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
678 - &child_ctx, sizeof(child_ctx));
679 - if (res != sizeof(child_ctx))
680 + res = ext4_get_encryption_info(parent);
683 + res = ext4_get_encryption_info(child);
686 + parent_ci = EXT4_I(parent)->i_crypt_info;
687 + child_ci = EXT4_I(child)->i_crypt_info;
688 + if (!parent_ci && !child_ci)
690 + if (!parent_ci || !child_ci)
692 - return (memcmp(parent_ctx.master_key_descriptor,
693 - child_ctx.master_key_descriptor,
695 + return (memcmp(parent_ci->ci_master_key,
696 + child_ci->ci_master_key,
697 EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
698 - (parent_ctx.contents_encryption_mode ==
699 - child_ctx.contents_encryption_mode) &&
700 - (parent_ctx.filenames_encryption_mode ==
701 - child_ctx.filenames_encryption_mode));
702 + (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
703 + (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
704 + (parent_ci->ci_flags == child_ci->ci_flags));
708 @@ -168,31 +170,37 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent,
709 int ext4_inherit_context(struct inode *parent, struct inode *child)
711 struct ext4_encryption_context ctx;
712 - int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
713 - EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
714 - &ctx, sizeof(ctx));
715 + struct ext4_crypt_info *ci;
718 + res = ext4_get_encryption_info(parent);
721 + ci = EXT4_I(parent)->i_crypt_info;
722 + BUG_ON(ci == NULL);
724 - if (res != sizeof(ctx)) {
725 - if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
726 - ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
727 - ctx.contents_encryption_mode =
728 - EXT4_ENCRYPTION_MODE_AES_256_XTS;
729 - ctx.filenames_encryption_mode =
730 - EXT4_ENCRYPTION_MODE_AES_256_CTS;
732 - memset(ctx.master_key_descriptor, 0x42,
733 - EXT4_KEY_DESCRIPTOR_SIZE);
738 + ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
739 + if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
740 + ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
741 + ctx.filenames_encryption_mode =
742 + EXT4_ENCRYPTION_MODE_AES_256_CTS;
744 + memset(ctx.master_key_descriptor, 0x42,
745 + EXT4_KEY_DESCRIPTOR_SIZE);
748 + ctx.contents_encryption_mode = ci->ci_data_mode;
749 + ctx.filenames_encryption_mode = ci->ci_filename_mode;
750 + ctx.flags = ci->ci_flags;
751 + memcpy(ctx.master_key_descriptor, ci->ci_master_key,
752 + EXT4_KEY_DESCRIPTOR_SIZE);
754 get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
755 res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
756 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
760 ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
764 diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
765 index d799d5d..28cb94f 100644
768 @@ -110,7 +110,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
769 struct super_block *sb = inode->i_sb;
770 struct buffer_head *bh = NULL;
771 int dir_has_error = 0;
772 - struct ext4_fname_crypto_ctx *enc_ctx = NULL;
773 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
775 if (is_dx_dir(inode)) {
776 @@ -134,16 +133,14 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
780 - enc_ctx = ext4_get_fname_crypto_ctx(inode, EXT4_NAME_LEN);
781 - if (IS_ERR(enc_ctx))
782 - return PTR_ERR(enc_ctx);
784 - err = ext4_fname_crypto_alloc_buffer(enc_ctx, EXT4_NAME_LEN,
785 + err = ext4_setup_fname_crypto(inode);
788 + if (ext4_encrypted_inode(inode)) {
789 + err = ext4_fname_crypto_alloc_buffer(inode, EXT4_NAME_LEN,
792 - ext4_put_fname_crypto_ctx(&enc_ctx);
798 offset = ctx->pos & (sb->s_blocksize - 1);
799 @@ -239,8 +236,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
800 offset += ext4_rec_len_from_disk(de->rec_len,
802 if (le32_to_cpu(de->inode)) {
803 - if (enc_ctx == NULL) {
804 - /* Directory is not encrypted */
805 + if (!ext4_encrypted_inode(inode)) {
806 if (!dir_emit(ctx, de->name,
808 le32_to_cpu(de->inode),
809 @@ -250,7 +246,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
810 int save_len = fname_crypto_str.len;
812 /* Directory is encrypted */
813 - err = ext4_fname_disk_to_usr(enc_ctx,
814 + err = ext4_fname_disk_to_usr(inode,
815 NULL, de, &fname_crypto_str);
816 fname_crypto_str.len = save_len;
818 @@ -275,7 +271,6 @@ done:
821 #ifdef CONFIG_EXT4_FS_ENCRYPTION
822 - ext4_put_fname_crypto_ctx(&enc_ctx);
823 ext4_fname_crypto_free_buffer(&fname_crypto_str);
826 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
827 index e3671c2..1db3bfa 100644
830 @@ -955,7 +955,7 @@ struct ext4_inode_info {
832 #ifdef CONFIG_EXT4_FS_ENCRYPTION
833 /* Encryption params */
834 - struct ext4_crypt_info i_crypt_info;
835 + struct ext4_crypt_info *i_crypt_info;
839 @@ -2096,37 +2096,30 @@ static inline int ext4_sb_has_crypto(struct super_block *sb)
841 bool ext4_valid_filenames_enc_mode(uint32_t mode);
842 u32 ext4_fname_crypto_round_up(u32 size, u32 blksize);
843 -int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx,
844 +int ext4_fname_crypto_alloc_buffer(struct inode *inode,
845 u32 ilen, struct ext4_str *crypto_str);
846 -int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
847 +int _ext4_fname_disk_to_usr(struct inode *inode,
848 struct dx_hash_info *hinfo,
849 const struct ext4_str *iname,
850 struct ext4_str *oname);
851 -int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx,
852 +int ext4_fname_disk_to_usr(struct inode *inode,
853 struct dx_hash_info *hinfo,
854 const struct ext4_dir_entry_2 *de,
855 struct ext4_str *oname);
856 -int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx,
857 +int ext4_fname_usr_to_disk(struct inode *inode,
858 const struct qstr *iname,
859 struct ext4_str *oname);
860 -int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx,
862 #ifdef CONFIG_EXT4_FS_ENCRYPTION
863 -void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx);
864 -struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(struct inode *inode,
866 +int ext4_setup_fname_crypto(struct inode *inode);
867 void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str);
868 int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
869 int lookup, struct ext4_filename *fname);
870 void ext4_fname_free_filename(struct ext4_filename *fname);
873 -void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx) { }
875 -struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(struct inode *inode,
877 +int ext4_setup_fname_crypto(struct inode *inode)
882 static inline void ext4_fname_crypto_free_buffer(struct ext4_str *p) { }
883 static inline int ext4_fname_setup_filename(struct inode *dir,
884 @@ -2143,15 +2136,34 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
888 -int ext4_generate_encryption_key(struct inode *inode);
889 +void ext4_free_encryption_info(struct inode *inode);
890 +int _ext4_get_encryption_info(struct inode *inode);
892 #ifdef CONFIG_EXT4_FS_ENCRYPTION
893 int ext4_has_encryption_key(struct inode *inode);
895 +static inline int ext4_get_encryption_info(struct inode *inode)
897 + struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
900 + (ci->ci_keyring_key &&
901 + (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
902 + (1 << KEY_FLAG_REVOKED) |
903 + (1 << KEY_FLAG_DEAD)))))
904 + return _ext4_get_encryption_info(inode);
909 static inline int ext4_has_encryption_key(struct inode *inode)
913 +static inline int ext4_get_encryption_info(struct inode *inode)
920 diff --git a/fs/ext4/ext4_crypto.h b/fs/ext4/ext4_crypto.h
921 index deecbe8..d29687c 100644
922 --- a/fs/ext4/ext4_crypto.h
923 +++ b/fs/ext4/ext4_crypto.h
924 @@ -76,7 +76,13 @@ struct ext4_encryption_key {
925 struct ext4_crypt_info {
926 unsigned char ci_mode;
927 unsigned char ci_size;
929 + char ci_filename_mode;
931 + struct crypto_ablkcipher *ci_ctfm;
932 + struct key *ci_keyring_key;
933 char ci_raw[EXT4_MAX_KEY_SIZE];
934 + char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
937 #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
938 @@ -128,16 +134,6 @@ struct ext4_str {
942 -struct ext4_fname_crypto_ctx {
944 - struct crypto_ablkcipher *ctfm;
945 - struct crypto_hash *htfm;
946 - struct ext4_crypt_info ci;
947 - unsigned flags : 8;
948 - unsigned has_valid_key : 1;
949 - unsigned ctfm_key_is_ready : 1;
953 * For encrypted symlinks, the ciphertext length is stored at the beginning
954 * of the string in little-endian format.
955 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
956 index 0613c25..875ca6b 100644
959 @@ -223,7 +223,7 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
960 struct inode *inode = file->f_mapping->host;
962 if (ext4_encrypted_inode(inode)) {
963 - int err = ext4_generate_encryption_key(inode);
964 + int err = ext4_get_encryption_info(inode);
968 @@ -289,7 +289,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
970 ret = dquot_file_open(inode, filp);
971 if (!ret && ext4_encrypted_inode(inode)) {
972 - ret = ext4_generate_encryption_key(inode);
973 + ret = ext4_get_encryption_info(inode);
977 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
978 index b340643..9bed99f 100644
979 --- a/fs/ext4/namei.c
980 +++ b/fs/ext4/namei.c
981 @@ -607,17 +607,14 @@ static struct stats dx_show_leaf(struct inode *dir,
983 struct ext4_str fname_crypto_str
984 = {.name = NULL, .len = 0};
985 - struct ext4_fname_crypto_ctx *ctx = NULL;
990 - ctx = ext4_get_fname_crypto_ctx(dir,
993 - printk(KERN_WARNING "Error acquiring"
994 - " crypto ctxt--skipping crypto\n");
996 + res = ext4_setup_fname_crypto(dir);
998 + printk(KERN_WARNING "Error setting up"
999 + " fname crypto: %d\n", res);
1002 /* Directory is not encrypted */
1003 @@ -637,7 +634,6 @@ static struct stats dx_show_leaf(struct inode *dir,
1004 "allocating crypto "
1007 - ext4_put_fname_crypto_ctx(&ctx);
1010 res = ext4_fname_disk_to_usr(ctx, NULL, de,
1011 @@ -658,7 +654,6 @@ static struct stats dx_show_leaf(struct inode *dir,
1012 printk("%*.s:(E)%x.%u ", len, name,
1013 h.hash, (unsigned) ((char *) de
1015 - ext4_put_fname_crypto_ctx(&ctx);
1016 ext4_fname_crypto_free_buffer(
1019 @@ -944,7 +939,6 @@ static int htree_dirblock_to_tree(struct file *dir_file,
1020 struct buffer_head *bh;
1021 struct ext4_dir_entry_2 *de, *top;
1022 int err = 0, count = 0;
1023 - struct ext4_fname_crypto_ctx *ctx = NULL;
1024 struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
1026 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
1027 @@ -959,17 +953,15 @@ static int htree_dirblock_to_tree(struct file *dir_file,
1028 EXT4_DIR_REC_LEN(0));
1029 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1030 /* Check if the directory is encrypted */
1031 - ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN);
1032 - if (IS_ERR(ctx)) {
1033 - err = PTR_ERR(ctx);
1034 + err = ext4_setup_fname_crypto(dir);
1039 - if (ctx != NULL) {
1040 - err = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN,
1041 + if (ext4_encrypted_inode(dir)) {
1042 + err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
1045 - ext4_put_fname_crypto_ctx(&ctx);
1049 @@ -990,8 +982,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
1053 - if (ctx == NULL) {
1054 - /* Directory is not encrypted */
1055 + if (!ext4_encrypted_inode(dir)) {
1056 tmp_str.name = de->name;
1057 tmp_str.len = de->name_len;
1058 err = ext4_htree_store_dirent(dir_file,
1059 @@ -1001,7 +992,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
1060 int save_len = fname_crypto_str.len;
1062 /* Directory is encrypted */
1063 - err = ext4_fname_disk_to_usr(ctx, hinfo, de,
1064 + err = ext4_fname_disk_to_usr(dir, hinfo, de,
1068 @@ -1021,7 +1012,6 @@ static int htree_dirblock_to_tree(struct file *dir_file,
1071 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1072 - ext4_put_fname_crypto_ctx(&ctx);
1073 ext4_fname_crypto_free_buffer(&fname_crypto_str);
1076 @@ -3107,7 +3097,6 @@ static int ext4_symlink(struct inode *dir,
1079 if (encryption_required) {
1080 - struct ext4_fname_crypto_ctx *ctx = NULL;
1082 struct ext4_str ostr;
1084 @@ -3119,19 +3108,14 @@ static int ext4_symlink(struct inode *dir,
1085 err = ext4_inherit_context(dir, inode);
1087 goto err_drop_inode;
1088 - ctx = ext4_get_fname_crypto_ctx(inode,
1089 - inode->i_sb->s_blocksize);
1090 - if (IS_ERR_OR_NULL(ctx)) {
1091 - /* We just set the policy, so ctx should not be NULL */
1092 - err = (ctx == NULL) ? -EIO : PTR_ERR(ctx);
1093 + err = ext4_setup_fname_crypto(inode);
1095 goto err_drop_inode;
1097 istr.name = (const unsigned char *) symname;
1099 ostr.name = sd->encrypted_path;
1100 ostr.len = disk_link.len;
1101 - err = ext4_fname_usr_to_disk(ctx, &istr, &ostr);
1102 - ext4_put_fname_crypto_ctx(&ctx);
1103 + err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
1105 goto err_drop_inode;
1106 sd->len = cpu_to_le16(ostr.len);
1107 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
1108 index a9ebe3d..ef19bda 100644
1109 --- a/fs/ext4/super.c
1110 +++ b/fs/ext4/super.c
1111 @@ -877,9 +877,8 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
1112 atomic_set(&ei->i_unwritten, 0);
1113 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1114 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1115 - ei->i_crypt_info.ci_mode = EXT4_ENCRYPTION_MODE_INVALID;
1116 + ei->i_crypt_info = NULL;
1119 return &ei->vfs_inode;
1122 @@ -956,6 +955,10 @@ void ext4_clear_inode(struct inode *inode)
1123 jbd2_free_inode(EXT4_I(inode)->jinode);
1124 EXT4_I(inode)->jinode = NULL;
1126 +#ifdef CONFIG_EXT4_FS_ENCRYPTION
1127 + if (EXT4_I(inode)->i_crypt_info)
1128 + ext4_free_encryption_info(inode);
1132 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
1133 diff --git a/fs/ext4/symlink.c b/fs/ext4/symlink.c
1134 index ca65d45..3287088 100644
1135 --- a/fs/ext4/symlink.c
1136 +++ b/fs/ext4/symlink.c
1137 @@ -29,7 +29,6 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
1138 char *caddr, *paddr = NULL;
1139 struct ext4_str cstr, pstr;
1140 struct inode *inode = d_inode(dentry);
1141 - struct ext4_fname_crypto_ctx *ctx = NULL;
1142 struct ext4_encrypted_symlink_data *sd;
1143 loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
1145 @@ -38,19 +37,17 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
1146 if (!ext4_encrypted_inode(inode))
1147 return page_follow_link_light(dentry, nd);
1149 - ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize);
1152 + res = ext4_setup_fname_crypto(inode);
1154 + return ERR_PTR(res);
1156 if (ext4_inode_is_fast_symlink(inode)) {
1157 caddr = (char *) EXT4_I(inode)->i_data;
1158 max_size = sizeof(EXT4_I(inode)->i_data);
1160 cpage = read_mapping_page(inode->i_mapping, 0, NULL);
1161 - if (IS_ERR(cpage)) {
1162 - ext4_put_fname_crypto_ctx(&ctx);
1163 + if (IS_ERR(cpage))
1166 caddr = kmap(cpage);
1169 @@ -75,21 +72,19 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
1173 - res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr);
1174 + res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
1177 /* Null-terminate the name */
1180 nd_set_link(nd, paddr);
1181 - ext4_put_fname_crypto_ctx(&ctx);
1184 page_cache_release(cpage);
1188 - ext4_put_fname_crypto_ctx(&ctx);
1191 page_cache_release(cpage);