Skip to contentSkip to conversation

ceph: make sure directories aren't complete after setting crypt context

PATCH Superseded Luís Henriques 2 participants 8 messages Nov 16, 2022 – Nov 21, 2022 Ceph client

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ceph: make sure directories aren't complete after setting crypt context

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8  u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or directory
 fs/ceph/crypto.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/ceph/crypto.c +4 -0
index 35a2ccfe6899..dc1557967032 100644
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
 		return -ERANGE;
 
 	memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+	/* Directory isn't complete anymore */
+	if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+		__ceph_dir_clear_complete(ci);
 	return ctxlen;
 }
 
#1 lore raw
Quoted 27 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8  u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or directory
  fs/ceph/crypto.c | 4 ++++
  1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len) · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode, void *ctx, size_t len)
  		return -ERANGE;
 
  	memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+	/* Directory isn't complete anymore */
+	if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+		__ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Thanks!

- Xiubo

Quoted 4 lines
fs/ceph/crypto.c +0 -0
  	return ctxlen;
  }
 
#2 lore raw
Quoted 39 lines · Xiubo Li
Quoted 27 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or directory

  fs/ceph/crypto.c | 4 ++++

  1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode 
 *inode, void *ctx, size_t len)
           return -ERANGE;
         memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Quoted 8 lines

Good catch!

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 5 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

Thanks!

- Xiubo

Quoted 7 lines

Thanks!

- Xiubo

Quoted 3 lines
fs/ceph/crypto.c +0 -0
       return ctxlen;
   }
 
#3 lore raw
Quoted 60 lines · Xiubo Li
Quoted 43 lines · Xiubo Li
Quoted 29 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8
u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or
directory

  fs/ceph/crypto.c | 4 ++++

  1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode,
 void *ctx, size_t len)
           return -ERANGE;
         memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

Quoted 8 lines

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 7 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Quoted 7 lines

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

OK, that makes sense and to be honest I thought that there should be another place for doing this. Unfortunately, I didn't found it: in the test case I have the fscrypt_prepare_readdir() isn't called:

   # cd /cephfs
   # ls mydir
   nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
   # ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
   Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw

At this point readdir was executed, of course. And __ceph_dir_set_complete() is also used to indicate that we have the full contents. However, executing the following commands won't result in any new readdir():

   # fscrypt unlock mydir
   # touch /mnt/test/mydir/mysubdir/file

and since the encryption key is set at the sb level, I couldn't find a way to detect changes in inode->i_crypt_info. ceph_d_revalidate() is invoked but at that point I don't thing we have a way to know what is changing.

Any ideas?

Cheers,

Signature · 1 line
Luís
Quoted 14 lines

Thanks!

- Xiubo

Quoted 7 lines

Thanks!

- Xiubo

Quoted 3 lines
fs/ceph/crypto.c +0 -0
       return ctxlen;
   }
 
#4 lore raw
Quoted 81 lines · Luís Henriques
Quoted 57 lines · Xiubo Li
Quoted 41 lines · Xiubo Li
Quoted 29 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8
u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or
directory
   fs/ceph/crypto.c | 4 ++++
   1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode,
 void *ctx, size_t len)
           return -ERANGE;
         memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

Quoted 8 lines

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 6 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Quoted 7 lines

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

OK, that makes sense and to be honest I thought that there should be another place for doing this. Unfortunately, I didn't found it: in the test case I have the fscrypt_prepare_readdir() isn't called:

    # cd /cephfs
    # ls mydir
    nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
    # ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
    Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw

At this point readdir was executed, of course. And __ceph_dir_set_complete() is also used to indicate that we have the full contents. However, executing the following commands won't result in any new readdir():

Quoted 8 lines
    # fscrypt unlock mydir
    # touch /mnt/test/mydir/mysubdir/file

and since the encryption key is set at the sb level, I couldn't find a way to detect changes in inode->i_crypt_info. ceph_d_revalidate() is invoked but at that point I don't thing we have a way to know what is changing.

Any ideas?

# ls mydir/
zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU
# ls mydir/zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU/
24iNa8ICYc6nZZIkL,4n1sOtp9KiaTGtpq8PCZDQ6LU ivG4rQYzg6YENagbTZaV2kh2sVouEdlkmBZPfgVDl48

I have added the debug logs in all the places just like:

@@ -784,7 +786,9 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, return ERR_PTR(-ENAMETOOLONG);

        if (IS_ENCRYPTED(dir)) {
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__, fscrypt_has_encryption_key(dir)); err = __fscrypt_prepare_readdir(dir);
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__, fscrypt_has_encryption_key(dir)); if (err)
                        return ERR_PTR(err);
                if (!fscrypt_has_encryption_key(dir)) {

# fscrypt unlock mydir
Enter custom passphrase for protector "l":
"mydir" is now unlocked and ready for use.

And after "mydir/" was unlocked and then when doing:

# touch mydir/dir/file2

And when lookup the "dir/" dentry I can see the "mydir/" encryption key changed:

709 <7>[79125.023676] ceph:  __ceph_caps_issued_mask ino 0x10000000004 cap 000000004dc11892 issued pAsLsXsFs (mask As) 710 <7>[79125.023687] ceph:  __touch_cap 000000007071b095 cap 000000004dc11892 mds0
711 <7>[79125.023823] ceph:  lookup 000000007071b095 dentry 00000000f97501b2 'dir'
712 <4>[79125.023838] ceph_lookup fscrypt_has_encryption_key(dir): 0 ======
713 <4>[79125.024186] ceph_lookup fscrypt_has_encryption_key(dir): 1 ======
714 <7>[79125.024194] ceph:   dir 000000007071b095 flags are 0x0
715 <7>[79125.024269] ceph:  do_request on 00000000a93fafef

I am thinking could we just make __fscrypt_prepare_readdir(), which will return 0 when the key is already set or successfully set, to return 1 instead of 0 to mark that the key changed ?

Thanks!

- Xiubo

Quoted 2 lines

Cheers,

#5 lore raw
Quoted 135 lines · Xiubo Li
Quoted 81 lines · Luís Henriques
Quoted 57 lines · Xiubo Li
Quoted 41 lines · Xiubo Li
Quoted 29 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8
u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or
directory
   fs/ceph/crypto.c | 4 ++++
   1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode,
 void *ctx, size_t len)
           return -ERANGE;
         memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

Quoted 8 lines

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 6 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Quoted 7 lines

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

OK, that makes sense and to be honest I thought that there should be another place for doing this. Unfortunately, I didn't found it: in the test case I have the fscrypt_prepare_readdir() isn't called:

    # cd /cephfs
    # ls mydir
    nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
    # ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
    Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw

At this point readdir was executed, of course. And __ceph_dir_set_complete() is also used to indicate that we have the full contents. However, executing the following commands won't result in any new readdir():

Quoted 8 lines
    # fscrypt unlock mydir
    # touch /mnt/test/mydir/mysubdir/file

and since the encryption key is set at the sb level, I couldn't find a way to detect changes in inode->i_crypt_info. ceph_d_revalidate() is invoked but at that point I don't thing we have a way to know what is changing.

Any ideas?

# ls mydir/
zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU
# ls mydir/zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU/
24iNa8ICYc6nZZIkL,4n1sOtp9KiaTGtpq8PCZDQ6LU
ivG4rQYzg6YENagbTZaV2kh2sVouEdlkmBZPfgVDl48

I have added the debug logs in all the places just like:

(quoted diff) +2 -0
@@ -784,7 +786,9 @@ static struct dentry *ceph_lookup(struct inode *dir, struct
 dentry *dentry,
                 return ERR_PTR(-ENAMETOOLONG);
 
         if (IS_ENCRYPTED(dir)) {
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));
                 err = __fscrypt_prepare_readdir(dir);
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));

                if (err)
                        return ERR_PTR(err); if (!fscrypt_has_encryption_key(dir)) {

# fscrypt unlock mydir
Enter custom passphrase for protector "l":
"mydir" is now unlocked and ready for use.

And after "mydir/" was unlocked and then when doing:

# touch mydir/dir/file2

And when lookup the "dir/" dentry I can see the "mydir/" encryption key changed:

709 <7>[79125.023676] ceph:  __ceph_caps_issued_mask ino 0x10000000004 cap 000000004dc11892 issued pAsLsXsFs (mask As)
710 <7>[79125.023687] ceph:  __touch_cap 000000007071b095 cap 000000004dc11892 mds0
711 <7>[79125.023823] ceph:  lookup 000000007071b095 dentry 00000000f97501b2 'dir'
712 <4>[79125.023838] ceph_lookup fscrypt_has_encryption_key(dir): 0 ====== 713 <4>[79125.024186] ceph_lookup fscrypt_has_encryption_key(dir): 1 ====== 714 <7>[79125.024194] ceph:   dir 000000007071b095 flags are 0x0 715 <7>[79125.024269] ceph:  do_request on 00000000a93fafef

Quoted 3 lines

I am thinking could we just make __fscrypt_prepare_readdir(), which will return 0 when the key is already set or successfully set, to return 1 instead of 0 to mark that the key changed ?

OK, I see what you mean. Thanks. What about simply detect this change here instead of changing __fscrypt_prepare_readdir() semantics? Because I think that would require changes in several other places, including other filesystems.

What about something like this?

fs/ceph/dir.c +4 -0
index edc2bf0aab83..499ec75d2496 100644
@@ -784,6 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 		return ERR_PTR(-ENAMETOOLONG);
 
 	if (IS_ENCRYPTED(dir)) {
+		bool had_key = fscrypt_has_encryption_key(dir);
+
 		err = __fscrypt_prepare_readdir(dir);
 		if (err)
 			return ERR_PTR(err);
@@ -791,6 +793,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
 			spin_lock(&dentry->d_lock);
 			dentry->d_flags |= DCACHE_NOKEY_NAME;
 			spin_unlock(&dentry->d_lock);
+		} else if (!had_key) {
+			__ceph_dir_clear_complete(ceph_inode(dir));
 		}
 	}
 

Cheers,

Signature · 1 line
Luís
#6 lore raw
Quoted 160 lines · Luís Henriques
Quoted 134 lines · Xiubo Li
Quoted 81 lines · Luís Henriques
Quoted 57 lines · Xiubo Li
Quoted 41 lines · Xiubo Li
Quoted 29 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8
u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or
directory
    fs/ceph/crypto.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode,
 void *ctx, size_t len)
            return -ERANGE;
          memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

Quoted 8 lines

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 6 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Quoted 7 lines

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

OK, that makes sense and to be honest I thought that there should be another place for doing this. Unfortunately, I didn't found it: in the test case I have the fscrypt_prepare_readdir() isn't called:

     # cd /cephfs
     # ls mydir
     nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
     # ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
     Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw

At this point readdir was executed, of course. And __ceph_dir_set_complete() is also used to indicate that we have the full contents. However, executing the following commands won't result in any new readdir():

Quoted 8 lines
     # fscrypt unlock mydir
     # touch /mnt/test/mydir/mysubdir/file

and since the encryption key is set at the sb level, I couldn't find a way to detect changes in inode->i_crypt_info. ceph_d_revalidate() is invoked but at that point I don't thing we have a way to know what is changing.

Any ideas?

# ls mydir/
zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU
# ls mydir/zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU/
24iNa8ICYc6nZZIkL,4n1sOtp9KiaTGtpq8PCZDQ6LU
ivG4rQYzg6YENagbTZaV2kh2sVouEdlkmBZPfgVDl48

I have added the debug logs in all the places just like:

(quoted diff) +2 -0
@@ -784,7 +786,9 @@ static struct dentry *ceph_lookup(struct inode *dir, struct
 dentry *dentry,
                 return ERR_PTR(-ENAMETOOLONG);
 
         if (IS_ENCRYPTED(dir)) {
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));
                 err = __fscrypt_prepare_readdir(dir);
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));

                if (err)
                        return ERR_PTR(err); if (!fscrypt_has_encryption_key(dir)) {

# fscrypt unlock mydir
Enter custom passphrase for protector "l":
"mydir" is now unlocked and ready for use.

And after "mydir/" was unlocked and then when doing:

# touch mydir/dir/file2

And when lookup the "dir/" dentry I can see the "mydir/" encryption key changed:

709 <7>[79125.023676] ceph:  __ceph_caps_issued_mask ino 0x10000000004 cap 000000004dc11892 issued pAsLsXsFs (mask As)
710 <7>[79125.023687] ceph:  __touch_cap 000000007071b095 cap 000000004dc11892 mds0
711 <7>[79125.023823] ceph:  lookup 000000007071b095 dentry 00000000f97501b2 'dir'
712 <4>[79125.023838] ceph_lookup fscrypt_has_encryption_key(dir): 0 ====== 713 <4>[79125.024186] ceph_lookup fscrypt_has_encryption_key(dir): 1 ====== 714 <7>[79125.024194] ceph:   dir 000000007071b095 flags are 0x0 715 <7>[79125.024269] ceph:  do_request on 00000000a93fafef

Quoted 3 lines

I am thinking could we just make __fscrypt_prepare_readdir(), which will return 0 when the key is already set or successfully set, to return 1 instead of 0 to mark that the key changed ?

OK, I see what you mean. Thanks. What about simply detect this change here instead of changing __fscrypt_prepare_readdir() semantics? Because I think that would require changes in several other places, including other filesystems.

What about something like this?

fs/ceph/dir.c +2 -0
index edc2bf0aab83..499ec75d2496 100644
@@ -784,6 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
 
  	if (IS_ENCRYPTED(dir)) {
+		bool had_key = fscrypt_has_encryption_key(dir);
+
  		err = __fscrypt_prepare_readdir(dir);
  		if (err)
  			return ERR_PTR(err);

fs/ceph/dir.c static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, · line 800

Quoted 8 lines
fs/ceph/dir.c +2 -0
@@ -791,6 +793,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  			spin_lock(&dentry->d_lock);
  			dentry->d_flags |= DCACHE_NOKEY_NAME;
  			spin_unlock(&dentry->d_lock);
+		} else if (!had_key) {
+			__ceph_dir_clear_complete(ceph_inode(dir));
  		}
  	}
 

Yeah. Could we just add one helper, such as:

bool ceph_fscrypt_prepare_readdir(dir)

{

    bool had_key = fscrypt_has_encryption_key(dir);

    err = __fscrypt_prepare_readdir(dir);

    if (!err && !had_key || err = -ENOKEY && had_key) {

        return true;   // key changed

    }

    return false;  // key not changed

}

Thanks!

- Xiubo

Quoted 1 line

Cheers,

#7 lore raw
Quoted 162 lines · Xiubo Li
Quoted 161 lines · Luís Henriques
Quoted 134 lines · Xiubo Li
Quoted 81 lines · Luís Henriques
Quoted 57 lines · Xiubo Li
Quoted 41 lines · Xiubo Li
Quoted 29 lines · Luís Henriques

When setting a directory's crypt context, __ceph_dir_clear_complete() needs to be used otherwise, if it was complete before, any old dentry that's still around will be valid.

---
Hi!

Here's a simple way to trigger the bug this patch is fixing:

# cd /cephfs
# ls mydir
nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
# ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8
u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw
# fscrypt unlock mydir
# touch /mnt/test/mydir/mysubdir/file
touch: cannot touch '/mnt/test/mydir/mysubdir/file': No such file or
directory
    fs/ceph/crypto.c | 4 ++++
    1 file changed, 4 insertions(+)
fs/ceph/crypto.c +0 -0
index 35a2ccfe6899..dc1557967032 100644

fs/ceph/crypto.c static int ceph_crypt_get_context(struct inode *inode, · line 93

Quoted 7 lines
fs/ceph/crypto.c +4 -0
@@ -87,6 +87,10 @@ static int ceph_crypt_get_context(struct inode *inode,
 void *ctx, size_t len)
            return -ERANGE;
          memcpy(ctx, cfa->cfa_blob, ctxlen);
+
+    /* Directory isn't complete anymore */
+    if (S_ISDIR(inode->i_mode) && __ceph_dir_is_complete(ci))
+        __ceph_dir_clear_complete(ci);

Hi Luis,

Good catch!

Quoted 8 lines

BTW, why do this in the ceph_crypt_get_context() ? As my understanding is that we should mark 'mydir' as incomplete when unlocking it. While as I remembered the unlock operation will do:

Step1: get_encpolicy via 'mydir' as ctx
Step2: rm_enckey of ctx from the superblock

Sorry, it should be add_enckey.

Quoted 6 lines

Since I am still running the test cases for the file lock patches, so I didn't catch logs to confirm the above steps yet.

If I am right IMO then we should mark the dir as incomplete in the Step2 instead, because for non-unlock operations they may also do the Step1.

Quoted 7 lines

Your patch will work. But probably we should do this just around __fscrypt_prepare_readdir() or fscrypt_prepare_readdir() instead ? We need to detect that once the 'inode->i_crypt_info' changed then mark the dir as incomplete.

For now for the lock operation it will evict the inode, which will help do this for us already. But for unlock case, we need to handle it by ourself.

OK, that makes sense and to be honest I thought that there should be another place for doing this. Unfortunately, I didn't found it: in the test case I have the fscrypt_prepare_readdir() isn't called:

     # cd /cephfs
     # ls mydir
     nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0
     # ls mydir/nKRhofOAVNsAwVLvDw7a0c9ypsjbZfK3n0Npnmni6j0/
     Cyuer5xT+kBlEPgtwAqSj0WK2taEljP5vHZ,D8VXCJ8 u+46b2XVCt7Obpz0gznZyNLRj79Q2l4KmkwbKOzdQKw

At this point readdir was executed, of course. And __ceph_dir_set_complete() is also used to indicate that we have the full contents. However, executing the following commands won't result in any new readdir():

Quoted 8 lines
     # fscrypt unlock mydir
     # touch /mnt/test/mydir/mysubdir/file

and since the encryption key is set at the sb level, I couldn't find a way to detect changes in inode->i_crypt_info. ceph_d_revalidate() is invoked but at that point I don't thing we have a way to know what is changing.

Any ideas?

# ls mydir/
zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU
# ls mydir/zy94Zt01M90xwYq+nxJsEvea+HYq49mqVgrUBkYrJAU/
24iNa8ICYc6nZZIkL,4n1sOtp9KiaTGtpq8PCZDQ6LU
ivG4rQYzg6YENagbTZaV2kh2sVouEdlkmBZPfgVDl48

I have added the debug logs in all the places just like:

(quoted diff) +2 -0
@@ -784,7 +786,9 @@ static struct dentry *ceph_lookup(struct inode *dir, struct
 dentry *dentry,
                 return ERR_PTR(-ENAMETOOLONG);
 
         if (IS_ENCRYPTED(dir)) {
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));
                 err = __fscrypt_prepare_readdir(dir);
+       printk("%s fscrypt_has_encryption_key(dir): %d ======\n", __func__,
 fscrypt_has_encryption_key(dir));

                if (err)
                        return ERR_PTR(err); if (!fscrypt_has_encryption_key(dir)) {

# fscrypt unlock mydir
Enter custom passphrase for protector "l":
"mydir" is now unlocked and ready for use.

And after "mydir/" was unlocked and then when doing:

# touch mydir/dir/file2

And when lookup the "dir/" dentry I can see the "mydir/" encryption key changed:

709 <7>[79125.023676] ceph:  __ceph_caps_issued_mask ino 0x10000000004 cap 000000004dc11892 issued pAsLsXsFs (mask As)
710 <7>[79125.023687] ceph:  __touch_cap 000000007071b095 cap 000000004dc11892 mds0
711 <7>[79125.023823] ceph:  lookup 000000007071b095 dentry 00000000f97501b2 'dir'
712 <4>[79125.023838] ceph_lookup fscrypt_has_encryption_key(dir): 0 ====== 713 <4>[79125.024186] ceph_lookup fscrypt_has_encryption_key(dir): 1 ====== 714 <7>[79125.024194] ceph:   dir 000000007071b095 flags are 0x0 715 <7>[79125.024269] ceph:  do_request on 00000000a93fafef

Quoted 3 lines

I am thinking could we just make __fscrypt_prepare_readdir(), which will return 0 when the key is already set or successfully set, to return 1 instead of 0 to mark that the key changed ?

OK, I see what you mean. Thanks. What about simply detect this change here instead of changing __fscrypt_prepare_readdir() semantics? Because I think that would require changes in several other places, including other filesystems.

What about something like this?

fs/ceph/dir.c +2 -0
index edc2bf0aab83..499ec75d2496 100644
@@ -784,6 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  		return ERR_PTR(-ENAMETOOLONG);
    	if (IS_ENCRYPTED(dir)) {
+		bool had_key = fscrypt_has_encryption_key(dir);
+
  		err = __fscrypt_prepare_readdir(dir);
  		if (err)
  			return ERR_PTR(err);
@@ -791,6 +793,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry,
  			spin_lock(&dentry->d_lock);
  			dentry->d_flags |= DCACHE_NOKEY_NAME;
Quoted 8 lines

fs/ceph/dir.c static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, · line 800

Quoted 6 lines
fs/ceph/dir.c +2 -0
  			spin_unlock(&dentry->d_lock);
+		} else if (!had_key) {
+			__ceph_dir_clear_complete(ceph_inode(dir));
  		}
  	}
 

Yeah. Could we just add one helper, such as:

Sure, that makes sense. I'll send out a new version soon. Thanks!

Cheers,

Signature · 1 line
Luís
Quoted 24 lines

bool ceph_fscrypt_prepare_readdir(dir)

{

    bool had_key = fscrypt_has_encryption_key(dir);

    err = __fscrypt_prepare_readdir(dir);

    if (!err && !had_key || err = -ENOKEY && had_key) {

        return true;   // key changed

    }

    return false;  // key not changed

}

Thanks!

- Xiubo

Quoted 1 line

Cheers,