Block different type installation with same pkgid 78/285178/2
authorIlho Kim <ilho159.kim@samsung.com>
Wed, 7 Dec 2022 08:13:41 +0000 (17:13 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Thu, 8 Dec 2022 02:29:10 +0000 (11:29 +0900)
Change-Id: Ia33f3be69f1441b5ec79c0a0d9a5fe9f9eca9062
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
src/common/pkgmgr_interface.cc
src/common/pkgmgr_interface.h
src/common/step/configuration/step_configure.cc
src/common/step/configuration/step_configure.h
src/common/step/pkgmgr/step_check_removable.cc

index 184dfaf..a754585 100644 (file)
@@ -152,6 +152,10 @@ void PkgMgrInterface::AddAppQueryInterface(
   query_interface_map_.emplace(idx, interface);
 }
 
+std::shared_ptr<AppQueryInterface> PkgMgrInterface::GetAppQueryInterface() {
+  return query_interface_;
+}
+
 RequestType PkgMgrInterface::GetRequestType(int idx) const {
   // These type could be determined even if there are no query_interface_.
   switch (pkgmgr_installer_get_request_type(pi_)) {
index ded0faf..e085018 100644 (file)
@@ -86,6 +86,14 @@ class PkgMgrInterface {
       std::shared_ptr<AppQueryInterface> interface);
 
   /**
+   * Get AppQueryInterface for current request
+   *
+   * \return AppQueryInterface ptr if current AppQueryInterface exist.
+   *         Otherwise, return null shared_ptr
+   */
+  std::shared_ptr<AppQueryInterface> GetAppQueryInterface();
+
+  /**
    * Returns Request type passed from pkgmgr_installer
    *
    * \param idx index of request
index 2a3d052..6608e03 100644 (file)
@@ -28,6 +28,8 @@ namespace configuration {
 
 const char kStrEmpty[] = "";
 const char kAppFWUser[] = "app_fw";
+const char kRpmPkgType[] = "rpm";
+const char kRpmInstaller[] = "tpk";
 
 StepConfigure::StepConfigure(InstallerContext* context, PkgMgrPtr pkgmgr)
     : Step(context),
@@ -48,13 +50,15 @@ Step::Status StepConfigure::process() {
   RequestType request_type = context_->request_type.get();
   switch (request_type) {
     case RequestType::Install:
-    case RequestType::Update:
+    case RequestType::Update: {
       context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
       if (!pkgmgr_->GetTepPath().empty()) {
         context_->tep_path.set(pkgmgr_->GetTepPath());
         context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
       }
+      context_->pkgid.set(GetPkgIdFromFile());
       break;
+    }
     case RequestType::PartialUninstall:
     case RequestType::Uninstall:
       if (request_type == RequestType::PartialUninstall)
@@ -88,13 +92,15 @@ Step::Status StepConfigure::process() {
               pkgmgr_->GetRecoveryCleanup()));
       break;
     case RequestType::MountInstall:
-    case RequestType::MountUpdate:
+    case RequestType::MountUpdate: {
       context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
       if (!pkgmgr_->GetTepPath().empty()) {
         context_->tep_path.set(pkgmgr_->GetTepPath());
         context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
       }
+      context_->pkgid.set(GetPkgIdFromFile());
       break;
+    }
     case RequestType::ManifestPartialInstall:
     case RequestType::ManifestPartialUpdate:
     case RequestType::ManifestDirectInstall:
@@ -112,13 +118,15 @@ Step::Status StepConfigure::process() {
       context_->xml_path.set(xml_path);
       break;
     }
-    case RequestType::ReadonlyUpdateInstall:
+    case RequestType::ReadonlyUpdateInstall: {
       context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
       if (!pkgmgr_->GetTepPath().empty()) {
         context_->tep_path.set(pkgmgr_->GetTepPath());
         context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
       }
+      context_->pkgid.set(GetPkgIdFromFile());
       break;
+    }
     case RequestType::ReadonlyUpdateUninstall: {
       context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
       bf::path original_path =
@@ -145,6 +153,9 @@ Step::Status StepConfigure::process() {
       return Status::CONFIG_ERROR;
   }
 
+  if (!CheckInvalidBackendUsage())
+    return Status::OPERATION_NOT_ALLOWED;
+
   LOG(INFO) << "Request Type: " << GetRequestTypeString(request_type);
 
   return Status::OK;
@@ -286,5 +297,40 @@ void StepConfigure::SetupMoveType() {
   }
 }
 
+std::string StepConfigure::GetPkgIdFromFile() {
+  auto app_query_interface = pkgmgr_->GetAppQueryInterface();
+  if (!app_query_interface)
+    return "";
+
+  return app_query_interface->GetPkgId(
+      pkgmgr_->GetRequestInfo(context_->index.get()));
+}
+
+bool StepConfigure::CheckInvalidBackendUsage() {
+  if (context_->pkgid.get().empty())
+    return true;
+
+  PkgQueryInterface pkg_query(context_->pkgid.get().c_str(),
+      context_->uid.get());
+  if (!pkg_query.IsValid()) {
+    LOG(INFO) << "pkg[" << context_->pkgid.get()
+        << "] is not installed, skip checking backend with installed pkg";
+    return true;
+  }
+
+  const std::string& backend_type = context_->pkg_type.get();
+  std::string installed_pkg_type = pkg_query.Type();
+  if (installed_pkg_type == kRpmPkgType)
+    installed_pkg_type = kRpmInstaller;
+
+  if (installed_pkg_type != backend_type) {
+    LOG(ERROR) << "Wrong use of backend : package's type [" << pkg_query.Type()
+        << "] mismatched backend [" << backend_type << "]";
+    return false;
+  }
+
+  return true;
+}
+
 }  // namespace configuration
 }  // namespace common_installer
index b13a6b6..c5ff866 100644 (file)
@@ -69,6 +69,8 @@ class StepConfigure : public Step {
   void SetupSkipCheckReference();
   void SetupSkipOptimization();
   void SetupMoveType();
+  std::string GetPkgIdFromFile();
+  bool CheckInvalidBackendUsage();
 
   PkgMgrPtr pkgmgr_;
 
index 2ab1e2f..caf7c73 100644 (file)
@@ -36,16 +36,6 @@ Step::Status StepCheckRemovable::process() {
     return Status::OPERATION_NOT_ALLOWED;
   }
 
-  std::string pkg_type = pkg_query.Type();
-  // rpm package installed by tpk-backend
-  if (pkg_type == "rpm")
-    pkg_type = "tpk";
-  if (pkg_type != context_->pkg_type.get()) {
-    LOG(ERROR) << "Wrong use of backend : package's type [" << pkg_query.Type()
-               << "] mismatched backend [" << context_->pkg_type.get() << "]";
-    return Status::OPERATION_NOT_ALLOWED;
-  }
-
   return Status::OK;
 }