Skip to contentSkip to conversation

ceph: fscrypt: fix atomic open bug for encrypted directories

PATCH In review Luís Henriques 4 participants 10 messages Mar 16, 2023 – Mar 21, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover ceph: fscrypt: fix atomic open bug for encrypted directories

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

	if (IS_ENCRYPTED(dir)) {
		set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
		if (!fscrypt_has_encryption_key(dir)) {
			spin_lock(&dentry->d_lock);
			dentry->d_flags |= DCACHE_NOKEY_NAME;
			spin_unlock(&dentry->d_lock);
		}
	}

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()
ceph: switch ceph_open() to use new fscrypt helper
ceph: switch ceph_open_atomic() to use the new fscrypt helper

 fs/ceph/dir.c           | 13 +++++++------
 fs/ceph/file.c          |  8 +++-----
 fs/crypto/hooks.c       | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/fscrypt.h |  7 +++++++
 4 files changed, 54 insertions(+), 11 deletions(-)
#1 lore raw

PATCH v3 1/3 fscrypt: new helper function - fscrypt_prepare_lookup_partial()

This patch introduces a new helper function which can be used both in lookups and in atomic_open operations by filesystems that want to handle filename encryption and no-key dentries themselves.

The reason for this function to be used in atomic open is that this operation can act as a lookup if handed a dentry that is negative. And in this case we may need to set DCACHE_NOKEY_NAME.

---
 fs/crypto/hooks.c       | 37 +++++++++++++++++++++++++++++++++++++
 include/linux/fscrypt.h |  7 +++++++
 2 files changed, 44 insertions(+)
fs/crypto/hooks.c +37 -0
index 7b8c5a1104b5..4bebdeaadc52 100644
@@ -117,6 +117,43 @@ int __fscrypt_prepare_readdir(struct inode *dir)
 }
 EXPORT_SYMBOL_GPL(__fscrypt_prepare_readdir);
 
+/**
+ * fscrypt_prepare_lookup_partial() - prepare lookup without filenames handling
+ * @dir: inode of parent directory
+ * @dentry: dentry to lookup
+ *
+ * This function can be used by filesystems that want/need to handle filename
+ * encryption and no-key name encoding themselves, and thus aren't able to use
+ * function fscrypt_prepare_lookup().
+
+ * This helper can be called from lookup and atomic open operations.  It will
+ * try to set the encryption info on the in @dir if the key is available and, if
+ * it isn't, it will also set the @dentry as non-key.
+ *
+ * The reason it needs to get the encryption info before checking if the
+ * directory has the encryption key is because the key may be available but the
+ * encryption info isn't yet set (maybe due to a drop_caches).  The regular open
+ * path will use fscrypt_prepare_lookup(), but if a filesystem can't use this
+ * function (because it handles the filename encryption and no-key dentries) the
+ * atomic_open requires a different approach.
+ *
+ * Return: 0 on success, or an error code if fscrypt_get_encryption_info()
+ * 	   fails.
+ */
+int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry)
+{
+	int err = fscrypt_get_encryption_info(dir, true);
+
+	if (!err && !fscrypt_has_encryption_key(dir)) {
+		spin_lock(&dentry->d_lock);
+		dentry->d_flags |= DCACHE_NOKEY_NAME;
+		spin_unlock(&dentry->d_lock);
+	}
+
+	return err;
+}
+EXPORT_SYMBOL_GPL(fscrypt_prepare_lookup_partial);
+
 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr)
 {
 	if (attr->ia_valid & ATTR_SIZE)
include/linux/fscrypt.h +7 -0
index 4f5f8a651213..d9a94cd56cda 100644
@@ -362,6 +362,7 @@ int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
 			     struct fscrypt_name *fname);
 int __fscrypt_prepare_readdir(struct inode *dir);
+int fscrypt_prepare_lookup_partial(struct inode *dir, struct dentry *dentry);
 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
 int fscrypt_prepare_setflags(struct inode *inode,
 			     unsigned int oldflags, unsigned int flags);
@@ -688,6 +689,12 @@ static inline int __fscrypt_prepare_readdir(struct inode *dir)
 	return -EOPNOTSUPP;
 }
 
+static inline int fscrypt_prepare_lookup_partial(struct inode *dir,
+						 struct dentry *dentry)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
 					    struct iattr *attr)
 {
#2 lore raw

PATCH v3 2/3 ceph: switch ceph_open() to use new fscrypt helper

In reply to Luís Henriques

Instead of setting the no-key dentry in ceph_lookup(), use the new fscrypt_prepare_lookup_partial() helper. We still need to mark the directory as incomplete if the directory was just unlocked.

---
 fs/ceph/dir.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/ceph/dir.c +7 -6
index d8cc6e9d5351..836bc695e2e0 100644
@@ -784,14 +784,15 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 		return ERR_PTR(-ENAMETOOLONG);
 
 	if (IS_ENCRYPTED(dir)) {
-		err = ceph_fscrypt_prepare_readdir(dir);
+		bool had_key = fscrypt_has_encryption_key(dir);
+
+		err = fscrypt_prepare_lookup_partial(dir, dentry);
 		if (err < 0)
 			return ERR_PTR(err);
-		if (!fscrypt_has_encryption_key(dir)) {
-			spin_lock(&dentry->d_lock);
-			dentry->d_flags |= DCACHE_NOKEY_NAME;
-			spin_unlock(&dentry->d_lock);
-		}
+
+		/* mark directory as incomplete if it has been unlocked */
+		if (!had_key && fscrypt_has_encryption_key(dir))
+			ceph_dir_clear_complete(dir);
 	}
 
 	/* can we conclude ENOENT locally? */
#3 lore raw

PATCH v3 3/3 ceph: switch ceph_open_atomic() to use the new fscrypt helper

In reply to Luís Henriques

Switch ceph_atomic_open() to use new fscrypt helper function fscrypt_prepare_lookup_partial(). This fixes a bug in the atomic open operation where a dentry is incorrectly set with DCACHE_NOKEY_NAME when 'dir' has been evicted but the key is still available (for example, where there's a drop_caches).

---
 fs/ceph/file.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
fs/ceph/file.c +3 -5
index dee3b445f415..2448d0f1a9ea 100644
@@ -795,11 +795,9 @@ int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
 	ihold(dir);
 	if (IS_ENCRYPTED(dir)) {
 		set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
-		if (!fscrypt_has_encryption_key(dir)) {
-			spin_lock(&dentry->d_lock);
-			dentry->d_flags |= DCACHE_NOKEY_NAME;
-			spin_unlock(&dentry->d_lock);
-		}
+		err = fscrypt_prepare_lookup_partial(dir, dentry);
+		if (err < 0)
+			goto out_req;
 	}
 
 	if (flags & O_CREAT) {
Tested-by on the v3 series
#4 lore raw
In reply to Luís Henriques
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

	if (IS_ENCRYPTED(dir)) {
		set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
		if (!fscrypt_has_encryption_key(dir)) {
			spin_lock(&dentry->d_lock);
			dentry->d_flags |= DCACHE_NOKEY_NAME;
			spin_unlock(&dentry->d_lock);
		}
	}

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

This series looks good to me.

And I have run the test locally and worked well.

Quoted 2 lines

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()

Eric,

If possible I we can pick this together to ceph repo and need your ack about this. Or you can pick it to the crypto repo then please feel free to add:

Thanks

- Xiubo

Quoted 9 lines

ceph: switch ceph_open() to use new fscrypt helper
ceph: switch ceph_open_atomic() to use the new fscrypt helper

  fs/ceph/dir.c           | 13 +++++++------
  fs/ceph/file.c          |  8 +++-----
  fs/crypto/hooks.c       | 37 +++++++++++++++++++++++++++++++++++++
  include/linux/fscrypt.h |  7 +++++++
  4 files changed, 54 insertions(+), 11 deletions(-)
Signature · 6 lines
Best Regards,

Xiubo Li (李秀波)

Email: [email protected]/[email protected]
Slack: @Xiubo Li
maintainer
#5 lore raw
Quoted 72 lines · Xiubo Li
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

      if (IS_ENCRYPTED(dir)) {
              set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
              if (!fscrypt_has_encryption_key(dir)) {
                      spin_lock(&dentry->d_lock);
                      dentry->d_flags |= DCACHE_NOKEY_NAME;
                      spin_unlock(&dentry->d_lock);
              }
      }

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

This series looks good to me.

And I have run the test locally and worked well.

Quoted 2 lines

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()

Quoted 8 lines

Eric,

If possible I we can pick this together to ceph repo and need your ack about this. Or you can pick it to the crypto repo then please feel free to add:

<[email protected]>

I would prefer the fscrypt helper to go through the fscrypt tree.

Thanks,

Ilya

#6 lore raw
Quoted 72 lines · Ilya Dryomov
Quoted 71 lines · Xiubo Li
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

       if (IS_ENCRYPTED(dir)) {
               set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
               if (!fscrypt_has_encryption_key(dir)) {
                       spin_lock(&dentry->d_lock);
                       dentry->d_flags |= DCACHE_NOKEY_NAME;
                       spin_unlock(&dentry->d_lock);
               }
       }

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

This series looks good to me.

And I have run the test locally and worked well.

Quoted 2 lines

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()

Eric,

Quoted 7 lines
Quoted 6 lines

If possible I we can pick this together to ceph repo and need your ack about this. Or you can pick it to the crypto repo then please feel free to add:

<[email protected]>

I would prefer the fscrypt helper to go through the fscrypt tree.

Sure. This also LGTM.

Thanks

- Xiubo

Quoted 4 lines

Thanks,

Ilya

maintainer
#7 lore raw
Quoted 78 lines · Xiubo Li
Quoted 76 lines · Ilya Dryomov
Quoted 75 lines · Xiubo Li
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

       if (IS_ENCRYPTED(dir)) {
               set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
               if (!fscrypt_has_encryption_key(dir)) {
                       spin_lock(&dentry->d_lock);
                       dentry->d_flags |= DCACHE_NOKEY_NAME;
                       spin_unlock(&dentry->d_lock);
               }
       }

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

This series looks good to me.

And I have run the test locally and worked well.

Quoted 2 lines

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()

Eric,

If possible I we can pick this together to ceph repo and need your ack about this. Or you can pick it to the crypto repo then please feel free to add:

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

<[email protected]>

I would prefer the fscrypt helper to go through the fscrypt tree.

Sure. This also LGTM.

Thanks

I've applied it to
https://git.kernel.org/pub/scm/fs/fscrypt/linux.git/log/?h=for-next

But I ended up reworking the comment a bit and moving the function to be just below __fscrypt_prepare_lookup(). So I sent out v4 that matches what I applied.

BTW, I'm wondering if anyone has had any thoughts about the race condition I described at https://lore.kernel.org/r/[email protected]/. In particular, I'm wondering whether this helper function will need to be changed or not. Maybe not, because ceph could look at DCACHE_NOKEY_NAME to determine whether the name should be treated as a no-key name or not, instead of checking fscrypt_has_encryption_key() again (as I think it is doing currently)?

- Eric

#8 lore raw
Quoted 85 lines · Eric Biggers
Quoted 84 lines · Xiubo Li
Quoted 72 lines · Ilya Dryomov
Quoted 71 lines · Xiubo Li
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

       if (IS_ENCRYPTED(dir)) {
               set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
               if (!fscrypt_has_encryption_key(dir)) {
                       spin_lock(&dentry->d_lock);
                       dentry->d_flags |= DCACHE_NOKEY_NAME;
                       spin_unlock(&dentry->d_lock);
               }
       }

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

This series looks good to me.

And I have run the test locally and worked well.

Quoted 2 lines

Luís Henriques (3):
fscrypt: new helper function - fscrypt_prepare_lookup_partial()

Eric,

Quoted 7 lines
Quoted 6 lines

If possible I we can pick this together to ceph repo and need your ack about this. Or you can pick it to the crypto repo then please feel free to add:

<[email protected]>

I would prefer the fscrypt helper to go through the fscrypt tree.

Sure. This also LGTM.

Quoted 8 lines
Quoted 2 lines

Thanks

I've applied it to
https://git.kernel.org/pub/scm/fs/fscrypt/linux.git/log/?h=for-next

But I ended up reworking the comment a bit and moving the function to be just below __fscrypt_prepare_lookup(). So I sent out v4 that matches what I applied.

Awesome, thanks a lot, Eric.

Quoted 6 lines

BTW, I'm wondering if anyone has had any thoughts about the race condition I described at https://lore.kernel.org/r/[email protected]/. In particular, I'm wondering whether this helper function will need to be changed or not. Maybe not, because ceph could look at DCACHE_NOKEY_NAME to determine whether the name should be treated as a no-key name or not, instead of checking fscrypt_has_encryption_key() again (as I think it is doing currently)?

I started looking into that but, to be honest, I haven't yet reached any conclusion. It looks like the ceph code that handles filenames *may* have this race too (I'm looking at ceph_fill_trace()) but I'm still not 100% sure. In any case, I think that an eventual fix for this race (if it does indeed exist!) will likely be restricted to the ceph code and won't touch the generic fscrypt code. But I'm still looking...

Cheers,

Signature · 1 line
Luís
#9 lore raw
In reply to Xiubo Li
Quoted 62 lines · Xiubo Li
Quoted 53 lines · Luís Henriques

Hi!

I started seeing fstest generic/123 failing in ceph fscrypt, when running it with 'test_dummy_encryption'. This test is quite simple:

1. Creates a directory with write permissions for root only
2. Writes into a file in that directory
3. Uses 'su' to try to modify that file as a different user, and gets -EPERM

All the test steps succeed, but the test fails to cleanup: 'rm -rf <dir>' will fail with -ENOTEMPTY. 'strace' shows that calling unlinkat() to remove the file got a -ENOENT and then -ENOTEMPTY for the directory.

This is because 'su' does a drop_caches ('su (874): drop_caches: 2' in dmesg), and ceph's atomic open will do:

	if (IS_ENCRYPTED(dir)) {
		set_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags);
		if (!fscrypt_has_encryption_key(dir)) {
			spin_lock(&dentry->d_lock);
			dentry->d_flags |= DCACHE_NOKEY_NAME;
			spin_unlock(&dentry->d_lock);
		}
	}

Although 'dir' has the encryption key available, fscrypt_has_encryption_key() will return 'false' because fscrypt info isn't yet set after the cache cleanup.

The first patch will add a new helper for the atomic_open that will force the fscrypt info to be loaded into an inode that has been evicted recently but for which the key is still available.

The second patch switches ceph atomic_open to use the new fscrypt helper.

Cheers,
--
Luís

Changes since v2:
- Make helper more generic and to be used both in lookup and atomic open operations
- Modify ceph_lookup (patch 0002) and ceph_atomic_open (patch 0003) to use the new helper

Changes since v1:
- Dropped IS_ENCRYPTED() from helper function because kerneldoc says already that it applies to encrypted directories and, most importantly, because it would introduce a different behaviour for CONFIG_FS_ENCRYPTION and !CONFIG_FS_ENCRYPTION.
- Rephrased helper kerneldoc

Quoted 7 lines

Changes since initial RFC (after Eric's review):
- Added kerneldoc comments to the new fscrypt helper
- Dropped '__' from helper name (now fscrypt_prepare_atomic_open())
- Added IS_ENCRYPTED() check in helper
- DCACHE_NOKEY_NAME is not set if fscrypt_get_encryption_info() returns an error
- Fixed helper for !CONFIG_FS_ENCRYPTION (now defined 'static inline')

Quoted 3 lines

This series looks good to me.

And I have run the test locally and worked well.

Awesome, thanks a lot Xiubo. I've been testing it locally as well and I haven't observed any breakage either.

Cheers,

Signature · 1 line
Luís