From 76a8ea2c2b7b40a1185a96c366c61770d8ad1697 Mon Sep 17 00:00:00 2001 From: luyifan <842825214@qq.com> Date: Sun, 27 Apr 2025 16:59:12 +0800 Subject: [PATCH] Send apiTargetVersion to /dev/xpm Signed-off-by: luyifan <842825214@qq.com> --- README.md | 2 +- README_zh.md | 2 +- .../include/code_sign_attr_utils.h | 3 ++- .../src/code_sign_attr_utils.c | 22 +++++++++++----- test/unittest/code_sign_attr_utils_test.cpp | 26 ++++++++++--------- test/unittest/jit_code_sign_test.cpp | 2 +- test/unittest/utils/src/xpm_common.cpp | 2 +- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 0a91acf..a7fe4ac 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 4f6d880..14080f9 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 5e54a34..fc57051 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 04a39fb..22d11db 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 3254bde..4f53d66 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 718610b..72018c0 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 7370b9f..2e34f93 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; } -- Gitee