• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "plugin_adapter.h"
18 
19 #include <stddef.h>
20 #include "cred_plugin_def.h"
21 #include "ext_plugin_manager.h"
22 #include "dev_auth_dynamic_load.h"
23 #include "dev_auth_module_manager.h"
24 #include "device_auth_defines.h"
25 #include "device_auth_ext.h"
26 #include "ext_part_proxy.h"
27 #include "hc_log.h"
28 
29 typedef const CredPlugin *(*GetCredPluginFunc)(void);
30 typedef const AuthModuleBase *(*GetAuthModulePluginFunc)(void);
31 
32 #define CRED_PLUGIN_FUNC "GetExtendCredPlugin"
33 #define AUTH_MODULE_PLUGIN_FUNC "GetExtendAuthModulePlugin"
34 
35 #define FUNC_NAME_INIT_EXT_PART "InitExtPart"
36 #define FUNC_NAME_EXT_PLUGIN_LIST "GetExtPlugins"
37 #define FUNC_NAME_DESTROY_EXT_PART "DestroyExtPart"
38 #define LIBDEVICE_AUTH_EXT "libdevice_auth_ext.z.so"
39 
40 static void *g_handle = NULL;
41 static ExtPartProxy g_pluginFunc;
42 
43 #ifdef DEVAUTH_ENABLE_RUN_ON_DEMAND_QOS
44 #include <unistd.h>
45 #include <sys/resource.h>
46 #include <sys/syscall.h>
47 
48 static const int OPEN_SO_PRIO = -20;
49 static const int NORMAL_PRIO = 0;
50 
SetThreadPrio(int priority)51 static void SetThreadPrio(int priority)
52 {
53     id_t tid = syscall(SYS_gettid);
54     LOGI("set tid:%" LOG_PUB "d priority:%" LOG_PUB "d.", tid, priority);
55     if (setpriority(PRIO_PROCESS, tid, priority) != 0) {
56         LOGE("set tid:%" LOG_PUB "d priority:%" LOG_PUB "d failed.", tid, priority);
57     }
58 }
59 #endif
60 
GetPluginFuncFromLib(void * handle)61 static const ExtPartProxy *GetPluginFuncFromLib(void *handle)
62 {
63     do {
64         g_pluginFunc.initExtPartFunc = DevAuthDlsym(handle, FUNC_NAME_INIT_EXT_PART);
65         if (g_pluginFunc.initExtPartFunc == NULL) {
66             LOGE("[Plugin]: Get init func from dynamic plugin fail.");
67             break;
68         }
69         g_pluginFunc.getPluginListFunc = DevAuthDlsym(handle, FUNC_NAME_EXT_PLUGIN_LIST);
70         if (g_pluginFunc.getPluginListFunc == NULL) {
71             LOGE("[Plugin]: Get plug list func from dynamic plugin fail.");
72             break;
73         }
74         g_pluginFunc.destroyExtPartFunc = DevAuthDlsym(handle, FUNC_NAME_DESTROY_EXT_PART);
75         if (g_pluginFunc.destroyExtPartFunc == NULL) {
76             LOGE("[Plugin]: Get destroy func from dynamic plugin fail.");
77             break;
78         }
79         return &g_pluginFunc;
80     } while (0);
81     return NULL;
82 }
83 
GetCredPluginFromLib(void * handle)84 static const CredPlugin *GetCredPluginFromLib(void *handle)
85 {
86     GetCredPluginFunc getCredPluginFunc = (GetCredPluginFunc)DevAuthDlsym(handle, CRED_PLUGIN_FUNC);
87     if (getCredPluginFunc == NULL) {
88         LOGE("[Plugin]: get func from dynamic plugin fail.");
89         return NULL;
90     }
91     return getCredPluginFunc();
92 }
93 
GetAuthModulePluginFromLib(void * handle)94 static const AuthModuleBase *GetAuthModulePluginFromLib(void *handle)
95 {
96     GetAuthModulePluginFunc getAuthModulePluginFunc =
97         (GetAuthModulePluginFunc)DevAuthDlsym(handle, AUTH_MODULE_PLUGIN_FUNC);
98     if (getAuthModulePluginFunc == NULL) {
99         LOGE("[Plugin]: get func from dynamic plugin fail.");
100         return NULL;
101     }
102     return getAuthModulePluginFunc();
103 }
104 
LoadDynamicPlugin(void * handle)105 static int32_t LoadDynamicPlugin(void *handle)
106 {
107     const CredPlugin *credPlugin = GetCredPluginFromLib(handle);
108     int32_t res = HC_SUCCESS;
109     if (credPlugin != NULL) {
110         res = AddCredPlugin(credPlugin);
111         if (res != HC_SUCCESS) {
112             LOGE("[Plugin]: init cred plugin fail. [Res]: %" LOG_PUB "d", res);
113             return res;
114         }
115     }
116     const AuthModuleBase *authModulePlugin = GetAuthModulePluginFromLib(handle);
117     if (authModulePlugin != NULL) {
118         res = AddAuthModulePlugin(authModulePlugin);
119         if (res != HC_SUCCESS) {
120             LOGE("[Plugin]: init auth module plugin fail. [Res]: %" LOG_PUB "d", res);
121             return res;
122         }
123     }
124     const ExtPartProxy *pluginFunc = GetPluginFuncFromLib(handle);
125     if (pluginFunc != NULL) {
126         res = AddExtPlugin(pluginFunc);
127         if (res != HC_SUCCESS) {
128             LOGE("[Plugin]: init ext plugin fail. [Res]: %" LOG_PUB "d", res);
129             return res;
130         }
131     }
132     return HC_SUCCESS;
133 }
134 
UnloadDynamicPlugin(void * handle)135 static void UnloadDynamicPlugin(void *handle)
136 {
137     const CredPlugin *credPlugin = GetCredPluginFromLib(handle);
138     const AuthModuleBase *authModulePlugin = GetAuthModulePluginFromLib(handle);
139     if (credPlugin != NULL) {
140         DelCredPlugin(credPlugin->pluginName);
141     }
142     if (authModulePlugin != NULL) {
143         DelAuthModulePlugin(authModulePlugin->moduleType);
144     }
145 }
146 
LoadExtendPlugin(void)147 void LoadExtendPlugin(void)
148 {
149     if (g_handle != NULL) {
150         LOGE("[Plugin]: The plugin has been loaded.");
151         return;
152     }
153 #ifdef DEVAUTH_ENABLE_RUN_ON_DEMAND_QOS
154     SetThreadPrio(OPEN_SO_PRIO);
155 #endif
156     g_handle = DevAuthDlopen(LIBDEVICE_AUTH_EXT);
157 #ifdef DEVAUTH_ENABLE_RUN_ON_DEMAND_QOS
158     SetThreadPrio(NORMAL_PRIO);
159 #endif
160     if (g_handle == NULL) {
161         LOGI("[Plugin]: There are no plugin that need to be loaded.");
162         return;
163     }
164     LOGI("[Plugin]: Open lib32 dynamic plugin success.");
165     if (LoadDynamicPlugin(g_handle) != HC_SUCCESS) {
166         DevAuthDlclose(g_handle);
167         g_handle = NULL;
168     }
169 }
170 
UnloadExtendPlugin(void)171 void UnloadExtendPlugin(void)
172 {
173     if (g_handle == NULL) {
174         LOGE("[Plugin]: The plugin has not been loaded.");
175         return;
176     }
177     DestroyExt(&g_pluginFunc);
178     UnloadDynamicPlugin(g_handle);
179     DevAuthDlclose(g_handle);
180     g_handle = NULL;
181     LOGI("[Plugin]: unload extend plugin success.");
182 }