Commit b7236e21 authored by Theodore Ts'o's avatar Theodore Ts'o

ext4 crypto: reorganize how we store keys in the inode

This is a pretty massive patch which does a number of different things:

1) The per-inode encryption information is now stored in an allocated
   data structure, ext4_crypt_info, instead of directly in the node.
   This reduces the size usage of an in-memory inode when it is not
   using encryption.

2) We drop the ext4_fname_crypto_ctx entirely, and use the per-inode
   encryption structure instead.  This remove an unnecessary memory
   allocation and free for the fname_crypto_ctx as well as allowing us
   to reuse the ctfm in a directory for multiple lookups and file
   creations.

3) We also cache the inode's policy information in the ext4_crypt_info
   structure so we don't have to continually read it out of the
   extended attributes.

4) We now keep the keyring key in the inode's encryption structure
   instead of releasing it after we are done using it to derive the
   per-inode key.  This allows us to test to see if the key has been
   revoked; if it has, we prevent the use of the derived key and free
   it.

5) When an inode is released (or when the derived key is freed), we
   will use memset_explicit() to zero out the derived key, so it's not
   left hanging around in memory.  This implies that when a user logs
   out, it is important to first revoke the key, and then unlink it,
   and then finally, to use "echo 3 > /proc/sys/vm/drop_caches" to
   release any decrypted pages and dcache entries from the system
   caches.

6) All this, and we also shrink the number of lines of code by around
   100.  :-)
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent e2881b1b
...@@ -118,8 +118,9 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode) ...@@ -118,8 +118,9 @@ struct ext4_crypto_ctx *ext4_get_crypto_ctx(struct inode *inode)
struct ext4_crypto_ctx *ctx = NULL; struct ext4_crypto_ctx *ctx = NULL;
int res = 0; int res = 0;
unsigned long flags; unsigned long flags;
struct ext4_crypt_info *ci = &EXT4_I(inode)->i_crypt_info; struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
BUG_ON(ci == NULL);
if (!ext4_read_workqueue) if (!ext4_read_workqueue)
ext4_init_crypto(); ext4_init_crypto();
...@@ -322,7 +323,7 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx, ...@@ -322,7 +323,7 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
int res = 0; int res = 0;
BUG_ON(!ctx->tfm); BUG_ON(!ctx->tfm);
BUG_ON(ctx->mode != ei->i_crypt_info.ci_mode); BUG_ON(ctx->mode != ei->i_crypt_info->ci_mode);
if (ctx->mode != EXT4_ENCRYPTION_MODE_AES_256_XTS) { if (ctx->mode != EXT4_ENCRYPTION_MODE_AES_256_XTS) {
printk_ratelimited(KERN_ERR printk_ratelimited(KERN_ERR
...@@ -334,8 +335,8 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx, ...@@ -334,8 +335,8 @@ static int ext4_page_crypto(struct ext4_crypto_ctx *ctx,
crypto_ablkcipher_clear_flags(atfm, ~0); crypto_ablkcipher_clear_flags(atfm, ~0);
crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY); crypto_tfm_set_flags(ctx->tfm, CRYPTO_TFM_REQ_WEAK_KEY);
res = crypto_ablkcipher_setkey(atfm, ei->i_crypt_info.ci_raw, res = crypto_ablkcipher_setkey(atfm, ei->i_crypt_info->ci_raw,
ei->i_crypt_info.ci_size); ei->i_crypt_info->ci_size);
if (res) { if (res) {
printk_ratelimited(KERN_ERR printk_ratelimited(KERN_ERR
"%s: crypto_ablkcipher_setkey() failed\n", "%s: crypto_ablkcipher_setkey() failed\n",
......
This diff is collapsed.
...@@ -84,14 +84,26 @@ static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE], ...@@ -84,14 +84,26 @@ static int ext4_derive_key_aes(char deriving_key[EXT4_AES_128_ECB_KEY_SIZE],
return res; return res;
} }
/** void ext4_free_encryption_info(struct inode *inode)
* ext4_generate_encryption_key() - generates an encryption key {
* @inode: The inode to generate the encryption key for. struct ext4_inode_info *ei = EXT4_I(inode);
*/ struct ext4_crypt_info *ci = ei->i_crypt_info;
int ext4_generate_encryption_key(struct inode *inode)
if (!ci)
return;
if (ci->ci_keyring_key)
key_put(ci->ci_keyring_key);
crypto_free_ablkcipher(ci->ci_ctfm);
memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
kfree(ci);
ei->i_crypt_info = NULL;
}
int _ext4_get_encryption_info(struct inode *inode)
{ {
struct ext4_inode_info *ei = EXT4_I(inode); struct ext4_inode_info *ei = EXT4_I(inode);
struct ext4_crypt_info *crypt_info = &ei->i_crypt_info; struct ext4_crypt_info *crypt_info;
char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
(EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1]; (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
struct key *keyring_key = NULL; struct key *keyring_key = NULL;
...@@ -99,18 +111,40 @@ int ext4_generate_encryption_key(struct inode *inode) ...@@ -99,18 +111,40 @@ int ext4_generate_encryption_key(struct inode *inode)
struct ext4_encryption_context ctx; struct ext4_encryption_context ctx;
struct user_key_payload *ukp; struct user_key_payload *ukp;
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, int res;
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
&ctx, sizeof(ctx));
if (res != sizeof(ctx)) { if (ei->i_crypt_info) {
if (res > 0) if (!ei->i_crypt_info->ci_keyring_key ||
res = -EINVAL; key_validate(ei->i_crypt_info->ci_keyring_key) == 0)
goto out; return 0;
ext4_free_encryption_info(inode);
} }
res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
&ctx, sizeof(ctx));
if (res < 0) {
if (!DUMMY_ENCRYPTION_ENABLED(sbi))
return res;
ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
ctx.filenames_encryption_mode =
EXT4_ENCRYPTION_MODE_AES_256_CTS;
ctx.flags = 0;
} else if (res != sizeof(ctx))
return -EINVAL;
res = 0; res = 0;
crypt_info = kmalloc(sizeof(struct ext4_crypt_info), GFP_KERNEL);
if (!crypt_info)
return -ENOMEM;
ei->i_crypt_policy_flags = ctx.flags; ei->i_crypt_policy_flags = ctx.flags;
crypt_info->ci_flags = ctx.flags;
crypt_info->ci_data_mode = ctx.contents_encryption_mode;
crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
crypt_info->ci_ctfm = NULL;
memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
sizeof(crypt_info->ci_master_key));
if (S_ISREG(inode->i_mode)) if (S_ISREG(inode->i_mode))
crypt_info->ci_mode = ctx.contents_encryption_mode; crypt_info->ci_mode = ctx.contents_encryption_mode;
else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
...@@ -151,17 +185,23 @@ int ext4_generate_encryption_key(struct inode *inode) ...@@ -151,17 +185,23 @@ int ext4_generate_encryption_key(struct inode *inode)
res = ext4_derive_key_aes(ctx.nonce, master_key->raw, res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
crypt_info->ci_raw); crypt_info->ci_raw);
out: out:
if (res < 0) {
if (res == -ENOKEY)
res = 0;
kfree(crypt_info);
} else {
ei->i_crypt_info = crypt_info;
crypt_info->ci_keyring_key = keyring_key;
keyring_key = NULL;
}
if (keyring_key) if (keyring_key)
key_put(keyring_key); key_put(keyring_key);
if (res < 0)
crypt_info->ci_mode = EXT4_ENCRYPTION_MODE_INVALID;
return res; return res;
} }
int ext4_has_encryption_key(struct inode *inode) int ext4_has_encryption_key(struct inode *inode)
{ {
struct ext4_inode_info *ei = EXT4_I(inode); struct ext4_inode_info *ei = EXT4_I(inode);
struct ext4_crypt_info *crypt_info = &ei->i_crypt_info;
return (crypt_info->ci_mode != EXT4_ENCRYPTION_MODE_INVALID); return (ei->i_crypt_info != NULL);
} }
...@@ -126,7 +126,7 @@ int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy) ...@@ -126,7 +126,7 @@ int ext4_get_policy(struct inode *inode, struct ext4_encryption_policy *policy)
int ext4_is_child_context_consistent_with_parent(struct inode *parent, int ext4_is_child_context_consistent_with_parent(struct inode *parent,
struct inode *child) struct inode *child)
{ {
struct ext4_encryption_context parent_ctx, child_ctx; struct ext4_crypt_info *parent_ci, *child_ci;
int res; int res;
if ((parent == NULL) || (child == NULL)) { if ((parent == NULL) || (child == NULL)) {
...@@ -136,26 +136,28 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent, ...@@ -136,26 +136,28 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent,
/* no restrictions if the parent directory is not encrypted */ /* no restrictions if the parent directory is not encrypted */
if (!ext4_encrypted_inode(parent)) if (!ext4_encrypted_inode(parent))
return 1; return 1;
res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION,
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
&parent_ctx, sizeof(parent_ctx));
if (res != sizeof(parent_ctx))
return 0;
/* if the child directory is not encrypted, this is always a problem */ /* if the child directory is not encrypted, this is always a problem */
if (!ext4_encrypted_inode(child)) if (!ext4_encrypted_inode(child))
return 0; return 0;
res = ext4_xattr_get(child, EXT4_XATTR_INDEX_ENCRYPTION, res = ext4_get_encryption_info(parent);
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, if (res)
&child_ctx, sizeof(child_ctx)); return 0;
if (res != sizeof(child_ctx)) res = ext4_get_encryption_info(child);
if (res)
return 0;
parent_ci = EXT4_I(parent)->i_crypt_info;
child_ci = EXT4_I(child)->i_crypt_info;
if (!parent_ci && !child_ci)
return 1;
if (!parent_ci || !child_ci)
return 0; return 0;
return (memcmp(parent_ctx.master_key_descriptor,
child_ctx.master_key_descriptor, return (memcmp(parent_ci->ci_master_key,
child_ci->ci_master_key,
EXT4_KEY_DESCRIPTOR_SIZE) == 0 && EXT4_KEY_DESCRIPTOR_SIZE) == 0 &&
(parent_ctx.contents_encryption_mode == (parent_ci->ci_data_mode == child_ci->ci_data_mode) &&
child_ctx.contents_encryption_mode) && (parent_ci->ci_filename_mode == child_ci->ci_filename_mode) &&
(parent_ctx.filenames_encryption_mode == (parent_ci->ci_flags == child_ci->ci_flags));
child_ctx.filenames_encryption_mode));
} }
/** /**
...@@ -168,31 +170,37 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent, ...@@ -168,31 +170,37 @@ int ext4_is_child_context_consistent_with_parent(struct inode *parent,
int ext4_inherit_context(struct inode *parent, struct inode *child) int ext4_inherit_context(struct inode *parent, struct inode *child)
{ {
struct ext4_encryption_context ctx; struct ext4_encryption_context ctx;
int res = ext4_xattr_get(parent, EXT4_XATTR_INDEX_ENCRYPTION, struct ext4_crypt_info *ci;
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, int res;
&ctx, sizeof(ctx));
res = ext4_get_encryption_info(parent);
if (res < 0)
return res;
ci = EXT4_I(parent)->i_crypt_info;
BUG_ON(ci == NULL);
if (res != sizeof(ctx)) { ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1;
if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) { if (DUMMY_ENCRYPTION_ENABLED(EXT4_SB(parent->i_sb))) {
ctx.format = EXT4_ENCRYPTION_CONTEXT_FORMAT_V1; ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
ctx.contents_encryption_mode = ctx.filenames_encryption_mode =
EXT4_ENCRYPTION_MODE_AES_256_XTS; EXT4_ENCRYPTION_MODE_AES_256_CTS;
ctx.filenames_encryption_mode = ctx.flags = 0;
EXT4_ENCRYPTION_MODE_AES_256_CTS; memset(ctx.master_key_descriptor, 0x42,
ctx.flags = 0; EXT4_KEY_DESCRIPTOR_SIZE);
memset(ctx.master_key_descriptor, 0x42, res = 0;
EXT4_KEY_DESCRIPTOR_SIZE); } else {
res = 0; ctx.contents_encryption_mode = ci->ci_data_mode;
} else { ctx.filenames_encryption_mode = ci->ci_filename_mode;
goto out; ctx.flags = ci->ci_flags;
} memcpy(ctx.master_key_descriptor, ci->ci_master_key,
EXT4_KEY_DESCRIPTOR_SIZE);
} }
get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE); get_random_bytes(ctx.nonce, EXT4_KEY_DERIVATION_NONCE_SIZE);
res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION, res = ext4_xattr_set(child, EXT4_XATTR_INDEX_ENCRYPTION,
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx, EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, &ctx,
sizeof(ctx), 0); sizeof(ctx), 0);
out:
if (!res) if (!res)
ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT); ext4_set_inode_flag(child, EXT4_INODE_ENCRYPT);
return res; return res;
} }
...@@ -110,7 +110,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) ...@@ -110,7 +110,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
struct buffer_head *bh = NULL; struct buffer_head *bh = NULL;
int dir_has_error = 0; int dir_has_error = 0;
struct ext4_fname_crypto_ctx *enc_ctx = NULL;
struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}; struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
if (is_dx_dir(inode)) { if (is_dx_dir(inode)) {
...@@ -134,16 +133,14 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) ...@@ -134,16 +133,14 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
return err; return err;
} }
enc_ctx = ext4_get_fname_crypto_ctx(inode, EXT4_NAME_LEN); err = ext4_setup_fname_crypto(inode);
if (IS_ERR(enc_ctx)) if (err)
return PTR_ERR(enc_ctx); return err;
if (enc_ctx) { if (ext4_encrypted_inode(inode)) {
err = ext4_fname_crypto_alloc_buffer(enc_ctx, EXT4_NAME_LEN, err = ext4_fname_crypto_alloc_buffer(inode, EXT4_NAME_LEN,
&fname_crypto_str); &fname_crypto_str);
if (err < 0) { if (err < 0)
ext4_put_fname_crypto_ctx(&enc_ctx);
return err; return err;
}
} }
offset = ctx->pos & (sb->s_blocksize - 1); offset = ctx->pos & (sb->s_blocksize - 1);
...@@ -239,8 +236,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) ...@@ -239,8 +236,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
offset += ext4_rec_len_from_disk(de->rec_len, offset += ext4_rec_len_from_disk(de->rec_len,
sb->s_blocksize); sb->s_blocksize);
if (le32_to_cpu(de->inode)) { if (le32_to_cpu(de->inode)) {
if (enc_ctx == NULL) { if (!ext4_encrypted_inode(inode)) {
/* Directory is not encrypted */
if (!dir_emit(ctx, de->name, if (!dir_emit(ctx, de->name,
de->name_len, de->name_len,
le32_to_cpu(de->inode), le32_to_cpu(de->inode),
...@@ -250,7 +246,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) ...@@ -250,7 +246,7 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
int save_len = fname_crypto_str.len; int save_len = fname_crypto_str.len;
/* Directory is encrypted */ /* Directory is encrypted */
err = ext4_fname_disk_to_usr(enc_ctx, err = ext4_fname_disk_to_usr(inode,
NULL, de, &fname_crypto_str); NULL, de, &fname_crypto_str);
fname_crypto_str.len = save_len; fname_crypto_str.len = save_len;
if (err < 0) if (err < 0)
...@@ -275,7 +271,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) ...@@ -275,7 +271,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
err = 0; err = 0;
errout: errout:
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
ext4_put_fname_crypto_ctx(&enc_ctx);
ext4_fname_crypto_free_buffer(&fname_crypto_str); ext4_fname_crypto_free_buffer(&fname_crypto_str);
#endif #endif
brelse(bh); brelse(bh);
......
...@@ -955,7 +955,7 @@ struct ext4_inode_info { ...@@ -955,7 +955,7 @@ struct ext4_inode_info {
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
/* Encryption params */ /* Encryption params */
struct ext4_crypt_info i_crypt_info; struct ext4_crypt_info *i_crypt_info;
#endif #endif
}; };
...@@ -2096,37 +2096,30 @@ static inline int ext4_sb_has_crypto(struct super_block *sb) ...@@ -2096,37 +2096,30 @@ static inline int ext4_sb_has_crypto(struct super_block *sb)
/* crypto_fname.c */ /* crypto_fname.c */
bool ext4_valid_filenames_enc_mode(uint32_t mode); bool ext4_valid_filenames_enc_mode(uint32_t mode);
u32 ext4_fname_crypto_round_up(u32 size, u32 blksize); u32 ext4_fname_crypto_round_up(u32 size, u32 blksize);
int ext4_fname_crypto_alloc_buffer(struct ext4_fname_crypto_ctx *ctx, int ext4_fname_crypto_alloc_buffer(struct inode *inode,
u32 ilen, struct ext4_str *crypto_str); u32 ilen, struct ext4_str *crypto_str);
int _ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, int _ext4_fname_disk_to_usr(struct inode *inode,
struct dx_hash_info *hinfo, struct dx_hash_info *hinfo,
const struct ext4_str *iname, const struct ext4_str *iname,
struct ext4_str *oname); struct ext4_str *oname);
int ext4_fname_disk_to_usr(struct ext4_fname_crypto_ctx *ctx, int ext4_fname_disk_to_usr(struct inode *inode,
struct dx_hash_info *hinfo, struct dx_hash_info *hinfo,
const struct ext4_dir_entry_2 *de, const struct ext4_dir_entry_2 *de,
struct ext4_str *oname); struct ext4_str *oname);
int ext4_fname_usr_to_disk(struct ext4_fname_crypto_ctx *ctx, int ext4_fname_usr_to_disk(struct inode *inode,
const struct qstr *iname, const struct qstr *iname,
struct ext4_str *oname); struct ext4_str *oname);
int ext4_fname_crypto_namelen_on_disk(struct ext4_fname_crypto_ctx *ctx,
u32 namelen);
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx); int ext4_setup_fname_crypto(struct inode *inode);
struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(struct inode *inode,
u32 max_len);
void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str); void ext4_fname_crypto_free_buffer(struct ext4_str *crypto_str);
int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname, int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
int lookup, struct ext4_filename *fname); int lookup, struct ext4_filename *fname);
void ext4_fname_free_filename(struct ext4_filename *fname); void ext4_fname_free_filename(struct ext4_filename *fname);
#else #else
static inline static inline
void ext4_put_fname_crypto_ctx(struct ext4_fname_crypto_ctx **ctx) { } int ext4_setup_fname_crypto(struct inode *inode)
static inline
struct ext4_fname_crypto_ctx *ext4_get_fname_crypto_ctx(struct inode *inode,
u32 max_len)
{ {
return NULL; return 0;
} }
static inline void ext4_fname_crypto_free_buffer(struct ext4_str *p) { } static inline void ext4_fname_crypto_free_buffer(struct ext4_str *p) { }
static inline int ext4_fname_setup_filename(struct inode *dir, static inline int ext4_fname_setup_filename(struct inode *dir,
...@@ -2143,15 +2136,34 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) { } ...@@ -2143,15 +2136,34 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname) { }
/* crypto_key.c */ /* crypto_key.c */
int ext4_generate_encryption_key(struct inode *inode); void ext4_free_encryption_info(struct inode *inode);
int _ext4_get_encryption_info(struct inode *inode);
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
int ext4_has_encryption_key(struct inode *inode); int ext4_has_encryption_key(struct inode *inode);
static inline int ext4_get_encryption_info(struct inode *inode)
{
struct ext4_crypt_info *ci = EXT4_I(inode)->i_crypt_info;
if (!ci ||
(ci->ci_keyring_key &&
(ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
(1 << KEY_FLAG_REVOKED) |
(1 << KEY_FLAG_DEAD)))))
return _ext4_get_encryption_info(inode);
return 0;
}
#else #else
static inline int ext4_has_encryption_key(struct inode *inode) static inline int ext4_has_encryption_key(struct inode *inode)
{ {
return 0; return 0;
} }
static inline int ext4_get_encryption_info(struct inode *inode)
{
return 0;
}
#endif #endif
......
...@@ -76,7 +76,13 @@ struct ext4_encryption_key { ...@@ -76,7 +76,13 @@ struct ext4_encryption_key {
struct ext4_crypt_info { struct ext4_crypt_info {
unsigned char ci_mode; unsigned char ci_mode;
unsigned char ci_size; unsigned char ci_size;
char ci_data_mode;
char ci_filename_mode;
char ci_flags;
struct crypto_ablkcipher *ci_ctfm;
struct key *ci_keyring_key;
char ci_raw[EXT4_MAX_KEY_SIZE]; char ci_raw[EXT4_MAX_KEY_SIZE];
char ci_master_key[EXT4_KEY_DESCRIPTOR_SIZE];
}; };
#define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001 #define EXT4_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
...@@ -128,16 +134,6 @@ struct ext4_str { ...@@ -128,16 +134,6 @@ struct ext4_str {
u32 len; u32 len;
}; };
struct ext4_fname_crypto_ctx {
u32 lim;
struct crypto_ablkcipher *ctfm;
struct crypto_hash *htfm;
struct ext4_crypt_info ci;
unsigned flags : 8;
unsigned has_valid_key : 1;
unsigned ctfm_key_is_ready : 1;
};
/** /**
* For encrypted symlinks, the ciphertext length is stored at the beginning * For encrypted symlinks, the ciphertext length is stored at the beginning
* of the string in little-endian format. * of the string in little-endian format.
......
...@@ -223,7 +223,7 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma) ...@@ -223,7 +223,7 @@ static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
struct inode *inode = file->f_mapping->host; struct inode *inode = file->f_mapping->host;
if (ext4_encrypted_inode(inode)) { if (ext4_encrypted_inode(inode)) {
int err = ext4_generate_encryption_key(inode); int err = ext4_get_encryption_info(inode);
if (err) if (err)
return 0; return 0;
} }
...@@ -289,7 +289,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp) ...@@ -289,7 +289,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
} }
ret = dquot_file_open(inode, filp); ret = dquot_file_open(inode, filp);
if (!ret && ext4_encrypted_inode(inode)) { if (!ret && ext4_encrypted_inode(inode)) {
ret = ext4_generate_encryption_key(inode); ret = ext4_get_encryption_info(inode);
if (ret) if (ret)
ret = -EACCES; ret = -EACCES;
} }
......
...@@ -607,17 +607,14 @@ static struct stats dx_show_leaf(struct inode *dir, ...@@ -607,17 +607,14 @@ static struct stats dx_show_leaf(struct inode *dir,
char *name; char *name;
struct ext4_str fname_crypto_str struct ext4_str fname_crypto_str
= {.name = NULL, .len = 0}; = {.name = NULL, .len = 0};
struct ext4_fname_crypto_ctx *ctx = NULL;
int res; int res;
name = de->name; name = de->name;
len = de->name_len; len = de->name_len;
ctx = ext4_get_fname_crypto_ctx(dir, res = ext4_setup_fname_crypto(dir);
EXT4_NAME_LEN); if (res) {
if (IS_ERR(ctx)) { printk(KERN_WARNING "Error setting up"
printk(KERN_WARNING "Error acquiring" " fname crypto: %d\n", res);
" crypto ctxt--skipping crypto\n");
ctx = NULL;
} }
if (ctx == NULL) { if (ctx == NULL) {
/* Directory is not encrypted */ /* Directory is not encrypted */
...@@ -637,7 +634,6 @@ static struct stats dx_show_leaf(struct inode *dir, ...@@ -637,7 +634,6 @@ static struct stats dx_show_leaf(struct inode *dir,
"allocating crypto " "allocating crypto "
"buffer--skipping " "buffer--skipping "
"crypto\n"); "crypto\n");
ext4_put_fname_crypto_ctx(&ctx);
ctx = NULL; ctx = NULL;
} }
res = ext4_fname_disk_to_usr(ctx, NULL, de, res = ext4_fname_disk_to_usr(ctx, NULL, de,
...@@ -658,7 +654,6 @@ static struct stats dx_show_leaf(struct inode *dir, ...@@ -658,7 +654,6 @@ static struct stats dx_show_leaf(struct inode *dir,
printk("%*.s:(E)%x.%u ", len, name, printk("%*.s:(E)%x.%u ", len, name,
h.hash, (unsigned) ((char *) de h.hash, (unsigned) ((char *) de
- base)); - base));
ext4_put_fname_crypto_ctx(&ctx);
ext4_fname_crypto_free_buffer( ext4_fname_crypto_free_buffer(
&fname_crypto_str); &fname_crypto_str);
} }
...@@ -944,7 +939,6 @@ static int htree_dirblock_to_tree(struct file *dir_file, ...@@ -944,7 +939,6 @@ static int htree_dirblock_to_tree(struct file *dir_file,
struct buffer_head *bh; struct buffer_head *bh;
struct ext4_dir_entry_2 *de, *top; struct ext4_dir_entry_2 *de, *top;
int err = 0, count = 0; int err = 0, count = 0;
struct ext4_fname_crypto_ctx *ctx = NULL;
struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str; struct ext4_str fname_crypto_str = {.name = NULL, .len = 0}, tmp_str;
dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n", dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
...@@ -959,17 +953,15 @@ static int htree_dirblock_to_tree(struct file *dir_file, ...@@ -959,17 +953,15 @@ static int htree_dirblock_to_tree(struct file *dir_file,
EXT4_DIR_REC_LEN(0)); EXT4_DIR_REC_LEN(0));
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
/* Check if the directory is encrypted */ /* Check if the directory is encrypted */
ctx = ext4_get_fname_crypto_ctx(dir, EXT4_NAME_LEN); err = ext4_setup_fname_crypto(dir);
if (IS_ERR(ctx)) { if (err) {
err = PTR_ERR(ctx);
brelse(bh); brelse(bh);
return err; return err;
} }
if (ctx != NULL) { if (ext4_encrypted_inode(dir)) {
err = ext4_fname_crypto_alloc_buffer(ctx, EXT4_NAME_LEN, err = ext4_fname_crypto_alloc_buffer(dir, EXT4_NAME_LEN,
&fname_crypto_str); &fname_crypto_str);
if (err < 0) { if (err < 0) {
ext4_put_fname_crypto_ctx(&ctx);
brelse(bh); brelse(bh);
return err; return err;
} }
...@@ -990,8 +982,7 @@ static int htree_dirblock_to_tree(struct file *dir_file, ...@@ -990,8 +982,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
continue; continue;
if (de->inode == 0) if (de->inode == 0)
continue; continue;
if (ctx == NULL) { if (!ext4_encrypted_inode(dir)) {
/* Directory is not encrypted */
tmp_str.name = de->name; tmp_str.name = de->name;
tmp_str.len = de->name_len; tmp_str.len = de->name_len;
err = ext4_htree_store_dirent(dir_file, err = ext4_htree_store_dirent(dir_file,
...@@ -1001,7 +992,7 @@ static int htree_dirblock_to_tree(struct file *dir_file, ...@@ -1001,7 +992,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
int save_len = fname_crypto_str.len; int save_len = fname_crypto_str.len;
/* Directory is encrypted */ /* Directory is encrypted */
err = ext4_fname_disk_to_usr(ctx, hinfo, de, err = ext4_fname_disk_to_usr(dir, hinfo, de,
&fname_crypto_str); &fname_crypto_str);
if (err < 0) { if (err < 0) {
count = err; count = err;
...@@ -1021,7 +1012,6 @@ static int htree_dirblock_to_tree(struct file *dir_file, ...@@ -1021,7 +1012,6 @@ static int htree_dirblock_to_tree(struct file *dir_file,
errout: errout:
brelse(bh); brelse(bh);
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
ext4_put_fname_crypto_ctx(&ctx);
ext4_fname_crypto_free_buffer(&fname_crypto_str); ext4_fname_crypto_free_buffer(&fname_crypto_str);
#endif #endif
return count; return count;
...@@ -3107,7 +3097,6 @@ static int ext4_symlink(struct inode *dir, ...@@ -3107,7 +3097,6 @@ static int ext4_symlink(struct inode *dir,
} }
if (encryption_required) { if (encryption_required) {
struct ext4_fname_crypto_ctx *ctx = NULL;
struct qstr istr; struct qstr istr;
struct ext4_str ostr; struct ext4_str ostr;
...@@ -3119,19 +3108,14 @@ static int ext4_symlink(struct inode *dir, ...@@ -3119,19 +3108,14 @@ static int ext4_symlink(struct inode *dir,
err = ext4_inherit_context(dir, inode); err = ext4_inherit_context(dir, inode);
if (err) if (err)
goto err_drop_inode; goto err_drop_inode;
ctx = ext4_get_fname_crypto_ctx(inode, err = ext4_setup_fname_crypto(inode);
inode->i_sb->s_blocksize); if (err)
if (IS_ERR_OR_NULL(ctx)) {
/* We just set the policy, so ctx should not be NULL */
err = (ctx == NULL) ? -EIO : PTR_ERR(ctx);
goto err_drop_inode; goto err_drop_inode;
}
istr.name = (const unsigned char *) symname; istr.name = (const unsigned char *) symname;
istr.len = len; istr.len = len;
ostr.name = sd->encrypted_path; ostr.name = sd->encrypted_path;
ostr.len = disk_link.len; ostr.len = disk_link.len;
err = ext4_fname_usr_to_disk(ctx, &istr, &ostr); err = ext4_fname_usr_to_disk(inode, &istr, &ostr);
ext4_put_fname_crypto_ctx(&ctx);
if (err < 0) if (err < 0)
goto err_drop_inode; goto err_drop_inode;
sd->len = cpu_to_le16(ostr.len); sd->len = cpu_to_le16(ostr.len);
......
...@@ -879,9 +879,8 @@ static struct inode *ext4_alloc_inode(struct super_block *sb) ...@@ -879,9 +879,8 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
atomic_set(&ei->i_unwritten, 0); atomic_set(&ei->i_unwritten, 0);
INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
#ifdef CONFIG_EXT4_FS_ENCRYPTION #ifdef CONFIG_EXT4_FS_ENCRYPTION
ei->i_crypt_info.ci_mode = EXT4_ENCRYPTION_MODE_INVALID; ei->i_crypt_info = NULL;
#endif #endif
return &ei->vfs_inode; return &ei->vfs_inode;
} }
...@@ -958,6 +957,10 @@ void ext4_clear_inode(struct inode *inode) ...@@ -958,6 +957,10 @@ void ext4_clear_inode(struct inode *inode)
jbd2_free_inode(EXT4_I(inode)->jinode); jbd2_free_inode(EXT4_I(inode)->jinode);
EXT4_I(inode)->jinode = NULL; EXT4_I(inode)->jinode = NULL;
} }
#ifdef CONFIG_EXT4_FS_ENCRYPTION
if (EXT4_I(inode)->i_crypt_info)
ext4_free_encryption_info(inode);
#endif
} }
static struct inode *ext4_nfs_get_inode(struct super_block *sb, static struct inode *ext4_nfs_get_inode(struct super_block *sb,
......
...@@ -29,7 +29,6 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd) ...@@ -29,7 +29,6 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
char *caddr, *paddr = NULL; char *caddr, *paddr = NULL;
struct ext4_str cstr, pstr; struct ext4_str cstr, pstr;
struct inode *inode = d_inode(dentry); struct inode *inode = d_inode(dentry);
struct ext4_fname_crypto_ctx *ctx = NULL;
struct ext4_encrypted_symlink_data *sd; struct ext4_encrypted_symlink_data *sd;
loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1); loff_t size = min_t(loff_t, i_size_read(inode), PAGE_SIZE - 1);
int res; int res;
...@@ -38,19 +37,17 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd) ...@@ -38,19 +37,17 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
if (!ext4_encrypted_inode(inode)) if (!ext4_encrypted_inode(inode))
return page_follow_link_light(dentry, nd); return page_follow_link_light(dentry, nd);
ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize); res = ext4_setup_fname_crypto(inode);
if (IS_ERR(ctx)) if (res)
return ctx; return ERR_PTR(res);
if (ext4_inode_is_fast_symlink(inode)) { if (ext4_inode_is_fast_symlink(inode)) {
caddr = (char *) EXT4_I(inode)->i_data; caddr = (char *) EXT4_I(inode)->i_data;
max_size = sizeof(EXT4_I(inode)->i_data); max_size = sizeof(EXT4_I(inode)->i_data);
} else { } else {
cpage = read_mapping_page(inode->i_mapping, 0, NULL); cpage = read_mapping_page(inode->i_mapping, 0, NULL);
if (IS_ERR(cpage)) { if (IS_ERR(cpage))
ext4_put_fname_crypto_ctx(&ctx);
return cpage; return cpage;
}
caddr = kmap(cpage); caddr = kmap(cpage);
caddr[size] = 0; caddr[size] = 0;
} }
...@@ -75,21 +72,19 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd) ...@@ -75,21 +72,19 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
} }
pstr.name = paddr; pstr.name = paddr;
pstr.len = plen; pstr.len = plen;
res = _ext4_fname_disk_to_usr(ctx, NULL, &cstr, &pstr); res = _ext4_fname_disk_to_usr(inode, NULL, &cstr, &pstr);
if (res < 0) if (res < 0)
goto errout; goto errout;
/* Null-terminate the name */ /* Null-terminate the name */
if (res <= plen) if (res <= plen)
paddr[res] = '\0'; paddr[res] = '\0';
nd_set_link(nd, paddr); nd_set_link(nd, paddr);
ext4_put_fname_crypto_ctx(&ctx);
if (cpage) { if (cpage) {
kunmap(cpage); kunmap(cpage);
page_cache_release(cpage); page_cache_release(cpage);
} }
return NULL; return NULL;
errout: errout:
ext4_put_fname_crypto_ctx(&ctx);
if (cpage) { if (cpage) {
kunmap(cpage); kunmap(cpage);
page_cache_release(cpage); page_cache_release(cpage);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment