[v2,2/3] fscrypt: Don't allow v1 policies with casefolding
diff mbox series

Message ID 20200107023323.38394-3-drosen@google.com
State Superseded
Headers show
Series
  • Fscrypt support for casefolded encryption
Related show

Commit Message

Daniel Rosenberg Jan. 7, 2020, 2:33 a.m. UTC
Casefolding currently requires a derived key for computing the siphash.
This is available for v2 policies, but not v1, so we disallow it for v1.

Signed-off-by: Daniel Rosenberg <drosen@google.com>
---
 fs/crypto/keysetup.c    |  7 ++++---
 fs/crypto/policy.c      | 39 +++++++++++++++++++++++++++++++++++++++
 fs/inode.c              |  7 +++++++
 include/linux/fscrypt.h | 11 +++++++++++
 4 files changed, 61 insertions(+), 3 deletions(-)

Comments

Eric Biggers Jan. 7, 2020, 3:35 a.m. UTC | #1
On Mon, Jan 06, 2020 at 06:33:22PM -0800, Daniel Rosenberg wrote:
> Casefolding currently requires a derived key for computing the siphash.
> This is available for v2 policies, but not v1, so we disallow it for v1.
> 
> Signed-off-by: Daniel Rosenberg <drosen@google.com>
> ---
>  fs/crypto/keysetup.c    |  7 ++++---
>  fs/crypto/policy.c      | 39 +++++++++++++++++++++++++++++++++++++++
>  fs/inode.c              |  7 +++++++
>  include/linux/fscrypt.h | 11 +++++++++++
>  4 files changed, 61 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
> index c1bd897c9310..7445ab76e0b3 100644
> --- a/fs/crypto/keysetup.c
> +++ b/fs/crypto/keysetup.c
> @@ -224,10 +224,11 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
>  					  FS_KEY_DERIVATION_NONCE_SIZE,
>  					  (u8 *)&ci->ci_hash_key,
>  					  sizeof(ci->ci_hash_key));
> -		if (!err)
> -			ci->ci_hash_key_initialized = true;
> +		if (err)
> +			return err;
> +		ci->ci_hash_key_initialized = true;
>  	}
> -	return err;
> +	return 0;
>  }

This part should be folded into patch 1.

>  
>  /*
> diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
> index f1cff83c151a..9e937cfa732c 100644
> --- a/fs/crypto/policy.c
> +++ b/fs/crypto/policy.c
> @@ -124,6 +124,12 @@ static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
>  					policy->filenames_encryption_mode))
>  		return false;
>  
> +	if (IS_CASEFOLDED(inode)) {
> +		fscrypt_warn(inode,
> +			     "v1 policy does not support casefolded directories");
> +		return false;
> +	}
> +
>  	return true;
>  }
>  
> @@ -579,3 +585,36 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  	return preload ? fscrypt_get_encryption_info(child): 0;
>  }
>  EXPORT_SYMBOL(fscrypt_inherit_context);
> +
> +static int fscrypt_set_casefolding_allowed(struct inode *inode)
> +{
> +	union fscrypt_policy policy;
> +	int err = fscrypt_get_policy(inode, &policy);
> +
> +	if (err)
> +		return err;
> +
> +	if (policy.version != FSCRYPT_POLICY_V2)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +int fscrypt_ioc_setflags_prepare(struct inode *inode,
> +				 unsigned int oldflags,
> +				 unsigned int flags)
> +{
> +	int err;
> +
> +	/*
> +	 * When a directory is encrypted, the CASEFOLD flag can only be turned
> +	 * on if the fscrypt policy supports it.
> +	 */
> +	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
> +		err = fscrypt_set_casefolding_allowed(inode);
> +		if (err)
> +			return err;
> +	}
> +
> +	return 0;
> +}

There's not really any point to the fscrypt_set_casefolding_allowed() function.
It can just be folded into fscrypt_ioc_setflags_prepare():

int fscrypt_ioc_setflags_prepare(struct inode *inode,
				 unsigned int oldflags,
				 unsigned int flags)
{
	union fscrypt_policy policy;
	int err;

	/*
	 * When a directory is encrypted, the CASEFOLD flag can only be turned
	 * on if the fscrypt policy supports it.
	 */
	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
		err = fscrypt_get_policy(inode, &policy);
		if (err)
			return err;
		if (policy.version != FSCRYPT_POLICY_V2)
			return -EINVAL;
	}

	return 0;
}

> @@ -2242,6 +2243,8 @@ EXPORT_SYMBOL(current_time);
>  int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
>  			     unsigned int flags)
>  {
> +	int err;
> +
>  	/*
>  	 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
>  	 * the relevant capability.
> @@ -2252,6 +2255,10 @@ int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
>  	    !capable(CAP_LINUX_IMMUTABLE))
>  		return -EPERM;
>  
> +	err = fscrypt_ioc_setflags_prepare(inode, oldflags, flags);
> +	if (err)
> +		return err;
> +
>  	return 0;
>  }

Can just do 'return fscrypt_ioc_setflags_prepare(inode, oldflags, flags);'

- Eric

Patch
diff mbox series

diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index c1bd897c9310..7445ab76e0b3 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -224,10 +224,11 @@  static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
 					  FS_KEY_DERIVATION_NONCE_SIZE,
 					  (u8 *)&ci->ci_hash_key,
 					  sizeof(ci->ci_hash_key));
-		if (!err)
-			ci->ci_hash_key_initialized = true;
+		if (err)
+			return err;
+		ci->ci_hash_key_initialized = true;
 	}
-	return err;
+	return 0;
 }
 
 /*
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index f1cff83c151a..9e937cfa732c 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -124,6 +124,12 @@  static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy,
 					policy->filenames_encryption_mode))
 		return false;
 
+	if (IS_CASEFOLDED(inode)) {
+		fscrypt_warn(inode,
+			     "v1 policy does not support casefolded directories");
+		return false;
+	}
+
 	return true;
 }
 
@@ -579,3 +585,36 @@  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 	return preload ? fscrypt_get_encryption_info(child): 0;
 }
 EXPORT_SYMBOL(fscrypt_inherit_context);
+
+static int fscrypt_set_casefolding_allowed(struct inode *inode)
+{
+	union fscrypt_policy policy;
+	int err = fscrypt_get_policy(inode, &policy);
+
+	if (err)
+		return err;
+
+	if (policy.version != FSCRYPT_POLICY_V2)
+		return -EINVAL;
+
+	return 0;
+}
+
+int fscrypt_ioc_setflags_prepare(struct inode *inode,
+				 unsigned int oldflags,
+				 unsigned int flags)
+{
+	int err;
+
+	/*
+	 * When a directory is encrypted, the CASEFOLD flag can only be turned
+	 * on if the fscrypt policy supports it.
+	 */
+	if (IS_ENCRYPTED(inode) && (flags & ~oldflags & FS_CASEFOLD_FL)) {
+		err = fscrypt_set_casefolding_allowed(inode);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
diff --git a/fs/inode.c b/fs/inode.c
index 96d62d97694e..77f3e6e2e934 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -20,6 +20,7 @@ 
 #include <linux/ratelimit.h>
 #include <linux/list_lru.h>
 #include <linux/iversion.h>
+#include <linux/fscrypt.h>
 #include <trace/events/writeback.h>
 #include "internal.h"
 
@@ -2242,6 +2243,8 @@  EXPORT_SYMBOL(current_time);
 int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
 			     unsigned int flags)
 {
+	int err;
+
 	/*
 	 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
 	 * the relevant capability.
@@ -2252,6 +2255,10 @@  int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
 	    !capable(CAP_LINUX_IMMUTABLE))
 		return -EPERM;
 
+	err = fscrypt_ioc_setflags_prepare(inode, oldflags, flags);
+	if (err)
+		return err;
+
 	return 0;
 }
 EXPORT_SYMBOL(vfs_ioc_setflags_prepare);
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 1dfbed855bee..2c292f19c6b9 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -142,6 +142,10 @@  extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
 extern int fscrypt_inherit_context(struct inode *, struct inode *,
 					void *, bool);
+extern int fscrypt_ioc_setflags_prepare(struct inode *inode,
+					unsigned int oldflags,
+					unsigned int flags);
+
 /* keyring.c */
 extern void fscrypt_sb_free(struct super_block *sb);
 extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
@@ -383,6 +387,13 @@  static inline int fscrypt_inherit_context(struct inode *parent,
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_ioc_setflags_prepare(struct inode *inode,
+					       unsigned int oldflags,
+					       unsigned int flags)
+{
+	return 0;
+}
+
 /* keyring.c */
 static inline void fscrypt_sb_free(struct super_block *sb)
 {