diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 6cea0bd311034a88726906562bc705ac480ea183..92b500aef0ccdb4220a40f78ce876178a27279a6 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 37f76141a30a0ce1bd3574785c50088fb6c49e0e..62e0c7eea5319719b10c61113d146a9c71c7f919 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 be352eafdb10ce85c32638be6247ad42cdabe2d6..0d65e1d6894938e7bcb9ade06b111b8c84456e13 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 55215541c428d74b0818ad7899d256615a3b8b40..0f3593067d484757ae18e93e28be0b3f2f9832ca 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 2088b7be42d392418c714b1384c84efb0b85b132..baddb4118e1c73bc723ce4897e0a471a630a9d28 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) == diff --git a/services/backup_sa/src/module_sched/sched_scheduler.cpp b/services/backup_sa/src/module_sched/sched_scheduler.cpp index 8a1aac6a6696212493d225ed6ab416b41a98b0de..18d5e6493a41bd881431127925d73709ef0ca057 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) } } +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; +} + 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 c02b6682ab847aa4f4059305975066e3dd4dc292..f978d3a429e1c29e328d108c47fbdc23ff9cc169 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -233,14 +233,30 @@ 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"); - if (access(pathCapFile.data(), F_OK) != 0) { + string realPath = pathCapFile; + if (!GetRealPath(realPath)) { + fprintf(stderr, "path to realpath error"); + return -errno; + } + + if (access(realPath.data(), F_OK) != 0) { return -errno; } - UniqueFd fd(open(pathCapFile.data(), O_RDWR, S_IRWXU)); + 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);