All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction
@ 2020-08-24  6:17 ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

This series fixes some deadlocks which are theoretically possible due to
fscrypt_get_encryption_info() being GFP_NOFS-unsafe, and thus not safe
to be called from within an ext4 transaction or under f2fs_lock_op().

The problem is solved by new helper functions which allow setting up the
key for new inodes earlier.  Patch 1 adds these helper functions.  Also
see that patch for a more detailed description of this problem.

Patches 2-6 then convert ext4, f2fs, and ubifs to use these new helpers.

Patch 7-8 then clean up a few things afterwards.

Coincidentally, this also solves some of the ordering problems that
ceph fscrypt support will have.  For more details about this, see the
discussion on Jeff Layton's RFC patchset for ceph fscrypt support
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
However, fscrypt_prepare_new_inode() still requires that the new
'struct inode' exist already, so it might not be enough for ceph yet.

This patchset applies to v5.9-rc2.

Eric Biggers (8):
  fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  ext4: factor out ext4_xattr_credits_for_new_inode()
  ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
  ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  fscrypt: remove fscrypt_inherit_context()
  fscrypt: stop pretending that key setup is nofs-safe

 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/hooks.c           |  10 +-
 fs/crypto/inline_crypt.c    |   7 +-
 fs/crypto/keysetup.c        | 190 ++++++++++++++++++++++++++++--------
 fs/crypto/keysetup_v1.c     |   8 +-
 fs/crypto/policy.c          |  64 +++++++-----
 fs/ext4/ialloc.c            | 118 +++++++++++-----------
 fs/f2fs/dir.c               |   2 +-
 fs/f2fs/f2fs.h              |  16 ---
 fs/f2fs/namei.c             |   7 +-
 fs/ubifs/dir.c              |  26 ++---
 include/linux/fscrypt.h     |  18 +++-
 12 files changed, 293 insertions(+), 176 deletions(-)

-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction
@ 2020-08-24  6:17 ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

This series fixes some deadlocks which are theoretically possible due to
fscrypt_get_encryption_info() being GFP_NOFS-unsafe, and thus not safe
to be called from within an ext4 transaction or under f2fs_lock_op().

The problem is solved by new helper functions which allow setting up the
key for new inodes earlier.  Patch 1 adds these helper functions.  Also
see that patch for a more detailed description of this problem.

Patches 2-6 then convert ext4, f2fs, and ubifs to use these new helpers.

Patch 7-8 then clean up a few things afterwards.

Coincidentally, this also solves some of the ordering problems that
ceph fscrypt support will have.  For more details about this, see the
discussion on Jeff Layton's RFC patchset for ceph fscrypt support
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
However, fscrypt_prepare_new_inode() still requires that the new
'struct inode' exist already, so it might not be enough for ceph yet.

This patchset applies to v5.9-rc2.

Eric Biggers (8):
  fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  ext4: factor out ext4_xattr_credits_for_new_inode()
  ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
  ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  fscrypt: remove fscrypt_inherit_context()
  fscrypt: stop pretending that key setup is nofs-safe

 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/hooks.c           |  10 +-
 fs/crypto/inline_crypt.c    |   7 +-
 fs/crypto/keysetup.c        | 190 ++++++++++++++++++++++++++++--------
 fs/crypto/keysetup_v1.c     |   8 +-
 fs/crypto/policy.c          |  64 +++++++-----
 fs/ext4/ialloc.c            | 118 +++++++++++-----------
 fs/f2fs/dir.c               |   2 +-
 fs/f2fs/f2fs.h              |  16 ---
 fs/f2fs/namei.c             |   7 +-
 fs/ubifs/dir.c              |  26 ++---
 include/linux/fscrypt.h     |  18 +++-
 12 files changed, 293 insertions(+), 176 deletions(-)

-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction
@ 2020-08-24  6:17 ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

This series fixes some deadlocks which are theoretically possible due to
fscrypt_get_encryption_info() being GFP_NOFS-unsafe, and thus not safe
to be called from within an ext4 transaction or under f2fs_lock_op().

The problem is solved by new helper functions which allow setting up the
key for new inodes earlier.  Patch 1 adds these helper functions.  Also
see that patch for a more detailed description of this problem.

Patches 2-6 then convert ext4, f2fs, and ubifs to use these new helpers.

Patch 7-8 then clean up a few things afterwards.

Coincidentally, this also solves some of the ordering problems that
ceph fscrypt support will have.  For more details about this, see the
discussion on Jeff Layton's RFC patchset for ceph fscrypt support
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
However, fscrypt_prepare_new_inode() still requires that the new
'struct inode' exist already, so it might not be enough for ceph yet.

This patchset applies to v5.9-rc2.

Eric Biggers (8):
  fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  ext4: factor out ext4_xattr_credits_for_new_inode()
  ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
  ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  fscrypt: remove fscrypt_inherit_context()
  fscrypt: stop pretending that key setup is nofs-safe

 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/hooks.c           |  10 +-
 fs/crypto/inline_crypt.c    |   7 +-
 fs/crypto/keysetup.c        | 190 ++++++++++++++++++++++++++++--------
 fs/crypto/keysetup_v1.c     |   8 +-
 fs/crypto/policy.c          |  64 +++++++-----
 fs/ext4/ialloc.c            | 118 +++++++++++-----------
 fs/f2fs/dir.c               |   2 +-
 fs/f2fs/f2fs.h              |  16 ---
 fs/f2fs/namei.c             |   7 +-
 fs/ubifs/dir.c              |  26 ++---
 include/linux/fscrypt.h     |  18 +++-
 12 files changed, 293 insertions(+), 176 deletions(-)

-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() (setting up an inode's encryption key) is
intended to be GFP_NOFS safe.  But actually it isn't, since it uses
functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
when called with memalloc_nofs_save().  Therefore it can deadlock when
called from a context that needs GFP_NOFS, e.g. during an ext4
filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
Currently, this happens when creating a new encrypted file.

We can't fix this just by not setting up the key for new inodes right
away, since new symlinks need their key to encrypt the symlink target.

So we need to set up the new inode's key before starting the
transaction.  But fscrypt_get_encryption_info() can't do this currently,
since it assumes the encryption xattr is already set, and the encryption
xattr can't be set until the transaction.

The recently proposed fscrypt support for the ceph filesystem
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
will have this same ordering problem too, since when ceph creates a new
symlink, it will need to encrypt it before setting its encryption xattr.

To solve this problem, add new helper functions:

- fscrypt_prepare_new_inode() sets up a new inode's encryption key
  (fscrypt_info), using the parent directory's encryption policy and a
  new random nonce.  It neither reads nor writes the xattr.

- fscrypt_set_context() sets the encryption xattr from the fscrypt_info
  already in memory.  This replaces fscrypt_inherit_context().

Keep fscrypt_inherit_context() around temporarily until all filesystems
are converted to use fscrypt_set_context().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
 fs/crypto/policy.c          |  61 ++++++++++--
 include/linux/fscrypt.h     |  17 ++++
 4 files changed, 220 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 8117a61b6f558..355f6d9377517 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 			       const struct fscrypt_master_key *mk);
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk);
+
 /* keysetup_v1.c */
 
 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index fea6226afc2b0..6ac816d3e8478 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -10,6 +10,7 @@
 
 #include <crypto/skcipher.h>
 #include <linux/key.h>
+#include <linux/random.h>
 
 #include "fscrypt_private.h"
 
@@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 	return 0;
 }
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk)
+{
+	WARN_ON(ci->ci_inode->i_ino == 0);
+	WARN_ON(!mk->mk_ino_hash_key_initialized);
+
+	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
+					      &mk->mk_ino_hash_key);
+}
+
 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 					    struct fscrypt_master_key *mk)
 {
@@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 			return err;
 	}
 
-	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
-					      &mk->mk_ino_hash_key);
+	if (ci->ci_inode->i_ino == 0)
+		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
+	else
+		fscrypt_hash_inode_number(ci, mk);
 	return 0;
 }
 
@@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
 	kmem_cache_free(fscrypt_info_cachep, ci);
 }
 
-int fscrypt_get_encryption_info(struct inode *inode)
+static int
+fscrypt_setup_encryption_info(struct inode *inode,
+			      const union fscrypt_policy *policy,
+			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	struct fscrypt_info *crypt_info;
-	union fscrypt_context ctx;
 	struct fscrypt_mode *mode;
 	struct key *master_key = NULL;
 	int res;
 
-	if (fscrypt_has_encryption_key(inode))
-		return 0;
-
-	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
-	if (res)
-		return res;
-
-	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
-	if (res < 0) {
-		const union fscrypt_context *dummy_ctx =
-			fscrypt_get_dummy_context(inode->i_sb);
-
-		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
-			fscrypt_warn(inode,
-				     "Error %d getting encryption context",
-				     res);
-			return res;
-		}
-		/* Fake up a context for an unencrypted directory */
-		res = fscrypt_context_size(dummy_ctx);
-		memcpy(&ctx, dummy_ctx, res);
-	}
-
 	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
 	if (!crypt_info)
 		return -ENOMEM;
 
 	crypt_info->ci_inode = inode;
-
-	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
-	if (res) {
-		fscrypt_warn(inode,
-			     "Unrecognized or corrupt encryption context");
-		goto out;
-	}
-
-	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
-	       FSCRYPT_FILE_NONCE_SIZE);
-
-	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
-		res = -EINVAL;
-		goto out;
-	}
+	crypt_info->ci_policy = *policy;
+	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 
 	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
 	if (IS_ERR(mode)) {
@@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
 	put_crypt_info(crypt_info);
 	return res;
 }
+
+/**
+ * fscrypt_get_encryption_info() - set up an inode's encryption key
+ * @inode: the inode to set up the key for.  Must either be encrypted, or be an
+ *	   unencrypted directory with '-o test_dummy_encryption'.
+ *
+ * Create ->i_crypt_info, if it's not already set.
+ *
+ * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
+ * generally this shouldn't be called from within a filesystem transaction.
+ *
+ * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
+ *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
+ *	   distinguish these cases.)  Also can return another -errno code.
+ */
+int fscrypt_get_encryption_info(struct inode *inode)
+{
+	int res;
+	union fscrypt_context ctx;
+	union fscrypt_policy policy;
+
+	if (fscrypt_has_encryption_key(inode))
+		return 0;
+
+	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
+	if (res)
+		return res;
+
+	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
+	if (res < 0) {
+		const union fscrypt_context *dummy_ctx =
+			fscrypt_get_dummy_context(inode->i_sb);
+
+		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
+			fscrypt_warn(inode,
+				     "Error %d getting encryption context",
+				     res);
+			return res;
+		}
+		/* Fake up a context for an unencrypted directory */
+		res = fscrypt_context_size(dummy_ctx);
+		memcpy(&ctx, dummy_ctx, res);
+	}
+
+	res = fscrypt_policy_from_context(&policy, &ctx, res);
+	if (res) {
+		fscrypt_warn(inode,
+			     "Unrecognized or corrupt encryption context");
+		return res;
+	}
+
+	if (!fscrypt_supported_policy(&policy, inode))
+		return -EINVAL;
+
+	return fscrypt_setup_encryption_info(inode, &policy,
+					     fscrypt_context_nonce(&ctx));
+}
 EXPORT_SYMBOL(fscrypt_get_encryption_info);
 
+/**
+ * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
+ * @dir: a possibly-encrypted directory
+ * @inode: the inode that is being created.  ->i_mode must be set already.
+ *	   ->i_ino does *not* need to be set yet.
+ * @encrypt_ret: (output) set to true if the new inode will be encrypted.
+ *
+ * Prepares to create a new inode in a directory.  If either the inode or its
+ * filename will be encrypted, this sets up the directory's
+ * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
+ * its ->i_crypt_info and sets *encrypt_ret to true.
+ *
+ * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
+ * right away.  However, symlinks do need it.
+ *
+ * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
+ * any filesystem transaction to create the inode.  For this reason, ->i_ino
+ * isn't required to be set yet, as the filesystem may not have set it yet.
+ *
+ * This doesn't actually store the new inode's encryption context to disk.
+ * That still needs to be done later by calling fscrypt_set_context().
+ *
+ * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
+ *	   or another -errno code
+ */
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret)
+{
+	int err;
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
+
+	/*
+	 * If the filesystem is mounted with '-o test_dummy_encryption', files
+	 * created in unencrypted directories are automatically encrypted.  For
+	 * that case, we just need to treat the directory as encrypted here.
+	 */
+
+	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
+		return 0;
+
+	err = fscrypt_get_encryption_info(dir);
+	if (err)
+		return err;
+	if (!fscrypt_has_encryption_key(dir))
+		return -ENOKEY;
+
+	if (WARN_ON_ONCE(inode->i_mode == 0))
+		return -EINVAL;
+
+	/*
+	 * Only regular files, directories, and symlinks are encrypted.
+	 * Special files like device nodes and named pipes aren't.
+	 */
+	if (!S_ISREG(inode->i_mode) &&
+	    !S_ISDIR(inode->i_mode) &&
+	    !S_ISLNK(inode->i_mode))
+		return 0;
+
+	*encrypt_ret = true;
+
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+
+	return fscrypt_setup_encryption_info(inode,
+					     &dir->i_crypt_info->ci_policy,
+					     nonce);
+}
+EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
+
 /**
  * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
  * @inode: an inode being evicted
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index 2d73fd39ad96f..fbe4933206469 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  *				       an fscrypt_policy
  * @ctx_u: output context
  * @policy_u: input policy
+ * @nonce: nonce to use
  *
  * Create an fscrypt_context for an inode that is being assigned the given
- * encryption policy.  A new nonce is randomly generated.
+ * encryption policy.  @nonce must be a new random nonce.
  *
  * Return: the size of the new context in bytes.
  */
-static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
-					   const union fscrypt_policy *policy_u)
+static int
+fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
+				const union fscrypt_policy *policy_u,
+				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	memset(ctx_u, 0, sizeof(*ctx_u));
 
@@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_descriptor,
 		       policy->master_key_descriptor,
 		       sizeof(ctx->master_key_descriptor));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	case FSCRYPT_POLICY_V2: {
@@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_identifier,
 		       policy->master_key_identifier,
 		       sizeof(ctx->master_key_identifier));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	}
@@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
 static int set_encryption_policy(struct inode *inode,
 				 const union fscrypt_policy *policy)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	int err;
@@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
 		return -EINVAL;
 	}
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
 
 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
 }
@@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 						void *fs_data, bool preload)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	struct fscrypt_info *ci;
@@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 	if (ci == NULL)
 		return -ENOKEY;
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
 
 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
 	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
@@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 }
 EXPORT_SYMBOL(fscrypt_inherit_context);
 
+/**
+ * fscrypt_set_context() - Set the fscrypt context of a new inode
+ * @inode: A new inode
+ * @fs_data: private data given by FS and passed to ->set_context()
+ *
+ * This should be called after fscrypt_prepare_new_inode(), generally during a
+ * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
+ *
+ * Return: 0 on success, -errno on failure
+ */
+int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	struct fscrypt_info *ci = inode->i_crypt_info;
+	union fscrypt_context ctx;
+	int ctxsize;
+
+	/* fscrypt_prepare_new_inode() should have set up the key already. */
+	if (WARN_ON_ONCE(!ci))
+		return -ENOKEY;
+
+	/*
+	 * This may be the first time the inode number is available, so do any
+	 * delayed key setup that requires the inode number.
+	 */
+	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
+	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
+		const struct fscrypt_master_key *mk =
+			ci->ci_master_key->payload.data[0];
+
+		fscrypt_hash_inode_number(ci, mk);
+	}
+
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
+						  ci->ci_nonce);
+
+	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
+	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
+}
+EXPORT_SYMBOL_GPL(fscrypt_set_context);
+
 /**
  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
  * @sb: the filesystem on which test_dummy_encryption is being specified
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 991ff8575d0e7..726131dfa0a9b 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 			    void *fs_data, bool preload);
+int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
 	const union fscrypt_context *ctx;
@@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
 
 /* keysetup.c */
 int fscrypt_get_encryption_info(struct inode *inode);
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret);
 void fscrypt_put_encryption_info(struct inode *inode);
 void fscrypt_free_inode(struct inode *inode);
 int fscrypt_drop_inode(struct inode *inode);
@@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	return -EOPNOTSUPP;
+}
+
 struct fscrypt_dummy_context {
 };
 
@@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_prepare_new_inode(struct inode *dir,
+					    struct inode *inode,
+					    bool *encrypt_ret)
+{
+	if (IS_ENCRYPTED(dir))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
 static inline void fscrypt_put_encryption_info(struct inode *inode)
 {
 	return;
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() (setting up an inode's encryption key) is
intended to be GFP_NOFS safe.  But actually it isn't, since it uses
functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
when called with memalloc_nofs_save().  Therefore it can deadlock when
called from a context that needs GFP_NOFS, e.g. during an ext4
filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
Currently, this happens when creating a new encrypted file.

We can't fix this just by not setting up the key for new inodes right
away, since new symlinks need their key to encrypt the symlink target.

So we need to set up the new inode's key before starting the
transaction.  But fscrypt_get_encryption_info() can't do this currently,
since it assumes the encryption xattr is already set, and the encryption
xattr can't be set until the transaction.

The recently proposed fscrypt support for the ceph filesystem
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
will have this same ordering problem too, since when ceph creates a new
symlink, it will need to encrypt it before setting its encryption xattr.

To solve this problem, add new helper functions:

- fscrypt_prepare_new_inode() sets up a new inode's encryption key
  (fscrypt_info), using the parent directory's encryption policy and a
  new random nonce.  It neither reads nor writes the xattr.

- fscrypt_set_context() sets the encryption xattr from the fscrypt_info
  already in memory.  This replaces fscrypt_inherit_context().

Keep fscrypt_inherit_context() around temporarily until all filesystems
are converted to use fscrypt_set_context().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
 fs/crypto/policy.c          |  61 ++++++++++--
 include/linux/fscrypt.h     |  17 ++++
 4 files changed, 220 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 8117a61b6f558..355f6d9377517 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 			       const struct fscrypt_master_key *mk);
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk);
+
 /* keysetup_v1.c */
 
 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index fea6226afc2b0..6ac816d3e8478 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -10,6 +10,7 @@
 
 #include <crypto/skcipher.h>
 #include <linux/key.h>
+#include <linux/random.h>
 
 #include "fscrypt_private.h"
 
@@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 	return 0;
 }
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk)
+{
+	WARN_ON(ci->ci_inode->i_ino == 0);
+	WARN_ON(!mk->mk_ino_hash_key_initialized);
+
+	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
+					      &mk->mk_ino_hash_key);
+}
+
 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 					    struct fscrypt_master_key *mk)
 {
@@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 			return err;
 	}
 
-	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
-					      &mk->mk_ino_hash_key);
+	if (ci->ci_inode->i_ino == 0)
+		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
+	else
+		fscrypt_hash_inode_number(ci, mk);
 	return 0;
 }
 
@@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
 	kmem_cache_free(fscrypt_info_cachep, ci);
 }
 
-int fscrypt_get_encryption_info(struct inode *inode)
+static int
+fscrypt_setup_encryption_info(struct inode *inode,
+			      const union fscrypt_policy *policy,
+			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	struct fscrypt_info *crypt_info;
-	union fscrypt_context ctx;
 	struct fscrypt_mode *mode;
 	struct key *master_key = NULL;
 	int res;
 
-	if (fscrypt_has_encryption_key(inode))
-		return 0;
-
-	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
-	if (res)
-		return res;
-
-	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
-	if (res < 0) {
-		const union fscrypt_context *dummy_ctx =
-			fscrypt_get_dummy_context(inode->i_sb);
-
-		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
-			fscrypt_warn(inode,
-				     "Error %d getting encryption context",
-				     res);
-			return res;
-		}
-		/* Fake up a context for an unencrypted directory */
-		res = fscrypt_context_size(dummy_ctx);
-		memcpy(&ctx, dummy_ctx, res);
-	}
-
 	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
 	if (!crypt_info)
 		return -ENOMEM;
 
 	crypt_info->ci_inode = inode;
-
-	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
-	if (res) {
-		fscrypt_warn(inode,
-			     "Unrecognized or corrupt encryption context");
-		goto out;
-	}
-
-	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
-	       FSCRYPT_FILE_NONCE_SIZE);
-
-	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
-		res = -EINVAL;
-		goto out;
-	}
+	crypt_info->ci_policy = *policy;
+	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 
 	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
 	if (IS_ERR(mode)) {
@@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
 	put_crypt_info(crypt_info);
 	return res;
 }
+
+/**
+ * fscrypt_get_encryption_info() - set up an inode's encryption key
+ * @inode: the inode to set up the key for.  Must either be encrypted, or be an
+ *	   unencrypted directory with '-o test_dummy_encryption'.
+ *
+ * Create ->i_crypt_info, if it's not already set.
+ *
+ * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
+ * generally this shouldn't be called from within a filesystem transaction.
+ *
+ * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
+ *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
+ *	   distinguish these cases.)  Also can return another -errno code.
+ */
+int fscrypt_get_encryption_info(struct inode *inode)
+{
+	int res;
+	union fscrypt_context ctx;
+	union fscrypt_policy policy;
+
+	if (fscrypt_has_encryption_key(inode))
+		return 0;
+
+	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
+	if (res)
+		return res;
+
+	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
+	if (res < 0) {
+		const union fscrypt_context *dummy_ctx =
+			fscrypt_get_dummy_context(inode->i_sb);
+
+		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
+			fscrypt_warn(inode,
+				     "Error %d getting encryption context",
+				     res);
+			return res;
+		}
+		/* Fake up a context for an unencrypted directory */
+		res = fscrypt_context_size(dummy_ctx);
+		memcpy(&ctx, dummy_ctx, res);
+	}
+
+	res = fscrypt_policy_from_context(&policy, &ctx, res);
+	if (res) {
+		fscrypt_warn(inode,
+			     "Unrecognized or corrupt encryption context");
+		return res;
+	}
+
+	if (!fscrypt_supported_policy(&policy, inode))
+		return -EINVAL;
+
+	return fscrypt_setup_encryption_info(inode, &policy,
+					     fscrypt_context_nonce(&ctx));
+}
 EXPORT_SYMBOL(fscrypt_get_encryption_info);
 
+/**
+ * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
+ * @dir: a possibly-encrypted directory
+ * @inode: the inode that is being created.  ->i_mode must be set already.
+ *	   ->i_ino does *not* need to be set yet.
+ * @encrypt_ret: (output) set to true if the new inode will be encrypted.
+ *
+ * Prepares to create a new inode in a directory.  If either the inode or its
+ * filename will be encrypted, this sets up the directory's
+ * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
+ * its ->i_crypt_info and sets *encrypt_ret to true.
+ *
+ * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
+ * right away.  However, symlinks do need it.
+ *
+ * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
+ * any filesystem transaction to create the inode.  For this reason, ->i_ino
+ * isn't required to be set yet, as the filesystem may not have set it yet.
+ *
+ * This doesn't actually store the new inode's encryption context to disk.
+ * That still needs to be done later by calling fscrypt_set_context().
+ *
+ * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
+ *	   or another -errno code
+ */
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret)
+{
+	int err;
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
+
+	/*
+	 * If the filesystem is mounted with '-o test_dummy_encryption', files
+	 * created in unencrypted directories are automatically encrypted.  For
+	 * that case, we just need to treat the directory as encrypted here.
+	 */
+
+	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
+		return 0;
+
+	err = fscrypt_get_encryption_info(dir);
+	if (err)
+		return err;
+	if (!fscrypt_has_encryption_key(dir))
+		return -ENOKEY;
+
+	if (WARN_ON_ONCE(inode->i_mode == 0))
+		return -EINVAL;
+
+	/*
+	 * Only regular files, directories, and symlinks are encrypted.
+	 * Special files like device nodes and named pipes aren't.
+	 */
+	if (!S_ISREG(inode->i_mode) &&
+	    !S_ISDIR(inode->i_mode) &&
+	    !S_ISLNK(inode->i_mode))
+		return 0;
+
+	*encrypt_ret = true;
+
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+
+	return fscrypt_setup_encryption_info(inode,
+					     &dir->i_crypt_info->ci_policy,
+					     nonce);
+}
+EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
+
 /**
  * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
  * @inode: an inode being evicted
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index 2d73fd39ad96f..fbe4933206469 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  *				       an fscrypt_policy
  * @ctx_u: output context
  * @policy_u: input policy
+ * @nonce: nonce to use
  *
  * Create an fscrypt_context for an inode that is being assigned the given
- * encryption policy.  A new nonce is randomly generated.
+ * encryption policy.  @nonce must be a new random nonce.
  *
  * Return: the size of the new context in bytes.
  */
-static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
-					   const union fscrypt_policy *policy_u)
+static int
+fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
+				const union fscrypt_policy *policy_u,
+				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	memset(ctx_u, 0, sizeof(*ctx_u));
 
@@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_descriptor,
 		       policy->master_key_descriptor,
 		       sizeof(ctx->master_key_descriptor));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	case FSCRYPT_POLICY_V2: {
@@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_identifier,
 		       policy->master_key_identifier,
 		       sizeof(ctx->master_key_identifier));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	}
@@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
 static int set_encryption_policy(struct inode *inode,
 				 const union fscrypt_policy *policy)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	int err;
@@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
 		return -EINVAL;
 	}
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
 
 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
 }
@@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 						void *fs_data, bool preload)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	struct fscrypt_info *ci;
@@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 	if (ci == NULL)
 		return -ENOKEY;
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
 
 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
 	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
@@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 }
 EXPORT_SYMBOL(fscrypt_inherit_context);
 
+/**
+ * fscrypt_set_context() - Set the fscrypt context of a new inode
+ * @inode: A new inode
+ * @fs_data: private data given by FS and passed to ->set_context()
+ *
+ * This should be called after fscrypt_prepare_new_inode(), generally during a
+ * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
+ *
+ * Return: 0 on success, -errno on failure
+ */
+int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	struct fscrypt_info *ci = inode->i_crypt_info;
+	union fscrypt_context ctx;
+	int ctxsize;
+
+	/* fscrypt_prepare_new_inode() should have set up the key already. */
+	if (WARN_ON_ONCE(!ci))
+		return -ENOKEY;
+
+	/*
+	 * This may be the first time the inode number is available, so do any
+	 * delayed key setup that requires the inode number.
+	 */
+	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
+	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
+		const struct fscrypt_master_key *mk =
+			ci->ci_master_key->payload.data[0];
+
+		fscrypt_hash_inode_number(ci, mk);
+	}
+
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
+						  ci->ci_nonce);
+
+	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
+	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
+}
+EXPORT_SYMBOL_GPL(fscrypt_set_context);
+
 /**
  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
  * @sb: the filesystem on which test_dummy_encryption is being specified
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 991ff8575d0e7..726131dfa0a9b 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 			    void *fs_data, bool preload);
+int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
 	const union fscrypt_context *ctx;
@@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
 
 /* keysetup.c */
 int fscrypt_get_encryption_info(struct inode *inode);
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret);
 void fscrypt_put_encryption_info(struct inode *inode);
 void fscrypt_free_inode(struct inode *inode);
 int fscrypt_drop_inode(struct inode *inode);
@@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	return -EOPNOTSUPP;
+}
+
 struct fscrypt_dummy_context {
 };
 
@@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_prepare_new_inode(struct inode *dir,
+					    struct inode *inode,
+					    bool *encrypt_ret)
+{
+	if (IS_ENCRYPTED(dir))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
 static inline void fscrypt_put_encryption_info(struct inode *inode)
 {
 	return;
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() (setting up an inode's encryption key) is
intended to be GFP_NOFS safe.  But actually it isn't, since it uses
functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
when called with memalloc_nofs_save().  Therefore it can deadlock when
called from a context that needs GFP_NOFS, e.g. during an ext4
filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
Currently, this happens when creating a new encrypted file.

We can't fix this just by not setting up the key for new inodes right
away, since new symlinks need their key to encrypt the symlink target.

So we need to set up the new inode's key before starting the
transaction.  But fscrypt_get_encryption_info() can't do this currently,
since it assumes the encryption xattr is already set, and the encryption
xattr can't be set until the transaction.

The recently proposed fscrypt support for the ceph filesystem
(https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
will have this same ordering problem too, since when ceph creates a new
symlink, it will need to encrypt it before setting its encryption xattr.

To solve this problem, add new helper functions:

- fscrypt_prepare_new_inode() sets up a new inode's encryption key
  (fscrypt_info), using the parent directory's encryption policy and a
  new random nonce.  It neither reads nor writes the xattr.

- fscrypt_set_context() sets the encryption xattr from the fscrypt_info
  already in memory.  This replaces fscrypt_inherit_context().

Keep fscrypt_inherit_context() around temporarily until all filesystems
are converted to use fscrypt_set_context().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/fscrypt_private.h |   3 +
 fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
 fs/crypto/policy.c          |  61 ++++++++++--
 include/linux/fscrypt.h     |  17 ++++
 4 files changed, 220 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index 8117a61b6f558..355f6d9377517 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
 int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 			       const struct fscrypt_master_key *mk);
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk);
+
 /* keysetup_v1.c */
 
 void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index fea6226afc2b0..6ac816d3e8478 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -10,6 +10,7 @@
 
 #include <crypto/skcipher.h>
 #include <linux/key.h>
+#include <linux/random.h>
 
 #include "fscrypt_private.h"
 
@@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
 	return 0;
 }
 
+void fscrypt_hash_inode_number(struct fscrypt_info *ci,
+			       const struct fscrypt_master_key *mk)
+{
+	WARN_ON(ci->ci_inode->i_ino == 0);
+	WARN_ON(!mk->mk_ino_hash_key_initialized);
+
+	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
+					      &mk->mk_ino_hash_key);
+}
+
 static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 					    struct fscrypt_master_key *mk)
 {
@@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
 			return err;
 	}
 
-	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
-					      &mk->mk_ino_hash_key);
+	if (ci->ci_inode->i_ino == 0)
+		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
+	else
+		fscrypt_hash_inode_number(ci, mk);
 	return 0;
 }
 
@@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
 	kmem_cache_free(fscrypt_info_cachep, ci);
 }
 
-int fscrypt_get_encryption_info(struct inode *inode)
+static int
+fscrypt_setup_encryption_info(struct inode *inode,
+			      const union fscrypt_policy *policy,
+			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	struct fscrypt_info *crypt_info;
-	union fscrypt_context ctx;
 	struct fscrypt_mode *mode;
 	struct key *master_key = NULL;
 	int res;
 
-	if (fscrypt_has_encryption_key(inode))
-		return 0;
-
-	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
-	if (res)
-		return res;
-
-	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
-	if (res < 0) {
-		const union fscrypt_context *dummy_ctx =
-			fscrypt_get_dummy_context(inode->i_sb);
-
-		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
-			fscrypt_warn(inode,
-				     "Error %d getting encryption context",
-				     res);
-			return res;
-		}
-		/* Fake up a context for an unencrypted directory */
-		res = fscrypt_context_size(dummy_ctx);
-		memcpy(&ctx, dummy_ctx, res);
-	}
-
 	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
 	if (!crypt_info)
 		return -ENOMEM;
 
 	crypt_info->ci_inode = inode;
-
-	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
-	if (res) {
-		fscrypt_warn(inode,
-			     "Unrecognized or corrupt encryption context");
-		goto out;
-	}
-
-	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
-	       FSCRYPT_FILE_NONCE_SIZE);
-
-	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
-		res = -EINVAL;
-		goto out;
-	}
+	crypt_info->ci_policy = *policy;
+	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 
 	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
 	if (IS_ERR(mode)) {
@@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
 	put_crypt_info(crypt_info);
 	return res;
 }
+
+/**
+ * fscrypt_get_encryption_info() - set up an inode's encryption key
+ * @inode: the inode to set up the key for.  Must either be encrypted, or be an
+ *	   unencrypted directory with '-o test_dummy_encryption'.
+ *
+ * Create ->i_crypt_info, if it's not already set.
+ *
+ * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
+ * generally this shouldn't be called from within a filesystem transaction.
+ *
+ * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
+ *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
+ *	   distinguish these cases.)  Also can return another -errno code.
+ */
+int fscrypt_get_encryption_info(struct inode *inode)
+{
+	int res;
+	union fscrypt_context ctx;
+	union fscrypt_policy policy;
+
+	if (fscrypt_has_encryption_key(inode))
+		return 0;
+
+	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
+	if (res)
+		return res;
+
+	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
+	if (res < 0) {
+		const union fscrypt_context *dummy_ctx =
+			fscrypt_get_dummy_context(inode->i_sb);
+
+		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
+			fscrypt_warn(inode,
+				     "Error %d getting encryption context",
+				     res);
+			return res;
+		}
+		/* Fake up a context for an unencrypted directory */
+		res = fscrypt_context_size(dummy_ctx);
+		memcpy(&ctx, dummy_ctx, res);
+	}
+
+	res = fscrypt_policy_from_context(&policy, &ctx, res);
+	if (res) {
+		fscrypt_warn(inode,
+			     "Unrecognized or corrupt encryption context");
+		return res;
+	}
+
+	if (!fscrypt_supported_policy(&policy, inode))
+		return -EINVAL;
+
+	return fscrypt_setup_encryption_info(inode, &policy,
+					     fscrypt_context_nonce(&ctx));
+}
 EXPORT_SYMBOL(fscrypt_get_encryption_info);
 
+/**
+ * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
+ * @dir: a possibly-encrypted directory
+ * @inode: the inode that is being created.  ->i_mode must be set already.
+ *	   ->i_ino does *not* need to be set yet.
+ * @encrypt_ret: (output) set to true if the new inode will be encrypted.
+ *
+ * Prepares to create a new inode in a directory.  If either the inode or its
+ * filename will be encrypted, this sets up the directory's
+ * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
+ * its ->i_crypt_info and sets *encrypt_ret to true.
+ *
+ * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
+ * right away.  However, symlinks do need it.
+ *
+ * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
+ * any filesystem transaction to create the inode.  For this reason, ->i_ino
+ * isn't required to be set yet, as the filesystem may not have set it yet.
+ *
+ * This doesn't actually store the new inode's encryption context to disk.
+ * That still needs to be done later by calling fscrypt_set_context().
+ *
+ * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
+ *	   or another -errno code
+ */
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret)
+{
+	int err;
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
+
+	/*
+	 * If the filesystem is mounted with '-o test_dummy_encryption', files
+	 * created in unencrypted directories are automatically encrypted.  For
+	 * that case, we just need to treat the directory as encrypted here.
+	 */
+
+	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
+		return 0;
+
+	err = fscrypt_get_encryption_info(dir);
+	if (err)
+		return err;
+	if (!fscrypt_has_encryption_key(dir))
+		return -ENOKEY;
+
+	if (WARN_ON_ONCE(inode->i_mode == 0))
+		return -EINVAL;
+
+	/*
+	 * Only regular files, directories, and symlinks are encrypted.
+	 * Special files like device nodes and named pipes aren't.
+	 */
+	if (!S_ISREG(inode->i_mode) &&
+	    !S_ISDIR(inode->i_mode) &&
+	    !S_ISLNK(inode->i_mode))
+		return 0;
+
+	*encrypt_ret = true;
+
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+
+	return fscrypt_setup_encryption_info(inode,
+					     &dir->i_crypt_info->ci_policy,
+					     nonce);
+}
+EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
+
 /**
  * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
  * @inode: an inode being evicted
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index 2d73fd39ad96f..fbe4933206469 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
  *				       an fscrypt_policy
  * @ctx_u: output context
  * @policy_u: input policy
+ * @nonce: nonce to use
  *
  * Create an fscrypt_context for an inode that is being assigned the given
- * encryption policy.  A new nonce is randomly generated.
+ * encryption policy.  @nonce must be a new random nonce.
  *
  * Return: the size of the new context in bytes.
  */
-static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
-					   const union fscrypt_policy *policy_u)
+static int
+fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
+				const union fscrypt_policy *policy_u,
+				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
 {
 	memset(ctx_u, 0, sizeof(*ctx_u));
 
@@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_descriptor,
 		       policy->master_key_descriptor,
 		       sizeof(ctx->master_key_descriptor));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	case FSCRYPT_POLICY_V2: {
@@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
 		memcpy(ctx->master_key_identifier,
 		       policy->master_key_identifier,
 		       sizeof(ctx->master_key_identifier));
-		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
+		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
 		return sizeof(*ctx);
 	}
 	}
@@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
 static int set_encryption_policy(struct inode *inode,
 				 const union fscrypt_policy *policy)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	int err;
@@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
 		return -EINVAL;
 	}
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
 
 	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
 }
@@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 						void *fs_data, bool preload)
 {
+	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
 	union fscrypt_context ctx;
 	int ctxsize;
 	struct fscrypt_info *ci;
@@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 	if (ci == NULL)
 		return -ENOKEY;
 
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
+	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
 
 	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
 	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
@@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 }
 EXPORT_SYMBOL(fscrypt_inherit_context);
 
+/**
+ * fscrypt_set_context() - Set the fscrypt context of a new inode
+ * @inode: A new inode
+ * @fs_data: private data given by FS and passed to ->set_context()
+ *
+ * This should be called after fscrypt_prepare_new_inode(), generally during a
+ * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
+ *
+ * Return: 0 on success, -errno on failure
+ */
+int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	struct fscrypt_info *ci = inode->i_crypt_info;
+	union fscrypt_context ctx;
+	int ctxsize;
+
+	/* fscrypt_prepare_new_inode() should have set up the key already. */
+	if (WARN_ON_ONCE(!ci))
+		return -ENOKEY;
+
+	/*
+	 * This may be the first time the inode number is available, so do any
+	 * delayed key setup that requires the inode number.
+	 */
+	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
+	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
+		const struct fscrypt_master_key *mk =
+			ci->ci_master_key->payload.data[0];
+
+		fscrypt_hash_inode_number(ci, mk);
+	}
+
+	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
+						  ci->ci_nonce);
+
+	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
+	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
+}
+EXPORT_SYMBOL_GPL(fscrypt_set_context);
+
 /**
  * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
  * @sb: the filesystem on which test_dummy_encryption is being specified
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 991ff8575d0e7..726131dfa0a9b 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
 int fscrypt_inherit_context(struct inode *parent, struct inode *child,
 			    void *fs_data, bool preload);
+int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
 	const union fscrypt_context *ctx;
@@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
 
 /* keysetup.c */
 int fscrypt_get_encryption_info(struct inode *inode);
+int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
+			      bool *encrypt_ret);
 void fscrypt_put_encryption_info(struct inode *inode);
 void fscrypt_free_inode(struct inode *inode);
 int fscrypt_drop_inode(struct inode *inode);
@@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
+{
+	return -EOPNOTSUPP;
+}
+
 struct fscrypt_dummy_context {
 };
 
@@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_prepare_new_inode(struct inode *dir,
+					    struct inode *inode,
+					    bool *encrypt_ret)
+{
+	if (IS_ENCRYPTED(dir))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
 static inline void fscrypt_put_encryption_info(struct inode *inode)
 {
 	return;
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 2/8] ext4: factor out ext4_xattr_credits_for_new_inode()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

To compute a new inode's xattr credits, we need to know whether the
inode will be encrypted or not.  When we switch to use the new helper
function fscrypt_prepare_new_inode(), we won't find out whether the
inode will be encrypted until slightly later than is currently the case,
which requires moving the code block that computes the xattr credits.

To make this easier and reduce the length of __ext4_new_inode(), move
this code block into a new function ext4_xattr_credits_for_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 90 +++++++++++++++++++++++++++---------------------
 1 file changed, 51 insertions(+), 39 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index df25d38d65393..0cc576005a923 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -742,6 +742,53 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
 	return 1;
 }
 
+static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
+					    bool encrypt)
+{
+	struct super_block *sb = dir->i_sb;
+	int nblocks = 0;
+#ifdef CONFIG_EXT4_FS_POSIX_ACL
+	struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
+
+	if (IS_ERR(p))
+		return PTR_ERR(p);
+	if (p) {
+		int acl_size = p->a_count * sizeof(ext4_acl_entry);
+
+		nblocks += (S_ISDIR(mode) ? 2 : 1) *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, acl_size,
+						 true /* is_create */);
+		posix_acl_release(p);
+	}
+#endif
+
+#ifdef CONFIG_SECURITY
+	{
+		int num_security_xattrs = 1;
+
+#ifdef CONFIG_INTEGRITY
+		num_security_xattrs++;
+#endif
+		/*
+		 * We assume that security xattrs are never more than 1k.
+		 * In practice they are under 128 bytes.
+		 */
+		nblocks += num_security_xattrs *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, 1024,
+						 true /* is_create */);
+	}
+#endif
+	if (encrypt)
+		nblocks += __ext4_xattr_set_credits(sb,
+						    NULL /* inode */,
+						    NULL /* block_bh */,
+						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
+						    true /* is_create */);
+	return nblocks;
+}
+
 /*
  * There are two policies for allocating an inode.  If the new inode is
  * a directory, then a forward search is made for a block group with both
@@ -796,45 +843,10 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	}
 
 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-#ifdef CONFIG_EXT4_FS_POSIX_ACL
-		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
-
-		if (IS_ERR(p))
-			return ERR_CAST(p);
-		if (p) {
-			int acl_size = p->a_count * sizeof(ext4_acl_entry);
-
-			nblocks += (S_ISDIR(mode) ? 2 : 1) *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, acl_size,
-					true /* is_create */);
-			posix_acl_release(p);
-		}
-#endif
-
-#ifdef CONFIG_SECURITY
-		{
-			int num_security_xattrs = 1;
-
-#ifdef CONFIG_INTEGRITY
-			num_security_xattrs++;
-#endif
-			/*
-			 * We assume that security xattrs are never
-			 * more than 1k.  In practice they are under
-			 * 128 bytes.
-			 */
-			nblocks += num_security_xattrs *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, 1024,
-					true /* is_create */);
-		}
-#endif
-		if (encrypt)
-			nblocks += __ext4_xattr_set_credits(sb,
-					NULL /* inode */, NULL /* block_bh */,
-					FSCRYPT_SET_CONTEXT_MAX_SIZE,
-					true /* is_create */);
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0)
+			return ERR_PTR(ret2);
+		nblocks += ret2;
 	}
 
 	ngroups = ext4_get_groups_count(sb);
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 2/8] ext4: factor out ext4_xattr_credits_for_new_inode()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

To compute a new inode's xattr credits, we need to know whether the
inode will be encrypted or not.  When we switch to use the new helper
function fscrypt_prepare_new_inode(), we won't find out whether the
inode will be encrypted until slightly later than is currently the case,
which requires moving the code block that computes the xattr credits.

To make this easier and reduce the length of __ext4_new_inode(), move
this code block into a new function ext4_xattr_credits_for_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 90 +++++++++++++++++++++++++++---------------------
 1 file changed, 51 insertions(+), 39 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index df25d38d65393..0cc576005a923 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -742,6 +742,53 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
 	return 1;
 }
 
+static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
+					    bool encrypt)
+{
+	struct super_block *sb = dir->i_sb;
+	int nblocks = 0;
+#ifdef CONFIG_EXT4_FS_POSIX_ACL
+	struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
+
+	if (IS_ERR(p))
+		return PTR_ERR(p);
+	if (p) {
+		int acl_size = p->a_count * sizeof(ext4_acl_entry);
+
+		nblocks += (S_ISDIR(mode) ? 2 : 1) *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, acl_size,
+						 true /* is_create */);
+		posix_acl_release(p);
+	}
+#endif
+
+#ifdef CONFIG_SECURITY
+	{
+		int num_security_xattrs = 1;
+
+#ifdef CONFIG_INTEGRITY
+		num_security_xattrs++;
+#endif
+		/*
+		 * We assume that security xattrs are never more than 1k.
+		 * In practice they are under 128 bytes.
+		 */
+		nblocks += num_security_xattrs *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, 1024,
+						 true /* is_create */);
+	}
+#endif
+	if (encrypt)
+		nblocks += __ext4_xattr_set_credits(sb,
+						    NULL /* inode */,
+						    NULL /* block_bh */,
+						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
+						    true /* is_create */);
+	return nblocks;
+}
+
 /*
  * There are two policies for allocating an inode.  If the new inode is
  * a directory, then a forward search is made for a block group with both
@@ -796,45 +843,10 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	}
 
 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-#ifdef CONFIG_EXT4_FS_POSIX_ACL
-		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
-
-		if (IS_ERR(p))
-			return ERR_CAST(p);
-		if (p) {
-			int acl_size = p->a_count * sizeof(ext4_acl_entry);
-
-			nblocks += (S_ISDIR(mode) ? 2 : 1) *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, acl_size,
-					true /* is_create */);
-			posix_acl_release(p);
-		}
-#endif
-
-#ifdef CONFIG_SECURITY
-		{
-			int num_security_xattrs = 1;
-
-#ifdef CONFIG_INTEGRITY
-			num_security_xattrs++;
-#endif
-			/*
-			 * We assume that security xattrs are never
-			 * more than 1k.  In practice they are under
-			 * 128 bytes.
-			 */
-			nblocks += num_security_xattrs *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, 1024,
-					true /* is_create */);
-		}
-#endif
-		if (encrypt)
-			nblocks += __ext4_xattr_set_credits(sb,
-					NULL /* inode */, NULL /* block_bh */,
-					FSCRYPT_SET_CONTEXT_MAX_SIZE,
-					true /* is_create */);
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0)
+			return ERR_PTR(ret2);
+		nblocks += ret2;
 	}
 
 	ngroups = ext4_get_groups_count(sb);
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 2/8] ext4: factor out ext4_xattr_credits_for_new_inode()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

To compute a new inode's xattr credits, we need to know whether the
inode will be encrypted or not.  When we switch to use the new helper
function fscrypt_prepare_new_inode(), we won't find out whether the
inode will be encrypted until slightly later than is currently the case,
which requires moving the code block that computes the xattr credits.

To make this easier and reduce the length of __ext4_new_inode(), move
this code block into a new function ext4_xattr_credits_for_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 90 +++++++++++++++++++++++++++---------------------
 1 file changed, 51 insertions(+), 39 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index df25d38d65393..0cc576005a923 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -742,6 +742,53 @@ static int find_inode_bit(struct super_block *sb, ext4_group_t group,
 	return 1;
 }
 
+static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
+					    bool encrypt)
+{
+	struct super_block *sb = dir->i_sb;
+	int nblocks = 0;
+#ifdef CONFIG_EXT4_FS_POSIX_ACL
+	struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
+
+	if (IS_ERR(p))
+		return PTR_ERR(p);
+	if (p) {
+		int acl_size = p->a_count * sizeof(ext4_acl_entry);
+
+		nblocks += (S_ISDIR(mode) ? 2 : 1) *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, acl_size,
+						 true /* is_create */);
+		posix_acl_release(p);
+	}
+#endif
+
+#ifdef CONFIG_SECURITY
+	{
+		int num_security_xattrs = 1;
+
+#ifdef CONFIG_INTEGRITY
+		num_security_xattrs++;
+#endif
+		/*
+		 * We assume that security xattrs are never more than 1k.
+		 * In practice they are under 128 bytes.
+		 */
+		nblocks += num_security_xattrs *
+			__ext4_xattr_set_credits(sb, NULL /* inode */,
+						 NULL /* block_bh */, 1024,
+						 true /* is_create */);
+	}
+#endif
+	if (encrypt)
+		nblocks += __ext4_xattr_set_credits(sb,
+						    NULL /* inode */,
+						    NULL /* block_bh */,
+						    FSCRYPT_SET_CONTEXT_MAX_SIZE,
+						    true /* is_create */);
+	return nblocks;
+}
+
 /*
  * There are two policies for allocating an inode.  If the new inode is
  * a directory, then a forward search is made for a block group with both
@@ -796,45 +843,10 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	}
 
 	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-#ifdef CONFIG_EXT4_FS_POSIX_ACL
-		struct posix_acl *p = get_acl(dir, ACL_TYPE_DEFAULT);
-
-		if (IS_ERR(p))
-			return ERR_CAST(p);
-		if (p) {
-			int acl_size = p->a_count * sizeof(ext4_acl_entry);
-
-			nblocks += (S_ISDIR(mode) ? 2 : 1) *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, acl_size,
-					true /* is_create */);
-			posix_acl_release(p);
-		}
-#endif
-
-#ifdef CONFIG_SECURITY
-		{
-			int num_security_xattrs = 1;
-
-#ifdef CONFIG_INTEGRITY
-			num_security_xattrs++;
-#endif
-			/*
-			 * We assume that security xattrs are never
-			 * more than 1k.  In practice they are under
-			 * 128 bytes.
-			 */
-			nblocks += num_security_xattrs *
-				__ext4_xattr_set_credits(sb, NULL /* inode */,
-					NULL /* block_bh */, 1024,
-					true /* is_create */);
-		}
-#endif
-		if (encrypt)
-			nblocks += __ext4_xattr_set_credits(sb,
-					NULL /* inode */, NULL /* block_bh */,
-					FSCRYPT_SET_CONTEXT_MAX_SIZE,
-					true /* is_create */);
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0)
+			return ERR_PTR(ret2);
+		nblocks += ret2;
 	}
 
 	ngroups = ext4_get_groups_count(sb);
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 3/8] ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

We don't need #ifdefs for CONFIG_SECURITY and CONFIG_INTEGRITY;
IS_ENABLED() is sufficient.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 0cc576005a923..3e9c50eb857be 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -763,13 +763,12 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 	}
 #endif
 
-#ifdef CONFIG_SECURITY
-	{
+	if (IS_ENABLED(CONFIG_SECURITY)) {
 		int num_security_xattrs = 1;
 
-#ifdef CONFIG_INTEGRITY
-		num_security_xattrs++;
-#endif
+		if (IS_ENABLED(CONFIG_INTEGRITY))
+			num_security_xattrs++;
+
 		/*
 		 * We assume that security xattrs are never more than 1k.
 		 * In practice they are under 128 bytes.
@@ -779,7 +778,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 						 NULL /* block_bh */, 1024,
 						 true /* is_create */);
 	}
-#endif
+
 	if (encrypt)
 		nblocks += __ext4_xattr_set_credits(sb,
 						    NULL /* inode */,
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 3/8] ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

We don't need #ifdefs for CONFIG_SECURITY and CONFIG_INTEGRITY;
IS_ENABLED() is sufficient.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 0cc576005a923..3e9c50eb857be 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -763,13 +763,12 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 	}
 #endif
 
-#ifdef CONFIG_SECURITY
-	{
+	if (IS_ENABLED(CONFIG_SECURITY)) {
 		int num_security_xattrs = 1;
 
-#ifdef CONFIG_INTEGRITY
-		num_security_xattrs++;
-#endif
+		if (IS_ENABLED(CONFIG_INTEGRITY))
+			num_security_xattrs++;
+
 		/*
 		 * We assume that security xattrs are never more than 1k.
 		 * In practice they are under 128 bytes.
@@ -779,7 +778,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 						 NULL /* block_bh */, 1024,
 						 true /* is_create */);
 	}
-#endif
+
 	if (encrypt)
 		nblocks += __ext4_xattr_set_credits(sb,
 						    NULL /* inode */,
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 3/8] ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

We don't need #ifdefs for CONFIG_SECURITY and CONFIG_INTEGRITY;
IS_ENABLED() is sufficient.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 0cc576005a923..3e9c50eb857be 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -763,13 +763,12 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 	}
 #endif
 
-#ifdef CONFIG_SECURITY
-	{
+	if (IS_ENABLED(CONFIG_SECURITY)) {
 		int num_security_xattrs = 1;
 
-#ifdef CONFIG_INTEGRITY
-		num_security_xattrs++;
-#endif
+		if (IS_ENABLED(CONFIG_INTEGRITY))
+			num_security_xattrs++;
+
 		/*
 		 * We assume that security xattrs are never more than 1k.
 		 * In practice they are under 128 bytes.
@@ -779,7 +778,7 @@ static int ext4_xattr_credits_for_new_inode(struct inode *dir, mode_t mode,
 						 NULL /* block_bh */, 1024,
 						 true /* is_create */);
 	}
-#endif
+
 	if (encrypt)
 		nblocks += __ext4_xattr_set_credits(sb,
 						    NULL /* inode */,
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 4/8] ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

Convert ext4 to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from within a transaction, which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 3e9c50eb857be..495ceb010a99b 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -818,7 +818,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	ext4_group_t i;
 	ext4_group_t flex_group;
 	struct ext4_group_info *grp;
-	int encrypt = 0;
+	bool encrypt = false;
 
 	/* Cannot create files in a deleted directory */
 	if (!dir || !dir->i_nlink)
@@ -830,24 +830,6 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	if (unlikely(ext4_forced_shutdown(sbi)))
 		return ERR_PTR(-EIO);
 
-	if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
-	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
-	    !(i_flags & EXT4_EA_INODE_FL)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err)
-			return ERR_PTR(err);
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-ENOKEY);
-		encrypt = 1;
-	}
-
-	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
-		if (ret2 < 0)
-			return ERR_PTR(ret2);
-		nblocks += ret2;
-	}
-
 	ngroups = ext4_get_groups_count(sb);
 	trace_ext4_request_inode(dir, mode);
 	inode = new_inode(sb);
@@ -877,10 +859,25 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	else
 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
 
+	if (!(i_flags & EXT4_EA_INODE_FL)) {
+		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+		if (err)
+			goto out;
+	}
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto out;
 
+	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0) {
+			err = ret2;
+			goto out;
+		}
+		nblocks += ret2;
+	}
+
 	if (!goal)
 		goal = sbi->s_inode_goal;
 
@@ -1173,7 +1170,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	 * prevent its deduplication.
 	 */
 	if (encrypt) {
-		err = fscrypt_inherit_context(dir, inode, handle, true);
+		err = fscrypt_set_context(inode, handle);
 		if (err)
 			goto fail_free_drop;
 	}
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 4/8] ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert ext4 to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from within a transaction, which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 3e9c50eb857be..495ceb010a99b 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -818,7 +818,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	ext4_group_t i;
 	ext4_group_t flex_group;
 	struct ext4_group_info *grp;
-	int encrypt = 0;
+	bool encrypt = false;
 
 	/* Cannot create files in a deleted directory */
 	if (!dir || !dir->i_nlink)
@@ -830,24 +830,6 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	if (unlikely(ext4_forced_shutdown(sbi)))
 		return ERR_PTR(-EIO);
 
-	if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
-	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
-	    !(i_flags & EXT4_EA_INODE_FL)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err)
-			return ERR_PTR(err);
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-ENOKEY);
-		encrypt = 1;
-	}
-
-	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
-		if (ret2 < 0)
-			return ERR_PTR(ret2);
-		nblocks += ret2;
-	}
-
 	ngroups = ext4_get_groups_count(sb);
 	trace_ext4_request_inode(dir, mode);
 	inode = new_inode(sb);
@@ -877,10 +859,25 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	else
 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
 
+	if (!(i_flags & EXT4_EA_INODE_FL)) {
+		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+		if (err)
+			goto out;
+	}
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto out;
 
+	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0) {
+			err = ret2;
+			goto out;
+		}
+		nblocks += ret2;
+	}
+
 	if (!goal)
 		goal = sbi->s_inode_goal;
 
@@ -1173,7 +1170,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	 * prevent its deduplication.
 	 */
 	if (encrypt) {
-		err = fscrypt_inherit_context(dir, inode, handle, true);
+		err = fscrypt_set_context(inode, handle);
 		if (err)
 			goto fail_free_drop;
 	}
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 4/8] ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert ext4 to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from within a transaction, which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ext4/ialloc.c | 37 +++++++++++++++++--------------------
 1 file changed, 17 insertions(+), 20 deletions(-)

diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
index 3e9c50eb857be..495ceb010a99b 100644
--- a/fs/ext4/ialloc.c
+++ b/fs/ext4/ialloc.c
@@ -818,7 +818,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	ext4_group_t i;
 	ext4_group_t flex_group;
 	struct ext4_group_info *grp;
-	int encrypt = 0;
+	bool encrypt = false;
 
 	/* Cannot create files in a deleted directory */
 	if (!dir || !dir->i_nlink)
@@ -830,24 +830,6 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	if (unlikely(ext4_forced_shutdown(sbi)))
 		return ERR_PTR(-EIO);
 
-	if ((IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
-	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) &&
-	    !(i_flags & EXT4_EA_INODE_FL)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err)
-			return ERR_PTR(err);
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-ENOKEY);
-		encrypt = 1;
-	}
-
-	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
-		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
-		if (ret2 < 0)
-			return ERR_PTR(ret2);
-		nblocks += ret2;
-	}
-
 	ngroups = ext4_get_groups_count(sb);
 	trace_ext4_request_inode(dir, mode);
 	inode = new_inode(sb);
@@ -877,10 +859,25 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	else
 		ei->i_projid = make_kprojid(&init_user_ns, EXT4_DEF_PROJID);
 
+	if (!(i_flags & EXT4_EA_INODE_FL)) {
+		err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+		if (err)
+			goto out;
+	}
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto out;
 
+	if (!handle && sbi->s_journal && !(i_flags & EXT4_EA_INODE_FL)) {
+		ret2 = ext4_xattr_credits_for_new_inode(dir, mode, encrypt);
+		if (ret2 < 0) {
+			err = ret2;
+			goto out;
+		}
+		nblocks += ret2;
+	}
+
 	if (!goal)
 		goal = sbi->s_inode_goal;
 
@@ -1173,7 +1170,7 @@ struct inode *__ext4_new_inode(handle_t *handle, struct inode *dir,
 	 * prevent its deduplication.
 	 */
 	if (encrypt) {
-		err = fscrypt_inherit_context(dir, inode, handle, true);
+		err = fscrypt_set_context(inode, handle);
 		if (err)
 			goto fail_free_drop;
 	}
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 5/8] f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

Convert f2fs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from under f2fs_lock_op(), which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/f2fs/dir.c   |  2 +-
 fs/f2fs/f2fs.h  | 16 ----------------
 fs/f2fs/namei.c |  7 ++++++-
 3 files changed, 7 insertions(+), 18 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 069f498af1e38..d627ca97fc500 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -537,7 +537,7 @@ struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
 			goto put_error;
 
 		if (IS_ENCRYPTED(inode)) {
-			err = fscrypt_inherit_context(dir, inode, page, false);
+			err = fscrypt_set_context(inode, page);
 			if (err)
 				goto put_error;
 		}
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 16322ea5b4630..eb37d1974ba8e 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4022,22 +4022,6 @@ static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
 }
 
-static inline bool f2fs_may_encrypt(struct inode *dir, struct inode *inode)
-{
-#ifdef CONFIG_FS_ENCRYPTION
-	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
-	umode_t mode = inode->i_mode;
-
-	/*
-	 * If the directory encrypted or dummy encryption enabled,
-	 * then we should encrypt the inode.
-	 */
-	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi))
-		return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
-#endif
-	return false;
-}
-
 static inline bool f2fs_may_compress(struct inode *inode)
 {
 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 84e4bbc1a64de..45f324511a19e 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -28,6 +28,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 	nid_t ino;
 	struct inode *inode;
 	bool nid_free = false;
+	bool encrypt = false;
 	int xattr_size = 0;
 	int err;
 
@@ -69,13 +70,17 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
 							F2FS_DEF_PROJID);
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+	if (err)
+		goto fail_drop;
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto fail_drop;
 
 	set_inode_flag(inode, FI_NEW_INODE);
 
-	if (f2fs_may_encrypt(dir, inode))
+	if (encrypt)
 		f2fs_set_encrypted_inode(inode);
 
 	if (f2fs_sb_has_extra_attr(sbi)) {
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 5/8] f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert f2fs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from under f2fs_lock_op(), which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/f2fs/dir.c   |  2 +-
 fs/f2fs/f2fs.h  | 16 ----------------
 fs/f2fs/namei.c |  7 ++++++-
 3 files changed, 7 insertions(+), 18 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 069f498af1e38..d627ca97fc500 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -537,7 +537,7 @@ struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
 			goto put_error;
 
 		if (IS_ENCRYPTED(inode)) {
-			err = fscrypt_inherit_context(dir, inode, page, false);
+			err = fscrypt_set_context(inode, page);
 			if (err)
 				goto put_error;
 		}
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 16322ea5b4630..eb37d1974ba8e 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4022,22 +4022,6 @@ static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
 }
 
-static inline bool f2fs_may_encrypt(struct inode *dir, struct inode *inode)
-{
-#ifdef CONFIG_FS_ENCRYPTION
-	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
-	umode_t mode = inode->i_mode;
-
-	/*
-	 * If the directory encrypted or dummy encryption enabled,
-	 * then we should encrypt the inode.
-	 */
-	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi))
-		return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
-#endif
-	return false;
-}
-
 static inline bool f2fs_may_compress(struct inode *inode)
 {
 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 84e4bbc1a64de..45f324511a19e 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -28,6 +28,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 	nid_t ino;
 	struct inode *inode;
 	bool nid_free = false;
+	bool encrypt = false;
 	int xattr_size = 0;
 	int err;
 
@@ -69,13 +70,17 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
 							F2FS_DEF_PROJID);
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+	if (err)
+		goto fail_drop;
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto fail_drop;
 
 	set_inode_flag(inode, FI_NEW_INODE);
 
-	if (f2fs_may_encrypt(dir, inode))
+	if (encrypt)
 		f2fs_set_encrypted_inode(inode);
 
 	if (f2fs_sb_has_extra_attr(sbi)) {
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 5/8] f2fs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert f2fs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().  This avoids calling
fscrypt_get_encryption_info() from under f2fs_lock_op(), which can
deadlock because fscrypt_get_encryption_info() isn't GFP_NOFS-safe.

For more details about this problem, see the earlier patch
"fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()".

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/f2fs/dir.c   |  2 +-
 fs/f2fs/f2fs.h  | 16 ----------------
 fs/f2fs/namei.c |  7 ++++++-
 3 files changed, 7 insertions(+), 18 deletions(-)

diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index 069f498af1e38..d627ca97fc500 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -537,7 +537,7 @@ struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
 			goto put_error;
 
 		if (IS_ENCRYPTED(inode)) {
-			err = fscrypt_inherit_context(dir, inode, page, false);
+			err = fscrypt_set_context(inode, page);
 			if (err)
 				goto put_error;
 		}
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 16322ea5b4630..eb37d1974ba8e 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -4022,22 +4022,6 @@ static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
 }
 
-static inline bool f2fs_may_encrypt(struct inode *dir, struct inode *inode)
-{
-#ifdef CONFIG_FS_ENCRYPTION
-	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
-	umode_t mode = inode->i_mode;
-
-	/*
-	 * If the directory encrypted or dummy encryption enabled,
-	 * then we should encrypt the inode.
-	 */
-	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi))
-		return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
-#endif
-	return false;
-}
-
 static inline bool f2fs_may_compress(struct inode *inode)
 {
 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
index 84e4bbc1a64de..45f324511a19e 100644
--- a/fs/f2fs/namei.c
+++ b/fs/f2fs/namei.c
@@ -28,6 +28,7 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 	nid_t ino;
 	struct inode *inode;
 	bool nid_free = false;
+	bool encrypt = false;
 	int xattr_size = 0;
 	int err;
 
@@ -69,13 +70,17 @@ static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
 		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
 							F2FS_DEF_PROJID);
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypt);
+	if (err)
+		goto fail_drop;
+
 	err = dquot_initialize(inode);
 	if (err)
 		goto fail_drop;
 
 	set_inode_flag(inode, FI_NEW_INODE);
 
-	if (f2fs_may_encrypt(dir, inode))
+	if (encrypt)
 		f2fs_set_encrypted_inode(inode);
 
 	if (f2fs_sb_has_extra_attr(sbi)) {
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 6/8] ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

Convert ubifs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().

Unlike ext4 and f2fs, this doesn't appear to fix any deadlock bug.  But
it does shorten the code slightly and get all filesystems using the same
helper functions, so that fscrypt_inherit_context() can be removed.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ubifs/dir.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 9d042942d8b29..26739ae3ffee7 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -81,19 +81,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	struct ubifs_inode *ui;
 	bool encrypted = false;
 
-	if (IS_ENCRYPTED(dir)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err) {
-			ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
-			return ERR_PTR(err);
-		}
-
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-EPERM);
-
-		encrypted = true;
-	}
-
 	inode = new_inode(c->vfs_sb);
 	ui = ubifs_inode(inode);
 	if (!inode)
@@ -112,6 +99,14 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 			 current_time(inode);
 	inode->i_mapping->nrpages = 0;
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
+	if (err) {
+		ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
+		make_bad_inode(inode);
+		iput(inode);
+		return ERR_PTR(err);
+	}
+
 	switch (mode & S_IFMT) {
 	case S_IFREG:
 		inode->i_mapping->a_ops = &ubifs_file_address_operations;
@@ -131,7 +126,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	case S_IFBLK:
 	case S_IFCHR:
 		inode->i_op  = &ubifs_file_inode_operations;
-		encrypted = false;
 		break;
 	default:
 		BUG();
@@ -171,9 +165,9 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	spin_unlock(&c->cnt_lock);
 
 	if (encrypted) {
-		err = fscrypt_inherit_context(dir, inode, &encrypted, true);
+		err = fscrypt_set_context(inode, NULL);
 		if (err) {
-			ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
+			ubifs_err(c, "fscrypt_set_context failed: %i", err);
 			make_bad_inode(inode);
 			iput(inode);
 			return ERR_PTR(err);
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 6/8] ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert ubifs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().

Unlike ext4 and f2fs, this doesn't appear to fix any deadlock bug.  But
it does shorten the code slightly and get all filesystems using the same
helper functions, so that fscrypt_inherit_context() can be removed.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ubifs/dir.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 9d042942d8b29..26739ae3ffee7 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -81,19 +81,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	struct ubifs_inode *ui;
 	bool encrypted = false;
 
-	if (IS_ENCRYPTED(dir)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err) {
-			ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
-			return ERR_PTR(err);
-		}
-
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-EPERM);
-
-		encrypted = true;
-	}
-
 	inode = new_inode(c->vfs_sb);
 	ui = ubifs_inode(inode);
 	if (!inode)
@@ -112,6 +99,14 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 			 current_time(inode);
 	inode->i_mapping->nrpages = 0;
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
+	if (err) {
+		ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
+		make_bad_inode(inode);
+		iput(inode);
+		return ERR_PTR(err);
+	}
+
 	switch (mode & S_IFMT) {
 	case S_IFREG:
 		inode->i_mapping->a_ops = &ubifs_file_address_operations;
@@ -131,7 +126,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	case S_IFBLK:
 	case S_IFCHR:
 		inode->i_op  = &ubifs_file_inode_operations;
-		encrypted = false;
 		break;
 	default:
 		BUG();
@@ -171,9 +165,9 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	spin_unlock(&c->cnt_lock);
 
 	if (encrypted) {
-		err = fscrypt_inherit_context(dir, inode, &encrypted, true);
+		err = fscrypt_set_context(inode, NULL);
 		if (err) {
-			ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
+			ubifs_err(c, "fscrypt_set_context failed: %i", err);
 			make_bad_inode(inode);
 			iput(inode);
 			return ERR_PTR(err);
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 6/8] ubifs: use fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Convert ubifs to use the new functions fscrypt_prepare_new_inode() and
fscrypt_set_context().

Unlike ext4 and f2fs, this doesn't appear to fix any deadlock bug.  But
it does shorten the code slightly and get all filesystems using the same
helper functions, so that fscrypt_inherit_context() can be removed.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/ubifs/dir.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
index 9d042942d8b29..26739ae3ffee7 100644
--- a/fs/ubifs/dir.c
+++ b/fs/ubifs/dir.c
@@ -81,19 +81,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	struct ubifs_inode *ui;
 	bool encrypted = false;
 
-	if (IS_ENCRYPTED(dir)) {
-		err = fscrypt_get_encryption_info(dir);
-		if (err) {
-			ubifs_err(c, "fscrypt_get_encryption_info failed: %i", err);
-			return ERR_PTR(err);
-		}
-
-		if (!fscrypt_has_encryption_key(dir))
-			return ERR_PTR(-EPERM);
-
-		encrypted = true;
-	}
-
 	inode = new_inode(c->vfs_sb);
 	ui = ubifs_inode(inode);
 	if (!inode)
@@ -112,6 +99,14 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 			 current_time(inode);
 	inode->i_mapping->nrpages = 0;
 
+	err = fscrypt_prepare_new_inode(dir, inode, &encrypted);
+	if (err) {
+		ubifs_err(c, "fscrypt_prepare_new_inode failed: %i", err);
+		make_bad_inode(inode);
+		iput(inode);
+		return ERR_PTR(err);
+	}
+
 	switch (mode & S_IFMT) {
 	case S_IFREG:
 		inode->i_mapping->a_ops = &ubifs_file_address_operations;
@@ -131,7 +126,6 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	case S_IFBLK:
 	case S_IFCHR:
 		inode->i_op  = &ubifs_file_inode_operations;
-		encrypted = false;
 		break;
 	default:
 		BUG();
@@ -171,9 +165,9 @@ struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
 	spin_unlock(&c->cnt_lock);
 
 	if (encrypted) {
-		err = fscrypt_inherit_context(dir, inode, &encrypted, true);
+		err = fscrypt_set_context(inode, NULL);
 		if (err) {
-			ubifs_err(c, "fscrypt_inherit_context failed: %i", err);
+			ubifs_err(c, "fscrypt_set_context failed: %i", err);
 			make_bad_inode(inode);
 			iput(inode);
 			return ERR_PTR(err);
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 7/8] fscrypt: remove fscrypt_inherit_context()
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

Now that all filesystems have been converted to use
fscrypt_prepare_new_inode() and fscrypt_set_context(),
fscrypt_inherit_context() is no longer used.  So remove it.

Also change __fscrypt_encrypt_symlink() to no longer set up the inode's
key, since it's guaranteed to be set up already now that all filesystems
have been converted to fscrypt_prepare_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/hooks.c       | 10 +++++++---
 fs/crypto/policy.c      | 37 -------------------------------------
 include/linux/fscrypt.h |  9 ---------
 3 files changed, 7 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c
index 09fb8aa0f2e93..b69cd29a01a2f 100644
--- a/fs/crypto/hooks.c
+++ b/fs/crypto/hooks.c
@@ -217,9 +217,13 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
 	struct fscrypt_symlink_data *sd;
 	unsigned int ciphertext_len;
 
-	err = fscrypt_require_key(inode);
-	if (err)
-		return err;
+	/*
+	 * fscrypt_prepare_new_inode() should have already set up the inode's
+	 * encryption key.  We don't wait until now to do it, since we may be in
+	 * a filesystem transaction now.
+	 */
+	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
+		return -ENOKEY;
 
 	if (disk_link->name) {
 		/* filesystem-provided buffer */
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index fbe4933206469..2220ef48d5846 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -625,43 +625,6 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
 }
 EXPORT_SYMBOL(fscrypt_has_permitted_context);
 
-/**
- * fscrypt_inherit_context() - Sets a child context from its parent
- * @parent: Parent inode from which the context is inherited.
- * @child:  Child inode that inherits the context from @parent.
- * @fs_data:  private data given by FS.
- * @preload:  preload child i_crypt_info if true
- *
- * Return: 0 on success, -errno on failure
- */
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-						void *fs_data, bool preload)
-{
-	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
-	union fscrypt_context ctx;
-	int ctxsize;
-	struct fscrypt_info *ci;
-	int res;
-
-	res = fscrypt_get_encryption_info(parent);
-	if (res < 0)
-		return res;
-
-	ci = fscrypt_get_info(parent);
-	if (ci == NULL)
-		return -ENOKEY;
-
-	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
-
-	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
-	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
-	if (res)
-		return res;
-	return preload ? fscrypt_get_encryption_info(child): 0;
-}
-EXPORT_SYMBOL(fscrypt_inherit_context);
-
 /**
  * fscrypt_set_context() - Set the fscrypt context of a new inode
  * @inode: A new inode
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 726131dfa0a9b..4ee636e9e1fca 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -156,8 +156,6 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-			    void *fs_data, bool preload);
 int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
@@ -343,13 +341,6 @@ static inline int fscrypt_has_permitted_context(struct inode *parent,
 	return 0;
 }
 
-static inline int fscrypt_inherit_context(struct inode *parent,
-					  struct inode *child,
-					  void *fs_data, bool preload)
-{
-	return -EOPNOTSUPP;
-}
-
 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
 {
 	return -EOPNOTSUPP;
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 7/8] fscrypt: remove fscrypt_inherit_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Now that all filesystems have been converted to use
fscrypt_prepare_new_inode() and fscrypt_set_context(),
fscrypt_inherit_context() is no longer used.  So remove it.

Also change __fscrypt_encrypt_symlink() to no longer set up the inode's
key, since it's guaranteed to be set up already now that all filesystems
have been converted to fscrypt_prepare_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/hooks.c       | 10 +++++++---
 fs/crypto/policy.c      | 37 -------------------------------------
 include/linux/fscrypt.h |  9 ---------
 3 files changed, 7 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c
index 09fb8aa0f2e93..b69cd29a01a2f 100644
--- a/fs/crypto/hooks.c
+++ b/fs/crypto/hooks.c
@@ -217,9 +217,13 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
 	struct fscrypt_symlink_data *sd;
 	unsigned int ciphertext_len;
 
-	err = fscrypt_require_key(inode);
-	if (err)
-		return err;
+	/*
+	 * fscrypt_prepare_new_inode() should have already set up the inode's
+	 * encryption key.  We don't wait until now to do it, since we may be in
+	 * a filesystem transaction now.
+	 */
+	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
+		return -ENOKEY;
 
 	if (disk_link->name) {
 		/* filesystem-provided buffer */
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index fbe4933206469..2220ef48d5846 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -625,43 +625,6 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
 }
 EXPORT_SYMBOL(fscrypt_has_permitted_context);
 
-/**
- * fscrypt_inherit_context() - Sets a child context from its parent
- * @parent: Parent inode from which the context is inherited.
- * @child:  Child inode that inherits the context from @parent.
- * @fs_data:  private data given by FS.
- * @preload:  preload child i_crypt_info if true
- *
- * Return: 0 on success, -errno on failure
- */
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-						void *fs_data, bool preload)
-{
-	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
-	union fscrypt_context ctx;
-	int ctxsize;
-	struct fscrypt_info *ci;
-	int res;
-
-	res = fscrypt_get_encryption_info(parent);
-	if (res < 0)
-		return res;
-
-	ci = fscrypt_get_info(parent);
-	if (ci == NULL)
-		return -ENOKEY;
-
-	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
-
-	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
-	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
-	if (res)
-		return res;
-	return preload ? fscrypt_get_encryption_info(child): 0;
-}
-EXPORT_SYMBOL(fscrypt_inherit_context);
-
 /**
  * fscrypt_set_context() - Set the fscrypt context of a new inode
  * @inode: A new inode
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 726131dfa0a9b..4ee636e9e1fca 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -156,8 +156,6 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-			    void *fs_data, bool preload);
 int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
@@ -343,13 +341,6 @@ static inline int fscrypt_has_permitted_context(struct inode *parent,
 	return 0;
 }
 
-static inline int fscrypt_inherit_context(struct inode *parent,
-					  struct inode *child,
-					  void *fs_data, bool preload)
-{
-	return -EOPNOTSUPP;
-}
-
 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
 {
 	return -EOPNOTSUPP;
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 7/8] fscrypt: remove fscrypt_inherit_context()
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

Now that all filesystems have been converted to use
fscrypt_prepare_new_inode() and fscrypt_set_context(),
fscrypt_inherit_context() is no longer used.  So remove it.

Also change __fscrypt_encrypt_symlink() to no longer set up the inode's
key, since it's guaranteed to be set up already now that all filesystems
have been converted to fscrypt_prepare_new_inode().

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/hooks.c       | 10 +++++++---
 fs/crypto/policy.c      | 37 -------------------------------------
 include/linux/fscrypt.h |  9 ---------
 3 files changed, 7 insertions(+), 49 deletions(-)

diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c
index 09fb8aa0f2e93..b69cd29a01a2f 100644
--- a/fs/crypto/hooks.c
+++ b/fs/crypto/hooks.c
@@ -217,9 +217,13 @@ int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
 	struct fscrypt_symlink_data *sd;
 	unsigned int ciphertext_len;
 
-	err = fscrypt_require_key(inode);
-	if (err)
-		return err;
+	/*
+	 * fscrypt_prepare_new_inode() should have already set up the inode's
+	 * encryption key.  We don't wait until now to do it, since we may be in
+	 * a filesystem transaction now.
+	 */
+	if (WARN_ON_ONCE(!fscrypt_has_encryption_key(inode)))
+		return -ENOKEY;
 
 	if (disk_link->name) {
 		/* filesystem-provided buffer */
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
index fbe4933206469..2220ef48d5846 100644
--- a/fs/crypto/policy.c
+++ b/fs/crypto/policy.c
@@ -625,43 +625,6 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child)
 }
 EXPORT_SYMBOL(fscrypt_has_permitted_context);
 
-/**
- * fscrypt_inherit_context() - Sets a child context from its parent
- * @parent: Parent inode from which the context is inherited.
- * @child:  Child inode that inherits the context from @parent.
- * @fs_data:  private data given by FS.
- * @preload:  preload child i_crypt_info if true
- *
- * Return: 0 on success, -errno on failure
- */
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-						void *fs_data, bool preload)
-{
-	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
-	union fscrypt_context ctx;
-	int ctxsize;
-	struct fscrypt_info *ci;
-	int res;
-
-	res = fscrypt_get_encryption_info(parent);
-	if (res < 0)
-		return res;
-
-	ci = fscrypt_get_info(parent);
-	if (ci == NULL)
-		return -ENOKEY;
-
-	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
-	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
-
-	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
-	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
-	if (res)
-		return res;
-	return preload ? fscrypt_get_encryption_info(child): 0;
-}
-EXPORT_SYMBOL(fscrypt_inherit_context);
-
 /**
  * fscrypt_set_context() - Set the fscrypt context of a new inode
  * @inode: A new inode
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
index 726131dfa0a9b..4ee636e9e1fca 100644
--- a/include/linux/fscrypt.h
+++ b/include/linux/fscrypt.h
@@ -156,8 +156,6 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
-int fscrypt_inherit_context(struct inode *parent, struct inode *child,
-			    void *fs_data, bool preload);
 int fscrypt_set_context(struct inode *inode, void *fs_data);
 
 struct fscrypt_dummy_context {
@@ -343,13 +341,6 @@ static inline int fscrypt_has_permitted_context(struct inode *parent,
 	return 0;
 }
 
-static inline int fscrypt_inherit_context(struct inode *parent,
-					  struct inode *child,
-					  void *fs_data, bool preload)
-{
-	return -EOPNOTSUPP;
-}
-
 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
 {
 	return -EOPNOTSUPP;
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 8/8] fscrypt: stop pretending that key setup is nofs-safe
  2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24  6:17   ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel, Jeff Layton

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() has never actually been safe to call in a
context that needs GFP_NOFS, since it calls crypto_alloc_skcipher().

crypto_alloc_skcipher() isn't GFP_NOFS-safe, even if called under
memalloc_nofs_save().  This is because it may load kernel modules, and
also because it internally takes crypto_alg_sem.  Other tasks can do
GFP_KERNEL allocations while holding crypto_alg_sem for write.

The use of fscrypt_init_mutex isn't GFP_NOFS-safe either.

So, stop pretending that fscrypt_get_encryption_info() is nofs-safe.
I.e., when it allocates memory, just use GFP_KERNEL instead of GFP_NOFS.

Note, another reason to do this is that GFP_NOFS is deprecated in favor
of using memalloc_nofs_save() in the proper places.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/inline_crypt.c | 7 ++-----
 fs/crypto/keysetup.c     | 2 +-
 fs/crypto/keysetup_v1.c  | 8 ++++----
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c
index faa25541ccb68..89bffa82ed74a 100644
--- a/fs/crypto/inline_crypt.c
+++ b/fs/crypto/inline_crypt.c
@@ -106,7 +106,7 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
 	crypto_cfg.data_unit_size = sb->s_blocksize;
 	crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
 	num_devs = fscrypt_get_num_devices(sb);
-	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_NOFS);
+	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL);
 	if (!devs)
 		return -ENOMEM;
 	fscrypt_get_devices(sb, num_devs, devs);
@@ -135,9 +135,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 	struct fscrypt_blk_crypto_key *blk_key;
 	int err;
 	int i;
-	unsigned int flags;
 
-	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_NOFS);
+	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL);
 	if (!blk_key)
 		return -ENOMEM;
 
@@ -166,10 +165,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 		}
 		queue_refs++;
 
-		flags = memalloc_nofs_save();
 		err = blk_crypto_start_using_key(&blk_key->base,
 						 blk_key->devs[i]);
-		memalloc_nofs_restore(flags);
 		if (err) {
 			fscrypt_err(inode,
 				    "error %d starting to use blk-crypto", err);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index 6ac816d3e8478..ad64525ec6800 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -477,7 +477,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
 	struct key *master_key = NULL;
 	int res;
 
-	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
+	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL);
 	if (!crypt_info)
 		return -ENOMEM;
 
diff --git a/fs/crypto/keysetup_v1.c b/fs/crypto/keysetup_v1.c
index a3cb52572b05c..2762c53504323 100644
--- a/fs/crypto/keysetup_v1.c
+++ b/fs/crypto/keysetup_v1.c
@@ -60,7 +60,7 @@ static int derive_key_aes(const u8 *master_key,
 		goto out;
 	}
 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
-	req = skcipher_request_alloc(tfm, GFP_NOFS);
+	req = skcipher_request_alloc(tfm, GFP_KERNEL);
 	if (!req) {
 		res = -ENOMEM;
 		goto out;
@@ -99,7 +99,7 @@ find_and_lock_process_key(const char *prefix,
 	const struct user_key_payload *ukp;
 	const struct fscrypt_key *payload;
 
-	description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
+	description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
 				FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
 	if (!description)
 		return ERR_PTR(-ENOMEM);
@@ -228,7 +228,7 @@ fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
 		return dk;
 
 	/* Nope, allocate one. */
-	dk = kzalloc(sizeof(*dk), GFP_NOFS);
+	dk = kzalloc(sizeof(*dk), GFP_KERNEL);
 	if (!dk)
 		return ERR_PTR(-ENOMEM);
 	refcount_set(&dk->dk_refcount, 1);
@@ -272,7 +272,7 @@ static int setup_v1_file_key_derived(struct fscrypt_info *ci,
 	 * This cannot be a stack buffer because it will be passed to the
 	 * scatterlist crypto API during derive_key_aes().
 	 */
-	derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
+	derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
 	if (!derived_key)
 		return -ENOMEM;
 
-- 
2.28.0


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [f2fs-dev] [RFC PATCH 8/8] fscrypt: stop pretending that key setup is nofs-safe
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() has never actually been safe to call in a
context that needs GFP_NOFS, since it calls crypto_alloc_skcipher().

crypto_alloc_skcipher() isn't GFP_NOFS-safe, even if called under
memalloc_nofs_save().  This is because it may load kernel modules, and
also because it internally takes crypto_alg_sem.  Other tasks can do
GFP_KERNEL allocations while holding crypto_alg_sem for write.

The use of fscrypt_init_mutex isn't GFP_NOFS-safe either.

So, stop pretending that fscrypt_get_encryption_info() is nofs-safe.
I.e., when it allocates memory, just use GFP_KERNEL instead of GFP_NOFS.

Note, another reason to do this is that GFP_NOFS is deprecated in favor
of using memalloc_nofs_save() in the proper places.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/inline_crypt.c | 7 ++-----
 fs/crypto/keysetup.c     | 2 +-
 fs/crypto/keysetup_v1.c  | 8 ++++----
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c
index faa25541ccb68..89bffa82ed74a 100644
--- a/fs/crypto/inline_crypt.c
+++ b/fs/crypto/inline_crypt.c
@@ -106,7 +106,7 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
 	crypto_cfg.data_unit_size = sb->s_blocksize;
 	crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
 	num_devs = fscrypt_get_num_devices(sb);
-	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_NOFS);
+	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL);
 	if (!devs)
 		return -ENOMEM;
 	fscrypt_get_devices(sb, num_devs, devs);
@@ -135,9 +135,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 	struct fscrypt_blk_crypto_key *blk_key;
 	int err;
 	int i;
-	unsigned int flags;
 
-	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_NOFS);
+	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL);
 	if (!blk_key)
 		return -ENOMEM;
 
@@ -166,10 +165,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 		}
 		queue_refs++;
 
-		flags = memalloc_nofs_save();
 		err = blk_crypto_start_using_key(&blk_key->base,
 						 blk_key->devs[i]);
-		memalloc_nofs_restore(flags);
 		if (err) {
 			fscrypt_err(inode,
 				    "error %d starting to use blk-crypto", err);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index 6ac816d3e8478..ad64525ec6800 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -477,7 +477,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
 	struct key *master_key = NULL;
 	int res;
 
-	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
+	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL);
 	if (!crypt_info)
 		return -ENOMEM;
 
diff --git a/fs/crypto/keysetup_v1.c b/fs/crypto/keysetup_v1.c
index a3cb52572b05c..2762c53504323 100644
--- a/fs/crypto/keysetup_v1.c
+++ b/fs/crypto/keysetup_v1.c
@@ -60,7 +60,7 @@ static int derive_key_aes(const u8 *master_key,
 		goto out;
 	}
 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
-	req = skcipher_request_alloc(tfm, GFP_NOFS);
+	req = skcipher_request_alloc(tfm, GFP_KERNEL);
 	if (!req) {
 		res = -ENOMEM;
 		goto out;
@@ -99,7 +99,7 @@ find_and_lock_process_key(const char *prefix,
 	const struct user_key_payload *ukp;
 	const struct fscrypt_key *payload;
 
-	description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
+	description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
 				FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
 	if (!description)
 		return ERR_PTR(-ENOMEM);
@@ -228,7 +228,7 @@ fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
 		return dk;
 
 	/* Nope, allocate one. */
-	dk = kzalloc(sizeof(*dk), GFP_NOFS);
+	dk = kzalloc(sizeof(*dk), GFP_KERNEL);
 	if (!dk)
 		return ERR_PTR(-ENOMEM);
 	refcount_set(&dk->dk_refcount, 1);
@@ -272,7 +272,7 @@ static int setup_v1_file_key_derived(struct fscrypt_info *ci,
 	 * This cannot be a stack buffer because it will be passed to the
 	 * scatterlist crypto API during derive_key_aes().
 	 */
-	derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
+	derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
 	if (!derived_key)
 		return -ENOMEM;
 
-- 
2.28.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* [RFC PATCH 8/8] fscrypt: stop pretending that key setup is nofs-safe
@ 2020-08-24  6:17   ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24  6:17 UTC (permalink / raw)
  To: linux-fscrypt
  Cc: Jeff Layton, ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

From: Eric Biggers <ebiggers@google.com>

fscrypt_get_encryption_info() has never actually been safe to call in a
context that needs GFP_NOFS, since it calls crypto_alloc_skcipher().

crypto_alloc_skcipher() isn't GFP_NOFS-safe, even if called under
memalloc_nofs_save().  This is because it may load kernel modules, and
also because it internally takes crypto_alg_sem.  Other tasks can do
GFP_KERNEL allocations while holding crypto_alg_sem for write.

The use of fscrypt_init_mutex isn't GFP_NOFS-safe either.

So, stop pretending that fscrypt_get_encryption_info() is nofs-safe.
I.e., when it allocates memory, just use GFP_KERNEL instead of GFP_NOFS.

Note, another reason to do this is that GFP_NOFS is deprecated in favor
of using memalloc_nofs_save() in the proper places.

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/crypto/inline_crypt.c | 7 ++-----
 fs/crypto/keysetup.c     | 2 +-
 fs/crypto/keysetup_v1.c  | 8 ++++----
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/fs/crypto/inline_crypt.c b/fs/crypto/inline_crypt.c
index faa25541ccb68..89bffa82ed74a 100644
--- a/fs/crypto/inline_crypt.c
+++ b/fs/crypto/inline_crypt.c
@@ -106,7 +106,7 @@ int fscrypt_select_encryption_impl(struct fscrypt_info *ci)
 	crypto_cfg.data_unit_size = sb->s_blocksize;
 	crypto_cfg.dun_bytes = fscrypt_get_dun_bytes(ci);
 	num_devs = fscrypt_get_num_devices(sb);
-	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_NOFS);
+	devs = kmalloc_array(num_devs, sizeof(*devs), GFP_KERNEL);
 	if (!devs)
 		return -ENOMEM;
 	fscrypt_get_devices(sb, num_devs, devs);
@@ -135,9 +135,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 	struct fscrypt_blk_crypto_key *blk_key;
 	int err;
 	int i;
-	unsigned int flags;
 
-	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_NOFS);
+	blk_key = kzalloc(struct_size(blk_key, devs, num_devs), GFP_KERNEL);
 	if (!blk_key)
 		return -ENOMEM;
 
@@ -166,10 +165,8 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
 		}
 		queue_refs++;
 
-		flags = memalloc_nofs_save();
 		err = blk_crypto_start_using_key(&blk_key->base,
 						 blk_key->devs[i]);
-		memalloc_nofs_restore(flags);
 		if (err) {
 			fscrypt_err(inode,
 				    "error %d starting to use blk-crypto", err);
diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
index 6ac816d3e8478..ad64525ec6800 100644
--- a/fs/crypto/keysetup.c
+++ b/fs/crypto/keysetup.c
@@ -477,7 +477,7 @@ fscrypt_setup_encryption_info(struct inode *inode,
 	struct key *master_key = NULL;
 	int res;
 
-	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
+	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_KERNEL);
 	if (!crypt_info)
 		return -ENOMEM;
 
diff --git a/fs/crypto/keysetup_v1.c b/fs/crypto/keysetup_v1.c
index a3cb52572b05c..2762c53504323 100644
--- a/fs/crypto/keysetup_v1.c
+++ b/fs/crypto/keysetup_v1.c
@@ -60,7 +60,7 @@ static int derive_key_aes(const u8 *master_key,
 		goto out;
 	}
 	crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
-	req = skcipher_request_alloc(tfm, GFP_NOFS);
+	req = skcipher_request_alloc(tfm, GFP_KERNEL);
 	if (!req) {
 		res = -ENOMEM;
 		goto out;
@@ -99,7 +99,7 @@ find_and_lock_process_key(const char *prefix,
 	const struct user_key_payload *ukp;
 	const struct fscrypt_key *payload;
 
-	description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
+	description = kasprintf(GFP_KERNEL, "%s%*phN", prefix,
 				FSCRYPT_KEY_DESCRIPTOR_SIZE, descriptor);
 	if (!description)
 		return ERR_PTR(-ENOMEM);
@@ -228,7 +228,7 @@ fscrypt_get_direct_key(const struct fscrypt_info *ci, const u8 *raw_key)
 		return dk;
 
 	/* Nope, allocate one. */
-	dk = kzalloc(sizeof(*dk), GFP_NOFS);
+	dk = kzalloc(sizeof(*dk), GFP_KERNEL);
 	if (!dk)
 		return ERR_PTR(-ENOMEM);
 	refcount_set(&dk->dk_refcount, 1);
@@ -272,7 +272,7 @@ static int setup_v1_file_key_derived(struct fscrypt_info *ci,
 	 * This cannot be a stack buffer because it will be passed to the
 	 * scatterlist crypto API during derive_key_aes().
 	 */
-	derived_key = kmalloc(ci->ci_mode->keysize, GFP_NOFS);
+	derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL);
 	if (!derived_key)
 		return -ENOMEM;
 
-- 
2.28.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24 16:48     ` Jeff Layton
  -1 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 16:48 UTC (permalink / raw)
  To: Eric Biggers, linux-fscrypt
  Cc: linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Sun, 2020-08-23 at 23:17 -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> fscrypt_get_encryption_info() (setting up an inode's encryption key) is
> intended to be GFP_NOFS safe.  But actually it isn't, since it uses
> functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
> when called with memalloc_nofs_save().  Therefore it can deadlock when
> called from a context that needs GFP_NOFS, e.g. during an ext4
> filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
> Currently, this happens when creating a new encrypted file.
> 
> We can't fix this just by not setting up the key for new inodes right
> away, since new symlinks need their key to encrypt the symlink target.
> 
> So we need to set up the new inode's key before starting the
> transaction.  But fscrypt_get_encryption_info() can't do this currently,
> since it assumes the encryption xattr is already set, and the encryption
> xattr can't be set until the transaction.
> 
> The recently proposed fscrypt support for the ceph filesystem
> (https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
> will have this same ordering problem too, since when ceph creates a new
> symlink, it will need to encrypt it before setting its encryption xattr.
> 
> To solve this problem, add new helper functions:
> 
> - fscrypt_prepare_new_inode() sets up a new inode's encryption key
>   (fscrypt_info), using the parent directory's encryption policy and a
>   new random nonce.  It neither reads nor writes the xattr.
> 
> - fscrypt_set_context() sets the encryption xattr from the fscrypt_info
>   already in memory.  This replaces fscrypt_inherit_context().
> 
> Keep fscrypt_inherit_context() around temporarily until all filesystems
> are converted to use fscrypt_set_context().
> 
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  fs/crypto/fscrypt_private.h |   3 +
>  fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
>  fs/crypto/policy.c          |  61 ++++++++++--
>  include/linux/fscrypt.h     |  17 ++++
>  4 files changed, 220 insertions(+), 49 deletions(-)
> 
> diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
> index 8117a61b6f558..355f6d9377517 100644
> --- a/fs/crypto/fscrypt_private.h
> +++ b/fs/crypto/fscrypt_private.h
> @@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
>  int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  			       const struct fscrypt_master_key *mk);
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk);
> +
>  /* keysetup_v1.c */
>  
>  void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
> diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
> index fea6226afc2b0..6ac816d3e8478 100644
> --- a/fs/crypto/keysetup.c
> +++ b/fs/crypto/keysetup.c
> @@ -10,6 +10,7 @@
>  
>  #include <crypto/skcipher.h>
>  #include <linux/key.h>
> +#include <linux/random.h>
>  
>  #include "fscrypt_private.h"
>  
> @@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  	return 0;
>  }
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk)
> +{
> +	WARN_ON(ci->ci_inode->i_ino == 0);
> +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> +
> +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> +					      &mk->mk_ino_hash_key);

i_ino is an unsigned long. Will this produce a consistent results on
arches with 32 and 64 bit long values? I think it'd be nice to ensure
that we can access an encrypted directory created on a 32-bit host from
(e.g.) a 64-bit host.

It may be better to base this on something besides i_ino or provide an
alternate way of fetching a full 64-bit inode number here when i_ino is
32-bits.


> +}
> +
>  static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  					    struct fscrypt_master_key *mk)
>  {
> @@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  			return err;
>  	}
>  
> -	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> -					      &mk->mk_ino_hash_key);
> +	if (ci->ci_inode->i_ino == 0)
> +		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
> +	else
> +		fscrypt_hash_inode_number(ci, mk);
>  	return 0;
>  }
>  
> @@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
>  	kmem_cache_free(fscrypt_info_cachep, ci);
>  }
>  
> -int fscrypt_get_encryption_info(struct inode *inode)
> +static int
> +fscrypt_setup_encryption_info(struct inode *inode,
> +			      const union fscrypt_policy *policy,
> +			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	struct fscrypt_info *crypt_info;
> -	union fscrypt_context ctx;
>  	struct fscrypt_mode *mode;
>  	struct key *master_key = NULL;
>  	int res;
>  
> -	if (fscrypt_has_encryption_key(inode))
> -		return 0;
> -
> -	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> -	if (res)
> -		return res;
> -
> -	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> -	if (res < 0) {
> -		const union fscrypt_context *dummy_ctx =
> -			fscrypt_get_dummy_context(inode->i_sb);
> -
> -		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> -			fscrypt_warn(inode,
> -				     "Error %d getting encryption context",
> -				     res);
> -			return res;
> -		}
> -		/* Fake up a context for an unencrypted directory */
> -		res = fscrypt_context_size(dummy_ctx);
> -		memcpy(&ctx, dummy_ctx, res);
> -	}
> -
>  	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
>  	if (!crypt_info)
>  		return -ENOMEM;
>  
>  	crypt_info->ci_inode = inode;
> -
> -	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
> -	if (res) {
> -		fscrypt_warn(inode,
> -			     "Unrecognized or corrupt encryption context");
> -		goto out;
> -	}
> -
> -	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
> -	       FSCRYPT_FILE_NONCE_SIZE);
> -
> -	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
> -		res = -EINVAL;
> -		goto out;
> -	}
> +	crypt_info->ci_policy = *policy;
> +	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  
>  	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
>  	if (IS_ERR(mode)) {
> @@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
>  	put_crypt_info(crypt_info);
>  	return res;
>  }
> +
> +/**
> + * fscrypt_get_encryption_info() - set up an inode's encryption key
> + * @inode: the inode to set up the key for.  Must either be encrypted, or be an
> + *	   unencrypted directory with '-o test_dummy_encryption'.
> + *
> + * Create ->i_crypt_info, if it's not already set.
> + *
> + * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
> + * generally this shouldn't be called from within a filesystem transaction.
> + *
> + * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
> + *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
> + *	   distinguish these cases.)  Also can return another -errno code.
> + */
> +int fscrypt_get_encryption_info(struct inode *inode)
> +{
> +	int res;
> +	union fscrypt_context ctx;
> +	union fscrypt_policy policy;
> +
> +	if (fscrypt_has_encryption_key(inode))
> +		return 0;
> +
> +	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> +	if (res)
> +		return res;
> +
> +	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> +	if (res < 0) {
> +		const union fscrypt_context *dummy_ctx =
> +			fscrypt_get_dummy_context(inode->i_sb);
> +
> +		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> +			fscrypt_warn(inode,
> +				     "Error %d getting encryption context",
> +				     res);
> +			return res;
> +		}
> +		/* Fake up a context for an unencrypted directory */
> +		res = fscrypt_context_size(dummy_ctx);
> +		memcpy(&ctx, dummy_ctx, res);
> +	}
> +
> +	res = fscrypt_policy_from_context(&policy, &ctx, res);
> +	if (res) {
> +		fscrypt_warn(inode,
> +			     "Unrecognized or corrupt encryption context");
> +		return res;
> +	}
> +
> +	if (!fscrypt_supported_policy(&policy, inode))
> +		return -EINVAL;
> +
> +	return fscrypt_setup_encryption_info(inode, &policy,
> +					     fscrypt_context_nonce(&ctx));
> +}
>  EXPORT_SYMBOL(fscrypt_get_encryption_info);
>  
> +/**
> + * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
> + * @dir: a possibly-encrypted directory
> + * @inode: the inode that is being created.  ->i_mode must be set already.
> + *	   ->i_ino does *not* need to be set yet.
> + * @encrypt_ret: (output) set to true if the new inode will be encrypted.
> + *
> + * Prepares to create a new inode in a directory.  If either the inode or its
> + * filename will be encrypted, this sets up the directory's
> + * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
> + * its ->i_crypt_info and sets *encrypt_ret to true.
> + *
> + * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
> + * right away.  However, symlinks do need it.
> + *
> + * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
> + * any filesystem transaction to create the inode.  For this reason, ->i_ino
> + * isn't required to be set yet, as the filesystem may not have set it yet.
> + *
> + * This doesn't actually store the new inode's encryption context to disk.
> + * That still needs to be done later by calling fscrypt_set_context().
> + *
> + * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
> + *	   or another -errno code
> + */
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret)
> +{
> +	int err;
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
> +
> +	/*
> +	 * If the filesystem is mounted with '-o test_dummy_encryption', files
> +	 * created in unencrypted directories are automatically encrypted.  For
> +	 * that case, we just need to treat the directory as encrypted here.
> +	 */
> +
> +	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
> +		return 0;
> +
> +	err = fscrypt_get_encryption_info(dir);
> +	if (err)
> +		return err;
> +	if (!fscrypt_has_encryption_key(dir))
> +		return -ENOKEY;
> +
> +	if (WARN_ON_ONCE(inode->i_mode == 0))
> +		return -EINVAL;
> +
> +	/*
> +	 * Only regular files, directories, and symlinks are encrypted.
> +	 * Special files like device nodes and named pipes aren't.
> +	 */
> +	if (!S_ISREG(inode->i_mode) &&
> +	    !S_ISDIR(inode->i_mode) &&
> +	    !S_ISLNK(inode->i_mode))
> +		return 0;
> +
> +	*encrypt_ret = true;
> +
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +
> +	return fscrypt_setup_encryption_info(inode,
> +					     &dir->i_crypt_info->ci_policy,
> +					     nonce);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
> +
>  /**
>   * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
>   * @inode: an inode being evicted
> diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
> index 2d73fd39ad96f..fbe4933206469 100644
> --- a/fs/crypto/policy.c
> +++ b/fs/crypto/policy.c
> @@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
>   *				       an fscrypt_policy
>   * @ctx_u: output context
>   * @policy_u: input policy
> + * @nonce: nonce to use
>   *
>   * Create an fscrypt_context for an inode that is being assigned the given
> - * encryption policy.  A new nonce is randomly generated.
> + * encryption policy.  @nonce must be a new random nonce.
>   *
>   * Return: the size of the new context in bytes.
>   */
> -static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> -					   const union fscrypt_policy *policy_u)
> +static int
> +fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> +				const union fscrypt_policy *policy_u,
> +				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	memset(ctx_u, 0, sizeof(*ctx_u));
>  
> @@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_descriptor,
>  		       policy->master_key_descriptor,
>  		       sizeof(ctx->master_key_descriptor));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	case FSCRYPT_POLICY_V2: {
> @@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_identifier,
>  		       policy->master_key_identifier,
>  		       sizeof(ctx->master_key_identifier));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	}
> @@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
>  static int set_encryption_policy(struct inode *inode,
>  				 const union fscrypt_policy *policy)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	int err;
> @@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
>  		return -EINVAL;
>  	}
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
>  
>  	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
>  }
> @@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  						void *fs_data, bool preload)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	struct fscrypt_info *ci;
> @@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  	if (ci == NULL)
>  		return -ENOKEY;
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
>  
>  	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
>  	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
> @@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  }
>  EXPORT_SYMBOL(fscrypt_inherit_context);
>  
> +/**
> + * fscrypt_set_context() - Set the fscrypt context of a new inode
> + * @inode: A new inode
> + * @fs_data: private data given by FS and passed to ->set_context()
> + *
> + * This should be called after fscrypt_prepare_new_inode(), generally during a
> + * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
> + *
> + * Return: 0 on success, -errno on failure
> + */
> +int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	struct fscrypt_info *ci = inode->i_crypt_info;
> +	union fscrypt_context ctx;
> +	int ctxsize;
> +
> +	/* fscrypt_prepare_new_inode() should have set up the key already. */
> +	if (WARN_ON_ONCE(!ci))
> +		return -ENOKEY;
> +
> +	/*
> +	 * This may be the first time the inode number is available, so do any
> +	 * delayed key setup that requires the inode number.
> +	 */
> +	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
> +	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
> +		const struct fscrypt_master_key *mk =
> +			ci->ci_master_key->payload.data[0];
> +
> +		fscrypt_hash_inode_number(ci, mk);
> +	}
> +
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
> +						  ci->ci_nonce);
> +
> +	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
> +	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_set_context);
> +
>  /**
>   * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
>   * @sb: the filesystem on which test_dummy_encryption is being specified
> diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
> index 991ff8575d0e7..726131dfa0a9b 100644
> --- a/include/linux/fscrypt.h
> +++ b/include/linux/fscrypt.h
> @@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
>  int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  			    void *fs_data, bool preload);
> +int fscrypt_set_context(struct inode *inode, void *fs_data);
>  
>  struct fscrypt_dummy_context {
>  	const union fscrypt_context *ctx;
> @@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
>  
>  /* keysetup.c */
>  int fscrypt_get_encryption_info(struct inode *inode);
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret);
>  void fscrypt_put_encryption_info(struct inode *inode);
>  void fscrypt_free_inode(struct inode *inode);
>  int fscrypt_drop_inode(struct inode *inode);
> @@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
>  struct fscrypt_dummy_context {
>  };
>  
> @@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_prepare_new_inode(struct inode *dir,
> +					    struct inode *inode,
> +					    bool *encrypt_ret)
> +{
> +	if (IS_ENCRYPTED(dir))
> +		return -EOPNOTSUPP;
> +	return 0;
> +}
> +
>  static inline void fscrypt_put_encryption_info(struct inode *inode)
>  {
>  	return;

-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 16:48     ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 16:48 UTC (permalink / raw)
  To: Eric Biggers, linux-fscrypt
  Cc: ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

On Sun, 2020-08-23 at 23:17 -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> fscrypt_get_encryption_info() (setting up an inode's encryption key) is
> intended to be GFP_NOFS safe.  But actually it isn't, since it uses
> functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
> when called with memalloc_nofs_save().  Therefore it can deadlock when
> called from a context that needs GFP_NOFS, e.g. during an ext4
> filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
> Currently, this happens when creating a new encrypted file.
> 
> We can't fix this just by not setting up the key for new inodes right
> away, since new symlinks need their key to encrypt the symlink target.
> 
> So we need to set up the new inode's key before starting the
> transaction.  But fscrypt_get_encryption_info() can't do this currently,
> since it assumes the encryption xattr is already set, and the encryption
> xattr can't be set until the transaction.
> 
> The recently proposed fscrypt support for the ceph filesystem
> (https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
> will have this same ordering problem too, since when ceph creates a new
> symlink, it will need to encrypt it before setting its encryption xattr.
> 
> To solve this problem, add new helper functions:
> 
> - fscrypt_prepare_new_inode() sets up a new inode's encryption key
>   (fscrypt_info), using the parent directory's encryption policy and a
>   new random nonce.  It neither reads nor writes the xattr.
> 
> - fscrypt_set_context() sets the encryption xattr from the fscrypt_info
>   already in memory.  This replaces fscrypt_inherit_context().
> 
> Keep fscrypt_inherit_context() around temporarily until all filesystems
> are converted to use fscrypt_set_context().
> 
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  fs/crypto/fscrypt_private.h |   3 +
>  fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
>  fs/crypto/policy.c          |  61 ++++++++++--
>  include/linux/fscrypt.h     |  17 ++++
>  4 files changed, 220 insertions(+), 49 deletions(-)
> 
> diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
> index 8117a61b6f558..355f6d9377517 100644
> --- a/fs/crypto/fscrypt_private.h
> +++ b/fs/crypto/fscrypt_private.h
> @@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
>  int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  			       const struct fscrypt_master_key *mk);
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk);
> +
>  /* keysetup_v1.c */
>  
>  void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
> diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
> index fea6226afc2b0..6ac816d3e8478 100644
> --- a/fs/crypto/keysetup.c
> +++ b/fs/crypto/keysetup.c
> @@ -10,6 +10,7 @@
>  
>  #include <crypto/skcipher.h>
>  #include <linux/key.h>
> +#include <linux/random.h>
>  
>  #include "fscrypt_private.h"
>  
> @@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  	return 0;
>  }
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk)
> +{
> +	WARN_ON(ci->ci_inode->i_ino == 0);
> +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> +
> +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> +					      &mk->mk_ino_hash_key);

i_ino is an unsigned long. Will this produce a consistent results on
arches with 32 and 64 bit long values? I think it'd be nice to ensure
that we can access an encrypted directory created on a 32-bit host from
(e.g.) a 64-bit host.

It may be better to base this on something besides i_ino or provide an
alternate way of fetching a full 64-bit inode number here when i_ino is
32-bits.


> +}
> +
>  static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  					    struct fscrypt_master_key *mk)
>  {
> @@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  			return err;
>  	}
>  
> -	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> -					      &mk->mk_ino_hash_key);
> +	if (ci->ci_inode->i_ino == 0)
> +		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
> +	else
> +		fscrypt_hash_inode_number(ci, mk);
>  	return 0;
>  }
>  
> @@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
>  	kmem_cache_free(fscrypt_info_cachep, ci);
>  }
>  
> -int fscrypt_get_encryption_info(struct inode *inode)
> +static int
> +fscrypt_setup_encryption_info(struct inode *inode,
> +			      const union fscrypt_policy *policy,
> +			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	struct fscrypt_info *crypt_info;
> -	union fscrypt_context ctx;
>  	struct fscrypt_mode *mode;
>  	struct key *master_key = NULL;
>  	int res;
>  
> -	if (fscrypt_has_encryption_key(inode))
> -		return 0;
> -
> -	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> -	if (res)
> -		return res;
> -
> -	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> -	if (res < 0) {
> -		const union fscrypt_context *dummy_ctx =
> -			fscrypt_get_dummy_context(inode->i_sb);
> -
> -		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> -			fscrypt_warn(inode,
> -				     "Error %d getting encryption context",
> -				     res);
> -			return res;
> -		}
> -		/* Fake up a context for an unencrypted directory */
> -		res = fscrypt_context_size(dummy_ctx);
> -		memcpy(&ctx, dummy_ctx, res);
> -	}
> -
>  	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
>  	if (!crypt_info)
>  		return -ENOMEM;
>  
>  	crypt_info->ci_inode = inode;
> -
> -	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
> -	if (res) {
> -		fscrypt_warn(inode,
> -			     "Unrecognized or corrupt encryption context");
> -		goto out;
> -	}
> -
> -	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
> -	       FSCRYPT_FILE_NONCE_SIZE);
> -
> -	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
> -		res = -EINVAL;
> -		goto out;
> -	}
> +	crypt_info->ci_policy = *policy;
> +	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  
>  	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
>  	if (IS_ERR(mode)) {
> @@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
>  	put_crypt_info(crypt_info);
>  	return res;
>  }
> +
> +/**
> + * fscrypt_get_encryption_info() - set up an inode's encryption key
> + * @inode: the inode to set up the key for.  Must either be encrypted, or be an
> + *	   unencrypted directory with '-o test_dummy_encryption'.
> + *
> + * Create ->i_crypt_info, if it's not already set.
> + *
> + * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
> + * generally this shouldn't be called from within a filesystem transaction.
> + *
> + * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
> + *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
> + *	   distinguish these cases.)  Also can return another -errno code.
> + */
> +int fscrypt_get_encryption_info(struct inode *inode)
> +{
> +	int res;
> +	union fscrypt_context ctx;
> +	union fscrypt_policy policy;
> +
> +	if (fscrypt_has_encryption_key(inode))
> +		return 0;
> +
> +	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> +	if (res)
> +		return res;
> +
> +	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> +	if (res < 0) {
> +		const union fscrypt_context *dummy_ctx =
> +			fscrypt_get_dummy_context(inode->i_sb);
> +
> +		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> +			fscrypt_warn(inode,
> +				     "Error %d getting encryption context",
> +				     res);
> +			return res;
> +		}
> +		/* Fake up a context for an unencrypted directory */
> +		res = fscrypt_context_size(dummy_ctx);
> +		memcpy(&ctx, dummy_ctx, res);
> +	}
> +
> +	res = fscrypt_policy_from_context(&policy, &ctx, res);
> +	if (res) {
> +		fscrypt_warn(inode,
> +			     "Unrecognized or corrupt encryption context");
> +		return res;
> +	}
> +
> +	if (!fscrypt_supported_policy(&policy, inode))
> +		return -EINVAL;
> +
> +	return fscrypt_setup_encryption_info(inode, &policy,
> +					     fscrypt_context_nonce(&ctx));
> +}
>  EXPORT_SYMBOL(fscrypt_get_encryption_info);
>  
> +/**
> + * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
> + * @dir: a possibly-encrypted directory
> + * @inode: the inode that is being created.  ->i_mode must be set already.
> + *	   ->i_ino does *not* need to be set yet.
> + * @encrypt_ret: (output) set to true if the new inode will be encrypted.
> + *
> + * Prepares to create a new inode in a directory.  If either the inode or its
> + * filename will be encrypted, this sets up the directory's
> + * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
> + * its ->i_crypt_info and sets *encrypt_ret to true.
> + *
> + * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
> + * right away.  However, symlinks do need it.
> + *
> + * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
> + * any filesystem transaction to create the inode.  For this reason, ->i_ino
> + * isn't required to be set yet, as the filesystem may not have set it yet.
> + *
> + * This doesn't actually store the new inode's encryption context to disk.
> + * That still needs to be done later by calling fscrypt_set_context().
> + *
> + * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
> + *	   or another -errno code
> + */
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret)
> +{
> +	int err;
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
> +
> +	/*
> +	 * If the filesystem is mounted with '-o test_dummy_encryption', files
> +	 * created in unencrypted directories are automatically encrypted.  For
> +	 * that case, we just need to treat the directory as encrypted here.
> +	 */
> +
> +	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
> +		return 0;
> +
> +	err = fscrypt_get_encryption_info(dir);
> +	if (err)
> +		return err;
> +	if (!fscrypt_has_encryption_key(dir))
> +		return -ENOKEY;
> +
> +	if (WARN_ON_ONCE(inode->i_mode == 0))
> +		return -EINVAL;
> +
> +	/*
> +	 * Only regular files, directories, and symlinks are encrypted.
> +	 * Special files like device nodes and named pipes aren't.
> +	 */
> +	if (!S_ISREG(inode->i_mode) &&
> +	    !S_ISDIR(inode->i_mode) &&
> +	    !S_ISLNK(inode->i_mode))
> +		return 0;
> +
> +	*encrypt_ret = true;
> +
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +
> +	return fscrypt_setup_encryption_info(inode,
> +					     &dir->i_crypt_info->ci_policy,
> +					     nonce);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
> +
>  /**
>   * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
>   * @inode: an inode being evicted
> diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
> index 2d73fd39ad96f..fbe4933206469 100644
> --- a/fs/crypto/policy.c
> +++ b/fs/crypto/policy.c
> @@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
>   *				       an fscrypt_policy
>   * @ctx_u: output context
>   * @policy_u: input policy
> + * @nonce: nonce to use
>   *
>   * Create an fscrypt_context for an inode that is being assigned the given
> - * encryption policy.  A new nonce is randomly generated.
> + * encryption policy.  @nonce must be a new random nonce.
>   *
>   * Return: the size of the new context in bytes.
>   */
> -static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> -					   const union fscrypt_policy *policy_u)
> +static int
> +fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> +				const union fscrypt_policy *policy_u,
> +				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	memset(ctx_u, 0, sizeof(*ctx_u));
>  
> @@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_descriptor,
>  		       policy->master_key_descriptor,
>  		       sizeof(ctx->master_key_descriptor));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	case FSCRYPT_POLICY_V2: {
> @@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_identifier,
>  		       policy->master_key_identifier,
>  		       sizeof(ctx->master_key_identifier));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	}
> @@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
>  static int set_encryption_policy(struct inode *inode,
>  				 const union fscrypt_policy *policy)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	int err;
> @@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
>  		return -EINVAL;
>  	}
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
>  
>  	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
>  }
> @@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  						void *fs_data, bool preload)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	struct fscrypt_info *ci;
> @@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  	if (ci == NULL)
>  		return -ENOKEY;
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
>  
>  	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
>  	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
> @@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  }
>  EXPORT_SYMBOL(fscrypt_inherit_context);
>  
> +/**
> + * fscrypt_set_context() - Set the fscrypt context of a new inode
> + * @inode: A new inode
> + * @fs_data: private data given by FS and passed to ->set_context()
> + *
> + * This should be called after fscrypt_prepare_new_inode(), generally during a
> + * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
> + *
> + * Return: 0 on success, -errno on failure
> + */
> +int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	struct fscrypt_info *ci = inode->i_crypt_info;
> +	union fscrypt_context ctx;
> +	int ctxsize;
> +
> +	/* fscrypt_prepare_new_inode() should have set up the key already. */
> +	if (WARN_ON_ONCE(!ci))
> +		return -ENOKEY;
> +
> +	/*
> +	 * This may be the first time the inode number is available, so do any
> +	 * delayed key setup that requires the inode number.
> +	 */
> +	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
> +	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
> +		const struct fscrypt_master_key *mk =
> +			ci->ci_master_key->payload.data[0];
> +
> +		fscrypt_hash_inode_number(ci, mk);
> +	}
> +
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
> +						  ci->ci_nonce);
> +
> +	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
> +	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_set_context);
> +
>  /**
>   * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
>   * @sb: the filesystem on which test_dummy_encryption is being specified
> diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
> index 991ff8575d0e7..726131dfa0a9b 100644
> --- a/include/linux/fscrypt.h
> +++ b/include/linux/fscrypt.h
> @@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
>  int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  			    void *fs_data, bool preload);
> +int fscrypt_set_context(struct inode *inode, void *fs_data);
>  
>  struct fscrypt_dummy_context {
>  	const union fscrypt_context *ctx;
> @@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
>  
>  /* keysetup.c */
>  int fscrypt_get_encryption_info(struct inode *inode);
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret);
>  void fscrypt_put_encryption_info(struct inode *inode);
>  void fscrypt_free_inode(struct inode *inode);
>  int fscrypt_drop_inode(struct inode *inode);
> @@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
>  struct fscrypt_dummy_context {
>  };
>  
> @@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_prepare_new_inode(struct inode *dir,
> +					    struct inode *inode,
> +					    bool *encrypt_ret)
> +{
> +	if (IS_ENCRYPTED(dir))
> +		return -EOPNOTSUPP;
> +	return 0;
> +}
> +
>  static inline void fscrypt_put_encryption_info(struct inode *inode)
>  {
>  	return;

-- 
Jeff Layton <jlayton@kernel.org>



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 16:48     ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 16:48 UTC (permalink / raw)
  To: Eric Biggers, linux-fscrypt
  Cc: ceph-devel, linux-ext4, linux-mtd, linux-f2fs-devel

On Sun, 2020-08-23 at 23:17 -0700, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> fscrypt_get_encryption_info() (setting up an inode's encryption key) is
> intended to be GFP_NOFS safe.  But actually it isn't, since it uses
> functions like crypto_alloc_skcipher() which aren't GFP_NOFS safe, even
> when called with memalloc_nofs_save().  Therefore it can deadlock when
> called from a context that needs GFP_NOFS, e.g. during an ext4
> filesystem transaction or between f2fs_lock_op() and f2fs_unlock_op().
> Currently, this happens when creating a new encrypted file.
> 
> We can't fix this just by not setting up the key for new inodes right
> away, since new symlinks need their key to encrypt the symlink target.
> 
> So we need to set up the new inode's key before starting the
> transaction.  But fscrypt_get_encryption_info() can't do this currently,
> since it assumes the encryption xattr is already set, and the encryption
> xattr can't be set until the transaction.
> 
> The recently proposed fscrypt support for the ceph filesystem
> (https://lkml.kernel.org/linux-fscrypt/20200821182813.52570-1-jlayton@kernel.org/T/#u)
> will have this same ordering problem too, since when ceph creates a new
> symlink, it will need to encrypt it before setting its encryption xattr.
> 
> To solve this problem, add new helper functions:
> 
> - fscrypt_prepare_new_inode() sets up a new inode's encryption key
>   (fscrypt_info), using the parent directory's encryption policy and a
>   new random nonce.  It neither reads nor writes the xattr.
> 
> - fscrypt_set_context() sets the encryption xattr from the fscrypt_info
>   already in memory.  This replaces fscrypt_inherit_context().
> 
> Keep fscrypt_inherit_context() around temporarily until all filesystems
> are converted to use fscrypt_set_context().
> 
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  fs/crypto/fscrypt_private.h |   3 +
>  fs/crypto/keysetup.c        | 188 ++++++++++++++++++++++++++++--------
>  fs/crypto/policy.c          |  61 ++++++++++--
>  include/linux/fscrypt.h     |  17 ++++
>  4 files changed, 220 insertions(+), 49 deletions(-)
> 
> diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
> index 8117a61b6f558..355f6d9377517 100644
> --- a/fs/crypto/fscrypt_private.h
> +++ b/fs/crypto/fscrypt_private.h
> @@ -572,6 +572,9 @@ int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key);
>  int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  			       const struct fscrypt_master_key *mk);
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk);
> +
>  /* keysetup_v1.c */
>  
>  void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
> diff --git a/fs/crypto/keysetup.c b/fs/crypto/keysetup.c
> index fea6226afc2b0..6ac816d3e8478 100644
> --- a/fs/crypto/keysetup.c
> +++ b/fs/crypto/keysetup.c
> @@ -10,6 +10,7 @@
>  
>  #include <crypto/skcipher.h>
>  #include <linux/key.h>
> +#include <linux/random.h>
>  
>  #include "fscrypt_private.h"
>  
> @@ -222,6 +223,16 @@ int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
>  	return 0;
>  }
>  
> +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> +			       const struct fscrypt_master_key *mk)
> +{
> +	WARN_ON(ci->ci_inode->i_ino == 0);
> +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> +
> +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> +					      &mk->mk_ino_hash_key);

i_ino is an unsigned long. Will this produce a consistent results on
arches with 32 and 64 bit long values? I think it'd be nice to ensure
that we can access an encrypted directory created on a 32-bit host from
(e.g.) a 64-bit host.

It may be better to base this on something besides i_ino or provide an
alternate way of fetching a full 64-bit inode number here when i_ino is
32-bits.


> +}
> +
>  static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  					    struct fscrypt_master_key *mk)
>  {
> @@ -254,8 +265,10 @@ static int fscrypt_setup_iv_ino_lblk_32_key(struct fscrypt_info *ci,
>  			return err;
>  	}
>  
> -	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> -					      &mk->mk_ino_hash_key);
> +	if (ci->ci_inode->i_ino == 0)
> +		WARN_ON(!(ci->ci_inode->i_state & I_NEW));
> +	else
> +		fscrypt_hash_inode_number(ci, mk);
>  	return 0;
>  }
>  
> @@ -454,57 +467,23 @@ static void put_crypt_info(struct fscrypt_info *ci)
>  	kmem_cache_free(fscrypt_info_cachep, ci);
>  }
>  
> -int fscrypt_get_encryption_info(struct inode *inode)
> +static int
> +fscrypt_setup_encryption_info(struct inode *inode,
> +			      const union fscrypt_policy *policy,
> +			      const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	struct fscrypt_info *crypt_info;
> -	union fscrypt_context ctx;
>  	struct fscrypt_mode *mode;
>  	struct key *master_key = NULL;
>  	int res;
>  
> -	if (fscrypt_has_encryption_key(inode))
> -		return 0;
> -
> -	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> -	if (res)
> -		return res;
> -
> -	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> -	if (res < 0) {
> -		const union fscrypt_context *dummy_ctx =
> -			fscrypt_get_dummy_context(inode->i_sb);
> -
> -		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> -			fscrypt_warn(inode,
> -				     "Error %d getting encryption context",
> -				     res);
> -			return res;
> -		}
> -		/* Fake up a context for an unencrypted directory */
> -		res = fscrypt_context_size(dummy_ctx);
> -		memcpy(&ctx, dummy_ctx, res);
> -	}
> -
>  	crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
>  	if (!crypt_info)
>  		return -ENOMEM;
>  
>  	crypt_info->ci_inode = inode;
> -
> -	res = fscrypt_policy_from_context(&crypt_info->ci_policy, &ctx, res);
> -	if (res) {
> -		fscrypt_warn(inode,
> -			     "Unrecognized or corrupt encryption context");
> -		goto out;
> -	}
> -
> -	memcpy(crypt_info->ci_nonce, fscrypt_context_nonce(&ctx),
> -	       FSCRYPT_FILE_NONCE_SIZE);
> -
> -	if (!fscrypt_supported_policy(&crypt_info->ci_policy, inode)) {
> -		res = -EINVAL;
> -		goto out;
> -	}
> +	crypt_info->ci_policy = *policy;
> +	memcpy(crypt_info->ci_nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  
>  	mode = select_encryption_mode(&crypt_info->ci_policy, inode);
>  	if (IS_ERR(mode)) {
> @@ -555,8 +534,133 @@ int fscrypt_get_encryption_info(struct inode *inode)
>  	put_crypt_info(crypt_info);
>  	return res;
>  }
> +
> +/**
> + * fscrypt_get_encryption_info() - set up an inode's encryption key
> + * @inode: the inode to set up the key for.  Must either be encrypted, or be an
> + *	   unencrypted directory with '-o test_dummy_encryption'.
> + *
> + * Create ->i_crypt_info, if it's not already set.
> + *
> + * Note: unless ->i_crypt_info is already set, this isn't %GFP_NOFS-safe.  So
> + * generally this shouldn't be called from within a filesystem transaction.
> + *
> + * Return: 0 if ->i_crypt_info was set or was already set, *or* if the
> + *	   encryption key is unavailable.  (Use fscrypt_has_encryption_key() to
> + *	   distinguish these cases.)  Also can return another -errno code.
> + */
> +int fscrypt_get_encryption_info(struct inode *inode)
> +{
> +	int res;
> +	union fscrypt_context ctx;
> +	union fscrypt_policy policy;
> +
> +	if (fscrypt_has_encryption_key(inode))
> +		return 0;
> +
> +	res = fscrypt_initialize(inode->i_sb->s_cop->flags);
> +	if (res)
> +		return res;
> +
> +	res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
> +	if (res < 0) {
> +		const union fscrypt_context *dummy_ctx =
> +			fscrypt_get_dummy_context(inode->i_sb);
> +
> +		if (IS_ENCRYPTED(inode) || !dummy_ctx) {
> +			fscrypt_warn(inode,
> +				     "Error %d getting encryption context",
> +				     res);
> +			return res;
> +		}
> +		/* Fake up a context for an unencrypted directory */
> +		res = fscrypt_context_size(dummy_ctx);
> +		memcpy(&ctx, dummy_ctx, res);
> +	}
> +
> +	res = fscrypt_policy_from_context(&policy, &ctx, res);
> +	if (res) {
> +		fscrypt_warn(inode,
> +			     "Unrecognized or corrupt encryption context");
> +		return res;
> +	}
> +
> +	if (!fscrypt_supported_policy(&policy, inode))
> +		return -EINVAL;
> +
> +	return fscrypt_setup_encryption_info(inode, &policy,
> +					     fscrypt_context_nonce(&ctx));
> +}
>  EXPORT_SYMBOL(fscrypt_get_encryption_info);
>  
> +/**
> + * fscrypt_prepare_new_inode() - prepare to create a new inode in a directory
> + * @dir: a possibly-encrypted directory
> + * @inode: the inode that is being created.  ->i_mode must be set already.
> + *	   ->i_ino does *not* need to be set yet.
> + * @encrypt_ret: (output) set to true if the new inode will be encrypted.
> + *
> + * Prepares to create a new inode in a directory.  If either the inode or its
> + * filename will be encrypted, this sets up the directory's
> + * ->i_crypt_info.  Additionally, if the inode will be encrypted, this sets up
> + * its ->i_crypt_info and sets *encrypt_ret to true.
> + *
> + * Note that the new inode's ->i_crypt_info *usually* isn't actually needed
> + * right away.  However, symlinks do need it.
> + *
> + * This isn't %GFP_NOFS-safe, and therefore it should be called before starting
> + * any filesystem transaction to create the inode.  For this reason, ->i_ino
> + * isn't required to be set yet, as the filesystem may not have set it yet.
> + *
> + * This doesn't actually store the new inode's encryption context to disk.
> + * That still needs to be done later by calling fscrypt_set_context().
> + *
> + * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
> + *	   or another -errno code
> + */
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret)
> +{
> +	int err;
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
> +
> +	/*
> +	 * If the filesystem is mounted with '-o test_dummy_encryption', files
> +	 * created in unencrypted directories are automatically encrypted.  For
> +	 * that case, we just need to treat the directory as encrypted here.
> +	 */
> +
> +	if (!IS_ENCRYPTED(dir) && fscrypt_get_dummy_context(dir->i_sb) == NULL)
> +		return 0;
> +
> +	err = fscrypt_get_encryption_info(dir);
> +	if (err)
> +		return err;
> +	if (!fscrypt_has_encryption_key(dir))
> +		return -ENOKEY;
> +
> +	if (WARN_ON_ONCE(inode->i_mode == 0))
> +		return -EINVAL;
> +
> +	/*
> +	 * Only regular files, directories, and symlinks are encrypted.
> +	 * Special files like device nodes and named pipes aren't.
> +	 */
> +	if (!S_ISREG(inode->i_mode) &&
> +	    !S_ISDIR(inode->i_mode) &&
> +	    !S_ISLNK(inode->i_mode))
> +		return 0;
> +
> +	*encrypt_ret = true;
> +
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +
> +	return fscrypt_setup_encryption_info(inode,
> +					     &dir->i_crypt_info->ci_policy,
> +					     nonce);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_prepare_new_inode);
> +
>  /**
>   * fscrypt_put_encryption_info() - free most of an inode's fscrypt data
>   * @inode: an inode being evicted
> diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c
> index 2d73fd39ad96f..fbe4933206469 100644
> --- a/fs/crypto/policy.c
> +++ b/fs/crypto/policy.c
> @@ -235,14 +235,17 @@ bool fscrypt_supported_policy(const union fscrypt_policy *policy_u,
>   *				       an fscrypt_policy
>   * @ctx_u: output context
>   * @policy_u: input policy
> + * @nonce: nonce to use
>   *
>   * Create an fscrypt_context for an inode that is being assigned the given
> - * encryption policy.  A new nonce is randomly generated.
> + * encryption policy.  @nonce must be a new random nonce.
>   *
>   * Return: the size of the new context in bytes.
>   */
> -static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> -					   const union fscrypt_policy *policy_u)
> +static int
> +fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
> +				const union fscrypt_policy *policy_u,
> +				const u8 nonce[FSCRYPT_FILE_NONCE_SIZE])
>  {
>  	memset(ctx_u, 0, sizeof(*ctx_u));
>  
> @@ -260,7 +263,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_descriptor,
>  		       policy->master_key_descriptor,
>  		       sizeof(ctx->master_key_descriptor));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	case FSCRYPT_POLICY_V2: {
> @@ -276,7 +279,7 @@ static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u,
>  		memcpy(ctx->master_key_identifier,
>  		       policy->master_key_identifier,
>  		       sizeof(ctx->master_key_identifier));
> -		get_random_bytes(ctx->nonce, sizeof(ctx->nonce));
> +		memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE);
>  		return sizeof(*ctx);
>  	}
>  	}
> @@ -372,6 +375,7 @@ static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy)
>  static int set_encryption_policy(struct inode *inode,
>  				 const union fscrypt_policy *policy)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	int err;
> @@ -409,7 +413,8 @@ static int set_encryption_policy(struct inode *inode,
>  		return -EINVAL;
>  	}
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, policy, nonce);
>  
>  	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL);
>  }
> @@ -632,6 +637,7 @@ EXPORT_SYMBOL(fscrypt_has_permitted_context);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  						void *fs_data, bool preload)
>  {
> +	u8 nonce[FSCRYPT_FILE_NONCE_SIZE];
>  	union fscrypt_context ctx;
>  	int ctxsize;
>  	struct fscrypt_info *ci;
> @@ -645,7 +651,8 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  	if (ci == NULL)
>  		return -ENOKEY;
>  
> -	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy);
> +	get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE);
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy, nonce);
>  
>  	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
>  	res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data);
> @@ -655,6 +662,46 @@ int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  }
>  EXPORT_SYMBOL(fscrypt_inherit_context);
>  
> +/**
> + * fscrypt_set_context() - Set the fscrypt context of a new inode
> + * @inode: A new inode
> + * @fs_data: private data given by FS and passed to ->set_context()
> + *
> + * This should be called after fscrypt_prepare_new_inode(), generally during a
> + * filesystem transaction.  Everything here must be %GFP_NOFS-safe.
> + *
> + * Return: 0 on success, -errno on failure
> + */
> +int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	struct fscrypt_info *ci = inode->i_crypt_info;
> +	union fscrypt_context ctx;
> +	int ctxsize;
> +
> +	/* fscrypt_prepare_new_inode() should have set up the key already. */
> +	if (WARN_ON_ONCE(!ci))
> +		return -ENOKEY;
> +
> +	/*
> +	 * This may be the first time the inode number is available, so do any
> +	 * delayed key setup that requires the inode number.
> +	 */
> +	if (ci->ci_policy.version == FSCRYPT_POLICY_V2 &&
> +	    (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) {
> +		const struct fscrypt_master_key *mk =
> +			ci->ci_master_key->payload.data[0];
> +
> +		fscrypt_hash_inode_number(ci, mk);
> +	}
> +
> +	ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy,
> +						  ci->ci_nonce);
> +
> +	BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE);
> +	return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data);
> +}
> +EXPORT_SYMBOL_GPL(fscrypt_set_context);
> +
>  /**
>   * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption'
>   * @sb: the filesystem on which test_dummy_encryption is being specified
> diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h
> index 991ff8575d0e7..726131dfa0a9b 100644
> --- a/include/linux/fscrypt.h
> +++ b/include/linux/fscrypt.h
> @@ -158,6 +158,7 @@ int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
>  int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
>  int fscrypt_inherit_context(struct inode *parent, struct inode *child,
>  			    void *fs_data, bool preload);
> +int fscrypt_set_context(struct inode *inode, void *fs_data);
>  
>  struct fscrypt_dummy_context {
>  	const union fscrypt_context *ctx;
> @@ -184,6 +185,8 @@ int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
>  
>  /* keysetup.c */
>  int fscrypt_get_encryption_info(struct inode *inode);
> +int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
> +			      bool *encrypt_ret);
>  void fscrypt_put_encryption_info(struct inode *inode);
>  void fscrypt_free_inode(struct inode *inode);
>  int fscrypt_drop_inode(struct inode *inode);
> @@ -347,6 +350,11 @@ static inline int fscrypt_inherit_context(struct inode *parent,
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
> +{
> +	return -EOPNOTSUPP;
> +}
> +
>  struct fscrypt_dummy_context {
>  };
>  
> @@ -394,6 +402,15 @@ static inline int fscrypt_get_encryption_info(struct inode *inode)
>  	return -EOPNOTSUPP;
>  }
>  
> +static inline int fscrypt_prepare_new_inode(struct inode *dir,
> +					    struct inode *inode,
> +					    bool *encrypt_ret)
> +{
> +	if (IS_ENCRYPTED(dir))
> +		return -EOPNOTSUPP;
> +	return 0;
> +}
> +
>  static inline void fscrypt_put_encryption_info(struct inode *inode)
>  {
>  	return;

-- 
Jeff Layton <jlayton@kernel.org>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24 16:48     ` [f2fs-dev] " Jeff Layton
  (?)
@ 2020-08-24 18:21       ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 18:21 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > +			       const struct fscrypt_master_key *mk)
> > +{
> > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > +
> > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > +					      &mk->mk_ino_hash_key);
> 
> i_ino is an unsigned long. Will this produce a consistent results on
> arches with 32 and 64 bit long values? I think it'd be nice to ensure
> that we can access an encrypted directory created on a 32-bit host from
> (e.g.) a 64-bit host.

The result is the same regardless of word size and endianness.
siphash_1u64(v, k) is equivalent to:

	__le64 x = cpu_to_le64(v);
	siphash(&x, 8, k);

> It may be better to base this on something besides i_ino

This code that hashes the inode number is only used when userspace used
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
the encryption to be optimized for eMMC inline encryption hardware.  For more
details, see commit e3b1078bedd3 which added this feature.

We actually could have hashed the file nonce instead of the inode number.  But I
wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
format optimized for UFS inline encryption hardware.

Both of these flags have very specific use cases; they make it feasible to use
inline encryption hardware
(https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
that only supports a small number of keyslots and that limits the IV length.

You don't need to worry about these flags at all for ceph, since there won't be
any use case to use them on ceph, and ceph won't be declaring support for them.

- Eric

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 18:21       ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 18:21 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > +			       const struct fscrypt_master_key *mk)
> > +{
> > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > +
> > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > +					      &mk->mk_ino_hash_key);
> 
> i_ino is an unsigned long. Will this produce a consistent results on
> arches with 32 and 64 bit long values? I think it'd be nice to ensure
> that we can access an encrypted directory created on a 32-bit host from
> (e.g.) a 64-bit host.

The result is the same regardless of word size and endianness.
siphash_1u64(v, k) is equivalent to:

	__le64 x = cpu_to_le64(v);
	siphash(&x, 8, k);

> It may be better to base this on something besides i_ino

This code that hashes the inode number is only used when userspace used
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
the encryption to be optimized for eMMC inline encryption hardware.  For more
details, see commit e3b1078bedd3 which added this feature.

We actually could have hashed the file nonce instead of the inode number.  But I
wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
format optimized for UFS inline encryption hardware.

Both of these flags have very specific use cases; they make it feasible to use
inline encryption hardware
(https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
that only supports a small number of keyslots and that limits the IV length.

You don't need to worry about these flags at all for ceph, since there won't be
any use case to use them on ceph, and ceph won't be declaring support for them.

- Eric


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 18:21       ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 18:21 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > +			       const struct fscrypt_master_key *mk)
> > +{
> > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > +
> > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > +					      &mk->mk_ino_hash_key);
> 
> i_ino is an unsigned long. Will this produce a consistent results on
> arches with 32 and 64 bit long values? I think it'd be nice to ensure
> that we can access an encrypted directory created on a 32-bit host from
> (e.g.) a 64-bit host.

The result is the same regardless of word size and endianness.
siphash_1u64(v, k) is equivalent to:

	__le64 x = cpu_to_le64(v);
	siphash(&x, 8, k);

> It may be better to base this on something besides i_ino

This code that hashes the inode number is only used when userspace used
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
the encryption to be optimized for eMMC inline encryption hardware.  For more
details, see commit e3b1078bedd3 which added this feature.

We actually could have hashed the file nonce instead of the inode number.  But I
wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
format optimized for UFS inline encryption hardware.

Both of these flags have very specific use cases; they make it feasible to use
inline encryption hardware
(https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
that only supports a small number of keyslots and that limits the IV length.

You don't need to worry about these flags at all for ceph, since there won't be
any use case to use them on ceph, and ceph won't be declaring support for them.

- Eric

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24 18:21       ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24 18:47         ` Jeff Layton
  -1 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 18:47 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > +			       const struct fscrypt_master_key *mk)
> > > +{
> > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > +
> > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > +					      &mk->mk_ino_hash_key);
> > 
> > i_ino is an unsigned long. Will this produce a consistent results on
> > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > that we can access an encrypted directory created on a 32-bit host from
> > (e.g.) a 64-bit host.
> 
> The result is the same regardless of word size and endianness.
> siphash_1u64(v, k) is equivalent to:
> 
> 	__le64 x = cpu_to_le64(v);
> 	siphash(&x, 8, k);
> 

In the case where you have an (on-storage) inode number that is larger
than 2^32, x will almost certainly be different on a 32 vs. 64-bit
wordsize.

On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
64-bit word and the upper 32 bits will be zeroed out. So it seems like
this means that if you're using inline hardware you're going to end up
with a result that won't work correctly across different wordsizes.

Maybe that's ok, but it seems like something that could be handled by
hashing a different value.

> > It may be better to base this on something besides i_ino
> 
> This code that hashes the inode number is only used when userspace used
> FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
> the encryption to be optimized for eMMC inline encryption hardware.  For more
> details, see commit e3b1078bedd3 which added this feature.
> 
> We actually could have hashed the file nonce instead of the inode number.  But I
> wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
> format optimized for UFS inline encryption hardware.
> 
> Both of these flags have very specific use cases; they make it feasible to use
> inline encryption hardware
> (https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
> that only supports a small number of keyslots and that limits the IV length.
> 
> You don't need to worry about these flags at all for ceph, since there won't be
> any use case to use them on ceph, and ceph won't be declaring support for them.

Ahh, good to know. Thanks!
-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 18:47         ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 18:47 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > +			       const struct fscrypt_master_key *mk)
> > > +{
> > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > +
> > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > +					      &mk->mk_ino_hash_key);
> > 
> > i_ino is an unsigned long. Will this produce a consistent results on
> > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > that we can access an encrypted directory created on a 32-bit host from
> > (e.g.) a 64-bit host.
> 
> The result is the same regardless of word size and endianness.
> siphash_1u64(v, k) is equivalent to:
> 
> 	__le64 x = cpu_to_le64(v);
> 	siphash(&x, 8, k);
> 

In the case where you have an (on-storage) inode number that is larger
than 2^32, x will almost certainly be different on a 32 vs. 64-bit
wordsize.

On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
64-bit word and the upper 32 bits will be zeroed out. So it seems like
this means that if you're using inline hardware you're going to end up
with a result that won't work correctly across different wordsizes.

Maybe that's ok, but it seems like something that could be handled by
hashing a different value.

> > It may be better to base this on something besides i_ino
> 
> This code that hashes the inode number is only used when userspace used
> FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
> the encryption to be optimized for eMMC inline encryption hardware.  For more
> details, see commit e3b1078bedd3 which added this feature.
> 
> We actually could have hashed the file nonce instead of the inode number.  But I
> wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
> format optimized for UFS inline encryption hardware.
> 
> Both of these flags have very specific use cases; they make it feasible to use
> inline encryption hardware
> (https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
> that only supports a small number of keyslots and that limits the IV length.
> 
> You don't need to worry about these flags at all for ceph, since there won't be
> any use case to use them on ceph, and ceph won't be declaring support for them.

Ahh, good to know. Thanks!
-- 
Jeff Layton <jlayton@kernel.org>



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 18:47         ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 18:47 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > +			       const struct fscrypt_master_key *mk)
> > > +{
> > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > +
> > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > +					      &mk->mk_ino_hash_key);
> > 
> > i_ino is an unsigned long. Will this produce a consistent results on
> > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > that we can access an encrypted directory created on a 32-bit host from
> > (e.g.) a 64-bit host.
> 
> The result is the same regardless of word size and endianness.
> siphash_1u64(v, k) is equivalent to:
> 
> 	__le64 x = cpu_to_le64(v);
> 	siphash(&x, 8, k);
> 

In the case where you have an (on-storage) inode number that is larger
than 2^32, x will almost certainly be different on a 32 vs. 64-bit
wordsize.

On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
64-bit word and the upper 32 bits will be zeroed out. So it seems like
this means that if you're using inline hardware you're going to end up
with a result that won't work correctly across different wordsizes.

Maybe that's ok, but it seems like something that could be handled by
hashing a different value.

> > It may be better to base this on something besides i_ino
> 
> This code that hashes the inode number is only used when userspace used
> FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32 for the directory.  IV_INO_LBLK_32 modifies
> the encryption to be optimized for eMMC inline encryption hardware.  For more
> details, see commit e3b1078bedd3 which added this feature.
> 
> We actually could have hashed the file nonce instead of the inode number.  But I
> wanted to make the eMMC-optimized format similar to IV_INO_LBLK_64, which is the
> format optimized for UFS inline encryption hardware.
> 
> Both of these flags have very specific use cases; they make it feasible to use
> inline encryption hardware
> (https://www.kernel.org/doc/html/latest/block/inline-encryption.html)
> that only supports a small number of keyslots and that limits the IV length.
> 
> You don't need to worry about these flags at all for ceph, since there won't be
> any use case to use them on ceph, and ceph won't be declaring support for them.

Ahh, good to know. Thanks!
-- 
Jeff Layton <jlayton@kernel.org>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24 18:47         ` [f2fs-dev] " Jeff Layton
  (?)
@ 2020-08-24 19:02           ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 19:02 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > +			       const struct fscrypt_master_key *mk)
> > > > +{
> > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > +
> > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > +					      &mk->mk_ino_hash_key);
> > > 
> > > i_ino is an unsigned long. Will this produce a consistent results on
> > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > that we can access an encrypted directory created on a 32-bit host from
> > > (e.g.) a 64-bit host.
> > 
> > The result is the same regardless of word size and endianness.
> > siphash_1u64(v, k) is equivalent to:
> > 
> > 	__le64 x = cpu_to_le64(v);
> > 	siphash(&x, 8, k);
> > 
> 
> In the case where you have an (on-storage) inode number that is larger
> than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> wordsize.
> 
> On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> this means that if you're using inline hardware you're going to end up
> with a result that won't work correctly across different wordsizes.

That's only possible if the VFS is truncating the inode number, which would also
break userspace in lots of ways like making applications think that files are
hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.

The correct fix for that would be to make inode::i_ino 64-bit.

Note that ext4 and f2fs (currently the only filesystems that support the
IV_INO_LBLK_* flags) only support 32-bit inode numbers.

- Eric

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 19:02           ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 19:02 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > +			       const struct fscrypt_master_key *mk)
> > > > +{
> > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > +
> > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > +					      &mk->mk_ino_hash_key);
> > > 
> > > i_ino is an unsigned long. Will this produce a consistent results on
> > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > that we can access an encrypted directory created on a 32-bit host from
> > > (e.g.) a 64-bit host.
> > 
> > The result is the same regardless of word size and endianness.
> > siphash_1u64(v, k) is equivalent to:
> > 
> > 	__le64 x = cpu_to_le64(v);
> > 	siphash(&x, 8, k);
> > 
> 
> In the case where you have an (on-storage) inode number that is larger
> than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> wordsize.
> 
> On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> this means that if you're using inline hardware you're going to end up
> with a result that won't work correctly across different wordsizes.

That's only possible if the VFS is truncating the inode number, which would also
break userspace in lots of ways like making applications think that files are
hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.

The correct fix for that would be to make inode::i_ino 64-bit.

Note that ext4 and f2fs (currently the only filesystems that support the
IV_INO_LBLK_* flags) only support 32-bit inode numbers.

- Eric


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 19:02           ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 19:02 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > +			       const struct fscrypt_master_key *mk)
> > > > +{
> > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > +
> > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > +					      &mk->mk_ino_hash_key);
> > > 
> > > i_ino is an unsigned long. Will this produce a consistent results on
> > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > that we can access an encrypted directory created on a 32-bit host from
> > > (e.g.) a 64-bit host.
> > 
> > The result is the same regardless of word size and endianness.
> > siphash_1u64(v, k) is equivalent to:
> > 
> > 	__le64 x = cpu_to_le64(v);
> > 	siphash(&x, 8, k);
> > 
> 
> In the case where you have an (on-storage) inode number that is larger
> than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> wordsize.
> 
> On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> this means that if you're using inline hardware you're going to end up
> with a result that won't work correctly across different wordsizes.

That's only possible if the VFS is truncating the inode number, which would also
break userspace in lots of ways like making applications think that files are
hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.

The correct fix for that would be to make inode::i_ino 64-bit.

Note that ext4 and f2fs (currently the only filesystems that support the
IV_INO_LBLK_* flags) only support 32-bit inode numbers.

- Eric

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24 19:02           ` [f2fs-dev] " Eric Biggers
  (?)
@ 2020-08-24 19:42             ` Jeff Layton
  -1 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 19:42 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > +			       const struct fscrypt_master_key *mk)
> > > > > +{
> > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > +
> > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > +					      &mk->mk_ino_hash_key);
> > > > 
> > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > that we can access an encrypted directory created on a 32-bit host from
> > > > (e.g.) a 64-bit host.
> > > 
> > > The result is the same regardless of word size and endianness.
> > > siphash_1u64(v, k) is equivalent to:
> > > 
> > > 	__le64 x = cpu_to_le64(v);
> > > 	siphash(&x, 8, k);
> > > 
> > 
> > In the case where you have an (on-storage) inode number that is larger
> > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > wordsize.
> > 
> > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > this means that if you're using inline hardware you're going to end up
> > with a result that won't work correctly across different wordsizes.
> 
> That's only possible if the VFS is truncating the inode number, which would also
> break userspace in lots of ways like making applications think that files are
> hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> 
> The correct fix for that would be to make inode::i_ino 64-bit.
> 

...or just ask the filesystem for the 64-bit inode number via ->getattr
or a new op. You could also just truncate it down to 32 bits or xor the
top and bottom bits together first, etc...

> Note that ext4 and f2fs (currently the only filesystems that support the
> IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> 

Ahh, ok. That explains why it's not been an issue so far. Still, if
you're reworking this code anyway, you might want to consider avoiding
i_ino here.

-- 
Jeff Layton <jlayton@kernel.org>


^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 19:42             ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 19:42 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > +			       const struct fscrypt_master_key *mk)
> > > > > +{
> > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > +
> > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > +					      &mk->mk_ino_hash_key);
> > > > 
> > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > that we can access an encrypted directory created on a 32-bit host from
> > > > (e.g.) a 64-bit host.
> > > 
> > > The result is the same regardless of word size and endianness.
> > > siphash_1u64(v, k) is equivalent to:
> > > 
> > > 	__le64 x = cpu_to_le64(v);
> > > 	siphash(&x, 8, k);
> > > 
> > 
> > In the case where you have an (on-storage) inode number that is larger
> > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > wordsize.
> > 
> > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > this means that if you're using inline hardware you're going to end up
> > with a result that won't work correctly across different wordsizes.
> 
> That's only possible if the VFS is truncating the inode number, which would also
> break userspace in lots of ways like making applications think that files are
> hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> 
> The correct fix for that would be to make inode::i_ino 64-bit.
> 

...or just ask the filesystem for the 64-bit inode number via ->getattr
or a new op. You could also just truncate it down to 32 bits or xor the
top and bottom bits together first, etc...

> Note that ext4 and f2fs (currently the only filesystems that support the
> IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> 

Ahh, ok. That explains why it's not been an issue so far. Still, if
you're reworking this code anyway, you might want to consider avoiding
i_ino here.

-- 
Jeff Layton <jlayton@kernel.org>



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 19:42             ` Jeff Layton
  0 siblings, 0 replies; 45+ messages in thread
From: Jeff Layton @ 2020-08-24 19:42 UTC (permalink / raw)
  To: Eric Biggers
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > +			       const struct fscrypt_master_key *mk)
> > > > > +{
> > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > +
> > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > +					      &mk->mk_ino_hash_key);
> > > > 
> > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > that we can access an encrypted directory created on a 32-bit host from
> > > > (e.g.) a 64-bit host.
> > > 
> > > The result is the same regardless of word size and endianness.
> > > siphash_1u64(v, k) is equivalent to:
> > > 
> > > 	__le64 x = cpu_to_le64(v);
> > > 	siphash(&x, 8, k);
> > > 
> > 
> > In the case where you have an (on-storage) inode number that is larger
> > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > wordsize.
> > 
> > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > this means that if you're using inline hardware you're going to end up
> > with a result that won't work correctly across different wordsizes.
> 
> That's only possible if the VFS is truncating the inode number, which would also
> break userspace in lots of ways like making applications think that files are
> hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> 
> The correct fix for that would be to make inode::i_ino 64-bit.
> 

...or just ask the filesystem for the 64-bit inode number via ->getattr
or a new op. You could also just truncate it down to 32 bits or xor the
top and bottom bits together first, etc...

> Note that ext4 and f2fs (currently the only filesystems that support the
> IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> 

Ahh, ok. That explains why it's not been an issue so far. Still, if
you're reworking this code anyway, you might want to consider avoiding
i_ino here.

-- 
Jeff Layton <jlayton@kernel.org>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
  2020-08-24 19:42             ` [f2fs-dev] " Jeff Layton
  (?)
@ 2020-08-24 20:49               ` Eric Biggers
  -1 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 20:49 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-f2fs-devel, linux-mtd, ceph-devel

On Mon, Aug 24, 2020 at 03:42:59PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > > +			       const struct fscrypt_master_key *mk)
> > > > > > +{
> > > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > > +
> > > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > > +					      &mk->mk_ino_hash_key);
> > > > > 
> > > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > > that we can access an encrypted directory created on a 32-bit host from
> > > > > (e.g.) a 64-bit host.
> > > > 
> > > > The result is the same regardless of word size and endianness.
> > > > siphash_1u64(v, k) is equivalent to:
> > > > 
> > > > 	__le64 x = cpu_to_le64(v);
> > > > 	siphash(&x, 8, k);
> > > > 
> > > 
> > > In the case where you have an (on-storage) inode number that is larger
> > > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > > wordsize.
> > > 
> > > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > > this means that if you're using inline hardware you're going to end up
> > > with a result that won't work correctly across different wordsizes.
> > 
> > That's only possible if the VFS is truncating the inode number, which would also
> > break userspace in lots of ways like making applications think that files are
> > hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> > 
> > The correct fix for that would be to make inode::i_ino 64-bit.
> > 
> 
> ...or just ask the filesystem for the 64-bit inode number via ->getattr
> or a new op. You could also just truncate it down to 32 bits or xor the
> top and bottom bits together first, etc...
> 
> > Note that ext4 and f2fs (currently the only filesystems that support the
> > IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> > 
> 
> Ahh, ok. That explains why it's not been an issue so far. Still, if
> you're reworking this code anyway, you might want to consider avoiding
> i_ino here.

Let's just enforce ino_bits <= 32 for IV_INO_LBLK_32 for now,
like is done for IV_INO_LBLK_64:
https://lkml.kernel.org/r/20200824203841.1707847-1-ebiggers@kernel.org

There's no need to add extra complexity for something that no one wants yet.

(And as mentioned, this won't prevent ceph or other filesystems with 64-bit
inode numbers from adding support for fscrypt, as IV_INO_LBLK_32 support is
optional and has a pretty specific use case.)

- Eric

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [f2fs-dev] [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 20:49               ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 20:49 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 03:42:59PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > > +			       const struct fscrypt_master_key *mk)
> > > > > > +{
> > > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > > +
> > > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > > +					      &mk->mk_ino_hash_key);
> > > > > 
> > > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > > that we can access an encrypted directory created on a 32-bit host from
> > > > > (e.g.) a 64-bit host.
> > > > 
> > > > The result is the same regardless of word size and endianness.
> > > > siphash_1u64(v, k) is equivalent to:
> > > > 
> > > > 	__le64 x = cpu_to_le64(v);
> > > > 	siphash(&x, 8, k);
> > > > 
> > > 
> > > In the case where you have an (on-storage) inode number that is larger
> > > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > > wordsize.
> > > 
> > > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > > this means that if you're using inline hardware you're going to end up
> > > with a result that won't work correctly across different wordsizes.
> > 
> > That's only possible if the VFS is truncating the inode number, which would also
> > break userspace in lots of ways like making applications think that files are
> > hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> > 
> > The correct fix for that would be to make inode::i_ino 64-bit.
> > 
> 
> ...or just ask the filesystem for the 64-bit inode number via ->getattr
> or a new op. You could also just truncate it down to 32 bits or xor the
> top and bottom bits together first, etc...
> 
> > Note that ext4 and f2fs (currently the only filesystems that support the
> > IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> > 
> 
> Ahh, ok. That explains why it's not been an issue so far. Still, if
> you're reworking this code anyway, you might want to consider avoiding
> i_ino here.

Let's just enforce ino_bits <= 32 for IV_INO_LBLK_32 for now,
like is done for IV_INO_LBLK_64:
https://lkml.kernel.org/r/20200824203841.1707847-1-ebiggers@kernel.org

There's no need to add extra complexity for something that no one wants yet.

(And as mentioned, this won't prevent ceph or other filesystems with 64-bit
inode numbers from adding support for fscrypt, as IV_INO_LBLK_32 support is
optional and has a pretty specific use case.)

- Eric


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context()
@ 2020-08-24 20:49               ` Eric Biggers
  0 siblings, 0 replies; 45+ messages in thread
From: Eric Biggers @ 2020-08-24 20:49 UTC (permalink / raw)
  To: Jeff Layton
  Cc: linux-fscrypt, linux-ext4, linux-mtd, ceph-devel, linux-f2fs-devel

On Mon, Aug 24, 2020 at 03:42:59PM -0400, Jeff Layton wrote:
> On Mon, 2020-08-24 at 12:02 -0700, Eric Biggers wrote:
> > On Mon, Aug 24, 2020 at 02:47:07PM -0400, Jeff Layton wrote:
> > > On Mon, 2020-08-24 at 11:21 -0700, Eric Biggers wrote:
> > > > On Mon, Aug 24, 2020 at 12:48:48PM -0400, Jeff Layton wrote:
> > > > > > +void fscrypt_hash_inode_number(struct fscrypt_info *ci,
> > > > > > +			       const struct fscrypt_master_key *mk)
> > > > > > +{
> > > > > > +	WARN_ON(ci->ci_inode->i_ino == 0);
> > > > > > +	WARN_ON(!mk->mk_ino_hash_key_initialized);
> > > > > > +
> > > > > > +	ci->ci_hashed_ino = (u32)siphash_1u64(ci->ci_inode->i_ino,
> > > > > > +					      &mk->mk_ino_hash_key);
> > > > > 
> > > > > i_ino is an unsigned long. Will this produce a consistent results on
> > > > > arches with 32 and 64 bit long values? I think it'd be nice to ensure
> > > > > that we can access an encrypted directory created on a 32-bit host from
> > > > > (e.g.) a 64-bit host.
> > > > 
> > > > The result is the same regardless of word size and endianness.
> > > > siphash_1u64(v, k) is equivalent to:
> > > > 
> > > > 	__le64 x = cpu_to_le64(v);
> > > > 	siphash(&x, 8, k);
> > > > 
> > > 
> > > In the case where you have an (on-storage) inode number that is larger
> > > than 2^32, x will almost certainly be different on a 32 vs. 64-bit
> > > wordsize.
> > > 
> > > On the box with the 32-bit wordsize, you'll end up promoting i_ino to a
> > > 64-bit word and the upper 32 bits will be zeroed out. So it seems like
> > > this means that if you're using inline hardware you're going to end up
> > > with a result that won't work correctly across different wordsizes.
> > 
> > That's only possible if the VFS is truncating the inode number, which would also
> > break userspace in lots of ways like making applications think that files are
> > hard-linked together when they aren't.  Also, IV_INO_LBLK_64 would break.
> > 
> > The correct fix for that would be to make inode::i_ino 64-bit.
> > 
> 
> ...or just ask the filesystem for the 64-bit inode number via ->getattr
> or a new op. You could also just truncate it down to 32 bits or xor the
> top and bottom bits together first, etc...
> 
> > Note that ext4 and f2fs (currently the only filesystems that support the
> > IV_INO_LBLK_* flags) only support 32-bit inode numbers.
> > 
> 
> Ahh, ok. That explains why it's not been an issue so far. Still, if
> you're reworking this code anyway, you might want to consider avoiding
> i_ino here.

Let's just enforce ino_bits <= 32 for IV_INO_LBLK_32 for now,
like is done for IV_INO_LBLK_64:
https://lkml.kernel.org/r/20200824203841.1707847-1-ebiggers@kernel.org

There's no need to add extra complexity for something that no one wants yet.

(And as mentioned, this won't prevent ceph or other filesystems with 64-bit
inode numbers from adding support for fscrypt, as IV_INO_LBLK_32 support is
optional and has a pretty specific use case.)

- Eric

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2020-08-24 20:49 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-24  6:17 [RFC PATCH 0/8] fscrypt: avoid GFP_NOFS-unsafe key setup during transaction Eric Biggers
2020-08-24  6:17 ` Eric Biggers
2020-08-24  6:17 ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 1/8] fscrypt: add fscrypt_prepare_new_inode() and fscrypt_set_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24 16:48   ` Jeff Layton
2020-08-24 16:48     ` Jeff Layton
2020-08-24 16:48     ` [f2fs-dev] " Jeff Layton
2020-08-24 18:21     ` Eric Biggers
2020-08-24 18:21       ` Eric Biggers
2020-08-24 18:21       ` [f2fs-dev] " Eric Biggers
2020-08-24 18:47       ` Jeff Layton
2020-08-24 18:47         ` Jeff Layton
2020-08-24 18:47         ` [f2fs-dev] " Jeff Layton
2020-08-24 19:02         ` Eric Biggers
2020-08-24 19:02           ` Eric Biggers
2020-08-24 19:02           ` [f2fs-dev] " Eric Biggers
2020-08-24 19:42           ` Jeff Layton
2020-08-24 19:42             ` Jeff Layton
2020-08-24 19:42             ` [f2fs-dev] " Jeff Layton
2020-08-24 20:49             ` Eric Biggers
2020-08-24 20:49               ` Eric Biggers
2020-08-24 20:49               ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 2/8] ext4: factor out ext4_xattr_credits_for_new_inode() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 3/8] ext4: remove some #ifdefs in ext4_xattr_credits_for_new_inode() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 4/8] ext4: use fscrypt_prepare_new_inode() and fscrypt_set_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 5/8] f2fs: " Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 6/8] ubifs: " Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 7/8] fscrypt: remove fscrypt_inherit_context() Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers
2020-08-24  6:17 ` [RFC PATCH 8/8] fscrypt: stop pretending that key setup is nofs-safe Eric Biggers
2020-08-24  6:17   ` Eric Biggers
2020-08-24  6:17   ` [f2fs-dev] " Eric Biggers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.