• 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 #include "sec_comp_client.h"
16 
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "iservice_registry.h"
20 #include "sec_comp_click_event_parcel.h"
21 #include "sec_comp_load_callback.h"
22 #include "sec_comp_log.h"
23 #include "sec_comp_service_proxy.h"
24 #include "sys_binder.h"
25 #include "tokenid_kit.h"
26 #include <algorithm>
27 #include <chrono>
28 
29 namespace OHOS {
30 namespace Security {
31 namespace SecurityComponent {
32 namespace {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompClient"};
34 constexpr int32_t SA_ID_SECURITY_COMPONENT_SERVICE = 3506;
35 static std::mutex g_instanceMutex;
36 static constexpr int32_t SENDREQ_FAIL_ERR = 32;
37 static const std::vector<int32_t> RETRY_CODE_LIST = {
38     SC_SERVICE_ERROR_SERVICE_NOT_EXIST, BR_DEAD_REPLY, BR_FAILED_REPLY, SENDREQ_FAIL_ERR };
39 static constexpr int32_t SA_DIED_TIME_OUT = 500;
40 constexpr int32_t SA_LOAD_TIME_OUT = 3000;
41 }  // namespace
42 
GetInstance()43 SecCompClient& SecCompClient::GetInstance()
44 {
45     static SecCompClient* instance = nullptr;
46     if (instance == nullptr) {
47         std::lock_guard<std::mutex> lock(g_instanceMutex);
48         if (instance == nullptr) {
49             SecCompClient* tmp = new (std::nothrow)SecCompClient();
50             instance = std::move(tmp);
51         }
52     }
53     return *instance;
54 }
55 
SecCompClient()56 SecCompClient::SecCompClient()
57 {}
58 
~SecCompClient()59 SecCompClient::~SecCompClient()
60 {
61     if (proxy_ == nullptr) {
62         return;
63     }
64     auto remoteObj = proxy_->AsObject();
65     if ((remoteObj != nullptr) && (serviceDeathObserver_ != nullptr)) {
66         remoteObj->RemoveDeathRecipient(serviceDeathObserver_);
67     }
68 }
69 
RegisterWriteToRawdata(SecCompType type,const std::string & componentInfo,SecCompRawdata & rawData)70 int32_t SecCompClient::RegisterWriteToRawdata(SecCompType type, const std::string& componentInfo,
71     SecCompRawdata& rawData)
72 {
73     MessageParcel dataParcel;
74 
75     if (!dataParcel.WriteUint32(type)) {
76         SC_LOG_ERROR(LABEL, "Register write type failed.");
77         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
78     }
79 
80     if (!dataParcel.WriteString(componentInfo)) {
81         SC_LOG_ERROR(LABEL, "Register write componentInfo failed.");
82         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
83     }
84 
85     if (!SecCompEnhanceAdapter::EnhanceClientSerialize(dataParcel, rawData)) {
86         SC_LOG_ERROR(LABEL, "Register serialize session info failed.");
87         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
88     }
89     return SC_OK;
90 }
91 
TryRegisterSecurityComponent(SecCompType type,const std::string & componentInfo,int32_t & scId,sptr<ISecCompService> proxy)92 int32_t SecCompClient::TryRegisterSecurityComponent(SecCompType type, const std::string& componentInfo,
93     int32_t& scId, sptr<ISecCompService> proxy)
94 {
95     std::lock_guard<std::mutex> lock(useIPCMutex_);
96     SecCompRawdata rawData;
97     if (RegisterWriteToRawdata(type, componentInfo, rawData) != SC_OK) {
98         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
99     }
100 
101     SecCompRawdata rawReply;
102     int32_t res = proxy->RegisterSecurityComponent(rawData, rawReply);
103     MessageParcel deserializedReply;
104 
105     if (res != SC_OK) {
106         SC_LOG_ERROR(LABEL, "Register request failed, result: %{public}d.", res);
107         return res;
108     }
109 
110     if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(rawReply, deserializedReply)) {
111         SC_LOG_ERROR(LABEL, "Register deserialize session info failed.");
112         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
113     }
114 
115     int32_t serviceRes;
116     if (!deserializedReply.ReadInt32(serviceRes)) {
117         SC_LOG_ERROR(LABEL, "Register read serviceRes failed.");
118         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
119     }
120 
121     if (serviceRes != SC_OK) {
122         scId = INVALID_SC_ID;
123         return serviceRes;
124     }
125 
126     if (!deserializedReply.ReadInt32(scId)) {
127         SC_LOG_ERROR(LABEL, "Register read scId failed.");
128         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
129     }
130     return res;
131 }
132 
RegisterSecurityComponent(SecCompType type,const std::string & componentInfo,int32_t & scId)133 int32_t SecCompClient::RegisterSecurityComponent(SecCompType type,
134     const std::string& componentInfo, int32_t& scId)
135 {
136     auto proxy = GetProxy(true);
137     if (proxy == nullptr) {
138         SC_LOG_ERROR(LABEL, "Proxy is null.");
139         return SC_SERVICE_ERROR_VALUE_INVALID;
140     }
141 
142     auto res = TryRegisterSecurityComponent(type, componentInfo, scId, proxy);
143     if (std::find(RETRY_CODE_LIST.begin(), RETRY_CODE_LIST.end(), res) == RETRY_CODE_LIST.end()) {
144         return res;
145     }
146 
147     std::unique_lock<std::mutex> lock(secCompSaMutex_);
148     auto waitStatus = secCompSACon_.wait_for(lock, std::chrono::milliseconds(SA_DIED_TIME_OUT),
149         [this]() { return serviceAbilityNeedLoadFlag_; });
150     if (waitStatus) {
151         proxy = GetProxy(true);
152         if (proxy != nullptr) {
153             return TryRegisterSecurityComponent(type, componentInfo, scId, proxy);
154         }
155     }
156     return res;
157 }
158 
UpdateWriteToRawdata(int32_t scId,const std::string & componentInfo,SecCompRawdata & rawData)159 int32_t SecCompClient::UpdateWriteToRawdata(int32_t scId, const std::string& componentInfo, SecCompRawdata& rawData)
160 {
161     MessageParcel dataParcel;
162     if (!dataParcel.WriteInt32(scId)) {
163         SC_LOG_ERROR(LABEL, "Update write scId failed.");
164         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
165     }
166     if (!dataParcel.WriteString(componentInfo)) {
167         SC_LOG_ERROR(LABEL, "Update write componentInfo failed.");
168         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
169     }
170 
171     if (!SecCompEnhanceAdapter::EnhanceClientSerialize(dataParcel, rawData)) {
172         SC_LOG_ERROR(LABEL, "Update serialize session info failed.");
173         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
174     }
175     return SC_OK;
176 }
177 
UpdateSecurityComponent(int32_t scId,const std::string & componentInfo)178 int32_t SecCompClient::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo)
179 {
180     auto proxy = GetProxy(true);
181     if (proxy == nullptr) {
182         SC_LOG_ERROR(LABEL, "Proxy is null.");
183         return SC_SERVICE_ERROR_VALUE_INVALID;
184     }
185 
186     std::lock_guard<std::mutex> lock(useIPCMutex_);
187     SecCompRawdata rawData;
188     int32_t res = UpdateWriteToRawdata(scId, componentInfo, rawData);
189     if (res != SC_OK) {
190         return res;
191     }
192 
193     SecCompRawdata rawReply;
194     res = proxy->UpdateSecurityComponent(rawData, rawReply);
195 
196     MessageParcel deserializedReply;
197     if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(rawReply, deserializedReply)) {
198         SC_LOG_ERROR(LABEL, "Update deserialize session info failed.");
199         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
200     }
201 
202     if (res != SC_OK) {
203         SC_LOG_ERROR(LABEL, "Update request failed, result: %{public}d.", res);
204         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
205     }
206 
207     int32_t serviceRes;
208     if (!deserializedReply.ReadInt32(serviceRes)) {
209         SC_LOG_ERROR(LABEL, "Update read res failed.");
210         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
211     }
212 
213     return serviceRes;
214 }
215 
UnregisterWriteToRawdata(int32_t scId,SecCompRawdata & rawData)216 int32_t SecCompClient::UnregisterWriteToRawdata(int32_t scId, SecCompRawdata& rawData)
217 {
218     MessageParcel dataParcel;
219 
220     if (!dataParcel.WriteInt32(scId)) {
221         SC_LOG_ERROR(LABEL, "Unregister write scId failed.");
222         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
223     }
224 
225     if (!SecCompEnhanceAdapter::EnhanceClientSerialize(dataParcel, rawData)) {
226         SC_LOG_ERROR(LABEL, "Unregister serialize session info failed.");
227         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
228     }
229     return SC_OK;
230 }
231 
UnregisterSecurityComponent(int32_t scId)232 int32_t SecCompClient::UnregisterSecurityComponent(int32_t scId)
233 {
234     auto proxy = GetProxy(true);
235     if (proxy == nullptr) {
236         SC_LOG_ERROR(LABEL, "Proxy is null");
237         return SC_SERVICE_ERROR_VALUE_INVALID;
238     }
239 
240     std::lock_guard<std::mutex> lock(useIPCMutex_);
241     SecCompRawdata rawData;
242     int32_t res = UnregisterWriteToRawdata(scId, rawData);
243     if (res != SC_OK) {
244         return res;
245     }
246 
247     SecCompRawdata rawReply;
248     res = proxy->UnregisterSecurityComponent(rawData, rawReply);
249 
250     MessageParcel deserializedReply;
251     if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(rawReply, deserializedReply)) {
252         SC_LOG_ERROR(LABEL, "Unregister deserialize session info failed.");
253         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
254     }
255 
256     if (res != SC_OK) {
257         SC_LOG_ERROR(LABEL, "Unregister request failed, result: %{public}d.", res);
258         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
259     }
260 
261     int32_t serviceRes;
262     if (!deserializedReply.ReadInt32(serviceRes)) {
263         SC_LOG_ERROR(LABEL, "Unregister read res failed.");
264         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
265     }
266 
267     return serviceRes;
268 }
269 
ReportWriteToRawdata(SecCompInfo & secCompInfo,SecCompRawdata & rawData,std::string & message)270 int32_t SecCompClient::ReportWriteToRawdata(SecCompInfo& secCompInfo, SecCompRawdata& rawData, std::string& message)
271 {
272     MessageParcel dataParcel;
273 
274     if (!dataParcel.WriteInt32(secCompInfo.scId)) {
275         SC_LOG_ERROR(LABEL, "Report write scId failed.");
276         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
277     }
278 
279     if (!dataParcel.WriteString(secCompInfo.componentInfo)) {
280         SC_LOG_ERROR(LABEL, "Report write componentInfo failed.");
281         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
282     }
283 
284     if (!dataParcel.WriteString(message)) {
285         SC_LOG_ERROR(LABEL, "Report write message failed.");
286         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
287     }
288 
289     sptr<SecCompClickEventParcel> parcel = new (std::nothrow) SecCompClickEventParcel();
290     if (parcel == nullptr) {
291         SC_LOG_ERROR(LABEL, "Report new click event parcel failed.");
292         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
293     }
294     parcel->clickInfoParams_ = secCompInfo.clickInfo;
295     if (!dataParcel.WriteParcelable(parcel)) {
296         SC_LOG_ERROR(LABEL, "Report write clickInfo failed.");
297         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
298     }
299 
300     if (!SecCompEnhanceAdapter::EnhanceClientSerialize(dataParcel, rawData)) {
301         SC_LOG_ERROR(LABEL, "Unregister serialize session info failed.");
302         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
303     }
304     return SC_OK;
305 }
306 
ReportSecurityComponentClickEvent(SecCompInfo & secCompInfo,sptr<IRemoteObject> callerToken,sptr<IRemoteObject> dialogCallback,std::string & message)307 int32_t SecCompClient::ReportSecurityComponentClickEvent(SecCompInfo& secCompInfo,
308     sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback, std::string& message)
309 {
310     auto proxy = GetProxy(true);
311     if (proxy == nullptr) {
312         SC_LOG_ERROR(LABEL, "Proxy is null");
313         return SC_SERVICE_ERROR_VALUE_INVALID;
314     }
315 
316     std::lock_guard<std::mutex> lock(useIPCMutex_);
317     SecCompRawdata rawData;
318     int32_t res = ReportWriteToRawdata(secCompInfo, rawData, message);
319     if (res != SC_OK) {
320         return res;
321     }
322 
323     SecCompRawdata rawReply;
324     res = proxy->ReportSecurityComponentClickEvent(callerToken, dialogCallback, rawData, rawReply);
325     MessageParcel deserializedReply;
326     if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(rawReply, deserializedReply)) {
327         SC_LOG_ERROR(LABEL, "Report deserialize session info failed.");
328         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
329     }
330 
331     if (res != SC_OK) {
332         SC_LOG_ERROR(LABEL, "Report request failed, result: %{public}d.", res);
333         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
334     }
335 
336     int32_t serviceRes;
337     if (!deserializedReply.ReadInt32(serviceRes)) {
338         SC_LOG_ERROR(LABEL, "Report read res failed.");
339         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
340     }
341 
342     if (!deserializedReply.ReadString(message)) {
343         SC_LOG_ERROR(LABEL, "Report read error message failed.");
344         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
345     }
346     return serviceRes;
347 }
348 
VerifySavePermission(AccessToken::AccessTokenID tokenId)349 bool SecCompClient::VerifySavePermission(AccessToken::AccessTokenID tokenId)
350 {
351     auto proxy = GetProxy(false);
352     if (proxy == nullptr) {
353         SC_LOG_ERROR(LABEL, "Proxy is null");
354         return false;
355     }
356 
357     bool isGranted;
358     int32_t res = proxy->VerifySavePermission(tokenId, isGranted);
359     if (res != SC_OK) {
360         SC_LOG_ERROR(LABEL, "Verify save permission fail");
361         return false;
362     }
363     return isGranted;
364 }
365 
IsSystemAppCalling()366 bool SecCompClient::IsSystemAppCalling()
367 {
368     auto selfToken = IPCSkeleton::GetSelfTokenID();
369     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
370 }
371 
PreRegisterWriteToRawdata(SecCompRawdata & rawData)372 int32_t SecCompClient::PreRegisterWriteToRawdata(SecCompRawdata& rawData)
373 {
374     MessageParcel dataParcel;
375     if (!SecCompEnhanceAdapter::EnhanceClientSerialize(dataParcel, rawData)) {
376         SC_LOG_ERROR(LABEL, "PreRegister serialize session info failed.");
377         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
378     }
379 
380     return SC_OK;
381 }
382 
PreRegisterSecCompProcess()383 int32_t SecCompClient::PreRegisterSecCompProcess()
384 {
385     auto proxy = GetProxy(true);
386     if (proxy == nullptr) {
387         SC_LOG_ERROR(LABEL, "Proxy is null");
388         return SC_SERVICE_ERROR_VALUE_INVALID;
389     }
390     std::lock_guard<std::mutex> lock(useIPCMutex_);
391     SecCompRawdata rawData;
392     int32_t res = PreRegisterWriteToRawdata(rawData);
393     if (res != SC_OK) {
394         return res;
395     }
396 
397     SecCompRawdata rawReply;
398     res = proxy->PreRegisterSecCompProcess(rawData, rawReply);
399     MessageParcel deserializedReply;
400     if (!SecCompEnhanceAdapter::EnhanceClientDeserialize(rawReply, deserializedReply)) {
401         SC_LOG_ERROR(LABEL, "PreRegister deserialize session info failed.");
402         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
403     }
404 
405     if (res != SC_OK) {
406         SC_LOG_ERROR(LABEL, "PreRegister request failed, result: %{public}d.", res);
407         return res;
408     }
409 
410     int32_t serviceRes;
411     if (!deserializedReply.ReadInt32(serviceRes)) {
412         SC_LOG_ERROR(LABEL, "PreRegister read serviceRes failed.");
413         return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL;
414     }
415 
416     return serviceRes;
417 }
418 
IsServiceExist()419 bool SecCompClient::IsServiceExist()
420 {
421     return GetProxy(false) != nullptr;
422 }
423 
LoadService()424 bool SecCompClient::LoadService()
425 {
426     return GetProxy(true) != nullptr;
427 }
428 
StartLoadSecCompSa()429 bool SecCompClient::StartLoadSecCompSa()
430 {
431     {
432         std::unique_lock<std::mutex> lock(cvLock_);
433         readyFlag_ = false;
434     }
435     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
436     if (sam == nullptr) {
437         SC_LOG_ERROR(LABEL, "GetSystemAbilityManager return null");
438         return false;
439     }
440     sptr<SecCompLoadCallback> ptrSecCompLoadCallback =
441         new (std::nothrow) SecCompLoadCallback();
442     if (ptrSecCompLoadCallback == nullptr) {
443         SC_LOG_ERROR(LABEL, "New ptrSecCompLoadCallback fail.");
444         return false;
445     }
446 
447     int32_t result = sam->LoadSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE,
448         ptrSecCompLoadCallback);
449     if (result != SC_OK) {
450         SC_LOG_ERROR(LABEL, "LoadSystemAbility %{public}d failed", SA_ID_SECURITY_COMPONENT_SERVICE);
451         return false;
452     }
453     SC_LOG_INFO(LABEL, "Notify samgr load sa %{public}d, waiting for service start", SA_ID_SECURITY_COMPONENT_SERVICE);
454     return true;
455 }
456 
TryToGetSecCompSa()457 bool SecCompClient::TryToGetSecCompSa()
458 {
459     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
460     if (sam == nullptr) {
461         SC_LOG_ERROR(LABEL, "GetSystemAbilityManager return null");
462         return false;
463     }
464 
465     auto secCompSa = sam->CheckSystemAbility(SA_ID_SECURITY_COMPONENT_SERVICE);
466     if (secCompSa == nullptr) {
467         SC_LOG_INFO(LABEL, "Service is not start.");
468         return false;
469     }
470     GetProxyFromRemoteObject(secCompSa);
471     return true;
472 }
473 
WaitForSecCompSa()474 void SecCompClient::WaitForSecCompSa()
475 {
476     // wait_for release lock and block until time out(1s) or match the condition with notice
477     std::unique_lock<std::mutex> lock(cvLock_);
478     auto waitStatus = secComCon_.wait_for(
479         lock, std::chrono::milliseconds(SA_LOAD_TIME_OUT), [this]() { return readyFlag_; });
480     if (!waitStatus) {
481         // time out or loadcallback fail
482         SC_LOG_ERROR(LABEL, "security component load sa timeout");
483         return;
484     }
485 }
486 
FinishStartSASuccess(const sptr<IRemoteObject> & remoteObject)487 void SecCompClient::FinishStartSASuccess(const sptr<IRemoteObject>& remoteObject)
488 {
489     GetProxyFromRemoteObject(remoteObject);
490     // get lock which wait_for release and send a notice so that wait_for can out of block
491     {
492         std::unique_lock<std::mutex> lock(cvLock_);
493         readyFlag_ = true;
494         secComCon_.notify_one();
495     }
496     {
497         std::unique_lock<std::mutex> lock(secCompSaMutex_);
498         serviceAbilityNeedLoadFlag_ = false;
499     }
500 }
501 
FinishStartSAFail()502 void SecCompClient::FinishStartSAFail()
503 {
504     SC_LOG_ERROR(LABEL, "get security component sa failed.");
505     // get lock which wait_for release and send a notice
506     std::unique_lock<std::mutex> lock(cvLock_);
507     readyFlag_ = false;
508     secComCon_.notify_one();
509 }
510 
LoadSecCompSa()511 void SecCompClient::LoadSecCompSa()
512 {
513     if (!StartLoadSecCompSa()) {
514         return;
515     }
516     WaitForSecCompSa();
517 }
518 
OnRemoteDiedHandle()519 void SecCompClient::OnRemoteDiedHandle()
520 {
521     SC_LOG_ERROR(LABEL, "Remote service died");
522     std::unique_lock<std::mutex> lock(proxyMutex_);
523     if (proxy_ != nullptr) {
524         auto remoteObj = proxy_->AsObject();
525         if ((remoteObj != nullptr) && (serviceDeathObserver_ != nullptr)) {
526             remoteObj->RemoveDeathRecipient(serviceDeathObserver_);
527         }
528     }
529     proxy_ = nullptr;
530     serviceDeathObserver_ = nullptr;
531     {
532         std::unique_lock<std::mutex> lock1(cvLock_);
533         readyFlag_ = false;
534     }
535     {
536         std::unique_lock<std::mutex> lock1(secCompSaMutex_);
537         serviceAbilityNeedLoadFlag_ = true;
538         secCompSACon_.notify_one();
539     }
540 }
541 
GetProxyFromRemoteObject(const sptr<IRemoteObject> & remoteObject)542 void SecCompClient::GetProxyFromRemoteObject(const sptr<IRemoteObject>& remoteObject)
543 {
544     if (remoteObject == nullptr) {
545         return;
546     }
547 
548     sptr<SecCompDeathRecipient> serviceDeathObserver = new (std::nothrow) SecCompDeathRecipient();
549     if (serviceDeathObserver == nullptr) {
550         SC_LOG_ERROR(LABEL, "Alloc service death observer fail");
551         return;
552     }
553 
554     if (!remoteObject->AddDeathRecipient(serviceDeathObserver)) {
555         SC_LOG_ERROR(LABEL, "Add service death observer fail");
556         return;
557     }
558 
559     auto proxy = iface_cast<ISecCompService>(remoteObject);
560     if (proxy == nullptr) {
561         SC_LOG_ERROR(LABEL, "iface_cast get null");
562         return;
563     }
564     proxy_ = proxy;
565     serviceDeathObserver_ = serviceDeathObserver;
566     SC_LOG_INFO(LABEL, "GetSystemAbility %{public}d success", SA_ID_SECURITY_COMPONENT_SERVICE);
567     return;
568 }
569 
GetProxy(bool doLoadSa)570 sptr<ISecCompService> SecCompClient::GetProxy(bool doLoadSa)
571 {
572     std::unique_lock<std::mutex> lock(proxyMutex_);
573     if (proxy_ != nullptr) {
574         return proxy_;
575     }
576     if (TryToGetSecCompSa() || !doLoadSa) {
577         return proxy_;
578     }
579 
580     LoadSecCompSa();
581     return proxy_;
582 }
583 }  // namespace SecurityComponent
584 }  // namespace Security
585 }  // namespace OHOS
586