1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "code_sign_attr_utils.h"
17 #include "ownerid_utils.h"
18
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <securec.h>
25 #include <sys/ioctl.h>
26
27 #include "errcode.h"
28 #include "log.h"
29
30 #define XPM_DEV_PATH "/dev/xpm"
31
32 #define XPM_SET_REGION _IOW('x', 0x01, struct XpmConfig)
33 #define XPM_SET_OWNERID _IOW('x', 0x02, struct XpmConfig)
34 #define XPM_SET_JITFORT_ENABLE _IOW('x', 0x3, unsigned long)
35
XpmIoctl(int fd,uint32_t cmd,struct XpmConfig * config)36 static int XpmIoctl(int fd, uint32_t cmd, struct XpmConfig *config)
37 {
38 int ret = ioctl(fd, cmd, config);
39 if (ret == -1) {
40 LOG_ERROR("Ioctl cmd %{public}x failed: %{public}s (ignore)", cmd, strerror(errno));
41 } else {
42 LOG_DEBUG("Ioctl cmd %{public}x success", cmd);
43 }
44 return CS_SUCCESS;
45 }
46
DoSetXpmOwnerId(int fd,uint32_t idType,const char * ownerId,uint32_t apiTargetVersion)47 static int DoSetXpmOwnerId(int fd, uint32_t idType, const char *ownerId, uint32_t apiTargetVersion)
48 {
49 struct XpmConfig config = {0};
50
51 if (idType >= PROCESS_OWNERID_MAX) {
52 LOG_ERROR("Input idType is invalid: %{public}u", idType);
53 return CS_ERR_PARAM_INVALID;
54 }
55
56 config.idType = idType;
57 if ((ownerId != NULL) && (strlen(ownerId) != 0)) {
58 if (memcpy_s(config.ownerId, sizeof(config.ownerId) - 1, ownerId, strlen(ownerId)) != EOK) {
59 LOG_ERROR("Memcpy ownerId failed, ownerId: %{public}s", ownerId);
60 return CS_ERR_MEMORY;
61 }
62 }
63 config.apiTargetVersion = apiTargetVersion;
64 LOG_DEBUG("Set type = %{public}u, ownerId = %{public}s, apiTargetVersion is %{public}u",
65 idType, ownerId ? ownerId : "NULL", apiTargetVersion);
66 (void)XpmIoctl(fd, XPM_SET_OWNERID, &config);
67 return CS_SUCCESS;
68 }
69
70 #define API_VERSION_DECIMAL 10
InitXpm(int enableJitFort,uint32_t idType,const char * ownerId,const char * apiTargetVersionStr)71 int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId, const char *apiTargetVersionStr)
72 {
73 // open /dev/xpm
74 int fd = open(XPM_DEV_PATH, O_RDWR);
75 if (fd == -1) {
76 LOG_INFO("Open device file failed: %{public}s (ignore)", strerror(errno));
77 return CS_SUCCESS;
78 }
79
80 // init xpm region
81 struct XpmConfig config = {0};
82 config.regionAddr = 0;
83 config.regionLength = XPM_REGION_LEN;
84 (void)XpmIoctl(fd, XPM_SET_REGION, &config);
85
86 // set owner id
87 int ret = CS_SUCCESS;
88 uint32_t apiTargetVersion = 0;
89 if (idType != PROCESS_OWNERID_UNINIT) {
90 idType = ConvertIdType(idType, ownerId);
91 if (apiTargetVersionStr != NULL) {
92 char *endPtr = NULL;
93 // we use 0 as default, and strtoul returns 0 if failed
94 apiTargetVersion = strtoul(apiTargetVersionStr, &endPtr, API_VERSION_DECIMAL);
95 }
96 ret = DoSetXpmOwnerId(fd, idType, ownerId, apiTargetVersion);
97 }
98
99 // enable jitfort
100 if (enableJitFort != 0) {
101 (void)XpmIoctl(fd, XPM_SET_JITFORT_ENABLE, NULL);
102 }
103
104 // close /dev/xpm
105 close(fd);
106 return ret;
107 }
108
SetXpmOwnerId(uint32_t idType,const char * ownerId)109 int SetXpmOwnerId(uint32_t idType, const char *ownerId)
110 {
111 int fd = open(XPM_DEV_PATH, O_RDWR);
112 if (fd == -1) {
113 LOG_INFO("Open device file failed: %{public}s (ignore)", strerror(errno));
114 return CS_SUCCESS;
115 }
116 int ret = DoSetXpmOwnerId(fd, idType, ownerId, 0);
117 close(fd);
118 return ret;
119 }
120