From 553e27c39b7391368379119ac42b7ca6f9d6a972 Mon Sep 17 00:00:00 2001 From: zhangkaixiang Date: Fri, 21 Jul 2023 11:59:38 +0800 Subject: [PATCH] fixed 0638348 from https://gitee.com/zkx48/filemanagement_app_file_service/pulls/211 add sandbox_helper interface from common_func Signed-off-by: zhangkaixiang --- interfaces/common/include/common_func.h | 4 - interfaces/common/include/sandbox_helper.h | 34 +++++ interfaces/common/src/common_func.cpp | 98 -------------- interfaces/common/src/sandbox_helper.cpp | 127 ++++++++++++++++++ interfaces/innerkits/native/BUILD.gn | 14 +- .../native/file_share/src/file_share.cpp | 6 +- .../src/remote_file_share.cpp | 7 +- 7 files changed, 169 insertions(+), 121 deletions(-) create mode 100644 interfaces/common/include/sandbox_helper.h create mode 100644 interfaces/common/src/sandbox_helper.cpp diff --git a/interfaces/common/include/common_func.h b/interfaces/common/include/common_func.h index da1facccf..d90a148f9 100644 --- a/interfaces/common/include/common_func.h +++ b/interfaces/common/include/common_func.h @@ -22,11 +22,7 @@ namespace OHOS { namespace AppFileService { class CommonFunc { - static std::unordered_map sandboxPathMap_; public: - static bool CheckValidPath(const std::string &filePath); - static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, - std::string &physicalPath); static std::string GetSelfBundleName(); static std::string GetUriFromPath(const std::string &path); static bool CheckPublicDirPath(const std::string &sandboxPath); diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h new file mode 100644 index 000000000..43e92eb27 --- /dev/null +++ b/interfaces/common/include/sandbox_helper.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_SANDBOX_HELPER_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_SANDBOX_HELPER_H + +#include +#include + +namespace OHOS { +namespace AppFileService { +class SandboxHelper { + static std::unordered_map sandboxPathMap_; +public: + static bool CheckValidPath(const std::string &filePath); + static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath); +}; +} // namespace AppFileService +} // namespace OHOS + +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_INTERFACES_INNERKITS_NATIVE_COMMON_INCLUDE_SANDBOX_HELPER_H \ No newline at end of file diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index 575328f76..59f34e85f 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -21,11 +21,9 @@ #include "bundle_mgr_proxy.h" #include "ipc_skeleton.h" #include "iservice_registry.h" -#include "uri.h" #include "system_ability_definition.h" #include "log.h" -#include "json_utils.h" using namespace std; @@ -33,108 +31,12 @@ namespace OHOS { namespace AppFileService { using namespace OHOS::AppExecFwk; namespace { - const string PACKAGE_NAME_FLAG = ""; - const string CURRENT_USER_ID_FLAG = ""; - const string PHYSICAL_PATH_KEY = "src-path"; - const string SANDBOX_PATH_KEY = "sandbox-path"; - const string MOUNT_PATH_MAP_KEY = "mount-path-map"; - const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; const std::string FILE_SCHEME_PREFIX = "file://"; const char BACKFLASH = '/'; const std::vector PUBLIC_DIR_PATHS = { "/Documents" }; } -std::unordered_map CommonFunc::sandboxPathMap_; - -static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, - const string &userId, const string &bundleName) -{ - if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { - lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), - CURRENT_USER_ID_FLAG.length(), userId); - } - - if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { - lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), - PACKAGE_NAME_FLAG.length(), bundleName); - } - - return lowerPathHead + lowerPathTail; -} - -static void GetSandboxPathMap(unordered_map &sandboxPathMap) -{ - nlohmann::json jsonObj; - int ret = JsonUtils::GetJsonObjFromPath(jsonObj, SANDBOX_JSON_FILE_PATH); - if (ret != 0) { - LOGE("Get json object failed from %{public}s with %{public}d", SANDBOX_JSON_FILE_PATH.c_str(), ret); - return; - } - - if (jsonObj.find(MOUNT_PATH_MAP_KEY) == jsonObj.end()) { - LOGE("Json object find mount path map failed"); - return; - } - - nlohmann::json mountPathMap = jsonObj[MOUNT_PATH_MAP_KEY]; - for (size_t i = 0; i < mountPathMap.size(); i++) { - string srcPath = mountPathMap[i][PHYSICAL_PATH_KEY]; - string sandboxPath = mountPathMap[i][SANDBOX_PATH_KEY]; - sandboxPathMap[sandboxPath] = srcPath; - } - return; -} - -int32_t CommonFunc::GetPhysicalPath(const std::string &fileUri, const std::string &userId, - std::string &physicalPath) -{ - Uri uri(fileUri); - string bundleName = uri.GetAuthority(); - string sandboxPath = uri.GetPath(); - - string lowerPathTail = ""; - string lowerPathHead = ""; - - if (sandboxPathMap_.size() == 0) { - GetSandboxPathMap(sandboxPathMap_); - } - - for (auto it = sandboxPathMap_.begin(); it != sandboxPathMap_.end(); it++) { - string sandboxPathPrefix = it->first; - if (sandboxPath.length() >= sandboxPathPrefix.length()) { - string sandboxPathTemp = sandboxPath.substr(0, sandboxPathPrefix.length()); - if (sandboxPathTemp == sandboxPathPrefix) { - lowerPathHead = it->second; - lowerPathTail = sandboxPath.substr(sandboxPathPrefix.length()); - break; - } - } - } - - if (lowerPathHead == "") { - return -EINVAL; - } else { - physicalPath = GetLowerPath(lowerPathHead, lowerPathTail, userId, bundleName); - return 0; - } -} - -bool CommonFunc::CheckValidPath(const std::string &filePath) -{ - if (filePath.empty() || filePath.size() >= PATH_MAX) { - return false; - } - - char realPath[PATH_MAX]{'\0'}; - if (realpath(filePath.c_str(), realPath) != nullptr && - strncmp(realPath, filePath.c_str(), filePath.size()) == 0) { - return true; - } else { - return false; - } -} - static sptr GetBundleMgrProxy() { sptr systemAbilityManager = diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp new file mode 100644 index 000000000..0a69e0b50 --- /dev/null +++ b/interfaces/common/src/sandbox_helper.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sandbox_helper.h" + +#include + +#include "log.h" +#include "json_utils.h" +#include "uri.h" + +using namespace std; + +namespace OHOS { +namespace AppFileService { +namespace { + const string PACKAGE_NAME_FLAG = ""; + const string CURRENT_USER_ID_FLAG = ""; + const string PHYSICAL_PATH_KEY = "src-path"; + const string SANDBOX_PATH_KEY = "sandbox-path"; + const string MOUNT_PATH_MAP_KEY = "mount-path-map"; + const string SANDBOX_JSON_FILE_PATH = "/etc/app_file_service/file_share_sandbox.json"; +} +std::unordered_map SandboxHelper::sandboxPathMap_; + +static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, + const string &userId, const string &bundleName) +{ + if (lowerPathHead.find(CURRENT_USER_ID_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(CURRENT_USER_ID_FLAG), + CURRENT_USER_ID_FLAG.length(), userId); + } + + if (lowerPathHead.find(PACKAGE_NAME_FLAG) != string::npos) { + lowerPathHead = lowerPathHead.replace(lowerPathHead.find(PACKAGE_NAME_FLAG), + PACKAGE_NAME_FLAG.length(), bundleName); + } + + return lowerPathHead + lowerPathTail; +} + +static void GetSandboxPathMap(unordered_map &sandboxPathMap) +{ + nlohmann::json jsonObj; + int ret = JsonUtils::GetJsonObjFromPath(jsonObj, SANDBOX_JSON_FILE_PATH); + if (ret != 0) { + LOGE("Get json object failed from %{public}s with %{public}d", SANDBOX_JSON_FILE_PATH.c_str(), ret); + return; + } + + if (jsonObj.find(MOUNT_PATH_MAP_KEY) == jsonObj.end()) { + LOGE("Json object find mount path map failed"); + return; + } + + nlohmann::json mountPathMap = jsonObj[MOUNT_PATH_MAP_KEY]; + for (size_t i = 0; i < mountPathMap.size(); i++) { + string srcPath = mountPathMap[i][PHYSICAL_PATH_KEY]; + string sandboxPath = mountPathMap[i][SANDBOX_PATH_KEY]; + sandboxPathMap[sandboxPath] = srcPath; + } + return; +} + +int32_t SandboxHelper::GetPhysicalPath(const std::string &fileUri, const std::string &userId, + std::string &physicalPath) +{ + Uri uri(fileUri); + string bundleName = uri.GetAuthority(); + string sandboxPath = uri.GetPath(); + + string lowerPathTail = ""; + string lowerPathHead = ""; + + if (sandboxPathMap_.size() == 0) { + GetSandboxPathMap(sandboxPathMap_); + } + + for (auto it = sandboxPathMap_.begin(); it != sandboxPathMap_.end(); it++) { + string sandboxPathPrefix = it->first; + if (sandboxPath.length() >= sandboxPathPrefix.length()) { + string sandboxPathTemp = sandboxPath.substr(0, sandboxPathPrefix.length()); + if (sandboxPathTemp == sandboxPathPrefix) { + lowerPathHead = it->second; + lowerPathTail = sandboxPath.substr(sandboxPathPrefix.length()); + break; + } + } + } + + if (lowerPathHead == "") { + return -EINVAL; + } else { + physicalPath = GetLowerPath(lowerPathHead, lowerPathTail, userId, bundleName); + return 0; + } +} + +bool SandboxHelper::CheckValidPath(const std::string &filePath) +{ + if (filePath.empty() || filePath.size() >= PATH_MAX) { + return false; + } + + char realPath[PATH_MAX]{'\0'}; + if (realpath(filePath.c_str(), realPath) != nullptr && + strncmp(realPath, filePath.c_str(), filePath.size()) == 0) { + return true; + } else { + return false; + } +} +} // namespace AppFileService +} // namespace OHOS + diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 6e53db4b5..2810c8a4e 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -34,8 +34,8 @@ config("file_uri_config") { ohos_shared_library("fileshare_native") { sources = [ - "../../common/src/common_func.cpp", "../../common/src/json_utils.cpp", + "../../common/src/sandbox_helper.cpp", "file_share/src/file_share.cpp", ] @@ -45,14 +45,9 @@ ohos_shared_library("fileshare_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", - "ability_runtime:abilitykit_native", "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", ] part_name = "app_file_service" @@ -108,8 +103,8 @@ config("remote_file_share_config") { ohos_shared_library("remote_file_share_native") { sources = [ - "../../common/src/common_func.cpp", "../../common/src/json_utils.cpp", + "../../common/src/sandbox_helper.cpp", "remote_file_share/src/remote_file_share.cpp", ] @@ -119,13 +114,8 @@ ohos_shared_library("remote_file_share_native") { "ability_base:base", "ability_base:want", "ability_base:zuri", - "ability_runtime:abilitykit_native", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", ] innerapi_tags = [ "platformsdk_indirect" ] diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 5cfea0e65..8045d8552 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -23,9 +23,9 @@ #include #include "accesstoken_kit.h" -#include "common_func.h" #include "hap_token_info.h" #include "log.h" +#include "sandbox_helper.h" #include "uri.h" namespace OHOS { @@ -139,7 +139,7 @@ static int32_t GetFileShareInfo(const string &uri, uint32_t tokenId, uint32_t fl GetProviderInfo(uri, info); - ret = CommonFunc::GetPhysicalPath(uri, info.currentUid_, info.providerLowerPath_); + ret = SandboxHelper::GetPhysicalPath(uri, info.currentUid_, info.providerLowerPath_); if (ret != 0) { LOGE("Failed to get lower path %{public}d", ret); return ret; @@ -190,7 +190,7 @@ static void DeleteExistShareFile(const string &path) static int32_t PreparePreShareDir(FileShareInfo &info) { - if (!CommonFunc::CheckValidPath(info.providerLowerPath_)) { + if (!SandboxHelper::CheckValidPath(info.providerLowerPath_)) { LOGE("Invalid share path with %{private}s", info.providerLowerPath_.c_str()); return -EINVAL; } diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index bfcc1446b..1f5fdf38f 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -23,11 +23,10 @@ #include #include -#include "directory_ex.h" +#include "log.h" +#include "sandbox_helper.h" #include "securec.h" -#include "common_func.h" #include "uri.h" -#include "log.h" namespace OHOS { namespace AppFileService { @@ -268,7 +267,7 @@ static std::string GetPhysicalPath(Uri &uri, const std::string &userId) } std::string physicalPath = ""; - int ret = CommonFunc::GetPhysicalPath(uri.ToString(), userId, physicalPath); + int ret = SandboxHelper::GetPhysicalPath(uri.ToString(), userId, physicalPath); if (ret != 0) { LOGE("Get physical path failed with %{public}d", ret); return ""; -- Gitee