]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
client: make FSCryptDecryptedInodes std::shared_ptr
authorChristopher Hoffman <choffman@redhat.com>
Mon, 18 Aug 2025 19:39:07 +0000 (19:39 +0000)
committerChristopher Hoffman <choffman@redhat.com>
Wed, 5 Nov 2025 13:59:36 +0000 (13:59 +0000)
To help eliminate memory leaks, use std::shared_ptr
for keeping track of FSCryptDecryptedInodes instances.

Signed-off-by: Christopher Hoffman <choffman@redhat.com>
src/client/Client.cc
src/client/FSCrypt.cc
src/client/FSCrypt.h

index c75e097cd7e95b7b6cd9b9f05d0de15b34b3cf52..51f7ae5d072e1f1b16b5e2514a8f35dedad98e3a 100644 (file)
@@ -10833,8 +10833,11 @@ int Client::_release_fh(Fh *f)
   if (in->snapid == CEPH_NOSNAP) {
     FSCryptKeyHandlerRef kh;
     get_keyhandler(in->fscrypt_ctx, kh);
-    if (kh && kh->di) {
-      kh->di->del_inode(in->ino);
+    if (kh) {
+      auto& di = kh->get_di();
+      if (di) {
+         di->del_inode(in->ino);
+      }
     }
     if (in->put_open_ref(f->mode)) {
       _flush(in, new C_Client_FlushComplete(this, in));
@@ -10889,8 +10892,11 @@ int Client::_open(const InodeRef& in, int flags, mode_t mode, Fh **fhp,
 
   FSCryptKeyHandlerRef kh;
   get_keyhandler(in->fscrypt_ctx, kh);
-  if (kh && kh->di) {
-    kh->di->add_inode(in->ino);
+  if (kh) {
+    auto& di = kh->get_di();
+    if (di) {
+      di->add_inode(in->ino);
+    }
   }
 
   in->get_open_ref(cmode);  // make note of pending open, since it effects _wanted_ caps.
@@ -10977,8 +10983,11 @@ int Client::_open(const InodeRef& in, int flags, mode_t mode, Fh **fhp,
   } else {
     FSCryptKeyHandlerRef kh;
     get_keyhandler(in->fscrypt_ctx, kh);
-    if (kh && kh->di) {
-      kh->di->del_inode(in->ino);
+    if (kh) {
+      auto& di = kh->get_di();
+      if (di) {
+        di->del_inode(in->ino);
+      }
     }
     in->put_open_ref(cmode);
   }
@@ -15872,8 +15881,11 @@ int Client::_create(const walk_dentry_result& wdr, int flags, mode_t mode,
   if(fhp) {
     FSCryptKeyHandlerRef kh;
     get_keyhandler((*inp)->fscrypt_ctx, kh);
-    if (kh && kh->di) {
-      kh->di->add_inode((*inp)->ino);
+    if (kh) {
+      auto& di = kh->get_di();
+      if (di) {
+        di->add_inode((*inp)->ino);
+      }
     }
 
     (*inp)->get_open_ref(cmode);
@@ -16302,9 +16314,12 @@ int Client::ll_rmdir(Inode *in, const char *name, const UserPerm& perms)
 int Client::get_keyhandler(FSCryptContextRef fscrypt_ctx, FSCryptKeyHandlerRef& kh){
   if (fscrypt_ctx) {
     int r = fscrypt->get_key_store().find(fscrypt_ctx->master_key_identifier, kh);
-    if (kh)
-      if (kh && kh->di)
+    if (kh) {
+      auto& di = kh->get_di();
+      if (di) {
         return r;
+      }
+    }
   }
   return 0;
 }
index 466139a951207d59440c982d0aaeacd4adcf0841..60538079629c97588280df93bdd10f0dccdb9ab4 100644 (file)
@@ -385,6 +385,12 @@ FSCryptKeyRef& FSCryptKeyHandler::get_key()
   return key;
 }
 
+FSCryptDecryptedInodesRef& FSCryptKeyHandler::get_di()
+{
+  std::shared_lock rl{lock};
+  return di;
+}
+
 //taken from fs/crypto/keyring.h
 bool FSCryptKeyStore::valid_key_spec(const struct fscrypt_key_specifier& k)
 {
@@ -479,7 +485,9 @@ int FSCryptKeyStore::create(const char *k, int klen, FSCryptKeyHandlerRef& key_h
       return 0; //returns 0 regardless
     }
     key_handler->present = true;
-    key_handler->di = new FSCryptDecryptedInodes();
+
+    auto di = new FSCryptDecryptedInodes();
+    key_handler->di = std::shared_ptr<FSCryptDecryptedInodes>((FSCryptDecryptedInodes *)di);
     m[id] = key_handler;
   }
 
@@ -533,7 +541,6 @@ int FSCryptKeyStore::invalidate(struct fscrypt_remove_key_arg* arg, int user)
   //do a final clean up
   if (!kh->present && kh->di->get_inodes().empty()) {
     kh->reset(++epoch, nullptr);
-    free(kh->di);
     m.erase(id);
   } else {
     r = 0;
index 97d3f1029522e2d661f7d7c4485607e630ef2a72..9d58100ec655e75ae7a007e37a13a0b1de0e722f 100644 (file)
@@ -324,6 +324,8 @@ public:
   }
 };
 
+using FSCryptDecryptedInodesRef = std::shared_ptr<FSCryptDecryptedInodes>;
+
 class FSCryptKeyHandler {
   ceph::shared_mutex lock = ceph::make_shared_mutex("FSCryptKeyHandler");
   int64_t epoch = -1;
@@ -336,9 +338,10 @@ public:
   void reset(int64_t epoch, FSCryptKeyRef k);
 
   int64_t get_epoch();
+  FSCryptDecryptedInodesRef di;
   std::list<int>& get_users() { return users; }
   FSCryptKeyRef& get_key();
-  FSCryptDecryptedInodes* di;
+  FSCryptDecryptedInodesRef& get_di();
   bool present = false;
 };