已开启
fix: 按新方案提交代码 #2949
fan-jingle创建于 8 天前
fix: 按新方案提交代码 #2949
已开启
共 2 个文件变更+194-3887
| @@ -86,12 +86,6 @@ static const PermissionCapabilityMap g_permissionCapabilityMap[] = { | |||
| 86 | {"ohos.permission.kernel.NET_RAW", CAP_NET_RAW}, | 86 | {"ohos.permission.kernel.NET_RAW", CAP_NET_RAW}, |
| 87 | }; | 87 | }; |
| 88 | 88 | ||
| 89 | -typedef struct { | ||
| 90 | - uint64_t caps; | ||
| 91 | - int capValues[ARRAY_LENGTH(g_permissionCapabilityMap)]; | ||
| 92 | - int capCount; | ||
| 93 | -} ExtPermResult; | ||
| 94 | - | ||
| 95 | int __attribute__((weak)) SetUserId(char *userIdStr) | 89 | int __attribute__((weak)) SetUserId(char *userIdStr) |
| 96 | { | 90 | { |
| 97 | APPSPAWN_LOGV("SetUserId called (weak implementation)"); | 91 | APPSPAWN_LOGV("SetUserId called (weak implementation)"); |
| @@ -154,67 +148,52 @@ static int SetAmbientCapability(int cap) | |||
| 154 | return 0; | 148 | return 0; |
| 155 | } | 149 | } |
| 156 | 150 | ||
| 157 | -APPSPAWN_STATIC void MatchPermToCap(const char *permName, ExtPermResult *result) | 151 | +APPSPAWN_STATIC void MatchPermToCap(const char *permName, uint64_t *caps) |
| 158 | { | 152 | { |
| 159 | for (size_t j = 0; j < ARRAY_LENGTH(g_permissionCapabilityMap); j++) { | 153 | for (size_t j = 0; j < ARRAY_LENGTH(g_permissionCapabilityMap); j++) { |
| 160 | if (strcmp(permName, g_permissionCapabilityMap[j].permissionName) != 0) { | 154 | if (strcmp(permName, g_permissionCapabilityMap[j].permissionName) != 0) { |
| 161 | continue; | 155 | continue; |
| 162 | } | 156 | } |
| 163 | - result->caps |= CAP_TO_MASK(g_permissionCapabilityMap[j].capability); | 157 | + *caps |= CAP_TO_MASK(g_permissionCapabilityMap[j].capability); |
| 164 | - APPSPAWN_LOGV("SetExtPermAmbient Permission %{public}s maps to cap %{public}d", | 158 | + APPSPAWN_LOGV("ExtPerm permission %{public}s maps to cap %{public}d", |
| 165 | permName, g_permissionCapabilityMap[j].capability); | 159 | permName, g_permissionCapabilityMap[j].capability); |
| 166 | - if (result->capCount < (int)ARRAY_LENGTH(g_permissionCapabilityMap)) { | ||
| 167 | - result->capValues[result->capCount++] = g_permissionCapabilityMap[j].capability; | ||
| 168 | - } | ||
| 169 | break; | 160 | break; |
| 170 | } | 161 | } |
| 171 | } | 162 | } |
| 172 | 163 | ||
| 173 | -APPSPAWN_STATIC void GetExtPermResult(const AppSpawningCtx *property, ExtPermResult *result) | 164 | +APPSPAWN_STATIC uint64_t GetExtPermCaps(const AppSpawningCtx *property) |
| 174 | { | 165 | { |
| 175 | - if (property == NULL || result == NULL) { | 166 | + uint64_t caps = 0; |
| 176 | - return; | 167 | + if (property == NULL) { |
| 168 | + return caps; | ||
| 177 | } | 169 | } |
| 178 | - result->caps = 0; | ||
| 179 | - result->capCount = 0; | ||
| 180 | uint32_t size = 0; | 170 | uint32_t size = 0; |
| 181 | char *extInfo = (char *)(GetAppSpawnMsgExtInfo(property->message, | 171 | char *extInfo = (char *)(GetAppSpawnMsgExtInfo(property->message, |
| 182 | MSG_EXT_NAME_JIT_PERMISSIONS, &size)); | 172 | MSG_EXT_NAME_JIT_PERMISSIONS, &size)); |
| 183 | - APPSPAWN_CHECK_ONLY_EXPER(size != 0 && extInfo != NULL, return); | 173 | + APPSPAWN_CHECK_ONLY_EXPER(size != 0 && extInfo != NULL, return caps); |
| 184 | - APPSPAWN_LOGV("GetExtPermResult: %{public}s", extInfo); | 174 | + APPSPAWN_LOGV("GetExtPermCaps: %{public}s", extInfo); |
| 185 | cJSON *extInfoJson = cJSON_Parse(extInfo); | 175 | cJSON *extInfoJson = cJSON_Parse(extInfo); |
| 186 | - APPSPAWN_CHECK(extInfoJson != NULL, return, | 176 | + APPSPAWN_CHECK(extInfoJson != NULL, return caps, |
| 187 | - "GetExtPermResult: Invalid ext info for %{public}s", MSG_EXT_NAME_JIT_PERMISSIONS); | 177 | + "GetExtPermCaps: Invalid ext info for %{public}s", MSG_EXT_NAME_JIT_PERMISSIONS); |
| 188 | - | 178 | + |
| 189 | cJSON *permissionsArray = cJSON_GetObjectItemCaseSensitive(extInfoJson, "permissions"); | 179 | cJSON *permissionsArray = cJSON_GetObjectItemCaseSensitive(extInfoJson, "permissions"); |
| 190 | if (permissionsArray == NULL || !cJSON_IsArray(permissionsArray)) { | 180 | if (permissionsArray == NULL || !cJSON_IsArray(permissionsArray)) { |
| 191 | cJSON_Delete(extInfoJson); | 181 | cJSON_Delete(extInfoJson); |
| 192 | - return; | 182 | + return caps; |
| 193 | } | 183 | } |
| 194 | int count = cJSON_GetArraySize(permissionsArray); | 184 | int count = cJSON_GetArraySize(permissionsArray); |
| 195 | 185 | ||
| 196 | for (int i = 0; i < count; i++) { | 186 | for (int i = 0; i < count; i++) { |
| 197 | cJSON *permItem = cJSON_GetArrayItem(permissionsArray, i); | 187 | cJSON *permItem = cJSON_GetArrayItem(permissionsArray, i); |
| 198 | - APPSPAWN_CHECK_ONLY_EXPER(permItem != NULL && permItem->child != NULL, continue); | 188 | + APPSPAWN_CHECK_ONLY_EXPER(permItem != NULL && permItem->child != NULL && |
| 199 | - MatchPermToCap(permItem->child->string, result); | 189 | + permItem->child->string != NULL, continue); |
| 190 | + MatchPermToCap(permItem->child->string, &caps); | ||
| 200 | } | 191 | } |
| 201 | cJSON_Delete(extInfoJson); | 192 | cJSON_Delete(extInfoJson); |
| 193 | + return caps; | ||
| 202 | } | 194 | } |
| 203 | 195 | ||
| 204 | -APPSPAWN_STATIC int SetExtPermAmbientFromResult(const ExtPermResult *result) | 196 | +APPSPAWN_STATIC int SetAmbientCapabilities(const AppSpawningCtx *property) |
| 205 | -{ | ||
| 206 | - if (result == NULL) { | ||
| 207 | - return 0; | ||
| 208 | - } | ||
| 209 | - for (int i = 0; i < result->capCount; i++) { | ||
| 210 | - APPSPAWN_CHECK(SetAmbientCapability(result->capValues[i]) == 0, | ||
| 211 | - return -1, "SetExtPermAmbientFromResult set ambient failed:%{public}d", result->capValues[i]); | ||
| 212 | - } | ||
| 213 | - return 0; | ||
| 214 | -} | ||
| 215 | - | ||
| 216 | - | ||
| 217 | -APPSPAWN_STATIC int SetAmbientCapabilities(const AppSpawningCtx *property, const ExtPermResult *result) | ||
| 218 | { | 197 | { |
| 219 | if (!IsNoShareFsEnable()) { | 198 | if (!IsNoShareFsEnable()) { |
| 220 | return 0; | 199 | return 0; |
| @@ -231,9 +210,7 @@ APPSPAWN_STATIC int SetAmbientCapabilities(const AppSpawningCtx *property, const | |||
| 231 | if (CheckAppMsgFlagsSet(property, APP_FLAGS_SET_CAPS_FOWNER)) { | 210 | if (CheckAppMsgFlagsSet(property, APP_FLAGS_SET_CAPS_FOWNER)) { |
| 232 | APPSPAWN_CHECK(SetAmbientCapability(CAP_FOWNER) == 0, return -1, "set ambient failed:%{public}d", CAP_FOWNER); | 211 | APPSPAWN_CHECK(SetAmbientCapability(CAP_FOWNER) == 0, return -1, "set ambient failed:%{public}d", CAP_FOWNER); |
| 233 | } | 212 | } |
| 234 | - | 213 | + return 0; |
| 235 | - int ret = SetExtPermAmbientFromResult(result); | ||
| 236 | - return ret; | ||
| 237 | } | 214 | } |
| 238 | 215 | ||
| 239 | APPSPAWN_STATIC int SetCapabilities(const AppSpawnMgr *content, const AppSpawningCtx *property) | 216 | APPSPAWN_STATIC int SetCapabilities(const AppSpawnMgr *content, const AppSpawningCtx *property) |
| @@ -251,9 +228,9 @@ APPSPAWN_STATIC int SetCapabilities(const AppSpawnMgr *content, const AppSpawnin | |||
| 251 | bool needExtPerm = IsNoShareFsEnable() && | 228 | bool needExtPerm = IsNoShareFsEnable() && |
| 252 | !CheckAppMsgFlagsSet(property, APP_FLAGS_ISOLATED_SANDBOX_TYPE) && | 229 | !CheckAppMsgFlagsSet(property, APP_FLAGS_ISOLATED_SANDBOX_TYPE) && |
| 253 | (IsAppSpawnMode(content) || IsNativeSpawnMode(content)); | 230 | (IsAppSpawnMode(content) || IsNativeSpawnMode(content)); |
| 254 | - ExtPermResult extResult = {0}; | 231 | + uint64_t extPermCaps = 0; |
| 255 | if (needExtPerm) { | 232 | if (needExtPerm) { |
| 256 | - GetExtPermResult(property, &extResult); | 233 | + extPermCaps = GetExtPermCaps(property); |
| 257 | } | 234 | } |
| 258 | // init inheritable permitted effective zero | 235 | // init inheritable permitted effective zero |
| 259 | 236 | ||
| @@ -262,7 +239,7 @@ APPSPAWN_STATIC int SetCapabilities(const AppSpawnMgr *content, const AppSpawnin | |||
| 262 | baseCaps = CAP_TO_MASK(CAP_DAC_OVERRIDE); | 239 | baseCaps = CAP_TO_MASK(CAP_DAC_OVERRIDE); |
| 263 | baseCaps |= CheckAppMsgFlagsSet(property, APP_FLAGS_CUSTOM_SANDBOX) ? CAP_TO_MASK(CAP_KILL) : 0; | 240 | baseCaps |= CheckAppMsgFlagsSet(property, APP_FLAGS_CUSTOM_SANDBOX) ? CAP_TO_MASK(CAP_KILL) : 0; |
| 264 | baseCaps |= CheckAppMsgFlagsSet(property, APP_FLAGS_SET_CAPS_FOWNER) ? CAP_TO_MASK(CAP_FOWNER) : 0; | 241 | baseCaps |= CheckAppMsgFlagsSet(property, APP_FLAGS_SET_CAPS_FOWNER) ? CAP_TO_MASK(CAP_FOWNER) : 0; |
| 265 | - baseCaps |= extResult.caps; | 242 | + baseCaps |= extPermCaps; |
| 266 | } | 243 | } |
| 267 | const uint64_t inheriTable = baseCaps; | 244 | const uint64_t inheriTable = baseCaps; |
| 268 | const uint64_t permitted = baseCaps; | 245 | const uint64_t permitted = baseCaps; |
| @@ -282,7 +259,7 @@ APPSPAWN_STATIC int SetCapabilities(const AppSpawnMgr *content, const AppSpawnin | |||
| 282 | APPSPAWN_CHECK(!isRet, return -errno, "Failed to capset errno: %{public}d", errno); | 259 | APPSPAWN_CHECK(!isRet, return -errno, "Failed to capset errno: %{public}d", errno); |
| 283 | 260 | ||
| 284 | if (needExtPerm) { | 261 | if (needExtPerm) { |
| 285 | - isRet = SetAmbientCapabilities(property, &extResult); | 262 | + isRet = SetAmbientCapabilities(property); |
| 286 | APPSPAWN_CHECK(!isRet, return -1, "Failed to set ambient"); | 263 | APPSPAWN_CHECK(!isRet, return -1, "Failed to set ambient"); |
| 287 | } | 264 | } |
| 288 | return 0; | 265 | return 0; |