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 {
26 namespace {
ReportSecurityEvent(const std::shared_ptr<EventInfo> & info,bool isSync)27 int32_t ReportSecurityEvent(const std::shared_ptr<EventInfo> &info, bool isSync)
28 {
29 if (info == nullptr) {
30 return BAD_PARAM;
31 }
32 sptr<IDataCollectManager> proxy = SgCollectClient::GetInstance().GetProxy();
33 if (proxy == nullptr) {
34 return NULL_OBJECT;
35 }
36
37 int64_t eventId = info->GetEventId();
38 std::string version = info->GetVersion();
39 std::string content = info->GetContent();
40 std::string date = SecurityGuardUtils::GetDate();
41 int32_t ret = proxy->RequestDataSubmit(eventId, version, date, content, isSync);
42 if (ret != SUCCESS) {
43 SGLOGE("RequestSecurityInfo error, ret=%{public}d", ret);
44 return ret;
45 }
46 return SUCCESS;
47 }
48 }
49
GetInstance()50 SgCollectClient &SgCollectClient::GetInstance()
51 {
52 static SgCollectClient instance;
53 return instance;
54 }
55
GetProxy()56 sptr<IDataCollectManager> SgCollectClient::GetProxy()
57 {
58 if (proxy_ == nullptr) {
59 InitProxy();
60 }
61 return proxy_;
62 }
63
InitProxy()64 void SgCollectClient::InitProxy()
65 {
66 std::lock_guard<std::mutex> lock(proxyMutex_);
67 if (proxy_ == nullptr) {
68 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
69 if (registry == nullptr) {
70 SGLOGE("registry is nullptr");
71 return;
72 }
73 auto object = registry->GetSystemAbility(DATA_COLLECT_MANAGER_SA_ID);
74 if (object == nullptr) {
75 SGLOGE("object is nullptr");
76 return;
77 }
78 proxy_ = iface_cast<IDataCollectManager>(object);
79 if (proxy_ == nullptr) {
80 SGLOGE("proxy is nullptr");
81 return;
82 }
83
84 deathRecipient_ = new (std::nothrow) SgCollectClientDeathRecipient();
85 if (deathRecipient_ != nullptr) {
86 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
87 }
88 }
89 }
90
ReleaseProxy()91 void SgCollectClient::ReleaseProxy()
92 {
93 std::lock_guard<std::mutex> lock(proxyMutex_);
94 if (proxy_ != nullptr && proxy_->AsObject() != nullptr) {
95 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
96 }
97 proxy_ = nullptr;
98 }
99
OnRemoteDied(const wptr<IRemoteObject> & remote)100 void SgCollectClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
101 {
102 SgCollectClient::GetInstance().ReleaseProxy();
103 }
104
ReportSecurityInfo(const std::shared_ptr<EventInfo> & info)105 int32_t NativeDataCollectKit::ReportSecurityInfo(const std::shared_ptr<EventInfo> &info)
106 {
107 if (info == nullptr) {
108 return BAD_PARAM;
109 }
110 sptr<IDataCollectManager> proxy = SgCollectClient::GetInstance().GetProxy();
111 if (proxy == nullptr) {
112 return NULL_OBJECT;
113 }
114 return ReportSecurityEvent(info, true);
115 }
116
ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> & info)117 int32_t NativeDataCollectKit::ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> &info)
118 {
119 return ReportSecurityEvent(info, false);
120 }
121 }
122
ReportSecurityInfoImpl(const struct EventInfoSt * info,bool isSync)123 static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info, bool isSync)
124 {
125 if (info == nullptr || info->contentLen >= CONTENT_MAX_LEN) {
126 return OHOS::Security::SecurityGuard::BAD_PARAM;
127 }
128 int64_t eventId = info->eventId;
129 std::string version = reinterpret_cast<const char *>(info->version);
130 uint8_t tmp[CONTENT_MAX_LEN] = {};
131 (void) memset_s(tmp, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
132 errno_t rc = memcpy_s(tmp, CONTENT_MAX_LEN, info->content, info->contentLen);
133 if (rc != EOK) {
134 return OHOS::Security::SecurityGuard::NULL_OBJECT;
135 }
136 std::string content(reinterpret_cast<const char *>(tmp));
137 auto eventInfo = std::make_shared<OHOS::Security::SecurityGuard::EventInfo>(eventId, version, content);
138 if (isSync) {
139 return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
140 } else {
141 return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfoAsync(eventInfo);
142 }
143 }
144
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148
ReportSecurityInfo(const struct EventInfoSt * info)149 int32_t ReportSecurityInfo(const struct EventInfoSt *info)
150 {
151 return ReportSecurityInfoImpl(info, true);
152 }
153
ReportSecurityInfoAsync(const struct EventInfoSt * info)154 int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info)
155 {
156 return ReportSecurityInfoImpl(info, false);
157 }
158
159 #ifdef __cplusplus
160 }
161 #endif