From a1008520ef1e74b0f0b207b6b5d7d9e808f5b93d Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 23 May 2023 02:32:38 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: I8e6cde1b27c64de8b217e246535deb8ac6db4f63 --- .../native/backup_ext/src/ext_extension.cpp | 1 + .../native/file_share/include/file_share.h | 2 +- .../native/file_share/src/file_share.cpp | 4 +- .../js/backup/session_restore_n_exporter.cpp | 45 ++++++++++--------- services/backup_sa/src/module_ipc/service.cpp | 5 +-- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 6cea0bd31..92b500aef 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -305,6 +305,7 @@ int BackupExtExtension::HandleBackup(const BJsonEntityExtensionConfig &usrConfig ErrCode ret = ERR_OK; ret = IndexFileReady(bigFileInfo, proxy); ret = BigFileReady(proxy); + HILOGE("HandleBackup finish, ret = %{public}d", ret); return ret; } catch (const BError &e) { return e.GetCode(); diff --git a/interfaces/innerkits/native/file_share/include/file_share.h b/interfaces/innerkits/native/file_share/include/file_share.h index 37f76141a..62e0c7eea 100644 --- a/interfaces/innerkits/native/file_share/include/file_share.h +++ b/interfaces/innerkits/native/file_share/include/file_share.h @@ -33,7 +33,7 @@ enum SHARE_FILE_TYPE { DIR_TYPE = 0, FILE_TYPE }; namespace { const string FILE_SCHEME = "file"; -const string CMD_GET_PID = "pidof "; +const string CMD_GET_PID = "pidof "; const string PID_FLAG = ""; const string PACKAGE_NAME_FLAG = ""; const string CURRENT_USER_ID_FLAG = ""; diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index be352eafd..0d65e1d68 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -85,8 +85,8 @@ static string GetPidFromProcessName(const string &processName) const int32_t bufLen = 100; char buf[bufLen] = {'\0'}; string cmd = CMD_GET_PID + processName; - if((fp = popen(cmd.c_str(), "r")) != NULL) { - if(fgets(buf, bufLen, fp) != NULL) { + if ((fp = popen(cmd.c_str(), "r")) != NULL) { + if (fgets(buf, bufLen, fp) != NULL) { return string(buf); } } diff --git a/interfaces/kits/js/backup/session_restore_n_exporter.cpp b/interfaces/kits/js/backup/session_restore_n_exporter.cpp index 55215541c..0f3593067 100644 --- a/interfaces/kits/js/backup/session_restore_n_exporter.cpp +++ b/interfaces/kits/js/backup/session_restore_n_exporter.cpp @@ -254,6 +254,27 @@ napi_value SessionRestoreNExporter::AppendBundles(napi_env env, napi_callback_in } } +static std::tuple, std::unique_ptr> ParseFileMeta(napi_env env, + const NVal &fileMeta) +{ + bool succ = false; + std::unique_ptr bundleName = nullptr; + tie(succ, bundleName, ignore) = fileMeta.GetProp("bundleName").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property bundle name."); + return { false, nullptr, nullptr }; + } + + std::unique_ptr fileName = nullptr; + tie(succ, fileName, ignore) = fileMeta.GetProp("uri").ToUTF8String(); + if (!succ) { + HILOGE("First argument is not have property file name."); + return { false, nullptr, nullptr }; + } + + return { true, move(bundleName), move(fileName) }; +} + napi_value SessionRestoreNExporter::PublishFile(napi_env env, napi_callback_info cbinfo) { HILOGI("called SessionRestore::PublishFile begin"); @@ -271,17 +292,9 @@ napi_value SessionRestoreNExporter::PublishFile(napi_env env, napi_callback_info return nullptr; } - auto [succ, bundleName, size] = fileMeta.GetProp("bundleName").ToUTF8String(); + auto [succ, bundleName, fileName] = ParseFileMeta(env, fileMeta); if (!succ) { - HILOGE("First argument is not have property bundle name."); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - std::unique_ptr fileName = nullptr; - tie(succ, fileName, ignore) = fileMeta.GetProp("uri").ToUTF8String(); - if (!succ) { - HILOGE("First argument is not have property file name."); + HILOGE("ParseFileMeta failed."); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -333,17 +346,9 @@ napi_value SessionRestoreNExporter::GetFileHandle(napi_env env, napi_callback_in return nullptr; } - auto [succ, bundleName, ignore] = fileMeta.GetProp("bundleName").ToUTF8String(); + auto [succ, bundleName, fileName] = ParseFileMeta(env, fileMeta); if (!succ) { - HILOGE("First argument is not have property bundle name."); - NError(EINVAL).ThrowErr(env); - return nullptr; - } - - std::unique_ptr fileName = nullptr; - tie(succ, fileName, ignore) = fileMeta.GetProp("uri").ToUTF8String(); - if (!succ) { - HILOGE("First argument is not have property file name."); + HILOGE("ParseFileMeta failed."); NError(EINVAL).ThrowErr(env); return nullptr; } diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 2088b7be4..baddb4118 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -133,9 +133,8 @@ void Service::VerifyCaller() case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: { auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid()); if ((multiuser.userId != BConstants::DEFAULT_USER_ID) && (multiuser.userId != BConstants::XTS_UID)) { - throw BError(BError::Codes::SA_INVAL_ARG, string("Calling user is ") - .append(to_string(multiuser.userId)) - .append(", which is currently not supported")); + throw BError(BError::Codes::SA_INVAL_ARG, string("Calling user is ").append( + to_string(multiuser.userId)).append(", which is currently not supported")); } const string permission = "ohos.permission.BACKUP"; if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) == -- Gitee From 437afcfb29499603e6744f2167f1986344a934f5 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 23 May 2023 11:27:34 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ida47b9eb9a413a40705bbd4c4e8754546c214fe1 --- .../backup_sa/src/module_sched/sched_scheduler.cpp | 14 ++++++++++++++ tools/backup_tool/src/tools_op_restore.cpp | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index 8a1aac6a6..a5a52ea4c 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -119,6 +119,16 @@ void SchedScheduler::RemoveExtConn(const string &bundleName) } } +bool GetRealPath(string &path) +{ + unique_ptr absPath = make_unique(PATH_MAX + 1); + if (realpath(path.c_str(), absPath.get()) == nullptr) { + return false; + } + path = absPath.get(); + return true; +} + void SchedScheduler::InstallingState(const string &bundleName) { BConstants::ServiceSchedAction action = sessionPtr_->GetServiceSchedAction(bundleName); @@ -131,6 +141,10 @@ void SchedScheduler::InstallingState(const string &bundleName) string state = sessionPtr_->GetInstallState(bundleName); string path = string(BConstants::SA_BUNDLE_BACKUP_ROOT_DIR).append(bundleName); string filePath = path + "/bundle.hap"; + if (!GetRealPath(filePath)) { + throw BError(BError::Codes::SA_INVAL_ARG, string("File path is invalid")); + } + if (state == BConstants::RESTORE_INSTALL_PATH) { if (!ForceCreateDirectory(path)) { throw BError(BError::Codes::SA_INVAL_ARG, string("Failed to create directory")); diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index c02b6682a..9f3f92b6f 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -236,11 +236,18 @@ static void RestoreApp(shared_ptr restore, vector &bundleNa static int32_t InitPathCapFile(const string &pathCapFile, vector bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Init"); - if (access(pathCapFile.data(), F_OK) != 0) { + char tmpPath[PATH_MAX] = {0}; + if (realpath(pathCapFile.c_str(), tmpPath) == nullptr) { + fprintf(stderr, "path to realpath error"); return -errno; } - UniqueFd fd(open(pathCapFile.data(), O_RDWR, S_IRWXU)); + string realPath = tmpPath; + if (access(realPath.data(), F_OK) != 0) { + return -errno; + } + + UniqueFd fd(open(realPath.data(), O_RDWR, S_IRWXU)); if (fd < 0) { fprintf(stderr, "Failed to open file error: %d %s\n", errno, strerror(errno)); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); -- Gitee From cd1352b82ea9c6a87eceff1b6250bc456d835388 Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 23 May 2023 11:45:17 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ic7ee31241d7370ed79ae33572dfdab37cc5e8d97 --- services/backup_sa/src/module_sched/sched_scheduler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index a5a52ea4c..18d5e6493 100644 --- a/services/backup_sa/src/module_sched/sched_scheduler.cpp +++ b/services/backup_sa/src/module_sched/sched_scheduler.cpp @@ -119,7 +119,7 @@ void SchedScheduler::RemoveExtConn(const string &bundleName) } } -bool GetRealPath(string &path) +static bool GetRealPath(string &path) { unique_ptr absPath = make_unique(PATH_MAX + 1); if (realpath(path.c_str(), absPath.get()) == nullptr) { -- Gitee From e13e0ccf1cbe68d890eee5e8e13fcbe14cabbd1c Mon Sep 17 00:00:00 2001 From: lvyuanyuan Date: Tue, 23 May 2023 12:47:33 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lvyuanyuan Change-Id: Ie0898ff64db11ad0fca8ea75dc864247bfcf3c97 --- tools/backup_tool/src/tools_op_restore.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 9f3f92b6f..f978d3a42 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -233,16 +233,25 @@ static void RestoreApp(shared_ptr restore, vector &bundleNa FinishTrace(HITRACE_TAG_FILEMANAGEMENT); } +static bool GetRealPath(string &path) +{ + unique_ptr absPath = make_unique(PATH_MAX + 1); + if (realpath(path.c_str(), absPath.get()) == nullptr) { + return false; + } + path = absPath.get(); + return true; +} + static int32_t InitPathCapFile(const string &pathCapFile, vector bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Init"); - char tmpPath[PATH_MAX] = {0}; - if (realpath(pathCapFile.c_str(), tmpPath) == nullptr) { + string realPath = pathCapFile; + if (!GetRealPath(realPath)) { fprintf(stderr, "path to realpath error"); return -errno; } - string realPath = tmpPath; if (access(realPath.data(), F_OK) != 0) { return -errno; } -- Gitee