Commit 0d05871e authored by Eric Biggers's avatar Eric Biggers Committed by Greg Kroah-Hartman

fscrypt: remove broken support for detecting keyring key revocation

commit 1b53cf98 upstream.

Filesystem encryption ostensibly supported revoking a keyring key that
had been used to "unlock" encrypted files, causing those files to become
"locked" again.  This was, however, buggy for several reasons, the most
severe of which was that when key revocation happened to be detected for
an inode, its fscrypt_info was immediately freed, even while other
threads could be using it for encryption or decryption concurrently.
This could be exploited to crash the kernel or worse.

This patch fixes the use-after-free by removing the code which detects
the keyring key having been revoked, invalidated, or expired.  Instead,
an encrypted inode that is "unlocked" now simply remains unlocked until
it is evicted from memory.  Note that this is no worse than the case for
block device-level encryption, e.g. dm-crypt, and it still remains
possible for a privileged user to evict unused pages, inodes, and
dentries by running 'sync; echo 3 > /proc/sys/vm/drop_caches', or by
simply unmounting the filesystem.  In fact, one of those actions was
already needed anyway for key revocation to work even somewhat sanely.
This change is not expected to break any applications.

In the future I'd like to implement a real API for fscrypt key
revocation that interacts sanely with ongoing filesystem operations ---
waiting for existing operations to complete and blocking new operations,
and invalidating and sanitizing key material and plaintext from the VFS
caches.  But this is a hard problem, and for now this bug must be fixed.

This bug affected almost all versions of ext4, f2fs, and ubifs
encryption, and it was potentially reachable in any kernel configured
with encryption support (CONFIG_EXT4_ENCRYPTION=y,
CONFIG_EXT4_FS_ENCRYPTION=y, CONFIG_F2FS_FS_ENCRYPTION=y, or
CONFIG_UBIFS_FS_ENCRYPTION=y).  Note that older kernels did not use the
shared fs/crypto/ code, but due to the potential security implications
of this bug, it may still be worthwhile to backport this fix to them.

Fixes: b7236e21 ("ext4 crypto: reorganize how we store keys in the inode")
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
Acked-by: default avatarMichael Halcrow <mhalcrow@google.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent f115bf08
...@@ -394,7 +394,6 @@ EXPORT_SYMBOL(fscrypt_zeroout_range); ...@@ -394,7 +394,6 @@ EXPORT_SYMBOL(fscrypt_zeroout_range);
static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
{ {
struct dentry *dir; struct dentry *dir;
struct fscrypt_info *ci;
int dir_has_key, cached_with_key; int dir_has_key, cached_with_key;
if (flags & LOOKUP_RCU) if (flags & LOOKUP_RCU)
...@@ -406,18 +405,11 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) ...@@ -406,18 +405,11 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
return 0; return 0;
} }
ci = d_inode(dir)->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))))
ci = NULL;
/* this should eventually be an flag in d_flags */ /* this should eventually be an flag in d_flags */
spin_lock(&dentry->d_lock); spin_lock(&dentry->d_lock);
cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY; cached_with_key = dentry->d_flags & DCACHE_ENCRYPTED_WITH_KEY;
spin_unlock(&dentry->d_lock); spin_unlock(&dentry->d_lock);
dir_has_key = (ci != NULL); dir_has_key = (d_inode(dir)->i_crypt_info != NULL);
dput(dir); dput(dir);
/* /*
......
...@@ -350,7 +350,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, ...@@ -350,7 +350,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
fname->disk_name.len = iname->len; fname->disk_name.len = iname->len;
return 0; return 0;
} }
ret = fscrypt_get_crypt_info(dir); ret = fscrypt_get_encryption_info(dir);
if (ret && ret != -EOPNOTSUPP) if (ret && ret != -EOPNOTSUPP)
return ret; return ret;
......
...@@ -67,7 +67,6 @@ struct fscrypt_info { ...@@ -67,7 +67,6 @@ struct fscrypt_info {
u8 ci_filename_mode; u8 ci_filename_mode;
u8 ci_flags; u8 ci_flags;
struct crypto_skcipher *ci_ctfm; struct crypto_skcipher *ci_ctfm;
struct key *ci_keyring_key;
u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE]; u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
}; };
...@@ -87,7 +86,4 @@ struct fscrypt_completion_result { ...@@ -87,7 +86,4 @@ struct fscrypt_completion_result {
/* crypto.c */ /* crypto.c */
int fscrypt_initialize(unsigned int cop_flags); int fscrypt_initialize(unsigned int cop_flags);
/* keyinfo.c */
extern int fscrypt_get_crypt_info(struct inode *);
#endif /* _FSCRYPT_PRIVATE_H */ #endif /* _FSCRYPT_PRIVATE_H */
...@@ -99,6 +99,7 @@ static int validate_user_key(struct fscrypt_info *crypt_info, ...@@ -99,6 +99,7 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
kfree(full_key_descriptor); kfree(full_key_descriptor);
if (IS_ERR(keyring_key)) if (IS_ERR(keyring_key))
return PTR_ERR(keyring_key); return PTR_ERR(keyring_key);
down_read(&keyring_key->sem);
if (keyring_key->type != &key_type_logon) { if (keyring_key->type != &key_type_logon) {
printk_once(KERN_WARNING printk_once(KERN_WARNING
...@@ -106,11 +107,9 @@ static int validate_user_key(struct fscrypt_info *crypt_info, ...@@ -106,11 +107,9 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
res = -ENOKEY; res = -ENOKEY;
goto out; goto out;
} }
down_read(&keyring_key->sem);
ukp = user_key_payload(keyring_key); ukp = user_key_payload(keyring_key);
if (ukp->datalen != sizeof(struct fscrypt_key)) { if (ukp->datalen != sizeof(struct fscrypt_key)) {
res = -EINVAL; res = -EINVAL;
up_read(&keyring_key->sem);
goto out; goto out;
} }
master_key = (struct fscrypt_key *)ukp->data; master_key = (struct fscrypt_key *)ukp->data;
...@@ -121,17 +120,11 @@ static int validate_user_key(struct fscrypt_info *crypt_info, ...@@ -121,17 +120,11 @@ static int validate_user_key(struct fscrypt_info *crypt_info,
"%s: key size incorrect: %d\n", "%s: key size incorrect: %d\n",
__func__, master_key->size); __func__, master_key->size);
res = -ENOKEY; res = -ENOKEY;
up_read(&keyring_key->sem);
goto out; goto out;
} }
res = derive_key_aes(ctx->nonce, master_key->raw, raw_key); res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
up_read(&keyring_key->sem);
if (res)
goto out;
crypt_info->ci_keyring_key = keyring_key;
return 0;
out: out:
up_read(&keyring_key->sem);
key_put(keyring_key); key_put(keyring_key);
return res; return res;
} }
...@@ -173,12 +166,11 @@ static void put_crypt_info(struct fscrypt_info *ci) ...@@ -173,12 +166,11 @@ static void put_crypt_info(struct fscrypt_info *ci)
if (!ci) if (!ci)
return; return;
key_put(ci->ci_keyring_key);
crypto_free_skcipher(ci->ci_ctfm); crypto_free_skcipher(ci->ci_ctfm);
kmem_cache_free(fscrypt_info_cachep, ci); kmem_cache_free(fscrypt_info_cachep, ci);
} }
int fscrypt_get_crypt_info(struct inode *inode) int fscrypt_get_encryption_info(struct inode *inode)
{ {
struct fscrypt_info *crypt_info; struct fscrypt_info *crypt_info;
struct fscrypt_context ctx; struct fscrypt_context ctx;
...@@ -188,21 +180,15 @@ int fscrypt_get_crypt_info(struct inode *inode) ...@@ -188,21 +180,15 @@ int fscrypt_get_crypt_info(struct inode *inode)
u8 *raw_key = NULL; u8 *raw_key = NULL;
int res; int res;
if (inode->i_crypt_info)
return 0;
res = fscrypt_initialize(inode->i_sb->s_cop->flags); res = fscrypt_initialize(inode->i_sb->s_cop->flags);
if (res) if (res)
return res; return res;
if (!inode->i_sb->s_cop->get_context) if (!inode->i_sb->s_cop->get_context)
return -EOPNOTSUPP; return -EOPNOTSUPP;
retry:
crypt_info = ACCESS_ONCE(inode->i_crypt_info);
if (crypt_info) {
if (!crypt_info->ci_keyring_key ||
key_validate(crypt_info->ci_keyring_key) == 0)
return 0;
fscrypt_put_encryption_info(inode, crypt_info);
goto retry;
}
res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
if (res < 0) { if (res < 0) {
...@@ -230,7 +216,6 @@ int fscrypt_get_crypt_info(struct inode *inode) ...@@ -230,7 +216,6 @@ int fscrypt_get_crypt_info(struct inode *inode)
crypt_info->ci_data_mode = ctx.contents_encryption_mode; crypt_info->ci_data_mode = ctx.contents_encryption_mode;
crypt_info->ci_filename_mode = ctx.filenames_encryption_mode; crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
crypt_info->ci_ctfm = NULL; crypt_info->ci_ctfm = NULL;
crypt_info->ci_keyring_key = NULL;
memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor, memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
sizeof(crypt_info->ci_master_key)); sizeof(crypt_info->ci_master_key));
...@@ -286,14 +271,8 @@ int fscrypt_get_crypt_info(struct inode *inode) ...@@ -286,14 +271,8 @@ int fscrypt_get_crypt_info(struct inode *inode)
if (res) if (res)
goto out; goto out;
kzfree(raw_key); if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
raw_key = NULL; crypt_info = NULL;
if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
put_crypt_info(crypt_info);
goto retry;
}
return 0;
out: out:
if (res == -ENOKEY) if (res == -ENOKEY)
res = 0; res = 0;
...@@ -301,6 +280,7 @@ int fscrypt_get_crypt_info(struct inode *inode) ...@@ -301,6 +280,7 @@ int fscrypt_get_crypt_info(struct inode *inode)
kzfree(raw_key); kzfree(raw_key);
return res; return res;
} }
EXPORT_SYMBOL(fscrypt_get_encryption_info);
void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
{ {
...@@ -318,17 +298,3 @@ void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci) ...@@ -318,17 +298,3 @@ void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
put_crypt_info(ci); put_crypt_info(ci);
} }
EXPORT_SYMBOL(fscrypt_put_encryption_info); EXPORT_SYMBOL(fscrypt_put_encryption_info);
int fscrypt_get_encryption_info(struct inode *inode)
{
struct fscrypt_info *ci = 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 fscrypt_get_crypt_info(inode);
return 0;
}
EXPORT_SYMBOL(fscrypt_get_encryption_info);
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