Protect to access released handle 70/303670/5
authorChanggyu Choi <changyu.choi@samsung.com>
Fri, 4 Aug 2023 02:17:08 +0000 (11:17 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Wed, 3 Jan 2024 09:12:03 +0000 (18:12 +0900)
Change-Id: Ib228d1991930e220226e83b2d7ed87155589e595
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/ac-internal.cc
src/ac-internal.hh
src/cynara_thread.cc
src/cynara_thread.hh
src/stub-internal.cc
src/stub-internal.hh

index 70821d6576112ca060d05ed926e2c9876f8577c5..9f44608bcafa41ae291548abd66342bd9269a7c3 100644 (file)
@@ -96,7 +96,14 @@ int AccessController::Check(int fd, const std::string& sender_appid) {
 
 void AccessController::CheckAsync(int fd, std::string sender_appid,
     CompleteCallback callback) {
+  /* This is for handle freed issue */
+  auto tmp_handle = new std::shared_ptr<AccessController>(shared_from_this());
   Job job([=]() -> Job::Type {
+    if ((*tmp_handle).use_count() == 1) {
+      delete tmp_handle;
+      return Job::Type::Continue;
+    }
+
     int res = Check(fd, sender_appid);
     auto* cbdata = new std::pair<CompleteCallback, int>(callback, res);
     guint sid = g_idle_add(
@@ -112,6 +119,7 @@ void AccessController::CheckAsync(int fd, std::string sender_appid,
       delete cbdata;
     }
 
+    delete tmp_handle;
     return Job::Type::Continue;
   });
 
index a2309ee4a5170a0bf0c785ac553c19ad458230d6..daefbd752fc2054d2ce0d5b9b8d6b512ae382cc4 100644 (file)
@@ -33,7 +33,7 @@ namespace internal {
 
 using CompleteCallback = std::function<void(int)>;
 
-class AccessController {
+class AccessController : public std::enable_shared_from_this<AccessController> {
  public:
   explicit AccessController(bool trusted = false) : trusted_(trusted) {}
 
index 0a8efd2cc31915f773337032377d4b63d08da3ce..a5ee3e970e2ff25d5c600416b96130f6d1f33830 100644 (file)
@@ -41,16 +41,15 @@ CynaraThread::CynaraThread() {
 
 CynaraThread::~CynaraThread() {
   Job finish_job;
-  Push(finish_job);
+  Push(std::move(finish_job));
   thread_.join();
 }
 
 void CynaraThread::ThreadRun() {
+  Job job;
   while (true) {
-    Job job;
     queue_.WaitAndPop(job);
-    Job::Type ret = job.Do();
-    if (ret == Job::Type::Finish)
+    if (job.Do() == Job::Type::Finish)
       return;
   }
 }
index 412931a7256ebef4b7427d12deac0d99c051dcd5..b4c7944c5c53dac0e6cb497d5c738a55164f2e51 100644 (file)
@@ -56,7 +56,6 @@ class CynaraThread {
 
  private:
   CynaraThread();
-  Job Pop();
 
   std::thread thread_;
   mutable SharedQueue<Job> queue_;
index 72b59b213a6159051ebc0f1dabfb668525274574..b0bc8833d664e9f817721022a149700a89d3134b 100644 (file)
@@ -129,12 +129,12 @@ void Stub::Ignore() {
 
 void Stub::AddPrivilege(const std::string& privilege) {
   std::lock_guard<std::recursive_mutex> lock(GetMutex());
-  access_controller_.AddPrivilege(privilege);
+  access_controller_->AddPrivilege(privilege);
 }
 
 void Stub::SetTrusted(const bool trusted) {
   std::lock_guard<std::recursive_mutex> lock(GetMutex());
-  access_controller_.SetTrusted(trusted);
+  access_controller_->SetTrusted(trusted);
 }
 
 std::shared_ptr<Port> Stub::FindPort(const std::string& instance) const {
@@ -412,7 +412,7 @@ gboolean Stub::Server::OnRequestReceived(GIOChannel* channel, GIOCondition cond,
       _E("Reject request. %u:%u", cred->GetUid(), getuid());
       res = -1;
     } else {
-      stub->access_controller_.CheckAsync(client->GetFd(), app_id,
+      stub->access_controller_->CheckAsync(client->GetFd(), app_id,
           response_func);
       return G_SOURCE_CONTINUE;
     }
index 0f2723cdcaf3b0165e8beb02df8b5e1a529723b9..f58413f24706de951c4d95cd1669663bc60e1afd 100644 (file)
@@ -108,7 +108,8 @@ class Stub {
   std::recursive_mutex& GetMutex() const;
 
  private:
-  AccessController access_controller_;
+  std::shared_ptr<AccessController> access_controller_ =
+      std::make_shared<AccessController>();
   std::string port_name_;
   std::list<std::shared_ptr<AcceptedPort>> ports_;
   IEventListener* listener_ = nullptr;