From fc4ffa44be5a2342440d2288fec1a3c74c891c55 Mon Sep 17 00:00:00 2001 From: lujixuan Date: Fri, 25 Jul 2025 19:30:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E6=8B=A5=E6=9C=89=E6=B2=99?= =?UTF-8?q?=E7=AE=B1=E6=9D=83=E9=99=90=E5=BA=94=E7=94=A8=E7=9A=84=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=8C=82=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujixuan --- modules/sandbox/normal/sandbox_common.cpp | 16 +++- modules/sandbox/normal/sandbox_common.h | 1 + modules/sandbox/normal/sandbox_core.cpp | 45 +++++++++ modules/sandbox/normal/sandbox_core.h | 6 ++ modules/sandbox/normal/sandbox_def.h | 4 + .../app_spawn_sandbox_test.cpp | 91 +++++++++++++++++++ 6 files changed, 162 insertions(+), 1 deletion(-) diff --git a/modules/sandbox/normal/sandbox_common.cpp b/modules/sandbox/normal/sandbox_common.cpp index c34e2fc1..f709c937 100644 --- a/modules/sandbox/normal/sandbox_common.cpp +++ b/modules/sandbox/normal/sandbox_common.cpp @@ -398,7 +398,8 @@ uint32_t SandboxCommon::ConvertFlagStr(const std::string &flagStr) {"DLP_MANAGER", 2}, {"DEVELOPER_MODE", 17}, {"PREINSTALLED_HAP", 29}, - {"CUSTOM_SANDBOX_HAP", 31}}; + {"CUSTOM_SANDBOX_HAP", 31}, + {"PREINSTALLED_SHELL_HAP", 35}}; if (flagsMap.count(flagStr)) { return 1 << flagsMap.at(flagStr); @@ -882,6 +883,15 @@ const std::string& SandboxCommon::GetArkWebPackageName(void) return arkWebPackageName; } +const std::string& SandboxCommon::GetDevModel(void) +{ + static std::string devModel; + if (devModel.empty()) { + devModel = system::GetParameter(SandboxCommonDef::DEVICE_MODEL_NAME_PARAM, ""); + } + return devModel; +} + std::string SandboxCommon::ConvertToRealPathWithPermission(const AppSpawningCtx *appProperty, std::string path) { AppSpawnMsgBundleInfo *info = @@ -955,6 +965,10 @@ std::string SandboxCommon::ConvertToRealPath(const AppSpawningCtx *appProperty, path.c_str(), GetArkWebPackageName().c_str()); } + if (path.find(SandboxCommonDef::g_devModel) != std::string::npos) { + path = ReplaceAllVariables(path, SandboxCommonDef::g_devModel, GetDevModel()); + } + return path; } diff --git a/modules/sandbox/normal/sandbox_common.h b/modules/sandbox/normal/sandbox_common.h index 2bdee646..9d34623d 100644 --- a/modules/sandbox/normal/sandbox_common.h +++ b/modules/sandbox/normal/sandbox_common.h @@ -127,6 +127,7 @@ private: static std::string ReplaceHostUserId(const AppSpawningCtx *appProperty, const std::string &path); static std::string ReplaceClonePackageName(const AppSpawningCtx *appProperty, const std::string &path); static const std::string &GetArkWebPackageName(void); + static const std::string &GetDevModel(void); private: static int32_t deviceTypeEnable_; diff --git a/modules/sandbox/normal/sandbox_core.cpp b/modules/sandbox/normal/sandbox_core.cpp index 0959eafb..1190ea3b 100644 --- a/modules/sandbox/normal/sandbox_core.cpp +++ b/modules/sandbox/normal/sandbox_core.cpp @@ -703,6 +703,39 @@ int32_t SandboxCore::DoSandboxRootFolderCreate(const AppSpawningCtx *appProperty return 0; } +void SandboxCore::GetSpecialMountCondition(bool &isPreInstalled, bool &isHaveSandBoxPermission, + const AppSpawningCtx *appProperty) +{ + const std::string preInstallFlag = "PREINSTALLED_HAP"; + const std::string customSandBoxFlag = "CUSTOM_SANDBOX_HAP"; + isPreInstalled = (GetAppMsgFlags(appProperty) & SandboxCommon::ConvertFlagStr(preInstallFlag)) != 0; + isHaveSandBoxPermission = (GetAppMsgFlags(appProperty) & SandboxCommon::ConvertFlagStr(customSandBoxFlag)) != 0; +} + +int32_t SandboxCore::MountNonShellPreInstallHap(const AppSpawningCtx *appProperty, cJSON *item) +{ + bool isPreInstalled = false; + bool isHaveSandBoxPermission = false; + GetSpecialMountCondition(isPreInstalled, isHaveSandBoxPermission, appProperty); + bool preInstallMount = (isPreInstalled && !isHaveSandBoxPermission); + if (preInstallMount) { + return DoAllMntPointsMount(appProperty, item, nullptr, SandboxCommonDef::g_flagePoint); + } + return 0; +} + +int32_t SandboxCore::MountShellPreInstallHap(const AppSpawningCtx *appProperty, cJSON *item) +{ + bool isPreInstalled = false; + bool isHaveSandBoxPermission = false; + GetSpecialMountCondition(isPreInstalled, isHaveSandBoxPermission, appProperty); + bool preInstallShellMount = (isPreInstalled && isHaveSandBoxPermission); + if (preInstallShellMount) { + return DoAllMntPointsMount(appProperty, item, nullptr, SandboxCommonDef::g_flagePoint); + } + return 0; +} + int32_t SandboxCore::HandleFlagsPoint(const AppSpawningCtx *appProperty, cJSON *appConfig) { cJSON *flagsPoints = cJSON_GetObjectItemCaseSensitive(appConfig, SandboxCommonDef::g_flagePoint); @@ -717,6 +750,18 @@ int32_t SandboxCore::HandleFlagsPoint(const AppSpawningCtx *appProperty, cJSON * return 0; } std::string flagsStr(flagsChr); + + const std::string preInstallFlag = "PREINSTALLED_HAP"; + const std::string preInstallShellFlag = "PREINSTALLED_SHELL_HAP"; + + if (flagsStr == preInstallFlag) { + return MountNonShellPreInstallHap(appProperty, item); + } + + if (flagsStr == preInstallShellFlag) { + return MountShellPreInstallHap(appProperty, item); + } + uint32_t flag = SandboxCommon::ConvertFlagStr(flagsStr); if ((GetAppMsgFlags(appProperty) & flag) == 0) { return 0; diff --git a/modules/sandbox/normal/sandbox_core.h b/modules/sandbox/normal/sandbox_core.h index 2d61cd18..f736dfd0 100644 --- a/modules/sandbox/normal/sandbox_core.h +++ b/modules/sandbox/normal/sandbox_core.h @@ -121,6 +121,12 @@ private: static int32_t DoMountDebugPoints(const AppSpawningCtx *appProperty, cJSON *appConfig); static int32_t MountDebugSharefs(const AppSpawningCtx *property, const char *src, const char *target); + + // 处理拥有沙箱权限应用的挂载 + static void GetSpecialMountCondition(bool &isPreInstalled, bool &isHaveSandBoxPermission, + const AppSpawningCtx *appProperty); + static int32_t MountNonShellPreInstallHap(const AppSpawningCtx *appProperty, cJSON *item); + static int32_t MountShellPreInstallHap(const AppSpawningCtx *appProperty, cJSON *item); }; } // namespace AppSpawn diff --git a/modules/sandbox/normal/sandbox_def.h b/modules/sandbox/normal/sandbox_def.h index be6b7deb..06c81e7b 100644 --- a/modules/sandbox/normal/sandbox_def.h +++ b/modules/sandbox/normal/sandbox_def.h @@ -93,6 +93,7 @@ const std::string g_variablePackageName = ""; const std::string g_clonePackageName = ""; const std::string g_arkWebPackageName = ""; const std::string g_hostUserId = ""; +const std::string g_devModel = ""; /* HSP */ const std::string HSPLIST_SOCKET_TYPE = "HspList"; @@ -149,6 +150,9 @@ const std::string GET_ALL_PROCESSES_MODE = "ohos.permission.GET_ALL_PROCESSES"; const std::string APP_ALLOW_IOURING = "ohos.permission.ALLOW_IOURING"; const std::string ARK_WEB_PERSIST_PACKAGE_NAME = "persist.arkwebcore.package_name"; +/* 系统参数 */ +const std::string DEVICE_MODEL_NAME_PARAM = "const.cust.devmodel"; + // 枚举类型 enum SandboxConfigType { SANDBOX_APP_JSON_CONFIG, diff --git a/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp index d0e46d92..b33ea9d1 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_sandbox_test.cpp @@ -32,6 +32,7 @@ #include "app_spawn_test_helper.h" #include "sandbox_dec.h" #include "sandbox_shared_mount.h" +#include "parameters.h" using namespace testing; using namespace testing::ext; @@ -2341,4 +2342,94 @@ HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_Shared_Mount_24, TestSize.Level0 } EXPECT_EQ(res, 0); } + +HWTEST_F(AppSpawnSandboxTest, App_Spawn_Sandbox_DevModel_001, TestSize.Level0) +{ + AppSpawningCtx* spawningCtx = GetTestAppProperty(); + std::string path = AppSpawn::SandboxCommon::ConvertToRealPath(spawningCtx, "/version/special_cust/"); + ASSERT_NE(path.c_str(), nullptr); + std::string devModelPath = + "/version/special_cust/" + system::GetParameter(SandboxCommonDef::DEVICE_MODEL_NAME_PARAM, ""); + + ASSERT_EQ(strcmp(path.c_str(), devModelPath.c_str()), 0); + DeleteAppSpawningCtx(spawningCtx); +} + +HWTEST_F(AppSpawnSandboxTest, Handle_Flag_Point_PreInstall_Shell_Hap_001, TestSize.Level0) +{ + std::string flagPointConfigStr = "{ \ + \"flags-point\": [ \ + { \ + \"flags\": \"PREINSTALLED_HAP\", \ + \"mount-paths\": [{ \ + \"src-path\": \"/version/special_cust/app\", \ + \"sandbox-path\": \"/version/special_cust/app\", \ + \"sandbox-flags\": [ \ + \"bind\", \ + \"rec\" \ + ], \ + \"check-action-status\": \"false\" \ + }] \ + }, \ + { \ + \"flags\": \"PREINSTALLED_SHELL_HAP\", \ + \"mount-paths\": [{ \ + \"src-path\": \"/system/app/HiShell\", \ + \"sandbox-path\": \"/system/app/HiShell\", \ + \"sandbox-flags\": [ \ + \"bind\", \ + \"rec\" \ + ], \ + \"check-action-status\": \"false\" \ + }] \ + } \ + ] \ + }"; + cJSON *flagPointConfig = cJSON_Parse(flagPointConfigStr.c_str()); + AppSpawningCtx *appProperty = GetTestAppProperty(); + ASSERT_EQ(appProperty != nullptr, 1); + int32_t ret = AppSpawn::SandboxCore::HandleFlagsPoint(appProperty, flagPointConfig); + EXPECT_EQ(ret, 0); + DeleteAppSpawningCtx(appProperty); +} + +HWTEST_F(AppSpawnSandboxTest, Handle_Flag_Point_PreInstall_Shell_Hap_002, TestSize.Level0) +{ + std::string flagPointConfigStr = "{ \ + \"flags-point\": [ \ + { \ + \"flags\": \"PREINSTALLED_HAP\", \ + \"mount-paths\": [{ \ + \"src-path\": \"/version/special_cust/app\", \ + \"sandbox-path\": \"/version/special_cust/app\", \ + \"sandbox-flags\": [ \ + \"bind\", \ + \"rec\" \ + ], \ + \"check-action-status\": \"false\" \ + }] \ + }, \ + { \ + \"flags\": \"PREINSTALLED_SHELL_HAP\", \ + \"mount-paths\": [{ \ + \"src-path\": \"/system/app/HiShell\", \ + \"sandbox-path\": \"/system/app/HiShell\", \ + \"sandbox-flags\": [ \ + \"bind\", \ + \"rec\" \ + ], \ + \"check-action-status\": \"false\" \ + }] \ + } \ + ] \ + }"; + cJSON *flagPointConfig = cJSON_Parse(flagPointConfigStr.c_str()); + AppSpawningCtx *appProperty = GetTestAppProperty(); + ASSERT_EQ(appProperty != nullptr, 1); + int ret = SetAppSpawnMsgFlag(appProperty->message, TLV_MSG_FLAGS, APP_FLAGS_PRE_INSTALLED_HAP); + ASSERT_EQ(ret, 0); + int32_t res = AppSpawn::SandboxCore::HandleFlagsPoint(appProperty, flagPointConfig); + EXPECT_EQ(res, 0); + DeleteAppSpawningCtx(appProperty); +} } // namespace OHOS -- Gitee