aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/crypto_key.c
diff options
context:
space:
mode:
authorTheodore Ts'o <tytso@mit.edu>2015-05-18 13:17:47 -0400
committerTheodore Ts'o <tytso@mit.edu>2015-05-18 13:17:47 -0400
commitb7236e21d55ff9008737621c84dd8ee6c37c7c6d (patch)
tree2bd49c631e0ae11f9c9641a954f692cb0d0bdca3 /fs/ext4/crypto_key.c
parente2881b1b51d871a72911faf2fc7e090655940506 (diff)
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: Theodore Ts'o <tytso@mit.edu>
Diffstat (limited to 'fs/ext4/crypto_key.c')
-rw-r--r--fs/ext4/crypto_key.c74
1 files changed, 57 insertions, 17 deletions
diff --git a/fs/ext4/crypto_key.c b/fs/ext4/crypto_key.c
index ec6635dc50f9..0075e43ffea6 100644
--- a/fs/ext4/crypto_key.c
+++ b/fs/ext4/crypto_key.c
@@ -84,14 +84,26 @@ out:
84 return res; 84 return res;
85} 85}
86 86
87/** 87void ext4_free_encryption_info(struct inode *inode)
88 * ext4_generate_encryption_key() - generates an encryption key 88{
89 * @inode: The inode to generate the encryption key for. 89 struct ext4_inode_info *ei = EXT4_I(inode);
90 */ 90 struct ext4_crypt_info *ci = ei->i_crypt_info;
91int ext4_generate_encryption_key(struct inode *inode) 91
92 if (!ci)
93 return;
94
95 if (ci->ci_keyring_key)
96 key_put(ci->ci_keyring_key);
97 crypto_free_ablkcipher(ci->ci_ctfm);
98 memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
99 kfree(ci);
100 ei->i_crypt_info = NULL;
101}
102
103int _ext4_get_encryption_info(struct inode *inode)
92{ 104{
93 struct ext4_inode_info *ei = EXT4_I(inode); 105 struct ext4_inode_info *ei = EXT4_I(inode);
94 struct ext4_crypt_info *crypt_info = &ei->i_crypt_info; 106 struct ext4_crypt_info *crypt_info;
95 char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE + 107 char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
96 (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1]; 108 (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
97 struct key *keyring_key = NULL; 109 struct key *keyring_key = NULL;
@@ -99,18 +111,40 @@ int ext4_generate_encryption_key(struct inode *inode)
99 struct ext4_encryption_context ctx; 111 struct ext4_encryption_context ctx;
100 struct user_key_payload *ukp; 112 struct user_key_payload *ukp;
101 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 113 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
102 int res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, 114 int res;
103 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
104 &ctx, sizeof(ctx));
105 115
106 if (res != sizeof(ctx)) { 116 if (ei->i_crypt_info) {
107 if (res > 0) 117 if (!ei->i_crypt_info->ci_keyring_key ||
108 res = -EINVAL; 118 key_validate(ei->i_crypt_info->ci_keyring_key) == 0)
109 goto out; 119 return 0;
120 ext4_free_encryption_info(inode);
110 } 121 }
122
123 res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
124 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
125 &ctx, sizeof(ctx));
126 if (res < 0) {
127 if (!DUMMY_ENCRYPTION_ENABLED(sbi))
128 return res;
129 ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
130 ctx.filenames_encryption_mode =
131 EXT4_ENCRYPTION_MODE_AES_256_CTS;
132 ctx.flags = 0;
133 } else if (res != sizeof(ctx))
134 return -EINVAL;
111 res = 0; 135 res = 0;
112 136
137 crypt_info = kmalloc(sizeof(struct ext4_crypt_info), GFP_KERNEL);
138 if (!crypt_info)
139 return -ENOMEM;
140
113 ei->i_crypt_policy_flags = ctx.flags; 141 ei->i_crypt_policy_flags = ctx.flags;
142 crypt_info->ci_flags = ctx.flags;
143 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
144 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
145 crypt_info->ci_ctfm = NULL;
146 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
147 sizeof(crypt_info->ci_master_key));
114 if (S_ISREG(inode->i_mode)) 148 if (S_ISREG(inode->i_mode))
115 crypt_info->ci_mode = ctx.contents_encryption_mode; 149 crypt_info->ci_mode = ctx.contents_encryption_mode;
116 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 150 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 res = ext4_derive_key_aes(ctx.nonce, master_key->raw, 185 res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
152 crypt_info->ci_raw); 186 crypt_info->ci_raw);
153out: 187out:
188 if (res < 0) {
189 if (res == -ENOKEY)
190 res = 0;
191 kfree(crypt_info);
192 } else {
193 ei->i_crypt_info = crypt_info;
194 crypt_info->ci_keyring_key = keyring_key;
195 keyring_key = NULL;
196 }
154 if (keyring_key) 197 if (keyring_key)
155 key_put(keyring_key); 198 key_put(keyring_key);
156 if (res < 0)
157 crypt_info->ci_mode = EXT4_ENCRYPTION_MODE_INVALID;
158 return res; 199 return res;
159} 200}
160 201
161int ext4_has_encryption_key(struct inode *inode) 202int ext4_has_encryption_key(struct inode *inode)
162{ 203{
163 struct ext4_inode_info *ei = EXT4_I(inode); 204 struct ext4_inode_info *ei = EXT4_I(inode);
164 struct ext4_crypt_info *crypt_info = &ei->i_crypt_info;
165 205
166 return (crypt_info->ci_mode != EXT4_ENCRYPTION_MODE_INVALID); 206 return (ei->i_crypt_info != NULL);
167} 207}