From 22a0fd0a34b957975afac4605c656eecdef5b99d Mon Sep 17 00:00:00 2001 From: z00500798 Date: Wed, 26 Feb 2025 14:28:47 +0800 Subject: [PATCH] Description:appspawn lock status fix Feature or Bugfix:Bugfix Binary Source: No Signed-off-by: zl_zl --- interfaces/innerkits/client/appspawn_client.c | 34 ++++++++++++++++ .../client/libappspawn_client.versionscript | 1 + interfaces/innerkits/include/appspawn.h | 11 +++++ modules/sandbox/sandbox_utils.cpp | 31 ++++++-------- standard/appspawn_service.c | 40 ++++++++++++++++++- 5 files changed, 97 insertions(+), 20 deletions(-) diff --git a/interfaces/innerkits/client/appspawn_client.c b/interfaces/innerkits/client/appspawn_client.c index b81556d2..e7b52a23 100644 --- a/interfaces/innerkits/client/appspawn_client.c +++ b/interfaces/innerkits/client/appspawn_client.c @@ -35,6 +35,7 @@ #include "parameter.h" #include "securec.h" +#define USER_LOCK_STATUS_SIZE 8 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; static AppSpawnReqMsgMgr *g_clientInstance[CLIENT_MAX] = {NULL}; @@ -358,3 +359,36 @@ int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqH AppSpawnReqMsgFree(reqHandle); return ret; } + +int AppSpawnClientSendUserLockStatus(uint32_t userId, bool isLocked) +{ + char lockstatus[USER_LOCK_STATUS_SIZE] = {0}; + int ret = snprintf_s(lockstatus, USER_LOCK_STATUS_SIZE, USER_LOCK_STATUS_SIZE - 1, "%u:%d", userId, isLocked); + APPSPAWN_CHECK(ret > 0, return ret, "Failed to build lockstatus req msg, ret = %{public}d", ret); + APPSPAWN_LOGI("Send lockstatus msg to appspawn %{public}s", lockstatus); + + AppSpawnReqMsgHandle reqHandle; + ret = AppSpawnReqMsgCreate(MSG_LOCK_STATUS, "storage_manager", &reqHandle); + APPSPAWN_CHECK(ret == 0, return ret, "Failed to create appspawn req msg, ret = %{public}d", ret); + + ret = AppSpawnReqMsgAddStringInfo(reqHandle, "lockstatus", lockstatus); + APPSPAWN_CHECK(ret == 0, AppSpawnReqMsgFree(reqHandle); + return ret, "Failed to add lockstatus message, ret=%{public}d", ret); + + AppSpawnClientHandle clientHandle; + ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); + APPSPAWN_CHECK(ret == 0, AppSpawnReqMsgFree(reqHandle); + return ret, "Appspawn client failed to init, ret=%{public}d", ret); + + AppSpawnResult result = {0}; + ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result); + AppSpawnClientDestroy(clientHandle); + APPSPAWN_CHECK(ret == 0, return ret, "Send msg to appspawn failed, ret=%{public}d", ret); + + if (result.result != 0) { + APPSPAWN_LOGE("Appspawn failed to handle message, result=%{public}d", result.result); + return result.result; + } + APPSPAWN_LOGI("Send lockstatus msg to appspawn success"); + return 0; +} \ No newline at end of file diff --git a/interfaces/innerkits/client/libappspawn_client.versionscript b/interfaces/innerkits/client/libappspawn_client.versionscript index 0863f541..03ebc5b4 100644 --- a/interfaces/innerkits/client/libappspawn_client.versionscript +++ b/interfaces/innerkits/client/libappspawn_client.versionscript @@ -33,6 +33,7 @@ AppSpawnReqMsgAddStringInfo; AppSpawnTerminateMsgCreate; AppSpawnClientAddPermission; + AppSpawnClientSendUserLockStatus; GetPermissionIndex; GetMaxPermissionIndex; GetPermissionByIndex; diff --git a/interfaces/innerkits/include/appspawn.h b/interfaces/innerkits/include/appspawn.h index d7717f99..47925f7b 100644 --- a/interfaces/innerkits/include/appspawn.h +++ b/interfaces/innerkits/include/appspawn.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #ifdef __cplusplus @@ -97,6 +98,15 @@ int AppSpawnClientDestroy(AppSpawnClientHandle handle); */ int AppSpawnClientSendMsg(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, AppSpawnResult *result); +/** + * @brief send client user lock status request + * + * @param userId user id + * @param isLocked lock status + * @return if succeed return 0,else return other value + */ +int AppSpawnClientSendUserLockStatus(uint32_t userId, bool isLocked); + typedef enum { MSG_APP_SPAWN = 0, MSG_GET_RENDER_TERMINATION_STATUS, @@ -108,6 +118,7 @@ typedef enum { MSG_RESTART_SPAWNER, MSG_DEVICE_DEBUG, MSG_UNINSTALL_DEBUG_HAP, + MSG_LOCK_STATUS, MAX_TYPE_INVALID } AppSpawnMsgType; diff --git a/modules/sandbox/sandbox_utils.cpp b/modules/sandbox/sandbox_utils.cpp index 397721e2..4081eb2a 100644 --- a/modules/sandbox/sandbox_utils.cpp +++ b/modules/sandbox/sandbox_utils.cpp @@ -53,6 +53,7 @@ #endif #define MAX_MOUNT_TIME 500 // 500us +#define LOCK_STATUS_SIZE 16 using namespace std; using namespace OHOS; @@ -1859,7 +1860,7 @@ int32_t SetAppSandboxProperty(AppSpawnMgr *content, AppSpawningCtx *property) #define DIR_MODE 0711 #ifndef APPSPAWN_SANDBOX_NEW -static bool IsUnlockStatus(uint32_t uid, const char *bundleName, size_t bundleNameLen) +static bool IsUnlockStatus(uint32_t uid) { const int userIdBase = 200000; uid = uid / userIdBase; @@ -1867,22 +1868,14 @@ static bool IsUnlockStatus(uint32_t uid, const char *bundleName, size_t bundleNa return true; } - const char rootPath[] = "/data/app/el2/"; - const char basePath[] = "/base/"; - size_t allPathSize = strlen(rootPath) + strlen(basePath) + 1 + USER_ID_SIZE + bundleNameLen; - char *path = reinterpret_cast(malloc(sizeof(char) * allPathSize)); - APPSPAWN_CHECK(path != NULL, return true, "Failed to malloc path"); - int len = sprintf_s(path, allPathSize, "%s%u%s%s", rootPath, uid, basePath, bundleName); - APPSPAWN_CHECK(len > 0 && ((size_t)len < allPathSize), free(path); - return true, "Failed to get base path"); + string lockStatusParam = "startup.appspawn.lockstatus_" + to_string(uid); + char userLockStatus[LOCK_STATUS_SIZE] = {0}; + int ret = GetParameter(lockStatusParam.c_str(), "1", userLockStatus, sizeof(userLockStatus)); + APPSPAWN_LOGI("get param %{public}s %{public}s", lockStatusParam.c_str(), userLockStatus); - if (access(path, F_OK) == 0) { - APPSPAWN_LOGI("bundleName:%{public}s this is unlock status", bundleName); - free(path); + if (ret > 0 && (strcmp(userLockStatus, "0") == 0)) { // 0:unlock 1:lock return true; } - free(path); - APPSPAWN_LOGI("bundleName:%{public}s this is lock status", bundleName); return false; } @@ -2091,9 +2084,7 @@ static void MountDirToShared(const AppSpawningCtx *property) string sourcePath = "/data/app/el1/bundle/public/" + string(bundleName); MountDir(property, rootPath, sourcePath.c_str(), el1Path); - - size_t bundleNameLen = strlen(bundleName); - if (IsUnlockStatus(info->uid, bundleName, bundleNameLen)) { + if (IsUnlockStatus(info->uid)) { return; } @@ -2121,8 +2112,10 @@ static void MountDirToShared(const AppSpawningCtx *property) static int SpawnMountDirToShared(AppSpawnMgr *content, AppSpawningCtx *property) { #ifndef APPSPAWN_SANDBOX_NEW - // mount dynamic directory - MountDirToShared(property); + if (!IsNWebSpawnMode(content)) { + // mount dynamic directory + MountDirToShared(property); + } #endif return 0; } diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index b9d79baf..42376733 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -54,7 +54,10 @@ #define FD_PATH_SIZE 128 #define MAX_MEM_SIZE (4 * 1024) #define PREFORK_PROCESS "apppool" -#define APPSPAWN_MSG_USER_CHECK_COUNT 3 +#define APPSPAWN_MSG_USER_CHECK_COUNT 4 +#define USER_ID_MIN_VALUE 100 +#define USER_ID_MAX_VALUE 10736 +#define LOCK_STATUS_PARAM_SIZE 64 #ifndef PIDFD_NONBLOCK #define PIDFD_NONBLOCK O_NONBLOCK #endif @@ -340,6 +343,7 @@ APPSPAWN_STATIC bool OnConnectionUserCheck(uid_t uid) 0, // root 0 3350, // app_fwk_update 3350 5523, // foundation 5523 + 1090, //storage_manager 1090 }; for (int i = 0; i < APPSPAWN_MSG_USER_CHECK_COUNT; i++) { @@ -1622,6 +1626,35 @@ APPSPAWN_STATIC int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message) return result; } +static void ProcessAppSpawnLockStatusMsg(AppSpawnMsgNode *message) +{ + APPSPAWN_CHECK_ONLY_EXPER(message != NULL, return); + uint32_t len = 0; + char *lockstatus = (char *)GetAppSpawnMsgExtInfo(message, "lockstatus", &len); + APPSPAWN_CHECK(lockstatus != NULL, return, "failed to get lockstatus"); + APPSPAWN_LOGI("appspawn get lockstatus %{public}s from storage_manager", lockstatus); + char *userLockStatus = NULL; + // userLockStatus format example: 100:0 100 for userid 0:unlock 1:lock + char *userIdStr = strtok_r(lockstatus, ":", &userLockStatus); + APPSPAWN_CHECK(userIdStr != NULL && userLockStatus != NULL, return, + "lockstatus not satisfied format, failed to get userLockStatus"); + int userId = atoi(userIdStr); + if (userId < USER_ID_MIN_VALUE || userId > USER_ID_MAX_VALUE) { + APPSPAWN_LOGE("userId err %{public}s", userIdStr); + return; + } + if (strcmp(userLockStatus, "0") != 0 && strcmp(userLockStatus, "1") != 0) { + APPSPAWN_LOGE("userLockStatus err %{public}s", userLockStatus); + return; + } + char lockStatusParam[LOCK_STATUS_PARAM_SIZE] = {0}; + int ret = snprintf_s(lockStatusParam, sizeof(lockStatusParam), sizeof(lockStatusParam) - 1, + "startup.appspawn.lockstatus_%d", userId); + APPSPAWN_CHECK(ret > 0, return, "get lock status param failed, errno %{public}d", errno); + ret = SetParameter(lockStatusParam, userLockStatus); + APPSPAWN_CHECK(ret == 0, return, "failed to set lockstatus param value ret %{public}d", ret); +} + static void ProcessRecvMsg(AppSpawnConnection *connection, AppSpawnMsgNode *message) { AppSpawnMsg *msg = &message->msgHeader; @@ -1674,6 +1707,11 @@ static void ProcessRecvMsg(AppSpawnConnection *connection, AppSpawnMsgNode *mess case MSG_UNINSTALL_DEBUG_HAP: ProcessUninstallDebugHap(connection, message); break; + case MSG_LOCK_STATUS: + ProcessAppSpawnLockStatusMsg(message); + SendResponse(connection, msg, 0, 0); + DeleteAppSpawnMsg(message); + break; default: SendResponse(connection, msg, APPSPAWN_MSG_INVALID, 0); DeleteAppSpawnMsg(message); -- Gitee