1 /*
2 * Copyright (c) 2024 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 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include "bootstage.h"
20 #include "init_log.h"
21 #include "init_module_engine.h"
22 #include "init_service.h"
23
24 #define SA_MAIN_PATH ("/system/bin/sa_main")
25
26 #define OH_ENCAPS_PROC_TYPE_BASE 0x18
27 #define OH_ENCAPS_PERMISSION_TYPE_BASE 0x1A
28 #define OH_ENCAPS_MAGIC 'E'
29 #define OH_PROC_SYS 3
30 #define SET_PROC_TYPE_CMD _IOW(OH_ENCAPS_MAGIC, OH_ENCAPS_PROC_TYPE_BASE, uint32_t)
31 #define SET_KERNEL_PERM_TYPE_CMD _IOW(OH_ENCAPS_MAGIC, OH_ENCAPS_PERMISSION_TYPE_BASE, char *)
32
SetKernelPerm(SERVICE_INFO_CTX * serviceCtx)33 static void SetKernelPerm(SERVICE_INFO_CTX *serviceCtx)
34 {
35 int fd = 0;
36 int ret = 0;
37 int procType = OH_PROC_SYS;
38
39 fd = open("/dev/encaps", O_RDWR);
40 if (fd < 0) {
41 INIT_LOGI("SetKernelPerm open failed, maybe this device is not supported");
42 return;
43 }
44
45 ret = ioctl(fd, SET_PROC_TYPE_CMD, &procType);
46 if (ret != 0) {
47 close(fd);
48 INIT_LOGE("SetKernelPerm set flag failed %d", ret);
49 return;
50 }
51 Service *service = GetServiceByName(serviceCtx->serviceName);
52 if (service == NULL) {
53 close(fd);
54 INIT_LOGE("SetKernelPerm get service failed");
55 return;
56 }
57 if (service->kernelPerms != NULL) {
58 ret = ioctl(fd, SET_KERNEL_PERM_TYPE_CMD, service->kernelPerms);
59 if (ret != 0) {
60 INIT_LOGE("SetKernelPerm set encaps permission failed");
61 }
62 }
63 close(fd);
64 }
65
SetKernelPermForSa(SERVICE_INFO_CTX * serviceCtx)66 static void SetKernelPermForSa(SERVICE_INFO_CTX *serviceCtx)
67 {
68 if (serviceCtx->reserved == NULL) {
69 return;
70 }
71 if (strncmp(SA_MAIN_PATH, serviceCtx->reserved, strlen(SA_MAIN_PATH)) == 0) {
72 SetKernelPerm(serviceCtx);
73 }
74 }
75
MODULE_CONSTRUCTOR(void)76 MODULE_CONSTRUCTOR(void)
77 {
78 // Add hook to set encaps
79 InitAddServiceHook(SetKernelPermForSa, INIT_SERVICE_SET_PERMS_BEFORE);
80 }
81