From 9cd706d1c9b33d38bfea2338ca1eaffe4ce99747 Mon Sep 17 00:00:00 2001 From: Jiang Liu Date: Fri, 1 Sep 2023 13:51:56 +0800 Subject: [PATCH] agent: avoid possible leakage of storage device When a storage device is used by more than one container, the second and forth instances will cause storage device reference count leakage, thus cause storage device leakage. The reason is: add_storages() will increase reference count of existing storage device, but forget to add the device to the `mount_list` array, thus leak the reference count. Fixes: #7820 Signed-off-by: Jiang Liu --- src/agent/src/sandbox.rs | 4 ++++ src/agent/src/storage/mod.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/agent/src/sandbox.rs b/src/agent/src/sandbox.rs index 788f29278f..aac0f81584 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -71,6 +71,10 @@ impl StorageState { } } + pub fn path(&self) -> &str { + self.device.path() + } + pub async fn ref_count(&self) -> u32 { self.count.load(Ordering::Relaxed) } diff --git a/src/agent/src/storage/mod.rs b/src/agent/src/storage/mod.rs index 84348c972c..80cc081faa 100644 --- a/src/agent/src/storage/mod.rs +++ b/src/agent/src/storage/mod.rs @@ -103,6 +103,10 @@ pub async fn add_storages( let path = storage.mount_point.clone(); let state = sandbox.lock().await.add_sandbox_storage(&path).await; if state.ref_count().await > 1 { + let path = state.path(); + if !path.is_empty() { + mount_list.push(path.to_string()); + } // The device already exists. continue; }