• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "iservice_registry.h"
17 #include "securec.h"
18 
19 #include "data_collect_manager_proxy.h"
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "security_guard_utils.h"
23 #include "sg_collect_client.h"
24 
25 namespace OHOS::Security::SecurityGuard {
GetInstance()26 SgCollectClient &SgCollectClient::GetInstance()
27 {
28     static SgCollectClient instance;
29     return instance;
30 }
31 
GetProxy()32 sptr<IDataCollectManager> SgCollectClient::GetProxy()
33 {
34     if (proxy_ == nullptr) {
35         InitProxy();
36     }
37     return proxy_;
38 }
39 
InitProxy()40 void SgCollectClient::InitProxy()
41 {
42     std::lock_guard<std::mutex> lock(proxyMutex_);
43     if (proxy_ == nullptr) {
44         auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45         if (registry == nullptr) {
46             SGLOGE("registry is nullptr");
47             return;
48         }
49         auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
50         if (object == nullptr) {
51             SGLOGE("object is nullptr");
52             return;
53         }
54         proxy_ = iface_cast<IDataCollectManager>(object);
55         if (proxy_ == nullptr) {
56             SGLOGE("proxy is nullptr");
57             return;
58         }
59 
60         deathRecipient_ = new (std::nothrow) SgCollectClientDeathRecipient();
61         if (deathRecipient_ != nullptr) {
62             proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
63         }
64     }
65 }
66 
ReleaseProxy()67 void SgCollectClient::ReleaseProxy()
68 {
69     std::lock_guard<std::mutex> lock(proxyMutex_);
70     if (proxy_ != nullptr && proxy_->AsObject() != nullptr) {
71         proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
72     }
73     proxy_ = nullptr;
74 }
75 
OnRemoteDied(const wptr<IRemoteObject> & remote)76 void SgCollectClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
77 {
78     SgCollectClient::GetInstance().ReleaseProxy();
79 }
80 
ReportSecurityInfo(const std::shared_ptr<EventInfo> & info)81 int32_t NativeDataCollectKit::ReportSecurityInfo(const std::shared_ptr<EventInfo> &info)
82 {
83     if (info == nullptr) {
84         return BAD_PARAM;
85     }
86     sptr<IDataCollectManager> proxy = SgCollectClient::GetInstance().GetProxy();
87     if (proxy == nullptr) {
88         return NULL_OBJECT;
89     }
90 
91     int64_t eventId = info->GetEventId();
92     std::string version = info->GetVersion();
93     std::string content = info->GetContent();
94     std::string date = SecurityGuardUtils::GetDate();
95     int32_t ret = proxy->RequestDataSubmit(eventId, version, date, content);
96     if (ret != SUCCESS) {
97         SGLOGE("RequestSecurityInfo error, ret=%{public}d", ret);
98         return ret;
99     }
100     return SUCCESS;
101 }
102 }
103 
ReportSecurityInfoImpl(const struct EventInfoSt * info)104 static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info)
105 {
106     if (info == nullptr || info->contentLen >= CONTENT_MAX_LEN) {
107         return OHOS::Security::SecurityGuard::BAD_PARAM;
108     }
109     int64_t eventId = info->eventId;
110     std::string version = reinterpret_cast<const char *>(info->version);
111     uint8_t tmp[CONTENT_MAX_LEN] = {};
112     (void) memset_s(tmp, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
113     errno_t rc = memcpy_s(tmp, CONTENT_MAX_LEN, info->content, info->contentLen);
114     if (rc != EOK) {
115         return OHOS::Security::SecurityGuard::NULL_OBJECT;
116     }
117     std::string content(reinterpret_cast<const char *>(tmp));
118     auto eventInfo = std::make_shared<OHOS::Security::SecurityGuard::EventInfo>(eventId, version, content);
119     return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
120 }
121 
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
ReportSecurityInfo(const struct EventInfoSt * info)126 int32_t ReportSecurityInfo(const struct EventInfoSt *info)
127 {
128     return ReportSecurityInfoImpl(info);
129 }
130 
131 #ifdef __cplusplus
132 }
133 #endif