1 /*
2 * Copyright (c) 2024 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 "netfirewall_client_test.h"
17
18 #include "http_proxy.h"
19 #include "inet_addr.h"
20 #include "net_manager_constants.h"
21 #include "netmanager_ext_test_security.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "refbase.h"
24 #include "singleton.h"
25
26 #define private public
27 #define protected public
28
29 #include "netfirewall_client.h"
30 #include "netfirewall_proxy.h"
31 #include "netfirewall_common.h"
32 #include "netfirewall_service.h"
33 #include "i_netfirewall_service.h"
34
35 namespace OHOS {
36 namespace NetManagerStandard {
37 namespace {
38 const uint8_t *g_baseFuzzData = nullptr;
39 size_t g_baseFuzzSize = 0;
40 size_t g_baseFuzzPos;
41 constexpr size_t IFACE_LEN = 5;
42 }
43
GetData()44 template <class T> T GetData()
45 {
46 T object {};
47 size_t objectSize = sizeof(object);
48 if (g_baseFuzzData == nullptr || objectSize > g_baseFuzzSize - g_baseFuzzPos) {
49 return object;
50 }
51 errno_t ret = memcpy_s(&object, objectSize, g_baseFuzzData + g_baseFuzzPos, objectSize);
52 if (ret != EOK) {
53 return {};
54 }
55 g_baseFuzzPos += objectSize;
56 return object;
57 }
58
GetStringFromData(int strlen)59 std::string GetStringFromData(int strlen)
60 {
61 char cstr[strlen];
62 cstr[strlen - 1] = '\0';
63 for (int i = 0; i < strlen - 1; i++) {
64 cstr[i] = GetData<char>();
65 }
66 std::string str(cstr);
67 return str;
68 }
69
WriteInterfaceToken(MessageParcel & data)70 bool WriteInterfaceToken(MessageParcel &data)
71 {
72 if (!data.WriteInterfaceToken(NetFirewallStub::GetDescriptor())) {
73 return false;
74 }
75 return true;
76 }
77
IsDataAndWriteVaild(const uint8_t * data,size_t size,MessageParcel & parcel)78 bool IsDataAndWriteVaild(const uint8_t *data, size_t size, MessageParcel &parcel)
79 {
80 if ((data == nullptr) || (size == 0)) {
81 return false;
82 }
83
84 g_baseFuzzData = data;
85 g_baseFuzzSize = size;
86 g_baseFuzzPos = 0;
87
88 std::string iface = GetStringFromData(IFACE_LEN);
89 WriteInterfaceToken(parcel);
90 if (!parcel.WriteString(iface)) {
91 return false;
92 }
93
94 return true;
95 }
96
OnRemoteRequest(uint32_t code,MessageParcel & data)97 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data)
98 {
99 MessageParcel reply;
100 MessageOption option;
101 return DelayedSingleton<NetFirewallService>::GetInstance()->OnRemoteRequest(code, data, reply, option);
102 }
103
SetNetFirewallPolicyTest(const uint8_t * data,size_t size)104 void SetNetFirewallPolicyTest(const uint8_t *data, size_t size)
105 {
106 MessageParcel parcel;
107 if (!IsDataAndWriteVaild(data, size, parcel)) {
108 return;
109 }
110 auto status = std::make_unique<NetFirewallPolicy>();
111 if (!status->Marshalling(parcel)) {
112 return;
113 }
114 int32_t userId = GetData<int32_t>();
115 parcel.WriteInt32(userId);
116 OnRemoteRequest(static_cast<uint32_t>(INetFirewallService::SET_NET_FIREWALL_STATUS), parcel);
117 }
118 } // namespace NetManagerStandard
119 } // namespace OHOS
120
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
122 {
123 OHOS::NetManagerStandard::SetNetFirewallPolicyTest(data, size);
124 return 0;
125 }
126