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
16 #include "risk_analysis_manager_fuzzer.h"
17
18 #include <string>
19
20 #include "risk_analysis_manager_callback_service.h"
21 #include "risk_analysis_manager_service.h"
22 #include "security_guard_log.h"
23
24
25 namespace OHOS::Security::SecurityGuard {
26 RiskAnalysisManagerService g_service(RISK_ANALYSIS_MANAGER_SA_ID, true);
27 constexpr int32_t REMAINDER_VALUE = 2;
28
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)29 void OnRemoteRequestFuzzTest(const uint8_t* data, size_t size)
30 {
31 if (data == nullptr || size < sizeof(uint32_t)) {
32 return;
33 }
34 size_t offset = 0;
35 uint32_t modelId = *(reinterpret_cast<const uint32_t *>(data + offset));
36 offset += sizeof(uint32_t);
37 MessageParcel datas;
38 MessageParcel reply;
39 MessageOption option;
40 datas.WriteInterfaceToken(IRiskAnalysisManager::GetDescriptor());
41 if (size % REMAINDER_VALUE == 0) {
42 // handle get security model result cmd
43 std::string deviceId(reinterpret_cast<const char *>(data + offset), size - offset);
44 datas.WriteUint32(modelId);
45 ResultCallback func = [] (const std::string &devId, uint32_t modelId, const std::string &result) -> int32_t {
46 SGLOGI("RiskAnalysisManagerCallbackService called");
47 return 0;
48 };
49 sptr<IRemoteObject> callback = new (std::nothrow) RiskAnalysisManagerCallbackService(func);
50 datas.WriteRemoteObject(callback);
51 g_service.OnRemoteRequest(RiskAnalysisManagerStub::CMD_GET_SECURITY_MODEL_RESULT, datas, reply, option);
52 return;
53 }
54 // handle set model state cmd
55 datas.WriteUint32(modelId);
56 datas.WriteBool(size % REMAINDER_VALUE == 0);
57 g_service.OnRemoteRequest(RiskAnalysisManagerStub::CMD_SET_MODEL_STATE, datas, reply, option);
58 }
59 } // namespace OHOS::Security::SecurityGuard
60
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64 /* Run your code on date */
65 OHOS::Security::SecurityGuard::OnRemoteRequestFuzzTest(data, size);
66 return 0;
67 }
68