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 #include "sec_comp_enhance_adapter.h"
16
17 #include <dlfcn.h>
18 #include "sec_comp_err.h"
19 #include "sec_comp_log.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace SecurityComponent {
24 namespace {
25 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
26 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompEnhanceAdapter"};
27
28 #ifdef _ARM64_
29 static const std::string LIB_PATH = "/system/lib64/";
30 #else
31 static const std::string LIB_PATH = "/system/lib/";
32 #endif
33 static const std::string ENHANCE_INPUT_INTERFACE_LIB = LIB_PATH + "libsec_comp_input_enhance.z.so";
34 static const std::string ENHANCE_SRV_INTERFACE_LIB = LIB_PATH + "libsec_comp_service_enhance.z.so";
35 static const std::string ENHANCE_CLIENT_INTERFACE_LIB = LIB_PATH + "libsec_comp_client_enhance.z.so";
36 }
37
38 SecCompInputEnhanceInterface* SecCompEnhanceAdapter::inputHandler = nullptr;
39 bool SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false;
40
41 SecCompSrvEnhanceInterface* SecCompEnhanceAdapter::srvHandler = nullptr;
42 bool SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false;
43
44 SecCompClientEnhanceInterface* SecCompEnhanceAdapter::clientHandler = nullptr;
45 bool SecCompEnhanceAdapter::isEnhanceClientHandlerInit = false;
46
47 std::mutex SecCompEnhanceAdapter::initMtx;
48
InitEnhanceHandler(EnhanceInterfaceType type)49 void SecCompEnhanceAdapter::InitEnhanceHandler(EnhanceInterfaceType type)
50 {
51 std::unique_lock<std::mutex> lck(initMtx);
52 std::string libPath = "";
53 switch (type) {
54 case SEC_COMP_ENHANCE_INPUT_INTERFACE:
55 libPath = ENHANCE_INPUT_INTERFACE_LIB;
56 isEnhanceInputHandlerInit = true;
57 break;
58 case SEC_COMP_ENHANCE_SRV_INTERFACE:
59 libPath = ENHANCE_SRV_INTERFACE_LIB;
60 isEnhanceSrvHandlerInit = true;
61 break;
62 case SEC_COMP_ENHANCE_CLIENT_INTERFACE:
63 libPath = ENHANCE_CLIENT_INTERFACE_LIB;
64 isEnhanceClientHandlerInit = true;
65 break;
66 default:
67 break;
68 }
69 if (dlopen(libPath.c_str(), RTLD_LAZY) == nullptr) {
70 SC_LOG_ERROR(LABEL, "init enhance lib %{public}s failed, error %{public}s", libPath.c_str(), dlerror());
71 }
72 }
73
SetEnhanceCfg(uint8_t * cfg,uint32_t cfgLen)74 int32_t SecCompEnhanceAdapter::SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen)
75 {
76 if (!isEnhanceInputHandlerInit) {
77 InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE);
78 }
79 if (inputHandler != nullptr) {
80 return inputHandler->SetEnhanceCfg(cfg, cfgLen);
81 }
82 return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
83 }
84
GetPointerEventEnhanceData(void * data,uint32_t dataLen,uint8_t * enhanceData,uint32_t & enHancedataLen)85 int32_t SecCompEnhanceAdapter::GetPointerEventEnhanceData(void* data, uint32_t dataLen,
86 uint8_t* enhanceData, uint32_t& enHancedataLen)
87 {
88 if (!isEnhanceInputHandlerInit) {
89 InitEnhanceHandler(SEC_COMP_ENHANCE_INPUT_INTERFACE);
90 }
91 if (inputHandler != nullptr) {
92 return inputHandler->GetPointerEventEnhanceData(data, dataLen, enhanceData, enHancedataLen);
93 }
94 return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
95 }
96
CheckExtraInfo(const SecCompClickEvent & touchInfo)97 int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& touchInfo)
98 {
99 if (!isEnhanceSrvHandlerInit) {
100 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
101 }
102 if (srvHandler != nullptr) {
103 return srvHandler->CheckExtraInfo(touchInfo);
104 }
105 return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
106 }
107
EnhanceDataPreprocess(std::string & componentInfo)108 bool SecCompEnhanceAdapter::EnhanceDataPreprocess(std::string& componentInfo)
109 {
110 if (!isEnhanceClientHandlerInit) {
111 InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE);
112 }
113
114 uintptr_t enhanceCallerAddr = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
115 if (clientHandler != nullptr) {
116 return clientHandler->EnhanceDataPreprocess(enhanceCallerAddr, componentInfo);
117 }
118 return true;
119 }
120
EnhanceDataPreprocess(int32_t scId,std::string & componentInfo)121 bool SecCompEnhanceAdapter::EnhanceDataPreprocess(int32_t scId, std::string& componentInfo)
122 {
123 if (!isEnhanceClientHandlerInit) {
124 InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE);
125 }
126
127 uintptr_t enhanceCallerAddr = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
128 if (clientHandler != nullptr) {
129 return clientHandler->EnhanceDataPreprocess(enhanceCallerAddr, scId, componentInfo);
130 }
131 return true;
132 }
133
RegisterScIdEnhance(int32_t scId)134 void SecCompEnhanceAdapter::RegisterScIdEnhance(int32_t scId)
135 {
136 if (!isEnhanceClientHandlerInit) {
137 InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE);
138 }
139
140 uintptr_t enhanceCallerAddr = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
141 if (clientHandler != nullptr) {
142 clientHandler->RegisterScIdEnhance(enhanceCallerAddr, scId);
143 }
144 }
145
UnregisterScIdEnhance(int32_t scId)146 void SecCompEnhanceAdapter::UnregisterScIdEnhance(int32_t scId)
147 {
148 if (!isEnhanceClientHandlerInit) {
149 InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE);
150 }
151
152 uintptr_t enhanceCallerAddr = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
153 if (clientHandler != nullptr) {
154 clientHandler->UnregisterScIdEnhance(enhanceCallerAddr, scId);
155 }
156 }
157
EnableInputEnhance()158 int32_t SecCompEnhanceAdapter::EnableInputEnhance()
159 {
160 if (!isEnhanceSrvHandlerInit) {
161 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
162 }
163 if (srvHandler != nullptr) {
164 return srvHandler->EnableInputEnhance();
165 }
166 return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
167 }
168
DisableInputEnhance()169 int32_t SecCompEnhanceAdapter::DisableInputEnhance()
170 {
171 if (!isEnhanceSrvHandlerInit) {
172 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
173 }
174 if (srvHandler != nullptr) {
175 return srvHandler->DisableInputEnhance();
176 }
177 return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE;
178 }
179
StartEnhanceService()180 void SecCompEnhanceAdapter::StartEnhanceService()
181 {
182 if (!isEnhanceSrvHandlerInit) {
183 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
184 }
185 if (srvHandler != nullptr) {
186 srvHandler->StartEnhanceService();
187 }
188 }
189
ExistEnhanceService()190 void SecCompEnhanceAdapter::ExistEnhanceService()
191 {
192 if (!isEnhanceSrvHandlerInit) {
193 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
194 }
195 if (srvHandler != nullptr) {
196 srvHandler->ExitEnhanceService();
197 }
198 }
199
NotifyProcessDied(int32_t pid)200 void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid)
201 {
202 if (!isEnhanceSrvHandlerInit) {
203 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
204 }
205 if (srvHandler != nullptr) {
206 srvHandler->NotifyProcessDied(pid);
207 }
208 }
209
CheckComponentInfoEnhnace(int32_t pid,std::shared_ptr<SecCompBase> & compInfo,const nlohmann::json & jsonComponent)210 int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid,
211 std::shared_ptr<SecCompBase>& compInfo, const nlohmann::json& jsonComponent)
212 {
213 if (!isEnhanceSrvHandlerInit) {
214 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
215 }
216 if (srvHandler != nullptr) {
217 return srvHandler->CheckComponentInfoEnhnace(pid, compInfo, jsonComponent);
218 }
219 return SC_OK;
220 }
221
GetEnhanceRemoteObject()222 sptr<IRemoteObject> SecCompEnhanceAdapter::GetEnhanceRemoteObject()
223 {
224 if (!isEnhanceSrvHandlerInit) {
225 InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE);
226 }
227 if (srvHandler != nullptr) {
228 auto service = srvHandler->GetEnhanceRemoteObject();
229 return service;
230 }
231 return nullptr;
232 }
233 } // namespace SecurityComponent
234 } // namespace Security
235 } // namespace OHOS
236