Commit efee590e authored by Eric Biggers's avatar Eric Biggers Committed by Theodore Ts'o

fscrypt: pass up error codes from ->get_context()

It was possible for the ->get_context() operation to fail with a
specific error code, which was then not returned to the caller of
FS_IOC_SET_ENCRYPTION_POLICY or FS_IOC_GET_ENCRYPTION_POLICY.  Make sure
to pass through these error codes.  Also reorganize the code so that
->get_context() only needs to be called one time when setting an
encryption policy, and handle contexts of unrecognized sizes more
appropriately.
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent 868e1bc6
...@@ -13,37 +13,20 @@ ...@@ -13,37 +13,20 @@
#include <linux/mount.h> #include <linux/mount.h>
#include "fscrypt_private.h" #include "fscrypt_private.h"
static int inode_has_encryption_context(struct inode *inode)
{
if (!inode->i_sb->s_cop->get_context)
return 0;
return (inode->i_sb->s_cop->get_context(inode, NULL, 0L) > 0);
}
/* /*
* check whether the policy is consistent with the encryption context * check whether an encryption policy is consistent with an encryption context
* for the inode
*/ */
static int is_encryption_context_consistent_with_policy(struct inode *inode, static bool is_encryption_context_consistent_with_policy(
const struct fscrypt_context *ctx,
const struct fscrypt_policy *policy) const struct fscrypt_policy *policy)
{ {
struct fscrypt_context ctx; return memcmp(ctx->master_key_descriptor, policy->master_key_descriptor,
int res;
if (!inode->i_sb->s_cop->get_context)
return 0;
res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
if (res != sizeof(ctx))
return 0;
return (memcmp(ctx.master_key_descriptor, policy->master_key_descriptor,
FS_KEY_DESCRIPTOR_SIZE) == 0 && FS_KEY_DESCRIPTOR_SIZE) == 0 &&
(ctx.flags == policy->flags) && (ctx->flags == policy->flags) &&
(ctx.contents_encryption_mode == (ctx->contents_encryption_mode ==
policy->contents_encryption_mode) && policy->contents_encryption_mode) &&
(ctx.filenames_encryption_mode == (ctx->filenames_encryption_mode ==
policy->filenames_encryption_mode)); policy->filenames_encryption_mode);
} }
static int create_encryption_context_from_policy(struct inode *inode, static int create_encryption_context_from_policy(struct inode *inode,
...@@ -90,6 +73,7 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) ...@@ -90,6 +73,7 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
struct fscrypt_policy policy; struct fscrypt_policy policy;
struct inode *inode = file_inode(filp); struct inode *inode = file_inode(filp);
int ret; int ret;
struct fscrypt_context ctx;
if (copy_from_user(&policy, arg, sizeof(policy))) if (copy_from_user(&policy, arg, sizeof(policy)))
return -EFAULT; return -EFAULT;
...@@ -106,7 +90,8 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) ...@@ -106,7 +90,8 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
inode_lock(inode); inode_lock(inode);
if (!inode_has_encryption_context(inode)) { ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
if (ret == -ENODATA) {
if (!S_ISDIR(inode->i_mode)) if (!S_ISDIR(inode->i_mode))
ret = -ENOTDIR; ret = -ENOTDIR;
else if (!inode->i_sb->s_cop->empty_dir) else if (!inode->i_sb->s_cop->empty_dir)
...@@ -116,8 +101,13 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) ...@@ -116,8 +101,13 @@ int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg)
else else
ret = create_encryption_context_from_policy(inode, ret = create_encryption_context_from_policy(inode,
&policy); &policy);
} else if (!is_encryption_context_consistent_with_policy(inode, } else if (ret == sizeof(ctx) &&
is_encryption_context_consistent_with_policy(&ctx,
&policy)) { &policy)) {
/* The file already uses the same encryption policy. */
ret = 0;
} else if (ret >= 0 || ret == -ERANGE) {
/* The file already uses a different encryption policy. */
ret = -EEXIST; ret = -EEXIST;
} }
...@@ -140,8 +130,10 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) ...@@ -140,8 +130,10 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
return -ENODATA; return -ENODATA;
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 && res != -ERANGE)
return res;
if (res != sizeof(ctx)) if (res != sizeof(ctx))
return -ENODATA; return -EINVAL;
if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
return -EINVAL; return -EINVAL;
......
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