From 9edd80cb77e3b813c32b792abdb8c87cf243f3f5 Mon Sep 17 00:00:00 2001 From: Junghyun Yeon Date: Tue, 31 Aug 2021 16:40:53 +0900 Subject: [PATCH] Add Steps for handling privileged shared resources Add new steps responsible for removing privileged shared resources while uninstall and its recovery requests. Change-Id: Iec89a431e415b6abc84c2bf6409fc30445818536 Signed-off-by: Junghyun Yeon --- src/common/installer/app_installer.cc | 7 ++ .../step/pkgmgr/step_recover_priv_sharedres.cc | 23 ++++++ .../step/pkgmgr/step_recover_priv_sharedres.h | 30 +++++++ .../step/pkgmgr/step_remove_priv_sharedres.cc | 95 ++++++++++++++++++++++ .../step/pkgmgr/step_remove_priv_sharedres.h | 35 ++++++++ 5 files changed, 190 insertions(+) create mode 100644 src/common/step/pkgmgr/step_recover_priv_sharedres.cc create mode 100644 src/common/step/pkgmgr/step_recover_priv_sharedres.h create mode 100644 src/common/step/pkgmgr/step_remove_priv_sharedres.cc create mode 100644 src/common/step/pkgmgr/step_remove_priv_sharedres.h diff --git a/src/common/installer/app_installer.cc b/src/common/installer/app_installer.cc index 2d064ac..0f767c3 100644 --- a/src/common/installer/app_installer.cc +++ b/src/common/installer/app_installer.cc @@ -76,7 +76,9 @@ #include "common/step/pkgmgr/step_recover_application.h" #include "common/step/pkgmgr/step_recover_parser_plugins.h" #include "common/step/pkgmgr/step_register_app.h" +#include "common/step/pkgmgr/step_recover_priv_sharedres.h" #include "common/step/pkgmgr/step_remove_manifest.h" +#include "common/step/pkgmgr/step_remove_priv_sharedres.h" #include "common/step/pkgmgr/step_run_parser_plugins.h" #include "common/step/pkgmgr/step_unregister_app.h" #include "common/step/pkgmgr/step_update_app.h" @@ -401,6 +403,7 @@ void AppInstaller::UninstallSteps() { AddStep( ci::security::StepPrivacyPrivilege::ActionType::Uninstall); AddStep(); + AddStep(); AddStep(); AddStep(); AddStep(); @@ -514,6 +517,7 @@ void AppInstaller::RecoverySteps() { AddStep(); AddStep(); AddStep(); + AddStep(); AddStep(); AddStep(); AddStep(); @@ -692,6 +696,7 @@ void AppInstaller::PartialUninstallSteps() { AddStep(); AddStep(); AddStep(); + AddStep(); AddStep(); AddStep(); AddStep( @@ -759,6 +764,7 @@ void AppInstaller::ReadonlyUpdateUninstallSteps() { AddStep( ci::security::StepPrivacyPrivilege::ActionType::Update); AddStep(); + AddStep(); AddStep(); AddStep(); AddStep(); @@ -777,6 +783,7 @@ void AppInstaller::DisablePkgSteps() { ci::configuration::StepParseManifest::ManifestLocation::INSTALLED, ci::configuration::StepParseManifest::StoreLocation::NORMAL); AddStep(); + AddStep(); AddStep( ci::pkgmgr::StepUpdatePkgDisableInfo::ActionType::Disable); AddStep( diff --git a/src/common/step/pkgmgr/step_recover_priv_sharedres.cc b/src/common/step/pkgmgr/step_recover_priv_sharedres.cc new file mode 100644 index 0000000..3478e6d --- /dev/null +++ b/src/common/step/pkgmgr/step_recover_priv_sharedres.cc @@ -0,0 +1,23 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache-2.0 license that can be +// found in the LICENSE file. + +#include "common/step/pkgmgr/step_recover_priv_sharedres.h" + +#include + +#include "common/utils/paths.h" +#include "common/utils/pkgmgr_query.h" +#include "common/pkgmgr_registration.h" +#include "common/step/pkgmgr/step_remove_priv_sharedres.h" + +namespace common_installer { +namespace pkgmgr { + +Step::Status StepRecoverPrivSharedres::RecoveryUninstall() { + StepRemovePrivSharedres instance(context_); + return instance.process(); +} + +} // namespace pkgmgr +} // namespace common_installer diff --git a/src/common/step/pkgmgr/step_recover_priv_sharedres.h b/src/common/step/pkgmgr/step_recover_priv_sharedres.h new file mode 100644 index 0000000..cea9cc6 --- /dev/null +++ b/src/common/step/pkgmgr/step_recover_priv_sharedres.h @@ -0,0 +1,30 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache-2.0 license that can be +// found in the LICENSE file. + +#ifndef COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_ +#define COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_ + +#include + +#include "common/installer_context.h" +#include "common/step/recovery/step_recovery.h" + +namespace common_installer { +namespace pkgmgr { + +class StepRecoverPrivSharedres : public recovery::StepRecovery { + public: + using StepRecovery::StepRecovery; + + Status RecoveryNew() override { return Status::OK; } + Status RecoveryUpdate() override { return Status::OK; } + Status RecoveryUninstall() override; + + STEP_NAME(RecoverPrivSharedres) +}; + +} // namespace pkgmgr +} // namespace common_installer + +#endif // COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_ diff --git a/src/common/step/pkgmgr/step_remove_priv_sharedres.cc b/src/common/step/pkgmgr/step_remove_priv_sharedres.cc new file mode 100644 index 0000000..503d335 --- /dev/null +++ b/src/common/step/pkgmgr/step_remove_priv_sharedres.cc @@ -0,0 +1,95 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache 2.0 license that can be +// found in the LICENSE file. + +#include "common/step/pkgmgr/step_remove_priv_sharedres.h" + +#include +#include + +#include + +#include "common/utils/file_util.h" +#include "common/utils/user_util.h" + +namespace bf = boost::filesystem; + +namespace { + +bf::path GetRootPathForUid(uid_t uid) { + tzplatform_set_user(uid); + const char* rootpath = tzplatform_getenv(TZ_USER_HOME); + tzplatform_reset_user(); + return rootpath; +} + +bool RemoveResForUid(const std::string& pkgid, uid_t uid) { + bf::path target_path = GetRootPathForUid(uid) / pkgid; + + if (bf::exists(target_path)) + return true; + + if (!common_installer::RemoveAll(target_path)) + return false; + + return true; +} + +bool SendUninstallRequest(const std::string& pkgid, uid_t uid) { + pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST); + + int ret = pkgmgr_client_res_usr_uninstall(pc, pkgid.c_str(), uid); + pkgmgr_client_free(pc); + + return (ret == PKGMGR_R_OK) ? true : false; +} + +} // namespace + +namespace common_installer { +namespace pkgmgr { + +Step::Status StepRemovePrivSharedres::process() { + if (context_->installation_mode.get() == InstallationMode::OFFLINE) + return OfflineRemove(); + else + return RequestRemove(); +} + +Step::Status StepRemovePrivSharedres::OfflineRemove() { + if (context_->request_mode.get() == RequestMode::GLOBAL) { + common_installer::UserList user_list = common_installer::GetUserList(); + for (auto l : user_list) { + uid_t uid = std::get<0>(l); + if (!RemoveResForUid(context_->pkgid.get(), uid)) + return Status::ERROR; + } + } else { + if (!RemoveResForUid(context_->pkgid.get(), context_->uid.get())) + return Status::ERROR; + } + + return Status::OK; +} + +Step::Status StepRemovePrivSharedres::RequestRemove() { + if (context_->request_mode.get() == RequestMode::GLOBAL) { + common_installer::UserList user_list = common_installer::GetUserList(); + for (auto l : user_list) { + uid_t uid = std::get<0>(l); + if (!SendUninstallRequest(context_->pkgid.get(), uid)) { + LOG(ERROR) << "Failed to remove privileged shared resources"; + return Status::ERROR; + } + } + } else { + if (!SendUninstallRequest(context_->pkgid.get(), context_->uid.get())) { + LOG(ERROR) << "Failed to remove privileged shared resources"; + return Status::ERROR; + } + } + return Status::OK; +} + +} // namespace pkgmgr +} // namespace common_installer diff --git a/src/common/step/pkgmgr/step_remove_priv_sharedres.h b/src/common/step/pkgmgr/step_remove_priv_sharedres.h new file mode 100644 index 0000000..403072f --- /dev/null +++ b/src/common/step/pkgmgr/step_remove_priv_sharedres.h @@ -0,0 +1,35 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#ifndef COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_ +#define COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_ + +#include + +#include "common/installer_context.h" +#include "common/step/step.h" + +namespace common_installer { +namespace pkgmgr { + +class StepRemovePrivSharedres : public Step { + public: + using Step::Step; + + Status process() override; + Status clean() override { return Status::OK; } + Status undo() override { return Status::OK; } + Status precheck() override { return Status::OK; } + + private: + Status OfflineRemove(); + Status RequestRemove(); + + STEP_NAME(RemovePrivSharedres) +}; + +} // namespace pkgmgr +} // namespace common_installer + +#endif // COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_ -- 2.7.4