Skip to content

Commit 8f39850

Browse files
ebiggerstytso
authored andcommitted
fscrypto: improved validation when loading inode encryption metadata
- Validate fscrypt_context.format and fscrypt_context.flags. If unrecognized values are set, then the kernel may not know how to interpret the encrypted file, so it should fail the operation. - Validate that AES_256_XTS is used for contents and that AES_256_CTS is used for filenames. It was previously possible for the kernel to accept these reversed, though it would have taken manual editing of the block device. This was not intended. - Fail cleanly rather than BUG()-ing if a file has an unexpected type. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent dcce7a4 commit 8f39850

2 files changed

Lines changed: 45 additions & 47 deletions

File tree

‎fs/crypto/keyinfo.c‎

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,38 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
139139
return res;
140140
}
141141

142+
static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
143+
const char **cipher_str_ret, int *keysize_ret)
144+
{
145+
if (S_ISREG(inode->i_mode)) {
146+
if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
147+
*cipher_str_ret = "xts(aes)";
148+
*keysize_ret = FS_AES_256_XTS_KEY_SIZE;
149+
return 0;
150+
}
151+
pr_warn_once("fscrypto: unsupported contents encryption mode "
152+
"%d for inode %lu\n",
153+
ci->ci_data_mode, inode->i_ino);
154+
return -ENOKEY;
155+
}
156+
157+
if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
158+
if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
159+
*cipher_str_ret = "cts(cbc(aes))";
160+
*keysize_ret = FS_AES_256_CTS_KEY_SIZE;
161+
return 0;
162+
}
163+
pr_warn_once("fscrypto: unsupported filenames encryption mode "
164+
"%d for inode %lu\n",
165+
ci->ci_filename_mode, inode->i_ino);
166+
return -ENOKEY;
167+
}
168+
169+
pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
170+
(inode->i_mode & S_IFMT), inode->i_ino);
171+
return -ENOKEY;
172+
}
173+
142174
static void put_crypt_info(struct fscrypt_info *ci)
143175
{
144176
if (!ci)
@@ -155,8 +187,8 @@ int get_crypt_info(struct inode *inode)
155187
struct fscrypt_context ctx;
156188
struct crypto_skcipher *ctfm;
157189
const char *cipher_str;
190+
int keysize;
158191
u8 raw_key[FS_MAX_KEY_SIZE];
159-
u8 mode;
160192
int res;
161193

162194
res = fscrypt_initialize();
@@ -179,13 +211,19 @@ int get_crypt_info(struct inode *inode)
179211
if (res < 0) {
180212
if (!fscrypt_dummy_context_enabled(inode))
181213
return res;
214+
ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
182215
ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
183216
ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
184217
ctx.flags = 0;
185218
} else if (res != sizeof(ctx)) {
186219
return -EINVAL;
187220
}
188-
res = 0;
221+
222+
if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
223+
return -EINVAL;
224+
225+
if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
226+
return -EINVAL;
189227

190228
crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
191229
if (!crypt_info)
@@ -198,27 +236,11 @@ int get_crypt_info(struct inode *inode)
198236
crypt_info->ci_keyring_key = NULL;
199237
memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
200238
sizeof(crypt_info->ci_master_key));
201-
if (S_ISREG(inode->i_mode))
202-
mode = crypt_info->ci_data_mode;
203-
else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
204-
mode = crypt_info->ci_filename_mode;
205-
else
206-
BUG();
207-
208-
switch (mode) {
209-
case FS_ENCRYPTION_MODE_AES_256_XTS:
210-
cipher_str = "xts(aes)";
211-
break;
212-
case FS_ENCRYPTION_MODE_AES_256_CTS:
213-
cipher_str = "cts(cbc(aes))";
214-
break;
215-
default:
216-
printk_once(KERN_WARNING
217-
"%s: unsupported key mode %d (ino %u)\n",
218-
__func__, mode, (unsigned) inode->i_ino);
219-
res = -ENOKEY;
239+
240+
res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
241+
if (res)
220242
goto out;
221-
}
243+
222244
if (fscrypt_dummy_context_enabled(inode)) {
223245
memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
224246
goto got_key;
@@ -253,7 +275,7 @@ int get_crypt_info(struct inode *inode)
253275
crypt_info->ci_ctfm = ctfm;
254276
crypto_skcipher_clear_flags(ctfm, ~0);
255277
crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
256-
res = crypto_skcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
278+
res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
257279
if (res)
258280
goto out;
259281

‎include/linux/fscrypto.h‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,6 @@ struct fscrypt_completion_result {
111111
struct fscrypt_completion_result ecr = { \
112112
COMPLETION_INITIALIZER((ecr).completion), 0 }
113113

114-
static inline int fscrypt_key_size(int mode)
115-
{
116-
switch (mode) {
117-
case FS_ENCRYPTION_MODE_AES_256_XTS:
118-
return FS_AES_256_XTS_KEY_SIZE;
119-
case FS_ENCRYPTION_MODE_AES_256_GCM:
120-
return FS_AES_256_GCM_KEY_SIZE;
121-
case FS_ENCRYPTION_MODE_AES_256_CBC:
122-
return FS_AES_256_CBC_KEY_SIZE;
123-
case FS_ENCRYPTION_MODE_AES_256_CTS:
124-
return FS_AES_256_CTS_KEY_SIZE;
125-
default:
126-
BUG();
127-
}
128-
return 0;
129-
}
130-
131114
#define FS_FNAME_NUM_SCATTER_ENTRIES 4
132115
#define FS_CRYPTO_BLOCK_SIZE 16
133116
#define FS_FNAME_CRYPTO_DIGEST_SIZE 32
@@ -202,13 +185,6 @@ static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
202185
return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
203186
}
204187

205-
static inline u32 fscrypt_validate_encryption_key_size(u32 mode, u32 size)
206-
{
207-
if (size == fscrypt_key_size(mode))
208-
return size;
209-
return 0;
210-
}
211-
212188
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
213189
{
214190
if (str->len == 1 && str->name[0] == '.')

0 commit comments

Comments
 (0)