diff --git a/BUILD.gn b/BUILD.gn index 870dbae9a6a1b2a8eb82418c228feaf7d495eee1..c7becf11ea706d57763d3b78c6976e11360fe6c1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -29,6 +29,7 @@ sources_platform_common = [ "./src/create_pcid.c", "./src/endian_internal.c", "./src/context_tool.c", + "./src/common_method.c", ] ohos_executable("syscap_tool_bin") { @@ -97,6 +98,7 @@ if (defined(ohos_lite)) { "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", + "./src/common_method.c", ] deps += [ @@ -121,6 +123,7 @@ if (defined(ohos_lite)) { "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", + "./src/common_method.c", ] external_deps = [ "bounds_checking_function:libsec_static", diff --git a/include/codec_config/syscap_define.h b/include/codec_config/syscap_define.h index d1675f6bb1d3802df26dbf3fd2ba8bce9c2bf32c..ce2e07d259d46a7b49d3099b83fe86b2cc324045 100644 --- a/include/codec_config/syscap_define.h +++ b/include/codec_config/syscap_define.h @@ -354,6 +354,19 @@ typedef enum SystemCapabilityNum { ABILITY_APPEXTENSION_PHOTOEDITOREXTENSION, HIVIEWDFX_HICOLLIE, BUNDLEMANAGER_APPDOMAINVERIFY, + COMMUNICATION_NETMANAGER_BLUETOOTH, + NOTIFICATION_NOTIFICATIONSETTINGS, + MULTIMEDIA_VIDEOPROCESSINGENGINE, + SECURITY_CRYPTOFRAMEWORK_KEY, + SECURITY_CRYPTOFRAMEWORK_KEY_SYMKEY, + SECURITY_CRYPTOFRAMEWORK_KEY_ASYMKEY, + SECURITY_CRYPTOFRAMEWORK_SIGNATURE, + SECURITY_CRYPTOFRAMEWORK_CIPHER, + SECURITY_CRYPTOFRAMEWORK_KEYAGREEMENT, + SECURITY_CRYPTOFRAMEWORK_MESSAGEDIGEST, + SECURITY_CRYPTOFRAMEWORK_MAC, + SECURITY_CRYPTOFRAMEWORK_KDF, + SECURITY_CRYPTOFRAMEWORK_RAND, // Add before here SYSCAP_BASIC_END = 500, } SyscapNum; @@ -691,6 +704,19 @@ const static SyscapWithNum g_arraySyscap[] = { {"SystemCapability.Customization.CustomConfig", CUSTOMIZATION_CUSTOMCONFIG}, {"SystemCapability.Ability.AppExtension.PhotoEditorExtension", ABILITY_APPEXTENSION_PHOTOEDITOREXTENSION}, {"SystemCapability.BundleManager.AppDomainVerify", BUNDLEMANAGER_APPDOMAINVERIFY}, + {"SystemCapability.Communication.NetManager.Bluetooth", COMMUNICATION_NETMANAGER_BLUETOOTH}, + {"SystemCapability.Notification.NotificationSettings", NOTIFICATION_NOTIFICATIONSETTINGS}, + {"SystemCapability.Multimedia.VideoProcessingEngine", MULTIMEDIA_VIDEOPROCESSINGENGINE}, + {"SystemCapability.Security.CryptoFramework.Key", SECURITY_CRYPTOFRAMEWORK_KEY}, + {"SystemCapability.Security.CryptoFramework.Key.SymKey", SECURITY_CRYPTOFRAMEWORK_KEY_SYMKEY}, + {"SystemCapability.Security.CryptoFramework.Key.AsymKey", SECURITY_CRYPTOFRAMEWORK_KEY_ASYMKEY}, + {"SystemCapability.Security.CryptoFramework.Signature", SECURITY_CRYPTOFRAMEWORK_SIGNATURE}, + {"SystemCapability.Security.CryptoFramework.Cipher", SECURITY_CRYPTOFRAMEWORK_CIPHER}, + {"SystemCapability.Security.CryptoFramework.KeyAgreement", SECURITY_CRYPTOFRAMEWORK_KEYAGREEMENT}, + {"SystemCapability.Security.CryptoFramework.MessageDigest", SECURITY_CRYPTOFRAMEWORK_MESSAGEDIGEST}, + {"SystemCapability.Security.CryptoFramework.Mac", SECURITY_CRYPTOFRAMEWORK_MAC}, + {"SystemCapability.Security.CryptoFramework.Kdf", SECURITY_CRYPTOFRAMEWORK_KDF}, + {"SystemCapability.Security.CryptoFramework.Rand", SECURITY_CRYPTOFRAMEWORK_RAND}, }; #ifdef __cplusplus diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 017eb5b7523edf5f04b7d9d111ae090cc219db0f..0580c69396f0b19915b1e2a3e9a1bc147407d14b 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -26,6 +26,7 @@ #include "endian_internal.h" #include "syscap_interface.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -292,6 +293,10 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; cJSON *sysCapJson = cJSON_CreateArray(); + if (sysCapJson == NULL) { + PRINT_ERR("Get sysCapJson failed, sysCapJson is empty.\n"); + ret = -1; + } for (i = 0; i < sysCapCount; i++) { char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; if (strlen(temp) >= SINGLE_FEAT_LEN) { @@ -443,7 +448,7 @@ static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAf { switch (type) { case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID: - free(freeAfterDecodeRpcidInfo.priSyscap); + SafeFree(freeAfterDecodeRpcidInfo.priSyscap); free(freeAfterDecodeRpcidInfo.osSysCapIndex); cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine); cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot); @@ -495,6 +500,11 @@ char *DecodeRpcidToStringFormat(const char *inputFile) // parse rpcid to json freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject(); + if (freeAfterDecodeRpcidInfo.rpcidRoot == NULL) { + PRINT_ERR("Failed to create cJSON object for rpcidRoot\n"); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer); + } + if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) { PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer); diff --git a/src/common_method.c b/src/common_method.c new file mode 100644 index 0000000000000000000000000000000000000000..cf7880f668cf97524f2adc221be591510b2a197d --- /dev/null +++ b/src/common_method.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024-2024 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 "common_method.h" +#include + +void SafeFree(char *pointer) +{ + if ((pointer) != NULL) { + free(pointer); + } +} diff --git a/src/common_method.h b/src/common_method.h new file mode 100644 index 0000000000000000000000000000000000000000..a9030bbc6af74059c1cc4e928e7537542bb7f3b2 --- /dev/null +++ b/src/common_method.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024-2024 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 COMMON_METHOD_H +#define COMMON_METHOD_H + +#include + +void SafeFree(char *pointer); + +#endif \ No newline at end of file diff --git a/src/context_tool.c b/src/context_tool.c index 1440a6bc3c60a28a667742b63f39c41140d630af..ea5359fdecfe6b2495939377eb64a1fd891a9bdd 100644 --- a/src/context_tool.c +++ b/src/context_tool.c @@ -155,23 +155,25 @@ int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len) } if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile); - free(contextBuffer); + FreeContextBuffer(contextBuffer); return -1; } rpcidHeader = (RPCIDHead *)contextBuffer; if (rpcidHeader->apiVersionType != 1) { PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile); - free(contextBuffer); + FreeContextBuffer(contextBuffer); return -1; } sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1)); if (sysCaptype != 2) { // 2, app syscap type PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile); + FreeContextBuffer(contextBuffer); return -1; } sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t))); if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) { PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile); + FreeContextBuffer(contextBuffer); return -1; } diff --git a/src/create_pcid.c b/src/create_pcid.c index 4972ac92879591ab66fb310e38c6c5751389d63f..3c0db875fe8157b2cb43e08970a2e4f748571e99 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -25,6 +25,7 @@ #include "endian_internal.h" #include "create_pcid.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -58,7 +59,6 @@ struct FreeEncodePcidInfo { char *outDirPathFinal; }; - struct FreeDecodePcidJsonInfo { char *strJson; char *contextBuffer; @@ -271,6 +271,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os"); cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private"); + ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize); if (ret != 0) { return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0); @@ -358,7 +359,7 @@ static int32_t GetPriSyscapResult(cJSON *capVectorPtr, int32_t ret) return ret; } -int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufLen) +static int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, int32_t contextBufLen) { cJSON *capVectorPtr = cJSON_CreateArray(); if (capVectorPtr == NULL) { @@ -366,12 +367,7 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL return -1; } - if (contextBufLen > UINT32_MAX) { - PRINT_ERR("the data privateSyscapLen is out of scope."); - return GetPriSyscapResult(capVectorPtr, -1); - } - - int32_t privateSyscapLen = (int32_t)(contextBufLen - sizeof(PCIDMain) - 1); + int32_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1; if (privateSyscapLen < 0 || privateSyscapLen > INT32_MAX) { PRINT_ERR("parse private syscap failed."); return GetPriSyscapResult(capVectorPtr, -1); @@ -454,7 +450,6 @@ static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInf case FREE_DECODE_PCID_CONVERT_OUT: free(freePcidJsonInfo.strJson); cJSON_Delete(freePcidJsonInfo.jsonRootObj); - cJSON_Delete(freePcidJsonInfo.sysCapObj); FreeContextBuffer(freePcidJsonInfo.contextBuffer); break; case FREE_DECODE_PCID_ROOT_OUT: @@ -467,6 +462,7 @@ static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInf FreeContextBuffer(freePcidJsonInfo.contextBuffer); break; case FREE_DECODE_PCID_CONTEXT_OUT: + cJSON_Delete(freePcidJsonInfo.sysCapObj); default: FreeContextBuffer(freePcidJsonInfo.contextBuffer); } @@ -526,15 +522,14 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret); } - freePcidJsonInfo.sysCapObj = NULL; // avoid being released repeatedly. + freePcidJsonInfo.strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj); - char *strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj); const char outputFileName[] = "pcid.json"; - ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, strJson, strlen(strJson)); + ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, freePcidJsonInfo.strJson, + strlen(freePcidJsonInfo.strJson)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName); } - free(strJson); return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret); } @@ -665,7 +660,6 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt cJSON *sysCapArray = cJSON_CreateArray(); if (sysCapArray == NULL) { PRINT_ERR("Create cJSON array failed.\n"); - free(sysCapArray); return -1; } if (priSyscapStringLen == 0) { @@ -701,7 +695,6 @@ static int32_t GetSyscapStr(char *input, char const *priSyscapStr, uint32_t* osS uint32_t fileContextLen; if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", input); - free(ctx); return -1; } if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) { @@ -719,6 +712,7 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) uint32_t osSyscap[OS_SYSCAP_NUM] = {0}; uint32_t pcidHeader[PCID_HEADER]; char *priSyscapStr = NULL; + char *jsonBuffer = NULL; ret = GetSyscapStr(input, priSyscapStr, osSyscap, pcidHeader); if (ret == -1) { return ret; @@ -727,40 +721,44 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) // add to json object cJSON *sysCapObj = cJSON_CreateObject(); cJSON *rootObj = cJSON_CreateObject(); + if (sysCapObj == NULL || rootObj == NULL) { + PRINT_ERR("Failed to create cJSON objects.\n"); + goto FAILED; + } + if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) { PRINT_ERR("Add syscap to json failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) { PRINT_ERR("Add header to json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) { PRINT_ERR("Add os syscap json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } - if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) { + if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t) strlen(priSyscapStr), sysCapObj) != 0) { PRINT_ERR("Add private syscap json object failed.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } // save as json file - char *jsonBuffer = cJSON_Print(rootObj); + jsonBuffer = cJSON_Print(rootObj); if (jsonBuffer == NULL) { PRINT_ERR("json buffer is null.\n"); - goto ADD_JSON_FAILED; + goto FAILED; } const char outputFileName[] = "pcid.json"; - if (ConvertedContextSaveAsFile(outDirPath, outputFileName, - jsonBuffer, strlen(jsonBuffer)) != 0) { + if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer)) != 0) { PRINT_ERR("Save as json file failed.\n"); - goto SAVE_FAILED; + goto FAILED; } ret = 0; -SAVE_FAILED: - free(jsonBuffer); -ADD_JSON_FAILED: +FAILED: + SafeFree(jsonBuffer); + SafeFree(priSyscapStr); cJSON_Delete(sysCapObj); cJSON_Delete(rootObj); return ret; diff --git a/src/syscap_tool.c b/src/syscap_tool.c index b2ab4fdb4581cd1dbd7cd0b19f4e2d5e44e130a7..93d33448ecb382319c57cbddecd869a5e5d6f292 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -27,6 +27,7 @@ #include "create_pcid.h" #include "syscap_tool.h" #include "context_tool.h" +#include "common_method.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -71,6 +72,14 @@ struct FreeAfterEncodeRpcidscInfo { int16_t flag; }; +static void FreePointerMemory(char *pcidContent, char *rpcidContent, char *pcidPriSyscap, char *rpcidPriSyscap) +{ + SafeFree(pcidContent); + SafeFree(rpcidPriSyscap); + SafeFree(rpcidContent); + SafeFree(pcidPriSyscap); +} + static int32_t FillOsCapLength(char *convertedBuffer, char *contextBuffer, struct JsonObjectSysCap gJsonObjectSysCap, uint32_t sysCapSize, int32_t ret) { @@ -204,6 +213,11 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; cJSON *sysCapJson = cJSON_CreateArray(); + if (sysCapJson == NULL) { + PRINT_ERR("Failed to create sysCapJson array\n"); + return -1; + } + for (i = 0; i < sysCapCount; i++) { char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) { @@ -263,13 +277,16 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath) // save to json file convertedBuffer = cJSON_Print(rpcidRoot); + if (convertedBuffer == NULL) { + PRINT_ERR("cJSON_Print failed to create JSON string\n"); + goto FREE_RPCID_ROOT; + } + ret = ConvertedContextSaveAsFile(outputPath, "rpcid.json", convertedBuffer, strlen(convertedBuffer)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath); - free(convertedBuffer); goto FREE_RPCID_ROOT; } - free(convertedBuffer); FREE_RPCID_ROOT: cJSON_Delete(rpcidRoot); @@ -705,10 +722,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ &rpcidPriSyscap, &rpcidPriSyscapLen); if (ret != 0) { PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret); - free(pcidContent); - free(rpcidContent); - free(pcidPriSyscap); - free(rpcidPriSyscap); + FreePointerMemory(pcidContent, rpcidContent, pcidPriSyscap, rpcidPriSyscap); return -1; } @@ -720,5 +734,6 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ } else { printf("Fail! The pcid does not meet the rpcid\n"); } + return 0; -} \ No newline at end of file +} diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index 668ac7ded2f1467ea3f9c6cee20268b6d330922e..262134296f2008458458a825b58e9adc52aff787 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -41,6 +41,7 @@ if (defined(ohos_lite)) { "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", + "../../../src/common_method.c", "syscap_codec_test.cpp", ] @@ -82,6 +83,7 @@ if (defined(ohos_lite)) { "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", + "../../../src/common_method.c", "syscap_codec_test.cpp", ]