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 "risk_collect_fuzzer.h"
17
18 #include <string>
19
20 #include "securec.h"
21
22 #include "security_guard_define.h"
23 #include "security_guard_log.h"
24 #include "security_guard_utils.h"
25 #define private public
26 #define protected public
27 #include "config_data_manager.h"
28 #include "database_manager.h"
29 #include "uevent_notify.h"
30 #include "data_format.h"
31 #include "kernel_interface_adapter.h"
32 #undef private
33 #undef protected
34
35 using namespace OHOS::Security::SecurityGuard;
36
37 namespace OHOS {
DataFormatFuzzTest()38 void DataFormatFuzzTest()
39 {
40 std::string test = "test";
41 DataFormat::CheckRiskContent(test);
42 uint32_t oversize = 1000;
43 std::string oversizeString(oversize, 'c');
44 DataFormat::CheckRiskContent(oversizeString);
45
46 RequestCondition reqCondition;
47 std::string condition1 = "{\"eventId\":0}";
48 std::string condition2 = "{\"eventId\":[\"t\", \"e\", \"s\", \"t\"]}";
49 std::string condition3 = "{\"eventId\":[1, 2, 3, 4]}";
50 std::string condition4 = "{\"beginTime\":1}";
51 std::string condition5 = "{\"beginTime\":\"0001\"}";
52 std::string condition6 = "{\"endTime\":1}";
53 std::string condition7 = "{\"endTime\":\"0001\"}";
54 DataFormat::ParseConditions(test, reqCondition);
55 DataFormat::ParseConditions(condition1, reqCondition);
56 DataFormat::ParseConditions(condition2, reqCondition);
57 DataFormat::ParseConditions(condition3, reqCondition);
58 DataFormat::ParseConditions(condition4, reqCondition);
59 DataFormat::ParseConditions(condition5, reqCondition);
60 DataFormat::ParseConditions(condition6, reqCondition);
61 DataFormat::ParseConditions(condition7, reqCondition);
62 }
63
KernelInterfaceAdapterFuzzTest()64 void KernelInterfaceAdapterFuzzTest()
65 {
66 KernelInterfaceAdapter adapter;
67 adapter.Socket(0, 0, 0);
68
69 struct sockaddr addr = {};
70 adapter.Bind(0, nullptr, 0);
71 adapter.Bind(0, &addr, sizeof(addr));
72
73 struct pollfd fds = {};
74 adapter.Poll(&fds, 0, 0);
75 adapter.Poll(nullptr, 0, 0);
76
77 char buffer[1] = {};
78 adapter.Recv(0, buffer, sizeof(buffer), 0);
79 adapter.Recv(0, nullptr, 0, 0);
80
81 const char* pathName = "test";
82 adapter.Open(pathName, 0);
83 const char* pathName2 = "/proc/kernel_sg";
84 adapter.Open(pathName2, 0);
85
86 char buffer2[1] = {};
87 adapter.Write(0, buffer2, sizeof(buffer2));
88 adapter.Write(0, nullptr, 0);
89 }
90 } // namespace OHOS
91
LLVMFuzzerInitialize(int * argc,char *** argv)92 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
93 {
94 OHOS::DataFormatFuzzTest();
95 OHOS::KernelInterfaceAdapterFuzzTest();
96 return 0;
97 }
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102 /* Run your code on date */
103 return 0;
104 }
105