diff --git a/README.md b/README.md index 0a91acf363d8a8856c8fa0206fc955df7bf0d10b..a7fe4ac5157ddd6cf07966d30608645ed4dfe342 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ The code signature component provides the following functions: | int32_t InitLocalCertificate(ByteBuffer &cert); | Initializes a local code signing certificate.| | int32_t SignLocalCode(const std::string &filePath, ByteBuffer &signature); | Signs the local code.| | int32_t SignLocalCode(const std::string &ownerID, const std::string &filePath, ByteBuffer &signature); | Signs the local code with the owner ID.| -| int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId); | Initializes XPM related resources(XPM region, JitFort, OwnerId).| +| int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId, const char *apiTargetVersionStr); | Initializes XPM related resources(XPM region, JitFort, OwnerId).| | int SetXpmOwnerId(uint32_t idType, const char *ownerId); | Sets an owner ID.| ### Usage Guidelines diff --git a/README_zh.md b/README_zh.md index 4f6d880bcf25c629f1a7fa939ecab1ee3fabe706..14080f93bf3b3e3659e10955113ba72bb8f88417 100644 --- a/README_zh.md +++ b/README_zh.md @@ -51,7 +51,7 @@ | int32_t InitLocalCertificate(ByteBuffer &cert); | 初始化本地签名证书 | | int32_t SignLocalCode(const std::string &filePath, ByteBuffer &signature); | 本地代码签名 | | int32_t SignLocalCode(const std::string &ownerID, const std::string &filePath, ByteBuffer &signature); | 带OwnerId的本地代码签名 | -| int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId); | 初始化XPM相关资源(XPM地址范围、JitFort模式、OwnerId配置)| +| int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId, const char *apiTargetVersionStr); | 初始化XPM相关资源(XPM地址范围、JitFort模式、OwnerId配置)| | int SetXpmOwnerId(uint32_t idType, const char *ownerId); | 设置OwnerId | | int32_t RegisterTmpBuffer(JitCodeSignerBase *signer, void *tmpBuffer); | 注册临时Buffer起始地址 | | int32_t AppendInstruction(JitCodeSignerBase *signer, Instr instr); | 对添加到临时Buffer的指令签名 | diff --git a/interfaces/inner_api/code_sign_attr_utils/include/code_sign_attr_utils.h b/interfaces/inner_api/code_sign_attr_utils/include/code_sign_attr_utils.h index 5e54a34cbf045ac056499d49d858010445e8ad0c..fc570515f7478c5aa4189fbbd89d5214b342c14f 100755 --- a/interfaces/inner_api/code_sign_attr_utils/include/code_sign_attr_utils.h +++ b/interfaces/inner_api/code_sign_attr_utils/include/code_sign_attr_utils.h @@ -72,9 +72,10 @@ struct XpmConfig { uint32_t idType; char ownerId[MAX_OWNERID_LEN]; + uint32_t apiTargetVersion; }; -int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId); +int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId, const char *apiTargetVersionStr); int SetXpmOwnerId(uint32_t idType, const char *ownerId); diff --git a/interfaces/inner_api/code_sign_attr_utils/src/code_sign_attr_utils.c b/interfaces/inner_api/code_sign_attr_utils/src/code_sign_attr_utils.c index 04a39fbacd9bae3309c3dd66d965e6dd27ed6e24..22d11dbf2e5123de6aac40cc95c66334a09b675f 100755 --- a/interfaces/inner_api/code_sign_attr_utils/src/code_sign_attr_utils.c +++ b/interfaces/inner_api/code_sign_attr_utils/src/code_sign_attr_utils.c @@ -17,6 +17,8 @@ #include "ownerid_utils.h" #include +#include +#include #include #include #include @@ -42,7 +44,7 @@ static int XpmIoctl(int fd, uint32_t cmd, struct XpmConfig *config) return CS_SUCCESS; } -static int DoSetXpmOwnerId(int fd, uint32_t idType, const char *ownerId) +static int DoSetXpmOwnerId(int fd, uint32_t idType, const char *ownerId, uint32_t apiTargetVersion) { struct XpmConfig config = {0}; @@ -58,13 +60,15 @@ static int DoSetXpmOwnerId(int fd, uint32_t idType, const char *ownerId) return CS_ERR_MEMORY; } } - - LOG_DEBUG("Set type = %{public}u, ownerId = %{public}s", idType, ownerId ? ownerId : "NULL"); + config.apiTargetVersion = apiTargetVersion; + LOG_DEBUG("Set type = %{public}u, ownerId = %{public}s, apiTargetVersion is %{public}d", + idType, ownerId ? ownerId : "NULL", apiTargetVersion); (void)XpmIoctl(fd, XPM_SET_OWNERID, &config); return CS_SUCCESS; } -int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId) +#define API_VERSION_DECIMAL 10 +int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId, const char *apiTargetVersionStr) { // open /dev/xpm int fd = open(XPM_DEV_PATH, O_RDWR); @@ -86,9 +90,15 @@ int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId) // set owner id int ret = CS_SUCCESS; + uint32_t apiTargetVersion = 0; if (idType != PROCESS_OWNERID_UNINIT) { idType = ConvertIdType(idType, ownerId); - ret = DoSetXpmOwnerId(fd, idType, ownerId); + if (apiTargetVersionStr != NULL) { + char *endPtr = NULL; + /* we use 0 as default, and strtoul returns 0 if failed */ + apiTargetVersion = strtoul(apiTargetVersionStr, &endPtr, API_VERSION_DECIMAL); + } + ret = DoSetXpmOwnerId(fd, idType, ownerId, apiTargetVersion); } // close /dev/xpm @@ -103,7 +113,7 @@ int SetXpmOwnerId(uint32_t idType, const char *ownerId) LOG_INFO("Open device file failed: %{public}s (ignore)", strerror(errno)); return CS_SUCCESS; } - int ret = DoSetXpmOwnerId(fd, idType, ownerId); + int ret = DoSetXpmOwnerId(fd, idType, ownerId, 0); close(fd); return ret; } diff --git a/test/unittest/code_sign_attr_utils_test.cpp b/test/unittest/code_sign_attr_utils_test.cpp index 3254bde62839d1ec489a814aa743647ffd17b432..4f53d662f2e233c2e6c6676188f4eac032d54817 100644 --- a/test/unittest/code_sign_attr_utils_test.cpp +++ b/test/unittest/code_sign_attr_utils_test.cpp @@ -53,16 +53,18 @@ public: */ HWTEST_F(CodeSignAttrUtilsTest, CodeSignAttrUtilsTest_0001, TestSize.Level0) { - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_COMPAT, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_DEBUG, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_EXTEND, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_DEBUG_PLATFORM, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_PLATFORM, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_NWEB, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_SHARED, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_SYSTEM, NULL), CS_SUCCESS); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, "test"), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_COMPAT, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_DEBUG, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_EXTEND, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_DEBUG_PLATFORM, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_PLATFORM, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_NWEB, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_SHARED, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_SYSTEM, NULL, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, "test", NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, "test", "20"), CS_SUCCESS); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, "test", "NaN"), CS_SUCCESS); } /** @@ -74,11 +76,11 @@ HWTEST_F(CodeSignAttrUtilsTest, CodeSignAttrUtilsTest_0001, TestSize.Level0) HWTEST_F(CodeSignAttrUtilsTest, CodeSignAttrUtilsTest_0002, TestSize.Level0) { // test invalid ownerid type - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_MAX, NULL), CS_ERR_PARAM_INVALID); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_MAX, NULL, NULL), CS_ERR_PARAM_INVALID); // test invalid ownerid valud char ownerid[MAX_OWNERID_LEN + 1] = { 0 }; (void)memset_s(ownerid, MAX_OWNERID_LEN + 1, 'a', MAX_OWNERID_LEN + 1); - EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, ownerid), CS_ERR_MEMORY); + EXPECT_EQ(InitXpm(0, PROCESS_OWNERID_APP, ownerid, NULL), CS_ERR_MEMORY); } /** diff --git a/test/unittest/jit_code_sign_test.cpp b/test/unittest/jit_code_sign_test.cpp index 718610b47e7c15633e25a558457a7a754b442878..72018c06e0bec1ede21be14d469b03566ba38e1a 100644 --- a/test/unittest/jit_code_sign_test.cpp +++ b/test/unittest/jit_code_sign_test.cpp @@ -108,7 +108,7 @@ static inline void AllocJitMemory() static inline void JitFortPrepare() { #ifndef JIT_FORT_DISABLE - EXPECT_EQ(InitXpm(1, PROCESS_OWNERID_UNINIT, NULL), CS_SUCCESS); + EXPECT_EQ(InitXpm(1, PROCESS_OWNERID_UNINIT, NULL, NULL), CS_SUCCESS); PrctlWrapper(JITFORT_PRCTL_OPTION, JITFORT_CREATE_COPGTABLE); #endif } diff --git a/test/unittest/utils/src/xpm_common.cpp b/test/unittest/utils/src/xpm_common.cpp index 7370b9f0242474928ccae156a3140ad86a23b16f..2e34f9338836291ae307612c14cdcdf08309ff3b 100644 --- a/test/unittest/utils/src/xpm_common.cpp +++ b/test/unittest/utils/src/xpm_common.cpp @@ -43,7 +43,7 @@ constexpr unsigned long XPM_PROC_LENGTH = 50; static int GetXpmRegion(struct XpmRegionArea &area) { - if (InitXpm(0, PROCESS_OWNERID_UNINIT, NULL) != 0) { + if (InitXpm(0, PROCESS_OWNERID_UNINIT, NULL, NULL) != 0) { LOG_ERROR("init xpm region failed"); return -1; }