From: Josef Bacik <[email protected]>

Previously we were wiping the master key secret when we do
FS_IOC_REMOVE_ENCRYPTION_KEY, and then using the fact that it was
cleared as the mechanism from keeping new users from being setup.  This
works with inode based encryption, as the per-inode key is derived at
setup time, so the secret disappearing doesn't affect any currently open
files from being able to continue working.

However for extent based encryption we do our key derivation at page
writeout and readpage time, which means we need the master key secret to
be available while we still have our file open.

Since the master key lifetime is controlled by a flag, move the clearing
of the secret to the mk_active_users cleanup stage if we have extent
based encryption enabled on this super block.  This counter represents
the actively open files that still exist on the file system, and thus
should still be able to operate normally.  Once the last user is closed
we can clear the secret.  Until then no new users are allowed, and this
allows currently open files to continue to operate until they're closed.

Signed-off-by: Josef Bacik <[email protected]>
Signed-off-by: Daniel Vacek <[email protected]>
---

v7 changes:
 * Updated the comment about key status in fscrypt_master_key_secret structure
   as suggested by Eric.
No changes in v6.
v5: 
https://lore.kernel.org/linux-btrfs/5f28e46ce99d918a16f5bf4d8190870d0fffefc4.1706116485.git.jo...@toxicpanda.com/
---
 fs/crypto/fscrypt_private.h |  9 ++++++---
 fs/crypto/keyring.c         | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h
index e5ab9893dfed..2f5f4e7f8f65 100644
--- a/fs/crypto/fscrypt_private.h
+++ b/fs/crypto/fscrypt_private.h
@@ -592,9 +592,12 @@ struct fscrypt_master_key_secret {
  *
  * FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED
  *     Removal of this key has been initiated, but some inodes that were
- *     unlocked with it are still in-use.  Like ABSENT, ->mk_secret is wiped,
- *     and the key can no longer be used to unlock inodes.  Unlike ABSENT, the
- *     key is still in the keyring; ->mk_decrypted_inodes is nonempty; and
+ *     unlocked with it are still in-use.
+ *     For filesystems using per-extent encryption ->mk_secret is still
+ *     being kept as the per-extent keys are derived at writeout time.
+ *     Otherwise, like ABSENT, ->mk_secret is wiped, and the key can
+ *     no longer be used to unlock inodes.  Unlike ABSENT, the key is
+ *     still in the keyring; ->mk_decrypted_inodes is nonempty; and
  *     ->mk_active_refs > 0, being equal to the size of ->mk_decrypted_inodes.
  *
  *     This state transitions to ABSENT if ->mk_decrypted_inodes becomes empty,
diff --git a/fs/crypto/keyring.c b/fs/crypto/keyring.c
index be8e6e8011f2..796e02a0db25 100644
--- a/fs/crypto/keyring.c
+++ b/fs/crypto/keyring.c
@@ -110,6 +110,14 @@ void fscrypt_put_master_key_activeref(struct super_block 
*sb,
        WARN_ON_ONCE(mk->mk_present);
        WARN_ON_ONCE(!list_empty(&mk->mk_decrypted_inodes));
 
+       /* We can't wipe the master key secret until the last activeref is
+        * dropped on the master key with per-extent encryption since the key
+        * derivation continues to happen as long as there are active refs.
+        * Wipe it here now that we're done using it.
+        */
+       if (sb->s_cop->has_per_extent_encryption)
+               wipe_master_key_secret(&mk->mk_secret);
+
        for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
                fscrypt_destroy_prepared_key(
                                sb, &mk->mk_direct_keys[i]);
@@ -134,7 +142,15 @@ static void fscrypt_initiate_key_removal(struct 
super_block *sb,
                                         struct fscrypt_master_key *mk)
 {
        WRITE_ONCE(mk->mk_present, false);
-       wipe_master_key_secret(&mk->mk_secret);
+
+       /*
+        * Per-extent encryption requires the master key to stick around until
+        * writeout has completed as we derive the per-extent keys at writeout
+        * time.  Once the activeref drops to 0 we'll wipe the master secret
+        * key.
+        */
+       if (!sb->s_cop->has_per_extent_encryption)
+               wipe_master_key_secret(&mk->mk_secret);
        fscrypt_put_master_key_activeref(sb, mk);
 }
 
-- 
2.53.0


Reply via email to