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 "data_collect_manager_stub.h"
17
18 #include "string_ex.h"
19
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22
23 namespace OHOS::Security::SecurityGuard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)24 int32_t DataCollectManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
25 MessageParcel &reply, MessageOption &option)
26 {
27 SGLOGD("%{public}s", __func__);
28 do {
29 if (IDataCollectManager::GetDescriptor() != data.ReadInterfaceToken()) {
30 SGLOGE("descriptor error, %{public}s", Str16ToStr8(data.ReadInterfaceToken()).c_str());
31 break;
32 }
33
34 switch (code) {
35 case CMD_DATA_COLLECT: {
36 return HandleDataCollectCmd(data, reply);
37 }
38 case CMD_DATA_REQUEST: {
39 return HandleDataRequestCmd(data, reply);
40 }
41 case CMD_DATA_SUBSCRIBE: {
42 return HandleDataSubscribeCmd(data, reply);
43 }
44 case CMD_DATA_UNSUBSCRIBE: {
45 return HandleDataUnsubscribeCmd(data, reply);
46 }
47 case CMD_SECURITY_EVENT_QUERY: {
48 return HandleSecurityEventQueryCmd(data, reply);
49 }
50 case CMD_SECURITY_COLLECTOR_START: {
51 return HandleStartCmd(data, reply);
52 }
53 case CMD_SECURITY_COLLECTOR_STOP: {
54 return HandleStopCmd(data, reply);
55 }
56 case CMD_SECURITY_CONFIG_UPDATE: {
57 return HandleConfigUpdateCmd(data, reply);
58 }
59 default: {
60 break;
61 }
62 }
63 } while (false);
64 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65 }
66
HandleStartCmd(MessageParcel & data,MessageParcel & reply)67 int32_t DataCollectManagerStub::HandleStartCmd(MessageParcel &data, MessageParcel &reply)
68 {
69 SGLOGI("in HandleStartCmd");
70 uint32_t expected = sizeof(uint64_t);
71 uint32_t actual = data.GetReadableBytes();
72 if (expected >= actual) {
73 SGLOGE("actual length error, value=%{public}u", actual);
74 return BAD_PARAM;
75 }
76
77 std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
78 data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
79 if (!info) {
80 SGLOGE("failed to read parcelable for subscribeInfo");
81 return BAD_PARAM;
82 }
83
84 auto callback = data.ReadRemoteObject();
85 if (callback == nullptr) {
86 SGLOGE("callback is nullptr");
87 return BAD_PARAM;
88 }
89 int32_t ret = CollectorStart(*info, callback);
90 reply.WriteInt32(ret);
91 return ret;
92 }
93
HandleStopCmd(MessageParcel & data,MessageParcel & reply)94 int32_t DataCollectManagerStub::HandleStopCmd(MessageParcel &data, MessageParcel &reply)
95 {
96 SGLOGI("%{public}s", __func__);
97 uint32_t expected = sizeof(uint64_t);
98 uint32_t actual = data.GetReadableBytes();
99 if (expected >= actual) {
100 SGLOGE("actual length error, value=%{public}u", actual);
101 return BAD_PARAM;
102 }
103
104 std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
105 data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
106 if (!info) {
107 SGLOGE("failed to read parcelable for subscribeInfo");
108 return BAD_PARAM;
109 }
110
111 auto callback = data.ReadRemoteObject();
112 if (callback == nullptr) {
113 SGLOGE("callback is nullptr");
114 return BAD_PARAM;
115 }
116 int32_t ret = CollectorStop(*info, callback);
117 reply.WriteInt32(ret);
118 return ret;
119 }
120
HandleDataCollectCmd(MessageParcel & data,MessageParcel & reply)121 int32_t DataCollectManagerStub::HandleDataCollectCmd(MessageParcel &data, MessageParcel &reply)
122 {
123 SGLOGD("%{public}s", __func__);
124 uint32_t expected = sizeof(int64_t);
125 uint32_t actual = data.GetReadableBytes();
126 if (expected >= actual) {
127 SGLOGE("actual length error, value=%{public}u", actual);
128 return BAD_PARAM;
129 }
130
131 int64_t eventId = data.ReadInt64();
132 std::string version = data.ReadString();
133 std::string time = data.ReadString();
134 std::string content = data.ReadString();
135 return RequestDataSubmit(eventId, version, time, content);
136 }
137
HandleDataRequestCmd(MessageParcel & data,MessageParcel & reply)138 int32_t DataCollectManagerStub::HandleDataRequestCmd(MessageParcel &data, MessageParcel &reply)
139 {
140 SGLOGD("%{public}s", __func__);
141 const uint32_t expected = 4;
142 uint32_t actual = data.GetReadableBytes();
143 if (expected >= actual) {
144 SGLOGE("actual length error, value=%{public}u", actual);
145 return BAD_PARAM;
146 }
147
148 std::string devId = data.ReadString();
149 std::string eventList = data.ReadString();
150 auto object = data.ReadRemoteObject();
151 if (object == nullptr) {
152 SGLOGE("object is nullptr");
153 return BAD_PARAM;
154 }
155 return RequestRiskData(devId, eventList, object);
156 }
157
HandleDataSubscribeCmd(MessageParcel & data,MessageParcel & reply)158 int32_t DataCollectManagerStub::HandleDataSubscribeCmd(MessageParcel &data, MessageParcel &reply)
159 {
160 SGLOGI("%{public}s", __func__);
161 uint32_t expected = sizeof(uint64_t);
162 uint32_t actual = data.GetReadableBytes();
163 if (expected >= actual) {
164 SGLOGE("actual length error, value=%{public}u", actual);
165 return BAD_PARAM;
166 }
167
168 std::unique_ptr<SecurityCollector::SecurityCollectorSubscribeInfo> info(
169 data.ReadParcelable<SecurityCollector::SecurityCollectorSubscribeInfo>());
170 if (!info) {
171 SGLOGE("failed to read parcelable for subscribeInfo");
172 return BAD_PARAM;
173 }
174
175 auto callback = data.ReadRemoteObject();
176 if (callback == nullptr) {
177 SGLOGE("callback is nullptr");
178 return BAD_PARAM;
179 }
180 int32_t ret = Subscribe(*info, callback);
181 reply.WriteInt32(ret);
182 return ret;
183 }
184
HandleDataUnsubscribeCmd(MessageParcel & data,MessageParcel & reply)185 int32_t DataCollectManagerStub::HandleDataUnsubscribeCmd(MessageParcel &data, MessageParcel &reply)
186 {
187 SGLOGI("%{public}s", __func__);
188 uint32_t expected = sizeof(uint64_t);
189 uint32_t actual = data.GetReadableBytes();
190 if (expected >= actual) {
191 SGLOGE("actual length error, value=%{public}u", actual);
192 return BAD_PARAM;
193 }
194 auto callback = data.ReadRemoteObject();
195 if (callback == nullptr) {
196 SGLOGE("callback is nullptr");
197 return BAD_PARAM;
198 }
199
200 int32_t ret = Unsubscribe(callback);
201 reply.WriteInt32(ret);
202 return ret;
203 }
204
HandleSecurityEventQueryCmd(MessageParcel & data,MessageParcel & reply)205 int32_t DataCollectManagerStub::HandleSecurityEventQueryCmd(MessageParcel &data, MessageParcel &reply)
206 {
207 SGLOGI("%{public}s", __func__);
208 uint32_t expected = sizeof(uint32_t);
209 uint32_t actual = data.GetReadableBytes();
210 if (expected >= actual) {
211 SGLOGE("actual length error, value=%{public}u", actual);
212 return BAD_PARAM;
213 }
214
215 uint32_t size = 0;
216 if (!data.ReadUint32(size)) {
217 SGLOGE("failed to get the event size");
218 return BAD_PARAM;
219 }
220
221 if (size > MAX_QUERY_EVENT_SIZE) {
222 SGLOGE("the event size error");
223 return BAD_PARAM;
224 }
225 std::vector<SecurityCollector::SecurityEventRuler> rulers;
226 for (uint32_t index = 0; index < size; index++) {
227 std::shared_ptr<SecurityCollector::SecurityEventRuler> event(
228 data.ReadParcelable<SecurityCollector::SecurityEventRuler>());
229 if (event == nullptr) {
230 SGLOGE("failed read security event");
231 return BAD_PARAM;
232 }
233 rulers.emplace_back(*event);
234 }
235
236 auto callback = data.ReadRemoteObject();
237 if (callback == nullptr) {
238 SGLOGE("callback is nullptr");
239 return BAD_PARAM;
240 }
241
242 int32_t ret = QuerySecurityEvent(rulers, callback);
243 reply.WriteInt32(ret);
244 return ret;
245 }
246
HandleConfigUpdateCmd(MessageParcel & data,MessageParcel & reply)247 int32_t DataCollectManagerStub::HandleConfigUpdateCmd(MessageParcel &data, MessageParcel &reply)
248 {
249 SGLOGI("%{public}s", __func__);
250 uint32_t expected = sizeof(uint64_t);
251 uint32_t actual = data.GetReadableBytes();
252 if (expected >= actual) {
253 SGLOGE("actual length error, value=%{public}u", actual);
254 return BAD_PARAM;
255 }
256
257 std::string fileName = data.ReadString();
258 if (fileName.empty()) {
259 SGLOGE("failed to read fileName for config update");
260 return BAD_PARAM;
261 }
262 int32_t fd = data.ReadFileDescriptor();
263 if (fd < 0) {
264 SGLOGE("failed to read file fd for config update");
265 return BAD_PARAM;
266 }
267 SecurityGuard::SecurityConfigUpdateInfo info(fd, fileName);
268 int32_t ret = ConfigUpdate(info);
269 reply.WriteInt32(ret);
270 return ret;
271 }
272 }