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 <unistd.h>
21 #include <string.h>
22 #include <securec.h>
23 #include <sys/ioctl.h>
24
25 #include "errcode.h"
26 #include "log.h"
27
28 #define XPM_DEV_PATH "/dev/xpm"
29
30 #define XPM_SET_REGION _IOW('x', 0x01, struct XpmConfig)
31 #define XPM_SET_OWNERID _IOW('x', 0x02, struct XpmConfig)
32 #define XPM_SET_JITFORT_ENABLE _IOW('x', 0x3, unsigned long)
33
XpmIoctl(int fd,uint32_t cmd,struct XpmConfig * config)34 static int XpmIoctl(int fd, uint32_t cmd, struct XpmConfig *config)
35 {
36 int ret = ioctl(fd, cmd, config);
37 if (ret == -1) {
38 LOG_ERROR("Ioctl cmd %{public}x failed: %{public}s (ignore)", cmd, strerror(errno));
39 } else {
40 LOG_DEBUG("Ioctl cmd %{public}x success", cmd);
41 }
42 return CS_SUCCESS;
43 }
44
DoSetXpmOwnerId(int fd,uint32_t idType,const char * ownerId)45 static int DoSetXpmOwnerId(int fd, uint32_t idType, const char *ownerId)
46 {
47 struct XpmConfig config = {0};
48
49 if (idType >= PROCESS_OWNERID_MAX) {
50 LOG_ERROR("Input idType is invalid: %{public}u", idType);
51 return CS_ERR_PARAM_INVALID;
52 }
53
54 config.idType = idType;
55 if ((ownerId != NULL) && (strlen(ownerId) != 0)) {
56 if (memcpy_s(config.ownerId, sizeof(config.ownerId) - 1, ownerId, strlen(ownerId)) != EOK) {
57 LOG_ERROR("Memcpy ownerId failed, ownerId: %{public}s", ownerId);
58 return CS_ERR_MEMORY;
59 }
60 }
61
62 LOG_DEBUG("Set type = %{public}u, ownerId = %{public}s", idType, ownerId ? ownerId : "NULL");
63 (void)XpmIoctl(fd, XPM_SET_OWNERID, &config);
64 return CS_SUCCESS;
65 }
66
InitXpm(int enableJitFort,uint32_t idType,const char * ownerId)67 int InitXpm(int enableJitFort, uint32_t idType, const char *ownerId)
68 {
69 // open /dev/xpm
70 int fd = open(XPM_DEV_PATH, O_RDWR);
71 if (fd == -1) {
72 LOG_INFO("Open device file failed: %{public}s (ignore)", strerror(errno));
73 return CS_SUCCESS;
74 }
75
76 // init xpm region
77 struct XpmConfig config = {0};
78 config.regionAddr = 0;
79 config.regionLength = XPM_REGION_LEN;
80 (void)XpmIoctl(fd, XPM_SET_REGION, &config);
81
82 // enable jitfort
83 if (enableJitFort != 0) {
84 (void)XpmIoctl(fd, XPM_SET_JITFORT_ENABLE, NULL);
85 }
86
87 // set owner id
88 int ret = CS_SUCCESS;
89 if (idType != PROCESS_OWNERID_UNINIT) {
90 idType = ConvertIdType(idType, ownerId);
91 ret = DoSetXpmOwnerId(fd, idType, ownerId);
92 }
93
94 // close /dev/xpm
95 close(fd);
96 return ret;
97 }
98
SetXpmOwnerId(uint32_t idType,const char * ownerId)99 int SetXpmOwnerId(uint32_t idType, const char *ownerId)
100 {
101 int fd = open(XPM_DEV_PATH, O_RDWR);
102 if (fd == -1) {
103 LOG_INFO("Open device file failed: %{public}s (ignore)", strerror(errno));
104 return CS_SUCCESS;
105 }
106 int ret = DoSetXpmOwnerId(fd, idType, ownerId);
107 close(fd);
108 return ret;
109 }
110