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(
52 static_cast<uint32_t>(RiskAnalysisManagerIpcCode::COMMAND_REQUEST_SECURITY_MODEL_RESULT),
53 datas, reply, option);
54 return;
55 }
56 // handle set model state cmd
57 datas.WriteUint32(modelId);
58 datas.WriteBool(size % REMAINDER_VALUE == 0);
59 g_service.OnRemoteRequest(
60 static_cast<uint32_t>(RiskAnalysisManagerIpcCode::COMMAND_SET_MODEL_STATE), datas, reply, option);
61 }
62 } // namespace OHOS::Security::SecurityGuard
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67 /* Run your code on date */
68 OHOS::Security::SecurityGuard::OnRemoteRequestFuzzTest(data, size);
69 return 0;
70 }
71